Skip to content

Commit ef07046

Browse files
committed
fix units in gas_network example
1 parent 8c6c386 commit ef07046

File tree

1 file changed

+18
-25
lines changed

1 file changed

+18
-25
lines changed

docs/examples/gas_network.jl

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ The source/load behavior itself is provided via a time-dependent function.
6161
@structural_parameters begin
6262
load_profile # time dependent load profile
6363
end
64+
@constants begin
65+
load_unit = 1, [description="unit of the load profile", unit=u"m^3/s"]
66+
end
6467
@parameters begin
6568
C, [description="Lumped capacitance of connected pipes", unit=u"m^4 * s^2 / kg"]
6669
end
@@ -70,7 +73,7 @@ The source/load behavior itself is provided via a time-dependent function.
7073
q̃_nw(t), [description="aggregated flow from pipes into node", unit=u"m^3/s", input=true]
7174
end
7275
@equations begin
73-
q̃_inj ~ load_profile(t)
76+
q̃_inj ~ load_profile(t) * load_unit
7477
C * Dt(p) ~ q̃_inj + q̃_nw # (30)
7578
end
7679
end
@@ -203,16 +206,15 @@ nothing #hide
203206
#=
204207
## Load Profile
205208
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.
208211
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.
212214
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.
216218
217219
!!! note "Discontinuities in RHS"
218220
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.
221223
=#
222224
load2 = LinearInterpolation(-1*Float64[20, 30, 10, 30, 20], [0, 4, 12, 20, 24]*3600.0; extrapolation=ExtrapolationType.Constant)
223225
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!
226227
nothing #hide
227228

228229
#=
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+
229232
## Building the Network
230233
231234
To build the network, we first need to define the components. This is a two-step process:
@@ -293,22 +296,12 @@ uguess = NWState(nw)
293296
#=
294297
This is not a steady state of the system however. To find a true steady state, we want to ensure
295298
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)
306299
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.
310303
=#
311-
u0 = NWState(nw, initsol.u, uguess.p)
304+
u0 = find_fixpoint(nw, uguess)
312305

313306
#=
314307
## Solving the ODE

0 commit comments

Comments
 (0)