Message Boards Message Boards

0
|
6760 Views
|
6 Replies
|
0 Total Likes
View groups...
Share
Share this post:

How to change the initial conditions in a Modelica model within Mathematica

Posted 8 years ago

Hello

I am new to SystemModeler and in order to learn it I have decided to recode (in text format and using available blocks) my old fortran models. Using Modelica, I had not problem to recode some of the models and run them in WSM and Mathematica. My problem starts when I try to change the initial conditions using the following command:

model=
 WSMSimulate[mmodel, {0, 10}, WSMInitialValues -> {"n" -> 2 10^15}]

WSM returns the following error msg:

WSMSimulate::val: The variables {n} were not recognized. >>

I am not sure what is going on since WSMPlot[model, {"n"}] works just fine. What am I missing?

The initial conditions in the Modelica model are set as:

Real n(start = n0) "The electron";

Real m(start = n0) "The trapped electron";

Real p(start = p0) "The hole population densities";

Many thanks

Ed

PS. I have the same problem when I use the available blocks (integrator, gain, add, product, ...).

POSTED BY: Eduardo Mendes
6 Replies
Posted 8 years ago

Hi Patrick

Thank you ever so much. I will certainly watch the videos.

I have another question but I think it is for the best if I start new thread.

Regards

Ed

POSTED BY: Eduardo Mendes

If you want to change the initial values for states of built in blocks, there will often be associated parameters that describe these initial values. Since these are inside components, you access these parameters by specifying the component name followed by a dot and then the parameter name, like so:

WSMSimulate[mmodel, {0, 10}, WSMParameterValues -> {"Variable_n.y_start" -> 2 10^15}]

I think you could potentially also make use of WSMInitialValues, but I would advice against it since many of these built in blocks have different ways that they can handle the initial state parameter. The integrator component has a parameter called initType that will change this behavior, and then you need to have specified it through the parameter, rather than enforcing the initial state with WSMInitialValue.

You can find a list of the parameters that you can change by highlighting the component in SystemModeler. Alternatively you can run:

WSMModelData["Integrator", "ParameterNames"]
[Out]: {k, y_start}

in Mathematica. (Note that the short hand name, "Integrator" will only work if there is only one component loaded with that name, else you will have to specify the full path "Modelica.Blocks.Continuous.Integrator")

You also mentioned gain, product and add, but since they have not states, their initial values cannot be set and rather are determined by the other states in your system. Their parameters, like the gain constant, can of course be set through Mathematica with WSMParameterValues.

I don't fully understand what you mean by connecting to a line, are you referring to the following?

connection line

The dot/junction point is there to signal that the value being transmitted through the connection is the same for all the connections extending from the junction point. It is automatically displayed when two connection lines that share a common port cross each other. The connections are still being made from each of the ports, not to the line itself.

Similarly, a gap is displayed when two connection lines that does not share a common port crosses each other, like so:

gap line

Regards, Patrik

POSTED BY: Patrik Ekenberg
Posted 8 years ago

Hi Patrik

Thanks again. Yes, that was what I meant by the issue with the Connector line. So it seems that I have no choice but to connect all the way to a port.

One final question, it seems that I always need WSM running with the model I want to use within WM. Is that the case?

Many thanks

Regards

Ed

POSTED BY: Eduardo Mendes

No problem!

No, you can load the model directly from Mathematica using the Import function:

Import["PhotoConductor_Block.mo", "ModelicaModel"]

You need to have called Needs["WSMLink`"] before that so that "ModelicaModel" is a recognized format. You don't have to start SystemModeler though. Note that if you don't have the file in one of the folder specified in $Path (in Mathematica) you will have to give the absolute path to your model file, I omitted that in the expression above out of simplicity.

Don't hesitate if you encounter any problems later.

If you are interested, here is a quick video introduction to using SystemModeler with Mathematica: https://www.wolfram.com/training/videos/ENG022/ . More are available here: https://www.wolfram.com/training/courses/system-modeler/

Regards, Patrik

POSTED BY: Patrik Ekenberg
Posted 8 years ago

Hi Patrik

Thank you ever so much. It worked just like you said. I made a terrible mistake when calling the function in WSM. BTW, I am running WM 10.4 and WSM 4.2.

Since I have a similar problem with the diagram (block) implementation, I wonder whether you could help me again. In this case I cannot change the initial value.

I have attached the WM and WSM files.

Thanks again.

Regards

Ed

PS. BTW, could you tell me how to use the Connector line to connect to a line? You will see what I mean when opening my WSM file. Thanks.

Attachments:
POSTED BY: Eduardo Mendes

Hello Eduardo,

I am not able to reproduce your problem. What version of SystemModeler are you using? Would you be willing to share the model that you have tried, or could you show what Mathematica code you used to try and set the initial values of a block?

I used the following model in SystemModeler:

model Model1
  Real n(start = n0)"The electron";
  parameter Real n0=0;
equation
  der(n)=0;
end Model1;

And called:

WSMSimulate["Model1", {0, 10}, WSMInitialValues -> {"n" -> 2 10^15}]

In Mathematica, and I got the expected result. An alternative you could try is to change the parameter "n0" that you have set to correspond to the initial value of n. You can do this with the WSMParameterValues option:

WSMSimulate["Model1", {0, 10}, WSMParameterValues -> {"n0" -> 2 10^15}]

Regards, Patrik

POSTED BY: Patrik Ekenberg
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