|
| 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 | + N = 8 |
| 16 | + mesh = dolfinx.mesh.create_rectangle( |
| 17 | + MPI.COMM_WORLD, |
| 18 | + [[-1, -1], [1, 1]], |
| 19 | + [N, N], |
| 20 | + diagonal=dolfinx.mesh.DiagonalType.crossed, |
| 21 | + dtype=dtype, |
| 22 | + ) |
| 23 | + org_area = dolfinx.fem.form(1 * ufl.dx(domain=mesh), dtype=dtype) |
| 24 | + |
| 25 | + curved_mesh = reconstruct_mesh(mesh, degree) |
| 26 | + |
| 27 | + def transform(x): |
| 28 | + u = R * x[0] * np.sqrt(1 - (x[1] ** 2 / (2))) |
| 29 | + v = R * x[1] * np.sqrt(1 - (x[0] ** 2 / (2))) |
| 30 | + return np.asarray([u, v]) |
| 31 | + |
| 32 | + curved_mesh.geometry.x[:, : curved_mesh.geometry.dim] = transform(curved_mesh.geometry.x.T).T |
| 33 | + |
| 34 | + area = dolfinx.fem.form(1 * ufl.dx(domain=curved_mesh), dtype=dtype) |
| 35 | + circumference = dolfinx.fem.form(1 * ufl.ds(domain=curved_mesh), dtype=dtype) |
| 36 | + |
| 37 | + computed_area = curved_mesh.comm.allreduce(dolfinx.fem.assemble_scalar(area), op=MPI.SUM) |
| 38 | + computed_circumference = curved_mesh.comm.allreduce( |
| 39 | + dolfinx.fem.assemble_scalar(circumference), op=MPI.SUM |
| 40 | + ) |
| 41 | + |
| 42 | + tol = 10 * np.finfo(dtype).eps |
| 43 | + assert np.isclose(computed_area, np.pi * R**2, atol=tol) |
| 44 | + assert np.isclose(computed_circumference, 2 * np.pi * R, atol=tol) |
| 45 | + |
| 46 | + linear_mesh = reconstruct_mesh(curved_mesh, 1) |
| 47 | + linear_area = dolfinx.fem.form(1 * ufl.dx(domain=linear_mesh), dtype=dtype) |
| 48 | + |
| 49 | + recovered_area = linear_mesh.comm.allreduce( |
| 50 | + dolfinx.fem.assemble_scalar(linear_area), op=MPI.SUM |
| 51 | + ) |
| 52 | + |
| 53 | + # Curve original mesh |
| 54 | + mesh.geometry.x[:, : mesh.geometry.dim] = transform(mesh.geometry.x.T).T |
| 55 | + ref_area = mesh.comm.allreduce(dolfinx.fem.assemble_scalar(org_area), op=MPI.SUM) |
| 56 | + assert np.isclose(recovered_area, ref_area, atol=tol) |
0 commit comments