Peano
Loading...
Searching...
No Matches
test_exaparty.py
Go to the documentation of this file.
1"""!
2Run the ExaParty test cases through pytest
3
4Each ExaParty test case lives in its own directory and ships a run script
5which generates the code, compiles it, runs the executable and, where
6available, compares the outcome against a reference. This module runs these
7scripts one by one. A test passes if its script returns with exit code zero.
8
9All tests are integration tests, i.e. you have to request them explicitly:
10
11 python3 -m pytest -m integration tests/exaparty
12 python3 -m pytest -m "integration and not slow" tests/exaparty
13 python3 -m pytest -m integration tests/exaparty -k sod-1d
14
15They require a Peano build with --enable-particles --enable-exaparty and are
16skipped otherwise. The scripts write their generated code, executables and
17outputs into their own directories, exactly as if you ran them by hand.
18"""
19
20import os
21import subprocess
22import sys
23from pathlib import Path
24
25import pytest
26
27TESTS_DIRECTORY = Path(__file__).resolve().parent
28PEANO_ROOT_DIRECTORY = TESTS_DIRECTORY.parent.parent
29
30FUNCTIONAL_TESTS = [
31 "dastgen-test/run.sh",
32 "test-density-calculation/run.sh",
33 "test-boundary-conditions/periodic_1D/run.sh",
34 "test-boundary-conditions/periodic_2D/run.sh",
35 "test-boundary-conditions/periodic_3D/run.sh",
36 "test-smoothing-length-computation/test-smoothing-length-computation-1D/run.sh",
37 "test-smoothing-length-computation/test-smoothing-length-computation-1D/run_multiscale.sh",
38 "test-smoothing-length-computation/test-smoothing-length-computation-2D/run.sh",
39 "test-smoothing-length-computation/test-smoothing-length-computation-2D/run_multiscale.sh",
40 "test-smoothing-length-computation/test-smoothing-length-computation-3D/run.sh",
41 "test-smoothing-length-computation/test-smoothing-length-computation-3D/run_multiscale.sh",
42]
43
44PHYSICS_TESTS = [
45 "planet-orbit/run.sh",
46 "hydro-simple-sph-physics/sod-1d/run.sh",
47 "hydro-simple-sph-physics/noh-2d/run_32.sh",
48 "hydro-simple-sph-physics/noh-3d/run_20.sh",
49 "hydro-simple-sph-physics/sedov-1d/run.sh",
50 "hydro-simple-sph-physics/sedov-2d/run.sh",
51 "hydro-simple-sph-physics/sedov-3d/run.sh",
52]
53
54
56 """!
57 Check if the ExaParty libraries exist
58
59 We look into the directories which the generated Makefiles use as well,
60 i.e. PEANO_CMAKE_BUILD_DIR and PEANO_SRC_ROOT_DIR take precedence over
61 the repository this file belongs to.
62 """
63 if "PEANO_CMAKE_BUILD_DIR" in os.environ:
64 candidates = Path(os.environ["PEANO_CMAKE_BUILD_DIR"]).rglob(
65 "libExaPartyCore2d*"
66 )
67 return any(True for _ in candidates)
68 root = Path(os.environ.get("PEANO_SRC_ROOT_DIR", PEANO_ROOT_DIRECTORY))
69 return (root / "src" / "exaparty" / "libExaPartyCore2d.a").exists()
70
71
72def run_script(script):
73 """!
74 Run one ExaParty run script from within its directory
75
76 @param script: str
77 Path of the run script relative to tests/exaparty.
78
79 We run the script through bash with pipefail, as most scripts pipe the
80 executable's output through tee, which otherwise hides a crash. python3
81 resolves to the interpreter that runs pytest, and src/ is added to the
82 PYTHONPATH, so the generator scripts find Peano's Python API.
83 """
84 if not exaparty_is_built():
85 pytest.skip("Peano is not built with --enable-particles --enable-exaparty")
86 script_path = TESTS_DIRECTORY / script
87 environment = dict(os.environ)
88 environment["PATH"] = (
89 str(Path(sys.executable).parent) + os.pathsep + environment.get("PATH", "")
90 )
91 environment["PYTHONPATH"] = os.pathsep.join(
92 filter(None, [str(PEANO_ROOT_DIRECTORY / "src"), environment.get("PYTHONPATH")])
93 )
94 environment["MPLBACKEND"] = "Agg"
95 result = subprocess.run(
96 ["bash", "-o", "pipefail", script_path.name],
97 cwd=script_path.parent,
98 env=environment,
99 )
100 assert result.returncode == 0, f"{script} failed with exit code {result.returncode}"
101
102
103@pytest.mark.integration
104@pytest.mark.parametrize("script", FUNCTIONAL_TESTS)
105def test_functional(script):
106 run_script(script)
107
108
109@pytest.mark.integration
110@pytest.mark.slow
111@pytest.mark.parametrize("script", PHYSICS_TESTS)
112def test_physics(script):
113 run_script(script)
test_physics(script)
exaparty_is_built()
Check if the ExaParty libraries exist.
test_functional(script)
run_script(script)
Run one ExaParty run script from within its directory.