Page - 155 - in Programming for Computations β Python - A Gentle Introduction to Numerical Simulations with Python
Image of the Page - 155 -
Text of the Page - 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.
back to the
book Programming for Computations β Python - A Gentle Introduction to Numerical Simulations with Python"
Programming for Computations β Python
A Gentle Introduction to Numerical Simulations with Python
- Title
- Programming for Computations β Python
- Subtitle
- A Gentle Introduction to Numerical Simulations with Python
- Authors
- Svein Linge
- Hans Petter Langtangen
- Publisher
- Springer Open
- Date
- 2016
- Language
- English
- License
- CC BY-NC 4.0
- ISBN
- 978-3-319-32428-9
- Size
- 17.8 x 25.4 cm
- Pages
- 248
- Keywords
- Programmiersprache, Informatik, programming language, functional, imperative, object-oriented, reflective
- Category
- Informatik