Piwi447
Piwi447

Reputation: 31

Modelica HNx mixing simulation, initial Mass_fraction as input or parameter

enter image description herethis question refer to the model example given as answer in this previous question Modelica fluid mixing simulation In the example, the initial mass fraction are defined in the Medium definition:

package MixtureGas "Mixture gas"
  extends Modelica.Media.IdealGases.Common.MixtureGasNasa(
    mediumName="MixtureGas",
data={Modelica.Media.IdealGases.Common.SingleGasesData.N2,Modelica.Media.IdealGases.Common.SingleGasesData.H2},
    fluidConstants={Modelica.Media.IdealGases.Common.FluidData.N2,Modelica.Media.IdealGases.Common.FluidData.H2},
    substanceNames={"N2","H2"},
    reference_X={0.4,0.6});
end MixtureGas;

when i simulate the model Mixing_separateSource given in the same post on a 200 s simulation time, it works well. If i want to resimulated from time 100 to 200, i would expect the simulation to restart from the mass fraction stored in the result Mixing_separateSource.mat for the time 100s but it seems to restart always from Reference_X Is it a strange behaviour of OpenModelical or a normal Modelica behaviour?

A workaround could be to be able to give initial values for mass fraction as parameter of my mixing volume submodel. When i define {0.8,0.2} as X_Start in the Initialize folder of my Vessel parameter, and run the simulation, it starts from {0.4,0.6}.

[Edit ]Digging more on this subject this afternoon, i discovered that the volume medium mass fraction are well initialized but not the massfraction measurement. see the plot picture. How is it possible? [edit2] resimulate from 50s the second picture show that OpenModelica restart the simulation at 50s but with the initial values, no the states from the previous simulation

[edit3] I further investigated by activating resimulate from a result file at time 100s. The Init loc show that init variable are properly read but that a lot of Port config are using the default_Xi or reference_X constants for their initialization process. extract from media modelica standard library:

partial package PartialMedium
  "Partial medium properties (base package of all media packages)"
  extends Modelica.Media.Interfaces.Types;
  extends Modelica.Icons.MaterialPropertiesPackage;

  // Constants to be set in Medium
  constant Modelica.Media.Interfaces.Choices.IndependentVariables
    ThermoStates "Enumeration type for independent variables";
  constant String mediumName="unusablePartialMedium" "Name of the medium";
  constant String substanceNames[:]={mediumName}
    "Names of the mixture substances. Set substanceNames={mediumName} if only one substance.";
  constant String extraPropertiesNames[:]=fill("", 0)
    "Names of the additional (extra) transported properties. Set extraPropertiesNames=fill(\"\",0) if unused";
  constant Boolean singleState
    "= true, if u and d are not a function of pressure";
  constant Boolean reducedX=true
    "= true if medium contains the equation sum(X) = 1.0; set reducedX=true if only one substance (see docu for details)";
  constant Boolean fixedX=false
    "= true if medium contains the equation X = reference_X";
  constant AbsolutePressure reference_p=101325
    "Reference pressure of Medium: default 1 atmosphere";
  constant Temperature reference_T=298.15
    "Reference temperature of Medium: default 25 deg Celsius";
  constant MassFraction reference_X[nX]=fill(1/nX, nX)
    "Default mass fractions of medium";
  constant AbsolutePressure p_default=101325
    "Default value for pressure of medium (for initialization)";
  constant Temperature T_default=Modelica.Units.Conversions.from_degC(20)
    "Default value for temperature of medium (for initialization)";
  constant SpecificEnthalpy h_default=specificEnthalpy_pTX(
          p_default,
          T_default,
          X_default)
    "Default value for specific enthalpy of medium (for initialization)";
  constant MassFraction X_default[nX]=reference_X
    "Default value for mass fractions of medium (for initialization)";
  constant ExtraProperty C_default[nC]=fill(0, nC)
    "Default value for trace substances of medium (for initialization)";

  final constant Integer nS=size(substanceNames, 1) "Number of substances";
  constant Integer nX=nS "Number of mass fractions";
  constant Integer nXi=if fixedX then 0 else if reducedX then nS - 1 else nS
    "Number of structurally independent mass fractions (see docu for details)";

  final constant Integer nC=size(extraPropertiesNames, 1)
    "Number of extra (outside of standard mass-balance) transported properties";
  constant Real C_nominal[nC](min=fill(Modelica.Constants.eps, nC)) = 1.0e-6*
    ones(nC) "Default for the nominal values for the extra properties";
  replaceable record FluidConstants =
      Modelica.Media.Interfaces.Types.Basic.FluidConstants
    "Critical, triple, molecular and other standard data of fluid";

  replaceable record ThermodynamicState
    "Minimal variable set that is available as input argument to every medium function"
    extends Modelica.Icons.Record;
  end ThermodynamicState;

  replaceable partial model BaseProperties
    "Base properties (p, d, T, h, u, R_s, MM and, if applicable, X and Xi) of a medium"
    InputAbsolutePressure p "Absolute pressure of medium";
    InputMassFraction[nXi] Xi(start=reference_X[1:nXi])
      "Structurally independent mass fractions";
    InputSpecificEnthalpy h "Specific enthalpy of medium";
    Density d "Density of medium";
    Temperature T "Temperature of medium";
    MassFraction[nX] X(start=reference_X)
      "Mass fractions (= (component mass)/total mass  m_i/m)";
    SpecificInternalEnergy u "Specific internal energy of medium";
    SpecificHeatCapacity R_s "Gas constant (of mixture if applicable)";
    MolarMass MM "Molar mass (of mixture or single fluid)";
    ThermodynamicState state
      "Thermodynamic state record for optional functions";
    parameter Boolean preferredMediumStates=false
      "= true if StateSelect.prefer shall be used for the independent property variables of the medium"
      annotation (Evaluate=true, Dialog(tab="Advanced"));
    parameter Boolean standardOrderComponents=true
      "If true, and reducedX = true, the last element of X will be computed from the other ones";
    Modelica.Units.NonSI.Temperature_degC T_degC=
        Modelica.Units.Conversions.to_degC(T)
      "Temperature of medium in [degC]";
    Modelica.Units.NonSI.Pressure_bar p_bar=
        Modelica.Units.Conversions.to_bar(p)
      "Absolute pressure of medium in [bar]";

