MapleSim[Tools]
MapleToModelica
create a Modelica model from Maple input
Calling Sequence
Parameters
Options
Description
Examples
MapleToModelica(sys, opts)
sys
-
(optional) list or set of equations, a DynamicSystems object, or a string
opts
(optional) equation(s) of the form option = value; specify options for MapleToModelica
annotation = string
A Maple string containing the content of a Modelica annotation. It is used to create an annotation for the generated Modelica.
class = model or connector
Specifies whether the Modelica is a model or a connector. The default is model.
class_name = string
The name of the generated Modelica class (model). The default is "custom" with a positive integer appended.
comment = string
A string used to annotate the generated Modelica.
display = true or false
True means print a formatted version of the generated Modelica. The default is false.
equations_only = true or false
True means return only the Modelica initial equations, and equations, as a string. This is useful for displaying how sys is interpreted as Modelica.
flatten = true or false
True means flatten the Modelica. The default is false.
omit_annotations = true or false
True means omit annotations from the generated Modelica. This may be used with display to view the code without the distraction of the annotations. The default is false.
warning_only = true or false
True means generate a warning, but do not raise an error, if there is a problem with the generated Modelica code. Using this allows the code to be inspected. The default is false.
Interface Options
These options specify the interface between the component equations, sys, and the outside world.
append_custom_connectors = truefalse
True means append any custom connectors assigned by the custom_connectors option to the generated modelica. The default is true.
components = list or set
Specifies Modelica components used in the model. The general form of each element is id::type=modifications.
constants = list or set
Specifies the constants, their types, and their values. The format is identical to parameters. Note that because constants is an assigned Maple global name, this keyword must be forward-quoted when used. The default is the empty set.
connections or connectors = list or set
Specifies the connections to the component. The order of the corresponding declarations in the output matches that of the input. A list should be used when using the default pin placement strategy, the order determines the order on an edge. This option is ignored if sys is a DynamicSystems object.
Each element of connections specifies the characteristics of one port.
The full specification has the form id::type=signals, where id is a string that becomes the port name, type specifies the type of connection, and signals specify the Maple expressions connected to the port.
The id field is required. It is a string or name that corresponds to a valid Modelica unquoted identifier.
A Modelica flow or stream variable in a connector is designated by wrapping the id with flow or stream, for example, flow⁡i.
The type field is a Maple name or string that specifies the type of the connection. The type may be omitted. The default is real.
The names real, boolean, and integer correspond to the causal Modelica types Real, Boolean, and Integer. The names electrical, fluidheatflow, magnetic, rotational, thermal, and translational correspond to standard Modelica acausal interface ports.
Type names may be indexed by a positive integer to indicate the dimension of the connector. For example, electrical[3] specifies an electrical port with dimension three.
The type field may be a full Modelica type, or can be an identifier for a custom connector defined via the custom_connectors option.
The signals field specifies the Maple expression that is connected to the port.
It is optional for causal types. If omitted, the id is used as the connection.
An acausal connector type requires a list of Maple expressions, the first being the across-variable (potential), the second the through-variable (flow). For example, for an electric port the signals may be v⁡t,−i⁡t.
A custom connector type also requires a list of Maple expressions, the list corresponds to the signals in the connector.
An array type must have a signal field that is a list of expressions, or listlist of expressions.
The neutral-operator &c may be used to a attach a comment string to a connection. For example, p :: electrical = [v(t),i(t)] &c "positive pin" adds the comment string "positive pin" to the Modelica declaration of the p connector.
custom_connectors = list or set of equations
Specifies custom connectors. The left side of the equation is a string or symbol that identifies the connector type. The right side of the equation is a string of Modelica code that defines the connector. For example, MyConn = "connector MyConn input Real u; output Real y; end MyConn;".
dont_declare = set of symbols
Specifies variables that should not be declared. These are variables that are used in the system equations but have been inherited from an extension (see the extends options). The default is an empty set.
extends = name, string, or list of such
Generates Modelica extends clause(s). The value is the class name from which the component is extended. A list may be used to create multiple extends-clauses.
import = list
List of Modelica imports. Each element can be a name or string, or an equation of such.
initial = list or set of equations
Specifies initial equations. Values at the initial time are specified as x⁡0,D⁡x⁡0,D2⁡x⁡0.
parameters = list or set
Specifies the parameters, their types, and their default values. The order of the corresponding declarations in the output matches that of the input.
Each element specifies one parameter.
The full specification has the form id::type=value
The id field is required. It is a string or symbol that corresponds to a valid Modelica unquoted identifier. It should match the Maple symbol used in the eqs.
A Modelica protected parameter is indicated by wrapping the id with protected; for example, protected⁡K. A Modelica public parameter is indicated in a similar manner, using public. By default, parameters are public.
The optional type field specifies the parameter type. The allowable types are real, boolean, and integer. The default is real. A type may be indexed with a positive integer to specify an array parameter.
The optional value field specifies the start value of the parameter. The value is an algebraic expression, or, if the parameter is an array type, a list of algebraic expressions.
The neutral operator &c may be used to attach a comment string to the parameter declaration. For example, Larm :: real = 3 &c "length of arm" adds the comment string "length of arm" to the Modelica declaration of the parameter Larm.
tvar = symbol
Specifies the independent time variable used in the equations. Used when sys is a set of equations. The default is t.
variables = list or set
Specifies variables, their types, and, optionally, their initial values. The specification and operation is essentially identical to that of the parameters option; see the parameters option in this section for details.
Icon Options
These options specify the component icon and the placement and type of pins on its edge. Component icons are always rectangular.
bitmap = string
Specify a bitmap file to use for the component icon. The bitmap file may have extension bmp, jpg, jpeg, or png. The syntax is that of a URI: a scheme name (file or attachment), followed by a colon, followed by the location. For example, "file:///home/bob/icons/myicon.png". The attachment scheme is used for bitmap files attached to the MapleSim document. The default is the empty string, which specifies that no bitmap file is used.
copy_icon = string or none
Specifies a Modelica component. The icon of the specified component is used for this component. If none is used, no icon is created. This is useful when extending a model that assigns the icon. The default icon is "Maplesoft.Icons.CustomComponent".
filled_pins = list or set of strings
Specifies which connection icons are filled (solid). The keyword input_pins is a synonym. By convention, the inputs of causal connections are filled, the outputs are unfilled. The strings correspond to the connection names (see the connections option in the Interface Options section). By default all connections are filled, though connections generated automatically from a DynamicSystems object get the proper convention. See also unfilled_pins.
icon_graphics = list of strings
The strings are Modelica graphical annotations; they are combined with the existing graphical annotations of the icon. This can be used to add some small detail to an existing icon, say a line for an added connection. The default is an empty list.
icon_size = list of two positive integers
Specifies the dimensions of the icon in pixels, as width,height. The default is 200 x 200.
input_pins = list or set of strings
A synonym for filled_pins.
min_gap = positive integer
Specifies the minimum gap between pins, when placed automatically. If this cannot be met, the edge dimension is increased, unless icon_size is specified, in which case an error is raised. The default is 10.
output_pins = list or set of strings
A synonym for unfilled_pins.
pin_placement = list or set of equations
Specifies the placement of the connections.
The left side of each equation is a name corresponding to an edge, left, right, top, or bottom.
The right side of each equation specifies the connections made on the corresponding edge. It is a string, or list of strings. A string must match one of the strings used on the left side of the connection equations (see the connections option in the Interface Options section). If a string, the pin is placed in the center of the edge. If a list, the pins are arranged in that order, from top to bottom, or left to right, depending whether the edge is vertical or horizontal.
The default for a DynamicSystems object is to place the inputs on the left and the outputs on the right. Other systems must specify the locations.
An alternative syntax is for each equation to have the form string=numeric,numeric, where the left side is the name of a pin and the right side specifies its location. The locations should lie on the edge of the icon.
unfilled_pins = list or set of strings
Specifies which connection icons are unfilled (open). The keyword output_pins is a synonym. By convention, the inputs of causal connections are filled and the outputs are unfilled. The strings correspond to the connection names (see the connections option in the Interface Options section). By default all connections are filled, however, connections generated automatically from a DynamicSystems object get the proper convention. See also filled_pins.
The MapleToModelica command creates a Modelica model from either Maple equations, a DynamicSystems object, or a Modelica class name. It also creates a Modelica connector from a connector specification.
The optional sys parameter specifies the system equations. It may be a list or set of differential-algebraic equations, a DynamicSystems object, or a string corresponding to a Modelica class. The default value of sys is an empty set. This is useful when creating a custom connector.
When sys is a list or set of equations, the following considerations apply:
Variables in the equations should be represented as functions of time, for example, x⁡t. The tvar option specifies the time variable, by default t.
Time derivatives are expressed as diff(x(t),t).
Time derivatives of order greater than one are converted to first-order derivatives using auxiliary variables.
Initial conditions are expressed by substituting 0 for the time-variable; for example, x⁡0=0. The D operator expresses initial derivatives; for example, D⁡x⁡0=1.
Conditional expressions may be represented as piecewise expressions; for example, x⁡tx⁡t<y⁡ty⁡totherwise. A symbolic expression, say b⁡t, in a condition may be expressed as b⁡t=true; piecewise requires a Maple boolean expression.
The and, or, and not boolean operators may be used in boolean expressions.
The Modelica function noEvent may be applied to an expression to prevent events from being generated by the inequalities of the expression; for example, noEvent⁡tt<1t2otherwise.
The following standard Maple functions are handled: sin, cos, tan, arcsin, arccos, arctan, sinh, cosh, tanh, exp, ln, and log; arctan may be used in either the one or two argument form. The log function is identical to the ln function.
When sys is a DynamicSystems object, the inputs and output of the object become the inputs and outputs of the model, respectively. The inputs are placed on the left side of the icon, the outputs on the right. The systemname field of the DynamicSystems object is used as the class name of the model, unless it is the empty string or the class_name parameter is passed to MapleToModelica.
When sys is a string corresponding to a fully-qualified Modelica class, the modelica corresponding to the class definition is returned. This is primarily useful for inspecting the equations of a class to be extended. See the Modelon Hydraulics example.
The generated Modelica model is returned as a string.
Access MapleToModelica directly.
with⁡MapleSim:-Tools:
DynamicSystems
Create a DynamicSystems object and convert it to Modelica.
with⁡DynamicSystems:
sys≔NewSystem⁡Ks2+ω0Q⁢s+ω02:
MapleToModelica⁡sys,class_name=second_order,display:
model second_order extends Maplesoft.Icons.CustomComponent; Modelica.Blocks.Interfaces.RealInput u[1] annotation (Placement(transformation( extent = { {-110, -10}, {-90, 10}}, rotation = 0))); Modelica.Blocks.Interfaces.RealOutput y[1] annotation (Placement(transformation( extent = { {90, -10}, {110, 10}}, rotation = 0))); parameter Real K = 0; parameter Real Q = 0; parameter Real omega0 = 0; Real x[2]; equation der(x[1]) = -omega0 ^ (-1) * Q * x[2]; der(x[2]) = omega0 ^ 3 / Q * x[1] - omega0 / Q * x[2] - omega0 / Q * u[1]; y[1] = K * x[1]; end second_order;
Create a multi-input, multi-output DynamicSystems object.
sys2≔NewSystem⁡1s2|s2s3+5⁢s2+7⁢s+6,1s|cs2+a⁢s+b
sys2≔Transfer Functioncontinuous2 output(s); 2 input(s)inputvariable=u1⁡s,u2⁡soutputvariable=y1⁡s,y2⁡s
Convert the system to Modelica. Override the default connector names, specify initial values for the state variables, and specify the default values for the parameters a, b, and c.
MapleToModelica⁡sys2,connectors=inp::real2=u1,u2⁡t,out::real2=y1,y2⁡t,initial=x=0,0,0,0,0,0,0,class_name=MIMO,output_pins=out,parameters=a=1,b=2,c=π,display:
model MIMO extends Maplesoft.Icons.CustomComponent; Modelica.Blocks.Interfaces.RealInput inp[2] annotation (Placement(transformation( extent = { {-110, -10}, {-90, 10}}, rotation = 0))); Modelica.Blocks.Interfaces.RealOutput out[2] annotation (Placement(transformation( extent = { {90, -10}, {110, 10}}, rotation = 0))); parameter Real a = 1; parameter Real b = 2; parameter Real c = Modelica.Constants.pi; Real x[7]; Real u[2]; Real y[2]; initial equation x = {0, 0, 0, 0, 0, 0, 0}; equation der(x[1]) = x[2]; der(x[2]) = u[1]; der(x[3]) = x[4]; der(x[4]) = x[5]; der(x[5]) = x[6]; der(x[6]) = (-5 - a) ^ (-1) * x[7]; der(x[7]) = -6 * b * (-5 - a) * x[3] + (-6 * a - 7 * b) * (-5 - a) * x[4] + (-6 - 7 * a - 5 * b) * (-5 - a) * x[5] + (-7 - 5 * a - b) * (-5 - a) * x[6] + (-5 - a) * x[7] + (-5 - a) * u[2]; y[1] = x[1] + b * x[5] + a * x[6] + (-5 - a) ^ (-1) * x[7]; y[2] = x[2] + 6 * c * x[3] + 7 * c * x[4] + 5 * c * x[5] + c * x[6]; inp[1] = u[1]; inp[2] = u[2]; out[1] = y[1]; out[2] = y[2]; end MIMO;
Simple Causal Model
Implement the equation y=u1+K⁢v1+v2, with v1 and v2 two components of a vector input.
MapleToModelica⁡y⁡t=u1⁡t+K⁢v1⁡t+v2⁡t,class_name=mymodel,parameters=K=1,connections=u::real=u1,v::real2=v1,v2,y::real=y,unfilled_pins=y,omit_annotations,display:
model mymodel extends Maplesoft.Icons.CustomComponent; Modelica.Blocks.Interfaces.RealInput u; Modelica.Blocks.Interfaces.RealInput v[2]; Modelica.Blocks.Interfaces.RealOutput y; parameter Real K = 1; Real u1; Real v1; Real v2; equation u = u1; y = u1 + K * (v1 + v2); v[1] = v1; v[2] = v2; end mymodel;
Simple Acausal Model
Implement a resistor model with a power variable.
eqs≔P⁡t=v⁡t⁢i⁡t,v⁡t=R⁢i⁡t,v⁡t=vp⁡t−vn⁡t:
MapleToModelica⁡eqs,class_name=resistor_with_power,import=Modelica.Units.SI.Resistance,parameters=R::Resistance=1&cresistance,variables=P::Power,i::Current,v::Voltage,vn::Voltage,vp::Voltage,connections=p::electrical=vp,i⁡t&cpositive pin,n::electrical=vn,−i⁡t&cnegative pin,unfilled_pins=n,omit_annotations,display:
model resistor_with_power import Modelica.Units.SI.Resistance; extends Maplesoft.Icons.CustomComponent; Modelica.Electrical.Analog.Interfaces.PositivePin p "positive pin"; Modelica.Electrical.Analog.Interfaces.NegativePin n "negative pin"; parameter Modelica.Units.SI.Resistance R = 1 "resistance"; Modelica.Units.SI.Power P; Modelica.Units.SI.Current i; Modelica.Units.SI.Voltage v; Modelica.Units.SI.Voltage vn; Modelica.Units.SI.Voltage vp; equation P = v * i; v = R * i; v = vp - vn; n.i = -i; n.v = vn; p.i = i; p.v = vp; end resistor_with_power;
Polyphase Resistor
Create a polyphase resistor with a common value. While one should normally declare all variables, here we use the defaults chosen by MapleToModelica, which is to declare undeclared variables as Real. The declared dimension is the largest index used.
The copy_icon option is used to copy the resistor symbol from an existing component.
num≔3:
eqs≔seq⁡op⁡vk⁡t=R⁢ik⁡t,vk⁡t=vpk⁡t−vnk⁡t,k=1..num
eqs≔v1⁡t=R⁢i1⁡t,v1⁡t=vp1⁡t−vn1⁡t,v2⁡t=R⁢i2⁡t,v2⁡t=vp2⁡t−vn2⁡t,v3⁡t=R⁢i3⁡t,v3⁡t=vp3⁡t−vn3⁡t
MapleToModelica⁡eqs,class_name=polyphase_common_resistor,parameters=R=1,copy_icon=Modelica.Electrical.Polyphase.Basic.Resistor,connections=p::electricalnum=seq⁡vpk,ik,k=1..num⁡t,n::electricalnum=seq⁡vnk,−ik,k=1..num⁡t,unfilled_pins=n,omit_annotations,display:
model polyphase_common_resistor Modelica.Electrical.Analog.Interfaces.PositivePin p[3]; Modelica.Electrical.Analog.Interfaces.NegativePin n[3]; parameter Real R = 1; Real i[3]; Real v[3]; Real vn[3]; Real vp[3]; equation v[1] = R * i[1]; v[1] = vp[1] - vn[1]; v[2] = R * i[2]; v[2] = vp[2] - vn[2]; v[3] = R * i[3]; v[3] = vp[3] - vn[3]; n[1].i = -i[1]; n[1].v = vn[1]; n[2].i = -i[2]; n[2].v = vn[2]; n[3].i = -i[3]; n[3].v = vn[3]; p[1].i = i[1]; p[1].v = vp[1]; p[2].i = i[2]; p[2].v = vp[2]; p[3].i = i[3]; p[3].v = vp[3]; end polyphase_common_resistor;
Custom Connector
If the option class=connector is passed to MapleToModelica, the output is a Modelica connector definition. This can then be used with the custom_connectors option in a subsequent call to MapleToModelica.
Assign a function that prepends Modelica.Units.SI. to a string.
SI≔dim↦sprintf⁡Modelica.Units.SI.%s,dim:
Create the modelica for a custom connector that models both electrical and thermal connections. It has variables for voltage, current, temperature, and heat-flow.
ETmo≔MapleToModelica⁡class=connector,class_name=ETconn,connections=v::SI⁡Voltage&cVoltage at connection,flow⁡i::SI⁡Current&cCurrent into connection,T::SI⁡Temperature&cTemperature at connection,flow⁡Q::SI⁡HeatFlowRate&cHeat flow into connection,display:
connector ETconn Modelica.Units.SI.Voltage v "Voltage at connection"; flow Modelica.Units.SI.Current i "Current into connection"; Modelica.Units.SI.Temperature T "Temperature at connection"; flow Modelica.Units.SI.HeatFlowRate Q "Heat flow into connection"; end ETconn;
Assign the equations for a voltage source with a series resistor.
eqs≔P⁡t=R⁢i⁡t2,T⁡t=Tr⁡t+Rth1⁢Q⁡t,Tr⁡t=Rth2⁢P⁡t+Q⁡t+Ta,v⁡t=V+R⁢i⁡t
Generate the modelica, using the custom connector.
MapleToModelica⁡eqs,class_name=supply,connections=ET::ETconn=v,i,T,Q⁡t,custom_connectors=ETconn=ETmo,parameters=R::SI⁡Resistance=1&cSupply series resistance,Rth1::SI⁡ThermalResistance=1&cThermal resistance from port to resistor,Rth2::SI⁡ThermalResistance=1&cThermal resistance from resistor to ambient,Ta::SI⁡Temperature=300&cAmbient temperature,V::SI⁡Voltage=1&cSupply open-circuit voltage,variables=P::SI⁡Power&cPower dissipation in resistor,Q::SI⁡HeatFlowRate&cHeat flow rate into thermal port,T::SI⁡Temperature&cTemperature at thermal port,Tr::SI⁡Temperature&cTemperature of resistor,i::SI⁡Current&cCurrent into electrical pin,v::SI⁡Voltage&cVoltage at electrical pin,omit_annotations,display:
model supply extends Maplesoft.Icons.CustomComponent; ETconn ET; parameter Modelica.Units.SI.Resistance R = 1 "Supply series resistance"; parameter Modelica.Units.SI.ThermalResistance Rth1 = 1 "Thermal resistance from port to resistor"; parameter Modelica.Units.SI.ThermalResistance Rth2 = 1 "Thermal resistance from resistor to ambient"; parameter Modelica.Units.SI.Temperature Ta = 300 "Ambient temperature"; parameter Modelica.Units.SI.Voltage V = 1 "Supply open-circuit voltage"; Modelica.Units.SI.Power P "Power dissipation in resistor"; Modelica.Units.SI.HeatFlowRate Q "Heat flow rate into thermal port"; Modelica.Units.SI.Temperature T "Temperature at thermal port"; Modelica.Units.SI.Temperature Tr "Temperature of resistor"; Modelica.Units.SI.Current i "Current into electrical pin"; Modelica.Units.SI.Voltage v "Voltage at electrical pin"; equation P = R * i ^ 2; T = Tr + Rth1 * Q; v = V + R * i; Tr = Rth2 * (P + Q) + Ta; ET.Q = Q; ET.T = T; ET.i = i; ET.v = v; end supply; connector ETconn Modelica.Units.SI.Voltage v "Voltage at connection"; flow Modelica.Units.SI.Current i "Current into connection"; Modelica.Units.SI.Temperature T "Temperature at connection"; flow Modelica.Units.SI.HeatFlowRate Q "Heat flow into connection"; end ETconn;
See Also
MapleSim
MapleSim[LinkModel]
Download Help Document