Debian Codegraph @

tests/test_exodus.py

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
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 status = DownloadStatus.no_connection
32 else:
33 status = DownloadStatus.failed
34 else:
35 status = DownloadStatus.success
36 status = comm.bcast(status, root=rank)
37 comm.Barrier()
38 return status
39
40
41 def test_read_mesh_and_cell_data(tmp_path):
42 tmp_path = MPI.COMM_WORLD.bcast(tmp_path, root=0)
43 filename = tmp_path / "openmc_master_out_openmc0.e"
44 url = "https://github.com/neams-th-coe/cardinal/blob/devel/test/tests/neutronics/feedback/single_level/gold/openmc_master_out_openmc0.e?raw=true"
45 status = download_file_if_not_exists(url, filename)
46 if status == DownloadStatus.no_connection:
47 pytest.skip("No internet connection")
48 mesh = io4dolfinx.read_mesh(filename, MPI.COMM_WORLD, backend="exodus")
49 io4dolfinx.read_meshtags(filename, mesh, meshtag_name="cell", backend="exodus")
50 io4dolfinx.read_meshtags(filename, mesh, meshtag_name="facet", backend="exodus")
51 io4dolfinx.read_cell_data(
52 filename, name="cell_temperature", mesh=mesh, backend="exodus", time=1.0
53 )
54
55
56 def test_read_mesh_point_data(tmp_path):
57 tmp_path = MPI.COMM_WORLD.bcast(tmp_path, root=0)
58
59 filename = tmp_path / "openmc_master_out_openmc0.e"
60 url = "https://github.com/idaholab/moose/blob/next/test/tests/kernels/2d_diffusion/gold/matdiffusion_out.e?raw=true"
61 status = download_file_if_not_exists(url, filename)
62 if status == DownloadStatus.no_connection:
63 pytest.skip("No internet connection")
64
65 mesh = io4dolfinx.read_mesh(filename, MPI.COMM_WORLD, backend="exodus")
66 u = io4dolfinx.read_point_data(filename, name="u", mesh=mesh, backend="exodus", time=1.0)
67 assert mesh.topology.index_map(mesh.topology.dim).size_global == 4
68 assert mesh.geometry.index_map().size_global == 9
69 assert np.isclose(mesh.comm.allreduce(np.max(u.x.array), op=MPI.MAX), 1.1140844375981802)
70
71
72 def test_read_second_order_mesh(tmp_path):
73 tmp_path = MPI.COMM_WORLD.bcast(tmp_path, root=0)
74 filename = tmp_path / "box-test_out_nek0.e"
75 url = "https://github.com/neams-th-coe/cardinal/blob/devel/test/tests/deformation/simple-cube/gold/box-test_out_nek0.e?raw=true"
76 status = download_file_if_not_exists(url, filename)
77 if status == DownloadStatus.no_connection:
78 pytest.skip("No internet connection")
79
80 mesh = io4dolfinx.read_mesh(filename, MPI.COMM_WORLD, backend="exodus", time=5)
81 assert mesh.topology.index_map(mesh.topology.dim).size_global == 64
82 assert mesh.geometry.index_map().size_global == 1728
83
84 u_x = io4dolfinx.read_point_data(filename, name="disp_x", mesh=mesh, backend="exodus", time=5.0)
85 ref_min = -0.7699306351843485
86 ref_max = 0.7699306351843485
87 assert np.isclose(mesh.comm.allreduce(np.min(u_x.x.array), op=MPI.MIN), ref_min)
88 assert np.isclose(mesh.comm.allreduce(np.max(u_x.x.array), op=MPI.MAX), ref_max)