Skip to content

Commit a422c4c

Browse files
authored
Merge pull request #383 from simvue-io/296-ensure-all-examples-are-up-to-date-and-verified
Ensure all examples are up to date and verified
2 parents fa78e52 + 428490d commit a422c4c

File tree

16 files changed

+1129
-377
lines changed

16 files changed

+1129
-377
lines changed

.github/workflows/examples.yml

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
name: Simvue Examples
2+
3+
on:
4+
push:
5+
tags:
6+
- 'v*-*-rc*'
7+
branches:
8+
- "296-*"
9+
workflow_dispatch:
10+
11+
permissions:
12+
contents: read
13+
14+
jobs:
15+
GeometryOptimisation:
16+
runs-on: ubuntu-latest
17+
steps:
18+
- uses: actions/checkout@v4
19+
- name: Download BlueMira
20+
uses: actions/checkout@v4
21+
with:
22+
repository: Fusion-Power-Plant-Framework/bluemira
23+
ref: v1.10.0
24+
path: blue_mira
25+
- uses: conda-incubator/setup-miniconda@v3
26+
with:
27+
activate-environment: bluemira
28+
environment-file: blue_mira/conda/environment.yml
29+
miniforge-version: "latest"
30+
use-only-tar-bz2: false
31+
miniforge-variant: Mambaforge
32+
use-mamba: true
33+
python-version: "3.11"
34+
- name: Install bluemira
35+
shell: bash -l {0}
36+
run: pip install -e blue_mira
37+
- name: Install Simvue
38+
shell: bash -l {0}
39+
run: pip install -e .
40+
- name: Run Example
41+
shell: bash -l {0}
42+
run: python ./examples/GeometryOptimisation/bluemira_simvue_geometry_optimisation.py
43+
Logging:
44+
runs-on: ubuntu-latest
45+
steps:
46+
- uses: actions/checkout@v4
47+
- name: Setup Python
48+
uses: actions/setup-python@v5
49+
with:
50+
python-version: "3.11"
51+
- name: Install Simvue
52+
run: python3 -m pip install .
53+
- name: Run Example
54+
run: |
55+
export SIMVUE_URL=${{ secrets.SIMVUE_URL }}
56+
export SIMVUE_TOKEN=${{ secrets.SIMVUE_TOKEN }}
57+
python3 examples/Logging/logging-to-simvue.py --ci
58+
OpenFOAM:
59+
runs-on: ubuntu-latest
60+
container:
61+
image: openfoam/openfoam10-paraview56
62+
options: --user root
63+
steps:
64+
- uses: actions/checkout@v4
65+
- name: Manual Python Install
66+
run: |
67+
add-apt-repository ppa:deadsnakes/ppa -y
68+
apt-get update && \
69+
apt-get install -y git wget curl python3.11-full
70+
71+
- name: Install dependencies
72+
run: |
73+
python3.11 -m ensurepip --upgrade
74+
python3.11 -m pip install ukaea-multiparser
75+
- name: Install Package
76+
run: python3.11 -m pip install .
77+
- name: Run Example
78+
shell: bash
79+
run: |
80+
export SIMVUE_URL=${{ secrets.SIMVUE_URL }}
81+
export SIMVUE_TOKEN=${{ secrets.SIMVUE_TOKEN }}
82+
python3.11 ./examples/OpenFOAM/simvue_openfoam.py /opt/openfoam10/tutorials/incompressible/pimpleFoam/laminar/movingCone/Allrun --ci
83+
Optuna:
84+
runs-on: ubuntu-latest
85+
steps:
86+
- uses: actions/checkout@v4
87+
- name: Setup Python
88+
uses: actions/setup-python@v5
89+
with:
90+
python-version: "3.11"
91+
- name: Install Simvue
92+
run: python3 -m pip install .
93+
- name: Install Dependencies
94+
run: |
95+
python3 -m pip install torch torchvision --index-url https://download.pytorch.org/whl/cpu
96+
python3 -m pip install -r examples/Optuna/PyTorch/requirements.txt
97+
- name: Run Example
98+
run: |
99+
export SIMVUE_URL=${{ secrets.SIMVUE_URL }}
100+
export SIMVUE_TOKEN=${{ secrets.SIMVUE_TOKEN }}
101+
python3.11 ./examples/Optuna/PyTorch/simvue_optuna_pytorch.py --ci
102+
PyTorch:
103+
runs-on: ubuntu-latest
104+
steps:
105+
- uses: actions/checkout@v4
106+
- name: Setup Python
107+
uses: actions/setup-python@v5
108+
with:
109+
python-version: "3.11"
110+
- name: Install Simvue
111+
run: python3 -m pip install .
112+
- name: Install Dependencies
113+
run: |
114+
python3 -m pip install torch torchvision --index-url https://download.pytorch.org/whl/cpu
115+
python3 -m pip install -r examples/PyTorch/requirements.txt
116+
- name: Run Example
117+
run: |
118+
export SIMVUE_URL=${{ secrets.SIMVUE_URL }}
119+
export SIMVUE_TOKEN=${{ secrets.SIMVUE_TOKEN }}
120+
python3.11 ./examples/PyTorch/main.py --ci
121+
SU2:
122+
runs-on: ubuntu-latest
123+
steps:
124+
- uses: actions/checkout@v4
125+
- name: Setup Python
126+
uses: actions/setup-python@v5
127+
with:
128+
python-version: "3.11"
129+
- name: Download SU2
130+
run: |
131+
wget https://github.com/su2code/SU2/releases/download/v8.0.1/SU2-v8.0.1-linux64.zip
132+
unzip SU2-v8.0.1-linux64.zip
133+
- name: Install dependencies
134+
run: |
135+
python -m pip install ukaea-multiparser
136+
python -m pip install .
137+
- name: Run Example
138+
run: |
139+
export SIMVUE_URL=${{ secrets.SIMVUE_URL }}
140+
export SIMVUE_TOKEN=${{ secrets.SIMVUE_TOKEN }}
141+
python examples/SU2/SU2.py bin --config tests/example_data/SU2_inv_ONERAM6.cfg --ci
142+
TensorFlow:
143+
runs-on: ubuntu-latest
144+
steps:
145+
- uses: actions/checkout@v4
146+
- name: Setup Python
147+
uses: actions/setup-python@v5
148+
with:
149+
python-version: "3.11"
150+
- name: Install Simvue
151+
run: python3 -m pip install .
152+
- name: Install Dependencies
153+
run: python3 -m pip install -r examples/Tensorflow/requirements.txt
154+
- name: Run Example
155+
run: |
156+
export SIMVUE_URL=${{ secrets.SIMVUE_URL }}
157+
export SIMVUE_TOKEN=${{ secrets.SIMVUE_TOKEN }}
158+
python3.11 ./examples/Tensorflow/dynamic_rnn.py --ci
159+
160+
GEANT4:
161+
runs-on: ubuntu-latest
162+
container:
163+
image: artemisbeta/geant4:11.2.1
164+
steps:
165+
- uses: actions/checkout@v4
166+
with:
167+
submodules: recursive
168+
- name: Setup Python
169+
uses: actions/setup-python@v5
170+
with:
171+
python-version: "3.11"
172+
- name: Install Simvue
173+
run: python3 -m pip install .
174+
- name: Install Dependencies
175+
run: python3 -m pip install -r examples/Geant4/requirements.txt
176+
- name: Build Example
177+
run: |
178+
cmake -DCMAKE_PREFIX_PATH=/usr/local/share/geant4/install/4.11.2/ -Bbuild examples/Geant4/FixedTarget/
179+
cmake --build build
180+
- name: Run Example
181+
run: |
182+
export SIMVUE_URL=${{ secrets.SIMVUE_URL }}
183+
export SIMVUE_TOKEN=${{ secrets.SIMVUE_TOKEN }}
184+
python examples/Geant4/geant4_simvue.py build/MaterialTesting --ci --events 10

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,3 +141,6 @@ offline/
141141

142142
# VSCode
143143
.vscode/
144+
145+
# Vagrant
146+
Vagrantfile

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "examples/Geant4/FixedTarget"]
2+
path = examples/Geant4/FixedTarget
3+
url = https://github.com/artemis-beta/Geant4-Example.git

examples/Geant4/FixedTarget

Submodule FixedTarget added at 4113c4c

examples/Geant4/geant4_simvue.py

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
"""
2+
Geant4 Simvue
3+
=============
4+
5+
Example of repeating simulation of a proton fired at a target of beryllium
6+
monitoring the yield of key particles of interest
7+
"""
8+
9+
import multiparser
10+
import multiparser.parsing.file as mp_file_parse
11+
import simvue
12+
import uproot
13+
import multiprocessing
14+
import typing
15+
import click
16+
import pathlib
17+
import os
18+
import tempfile
19+
20+
from particle import Particle
21+
22+
23+
@click.command
24+
@click.argument("g4_binary", type=click.Path(exists=True))
25+
@click.option("--config", type=click.Path(exists=True), default=None)
26+
@click.option("--ci", is_flag=True, default=False)
27+
@click.option("--momentum", type=float, default=10)
28+
@click.option("--events", type=int, default=100)
29+
def geant4_simvue_example(
30+
g4_binary: str, config: typing.Optional[str], ci: bool, momentum: float, events: int
31+
) -> None:
32+
@mp_file_parse.file_parser
33+
def root_file_parser(
34+
file_name: str, *_, **__
35+
) -> tuple[dict[str, typing.Any], dict[str, typing.Any]]:
36+
with uproot.open(file_name) as root_data:
37+
hit_data: dict[str, uproot.TBranch]
38+
if not (hit_data := root_data.get("Hits")):
39+
raise RuntimeError("Expected key 'Hits' in ROOT file")
40+
41+
particles_of_interest = [2212, 211, 11, 22, 2112]
42+
43+
all_particles = hit_data["fID"].array(library="np").tolist()
44+
45+
out_data = {
46+
Particle.from_pdgid(abs(identifier))
47+
.name.replace("+", "plus")
48+
.replace("-", "minus"): [abs(i) for i in all_particles].count(
49+
abs(identifier)
50+
)
51+
for identifier in particles_of_interest
52+
}
53+
54+
return {}, out_data
55+
56+
termination_trigger = multiprocessing.Event()
57+
58+
with simvue.Run() as run:
59+
run.init(
60+
"Geant4_simvue_demo",
61+
folder="/simvue_client_demos",
62+
tags=[
63+
"Geant4",
64+
],
65+
description="Geant4 fixed target scenario",
66+
retention_period="1 hour" if ci else None,
67+
visibility="tenant" if ci else None,
68+
)
69+
70+
kwargs: dict[str, typing.Any] = {}
71+
72+
if config:
73+
kwargs["script"] = config
74+
with tempfile.TemporaryDirectory() as tempd:
75+
with multiparser.FileMonitor(
76+
per_thread_callback=lambda metrics, *_: run.log_metrics(metrics),
77+
exception_callback=run.log_event,
78+
terminate_all_on_fail=False,
79+
plain_logging=True,
80+
flatten_data=True,
81+
termination_trigger=termination_trigger,
82+
) as monitor:
83+
monitor.track(
84+
path_glob_exprs=[f'{pathlib.Path(tempd).joinpath("*")}'],
85+
parser_func=root_file_parser,
86+
static=True,
87+
)
88+
monitor.run()
89+
90+
for i in range(events):
91+
if i % 10 == 0:
92+
click.secho(
93+
f"Running {i+1}/{events} with momentum {momentum} GeV",
94+
bold=True,
95+
fg="cyan",
96+
)
97+
running_simulation = multiprocessing.Event()
98+
run.add_process(
99+
identifier=f"Geant4_simulation_{momentum}GeV_{i}",
100+
executable=g4_binary,
101+
momentum=momentum,
102+
batch=True,
103+
output=pathlib.Path(tempd).joinpath(
104+
f"output_{momentum}GeV_{i+1}.root"
105+
),
106+
completion_trigger=running_simulation
107+
if i == events - 1
108+
else None,
109+
**kwargs,
110+
)
111+
112+
termination_trigger.set()
113+
114+
for file in pathlib.Path().cwd().glob("Geant4*.err"):
115+
os.remove(file)
116+
117+
for file in pathlib.Path().cwd().glob("Geant4*.out"):
118+
os.remove(file)
119+
120+
121+
if __name__ in "__main__":
122+
geant4_simvue_example()

examples/Geant4/requirements.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
simvue
2+
uproot
3+
ukaea-multiparser
4+
click
5+
particle

examples/GeometryOptimisation/bluemira_simvue_geometry_optimisation.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
A quick tutorial on the optimisation of geometry in bluemira
2323
"""
2424

25+
import os
2526
import logging
2627

2728
from bluemira.geometry.optimisation import GeometryOptimisationProblem
@@ -68,6 +69,9 @@
6869
"optimiser.ftol_abs": ftol_abs,
6970
"optimiser.ftol_rel": ftol_rel,
7071
},
72+
folder="/simvue_client_demos",
73+
visibility="tenant" if os.environ.get("CI") else None,
74+
tags=["bluemira", "simvue_client_examples"],
7175
description="A simple GeometryOptimisationProblem, where we minimise the length of parameterised geometry using gradient-based optimisation algorithm.",
7276
)
7377

examples/Logging/logging-to-simvue.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
11
import logging
2+
import click
23

34
from simvue import Handler, Run
45

5-
if __name__ == "__main__":
6-
run = Run()
76

8-
run.init(tags=["logging"], description="Logging test")
7+
@click.command
8+
@click.option("--ci", is_flag=True, default=False)
9+
def simvue_logger_demo(ci: bool) -> None:
10+
with Run() as run:
11+
run.init(
12+
tags=["logging", "simvue_client_examples"],
13+
folder="/simvue_client_demos",
14+
description="Logging test",
15+
retention_period="1 hour" if ci else None,
16+
visibility="tenant" if ci else None,
17+
)
918

10-
logger = logging.getLogger(__name__)
11-
logger.setLevel(logging.DEBUG)
12-
sth = Handler(run)
13-
logger.addHandler(sth)
19+
logger = logging.getLogger(__name__)
20+
logger.setLevel(logging.DEBUG)
21+
sth = Handler(run)
22+
logger.addHandler(sth)
1423

15-
logger.info("This is a Simvue logging test")
16-
17-
run.close()
24+
logger.info("This is a Simvue logging test")

examples/OpenFOAM/Dockerfile

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
FROM openfoam/openfoam10-paraview56
2+
ARG simvue_file
3+
USER root
4+
RUN add-apt-repository ppa:deadsnakes/ppa -y
5+
RUN apt-get update && \
6+
apt-get install -y python3.11-full
7+
RUN useradd simvue -m
8+
COPY . /simvue_client
9+
RUN python3.11 -m ensurepip --upgrade
10+
RUN python3.11 -m pip install /simvue_client
11+
RUN python3.11 -m pip install -r /simvue_client/examples/OpenFOAM/requirements.txt

examples/OpenFOAM/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ukaea-multiparser>=1.0.1

0 commit comments

Comments
 (0)