Skip to content

Commit 2c1eae7

Browse files
committed
Bug fix (0-length array for unitless=false) + _buildFunction argument list changed (options of @instantiateModel added) +
Test models adapted to this no-backwards compatible change
1 parent 78048a7 commit 2c1eae7

File tree

6 files changed

+44
-21
lines changed

6 files changed

+44
-21
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
authors = ["Hilding Elmqvist <[email protected]>", "Martin Otter <[email protected]>"]
22
name = "Modia"
33
uuid = "cb905087-75eb-5f27-8515-1ce0ec8e839e"
4-
version = "0.10.0"
4+
version = "0.11.0"
55

66
[deps]
77
DiffEqBase = "2b5f629d-d688-5b77-993f-72d75c75574e"

models/HeatTransfer/InsulatedRod2.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ end
2424

2525

2626
# Called from @instantiateModel(..) before getDerivatives!(..) is generated
27-
function build_InsulatedRod2!(model::AbstractDict, FloatType, TimeType, unitless::Bool, ID, pathAST)
27+
function build_InsulatedRod2!(model::AbstractDict, modelModule, FloatType, TimeType, instantiateModelOptions, ID, pathAST)
28+
unitless = instantiateModelOptions[:unitless]
2829
model = model | Model(
2930
insRod = Var(hideResult=true), # InsulatedRodStruct instance (an equation to return this variable is generated by _buildFunction)
3031
success = Var(hideResult=true), # Dummy return argument

src/Modia.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ Main module of Modia.
99
module Modia
1010

1111
const path = dirname(dirname(@__FILE__)) # Absolute path of package directory
12-
const Version = "0.10.0"
13-
const Date = "2023-05-29"
12+
const Version = "0.11.0"
13+
const Date = "2023-06-04"
1414
const modelsPath = joinpath(Modia.path, "models")
1515

1616
print(" \n\nWelcome to ")

src/ModiaLang.jl

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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
836838
appendSymbol(path::Nothing, name::Symbol) = name
837839
appendSymbol(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
842846
Traverse `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
850854
A new `updatedSubmodel` is generated from `submodel` merged with additional code and then returned.
851855
The 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
861866
Note, 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)

test/TestLinearSystems.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,9 @@ mutable struct LinearStateSpaceBuild{FloatType}
121121
end
122122

123123

124-
function build_LinearStateSpace!(model::AbstractDict, FloatType::Type, TimeType::Type, unitless::Bool,
125-
ID, pathAST::Union{Expr,Symbol,Nothing})
126-
# Called from @instantiatedModel, during instantiation of the model.
124+
function build_LinearStateSpace!(model::AbstractDict, modelModule, FloatType::Type, TimeType::Type,
125+
instantiateModelOptions, ID, pathAST::Union{Expr,Symbol,Nothing})
126+
# Called from @instantiateModel, during instantiation of the model.
127127
pathAsString = Modia.modelPathAsString(pathAST)
128128
#println("... 1: build_LinearStateSpace! called for path = ", pathAsString)
129129

test/TestMultiReturningFunction10.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ end
2828
struct Dummy
2929
end;
3030

31-
function myBuildFunction(model::AbstractDict, FloatType::Type, TimeType::Type, unitless::Bool,
32-
ID, modelPathAST; buildOption = "Default")
31+
function myBuildFunction(model::AbstractDict, modelModule, FloatType::Type, TimeType::Type,
32+
instantiateModelOptions, ID, modelPathAST; buildOption = "Default")
3333
modelPathAsString = if isnothing(modelPathAST); "" else string(modelPathAST) end
3434
println(" TestMultiReturningFunction10: Test output from function myBuildFunction at modelPath = \"$modelPathAsString\":\n Code could be constructed here and merged to the model with buildOption=$buildOption")
3535
return (model, Dummy())

0 commit comments

Comments
 (0)