Skip to content

Commit edabadd

Browse files
authored
Merge pull request #15 from chahak13/cli
Add a CLI to the repo
2 parents 86b987a + 0f856db commit edabadd

16 files changed

Lines changed: 132 additions & 98 deletions

File tree

benchmarks/2d/__init__.py

Whitespace-only changes.

benchmarks/2d/uniaxial_nodal_forces/__init__.py

Whitespace-only changes.
Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,32 @@
11
import os
22
from pathlib import Path
33
import jax.numpy as jnp
4-
from diffmpm.solver import MPM
4+
from diffmpm import MPM
55

6-
curr_filepath = Path(__file__).absolute()
7-
curr_dir = curr_filepath.parent
8-
os.chdir(curr_dir)
9-
mpm = MPM("mpm-nodal-forces.toml")
10-
mpm.solve()
116

12-
result = jnp.load("results/uniaxial-nodal-forces/particles_0300.npz")
13-
## Step 300
14-
assert jnp.round(result["stress"][0, :, 0].min() - 0.5925210678182377, 6) == 0.0
15-
assert jnp.round(result["stress"][0, :, 0].max() - 0.5974539476363379, 6) == 0.0
7+
def test_benchmarks():
8+
curr_filepath = Path(__file__).absolute()
9+
curr_dir = curr_filepath.parent
10+
os.chdir(curr_dir)
11+
mpm = MPM("mpm-nodal-forces.toml")
12+
mpm.solve()
1613

17-
## Step 510
18-
result = jnp.load("results/uniaxial-nodal-forces/particles_0510.npz")
19-
assert jnp.round(result["stress"][0, :, 0].min() - 1.0026665338366039, 5) == 0.0
20-
assert jnp.round(result["stress"][0, :, 0].max() - 1.0112231542459431, 5) == 0.0
14+
result = jnp.load("results/uniaxial-nodal-forces/particles_0300.npz")
15+
## Step 300
16+
assert jnp.round(result["stress"][0, :, 0].min() - 0.5925210678182377, 6) == 0.0
17+
assert jnp.round(result["stress"][0, :, 0].max() - 0.5974539476363379, 6) == 0.0
2118

22-
## Step 750
23-
result = jnp.load("results/uniaxial-nodal-forces/particles_0750.npz")
24-
assert jnp.round(result["stress"][0, :, 0].min() - 1.0000053000532143, 5) == 0.0
25-
assert jnp.round(result["stress"][0, :, 0].max() - 1.0000225119807862, 5) == 0.0
19+
## Step 510
20+
result = jnp.load("results/uniaxial-nodal-forces/particles_0510.npz")
21+
assert jnp.round(result["stress"][0, :, 0].min() - 1.0026665338366039, 5) == 0.0
22+
assert jnp.round(result["stress"][0, :, 0].max() - 1.0112231542459431, 5) == 0.0
2623

27-
## Step 990
28-
result = jnp.load("results/uniaxial-nodal-forces/particles_0990.npz")
29-
assert jnp.round(result["stress"][0, :, 0].min() - 0.9999990078443788, 5) == 0.0
30-
assert jnp.round(result["stress"][0, :, 0].max() - 0.9999990292713694, 5) == 0.0
24+
## Step 750
25+
result = jnp.load("results/uniaxial-nodal-forces/particles_0750.npz")
26+
assert jnp.round(result["stress"][0, :, 0].min() - 1.0000053000532143, 5) == 0.0
27+
assert jnp.round(result["stress"][0, :, 0].max() - 1.0000225119807862, 5) == 0.0
28+
29+
## Step 990
30+
result = jnp.load("results/uniaxial-nodal-forces/particles_0990.npz")
31+
assert jnp.round(result["stress"][0, :, 0].min() - 0.9999990078443788, 5) == 0.0
32+
assert jnp.round(result["stress"][0, :, 0].max() - 0.9999990292713694, 5) == 0.0

benchmarks/2d/uniaxial_particle_traction/__init__.py

Whitespace-only changes.
Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,31 @@
11
import os
22
from pathlib import Path
33
import jax.numpy as jnp
4-
from diffmpm.solver import MPM
4+
from diffmpm import MPM
55

6-
curr_filepath = Path(__file__).absolute()
7-
curr_dir = curr_filepath.parent
8-
os.chdir(curr_dir)
9-
mpm = MPM("mpm-particle-traction.toml")
10-
mpm.solve()
11-
result = jnp.load("results/uniaxial-particle-traction/particles_0300.npz")
12-
## Step 300
13-
assert jnp.round(result["stress"][0, :, 0].min() - 0.4450086768966724, 5) == 0.0
14-
assert jnp.round(result["stress"][0, :, 0].max() - 0.5966527842046769, 5) == 0.0
156

16-
## Step 510
17-
result = jnp.load("results/uniaxial-particle-traction/particles_0510.npz")
18-
assert jnp.round(result["stress"][0, :, 0].min() - 0.7528092313640623, 5) == 0.0
19-
assert jnp.round(result["stress"][0, :, 0].max() - 1.0109599915279937, 5) == 0.0
7+
def test_benchmarks():
8+
curr_filepath = Path(__file__).absolute()
9+
curr_dir = curr_filepath.parent
10+
os.chdir(curr_dir)
11+
mpm = MPM("mpm-particle-traction.toml")
12+
mpm.solve()
13+
result = jnp.load("results/uniaxial-particle-traction/particles_0300.npz")
14+
## Step 300
15+
assert jnp.round(result["stress"][0, :, 0].min() - 0.4450086768966724, 5) == 0.0
16+
assert jnp.round(result["stress"][0, :, 0].max() - 0.5966527842046769, 5) == 0.0
2017

21-
## Step 750
22-
result = jnp.load("results/uniaxial-particle-traction/particles_0750.npz")
23-
assert jnp.round(result["stress"][0, :, 0].min() - 0.7500090055681591, 5) == 0.0
24-
assert jnp.round(result["stress"][0, :, 0].max() - 1.0000224746314728, 5) == 0.0
18+
## Step 510
19+
result = jnp.load("results/uniaxial-particle-traction/particles_0510.npz")
20+
assert jnp.round(result["stress"][0, :, 0].min() - 0.7528092313640623, 5) == 0.0
21+
assert jnp.round(result["stress"][0, :, 0].max() - 1.0109599915279937, 5) == 0.0
2522

26-
## Step 990
27-
result = jnp.load("results/uniaxial-particle-traction/particles_0990.npz")
28-
assert jnp.round(result["stress"][0, :, 0].min() - 0.750002924022295, 5) == 0.0
29-
assert jnp.round(result["stress"][0, :, 0].max() - 0.9999997782938734, 5) == 0.0
23+
## Step 750
24+
result = jnp.load("results/uniaxial-particle-traction/particles_0750.npz")
25+
assert jnp.round(result["stress"][0, :, 0].min() - 0.7500090055681591, 5) == 0.0
26+
assert jnp.round(result["stress"][0, :, 0].max() - 1.0000224746314728, 5) == 0.0
27+
28+
## Step 990
29+
result = jnp.load("results/uniaxial-particle-traction/particles_0990.npz")
30+
assert jnp.round(result["stress"][0, :, 0].min() - 0.750002924022295, 5) == 0.0
31+
assert jnp.round(result["stress"][0, :, 0].max() - 0.9999997782938734, 5) == 0.0

benchmarks/2d/uniaxial_stress/__init__.py

Whitespace-only changes.
Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
import os
22
from pathlib import Path
33
import jax.numpy as jnp
4-
from diffmpm.solver import MPM
4+
from diffmpm import MPM
55

6-
curr_filepath = Path(__file__).absolute()
7-
curr_dir = curr_filepath.parent
8-
os.chdir(curr_dir)
9-
mpm = MPM("mpm-uniaxial-stress.toml")
10-
mpm.solve()
116

12-
result = jnp.load("results/uniaxial-stress-usf/particles_09.npz")
13-
true_stress_yy = -1.0
14-
true_stress_xx = 0.0
7+
def test_benchmarks():
8+
curr_filepath = Path(__file__).absolute()
9+
curr_dir = curr_filepath.parent
10+
os.chdir(curr_dir)
11+
mpm = MPM("mpm-uniaxial-stress.toml")
12+
mpm.solve()
1513

16-
assert jnp.round(result["stress"][0, :, 1].max() - true_stress_yy, 8) == 0.0
17-
assert jnp.round(result["stress"][0, :, 0].max() - true_stress_xx, 8) == 0.0
14+
result = jnp.load("results/uniaxial-stress-usf/particles_09.npz")
15+
true_stress_yy = -1.0
16+
true_stress_xx = 0.0
17+
18+
assert jnp.round(result["stress"][0, :, 1].max() - true_stress_yy, 8) == 0.0
19+
assert jnp.round(result["stress"][0, :, 0].max() - true_stress_xx, 8) == 0.0

benchmarks/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

diffmpm/__init__.py

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,47 @@
1-
#!/usr/bin/env python3
1+
from importlib.metadata import version
2+
from pathlib import Path
3+
4+
import diffmpm.writers as writers
5+
from diffmpm.io import Config
6+
from diffmpm.solver import MPMExplicit
7+
8+
__all__ = ["MPM", "__version__"]
9+
10+
__version__ = version("diffmpm")
11+
12+
13+
class MPM:
14+
def __init__(self, filepath):
15+
self._config = Config(filepath)
16+
mesh = self._config.parse()
17+
out_dir = Path(self._config.parsed_config["output"]["folder"]).joinpath(
18+
self._config.parsed_config["meta"]["title"],
19+
)
20+
21+
write_format = self._config.parsed_config["output"].get("format", None)
22+
if write_format is None or write_format.lower() == "none":
23+
writer_func = None
24+
elif write_format == "npz":
25+
writer_func = writers.NPZWriter().write
26+
else:
27+
raise ValueError(f"Specified output format not supported: {write_format}")
28+
29+
if self._config.parsed_config["meta"]["type"] == "MPMExplicit":
30+
self.solver = MPMExplicit(
31+
mesh,
32+
self._config.parsed_config["meta"]["dt"],
33+
velocity_update=self._config.parsed_config["meta"]["velocity_update"],
34+
sim_steps=self._config.parsed_config["meta"]["nsteps"],
35+
out_steps=self._config.parsed_config["output"]["step_frequency"],
36+
out_dir=out_dir,
37+
writer_func=writer_func,
38+
)
39+
else:
40+
raise ValueError("Wrong type of solver specified.")
41+
42+
def solve(self):
43+
"""Solve the MPM simulation."""
44+
arrays = self.solver.solve_jit(
45+
self._config.parsed_config["external_loading"]["gravity"],
46+
)
47+
return arrays

diffmpm/cli/mpm.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import click
2+
3+
from diffmpm import MPM
4+
5+
6+
@click.command()
7+
@click.option(
8+
"-f", "--file", "filepath", required=True, type=str, help="Input TOML file"
9+
)
10+
@click.version_option(package_name="diffmpm")
11+
def mpm(filepath):
12+
solver = MPM(filepath)
13+
solver.solve()

0 commit comments

Comments
 (0)