Skip to content

Commit

Permalink
Restructure and organize source code FMU handling
Browse files Browse the repository at this point in the history
  - It is better to rewrite this than to try and make sense of it.
    - Name variables properly.
    - Follow the same code structure and style.
    - Make intentions clear.
    - Add comments where needed.

  - I am still not sure what more is needed or what needs to be removed.
    We will see as we go.
    For now this simplification is a good starting place.
  • Loading branch information
mahge committed Sep 28, 2021
1 parent 6ecc2c6 commit 683c6a4
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 62 deletions.
91 changes: 54 additions & 37 deletions OMCompiler/Compiler/SimCode/SimCodeMain.mo
Expand Up @@ -686,7 +686,10 @@ algorithm
Boolean b;
Boolean needSundials = false;
String fileprefix;
list<String> allFiles, sourceFiles, defaultFiles, extraFiles, runtimeFiles, dgesvFiles, sundialsFiles;
String install_include_dir, install_fmu_sources_dir, fmu_tmp_sources_dir;
list<String> sourceFiles, model_desc_src_files;
list<String> dgesv_sources, simrt_c_sundials_sources, simrt_linear_solver_sources, simrt_non_linear_solver_sources;
list<String> simrt_mixed_solver_sources, fmi_export_files, model_gen_files, model_all_gen_files, shared_source_files;
SimCode.VarInfo varInfo;
case (SimCode.SIMCODE(),"C")
algorithm
Expand Down Expand Up @@ -755,61 +758,75 @@ algorithm
varInfo := simCode.modelInfo.varInfo;


// The headers are in the include directory.
copyFiles(RuntimeSources.commonHeaders, source=Settings.getInstallationDirectoryPath() + "/include/omc/c/", destination=fmutmp+"/sources/");
install_include_dir := Settings.getInstallationDirectoryPath() + "/include/omc/c/";
install_fmu_sources_dir := Settings.getInstallationDirectoryPath() + RuntimeSources.fmu_sources_dir;
fmu_tmp_sources_dir := fmutmp + "/sources/";

allFiles := {};
// The C source files are installed to the folder specified by RuntimeSources.fmu_sources_dir. Copy them from there.
copyFiles(RuntimeSources.commonFiles, source=Settings.getInstallationDirectoryPath() + RuntimeSources.fmu_sources_dir, destination=fmutmp+"/sources/");
allFiles := listAppend(RuntimeSources.commonFiles, allFiles);
// The simrt c headers are in the include directory.
copyFiles(RuntimeSources.simrt_c_headers, source=install_include_dir, destination=fmu_tmp_sources_dir);
// The simrt C source files are installed to the folder specified by RuntimeSources.fmu_sources_dir. Copy them from there.
copyFiles(RuntimeSources.simrt_c_sources, source=install_fmu_sources_dir, destination=fmu_tmp_sources_dir);

dgesvFiles := if varInfo.numLinearSystems > 0 or varInfo.numNonLinearSystems > 0 then RuntimeSources.dgesvFiles else {};
copyFiles(RuntimeSources.dgesvFiles, source=Settings.getInstallationDirectoryPath() + RuntimeSources.fmu_sources_dir, destination=fmutmp+"/sources/");
if varInfo.numLinearSystems > 0 or varInfo.numNonLinearSystems > 0 then
// The dgesv headers are in the RuntimeSources.fmu_sources_dir for now since they are not properly installed in the include folder
copyFiles(RuntimeSources.dgesv_headers, source=install_fmu_sources_dir, destination=fmu_tmp_sources_dir);
copyFiles(RuntimeSources.dgesv_sources, source=install_fmu_sources_dir, destination=fmu_tmp_sources_dir);
dgesv_sources := RuntimeSources.sundials_headers;
else
dgesv_sources := {};
end if;

