| 1 |
"""Unit tests for quadrature compression""" |
| 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: 2016-06-28 |
| 21 |
# Last changed: 2017-05-28 |
| 22 |
|
| 23 |
import pytest |
| 24 |
|
| 25 |
from dolfin import * |
| 26 |
from dolfin_utils.test import skip_in_parallel |
| 27 |
from numpy import random, sort, any |
| 28 |
|
| 29 |
def build_multimesh_2d(compress_volume, compress_interface): |
| 30 |
|
| 31 |
# Create multimesh |
| 32 |
multimesh = MultiMesh() |
| 33 |
multimesh.parameters["compress_volume_quadrature"] = compress_volume |
| 34 |
multimesh.parameters["compress_interface_quadrature"] = compress_interface |
| 35 |
|
| 36 |
# Add background mesh |
| 37 |
N_x = 2 |
| 38 |
mesh = UnitSquareMesh(N_x, N_x) |
| 39 |
multimesh.add(mesh) |
| 40 |
|
| 41 |
# Set seed |
| 42 |
random.seed(1) |
| 43 |
|
| 44 |
# Add num_parts-1 random sized and rotated rectangular meshes |
| 45 |
num_parts = 2 |
| 46 |
while (multimesh.num_parts() < num_parts): |
| 47 |
|
| 48 |
x0, x1 = sort(random.rand(2)) |
| 49 |
y0, y1 = sort(random.rand(2)) |
| 50 |
if abs(x1 - x0) < DOLFIN_EPS: |
| 51 |
x1 += DOLFIN_EPS |
| 52 |
if abs(y1 - y0) < DOLFIN_EPS: |
| 53 |
y1 += DOLFIN_EPS |
| 54 |
|
| 55 |
N_x_part = int(max(abs(x1-x0)*N_x, 1)) |
| 56 |
N_y_part = int(max(abs(y1-y0)*N_x, 1)) |
| 57 |
mesh = RectangleMesh(Point(x0, y0), Point(x1, y1), |
| 58 |
N_x_part, N_y_part) |
| 59 |
|
| 60 |
# Rotate |
| 61 |
phi = random.rand()*180 |
| 62 |
mesh.rotate(phi) |
| 63 |
coords = mesh.coordinates() |
| 64 |
is_interior = not any(coords < 0) and not any(coords > 1.) |
| 65 |
|
| 66 |
if is_interior: |
| 67 |
multimesh.add(mesh) |
| 68 |
|
| 69 |
multimesh.build() |
| 70 |
return multimesh |
| 71 |
|
| 72 |
def volume_area(multimesh): |
| 73 |
volume = multimesh.compute_volume() |
| 74 |
area = multimesh.compute_area() |
| 75 |
return volume, area |
| 76 |
|
| 77 |
@skip_in_parallel |
| 78 |
def test_compression_2d(): |
| 79 |
# Reference volume and area |
| 80 |
multimesh = build_multimesh_2d(False, False) |
| 81 |
volume, area = volume_area(multimesh) |
| 82 |
|
| 83 |
# Volume compression |
| 84 |
multimesh_v = build_multimesh_2d(True, False) |
| 85 |
volume_v, area_v = volume_area(multimesh) |
| 86 |
|
| 87 |
# Interface compression |
| 88 |
multimesh_i = build_multimesh_2d(False, True) |
| 89 |
volume_i, area_i = volume_area(multimesh_i) |
| 90 |
|
| 91 |
# Tolerances |
| 92 |
volume_tol = 1e-10 |
| 93 |
area_tol = 1e-10 |
| 94 |
|
| 95 |
# Assert that the compressed volume / area is close |
| 96 |
assert abs(volume - volume_v) < volume_tol |
| 97 |
assert abs(area - area_i) < area_tol |
| 98 |
|
| 99 |
# Assert that the compressed volume / area does not influence the area / volume |
| 100 |
assert abs(volume - volume_i) < DOLFIN_EPS |
| 101 |
assert abs(area - area_v) < DOLFIN_EPS |