From 93240f54eb183159e4c3eb3820effd36c35d229e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=BCdiger=20Franke?= Date: Mon, 28 Jun 2021 13:51:26 +0200 Subject: [PATCH] Fix Cpp external F77 calls with string arguments and return values --- OMCompiler/Compiler/Template/CodegenCpp.tpl | 11 +++++++---- OMCompiler/Compiler/Template/CodegenCppOld.tpl | 11 +++++++---- testsuite/openmodelica/cppruntime/solveTest.mos | 16 ++++++++++------ 3 files changed, 24 insertions(+), 14 deletions(-) diff --git a/OMCompiler/Compiler/Template/CodegenCpp.tpl b/OMCompiler/Compiler/Template/CodegenCpp.tpl index c85709d3e9e..905d1c588a4 100644 --- a/OMCompiler/Compiler/Template/CodegenCpp.tpl +++ b/OMCompiler/Compiler/Template/CodegenCpp.tpl @@ -4710,7 +4710,7 @@ case EXTERNAL_FUNCTION(__) then separator="\n") match language case "C" then extFunCallC(fun, &preExp, &varDecls, &inputAssign, &outputAssign, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation, useTuple) - case "FORTRAN 77" then extFunCallF77(fun, &preExp, &varDecls, &inputAssign, &outputAssign, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation, useTuple) + case "FORTRAN 77" then extFunCallF77(fun, &preExp, &varDecls, &inputAssign, &outputAssign, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) end extFunCall; @@ -4945,7 +4945,7 @@ template extFunCallF77(Function fun, Text &preExp, Text &varDecls, Text &inputAssign, Text &outputAssign, SimCode simCode, Text& extraFuncs, Text& extraFuncsDecl, Text extraFuncsNamespace, Text stateDerVectorName /*=__zDot*/, - Boolean useFlatArrayNotation, Boolean useTuple) + Boolean useFlatArrayNotation) "Generates the call to an external F77 function." ::= match fun @@ -4961,7 +4961,6 @@ template extFunCallF77(Function fun, Text &preExp, let returnAssign = if returnVar then '<%returnVar%> = ' << <%returnAssign%><%extFunctionName(extName, language)%>(<%args%>); - <%match useTuple case false then '<%funName%> = <%returnVar%>;'%> >> end extFunCallF77; @@ -4999,7 +4998,11 @@ template extArgF77(SimExtArg extArg, Text &preExp, Text &varDecls, let varType = expTypeShort(t) let extType = extTypeF77(t, false, false) let extName = '<%varName%>_ext' - if stringEq(varType, extType) then + if stringEq(varType, "string") then + << + <%varName%>.c_str() + >> + else if stringEq(varType, extType) then << &<%varName%> >> diff --git a/OMCompiler/Compiler/Template/CodegenCppOld.tpl b/OMCompiler/Compiler/Template/CodegenCppOld.tpl index bf24b01120b..041864baf8e 100644 --- a/OMCompiler/Compiler/Template/CodegenCppOld.tpl +++ b/OMCompiler/Compiler/Template/CodegenCppOld.tpl @@ -4939,7 +4939,7 @@ case EXTERNAL_FUNCTION(__) then separator="\n") match language case "C" then extFunCallC(fun, &preExp, &varDecls, &inputAssign, &outputAssign, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation, useTuple) - case "FORTRAN 77" then extFunCallF77(fun, &preExp, &varDecls, &inputAssign, &outputAssign, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation, useTuple) + case "FORTRAN 77" then extFunCallF77(fun, &preExp, &varDecls, &inputAssign, &outputAssign, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) end extFunCall; @@ -5174,7 +5174,7 @@ template extFunCallF77(Function fun, Text &preExp, Text &varDecls, Text &inputAssign, Text &outputAssign, SimCode simCode, Text& extraFuncs, Text& extraFuncsDecl, Text extraFuncsNamespace, Text stateDerVectorName /*=__zDot*/, - Boolean useFlatArrayNotation, Boolean useTuple) + Boolean useFlatArrayNotation) "Generates the call to an external F77 function." ::= match fun @@ -5190,7 +5190,6 @@ template extFunCallF77(Function fun, Text &preExp, let returnAssign = if returnVar then '<%returnVar%> = ' << <%returnAssign%><%extFunctionName(extName, language)%>(<%args%>); - <%match useTuple case false then '<%funName%> = <%returnVar%>;'%> >> end extFunCallF77; @@ -5228,7 +5227,11 @@ template extArgF77(SimExtArg extArg, Text &preExp, Text &varDecls, let varType = expTypeShort(t) let extType = extTypeF77(t, false, false) let extName = '<%varName%>_ext' - if stringEq(varType, extType) then + if stringEq(varType, "string") then + << + <%varName%>.c_str() + >> + else if stringEq(varType, extType) then << &<%varName%> >> diff --git a/testsuite/openmodelica/cppruntime/solveTest.mos b/testsuite/openmodelica/cppruntime/solveTest.mos index 4025b658fd3..c8480949696 100644 --- a/testsuite/openmodelica/cppruntime/solveTest.mos +++ b/testsuite/openmodelica/cppruntime/solveTest.mos @@ -9,12 +9,14 @@ setCommandLineOptions("+simCodeTarget=Cpp"); loadModel(Modelica, {"3.2.3"}); loadString(" model SolveTest - Real[:, :] A = [0, 1; 2, 3]; - Real[:] b = {4, 5}; input Real u = 0; // prevent presolving during translation + Real[:, :] A = [u + 0, u + 1; u + 2, u + 3]; + Real[:] b = {4, 5}; output Real[2] x; + output Real r; equation - x = Modelica.Math.Matrices.solve(A, (u + 1) * b); + x = Modelica.Math.Matrices.solve(A, b); // general LAPACK call (dgesv) + r = Modelica.Math.Matrices.rcond(A); // string arg (dgcon) and return val (dlange) annotation(experiment(StopTime = 0)); end SolveTest; "); @@ -23,6 +25,7 @@ getErrorString(); simulate(SolveTest); val(x[1], 0); val(x[2], 0); +val(r, 0); getErrorString(); // Result: @@ -31,11 +34,12 @@ getErrorString(); // true // "" // record SimulationResult -// resultFile = "SolveTest_res.mat", -// simulationOptions = "startTime = 0.0, stopTime = 0.0, numberOfIntervals = 500, tolerance = 1e-06, method = 'dassl', fileNamePrefix = 'SolveTest', options = '', outputFormat = 'mat', variableFilter = '.*', cflags = '', simflags = ''", -// messages = "" +// resultFile = "SolveTest_res.mat", +// simulationOptions = "startTime = 0.0, stopTime = 0.0, numberOfIntervals = 500, tolerance = 1e-06, method = 'dassl', fileNamePrefix = 'SolveTest', options = '', outputFormat = 'mat', variableFilter = '.*', cflags = '', simflags = ''", +// messages = "" // end SimulationResult; // -3.5 // 4.0 +// 0.1 // "" // endResult