modelica-3rdparty / ExternalMedia

The ExternalMedia library provides a framework for interfacing external codes computing fluid properties to Modelica.Media-compatible component models.
53 stars 36 forks source link

Adding new fluids to CoolProp and use it in Modelica #109

Open bmazurie opened 3 months ago

bmazurie commented 3 months ago

According to CoolProp documentation, it is possible to add new fluids to CoolProp library. To do so, it should be

I've added my data to SolutionFLuids.py, regenerate the JSON files (I can find the new JSON file KOH.json in ExternalMedia/build/CoolProp.sources/json/ folder). To recompile, I used cmake --build build --config Release --target install mentioned in ExternalMedia documenation(which worked flawlessly for the first installation).

However, when I define my medium in Modelica with:

package KOHCoolProp "CoolProp model of KOH"
  extends ExternalMedia.Media.CoolPropMedium(
    mediumName = "KOH",
    substanceNames = {"KOH"},
    ThermoStates = Modelica.Media.Interfaces.Choices.IndependentVariables.ph,
    AbsolutePressure(start=10e5),
    SpecificEnthalpy(start=2e5));
end KOHCoolProp;

and run a simple model such as (I defined my saturation pressure in the CoolProp's data):

model KOH_functions
  replaceable package Medium_CoolProp_KOH = Test_ExternalMedia_ThermoSysPro.Media.KOHCoolProp;
  Modelica.Units.SI.Pressure P;
equation 
  P=Medium_CoolProp_KOH.saturationPressure(273.15+20);
end KOH_functions;

It runs into translation errors both with OpenModelica 1.23.1 and Dymola23x. (Linux Scibian11 on both): OpenModelica errors: image Dymola errors: image

Thank you very much Baptiste

ibeyers commented 3 months ago

I believe you maybe failed to recompile Coolprop? So after adding a custom fluid and modifying the json, you need to first recompile Coolprop (I used the python wrapper -> run setup.py file in the \wrappers\Python folder.) Then you need to recompile ExternalMedia, and tell it to use your modified, recompiled Coolprop.

(just like a high-level pointer, hope that helps)

bmazurie commented 3 months ago

Thank you for your answer.

Indeed CoolProp was the problem. In fact, when dev/incompressible_liquids/all_incompressibles.py is ran to regenrate the JSON files with the fitted parameters, the json goes into ExternalMedia/build/CoolProp.sources/json. But it should be in ExternalMedia/build/CoolProp.sources/dev/incompressible_liquids/json(in my case) in order to be taken into account for the recompilation of CoolProp.

I had another issue regarding the definition of my medium in Modelica, I forget to add INCOMP:: in substanceNames = {"INCOMP::KOH"},. Now it does find the medium when using ExternalMedia.

bmazurie commented 3 months ago

However I would like to point an issue that I have with ExternalMedia and a possible bug.

I start from the same example as before, I defined a medium with CoolProp where I defined data regarding density, cp, mu, k and saturation pressure at different temperature. In Modelica, I use:

package KOHCoolProp "CoolProp model of KOH"
  extends ExternalMedia.Media.CoolPropMedium(
    mediumName = "KOH",
    substanceNames = {"INCOMP::KOH"},
    ThermoStates = Modelica.Media.Interfaces.Choices.IndependentVariables.ph,
    SpecificEnthalpy(start=2e5));
end KOHCoolProp;

And I use it in a model to find again those data:

replaceable package Medium_CoolProp_KOH =
      Test_ExternalMedia_ThermoSysPro.Media.KOHCoolProp;

  parameter Modelica.Units.SI.Temperature T=273.15+20;
  parameter Modelica.Units.SI.Pressure P_atm=101325;

equation 
  h_KOH=Medium_CoolProp_KOH.specificEnthalpy_pT(P_atm,T);

  d_KOH=Medium_CoolProp_KOH.density_ph(P_atm,h_KOH);
  cp_KOH=Medium_CoolProp_KOH.specificHeatCapacityCp(Medium_CoolProp_KOH.setState_ph(p=P_atm, h=h_KOH, phase=0));
  mu_KOH=Medium_CoolProp_KOH.dynamicViscosity(Medium_CoolProp_KOH.setState_ph(p=P_atm, h=h_KOH, phase=0));
  k_KOH=Medium_CoolProp_KOH.thermalConductivity(Medium_CoolProp_KOH.setState_ph(p=P_atm, h=h_KOH, phase=0));
  P_sat_KOH=Medium_CoolProp_KOH.saturationPressure(T);

It works fine, EXCEPT for the saturation pressure which gives what seems to be a default value: image

As CoolProp has the Python wrapper, I used it to test my media:

PropsSI('D','T',293.15,'P',101325,'INCOMP::KOH'): 1293.591986384524
PropsSI('C','T',293.15,'P',101325,'INCOMP::KOH'): 2972.5632292009855
PropsSI('VISCOSITY','T',293.15,'P',101325,'INCOMP::KOH'): 0.0024487626442337763
PropsSI('CONDUCTIVITY','T',293.15,'P',101325,'INCOMP::KOH'): 0.5813769155106154
PropsSI('P','T',293.15,'Q',0,'INCOMP::KOH'): 1381.3038209276774

It can be seen that saturation pressure is well defined with Python (and it is the value I used in the data for the definition of the medium in CoolProp). Thus it seems to be a bug with ExternalMedia.

Any idea? Thank you in advance