﻿id	summary	reporter	owner	description	type	status	priority	milestone	component	version	resolution	keywords	cc
2569	Problem with code generation - external functions with records	Francesco Casella	Adrian Pop	"Please try to compile the TestStatesSat model below
{{{
package TweakedExternalMedia
  package TestMedium
    extends ExternalMedia.Media.TestMedium(
      externalFluidConstants = FluidConstants(
       iupacName=  ""unknown"",
       casRegistryNumber=  ""unknown"",
       chemicalFormula=  ""unknown"",
       structureFormula=  ""unknown"",
       molarMass=  0.028,
       criticalTemperature= 623.0,
       criticalPressure=  221e5,
       criticalMolarVolume= 15,
       acentricFactor=  0,
        triplePointTemperature=  280.0,
        triplePointPressure=  500.0,
        meltingPoint=  280,
        normalBoilingPoint=  380.0,
        dipoleMoment=  2.0));
  end TestMedium;

  model TestStatesSat 
    ""Test case using TestMedium with state + sat records""
    extends ExternalMedia.Test.TestMedium.TestStatesSat(
       redeclare package Medium = TweakedExternalMedia.TestMedium);
  end TestStatesSat;
end TweakedExternalMedia;
}}}
using ExternalMedia from the MA repo
​https://svn.modelica.org/projects/ExternalMediaLibrary

The executable code is generated, but the results in the state records state1 and state2 are all garbled up.

Some of the struct definitions in the C code (e.g. {{{typedef struct ExternalMedia_Test_TestMedium_GenericModels_CompleteThermodynamicState_Medium_ThermodynamicState_s}}}) contain int phase as the first element of the record, while the Modelica definition has it at the second to last place. Note that the Modelica spec, section 12.9.1.3, states: ""The elements of the Modelica record class are declared in the same order in the C struct"". For some reason, this is not the case here, as the Integer phase has been moved in pole position.

I have tried to replicate the problem in a simpler setting:
{{{
package TestExternalFunction
  record R
    Real y;
    Integer z;
    Real x;
  end R;

  function compute
    input Real u;
    output R r;
    external ""C"" compute(u, r)
    annotation(Include=
    ""typedef struct R{double y;int z;double x;} R;
  void compute(double u, R* r) {
    r->x = u;
    r->z = 12;
    r->y = 3.0*u;
    }"");
  end compute;

  model Test
    R r;
  equation 
    r = compute(time);
  end Test;
end TestExternalFunction;
}}}

but in this case the struct is declared in the right order, and correct results are obtained. Please help me sort this out, as it might be the last hurdle before we get ExternalMedia to run in OMC"	defect	closed	critical	1.9.1	Code Generation	trunk	fixed		Martin Sjölund
