Message Boards Message Boards

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

[?] Use Wolfram language functions into SystemModeler

Hi! I have a dynamical nonlinear model that uses functions: Mod and Quotient, and that I want to simulate in SystemModeler so the question is how to import these kind of functions inside SystemModeler.

I am almost sure this cannot be done but it would nice that someone would proof me wrong!

Regards

Jesus Rico

6 Replies

Neil The desired help came very fast, thanks!. I have a systems that receives very complex inputs that make use of crazy functions such as Mod and Quotient. I did not know there are name differences such quotient and div between mathematica and SystemModeler. Actually, your nonsensical examples make a lot of sense and really helped.

Thanks again.

Regards

Jesús

Jesus,

Glad to help. Your original post seemed so negative! You really were expecting the worst!

You (or the moderator) should add this thread to the SystemModeler group. Also, the PDF modelica manual should be very helpful in your case. Although the differences are not huge, the current version of WSM uses modelica 3.2. Future versions should support a later version (3.3 or 3.4) (hopefully). So the 3.2 version is most relevant now.

Regards

Neil

POSTED BY: Neil Singer

Thanks

SystemModeler has help me a lot in many ways but going from doing simulation in Mathematica to simulating in SystemModer has not been always easy. Mathematic has a function for virtually everything one requires in simulating dynamical systems and I am skeptical the catalog Modelica functions is as long. In this case, for example, I noticed that there are differences between Quotient[] and div(), Quotient gives the integer quotient of m and n and div seem to be a classical division. Am I right?

Regards

Jesus

Quotient gives the integer quotient of m and n and div seem to be a classical division. Am I right?

I don't think this is correct. They should be mathematically equivalent. From Mathematica:

Quotient[m,n] gives the integer quotient of m and n.

From Modelica:

div(x,y) Returns the algebraic quotient x/y with any fractional part discarded (also known as truncation toward zero). [Note: this is defined for / in C99; in C89 the result for negative numbers is implementation-defined, so the standard function div() must be used.]. Result and arguments shall have type Real or Integer. If either of the arguments is Real the result is Real otherwise Integer.

I think they get the same answer given the same m and n. This is supported by the fact that Mathematica translated Quotient to div when it did the conversion.

As to your use of WSM, do you generally create your models in Mathematica and export them to WSM? or do you create your models in WSM directly? What type of systems are you modeling (ie Powersystems with switching)?

I have found WSM to be far better at modeling dynamic systems than trying to do it in Mathematica. You may just need to refine your workflow to take advantage of the tools. Feel free to contact me directly at "neils" at the domain in my profile.

Regards

POSTED BY: Neil Singer

Jesus,

It works well. By saying you want to "import" them into SystemModeler I assume you mean from Mathematica. Here is a simple example from the documentation for taking an equation and creating a model:

mmodel = WSMCreateModel[
   "DiffEq2", {x''[t] + 1/10 x'[t] + Sin[x[t]] == 1/2 Cos[t],  x[0] == 0, x'[0] == 0}, t];
WSMModelData[mmodel, "ModelicaText"]

This produces a simple Modelica model:

model DiffEq2
  Real x;
  Real derx129;
initial equation
  x = 0;
  der(x) = 0;
equation
  sin(x) + 1 / 10 * der(x) + der(derx129) = 1 / 2 * cos(time);
  derx129 = der(x);
end DiffEq2;

If you add a (in this case, nonsensical) Mod and Quotient you get:

mmodel = WSMCreateModel[
   "DiffEq4", {x''[t] + 1/10 x'[t] + Mod[Sin[x[t]], 5] == 
     Quotient[1/2 Cos[t], 2], x[0] == 0, x'[0] == 0}, t];
WSMModelData[mmodel, "ModelicaText"]

you get the correct modelica code:

model DiffEq4
  Real x;
  Real derx131;
initial equation
  x = 0;
  der(x) = 0;
equation
  mod(sin(x), 5) + 1 / 10 * der(x) + der(derx131) = div(1 / 2 * cos(time), 2);
  derx131 = der(x);
end DiffEq4;

Note that the modelica function for Mod is mod() and the modelica function for Quotient is div(). You can see all the modelica functions in the modelica manual in PDF here

If on the other hand you meant "how do you use these functions in a modelica model?", the answer is to go to text mode and type mod() and div() directly. Modelica has a very rich and complete language so you can do just about anything,,,

You can also create a model in WSM, query the equations from Mathematica, alter the equations to add your nonlinear parts and send it back to WSM as a new nonlinear model.

I hope this helps.

Regards,

Neil

POSTED BY: Neil Singer

Neil The desired help came very fast, thanks!. I have a systems that receives very complex inputs that make use of crazy functions such as Mod and Quotient. I did not know there are name differences such quotient and div between mathematica and SystemModeler. Actually, your nonsensical examples make a lot of sense and really helped.

Thanks again.

Regards

Jesús

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