Archived OpenModelica forums. Posting is disabled.

Alternative forums include GitHub discussions or StackOverflow (make sure to read the Stack Overflow rules; you need to have well-formed questions)


Forgot password? | Forgot username? | Register

Assigning value to record an array annotation

Assigning value to record an array annotation

Hi , I am working on creating models using OpenModelica at IITB. I am trying make model where component can be selected by using drop down menu. I have done it as shown in code.

package Test1
  type Component = enumeration(Air, Argon, Bromine, Carbontetrachloride, Carbonmonoxide);

  package Database
    extends Modelica.Icons.VariantsPackage;
    record General_Properties
      parameter Integer SN;
      parameter String name;
      parameter Real Tc;
      parameter Real Pc;
      parameter Real Vc;
      parameter Real Cc;
      parameter Real Tb;
      parameter Real Tm;
      parameter Real TT;
      parameter Real TP;
      parameter Real MW;
     
    end General_Properties;

    record Air
      extends General_Properties(SN = 1, name = "Air", Tc = 132.45, Pc = 3774000, Vc = 0.09147, Cc = 0.313, Tb = 78.67, Tm = 59.15, TT = 59.15, TP = 5642.15, MW = 28.96);
    end Air;

    record Argon
      extends General_Properties(SN = 2, name = "Argon", Tc = 150.86, Pc = 4898000, Vc = 0.07457, Cc = 0.291, Tb = 87.27, Tm = 83.8039, TT = 83.8, TP = 68906.1, MW = 39.948);
    end Argon;

    record Bromine
      extends General_Properties(SN = 3, name = "Bromine", Tc = 584.15, Pc = 1.03E+07, Vc = 0.135, Cc = 0.286, Tb = 331.9, Tm = 265.9, TT = 265.85, TP = 5853.37);
    end Bromine;

    record Carbontetrachloride
      extends General_Properties(SN = 4, name = "Carbontetrachloride", Tc = 556.3, Pc = 4557000, Vc = 0.276, Cc = 0.271, Tb = 349.79, Tm = 250.33, TT = 250.33, TP = 1122.46, MW = 153.822);
    end Carbontetrachloride;

    record Carbonmonoxide
      extends General_Properties(SN = 5, name = "Carbonmonoxide", Tc = 132.85, Pc = 3494000, Vc = 0.0931, Cc = 0.292, Tb = 81.66, Tm = 68.15, TT = 68.15, TP = 15400, MW = 28.01);
    end Carbonmonoxide;

   
  end Database;

  function get_comp
    extends Modelica.Icons.Function;
    output Database.General_Properties Comp;
    input Component component;
  algorithm
    if component == Component.Air then
      Comp := Database.Air;
    elseif component == Component.Argon then
      Comp := Database.Argon;
    elseif component == Component.Bromine then
      Comp := Database.Bromine;
    elseif component == Component.Carbontetrachloride then
      Comp := Database.Carbontetrachloride;
    elseif component == component.Carbonmonoxide then
      Comp := Database.Carbonmonoxide;
    end if;
  end get_comp;

model MS
  extends Modelica.Icons.SourcesPackage;
  parameter Component component;
  Database.General_Properties comp;
 
equation
  comp = get_comp(component);
