Skip to content

Commit e81114f

Browse files
committed
Prepare release 0.12.0
- Rename functions of built-in components - Improve docu of built-in components
1 parent 572ab57 commit e81114f

File tree

7 files changed

+119
-89
lines changed

7 files changed

+119
-89
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 <Hilding.Elmqvist@Mogram.net>", "Martin Otter <Martin.Otter@dlr.de>"]
22
name = "Modia"
33
uuid = "cb905087-75eb-5f27-8515-1ce0ec8e839e"
4-
version = "0.11.0"
4+
version = "0.12.0"
55

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

docs/src/Functions.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,15 @@ showEvaluatedParameters
6060
CurrentModule = Modia
6161
```
6262

63-
The simulation result of a model `instantiatedModel` are provided as a *signal table*,
63+
The simulation result of a model `instantiatedModel` are provided as a *signal table*,
6464
see [SignalTables.jl](https://github.com/ModiaSim/SignalTables.jl).
6565

6666
Therefore, all [signal table functions](https://modiasim.github.io/SignalTables.jl/stable/Functions/OverviewOfFunctions.html)
6767
can be used on a simulated model
6868

6969
To activate the defined plot package, use
7070

71-
- [`@usingModiaPlot`](@ref)
71+
- [`@usingModiaPlot`](@ref)
7272

7373
Alternatively, `usingPlotPackage` (from Modia reexported macro of SignalTables) can be used,
7474
but then package `SignalTables` must be in your current environment.
@@ -87,11 +87,11 @@ FirstOrder = Model(
8787
)
8888
simulate!(firstOrder, stopTime=10)
8989
showInfo(firstOrder) # list info about the result
90-
t = getValues(firstOrder, "time")
90+
t = getValues(firstOrder, "time")
9191
y = getValues(firstOrder, "y") # use any plot program: plot(t,y)
9292
9393
# Write result on file
94-
writeSignalTable("firstOrder.json", firstOrder, indent=2, log=true)
94+
writeSignalTable("firstOrder.json", firstOrder, indent=2, log=true)
9595
```
9696

9797
See the generated [json-file](../resources/fileio/firstOrder.json).

docs/src/Internal.md

Lines changed: 58 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,91 @@
11
# Internal
22

33
This chapter documents internal functions that are typically only
4-
for use of the developers of package Modia.
4+
for use of the developers of a model library or of Modia.
55

6-
## Code Generation
7-
8-
This section provides functions to **generate Julia code** of the
9-
transformed equations.
6+
## Variables of built-in Components
107

118
```@meta
129
CurrentModule = Modia
1310
```
1411

12+
The following functions are provided to define and access new variables
13+
in built-in components (seee for example model `InsulatedRod2` in `Modia/models/HeatTransfer.jl`).
14+
15+
| Functions | Description |
16+
|:--------------------------------------------------------|:----------------------------------------------------------------------------------|
17+
| [`new_x_segmented_variable!`](@ref) | Generate new state variable (`x_segmented` and `der_x_segmented` variables) |
18+
| [`new_w_segmented_variable!`](@ref) | Generate new local variable (`w_segmented` variable) |
19+
| [`new_alias_segmented_variable!`](@ref) | Generate new alias variable |
20+
| [`new_z_segmented_variable!`](@ref) | Generate new zero crossing variables (`z_segmented` variables) |
21+
| [`get_x_startIndex_from_x_segmented_startIndex`](@ref) | Return start index of `x_segmented` variable with respect to state vector `x` |
22+
| [`copy_scalar_x_segmented_value_from_state`](@ref) | Return value of scalar `x_segmented` variable from state vector `x` |
23+
| [`copy_SVector3_x_segmented_value_from_state`](@ref) | Return value of `SVector{3,FloatType}` x_segmented variable from state vector `x` |
24+
| [`copy_Vector_x_segmented_value_from_state`](@ref) | Return value of `Vector{FloatType}` x_segmented variable from state vector `x` |
25+
| [`copy_der_x_segmented_value_to_state`](@ref) | Copy value of `der_x_segmented` variable to state derivative vector `der(x)` |
26+
| [`copy_w_segmented_value_to_result`](@ref) | Copy value of local variable (`w-segmented`) to result |
27+
28+
1529
```@docs
16-
SimulationModel
17-
generate_getDerivatives!
18-
init!
19-
outputs!
20-
terminate!
21-
derivatives!
22-
DAEresidualsForODE!
23-
affectEvent!
24-
zeroCrossings!
25-
affectStateEvent!
26-
timeEventCondition!
27-
affectTimeEvent!
28-
addToResult!
29-
getFloatType
30-
measurementToString
30+
new_x_segmented_variable!
31+
new_w_segmented_variable!
32+
new_alias_segmented_variable!
33+
new_z_segmented_variable!
34+
get_x_startIndex_from_x_segmented_startIndex
35+
copy_scalar_x_segmented_value_from_state
36+
copy_SVector3_x_segmented_value_from_state
37+
copy_Vector_x_segmented_value_from_state
38+
copy_der_x_segmented_value_to_state
39+
copy_w_segmented_value_to_result
3140
```
3241

