1+ from pathlib import Path
2+ import zipfile
3+ import json
4+ import shutil
5+ import subprocess
6+
7+ """
8+ The script performs the following steps:
9+
10+ 1. Extracts the benchmark files from a zip archive (currently assuming that it is an RO-Crate of the benchmark).
11+ 2. Iterates through the parameter configuration files, checks the "element-size" value, and if it meets the specified condition (>= 0.025)
12+ , it executes the Snakemake workflow for that configuration.
13+
14+ The results of each run (and the files used by it) are stored in the directory with the configuration name.
15+ """
16+
17+ ####################################################################################################
18+ ####################################################################################################
19+ # Benchmark Extraction
20+ ####################################################################################################
21+ ####################################################################################################
22+
23+ root_zipped_benchmark_dir = Path (__file__ ).resolve ().parent .parent
24+ root_unzipped_benchmark_dir = Path (__file__ ).resolve ().parent
25+
26+ with zipfile .ZipFile (root_zipped_benchmark_dir / "linear-elastic-plate-with-hole.zip" , 'r' ) as zip_ref :
27+ # Extract all files
28+ zip_ref .extractall (root_unzipped_benchmark_dir )
29+
30+
31+ #Creates a directory to store the conda environments. The environments are shared across different parameter configurations.
32+ #To avoid redundant creation of environments, this path will be passed to all snakemake files during execution.
33+
34+ shared_env_dir = root_unzipped_benchmark_dir / "conda_envs"
35+ shared_env_dir .mkdir (parents = True , exist_ok = True )
36+
37+ ####################################################################################################
38+ ####################################################################################################
39+ # Conditional execution of parameter configurations
40+ ####################################################################################################
41+ ####################################################################################################
42+
43+ for file in root_unzipped_benchmark_dir .glob ("parameters_*.json" ):
44+ with open (file , "r" ) as f :
45+ data = json .load (f )
46+ if data .get ("element-size" ).get ("value" ) >= 0.025 :
47+
48+ # Create output directory for the configuration
49+ output_dir = root_unzipped_benchmark_dir / "results" / data .get ("configuration" )
50+ output_dir .mkdir (parents = True , exist_ok = True )
51+
52+ # Copy the selected parameter file to the output directory with a standardised name
53+ with open (output_dir / "parameters.json" , "w" ) as outfile :
54+ json .dump (data , outfile , indent = 2 )
55+
56+ # Copy files from benchmark_dir to output_dir, excluding non-matching parameter files.
57+ for item in root_unzipped_benchmark_dir .iterdir ():
58+ if item .is_file ():
59+ if item .name .startswith ("parameters_" ) and item .suffix == ".json" :
60+ continue
61+ else :
62+ shutil .copy (item , output_dir / item .name )
63+
64+ # Run the Snakemake workflow for the configuration
65+ subprocess .run (["snakemake" , "--use-conda" , "--force" , "--cores" , "all" , "--conda-prefix" , str (shared_env_dir )], check = True , cwd = output_dir )
66+ print ("Workflow executed successfully." )
67+
68+ # For the scenario where the snakemake workflow doesn't exist, one can directly run the simulation script using the subprocess module, e.g.:
69+ #subprocess.run(["python", "run_fenics_simulation.py" \
70+ #"--input_parameter_file" "parameters.json" \
71+ #"--input_mesh_file" "mesh.msh" \
72+ #"--output_solution_file_zip" "solution_field_data.zip" \
73+ #"--output_metrics_file" "solution_metrics.json"], check=True, cwd=output_dir)
74+
75+ #Assuming the mesh.msh and parameters.json files are present/copied to the output_dir.
0 commit comments