| 1 |
"""Unit tests for MultiMesh PDE solvers""" |
| 2 |
|
| 3 |
# Copyright (C) 2017 August Johansson |
| 4 |
# |
| 5 |
# This file is part of DOLFIN. |
| 6 |
# |
| 7 |
# DOLFIN is free software: you can redistribute it and/or modify |
| 8 |
# it under the terms of the GNU Lesser General Public License as published by |
| 9 |
# the Free Software Foundation, either version 3 of the License, or |
| 10 |
# (at your option) any later version. |
| 11 |
# |
| 12 |
# DOLFIN is distributed in the hope that it will be useful, |
| 13 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
# GNU Lesser General Public License for more details. |
| 16 |
# |
| 17 |
# You should have received a copy of the GNU Lesser General Public License |
| 18 |
# along with DOLFIN. If not, see <http://www.gnu.org/licenses/>. |
| 19 |
# |
| 20 |
# First added: 2017-07-18 |
| 21 |
# Last changed: 2017-07-18 |
| 22 |
|
| 23 |
import pytest |
| 24 |
from dolfin import * |
| 25 |
|
| 26 |
from dolfin_utils.test import skip_in_parallel, fixture |
| 27 |
|
| 28 |
def exactsolution_2d_impl(): |
| 29 |
return Expression("x[0] + x[1]", degree=1) |
| 30 |
|
| 31 |
def exactsolution_3d_impl(): |
| 32 |
return Expression("x[0] + x[1] + x[2]", degree=1) |
| 33 |
|
| 34 |
def solve_multimesh_poisson(mesh_0, mesh_1, exactsolution): |
| 35 |
# Build multimesh |
| 36 |
multimesh = MultiMesh() |
| 37 |
multimesh.add(mesh_0) |
| 38 |
multimesh.add(mesh_1) |
| 39 |
multimesh.build() |
| 40 |
|
| 41 |
# FEM setup |
| 42 |
V = MultiMeshFunctionSpace(multimesh, "Lagrange", 1) |
| 43 |
u = TrialFunction(V) |
| 44 |
v = TestFunction(V) |
| 45 |
n = FacetNormal(multimesh) |
| 46 |
h = 2.0*Circumradius(multimesh) |
| 47 |
h = (h('+') + h('-')) / 2 |
| 48 |
|
| 49 |
# Set data |
| 50 |
f = Constant(0.0) |
| 51 |
|
| 52 |
# Set parameters |
| 53 |
alpha = 4.0 |
| 54 |
beta = 4.0 |
| 55 |
|
| 56 |
# Define bilinear form |
| 57 |
a = dot(grad(u), grad(v))*dX \ |
| 58 |
- dot(avg(grad(u)), jump(v, n))*dI \ |
| 59 |
- dot(avg(grad(v)), jump(u, n))*dI \ |
| 60 |
+ alpha/h*jump(u)*jump(v)*dI \ |
| 61 |
+ beta*dot(jump(grad(u)), jump(grad(v)))*dO |
| 62 |
|
| 63 |
# Define linear form |
| 64 |
L = f*v*dX |
| 65 |
|
| 66 |
# Assemble linear system |
| 67 |
A = assemble_multimesh(a) |
| 68 |
b = assemble_multimesh(L) |
| 69 |
|
| 70 |
# Apply boundary condition |
| 71 |
bc = MultiMeshDirichletBC(V, exactsolution, DomainBoundary()) |
| 72 |
bc.apply(A, b) |
| 73 |
|
| 74 |
# Solve |
| 75 |
uh = MultiMeshFunction(V) |
| 76 |
solve(A, uh.vector(), b) |
| 77 |
|
| 78 |
return uh |
| 79 |
|
| 80 |
@fixture |
| 81 |
def exactsolution_2d(): |
| 82 |
return exactsolution_2d_impl() |
| 83 |
|
| 84 |
@fixture |
| 85 |
def exactsolution_3d(): |
| 86 |
return exactsolution_3d_impl() |
| 87 |
|
| 88 |
@pytest.mark.slow |
| 89 |
@skip_in_parallel |
| 90 |
def test_multimesh_poisson_2d(exactsolution_2d): |
| 91 |
# This tests solves a Poisson problem on two meshes in 2D with u = |
| 92 |
# x+y as exact solution |
| 93 |
|
| 94 |
# FIXME: This test is quite slow. |
| 95 |
|
| 96 |
# Define meshes |
| 97 |
mesh_0 = UnitSquareMesh(2, 2) |
| 98 |
mesh_1 = RectangleMesh(Point(0.1*DOLFIN_PI, 0.1*DOLFIN_PI), |
| 99 |
Point(0.2*DOLFIN_PI, 0.2*DOLFIN_PI), |
| 100 |
2, 2) |
| 101 |
# Solve multimesh Poisson |
| 102 |
uh = solve_multimesh_poisson(mesh_0, mesh_1, exactsolution_2d) |
| 103 |
|
| 104 |
# Check error |
| 105 |
assert errornorm(exactsolution_2d, uh, 'L2', degree_rise=1) < DOLFIN_EPS_LARGE |
| 106 |
|
| 107 |
@pytest.mark.slow |
| 108 |
@pytest.mark.skip |
| 109 |
@skip_in_parallel |
| 110 |
@pytest.mark.skipif(True, reason="3D not fully implemented") |
| 111 |
def test_multimesh_poisson_3d(exactsolution_3d): |
| 112 |
# This tests solves a Poisson problem on two meshes in 3D with u = |
| 113 |
# x+y+z as exact solution |
| 114 |
|
| 115 |
# FIXME: This test is quite slow. |
| 116 |
|
| 117 |
# Define meshes |
| 118 |
mesh_0 = UnitCubeMesh(2, 2, 2) |
| 119 |
mesh_1 = BoxMesh(Point(0.1*DOLFIN_PI, 0.1*DOLFIN_PI, 0.1*DOLFIN_PI), |
| 120 |
Point(0.2*DOLFIN_PI, 0.2*DOLFIN_PI, 0.2*DOLFIN_PI), |
| 121 |
2, 2, 2) |
| 122 |
|
| 123 |
# Solve multimesh Poisson |
| 124 |
uh = solve_multimesh_poisson(mesh_0, mesh_1, exactsolution_3d) |
| 125 |
|
| 126 |
# Check error |
| 127 |
assert errornorm(exactsolution_3d, uh, 'L2', degree_rise=1) < DOLFIN_EPS_LARGE |