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