|
| 1 | +import os |
| 2 | +import shutil |
| 3 | +from pathlib import Path |
| 4 | + |
| 5 | +from pyvcell.vcml import Simulation, VcmlReader |
| 6 | +from pyvcell.vcml.vcml_simulation import VcmlSpatialSimulation as Solver |
| 7 | + |
| 8 | +# ----- make a workspace |
| 9 | +workspace_dir = Path(os.getcwd()) / "workspace" |
| 10 | +sim1_dir = workspace_dir / "sim1_dir" |
| 11 | +if sim1_dir.exists(): |
| 12 | + shutil.rmtree(sim1_dir) |
| 13 | +sim2_dir = workspace_dir / "sim2_dir" |
| 14 | +if sim2_dir.exists(): |
| 15 | + shutil.rmtree(sim2_dir) |
| 16 | + |
| 17 | +# ---- read in VCML file |
| 18 | +model_fp = Path(os.getcwd()).parent / "models" / "SmallSpatialProject_3D.vcml" |
| 19 | +bio_model1 = VcmlReader.biomodel_from_file(model_fp) |
| 20 | + |
| 21 | +# ---- get the application and the species mappings for species "s0" and "s1" |
| 22 | +app = bio_model1.applications[0] |
| 23 | +s1_mapping = next(s for s in app.species_mappings if s.species_name == "s1") |
| 24 | +s0_mapping = next(s for s in app.species_mappings if s.species_name == "s0") |
| 25 | + |
| 26 | +# ---- add a simulation to the first application in the biomodel (didn't already have a simulation in the VCML file) |
| 27 | +new_sim = Simulation(name="new_sim", duration=10.0, output_time_step=0.1, mesh_size=(20, 20, 20)) |
| 28 | +app.simulations.append(new_sim) |
| 29 | + |
| 30 | +# ---- set the initial concentration of species "s0" and "s1" in the first application |
| 31 | +s0_mapping.init_conc = "3+sin(x)+cos(y)+sin(z)" |
| 32 | +s1_mapping.init_conc = "3+sin(x+y+z)" |
| 33 | + |
| 34 | +# ---- run simulation, store in sim1_dir, and plot results |
| 35 | +# >>>>> This forms the data for the "Field Data" identified by 'sim1_dir' <<<<<< |
| 36 | +sim1_result = Solver(bio_model=bio_model1, out_dir=sim1_dir).run(new_sim.name) |
| 37 | +print([c.label for c in sim1_result.channel_data]) |
| 38 | +print(sim1_result.time_points[::11]) |
| 39 | +sim1_result.plotter.plot_slice_3d(time_index=0, channel_id="s0") |
| 40 | +sim1_result.plotter.plot_slice_3d(time_index=0, channel_id="s1") |
| 41 | +sim1_result.plotter.plot_concentrations() |
| 42 | + |
| 43 | + |
| 44 | +# ----- use field data from sim1_dir to set initial concentration of species "s0" |
| 45 | +s0_mapping.init_conc = "vcField('sim1_dir','s0',0.0,'Volume') * vcField('sim1_dir','s1',0.0,'Volume')" |
| 46 | +s1_mapping.init_conc = "5.0" |
| 47 | +# ---- re-run simulation and store in sim2_dir |
| 48 | +# note that the solution of s0 draws from the data from sim1_dir |
| 49 | +sim2_result = Solver(bio_model=bio_model1, out_dir=sim2_dir).run(new_sim.name) |
| 50 | +sim2_result.plotter.plot_slice_3d(time_index=0, channel_id="s0") |
| 51 | +sim2_result.plotter.plot_slice_3d(time_index=0, channel_id="s1") |
| 52 | +sim2_result.plotter.plot_concentrations() |
0 commit comments