when to use Function vs Coefficient

7 months ago by
I'm working on a solver for a certain nonlinear elliptic PDE; for demonstrative purposes let's just say it's the minimal surface equation. Rather than work with the PDE, I'm trying to use the variational principle directly, i.e. I'm minimizing the functional

\[J(u) = \int_\Omega\sqrt{1 + |\nabla u|^2} dx\]

subject to \(u|_{\partial\Omega} = g\). I could implement this in fenics by defining \(u\) as either a Function or a Coefficient. For example, a code snippet to compute the Newton search direction might look like this:

mesh = UnitSquareMesh(32, 32)
V = FunctionSpace(mesh, 'CG', 1)
u = Coefficient(V)
J = sqrt(1 + inner(grad(u), grad(u))) * dx

u0 = interpolate(Expression('sin(x[0] + 2*x[1]) - cos(5*x[0] - 3*x[1])', degree=1), V)
bc = DirichletBC(V, Constant(0), lambda x, on_boundary: on_boundary)

dJ = derivative(J, u)
H = derivative(dJ, u)
p = Function(V)
solve(replace(H, {u: u0}) == -replace(dJ, {u: u0}), p, bc)​

Alternatively, I could have defined \(u\) as a Function from the start, in which case the calls to replace would no longer be necessary. What are the use cases for Coefficient vs for Function? Does replace have the same performance when the object being replaced is a Function or a Coefficient? Are there any weird gotchas associated with one or the other?
Community: FEniCS Project

1 Answer

7 months ago by
Coefficient is UFL. In Python interface of DOLFIN GenericFunction subclasses work seamlessly is Coefficient in UFL language.

In other words, Coefficient is used in UFL when using C++ DOLFIN (or not using DOLFIN at all). Normally Function, Expression or Constant are used instead of Coefficient in Python DOLFIN.
Please login to add an answer/comment or follow this question.

Similar posts:
Search »