Skip to content

Commit

Permalink
Various FMU fixes (#11904)
Browse files Browse the repository at this point in the history
* Various FMU fixes
- Use the last identifier in the model path for the FMU build #11554
- Build FMUs in parallel with the specified number of processors #11808
- Build FMUs with the specified CC and CXX #11808

* Variable name
  • Loading branch information
adrpo committed Jan 31, 2024
1 parent aa78119 commit b97f9c5
Show file tree
Hide file tree
Showing 17 changed files with 53 additions and 31 deletions.
40 changes: 31 additions & 9 deletions OMCompiler/Compiler/Script/CevalScriptBackend.mo
Expand Up @@ -121,6 +121,7 @@ import SCodeUtil;
import SemanticVersion;
import Settings;
import SimCodeMain;
import SimCodeFunctionUtil;
import SimpleModelicaParser;
import SimulationResults;
import StaticScript;
Expand Down Expand Up @@ -3473,6 +3474,22 @@ algorithm
className, inFileNamePrefix, runBackend, Flags.getConfigBool(Flags.DAE_MODE), runSilent, inSimSettingsOpt, Absyn.FUNCTIONARGS({},{}));
end callTranslateModel;
protected function getProcsStr
input Boolean isMake = false;
output String s;
protected
Integer n;
String sn;
algorithm
n := Flags.getConfigInt(Flags.NUM_PROC);
sn := intString(n);
s := if (n == 0)
then ""
else (if isMake
then sn
else stringAppend("-j", sn));
end getProcsStr;
protected function configureFMU_cmake
"Configure and build binaries with CMake for target platform"
input String platform;
Expand All @@ -3485,10 +3502,15 @@ protected
String fmuSourceDir;
String CMAKE_GENERATOR = "", CMAKE_BUILD_TYPE;
String quote, dquote, defaultFmiIncludeDirectoy;
String CC, CXX;
SimCodeFunction.MakefileParams makefileParams;
algorithm
makefileParams := SimCodeFunctionUtil.createMakefileParams({}, {}, {}, false, true);
fmuSourceDir := fmutmp+"/sources/";
quote := "'";
dquote := if isWindows then "\"" else "'";
CC := "-DCMAKE_C_COMPILER=" + dquote + makefileParams.ccompiler + dquote;
CXX := "-DCMAKE_CXX_COMPILER=" + dquote + makefileParams.cxxcompiler + dquote;
defaultFmiIncludeDirectoy := dquote + Settings.getInstallationDirectoryPath() + "/include/omc/c/fmi" + dquote;

// Set build type
Expand Down Expand Up @@ -3522,15 +3544,15 @@ algorithm
end if;
buildDir := "build_cmake_dynamic";
cmakeCall := Autoconf.cmake + " " + CMAKE_GENERATOR +
CMAKE_BUILD_TYPE +
CMAKE_BUILD_TYPE + " " + CC + " " + CXX +
" ..";
cmd := "cd " + dquote + fmuSourceDir + dquote + " && " +
"mkdir " + buildDir + " && cd " + buildDir + " && " +
cmakeCall + " && " +
Autoconf.cmake + " --build . --target install && " +
Autoconf.cmake + " --build . --parallel " + getProcsStr() + " --target install && " +
"cd .. && rm -rf " + buildDir;
if 0 <> System.systemCall(cmd, outFile=logfile) then
Error.addMessage(Error.SIMULATOR_BUILD_ERROR, {System.readFile(logfile)});
Error.addMessage(Error.SIMULATOR_BUILD_ERROR, {"cmd: " + cmd + "\n" + System.readFile(logfile)});
fail();
end if;
then();
Expand All @@ -3541,15 +3563,15 @@ algorithm
end if;
buildDir := "build_cmake_dynamic";
cmakeCall := Autoconf.cmake + " " + CMAKE_GENERATOR +
CMAKE_BUILD_TYPE +
CMAKE_BUILD_TYPE + " " + CC + " " + CXX +
" ..";
cmd := "cd " + dquote + fmuSourceDir + dquote + " && " +
"mkdir " + buildDir + " && cd " + buildDir + " && " +
cmakeCall + " && " +
Autoconf.cmake + " --build . --target install && " +
Autoconf.cmake + " --build . --parallel " + getProcsStr() + " --target install && " +
"cd .. && rm -rf " + buildDir;
if 0 <> System.systemCall(cmd, outFile=logfile) then
Error.addMessage(Error.SIMULATOR_BUILD_ERROR, {System.readFile(logfile)});
Error.addMessage(Error.SIMULATOR_BUILD_ERROR, {"cmd: " + cmd + "\n" + System.readFile(logfile)});
fail();
end if;
then();
Expand Down Expand Up @@ -3621,7 +3643,7 @@ algorithm
"cd " + dquote + "/fmu/" + fmuSourceDir + dquote + " && " +
"mkdir " + buildDir + " && cd " + buildDir + " && " +
cmakeCall + " && " +
"cmake --build . && make install && " +
"cmake --build . && make " + getProcsStr(true) + " install && " +
"cd .. && rm -rf " + buildDir +
dquote;
runDockerCmd(cmd, dockerLogFile, cleanup=true, volumeID=volumeID, containerID=containerID);
Expand Down Expand Up @@ -3979,8 +4001,8 @@ algorithm

// NOTE: The FMUs use fileNamePrefix for the internal name when it would be expected to be fileNamePrefix that decides the .fmu filename
// The scripting environment from a user's perspective is like that. fmuTargetName is the name of the .fmu in the templates, etc.
filenameprefix := Util.stringReplaceChar(if inFileNamePrefix == "<default>" then AbsynUtil.pathString(className) else inFileNamePrefix, ".", "_");
fmuTargetName := if FMUVersion == "1.0" then filenameprefix else (if inFileNamePrefix == "<default>" then AbsynUtil.pathString(className) else inFileNamePrefix);
filenameprefix := Util.stringReplaceChar(if inFileNamePrefix == "<default>" then AbsynUtil.pathLastIdent(className) else inFileNamePrefix, ".", "_");
fmuTargetName := if FMUVersion == "1.0" then filenameprefix else (if inFileNamePrefix == "<default>" then AbsynUtil.pathLastIdent(className) else inFileNamePrefix);
if isSome(inSimSettings) then
SOME(simSettings) := inSimSettings;
else
Expand Down
4 changes: 2 additions & 2 deletions testsuite/omsimulator/DualMassOscillator_cs.mos
Expand Up @@ -4,8 +4,8 @@

loadFile("DualMassOscillator.mo"); getErrorString();

buildModelFMU(DualMassOscillator.System1, version="2.0", fmuType="cs", platforms={"static"}); getErrorString();
buildModelFMU(DualMassOscillator.System2, version="2.0", fmuType="cs", platforms={"static"}); getErrorString();
buildModelFMU(DualMassOscillator.System1, version="2.0", fmuType="cs", fileNamePrefix="DualMassOscillator.System1", platforms={"static"}); getErrorString();
buildModelFMU(DualMassOscillator.System2, version="2.0", fmuType="cs", fileNamePrefix="DualMassOscillator.System2", platforms={"static"}); getErrorString();

writeFile("DualMassOscillator_cs.lua", "
oms_setCommandLineOption(\"--suppressPath=true\")
Expand Down
4 changes: 2 additions & 2 deletions testsuite/omsimulator/DualMassOscillator_me.mos
Expand Up @@ -4,8 +4,8 @@

loadFile("DualMassOscillator.mo"); getErrorString();

buildModelFMU(DualMassOscillator.System1, version="2.0", fmuType="me", platforms={"static"}); getErrorString();
buildModelFMU(DualMassOscillator.System2, version="2.0", fmuType="me", platforms={"static"}); getErrorString();
buildModelFMU(DualMassOscillator.System1, version="2.0", fmuType="me", fileNamePrefix="DualMassOscillator.System1", platforms={"static"}); getErrorString();
buildModelFMU(DualMassOscillator.System2, version="2.0", fmuType="me", fileNamePrefix="DualMassOscillator.System2", platforms={"static"}); getErrorString();

writeFile("DualMassOscillator_me.lua", "
oms_setCommandLineOption(\"--suppressPath=true\")
Expand Down
Expand Up @@ -3,7 +3,7 @@
// teardown_command: rm -rf Modelica.Mechanics.MultiBody.Examples.Elementary.Pendulum.fmu Modelica_Mechanics_MultiBody_Examples_Elementary_Pendulum.log Pendulum_systemCall.log modelDescription.xml model_res.mat

loadModel(Modelica); getErrorString();
buildModelFMU(Modelica.Mechanics.MultiBody.Examples.Elementary.Pendulum, version="2.0", fmuType="me_cs", platforms={"static"}); getErrorString();
buildModelFMU(Modelica.Mechanics.MultiBody.Examples.Elementary.Pendulum, version="2.0", fmuType="me_cs", fileNamePrefix="Modelica.Mechanics.MultiBody.Examples.Elementary.Pendulum", platforms={"static"}); getErrorString();

system(getInstallationDirectoryPath() + "/bin/OMSimulator Modelica.Mechanics.MultiBody.Examples.Elementary.Pendulum.fmu", "Pendulum_systemCall.log");
readFile("Pendulum_systemCall.log");
Expand Down
2 changes: 1 addition & 1 deletion testsuite/omsimulator/cmakeFMU.mos
Expand Up @@ -4,7 +4,7 @@
// teardown_command: rm -rf DualMassOscillator.System1.fmu DualMassOscillator.System1_FMU* DualMassOscillator_System1_systemCall.log

loadFile("DualMassOscillator.mo"); getErrorString();
buildModelFMU(DualMassOscillator.System1, platforms={"dynamic"}); getErrorString();
buildModelFMU(DualMassOscillator.System1, fileNamePrefix="DualMassOscillator.System1", platforms={"dynamic"}); getErrorString();

system(getInstallationDirectoryPath() + "/bin/OMSimulator DualMassOscillator.System1.fmu", "DualMassOscillator_System1_systemCall.log");
readFile("DualMassOscillator_System1_systemCall.log");
Expand Down
2 changes: 1 addition & 1 deletion testsuite/omsimulator/resetWithLoops.mos
Expand Up @@ -6,7 +6,7 @@ loadModel(Modelica, {"3.2.3"}); getErrorString();

system("mkdir -p resetWithLoops-fmu");
echo(false); cd("resetWithLoops-fmu"); echo(true);
buildModelFMU(Modelica.Electrical.Analog.Examples.Rectifier); getErrorString();
buildModelFMU(Modelica.Electrical.Analog.Examples.Rectifier, fileNamePrefix="Modelica.Electrical.Analog.Examples.Rectifier"); getErrorString();
echo(false); cd(".."); echo(true);

writeFile("resetWithLoops.lua","
Expand Down
2 changes: 1 addition & 1 deletion testsuite/omsimulator/testDirectionalDerivatives.mos
Expand Up @@ -12,7 +12,7 @@ setCommandLineOptions("-d=newInst,-disableDirectionalDerivatives");
getErrorString();

// Build 2.0 FMU
buildModelFMU(Modelica.Electrical.Analog.Examples.CauerLowPassAnalog, version="2.0", fmuType="me_cs", platforms={"static"});
buildModelFMU(Modelica.Electrical.Analog.Examples.CauerLowPassAnalog, version="2.0", fmuType="me_cs", fileNamePrefix="Modelica.Electrical.Analog.Examples.CauerLowPassAnalog", platforms={"static"});
getErrorString();

// Simulate with OMSimulator
Expand Down
8 changes: 4 additions & 4 deletions testsuite/omsimulator/testLoopsOverFMUs.mos
Expand Up @@ -55,10 +55,10 @@ package loopsOverFMUs
end loopsOverFMUs;
"); getErrorString();

buildModelFMU(loopsOverFMUs.A, fmuType="me");
buildModelFMU(loopsOverFMUs.B, fmuType="me");
buildModelFMU(loopsOverFMUs.C, fmuType="me");
buildModelFMU(loopsOverFMUs.D, fmuType="me");
buildModelFMU(loopsOverFMUs.A, fmuType="me", fileNamePrefix="loopsOverFMUs.A");
buildModelFMU(loopsOverFMUs.B, fmuType="me", fileNamePrefix="loopsOverFMUs.B");
buildModelFMU(loopsOverFMUs.C, fmuType="me", fileNamePrefix="loopsOverFMUs.C");
buildModelFMU(loopsOverFMUs.D, fmuType="me", fileNamePrefix="loopsOverFMUs.D");


writeFile("loopsOverFMUs.lua","
Expand Down
2 changes: 1 addition & 1 deletion testsuite/omsimulator/testSynchronousFMU_01.mos
Expand Up @@ -18,7 +18,7 @@ echo(true);
simulate(Modelica_Synchronous.Examples.Elementary.ClockSignals.SubSample, stopTime=0.25, outputFormat="csv"); getErrorString();

// Build FMU with Synchronous features
buildModelFMU(Modelica_Synchronous.Examples.Elementary.ClockSignals.SubSample, version="2.0", fmuType="me"); getErrorString();
buildModelFMU(Modelica_Synchronous.Examples.Elementary.ClockSignals.SubSample, version="2.0", fmuType="me", fileNamePrefix="Modelica_Synchronous.Examples.Elementary.ClockSignals.SubSample"); getErrorString();

// Simulate FMU with OMSimulator
system(getInstallationDirectoryPath() + "/bin/OMSimulator Modelica_Synchronous.Examples.Elementary.ClockSignals.SubSample.fmu"
Expand Down
4 changes: 2 additions & 2 deletions testsuite/omsimulator/testSynchronousFMU_02.mos
Expand Up @@ -11,11 +11,11 @@ echo(false);
mkdir("tempSynchronous02/"); cd("tempSynchronous02/");
echo(true);

// Refernce results without FMU
// Reference results without FMU
simulate(Modelica.Clocked.Examples.CascadeControlledDrive.AbsoluteClocks, stopTime=4, outputFormat="csv"); getErrorString();

// Build FMU with Synchronous features
buildModelFMU(Modelica.Clocked.Examples.CascadeControlledDrive.AbsoluteClocks, version="2.0", fmuType="me"); getErrorString();
buildModelFMU(Modelica.Clocked.Examples.CascadeControlledDrive.AbsoluteClocks, version="2.0", fmuType="me", fileNamePrefix="Modelica.Clocked.Examples.CascadeControlledDrive.AbsoluteClocks"); getErrorString();

// Simulate FMU with OMSimulator
system(getInstallationDirectoryPath() + "/bin/OMSimulator Modelica.Clocked.Examples.CascadeControlledDrive.AbsoluteClocks.fmu"
Expand Down
Expand Up @@ -8,7 +8,7 @@
loadModel(Modelica, {"3.2.2"}); getErrorString();
setCommandLineOptions("+simCodeTarget=Cpp"); getErrorString();
loadFile("cranes.mo"); getErrorString();
buildModelFMU(cranes.crane, version="1.0"); getErrorString();
buildModelFMU(cranes.crane, version="1.0", fileNamePrefix="cranes.crane"); getErrorString();

setCommandLineOptions("+simCodeTarget=C"); getErrorString();
//importFMU("cranes_crane.fmu", "<default>", 6, false, true); getErrorString();
Expand Down
Expand Up @@ -82,7 +82,7 @@ end LocalIOs;
");
getErrorString();

buildModelFMU(LocalIOs.System, version="2.0"); getErrorString();
buildModelFMU(LocalIOs.System, version="2.0", fileNamePrefix="LocalIOs.System"); getErrorString();

// unzip to console, quiet, extra quiet
system("unzip -cqq LocalIOs.System.fmu modelDescription.xml | grep -v guid | grep -v generationDateAndTime | grep -v generationTool > modelDescription.tmp.xml");
Expand Down
Expand Up @@ -22,7 +22,7 @@ end Test;
"); getErrorString();

setCommandLineOptions("--fmiFlags=s:cvode"); getErrorString();
buildModelFMU(Test.SinSource, version="2.0", fmuType ="cs", platforms={"static"}); getErrorString();
buildModelFMU(Test.SinSource, version="2.0", fmuType ="cs", fileNamePrefix="Test.SinSource", platforms={"static"}); getErrorString();

// Check if CMake can re-compile a static library
system("rm -rf Test_SinSource_FMU && unzip -qq Test.SinSource.fmu -d Test_SinSource_FMU && mkdir Test_SinSource_FMU/sources/build_cmake", outputFile="reCompile.log"); getErrorString();
Expand Down
2 changes: 1 addition & 1 deletion testsuite/openmodelica/fmi/CoSimulation/2.0/issue10523.mos
Expand Up @@ -6,7 +6,7 @@
loadModel(Modelica);
getErrorString();

buildModelFMU(Modelica.Blocks.Sources.IntegerConstant, version="2.0", fmuType="cs");
buildModelFMU(Modelica.Blocks.Sources.IntegerConstant, version="2.0", fmuType="cs", fileNamePrefix="Modelica.Blocks.Sources.IntegerConstant");
getErrorString();


Expand Down
Expand Up @@ -12,7 +12,7 @@ mkdir("@");
echo(false);
cd("@");
echo(true);
buildModelFMU(FMUResourceTest.TestResource, version="2.0"); getErrorString();
buildModelFMU(FMUResourceTest.TestResource, version="2.0", fileNamePrefix="FMUResourceTest.TestResource"); getErrorString();
importFMU("FMUResourceTest.TestResource.fmu"); getErrorString();
loadFile("FMUResourceTest_TestResource_me_FMU.mo"); getErrorString();
echo(false);
Expand Down
Expand Up @@ -36,7 +36,7 @@ system("sed -n \"/<ModelVariables>/,/<\\/ModelVariables>/p\" aliasCheck.test2_in
readFile("Bug5673.xml"); getErrorString();


buildModelFMU(aliasCheck.test2,version="2.0");
buildModelFMU(aliasCheck.test2,version="2.0", fileNamePrefix="aliasCheck.test2");
getErrorString();

// unzip to console, quiet, extra quiet
Expand Down
Expand Up @@ -25,5 +25,5 @@ platforms := {
"i686-linux-gnu docker run docker.openmodelica.org/build-deps:v1.13-i386",
"x86_64-w64-mingw32 docker run multiarch/crossbuild",
"i686-w64-mingw32 docker run multiarch/crossbuild"};
buildModelFMU(Modelica.Mechanics.MultiBody.Examples.Elementary.Pendulum, version="2.0", fmuType="me_cs", platforms=platforms); getErrorString();
buildModelFMU(Modelica.Mechanics.MultiBody.Examples.Elementary.Pendulum, version="2.0", fmuType="me_cs", fileNamePrefix="Modelica.Mechanics.MultiBody.Examples.Elementary.Pendulum", platforms=platforms); getErrorString();

0 comments on commit b97f9c5

Please sign in to comment.