﻿id	summary	reporter	owner	description	type	status	priority	milestone	component	version	resolution	keywords	cc
5182	Issue in preOpt evalFunc involving functions with record outputs containing Real and Integer variables	Francesco Casella		"Please check [https://libraries.openmodelica.org/branches/newInst/HelmholtzMedia/files/HelmholtzMedia_HelmholtzMedia.Examples.ConvergenceTest.SinglePhase_setState_b.err HelmholtzMedia.Examples.ConvergenceTest.SinglePhase_setState_b]. The back-end fails during the preOpt evalFunc phase with a stack overflow error that seems to point to an infinite loop, since the sequence 
{{{
[bt] #534 /var/lib/hudson/slave/workspace/OpenModelica_TEST_LIBS/OpenModelica/OMCompiler/build/bin/../lib/x86_64-linux-gnu/omc/libOpenModelicaCompiler.so(EvaluateFunctions.evaluateConstantFunction)
[bt] #535 /var/lib/hudson/slave/workspace/OpenModelica_TEST_LIBS/OpenModelica/OMCompiler/build/bin/../lib/x86_64-linux-gnu/omc/libOpenModelicaCompiler.so(EvaluateFunctions.evaluateConstantFunctionWrapper)
[bt] #536 /var/lib/hudson/slave/workspace/OpenModelica_TEST_LIBS/OpenModelica/OMCompiler/build/bin/../lib/x86_64-linux-gnu/omc/libOpenModelicaCompiler.so(boxptr_EvaluateFunctions_evaluateConstantFunctionWrapper)
[bt] #537 /var/lib/hudson/slave/workspace/OpenModelica_TEST_LIBS/OpenModelica/OMCompiler/build/bin/../lib/x86_64-linux-gnu/omc/libOpenModelicaCompiler.so(Expression.traverseExpTopDown)
[bt] #538 /var/lib/hudson/slave/workspace/OpenModelica_TEST_LIBS/OpenModelica/OMCompiler/build/bin/../lib/x86_64-linux-gnu/omc/libOpenModelicaCompiler.so(EvaluateFunctions.evaluateFunctions_updateStatement)
[bt] #539 /var/lib/hudson/slave/workspace/OpenModelica_TEST_LIBS/OpenModelica/OMCompiler/build/bin/../lib/x86_64-linux-gnu/omc/libOpenModelicaCompiler.so(EvaluateFunctions.evaluateFunctions_updateAllStatements)
[bt] #540 /var/lib/hudson/slave/workspace/OpenModelica_TEST_LIBS/OpenModelica/OMCompiler/build/bin/../lib/x86_64-linux-gnu/omc/libOpenModelicaCompiler.so(EvaluateFunctions.predictIfOutput)
[bt] #541 /var/lib/hudson/slave/workspace/OpenModelica_TEST_LIBS/OpenModelica/OMCompiler/build/bin/../lib/x86_64-linux-gnu/omc/libOpenModelicaCompiler.so(EvaluateFunctions.evaluateFunctions_updateStatement)
[bt] #542 /var/lib/hudson/slave/workspace/OpenModelica_TEST_LIBS/OpenModelica/OMCompiler/build/bin/../lib/x86_64-linux-gnu/omc/libOpenModelicaCompiler.so(EvaluateFunctions.evaluateFunctions_updateAllStatements)
[bt] #543 /var/lib/hudson/slave/workspace/OpenModelica_TEST_LIBS/OpenModelica/OMCompiler/build/bin/../lib/x86_64-linux-gnu/omc/libOpenModelicaCompiler.so(EvaluateFunctions.predictIfOutput)
[bt] #544 /var/lib/hudson/slave/workspace/OpenModelica_TEST_LIBS/OpenModelica/OMCompiler/build/bin/../lib/x86_64-linux-gnu/omc/libOpenModelicaCompiler.so(EvaluateFunctions.evaluateFunctions_updateStatement)
[bt] #545 /var/lib/hudson/slave/workspace/OpenModelica_TEST_LIBS/OpenModelica/OMCompiler/build/bin/../lib/x86_64-linux-gnu/omc/libOpenModelicaCompiler.so(EvaluateFunctions.evaluateIfStatement)
[bt] #546 /var/lib/hudson/slave/workspace/OpenModelica_TEST_LIBS/OpenModelica/OMCompiler/build/bin/../lib/x86_64-linux-gnu/omc/libOpenModelicaCompiler.so(EvaluateFunctions.evaluateFunctions_updateStatement)
[bt] #547 /var/lib/hudson/slave/workspace/OpenModelica_TEST_LIBS/OpenModelica/OMCompiler/build/bin/../lib/x86_64-linux-gnu/omc/libOpenModelicaCompiler.so(EvaluateFunctions.evaluateFunctions_updateAlgElements)
[bt] #548 /var/lib/hudson/slave/workspace/OpenModelica_TEST_LIBS/OpenModelica/OMCompiler/build/bin/../lib/x86_64-linux-gnu/omc/libOpenModelicaCompiler.so(boxptr_EvaluateFunctions_evaluateFunctions__updateAlgElements)
[bt] #549 /var/lib/hudson/slave/workspace/OpenModelica_TEST_LIBS/OpenModelica/OMCompiler/build/bin/../lib/x86_64-linux-gnu/omc/libOpenModelicaCompiler.so(List.mapFold3)
[bt] #550 /var/lib/hudson/slave/workspace/OpenModelica_TEST_LIBS/OpenModelica/OMCompiler/build/bin/../lib/x86_64-linux-gnu/omc/libOpenModelicaCompiler.so(EvaluateFunctions.evaluateConstantFunction)
[bt] #551 /var/lib/hudson/slave/workspace/OpenModelica_TEST_LIBS/OpenModelica/OMCompiler/build/bin/../lib/x86_64-linux-gnu/omc/libOpenModelicaCompiler.so(EvaluateFunctions.evaluateConstantFunctionWrapper)
[bt] #552 /var/lib/hudson/slave/workspace/OpenModelica_TEST_LIBS/OpenModelica/OMCompiler/build/bin/../lib/x86_64-linux-gnu/omc/libOpenModelicaCompiler.so(boxptr_EvaluateFunctions_evaluateConstantFunctionWrapper)
[bt] #553 /var/lib/hudson/slave/workspace/OpenModelica_TEST_LIBS/OpenModelica/OMCompiler/build/bin/../lib/x86_64-linux-gnu/omc/libOpenModelicaCompiler.so(Expression.traverseExpTopDown)
[bt] #554 /var/lib/hudson/slave/workspace/OpenModelica_TEST_LIBS/OpenModelica/OMCompiler/build/bin/../lib/x86_64-linux-gnu/omc/libOpenModelicaCompiler.so(EvaluateFunctions.evaluateFunctions_updateStatement)
[bt] #555 /var/lib/hudson/slave/workspace/OpenModelica_TEST_LIBS/OpenModelica/OMCompiler/build/bin/../lib/x86_64-linux-gnu/omc/libOpenModelicaCompiler.so(EvaluateFunctions.evaluateFunctions_updateAllStatements)
[bt] #556 /var/lib/hudson/slave/workspace/OpenModelica_TEST_LIBS/OpenModelica/OMCompiler/build/bin/../lib/x86_64-linux-gnu/omc/libOpenModelicaCompiler.so(EvaluateFunctions.predictIfOutput)
[bt] #557 /var/lib/hudson/slave/workspace/OpenModelica_TEST_LIBS/OpenModelica/OMCompiler/build/bin/../lib/x86_64-linux-gnu/omc/libOpenModelicaCompiler.so(EvaluateFunctions.evaluateFunctions_updateStatement)
[bt] #558 /var/lib/hudson/slave/workspace/OpenModelica_TEST_LIBS/OpenModelica/OMCompiler/build/bin/../lib/x86_64-linux-gnu/omc/libOpenModelicaCompiler.so(EvaluateFunctions.evaluateFunctions_updateAlgElements)
[bt] #559 /var/lib/hudson/slave/workspace/OpenModelica_TEST_LIBS/OpenModelica/OMCompiler/build/bin/../lib/x86_64-linux-gnu/omc/libOpenModelicaCompiler.so(boxptr_EvaluateFunctions_evaluateFunctions__updateAlgElements)
[bt] #560 /var/lib/hudson/slave/workspace/OpenModelica_TEST_LIBS/OpenModelica/OMCompiler/build/bin/../lib/x86_64-linux-gnu/omc/libOpenModelicaCompiler.so(List.mapFold3)
}}}
repeats endlessly.

