How to link Wolfram system modeler with Mathematica ?

Hello. I have build my own library in WSM. And I want now to launch simulations from Mathematica. But it does not work for my libraries, despite it works with the examples given in the Wolfram documentation.
Here are the steps I followed unsuccessfully:

  • I load my library in WSM, I load my model (named “Model1”). I run a simulation. All is OK

  • Then I launch Mathematica

  • And I use the following instruction :

    sim1 = SystemModelSimulate[“Model1”]

It says the Model1 was not found
I also tried by putting the complete path instead of only “Model1”

I probably forget to put few things, but which ones ?

Thanks by advance for the help.
Yvan

Hi Yvan,

In order to load your library in Mathematica you can run

Import["full/path/to/your/library.mo"]

The expectation is that, if you have the same version of Mathematica and System Modeler, then loading the library in System Modeler would be enough for you to access it in Mathematica. This is also a necessary condition for the two to talk to each other.

Sergio

Hi Sergio. Thank you very much for your help. Anyway,
it seems that it loads only my “Model1.mo” drawing, that is the single .mo file of my schematic.
But my question is : How can I do to load all the libraries that I have made. It is a lot of files and various directories …

myIssues.nb (28 KB)

Hi Yvan,

By looking at the error messages seems like Vacuum is your full library? I presume you have a package.mo file associated with it, so if you import that one, you’ll load the whole library.

Sergio

Ok thanks. It seemed to work when I imported the package .mo

But now I think I face another kind of difficulties. My model runs correctly with WSM, but seems not to work properly if it is launched from Mathematica. Seeing the error message, there is a problem of inner / outer. Because I use it in my model.

it seems to be another problem.
So, I will try to remove inner and outer, just to confirm that the initial problem of this post is solved.

I am optimistic and thank you again Sergio for your fast and powerful answer.

myIssues.nb (92.6 KB)

If you want assistance, we would need more details to help with the inner/outer problem. If you are using different versions of Mathematica and System Modeler, you can experience different outcomes when running simulations.

I am using both 12 version for Mathematica an WSM.
But I think I will avoid to use inner outer. It seems to be quite complex to understand. Initially, I only wanted global variable for the whole schematic. But that is not a good idea. The concept of global variable doesn’t really exists in Modelica. I must modify my libraries. But I am now pretty sure that you have fixed my problem : I must load both my schematic, and the package.mo of the library I have made. I hope I will be able to work on it again soon. That is not possible for me for the moment.

Help … ?

.. I come back to my problem : I still have a model that is OK with WSM, but I don’t know how to launch a simulation from Mathematica. It is a basic problem , I know, but if anyone could help.

Thanks

Here is the total Model

After loading the sma file, opening the model “_002596_ed00_total._002596_ed00” in System Modeler version 12.3, and validating it, I get the errors

[18] 16:54 Validation of model _002596_ed00_total._002596_ed00 
Error: _002596_ed00_total.Vacmod.Components.qvcst_ [5:3-5:3] Missing inner could not be created. Outer element globalInfo1 does not have a corresponding inner declaration but an element with the same name already exists in _002596_ed00.
Error: _002596_ed00_total.Vacmod.Components.Volume_ [19:3-19:3] Missing inner could not be created. Outer element globalInfo1 does not have a corresponding inner declaration but an element with the same name already exists in _002596_ed00.
Validation of model _002596_ed00_total._002596_ed00 completed with 2 errors.

When I add the inner prefix to

Vacmod.GlobalInfo globalInfo1

as in

inner Vacmod.GlobalInfo globalInfo1

I am able to simulate it in both System Modeler and in Mathematica. In the latter you can do this with

SystemModelSimulate["_002596_ed00_total._002596_ed00", 10]

, where 10 is just there to provide a simulation time. Could you try adding the innner prefix and checking if that fixes it for you?

Sergio

Hi Sergio
Thank you so much to come back to my rescue again. yes !, it seems to compile now. great !
I have anyway questions, and would be very grateful if you can help me again:

  • Is there a simple way to simulate ? Because for the moment, I import a file like this,
    Import[“C:\Users\Yvan\Google \Drive\CHRONOS\_002596_ed01Total.sma”]
    → It gives me a mini-drawing, and I copy-paste the drawing inside the [ ] of SystemModelSimulate[ ].
    Thanks by advance
    Yvan

