﻿id	summary	reporter	owner	description	type	status	priority	milestone	component	version	resolution	keywords	cc
6076	The enclosed code translates, Test2 can be instantiated, but compilation fails. Test runs	Anton.Soppelsa@eurac.edu	Per Östlund	"Dear all,

trying to encapsulate an external object class (TSModelHandle) within an interface class (TSModelC, allowing, for example, to derive its icon from the graphic classes) I get a compilation error building Test2 (Test runs correctly). Despite my environment is complex and you will not be able to reproduce the problem yourself (the external object is an interface layer to Python3 code), I think that the compiler complains could be indicative enough for somebody of you to see immediately if my attempt is fundamentally flawed. If not, there could be a problem in the code generation.

This happen with version 1.14.1 and 1.16.0-dev.03. IF you need me to do some testing I will be happy to do it.

Sincerely,
  Anton
 
{{{
within SHCControls;

package TSModel
  extends Modelica.Icons.TypesPackage;

  class TSModelHandle
    extends ExternalObject;

    function constructor ""Gets an handle to a TSModel object loaded from file.""
      extends Modelica.Icons.Function;
      input String fileName;
      input String modelName;
      output TSModelHandle tsmh;
    
      external ""C"" tsmh = initTSModel(fileName, modelName) annotation(
        Library = ""TSModel-Interface"",
        LibraryDirectory = ""file:///C:/Development/shc-controls/SHCLib/C-Cpp/Cpp-ModelicaInterface/Build/Release"");
    end constructor;

    //C:\\Development\\shc-controls\\SHCLib\\C-Cpp\\Cpp-ModelicaInterface\\Build\\Release

    function destructor ""Release storage of the TSModel object.""
      extends Modelica.Icons.Function;
      input TSModelHandle tsmh;
    
      external ""C"" finalTSModel(tsmh) annotation(
        Library = ""TSModel-Interface"",
        LibraryDirectory = ""file:///C:/Development/shc-controls/SHCLib/C-Cpp/Cpp-ModelicaInterface/Build/Release"");
    end destructor;

    //      external ""C"" finalTSModel(tsmh) annotation(Library=""TSModel-Interface""); // If not specified looks in the default dir Library/win64
  end TSModelHandle;

  function tsmEval ""Evaluate model at point u""
    extends Modelica.Icons.Function;
    input TSModelHandle tsmh;
    input Real u[:];
    output Real y[ysize];
    input Integer ysize;
  
    external ""C"" tsmEval(tsmh, u, size(u, 1), y, size(y, 1)) annotation(
      Library = ""TSModel-Interface"",
      LibraryDirectory = ""file:///Development/shc-controls/SHCLib/C-Cpp/Cpp-ModelicaInterface/Build/Release"");
  end tsmEval;

  function invTsmEval ""Evaluate inverse model at point y""
    extends Modelica.Icons.Function;
    input TSModelHandle tsmh;
    input Real u0[:];
    input Real y[:];
    output Real u[size(u0, 1)];
  
    external ""C"" tsmEvalInv(tsmh, u0, size(u0, 1), y, size(y, 1), u, size(u, 1)) annotation(
      Library = ""TSModel-Interface"",
      LibraryDirectory = ""file:///Development/shc-controls/SHCLib/C-Cpp/Cpp-ModelicaInterface/Build/Release"");
  end invTsmEval;

  model Test ""Test of the TSModel package""
    import Modelica.Blocks.Sources.{Step,Constant};
    import Modelica.Blocks.Routing.{Multiplex2};
    TSModelHandle tsmh = TSModelHandle(fileName = ""C:\\Development\\shc-controls\\SHCLib\\Python\\tests\\cannonau2x2.mat"", modelName = ""Cannonau"");
    // call initTSModel
    Real u0[2] = {0.4, 75e3};
    Real u[2];
    Real y[2];
    Real uTilde[2];
    Step T1R(height = 5, offset = 273.15 + 50, startTime = 10) annotation(
      Placement(visible = true, transformation(origin = {-70, 70}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
    Step M1R(height = 0.03, offset = 0.3, startTime = 20) annotation(
      Placement(visible = true, transformation(origin = {-70, 30}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
    Step U(height = 0.05, offset = 0.5, startTime = 15) annotation(
      Placement(visible = true, transformation(origin = {-70, -30}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
    Step DP(height = 1000, offset = 50000, startTime = 15) annotation(
      Placement(visible = true, transformation(origin = {-70, -70}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
    Multiplex2 muxR annotation(
      Placement(visible = true, transformation(origin = {-30, 50}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
    Multiplex2 muxU annotation(
      Placement(visible = true, transformation(origin = {-30, -50}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
  equation
    u = muxU.y;
    y = tsmEval(tsmh, u, size(y, 1));
    uTilde = invTsmEval(tsmh, u0, muxR.y);
    connect(U.y, muxU.u1[1]) annotation(
      Line(points = {{-58, -30}, {-50, -30}, {-50, -44}, {-42, -44}, {-42, -44}}, color = {0, 0, 127}));
    connect(DP.y, muxU.u2[1]) annotation(
      Line(points = {{-58, -70}, {-50, -70}, {-50, -56}, {-42, -56}, {-42, -56}}, color = {0, 0, 127}));
    connect(M1R.y, muxR.u2[1]) annotation(
      Line(points = {{-59, 30}, {-50, 30}, {-50, 44}, {-42, 44}}, color = {0, 0, 127}));
    connect(T1R.y, muxR.u1[1]) annotation(
      Line(points = {{-59, 70}, {-50, 70}, {-50, 56}, {-42, 56}}, color = {0, 0, 127}));
    annotation(
      experiment(StartTime = 0, StopTime = 50, Tolerance = 1e-06, Interval = 0.1));
  end Test;

  class TSModelC
    parameter String fileName = ""C:\\Development\\shc-controls\\SHCLib\\Python\\tests\\cannonau2x2.mat"";
    parameter String modelName = ""Cannonau"";
    parameter TSModelHandle tsmh = TSModelHandle(fileName, modelName);

    function eval ""Evaluate model at point u""
      extends Modelica.Icons.Function;
      input Real u[:];
      output Real y[ysize];
      input Integer ysize;
    algorithm
      y := tsmEval(tsmh, u, size(y, 1));
    end eval;

    function evalInv ""Evaluate inverse model at point y with initial guess u0""
      extends Modelica.Icons.Function;
      input Real u0[:];
      input Real y[:];
      output Real u[size(u0, 1)];
    algorithm
      y := invTsmEval(tsmh, u0, y);
    end evalInv;
    annotation(
      Icon(graphics = {Rectangle(fillColor = {255, 255, 255}, fillPattern = FillPattern.Solid, extent = {{-100, 100}, {100, -100}}), Text(origin = {0, -130}, lineColor = {0, 0, 255}, fillColor = {0, 0, 255}, extent = {{-100, -20}, {100, 20}}, textString = ""%name""), Text(origin = {0, -40}, extent = {{-100, -20}, {100, 20}}, textString = ""Ext Obj""), Text(origin = {0, 40}, extent = {{-100, -20}, {100, 20}}, textString = ""TSModel"")}, coordinateSystem(initialScale = 0.1)));
  end TSModelC;

  model Test2 ""Test of the TSModelC class""
    SHCControls.TSModel.TSModelC tsm(fileName = ""C:\\Development\\shc-controls\\SHCLib\\Python\\tests\\cannonau2x2.mat"", modelName = ""Cannonau"") annotation(
      Placement(visible = true, transformation(origin = {0, 0}, extent = {{-20, -20}, {20, 20}}, rotation = 0)));
    Real u[2] = {0.4, 75e3};
    Real y[2];
    Real uTilde[2];
  equation
    y = tsm.eval(u, size(y, 1));
    uTilde = tsm.evalInv({0.5, 75e3}, y);
  end Test2;
end TSModel;
}}}

Compilation errors:
{{{
C:/Program Files/OpenModelica1.16.0-dev.03-64bit/share/omc/scripts/Compile.bat SHCControls.TSModel.Test2 gcc mingw64 parallel 6 0
PATH = ""C:\PROGRA~1\OPENMO~1.03-\tools\msys\mingw64\bin;C:\PROGRA~1\OPENMO~1.03-\tools\msys\mingw64\bin\..\..\usr\bin;""
mingw32-make: Entering directory 'C:/Users/ASOPPE~1/AppData/Local/Temp/OPENMO~1/OMEdit/SHCCON~2.TES'
gcc  -Os -falign-functions -fno-ipa-pure-const -mstackrealign -msse2 -mfpmath=sse     -I""C:/Program Files/OpenModelica1.16.0-dev.03-64bit/include/omc/c"" -I. -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME -DOMC_MODEL_PREFIX=SHCControls_TSModel_Test2 -DOMC_NUM_MIXED_SYSTEMS=0 -DOMC_NUM_LINEAR_SYSTEMS=0 -DOMC_NUM_NONLINEAR_SYSTEMS=0 -DOMC_NDELAY_EXPRESSIONS=0 -DOMC_NVAR_STRING=0  -c -o SHCControls.TSModel.Test2.o SHCControls.TSModel.Test2.c
gcc  -Os -falign-functions -fno-ipa-pure-const -mstackrealign -msse2 -mfpmath=sse     -I""C:/Program Files/OpenModelica1.16.0-dev.03-64bit/include/omc/c"" -I. -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME -DOMC_MODEL_PREFIX=SHCControls_TSModel_Test2 -DOMC_NUM_MIXED_SYSTEMS=0 -DOMC_NUM_LINEAR_SYSTEMS=0 -DOMC_NUM_NONLINEAR_SYSTEMS=0 -DOMC_NDELAY_EXPRESSIONS=0 -DOMC_NVAR_STRING=0  -c -o SHCControls.TSModel.Test2_functions.o SHCControls.TSModel.Test2_functions.c
gcc  -Os -falign-functions -fno-ipa-pure-const -mstackrealign -msse2 -mfpmath=sse     -I""C:/Program Files/OpenModelica1.16.0-dev.03-64bit/include/omc/c"" -I. -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME -DOMC_MODEL_PREFIX=SHCControls_TSModel_Test2 -DOMC_NUM_MIXED_SYSTEMS=0 -DOMC_NUM_LINEAR_SYSTEMS=0 -DOMC_NUM_NONLINEAR_SYSTEMS=0 -DOMC_NDELAY_EXPRESSIONS=0 -DOMC_NVAR_STRING=0  -c -o SHCControls.TSModel.Test2_records.o SHCControls.TSModel.Test2_records.c
gcc  -Os -falign-functions -fno-ipa-pure-const -mstackrealign -msse2 -mfpmath=sse     -I""C:/Program Files/OpenModelica1.16.0-dev.03-64bit/include/omc/c"" -I. -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME -DOMC_MODEL_PREFIX=SHCControls_TSModel_Test2 -DOMC_NUM_MIXED_SYSTEMS=0 -DOMC_NUM_LINEAR_SYSTEMS=0 -DOMC_NUM_NONLINEAR_SYSTEMS=0 -DOMC_NDELAY_EXPRESSIONS=0 -DOMC_NVAR_STRING=0  -c -o SHCControls.TSModel.Test2_01exo.o SHCControls.TSModel.Test2_01exo.c
gcc  -Os -falign-functions -fno-ipa-pure-const -mstackrealign -msse2 -mfpmath=sse     -I""C:/Program Files/OpenModelica1.16.0-dev.03-64bit/include/omc/c"" -I. -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME -DOMC_MODEL_PREFIX=SHCControls_TSModel_Test2 -DOMC_NUM_MIXED_SYSTEMS=0 -DOMC_NUM_LINEAR_SYSTEMS=0 -DOMC_NUM_NONLINEAR_SYSTEMS=0 -DOMC_NDELAY_EXPRESSIONS=0 -DOMC_NVAR_STRING=0  -c -o SHCControls.TSModel.Test2_02nls.o SHCControls.TSModel.Test2_02nls.c
gcc  -Os -falign-functions -fno-ipa-pure-const -mstackrealign -msse2 -mfpmath=sse     -I""C:/Program Files/OpenModelica1.16.0-dev.03-64bit/include/omc/c"" -I. -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME -DOMC_MODEL_PREFIX=SHCControls_TSModel_Test2 -DOMC_NUM_MIXED_SYSTEMS=0 -DOMC_NUM_LINEAR_SYSTEMS=0 -DOMC_NUM_NONLINEAR_SYSTEMS=0 -DOMC_NDELAY_EXPRESSIONS=0 -DOMC_NVAR_STRING=0  -c -o SHCControls.TSModel.Test2_03lsy.o SHCControls.TSModel.Test2_03lsy.c
SHCControls.TSModel.Test2_functions.c: In function 'omc_SHCControls_TSModel_Test2_tsm_eval':
SHCControls.TSModel.Test2_functions.c:99:68: error: '_tsm' undeclared (first use in this function)
   copy_real_array_data(omc_SHCControls_TSModel_tsmEval(threadData, _tsm._tsmh, _u, tmp1), &_y);
                                                                    ^
SHCControls.TSModel.Test2_functions.c:99:68: note: each undeclared identifier is reported only once for each function it appears in
SHCControls.TSModel.Test2_functions.c: In function 'omc_SHCControls_TSModel_Test2_tsm_evalInv':
SHCControls.TSModel.Test2_functions.c:122:71: error: '_tsm' undeclared (first use in this function)
   copy_real_array_data(omc_SHCControls_TSModel_invTsmEval(threadData, _tsm._tsmh, _u0, _y), &_y);
                                                                       ^
<builtin>: recipe for target 'SHCControls.TSModel.Test2_functions.o' failed
mingw32-make: *** [SHCControls.TSModel.Test2_functions.o] Error 1
mingw32-make: *** Waiting for unfinished jobs....
mingw32-make: Leaving directory 'C:/Users/ASOPPE~1/AppData/Local/Temp/OPENMO~1/OMEdit/SHCCON~2.TES'
Compilation process failed. Exited with code 2.
}}}
"	defect	assigned	high	Future	Code Generation	v1.16.0-dev			
