Skip to content

Add script for running stiffness sensitivty #259

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions demos/stiffness_sensitivity.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# # Simple demo
#
# In this demo we show the most simple usage of the `simcardems` library using the python API.
#
# Import the necessary libraries
#

from pathlib import Path
import simcardems
import matplotlib.pyplot as plt
import numpy as np

here = Path(__file__).absolute().parent
geometry_path = here / "geometries/slab.h5"
geometry_schema_path = here / "geometries/slab.json"


def run(outdir: Path, a: float = 2.28):
outdir.mkdir(exist_ok=True, parents=True)
results_file = outdir.joinpath("results.h5")
if results_file.exists():
values = np.load(outdir.joinpath("values.npy"), allow_pickle=True).item()

t = values["time"]
lmbda = values["mechanics"]["lambda"]
return t, lmbda
config = simcardems.Config(
outdir=outdir,
geometry_path=geometry_path,
geometry_schema_path=geometry_schema_path,
coupling_type="fully_coupled_ORdmm_Land",
T=1000,
material_parameter_a=a,
)
runner = simcardems.Runner(config)
runner.solve(T=config.T, save_freq=config.save_freq, show_progress_bar=True)

loader = simcardems.datacollector.DataLoader(results_file)
values = simcardems.postprocess.extract_traces(loader, reduction="center")
np.save(outdir.joinpath("values.npy"), values)

t = values["time"]
lmbda = values["mechanics"]["lambda"]
fig, ax = plt.subplots()
ax.plot(t, lmbda)
ax.set_xlabel("Time [ms]")
ax.set_ylabel("Lambda")
ax.set_title(f"Material parameter a = {a}")
fig.savefig(outdir.joinpath(f"lambda_{a}.png"))

return t, lmbda
# simcardems.postprocess.plot_state_traces(outdir.joinpath("results.h5"), "center")


def main():
outdir = here / "results_stiffness_sensitivity-new"
fig, ax = plt.subplots()
a_default = 2.28
for a in [
a_default / 4.0,
a_default / 2.0,
a_default,
a_default * 2.0,
a_default * 4.0,
]:
t, lmbda = run(outdir=outdir / f"a_{a}", a=a)
ax.plot(t, lmbda, label=f"a = {a}")
ax.legend()
ax.set_xlabel("Time [ms]")
ax.set_ylabel("Lambda")
ax.set_title("Stiffness sensitivity")
fig.savefig(outdir / "stiffness_sensitivity.png")


if __name__ == "__main__":
main()
1 change: 1 addition & 0 deletions src/simcardems/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class Config:
"explicit_ORdmm_Land",
"pureEP_ORdmm_Land",
] = "fully_coupled_ORdmm_Land"
material_parameter_a: float = 2.28

def as_dict(self):
return {k: v for k, v in self.__dict__.items()}
Expand Down
3 changes: 2 additions & 1 deletion src/simcardems/mechanics_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def setup_solver(
linear_solver="mumps",
use_custom_newton_solver: bool = config.Config.mechanics_use_custom_newton_solver,
state_prev=None,
material_parameter_a: float = 2.28,
):
"""Setup mechanics model with dirichlet boundary conditions or rigid motion."""

Expand All @@ -43,7 +44,7 @@ def setup_solver(

# Use parameters from Biaxial test in Holzapfel 2019 (Table 1).
material_parameters = dict(
a=2.28,
a=material_parameter_a,
a_f=1.686,
b=9.726,
b_f=15.779,
Expand Down
1 change: 1 addition & 0 deletions src/simcardems/models/em_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ def setup_EM_model(
use_custom_newton_solver=config.mechanics_use_custom_newton_solver,
debug_mode=config.debug_mode,
ActiveModel=cls_ActiveModel,
material_parameter_a=config.material_parameter_a,
)
if mech_state_init is not None:
mech_heart.state.assign(mech_state_init)
Expand Down