Message Boards Message Boards

0
|
11539 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 3 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

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 3 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
Posted 3 years ago

Dear Sergio, and all,

Tx very much, I'll check it out. For safety however I've uploaded my files, so communication is easier what I'm talking about. My model is RCGrid.mo. The tableCurrent component is located in the red square in the model. The data source is currently a list, but I would like to assign my external RCCurrentSource.xlsx data source the easiest way to it. (The list data and the Excel data are identical.)

Tx for all your nice help, best regards Andras

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 3 years ago

Dear Sergio and Neil,

Tx very much for the kind reply. Just one more question: can I do it directly in System Modeler without Mathematica, or I have to use also Mathematica?

Tx very much in advance, best regards.

Andras

POSTED BY: agilicz

You can indeed use System Modeler without Mathematica.

POSTED BY: Jan Brugard
Posted 3 years ago

Dear Jan,

Yes, but how I do it? E.g. I open my RC System Modeler model, I click on the tableCurrent component, and under Parameters at the bottom I see table[:;:] . I suppose this is the place, where I could specify my external input, but I don't know, how. So far I was able to specify here a list only, which worked fine, but not an external Excel file (which would be more convenient for me.) So this is my question: is it possible to specify here an external Excel file as a times series data source for tableCurrent?

Tx for the kind advice in advance, best regards

Andras

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

ConnectSystemModelComponents, CreateSystemModel, SystemModel and SetSystemModel allow the possibility of providing parameter values and initial values in the process of model creation.

You can, for instance, make a copy of the Modelica.Electrical.Analog.Sources.TableCurrent component and give a value for the table parameter in a single step. Let's say you have already imported the two column excel table into Mathematica in the symbol currentData, which for the purpose of this example I am just going to write as a Table:

currentData = Table[{k, k}, {k, 0, 10}];

Then with

SystemModel[
 "Modelica.Electrical.Analog.Sources.TableCurrent",
 <|
  "ModelName" -> "ExampleOfTableCurrent",
  "ParameterValues" -> {"table" -> currentData}
  |>
 ]

you create a copy of the TableCurrent component with the name "ExampleOfTableCurrent" and with currentData as the value for the table parameter. Now you can use this component in any other model as you wish.

POSTED BY: Sergio Vargas
Posted 3 years ago

Dear Sergio,

May I have another question? I have an RC electrical network System Modeler model, where in one of the nodes there is a tableCurrent component. How can I hook a table to this component as an external Excel file containing the time series data for this component? I only could specify it by typing in the time series data as a list in the table parameters input of the component, which is not only tedious, but also not very elegant. I could not find any hint in the documentation as well.

Tx very much for the kind help in advance, best regards Andras

POSTED BY: agilicz
Posted 3 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 3 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

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
Posted 3 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
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract