A FEniCS Tutorial - FEniCS Project

A FEniCS Tutorial - FEniCS Project A FEniCS Tutorial - FEniCS Project

fenicsproject.org
from fenicsproject.org More from this publisher
19.06.2015 Views

problem = NonlinearVariationalProblem(F, u, bcs, J) solver = NonlinearVariationalSolver(problem) solver.solve() where F corresponds to the nonlinear form F(u;v), u is the unknown Function object, bcs represents the essential boundary conditions (in general a list of DirichletBC objects), and J is a variational form for the Jacobian of F. Let us explain in detail how to use the built-in tools for nonlinear variational problems and their solution. The F form corresponding to (41) is straightforwardly defined as follows, assuming q(u) is coded as a Python function: u_ = Function(V) # most recently computed solution v = TestFunction(V) F = inner(q(u_)*nabla_grad(u_), nabla_grad(v))*dx Note here that u_ is a Function (not a TrialFunction). An alternative and perhaps more intuitive formula for F is to define F(u;v) directly in terms of a trial function for u and a test function for v, and then create the proper F by u = TrialFunction(V) v = TestFunction(V) F = inner(q(u)*nabla_grad(u), nabla_grad(v))*dx u_ = Function(V) # the most recently computed solution F = action(F, u_) The latter statement is equivalent to F(u = u − ;v), where u − is an existing finite element function representing the most recently computed approximation to the solution. (Note that u k and u k+1 in the previous notation correspond to u − and u in the present notation. We have changed notation to better align the mathematics with the associated UFL code.) ThederivativeJ (J)ofF (F)isformallytheGateauxderivativeDF(u k ;δu,v) of F(u;v) at u = u − in the direction of δu. Technically, this Gateaux derivative is derived by computing lim ǫ→0 d dǫ F i(u − +ǫδu;v). (62) The δu is now the trial function and u − is the previous approximation to the solution u. We start with ∫ d ∇v ·(q(u − +ǫδu)∇(u − +ǫδu)) dx dǫ Ω and obtain ∫ ∇v ·[q ′ (u − +ǫδu)δu∇(u − +ǫδu)+q(u − +ǫδu)∇δu] dx, Ω which leads to ∫ Ω ∇v ·[q ′ (u − )δu∇(u − )+q(u − )∇δu] dx, (63) 56

as ǫ → 0. This last expression is the Gateaux derivative of F. We may use J or a(δu,v) for this derivative, the latter having the advantage that we easily recognize the expression as a bilinear form. However, in the forthcoming code examples J is used as variable name for the Jacobian. The specification of J goes as follows if du is the TrialFunction: du = TrialFunction(V) v = TestFunction(V) u_ = Function(V) # the most recently computed solution F = inner(q(u_)*nabla_grad(u_), nabla_grad(v))*dx J = inner(q(u_)*nabla_grad(du), nabla_grad(v))*dx + \ inner(Dq(u_)*du*nabla_grad(u_), nabla_grad(v))*dx The alternative specification of F, with u as TrialFunction, leads to u = TrialFunction(V) v = TestFunction(V) u_ = Function(V) # the most recently computed solution F = inner(q(u)*nabla_grad(u), nabla_grad(v))*dx F = action(F, u_) J = inner(q(u_)*nabla_grad(u), nabla_grad(v))*dx + \ inner(Dq(u_)*u*nabla_grad(u_), nabla_grad(v))*dx The UFL language, used to specify weak forms, supports differentiation of forms. Thisfeaturefacilitatesautomaticsymbolic computationoftheJacobianJ by calling the function derivative with F, the most recently computed solution (Function), and the unknown (TrialFunction) as parameters: du = TrialFunction(V) v = TestFunction(V) u_ = Function(V) # the most recently computed solution F = inner(q(u_)*nabla_grad(u_), nabla_grad(v))*dx J = derivative(F, u_, du) # Gateaux derivative in dir. of du or u = TrialFunction(V) v = TestFunction(V) u_ = Function(V) # the most recently computed solution F = inner(q(u)*nabla_grad(u), nabla_grad(v))*dx F = action(F, u_) J = derivative(F, u_, u) # Gateaux derivative in dir. of u The derivative function is obviously very convenient in problems where differentiating F by hand implies lengthy calculations. ThepreferredimplementationofFandJ,dependingonwhetherduoruisthe TrialFunction object, is a matter of personal taste. Derivation of the Gateaux derivative by hand, as shown above, is most naturally matched by an implementation where du is the TrialFunction, while use of automatic symbolic differentiation with the aid of the derivative function is most naturally matched by 57

as ǫ → 0. This last expression is the Gateaux derivative of F. We may use<br />

J or a(δu,v) for this derivative, the latter having the advantage that we easily<br />

recognize the expression as a bilinear form. However, in the forthcoming code<br />

examples J is used as variable name for the Jacobian.<br />

The specification of J goes as follows if du is the TrialFunction:<br />

du = TrialFunction(V)<br />

v = TestFunction(V)<br />

u_ = Function(V) # the most recently computed solution<br />

F = inner(q(u_)*nabla_grad(u_), nabla_grad(v))*dx<br />

J = inner(q(u_)*nabla_grad(du), nabla_grad(v))*dx + \<br />

inner(Dq(u_)*du*nabla_grad(u_), nabla_grad(v))*dx<br />

The alternative specification of F, with u as TrialFunction, leads to<br />

u = TrialFunction(V)<br />

v = TestFunction(V)<br />

u_ = Function(V) # the most recently computed solution<br />

F = inner(q(u)*nabla_grad(u), nabla_grad(v))*dx<br />

F = action(F, u_)<br />

J = inner(q(u_)*nabla_grad(u), nabla_grad(v))*dx + \<br />

inner(Dq(u_)*u*nabla_grad(u_), nabla_grad(v))*dx<br />

The UFL language, used to specify weak forms, supports differentiation of<br />

forms. Thisfeaturefacilitatesautomaticsymbolic computationoftheJacobianJ<br />

by calling the function derivative with F, the most recently computed solution<br />

(Function), and the unknown (TrialFunction) as parameters:<br />

du = TrialFunction(V)<br />

v = TestFunction(V)<br />

u_ = Function(V) # the most recently computed solution<br />

F = inner(q(u_)*nabla_grad(u_), nabla_grad(v))*dx<br />

J = derivative(F, u_, du) # Gateaux derivative in dir. of du<br />

or<br />

u = TrialFunction(V)<br />

v = TestFunction(V)<br />

u_ = Function(V) # the most recently computed solution<br />

F = inner(q(u)*nabla_grad(u), nabla_grad(v))*dx<br />

F = action(F, u_)<br />

J = derivative(F, u_, u) # Gateaux derivative in dir. of u<br />

The derivative function is obviously very convenient in problems where differentiating<br />

F by hand implies lengthy calculations.<br />

ThepreferredimplementationofFandJ,dependingonwhetherduoruisthe<br />

TrialFunction object, is a matter of personal taste. Derivation of the Gateaux<br />

derivative by hand, as shown above, is most naturally matched by an implementation<br />

where du is the TrialFunction, while use of automatic symbolic differentiation<br />

with the aid of the derivative function is most naturally matched by<br />

57

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!