Search code examples
modelicaopenmodelica

How to use a component from some library


How can you use a component from some library (other than Modelica) and run the model from an OM Shell? For example,

model myModel
   Modelica.Electrical.Analog.Sources.ConstantVoltage cv(V=9) .... // Standard component
   SomeLibrary.Components.SomeComponent myComponent ....           // Some specific component
...
end myModel

If I want to run this model from an OM Shell by using loadFile("mymodel.mo") command, I typically get a message that says failed to build model because it cannot find "SomeLibrary".

I want to write the command to load the model in a script (.mos) and invoke the script with omc from a .bat file.

Thank you!


Solution

  • Either add a uses-annotation to the model (should work in any tool):

    model myModel
       Modelica.Electrical.Analog.Sources.ConstantVoltage cv(V=9) .... // Standard component
       SomeLibrary.Components.SomeComponent myComponent ....           // Some specific component
    ...
       annotation(uses(SomeLibrary(version="1.0.0")));
    end myModel;
    

    Or in your mos-script:

    loadFile("myModel.mo");getErrorString();
    loadModel(SomeLibrary);getErrorString();
    loadModel(SomeLibrary, {"1.0.0"});getErrorString(); // Or with a specific version
    

    Or use OpenModelica 1.19.0 or later (it should load the library automatically if it is on the MODELICAPATH).

    If the library is not installed either use OMEdit to install the package, or from OMShell/mos-script:

    updatePackageIndex();
    installPackage(SomeLibrary);
    installPackage(SomeLibrary, "1.0.0"); // Or with a specific version