| 1 |
"""This demo program solves Poisson's equation |
| 2 |
|
| 3 |
- div grad u(x, y) = f(x, y) |
| 4 |
|
| 5 |
on the unit square with source f given by |
| 6 |
|
| 7 |
f(x, y) = 10*exp(-((x - 0.5)^2 + (y - 0.5)^2) / 0.02) |
| 8 |
|
| 9 |
and boundary conditions given by |
| 10 |
|
| 11 |
u(x, y) = 0 for x = 0 or x = 1 |
| 12 |
du/dn(x, y) = sin(5*x) for y = 0 or y = 1 |
| 13 |
""" |
| 14 |
|
| 15 |
# Copyright (C) 2007-2011 Anders Logg |
| 16 |
# |
| 17 |
# This file is part of DOLFIN. |
| 18 |
# |
| 19 |
# DOLFIN is free software: you can redistribute it and/or modify |
| 20 |
# it under the terms of the GNU Lesser General Public License as published by |
| 21 |
# the Free Software Foundation, either version 3 of the License, or |
| 22 |
# (at your option) any later version. |
| 23 |
# |
| 24 |
# DOLFIN is distributed in the hope that it will be useful, |
| 25 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 26 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 27 |
# GNU Lesser General Public License for more details. |
| 28 |
# |
| 29 |
# You should have received a copy of the GNU Lesser General Public License |
| 30 |
# along with DOLFIN. If not, see <http://www.gnu.org/licenses/>. |
| 31 |
|
| 32 |
# Begin demo |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
from dolfin import * |
| 37 |
import math |
| 38 |
parameters["form_compiler"]["representation"] = "uflacs" |
| 39 |
|
| 40 |
def compute(nsteps, coordinate_degree, element_degree, gdim): |
| 41 |
# Create mesh and define function space |
| 42 |
|
| 43 |
print(nsteps) |
| 44 |
print(coordinate_degree) |
| 45 |
print(gdim) |
| 46 |
mesh = UnitDiscMesh.create(MPI.comm_world, nsteps, coordinate_degree, gdim) |
| 47 |
V = FunctionSpace(mesh, "Lagrange", element_degree) |
| 48 |
|
| 49 |
# Compute domain area and average h |
| 50 |
area = assemble(1.0*dx(mesh, degree=2)) |
| 51 |
h = (area / mesh.num_cells())**(1.0 / mesh.topology().dim()) |
| 52 |
|
| 53 |
# Define boundary condition |
| 54 |
u0 = Constant(0.0) |
| 55 |
bc = DirichletBC(V, u0, "on_boundary") |
| 56 |
|
| 57 |
# Define variational problem |
| 58 |
u = TrialFunction(V) |
| 59 |
v = TestFunction(V) |
| 60 |
f = Constant(1.0) |
| 61 |
a = inner(grad(u), grad(v))*dx(degree=3) |
| 62 |
L = f*v*dx(degree=3) |
| 63 |
|
| 64 |
# Compute solution |
| 65 |
u = Function(V) |
| 66 |
solve(a == L, u, bc) |
| 67 |
|
| 68 |
# Compute relative error norm |
| 69 |
x = SpatialCoordinate(mesh) |
| 70 |
uexact = (1.0 - x**2) / 4.0 |
| 71 |
M = (u - uexact)**2*dx(degree=5) |
| 72 |
M0 = uexact**2*dx(degree=5) |
| 73 |
err = sqrt(assemble(M) / assemble(M0)) |
| 74 |
|
| 75 |
return err, h, area, mesh.num_cells(), u |
| 76 |
|
| 77 |
def compute_rates(): |
| 78 |
"Compute convergence rates for degrees 1 and 2." |
| 79 |
gdim = 2 |
| 80 |
tdim = gdim |
| 81 |
for coordinate_degree in (1, 2): |
| 82 |
for element_degree in (1, 2): |
| 83 |
print("\nUsing coordinate degree %d, element degree %d" % (coordinate_degree, element_degree)) |
| 84 |
ufile = XDMFFile(MPI.comm_world, "poisson-disc-degree-x%d-e%d.xdmf" % (coordinate_degree, element_degree)) |
| 85 |
encoding = XDMFFile.Encoding.HDF5 if has_hdf5() else XDMFFile.Encoding.ASCII |
| 86 |
preverr = None |
| 87 |
prevh = None |
| 88 |
for i, nsteps in enumerate((1, 8, 64)): |
| 89 |
err, h, area, num_cells, u = compute(nsteps, coordinate_degree, element_degree, gdim) |
| 90 |
if preverr is None: |
| 91 |
conv = 0.0 |
| 92 |
print("conv = N/A, h = %.3e, err = %.3e, area = %.16f, num_cells = %d" % (h, err, area, num_cells)) |
| 93 |
else: |
| 94 |
conv = math.log(preverr/err, prevh/h) |
| 95 |
print("conv = %1.2f, h = %.3e, err = %.3e, area = %.16f, num_cells = %d" % (conv, h, err, area, num_cells)) |
| 96 |
preverr = err |
| 97 |
prevh = h |
| 98 |
|
| 99 |
# Save solution to file |
| 100 |
u.rename('u', 'u') |
| 101 |
|
| 102 |
if MPI.size(MPI.comm_world) > 1 and encoding == XDMFFile.Encoding.ASCII: |
| 103 |
print("XDMF file output not supported in parallel without HDF5") |
| 104 |
else: |
| 105 |
ufile.write(u, encoding) |
| 106 |
|
| 107 |
compute_rates() |