Skip to content

Commit c5355c2

Browse files
committed
New option @instantiateModel(..., saveCodeOnFile="", ...) to store the generated code on file.
1 parent e877c5f commit c5355c2

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

src/ModiaLang.jl

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ end
433433

434434

435435
function stateSelectionAndCodeGeneration(modStructure, Gexplicit, name, modelModule, buildDict, FloatType, TimeType, init, start, inputs, outputs, vEliminated, vProperty, unknownsWithEliminated, mappedParameters, hideResults;
436-
unitless=false, logStateSelection=false, logCode=false, logExecution=false, logCalculations=false, logTiming=false, evaluateParameters=false)
436+
unitless=false, logStateSelection=false, logCode=false, logExecution=false, logCalculations=false, logTiming=false, evaluateParameters=false, saveCodeOnFile="")
437437
(unknowns, equations, G, Avar, Bequ, assign, blt, parameters) = modStructure
438438
Goriginal = deepcopy(G)
439439
function getSolvedEquationAST(e, v)
@@ -703,6 +703,13 @@ function stateSelectionAndCodeGeneration(modStructure, Gexplicit, name, modelMod
703703
# generatedFunction = @RuntimeGeneratedFunction(modelModule, code)
704704
#getDerivatives = Core.eval(modelModule, code)
705705

706+
if saveCodeOnFile != ""
707+
println(" Save generated code on file ", joinpath(pwd(), saveCodeOnFile))
708+
open(saveCodeOnFile, "w") do io
709+
print(io, replace(sprint(show,code), r"( *#= .*=#\n)|(#= .*=#)" => ""))
710+
end
711+
end
712+
706713
if logTiming
707714
println("eval code")
708715
@time @timeit to "eval(code)" Core.eval(modelModule, code)
@@ -845,7 +852,7 @@ end
845852

846853
"""
847854
modelInstance = @instantiateModel(model; FloatType = Float64, aliasReduction=true, unitless=false,
848-
evaluateParameters=false, log=false, logModel=false, logDetails=false, logStateSelection=false,
855+
evaluateParameters=false, saveCodeOnFile="", log=false, logModel=false, logDetails=false, logStateSelection=false,
849856
logCode=false,logExecution=logExecution, logCalculations=logCalculations, logTiming=false)
850857
851858
Instantiates a model, i.e. performs structural and symbolic transformations and generates a function for calculation of derivatives suitable for simulation.
@@ -855,6 +862,7 @@ Instantiates a model, i.e. performs structural and symbolic transformations and
855862
* `aliasReduction`: Perform alias elimination and remove singularities
856863
* `unitless`: Remove units (useful while debugging models and needed for MonteCarloMeasurements)
857864
* `evaluateParameters`: Use evaluated parameters in the generated code.
865+
* `saveCodeOnFile`: If non-empty string, save generated code in file with name `saveCodeOnFile`.
858866
* `log`: Log the different phases of translation
859867
* `logModel`: Log the variables and equations of the model
860868
* `logDetails`: Log internal data during the different phases of translation
@@ -879,7 +887,7 @@ See documentation of macro [`@instantiateModel`]
879887
"""
880888
function instantiateModel(model; modelName="", modelModule=nothing, source=nothing, FloatType = Float64, aliasReduction=true, unitless=false,
881889
log=false, logModel=false, logDetails=false, logStateSelection=false, logCode=false,
882-
logExecution=logExecution, logCalculations=logCalculations, logTiming=false, evaluateParameters=false)
890+
logExecution=logExecution, logCalculations=logCalculations, logTiming=false, evaluateParameters=false, saveCodeOnFile="")
883891
#try
884892
# model = JSONModel.cloneModel(model, expressionsAsStrings=false)
885893
println("\nInstantiating model $modelName\n in module: $modelModule\n in file: $source")
@@ -1057,7 +1065,7 @@ function instantiateModel(model; modelName="", modelModule=nothing, source=nothi
10571065
if ! experimentalTranslation
10581066
inst = stateSelectionAndCodeGeneration(modStructure, Gexplicit, name, modelModule, buildDict, FloatType, TimeType, modelStructure.init, modelStructure.start, modelStructure.inputs, modelStructure.outputs,
10591067
vEliminated, vProperty, unknownsWithEliminated, modelStructure.mappedParameters, modelStructure.hideResults;
1060-
unitless, logStateSelection, logCode, logExecution, logCalculations, logTiming, evaluateParameters)
1068+
unitless, logStateSelection, logCode, logExecution, logCalculations, logTiming, evaluateParameters, saveCodeOnFile)
10611069
else
10621070
interface[:equations] = modStructure[:equations]
10631071
return interface

0 commit comments

Comments
 (0)