From 3665487733f991777e4b8e6d0cfb13358a9285c8 Mon Sep 17 00:00:00 2001 From: Nicolas Kruse Date: Mon, 2 Jun 2025 16:25:17 +0200 Subject: [PATCH] helper scripts for compile db added --- thermo_data/combine_data.py | 80 +++++++++++++++++++++++++++++++++++ thermo_data/compile_to_bin.py | 67 +++++++++++++++++++++++++++++ 2 files changed, 147 insertions(+) create mode 100644 thermo_data/combine_data.py create mode 100644 thermo_data/compile_to_bin.py diff --git a/thermo_data/combine_data.py b/thermo_data/combine_data.py new file mode 100644 index 0000000..b713dd3 --- /dev/null +++ b/thermo_data/combine_data.py @@ -0,0 +1,80 @@ +import yaml +import xml.etree.ElementTree as ET +import glob +import sys + + +def main(): + output_file = sys.argv[1] + input_files = sys.argv[2:] + + inp_therm_prop: list[dict] = [] + + for glop_filter in input_files: + for file_name in glob.glob(glop_filter): + print(f'Processing file: {file_name}...') + if file_name.endswith('.xml'): + inp_therm_prop += get_xml_data(file_name) + else: + with open(file_name, 'r') as f: + inp_therm_prop += yaml.safe_load(f)['species'] + + therm_prop = [] + added_species_names: set[str] = set() + + for species in inp_therm_prop: + name = species['name'] + assert isinstance(name, str) + for element in species['composition']: + name = name.replace(element.upper(), element) + if name not in added_species_names and 'E' not in species['composition']: + species['name'] = name + therm_prop.append(species) + added_species_names.add(name) + + with open(output_file, 'w') as f: + f.write('species:\n') + for species in sorted(therm_prop, key=lambda s: s['name']): + f.write('\n- ' + yaml.dump({'name': species['name']}, default_flow_style=False)) + f.write(f' composition: {yaml.dump(species["composition"],default_flow_style=True)}') + f.write(' thermo:\n') + f.write(f' model: {species["thermo"]["model"]}\n') + f.write(f' temperature-ranges: {yaml.dump(species["thermo"]["temperature-ranges"],default_flow_style=True)}') + f.write(' data:\n') + for d in species['thermo']['data']: + f.write(f' - {d}\n') + f.write(' ' + yaml.dump({'note': species['thermo']['note']}, default_flow_style=False)) + + print('Added: ', ', '.join(sorted(added_species_names))) + + +def get_xml_data(file_name: str) -> list[dict]: + xml_data_list: list[dict] = [] + tree = ET.parse(file_name) + root = tree.getroot() + for i, child in enumerate(root): + + elements = {el[0]: int(el[1]) for c in child.find('elements').findall('element') for el in c.items()} + + values_temp = [tr for tr in child.findall('T_range')] + t_ranges = [float(v.attrib['Tlow']) for v in values_temp] + [float(values_temp[-1].attrib['Thigh'])] + data = [[float(v.text) for v in tr] for tr in values_temp] + + is_gas = child.find('condensed').text == 'False' + + if is_gas: + xml_data_list.append({ + 'name': child.attrib['inp_file_name'], + 'composition': elements, + 'thermo': { + 'model': 'NASA9', + 'temperature-ranges': t_ranges, + 'data': data, + 'note': child.find('comment').text + } + }) + return xml_data_list + + +if __name__ == "__main__": + main() diff --git a/thermo_data/compile_to_bin.py b/thermo_data/compile_to_bin.py new file mode 100644 index 0000000..a50ff62 --- /dev/null +++ b/thermo_data/compile_to_bin.py @@ -0,0 +1,67 @@ +import yaml +import struct +import sys + + +def main(): + input_file = sys.argv[1] + output_file = sys.argv[2] + + assert not output_file.endswith('.yml') and not output_file.endswith('.yaml'), 'Binary output file should not have yaml-extension' + + with open(input_file) as f: + data = yaml.safe_load(f) + + data = list({v['name']: v for v in data['species']}.values()) + + species_names = ' '.join(s['name'] for s in data) + + header_len = 8 + + offset = 8 + len(species_names) + len(data) * header_len + + header_list = [] + body_list = [] + added_list = set() + + for dat in data: + if dat['name'] not in added_list: + + composition_count = len(dat['composition']) + model = {'NASA9': 9}[dat['thermo']['model']] + temperatures = dat['thermo']['temperature-ranges'] + ref_string = dat['thermo']['note'].encode('utf-8') + header = struct.pack('