Skip to content

Commit 76742ed

Browse files
committed
Tutorial improved
1 parent 333d446 commit 76742ed

File tree

2 files changed

+69
-5
lines changed

2 files changed

+69
-5
lines changed

docs/src/Tutorial.md

Lines changed: 67 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,9 @@ returning a vector `assign`:
134134

135135
```julia
136136
using ModiaBase
137-
M = 7 # Number of variables
138-
vActive = fill(true,M)
137+
vActive = fill(true,7)
139138
vActive[5] = false # state C.v is known
140-
assign = matching(G, M, vActive)
139+
assign = matching(G, 7, vActive)
141140

142141
# assign = [2,6,3,1,0,5,4]
143142
```
@@ -149,9 +148,10 @@ The meaning of vector `assign` is that
149148
- etc.
150149

151150

152-
### 1.4 Block Lower Triangular transformation
151+
### 1.4 Sorting
153152

154-
In a follow-up step, equations are sorted and algebraic loops determined:
153+
In a follow-up step, equations are sorted and algebraic loops determined
154+
(= Block Lower Triangular transformation):
155155

156156
![Incidence Matrix of sorted equations of Low Pass Filter](../resources/images/LowPassFilterReduced_BLT.png)
157157

@@ -176,6 +176,68 @@ The meaning is for example that the second BLT block consists of
176176
equations 3,4,2,1 and these equations form an algebraic loop.
177177

178178

179+
### 1.5 Reducing sizes of equation systems
180+
181+
In a follow-up step, the sizes of equation systems are reduced by
182+
variable substitution (= tearing). Applying [`ModiaBase.tearEquations!`](@ref) to the
183+
low pass filter circuit, reduces the dimension of BLT block 2 from size 4 to size 1
184+
resulting in the following equation system:
185+
186+
```julia
187+
# iteration variables (inputs): C.i
188+
# residual variables (outputs): residual
189+
190+
R.v := R.R*C.i
191+
R.i.v := -Ri.R*C.i
192+
R.p.v := Ri.v + V.v
193+
residual := R.v - R.p.v + C.v
194+
```
195+
196+
197+
### 1.6 Generation of AST
198+
199+
In a final step, the AST (Abstract Syntax Tree) of the model is
200+
generated. Hereby, it is determined that the equation system of section 1.4 and 1.5
201+
is linear in the iteration variable (`C.i`) and an AST is generated
202+
to build-up a linear equation system `A*C.i = b` and solve this system numerically
203+
with an LU decomposition whenever the AST is called (if the equation system has size 1,
204+
a simple division is used instead of calling a linear equation solver). Applying
205+
[`ModiaBase.getSortedAndSolvedAST`](@ref) results basically in a function
206+
`getDerivatives` that can be solved with the many ODE integrators of
207+
[DifferentialEquations.jl](https://github.com/SciML/DifferentialEquations.jl):
208+
209+
```julia
210+
function getDerivatives(_der_x, _x, _m, _time)::Nothing
211+
_m.time = ModiaLang.getValue(_time)
212+
_m.nGetDerivatives += 1
213+
instantiatedModel = _m
214+
_p = _m.evaluatedParameters
215+
_leq_mode = nothing
216+
time = _time
217+
var"C.v" = _x[1]
218+
var"V.v" = (_p[:V])[:V]
219+
begin
220+
local var"C.i", var"R.v", var"Ri.v", var"R.p.v"
221+
_leq_mode = _m.linearEquations[1]
222+
_leq_mode.mode = -2
223+
while ModiaBase.LinearEquationsIteration(_leq_mode, _m.isInitial, _m.time, _m.timer)
224+
var"C.i" = _leq_mode.vTear_value[1]
225+
var"R.v" = (_p[:R])[:R] * var"C.i"
226+
var"Ri.v" = (_p[:Ri])[:R] * -var"C.i"
227+
var"R.p.v" = var"Ri.v" + var"V.v"
228+
_leq_mode.residual_value[1] = (var"R.v" + -1var"R.p.v") + var"C.v"
229+
end
230+
_leq_mode = nothing
231+
end
232+
var"der(C.v)" = var"C.i" / (_p[:C])[:C]
233+
_der_x[1] = var"der(C.v)"
234+
if _m.storeResult
235+
ModiaLang.addToResult!(_m, _der_x, time, var"R.v", var"R.p.v", var"Ri.v", var"C.i", var"V.v")
236+
end
237+
return nothing
238+
end
239+
```
240+
179241

180242
## 2. Singular DAEs (Higher Index DAEs)
181243

docs/src/index.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ julia> ]add Unitful, Measurements, MonteCarloMeasurements, Distributions
8585

8686
### Version 0.7.6
8787

88+
- Fixed bug: If unitless=true, no unit is associated with the tearing variable.
89+
8890
- Solve linear equation systems optionally with [RecursiveFactorization.jl](https://github.com/YingboMa/RecursiveFactorization.jl)
8991
instead of the default `lu!(..)` and `ldiv!(..)`.
9092

0 commit comments

Comments
 (0)