12.07.2015 Views

FEniCS Course - FEniCS Project

FEniCS Course - FEniCS Project

FEniCS Course - FEniCS Project

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>FEniCS</strong> <strong>Course</strong>Lecture 3: Static nonlinear PDEsContributorsMarie E. RognesGarth N. Wells1 / 17


The Stokes equationsWe consider the stationary Stokes equations: find the velocity uand the pressure p such that− div(νɛ(u) − p I) = fdiv u = 0in Ωin Ωwhere ɛ(u) = grad u + (grad u) T and with boundary conditionsu = 0 on ∂Ω D−(νɛ − p I) · n = p 0 n on ∂Ω NIf viscosity ν varies with u (or p),ν = ν(u)this is a nonlinear system of partial differential equations.2 / 17


Canonical nonlinear variational problemThe following canonical notation is used in <strong>FEniCS</strong> for(possibly) nonlinear problems:Find w ∈ W such thatF (w; y) = 0for all y ∈ Ŵ .NoteHere, w is a function, and yis a test function, and so Fis a linear form.For the Stokes exampleThe functions are w = (u, p), y = (v, q) and the form F is∫∫∫F (w; y) = νɛ(u) · grad v dx − p div v dx − div u q dxΩ∫ΩΩ+ p 0 v · n ds∂Ω N4 / 17


The Stokes equations introduce some newconcepts• Mixed function spaces• Integration over boundaries• Solving nonlinear problems (if nonlinear viscosity)• (Reading a mesh from file)• (Adjusting parameters)5 / 17


Step by step: initializing a mesh from fileDOLFIN can read and write meshes from its own .xml or.xml.gz formatmesh = Mesh (" dolfin -1. xml ")plot ( mesh )Conversion tools exist for other mesh formats$ man dolfin - convertWe will need the normal on the mesh boundary facets:n = FacetNormal ( mesh )6 / 17


Step by step: creating mixed function spacesMixed function spaces are created by taking the product ofmore basic spacesV = VectorFunctionSpace (mesh , "CG", 2)Q = FunctionSpace (mesh , "CG", 1)W = V * Q# W = MixedFunctionSpace ([V, Q])You can define functions on mixed spaces and split intocomponents:w = Function (W)(u, p) = split (w)... and arguments:y = TestFunction (W)(v, q) = split (y)# (v, q) = TestFunctions (W)7 / 17


Step by step: more about defining expressionsAgain, the pressure boundary value can be defined using anexpression:p0 = Expression ("1 - a*x[0]", degree =1, a=2)When we specify the degree argument, this will be used as thepolynomial degree when the expression is used in forms.Otherwise, the degree will be estimated heuristically.All parameters (in this case a) must be specified atinitialization, and can be modified later8 / 17


FAQ: What is the difference between a Functionand an Expression?Function... is described by expansion coefficients with reference to aFunctionSpace with a given basis: u = ∑ i u iφ iu = Function (V) # Defines the function spaceu. vector () # The coefficientsExpression... given by an evaluation formula (more or less explicit)f = Expression (" ... ")class Source ( Expression ):def eval (self , values , x)...An Expression can be projected or interpolated, or in some otherway mapped, onto a Function, the converse is non-trivial.9 / 17


Step by step: defining the viscosityWe may want to play with different viscosities.In the simplest case, it is just constant: ν = 0.1nu = 0.1... or it can vary with the domain: ν = 1 + 100x 1nu = Expression ("1 + 100 *x[1]")... or it can vary with the unknown: ν = (u · u) 1/2w = Function (W)(u, p) = split (w)def viscosity (u):return inner (u, u)**(1./2)nu = viscosity (u)10 / 17


Stokes: defining the variational formAssume that we havew = Function (W)(u, p) = split (w)(v, q) = TestFunctions (W)p0 = ...; nu = ...; n = ...We can now specify the linear form Fepsilon = 2* sym ( grad (u))F = (nu* inner ( epsilon , grad (v)) \- div (u)*q - div (v)*p)*dx \+ p0*dot (v, n)*dsNote that dx denotes integration over cells, ds denotes integrationover exterior (boundary) facets, dS denotes integration over interiorfacets.FAQ: How to specify integration over only subdomains? See thePoisson with multiple subdomains demo.12 / 17


Step by step: solving (nonlinear) variationalproblemsOnce a variational problem has been defined, it may be solvedby calling the solve function (as for linear problems):solve (F == 0, w, bcs )Or more verboselydF = derivative (F, w)pde = NonlinearVariationalProblem (F, w, bcs , dF)solver = NonlinearVariationalSolver ( pde )solver . solve ()Extracting the subfunctions (as DOLFIN functions)(u, p) = w. split ( deepcopy = True )13 / 17


Step by step: adjusting parameters in DOLFINAdjusting global parametersfrom dolfin import *info ( parameters , True )parameters [" form_compiler "][" cpp_optimize "] = True# parameters [" form_compiler "][" optimize "] = TrueAdjusting local (and nested) parameterssolver = NonlinearVariationalSolver ( pde )info ( solver . parameters , True )solver . parameters [" symmetric "] = Truesolver . parameters [" newton_solver "][" maximum_iterations "]= 10014 / 17


Stokes: a complete code examplefrom dolfin import *# Use -02 optimizationparameters [" form_compiler "][" cpp_optimize "] = True# Define mesh and geometrymesh = Mesh (" dolfin -2. xml .gz")n = FacetNormal ( mesh )# Define Taylor -- Hood function space WV = VectorFunctionSpace (mesh , "CG" , 2)Q = FunctionSpace ( mesh , "CG", 1)W = V * Q# Define Function and TestFunction (s)w = Function (W)(u, p) = split (w)(v, q) = TestFunctions (W)# Define viscosity and bcsnu = Expression ("0.2*(1+ pow (x[1],2))", degree =2)p0 = Expression ("1.0-x[0]", degree =1)bcs = DirichletBC (W. sub (0), (0.0, 0.0)," on_boundary && !( near (x[0], 0.0) || near (x[0], 1.0))")# Define variational formepsilon = sym ( grad (u))F = (nu* inner ( epsilon , grad (v)) - div (u)*q - div (v)*p)*dx\+ p0*dot (v,n)*ds# Solve problemsolve (F == 0, w, bcs )# Plot solutionsplot (u, title =" Velocity ", interactive = True )plot (p, title =" Pressure ", interactive = True )15 / 17


Stokes: running the example yields$ python stokes_example .py16 / 17


<strong>FEniCS</strong> programming exercise!Solve the Stokes problem on Ω defined by the dolfin-2.xmlmesh, defined by the following data− div(νɛ(u) − p I) = 0div u = 0in Ωin Ω−(νɛ(u) − p I) · n = p 0 n on ∂Ω N = {(x 0 , x 1 )| x 0 = 0 or x 0 = 1}p 0 = 1 − x 0u = 0on ∂Ω D = ∂Ω\∂Ω NEx. 1 Consider a constant viscosity ν = 0.2, compute and plotthe solutions.Ex. 2 Consider the nonlinear viscosityν = ν(u) = 0.5(grad u · grad u) 1/(2(n−1)) , n = 4Compute and plot the solutions.Hint: For Ex. 2, you may need to compute an approximation first in order to provide asuitable initial guess to the Newton solver17 / 17

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

Saved successfully!

Ooh no, something went wrong!