Search code examples
modelicaopenmodelica

Is it possible to send global values to another model in OpenModelica?


I'm trying to create a model in OpenModelica which contains several other components (pipes, reservoirs). Currently I'm modifying a pipe in the Modelica.Fluid-library using a staggered grid, and need to determine the smallest step size dx in the entire model.

Is it possible to do the following?

  1. Calculate dx_1 in pipe 1 and dx_2 in pipe 2.
  2. Send dx_1 and dx_2 to an array in a global model (similar to Modelica.Fluid.System).
  3. Determine the smallest dx = min(dx_1, dx_2) and send back to both pipe 1 and pipe 2.

I have calculated both dx_1 in pipe 1 and dx_2 in pipe 2, and created an array in a data storage model similar to Fluid.System. I am, however, struggling with sending the step sizes to the data storage model, and sending them back again after determining the smallest dx.

Is this even possible? How would one go about to do so?


Solution

  • Yes, there are several possibilities.

    As you mention, the pipes can access variables/parameters in the data storage model if this is instantiated as inner in your global model and declared as outer in each pipe model. As with the Modelica.Fluid models referring to Fluid.System, the pipes can access dx_minin the data storage model.

    This is a code example, loosely based on your question:

    model Pipe
      outer DataStorage dataStorage;
    
      Real dx_min = dataStorage.dx_min;
      Real dx "calculated in this model";
      ...
    end Pipe;
    
    model DataStorage
      parameter Integer nPipes;
      input Real dx_array[nPipes];
      Real dx_min=min(dx_array);
      ...
    end DataStorage;
    
    model GlobalModel
      Pipe pipe1;
      Pipe pipe2;
      inner DataStorage dataStorage(nPipes=2, dx_array={pipe1.dx, pipe2.dx});
    
      ...
    end GlobalModel;
    

    You should beware of the variability of the different "dx's" as you cannot assign a time-varying "dx" to a "dx" declared as a parameter.

    If the only purpose of the DataStorage model is to take the minimum entry in an array then you could also put its three lines of code in GlobalModel, reducing the code to:

    model Pipe
      input Real dx_min;
      Real dx "calculated in this model";
      ...
    end Pipe;
    
    model GlobalModel
      parameter Integer nPipes=2;
      Real dx_array[nPipes]={pipe1.dx, pipe2.dx};
      Real dx_min=min(dx_array);
    
      Pipe pipe1(dx_min=dx_min);
      Pipe pipe2(dx_min=dx_min);
    
      ...
    end GlobalModel;