| 1 |
# -*- coding: utf-8 -*- |
| 2 |
"""Unit tests for the MultiMeshFunction class""" |
| 3 |
|
| 4 |
# Copyright (C) 2016 Jørgen S. Dokken |
| 5 |
# |
| 6 |
# This file is part of DOLFIN. |
| 7 |
# |
| 8 |
# DOLFIN is free software: you can redistribute it and/or modify |
| 9 |
# it under the terms of the GNU Lesser General Public License as published by |
| 10 |
# the Free Software Foundation, either version 3 of the License, or |
| 11 |
# (at your option) any later version. |
| 12 |
# |
| 13 |
# DOLFIN is distributed in the hope that it will be useful, |
| 14 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
# GNU Lesser General Public License for more details. |
| 17 |
# |
| 18 |
# You should have received a copy of the GNU Lesser General Public License |
| 19 |
# along with DOLFIN. If not, see <http://www.gnu.org/licenses/>. |
| 20 |
# |
| 21 |
# |
| 22 |
# First added: 2016-06-11 |
| 23 |
# Last changed: 2017-07-18 |
| 24 |
|
| 25 |
import pytest |
| 26 |
from dolfin import * |
| 27 |
import ufl_legacy as ufl |
| 28 |
import numpy |
| 29 |
|
| 30 |
from dolfin_utils.test import fixture, skip_in_parallel |
| 31 |
|
| 32 |
@fixture |
| 33 |
def multimesh(): |
| 34 |
mesh_0 = RectangleMesh(Point(0,0), Point(1, 1), 20, 20) |
| 35 |
mesh_1 = RectangleMesh(Point(numpy.pi/10,numpy.pi/9), |
| 36 |
Point(numpy.pi/8,numpy.pi/7),5,8) |
| 37 |
multimesh = MultiMesh() |
| 38 |
multimesh.add(mesh_0) |
| 39 |
multimesh.add(mesh_1) |
| 40 |
multimesh.build() |
| 41 |
return multimesh |
| 42 |
|
| 43 |
@fixture |
| 44 |
def V(multimesh): |
| 45 |
element = FiniteElement("Lagrange", triangle, 1) |
| 46 |
return MultiMeshFunctionSpace(multimesh, element) |
| 47 |
|
| 48 |
@fixture |
| 49 |
def V_high(multimesh): |
| 50 |
element = FiniteElement("Lagrange", triangle, 3) |
| 51 |
return MultiMeshFunctionSpace(multimesh, element) |
| 52 |
|
| 53 |
@fixture |
| 54 |
def V_vector(multimesh): |
| 55 |
return MultiMeshVectorFunctionSpace(multimesh,"CG", 1) |
| 56 |
|
| 57 |
@fixture |
| 58 |
def e_x(): |
| 59 |
return Expression(("1.0","0.0"), degree=1) |
| 60 |
|
| 61 |
@fixture |
| 62 |
def f_vec2(): |
| 63 |
return Expression(("sin(x[1])","1.0"), degree=3) |
| 64 |
|
| 65 |
@fixture |
| 66 |
def v_vec(V_vector): |
| 67 |
return MultiMeshFunction(V_vector) |
| 68 |
|
| 69 |
@fixture |
| 70 |
def f_vec(): |
| 71 |
return Expression(("x[0]","x[1]"),degree=2) |
| 72 |
|
| 73 |
|
| 74 |
@fixture |
| 75 |
def f(): |
| 76 |
return Expression("sin(pi*x[0])*cos(2*pi*x[1])", degree=4) |
| 77 |
|
| 78 |
@fixture |
| 79 |
def f_2(): |
| 80 |
return Expression("x[0]*x[1]",degree=2) |
| 81 |
@fixture |
| 82 |
def v(V): |
| 83 |
return MultiMeshFunction(V) |
| 84 |
|
| 85 |
@fixture |
| 86 |
def v_high(V_high): |
| 87 |
return MultiMeshFunction(V_high) |
| 88 |
|
| 89 |
@skip_in_parallel |
| 90 |
def test_measure_mul(v, multimesh): |
| 91 |
assert isinstance(v*dX, ufl.form.Form) |
| 92 |
|
| 93 |
@skip_in_parallel |
| 94 |
def test_assemble_zero(v, multimesh): |
| 95 |
assert numpy.isclose(assemble_multimesh(v*dX), 0) |
| 96 |
|
| 97 |
@skip_in_parallel |
| 98 |
def test_assemble_area(v, multimesh): |
| 99 |
v.vector()[:] = 1 |
| 100 |
assert numpy.isclose(assemble_multimesh(v*dX), 1) |
| 101 |
|
| 102 |
@skip_in_parallel |
| 103 |
def test_assemble_exterior_facet(v, multimesh): |
| 104 |
v.vector()[:] = 1 |
| 105 |
assert numpy.isclose(assemble_multimesh(v*ds), 4) |
| 106 |
|
| 107 |
@skip_in_parallel |
| 108 |
def test_interpolate(v_high, f): |
| 109 |
v_high.interpolate(f) |
| 110 |
assert numpy.isclose(assemble_multimesh(v_high*dX), 0) |
| 111 |
|
| 112 |
@skip_in_parallel |
| 113 |
def test_project(f, V_high): |
| 114 |
v = project(f ,V_high) |
| 115 |
assert numpy.isclose(assemble_multimesh(v*dX), 0) |
| 116 |
|
| 117 |
@skip_in_parallel |
| 118 |
def test_errornorm_L2(f_2, v_high): |
| 119 |
const = Expression("1", degree=1) |
| 120 |
v_high.interpolate(f_2) |
| 121 |
assert numpy.isclose(errornorm(const, v_high, norm_type="L2", degree_rise=3), numpy.sqrt(22)/6) |
| 122 |
|
| 123 |
@skip_in_parallel |
| 124 |
def test_errornorm_H1(f, f_2, v_high): |
| 125 |
v_high.interpolate(f_2) |
| 126 |
assert numpy.isclose(errornorm(f, v_high, norm_type="H1", degree_rise=3), numpy.sqrt(37./36+5*numpy.pi**2/4)) |
| 127 |
|
| 128 |
@skip_in_parallel |
| 129 |
def test_vector_space(v_vec,e_x,f_vec2,V_vector,f_vec): |
| 130 |
v_vec.interpolate(e_x) |
| 131 |
assert numpy.isclose(assemble_multimesh(inner(f_vec,v_vec)*dX), 0.5) |
| 132 |
v_vec.interpolate(f_vec) |
| 133 |
print((assemble_multimesh(inner(v_vec, f_vec2)*dX), |
| 134 |
1/2 + numpy.sin(1/2)*numpy.sin(1/2))) |
| 135 |
assert numpy.isclose(assemble_multimesh(inner(v_vec, f_vec2)*dX), |
| 136 |
1/2 + numpy.sin(1/2)*numpy.sin(1/2)) |