Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Modelica
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Examples== The following code fragment shows a very simple example of a first order system (<math>\dot x = - c \cdot x, x(0)=10 </math>): <syntaxhighlight lang=modelica> model FirstOrder parameter Real c=1 "Time constant"; Real x (start=10) "An unknown"; equation der(x) = -c*x "A first order differential equation"; end FirstOrder; </syntaxhighlight> The following code fragment shows an example to calculate the second derivative <ref>Introduction to Physical Modeling with Modelica, Michael Tiller</ref> of a trigonometric function, using OMShell, as a means to develop the program written below. <syntaxhighlight lang=modelica> model second_derivative Real l; Real z=sin(w*time); Real m; parameter Real w = 1; equation l=der(z); m=der(l); end second_derivative; </syntaxhighlight> [[File:Second_derivative_1.png|center|400px|caption]] Interesting things to note about this example are the 'parameter' qualifier, which indicates that a given variable is time-invariant and the 'der' operator, which represents (symbolically) the time derivative of a variable. Also worth noting are the documentation strings that can be associated with declarations and equations. The main application area of Modelica is the modeling of physical systems. The most basic structuring concepts are shown at hand of simple examples from the electrical domain: ===Built-in and user derived types=== Modelica has the four built-in types Real, Integer, Boolean, String. Typically, user-defined types are derived, to associate physical quantity, unit, nominal values, and other attributes: <syntaxhighlight lang=modelica> type Voltage = Real(quantity="ElectricalPotential", unit="V"); type Current = Real(quantity="ElectricalCurrent", unit="A"); ... </syntaxhighlight> ===Connectors describing physical interaction=== The interaction of a component to other components is defined by physical ports, called '''connectors''', e.g., an electrical pin is defined as <syntaxhighlight lang=modelica> connector Pin "Electrical pin" Voltage v "Potential at the pin"; flow Current i "Current flowing into the component"; end Pin; </syntaxhighlight> When drawing connection lines between ports, the meaning is that corresponding connector variables without the "flow" prefix are identical (here: "v") and that corresponding connector variables with the "flow" prefix (here: "i") are defined by a zero-sum equation (the sum of all corresponding "flow" variables is zero). The motivation is to automatically fulfill the relevant balance equations at the infinitesimally small connection point. ===Basic model components=== A basic model component is defined by a '''model''' and contains equations that describe the relationship between the connector variables in a declarative form (i.e., without specifying the calculation order): <syntaxhighlight lang=modelica> model Capacitor parameter Capacitance C; Voltage u "Voltage drop between pin_p and pin_n"; Pin pin_p, pin_n; equation 0 = pin_p.i + pin_n.i; u = pin_p.v - pin_n.v; C * der(u) = pin_p.i; end Capacitor; </syntaxhighlight> The goal is that a connected set of model components leads to a set of differential, algebraic and discrete equations where the number of unknowns and the number of equations is identical. In Modelica, this is achieved by requiring so called '''balanced models'''. The full rules for defining balanced models are rather complex, and can be read from <ref name="3.5spec"/> in section 4.7. However, for most cases, a simple rule can be issued, that counts variables and equations the same way as most simulation tools do: <pre> A model is balanced when the number of its equations equals the number of its variables. </pre> given that variables and equations must be counted according to the following rule: <pre> ->Number of model equations = Number of equations defined in the model + number of flow variables in the outside connectors ->Number of model variables = Number of variables defined in the model (including the variables in the physical connectors) </pre> ''Note that standard input connectors (such as RealInput or IntegerInput) do not contribute to the count of variables since no new variables are defined inside them.'' The reason for this rule can be understood thinking of the capacitor defined above. Its pins contain a flow variable, i.e. a current, each. When we check it, it is connected to nothing. This corresponds to set an equation pin.i=0 for each pin. That's why we must add an equation for each flow variable. Obviously the example can be extended to other cases, in which other kinds of flow variables are involved (e.g. forces, torques, etc.). When our capacitor is connected to another (balanced) model through one of its pins, a connection equation will be generated that will substitute the two i=0 equations of the pins being connected. Since the connection equation corresponds to two scalar equations, the connection operation will leave the balanced larger model (constituted by our Capacitor and the model it is connected to). The Capacitor model above is '''balanced''', since <pre> number of equations = 3+2=5 (flow variables: pin_p.i, pin_n.i, u) number of variables = 5 (u, pin_p.u, pin_p.i, pin_n.u, pi_n.i) </pre> Verification using OpenModelica<ref name="OpenModelica project"/> of this model gives, in fact <pre> Class Capacitor has 5 equation(s) and 5 variable(s). 3 of these are trivial equation(s). </pre> Another example, containing both input connectors and physical connectors is the following component from Modelica Standard Library: <syntaxhighlight lang=modelica> model SignalVoltage "Generic voltage source using the input signal as source voltage" Interfaces.PositivePin p; Interfaces.NegativePin n; Modelica.Blocks.Interfaces.RealInput v(unit="V") "Voltage between pin p and n (= p.v - n.v) as input signal"; SI.Current i "Current flowing from pin p to pin n"; equation v = p.v - n.v; 0 = p.i + n.i; i = p.i; end SignalVoltage; </syntaxhighlight> The component SignalVoltage is balanced since <pre> number of equations = 3+2=5 (flow variables: pin_p.i, pin_n.i, u) number of variables = 5 (i, pin_p.u, pin_p.i, pin_n.u, pi_n.i) </pre> Again, checking with OpenModelica<ref name="OpenModelica project"/> gives <pre> Class Modelica.Electrical.Analog.Sources.SignalVoltage has 5 equation(s) and 5 variable(s). 4 of these are trivial equation(s). </pre> ===Hierarchical models=== A hierarchical model is built-up from basic models, by instantiating basic models, providing suitable values for the model parameters, and by connecting model connectors. A typical example is the following electrical circuit: <syntaxhighlight lang=modelica> model Circuit Capacitor C1(C=1e-4) "A Capacitor instance from the model above"; Capacitor C2(C=1e-5) "A Capacitor instance from the model above"; ... equation connect(C1.pin_p, C2.pin_n); ... end Circuit; </syntaxhighlight> Via the language element '''annotation(...),''' definitions can be added to a model that do not have an influence on a simulation. Annotations are used to define graphical layout, documentation and version information. A basic set of graphical annotations is standardized to ensure that the graphical appearance and layout of models in different Modelica tools is the same.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)