physicsmodelingmodelicaopenmodelica

Calculating values based on inputs in a Modelica record constructor


I am simulating acoustic holes in OpenModelica. For this I have a generic "Hole" model which works with different cross section geometries.

For me, the most logical approach seems to save the cross sections in records with an interface.

partial record CrossSection
 constant String name="Generic";
 SI.Area area "cross sectional area";
 SI.Length hydR "Hydraulic Radius";
end CrossSection;

record Circle
  extends CrossSection(name="Circle");

  SI.Diameter d "Diameter of the cross section"; // "Input Variable"
end Circle;

Allowing the cross section to be selected in a drop down menu in diagram view:

replaceable parameter CrossSections.Circle cs "Cross Section"
  constrainedby CrossSections.CrossSection
  annotation(Dialog(group="Geometry"), choicesAllMatching=true);

Now, I need to calculate area and hydR as soon as model is instantiated, but they will remain constant during runtime. I initially used models for the cross sections, which works, but it seems the approach is quite computationally expensive.

I have worked through 12.6 in the specification, but I can't figure out explicitly define a record constructor function. And modify it to perform the required calculations on startup.


Solution

  • Not sure if I considered everything, but this code could be a starting point:

    package SO2
      import Modelica.Units.SI;
    
      partial record CrossSection
        constant String name="Generic";
        parameter SI.Area area "cross sectional area";
        parameter SI.Length hydR "Hydraulic Radius";
      end CrossSection;
    
      record Circle
        extends CrossSection(
          final area = Modelica.Constants.pi*(d/2)^2,
          name="Circle");
        parameter SI.Diameter d "Diameter of the cross section"; // "Input Variable"
      end Circle;
    
      model Example
        replaceable parameter Circle cs(d=1, hydR=2) constrainedby CrossSection "Cross Section"
          annotation (Dialog(group="Geometry"), choicesAllMatching=true);
      end Example;
    end SO2;
    
    1. I added the prefix parameter to area and hydR, as:

      area and hydR ... will remain constant during runtime

    2. circle adds the equation for area

    3. I have no idea what hydR is. You could also set it in circle, or provide it from outside as modifier, as shown.