| 1 |
"""Unit tests for multimesh volume computation""" |
| 2 |
|
| 3 |
# Copyright (C) 2016 Anders Logg |
| 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 2016 |
| 21 |
# |
| 22 |
# First added: 2016-11-02 |
| 23 |
# Last changed: 2016-11-14 |
| 24 |
|
| 25 |
import pytest |
| 26 |
|
| 27 |
from dolfin import * |
| 28 |
from dolfin_utils.test import skip_in_parallel |
| 29 |
|
| 30 |
def compute_area_using_quadrature(multimesh): |
| 31 |
total_area = 0 |
| 32 |
for part in range(multimesh.num_parts()): |
| 33 |
part_area = 0 |
| 34 |
|
| 35 |
for cell, cell_rules in multimesh.quadrature_rules_interface(part).items(): |
| 36 |
for qr in cell_rules: |
| 37 |
weights = qr[1] |
| 38 |
part_area += sum(weights) |
| 39 |
|
| 40 |
total_area += part_area |
| 41 |
return total_area |
| 42 |
|
| 43 |
def create_multimesh_with_meshes_on_diagonal(width, offset, Nx): |
| 44 |
|
| 45 |
# Mesh width (must be less than 1) |
| 46 |
assert width < 1 |
| 47 |
|
| 48 |
# Mesh placement (must be less than the width) |
| 49 |
assert offset < width |
| 50 |
|
| 51 |
# Background mesh |
| 52 |
mesh_0 = UnitSquareMesh(Nx, Nx) |
| 53 |
|
| 54 |
# Create multimesh |
| 55 |
multimesh = MultiMesh() |
| 56 |
multimesh.add(mesh_0) |
| 57 |
|
| 58 |
# Now we have num_parts = 1 |
| 59 |
num_parts = multimesh.num_parts() |
| 60 |
|
| 61 |
while num_parts*offset + width < 1: |
| 62 |
a = num_parts*offset |
| 63 |
b = a + width |
| 64 |
mesh_top = RectangleMesh(Point(a,a), Point(b,b), Nx, Nx) |
| 65 |
multimesh.add(mesh_top) |
| 66 |
num_parts = multimesh.num_parts() |
| 67 |
|
| 68 |
multimesh.build() |
| 69 |
|
| 70 |
area = compute_area_using_quadrature(multimesh) |
| 71 |
exact_area = 0 if multimesh.num_parts() == 1 else 4*width + (multimesh.num_parts()-2)*(2*width + 2*offset) |
| 72 |
error = abs(area - exact_area) |
| 73 |
relative_error = error / exact_area |
| 74 |
tol = max(DOLFIN_EPS_LARGE, multimesh.num_parts()*multimesh.part(0).num_cells()*DOLFIN_EPS) |
| 75 |
|
| 76 |
print("") |
| 77 |
print("width = {}, offset = {}, Nx = {}, num_parts = {}".format(width, offset, Nx, multimesh.num_parts())) |
| 78 |
print("error", error) |
| 79 |
print("relative error", relative_error) |
| 80 |
print("tol", tol) |
| 81 |
return relative_error < tol |
| 82 |
|
| 83 |
@skip_in_parallel |
| 84 |
def test_meshes_on_diagonal(): |
| 85 |
"Place meshes on the diagonal inside a background mesh and check the interface area" |
| 86 |
|
| 87 |
# for Nx in range(1, 50): |
| 88 |
# for width_factor in range(1, 100): |
| 89 |
# width = 3*width_factor/(100*DOLFIN_PI) |
| 90 |
# for offset_factor in range(1, 100): |
| 91 |
# offset = offset_factor*DOLFIN_PI / (100*3.2) |
| 92 |
# if (offset < width): |
| 93 |
# assert(create_multimesh_with_meshes_on_diagonal(width, offset, Nx)) |
| 94 |
|
| 95 |
width = DOLFIN_PI / 5 |
| 96 |
offset = 0.1111 |
| 97 |
Nx = 1 |
| 98 |
assert(create_multimesh_with_meshes_on_diagonal(width, offset, Nx)) |
| 99 |
|
| 100 |
# width = 1/DOLFIN_PI #0.18888 |
| 101 |
# offset = DOLFIN_PI/100 #1e-10 |
| 102 |
# for Nx in range(1, 50): |
| 103 |
# assert(create_multimesh_with_meshes_on_diagonal(width, offset, Nx)) |
| 104 |
|
| 105 |
@skip_in_parallel |
| 106 |
def test_meshes_with_boundary_edge_overlap_2d(): |
| 107 |
# start with boundary of mesh 1 overlapping edges of mesg 0 |
| 108 |
mesh0 = UnitSquareMesh(4,4) |
| 109 |
mesh1 = UnitSquareMesh(1,1) |
| 110 |
|
| 111 |
mesh1_coords = mesh1.coordinates() |
| 112 |
mesh1_coords *= 0.5 |
| 113 |
mesh1.translate(Point(0.25, 0.25)) |
| 114 |
|
| 115 |
multimesh = MultiMesh() |
| 116 |
multimesh.add(mesh0) |
| 117 |
multimesh.add(mesh1) |
| 118 |
multimesh.build() |
| 119 |
|
| 120 |
exact_area = 2.0 |
| 121 |
|
| 122 |
area = compute_area_using_quadrature(multimesh) |
| 123 |
assert abs(area - exact_area) < DOLFIN_EPS_LARGE |
| 124 |
|
| 125 |
# next translate mesh 1 such that only the horizontal part of the boundary overlaps |
| 126 |
mesh1.translate(Point(0.1, 0.0)) |
| 127 |
multimesh.build() |
| 128 |
area = compute_area_using_quadrature(multimesh) |
| 129 |
assert abs(area - exact_area) < DOLFIN_EPS_LARGE |
| 130 |
|
| 131 |
# next translate mesh 1 such that no boundaries overlap with edges |
| 132 |
mesh1.translate(Point(0.0, 0.1)) |
| 133 |
multimesh.build() |
| 134 |
area = compute_area_using_quadrature(multimesh) |
| 135 |
assert abs(area - exact_area) < DOLFIN_EPS_LARGE |