Message Boards Message Boards

GROUPS:

[?] Write a script for Wolfram SystemModeler to run several simulations?

Posted 9 months ago
1293 Views
|
6 Replies
|
4 Total Likes
|

I want to run around 100 simulations with my model changing two parameters f and TLoad and track the changes on the phase currents currentSensor.i[1] etc.

Now I'm stuck with the documentation on the Wolfram website because there is no definite explanation on how to use scripting with the SystemModeler. I found for example this link on the Wolfram site with some code but no explanation in which commandline I should use it.

I downloaded the WolframScript program and tried to open my model with wolframscript -file SMPM_VoltageSource_Inverter.mo but it says ToExpression::sntx: Invalid syntax in or before ... eventhough my model simulates totally fine and without any errors in the SimulationCenter.

Can someone explain to me:

  • Is it possible to write scripts ?

If Yes:

  • How can I simulate my model?
  • How can I do a parameter sweep of f and TLoad? Is it as described in the link?
  • Is it possible to export data of currentSensor.i[1] as a csv-file? And how to?

Thank you for any help!

6 Replies

David,

Look at the Documentation for SystemsModelSimulate. It does exactly what you want. You can control a WSM model and change parameters and control simulations. Your CSV file can be written using Export[].

Regards

Neil

Posted 8 months ago

Thank you for the reply Neil. I already had a look into the documetation and that's the reason for my post, because I don't understand in which program I can use those commands. So I'm kindly asking again: With which program can I use the commands in the documentation to write scripts for the SystemModeler?

Those commands are run from Mathematica (MMA). The results can be sent back from WSM to MMA.

Regards

Posted 8 months ago

Hi David,

You can write your wolfram language code in a wl file and run it with wolframscript, as in

 wolframscript -file Desktop/test.wl

, where I am placing my test.wl file in the Desktop folder.

In this test.wl, you can use the wolfram language functionality related to modelling, as you desire. You can, for instance, use SystemModelSimulate to run your simulations (see documentation here). Then, you can store whichever results you want in an array and export it as a csv with

Export["myresults.csv", results, "CSV"]

You can also indicate a full path for the destination of the file. Unfortunately, I cannot see the link you mention, but a simple example of the content of the wl file could be

model = "Modelica.Electrical.Analog.Examples.Rectifier";
sim = SystemModelSimulate[model, <|"ParameterValues"->{"Vknee"->Range[10,90,10],"f"->Range[51,59]}|>];
results = Through[sim["iAC[1]", 0.01]];
Export["myresults.csv", results, "CSV"]

Here, we run a simulation for each pair of values of two parameters of a model for a rectifier. Then ask for the value of a variable (a current, in this case), at time 0.01. Those values are then exported. I hope this helps.

Posted 8 months ago

Thank you so much Sergio, this is a great help!

But can you tell me how I can export all values of the variable iAC[1] of each simulation and not only at the time point t or in this example t=0.01?

I tried it with a for-loop like this:

For[i=0, i<=1, i+=0.1, results[i] = Through[sim["iAC[1]", i]]];

But apperantly the Wolfram language is a bit different than C/C++ and this doesn't work...

Posted 8 months ago

I'm glad this helps.

These simulation objects accept lists as well, so you can ask for, say, in this case, the values at times 0.01, 0.02, ..., 0.08 with

results = Through[sim["iAC[1]", Range[0.01, 0.08, 0.01]]];
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