9.2 Finite Difference Methods
295
Problems with reusing the rhs function
The rhs function must take u and t as arguments, because that is required by
the ode_FE function. What about the variables beta, dx, L, x, dsdt, g, and
dudx that the rhs function needs? These are global in the solution we have
presented so far. Unfortunately, this has an undesired side effect: we cannot
import the rhs function in a new file, define dudx and dsdt in this new file
and get the imported rhs to use these functions. The imported rhs will use
the global variables, including functions, in its own module.
How can we find solutions to this problem? Technically, we must pack the
extra data beta, dx, L, x, dsdt, g, and dudx with the rhs function, which
requires more advanced programming considered beyond the scope of this
text.
A class is the simplest construction for packing a function together with
data, see the beginning of Chapter 7 in [11] for a detailed example on how
classes can be used in such a context. Another solution in Python, and
especially in computer languages supporting functional programming, is so
called closures. They are also covered in Chapter 7 in the mentioned reference
and behave in a magic way. The third solution is to allow an arbitrary set of
arguments for rhs in a list to be transferred to ode_FE and then back to rhs.
Appendix H.4 in [11] explains the technical details.
9.2.4 Animation: Heat Conduction in a Rod
Let us return to the case with heat conduction in a rod (9.1)–(9.4). Assume that
the rod is 50 cm long and made of aluminum alloy 6082. The β parameter equals
κ/((c), where κ is the heat conduction coefficient, is the density, and c is the
heat capacity. We can find proper values for these physical quantities in the case
of aluminum alloy 6082: = 2.7 · 10 3 kg/m 3 , κ = 200 W
mK , c = 900 J
Kkg .
This results in β = κ/((c) = 8.2 · 10 −5 m 2 /s. Preliminary simulations show
that we are close to a constant steady state temperature after 1 h, i.e., T =
3600 s.
The rhs function from the previous section can be reused, only the functions s,
dsdt, g, and dudx must be changed (see file rod_FE.py):
def dudx(t):
return 0
def s(t):
return 323
def dsdt(t):
return 0
def g(x, t):
return 0
Précédent

- 314/350

Suivant