Debian Codegraph @

tests/test_exodus.py

raw ◀ ▶
1 Debian patch touch this file
1 import urllib.request
2 from enum import Enum
3
4 from mpi4py import MPI
5
6 import numpy as np
7 import pytest
8
9 import io4dolfinx
10
11 netcdf4 = pytest.importorskip("netCDF4")
12
13
14 class DownloadStatus(Enum):
15 success = 1
16 failed = -1
17 no_connection = -2
18
19
20 def download_file_if_not_exists(
21 url, filename, comm: MPI.Intracomm = MPI.COMM_WORLD, rank: int = 0
22 ) -> DownloadStatus:
23 status = DownloadStatus.failed
24 if comm.rank == rank:
25 if not filename.exists():
26 try:
27 urllib.request.urlretrieve(url, filename)
28 status = DownloadStatus.success
29 except urllib.error.URLError as e:
•30 if (str(e) == "<urlopen error [Errno -3] Temporary failure in name resolution>"
31 or str(e) == "<urlopen error [Errno 111] Connection refused>"):
32 status = DownloadStatus.no_connection
33 else:
34 status = DownloadStatus.failed
35 else:
36 status = DownloadStatus.success
37 status = comm.bcast(status, root=rank)
38 comm.Barrier()
39 return status
40
41
42 def test_read_mesh_and_cell_data(tmp_path):
43 tmp_path = MPI.COMM_WORLD.bcast(tmp_path, root=0)
44 filename = tmp_path / "openmc_master_out_openmc0.e"
45 url = "https://github.com/neams-th-coe/cardinal/blob/devel/test/tests/neutronics/feedback/single_level/gold/openmc_master_out_openmc0.e?raw=true"
46 status = download_file_if_not_exists(url, filename)
47 if status == DownloadStatus.no_connection:
48 pytest.skip("No internet connection")
49 mesh = io4dolfinx.read_mesh(filename, MPI.COMM_WORLD, backend="exodus")
50 io4dolfinx.read_meshtags(filename, mesh, meshtag_name="cell", backend="exodus")
51 io4dolfinx.read_meshtags(filename, mesh, meshtag_name="facet", backend="exodus")
52 io4dolfinx.read_cell_data(
53 filename, name="cell_temperature", mesh=mesh, backend="exodus", time=1.0
54 )
55
56
57 def test_read_mesh_point_data(tmp_path):
58 tmp_path = MPI.COMM_WORLD.bcast(tmp_path, root=0)
59
60 filename = tmp_path / "openmc_master_out_openmc0.e"
61 url = "https://github.com/idaholab/moose/blob/next/test/tests/kernels/2d_diffusion/gold/matdiffusion_out.e?raw=true"
62 status = download_file_if_not_exists(url, filename)
63 if status == DownloadStatus.no_connection:
64 pytest.skip("No internet connection")
65
66 mesh = io4dolfinx.read_mesh(filename, MPI.COMM_WORLD, backend="exodus")
67 u = io4dolfinx.read_point_data(filename, name="u", mesh=mesh, backend="exodus", time=1.0)
68 assert mesh.topology.index_map(mesh.topology.dim).size_global == 4
69 assert mesh.geometry.index_map().size_global == 9
70 assert np.isclose(mesh.comm.allreduce(np.max(u.x.array), op=MPI.MAX), 1.1140844375981802)
71
72
73 def test_read_second_order_mesh(tmp_path):
74 tmp_path = MPI.COMM_WORLD.bcast(tmp_path, root=0)
75 filename = tmp_path / "box-test_out_nek0.e"
76 url = "https://github.com/neams-th-coe/cardinal/blob/devel/test/tests/deformation/simple-cube/gold/box-test_out_nek0.e?raw=true"
77 status = download_file_if_not_exists(url, filename)
78 if status == DownloadStatus.no_connection:
79 pytest.skip("No internet connection")
80
81 mesh = io4dolfinx.read_mesh(filename, MPI.COMM_WORLD, backend="exodus", time=5)
82 assert mesh.topology.index_map(mesh.topology.dim).size_global == 64
83 assert mesh.geometry.index_map().size_global == 1728
84
85 u_x = io4dolfinx.read_point_data(filename, name="disp_x", mesh=mesh, backend="exodus", time=5.0)
86 ref_min = -0.7699306351843485
87 ref_max = 0.7699306351843485
88 assert np.isclose(mesh.comm.allreduce(np.min(u_x.x.array), op=MPI.MIN), ref_min)
89 assert np.isclose(mesh.comm.allreduce(np.max(u_x.x.array), op=MPI.MAX), ref_max)