11"""
22Compare the Mandyoc output data with the expected data
33"""
4- import os
54import pytest
65import numpy as np
76import numpy .testing as npt
87from pathlib import Path
98
10- # Test path
11- base_path = Path (os .path .realpath (os .path .abspath (__file__ ))).parent
129
13- scenarios = [
14- "vanKeken1997_case1a" ,
15- "Crameri2012_case2" ,
16- "continental_rift" ,
17- "punch" ,
18- "Schmalholz_2011" ,
19- "two_layers_with_variable_conductivity" ,
20- ]
10+ BASE_PATH = Path (__file__ ).parent .absolute ()
2111
22- # Name of the files to compare
23- fields = [
24- "time" ,
12+ MAIN_FIELDS = [
2513 "density" ,
2614 "heat" ,
2715 "pressure" ,
16+ "step" ,
2817 "strain" ,
2918 "strain_rate" ,
30- "surface" ,
3119 "temperature" ,
20+ "time" ,
3221 "velocity" ,
3322 "viscosity" ,
34- "step" ,
3523]
3624
37- # steps to compare
38- steps = [0 , 1 , 10000 ]
25+
26+ # Definition of each scenario
27+ SCENARIOS = {
28+ "vanKeken1997_case1a" : {
29+ "steps" : [0 , 1 ],
30+ "fields" : MAIN_FIELDS ,
31+ },
32+ "Crameri2012_case2" : {
33+ "steps" : [0 ],
34+ "fields" : MAIN_FIELDS ,
35+ "num_procs" : 1 ,
36+ },
37+ "continental_rift" : {
38+ "steps" : [0 , 1 ],
39+ "fields" : MAIN_FIELDS ,
40+ },
41+ "punch" : {
42+ "steps" : [0 ],
43+ "fields" : MAIN_FIELDS ,
44+ },
45+ "Schmalholz_2011" : {
46+ "steps" : [0 , 1 ],
47+ "fields" : MAIN_FIELDS ,
48+ },
49+ "two_layers_with_variable_conductivity" : {
50+ "steps" : [0 , 10000 ],
51+ "fields" : MAIN_FIELDS ,
52+ },
53+ }
54+
3955
4056def read (filename ):
4157 """
@@ -49,37 +65,40 @@ def read(filename):
4965 return data
5066
5167
52- @pytest .mark .parametrize ("step" , steps )
53- @pytest .mark .parametrize ("field" , fields )
54- @pytest .mark .parametrize ("scenario" , scenarios )
55- def test_result (scenario , field , step ):
56- """Run tests"""
68+ def get_test_cases ():
69+ """Generates valid test combinations."""
70+ cases = []
71+ for name , config in SCENARIOS .items ():
72+ for step in config ["steps" ]:
73+ for field in config ["fields" ]:
74+ cases .append ((name , field , step ))
75+ return cases
76+
5777
58- if scenario != 'Crameri2012_case2' and field == 'surface' :
59- pytest .skip ('No surface for this scenario' )
78+ @pytest .mark .parametrize ("scenario, field, step" , get_test_cases ())
79+ def test_result (scenario , field , step ):
80+ config = SCENARIOS [scenario ]
6081
61- if scenario == 'Crameri2012_case2' and step == 1 :
62- pytest . skip ( 'Tested with only one processor' )
82+ test_path = BASE_PATH / "data" / scenario / "output"
83+ expected_path = BASE_PATH / "data" / scenario / "expected"
6384
64- if ( scenario == 'punch' and step == 1 ) or ( scenario == 'punch' and field == 'step_1' ):
65- pytest .skip ( 'Test with only one step' )
85+ if not test_path . exists ( ):
86+ pytest .fail ( f"Simulation directory missing. Scenario { scenario } likely failed to run." )
6687
67- if (scenario != 'two_layers_with_variable_conductivity' and step == 10000 ) or (scenario == 'two_layers_with_variable_conductivity' and step == 1 ):
68- pytest .skip ('Test step/file not available for this scenario' )
88+ # Handle the 'step' field
89+ if field == 'step' :
90+ num_procs = config .get ("num_procs" , 2 )
91+ filenames = [f"step_{ step } _{ i } .txt" for i in range (num_procs )]
92+ else :
93+ filenames = [f"{ field } _{ step } .txt" ]
6994
70- test_path = base_path / "data" / scenario / "output"
71- expected_path = base_path / "data" / scenario / "expected"
95+ for fname in filenames :
96+ out_file = test_path / fname
97+ exp_file = expected_path / fname
7298
73- filenames = [ f" { field } _ { step } .txt" ]
99+ assert out_file . exists (), f"Missing output file: { out_file } "
74100
75- if field == 'step' :
76- if scenario in ['Crameri2012_case2' ]:
77- filenames = [f"step_{ step } _0.txt" ]
78- else :
79- filenames = [f"step_{ step } _0.txt" , f"step_{ step } _1.txt" ]
80-
81- for filename in filenames :
82- output = read (test_path / filename )
83- expected = read (expected_path / filename )
101+ output = read (out_file )
102+ expected = read (exp_file )
84103
85104 npt .assert_allclose (output , expected , rtol = 2e-4 , atol = 1.0e-18 )
0 commit comments