I have a working SystemModel derived from Wolfram System Modeler and I imported it into Mathematica. The model works, as I can derive the solutions in SystemModeler itself as well as with the SystemModelSimulate[] command in Mathematica.

Anyway, if I try to extract the model equations out of the SystemModel and derive them with NDSolve[] I run into massive problems and I am not able to reproduce the results given by SystemModelSimulate[].

What kind of additional steps do I have to do to make a stable equation system that can be solved with NDSolve? (I want to have a look at more complex systems in the future and be able to tune them by adapting them on formular level if necessary)

Here the Model I created in System Modeler: SystemModel

I extracted the equations and combined them for NDSolve using the information given by the documentation:

(*Get System Equations*)
eqs = model["SystemEquations"];

(*Get Initial Values*)
initEqs = Map[#[[1]][0] == #[[2]] &, model["InitialValues"]];

(*Get Initial Seedings*)
initSeeds = model["InitialSeedings"];

(*Get Parameters*)
params = model["ParameterValues"];

(*Extract name of wanted variable*)
var = model["SystemVariables"][[19]];

(*Insert parameters in equations*)
neqs = eqs //. params;

(*Simplify System*)
neqs = FullSimplify[Join[neqs, initEqs], \[FormalT] >= 0];

(*Solve System*)
s = NDSolve[neqs, var, {\[FormalT], 0, 2} , Method -> {"IndexReduction" -> Automatic},  InitialSeeding -> initSeeds, WorkingPrecision -> 100]

(*Show results*)
Plot[Evaluate[var[t] /. s], {t, 0, 2}]

The calculation fails as you can see below: Result of calculation

As the calculation can be done by Mathematica, SystemModeler and Modelica automatically I want to understand what additional steps I have to consider to derive a working system of equations and calculate a solution from it using Mathematica Functions like DSolve[] or NDSolve[]. (I am aware that in almost any case the later one will be the only possible way for complex systems)

Thanks in advance.


Edit: Here a link to the model

  • 1
    $\begingroup$ Is there a possibility of sharing this specific model so that one can try and load it into their Mathematica notebook? $\endgroup$ – dearN Jun 11 '20 at 17:56
  • $\begingroup$ Of course. Do you know if I can add it to my post or do I have to upload it to some file hoster? $\endgroup$ – Tschibi Jun 11 '20 at 19:14
  • $\begingroup$ I added a download link. $\endgroup$ – Tschibi Jun 11 '20 at 19:41
  • $\begingroup$ I maybe onto something regarding the InitialValues. Somehow System Modeler marks the derivatives as Partname.Vairableder[t] instead of Partname.Vairable'[t] I will try to generate a function to get rid of this and see if I have better results ... I am still open for any suggestions ... $\endgroup$ – Tschibi Jun 15 '20 at 8:29

I found out, that Mathematica has problems with the export of the initial conditions as well as the equations. The problems arise to various formating problems, as well as missing code parts. When you use SystemModel[,"SystemEquations"] as well as SystemModel[,"InitialValues"]. This new functions are still experimental so I guess Wolfram is aware of these problems.

To deal with this kind of problem I used WSMLink package. I testet it with Mathematica 12 and 12.1 and in my cases it works well:


model = Import["PATH/TO/MODEL.mo", "MO"]

precision = 10; (*Needed to be >=6 in to deal with numerical problems*)

eqs = WSMModelData[model, "SystemEquations", t]; (*Extract equations*)

initEqs = Map[#[[1]][0] == #[[2]] &, WSMModelData[model, "InitialValues"]]; (*Extract and format inital values*)

initSeeds = WSMModelData[model, "InitialSeedings"]; (*Extract initial seeds (optional)*)

params = WSMModelData[model, "ParameterValues"]; (*Extract model parameters*)

var = WSMModelData[model, "SystemVariables"]; (*Extract model parameters*)

{eqs, initEqs, initSeeds, params, var}=Map[SetPrecision[#, precision] &, {eqs,initEqs, initSeeds, params, var}]; (*Set Precision to avoid error*)

neqs = SetPrecision[eqs //. params, precision]; (*insert parameters in equations*)

neqs = FullSimplify[Join[neqs, initEqs], t >= 0]; (*Join all Equations and simplify*)

s = NDSolve[neqs, var, {t, 0, 2} , WorkingPrecision -> precision - 4,  InitialSeeding -> initSeeds] (*Solve the system*) 

For me this is working. Hopefully Wolfram will fix the bugs in the SystemModel[] command


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.