My fear is that this mixture gas model was not developped in a perspective of having a variable mixture between two gas component in the context of a simulation.

C:/Users/ruwp/AppData/Local/Temp/OpenModelica/OMEdit/HNx_Pack.Mixing_separateSources/Mixing_separateSources.exe -port=63833 -logFormat=xmltcp -override=startTime=0,stopTime=200,stepSize=1,tolerance=1e-06,solver=dassl,outputFormat=mat,variableFilter=.* -r=C:/Users/ruwp/AppData/Local/Temp/OpenModelica/OMEdit/HNx_Pack.Mixing_separateSources/Mixing_separateSources_res.mat -iif=C:/Users/ruwp/Documents/RD/simulation/OModel/appli/Initial.mat -iit=100 -w -lv=LOG_INIT,LOG_INIT_HOMOTOPY,LOG_INIT_V,LOG_STATS -inputPath=C:/Users/ruwp/AppData/Local/Temp/OpenModelica/OMEdit/HNx_Pack.Mixing_separateSources -outputPath=C:/Users/ruwp/AppData/Local/Temp/OpenModelica/OMEdit/HNx_Pack.Mixing_separateSources
### START INITIALIZATION ###
updating min-values
updating max-values
updating nominal-values
updating primary start-values
...
updated start value: volume.medium.Xi[2](start=0.2)
updated start value: volume.medium.Xi[1](start=0.8)
...
import start values
file: C:/Users/ruwp/Documents/RD/simulation/OModel/appli/Initial.mat
time: 100
import real variables
...
| volume.medium.Xi[1](start=0.990077)
| volume.medium.Xi[2](start=0.00992328)
...

