19.06.2015 Views

A FEniCS Tutorial - FEniCS Project

A FEniCS Tutorial - FEniCS Project

A FEniCS Tutorial - FEniCS Project

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

alpha = 3; beta = 1.2<br />

u0 = Expression(’1 + x[0]*x[0] + alpha*x[1]*x[1] + beta*t’,<br />

{’alpha’: alpha, ’beta’: beta})<br />

u0.t = 0<br />

Thisfunctionexpressionhasthecomponentsofxasindependentvariables, while<br />

alpha, beta, and t are parameters. The parameters can either be set through a<br />

dictionaryatconstructiontime,asdemonstratedforalphaandbeta,oranytime<br />

through attributes in the function object, as shown for the t parameter.<br />

The essential boundary conditions, along the whole boundary in this case,<br />

are set in the usual way,<br />

def boundary(x, on_boundary): # define the Dirichlet boundary<br />

return on_boundary<br />

bc = DirichletBC(V, u0, boundary)<br />

We shall use u for the unknown u at the new time level and u_1 for u at the<br />

previous time level. The initial value of u_1, implied by the initial condition on<br />

u, can be computed by either projecting or interpolating I. The I(x,y) function<br />

is available in the program through u0, as long as u0.t is zero. We can then<br />

do<br />

u_1 = interpolate(u0, V)<br />

# or<br />

u_1 = project(u0, V)<br />

Note that we could, as an equivalent alternative to using project, define a 0 and<br />

L 0 as we did in Section 1.9 and form the associated variational problem. To<br />

actually recover the exact solution (76) to machine precision, it is important not<br />

to compute the discrete initial condition by projecting I, but by interpolating I<br />

so that the nodal values are exact at t = 0 (projection results in approximative<br />

values at the nodes).<br />

The definition of a and L goes as follows:<br />

dt = 0.3<br />

# time step<br />

u = TrialFunction(V)<br />

v = TestFunction(V)<br />

f = Constant(beta - 2 - 2*alpha)<br />

a = u*v*dx + dt*inner(nabla_grad(u), nabla_grad(v))*dx<br />

L = (u_1 + dt*f)*v*dx<br />

A = assemble(a)<br />

# assemble only once, before the time stepping<br />

Finally, we perform the time stepping in a loop:<br />

u = Function(V)<br />

T = 2<br />

t = dt<br />

# the unknown at a new time level<br />

# total simulation time<br />

62

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

Saved successfully!

Ooh no, something went wrong!