Search code examples
unit-testingparametersmodelicaubuntu-18.04openmodelica

Definition of unit in Modelica (OpenModelica) for parameters, cannot be found in scope of parameters file


I'm new in Modelica programming (I'm using OpenModelica and text editor on Ubuntu 1.8), and I have to define some units, one of these is min^(-1), and I did it writing " type min_1=Real(unit="min^(-1)"); " in a model file with .mo extension (in this file I have no equations, I have only units definitions), and in another Modelica file (a record file) I have only parameters definitions (no equations) e.g. parameter min_1 beta=0.002; , but when I run the entire model I have an error concerning only (at least apparently, because I have no other erros) this particular unit, beacuse it says that min_1 is not found the scope of the file that cointains only parameters definitions (but min_1 is present is this file, in fact, apparently, I don't have the same problem with other units I defined), so I am not able to understand what is the real problem with that beacuse the error is really vague. Before I tryed to define units, all parameters were defined as Real, and the model plotted perfectly, so I'm pretty sure that the only possible problem is located in these 2 files. (Naturally all Modelica files of my entire Model are located in the same folder).

To define my units file I wrote in a .mo file (a model file):

model def_units
type dlkg=Real(unit="dl/kg");
type mg_1=Real(unit="mg^(-1)");
type kg_1=Real(unit="1/kg");
type min_kg_pmol=Real(unit="min*kg/pmol");
type min_1=Real(unit="min^(-1)");
type mg_kg_min=Real(unit="mg/kg/min");
type mg_kg_min_pmol_l=Real(unit="mg/kg/min*pmol*l");
type mg_kg_min_pmol_kg=Real(unit="mg/kg/min*pmol*kg");
type mg_kg=Real(unit="mg/kg");
type pmol_kg_mg_dl=Real(unit="pmol/kg*(mg/dl)");
type pmol_kg_min_dl=Real(unit="pmol/kg*(min/dl)");
end def_units;

To define my parameters I wrote in a separate .mo file (a record file) this:

record param
parameter min_1 alpha=0.001 ;
parameter min_1 beta=0.002;
parameter dl_kg gamma=0.003;
parameter mg_kg_min delta=0.004;
/* [... ] */
/* many other parameters defined in this way */
/* [... ] */
end param;

I did not use/import any libraries, those are standard units of the International System, they are only kinda "concatenated" between them, and reading on Internet the only way I found to define something more specific, like in this case, was the way I wrote my code above (my supervisor also approved the way I defined my units).

What could be the problem in this case?

Please if you can, try to explain in a simply and specific way.

Any help and advice will be very appreciate.

Thanks in advance.


Solution

  • Seems to be working fine, but you need to import def_units elements in your param record or you need to define them as parameter def_units.min_1 beta=0.002; I would also suggest changing def_units from a model to a package. Read more about Modelica imports and different Modelica classes such as packages, records, models and blocks in the Modelica Specification on modelica.org or the Modelica Book: http://book.xogeny.com/.

    File def_units.mo

    model def_units
      type dl_kg=Real(unit="dl/kg");
      type mg_1=Real(unit="mg^(-1)");
      type kg_1=Real(unit="1/kg");
      type min_kg_pmol=Real(unit="min*kg/pmol");
      type min_1=Real(unit="min^(-1)");
      type mg_kg_min=Real(unit="mg/kg/min");
      type mg_kg_min_pmol_l=Real(unit="mg/kg/min*pmol*l");
      type mg_kg_min_pmol_kg=Real(unit="mg/kg/min*pmol*kg");
      type mg_kg=Real(unit="mg/kg");
      type pmol_kg_mg_dl=Real(unit="pmol/kg*(mg/dl)");
      type pmol_kg_min_dl=Real(unit="pmol/kg*(min/dl)");
    end def_units;
    

    File param.mo

    record param
      import def_units.*; // you need to import all definitions from def_units!
      parameter min_1 alpha=0.001 ;
      parameter min_1 beta=0.002;
      parameter dl_kg gamma=0.003;
      parameter mg_kg_min delta=0.004;
      /* [... ] */
      /* many other parameters defined in this way */
      /* [... ] */
    end param;
    

    File script t.mos

    loadFile("def_units.mo"); getErrorString();
    loadFile("param.mo"); getErrorString();
    instantiateModel(param); getErrorString();
    

    Running the script with the OpenModelica compiler OMC:

    adrpo33@ida-0030 MINGW64 /c/home/adrpo33/dev/OMTesting/bugs/units
    $ ~/dev/OpenModelica/build/bin/omc t.mos
    true
    ""
    true
    ""
    "class param
      parameter Real alpha(unit = \"min^(-1)\") = 0.001;
      parameter Real beta(unit = \"min^(-1)\") = 0.002;
      parameter Real gamma(unit = \"dl/kg\") = 0.003;
      parameter Real delta(unit = \"mg/kg/min\") = 0.004;
    end param;
    "
    ""