Search code examples
packagemodelicaopenmodelica

How to compile and simulate a modelica model that is part of a package with JModelica?


My question is similar to the question of janpeter. I study the ebook by Tiller and try to simulate the example 'Architecture Driven Approach' with OpenModelica and JModelica. I tried the minimal example 'BaseSystem' in OpenModelica and it works fine. But with JModelica version 1.14 I get errors in the compiling process and my script fail. My python script is:

import matplotlib.pyplot as plt
from pymodelica import compile_fmu
from pyfmi import load_fmu

# Variables: modelName, modelFile, extraLibPath
modelName = 'BaseSystem'
modelFile = 'BaseSystem.mo'
extraLibPath = 'C:\Users\Tom\Desktop\Tiller2015a\ModelicaByExample\Architectures'
compilerOption = {'extra_lib_dirs':[extraLibPath]}

# Compile model
fmuName = compile_fmu( modelName, modelFile, compiler_options=compilerOption)

# Load model
model = load_fmu( fmuName)

# Simulate model
res = model.simulate( start_time=0.0, final_time=5.0)

# Extract interesting values
res_w = res['sensor.w']
res_y = res['setpoint.y']
tSim = res['time']

# Visualize results
fig = plt.figure(1)
ax1 = fig.add_subplot(111)
ax2 = ax1.twinx()
ax1.plot(tSim, res_w, 'g-')
ax2.plot(tSim, res_y, 'b-')
ax1.set_xlabel('t (s)')
ax1.set_ylabel('w (???)', color='g')
ax2.set_ylabel('y (???)', color='b')
plt.title('BaseSystem')
plt.legend()
plt.grid(True)
plt.show()

My problem is how to compile and simulate a model that is part of a package?


Solution

  • Thank you Michael Tiller. With your support I found the solution.

    First, the modelName has to be full qualified. Second, as you mentioned, the extraLibPath should end at the top-level directory of the library ModelicaByExample. But then I got errors, that JModelica couldn't find components or declarations which are part of the Modelica Standard Library (MSL).

    So I added the modelicaLibPath to the MSL, but the error messages remained the same. After many attempts, I launched the command line with administrator privileges and any errors were gone.

    Here is the executable python script: BaseSystem.py

    ###  Attention!
    # The script and/or the command line must be
    # started with administrator privileges
    
    import matplotlib.pyplot as plt
    from pymodelica import compile_fmu
    from pyfmi import load_fmu
    
    # Variables: modelName, modelFile, extraLibPath
    modelName = 'Architectures.SensorComparison.Examples.BaseSystem'
    modelFile = ''
    extraLibPath = 'C:\Users\Tom\Desktop\Tiller2015a\ModelicaByExample'
    modelicaLibPath = 'C:\OpenModelica1.9.2\lib\omlibrary\Modelica 3.2.1'
    compileToPath = 'C:\Users\Tom\Desktop\Tiller2015a'
    
    # Set the compiler options
    compilerOptions = {'extra_lib_dirs':[modelicaLibPath, extraLibPath]}
    
    # Compile model
    fmuName = compile_fmu( modelName, modelFile, compiler_options=compilerOptions, compile_to=compileToPath)
    
    # Load model
    model = load_fmu( fmuName)
    
    # Simulate model
    res = model.simulate( start_time=0.0, final_time=5.0)
    
    # Extract interesting values
    res_w = res['sensor.w']
    res_y = res['setpoint.y']
    tSim = res['time']
    
    # Visualize results
    fig = plt.figure(1)
    ax1 = fig.add_subplot(111)
    ax2 = ax1.twinx()
    ax1.plot(tSim, res_w, 'g-')
    ax2.plot(tSim, res_y, 'b-')
    ax1.set_xlabel('t (s)')
    ax1.set_ylabel('sensor.w (rad/s)', color='g')
    ax2.set_ylabel('setpoint.y (rad/s)', color='b')
    plt.title('BaseSystem')
    plt.legend()
    plt.grid(True)
    plt.show()