| 1 |
"""Unit tests for PointSources""" |
| 2 |
|
| 3 |
# Copyright (C) 2016 Ettie Unwin |
| 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 |
import pytest |
| 21 |
import numpy as np |
| 22 |
from dolfin import (UnitIntervalMesh, UnitSquareMesh, UnitCubeMesh, |
| 23 |
Point, MPI, FunctionSpace, TestFunction, |
| 24 |
TrialFunction, assemble, Constant, PointSource, |
| 25 |
dx, Cell, VectorFunctionSpace, near, |
| 26 |
vertex_to_dof_map, vertices, dot, FiniteElement, |
| 27 |
Function, VectorElement, MixedElement) |
| 28 |
|
| 29 |
meshes = [UnitIntervalMesh(10), UnitSquareMesh(10, 10), UnitCubeMesh(4, 4, 4)] |
| 30 |
data = [(UnitIntervalMesh(10), Point(0.5)), |
| 31 |
(UnitSquareMesh(10, 10), Point(0.5, 0.5)), |
| 32 |
(UnitCubeMesh(3, 3, 3), Point(0.5, 0.5, 0.5))] |
| 33 |
|
| 34 |
|
| 35 |
@pytest.mark.parametrize("mesh, point", data) |
| 36 |
def test_pointsource_vector_node(mesh, point): |
| 37 |
"""Tests point source when given constructor PointSource(V, point, |
| 38 |
mag) with a vector and when placed at a node for 1D, 2D and |
| 39 |
3D. Global points given to constructor from rank 0 processor. |
| 40 |
|
| 41 |
""" |
| 42 |
|
| 43 |
rank = MPI.rank(mesh.mpi_comm()) |
| 44 |
V = FunctionSpace(mesh, "CG", 1) |
| 45 |
v = TestFunction(V) |
| 46 |
b = assemble(Constant(0.0)*v*dx) |
| 47 |
if rank == 0: |
| 48 |
ps = PointSource(V, point, 10.0) |
| 49 |
else: |
| 50 |
ps = PointSource(V, []) |
| 51 |
ps.apply(b) |
| 52 |
|
| 53 |
# Checks array sums to correct value |
| 54 |
b_sum = b.sum() |
| 55 |
assert round(b_sum - 10.0) == 0 |
| 56 |
|
| 57 |
# Checks point source is added to correct part of the array |
| 58 |
v2d = vertex_to_dof_map(V) |
| 59 |
for v in vertices(mesh): |
| 60 |
if near(v.midpoint().distance(point), 0.0): |
| 61 |
ind = v2d[v.index()] |
| 62 |
if ind < len(b.get_local()): |
| 63 |
assert np.round(b.get_local()[ind]-10.0) == 0 |
| 64 |
|
| 65 |
|
| 66 |
@pytest.mark.parametrize("mesh", meshes) |
| 67 |
def test_pointsource_vector(mesh): |
| 68 |
"""Tests point source when given constructor PointSource(V, point, |
| 69 |
mag) with a vector that isn't placed at a node for 1D, 2D and |
| 70 |
3D. Global points given to constructor from rank 0 processor |
| 71 |
|
| 72 |
""" |
| 73 |
|
| 74 |
cell = Cell(mesh, 0) |
| 75 |
point = cell.midpoint() |
| 76 |
rank = MPI.rank(mesh.mpi_comm()) |
| 77 |
|
| 78 |
V = FunctionSpace(mesh, "CG", 1) |
| 79 |
v = TestFunction(V) |
| 80 |
b = assemble(Constant(0.0)*v*dx) |
| 81 |
if rank == 0: |
| 82 |
ps = PointSource(V, point, 10.0) |
| 83 |
else: |
| 84 |
ps = PointSource(V, []) |
| 85 |
ps.apply(b) |
| 86 |
|
| 87 |
# Checks array sums to correct value |
| 88 |
b_sum = b.sum() |
| 89 |
assert round(b_sum - 10.0) == 0 |
| 90 |
|
| 91 |
|
| 92 |
@pytest.mark.parametrize("mesh, point", data) |
| 93 |
def test_pointsource_vector_fs(mesh, point): |
| 94 |
"""Tests point source when given constructor PointSource(V, point, |
| 95 |
mag) with a vector for a vector function space that isn't placed |
| 96 |
at a node for 1D, 2D and 3D. Global points given to constructor |
| 97 |
from rank 0 processor. |
| 98 |
|
| 99 |
""" |
| 100 |
|
| 101 |
rank = MPI.rank(mesh.mpi_comm()) |
| 102 |
V = VectorFunctionSpace(mesh, "CG", 1) |
| 103 |
v = TestFunction(V) |
| 104 |
b = assemble(dot(Constant([0.0]*mesh.geometry().dim()), v)*dx) |
| 105 |
if rank == 0: |
| 106 |
ps = PointSource(V, point, 10.0) |
| 107 |
else: |
| 108 |
ps = PointSource(V, []) |
| 109 |
ps.apply(b) |
| 110 |
|
| 111 |
# Checks array sums to correct value |
| 112 |
b_sum = b.sum() |
| 113 |
assert round(b_sum - 10.0*V.num_sub_spaces()) == 0 |
| 114 |
|
| 115 |
# Checks point source is added to correct part of the array |
| 116 |
v2d = vertex_to_dof_map(V) |
| 117 |
for v in vertices(mesh): |
| 118 |
if near(v.midpoint().distance(point), 0.0): |
| 119 |
for spc_idx in range(V.num_sub_spaces()): |
| 120 |
ind = v2d[v.index()*V.num_sub_spaces() + spc_idx] |
| 121 |
if ind < len(b.get_local()): |
| 122 |
assert np.round(b.get_local()[ind] - 10.0) == 0 |
| 123 |
|
| 124 |
|
| 125 |
@pytest.mark.parametrize("mesh, point", data) |
| 126 |
def test_pointsource_mixed_space(mesh, point): |
| 127 |
"""Tests point source when given constructor PointSource(V, point, |
| 128 |
mag) with a vector for a mixed function space that isn't placed at |
| 129 |
a node for 1D, 2D and 3D. Global points given to constructor from |
| 130 |
rank 0 processor. |
| 131 |
|
| 132 |
""" |
| 133 |
|
| 134 |
rank = MPI.rank(mesh.mpi_comm()) |
| 135 |
ele1 = FiniteElement("CG", mesh.ufl_cell(), 1) |
| 136 |
ele2 = FiniteElement("DG", mesh.ufl_cell(), 2) |
| 137 |
ele3 = VectorElement("CG", mesh.ufl_cell(), 2) |
| 138 |
V = FunctionSpace(mesh, MixedElement([ele1, ele2, ele3])) |
| 139 |
value_dimension = V.element().value_dimension(0) |
| 140 |
v = TestFunction(V) |
| 141 |
b = assemble(dot(Constant([0.0]*value_dimension), v)*dx) |
| 142 |
if rank == 0: |
| 143 |
ps = PointSource(V, point, 10.0) |
| 144 |
else: |
| 145 |
ps = PointSource(V, []) |
| 146 |
ps.apply(b) |
| 147 |
|
| 148 |
# Checks array sums to correct value |
| 149 |
b_sum = b.sum() |
| 150 |
assert round(b_sum - 10.0*value_dimension) == 0 |
| 151 |
|
| 152 |
|
| 153 |
def test_point_outside(): |
| 154 |
"""Tests point source fails if given a point outside the domain.""" |
| 155 |
mesh = UnitIntervalMesh(10) |
| 156 |
point = Point(1.2) |
| 157 |
V = FunctionSpace(mesh, "CG", 1) |
| 158 |
v = TestFunction(V) |
| 159 |
assemble(Constant(0.0)*v*dx) |
| 160 |
# Runtime Error is only produced on one process which causes the |
| 161 |
# whole function to fail but makes this test hang in parallel. |
| 162 |
with pytest.raises(RuntimeError): |
| 163 |
PointSource(V, point, 10.0) |
| 164 |
|
| 165 |
|
| 166 |
@pytest.mark.parametrize("mesh, point", data) |
| 167 |
def test_pointsource_matrix(mesh, point): |
| 168 |
"""Tests point source when given constructor PointSource(V, point, |
| 169 |
mag) with a matrix and when placed at a node for 1D, 2D and |
| 170 |
3D. Global points given to constructor from rank 0 processor. |
| 171 |
|
| 172 |
""" |
| 173 |
|
| 174 |
rank = MPI.rank(mesh.mpi_comm()) |
| 175 |
V = FunctionSpace(mesh, "CG", 1) |
| 176 |
u, v = TrialFunction(V), TestFunction(V) |
| 177 |
w = Function(V) |
| 178 |
A = assemble(Constant(0.0)*u*v*dx) |
| 179 |
if rank == 0: |
| 180 |
ps = PointSource(V, point, 10.0) |
| 181 |
else: |
| 182 |
ps = PointSource(V, []) |
| 183 |
ps.apply(A) |
| 184 |
|
| 185 |
# Checks array sums to correct value |
| 186 |
a_sum = MPI.sum(mesh.mpi_comm(), np.sum(A.array())) |
| 187 |
assert round(a_sum - 10.0) == 0 |
| 188 |
|
| 189 |
# Checks point source is added to correct part of the array |
| 190 |
A.get_diagonal(w.vector()) |
| 191 |
v2d = vertex_to_dof_map(V) |
| 192 |
for v in vertices(mesh): |
| 193 |
if near(v.midpoint().distance(point), 0.0): |
| 194 |
ind = v2d[v.index()] |
| 195 |
if ind < len(A.array()): |
| 196 |
assert np.round(w.vector()[ind] - 10.0) == 0 |
| 197 |
|
| 198 |
|
| 199 |
# FIXME: Edit this test to have V1 != V2 when that is implemented |
| 200 |
@pytest.mark.parametrize("mesh, point", data) |
| 201 |
def test_pointsource_matrix_second_constructor(mesh, point): |
| 202 |
"""Tests point source when given different constructor PointSource(V1, |
| 203 |
V2, point, mag) with a matrix and when placed at a node for 1D, 2D |
| 204 |
and 3D. Global points given to constructor from rank 0 |
| 205 |
processor. Currently only implemented if V1=V2. |
| 206 |
|
| 207 |
""" |
| 208 |
|
| 209 |
V1 = FunctionSpace(mesh, "CG", 1) |
| 210 |
V2 = FunctionSpace(mesh, "CG", 1) |
| 211 |
|
| 212 |
rank = MPI.rank(mesh.mpi_comm()) |
| 213 |
u, v = TrialFunction(V1), TestFunction(V2) |
| 214 |
w = Function(V1) |
| 215 |
A = assemble(Constant(0.0)*u*v*dx) |
| 216 |
if rank == 0: |
| 217 |
ps = PointSource(V1, V2, point, 10.0) |
| 218 |
else: |
| 219 |
ps = PointSource(V1, V2, []) |
| 220 |
ps.apply(A) |
| 221 |
|
| 222 |
# Checks array sums to correct value |
| 223 |
a_sum = MPI.sum(mesh.mpi_comm(), np.sum(A.array())) |
| 224 |
assert round(a_sum - 10.0) == 0 |
| 225 |
|
| 226 |
# Checks point source is added to correct part of the array |
| 227 |
A.get_diagonal(w.vector()) |
| 228 |
v2d = vertex_to_dof_map(V1) |
| 229 |
for v in vertices(mesh): |
| 230 |
if near(v.midpoint().distance(point), 0.0): |
| 231 |
ind = v2d[v.index()] |
| 232 |
if ind < len(A.array()): |
| 233 |
assert np.round(w.vector()[ind] - 10.0) == 0 |
| 234 |
|
| 235 |
|
| 236 |
@pytest.mark.parametrize("mesh", meshes) |
| 237 |
def test_multi_ps_vector_node(mesh): |
| 238 |
"""Tests point source when given constructor PointSource(V, V, point, |
| 239 |
mag) with a matrix when points placed at 3 node for 1D, 2D and |
| 240 |
3D. Global points given to constructor from rank 0 processor. |
| 241 |
|
| 242 |
""" |
| 243 |
|
| 244 |
point = [0.0, 0.5, 1.0] |
| 245 |
dim = mesh.geometry().dim() |
| 246 |
rank = MPI.rank(mesh.mpi_comm()) |
| 247 |
V = FunctionSpace(mesh, "CG", 1) |
| 248 |
v = TestFunction(V) |
| 249 |
b = assemble(Constant(0.0)*v*dx) |
| 250 |
|
| 251 |
source = [] |
| 252 |
point_coords = np.zeros(dim) |
| 253 |
for p in point: |
| 254 |
for i in range(dim): |
| 255 |
point_coords[i-1] = p |
| 256 |
if rank == 0: |
| 257 |
source.append((Point(point_coords), 10.0)) |
| 258 |
ps = PointSource(V, source) |
| 259 |
ps.apply(b) |
| 260 |
|
| 261 |
# Checks b sums to correct value |
| 262 |
b_sum = b.sum() |
| 263 |
assert round(b_sum - len(point)*10.0) == 0 |
| 264 |
|
| 265 |
# Checks values added to correct part of vector |
| 266 |
mesh_coords = V.tabulate_dof_coordinates() |
| 267 |
for p in point: |
| 268 |
for i in range(dim): |
| 269 |
point_coords[i] = p |
| 270 |
|
| 271 |
j = 0 |
| 272 |
for i in range(len(mesh_coords)//(dim)): |
| 273 |
mesh_coords_check = mesh_coords[j:j + dim - 1] |
| 274 |
if np.array_equal(point_coords, mesh_coords_check) is True: |
| 275 |
assert np.round(b.array()[j//(dim)]-10.0) == 0.0 |
| 276 |
j += dim |
| 277 |
|
| 278 |
|
| 279 |
@pytest.mark.parametrize("mesh", meshes) |
| 280 |
def test_multi_ps_vector_node_local(mesh): |
| 281 |
"""Tests point source when given constructor PointSource(V, V, point, |
| 282 |
mag) with a matrix when points placed at 3 node for 1D, 2D and |
| 283 |
3D. Local points given to constructor. |
| 284 |
|
| 285 |
""" |
| 286 |
|
| 287 |
V = FunctionSpace(mesh, "CG", 1) |
| 288 |
v = TestFunction(V) |
| 289 |
b = assemble(Constant(0.0)*v*dx) |
| 290 |
|
| 291 |
source = [] |
| 292 |
point_coords = mesh.coordinates()[0] |
| 293 |
source.append((Point(point_coords), 10.0)) |
| 294 |
ps = PointSource(V, source) |
| 295 |
ps.apply(b) |
| 296 |
|
| 297 |
# Checks b sums to correct value |
| 298 |
size = MPI.size(mesh.mpi_comm()) |
| 299 |
b_sum = b.sum() |
| 300 |
assert round(b_sum - size*10.0) == 0 |
| 301 |
|
| 302 |
|
| 303 |
@pytest.mark.parametrize("mesh", meshes) |
| 304 |
def test_multi_ps_vector(mesh): |
| 305 |
"""Tests point source PointSource(V, source) for mulitple point |
| 306 |
sources applied to a vector for 1D, 2D and 3D. Global points given |
| 307 |
to constructor from rank 0 processor. |
| 308 |
|
| 309 |
""" |
| 310 |
|
| 311 |
c_ids = [0, 1, 2] |
| 312 |
rank = MPI.rank(mesh.mpi_comm()) |
| 313 |
V = FunctionSpace(mesh, "CG", 1) |
| 314 |
v = TestFunction(V) |
| 315 |
b = assemble(Constant(0.0)*v*dx) |
| 316 |
|
| 317 |
source = [] |
| 318 |
if rank == 0: |
| 319 |
for c_id in c_ids: |
| 320 |
cell = Cell(mesh, c_id) |
| 321 |
point = cell.midpoint() |
| 322 |
source.append((point, 10.0)) |
| 323 |
ps = PointSource(V, source) |
| 324 |
ps.apply(b) |
| 325 |
|
| 326 |
# Checks b sums to correct value |
| 327 |
b_sum = b.sum() |
| 328 |
assert round(b_sum - len(c_ids)*10.0) == 0 |
| 329 |
|
| 330 |
|
| 331 |
@pytest.mark.parametrize("mesh", meshes) |
| 332 |
def test_multi_ps_matrix_node(mesh): |
| 333 |
"""Tests point source when given constructor PointSource(V, source) |
| 334 |
with a matrix when points placed at 3 nodes for 1D, 2D and |
| 335 |
3D. Global points given to constructor from rank 0 processor. |
| 336 |
|
| 337 |
""" |
| 338 |
|
| 339 |
point = [0.0, 0.5, 1.0] |
| 340 |
rank = MPI.rank(mesh.mpi_comm()) |
| 341 |
V = FunctionSpace(mesh, "CG", 1) |
| 342 |
u, v = TrialFunction(V), TestFunction(V) |
| 343 |
w = Function(V) |
| 344 |
A = assemble(Constant(0.0)*u*v*dx) |
| 345 |
dim = mesh.geometry().dim() |
| 346 |
|
| 347 |
source = [] |
| 348 |
|
| 349 |
point_coords = np.zeros(dim) |
| 350 |
for p in point: |
| 351 |
for i in range(dim): |
| 352 |
point_coords[i-1] = p |
| 353 |
if rank == 0: |
| 354 |
source.append((Point(point_coords), 10.0)) |
| 355 |
ps = PointSource(V, source) |
| 356 |
ps.apply(A) |
| 357 |
|
| 358 |
# Checks matrix sums to correct value. |
| 359 |
A.get_diagonal(w.vector()) |
| 360 |
a_sum = MPI.sum(mesh.mpi_comm(), np.sum(A.array())) |
| 361 |
assert round(a_sum - len(point)*10) == 0 |
| 362 |
|
| 363 |
# Check if coordinates are in portion of mesh and if so check that |
| 364 |
# diagonal components sum to the correct value. |
| 365 |
mesh_coords = V.tabulate_dof_coordinates() |
| 366 |
for p in point: |
| 367 |
for i in range(dim): |
| 368 |
point_coords[i-1] = p |
| 369 |
|
| 370 |
j = 0 |
| 371 |
for i in range(len(mesh_coords)//(dim)): |
| 372 |
mesh_coords_check = mesh_coords[j:j+dim-1] |
| 373 |
if np.array_equal(point_coords, mesh_coords_check) is True: |
| 374 |
assert np.round(w.vector()[j//(dim)]-10.0) == 0.0 |
| 375 |
j += dim |
| 376 |
|
| 377 |
|
| 378 |
@pytest.mark.parametrize("mesh", meshes) |
| 379 |
def test_multi_ps_matrix_node_local(mesh): |
| 380 |
"""Tests point source when given constructor PointSource(V, V, point, |
| 381 |
mag) with a matrix when points placed at 3 node for 1D, 2D and |
| 382 |
3D. Local points given to constructor. |
| 383 |
|
| 384 |
""" |
| 385 |
|
| 386 |
V = FunctionSpace(mesh, "CG", 1) |
| 387 |
u, v = TrialFunction(V), TestFunction(V) |
| 388 |
w = Function(V) |
| 389 |
A = assemble(Constant(0.0)*u*v*dx) |
| 390 |
|
| 391 |
source = [] |
| 392 |
point_coords = mesh.coordinates()[0] |
| 393 |
source.append((Point(point_coords), 10.0)) |
| 394 |
ps = PointSource(V, source) |
| 395 |
ps.apply(A) |
| 396 |
|
| 397 |
# Checks matrix sums to correct value. |
| 398 |
A.get_diagonal(w.vector()) |
| 399 |
size = MPI.size(mesh.mpi_comm()) |
| 400 |
a_sum = MPI.sum(mesh.mpi_comm(), np.sum(A.array())) |
| 401 |
assert round(a_sum - size*10.0) == 0 |
| 402 |
|
| 403 |
|
| 404 |
@pytest.mark.parametrize("mesh", meshes) |
| 405 |
def test_multi_ps_matrix_node_vector_fs(mesh): |
| 406 |
"""Tests point source applied to a matrix with given constructor |
| 407 |
PointSource(V, source) and a vector function space when points |
| 408 |
placed at 3 vertices for 1D, 2D and 3D. Global points given to |
| 409 |
constructor from rank 0 processor. |
| 410 |
|
| 411 |
""" |
| 412 |
|
| 413 |
point = [0.0, 0.5, 1.0] |
| 414 |
rank = MPI.rank(mesh.mpi_comm()) |
| 415 |
V = VectorFunctionSpace(mesh, "CG", 1, dim=2) |
| 416 |
u, v = TrialFunction(V), TestFunction(V) |
| 417 |
w = Function(V) |
| 418 |
A = assemble(Constant(0.0)*dot(u, v)*dx) |
| 419 |
dim = mesh.geometry().dim() |
| 420 |
|
| 421 |
source = [] |
| 422 |
point_coords = np.zeros(dim) |
| 423 |
for p in point: |
| 424 |
for i in range(dim): |
| 425 |
point_coords[i - 1] = p |
| 426 |
if rank == 0: |
| 427 |
source.append((Point(point_coords), 10.0)) |
| 428 |
ps = PointSource(V, source) |
| 429 |
ps.apply(A) |
| 430 |
|
| 431 |
# Checks array sums to correct value |
| 432 |
A.get_diagonal(w.vector()) |
| 433 |
a_sum = MPI.sum(mesh.mpi_comm(), np.sum(A.array())) |
| 434 |
assert round(a_sum - 2*len(point)*10) == 0 |
| 435 |
|
| 436 |
# Check if coordinates are in portion of mesh and if so check that |
| 437 |
# diagonal components sum to the correct value. |
| 438 |
mesh_coords = V.tabulate_dof_coordinates() |
| 439 |
for p in point: |
| 440 |
for i in range(dim): |
| 441 |
point_coords[i] = p |
| 442 |
|
| 443 |
j = 0 |
| 444 |
for i in range(len(mesh_coords)//(dim)): |
| 445 |
mesh_coords_check = mesh_coords[j:j+dim-1] |
| 446 |
if np.array_equal(point_coords, mesh_coords_check) is True: |
| 447 |
assert np.round(w.vector()[j//(dim)] - 10.0) == 0.0 |
| 448 |
j += dim |
| 449 |
|
| 450 |
|
| 451 |
@pytest.mark.parametrize("mesh", meshes) |
| 452 |
def test_multi_ps_matrix(mesh): |
| 453 |
"""Tests point source PointSource(V, source) for mulitple point |
| 454 |
sources applied to a matrix for 1D, 2D and 3D. Global points given |
| 455 |
to constructor from rank 0 processor. |
| 456 |
|
| 457 |
""" |
| 458 |
|
| 459 |
c_ids = [0, 1, 2] |
| 460 |
rank = MPI.rank(mesh.mpi_comm()) |
| 461 |
V = VectorFunctionSpace(mesh, "CG", 1, dim=2) |
| 462 |
u, v = TrialFunction(V), TestFunction(V) |
| 463 |
A = assemble(Constant(0.0)*dot(u, v)*dx) |
| 464 |
|
| 465 |
source = [] |
| 466 |
if rank == 0: |
| 467 |
for c_id in c_ids: |
| 468 |
cell = Cell(mesh, c_id) |
| 469 |
point = cell.midpoint() |
| 470 |
source.append((point, 10.0)) |
| 471 |
ps = PointSource(V, source) |
| 472 |
ps.apply(A) |
| 473 |
|
| 474 |
# Checks b sums to correct value |
| 475 |
a_sum = MPI.sum(mesh.mpi_comm(), np.sum(A.array())) |
| 476 |
assert round(a_sum - 2*len(c_ids)*10) == 0 |