Debian Codegraph @

python/demo/undocumented/mixed-poisson-sphere/demo_mixed-poisson-sphere.py

raw
1 """This demo demonstrates how to solve a mixed Poisson type equation
2 defined over a sphere (the surface of a ball in 3D) including how to
3 create a cell_orientation map, needed for some forms defined over
4 manifolds."""
5
6 # Copyright (C) 2012 Marie E. Rognes
7 #
8 # This file is part of DOLFIN.
9 #
10 # DOLFIN is free software: you can redistribute it and/or modify
11 # it under the terms of the GNU Lesser General Public License as published by
12 # the Free Software Foundation, either version 3 of the License, or
13 # (at your option) any later version.
14 #
15 # DOLFIN is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU Lesser General Public License for more details.
19 #
20 # You should have received a copy of the GNU Lesser General Public License
21 # along with DOLFIN. If not, see <http://www.gnu.org/licenses/>.
22 #
23 # First added: 2012-12-09
24 # Last changed: 2012-12-09
25
26 # Begin demo
27
28 from dolfin import *
29 import numpy
30 import matplotlib.pyplot as plt
31
32
33 # Read mesh
34 mesh = Mesh("../sphere_16.xml.gz")
35
36 # Define global normal
37 global_normal = Expression(("x[0]", "x[1]", "x[2]"), degree=1)
38 mesh.init_cell_orientations(global_normal)
39
40 # Define function spaces and basis functions
41 RT1 = FiniteElement("RT", mesh.ufl_cell(), 1)
42 DG0 = FiniteElement("DG", mesh.ufl_cell(), 0)
43 R = FiniteElement("R", mesh.ufl_cell(), 0)
44 W = FunctionSpace(mesh, MixedElement((RT1, DG0, R)))
45
46 (sigma, u, r) = TrialFunctions(W)
47 (tau, v, t) = TestFunctions(W)
48
49 g = Expression("sin(0.5*pi*x[2])", degree=2)
50
51 # Define forms
52 a = (inner(sigma, tau) + div(sigma)*v + div(tau)*u + r*v + t*u)*dx
53 L = g*v*dx
54
55 # Tune some factorization options
56 if has_petsc():
57 # Avoid factors memory exhaustion due to excessive pivoting
58 PETScOptions.set("mat_mumps_icntl_14", 40.0)
59 PETScOptions.set("mat_mumps_icntl_7", "0")
60 # Avoid zero pivots on 64-bit SuperLU_dist
61 PETScOptions.set("mat_superlu_dist_colperm", "MMD_ATA")
62
63 # Solve problem
64 w = Function(W)
65 solve(a == L, w, solver_parameters={"symmetric": True})
66 (sigma, u, r) = w.split()
67
68 # Plot CG1 representation of solutions
69 sigma_cg = project(sigma, VectorFunctionSpace(mesh, "CG", 1))
70 u_cg = project(u, FunctionSpace(mesh, "CG", 1))
71 plt.figure()
72 plot(sigma_cg)
73 plt.figure()
74 plot(u_cg)
75 plt.show()
76
77 # Store solutions
78 file = File("sigma.pvd")
79 file << sigma
80 file = File("u.pvd")
81 file << u