@@ -583,13 +583,15 @@ function stateSelectionAndCodeGeneration(modStructure, Gexplicit, name, modelMod
583
583
if int_v > 0
584
584
value = value / u " s"
585
585
end
586
- # if length(value) == 1
586
+
587
587
if ! (typeof (value) <: AbstractArray )
588
588
un = unit (value)
589
- else
589
+ elseif length (value) > 0
590
590
un = unit .(value)
591
591
@assert all ([un[i] == un[1 ] for i in 2 : length (un)]) " The unit of all elements of state vector must be equal: $var ::$(value) "
592
592
un = un[1 ]
593
+ else
594
+ un = " "
593
595
end
594
596
return SignalTables. unitAsParseableString (un)
595
597
end
@@ -836,56 +838,59 @@ end
836
838
appendSymbol (path:: Nothing , name:: Symbol ) = name
837
839
appendSymbol (path , name:: Symbol ) = :( $ path.$ name )
838
840
841
+
839
842
"""
840
- modifiedModel = buildSubmodels!(model, modelModule, FloatType, TimeType, unitless, buildDict::OrderedDict)
843
+ modifiedModel = buildSubmodels!(model, modelModule, FloatType, TimeType,
844
+ instantiateModelOptions, buildDict::AbstractDict)
841
845
842
846
Traverse `model` and for every `<submodel>` that is a `Model(..)` and has a key-value pair
843
847
`:_buildFunction = Par(functionName = <buildFunction>)` and optionally `:_buildOption=<buildOption>`, call
844
848
845
849
```
846
- updatedSubmodel = <buildFunction>(submodel, FloatType::Type, TimeType::Type, unitless::Bool,
850
+ updatedSubmodel = <buildFunction>(submodel, modelModule, FloatType::Type, TimeType::Type, instantiateModelOptions,
847
851
ID, pathAST::Union{Expr,Symbol,Nothing}, buildOption = <buildOption>)
848
852
```
849
853
850
854
A new `updatedSubmodel` is generated from `submodel` merged with additional code and then returned.
851
855
The arguments of `<buildFunction>`are:
852
856
853
857
- `updatedSubmodel`: A potentially new reference to the updated `submodel`
858
+ - `modelModule`: Module in which the model is defined
854
859
- `FloatType`, `TimeType`: Types used when instantiating `SimulationModel{FloatType,TimeType}`
855
- - `unitless `: Argument `unitless` of `@instantiateModel`.
860
+ - `instantiateModelOptions `: Optional arguments of `@instantiateModel` provided as `OrderedDict{Symbol,Any} `.
856
861
- `ID`: Unique ID to identify the generated submodel (to be used in the code merged into the submodel)
857
862
- `pathAST`: Path upto `<submodel>` as Abstract Syntax Tree, such as: `:( a.b.c )`
858
863
(this path might be used as part of a variable name in the code merged into the submodel).
859
864
- `buildOption`: Option used for the generation of `buildCode`.
860
865
861
866
Note, keys `_buildFunction` and `_buildOption` have been deleted in the returned `updatedSubmodel`.
862
867
"""
863
- function buildSubmodels! (model:: AbstractDict , modelModule, FloatType:: Type , TimeType:: Type , unitless :: Bool ,
868
+ function buildSubmodels! (model:: AbstractDict , modelModule, FloatType:: Type , TimeType:: Type , instantiateModelOptions :: OrderedDict{Symbol,Any} ,
864
869
buildDict:: OrderedDict{String,Any} ; pathAST:: Union{Expr,Symbol,Nothing} = nothing )
865
870
if haskey (model, :_buildFunction )
866
871
_buildFunction = model[:_buildFunction ]
867
872
if haskey (_buildFunction, :functionName )
868
873
buildFunction = _buildFunction[:functionName ]
869
874
else
870
875
@error " Model $pathAST has key :_buildFunction but its value has no key :functionName"
871
- end
876
+ end
872
877
delete! (model, :_buildFunction )
873
- ID = modelPathAsString (pathAST)
878
+ ID = modelPathAsString (pathAST)
874
879
quotedPathAST = Meta. quot (pathAST)
875
880
if haskey (model, :_buildOption )
876
881
buildOption = model[:_buildOption ]
877
882
delete! (model, :_buildOption )
878
- (model, instantiatedSubmodelStruct) = Core. eval (modelModule, :($ buildFunction ($ model, $ FloatType, $ TimeType, $ unitless , $ ID, $ quotedPathAST, buildOption= $ buildOption)) )
883
+ (model, instantiatedSubmodelStruct) = Core. eval (modelModule, :($ buildFunction ($ model, $ modelModule, $ FloatType, $ TimeType, $ instantiateModelOptions , $ ID, $ quotedPathAST, buildOption= $ buildOption)))
879
884
else
880
- (model, instantiatedSubmodelStruct) = Core. eval (modelModule, :($ buildFunction ($ model, $ FloatType, $ TimeType, $ unitless , $ ID, $ quotedPathAST)))
885
+ (model, instantiatedSubmodelStruct) = Core. eval (modelModule, :($ buildFunction ($ model, $ modelModule, $ FloatType, $ TimeType, $ instantiateModelOptions , $ ID, $ quotedPathAST)))
881
886
end
882
887
buildDict[ID] = instantiatedSubmodelStruct
883
888
return model
884
889
end
885
890
886
891
for (key,value) in model
887
892
if typeof (value) <: OrderedDict && haskey (value, :_class ) && value[:_class ] == :Model
888
- model[key] = buildSubmodels! (value, modelModule, FloatType, TimeType, unitless , buildDict; pathAST= appendSymbol (pathAST,key))
893
+ model[key] = buildSubmodels! (value, modelModule, FloatType, TimeType, instantiateModelOptions , buildDict; pathAST= appendSymbol (pathAST,key))
889
894
end
890
895
end
891
896
return model
@@ -954,11 +959,28 @@ function instantiateModel(model; modelName="", modelModule=nothing, source=nothi
954
959
if typeof (model) <: NamedTuple || typeof (model) <: Dict || typeof (model) <: OrderedDict
955
960
# Traverse model and execute functions _buildFunction(..), to include code into sub-models
956
961
buildDict = OrderedDict {String,Any} ()
962
+ instantiateModelOptions = OrderedDict {Symbol, Any} (
963
+ :modelName => modelName,
964
+ :source => source,
965
+ :aliasReduction => aliasReduction,
966
+ :unitless => unitless,
967
+ :log => log,
968
+ :logModel => logModel,
969
+ :logDetails => logDetails,
970
+ :logStateSelection => logStateSelection,
971
+ :logCode => logCode,
972
+ :logExecution => logExecution,
973
+ :logCalculations => logCalculations,
974
+ :logTiming => logTiming,
975
+ :logFile => logFile,
976
+ :evaluateParameters => evaluateParameters,
977
+ :saveCodeOnFile => saveCodeOnFile)
978
+
957
979
TimeType = if FloatType <: Measurements.Measurement ||
958
980
FloatType <: MonteCarloMeasurements.AbstractParticles ;
959
981
baseType (FloatType) else FloatType end # baseType(..) is defined in CodeGeneration.jl
960
982
model = deepcopy (model)
961
- model = buildSubmodels! (model, modelModule, FloatType, TimeType, unitless , buildDict)
983
+ model = buildSubmodels! (model, modelModule, FloatType, TimeType, instantiateModelOptions , buildDict)
962
984
963
985
if logModel
964
986
@showModel (model)
0 commit comments