Seite - 303 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition
Bild der Seite - 303 -
Text der Seite - 303 -
9.3 Exercises 303
K[N,N] = (beta/dx**2)*(-2)
return K
import odespy
solver = odespy.BackwardEuler(rhs, f_is_linear=True, jac=K)
solver = odespy.ThetaRule(rhs, f_is_linear=True, jac=K, theta=0.5)
solver.set_initial_condition(U_0)
T = 1*60*60
N_t = int(round(T/dt))
time_points = linspace(0, T, N_t+1)
u, t = solver.solve(time_points)
The filerod_BE.pyhas all the details and shows a movie of the solution. We can
run it withanyΔt wewant, its size just impacts theaccuracyof thefirst steps.
Odespysolversapplydense matrices!
Lookingat the entriesof theK matrix, we realize that there are at maximum
three entries different from zero in each row. Therefore, most of the entries
are zeroes. The Odespy solvers expect dense square matrices as input, here
with (N + 1)× (N + 1) elements. When solving the linear systems, a lot
of storage and work are spent on the zero entries in the matrix. It would be
much more efficient to store the matrix as a tridiagonal matrix and apply a
specializedGaussianeliminationsolver for tridiagonalsystems.Actually, this
reduces thework fromtheorderN3 to the orderN.
In one-dimensionaldiffusion problems, the savings of using a tridiagonal
matrix are modest in practice, since the matrices are very small anyway. In
two-andthree-dimensionalPDEproblems,however,onecannotafforddense
squarematrices.Rather,onemust resort tomoreefficient storageformatsand
algorithmstailoredtosuchformats,but this isbeyondthescopeof thepresent
text.
9.3 Exercises
Exercise9.1:SimulateaDiffusion EquationbyHand
Consider the problem given by (9.9), (9.10) and (9.14). Set N = 2 and compute
u0i , u 1
i and u 2
i by hand for i = 0,1,2. Use these values to construct a test
function for checking that the implementation is correct. Copy useful functions
from test_diffusion_pde_exact_linear.py and make a new test function
test_diffusion_hand_calculation.
Filename:test_rod_hand_calculations.py.
Exercise9.2:ComputeTemperatureVariations in theGround
Thesurface temperatureat thegroundshowsdailyandseasonal oscillations.When
the temperature rises at the surface, heat is propagated into the ground, and the
coefficientβ in the diffusion equation determines how fast this propagation is. It
takessometimebeforethe temperaturerisesdownin theground.At thesurface, the
Programming for Computations – Python
A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition
- Titel
- Programming for Computations – Python
- Untertitel
- A Gentle Introduction to Numerical Simulations with Python 3.6
- Band
- Second Edition
- Autoren
- Svein Linge
- Hans Petter Langtangen
- Verlag
- Springer Open
- Datum
- 2020
- Sprache
- englisch
- Lizenz
- CC BY 4.0
- ISBN
- 978-3-319-32428-9
- Abmessungen
- 17.8 x 25.4 cm
- Seiten
- 356
- Schlagwörter
- Programmiersprache, Informatik, programming language, functional, imperative, object-oriented, reflective
- Kategorie
- Informatik