Seite - 155 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python
Bild der Seite - 155 -
Text der Seite - 155 -
4.4 Exercises 155
Write up the complete model, implement it, and rerun the case from Sect. 4.2.8
withvariouschoicesofparameters to illustratevariouseffects.
Filename:SIRV1_V2S.py.
Exercise4.8:Refactoraflatprogram
Consider the fileosc_FE.py implementing the ForwardEulermethod for the os-
cillating systemmodel (4.43)–(4.44). Theosc_FE.py iswhatweoften refer to as
aflat program,meaning that it is just onemainprogramwithno functions. Toeas-
ily reuse thenumericalcomputations inothercontexts,place thepart thatproduces
thenumerical solution (allocationofarrays, initializing thearraysat timezero, and
the time loop) in a functionosc_FE(X_0, omega, dt, T),which returnsu, v,
t. Place theparticularcomputationalexample inosc_FE.py ina functiondemo().
Construct the file osc_FE_func.py such that the osc_FE function can easily be
reused inotherprograms. InPython, thismeans thatosc_FE_func.pyis amodule
that canbe imported in other programs. The requirementof amodule is that there
shouldbenomainprogram,except in the test block. Youmust thereforecalldemo
froma test block (i.e., theblockafterif __name__ == ’__main__’).
Filename:osc_FE_func.py.
Exercise4.9: SimulateoscillationsbyageneralODEsolver
Solve the system (4.43)–(4.44) using the general solver ode_FE in the file ode_
system_FE.pydescribed in Sect. 4.2.6. Program theODEsystemand the call to
theode_FE function inaseparatefileosc_ode_FE.py.
Equip this file with a test function that reads a file with correctu values and
compares these with those computed by the ode_FE function. To find correctu
values,modifytheprogramosc_FE.py todumptheuarraytofile, runosc_FE.py,
and let the test function read the referenceresults fromthatfile.
Filename:osc_ode_FE.py.
Exercise4.10:Compute theenergy inoscillations
a) Make a function osc_energy(u, v, omega) for returning the potential and
kineticenergyofanoscillatingsystemdescribedby(4.43)–(4.44).Thepotential
energy is taken as 1
2 !2u2 while the kinetic energy is 1
2 v2. (Note that these
expressionsarenotexactly thephysicalpotentialandkineticenergy,since these
wouldbe 1
2 mv2 and 1
2 ku2 foramodelmx00CkxD0.)
Place theosc_energy ina separatefileosc_energy.pysuch that the function
canbecalled fromother functions.
b) Addacall toosc_energyin theprogramsosc_FE.pyandosc_EC.pyandplot
the sumof thekinetic andpotential energy. Howdoes the total energydevelop
for theForwardEuler and theEuler-Cromerschemes?
Filenames: osc_energy.py,osc_FE_energy.py,osc_EC_energy.py.
Programming for Computations – Python
A Gentle Introduction to Numerical Simulations with Python
- Titel
- Programming for Computations – Python
- Untertitel
- A Gentle Introduction to Numerical Simulations with Python
- Autoren
- Svein Linge
- Hans Petter Langtangen
- Verlag
- Springer Open
- Datum
- 2016
- Sprache
- englisch
- Lizenz
- CC BY-NC 4.0
- ISBN
- 978-3-319-32428-9
- Abmessungen
- 17.8 x 25.4 cm
- Seiten
- 248
- Schlagwörter
- Programmiersprache, Informatik, programming language, functional, imperative, object-oriented, reflective
- Kategorie
- Informatik