diff --git a/OMCompiler/Compiler/Main/Main.mo b/OMCompiler/Compiler/Main/Main.mo index c64236c0b74..dc2c7eeecbe 100644 --- a/OMCompiler/Compiler/Main/Main.mo +++ b/OMCompiler/Compiler/Main/Main.mo @@ -399,15 +399,16 @@ algorithm local Absyn.Program p, pLibs; DAE.DAElist d; - String s,str,f; + String flatString,str,f; list libs; Absyn.Path cname; - Boolean silent,notsilent; + Boolean runBackend, runSilent; GlobalScript.Statements stmts; FCore.Cache cache; FCore.Graph env; DAE.FunctionTree funcs; - list cls; + String cls, fileNamePrefix; + SimCode.SimulationSettings sim_settings; // A .mo-file, followed by an optional list of extra .mo-files and libraries. // The last class in the first file will be instantiated. @@ -439,35 +440,60 @@ algorithm execStat("Parsed file"); // Instantiate the program. - (cache, env, d, cname, s) := instantiate(); - p := SymbolTable.getAbsyn(); + // (cache, env, d, cname, flatString) := instantiate(); - d := if Flags.isSet(Flags.TRANSFORMS_BEFORE_DUMP) then DAEUtil.transformationsBeforeBackend(cache,env,d) else d; + cls := Config.classToInstantiate(); + // If no class was explicitly specified, instantiate the last class in the + // program. Otherwise, instantiate the given class name. + cname := if stringEmpty(cls) then AbsynUtil.lastClassname(SymbolTable.getAbsyn()) else AbsynUtil.stringPath(cls); + fileNamePrefix := Util.stringReplaceChar(AbsynUtil.pathString(cname), ".", "_"); - funcs := FCore.getFunctionTree(cache); + runBackend := Config.simulationCg() or Config.simulation(); + runSilent := Config.silent(); - Print.clearBuf(); - execStat("Transformations before Dump"); - if stringEmpty(s) and not Config.silent() then - s := DAEDump.dumpStr(d, funcs); - execStat("DAEDump done"); - end if; - Print.printBuf(s); - if Flags.isSet(Flags.DAE_DUMP_GRAPHV) then - DAEDump.dumpGraphviz(d); - end if; - execStat("Misc Dump"); + // If accepting parModelica create a slightly different default settings. + // Temporary solution for now since Intel OpenCL dll calls hang. + // sim_settings := if Config.acceptParModelicaGrammar() then + // SimCodeMain.createSimulationSettings(0.0, 1.0, 1, 1e-6, "dassl", "", "plt", ".*", "") else + // SimCodeMain.createSimulationSettings(0.0, 1.0, 500, 1e-6, "dassl", "", "mat", ".*", ""); - // Do any transformations required before going into code generation, e.g. if-equations to expressions. - d := if boolNot(Flags.isSet(Flags.TRANSFORMS_BEFORE_DUMP)) then DAEUtil.transformationsBeforeBackend(cache,env,d) else d; + (_ , _, _, _, _) := CevalScriptBackend.translateModel(FCore.emptyCache(), FGraph.empty(), cname, + fileNamePrefix, runBackend, runSilent, NONE()); - if not Config.silent() then - print(Print.getString()); - end if; - execStat("Transformations before backend"); - // Run the backend. - optimizeDae(cache, env, d, p, cname); + + + + + + // p := SymbolTable.getAbsyn(); + + // d := if Flags.isSet(Flags.TRANSFORMS_BEFORE_DUMP) then DAEUtil.transformationsBeforeBackend(cache,env,d) else d; + + // funcs := FCore.getFunctionTree(cache); + + // Print.clearBuf(); + // execStat("Transformations before Dump"); + // if stringEmpty(flatString) and not Config.silent() then + // flatString := DAEDump.dumpStr(d, funcs); + // execStat("DAEDump done"); + // end if; + // Print.printBuf(flatString); + // if Flags.isSet(Flags.DAE_DUMP_GRAPHV) then + // DAEDump.dumpGraphviz(d); + // end if; + // execStat("Misc Dump"); + + // // Do any transformations required before going into code generation, e.g. if-equations to expressions. + // d := if boolNot(Flags.isSet(Flags.TRANSFORMS_BEFORE_DUMP)) then DAEUtil.transformationsBeforeBackend(cache,env,d) else d; + + // if not Config.silent() then + // print(Print.getString()); + // end if; + // execStat("Transformations before backend"); + + // // Run the backend. + // optimizeDae(cache, env, d, p, cname); // Show any errors or warnings if there are any! showErrors(Print.getErrorString(), ErrorExt.printMessagesStr(false)); then (); diff --git a/OMCompiler/Compiler/Script/CevalScriptBackend.mo b/OMCompiler/Compiler/Script/CevalScriptBackend.mo index 5599bc93e15..8bec57063e4 100644 --- a/OMCompiler/Compiler/Script/CevalScriptBackend.mo +++ b/OMCompiler/Compiler/Script/CevalScriptBackend.mo @@ -1233,7 +1233,7 @@ algorithm case ("translateModel",vals as {Values.CODE(Absyn.C_TYPENAME(className)),_,_,_,_,_,Values.STRING(filenameprefix),_,_,_,_,_}) equation (outCache,simSettings) = calculateSimulationSettings(outCache, vals); - (b,outCache) = translateModel(outCache, inEnv, className, filenameprefix, true, SOME(simSettings)); + (b,outCache) = translateModel(outCache, inEnv, className, filenameprefix, true, true, SOME(simSettings)); then Values.BOOL(b); @@ -3183,7 +3183,7 @@ algorithm end getAdjacencyMatrix; /* ------------------------------------------------------------------- - RUN OLD FRONTEND + RUN FRONTEND ------------------------------------------------------------------- */ public function runFrontEnd input output FCore.Cache cache; @@ -3382,22 +3382,22 @@ algorithm end if; end runFrontEndWorkNF; -protected function translateModel " author: x02lucpo - translates a model into cpp code and writes also a makefile" +public function translateModel input FCore.Cache inCache; input FCore.Graph inEnv; input Absyn.Path className "path for the model"; input String inFileNamePrefix; - input Boolean addDummy "if true, add a dummy state"; + input Boolean runBackend "if true, run the backend as well. This will run SimCode and Codegen as well."; + input Boolean runSilent "if true, flat modelica code will not be dumped to out stream"; input Option inSimSettingsOpt; output Boolean success; output FCore.Cache outCache; - output list outStringLst; + output list outLibs; output String outFileDir; output list> resultValues; algorithm - (outCache,outStringLst,outFileDir,resultValues):= - match (inCache,inEnv,className,inFileNamePrefix,addDummy,inSimSettingsOpt) + (outCache,outLibs,outFileDir,resultValues):= + match (inCache,inEnv,className,inFileNamePrefix,inSimSettingsOpt) local FCore.Cache cache; FCore.Graph env; @@ -3408,79 +3408,25 @@ algorithm String commandLineOptions; list args; Boolean haveAnnotation; + SimCode.SimulationSettings simSettings; + GlobalScript.SimulationOptions defaulSimOpt; - case (cache,env,_,fileNamePrefix,_,_) + case (cache,env,_,fileNamePrefix,_) algorithm - if Config.ignoreCommandLineOptionsAnnotation() then - (success, cache, libs, file_dir, resultValues) := - callTranslateModel(cache,env,className,fileNamePrefix,inSimSettingsOpt); + if isSome(inSimSettingsOpt) then + SOME(simSettings) := inSimSettingsOpt; else - // read the __OpenModelica_commandLineOptions - Absyn.STRING(commandLineOptions) := Interactive.getNamedAnnotation(className, SymbolTable.getAbsyn(), Absyn.IDENT("__OpenModelica_commandLineOptions"), SOME(Absyn.STRING("")), Interactive.getAnnotationExp); - haveAnnotation := boolNot(stringEq(commandLineOptions, "")); - // backup the flags. - flags := if haveAnnotation then FlagsUtil.backupFlags() else FlagsUtil.loadFlags(); - try - // apply if there are any new flags - if haveAnnotation then - args := System.strtok(commandLineOptions, " "); - FlagsUtil.readArgs(args); - end if; - - (success, cache, libs, file_dir, resultValues) := - callTranslateModel(cache,env,className,fileNamePrefix,inSimSettingsOpt); - // reset to the original flags - FlagsUtil.saveFlags(flags); - else - FlagsUtil.saveFlags(flags); - fail(); - end try; + defaulSimOpt := buildSimulationOptionsFromModelExperimentAnnotation(className, fileNamePrefix, SOME(defaultSimulationOptions)); + simSettings := convertSimulationOptionsToSimCode(defaulSimOpt); end if; - then - (cache,libs,file_dir,resultValues); - - end match; -end translateModel; - -protected function translateLabeledModel " author: Fatima - translates a labeled model into cpp code and writes also a makefile" - input FCore.Cache inCache; - input FCore.Graph inEnv; - input Absyn.Path className "path for the model"; - input String inFileNamePrefix; - input Boolean addDummy "if true, add a dummy state"; - input Option inSimSettingsOpt; - input list inLabelstoCancel; - output FCore.Cache outCache; - output BackendDAE.BackendDAE outBackendDAE; - output list outStringLst; - output String outFileDir; - output list> resultValues; -algorithm - (outCache,outStringLst,outFileDir,resultValues):= - match (inCache,inEnv,className,inFileNamePrefix,addDummy,inSimSettingsOpt,inLabelstoCancel) - local - FCore.Cache cache; - FCore.Graph env; - BackendDAE.BackendDAE indexed_dlow; - list libs; - String file_dir, fileNamePrefix; - Absyn.Program p; - Flags.Flag flags; - String commandLineOptions; - list args; - Boolean haveAnnotation; - list labelstoCancel; - - case (cache,env,_,fileNamePrefix,_,_,labelstoCancel) - algorithm if Config.ignoreCommandLineOptionsAnnotation() then - (true, cache, libs, file_dir, resultValues) := - SimCodeMain.translateModel(SimCodeMain.TranslateModelKind.NORMAL(),cache,env,className,fileNamePrefix,addDummy,inSimSettingsOpt,Absyn.FUNCTIONARGS({},argNames =labelstoCancel)); + (success, cache, libs, file_dir, resultValues) := + callTranslateModel(cache, env, className, fileNamePrefix, runBackend, runSilent, SOME(simSettings)); else // read the __OpenModelica_commandLineOptions - Absyn.STRING(commandLineOptions) := Interactive.getNamedAnnotation(className, SymbolTable.getAbsyn(), Absyn.IDENT("__OpenModelica_commandLineOptions"), SOME(Absyn.STRING("")), Interactive.getAnnotationExp); + Absyn.STRING(commandLineOptions) := Interactive.getNamedAnnotation(className, SymbolTable.getAbsyn(), Absyn.IDENT + ("__OpenModelica_commandLineOptions"), SOME(Absyn.STRING("")), Interactive.getAnnotationExp); haveAnnotation := boolNot(stringEq(commandLineOptions, "")); // backup the flags. flags := if haveAnnotation then FlagsUtil.backupFlags() else FlagsUtil.loadFlags(); @@ -3491,8 +3437,8 @@ algorithm FlagsUtil.readArgs(args); end if; - (true, cache, libs, file_dir, resultValues) := - SimCodeMain.translateModel(SimCodeMain.TranslateModelKind.NORMAL(),cache,env,className,fileNamePrefix,addDummy,inSimSettingsOpt,Absyn.FUNCTIONARGS({},argNames =labelstoCancel)); + (success, cache, libs, file_dir, resultValues) := + callTranslateModel(cache, env, className, fileNamePrefix, runBackend, runSilent, SOME(simSettings)); // reset to the original flags FlagsUtil.saveFlags(flags); else @@ -3504,7 +3450,7 @@ algorithm (cache,libs,file_dir,resultValues); end match; -end translateLabeledModel; +end translateModel; protected function callTranslateModel "Call the main translate function. This function @@ -3514,6 +3460,8 @@ protected function callTranslateModel input FCore.Graph inEnv; input Absyn.Path className "path for the model"; input String inFileNamePrefix; + input Boolean runBackend "if true, run the backend as well. This will run SimCode and Codegen as well."; + input Boolean runSilent "if true, flat modelica code will not be dumped to out stream"; input Option inSimSettingsOpt; output Boolean success; output FCore.Cache outCache; @@ -3528,8 +3476,8 @@ algorithm success := true; else (success, outCache, outStringLst, outFileDir, resultValues) := - SimCodeMain.translateModel(SimCodeMain.TranslateModelKind.NORMAL(),inCache,inEnv, - className,inFileNamePrefix,true,inSimSettingsOpt,Absyn.FUNCTIONARGS({},{})); + SimCodeMain.translateModel(SimCodeMain.TranslateModelKind.NORMAL(), inCache, inEnv, + className, inFileNamePrefix, runBackend, runSilent, inSimSettingsOpt, Absyn.FUNCTIONARGS({},{})); end if; end callTranslateModel; @@ -4013,7 +3961,8 @@ algorithm FlagsUtil.setConfigBool(Flags.BUILDING_FMU, true); FlagsUtil.setConfigString(Flags.FMI_VERSION, FMUVersion); try - (success, cache, libs, _, _) := SimCodeMain.translateModel(SimCodeMain.TranslateModelKind.FMU(FMUType, fmuTargetName), cache, inEnv, className, filenameprefix, addDummy, SOME(simSettings)); + (success, cache, libs, _, _) := SimCodeMain.translateModel(SimCodeMain.TranslateModelKind.FMU(FMUType, fmuTargetName), + cache, inEnv, className, filenameprefix, true, true, SOME(simSettings)); true := success; outValue := Values.STRING((if not Testsuite.isRunning() then System.pwd() + Autoconf.pathDelimiter else "") + fmuTargetName + ".fmu"); else @@ -4209,7 +4158,8 @@ protected function translateModelXML " author: Alachew protected Boolean success; algorithm - (success,cache) := SimCodeMain.translateModel(SimCodeMain.TranslateModelKind.XML(),cache,env,className,fileNamePrefix,addDummy,inSimSettingsOpt); + (success,cache) := SimCodeMain.translateModel(SimCodeMain.TranslateModelKind.XML(), cache, env, className, + fileNamePrefix, true, true, inSimSettingsOpt); outValue := Values.STRING(if success then ((if not Testsuite.isRunning() then System.pwd() + Autoconf.pathDelimiter else "") + fileNamePrefix+".xml") else ""); end translateModelXML; @@ -5605,7 +5555,7 @@ algorithm (cache,simSettings) := calculateSimulationSettings(cache, values); SimCode.SIMULATION_SETTINGS(method = method_str, outputFormat = outputFormat_str) := simSettings; - (success,cache,libsAndLibDirs,file_dir,resultValues) := translateModel(cache,env, classname, filenameprefix,true, SOME(simSettings)); + (success,cache,libsAndLibDirs,file_dir,resultValues) := translateModel(cache,env, classname, filenameprefix, true, true, SOME(simSettings)); //cname_str = AbsynUtil.pathString(classname); //SimCodeUtil.generateInitData(indexed_dlow_1, classname, filenameprefix, init_filename, // starttime_r, stoptime_r, interval_r, tolerance_r, method_str,options_str,outputFormat_str); diff --git a/OMCompiler/Compiler/SimCode/SimCodeMain.mo b/OMCompiler/Compiler/SimCode/SimCodeMain.mo index b07e0c7ce73..a9fc7beb365 100644 --- a/OMCompiler/Compiler/SimCode/SimCodeMain.mo +++ b/OMCompiler/Compiler/SimCode/SimCodeMain.mo @@ -91,6 +91,7 @@ import HashTableCrIListArray; import HashTableCrILst; import HpcOmSimCodeMain; import HpcOmTaskGraph; +import NFConvertDAE; import RuntimeSources; import SerializeModelInfo; import SerializeInitXML; @@ -554,8 +555,10 @@ algorithm String str, guid; list codegenFuncs; Integer numThreads, n; - list>> res; + list>> res = {}; + tuple> res_i; list strs, tmp, matches; + Integer i=0; case "Cpp" algorithm @@ -997,6 +1000,288 @@ algorithm end callTargetTemplatesXML; public function translateModel " + Entry point to translate a Modelica model for simulation / FMU / XML. + Called from other places in the compiler." + output Boolean success; + input TranslateModelKind kind; + input output FCore.Cache cache; + input FCore.Graph inEnv; + input Absyn.Path className "path for the model"; + input String inFileNamePrefix; + input Boolean runBackend "if true, run the backend as well. This will run SimCode and Codegen as well."; + input Boolean runSilent "if true, flat modelica code will not be dumped to out stream"; + input Option inSimSettingsOpt; + input Absyn.FunctionArgs args=Absyn.emptyFunctionArgs "labels for remove terms"; + output list outLibs; + output String outFileDir; + output list> resultValues; +protected + FCore.Cache inCache = cache; + Real timeFrontend=0.0; + DAE.DAElist dae; + FCore.Graph env; + Option odae; + DAE.FunctionTree funcs; + list> allRoots; + FlatModel flatModel; + FunctionTree funcTree; + NBackendDAE bdae; + Boolean dumpValidFlatModelicaNF; + String flatString = "", NFFlatString = ""; + +algorithm + FlagsUtil.setConfigBool(Flags.BUILDING_MODEL, true); + + outLibs := {}; + outFileDir := ""; + resultValues := {}; + + dumpValidFlatModelicaNF := not runSilent and Config.flatModelica(); + + // new backend - also activates new frontend by default + if Flags.getConfigBool(Flags.NEW_BACKEND) then + // ToDo: set permanently matching -> SBGraphs + System.realtimeTick(ClockIndexes.RT_CLOCK_FRONTEND); + ExecStat.execStatReset(); + + (flatModel, funcTree, NFFlatString) := CevalScriptBackend.runFrontEndWorkNF(className, false, dumpValidFlatModelicaNF); + timeFrontend := System.realtimeTock(ClockIndexes.RT_CLOCK_FRONTEND); + ExecStat.execStat("FrontEnd"); + + if dumpValidFlatModelicaNF then + flatString := NFFlatString; + elseif not runSilent then + (dae, funcs) := NFConvertDAE.convert(flatModel, funcTree); + flatString := DAEDump.dumpStr(dae, funcs); + end if; + + if runBackend then + (outLibs, outFileDir, resultValues) := translateModelCallBackendNB(flatModel, funcTree, className, inFileNamePrefix, inSimSettingsOpt); + end if; + + // old backend + else + // calculate stuff that we need to create SimCode data structure + System.realtimeTick(ClockIndexes.RT_CLOCK_FRONTEND); + ExecStat.execStatReset(); + (cache, env, odae, NFFlatString) := CevalScriptBackend.runFrontEnd(cache, inEnv, className, false, dumpValidFlatModelicaNF); + ExecStat.execStat("FrontEnd"); + SOME(dae) := odae; + + if dumpValidFlatModelicaNF then + flatString := NFFlatString; + elseif not runSilent then + funcs := FCore.getFunctionTree(cache); + flatString := DAEDump.dumpStr(dae, funcs); + end if; + + + if Flags.isSet(Flags.SERIALIZED_SIZE) then + allRoots := {}; + for i in 1:300 loop + try + allRoots := getGlobalRoot(i)::allRoots; + else + end try; + end for; + serializeNotify(allRoots, "All local+global roots (1:300)"); + serializeNotify(dae, "FrontEnd DAE"); + serializeNotify((env,inEnv,cache,inCache), "FCore.Graph + Cache + Old graph + Old cache"); + serializeNotify((SymbolTable.get(),dae,env,inEnv,cache,inCache), "Symbol Table, DAE, Graph, OldGraph, Cache, OldCache"); + ExecStat.execStat("Serialize FrontEnd"); + end if; + + timeFrontend := System.realtimeTock(ClockIndexes.RT_CLOCK_FRONTEND); + + if runBackend then + (cache, outLibs, outFileDir, resultValues) := translateModelCallBackendOB(kind, cache, env, dae, className, inFileNamePrefix, inSimSettingsOpt, args); + end if; + + end if; + + resultValues := List.appendElt(("timeFrontend", Values.REAL(timeFrontend)), resultValues); + FlagsUtil.setConfigBool(Flags.BUILDING_MODEL, false); + + if not stringEmpty(flatString) and runSilent then + Error.addInternalError("Flat model string generated but is not being dumped. Please make sure it is not generated if it is not shown." + , sourceInfo()); + elseif stringEmpty(flatString) and not runSilent then + Error.addInternalError("Flat model string generated but is empty.", sourceInfo()); + else + print(flatString); + end if; + + success := true; +end translateModel; + +protected function translateModelCallBackendOB + input TranslateModelKind kind; + input output FCore.Cache cache; + input FCore.Graph inEnv; + input DAE.DAElist inDae; + input Absyn.Path className "path for the model"; + input String inFileNamePrefix; + input Option inSimSettingsOpt; + input Absyn.FunctionArgs args=Absyn.emptyFunctionArgs "labels for remove terms"; + output list outLibs; + output String outFileDir; + output list> resultValues; +protected + Boolean generateFunctions = false; + Real timeSimCode=0.0, timeTemplates=0.0, timeBackend=0.0; +algorithm + FlagsUtil.setConfigBool(Flags.BUILDING_MODEL, true); + (outLibs, outFileDir) := match (inEnv) + local + String file_dir, description, fmuType; + list libs; + DAE.DAElist dae; + FCore.Graph graph; + BackendDAE.BackendDAE dlow, initDAE; + Option initDAE_lambda0; + Option inlineData; + list removedInitialEquationLst; + Option> strPreOptModules; + Boolean isFMI2; + BackendDAE.SymbolicJacobians fmiDer; + DAE.FunctionTree funcs; + + // old backend + case (graph) algorithm + System.realtimeTick(ClockIndexes.RT_CLOCK_BACKEND); + dae := DAEUtil.transformationsBeforeBackend(cache, graph, inDae); + ExecStat.execStat("Transformations before backend"); + + if Flags.isSet(Flags.SERIALIZED_SIZE) then + serializeNotify(dae, "FrontEnd DAE after transformations"); + serializeNotify((dae,inDae), "FrontEnd DAE before+after transformations"); + ExecStat.execStat("Serialize DAE (2)"); + end if; + GCExt.free(inDae); + // inDae := DAE.emptyDae; + + generateFunctions := FlagsUtil.set(Flags.GEN, false); + // We should not need to lookup constants and classes in the backend, + // so let's free up the old graph and just make it the initial environment. + if not Flags.isSet(Flags.BACKEND_KEEP_ENV_GRAPH) then + (cache,graph) := Builtin.initialGraph(cache); + end if; + + description := DAEUtil.daeDescription(dae); + dlow := BackendDAECreate.lower(dae, cache, graph, BackendDAE.EXTRA_INFO(description, inFileNamePrefix)); + + GCExt.free(dae); + dae := DAE.emptyDae; + + if Flags.isSet(Flags.SERIALIZED_SIZE) then + serializeNotify(dlow, "BackendDAECreate.lower"); + ExecStat.execStat("Serialize dlow"); + end if; + + isFMI2 := match kind + case TranslateModelKind.FMU(fmuType) then FMI.isFMIVersion20(); + else false; + end match; + // FMI 2.0: enable postOptModule to create alias variables for output states + strPreOptModules := if (isFMI2) then SOME("introduceOutputAliases"::BackendDAEUtil.getPreOptModulesString()) else NONE(); + + // FMI 2.0: enable postOptModule "introduceOutputRealDerivatives" to set maxOutputDerivativeOrder = 1 + if (isFMI2 and fmuType == "cs") then + strPreOptModules := SOME("introduceOutputRealDerivatives":: Util.getOption(strPreOptModules)); + end if; + + //BackendDump.printBackendDAE(dlow); + (dlow, initDAE, initDAE_lambda0, inlineData, removedInitialEquationLst) := BackendDAEUtil.getSolvedSystem(dlow,inFileNamePrefix,strPreOptModules=strPreOptModules); + + // generate derivatives + if (isFMI2) and not Flags.isSet(Flags.FMI20_DEPENDENCIES) then + // activate symolic jacobains for fmi 2.0 + // to provide dependence information and partial derivatives + (fmiDer, funcs) := SymbolicJacobian.createFMIModelDerivatives(dlow); + dlow := BackendDAEUtil.setFunctionTree(dlow, funcs); + else + fmiDer := {}; + end if; + timeBackend := System.realtimeTock(ClockIndexes.RT_CLOCK_BACKEND); + + if Flags.isSet(Flags.SERIALIZED_SIZE) then + serializeNotify(dlow, "BackendDAE (simulation)"); + serializeNotify(initDAE, "BackendDAE (initialization)"); + serializeNotify(initDAE_lambda0, "BackendDAE (lambda0)"); + serializeNotify((dlow,initDAE,initDAE_lambda0,inlineData,removedInitialEquationLst), "BackendDAE (simulation+initialization+lambda0+inlineData+removedInitialEquationLst)"); + ExecStat.execStat("Serialize solved system"); + end if; + + (libs, file_dir, timeSimCode, timeTemplates) := match kind + case TranslateModelKind.NORMAL() + algorithm + (libs, file_dir, timeSimCode, timeTemplates) := generateModelCode(dlow, initDAE, initDAE_lambda0, inlineData, removedInitialEquationLst, SymbolTable.getAbsyn(), className, inFileNamePrefix, inSimSettingsOpt, args,fmiDer); + then (libs, file_dir, timeSimCode, timeTemplates); + case TranslateModelKind.FMU() + algorithm + + (libs,file_dir,timeSimCode,timeTemplates) := generateModelCodeFMU(dlow, initDAE, initDAE_lambda0, fmiDer, removedInitialEquationLst, SymbolTable.getAbsyn(), className, FMI.getFMIVersionString(), kind.kind, inFileNamePrefix, kind.targetName, inSimSettingsOpt); + then (libs, file_dir, timeSimCode, timeTemplates); + case TranslateModelKind.XML() + algorithm + (libs, file_dir, timeSimCode, timeTemplates) := generateModelCodeXML(dlow, initDAE, initDAE_lambda0, removedInitialEquationLst, SymbolTable.getAbsyn(), className, inFileNamePrefix, inSimSettingsOpt); + then (libs, file_dir, timeSimCode, timeTemplates); + else + algorithm + Error.addInternalError("Unknown translateModel kind: " + anyString(kind), sourceInfo()); + then fail(); + end match; + then (libs, file_dir); + + end match; + + if generateFunctions then + FlagsUtil.set(Flags.GEN, true); + end if; + + resultValues := {("timeTemplates", Values.REAL(timeTemplates)), + ("timeSimCode", Values.REAL(timeSimCode)), + ("timeBackend", Values.REAL(timeBackend))}; +end translateModelCallBackendOB; + +protected function translateModelCallBackendNB + input FlatModel inFlatModel; + input FunctionTree inFuncTree; + input Absyn.Path inClassName "path for the model"; + input String inFileNamePrefix; + input Option inSimSettingsOpt; + output list outLibs; + output String outFileDir; + output list> resultValues; +protected + Real timeSimCode=0.0, timeTemplates=0.0, timeBackend=0.0; + NBackendDAE bdae; +algorithm + FlagsUtil.setConfigBool(Flags.BUILDING_MODEL, true); + + // ToDo: set permanently matching -> SBGraphs + + System.realtimeTick(ClockIndexes.RT_CLOCK_BACKEND); + bdae := NBackendDAE.lower(inFlatModel, inFuncTree); + if Flags.isSet(Flags.OPT_DAE_DUMP) then + print(NBackendDAE.toString(bdae, "(After Lowering)")); + end if; + bdae := NBackendDAE.main(bdae); + timeBackend := System.realtimeTock(ClockIndexes.RT_CLOCK_BACKEND); + ExecStat.execStat("backend"); + + // ================================ + // SIMCODE + // ================================ + (outLibs, outFileDir, timeSimCode, timeTemplates) := generateModelCodeNewBackend(bdae, inClassName, inFileNamePrefix, inSimSettingsOpt); + + resultValues := {("timeTemplates", Values.REAL(timeTemplates)), + ("timeSimCode", Values.REAL(timeSimCode)), + ("timeBackend", Values.REAL(timeBackend))}; +end translateModelCallBackendNB; + + +public function translateModelOld " Entry point to translate a Modelica model for simulation / FMU / XML. Called from other places in the compiler." output Boolean success; @@ -1225,7 +1510,7 @@ algorithm ("timeBackend", Values.REAL(timeBackend)), ("timeFrontend", Values.REAL(timeFrontend))}; FlagsUtil.setConfigBool(Flags.BUILDING_MODEL, false); -end translateModel; +end translateModelOld; public function translateModelDAEMode " Entry point to translate a Modelica model for simulation in DAE mode diff --git a/OMCompiler/Compiler/Stubs/CevalScriptBackend.mo b/OMCompiler/Compiler/Stubs/CevalScriptBackend.mo index f7fc3cfafb2..460dd1de89b 100644 --- a/OMCompiler/Compiler/Stubs/CevalScriptBackend.mo +++ b/OMCompiler/Compiler/Stubs/CevalScriptBackend.mo @@ -4,6 +4,7 @@ import Absyn; import DAE; import FCore; import GlobalScript; +import SimCode; import Values; function runFrontEnd @@ -20,6 +21,23 @@ algorithm assert(false, getInstanceName()); end runFrontEnd; +public function translateModel + input FCore.Cache inCache; + input FCore.Graph inEnv; + input Absyn.Path className "path for the model"; + input String inFileNamePrefix; + input Boolean runBackend "if true, run the backend as well. This will run SimCode and Codegen as well."; + input Boolean runSilent "if true, flat modelica code will not be dumped to out stream"; + input Option inSimSettingsOpt; + output Boolean success; + output FCore.Cache outCache; + output list outLibs; + output String outFileDir; + output list> resultValues; +algorithm + assert(false, getInstanceName()); +end translateModel; + function getSimulationResultType output DAE.Type t; algorithm