You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: doc/.src/chapters/softeng2/softeng2.do.txt
+44-4Lines changed: 44 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -452,16 +452,43 @@ of arguments, while a class-based solver naturally has a mix of method
452
452
arguments and user-supplied methods. (Well, to be more precise,
453
453
our solvers have demanded `user_action`
454
454
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.
456
462
457
463
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
460
469
arrays that describe the mesh in a special `Mesh` class and make
461
470
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
+
462
484
463
485
===== Class Problem =====
464
486
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
+
465
492
===== Class Mesh =====
466
493
467
494
The `Mesh` class can be made valid for a space-time mesh in any number
@@ -510,10 +537,23 @@ should explain the functionality.
510
537
===== Class Solver =====
511
538
512
539
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`:
514
541
515
542
[hpl: Rewrite solver!]
516
543
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
0 commit comments