This package contains quite involved examples that demonstrate how to use the functions of package Modelica.Utilities. In particular the following examples are present.
Name | Description |
---|---|
calculator | Interpreter to evaluate simple expressions consisting of +,-,*,/,(),sin(), cos(), tan(), sqrt(), pi |
expression | Expression interpreter that returns with the position after the expression (expression may consist of +,-,*,/,(),sin(), cos(), tan(), sqrt(), pi |
readRealParameter | Read the value of a Real parameter from file |
readRealParameterModel | Demonstrate usage of Examples.readRealParameter/.expression |
result = calculator(expression);
This function demonstrates how a simple expression calculator can be implemented in form of a recursive decent parser using basically the Strings.scanToken(..) and Strings.scanDelimiter(..) function.
The following operations are supported (pi=3.14.. is a predefined constant):
+, - *, / (expression) sin(expression) cos(expression) tan(expression) sqrt(expression) pi
Extends from Modelica.Icons.Function (Icon for functions).calculator("2+3*(4-1)"); // returns 11 calculator("sin(pi/6)"); // returns 0.5
Name | Description |
---|---|
string | Expression that is evaluated |
Name | Description |
---|---|
result | Value of expression |
result = expression(string); (result, nextIndex) = expression(string, startIndex=1, message="");
This function is nearly the same as Examples.calculator. The essential difference is that function "expression" might be used in other parsing operations: After the expression is parsed and evaluated, the function returns with the value of the expression as well as the position of the character directly after the expression.
This function demonstrates how a simple expression calculator can be implemented in form of a recursive decent parser using basically the Strings.scanToken(..) and scanDelimiters(..) function. There are 2 local functions (term, primary) that implement the corresponding part of the grammar.
The following operations are supported (pi=3.14.. is a predefined constant):
+, - *, / (expression) sin(expression) cos(expression) tan(expression) sqrt(expression) pi
The optional argument "startIndex" defines at which position scanning of the expression starts.
In case of error, the optional argument "message" is appended to the error message, in order to give additional information where the error occurred.
This function parses the following grammar
expression: [ add_op ] term { add_op term } add_op : "+" | "-" term : primary { mul_op primary } mul_op : "*" | "/" primary : UNSIGNED_NUMBER | pi | ( expression ) | functionName( expression ) function : sin | cos | tan | sqrt
Note, in Examples.readRealParameter it is shown, how the expression function can be used as part of another scan operation.
Extends from Modelica.Icons.Function (Icon for functions).expression("2+3*(4-1)"); // returns 11 expression("sin(pi/6)"); // returns 0.5
Name | Description |
---|---|
string | Expression that is evaluated |
startIndex | Start scanning of expression at character startIndex |
message | Message used in error message if scan is not successful |
Name | Description |
---|---|
result | Value of expression |
nextIndex | Index after the scanned expression |
result = readRealParameter(fileName, name);
This function demonstrates how a function can be implemented that reads the value of a parameter from file. The function performs the following actions:
On file "test.txt" the following lines might be present:
// Motor data J = 2.3 // inertia w_rel0 = 1.5*2; // relative angular velocity phi_rel0 = pi/3
The function returns the value "3.0" when called as:
Extends from Modelica.Icons.Function (Icon for functions).readRealParameter("test.txt", "w_rel0")
Name | Description |
---|---|
fileName | Name of file |
name | Name of parameter |
Name | Description |
---|---|
result | Actual value of parameter on file |
Model that shows the usage of Examples.readRealParameter and Examples.expression. The model has 3 parameters and the values of these parameters are read from a file.
Extends from Modelica.Icons.Example (Icon for runnable examples).
Name | Description |
---|---|
file | File on which data is present |
J | Inertia [kg.m2] |
phi_rel0 | Relative angle [rad] |
w_rel0 | Relative angular velocity [rad/s] |