helper scripts for compile db added

This commit is contained in:
Nicolas Kruse 2025-06-02 16:25:17 +02:00
parent 300965191f
commit 3665487733
2 changed files with 147 additions and 0 deletions

View File

@ -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()

View File

@ -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('<I4B', offset, composition_count, model, len(temperatures), len(ref_string))
assert len(header) == header_len
header_list.append(header)
composition = [el for k, v in dat['composition'].items() for el in [k.ljust(2).encode('ASCII'), v]]
data_vals = [d for darr in dat['thermo']['data'] for d in darr[:model]]
assert len(dat['thermo']['data']) == len(temperatures) - 1, f"Temperature data length mismatch for {dat['name']}."
if any(len(d) != model for d in dat['thermo']['data']):
print(f"Warning: Data length mismatch for {dat['name']}. Expected {model} coefficients, got {len(dat['thermo']['data'][0])}.")
format_string = '<' + '2sB' * composition_count + f'{len(temperatures)}f{len(data_vals)}f{len(ref_string)}s'
body = struct.pack(format_string, *(composition + temperatures + data_vals + [ref_string]))
body_list.append(body)
offset += len(body)
added_list.add(dat['name'])
with open(output_file, 'wb') as f:
f.write(b'gapy')
f.write(struct.pack('<I', len(species_names)))
f.write(species_names.encode('ASCII'))
for dat in header_list:
f.write(dat)
for dat in body_list:
f.write(dat)
if __name__ == '__main__':
main()