Package Modelica.​Utilities.​Examples
Examples to demonstrate the usage of package Modelica.Utilities

Information

This package contains quite involved examples that demonstrate how to use the functions of package Modelica.Utilities. In particular the following examples are present.

Extends from Modelica.​Icons.​ExamplesPackage (Icon for packages containing runnable examples).

Package Contents

NameDescription
calculatorInterpreter to evaluate simple expressions consisting of +, -, *, /, (), sin(), cos(), tan(), sqrt(), asin(), acos(), atan(), exp(), log(), pi
expressionExpression interpreter that returns with the position after the expression (expression may consist of +, -, *, /, (), sin(), cos(), tan(), sqrt(), asin(), acos(), atan(), exp(), log(), pi)
ReadRealMatrixFromFileDemonstrate usage of function Streams.readRealMatrix
readRealParameterRead the value of a Real parameter from file
readRealParameterModelDemonstrate usage of Examples.readRealParameter/.expression
WriteRealMatrixToFileDemonstrate usage of function Streams.writeRealMatrix

Function Modelica.​Utilities.​Examples.​calculator
Interpreter to evaluate simple expressions consisting of +, -, *, /, (), sin(), cos(), tan(), sqrt(), asin(), acos(), atan(), exp(), log(), pi

Information

Syntax

result = calculator(expression);

Description

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)
   asin(expression)
   acos(expression)
   atan(expression)
   exp(expression)
   log(expression)
   pi

Example

  calculator("2+3*(4-1)");  // returns 11
  calculator("sin(pi/6)");  // returns 0.5

Extends from Modelica.​Icons.​Function (Icon for functions).

Inputs

TypeNameDescription
StringstringExpression that is evaluated

Outputs

TypeNameDescription
RealresultValue of expression

Function Modelica.​Utilities.​Examples.​expression
Expression interpreter that returns with the position after the expression (expression may consist of +, -, *, /, (), sin(), cos(), tan(), sqrt(), asin(), acos(), atan(), exp(), log(), pi)

Information

Syntax

             result = expression(string);
(result, nextIndex) = expression(string, startIndex=1, message="");

Description

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)
   asin(expression)
   acos(expression)
   atan(expression)
   exp(expression)
   log(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
              | asin
              | acos
              | atan
              | exp
              | log

Note, in Examples.readRealParameter it is shown, how the expression function can be used as part of another scan operation.

Example

  expression("2+3*(4-1)");  // returns 11
  expression("sin(pi/6)");  // returns 0.5

Extends from Modelica.​Icons.​Function (Icon for functions).

Inputs

TypeNameDescription
StringstringExpression that is evaluated
IntegerstartIndexStart scanning of expression at character startIndex
StringmessageMessage used in error message if scan is not successful

Outputs

TypeNameDescription
RealresultValue of expression
IntegernextIndexIndex after the scanned expression

Function Modelica.​Utilities.​Examples.​readRealParameter
Read the value of a Real parameter from file

Information

Syntax

result = readRealParameter(fileName, name);

Description

This function demonstrates how a function can be implemented that reads the value of a parameter from file. The function performs the following actions:

  1. It opens file "fileName" and reads the lines of the file.
  2. In every line, Modelica line comments ("// ... end-of-line") are skipped
  3. If a line consists of "name = expression" and the "name" in this line is identical to the second argument "name" of the function call, the expression calculator Examples.expression is used to evaluate the expression after the "=" character. The expression can optionally be terminated with a ";".
  4. The result of the expression evaluation is returned as the value of the parameter "name".

Example

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:

readRealParameter("test.txt", "w_rel0")

Extends from Modelica.​Icons.​Function (Icon for functions).

Inputs

TypeNameDescription
StringfileNameName of file
StringnameName of parameter

Outputs

TypeNameDescription
RealresultActual value of parameter on file

Model Modelica.​Utilities.​Examples.​readRealParameterModel
Demonstrate usage of Examples.readRealParameter/.expression

Information

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).

Parameters

TypeNameDefaultDescription
StringfileModelica.Utilities.Files.loadResource("modelica://Modelica/Resources/Data/Utilities/Examples_readRealParameters.txt")File on which data is present
InertiaJreadRealParameter(file, "J")Inertia
Anglephi_rel0readRealParameter(file, "phi_rel0")Relative angle
AngularVelocityw_rel0readRealParameter(file, "w_rel0")Relative angular velocity

Model Modelica.​Utilities.​Examples.​WriteRealMatrixToFile
Demonstrate usage of function Streams.writeRealMatrix

Information

Example model that shows how to write a Real matrix in MATLAB MAT format on file using function writeRealMatrix.

Extends from Modelica.​Icons.​Example (Icon for runnable examples).

Parameters

TypeNameDefaultDescription
RealA[3,2][11,12; 21,22; 31,32]Matrix stored in different formats on files

Model Modelica.​Utilities.​Examples.​ReadRealMatrixFromFile
Demonstrate usage of function Streams.readRealMatrix

Information

Example model that shows how to read a Real matrix in MATLAB MAT format from file using functions readMatrixSize and readRealMatrix.

Additionally, specific matrices from the supported file formats are loaded and it is checked whether the loaded matrices have the expected values.

Extends from Modelica.​Icons.​Example (Icon for runnable examples).

Parameters

TypeNameDefaultDescription
StringfileModelica.Utilities.Files.loadResource("modelica://Modelica/Resources/Data/Utilities/Test_RealMatrix_v4.mat")File name of matrix
StringmatrixName"Matrix_A"Matrix name in file
final Integerdim[2]Modelica.Utilities.Streams.readMatrixSize(file, matrixName)Dimension of matrix
final RealA[:,:]Modelica.Utilities.Streams.readRealMatrix(file, matrixName, dim1[1], dim1[2])Matrix data
final Stringfile1Modelica.Utilities.Files.loadResource("modelica://Modelica/Resources/Data/Utilities/Test_RealMatrix_v4.mat")File name of check matrix 1
final Stringfile2Modelica.Utilities.Files.loadResource("modelica://Modelica/Resources/Data/Utilities/Test_RealMatrix_v6.mat")File name of check matrix 2
final Stringfile3Modelica.Utilities.Files.loadResource("modelica://Modelica/Resources/Data/Utilities/Test_RealMatrix_v7.mat")File name of check matrix 3
final StringmatrixName1"Matrix_A"Names of check matrices
final Integerdim1[2]Modelica.Utilities.Streams.readMatrixSize(file1, matrixName1)Dimension of check matrix 1
final Integerdim2[2]Modelica.Utilities.Streams.readMatrixSize(file2, matrixName1)Dimension of check matrix 2
final Integerdim3[2]Modelica.Utilities.Streams.readMatrixSize(file3, matrixName1)Dimension of check matrix 3
final RealA1[:,:]Modelica.Utilities.Streams.readRealMatrix(file1, matrixName1, dim1[1], dim1[2])Data of check matrix 1
final RealA2[:,:]Modelica.Utilities.Streams.readRealMatrix(file2, matrixName1, dim2[1], dim2[2])Data of check matrix 2
final RealA3[:,:]Modelica.Utilities.Streams.readRealMatrix(file3, matrixName1, dim3[1], dim3[2])Data of check matrix 3

Generated 2018-12-12 12:14:36 EST by MapleSim.