// Check if the sundials files are needed. Shouldn't this actually check what the flags are
// instead of just checking if flags are set only?
if isSome(simCode.fmiSimulationFlags) then
allFiles := listAppend(RuntimeSources.external3rdPartyFiles, allFiles);
sundialsFiles := RuntimeSources.cvodeRuntimeFiles;
allFiles := listAppend(sundialsFiles, allFiles);
// The sundials headers are in the include directory.
copyFiles(RuntimeSources.sundials_headers, source=install_include_dir, destination=fmu_tmp_sources_dir);
copyFiles(RuntimeSources.simrt_c_sundials_sources, source=install_fmu_sources_dir, destination=fmu_tmp_sources_dir);
simrt_c_sundials_sources := RuntimeSources.simrt_c_sundials_sources;
else
sundialsFiles := {""};
end if;
if varInfo.numLinearSystems > 0 then
allFiles := listAppend(RuntimeSources.lsFiles, allFiles);
end if;
if varInfo.numNonLinearSystems > 0 then
allFiles := listAppend(RuntimeSources.nlsFiles, allFiles);
end if;
if varInfo.numMixedSystems > 0 then
allFiles := listAppend(RuntimeSources.mixedFiles, allFiles);
simrt_c_sundials_sources := {};
end if;

copyFiles(allFiles, source=Settings.getInstallationDirectoryPath() + RuntimeSources.fmu_sources_dir, destination=fmutmp+"/sources/");

simrt_linear_solver_sources := if varInfo.numLinearSystems > 0 then RuntimeSources.simrt_linear_solver_sources else {};
copyFiles(simrt_linear_solver_sources, source=install_fmu_sources_dir, destination=fmu_tmp_sources_dir);

simrt_non_linear_solver_sources := if varInfo.numNonLinearSystems > 0 then RuntimeSources.simrt_non_linear_solver_sources else {};
copyFiles(simrt_non_linear_solver_sources, source=install_fmu_sources_dir, destination=fmu_tmp_sources_dir);

simrt_mixed_solver_sources := if varInfo.numMixedSystems > 0 then RuntimeSources.simrt_mixed_solver_sources else {};
copyFiles(simrt_mixed_solver_sources, source=install_fmu_sources_dir, destination=fmu_tmp_sources_dir);

// This fmu export files of OMC are located in a very unexpected place. Right now they are in SimulationRuntime/fmi/export/openmodelica
// and then then they are installed to include/omc/c/fmi-export for some reason. The source, install, and source fmu location
// for these files should be made consistent. For now to avoid modifing things a lot they are left as they are and copied here.
if FMUVersion=="1.0" then
copyFiles(RuntimeSources.fmi1Files, source=Settings.getInstallationDirectoryPath() + "/include/omc/c/", destination=fmutmp+"/sources/");
allFiles := listAppend(RuntimeSources.fmi1Files, allFiles);
else
copyFiles(RuntimeSources.fmi2Files, source=Settings.getInstallationDirectoryPath() + "/include/omc/c/", destination=fmutmp+"/sources/");
allFiles := listAppend(RuntimeSources.fmi2Files, allFiles);
end if;
fmi_export_files := if FMUVersion == "1.0" then RuntimeSources.fmi1Files else RuntimeSources.fmi2Files;
copyFiles(fmi_export_files, source=install_include_dir, destination=fmu_tmp_sources_dir);

System.writeFile(fmutmp+"/sources/isfmi" + (if FMUVersion=="1.0" then "1" else "2"), "");

defaultFiles := list(simCode.fileNamePrefix + f for f in RuntimeSources.defaultFileSuffixes);
runtimeFiles := list(f for f guard Util.endsWith(f, ".c") in allFiles);
model_gen_files := list(simCode.fileNamePrefix + f for f in RuntimeSources.defaultFileSuffixes);
model_all_gen_files := listAppend(model_gen_files, SimCodeUtil.getFunctionIndex());

// I need to see some tests failing or something not working to make sense of what to add here
shared_source_files := List.flatten({RuntimeSources.simrt_c_sources,
// dgesv_sources, // listed separately
// simrt_c_sundials_sources, // listed separately
simrt_linear_solver_sources,
simrt_non_linear_solver_sources,
simrt_mixed_solver_sources
});

// check for fmiSource=false or --fmiFilter=blackBox
if not Flags.getConfigBool(Flags.FMI_SOURCES) or Flags.getConfigEnum(Flags.FMI_FILTER) == Flags.FMI_BLACKBOX then
sourceFiles := {}; // set the sourceFiles to empty, to remove the sources in modeldescription.xml
model_desc_src_files := {}; // set the sourceFiles to empty, to remove the sources in modeldescription.xml
else
sourceFiles := listAppend(defaultFiles, runtimeFiles);
model_desc_src_files := listAppend(model_all_gen_files, shared_source_files);
end if;

