| 1 |
"""Unit tests for SubDomain""" |
| 2 |
|
| 3 |
# Copyright (C) 2013 Johan Hake |
| 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 |
|
| 21 |
import numpy as np |
| 22 |
from dolfin import * |
| 23 |
from dolfin_utils.test import skip_in_parallel |
| 24 |
import pytest |
| 25 |
|
| 26 |
|
| 27 |
def test_compiled_subdomains(): |
| 28 |
def noDefaultValues(): |
| 29 |
CompiledSubDomain("a") |
| 30 |
|
| 31 |
def wrongDefaultType(): |
| 32 |
CompiledSubDomain("a", a="1") |
| 33 |
|
| 34 |
def wrongParameterNames(): |
| 35 |
CompiledSubDomain("foo", bar=1.0) |
| 36 |
|
| 37 |
with pytest.raises(RuntimeError): |
| 38 |
noDefaultValues() |
| 39 |
with pytest.raises(TypeError): |
| 40 |
wrongDefaultType() |
| 41 |
with pytest.raises(RuntimeError): |
| 42 |
wrongParameterNames() |
| 43 |
|
| 44 |
|
| 45 |
@skip_in_parallel |
| 46 |
def test_compiled_subdomains_compilation_failure(): |
| 47 |
def invalidCppCode(): |
| 48 |
CompiledSubDomain("/") |
| 49 |
with pytest.raises(RuntimeError): |
| 50 |
invalidCppCode() |
| 51 |
|
| 52 |
def test_creation_and_marking(): |
| 53 |
|
| 54 |
class Left(SubDomain): |
| 55 |
def inside(self, x, on_boundary): |
| 56 |
return x[0] < DOLFIN_EPS |
| 57 |
|
| 58 |
class LeftOnBoundary(SubDomain): |
| 59 |
def inside(self, x, on_boundary): |
| 60 |
return x[0] < DOLFIN_EPS and on_boundary |
| 61 |
|
| 62 |
class Right(SubDomain): |
| 63 |
def inside(self, x, on_boundary): |
| 64 |
return x[0] > 1.0 - DOLFIN_EPS |
| 65 |
|
| 66 |
class RightOnBoundary(SubDomain): |
| 67 |
def inside(self, x, on_boundary): |
| 68 |
return x[0] > 1.0 - DOLFIN_EPS and on_boundary |
| 69 |
|
| 70 |
cpp_code = """ |
| 71 |
#include<pybind11/pybind11.h> |
| 72 |
#include<pybind11/eigen.h> |
| 73 |
namespace py = pybind11; |
| 74 |
|
| 75 |
#include<Eigen/Dense> |
| 76 |
#include<dolfin/mesh/SubDomain.h> |
| 77 |
|
| 78 |
class Left : public dolfin::SubDomain |
| 79 |
{ |
| 80 |
public: |
| 81 |
|
| 82 |
virtual bool inside(Eigen::Ref<const Eigen::VectorXd> x, bool on_boundary) const |
| 83 |
{ |
| 84 |
return x[0] < DOLFIN_EPS; |
| 85 |
} |
| 86 |
}; |
| 87 |
|
| 88 |
class LeftOnBoundary : public dolfin::SubDomain |
| 89 |
{ |
| 90 |
public: |
| 91 |
|
| 92 |
virtual bool inside(Eigen::Ref<const Eigen::VectorXd> x, bool on_boundary) const |
| 93 |
{ |
| 94 |
return x[0] < DOLFIN_EPS and on_boundary; |
| 95 |
} |
| 96 |
}; |
| 97 |
|
| 98 |
class Right : public dolfin::SubDomain |
| 99 |
{ |
| 100 |
public: |
| 101 |
|
| 102 |
virtual bool inside(Eigen::Ref<const Eigen::VectorXd> x, bool on_boundary) const |
| 103 |
{ |
| 104 |
return x[0] > 1.0 - DOLFIN_EPS; |
| 105 |
} |
| 106 |
}; |
| 107 |
|
| 108 |
class RightOnBoundary : public dolfin::SubDomain |
| 109 |
{ |
| 110 |
public: |
| 111 |
|
| 112 |
virtual bool inside(Eigen::Ref<const Eigen::VectorXd> x, bool on_boundary) const |
| 113 |
{ |
| 114 |
return x[0] > 1.0 - DOLFIN_EPS and on_boundary; |
| 115 |
} |
| 116 |
}; |
| 117 |
|
| 118 |
PYBIND11_MODULE(SIGNATURE, m) { |
| 119 |
//py::object SubDomain = (py::object) py::module::import("dolfin").attr("SubDomain"); |
| 120 |
py::class_<dolfin::SubDomain>(m, "SubDomain", py::module_local()); |
| 121 |
py::class_<Left, std::unique_ptr<Left>, dolfin::SubDomain>(m, "Left").def(py::init<>()); |
| 122 |
py::class_<Right, std::unique_ptr<Right>, dolfin::SubDomain>(m, "Right").def(py::init<>()); |
| 123 |
py::class_<LeftOnBoundary, std::unique_ptr<LeftOnBoundary>, dolfin::SubDomain>(m, "LeftOnBoundary").def(py::init<>()); |
| 124 |
py::class_<RightOnBoundary, std::unique_ptr<RightOnBoundary>, dolfin::SubDomain>(m, "RightOnBoundary").def(py::init<>()); |
| 125 |
} |
| 126 |
""" |
| 127 |
|
| 128 |
compiled_domain_module = compile_cpp_code(cpp_code) |
| 129 |
|
| 130 |
subdomain_pairs = [(Left(), Right()), |
| 131 |
(LeftOnBoundary(), RightOnBoundary()), |
| 132 |
(AutoSubDomain(lambda x, on_boundary: x[0] < DOLFIN_EPS), |
| 133 |
AutoSubDomain(lambda x, on_boundary: x[0] > 1.0 - DOLFIN_EPS)), |
| 134 |
(AutoSubDomain(lambda x, on_boundary: x[0] < DOLFIN_EPS and on_boundary), |
| 135 |
AutoSubDomain(lambda x, on_boundary: x[0] > 1.0 - DOLFIN_EPS and on_boundary)), |
| 136 |
(CompiledSubDomain("near(x[0], _a)", _a=0.0), |
| 137 |
CompiledSubDomain("near(x[0], _a)", _a=1.0)), |
| 138 |
(CompiledSubDomain("near(x[0], _a) and on_boundary", _a=0.0), |
| 139 |
CompiledSubDomain("near(x[0], _a) and on_boundary", _a=1.0)), |
| 140 |
(CompiledSubDomain("near(x[0], 0.0)"), |
| 141 |
CompiledSubDomain("near(x[0], 1.0)")), |
| 142 |
(CompiledSubDomain("near(x[0], 0.0) and on_boundary"), |
| 143 |
CompiledSubDomain("near(x[0], 1.0) and on_boundary")), |
| 144 |
# |
| 145 |
# (compiled_domain_module.Left(), |
| 146 |
# compiled_domain_module.Right()), |
| 147 |
# (compiled_domain_module.LeftOnBoundary(), |
| 148 |
# compiled_domain_module.RightOnBoundary()) |
| 149 |
] |
| 150 |
|
| 151 |
empty = CompiledSubDomain("false") |
| 152 |
every = CompiledSubDomain("true") |
| 153 |
|
| 154 |
for ind, MeshClass in enumerate([UnitIntervalMesh, UnitSquareMesh, |
| 155 |
UnitCubeMesh]): |
| 156 |
dim = ind + 1 |
| 157 |
args = [10]*dim |
| 158 |
mesh = MeshClass(*args) |
| 159 |
|
| 160 |
mesh.init() |
| 161 |
|
| 162 |
for left, right in subdomain_pairs: |
| 163 |
for t_dim, f_dim in [(0, 0), |
| 164 |
(mesh.topology().dim()-1, dim - 1), |
| 165 |
(mesh.topology().dim(), dim)]: |
| 166 |
f = MeshFunction("size_t", mesh, t_dim, 0) |
| 167 |
|
| 168 |
left.mark(f, int(1)) |
| 169 |
right.mark(f, 2) |
| 170 |
|
| 171 |
correct = {(1, 0): 1, |
| 172 |
(1, 1): 0, |
| 173 |
(2, 0): 11, |
| 174 |
(2, 1): 10, |
| 175 |
(2, 2): 0, |
| 176 |
(3, 0): 121, |
| 177 |
(3, 2): 200, |
| 178 |
(3, 3): 0} |
| 179 |
|
| 180 |
# Check that the number of marked entities are at least the |
| 181 |
# correct number (it can be larger in parallel) |
| 182 |
assert all(value >= correct[dim, f_dim] |
| 183 |
for value in [ |
| 184 |
MPI.sum(mesh.mpi_comm(), float((f.array() == 2).sum())), |
| 185 |
MPI.sum(mesh.mpi_comm(), float((f.array() == 1).sum())), |
| 186 |
]) |
| 187 |
|
| 188 |
for t_dim, f_dim in [(0, 0), |
| 189 |
(mesh.topology().dim()-1, dim-1), |
| 190 |
(mesh.topology().dim(), dim)]: |
| 191 |
f = MeshFunction("size_t", mesh, t_dim, 0)# |
| 192 |
|
| 193 |
empty.mark(f, 1) |
| 194 |
every.mark(f, 2) |
| 195 |
|
| 196 |
# Check that the number of marked entities is correct |
| 197 |
assert sum(f.array() == 1) == 0 |
| 198 |
assert sum(f.array() == 2) == mesh.num_entities(f_dim) |
| 199 |
|
| 200 |
# Skipping in parallel due to lack of parallel support for SubMesh |
| 201 |
@skip_in_parallel |
| 202 |
def test_snap_boundary(): |
| 203 |
|
| 204 |
# This test checks whether the expected modifications are made |
| 205 |
# to mesh vertex coordinates when snap_boundary is called. |
| 206 |
|
| 207 |
for ind, MeshClass in enumerate([UnitIntervalMesh, UnitSquareMesh, |
| 208 |
UnitCubeMesh]): |
| 209 |
# SubDomain whose snap method modifies all coordinate components |
| 210 |
# to be zero. |
| 211 |
dim = ind+1 |
| 212 |
class ZeroSnap(SubDomain): |
| 213 |
def inside(self,x,on_boundary): |
| 214 |
return True |
| 215 |
def snap(self,x): |
| 216 |
for i in range(0,dim): |
| 217 |
x[i] = 0.0 |
| 218 |
|
| 219 |
# Create mesh and modify coordinates via snapping. Note that all vertices |
| 220 |
# are on the boundary. |
| 221 |
args = [1,]*dim |
| 222 |
mesh = MeshClass(*args) |
| 223 |
mesh.snap_boundary(ZeroSnap()) |
| 224 |
|
| 225 |
# Verify that all mesh coordinates are now zero. |
| 226 |
for x in mesh.coordinates(): |
| 227 |
for x_i in x: |
| 228 |
assert x_i==0.0 |
| 229 |
|