﻿id	summary	reporter	owner	description	type	status	priority	milestone	component	version	resolution	keywords	cc
5298	Issue with the derivative annotation in the NF produces grossly incorrect results	Francesco Casella	Per Östlund	"Please check [https://libraries.openmodelica.org/branches/newInst/Modelica_trunk/files/Modelica_trunk_Modelica.Fluid.Examples.TraceSubstances.RoomCO2.sim Modelica.Fluid.Examples.TraceSubstances.RoomCO2]. The simulation fails after about 20 seconds. Analysis of the transient reveals that the temperature in the volume unexpectedly increases above 300 degC, while it is supposed to remain roughly constant trhough the transient, which mainly involves the concentration of CO2. 

Further analysis of the {{{-d=optdaedump}}} output reveals that in the process of changing the state variables from {{{volume.m, volume.U}}} to {{{volume.medium.p, volume.medium.T}}}, carried out via the Pantelides algorithm, when the flattening is carried out by NF, the backend computes
{{{
boundary1.ports[2].h_outflow = 
  Modelica.Fluid.Examples.TraceSubstances.RoomCO2.Medium.specificEnthalpy_pTX(
  volume.medium.p, 293.15, {0.01})
der(volume.ports[2].h_outflow) = 0.0
}}}
This is plain wrong and causes the derivatives of pressure and temperature to be computed in a grossly incorrect way, leading to a totally unphysical transient.

When the OF is used, the Pantelides algorithm instead correctly computes
{{{
boundary1.ports[2].h_outflow = 
  Modelica.Fluid.Sources.MassFlowSource_T$boundary1.Medium.specificEnthalpy_pTX(
    volume.medium.p, 293.15, {0.01})
der(volume.ports[2].h_outflow) = 
  Modelica.Fluid.Vessels.ClosedVolume$volume.Medium.h_pTX_der(
  volume.medium.p, volume.medium.T, {volume.medium.Xi[1], 
  1.0 - volume.medium.Xi[1]}, der(volume.medium.p), 
  der(volume.medium.T), {der(volume.medium.Xi[1]), 
  -der(volume.medium.Xi[1])})
}}}
and eventually produces the correct simulation results.

It is apparent that
- the enthalpy function is flattened differently in the two cases
- its derivative is correctly computed when starting from the OF, while it is computed to zero when starting from the NF.

Unfortunately the flat output is stripped of annotations, so I can't see if the Derivative annotation was flattened correctly, but I guess there's some problem with it.

@perost, can you please investigate and report?"	defect	new	high	2.0.0	New Instantiation				
