|
| 1 | +# # 0D Circulation Model with Regazzoni2020 |
| 2 | +# This script demonstrates how to set up and solve a 0D circulation model using the Regazzoni2020 model |
| 3 | +# from the `circulation` module in the `pulse` package. |
| 4 | + |
| 5 | +from mpi4py import MPI |
| 6 | +import dolfinx |
| 7 | +import numpy as np |
| 8 | +import pulse |
| 9 | +import circulation |
| 10 | +import logging |
| 11 | +import matplotlib.pyplot as plt |
| 12 | + |
| 13 | +logging.basicConfig(level=logging.INFO) |
| 14 | +logging.getLogger("scifem").setLevel(logging.WARNING) |
| 15 | + |
| 16 | +comm = MPI.COMM_WORLD |
| 17 | +domain = dolfinx.mesh.create_unit_square(comm, 5, 5) |
| 18 | +circulation_model = circulation.regazzoni2020.Regazzoni2020() |
| 19 | +y0 = circulation_model.state.copy() |
| 20 | +dt = 0.001 |
| 21 | +theta = 0.5 |
| 22 | + |
| 23 | +problem = pulse.problem.StaticProblem( |
| 24 | + model=pulse.CardiacModel(), |
| 25 | + geometry=pulse.Geometry(mesh=domain), |
| 26 | + circulation_model=circulation_model, |
| 27 | + parameters={"0D": True, "dt": dt, "theta": theta}, |
| 28 | +) |
| 29 | + |
| 30 | +time = np.arange(0, 10, dt) |
| 31 | +y = np.zeros((len(y0), len(time))) |
| 32 | +y[:, 0] = y0 |
| 33 | + |
| 34 | +for i, ti in enumerate(time[1:]): |
| 35 | + if i % 100 == 0: |
| 36 | + print(f"Solving for time {ti:.3f} s") |
| 37 | + problem.solve(ti) |
| 38 | + y[:, i + 1] = problem.circ.x.array[:] |
| 39 | + |
| 40 | +state_names = circulation_model.state_names() |
| 41 | +var_names = circulation_model.var_names() |
| 42 | +vars = circulation_model.update_static_variables(time, y) |
| 43 | + |
| 44 | +fig, ax = plt.subplots(2, 2, sharex="col", sharey="col", figsize=(12, 8)) |
| 45 | +ax[0, 0].set_title("Pressures") |
| 46 | +ax[0, 0].plot(time, vars[var_names.index("p_LV"), :], label="p_LV") |
| 47 | +ax[0, 0].plot(time, vars[var_names.index("p_LA"), :], label="p_LA") |
| 48 | +ax[0, 0].plot(time, y[state_names.index("p_AR_SYS"), :], label="p_AR_SYS") |
| 49 | +ax[0, 0].plot(time, vars[var_names.index("p_RV"), :], label="p_RV") |
| 50 | +ax[0, 0].plot(time, vars[var_names.index("p_RA"), :], label="p_RA") |
| 51 | +ax[0, 0].legend() |
| 52 | + |
| 53 | +ax[1, 0].set_title("Volumes") |
| 54 | +ax[1, 0].plot(time, y[state_names.index("V_LA"), :], label="V_LA") |
| 55 | +ax[1, 0].plot(time, y[state_names.index("V_LV"), :], label="V_LV") |
| 56 | +ax[1, 0].plot(time, y[state_names.index("V_RV"), :], label="V_RV") |
| 57 | +ax[1, 0].plot(time, y[state_names.index("V_RA"), :], label="V_RA") |
| 58 | +ax[1, 0].legend() |
| 59 | + |
| 60 | +ax[0, 1].set_title("LV PV Loop") |
| 61 | +ax[0, 1].plot(y[state_names.index("V_LV"), :], vars[var_names.index("p_LV"), :]) |
| 62 | + |
| 63 | +ax[1, 1].set_title("RV PV Loop") |
| 64 | +ax[1, 1].plot(y[state_names.index("V_RV"), :], vars[var_names.index("p_RV"), :]) |
| 65 | + |
| 66 | +for axi in ax.flatten(): |
| 67 | + axi.grid() |
| 68 | +fig.savefig("circulation_regazzoni2020.png") |
0 commit comments