Message Boards Message Boards

0
|
6417 Views
|
4 Replies
|
0 Total Likes
View groups...
Share
Share this post:

About WSMCreateModel

Dear Colleagues Working with WSMCreate model has giving me some problems. For instance, could you help me to understand why the Mathematica code rises a syntax error in the following statement?

APF = WSMCreateModel[
  "APFModel", {Derivative[1][if][t] == -0.055555555555 if[t] - 
     55.55555555555556 swt[t] vc[t] - 55.55555555555556 Vs[t], 
   Derivative[1][vc][t] == 1000 if[t] swt[t]}, 
  t, {swt[t] \[Element] "Modelica.Blocks.Interfaces.RealInput", 
   Vs[t] \[Element] "Modelica.Blocks.Interfaces.RealInput",
   if[t] \[Element] "Modelica.Blocks.Interfaces.RealOutput",
   vc[t] \[Element] "Modelica.Blocks.Interfaces.RealOutput"}]

I have double check this without finding the possible error. Thanks in advance. Jesus

4 Replies

No, it is case sensitive so creating a model directly in SystemModeler using "If" as a variable name would be ok, but I would highly advice against it since it is easy to mix up with the reserved keyword. You will not be able to create a variable called "If" from Mathematica since If[] is a built-in function in Mathematica and cannot be used as a symbol. (Well, cannot is a strong word, you could always Unprotect the it and remove the built-in function from the session).

In SystemModeler the "if" will be colored to show that it is a reserved keyword:

if coloring

The error you got is the same you would have gotten in SystemModeler when trying to validate the model:

[2] 10:28 Syntax errors in model APFModel 
APFModel(2): No viable alternative near token: RealOutput

The if-statement should be followed by expression, and it throws an error since it is only followed by a semicolon. You can have if-statements in component declarations, so it is not wholly out of place. See for example Modelica.Electrical.Analog.Interfaces.ConditionalHeatPort.

In the future, we will try and make the error message clearer from Mathematica when you are trying to use a reserved keyword as a component name. Hopefully, this will make it easier to diagnose similar problems!

Regards, Patrik

POSTED BY: Patrik Ekenberg

Ooh, yes! So in WSM If and if are the same? Thanks a lot

Jesus

Hello Jesus!

Thank you for reporting this. The syntax error arises since "if" is a reserved keyword in Modelica, denoting if-statements. Thus you cannot have variables named "if" (or other reserved keywords). The easiest work-around is to use another name for your variable. The code below, where I renamed "if", "x" should work.

APF = WSMCreateModel[
  "APFModel", {Derivative[1][x][t] == -0.055555555555 x[t] - 
     55.55555555555556 swt[t] vc[t] - 55.55555555555556 Vs[t], 
   Derivative[1][vc][t] == 1000 x[t] swt[t]}, 
  t, {swt[t] \[Element] "Modelica.Blocks.Interfaces.RealInput", 
   Vs[t] \[Element] "Modelica.Blocks.Interfaces.RealInput", 
   x[t] \[Element] "Modelica.Blocks.Interfaces.RealOutput", 
   vc[t] \[Element] "Modelica.Blocks.Interfaces.RealOutput"}]

Regards, Patrik

POSTED BY: Patrik Ekenberg

Hello Jesus!

Thank you for reporting this. The syntax error arises since "if" is a reserved keyword in Modelica, denoting if-statements. Thus you cannot have variables named "if" (or other reserved keywords). The easiest work-around is to use another name for your variable. The code below, where I renamed "if", "x" should work.

APF = WSMCreateModel[
  "APFModel", {Derivative[1][x][t] == -0.055555555555 x[t] - 
     55.55555555555556 swt[t] vc[t] - 55.55555555555556 Vs[t], 
   Derivative[1][vc][t] == 1000 x[t] swt[t]}, 
  t, {swt[t] \[Element] "Modelica.Blocks.Interfaces.RealInput", 
   Vs[t] \[Element] "Modelica.Blocks.Interfaces.RealInput", 
   x[t] \[Element] "Modelica.Blocks.Interfaces.RealOutput", 
   vc[t] \[Element] "Modelica.Blocks.Interfaces.RealOutput"}]

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