Tpl.tplNoret(function CodegenFMU.translateModel(in_a_FMUVersion=FMUVersion, in_a_FMUType=FMUType, in_a_sourceFiles=sourceFiles), simCode);
extraFiles := SimCodeUtil.getFunctionIndex();
Tpl.closeFile(Tpl.tplCallWithFailErrorNoArg(function CodegenFMU.fmuMakefile(a_target=Config.simulationCodeTarget(), a_simCode=simCode, a_FMUVersion=FMUVersion, a_sourceFiles=listAppend(extraFiles, defaultFiles), a_runtimeObjectFiles=list(System.stringReplace(f,".c",".o") for f in runtimeFiles), a_dgesvObjectFiles=list(System.stringReplace(f,".c",".o") for f guard Util.endsWith(f, ".c") in dgesvFiles), a_sundialsObjectFiles=list(System.stringReplace(f,".c",".o") for f guard Util.endsWith(f, ".c") in sundialsFiles)),
Tpl.tplNoret(function CodegenFMU.translateModel(in_a_FMUVersion=FMUVersion, in_a_FMUType=FMUType, in_a_sourceFiles=model_desc_src_files), simCode);

Tpl.closeFile(Tpl.tplCallWithFailErrorNoArg(function CodegenFMU.fmuMakefile(a_target=Config.simulationCodeTarget(), a_simCode=simCode, a_FMUVersion=FMUVersion, a_sourceFiles=model_all_gen_files, a_runtimeObjectFiles=list(System.stringReplace(f,".c",".o") for f in shared_source_files), a_dgesvObjectFiles=list(System.stringReplace(f,".c",".o") for f in dgesv_sources), a_sundialsObjectFiles=list(System.stringReplace(f,".c",".o") for f in simrt_c_sundials_sources)),
txt=Tpl.redirectToFile(Tpl.emptyTxt, simCode.fileNamePrefix+".fmutmp/sources/Makefile.in")));
Tpl.closeFile(Tpl.tplCallWithFailError(CodegenFMU.settingsfile, simCode,
txt=Tpl.redirectToFile(Tpl.emptyTxt, simCode.fileNamePrefix+".fmutmp/sources/omc_simulation_settings.h")));
Expand Down
2 changes: 1 addition & 1 deletion OMCompiler/SimulationRuntime/c/Makefile.common
Expand Up @@ -359,7 +359,7 @@ RuntimeSources.mo: Makefile.common Makefile.objs RuntimeSources.mo.tpl
-e "s#COMMON_FILES#`cat sources.tmp`#" \
-e "s#DGESV_FILES#`echo $(DGESV_OBJS:%=\\\"./external_solvers/%.c\\\") | tr " " ,`#" \
-e "s#NLS_FILES#`echo $(SOLVER_OBJS_NONLINEAR_SYSTEMS:%.o=\\\"./simulation/solver/%.c\\\") | tr " " ,`#" \
-e "s#CMINPACK_FILES#$(CMINPACK_OBJS:%=,\\\"./external_solvers/%.c\\\")#" \
-e "s#CMINPACK_FILES#`echo $(CMINPACK_OBJS:%=\\\"./external_solvers/%.c\\\") | tr " " ,`#" \
-e "s#LS_FILES#`echo $(SOLVER_OBJS_LINEAR_SYSTEMS:%.o=\\\"./simulation/solver/%.c\\\") | tr " " ,`#" \
-e "s#MIXED_FILES#`echo $(SOLVER_OBJS_MIXED_SYSTEMS:%.o=\\\"./simulation/solver/%.c\\\") | tr " " ,`#" \
RuntimeSources.mo.tpl > $@.tmp
Expand Down
55 changes: 31 additions & 24 deletions OMCompiler/SimulationRuntime/c/RuntimeSources.mo.tpl
@@ -1,33 +1,40 @@
encapsulated package RuntimeSources
constant String fmu_sources_dir = "/include/omc/c/";
constant list<String> commonFiles={COMMON_FILES};
constant list<String> commonHeaders={COMMON_HEADERS};
constant list<String> simrt_c_sources={COMMON_FILES};
constant list<String> simrt_c_headers={COMMON_HEADERS};
constant list<String> fmi1Files={"fmi-export/fmu1_model_interface.c.inc","fmi-export/fmu1_model_interface.h"};
constant list<String> fmi2Files={"fmi-export/fmu2_model_interface.c.inc","fmi-export/fmu2_model_interface.h", "fmi-export/fmu_read_flags.c.inc", "fmi-export/fmu_read_flags.h"};
constant list<String> defaultFileSuffixes={".c", "_functions.c", "_records.c", "_01exo.c", "_02nls.c", "_03lsy.c", "_04set.c", "_05evt.c", "_06inz.c", "_07dly.c", "_08bnd.c", "_09alg.c", "_10asr.c", "_11mix.c", "_12jac.c", "_13opt.c", "_14lnz.c", "_15syn.c", "_16dae.c", "_17inl.c", "_18spd.c", "_init_fmu.c", "_FMU.c"};
constant list<String> cvodeFiles={"sundials/cvode/cvode_ls.h",
"sundials/cvode/cvode_proj.h",
"sundials/cvode/cvode.h"};
constant list<String> sundialsFiles={"sundials/sundials/sundials_config.h",
"sundials/sundials/sundials_dense.h",
"sundials/sundials/sundials_direct.h",
"sundials/sundials/sundials_iterative.h",
"sundials/sundials/sundials_linearsolver.h",
"sundials/sundials/sundials_matrix.h",
"sundials/sundials/sundials_nonlinearsolver.h",
"sundials/sundials/sundials_types.h",
"sundials/sunlinsol/sunlinsol_dense.h",
"sundials/sunmatrix/sunmatrix_dense.h",
"sundials/sunnonlinsol/sunnonlinsol_fixedpoint.h"};
constant list<String> nvectorFiles={"sundials/nvector/nvector_serial.h",
"sundials/sundials/sundials_nvector.h"};
constant list<String> external3rdPartyFiles=listAppend(listAppend(cvodeFiles,sundialsFiles), nvectorFiles);
constant list<String> dgesvFiles={DGESV_FILES, "./external_solvers/blaswrap.h", "./external_solvers/clapack.h", "./external_solvers/f2c.h"};
constant list<String> cvodeRuntimeFiles={"simulation/solver/cvode_solver.c",

constant list<String> sundials_headers={"sundials/cvode/cvode_ls.h",
"sundials/cvode/cvode_proj.h",
"sundials/cvode/cvode.h",
"sundials/sundials/sundials_config.h",
"sundials/sundials/sundials_dense.h",
"sundials/sundials/sundials_direct.h",
"sundials/sundials/sundials_iterative.h",
"sundials/sundials/sundials_linearsolver.h",
"sundials/sundials/sundials_matrix.h",
"sundials/sundials/sundials_nonlinearsolver.h",
"sundials/sundials/sundials_types.h",
"sundials/sunlinsol/sunlinsol_dense.h",
"sundials/sunmatrix/sunmatrix_dense.h",
"sundials/sunnonlinsol/sunnonlinsol_fixedpoint.h",
"sundials/nvector/nvector_serial.h",
"sundials/sundials/sundials_nvector.h"};

constant list<String> simrt_c_sundials_sources={"simulation/solver/cvode_solver.c",
"simulation/solver/sundials_error.c"};
constant list<String> lsFiles={LS_FILES};
constant list<String> nlsFiles={NLS_FILESCMINPACK_FILES, "./external_solvers/cminpack.h", "./external_solvers/minpack.h"};
constant list<String> mixedFiles={MIXED_FILES};

constant list<String> dgesv_headers={"./external_solvers/blaswrap.h", "./external_solvers/clapack.h", "./external_solvers/f2c.h"};
constant list<String> dgesv_sources={DGESV_FILES};

constant list<String> cminpack_headers = {"./external_solvers/cminpack.h", "./external_solvers/minpack.h"};
constant list<String> cminpack_sources = {CMINPACK_FILES};

constant list<String> simrt_linear_solver_sources={LS_FILES};
constant list<String> simrt_non_linear_solver_sources={NLS_FILES};
constant list<String> simrt_mixed_solver_sources={MIXED_FILES};
annotation(__OpenModelica_Interface="backend");
end RuntimeSources;

0 comments on commit 683c6a4

Please sign in to comment.