Skip to content

Commit b99c811

Browse files
committed
Functional test comparing Cantera YAML outputs to Chemkin-converted YAML
Made test_cantera_input_files_match_chemkin that uses CompareYaml to verify RMG-generated Cantera YAML files match those converted from Chemkin format. Changes to compare_yaml_outputs.py: - Update get_absolute_path() to handle absolute paths correctly - Add support for plain 'reactions' key used by gas-only mechanisms - Reduce redundant load_yaml_file() calls in get_reactions_dict() The test compares chem{N}.yaml (from CanteraWriter) with chem.yaml (from ck2yaml conversion), checking species counts, names, and reactions.
1 parent 409ace2 commit b99c811

2 files changed

Lines changed: 84 additions & 10 deletions

File tree

test/rmgpy/rmg/mainTest.py

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,10 @@ def setup_class(cls):
5555
cls.seedKinetics = os.path.join(cls.databaseDirectory, "kinetics", "libraries", "testSeed")
5656
cls.seedKineticsEdge = os.path.join(cls.databaseDirectory, "kinetics", "libraries", "testSeed_edge")
5757

58-
os.mkdir(os.path.join(cls.testDir, cls.outputDir))
58+
output_path = os.path.join(cls.testDir, cls.outputDir)
59+
if os.path.exists(output_path):
60+
shutil.rmtree(output_path)
61+
os.mkdir(output_path)
5962

6063
cls.rmg = RMG(
6164
input_file=os.path.join(cls.testDir, "input.py"),
@@ -186,6 +189,69 @@ def test_make_cantera_input_file(self):
186189
ct.Solution(os.path.join(outName, f))
187190
except:
188191
assert False, "The output Cantera file is not loadable in Cantera."
192+
193+
def test_cantera_input_files_match_chemkin(self):
194+
"""
195+
Test that the Cantera YAML files generated directly by RMG match
196+
those converted from Chemkin files.
197+
"""
198+
import sys
199+
# Add the yaml_writer test directory to path for importing CompareYaml
200+
yaml_writer_test_dir = os.path.join(
201+
originalPath, "..", "test", "rmgpy", "yaml_writer"
202+
)
203+
sys.path.insert(0, yaml_writer_test_dir)
204+
from compare_yaml_outputs import CompareYaml
205+
sys.path.pop(0)
206+
207+
# Find the RMG-generated cantera yaml file (named chem{N}.yaml)
208+
cantera_dir = os.path.join(self.rmg.output_directory, "cantera")
209+
cantera_from_ck_dir = os.path.join(
210+
self.rmg.output_directory, "cantera_from_ck"
211+
)
212+
213+
# Get the yaml files generated directly by RMG
214+
cantera_files = [
215+
f for f in os.listdir(cantera_dir) if f.endswith('.yaml')
216+
]
217+
assert len(cantera_files) > 0, \
218+
"No Cantera YAML files found in cantera directory"
219+
# Sort by the number in the filename to get the final mechanism
220+
cantera_files.sort(
221+
key=lambda x: int(''.join(filter(str.isdigit, x)) or 0),
222+
reverse=True
223+
)
224+
rmg_yaml_file = cantera_files[0]
225+
226+
# Get the yaml file converted from chemkin
227+
ck_yaml_file = "chem.yaml"
228+
assert os.path.exists(os.path.join(cantera_from_ck_dir, ck_yaml_file)),\
229+
f"Chemkin-converted YAML file {ck_yaml_file} not found"
230+
231+
# Compare the two yaml files
232+
yaml_files = {
233+
'yaml1': [cantera_dir, rmg_yaml_file],
234+
'yaml2': [cantera_from_ck_dir, ck_yaml_file]
235+
}
236+
compare = CompareYaml(yaml_files)
237+
238+
# Check species count matches
239+
assert compare.compare_species_count(), (
240+
f"Species count mismatch between RMG yaml ({rmg_yaml_file}) "
241+
f"and chemkin yaml ({ck_yaml_file})"
242+
)
243+
244+
# Check species names match
245+
assert compare.compare_species_names(), (
246+
f"Species names mismatch between RMG yaml ({rmg_yaml_file}) "
247+
f"and chemkin yaml ({ck_yaml_file})"
248+
)
249+
250+
# Check reactions match
251+
assert compare.compare_reactions(), (
252+
f"Reactions mismatch between RMG yaml ({rmg_yaml_file}) "
253+
f"and chemkin yaml ({ck_yaml_file})"
254+
)
189255

190256

191257
@pytest.mark.functional

test/rmgpy/yaml_writer/compare_yaml_outputs.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,16 @@
44
import re
55

66
class YamlAnalyst:
7-
def __init__(self, path_to_chemkin_yaml, chemkin_yaml_file):
8-
self.path_to_chemkin_yaml = path_to_chemkin_yaml
9-
self.chemkin_yaml_file = chemkin_yaml_file
7+
def __init__(self, path_to_yaml, yaml_file):
8+
self.path_to_yaml = path_to_yaml
9+
self.yaml_file = yaml_file
1010

1111
def get_absolute_path(self):
12-
return os.path.join(os.getcwd(), self.path_to_chemkin_yaml, self.chemkin_yaml_file)
12+
path = os.path.join(self.path_to_yaml, self.yaml_file)
13+
# If path is already absolute, use it; otherwise join with cwd
14+
if os.path.isabs(path):
15+
return path
16+
return os.path.join(os.getcwd(), path)
1317

1418
def load_yaml_file(self):
1519
with open(self.get_absolute_path(), 'r') as file:
@@ -29,13 +33,17 @@ def get_species_count_per_phase(self):
2933

3034
def get_reactions_dict(self):
3135
reactions_dict = {}
32-
for key, values in self.load_yaml_file().items():
33-
if key in [f"{phase['name']}-reactions" for phase in self.load_yaml_file()['phases']]:
34-
reactions_dict[key] = self.load_yaml_file()[key]
36+
data = self.load_yaml_file()
37+
for key, values in data.items():
38+
if key in [f"{phase['name']}-reactions" for phase in data['phases']]:
39+
reactions_dict[key] = data[key]
3540
elif key == 'gas_reactions':
36-
reactions_dict['gas_reactions'] = self.load_yaml_file()['gas_reactions']
41+
reactions_dict['gas_reactions'] = data['gas_reactions']
3742
elif key == 'surface_reactions':
38-
reactions_dict['surface_reactions'] = self.load_yaml_file()['surface_reactions']
43+
reactions_dict['surface_reactions'] = data['surface_reactions']
44+
elif key == 'reactions':
45+
# Gas-only mechanisms use plain 'reactions' key
46+
reactions_dict['reactions'] = data['reactions']
3947
return reactions_dict
4048

4149
def create_reaction_df(self, reactions):

0 commit comments

Comments
 (0)