Skip to content

Commit be9f9e7

Browse files
author
sveinlin
committed
added class based wave solver in ch on SW engineering
1 parent c27e7f9 commit be9f9e7

52 files changed

Lines changed: 6571 additions & 14315 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

doc/.src/chapters/softeng2/softeng2.do.txt

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -452,16 +452,43 @@ of arguments, while a class-based solver naturally has a mix of method
452452
arguments and user-supplied methods. (Well, to be more precise,
453453
our solvers have demanded `user_action`
454454
to be a function provided by the user, so it is possible to mix variables
455-
and functions in the input also in a solver function.)
455+
and functions in the input also with a solver function.)
456+
457+
We will next illustrate how some of the functionality in `wave1D_dn_vc.py`
458+
may be implemented by using classes. Focusing on class implementation aspects,
459+
we restrict the example case to a simpler wave with constant wave speed $c$.
460+
Applying the method of manufactured solutions, we test whether the class based
461+
implementation is able to compute the known exact solution within machine precision.
456462

457463
We will create a class `Problem` to hold the physical parameters of the
458-
problem and a class `Solver` to hold the numerical parameters and
459-
the solver function. In addition, it is convenient to collect the
464+
problem and a class `Solver` to hold the numerical solution parameters besides
465+
the solver function itself. As the number of parameters increases, so does
466+
the amount of repetitive code. We therefore take the opportunity to illustrate
467+
how this may be counteracted by introducing a super class `Parameters` that allows
468+
code to be parameterized. In addition, it is convenient to collect the
460469
arrays that describe the mesh in a special `Mesh` class and make
461470
a class `Function` for a mesh function (mesh point values and its mesh).
471+
All the following code is found in "`wave1D_oo.py`": "${src_softeng2}/wave1D_oo.py".
472+
473+
===== Class Parameters =====
474+
475+
The classes `Problem` and `Solver` both inherit class `Parameters`, which
476+
handles reading of parameters from the command line and has methods
477+
for setting and getting parameter values. Since processing dictionaries
478+
is easier than processing a collection of individual attributes, the class
479+
`Parameters` requires each class `Problem` and `Solver` to represent their
480+
parameters by dictionaries, one compulsory and two optional ones. The compulsory dictionary, `self.prm`, contains all parameters, while a second and optional dictionary, `self.type`, holds the associated object types, and a third and optional dictionary, `self.help`, stores help strings. The `Parameters` class may be implemented as follows:
481+
482+
@@@CODE src-softeng2/wave1D_oo.py fromto: class Parameters@class Problem
483+
462484

463485
===== Class Problem =====
464486

487+
Inheriting the `Parameters` class, our class `Problem` is defined as:
488+
489+
@@@CODE src-softeng2/wave1D_oo.py fromto: class Problem@class Solver
490+
491+
465492
===== Class Mesh =====
466493

467494
The `Mesh` class can be made valid for a space-time mesh in any number
@@ -510,10 +537,23 @@ should explain the functionality.
510537
===== Class Solver =====
511538

512539
With the `Mesh` and `Function` classes in place, we can rewrite
513-
the `solver` function, but we put it as a method in class `Solver`:
540+
the `solver` function, but we make it a method in class `Solver`:
514541

515542
[hpl: Rewrite solver!]
516543

544+
@@@CODE src-softeng2/wave1D_oo.py fromto: class Solver@def test
545+
546+
Observe that the solutions from all time steps are stored in the mesh function,
547+
which allows error assessment (in `assert_no_error`) to take place after all
548+
solutions have been found. Of course, in 2D or 3D, such a strategy may place too
549+
high demands on available computer memory, in which case intermediate results
550+
could be stored on file.
551+
552+
Running `wave1D_oo.py` gives a printout showing that the class-based
553+
implementation performs as expected, i.e. that the known exact solution
554+
is reproduced (within machine precision).
555+
556+
517557
======= Migrating loops to Cython =======
518558
label{wave2D3D:impl:Cython}
519559

0 commit comments

Comments
 (0)