Search code examples
recordmodelicaopenmodelica

Modelica set multiple parameters with record


To override multiple parameters of a permanent magnet DC machine with the content of a DcPermanentMagnetData record I use this construct:

Modelica.Electrical.Machines.Utilities.ParameterRecords.DcPermanentMagnetData dcpmData(
    IaNominal = 1, 
    VaNominal = 2, 
    wNominal = 3);

Modelica.Electrical.Machines.BasicMachines.DCMachines.DC_PermanentMagnet dcpm(
    IaNominal = dcpmData.IaNominal,
    VaNominal = dcpmData.VaNominal,
    wNominal = dcpmData.wNominal);

Is it possible to set multiple parameter values of a model with a single command instead?

MWE:

model MWE

  record Rec
    parameter Real x_init;
    parameter Real y_init;
  end Rec;

  model HelloWorld
    parameter Real x_init;
    parameter Real y_init;
    Real x;
    Real y;
    initial equation
      x = x_init;
      y = y_init;
    equation
      der(x)=-x;
      der(y)=-y;
  end HelloWorld;

  Rec r (x_init = 1, y_init = 2);
  HelloWorld hi (x_init = r.x_init, y_init = r.y_init);  // this works
  //HelloWorld hi ( allValuesFrom(r) );  // <--- something like this

end MWE;

Solution

  • You can pass the whole record to the model. For that you have to replace your parameters with an instance of the record:

    model MWE
          record Rec
            parameter Real x_init;
            parameter Real y_init;
          end Rec;
    
          model HelloWorld
            input Rec r;
            Real x;
            Real y;
          initial equation 
              x = r.x_init;
              y = r.y_init;
          equation 
              der(x)=-x;
              der(y)=-y;
          end HelloWorld;
    
          Rec r( x_init = 1, y_init = 2);
          HelloWorld hi(r=r);
    end MWE;