fem.Function vs ufl.TrialFunctions

Hi all,

I’m defining a mixed function space in the following:

element_mix = ufl.MixedElement(element_u, element_p)
V = fem.FunctionSpace(domain, element_mix)

When I define the trial function for displacement and pressure fields:
(1)

state = fem.Function(V, name='state')
u, p = ufl.split(state)

(2)

u, p = ufl.TrialFunctions(V)

Which one is correct? What’s the difference between them?

Thanks

This depends on how you want to set up your system.
A TestFunction and a TrialFunction are both arguments, instructing UFL what kind of object that you would like to assemble.

If you have no test or trial function in a variational form, the resulting assembled object will be a scalar value.

If you have either a test or a trial function in your variational form, the assembled result will be a vector.

If you have both a test and trial function in your variational form, the result will be a matrix.

This is for instance described at: Form language — Unified Form Language (UFL) 2021.1.0 documentation

If you solve a non-linear problem, it is common to define the residual of the variational form F(u,v)=0 and then differentiate it to get the linearized Jacobian (for instance described at: Custom Newton solvers — FEniCSx tutorial). Then you would use a dolfinx.fem.Function, and not a ufl.TrialFunction to define the “unknown” in your form.

1 Like

Thanks a lot dokken for your clear explanation!