Skip to content

Commit cf11606

Browse files
authored
added the fenics examples for linear elastic plate with hole benchmark (#70)
* added the fenics examples for linear elastic plate with hole benchmark * rewritten the CI .yml to remove ambiguity. * removed comments
1 parent 4d17426 commit cf11606

5 files changed

Lines changed: 502 additions & 0 deletions

File tree

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
name: Examples-CI
2+
on:
3+
push:
4+
5+
pull_request:
6+
branches: [ main ]
7+
8+
# Allows you to run this workflow manually from the Actions tab
9+
workflow_dispatch:
10+
11+
# Runs the workflow once per day at 3:15am
12+
schedule:
13+
- cron: '3 16 * * *'
14+
15+
env:
16+
CACHE_NUMBER: 1 # increase to reset cache manually
17+
SNAKEMAKE_RESULT_FILE: metadata4ing_provenance
18+
PROVENANACE_FILE_NAME: element_size_vs_max_mises_stress.pdf
19+
20+
jobs:
21+
run-examples:
22+
runs-on: ubuntu-latest
23+
steps:
24+
- name: checkout repo content
25+
uses: actions/checkout@v2
26+
27+
- name: Setup Mambaforge
28+
uses: conda-incubator/setup-miniconda@v3
29+
with:
30+
miniforge-version: latest
31+
activate-environment: model-validation
32+
use-mamba: true
33+
34+
- name: Set strict channel priority
35+
run: conda config --set channel_priority strict
36+
37+
- name: Update environment
38+
run: mamba env update -n model-validation -f environment_benchmarks.yml
39+
40+
- name: Run linear-elastic-plate-with-hole using fenics
41+
shell: bash -l {0}
42+
run: |
43+
cd $GITHUB_WORKSPACE/examples/linear-elastic-plate-with-hole/fenics/
44+
python run_benchmark.py
45+
46+
- name: Archive results of the fenics run of linear-elastic-plate-with-hole
47+
uses: actions/upload-artifact@v4
48+
with:
49+
name: snakemake_results_linear-elastic-plate-with-hole
50+
path: |
51+
examples/linear-elastic-plate-with-hole/fenics/results/
52+
53+
54+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
name: fenics_simulation
2+
# Environment file for fenics simulation scripts. Called by fenics tool workflow.
3+
4+
channels:
5+
- conda-forge
6+
7+
channel_priority: strict
8+
9+
dependencies:
10+
- python=3.12
11+
- fenics-dolfinx=0.9.*
12+
- mpich
13+
- petsc4py
14+
- pint
15+
- python-gmsh
16+
- sympy
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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

Comments
 (0)