Skip to content

Commit

Permalink
Fix Cpp external F77 calls with string arguments and return values
Browse files Browse the repository at this point in the history
  • Loading branch information
rfranke authored and sjoelund committed Jun 28, 2021
1 parent 3d50ca5 commit 93240f5
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 14 deletions.
11 changes: 7 additions & 4 deletions OMCompiler/Compiler/Template/CodegenCpp.tpl
Expand Up @@ -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;


Expand Down Expand Up @@ -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
Expand All @@ -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;

Expand Down Expand Up @@ -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%>
>>
Expand Down
11 changes: 7 additions & 4 deletions OMCompiler/Compiler/Template/CodegenCppOld.tpl
Expand Up @@ -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;


Expand Down Expand Up @@ -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
Expand All @@ -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;

Expand Down Expand Up @@ -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%>
>>
Expand Down
16 changes: 10 additions & 6 deletions testsuite/openmodelica/cppruntime/solveTest.mos
Expand Up @@ -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;
");
Expand All @@ -23,6 +25,7 @@ getErrorString();
simulate(SolveTest);
val(x[1], 0);
val(x[2], 0);
val(r, 0);
getErrorString();

// Result:
Expand All @@ -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

0 comments on commit 93240f5

Please sign in to comment.