2

I am using the Dimensions model for placing parameters of the system that I use in many different models and call them by using extend, instead of declaring them again for each model. This is a simple example, but in reality, I have the way more of them.

A simple model with the structure I have:

package Main

  model Dimensions
    final parameter Modelica.SIunits.Length x = 10;
    final parameter Modelica.SIunits.Length y = 5;
  end Dimensions;

  package Test_env
    extends Main.Dimensions;

    model Test_model
      Real z;
    equation
      z = x + y;
    end Test_model;

  end Test_env;
end Main;

If I run this example in OMEdit it works without any problem. However, if I run it in OMShell or OMPython / OMCSessionZMQ it doesn't work.

Q - maybe I am using the extends clause incorrectly? If so, what would be the alternative of declaring parameters once and reusing them in other models?

This is what I get in OMShell:

>> loadFile("D:/1.Modelica/Simulations/Main.mo")
true

>> getClassNames()
{Main}

>> getClassNames(Main)
{Dimensions,Test_env}

>> getClassNames(Main.Test_env)
{Test_model}

>> simulate(Main.Test_env.Test_model, startTime=0, stopTime=1, numberOfIntervals=500, tolerance=1e-4, method="dassl", outputFormat="mat"); getErrorString()
record SimulationResult
    resultFile = "",
    simulationOptions = "startTime = 0.0, stopTime = 1.0, numberOfIntervals = 500, tolerance = 0.0001, method = 'dassl', fileNamePrefix = 'Main.Test_env.Test_model', options = '', outputFormat = 'mat', variableFilter = '.*', cflags = '', simflags = ''",
    messages = "Failed to build model: Main.Test_env.Test_model",
    timeFrontend = 0.0110966,
    timeBackend = 0.0,
    timeSimCode = 0.0,
    timeTemplates = 0.0,
    timeCompile = 0.0,
    timeSimulation = 0.0,
    timeTotal = 0.0111225
end SimulationResult;
"[D:/1.Modelica/Simulations/Main.mo:3:5-3:45:writable] Error: Class Modelica.SIunits.Length not found in scope Main.Dimensions.
[D:/1.Modelica/Simulations/Main.mo:1:1-18:9:writable] Error: Class Test_env.Test_model not found in scope Main.
Error: Class Main.Test_env.Test_model not found in scope .
Error: Error occurred while flattening model Main.Test_env.Test_model
"

And this is from OMPython / OMCSessionZMQ:

omc.sendExpression('simulate(Main.Test_env.Test_model, stopTime=1.0)')
---------------------------------------------------------------------------
{'resultFile': '',
 'simulationOptions': "startTime = 0.0, stopTime = 1.0, numberOfIntervals = 500, tolerance = 1e-006, method = 'dassl', fileNamePrefix = 'Main.Test_env.Test_model', options = '', outputFormat = 'mat', variableFilter = '.*', cflags = '', simflags = ''",
 'messages': 'Failed to build model: Main.Test_env.Test_model',
 'timeFrontend': 0.0018766,
 'timeBackend': 0.0,
 'timeSimCode': 0.0,
 'timeTemplates': 0.0,
 'timeCompile': 0.0,
 'timeSimulation': 0.0,
 'timeTotal': 0.0018919}
Tomillo
  • 157
  • 7
  • 2
    You need to load Modelica library: loadModel(Modelica); before loadFile. Or add uses annotation to your package. – Adrian Pop Dec 14 '19 at 19:18
  • 2
    Is strange to extend a package with a model. Do the extends in Test_model. – Adrian Pop Dec 14 '19 at 19:20
  • 2
    @Adrian. Strange is an understatement – it’s not allowed by the Modelica Spec that packages extend from models. See chapter *7.1.3 Restrictions on the Kind of Base Class* in the Modelica Spec 3.4. – marco Dec 16 '19 at 09:08
  • As a newbie in Modelica I found it convenient, so I did in this way. Nevertheless, there was a question possed if I am using it correctly, so thank you for the answer. – Tomillo Dec 16 '19 at 12:34

2 Answers2

1

To summarise the answers given via comments:

Use extends inside your model which will be extended.

 package Test_env
    model Test_model
      Real z;
      extends Main.Dimensions;
    equation
      z = x + y;
    end Test_model;
  end Test_env;

If all your models need the same fixed parameters it is still a good practise to add the extend to every model, so everybody knows where the variables come from.

Also compare with Modelica.Constants to see how the Modelica Standard Library defines constants. I used this to create the completed example:

package Main

  model Dimensions
    final constant Modelica.SIunits.Length x = 10;
    final constant Modelica.SIunits.Length y = 5;
  end Dimensions;

  package Test_env
    import Dim = Main.Dimensions;

    model Test_model
      Real z;
    equation
      z = Dim.x + Dim.y;
    end Test_model;

  end Test_env;
end Main;

And if you use something from a different package (here Modelica.SIunits.Length) you need to load the package. That's what your errors say with

Error: Class Modelica.SIunits.Length not found in scope Main.Dimensions.

In OpenModelica Modelica is already loaded on startup, so use loadModel(Modelica) or loadFile(...) in OMShell.

>> loadModel(Modelica)
true

>> loadFile("Path/To/Main.mo")
true

>> simulate(Main.Test_env.Test_model, startTime=0, stopTime=1, numberOfIntervals=500, tolerance=1e-4, method="dassl", outputFormat="mat")
record SimulationResult
    resultFile = "C:/Users/USERNAME/AppData/Local/Temp/OpenModelica/Main.Test_env.Test_model_res.mat",
    simulationOptions = "startTime = 0.0, stopTime = 1.0, numberOfIntervals = 500, tolerance = 0.0001, method = 'dassl', fileNamePrefix = 'Main.Test_env.Test_model', options = '', outputFormat = 'mat', variableFilter = '.*', cflags = '', simflags = ''",
    messages = "LOG_SUCCESS       | info    | The initialization finished successfully without homotopy method.
LOG_SUCCESS       | info    | The simulation finished successfully.
",
    timeFrontend = 0.3193980510936645,
    timeBackend = 0.00467019998960375,
    timeSimCode = 0.001078686094233897,
    timeTemplates = 0.02625684206983937,
    timeCompile = 9.15578961474681,
    timeSimulation = 0.2440117147112652,
    timeTotal = 9.751522705140404
end SimulationResult;
>> 
AnHeuermann
  • 761
  • 3
  • 14
0

Additional information: The error "Failed to build model" also appears in OMShell when simulating a model after an OMShell crash in Windows. In that case, restarting the computer only solves the issue (even restarting OMShell doesn't work).