| 1 |
from mpi4py import MPI |
| 2 |
|
| 3 |
import dolfinx |
| 4 |
import numpy as np |
| 5 |
import pytest |
| 6 |
import ufl |
| 7 |
|
| 8 |
from io4dolfinx import reconstruct_mesh |
| 9 |
|
| 10 |
|
| 11 |
@pytest.mark.parametrize("dtype", [np.float32, np.float64]) |
| 12 |
@pytest.mark.parametrize("degree", [2, 3]) |
| 13 |
@pytest.mark.parametrize("R", [0.1, 1, 10]) |
| 14 |
def test_curve_mesh(degree, dtype, R): |
| 15 |
if hasattr(dolfinx.fem, "interpolate_geometry"): |
| 16 |
pytest.skip("There are tests in DOLFINx for this for v0.11 and above.") |
| 17 |
N = 8 |
| 18 |
mesh = dolfinx.mesh.create_rectangle( |
| 19 |
MPI.COMM_WORLD, |
| 20 |
[[-1, -1], [1, 1]], |
| 21 |
[N, N], |
| 22 |
diagonal=dolfinx.mesh.DiagonalType.crossed, |
| 23 |
dtype=dtype, |
| 24 |
) |
| 25 |
org_area = dolfinx.fem.form(1 * ufl.dx(domain=mesh), dtype=dtype) |
| 26 |
|
| 27 |
curved_mesh = reconstruct_mesh(mesh, degree) |
| 28 |
|
| 29 |
def transform(x): |
| 30 |
u = R * x[0] * np.sqrt(1 - (x[1] ** 2 / (2))) |
| 31 |
v = R * x[1] * np.sqrt(1 - (x[0] ** 2 / (2))) |
| 32 |
return np.asarray([u, v]) |
| 33 |
|
| 34 |
curved_mesh.geometry.x[:, : curved_mesh.geometry.dim] = transform(curved_mesh.geometry.x.T).T |
| 35 |
|
| 36 |
area = dolfinx.fem.form(1 * ufl.dx(domain=curved_mesh), dtype=dtype) |
| 37 |
circumference = dolfinx.fem.form(1 * ufl.ds(domain=curved_mesh), dtype=dtype) |
| 38 |
|
| 39 |
computed_area = curved_mesh.comm.allreduce(dolfinx.fem.assemble_scalar(area), op=MPI.SUM) |
| 40 |
computed_circumference = curved_mesh.comm.allreduce( |
| 41 |
dolfinx.fem.assemble_scalar(circumference), op=MPI.SUM |
| 42 |
) |
| 43 |
|
| 44 |
tol = 15 * np.finfo(dtype).eps |
| 45 |
assert np.isclose(computed_area, np.pi * R**2, atol=tol) |
| 46 |
assert np.isclose(computed_circumference, 2 * np.pi * R, atol=tol) |
| 47 |
|
| 48 |
linear_mesh = reconstruct_mesh(curved_mesh, 1) |
| 49 |
linear_area = dolfinx.fem.form(1 * ufl.dx(domain=linear_mesh), dtype=dtype) |
| 50 |
|
| 51 |
recovered_area = linear_mesh.comm.allreduce( |
| 52 |
dolfinx.fem.assemble_scalar(linear_area), op=MPI.SUM |
| 53 |
) |
| 54 |
|
| 55 |
# Curve original mesh |
| 56 |
mesh.geometry.x[:, : mesh.geometry.dim] = transform(mesh.geometry.x.T).T |
| 57 |
ref_area = mesh.comm.allreduce(dolfinx.fem.assemble_scalar(org_area), op=MPI.SUM) |
| 58 |
assert np.isclose(recovered_area, ref_area, atol=tol) |