| massFraction_H2.Xi(start=0.188464)
| massFraction_H2.port_a.Xi_outflow[1](start=0.4)
| massFraction_H2.port_a.Xi_outflow[2](start=0.6)
| massFraction_H2.port_b.Xi_outflow[1](start=0.990077)
| massFraction_H2.port_b.Xi_outflow[2](start=0.00992328)
| massFraction_N2.Xi(start=0.811536)
| massFraction_N2.port_a.Xi_outflow[1](start=0.4)
| massFraction_N2.port_a.Xi_outflow[2](start=0.6)
| massFraction_N2.port_b.Xi_outflow[1](start=0.990077)
| massFraction_N2.port_b.Xi_outflow[2](start=0.00992328)
...
| volume.mXi[1](start=0.0991431)
| volume.mXi[2](start=0.000993685)
...
import real parameters
...
| boundary1.X[1](start=0.4)
| boundary1.X[2](start=0.6)
| boundary1.X_in_internal[1](start=0.4)
| boundary1.X_in_internal[2](start=0.6)
| boundary1.medium.T(start=293.15)
| boundary1.medium.X[1](start=0.4)
| boundary1.medium.X[2](start=0.6)
| boundary1.medium.Xi[1](start=0.4)
| boundary1.medium.Xi[2](start=0.6)
| boundary1.medium.p(start=101325)
| boundary1.medium.state.T(start=293.15)
| boundary1.medium.state.X[1](start=0.4)
| boundary1.medium.state.X[2](start=0.6)
| boundary1.medium.state.p(start=101325)
| boundary1.p(start=101325)
| boundary1.p_in_internal(start=101325)
| boundary1.ports[1].Xi_outflow[1](start=0.4)
| boundary1.ports[1].Xi_outflow[2](start=0.6)
...
| valve.m_flow_start(start=0)
| valve.opening(start=1)
| valve.port_a.Xi_outflow[1](start=0.4)
| valve.port_a.Xi_outflow[2](start=0.6)
| valve.port_b.p(start=101325)
| valve.state_b.X[1](start=0.4)
| valve.state_b.X[2](start=0.6)
| valve.state_b.p(start=101325)
| volume.T_start(start=500)
| volume.V(start=0.1)
| volume.X_start[1](start=0.8)
| volume.X_start[2](start=0.2)
| volume.fluidLevel_max(start=1)
...
import real discrete
...
initialization method: symbolic        [solves the initialization problem symbolically - default]
Model contains homotopy operator: Use adaptive homotopy method to solve initialization problem. To disable initialization with homotopy operator use "-noHomotopyOnFirstTry".
parameter values
real parameters
...
[49] parameter Real boundary1.X[1](start=0.4, fixed=true) = 0.4
[50] parameter Real boundary1.X[2](start=0.6, fixed=true) = 0.6
[51] parameter Real boundary1.X_in_internal[1](start=0.4, fixed=true) = 0.4
[52] parameter Real boundary1.X_in_internal[2](start=0.6, fixed=true) = 0.6
[53] parameter Real boundary1.medium.T(start=293.15, fixed=true) = 293.15
[54] parameter Real boundary1.medium.X[1](start=0.4, fixed=true) = 0.4
[55] parameter Real boundary1.medium.X[2](start=0.6, fixed=true) = 0.6
[56] parameter Real boundary1.medium.Xi[1](start=0.4, fixed=true) = 0.4
[57] parameter Real boundary1.medium.Xi[2](start=0.6, fixed=true) = 0.6
[58] parameter Real boundary1.medium.p(start=101325, fixed=true) = 101325
[59] parameter Real boundary1.medium.state.T(start=293.15, fixed=true) = 293.15
[60] parameter Real boundary1.medium.state.X[1](start=0.4, fixed=true) = 0.4
[61] parameter Real boundary1.medium.state.X[2](start=0.6, fixed=true) = 0.6
[62] parameter Real boundary1.medium.state.p(start=101325, fixed=true) = 101325
[63] parameter Real boundary1.p(start=101325, fixed=true) = 101325
[64] parameter Real boundary1.p_in_internal(start=101325, fixed=true) = 101325
[65] parameter Real boundary1.ports[1].Xi_outflow[1](start=0.4, fixed=true) = 0.4
[66] parameter Real boundary1.ports[1].Xi_outflow[2](start=0.6, fixed=true) = 0.6
[67] parameter Real boundary1.ports[1].p(start=101325, fixed=true) = 101325
...
[96] parameter Real valve.port_a.Xi_outflow[1](start=0.4, fixed=true) = 0.4
[97] parameter Real valve.port_a.Xi_outflow[2](start=0.6, fixed=true) = 0.6
[98] parameter Real valve.port_b.p(start=101325, fixed=true) = 101325
[99] parameter Real valve.state_b.X[1](start=0.4, fixed=true) = 0.4
[100] parameter Real valve.state_b.X[2](start=0.6, fixed=true) = 0.6
[101] parameter Real valve.state_b.p(start=101325, fixed=true) = 101325
[102] parameter Real volume.T_start(start=500, fixed=true) = 500
[103] parameter Real volume.V(start=0.1, fixed=true) = 0.1
[104] parameter Real volume.X_start[1](start=0.8, fixed=true) = 0.8
[105] parameter Real volume.X_start[2](start=0.2, fixed=true) = 0.2
...
integer parameters
...
boolean parameters
...
string parameters
[1] parameter String massFraction_H2.substanceName(start="H2") = "H2"
[2] parameter String massFraction_N2.substanceName(start="N2") = "N2"
### SOLUTION OF THE INITIALIZATION ###
states variables
[1] Real volume.medium.T(start=329.067, nominal=500) = 500 (pre: 329.067)
[2] Real volume.medium.Xi[1](start=0.990077, nominal=0.1) = 0.8 (pre: 0.990077)
[3] Real volume.medium.Xi[2](start=0.00992328, nominal=0.1) = 0.2 (pre: 0.00992328)
[4] Real volume.medium.p(start=110318, nominal=1e+06) = 101375 (pre: 110318)
derivatives variables
[5] Real der(volume.medium.T) = -4.67691 (pre: -1.04633)
[6] Real der(volume.medium.Xi[1]) = -0.0104721 (pre: 0.000344037)
[7] Real der(volume.medium.Xi[2]) = 0.0104721 (pre: -0.000344037)
[8] Real der(volume.medium.p) = 4124.17 (pre: 67.8659)
other real variables
...
[48] Real massFraction_H2.Xi(start=0.188464, nominal=1) = 0.39955 (pre: 0.188464)
[49] Real massFraction_H2.port_a.Xi_outflow[1](start=0.4, nominal=0.1) = 0.4 (pre: 0.4)
[50] Real massFraction_H2.port_a.Xi_outflow[2](start=0.6, nominal=0.1) = 0.6 (pre: 0.6)
[51] Real massFraction_H2.port_b.Xi_outflow[1](start=0.990077, nominal=0.1) = 0.8 (pre: 0.990077)
[52] Real massFraction_H2.port_b.Xi_outflow[2](start=0.00992328, nominal=0.1) = 0.2 (pre: 0.00992328)
[53] Real massFraction_N2.Xi(start=0.811536, nominal=1) = 0.60045 (pre: 0.811536)
[54] Real massFraction_N2.port_a.Xi_outflow[1](start=0.4, nominal=0.1) = 0.4 (pre: 0.4)
[55] Real massFraction_N2.port_a.Xi_outflow[2](start=0.6, nominal=0.1) = 0.6 (pre: 0.6)
[56] Real massFraction_N2.port_b.Xi_outflow[1](start=0.990077, nominal=0.1) = 0.8 (pre: 0.990077)
[57] Real massFraction_N2.port_b.Xi_outflow[2](start=0.00992328, nominal=0.1) = 0.2 (pre: 0.00992328)
...
[72] Real volume.mXi[1](start=0.0991431, nominal=1) = 0.0152682 (pre: 0.0991431)
[73] Real volume.mXi[2](start=0.000993685, nominal=1) = 0.00381704 (pre: 0.000993685)
...
boolean variables
...

