1
1
from pathlib import Path
2
2
3
3
files = list (Path ("." ).rglob ("parameters_*.json" ))
4
- names = [f .stem .split ("_" )[1 ] for f in files ]
4
+
5
+ # extract the configuration from the parameter files
6
+ # by reading in the json files and extracting the "configuration" value
7
+ # configuration stores the appendix in the output files)"
8
+ # in theory, you could make that identical so parameters_1.json with configuration "1"
9
+ # would produce summary_1.json
10
+ import json
11
+ def get_configuration (file ):
12
+ with open (file , 'r' ) as f :
13
+ data = json .load (f )
14
+ # Check if "configuration" key exists, otherwise use the file name
15
+ if "configuration" in data :
16
+ return data ["configuration" ]
17
+ # Fallback to using the file name if "configuration" is not present
18
+ # Assuming the file name is in the format "parameters_<configuration>.json"
19
+ if file .stem .startswith ("parameters_" ):
20
+ return file .stem .split ("_" )[1 ]
21
+ # If no configuration is found, raise an error
22
+ raise ValueError (f"Configuration key not found for file: { file } " )
23
+
24
+ # Create a dictionary of configurations (key is the name of the parameter file)
25
+ # configurations: {Path("parameters_1.json"): "1", ...}
26
+ # Reverse mapping for easy lookup by configuration name
27
+ configurations = {file : get_configuration (file ) for file in files if file .is_file ()}
28
+ configuration_to_parameter_file = {v : str (k ) for k , v in configurations .items ()}
5
29
6
30
rule all :
7
31
input :
8
- expand ( "summary_{name} .json", name = names ) ,
9
- #expand("output_{name} .h5", name=names)
32
+ "data/summary .json" ,
33
+ "data/summary .h5" ,
10
34
11
- rule generate_input_files :
35
+ rule create_mesh :
12
36
input :
13
- "experiment.json " ,
14
- "parameters_{name}.json" ,
37
+ script = "create_mesh.py " ,
38
+ parameters = lambda wildcards : configuration_to_parameter_file [ wildcards . configuration ] ,
15
39
output :
16
- "data/input_{name}.json" ,
17
- "data/mesh_{name}.msh" ,
18
- conda : "environment.yml"
19
- shell : "python3 create_input_files.py {wildcards.name} {input}"
40
+ mesh = "data/mesh_{configuration}.msh" ,
41
+ conda : "environment_mesh.yml"
42
+ shell :
43
+ """
44
+ python3 {input.script} --input_parameter_file {input.parameters} --output_mesh_file {output.mesh}
45
+ """
20
46
21
47
rule run_simulation :
22
48
input :
23
- "data/input_{name}.json" ,
24
- "data/mesh_{name}.msh" ,
49
+ script = "run_simulation.py" ,
50
+ parameters = lambda wildcards : configuration_to_parameter_file [wildcards .configuration ],
51
+ mesh = "data/mesh_{configuration}.msh" ,
25
52
output :
26
- "data/output_{name}.vtk" ,
53
+ hdf5 = "data/solution_field_data_{configuration}.h5" ,
54
+ metrics = "data/solution_metrics_{configuration}.json" ,
27
55
conda :
28
- "environment.yml" ,
29
- shell : "python3 run_simulation.py {wildcards.name} {input}"
56
+ "environment_simulation.yml" ,
57
+ shell :
58
+ """
59
+ python3 {input.script} --input_parameter_file {input.parameters} --input_mesh_file {input.mesh} --output_solution_file_hdf5 {output.hdf5} --output_metrics_file {output.metrics}
60
+ """
30
61
31
62
rule summary :
32
63
input :
33
- "data/output_{name}.vtk" ,
34
- "data/input_{name}.json" ,
35
- "data/mesh_{name}.msh" ,
36
- "parameters_{name}.json" ,
64
+ parameters = expand ( "{param}" , param = [ configuration_to_parameter_file [ c ] for c in configurations . values ()]) ,
65
+ mesh = expand ( "data/mesh_{configuration}.msh" , configuration = configurations . values ()) ,
66
+ metrics = expand ( "data/solution_metrics_{configuration}.json" , configuration = configurations . values ()) ,
67
+ solution_field_data = expand ( "data/solution_field_data_{configuration}.h5" , configuration = configurations . values ()) ,
37
68
output :
38
- "summary_{name}.json" ,
69
+ summary_json = "data/summary.json" ,
70
+ summary_h5 = "data/summary.h5" ,
71
+ conda : "environment_postprocessing.yml" ,
39
72
run :
40
73
import json
41
- import pyvista
42
- summary = {}
43
- summary ["name" ] = wildcards .name
44
- summary ["parameters" ] = input [3 ]
45
- summary ["input" ] = input [1 ]
46
- summary ["mesh" ] = input [2 ]
47
- summary ["output" ] = input [0 ]
48
- # Load the mesh and output data
49
- max_mises_stress = 42.0
50
- from xml .etree import ElementTree as ET
51
- tree = ET .parse (input [0 ])
52
- root = tree .getroot ()
53
- pvtu_filenames = []
54
- path = Path (input [0 ]).parent
55
- for dataset in root .findall (".//DataSet" ):
56
- pvtu_filenames .append (path / dataset .get ("file" ))
57
- meshes = [pyvista .read (pvtu_filename ) for pvtu_filename in pvtu_filenames ]
58
- print (pvtu_filenames )
59
- for mesh in meshes :
60
- # Assuming the mesh has a 'von_mises_stress' array
61
- try :
62
- max_mises_stress = float (mesh ["von_mises_stress" ].max ())
63
- except KeyError :
64
- print ("von_mises_stress not found in mesh." )
65
- summary ["max_mises_stress" ] = max_mises_stress # Replace with actual computation
66
- with open (output [0 ], "w" ) as f :
67
- json .dump (summary , f , indent = 4 )
74
+ import h5py
75
+ from pathlib import Path
76
+
77
+ all_summaries = []
78
+
79
+ with h5py .File (output .summary_h5 , "w" ) as h5f :
80
+ for idx , config in enumerate (configurations .values ()):
81
+ mesh_path = input .mesh [idx ]
82
+ mesh_dataset_name = Path (mesh_path ).name
83
+ with open (mesh_path , "rb" ) as mesh_file :
84
+ mesh_data = mesh_file .read ()
85
+ h5f .create_dataset (f"{ config } /mesh" , data = mesh_data )
86
+ with h5py .File (input .solution_field_data [idx ], "r" ) as src_h5f :
87
+ for name , dataset in src_h5f .items ():
88
+ h5f .create_dataset (f"{ config } /{ name } " , data = dataset [()])
89
+ for idx , config in enumerate (configurations .values ()):
90
+ summary = {}
91
+ summary ["benchmark" ] = "linear-elastic-plate-with-hole"
92
+ with open (input .parameters [idx ], "r" ) as param_file :
93
+ summary ["parameters" ] = json .load (param_file )
94
+ summary ["mesh" ] = f"{ config } /mesh"
95
+ with open (input .metrics [idx ], "r" ) as metrics_file :
96
+ summary ["metrics" ] = json .load (metrics_file )
97
+ summary ["configuration" ] = config
98
+ all_summaries .append (summary )
99
+
100
+ with open (output .summary_json , "w" ) as f :
101
+ json .dump (all_summaries , f , indent = 4 )
0 commit comments