13.07.2015 Views

FEniCS Course - FEniCS Project

FEniCS Course - FEniCS Project

FEniCS Course - 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.

Naive <strong>FEniCS</strong> code for the heat equationfrom dolfin import *# Mesh and function spacemesh = UnitSquare (8, 8)V = FunctionSpace (mesh , " Lagrange ", 1)# Time variablesdt = Constant (0.3); t = float (dt); T = 1.8g_expr = ’1 + x[0]*x[0] + alpha *x[1]*x[1] + beta *t’g = Expression ( g_expr , alpha =3.0, beta =1.2, t=0,degree =2)# Previous and current solutionu0 = interpolate (g, V)u1 = Function (V)# Variational problem at each timeu = TrialFunction (V)v = TestFunction (V)f = Constant (1.2 - 2. - 2*3.0)a = u*v*dx + dt* inner ( grad (u), grad (v))*dxL = u0*v*dx + dt*f*v*dxbc = DirichletBC (V, g, " on_boundary ")while (t


Detailed time-stepping algorithm for the heatequationDefine the boundary conditionCompute u 0 as the projection of the given initial valueDefine the forms a and LAssemble the matrix A from the bilinear form at ← ∆twhile t T doAssemble the vector b from the linear form LApply the boundary conditionSolve the linear system AU = b for U and store in u 1t ← t + ∆tu 0 ← u 1 (get ready for next step)end while6 / 13


A closer look at solveFor linear problems, this codesolve (a == L, u, bcs )is equivalent to this# Assembling a bilinear form yields a matrixA = assemble (a)# Assembling a linear form yields a vectorb = assemble (L)# Applying boundary condition info to systemfor bc in bcs :bc. apply (A, b)# Solve Ax = bsolve (A, u. vector () , b)10 / 13


Implementing the variational problemdt = 0.3u0 = project (g, V)u1 = Function (V)u = TrialFunction (V)v = TestFunction (V)f = Constant ( beta - 2 - 2* alpha )a = u*v*dx + dt* inner ( grad (u), grad (v))*dxL = u0*v*dx + dt*f*v*dxbc = DirichletBC (V, g, " on_boundary ")# assemble only once , before time - steppingA = assemble (a)11 / 13


Implementing the time-stepping loopT = 2t = dtwhile t


<strong>FEniCS</strong> programming exercise: heat equationConsider the heat equation problem:with∂u∂t − ∆u = f in Ω = [0, 1]2 for t > 0u(x, t) = g(x, t) for x ∈ ∂Ω for t > 0u(x, 0) = g(x, 0) for x ∈ Ωf = β − 2 − 2αg(x, t) = 1 + x 2 0 + αx 2 1 + βt (x = (x 0 , x 1 ))Ex. 1 Compute an approximated solution at T = 1.8Ex. 2 Compare the approximated solution to the exact solutionat T = 1.8. How large is the error (in the eyenorm and inthe L 2 (Ω) norm)?Ex. 3 Compute an approximated solution with the same set-upbut on Ω = [0, 1] 3 ⊂ R 3 .13 / 13

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

Saved successfully!

Ooh no, something went wrong!