3. Design philosophy
Before starting with the first examples, it is worth to have a look at the anatomy of a pyoomph script. Irrespective of whether you solve a single ordinary differential equation or a multi-phase flow with mass transfer, surfactants and a moving mesh, the very same three ingredients are always present: a Problem, one or more meshes providing the geometry, and equations that are attached to the domains and interfaces of these meshes. Everything else in this tutorial is a variation of this pattern.
3.1. The Problem class
A simulation is a class inherited from Problem. Its define_problem() method does exactly two things: it adds the geometry with add_mesh() and it adds the equations with add_equations():
from pyoomph import *
class MyProblem(Problem):
def define_problem(self):
self.add_mesh(MyMesh()) # the geometry
eqs = SomeEquations() + SomeOtherEquations()
eqs += SomeBoundaryCondition() @ "left" # bound to an interface
self.add_equations(eqs @ "domain") # bound to a bulk domain
with MyProblem() as problem:
problem.solve()
problem.output()
The two ingredients are joined by the restriction operator @, which reads as “on”: eqs@"domain" means these equations hold on the domain called "domain", and bc@"left" means this condition holds on its boundary called "left". Equations are combined by simple addition with +, so an equation system is assembled piece by piece, exactly in the way you would enumerate the physics of your problem on paper.
The result is an equation tree: the bulk domains form its first level, the interfaces of each domain are its children, interfaces of interfaces (e.g. a contact line, i.e. the boundary of a free surface) its grandchildren, and so on. Fig. 3.1 shows this for a typical multi-domain problem, an evaporating axisymmetric droplet on a substrate. The droplet and the surrounding gas are two separate meshes with their own unknowns. The interface between them appears in both branches of the tree, but "droplet/droplet_gas" and "gas/droplet_gas" are nevertheless two different interface meshes: they occupy the same geometrical position, however each of them sees the interface from its own side and therefore carries its own equations.
Fig. 3.1 A pyoomph problem is a geometry (left) combined with an equation tree (right). Droplet and gas are two separate meshes, connected at the shared interface. Each node of the tree corresponds to a domain or an interface of the geometry and collects all equations imposed there. The full case of an evaporating droplet is discussed in Section 8.6.
3.2. Geometry: meshes
The geometry enters as a MeshTemplate, i.e. a blueprint of nodes, elements and named boundaries, which pyoomph converts into the actual mesh. Simple geometries are predefined, e.g. LineMesh or RectangularQuadMesh. For custom geometries, there are two routes:
Inherit from
MeshTemplateand add nodes and elements by hand in thedefine_geometry()method (cf. Section 5.3.1).Inherit from
GmshTemplateand just describe the outline by points, lines, circular arcs and splines. The mesh itself is then generated by Gmsh (cf. Section 5.3.4):class DropletWithGasMesh(GmshTemplate): def define_geometry(self): p00, pr0 = self.point(0, 0), self.point(1, 0) p0h = self.point(0, 0.5) self.circle_arc(pr0, p0h, through_point=self.point(-1, 0), name="droplet_gas") self.create_lines(p0h, "droplet_axisymm", p00, "droplet_substrate", pr0) self.plane_surface("droplet_gas", "droplet_axisymm", "droplet_substrate", name="droplet") # ... the gas dome is added in the same way, reusing the curve "droplet_gas"
The names are the essential part here: the name given to a surface (or to a group of elements) becomes the name of a bulk domain, the name given to a curve becomes the name of an interface. These are the very names used in the @ operator afterwards, i.e. the names of the nodes of the equation tree. A single template may define several domains, and several templates may be added to the same problem. Coordinates may carry physical units, and moving meshes and remeshing are supported without changing anything in this description.
3.3. Equations
An equation in pyoomph is a class inherited from Equations, with two methods: define_fields() states which unknowns exist and on which finite element space they are discretized, and define_residuals() states the weak form. The Poisson equation (cf. Section 5.1), for instance, reads in its entirety:
class PoissonEquation(Equations):
def __init__(self, source=0):
super().__init__()
self.source = source
def define_fields(self):
self.define_scalar_field("u", "C2") # second order Lagrangian space
def define_residuals(self):
u, v = var_and_test("u") # unknown field and its test function
self.add_residual(weak(grad(u), grad(v)) - weak(self.source, v))
Nothing beyond the weak form has to be provided: no shape functions, no quadrature loop, and in particular no Jacobian, since the latter is obtained by symbolic differentiation. Time derivatives (partial_t), the coordinate system (Cartesian, axisymmetric, \(\ldots\)), the nondimensionalization by the chosen scales and the spatial discretization are all applied automatically when the residual is expanded into C code.
Since equations are just objects that are added up, they compose freely. Boundary conditions, initial conditions, output writers, integral observables, remeshing criteria and even the equations governing the mesh motion are all objects of the very same kind, which is why they all appear side by side in the tree of Fig. 3.1. A considerable number of equations is predefined in the pyoomph.equations package - Navier-Stokes, advection-diffusion, free surfaces, elasticity, and more - so custom classes as the one above are only required for the physics that is genuinely new to your problem.
The rest of this tutorial fills in these three ingredients step by step. We start with the simplest possible geometry, namely none at all: a single ODE.