0

Following this question, I'm trying to compile and simulate a Modelica model with JModelica. The model is:

package friction1D

  function coulombFriction
    input Real relVel;
    input Real shearForce;
    input Real normalForce;
    input Real statfricco;
    input Real kinfricco;
    input Real inertia;
    input Real relAcc;
    output Real fricForce;
  algorithm
    if (relVel == 0) and (abs(shearForce - inertia * relAcc) < statfricco * normalForce) then
      fricForce := shearForce - inertia * relAcc;
    else
      fricForce := kinfricco * normalForce * sign(relVel);
    end if;
  end coulombFriction;

  model fricexample_1
    //parameters
    parameter Real kco = 0.3;
    parameter Real sco = 0.4;
    parameter Real nfo = 1.0;
    parameter Real mass = 1;

    Real sfo;
    Real ffo;
    Real x;
    Real v;

  initial equation
    x = 0;
    v = 0;

  algorithm
    sfo := 1 * sin(time);

  equation
    v = der(x);
    mass * der(v) = sfo - ffo;

    ffo = coulombFriction(relVel = v, shearForce = sfo, normalForce = nfo, statfricco = sco, kinfricco = kco, inertia = mass, relAcc = der(v));
  annotation(
      experiment(StartTime = 0, StopTime = 10, Tolerance = 1e-8, Interval = 0.02),
      __OpenModelica_simulationFlags(lv = "LOG_STATS", outputFormat = "mat", s = "dassl"));end fricexample_1;

end friction1D;

and the Python code is:

from pymodelica import compile_fmu
from pyfmi import load_fmu

model_name = 'friction1D'
mofile = 'friction1D.mo'

fmu_name = compile_fmu(model_name, mofile)
sim = load_fmu(fmu_name)

res = sim.simulate(final_time=10)

when I try to summon the results back the time = res['time'] command seems to be working fine, but for al lother variables, for example, vel = res['v'], it returns this error:

---------------------------------------------------------------------------
VariableNotFoundError                     Traceback (most recent call last)
<ipython-input-19-a29270f8b1ab> in <module>()
----> 1 vel = res['v']

C:\Users\foobar\AppData\Roaming\JModelica.org-2.10\install\Python_64\pyfmi\common\algorithm_drivers.pyc in __getitem__(self, key)
    176                 Name of the variable/parameter/constant.
    177         """
--> 178         val_x = self.result_data.get_variable_data(key).x
    179 
    180         if self.result_data.is_variable(key):

C:\Users\foobar\AppData\Roaming\JModelica.org-2.10\install\Python_64\pyfmi\common\io.pyc in get_variable_data(self, name)
   1160             varInd = 0;
   1161         else:
-> 1162             varInd  = self.get_variable_index(name)
   1163 
   1164         dataInd = self.raw['dataInfo'][1][varInd]

C:\Users\foobar\AppData\Roaming\JModelica.org-2.10\install\Python_64\pyfmi\common\io.pyc in get_variable_index(self, name)
    151             else:
    152                 raise VariableNotFoundError("Cannot find variable " +
--> 153                                         name + " in data file.")
    154 
    155 

VariableNotFoundError: Cannot find variable v in data file.

I would appreciate it if you could help me know what the problem is and how I can solve it.

P.S.1. I have posted this question also on the Modelica Language Discord channel.

P.S.2. I think the issue arises because I'm solving a package. If instead a simple model is simulated, it can retrieve the variables.

P.S.3. I think I solved the problem. the line model_name = 'friction1D' needs to be changed to model_name = 'friction1D.fricexample_1'. Basically, it should be <packageName>.<modelName>

Foad S. Farimani
  • 12,396
  • 15
  • 78
  • 193
  • Check model.get_model_variables() for a list of which variables that are exposed in the FMU (and see if 'v' is there) – Christian Winther Oct 03 '19 at 13:26
  • @ChristianWinther tried but got the error `AttributeError: 'FMIResult' object has no attribute 'get_model_variables'`! – Foad S. Farimani Oct 03 '19 at 13:27
  • I tried the `sim.get_model_variables()` insteead of `res.get_model_variables()` and got a cryptic list of variables, e.g., > ), ('_block_jacobian_check_tol', ), – Foad S. Farimani Oct 03 '19 at 13:35
  • I tried the `res.keys()` and it doesn't include any of the variable names I have in the `.mo` file! – Foad S. Farimani Oct 03 '19 at 13:48
  • Hi, sorry do sim.get_model_variables().keys() #this will give you a list of variables that are included in the FMU – Christian Winther Oct 04 '19 at 06:07
  • @ChristianWinther thanks for the comment. I think I found the solution. please see **P.S.3.** in the post above and also the correct code here in [this new question](https://stackoverflow.com/q/58227795/4999991). – Foad S. Farimani Oct 04 '19 at 06:25
  • @ChristianWinther BTW the `sim.get_model_variables().keys()` doesn't show the model variables either. The problem is that I'm compiling the package, not the model. and as mentioned in **P.S.3.** the `model_name = 'friction1D'` command should become `model_name = 'friction1D.fricexample_1'` – Foad S. Farimani Oct 04 '19 at 07:12
  • 1
    Hi, it does show the model variables, it is just that you havn't compiled a model that contains any (as you've noted in P.S.3) – Christian Winther Oct 07 '19 at 08:05

0 Answers0