end MS;

  model Test
    MS mS1(component = Test1.Component.Argon)  annotation(Placement(visible = true, transformation(origin = {-2, 48}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
  end Test;
end Test1;

But I am getting type mismatch error. Is there any way to assign value to a record or a model in an algorithm or an equation section?

Also Is it possible to make separate dialog boxes for each parameter in an array, rather than using single box for all parameters in an array?

Re: Assigning value to record an array annotation

Note that you need components not records in package Database. See below:

Code:


package Test1
  type Component = enumeration(Air, Argon, Bromine, Carbontetrachloride, Carbonmonoxide);

  package Database
    extends Modelica.Icons.VariantsPackage;
    record General_Properties
      Integer SN;
      String name;
      Real Tc;
      Real Pc;
      Real Vc;
      Real Cc;
      Real Tb;
      Real Tm;
      Real TT;
      Real TP;
      Real MW;
    end General_Properties;

    constant General_Properties Air(SN = 1, name = "Air", Tc = 132.45, Pc = 3774000, Vc = 0.09147, Cc = 0.313, Tb = 78.67, Tm = 59.15, TT = 59.15, TP = 5642.15, MW = 28.96);

    constant General_Properties Argon(SN = 2, name = "Argon", Tc = 150.86, Pc = 4898000, Vc = 0.07457, Cc = 0.291, Tb = 87.27, Tm = 83.8039, TT = 83.8, TP = 68906.1, MW = 39.948);

    constant General_Properties Bromine(SN = 3, name = "Bromine", Tc = 584.15, Pc = 1.03E+07, Vc = 0.135, Cc = 0.286, Tb = 331.9, Tm = 265.9, TT = 265.85, TP = 5853.37, MW = 10 /* adrpo: MW was missing, fix it! */);

    constant General_Properties Carbontetrachloride(SN = 4, name = "Carbontetrachloride", Tc = 556.3, Pc = 4557000, Vc = 0.276, Cc = 0.271, Tb = 349.79, Tm = 250.33, TT = 250.33, TP = 1122.46, MW = 153.822);

    constant General_Properties Carbonmonoxide(SN = 5, name = "Carbonmonoxide", Tc = 132.85, Pc = 3494000, Vc = 0.0931, Cc = 0.292, Tb = 81.66, Tm = 68.15, TT = 68.15, TP = 15400, MW = 28.01);
   
  end Database;

  function get_comp
    extends Modelica.Icons.Function;
    output Database.General_Properties Comp;
    input Component component;
  algorithm
    if component == Component.Air then
      Comp := Database.Air;
    elseif component == Component.Argon then
      Comp := Database.Argon;
    elseif component == Component.Bromine then
      Comp := Database.Bromine;
    elseif component == Component.Carbontetrachloride then
      Comp := Database.Carbontetrachloride;
    elseif component == component.Carbonmonoxide then
      Comp := Database.Carbonmonoxide;
    end if;
  end get_comp;

model MS
  extends Modelica.Icons.SourcesPackage;
  parameter Component component;
  Database.General_Properties comp;
 
equation
  comp = get_comp(component);
end MS;

  model Test
    MS mS1(component = Test1.Component.Argon)  annotation(Placement(visible = true, transformation(origin = {-2, 48}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
  end Test;
end Test1;

We should support annotation choices for drop down list choices this but we don't yet. We're working on it.
You could also use replaceable and redeclares.

Re: Assigning value to record an array annotation

Thank you very much for instant reply.

I am new to coding. I have made enumeration of just names and then trying to assign value to record instantiated by general properties using if else statements. Since we can create drop down menu using enumeration which is not available as choices. I am trying to create model which can fully used with just GUI.

What do you mean by we need components not record in Database. Component is enumeration. enumeration cannot contain parameters and their values. I have more parameters also which are not given in code. Some of them are array.

Is it possible to define them inside enumeration? 

One different question. Is it possible to have different dialog boxes for different parameters in array. eg. If I want to make an array of Component component[2]. I have to write {Test1.Component.Air, Test1.Component.Argon} in GUI dialog box. So if we have different dialog box we can choose both by using drop down menu.

Is this making sense or am I missing something?

Thanks
Pravin

Re: Assigning value to record an array annotation

"What do you mean by we need components not record in Database.
Component is enumeration. enumeration cannot contain parameters and their values.
I have more parameters also which are not given in code. Some of them are array."

I was talking about "Comp". I mean that you need declare components which you can then assign in the functions, you tried to assign directly records to components and that does't work like that, it only works via a record constructor Record x = Record(var1=val1, var2=val2, etc).

In Modelica you can no longer modify only an array element, you can only modify the entire array that's why you cannot have a dialog for each array element.


Re: Assigning value to record an array annotation

Thank you very much. It have answered my all questions.

There are 0 guests and 0 other users also viewing this topic
You are here: