Skip to content
This repository was archived by the owner on May 18, 2019. It is now read-only.

Commit 67b8802

Browse files
AndreasOpenModelica-Hudson
authored andcommitted
[OMSI] Added buildModelFMU for simCodeTarget=omsic
It's now possible to build first FMUs with --simCodeTarget=omsic - Added new simCodeTarget flag "omsic" - buildModelFMU can now return resultValues and saves time needed to build model - Added OMSIC template functions to callTargetTemplatesFMU in SimCodeMain.mo - Added template compilation for OMSI templates in Compiler/Template/Makefile.common Belonging to [master]: - OpenModelica/OpenModelica#154 - #3067 Co-authored-by: niklwors <niiklas.worschech@boschrexroth.de> Co-authored-by: wibraun <wbraun@fh-bielefeld.de> Belonging to [master]: - OpenModelica/OpenModelica#154 - #3067
1 parent 10a0a9d commit 67b8802

File tree

9 files changed

+169
-101
lines changed

9 files changed

+169
-101
lines changed

Compiler/Script/CevalScript.mo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ protected
231231
Boolean isWindows = Autoconf.os == "Windows_NT";
232232
list<String> makeVarsNoBinding;
233233
algorithm
234-
libsfilename := fileprefix + ".libs";
234+
libsfilename := workDir + fileprefix + ".libs";
235235
libs_str := stringDelimitList(libs, " ");
236236
makeVarsNoBinding := makeVars; // OMC is stupid and wants to constant evaluate inputs with bindings for iterator variables...
237237

Compiler/Script/CevalScriptBackend.mo

Lines changed: 56 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1422,12 +1422,28 @@ algorithm
14221422
then
14231423
(cache,Values.BOOL(b));
14241424

1425-
case (cache,env,"buildModel",vals,_)
1426-
equation
1425+
case (cache,env,"buildModel", vals as Values.CODE(Absyn.C_TYPENAME(className))::_,_)
1426+
algorithm
14271427
List.map_0(ClockIndexes.buildModelClocks,System.realtimeClear);
14281428
System.realtimeTick(ClockIndexes.RT_CLOCK_SIMULATE_TOTAL);
1429-
(b,cache,compileDir,executable,_,_,initfilename,_,_,vals) = buildModel(cache,env, vals, msg);
1430-
executable = if not Config.getRunningTestsuite() then compileDir + executable else executable;
1429+
if not Config.simCodeTarget() == "omsic" then
1430+
(b,cache,compileDir,executable,_,_,initfilename,_,_,vals) := buildModel(cache,env, vals, msg);
1431+
else
1432+
filenameprefix := Absyn.pathString(className);
1433+
try
1434+
(cache, Values.STRING(str)) := buildModelFMU(cache, env, className, "2.0", "me", "<default>", true, {"static"});
1435+
if stringEmpty(str) then
1436+
fail();
1437+
end if;
1438+
b := true;
1439+
else
1440+
b := false;
1441+
end try;
1442+
compileDir := System.pwd() + Autoconf.pathDelimiter;
1443+
executable := filenameprefix + "_me_FMU";
1444+
initfilename := filenameprefix + "_init_xml";
1445+
end if;
1446+
executable := if not Config.getRunningTestsuite() then compileDir + executable else executable;
14311447
then
14321448
(cache,ValuesUtil.makeArray(if b then {Values.STRING(executable),Values.STRING(initfilename)} else {Values.STRING(""),Values.STRING("")}));
14331449

@@ -1498,7 +1514,12 @@ algorithm
14981514
case (cache,env,"simulate",vals as Values.CODE(Absyn.C_TYPENAME(className))::_,_)
14991515
algorithm
15001516
System.realtimeTick(ClockIndexes.RT_CLOCK_SIMULATE_TOTAL);
1501-
(b,cache,compileDir,executable,_,outputFormat_str,_,simflags,resultValues,vals) := buildModel(cache,env,vals,msg);
1517+
if not Config.simCodeTarget() == "omsic" then
1518+
(b,cache,compileDir,executable,_,outputFormat_str,_,simflags,resultValues,vals) := buildModel(cache,env,vals,msg);
1519+
else
1520+
Error.addMessage(Error.SIMULATOR_BUILD_ERROR, {"Can't simulate for SimCodeTarget=omsic!\n"});
1521+
fail();
1522+
end if;
15021523

15031524
if b then
15041525
exeDir := compileDir;
@@ -3544,6 +3565,7 @@ protected
35443565
list<String> libs;
35453566
Boolean isWindows;
35463567
String FMUType = inFMUType;
3568+
Real timeCompile;
35473569
algorithm
35483570
cache := inCache;
35493571
if not FMI.checkFMIVersion(FMUVersion) then
@@ -3564,6 +3586,7 @@ algorithm
35643586
Error.addMessage(Error.FMU_EXPORT_NOT_SUPPORTED_CPP, {FMUType});
35653587
FMUType := "me";
35663588
end if;
3589+
35673590
// NOTE: The FMUs use fileNamePrefix for the internal name when it would be expected to be fileNamePrefix that decides the .fmu filename
35683591
// The scripting environment from a user's perspective is like that. fmuTargetName is the name of the .fmu in the templates, etc.
35693592
filenameprefix := Util.stringReplaceChar(if inFileNamePrefix == "<default>" then Absyn.pathString(className) else inFileNamePrefix, ".", "_");
@@ -3572,7 +3595,7 @@ algorithm
35723595
simSettings := convertSimulationOptionsToSimCode(defaulSimOpt);
35733596
Flags.setConfigBool(Flags.BUILDING_FMU, true);
35743597
try
3575-
(success, cache, libs,_, _) := SimCodeMain.translateModel(SimCodeMain.TranslateModelKind.FMU(FMUVersion, FMUType, fmuTargetName), cache, inEnv, className, filenameprefix, addDummy, SOME(simSettings));
3598+
(success, cache, libs, _, _) := SimCodeMain.translateModel(SimCodeMain.TranslateModelKind.FMU(FMUVersion, FMUType, fmuTargetName), cache, inEnv, className, filenameprefix, addDummy, SOME(simSettings));
35763599
true := success;
35773600
outValue := Values.STRING((if not Config.getRunningTestsuite() then System.pwd() + Autoconf.pathDelimiter else "") + fmuTargetName + ".fmu");
35783601
else
@@ -3606,19 +3629,41 @@ algorithm
36063629
return;
36073630
end if;
36083631

3609-
CevalScript.compileModel(filenameprefix+"_FMU" , libs);
3632+
System.realtimeTick(ClockIndexes.RT_CLOCK_BUILD_MODEL);
3633+
if not Config.simCodeTarget() == "omsic" then
3634+
CevalScript.compileModel(filenameprefix+"_FMU" , libs);
36103635

3611-
ExecStat.execStat("buildModelFMU: Generate the FMI files");
3636+
ExecStat.execStat("buildModelFMU: Generate the FMI files");
3637+
3638+
fmutmp := filenameprefix + ".fmutmp";
3639+
logfile := filenameprefix + ".log";
3640+
dir := fmutmp+"/sources/";
3641+
else
3642+
fmutmp := filenameprefix+".fmutmp" + Autoconf.pathDelimiter;
3643+
try
3644+
CevalScript.compileModel(filenameprefix+"_FMU" , libs, fmutmp);
3645+
timeCompile := System.realtimeTock(ClockIndexes.RT_CLOCK_BUILD_MODEL);
3646+
else
3647+
Error.addMessage(Error.SIMULATOR_BUILD_ERROR, {System.readFile(fmutmp + filenameprefix+"_FMU.log")});
3648+
end try;
3649+
return;
3650+
end if;
36123651

36133652
for platform in platforms loop
3614-
configureFMU(platform, fmutmp, System.realpath(fmutmp)+"/resources/"+System.stringReplace(listGet(Util.stringSplitAtChar(platform," "),1),"/","-")+".log", isWindows);
3615-
ExecStat.execStat("buildModelFMU: Generate platform " + platform);
3653+
try
3654+
configureFMU(platform, fmutmp, System.realpath(fmutmp)+"/resources/"+System.stringReplace(listGet(Util.stringSplitAtChar(platform," "),1),"/","-")+".log", isWindows);
3655+
ExecStat.execStat("buildModelFMU: Generate platform " + platform);
3656+
else
3657+
Error.addMessage(Error.SIMULATOR_BUILD_ERROR, {"Configure for platform:\"" + platform + "\" does not exist"});
3658+
end try;
36163659
end for;
36173660

36183661
cmd := "rm -f \"" + fmuTargetName + ".fmu\" && cd \"" + fmutmp + "\" && zip -r \"../" + fmuTargetName + ".fmu\" *";
36193662
if 0 <> System.systemCall(cmd, outFile=logfile) then
36203663
Error.addMessage(Error.SIMULATOR_BUILD_ERROR, {cmd + "\n\n" + System.readFile(logfile)});
36213664
ExecStat.execStat("buildModelFMU failed");
3665+
else
3666+
timeCompile := System.realtimeTock(ClockIndexes.RT_CLOCK_BUILD_MODEL);
36223667
end if;
36233668

36243669
if not System.regularFileExists(fmuTargetName + ".fmu") then
@@ -5123,10 +5168,10 @@ algorithm
51235168
if success then
51245169
try
51255170
CevalScript.compileModel(filenameprefix, libs);
5171+
timeCompile := System.realtimeTock(ClockIndexes.RT_CLOCK_BUILD_MODEL);
51265172
else
51275173
success := false;
51285174
end try;
5129-
timeCompile := System.realtimeTock(ClockIndexes.RT_CLOCK_BUILD_MODEL);
51305175
else
51315176
timeCompile := 0.0;
51325177
end if;

Compiler/SimCode/SimCodeMain.mo

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ import CodegenSparseFMI;
7575
import CodegenCSharp;
7676
import CodegenCpp;
7777
import CodegenCppHpcom;
78+
import CodegenOMSIC;
79+
import CodegenOMSI_common;
80+
import CodegenOMSIC_Equations;
7881
import CodegenXML;
7982
import CodegenJava;
8083
import CodegenJS;
@@ -679,7 +682,9 @@ algorithm
679682
local
680683
String str, newdir, newpath, resourcesDir, dirname;
681684
String fmutmp;
685+
String guid;
682686
Boolean b;
687+
String fileprefix;
683688
list<String> allFiles, sourceFiles, defaultFiles, extraFiles, runtimeFiles, dgesvFiles;
684689
SimCode.VarInfo varInfo;
685690
case (SimCode.SIMCODE(),"C")
@@ -754,6 +759,34 @@ algorithm
754759
Tpl.closeFile(Tpl.tplCallWithFailError(CodegenFMU.settingsfile, simCode,
755760
txt=Tpl.redirectToFile(Tpl.emptyTxt, simCode.fileNamePrefix+".fmutmp/sources/omc_simulation_settings.h")));
756761
then ();
762+
case (_,"omsic")
763+
algorithm
764+
guid := System.getUUIDStr();
765+
fileprefix := simCode.fileNamePrefix;
766+
767+
// create tmp directory for generated files, but first remove the old one!
768+
if System.directoryExists(simCode.fullPathPrefix) then
769+
if not System.removeDirectory(simCode.fullPathPrefix) then
770+
Error.addInternalError("Failed to remove directory: " + simCode.fullPathPrefix, sourceInfo());
771+
fail();
772+
end if;
773+
end if;
774+
if not System.createDirectory(simCode.fullPathPrefix) then
775+
Error.addInternalError("Failed to create tmp folder", sourceInfo());
776+
fail();
777+
end if;
778+
779+
SerializeInitXML.simulationInitFileReturnBool(simCode=simCode, guid=guid);
780+
SerializeModelInfo.serialize(simCode, Flags.isSet(Flags.INFO_XML_OPERATIONS));
781+
782+
//runTplWriteFile(func = function CodegenFMU.fmuModelDescriptionFile(in_a_simCode=simCode, in_a_guid=guid, in_a_FMUVersion=FMUVersion, in_a_FMUType=FMUType, in_a_sourceFiles={}), file=simCode.fullPathPrefix+"/"+"modelDescription.xml");
783+
runTpl(func = function CodegenOMSI_common.generateFMUModelDescriptionFile(a_simCode=simCode, a_guid=guid, a_FMUVersion=FMUVersion, a_FMUType=FMUType, a_sourceFiles={}, a_fileName=simCode.fullPathPrefix+"/"+"modelDescription.xml"));
784+
runTplWriteFile(func = function CodegenOMSIC.createMakefile(a_simCode=simCode, a_target=Config.simulationCodeTarget(), a_makeflieName=fileprefix+"_FMU.makefile"), file=simCode.fullPathPrefix+"/"+fileprefix+"_FMU.makefile");
785+
786+
runTplWriteFile(func = function CodegenOMSIC.generateOMSIC(a_simCode=simCode), file=simCode.fullPathPrefix+"/"+fileprefix+"_omsic.c");
787+
788+
runTpl(func = function CodegenOMSI_common.generateEquationsCode(a_simCode=simCode, a_FileNamePrefix=fileprefix));
789+
then ();
757790
case (_,"Cpp")
758791
equation
759792
if(Flags.isSet(Flags.HPCOM)) then

Compiler/Template/CodegenOMSIC.tpl

Lines changed: 5 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -147,14 +147,15 @@ template createMakefile(SimCode simCode, String target, String makeflieName)
147147
let libEnding = match makefileParams.platform case "win32" case "win64" then 'dll' else 'so'
148148
let rpath = match makefileParams.platform case "win32" case "win64" then '' else "-rpath '$$ORIGIN/.'"
149149
let star = match makefileParams.platform case "win32" case "win64" then '' else '*'
150+
let fPIC = match makefileParams.platform case "win32" case "win64" then '' else '-fPIC '
150151

151152
<<
152153
# Makefile generated by OpenModelica
153154
OMHOME=<%makefileParams.omhome%>
154155
OMLIB=<%makefileParams.omhome%>/<%OMLibs%>
155156

156157
CC=<%makefileParams.ccompiler%>
157-
CFLAGS= -fPIC -Wall -Wextra -ansi -pedantic -g
158+
CFLAGS= <%fPIC%>-Wall -Wextra -ansi -pedantic -g
158159
CXX=<%makefileParams.cxxcompiler%>
159160
LD=$(CC) -shared
160161

@@ -197,7 +198,7 @@ template createMakefile(SimCode simCode, String target, String makeflieName)
197198
KINSOL_LIB=sundials_kinsol
198199
SUNDIALS_NVECSERIAL=sundials_nvecserial
199200

200-
OMSU_STATIC_LIB=-Wl,--whole-archive -lOMSISolver_static -lOMSIBase_static -lOMSU_static -Wl,--no-whole-archive
201+
OMSU_STATIC_LIB=-Wl,--whole-archive -lOMSISolver_static -lOMSIBase_static -lOMSIC_static -Wl,--no-whole-archive
201202
OMSU_STATIC_LIBDIR=-L$(OMLIB)/omc/omsi
202203
LIBS = $(OMSU_STATIC_LIB) -Wl,-Bdynamic -l$(EXPAT_LIB) -l$(LAPACK_LIB) <%match makefileParams.platform case "win32" case "win64" then '' else '-l$(BLAS_LIB)'%> $(KINSOL_LIBDIR)/lib$(KINSOL_LIB).<%libEnding%> $(KINSOL_LIBDIR)/lib$(SUNDIALS_NVECSERIAL).<%libEnding%>
203204
LIBSDIR= $(OMSU_STATIC_LIBDIR) -L$(EXPAT_LIBDIR) -L$(LAPACK_LIBDIR) -L$(KINSOL_LIBDIR)
@@ -221,7 +222,7 @@ template createMakefile(SimCode simCode, String target, String makeflieName)
221222
<%\t%>cp -a $(OMHOME)/include/omc/omsi/* <%includedir%>
222223
<%\t%>cp -a $(OMHOME)/include/omc/omsic/* <%includedir%>
223224
<%\t%>cp -a $(OMLIB)/omc/omsi/libOMSIBase_static.* <%fileNamePrefix%>.fmutmp/sources/libs
224-
<%\t%>cp -a $(OMLIB)/omc/omsi/libOMSU_static.* <%fileNamePrefix%>.fmutmp/sources/libs
225+
<%\t%>cp -a $(OMLIB)/omc/omsi/libOMSIC_static.* <%fileNamePrefix%>.fmutmp/sources/libs
225226
<%\t%>cp -a $(OMLIB)/omc/omsi/libOMSISolver_static.* <%fileNamePrefix%>.fmutmp/sources/libs
226227
<%\t%># Third party libraries
227228
<%\t%>cp -f $(EXPAT_LIBDIR)/lib$(EXPAT_LIB).a <%fileNamePrefix%>.fmutmp/sources/libs
@@ -373,7 +374,7 @@ template createMakefileIn(SimCode simCode, String target, String FileNamePrefix,
373374
# /MD - link with MSVCRT.LIB
374375
# /link - [linker options and libraries]
375376
# /LIBPATH: - Directories where libs can be found
376-
LDFLAGS=/MD /link /dll /debug /pdb:"<%fileNamePrefix%>.pdb" /LIBPATH:"<%makefileParams.omhome%>/lib/<%getTriple()%>/omc/msvc/" /LIBPATH:"<%makefileParams.omhome%>/lib/<%getTriple()%>/omc/msvc/release/" <%dirExtra%> <%libsPos1%> <%libsPos2%> f2c.lib initialization.lib libexpat.lib math-support.lib meta.lib results.lib simulation.lib solver.lib sundials_kinsol.lib sundials_nvecserial.lib util.lib lapack_win32_MT.lib lis.lib gc-lib.lib user32.lib pthreadVC2.lib wsock32.lib cminpack.lib umfpack.lib amd.lib
377+
LDFLAGS=/MD /link /dll /debug /pdb:"<%fileNamePrefix%>.pdb" /LIBPATH:"<%makefileParams.omhome%>/lib/omc/msvc/" /LIBPATH:"<%makefileParams.omhome%>/lib/omc/msvc/release/" <%dirExtra%> <%libsPos1%> <%libsPos2%> f2c.lib initialization.lib libexpat.lib math-support.lib meta.lib results.lib simulation.lib solver.lib sundials_kinsol.lib sundials_nvecserial.lib util.lib lapack_win32_MT.lib lis.lib gc-lib.lib user32.lib pthreadVC2.lib wsock32.lib cminpack.lib umfpack.lib amd.lib
377378

378379
# /MDd link with MSVCRTD.LIB debug lib
379380
# lib names should not be appended with a d just switch to lib/omc/msvc/debug
@@ -458,44 +459,5 @@ template createMakefileIn(SimCode simCode, String target, String FileNamePrefix,
458459
end createMakefileIn;
459460

460461

461-
template createFMIImportScript(String fileNamePrefix, String fmuTargetName)
462-
"Generated script for building executable from OMSU using OM FMI-Import."
463-
::=
464-
<<
465-
importFMU("<%fmuTargetName%>.fmu");
466-
getErrorString();
467-
setCommandLineOptions("--simCodeTarget=C");
468-
469-
loadFile("<%fileNamePrefix%>_me_FMU.mo");
470-
getErrorString();
471-
buildModel(<%fileNamePrefix%>_me_FMU);
472-
getErrorString();
473-
>>
474-
end createFMIImportScript;
475-
476-
477-
template createOMSimulationScript(String fileNamePrefix, String fmuTargetName)
478-
"Generate script for simulating OMSU with OMSimulator."
479-
::=
480-
<<
481-
oms_setCommandLineOption("--suppressPath=true")
482-
oms_setTempDirectory("./temp-<%fileNamePrefix%>/")
483-
484-
oms_newModel("<%fileNamePrefix%>")
485-
oms_addSystem("<%fileNamePrefix%>.root", oms_system_sc)
486-
oms_addSubModel("<%fileNamePrefix%>.root.A", "<%fmuTargetName%>.fmu")
487-
oms_setResultFile("<%fileNamePrefix%>", "<%fmuTargetName%>_res.mat")
488-
489-
oms_instantiate("<%fileNamePrefix%>")
490-
491-
oms_initialize("<%fileNamePrefix%>")
492-
493-
oms_simulate("<%fileNamePrefix%>")
494-
495-
oms_terminate("<%fileNamePrefix%>")
496-
oms_delete("<%fileNamePrefix%>")
497-
>>
498-
end createOMSimulationScript;
499-
500462
annotation(__OpenModelica_Interface="backend");
501463
end CodegenOMSIC;

Compiler/Template/CodegenOMSI_common.tpl

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,20 @@ import CodegenOMSIC_Equations;
4343
import CodegenUtil;
4444
import CodegenUtilSimulation;
4545
import CodegenCFunctions;
46+
import CodegenFMU;
4647

4748

4849
/* public */
50+
template generateFMUModelDescriptionFile(SimCode simCode, String guid, String FMUVersion, String FMUType, list<String> sourceFiles, String fileName)
51+
"Generate modelDescription.xml.
52+
ToDo: Workaround since runTplWriteFile from .mo file loses some spaces."
53+
::=
54+
let content = CodegenFMU.fmuModelDescriptionFile(simCode, guid, FMUVersion, FMUType, sourceFiles)
55+
let () = textFile(content, fileName)
56+
<<>>
57+
end generateFMUModelDescriptionFile;
58+
59+
4960
template generateEquationsCode (SimCode simCode, String FileNamePrefix)
5061
"Entrypoint to generate all Code for linear systems.
5162
Code is generated directly into files"
@@ -148,44 +159,44 @@ template generateOmsiFunctionCode(OMSIFunction omsiFunction, String FileNamePref
148159
#include <Core/System/IOMSI.h>
149160

150161
>>
151-
end match%>
162+
end match%>
152163

153164

154-
#if defined(__cplusplus)
155-
extern "C" {
156-
#endif
165+
#if defined(__cplusplus)
166+
extern "C" {
167+
#endif
157168
158-
/* Instantiation of omsi_function_t */
159-
<%initializationCode%>
169+
/* Instantiation of omsi_function_t */
170+
<%initializationCode%>
160171
161-
/* Evaluation functions for each equation */
162-
<%evaluationCode%>
172+
/* Evaluation functions for each equation */
173+
<%evaluationCode%>
163174
164175
165-
/* Equations evaluation */
166-
<%match Config.simCodeTarget()
167-
case "omsic" then
168-
'omsi_status <%FileNamePrefix%>_<%omsiName%>_allEqns(omsi_function_t* <%omsiName%>, omsi_values* model_vars_and_params, void* data){'
169-
case "omsicpp" then
170-
'omsi_status <%FileNamePrefix%>::omsi_<%modelFunctionnamePrefixStr%>All(omsi_function_t* <%omsiName%>, const omsi_values* model_vars_and_params, void* data){'
171-
end match%>
176+
/* Equations evaluation */
177+
<%match Config.simCodeTarget()
178+
case "omsic" then
179+
'omsi_status <%FileNamePrefix%>_<%omsiName%>_allEqns(omsi_function_t* <%omsiName%>, omsi_values* model_vars_and_params, void* data){'
180+
case "omsicpp" then
181+
'omsi_status <%FileNamePrefix%>::omsi_<%modelFunctionnamePrefixStr%>All(omsi_function_t* <%omsiName%>, const omsi_values* model_vars_and_params, void* data){'
182+
end match%>
172183
173-
/* Variables */
174-
omsi_status status, new_status;
184+
/* Variables */
185+
omsi_status status, new_status;
175186
176-
status = omsi_ok;
177-
<%if not nAlgebraicSystems then "new_status = omsi_ok;"%>
187+
status = omsi_ok;
188+
<%if not nAlgebraicSystems then "new_status = omsi_ok;"%>
178189
179-
<%functionCall%>
190+
<%functionCall%>
180191
181-
return status;
182-
}
192+
return status;
193+
}
183194

184-
#if defined(__cplusplus)
185-
}
186-
#endif
187-
<%\n%>
188-
>>
195+
#if defined(__cplusplus)
196+
}
197+
#endif
198+
<%\n%>
199+
>>
189200
/* leave a newline at the end of file to get rid of the warning */
190201
end generateOmsiFunctionCode;
191202

0 commit comments

Comments
 (0)