@@ -583,13 +583,15 @@ function stateSelectionAndCodeGeneration(modStructure, Gexplicit, name, modelMod
583583 if int_v > 0
584584 value = value / u " s"
585585 end
586- # if length(value) == 1
586+
587587 if ! (typeof (value) <: AbstractArray )
588588 un = unit (value)
589- else
589+ elseif length (value) > 0
590590 un = unit .(value)
591591 @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) "
592592 un = un[1 ]
593+ else
594+ un = " "
593595 end
594596 return SignalTables. unitAsParseableString (un)
595597 end
@@ -836,56 +838,59 @@ end
836838appendSymbol (path:: Nothing , name:: Symbol ) = name
837839appendSymbol (path , name:: Symbol ) = :( $ path.$ name )
838840
841+
839842"""
840- modifiedModel = buildSubmodels!(model, modelModule, FloatType, TimeType, unitless, buildDict::OrderedDict)
843+ modifiedModel = buildSubmodels!(model, modelModule, FloatType, TimeType,
844+ instantiateModelOptions, buildDict::AbstractDict)
841845
842846Traverse `model` and for every `<submodel>` that is a `Model(..)` and has a key-value pair
843847`:_buildFunction = Par(functionName = <buildFunction>)` and optionally `:_buildOption=<buildOption>`, call
844848
845849```
846- updatedSubmodel = <buildFunction>(submodel, FloatType::Type, TimeType::Type, unitless::Bool,
850+ updatedSubmodel = <buildFunction>(submodel, modelModule, FloatType::Type, TimeType::Type, instantiateModelOptions,
847851 ID, pathAST::Union{Expr,Symbol,Nothing}, buildOption = <buildOption>)
848852```
849853
850854A new `updatedSubmodel` is generated from `submodel` merged with additional code and then returned.
851855The arguments of `<buildFunction>`are:
852856
853857- `updatedSubmodel`: A potentially new reference to the updated `submodel`
858+ - `modelModule`: Module in which the model is defined
854859- `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} `.
856861- `ID`: Unique ID to identify the generated submodel (to be used in the code merged into the submodel)
857862- `pathAST`: Path upto `<submodel>` as Abstract Syntax Tree, such as: `:( a.b.c )`
858863 (this path might be used as part of a variable name in the code merged into the submodel).
859864- `buildOption`: Option used for the generation of `buildCode`.
860865
861866Note, keys `_buildFunction` and `_buildOption` have been deleted in the returned `updatedSubmodel`.
862867"""
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} ,
864869 buildDict:: OrderedDict{String,Any} ; pathAST:: Union{Expr,Symbol,Nothing} = nothing )
865870 if haskey (model, :_buildFunction )
866871 _buildFunction = model[:_buildFunction ]
867872 if haskey (_buildFunction, :functionName )
868873 buildFunction = _buildFunction[:functionName ]
869874 else
870875 @error " Model $pathAST has key :_buildFunction but its value has no key :functionName"
871- end
876+ end
872877 delete! (model, :_buildFunction )
873- ID = modelPathAsString (pathAST)
878+ ID = modelPathAsString (pathAST)
874879 quotedPathAST = Meta. quot (pathAST)
875880 if haskey (model, :_buildOption )
876881 buildOption = model[:_buildOption ]
877882 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)))
879884 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)))
881886 end
882887 buildDict[ID] = instantiatedSubmodelStruct
883888 return model
884889 end
885890
886891 for (key,value) in model
887892 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))
889894 end
890895 end
891896 return model
@@ -954,11 +959,28 @@ function instantiateModel(model; modelName="", modelModule=nothing, source=nothi
954959 if typeof (model) <: NamedTuple || typeof (model) <: Dict || typeof (model) <: OrderedDict
955960 # Traverse model and execute functions _buildFunction(..), to include code into sub-models
956961 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+
957979 TimeType = if FloatType <: Measurements.Measurement ||
958980 FloatType <: MonteCarloMeasurements.AbstractParticles ;
959981 baseType (FloatType) else FloatType end # baseType(..) is defined in CodeGeneration.jl
960982 model = deepcopy (model)
961- model = buildSubmodels! (model, modelModule, FloatType, TimeType, unitless , buildDict)
983+ model = buildSubmodels! (model, modelModule, FloatType, TimeType, instantiateModelOptions , buildDict)
962984
963985 if logModel
964986 @showModel (model)
0 commit comments