# WSM_MathematicaIntegration

0 (0 Likes / 0 Dislikes)

My name is Anneli Mossberg
and I will be presenting the new features in the integration between
Mathematica and Wolfram SystemModeler 4,
including improved streamline workflow, real-time simulation,
and programmatic model creation.
Let’s start with improved streamline workflow,
which has greatly deepened the integration
between SystemModeler and Mathematica
In the previous version of the SystemModeler Link,
the intended experiment settings—such as Stop Time and Step Size—
had to be guessed at or looked up in model documentation.
Although it’s still useful,
it could lead to problems as we can see in this plot,
where the most interesting behavior occurs during the first couple of seconds
but it becomes nearly undetectable when using the stop time of ten seconds.
The SystemModeler Link now automatically
uses the experiment settings set by the model creator,
thereby eliminating any unnecessary guesswork.
Here it picks up the stop time from the experiment settings
and we can easily see the behavior that the model creator intended us to see.
Mathematica and Simulation Center are now more deeply integrated than before.
Simulation Center can, for example,
be opened from within Mathematica,using function “WSMSimulationCenter”.
Adding a model name as an argument both starts Simulation Center
and opens an experiment with the model.
Also, the simulation results can be seamlessly transferred from Simulation Center
to Mathematica using the “Copy Result as Mathematica Object” button
in the Experiment Browser toolbar.
The copied object can be pasted into Mathematica
and be used as a regular WSM simulation data object,
for example, to plot or to further analyze the simulation result.
If you don’t want to copy the entire simulation results,
you can also choose to only copy the results for certain variables
by right clicking the variable and choosing “Copy as Mathematica Object”
in Simulation Center.
“WSMSimulate” now has support for using Mathematica’s powerful differential equation solver,“NDSolve”,
which adds another strong solver to choose from when simulating your models.
This functionality has a few nice advantages.
For example, NDSolve can solve some more complicated systems that the built-in solvers in System Modeler
might otherwise have trouble with and it also increases the performance in some scenarios.
For example, solving this bouncing ball system is approximately 3 times faster
using NDSolve compared to the automatically chosen solver.
When using WSMPlot to plot more than one curve,
plot legends are now automatically created.
The automatic legend is the name of the plotted variable.
In this case: X1 to X5.
But it is also possible to choose the variable description as the plot legend instead.
Model diagrams are graphic representations of models.
And in SystemModeler 4 we have added support for retrieving them into SystemModeler.
In Mathematica, model diagrams are now used to represent models.
Right clicking a diagram opens up a menu with several model related options,
such as “Simulate Model”, which launches WSMSimulate.
We can, of course, also copy and paste the model
and use it as in input anywhere you usually use the string of the model name.
Dynamic diagrams are graphic properties that make it possible
to build dynamic graphics into any model.
This can be very useful to illustrate system behavior,
such as the water level in tanks, as in this case, or the temperature changes in a thermometer.
Now we have so far covered streamlined workflow, Simulation Center integration and model diagrams,
which now takes us to programmatic model creation—
an exciting feature that is completely new to SystemModeler 4.
WSMCreateModel is a new function for the SystemModeler Link.
It allows you to create Modelica Models from Systems models in Mathematica,
either in the form of a transfer function model or saved space model.
Models created in this manner are instantly and automatically available in both Model Center
and Simulation Center for integration into a larger model or a simulation.
WSMCreateModel is not limited to Systems Models, though.
Modelica Models can also be created from Mathematica by explicitly stating the system equations
and the time variable.
Many Mathematica functions are automatically translated into the Modelica equivalent
and things like discrete events are appropriately converted.
The new model can be treated just as any Modelica model created in Model Center
and can, for example, be used together with WSMSimulate or WSMPlot.
To make your modeling process more efficient,
model creation in Mathematica can also be done by connecting already existing SystemModeler components—
simply construct a list of instance names and which Modelica class they belong to.
In this case, for example, “a” is an instance of component “abs” in the Modelica standard library.
You also need to specify a list of connections.
Here we wish to connect “sine.y” to “a.u”.
Finally, function WSMConnectComponents creates the model.
We have now reached the last part of the presentation,
namely real time simulation,
which is not new to SystemModeler as a whole but is a completely
new functionality for the SystemModeler Link.
With the link in SystemModeler 4, it is possible to start a real-time simulation
as well as visualize it all in real time from within Mathematica.
The link between SystemModeler and Mathematica
also makes it possible to interactively and programmatically provide model input—
for example, using hardware devices, gages, and sliders.
Model output can also be captured for processing, feedback, and visualization—
for instance, with lots, gages, and other animations.
The features mentioned in this presentation are only a sample of all the useful features available
through the integration between SystemModeler and Mathematica.
The newly released Mathematica 10 contains a number of new beneficial features,
such as enhanced control systems, hybrid and differential equations,
and new visualization methods, as we will see in the next presentation.