|
| 1 | +import os |
| 2 | +import tempfile |
| 3 | +from pathlib import Path |
| 4 | + |
| 5 | +import numpy as np |
| 6 | + |
| 7 | +from pyvcell.sim_results.var_types import NDArray3D |
| 8 | +from pyvcell.vcml import VcmlReader |
| 9 | +from pyvcell.vcml.field import Field |
| 10 | +from pyvcell.vcml.vcml_simulation import VcmlSpatialSimulation as Solver |
| 11 | + |
| 12 | + |
| 13 | +def create_sinusoid(shape: tuple[int, ...], freq: float) -> NDArray3D: |
| 14 | + indices = np.indices(shape) |
| 15 | + sinusoid: NDArray3D = ( |
| 16 | + np.cos(freq * indices[0, :, :, :]) * np.sin(freq * indices[1, :, :, :]) * np.sin(freq * indices[2, :, :, :]) |
| 17 | + ) |
| 18 | + return sinusoid.astype(dtype=np.float64) |
| 19 | + |
| 20 | + |
| 21 | +with tempfile.TemporaryDirectory() as temp_dir_name, Path(temp_dir_name) as temp_dir: |
| 22 | + print(f"temp_dir: {temp_dir}, exists={temp_dir.exists()}") |
| 23 | + # ----- make a workspace |
| 24 | + workspace_dir = temp_dir / "workspace" |
| 25 | + workspace_dir.mkdir(parents=True, exist_ok=True) |
| 26 | + print(f"workspace_dir: {workspace_dir}, exists={workspace_dir.exists()}") |
| 27 | + sim_dir = workspace_dir / "sim1_dir" |
| 28 | + sim_dir.mkdir(parents=True, exist_ok=True) |
| 29 | + |
| 30 | + print(f"sim_dir: {sim_dir}, exists={sim_dir.exists()}") |
| 31 | + |
| 32 | + # ---- read in VCML file |
| 33 | + model_path = Path(os.getcwd()).parent / "models" / "SmallSpatialProject_3D.vcml" |
| 34 | + bio_model = VcmlReader.biomodel_from_file(vcml_path=model_path) |
| 35 | + |
| 36 | + # ---- get the species mappings for species "s0" and "s1" |
| 37 | + app = bio_model.applications[0] |
| 38 | + s1_mapping = next(s for s in app.species_mappings if s.species_name == "s1") |
| 39 | + s0_mapping = next(s for s in app.species_mappings if s.species_name == "s0") |
| 40 | + |
| 41 | + # ---- set initial concentrations to reference external field data |
| 42 | + s0_mapping.init_conc = "vcField('test2_lsm_DEMO', 'species0_cyt', 0.5, 'Volume')" |
| 43 | + s1_mapping.init_conc = "vcField('checkerboard', 'v', 0.0, 'Volume')" |
| 44 | + |
| 45 | + sim = app.add_sim(name="new_sim", duration=10.0, output_time_step=0.1, mesh_size=(20, 20, 20)) |
| 46 | + fields = Field.create_fields(bio_model=bio_model, sim=sim) |
| 47 | + print(fields) |
| 48 | + |
| 49 | + shape = fields[0].data_nD.shape |
| 50 | + fields[0].data_nD = np.multiply(create_sinusoid(shape=shape, freq=0.5), 8.0) |
| 51 | + fields[1].data_nD = np.multiply(create_sinusoid(shape=shape, freq=0.3), 4.0) |
| 52 | + |
| 53 | + # ---- add field data to the simulation |
| 54 | + |
| 55 | + sim1_result = Solver(bio_model=bio_model, out_dir=sim_dir, fields=fields).run(sim.name) |
| 56 | + print([c.label for c in sim1_result.channel_data]) |
| 57 | + print(sim1_result.time_points[::11]) |
| 58 | + sim1_result.plotter.plot_slice_3d(time_index=0, channel_id="s0") |
| 59 | + sim1_result.plotter.plot_slice_3d(time_index=0, channel_id="s1") |
| 60 | + sim1_result.plotter.plot_concentrations() |
0 commit comments