33-
## Inquiries in Model
42+
## Inquiries in built-in Components
3443

35-
The functions in this section can be called in the model code or in
36-
functions that are called from the model code.
44+
The following functions are provided to inquire properties
45+
in built-in components at the current state of the simulation
46+
(see for example model `InsulatedRod2` in `Modia/models/HeatTransfer.jl`).
3747

3848
```@docs
3949
isInitial
50+
isFirstInitialOfAllSegments
4051
isTerminal
52+
isTerminalOfAllSegments
4153
isEvent
4254
isFirstEventIteration
4355
isFirstEventIterationDirectlyAfterInitial
56+
isFullRestart
4457
isAfterSimulationStart
4558
isZeroCrossing
4659
storeResults
4760
getTime
4861
```
4962

50-
## Variable definitions in functions
63+
## Code Generation
5164

52-
The following functions can be used to define states and algebraic variables inside functions:
65+
This section lists internal functions to **generate Julia code** of the
66+
transformed equations.
5367

54-
```@docs
55-
new_x_segmented_variable!
56-
new_w_segmented_variable!
57-
new_alias_segmented_variable!
58-
new_z_segmented_variable!
59-
get_x_startIndex_from_x_segmented_startIndex
60-
get_scalar_x_segmented_value
61-
get_SVector3_x_segmented_value
62-
get_Vector_x_segmented_value!
63-
add_der_x_segmented_value!
64-
add_w_segmented_value!
68+
```@meta
69+
CurrentModule = Modia
6570
```
6671

72+
```@docs
73+
SimulationModel
74+
generate_getDerivatives!
75+
init!
76+
outputs!
77+
terminate!
78+
derivatives!
79+
DAEresidualsForODE!
80+
affectEvent!
81+
zeroCrossings!
82+
affectStateEvent!
83+
timeEventCondition!
84+
affectTimeEvent!
85+
addToResult!
86+
getFloatType
87+
measurementToString
88+
```
6789

6890

6991

docs/src/index.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
activ# Modia Documentation
1+
# Modia Documentation
22

33
[Modia](https://github.com/ModiaSim/Modia.jl) is an environment in form of a Julia package to model and simulate physical systems (electrical, mechanical, thermo-dynamical, etc.) described by differential and algebraic equations. A user defines a model on a high level with model components (like a mechanical body, an electrical resistance, or a pipe) that are physically connected together. A model component is constructed by **`expression = expression` equations** or by Julia structs/functions, such as the pre-defined [Modia3D] (https://github.com/ModiaSim/Modia3D.jl) multibody components. The defined model is symbolically processed (for example, equations might be analytically differentiated) with algorithms from package [ModiaBase.jl](https://github.com/ModiaSim/ModiaBase.jl). From the transformed model a Julia function is generated that is used to simulate the model with integrators from [DifferentialEquations.jl](https://github.com/SciML/DifferentialEquations.jl).
44
The basic type of the floating point variables is usually `Float64`, but can be set to any
@@ -140,7 +140,7 @@ These changes should usually not influence user models.
140140
show all parameters.
141141

142142
- New functions to add states and algebraic variables from within functions that are not visible in the generated code
143-
(see [Variable definitions in functions](@ref) and example `Modia/test/TestLinearSystems.jl`).
143+
(see [Variables of built-in Components](@ref) and example `Modia/test/TestLinearSystems.jl`).
144144
This feature is used in the next version of
145145
Modia3D to allow (Modia3D) model changes after code generation and to get more light weight code.
146146

models/HeatTransfer/InsulatedRod2.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ end
101101
# Open an initialized InsulatedRod2 model and return a reference to it
102102
function openInsulatedRod!(instantiatedModel::SimulationModel{FloatType,TimeType}, ID)::InsulatedRodStruct{FloatType} where {FloatType,TimeType}
103103
obj::InsulatedRodStruct{FloatType} = Modia.get_instantiatedSubmodel(instantiatedModel, ID)
104-
Modia.get_Vector_x_segmented_value!(instantiatedModel, obj.T_startIndex, obj.T)
104+
Modia.copy_Vector_x_segmented_value_from_state(instantiatedModel, obj.T_startIndex, obj.T)
105105
return obj
106106
end
107107

@@ -127,6 +127,6 @@ function computeInsulatedRodDerivatives!(instantiatedModel, obj::InsulatedRodStr
127127
for i in 1:length(T)
128128
obj.der_T[i] = k*(T_grad1(T,Ta,i) - T_grad2(T,Tb,i))
129129
end
130-
Modia.add_der_x_segmented_value!(instantiatedModel, obj.T_startIndex, obj.der_T)
130+
Modia.copy_der_x_segmented_value_to_state(instantiatedModel, obj.T_startIndex, obj.der_T)
131131
return true
132132
end

src/CodeGeneration.jl

Lines changed: 49 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1680,20 +1680,22 @@ get_instantiatedSubmodel(instantiatedModel, ID) = instantiatedModel.buildDict[ID
16801680

16811681

16821682
"""
1683-
index = new_x_segmented_variable!(
1684-
instantiatedModel::SimulationModel,
1685-
x_name::String, der_x_name::String, startOrInit, x_unit::String="";
1686-
nominal::Float64 = NaN, unbounded::Bool = false)::Int
1683+
startIndex = new_x_segmented_variable!(
1684+
partiallyInstantiatedModel::SimulationModel,
1685+
x_name::String, der_x_name::String, startOrInit, x_unit::String="";
1686+
nominal::Float64 = NaN, unbounded::Bool = false)::Int
16871687
1688-
Reserves storage location for a new x_segmented and der_x_segmented variable and returns
1689-
the index (= x_segmented_startIndex) to access this storage location, in particular
1688+
Generate new states (`x_segmented` and `der_x_segmented` variables) and return the
1689+
`startIndex` of the variables in order that actual values can be inquired or copied from the
1690+
state `x` and state derivative `der(x)`vectors via [`get_x_startIndex_from_x_segmented_startIndex`](@ref).
1691+
`startOrInit` contain the `start` or `init` values of the newly generated `x_segmented` variable.
16901692
1691-
- to copy state values from instantiatedModel.x_segmented[index:index+prod(dims(startOrInit))-1]
1692-
into this storage location
1693-
- to copy state derivative values of this storage location to
1694-
instantiatedModel.der_x_segmented[index:index+prod(dims(startOrInit))-1]
1693+
Actual values of these new variables are stored in:
16951694
1696-
Value startOrInit is the start/init value used during re-initialization of the new segment with initFullRestart!(..).
1695+
- `instantiatedModel.x_segmented[startIndex:startIndex+prod(dims(startOrInit))-1]`
1696+
- `instantiatedModel.der_x_segmented[startIndex:startIndex+prod(dims(startOrInit))-1]`
1697+
1698+
Value `startOrInit` is the start/init value used during re-initialization of the new segment with `initFullRestart!(..)`.
16971699
"""
16981700
function new_x_segmented_variable!(m::SimulationModel{FloatType,TimeType}, x_name::String, der_x_name::String, startOrInit, x_unit::String="";
16991701
nominal::Float64 = NaN, unbounded::Bool = false)::Int where {FloatType,TimeType}
@@ -1760,23 +1762,26 @@ end
17601762

17611763

17621764
"""
1763-
x_startIndex = get_x_startIndex_from_x_segmented_startIndex(instantiatedModel::SimulationModel, x_segmented_startIndex)
1765+
x_startIndex = get_x_startIndex_from_x_segmented_startIndex(
1766+
instantiatedModel::SimulationModel, x_segmented_startIndex)
17641767
1765-
Return the startindex of an x_segmented state with respect to the x-vector,
1766-
given the startIndex with respect to the x_segmented vector
1767-
(x_segmented_startIndex is the return value of new_x_segmented_variable!(..)).
1768+
Return the startindex of an `x_segmented` state with respect to the `x`-vector,
1769+
given the startIndex with respect to the `x_segmented` vector
1770+
(`x_segmented_startIndex` is the return value of `new_x_segmented_variable!(..)`).
17681771
"""
17691772
get_x_startIndex_from_x_segmented_startIndex(m::SimulationModel, x_segmented_startIndex::Int) = m.equationInfo.nxInvariant + x_segmented_startIndex
17701773

17711774

17721775
"""
1773-
index = new_w_segmented_variable!(partiallyInstantiatedModel::SimulationModel, name::String,
1774-
w_segmented_default, unit::String="")::Int
1776+
index = new_w_segmented_variable!(
1777+
partiallyInstantiatedModel::SimulationModel, name::String,
1778+
w_segmented_default, unit::String="")::Int
17751779
1776-
Reserve storage location for a new w_segmented variable. The returned `index` is
1777-
used to store the w_segmented value at communication points in the result data structure.
1780+
Generate new local variable (`w_segmented` variable) and return the `index` of the variable
1781+
in order that actual values can be inquired or copied from the result data structure.
1782+
New values of `w_segmented` variables need only to be computed at communication points.
17781783
Value w_segmented_default is stored as default value and defines type and (fixed) size of the variable
1779-
in this segment.
1784+
in this simulation segment.
17801785
"""
17811786
function new_w_segmented_variable!(m::SimulationModel, name::String, w_segmented_default, unit::String="")::Int
17821787
result = m.result
@@ -1810,9 +1815,10 @@ end
18101815

18111816

18121817
"""
1813-
new_alias_segmented_variable!(partiallyInstantiatedModel, name, aliasName, aliasNegate=false)
1818+
new_alias_segmented_variable!(partiallyInstantiatedModel::SimulationModel,
1819+
name, aliasName, aliasNegate=false)
18141820
1815-
Define new alias segmented variable.
1821+
Define new alias variable.
18161822
"""
18171823
function new_alias_segmented_variable!(m::SimulationModel, name::String, aliasName::String, aliasNegate::Bool=false)::Int
18181824
result = m.result
@@ -1832,8 +1838,8 @@ end
18321838
"""
18331839
startIndex = new_z_segmented_variable!(instantiatedModel, nz)
18341840
1835-
Reserve storage location for nz new segmented zero crossing function and return the startIndex to
1836-
copy it in the vectors of zero crossings
1841+
Generate `nz` new zero crossing variables and return the startIndex to of the variables
1842+
in order that actual values can be copied into the vector of zero crossings.
18371843
"""
18381844
function new_z_segmented_variable!(m::SimulationModel{F,TimeType}, nz::Int)::Int where {F,TimeType}
18391845
eh = m.eventHandler
@@ -1850,58 +1856,60 @@ end
18501856

18511857

18521858
"""
1853-
value = Modia.get_scalar_x_segmented_value(instantiatedModel, startIndex)
1859+
value = Modia.copy_scalar_x_segmented_value_from_state(instantiatedModel, startIndex)
18541860
1855-
Return scalar segmented state value from instantiatedModel given `startIndex`
1861+
Return value of scalar x_segmented variable from state vector `x` by providing its `startIndex`
18561862
(returned from `new_x_segmented_variable!(..)`).
18571863
"""
1858-
get_scalar_x_segmented_value(m::SimulationModel, startIndex::Int) = m.x_segmented[startIndex]
1864+
copy_scalar_x_segmented_value_from_state(m::SimulationModel, startIndex::Int) = m.x_segmented[startIndex]
18591865

18601866

18611867
"""
1862-
value = Modia.get_SVector3_x_segmented_value(instantiatedModel, startIndex)
1868+
value = Modia.copy_SVector3_x_segmented_value_from_state(instantiatedModel, startIndex)
18631869
1864-
Return SVector{3,FloatType}(..) segmented state value from instantiatedModel given `startIndex`
1870+
Return value of `SVector{3,FloatType}` x_segmented variable from state vector `x` by providing its `startIndex`
18651871
(returned from `new_x_segmented_variable!(..)`).
18661872
"""
1867-
@inline get_SVector3_x_segmented_value(m::SimulationModel{FloatType,TimeType}, startIndex::Int) where {FloatType,TimeType} = begin
1873+
@inline copy_SVector3_x_segmented_value_from_state(m::SimulationModel{FloatType,TimeType}, startIndex::Int) where {FloatType,TimeType} = begin
18681874
x_segmented = m.x_segmented
18691875
return SVector{3,FloatType}(x_segmented[startIndex], x_segmented[startIndex+1], x_segmented[startIndex+2])
18701876
end
18711877

18721878
"""
1873-
Modia.get_Vector_x_segmented_value!(instantiatedModel::SimulationModel, startIndex, xi::Vector{FloatType})::Nothing
1879+
Modia.copy_Vector_x_segmented_value_from_state(instantiatedModel::SimulationModel, startIndex, xi::Vector{FloatType})::Nothing
18741880
1875-
Copy state from `instantiatedModel` at index `startIndex` into pre-allocated vector `xi`.
1881+
Return value of `Vector{FloatType}` x_segmented variable from state vector `x` by providing its `startIndex`
1882+
(returned from `new_x_segmented_variable!(..)`) and copying it into the pre-allocated vector `xi`.
18761883
"""
1877-
@inline function get_Vector_x_segmented_value!(m::SimulationModel{FloatType,TimeType}, startIndex::Int, xi::Vector{FloatType})::Nothing where {FloatType,TimeType}
1884+
@inline function copy_Vector_x_segmented_value_from_state(m::SimulationModel{FloatType,TimeType}, startIndex::Int, xi::Vector{FloatType})::Nothing where {FloatType,TimeType}
18781885
copyto!(xi, 1, m.x_segmented, startIndex, length(xi))
18791886
return nothing
18801887
end
18811888

18821889

18831890
"""
1884-
Modia.add_der_x_segmented_value!(instantiatedModel, startIndex, der_x_segmented_value::[FloatType|Vector{FloatType}])
1891+
Modia.copy_der_x_segmented_value_to_state(instantiatedModel, startIndex, der_x_segmented_value::[FloatType|Vector{FloatType}])
18851892
1886-
Copy scalar or array segmented state derivative value `der_x_segmented_value` into `instantiatedModel` starting at index `startIndex`
1887-
(returned from `new_x_segmented_variable!(..)`).
1893+
Copy `der_x_segmented_value` to state derivative vector `der(x)` by providing its `startIndex`
1894+
(returned from `new_x_segmented_variable!(..)`) and copying it into the pre-allocated vector `der_x_segmented_value`.
18881895
"""
1889-
@inline function add_der_x_segmented_value!(m::SimulationModel{FloatType,TimeType}, startIndex::Int, der_x_segmented_value::FloatType)::Nothing where {FloatType,TimeType}
1896+
@inline function copy_der_x_segmented_value_to_state(m::SimulationModel{FloatType,TimeType}, startIndex::Int, der_x_segmented_value::FloatType)::Nothing where {FloatType,TimeType}
18901897
m.der_x_segmented[startIndex] = der_x_segmented_value
18911898
return nothing
18921899
end
1893-
@inline function add_der_x_segmented_value!(m::SimulationModel{FloatType,TimeType}, startIndex::Int, der_x_segmented_value::Vector{FloatType})::Nothing where {FloatType,TimeType}
1900+
@inline function copy_der_x_segmented_value_to_state(m::SimulationModel{FloatType,TimeType}, startIndex::Int, der_x_segmented_value::Vector{FloatType})::Nothing where {FloatType,TimeType}
18941901
copyto!(m.der_x_segmented, startIndex, der_x_segmented_value, 1, length(der_x_segmented_value))
18951902
return nothing
18961903
end
18971904

18981905

18991906
"""
1900-
Modia.add_w_segmented_value!(instantiatedModel::SimulationModel, index::Int, w_segmented_value)::Nothing
1907+
Modia.copy_w_segmented_value_to_result(instantiatedModel::SimulationModel, index::Int, w_segmented_value)::Nothing
19011908
1902-
Store deepcopy(w_segmented_value) at index in instantiatedModel.
1909+
Copy value of local variable (`w-segmented`) to result by providing its `index`
1910+
(returned from `new_w_segmented_variable!`),
19031911
"""
1904-
@inline function add_w_segmented_value!(m::SimulationModel, index::Int, w_segmented_value)::Nothing
1912+
@inline function copy_w_segmented_value_to_result(m::SimulationModel, index::Int, w_segmented_value)::Nothing
19051913
w_segmented_temp = m.result.w_segmented_temp
19061914
@assert(typeof(w_segmented_value) == typeof(w_segmented_temp[index]))
19071915
@assert(size( w_segmented_value) == size( w_segmented_temp[index]))

test/TestLinearSystems.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,11 +170,11 @@ end
170170

171171
function openLinearStateSpace!(instantiatedModel::SimulationModel{FloatType,TimeType}, ID)::LinearStateSpaceStruct{FloatType} where {FloatType,TimeType}
172172
ls = Modia.get_instantiatedSubmodel(instantiatedModel,ID).ls
173-
Modia.get_Vector_x_segmented_value!(instantiatedModel, ls.x_startIndex, ls.x)
173+
Modia.copy_Vector_x_segmented_value_from_state(instantiatedModel, ls.x_startIndex, ls.x)
174174
if Modia.storeResults(instantiatedModel) && length(ls.W) > 0
175175
# w = W*x
176176
mul!(ls.w, ls.W, ls.x)
177-
Modia.add_w_segmented_value!(instantiatedModel, ls.w_index, ls.w)
177+
Modia.copy_w_segmented_value_to_result(instantiatedModel, ls.w_index, ls.w)
178178
end
179179
return ls
180180
end
@@ -188,7 +188,7 @@ function computeStateDerivatives!(instantiatedModel, ls, u)::Bool
188188
# der_x = A*x + B*u
189189
mul!(ls.der_x, ls.A, ls.x)
190190
mul!(ls.der_x, ls.B, u, 1.0, 1.0)
191-
Modia.add_der_x_segmented_value!(instantiatedModel, ls.x_startIndex, ls.der_x)
191+
Modia.copy_der_x_segmented_value_to_state(instantiatedModel, ls.x_startIndex, ls.der_x)
192192
return true
193193
end
194194

0 commit comments

Comments
 (0)