# How to do System dynamics simulations / diagrams in Mathematica?

System dynamics is an approach to understanding the behaviour of complex systems over time. It deals with internal feedback loops and time delays that affect the behaviour of the entire system. What makes using system dynamics different from other approaches to studying complex systems is the use of feedback loops and stocks and flows. These elements help describe how even seemingly simple systems display baffling nonlinearity

Here is how a system diagram looks like:

Apperently I should be able to this with System Modeler by loading this modelica library but I am also told here that:

The downside, as I understand it, is that each different use of a System Dynamics symbol requires its own entry in the library. For example, a stock with one inflow and one outflow requires a different Modelica symbol from one with one inflow and 2 outflows. Also (again, as I understand it), every separate equation requires its own symbol to be defined and added to the library before it can be used.

¿Anyone here can confirm or deny this? ¿Is this a limitation inherent in all Modelica enviroments? ¿or it is an implementation limitation on this particular library?

and, going back to the original aim of this question: Is there a less expensive way (than System Modeler) to do this in Mathematica, without System Modeler?

-
I am surprised that there is no tags for: "simulation" and "nonlinearity" –  Luxspes Jul 8 '12 at 17:00
–  belisarius Jul 8 '12 at 17:02
I was kind of hoping someone here had first had experience with System Modeler –  Luxspes Jul 8 '12 at 17:05
I found this: modelica.org/libraries/SystemDynamics, apparently since System Modeler uses the Modelica language it should be able to use it. –  Luxspes Jul 8 '12 at 17:21
I wrote a new answer on that meta thread: meta.mathematica.stackexchange.com/a/542/12 I think that if your question is about doing these simulations in SystemModeler / Modelica, that's off topic. If it's about doing it in Mathematica, it's on topic, but SystemModeler might be a better tool for the task ... –  Szabolcs Jul 9 '12 at 10:26

The limitation you quote is not a general limitation of Modelica. It is possible to define a Modelica component that has a variable number of inputs/outputs. Typically the number of inputs/outputs is then given by a parameter to that component.

For example, the following component has one input but 2 outputs, varied with the parameter nout:

model SIMO "Single input, multiple output"
parameter Integer nout=2 "Number of outputs";
RealInput u "Connector of Real input signal";
RealOutput y[nout] "Connector of Real output signals";
equation
end SIMO;


I have not used the System Dynamics modelica library, so cannot speak to how they have chosen to implement their interfaces.

It would be fully possible to create your custom models in Mathematica by defining equations from the ground up, and then simulating them using for example NDSolve. This would however require a significant amount of manual labor.

If you want to stay in the Wolfram product universe, SystemModeler would be the way to go. It allows you to simulate and interact with models from Mathematica, using a Mathematica package bundled with the product.

An overview of features of the Mathematica package, called WSMLink, can be found at http://wolfram.com/system-modeler/features/analysis-mathematica.html.

Documentation to see how the package actually is used is found at http://reference.wolfram.com/system-modeler/WSMLink/guide/WSMLink.html.

Update:

The SystemDynamics library mentioned in the original question is now available for download in a version compatible with Wolfram SystemModeler at the bottom of this blog post: http://blog.wolfram.com/2013/06/11/energy-resource-dynamics-with-the-new-system-dynamics-library-for-systemmodeler/

-
And... is there a way to achieve the same without System Modeler? –  Luxspes Jul 10 '12 at 23:16
Certainly possible, but not easy. NDSolve is powerful for solving differential equations, but Mathematica currently does not have an easy way to graphically build dynamic systems. It is of course possible to create "components" (Symbol structures with equations and graphical representations) and build functionality to connect them together, and then using NDSolve to simulate the system, and the Graphics functionality to build up the system diagram. This would be a massive undertaking though. –  Malte Lenz Jul 11 '12 at 9:36
And what about the part " Also (again, as I understand it), every separate equation requires its own symbol to be defined and added to the library before it can be used." Is that true? –  Luxspes Aug 9 '12 at 1:50
No, that is not true. A class (model or component) in Modelica (and therefore SystemModeler) can contain any number of equations, without defining separate classes for each equation. Of course, if you want to reuse an equation or a collection of equations, for example for the behavior of stock, you can create a class with those equations, and use that class as a component in your bigger model. –  Malte Lenz Aug 9 '12 at 7:54

There is an open-source Modelica system called OpenModelica at www.openmodelica.org.

Frankly, I am a little baffled at what you are calling a "downside". Modelica is an object oriented language, so only different classes need to be defined within the library. A single class can be implemented as many objects within your model. For example, a resistor class need only be defined once, but you may have several resistors with different values within your model. I would think that it should be expected that a component governed by a different set of constitutive equations would require a new class. What would you propose as an alternative?

-

One possible alternative might be Control Systems in Mathematica.

With Control Systems you can carry out analysis, design, and simulation of time control systems.

For an example here an automated house heating system controlled by a thermostat. (Sorry. I really tried to use the drawing tools for a nifty graphic, but...alas...i'm not even able to draw a simple house, so I count on your imagination)

Here I show how to analyze the controller and its performance in a closed loop:

system = TransferFunctionModel[(0.5/((0.2 s + 1) (2 s + 1))), s];

pid = pid = PIDTune[system, Automatic, "PIDData"];

cloop = pid["ReferenceOutput"];

or = OutputResponse[cloop, UnitStep[t], {t, 0, 4}];

p = Plot[%, {t, 0, 4}, PlotRange -> All, Frame -> True,
FrameLabel -> {"t", "y"}, LabelStyle -> Directive[15],
PlotStyle -> Blue]


Using a PID controller instead reduces the bump a little bit:

pid = pid = PIDTune[system, "PID", "PIDData"];


On Woflram blog in '11 Andrew Moylan wrote two very exciting and amusing posts on stabilising an inverted pendulum (segeway) using Control Systems and with decent animations. (http://blog.wolfram.com/2011/01/19/stabilized-inverted-pendulum/)

Hope that this was informative for you. I know that this is not at all visual modeling, but the principles are the same.

-
Any book, website or guide you recommend on how to take a systemdynamics "graphic model" and translate it into something that can be fed in to TransferFunctionModel? –  Luxspes Aug 17 '13 at 22:44
Maybe you could please answer this question: mathematica.stackexchange.com/questions/22798/… using this TransferFunctionModel? –  Luxspes Aug 17 '13 at 22:46