We can see that a lot of start condition related to mass fractions are linked to the 0.4 / 0.6 values that are defined as Reference_X. Reference_X and Default_X are Constant, so impossible to change them when i extend the HNx GasMixture package. Would it be possible to define them as parameters or input that can be defined for each instance of the media?

[ edit4]

My first idea was to change the reference_X definition and base it on a parameter but it was refused by the translation rule: reference_X is a constant and cannot be defined by a parameter…. Later yesterday, I tried to change the gasMixture definition by relating reference_X to one constant (H2 rate) . This was accepted by the syntax checking and I could define different mixture with 10% of H2, 50%, 90%) . I tried to simulate my full model starting with 50% HNx, but it did not work properly and OMEDit crashed during configuration of plot.

I need to check how to reactivate plot interface on an old simulation result or find a way to easy plot data from old result with other tools than OMEDit.. This is really difficult when I need to analyse data at the same time that are in a completely different range. I did not find a good way to zoom on multiple graph together, this is for me the base of simulation analysis. So I loose a lot of time on this bad working of OMEDit plot GUI, with no way to save plot and too many crashes of OMEDit.

I will investigate python to draw curve based on .mat or CSV files.

Any help is welcome

Upvotes: 0

Views: 122

Answers (1)

janpeter
janpeter

Reputation: 1084

I understand that the normal "handling" is that you need to give the initial value as the final value from the simulation before, as you describe aa s work around.

The "administration" of how to perform simulation may vary from each vendor of Modelica and is not part of the Modelica standard.

I usually do simulations using FMUs in a Python environment and I have the same need as you have and implemented a "help function" to automate the transfer of final values to initial values for the next simulation. There is in the standard around FMU a way to read-out all the continuous time states, and is a good help.

In OpenModelica I am not sure if there is a way to facilitate what you want to do. There is in the menu to the right, above the variable section, a panel with knobs: restart, run, and stop, which perhaps try to meet your need, but it can also be that it is related to animation. Someone else need to fill in here.

Upvotes: 0

Related Questions