(I think I have corrupted my previous model. Here is the new one just in case …)
https://drive.google.com/file/d/1BBYUDM_nxtCda8VcF4qa1mQh4Eb6BGqo/view?usp=sharing

@Yvan Abbe Please read the rules: http://wolfr.am/READ-1ST
The rules state that attaching files to posts and comments is preferred than providing external links. That is because external links expire eventually. Also your post contains several missing notebooks and this breaks the flow of the post, please try to attach them again.
Thank you.

@Sergio,
So it works when I have this code (I type manually “inner” in the text of the schematic.

model _002596_ed01
  inner Vacmod.GlobalInfo globalInfo1 annotation(Placement(visible = true, transformation(origin = {-40, 27.009}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
  Vacmod.Components.Volume_ volume_1(V = 0.01) annotation(Placement(visible = true, transformation(origin = {0, 20}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
  Vacmod.Components.qvcst_ qvcst_1(qvstd = 0.05) annotation(Placement(visible = true, transformation(origin = {0, -25}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
equation
  connect(volume_1.a, qvcst_1.a) annotation(Line(visible = true, origin = {0, -2.5}, points = {{0, 12.5}, {0, -12.5}}, color = {0, 0, 255}));
  annotation(Diagram(coordinateSystem(extent = {{-500, -90}, {500, 90}}, preserveAspectRatio = true, initialScale = 0.1, grid = {5, 5})), Icon(coordinateSystem(extent = {{-100, -100}, {100, 100}}, preserveAspectRatio = true, initialScale = 0.1, grid = {10, 10}), graphics = {Rectangle(visible = true, lineColor = {0, 114, 195}, fillColor = {255, 255, 255}, extent = {{-100, -100}, {100, 100}}, radius = 25), Text(visible = true, textColor = {64, 64, 64}, extent = {{-150, 110}, {150, 150}}, textString = "%name")}));
end _002596_ed01;

But I must add it manually in the text of the schematic. I tried to add it in my component GlobalInfo, but it failed.

model GlobalInfo
  Real T = 293.15 "Température (K)";
  Real Air = 100 "en %";
  Real H2 = 0 "en %";
  Real He = 0 "en %";
  Real Xe = 0 "en %";
  Real M = 0.01 * (Air * 0.029 + H2 * 0.002 + He * 0.004 + Xe * 0.1313) "Masse en kg";
  Real Visc = 0.01 * (1.71e-5 * Air + 8.4e-6 * H2 + 1.95e-5 * He + 2.12e-5 * Xe) "Viscosité (Pa.s)";
  Real G = 0.01 * (1.4 * (Air + H2) + 1.66 * (He + Xe)) "Coefficient adiabatique";
  Real R = 8.314;
  annotation(Documentation(info = "<html>
<p> MUST BE placed on each schematic. Contains gas data and temperature </em> </p>
</html>", revisions = "<html>
<ul>

<li><em> 2020   </em>
     by Yvan Abbe

initially implemented

     </li>
</ul>
</html>"), Icon(graphics = {Ellipse(fillColor = {255, 255, 255}, fillPattern = FillPattern.Solid, extent = {{-100, 100}, {100, -100}}, endAngle = 360), Text(origin = {18, -12}, fillColor = {255, 255, 255}, fillPattern = FillPattern.Solid, extent = {{-80, 60}, {42, -28}}, textString = "Gas")}, coordinateSystem(initialScale = 0.1)));
end GlobalInfo;

Thanks if you still have time to answer. I think after, I can manage by myslef.

Yvan

inner is added when you declare the component, not when you define it. This means you just have to add inner in the Vacmod.GlobalInfo globalInfo1 declaration inside _002596_ed01. For more details, you can look at the specification: link.

As for the use of SystemModelSimulate, you can use either the SystemModel object that you get when you import the model, or the model name, as I did in the previous answer. More details can be found in the documentation page: link.

Thanks a lot. I have to read again, make tests, and work again,

→ but your advices made me progress a lot.