Group Abstract Group Abstract

Message Boards Message Boards

0
|
15.4K Views
|
15 Replies
|
2 Total Likes
View groups...
Share
Share this post:

[Solved] Convert a linear system of ODEs to a System Modeler file?

Posted 5 years ago

Dear Community,

I have a simple four compartment model, governed by a system of four, linear ODEs. (FourCompartments.nb) ODE 2 (L2) has an external source, obtained from an Excel file (SourceIntensity.xlsx)

My question is: is there a way in Mathematica to convert this case to a System Modeler .mo file, which Sytem Modeler can read and execute? Or any advice, how to do it?

Many tx in advance, best regards Andras

POSTED BY: agilicz
15 Replies
Posted 5 years ago

Dear Neil and Sergio,

Works perfectly :-) Tx for the kind help. This is what I was looking for.

Best regards,

Andras

POSTED BY: agilicz

Hi Andras,

There a couple of options here. Since it seems you want to fully move your setup to System Modeler, you can relay on CreateSystemModel and CreateDataSystemModel to create models from your equations and data, respectively. To see this, let's pick this information from your notebook:

qtab = Import[FileNameJoin[{NotebookDirectory[], "SourceIntensity.xlsx"}], {"Data", 1}];
t12 = 23.1481 ;
t21 = 5.4537 ;
t23 = 9.752 ;
t32 = 14.9007 ;
t34 = 8.8235 ;
t43 = 363.7255 ;
odesys = {
       D[l1[t], t] == (l2[t]/t21) - (l1[t]/t12),(*L1*)
       D[l2[t], t] == (l1[t]/t12) - (l2[t]/t21) + (l3[t]/t32) - (l2[t]/t23) + u[t],(*L2*)
       D[l3[t], t] == (l2[t]/t23) - (l3[t]/t32) + (l4[t]/t43) - (l3[t]/t34),(*L3*)
       D[l4[t], t] == (l3[t]/t34) - (l4[t]/t43),(*L4*)
       l1[0] == 2500.,
       l2[0] == 589.,
       l3[0] == 900., 
       l4[0] == 37100. (*Initial conditions.*)
};

, where I have only modified your system of equations odesys replacing qinterp[t] for a generic input u[t]. Now you can do:

CreateDataSystemModel["SourceIntensity", qtab]

to directly pick your data and create a model with the name "SourceIntensity" that performs an interpolation of your data. Then you can do

CreateSystemModel["LinearSystem", odesys, t, {"u" \[Element] "RealInput"}]

to create a model with the name "LinearSystem" from your linear system, specifying that u is an input. Finally, with

ConnectSystemModelComponents["ConnectedSystem", {"si" \[Element] "SourceIntensity", "ln" \[Element] "LinearSystem"}, {"si.y[1]" \[UndirectedEdge] "ln.u"}]

you create a model called "ConnectedSystem" that connects the model with interpolated data to the model with the system of equations. These 3 models are now available in System Modeler for you to simulate, save or interact with them. You can simulate and plot in the Wolfram Language to check that your results match your expectations:

sim = SystemModelSimulate["ConnectedSystem", 400];
SystemModelPlot[sim, {"ln.u"}]
SystemModelPlot[sim, {"ln.l1", "ln.l2", "ln.l3", "ln.l4"}]

You can use the same plot formatting specifications that you have in your notebook in SystemModelPlot, to match your preffered style:

SystemModelPlot[sim, {"ln.u"}, PlotStyle -> Magenta , 
 PlotLabel -> "Source" , Frame -> True , 
 FrameLabel -> { "Elapsed time" , "Source" } , 
 PlotRange -> { 0. , 22. } , GridLines -> Automatic, 
 ImageSize -> Large ]
SystemModelPlot[sim, {"ln.l1", "ln.l2", "ln.l3", "ln.l4"}, 
 PlotLabel -> "All Four Compartments" , Frame -> True , 
 FrameLabel -> { "Elapsed time" , "L1, L2, L3, L4" } , 
 GridLines -> Automatic, 
 PlotLegends -> { "L1" , "L2" , "L3" , "L4" } , 
 PlotStyle -> { Red , Green , Blue , Black } , ImageSize -> Large ]