The following test model:
{{{
model TestHelmholtzMedium_ph
  package Medium = HelmholtzMedia.HelmholtzFluids.Carbondioxide;
  Medium.AbsolutePressure p;
  Medium.SpecificEnthalpy h;
  Medium.ThermodynamicState state;
equation
  p = 1e5*(1+time);
  h = 1e5+50*time;
  state = Medium.setState_ph(p,h);
end TestHelmholtzMedium_ph;
}}}
reproduces the problem, while this other model
{{{
model TestHelmholtzMedium_pT
  package Medium = HelmholtzMedia.HelmholtzFluids.Carbondioxide;
  Medium.AbsolutePressure p;
  Medium.Temperature T;  
  Medium.ThermodynamicState state;
equation
  p = 1e5*(1+time);
  T = 300+50*time;
  state = Medium.setState_pT(p,T);
end TestHelmholtzMedium_pT;
}}}
passes the back-end successfully. It then fails due to #5167, but that's a separate issue. Please note you need {{{-d=newInst}}} to run both models, as the old front-end fails because of look-up issues.

When processing TestHelmholtzMedium_pT with {{{-d=optdaedump}}}, the following output is produced:
{{{
########################################
pre-optimization module resolveLoops (simulation)
########################################

unspecified partition
========================================

Variables (9)
========================================
1: state.s:VARIABLE(flow=false min = 0.52129 max = 1e+060 start = 5e+059 unit = ""J/(kg.K)"" nominal = 1000.0 )  ""Specific entropy of medium"" type: Real 
2: state.h:VARIABLE(flow=false min = -100000.0 max = 1300000.0 start = 600000.0 unit = ""J/kg"" nominal = 1000000.0 )  ""Specific enthalpy of medium"" type: Real 
3: state.u:VARIABLE(flow=false min = -100000000.0 max = 100000000.0 unit = ""J/kg"" nominal = 1000000.0 )  ""Specific inner energy of medium"" type: Real 
4: state.d:VARIABLE(flow=false min = 1e-060 max = 1638.9 start = 467.6 unit = ""kg/m3"" nominal = 1.0 )  ""Density of medium"" type: Real 
5: state.p:VARIABLE(flow=false min = 0.0 max = 801000000.0 start = 101325.0 unit = ""Pa"" nominal = 100000.0 )  ""Absolute pressure of medium"" type: Real 
6: state.T:VARIABLE(flow=false min = 216.592 max = 2000.0 start = 298.15 unit = ""K"" nominal = 300.0 )  ""Temperature of medium"" type: Real 
7: state.phase:DISCRETE(flow=false min = 0 max = 2 start = 0 )  ""Phase of the fluid: 1 for 1-phase, 2 for two-phase, 0 for not known, e.g., interactive use"" type: Integer 
8: T:VARIABLE(flow=false min = 216.592 max = 2000.0 start = 298.15 unit = ""K"" nominal = 300.0 )  type: Real 
9: p:VARIABLE(flow=false min = 0.0 max = 801000000.0 start = 101325.0 unit = ""Pa"" nominal = 100000.0 )  type: Real 

Equations (3, 9)
========================================
1/1 (1): p = 100000.0 * (1.0 + time)   [dynamic |0|0|0|0|]
2/2 (1): T = 300.0 + 50.0 * time   [dynamic |0|0|0|0|]
3/3 (7): state = TestHelmholtzMedium_pT.Medium.setState_pTX(p, T, {}, 0)   [dynamic |0|0|0|0|]

########################################
pre-optimization module evalFunc (simulation)
########################################


unspecified partition
========================================

Variables (8)
========================================
1: state.s:VARIABLE(flow=false min = 0.52129 max = 1e+060 start = 5e+059 unit = ""J/(kg.K)"" nominal = 1000.0 )  ""Specific entropy of medium"" type: Real 
2: state.h:VARIABLE(flow=false min = -100000.0 max = 1300000.0 start = 600000.0 unit = ""J/kg"" nominal = 1000000.0 )  ""Specific enthalpy of medium"" type: Real 
3: state.u:VARIABLE(flow=false min = -100000000.0 max = 100000000.0 unit = ""J/kg"" nominal = 1000000.0 )  ""Specific inner energy of medium"" type: Real 
4: state.d:VARIABLE(flow=false min = 1e-060 max = 1638.9 start = 467.6 unit = ""kg/m3"" nominal = 1.0 )  ""Density of medium"" type: Real 
5: state.p:VARIABLE(flow=false min = 0.0 max = 801000000.0 start = 101325.0 unit = ""Pa"" nominal = 100000.0 )  ""Absolute pressure of medium"" type: Real 
6: state.T:VARIABLE(flow=false min = 216.592 max = 2000.0 start = 298.15 unit = ""K"" nominal = 300.0 )  ""Temperature of medium"" type: Real 
7: T:VARIABLE(flow=false min = 216.592 max = 2000.0 start = 298.15 unit = ""K"" nominal = 300.0 )  type: Real 
8: p:VARIABLE(flow=false min = 0.0 max = 801000000.0 start = 101325.0 unit = ""Pa"" nominal = 100000.0 )  type: Real 


Equations (3, 8)
========================================
1/1 (1): p = 100000.0 * (1.0 + time)   [dynamic |0|0|0|0|]
2/2 (1): T = 300.0 + 50.0 * time   [dynamic |0|0|0|0|]
3/3 (6): (state.T, state.p, state.d, state.u, state.h, state.s) = estHelmholtzMedium_pT.Medium.setState_pTX_eval1(p, T, {}, 0)   [dynamic |0|0|0|0|]

BackendDAEType: simulation


Known variables only depending on parameters and constants - globalKnownVars (5)
========================================
1: Medium.fluidConstants.molarMass:CONST(flow=false )  ""Molar mass"" type: Real  [1]
2: Medium.fluidConstants.gasConstant:CONST(flow=false )  type: Real  [1]
3: Medium.fluidConstants.criticalTemperature:CONST(flow=false )  ""Critical temperature"" type: Real  [1]
4: Medium.fluidConstants.criticalMolarVolume:CONST(flow=false )  ""Critical molar Volume"" type: Real  [1]
5: state.phase:DISCRETE(flow=false min = 0 max = 2 start = 0 )  = 1  ""Phase of the fluid: 1 for 1-phase, 2 for two-phase, 0 for not known, e.g., interactive use"" type: Integer 
}}}
Apparently, the back-end can figure out that the discrete variable {{{state.phase}}} in the {{{setState_pT()}}} function has a fixed value (because it only contains one assignment {{{state.phase := 1}}} and factors that out, computing it as a constant. BTW, this is quite remarkable.

In the case of {{{TestHelmholtzMedium_ph}}}, the {{{state.phase}}} output is instead depending on the values of p and h, so this trick doesn't work. The following output is produced:
{{{
########################################
pre-optimization module resolveLoops (simulation)
########################################


unspecified partition
========================================

Variables (9)
========================================
1: state.s:VARIABLE(flow=false min = 0.52129 max = 1e+060 start = 5e+059 unit = ""J/(kg.K)"" nominal = 1000.0 )  ""Specific entropy of medium"" type: Real 
2: state.h:VARIABLE(flow=false min = -100000.0 max = 1300000.0 start = 600000.0 unit = ""J/kg"" nominal = 1000000.0 )  ""Specific enthalpy of medium"" type: Real 
3: state.u:VARIABLE(flow=false min = -100000000.0 max = 100000000.0 unit = ""J/kg"" nominal = 1000000.0 )  ""Specific inner energy of medium"" type: Real 
4: state.d:VARIABLE(flow=false min = 1e-060 max = 1638.9 start = 467.6 unit = ""kg/m3"" nominal = 1.0 )  ""Density of medium"" type: Real 
5: state.p:VARIABLE(flow=false min = 0.0 max = 801000000.0 start = 101325.0 unit = ""Pa"" nominal = 100000.0 )  ""Absolute pressure of medium"" type: Real 
6: state.T:VARIABLE(flow=false min = 216.592 max = 2000.0 start = 298.15 unit = ""K"" nominal = 300.0 )  ""Temperature of medium"" type: Real 
7: state.phase:DISCRETE(flow=false min = 0 max = 2 start = 0 )  ""Phase of the fluid: 1 for 1-phase, 2 for two-phase, 0 for not known, e.g., interactive use"" type: Integer 
8: h:VARIABLE(flow=false min = -100000.0 max = 1300000.0 start = 600000.0 unit = ""J/kg"" nominal = 1000000.0 )  type: Real 
9: p:VARIABLE(flow=false min = 0.0 max = 801000000.0 start = 101325.0 unit = ""Pa"" nominal = 100000.0 )  type: Real 


Equations (3, 9)
========================================
1/1 (1): p = 100000.0 * (1.0 + time)   [dynamic |0|0|0|0|]
2/2 (1): h = 100000.0 + 50.0 * time   [dynamic |0|0|0|0|]
3/3 (7): state = TestHelmholtzMedium_ph.Medium.setState_phX(p, h, {}, 0)   [dynamic |0|0|0|0|]
}}}
then the execution hangs.

From what I understand, there is some issue with the mixed nature of the {{{state}}} output which contains both Real and Integer variables. The back-end tries to figure out the value of {{{state.phase}}}, and in doing so enters an infinite recursion, thus breaking the evalFunc module.

@lochel, can you please have a look at this issue, assess how easy/hard it is to fix it, and possibly re-assign it to someone else who is competent on this part of the back-end?"	defect	closed	critical	1.13.0	Backend		fixed		Lennart Ochel Willi Braun m.thorade@…
