I am facing a behavior I did not expect when projecting UFL expression onto given function space for function assignement.
a (not so) minimal example to reproduce can be found here. Steps are
define a symbolic expression with Sympy for scalar function
use Sympy codegen capabilities to build associated Dolfin Expression and assign to given Dolfin Function, say “u”, by projecting onto FunctionSpace
compute rho = 1 + (dot(grad(u), grad(u))**0.5 by three methods, namely
3.1) using symbolic evaluation of grad(u) with Sympy and dolfin.project,
3.2) using numerical evaluation of grad(u) with dolfin.grad and dolfin.project,
3.3) using numerical evaluation of grad(u) with UFL syntax “as_vector(u.dx(i), i)” and dolfin.project.
Results are quite surprising, as seen from printing of min and max values:
This isn’t surprising. You’re projecting the gradient to a less rich function space in your dolfin implementation than the exact solution you compute for your sympy expression by symbolic differentiation. Consider enriching the approximation spaces in dolfin by increasing the degree of the approximating polynomial.
Furthermore, your pseudo L_\infty norms aren’t a great method for measuring the projection error. Consider instead measuring in the L_2 and H^1 norms.
I’am not sure to understand. I detail my question below.
Analytic evaluation
Target function is \rho(x) = 1 + \sqrt{\nabla u(x) \cdot \nabla u(x) } .
Implementation of u from Sympy analytic expression:
order = 2 # usually 2 or 3
V_u = FunctionSpace(mesh, "Lagrange", order)
u = Function(V_u)
u_c_code = ... # C code generated from sympy analytic expression
u_dolfin_expr = Expression(u_c_code, element=u.ufl_element(), ...)
u.assign(project(u_dolfin_expr, u.function_space()))
Implementation of \rho with Sympy evaluation of derivatives (\rho is one order of approximation lesser than approximation order of function u due to spatial derivative):
I expected the implementation of rho_ufl and rho_grad to use the same UFL mechanism and numerical evaluation under the hood. Why are they so different?
The implementation of rho_ufl with order=2 yields a minimum value of -2.51 for the function 1 \leq \rho(x) = 1 + \sqrt{\nabla u(x) \cdot \nabla u(x) }. How is it possible?