19.06.2015 Views

A FEniCS Tutorial - FEniCS Project

A FEniCS Tutorial - FEniCS Project

A FEniCS Tutorial - 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.

solver = KrylovSolver(’cg’, ’ilu’)<br />

solver.parameters[’absolute_tolerance’] = 1E-7<br />

solver.parameters[’relative_tolerance’] = 1E-4<br />

solver.parameters[’maximum_iterations’] = 1000<br />

u = Function(V)<br />

U = u.vector()<br />

set_log_level(DEBUG)<br />

solver.solve(A, U, b)<br />

Theprogramdn4_p2D.pyisamodificationofdn3_p2D.pyillustratingthislatter<br />

approach.<br />

The choice of start vector for the iterations in a linear solver is often important.<br />

With the solver.solve(A, U, b) call the default start vector is the zero<br />

vector. A start vector with random numbers in the interval [−100,100] can be<br />

computed as<br />

n = u.vector().array().size<br />

U = u.vector()<br />

U[:] = numpy.random.uniform(-100, 100, n)<br />

solver.parameters[’nonzero_initial_guess’] = True<br />

solver.solve(A, U, b)<br />

Note that we must turn off the default behavior of setting the start vector<br />

(”initial guess”) to zero. A random start vector is included in the dn4_p2D.py<br />

code.<br />

Creating the linear system explicitly in a program can have some advantages<br />

inmoreadvancedproblemsettings. Forexample, Amaybeconstantthroughout<br />

a time-dependent simulation, so we can avoid recalculating A at every time level<br />

and save a significant amount of simulation time. Sections 3.2 and 3.3 deal with<br />

this topic in detail.<br />

1.16 Parameterizing the Number of Space Dimensions<br />

<strong>FEniCS</strong> makes it is easy to write a unified simulation code that can operate in<br />

1D, 2D, and 3D. We will conveniently make use of this feature in forthcoming<br />

examples. As an appetizer, go back to the introductory program d1_p2D.py<br />

in the stationary/poisson directory and change the mesh construction from<br />

UnitSquare(6, 4) to UnitCube(6, 4, 5). Now the domain is the unit cube<br />

partitioned into 6 × 4 × 5 boxes, and each box is divided into six tetrahedrashaped<br />

finite elements for computations. Run the program and observe that we<br />

can solve a 3D problem without any other modifications (!). The visualization<br />

allows you to rotate the cube and observe the function values as colors on the<br />

boundary.<br />

The forthcoming material introduces some convenient technicalities such<br />

that the same program can run in 1D, 2D, or 3D without any modifications.<br />

Consider the simple model problem<br />

u ′′ (x) = 2 in [0,1], u(0) = 0, u(1) = 1, (36)<br />

46

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

Saved successfully!

Ooh no, something went wrong!