| 1 |
"Unit tests for multimesh coefficients" |
| 2 |
|
| 3 |
# Copyright (C) 2006 Magne Nordaas |
| 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 |
# Modified by August Johansson 2017 |
| 21 |
# |
| 22 |
# First added: 2017-04-02 |
| 23 |
# Last changed: 2017-06-20 |
| 24 |
|
| 25 |
import pytest |
| 26 |
from dolfin import * |
| 27 |
from dolfin_utils.test import fixture, skip_in_parallel |
| 28 |
|
| 29 |
@fixture |
| 30 |
def mesh0(): |
| 31 |
return UnitSquareMesh(2,2) |
| 32 |
|
| 33 |
@fixture |
| 34 |
def mesh1(): |
| 35 |
return RectangleMesh(Point(0.25, 0.25), Point(0.75, 0.75), 2, 2) |
| 36 |
|
| 37 |
@fixture |
| 38 |
def multimesh(mesh0, mesh1): |
| 39 |
multimesh = MultiMesh() |
| 40 |
multimesh.add(mesh0) |
| 41 |
multimesh.add(mesh1) |
| 42 |
multimesh.build() |
| 43 |
return multimesh |
| 44 |
|
| 45 |
@fixture |
| 46 |
def V(multimesh): |
| 47 |
return MultiMeshFunctionSpace(multimesh, "P", 1) |
| 48 |
|
| 49 |
@fixture |
| 50 |
def V0(mesh0): |
| 51 |
return FunctionSpace(mesh0, "P", 1) |
| 52 |
|
| 53 |
@fixture |
| 54 |
def V1(mesh1): |
| 55 |
return FunctionSpace(mesh1, "P", 1) |
| 56 |
|
| 57 |
@fixture |
| 58 |
def f(V, V0, V1): |
| 59 |
f = MultiMeshFunction(V) |
| 60 |
f.assign_part(0, interpolate(Constant(1.0), V0)) |
| 61 |
f.assign_part(1, interpolate(Constant(2.0), V1)) |
| 62 |
return f |
| 63 |
|
| 64 |
@fixture |
| 65 |
def g(): |
| 66 |
return Constant(0.5) |
| 67 |
|
| 68 |
@fixture |
| 69 |
def h(V, V0, V1): |
| 70 |
h = MultiMeshFunction(V) |
| 71 |
h.assign_part(0, interpolate(Constant(1.0), V0)) |
| 72 |
h.assign_part(1, interpolate(Constant(1.5), V1)) |
| 73 |
return h |
| 74 |
|
| 75 |
@skip_in_parallel |
| 76 |
def test_dX_integral(f, g, h): |
| 77 |
f_dX = assemble_multimesh(f * dX) |
| 78 |
assert abs(f_dX - 1.25) < DOLFIN_EPS_LARGE |
| 79 |
|
| 80 |
fgh_dX = assemble_multimesh(f*g*h * dX) |
| 81 |
assert abs(fgh_dX - 0.75) < DOLFIN_EPS_LARGE |
| 82 |
|
| 83 |
@skip_in_parallel |
| 84 |
def test_dI_integral(f, g, h): |
| 85 |
f_dI0 = assemble_multimesh(f("-") * dI) |
| 86 |
assert abs(f_dI0 - 4.0) < DOLFIN_EPS_LARGE |
| 87 |
|
| 88 |
f_dI1 = assemble_multimesh(f("+") * dI) |
| 89 |
assert abs(f_dI1 - 2.0) < DOLFIN_EPS_LARGE |
| 90 |
|
| 91 |
fgh_dI0 = assemble_multimesh(f("-")*g("-")*h("-") * dI) |
| 92 |
assert abs(fgh_dI0 - 3.0) < DOLFIN_EPS_LARGE |
| 93 |
|
| 94 |
fgh_dI1 = assemble_multimesh(f("+")*g("+")*h("+") * dI) |
| 95 |
assert abs(fgh_dI1 - 1.0) < DOLFIN_EPS_LARGE |
| 96 |
|
| 97 |
@skip_in_parallel |
| 98 |
def test_dO_integral(f, g, h): |
| 99 |
f_dO0 = assemble_multimesh(f("-") * dO) |
| 100 |
assert abs(f_dO0 - 0.50) < DOLFIN_EPS_LARGE |
| 101 |
|
| 102 |
f_dO1 = assemble_multimesh(f("+") * dO) |
| 103 |
assert abs(f_dO1 - 0.25) < DOLFIN_EPS_LARGE |
| 104 |
|
| 105 |
fgh_dO0 = assemble_multimesh(f("-")*g("-")*h("-") * dO) |
| 106 |
assert abs(fgh_dO0 - 0.375) < DOLFIN_EPS_LARGE |
| 107 |
|
| 108 |
fgh_dO1 = assemble_multimesh(f("+")*g("+")*h("+") * dO) |
| 109 |
assert abs(fgh_dO1 - 0.125) < DOLFIN_EPS_LARGE |