POSTED BY: Sergio Vargas

Change your model as follows:

enter image description here

Then add two lines to the beginning of your .xlsx file:

#1

double currentdata(151,2)

See the attached files. You can also enter the 151 value as a formula by having the spreadsheet calculate the number of rows (I just typed it). Save your file and then do a Save As and choose "Space Delimited Text". This file can be directly loaded in the combiTimeTable.

Your Combi TimeTable should look like this:

enter image description here Regards

Neil

Attachments:
POSTED BY: Neil Singer
Posted 5 years ago

WOW, this does help me, I also have the same problem before and still got no answer on how. THANK YOU

POSTED BY: Yasmin Hussain
Posted 5 years ago
POSTED BY: agilicz

An option to work only with System Modeler would be to follow Neil's solution and use a CombiTimeTable. The idea is that you save your table in a text file, say combitimetable.txt, using the format indicated in the documentation:

https://reference.wolfram.com/system-modeler/libraries/Modelica/Modelica.Blocks.Sources.CombiTimeTable.html

For instance, the contents of your text file can look like

#1
double currentData(11,2)
  0  0.5
  1  1.5
  2  2.5
  3  3.5
  4  4.5
  5  5.5
  6  6.5
  7  7.5
  8  8.5
  9  9.5
  10  10.5

You can then call this table in a component like the following:

model CurrentSourceWithCombiTimeTable
  extends Modelica.Electrical.Analog.Interfaces.OnePort;
  Modelica.Blocks.Sources.CombiTimeTable signalSource(tableOnFile = true, fileName = "/home/user/combitimetable.txt", tableName = "currentData");
equation
  i = signalSource.y[1];
end CurrentSourceWithCombiTimeTable;

in which the value for fileName is the file path of combitimetable.txt. Feel free to add any icon or diagram primitives to the component to make it look as you wish.

POSTED BY: Sergio Vargas
Posted 5 years ago
POSTED BY: agilicz

You can indeed use System Modeler without Mathematica.

POSTED BY: Jan Brugard
Posted 5 years ago
POSTED BY: agilicz

Sergio's suggestion is excellent.

Another option is to save your excel file to a text file (see the format in the help section) and load it in a CombiTimeTable (or one of the other CombiTables) and connect the output to a current source.

You can also load the excel file in Mathematica and save it as a MAT file (which might be easier than text format) and then load it into a CombiTimeTable.

Regards.

Neil

POSTED BY: Neil Singer
POSTED BY: Sergio Vargas
Posted 5 years ago
POSTED BY: agilicz
Posted 5 years ago

Dear Sergio,

Thank you very much, I'll follow your instructions. I learned a lot, tx indeed.

Tx for the kind help, all the best

Andras

POSTED BY: agilicz

If you have both System Modeler and Mathematica open, as you run these lines of code in Mathematica, you'll see the models appearing in the User Classes section of the class browser in System Modeler. When the models are created, they only exist in memory and have not been saved yet. You can either save them with Mathematica or save them with System Modeler.

With Mathematica you do this by using Export:

Export[dest, SystemModel["LinearSystem"]]

where dest is the destination of the MO file, which can be, for instance, a full file path like "/home/user/Desktop/LinearSystem.mo".

With System Modeler you can just select the model in the class browser, right click and select save. Or open the model and save by selecting the save icon at the top left.

POSTED BY: Sergio Vargas
Posted 5 years ago

Dear Sergio,

Thank you very much for the kind help. This is great! Pls. give me some time to explore it, many unknown stuff here for me. So you say these commands will put some files to a directory (where?) and I can open them with System Modeler's Open command, and run it? Which one should I open?

Tx very much for the kind help once again, best regards Andras

POSTED BY: agilicz
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard