Search code examples
openmodelica

Transfer simulation outputs as starting values for another simulation


I'm working on simulating a transient system. To achieve this, I must run the simulation with constant parameters (fluid flow) until it reaches a steady state. After that, I manually input these values for each parameter in the transient model. Is there a method to automatically transfer the value of variables from the steady state analysis into the transient analysis as initial values ?

I made an attempt to duplicate the information seen in https://github.com/OpenModelica/OpenModelica/issues/11787. failing to succeed, I selected the file for the starting equation system instead of the initialization technique, but it didn't work.

open modelica version :

Connected to OpenModelica v1.14.1 (64-bit)

Connected to OMSimulator v2.1.0-dev-147-g36ec2c7-mingw

package version : 3.2.3


Solution

  • Let's look at an example with the classic Lotka Volterra example. We want to simulate the population of foxes and rabbits for 10 time units and then start a new simulation with the start values being the previous simulation result.

    LotkaVolterra.mo

    model LotkaVolterra
      parameter Real g_r =0.04 "Natural growth rate for rabbits";
      parameter Real d_rf=0.0005 "Death rate of rabbits due to foxes";
      parameter Real d_f =0.09 "Natural deathrate for foxes";
      parameter Real g_fr=0.1 "Efficency in growing foxes from rabbits";
      Real rabbits(start=700) "Rabbits,(R) with start population 700";
      Real foxes(start=10) "Foxes,(F) with start population 10";
    equation
      der(rabbits) = g_r*rabbits - d_rf*rabbits*foxes;
      der(foxes) = g_fr*d_rf*rabbits*foxes -d_f*foxes;
    end LotkaVolterra;
    

    First we simulate LotkaVoltera for 10.0 seconds:

    loadFile("LotkaVolterra.mo"); getErrorString();
    
    simulate(LotkaVolterra, stopTime = 10.0);
    val(rabbits, 10.0);   // 1004.1042956221384
    val(foxes, 10.0);     // 6.1928347062092755
    

    And write the resulting values of ``rabbitsandfoxes` into a CSV file:

    inputs.csv

    time, rabbits_0, foxes_0
    0.0, 1004.1042956221384, 6.1928347062092755
    10.0, 1004.1042956221384, 6.1928347062092755
    

    The second row is needed to make the values constant over time.

    Now let's modify our original model to use inputs as start values for foxes and rabbits:

    LotkaVolterraWithInput.mo

    model LotkaVolterraWithInput
      parameter Real g_r =0.04 "Natural growth rate for rabbits";
      parameter Real d_rf=0.0005 "Death rate of rabbits due to foxes";
      parameter Real d_f =0.09 "Natural deathrate for foxes";
      parameter Real g_fr=0.1 "Efficency in growing foxes from rabbits";
      input Real rabbits_0;
      input Real foxes_0;
      Real rabbits "Rabbits,(R) with start population rabbits_0";
      Real foxes "Foxes,(F) with start population foxes_0";
    initial equation
      rabbits = rabbits_0;
      foxes = foxes_0;
    equation
      der(rabbits) = g_r*rabbits - d_rf*rabbits*foxes;
      der(foxes) = g_fr*d_rf*rabbits*foxes -d_f*foxes;
    end LotkaVolterraWithInput;
    

    and run the simulation with additional simulation flag -csvInput=inputs.csv:

    loadFile("LotkaVolterraWithInput.mo"); getErrorString();
    simulate(LotkaVolterraWithInput, startTime=10.0, stopTime = 100.0, simflags="-csvInput=inputs.csv"); getErrorString();
    

    I used the OpenModelica ScriptingAPI to simulate the models. You can also use OMEdit to do this. Simulation flags can be added in the Simulation Setup->Simulation Flags->Additional Simulation Flags (Optional):.

    OpenModelica Simulation Setup for Lotka Voltera Model

    To verify everything works as expected compare the results to LotkaVoltera simulated up to stopTime=100.0.