Search code examples
modelica

Modelica parameters as inputs


Consider the following simple package.

package Test

  connector Param
    parameter Real k = 1.5;
  end Param;

  model Component
    input Param p;
    Real x;
  equation 
    der(x) = p.k;
  end Component;

  model System
    Param p;
    Component c;
  equation 
    connect(p, c.p);
  end System;

end Test;

This works fine but as soon as I change System.p.k in the simulation, I get the following error:

abs(p.k-c.p.k) <= 0.0
The following error was detected at time: 0
Parameters in connected connectors must be equal
Error: Failed to start model.

Somehow the variables p.k and c.p.k are not aliasing each other. Hence, when I change only p.k, a discrepancy is detected which is not allowed since both have to be equal due to the equations induced by connect(p, c.p).

How can I use parameters as inputs properly and avoid these effects?


Solution

  • Which version of OpenModelica are you using? For me this doesn't happen. The simulation works fine:

    adrpo@ida-liu050 ~/dev/OpenModelica/build/bin/
    $ ./omc TestConnectionParameter.mos
    true
    ""
    record SimulationResult
        resultFile = "c:/bin/cygwin/home/adrpo/dev/OpenModelica/build/bin/media/TestConnectionParameter.System_res.mat",
        simulationOptions = "startTime = 0.0, stopTime = 1.0, numberOfIntervals = 500, tolerance = 1e-006, method = 'dassl', fileNamePrefix = 'TestConnectionParameter.System', options = '', outputFormat = 'mat', variableFilter = '.*', cflags = '', simflags = ''",
        messages = "",
        timeFrontend = 0.01857038093533281,
        timeBackend = 0.009237455657633623,
        timeSimCode = 0.002007941686540131,
        timeTemplates = 0.06294835594000042,
        timeCompile = 2.89228755603547,
        timeSimulation = 0.463245543628269,
        timeTotal = 3.4489112421252
    end SimulationResult;
    "Warning: The initial conditions are not fully specified. Use +d=initialization for more information.
    "
    true
    1.5
    1.5
    1.5
    1.5
    1.5
    1.5
    

    I'm using file: TestConnectionParameter.mo:

    package TestConnectionParameter
    
      connector Param
        parameter Real k = 1.5;
      end Param;
    
      model Component
        input Param p;
        Real x;
      equation 
        der(x) = p.k;
      end Component;
    
      model System
        Param p;
        Component c;
      equation 
        connect(p, c.p);
      end System;
    
    end TestConnectionParameter;
    

    and the script: TestConnectionParameter.mos

    loadFile("TestConnectionParameter.mo"); getErrorString();
    simulate(TestConnectionParameter.System); getErrorString();
    plot({c.x, p.k, c.p.k});
    val(p.k, 0);
    val(p.k, 0.5);
    val(p.k, 1);
    val(c.p.k, 0);
    val(c.p.k, 0.5);
    val(c.p.k, 1);
    

    Getting: enter image description here