When you first start to use a piece of software, it is good to have some really stupid examples to refer to, which expert users would probably laugh at. For the noob, like me, it is sometimes difficult to figure out how to do even the most basic things. Take the following example:
You have some kind of partial differential equation involving a function , in one time variable
and one space variable
. The given boundary and initial conditions are
and
. How to formulate these conditions in FEniCS?
Note that in the FEniCS implementation we are actually only considering a single variable, since the time differential will be incorporated by applying a finite difference-type calculation over specified time intervals.
First, we need to define the relevant boundaries. For the first one, , In FEniCS, we define a function that tests whether a point is on the boundary by
def GammaD(x, on_boundary):
return near(x[0],0)
The use of “near” is because we are using floating point numbers, and can’t test for exact equality. Now that we can test for this boundary, we can specify the boundary condition:
g = Expression('...')
bc = DirichletBC(V, g, GammaD)
where was specified earlier in the code, as usual. In my case, I wanted
, so I set
. What about the initial condition
? That is also taken care of by the expression for
. But does the restricted boundary condition take this initial condition into account? Since we later specify
u_n = interpolate(u_D, V)
and initially set in
, it is indeed specified. If we want to add Neumann boundary conditions, we don’t need to do anything more – they are included by default.
Credit where it is due: the post at https://sites.math.rutgers.edu/~falk/math575/Boundary-conditions.html was very helpful, as was http://vefur.simula.no/~hpl/homepage/fenics-tutorial/release-1.0/webm/timedep.html.
As I need to add more and trickier conditions, I’ll update this post.