@@ -61,6 +61,9 @@ The source/load behavior itself is provided via a time-dependent function.
61
61
@structural_parameters begin
62
62
load_profile # time dependent load profile
63
63
end
64
+ @constants begin
65
+ load_unit = 1 , [description= " unit of the load profile" , unit= u " m^3/s" ]
66
+ end
64
67
@parameters begin
65
68
C, [description= " Lumped capacitance of connected pipes" , unit= u " m^4 * s^2 / kg" ]
66
69
end
@@ -70,7 +73,7 @@ The source/load behavior itself is provided via a time-dependent function.
70
73
q̃_nw (t), [description= " aggregated flow from pipes into node" , unit= u " m^3/s" , input= true ]
71
74
end
72
75
@equations begin
73
- q̃_inj ~ load_profile (t)
76
+ q̃_inj ~ load_profile (t) * load_unit
74
77
C * Dt (p) ~ q̃_inj + q̃_nw # (30)
75
78
end
76
79
end
@@ -203,16 +206,15 @@ nothing #hide
203
206
#=
204
207
## Load Profile
205
208
206
- The paper specifies the load profile at two nodes. We use the package [`LinearInterpolations .jl`](https://github.com/jw3126/LinearInterpolations .jl)
207
- to get a callable object which represents this picewise linear interpolation.
209
+ The paper specifies the load profile at two nodes. We use the package [`DataInterpolations .jl`](https://github.com/SciML/DataInterpolations .jl)
210
+ to get a callable object which represents this piecewise linear interpolation.
208
211
209
- However, this function is not Symbolics.jl compatible, so we need to stop Symbolics.jl/ModelingToolkit.jl
210
- from tracing it. To do so, we use `@register_symbolic` to declare it as a symbolic function which is treated
211
- as a blackbox.
212
+ Currently, the linear interpolation does not support any units yet. To satisfy the static unit check,
213
+ we multipy the interpolation output by a constant 1 of that unit.
212
214
213
- Additionally, we need to tell ModelingToolkit about the units of this object. This is just used
214
- for the static unit check during construction of the model. Later on, when we generate the
215
- Julia code from the symbolic representation, all units will be stripped.
215
+ Note however, that the unit check is only performed at the construction of the
216
+ model. Later on, when the nummeric code will be generated from the symbolic
217
+ representation, all units will be stripped.
216
218
217
219
!!! note "Discontinuities in RHS"
218
220
The piecewise linear interpolated function creates discontinuities in the RHS of the system.
@@ -221,11 +223,12 @@ Julia code from the symbolic representation, all units will be stripped.
221
223
=#
222
224
load2 = LinearInterpolation (- 1 * Float64[20 , 30 , 10 , 30 , 20 ], [0 , 4 , 12 , 20 , 24 ]* 3600.0 ; extrapolation= ExtrapolationType. Constant)
223
225
load3 = LinearInterpolation (- 1 * Float64[40 , 50 , 30 , 50 , 40 ], [0 , 4 , 12 , 20 , 24 ]* 3600.0 ; extrapolation= ExtrapolationType. Constant)
224
- ModelingToolkit. get_unit (op:: typeof (load2), _) = u " m^3/s"
225
- ModelingToolkit. get_unit (op:: typeof (load3), _) = u " m^3/s"
226
+ ModelingToolkit. get_unit (:: LinearInterpolation , _ ) = 1.0 # type piracy!
226
227
nothing # hide
227
228
228
229
#=
230
+ As a workaround we had to explicitly define `LinearInterpolations` as unitless, which is type piracy! Don't to this in any package code!
231
+
229
232
## Building the Network
230
233
231
234
To build the network, we first need to define the components. This is a two-step process:
@@ -293,22 +296,12 @@ uguess = NWState(nw)
293
296
#=
294
297
This is not a steady state of the system however. To find a true steady state, we want to ensure
295
298
that the LHS of the system is zero.
296
- We can solve for a steady state numerically by defining a Nonlinear Rootfinding problem.
297
-
298
- To do so, we need to wrap the Network object in a closure.
299
- =#
300
- nwwrap = (du, u, p) -> begin
301
- nw (du, u, p, 0 )
302
- nothing
303
- end
304
- initprob = NonlinearProblem (nwwrap, uflat (uguess), pflat (uguess))
305
- initsol = solve (initprob)
306
299
307
- #=
308
- We can create a new `NWState` object by wrapping the solution from the nonlinear problem and the
309
- original parameters in a new `NWState` object.
300
+ We can use the [`find_fixpoint`](@ref) from `NetworkDynamics.jl` to initialize the system.
301
+ Internally, this uses a nummerical solve for the rootfind problem `0 = rhs`.
302
+ The result is automaticially wrapped as a [ `NWState`](@ref) object.
310
303
=#
311
- u0 = NWState (nw, initsol . u, uguess. p )
304
+ u0 = find_fixpoint (nw, uguess)
312
305
313
306
#=
314
307
## Solving the ODE
0 commit comments