model.yaml 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. Model:
  2. superclasses: [Subsystem, Instantiation, Observables, Introspection]
  3. doc: |
  4. This is the main class that is used to assemble all simulation input
  5. and configuration. It also provides methods to do initialization,
  6. run simulation, and introspect the running simulation.
  7. examples: tests/pymcell4/1400_rel_site_for_each_it/model.py
  8. items:
  9. - name: config
  10. type: Config
  11. default: Config()
  12. doc: Simulation configuration.
  13. - name: warnings
  14. type: Warnings
  15. default: Warnings()
  16. doc: Configuration on how to report warnings.
  17. - name: notifications
  18. type: Notifications
  19. default: Notifications()
  20. doc: Configuration on how to report certain notifications.
  21. methods:
  22. - name: initialize
  23. doc: |
  24. Initializes model, initialization blocks most of changes to
  25. contained components.
  26. params:
  27. - name: print_copyright
  28. type: bool
  29. default: True
  30. doc: Prints information about MCell.
  31. - name: run_iterations
  32. doc: |
  33. Runs specified number of iterations. Returns the number of iterations
  34. executed (it might be less than the requested number of iterations when
  35. a checkpoint was scheduled).
  36. return_type: uint64
  37. params:
  38. - name: iterations
  39. type: float
  40. doc: Number of iterations to run. Value is truncated to an integer.
  41. - name: end_simulation
  42. doc: |
  43. Generates the last visualization and reaction output (if they are included
  44. in the model), then flushes all buffers and optionally prints simulation report.
  45. Buffers are also flushed when the Model object is destroyed such as when Ctrl-C
  46. is pressed during simulation.
  47. params:
  48. - name: print_final_report
  49. type: bool
  50. default: True
  51. doc: Print information on simulation time and counts of selected events.
  52. - name: add_subsystem
  53. doc: Adds all components of a Subsystem object to the model.
  54. params:
  55. - name: subsystem
  56. type: Subsystem*
  57. - name: add_instantiation
  58. doc: Adds all components of an Instantiation object to the model.
  59. params:
  60. - name: instantiation
  61. type: Instantiation*
  62. - name: add_observables
  63. doc: Adds all counts and viz outputs of an Observables object to the model.
  64. params:
  65. - name: observables
  66. type: Observables*
  67. - name: dump_internal_state
  68. doc: Prints out the simulation engine's internal state, mainly for debugging.
  69. params:
  70. - name: with_geometry
  71. type: bool
  72. default: false
  73. doc: Include geometry in the dump.
  74. - name: export_data_model
  75. doc: |
  76. Exports the current state of the model into a data model JSON format.
  77. Does not export state of molecules.
  78. Must be called after model initialization.
  79. Always exports the current state, i.e. with the current geometry and reaction rates.
  80. Events (ReleaseSites and VizOutputs) with scheduled time other than zero are not exported correctly yet.
  81. params:
  82. - name: file
  83. type: str
  84. default: unset
  85. doc: |
  86. If file is not set, then uses the first VizOutput to determine the target directory
  87. and creates name using the current iteration. Fails if argument file is not set and
  88. there is no VizOutput in the model.
  89. - name: export_viz_data_model
  90. doc: |
  91. Same as export_data_model, only the created data model will contain only information required for visualization
  92. in CellBlender. This makes the loading of the model by CellBlender faster and also allows to avoid potential
  93. compatibility issues.
  94. Must be called after model initialization.
  95. examples: tests/pymcell4_positive/1520_sphere_collision/model.py
  96. params:
  97. - name: file
  98. type: str
  99. default: unset
  100. doc: Optional path to the output data model file.
  101. - name: export_geometry
  102. doc: |
  103. Exports model geometry as Wavefront OBJ format.
  104. Must be called after model initialization.
  105. Does not export material colors (yet).
  106. params:
  107. - name: output_files_prefix
  108. type: str
  109. default: unset
  110. doc: |
  111. Optional prefix for .obj and .mtl files that will be created on export.
  112. If output_files_prefix is not set, then uses the first VizOutput to determine the target directory
  113. and creates names using the current iteration. Fails if argument output_files_prefix is not set and
  114. there is no VizOutput in the model.
  115. # --- state modification ---
  116. - name: release_molecules
  117. doc: |
  118. Performs immediate release of molecules based on the definition of the release site argument.
  119. The ReleaseSite.release_time must not be in the past and must be within the current iteration
  120. meaning that the time must be greater or equal iteration * time_step and less than (iteration + 1) * time_step.
  121. The ReleaseEvent must not use a release_pattern because this is an immediate release and it is not
  122. scheduled into the global scheduler.
  123. examples: tests/pymcell4/2300_immediate_release/model.py
  124. params:
  125. - name: release_site
  126. type: ReleaseSite*
  127. - name: run_reaction
  128. doc: |
  129. Run a single reaction on reactants. Callbacks will be called if they are registered for the given reaction.
  130. Returns a list of product IDs.
  131. Note\: only unimolecular reactions are currently supported.
  132. examples: tests/pymcell4_positive/1850_run_unimol_rxn_in_callback/model.py
  133. return_type: List[int]
  134. params:
  135. - name: reaction_rule
  136. type: ReactionRule*
  137. doc: Reaction rule to run.
  138. - name: reactant_ids
  139. type: List[int]
  140. doc: |
  141. The number of reactants for a unimolecular reaction must be 1 and for a bimolecular reaction must be 2.
  142. Reactants for a bimolecular reaction do not have to be listed in the same order as in the reaction rule definition.
  143. - name: time
  144. type: float
  145. doc: |
  146. Precise time in seconds when this reaction occurs. Important to know for how long the products
  147. will be diffused when they are created in a middle of a time step.
  148. # --- dynamic geometry ---
  149. - name: add_vertex_move
  150. doc: |
  151. Appends information about a displacement for given object's vertex into an internal list of vertex moves.
  152. To do the actual geometry change, call Model.apply_vertex_moves.
  153. The reason why we first need to collect all changes and then apply them all at the same time is for performance
  154. reasons.
  155. examples: tests/pymcell4_positive/1510_tetrahedron_box_collision_moving_3_verts/model.py
  156. params:
  157. - name: object
  158. type: GeometryObject*
  159. doc: Object whose vertex will be changed.
  160. - name: vertex_index
  161. type: int
  162. doc: Index of vertex in object's vertex list that will be changed.
  163. - name: displacement
  164. type: List[float]
  165. doc: |
  166. Change of vertex coordinates [x, y, z] (in um) that will be added to the current
  167. coordinates of the vertex.
  168. - name: apply_vertex_moves
  169. doc: |
  170. Applies all the vertex moves specified with Model.add_vertex_move call.
  171. All affected vertices are first divided based on to which geometery object they belong.
  172. Then each object is manipulated one by one.
  173. During vertex moves, collisions are checked\:
  174. a) When a moved vertex hits a wall of another object, it is stopped at the wall.
  175. b) When a second object's vertex would end up inside the moved object, the vertex move
  176. that would cause it is canceled (its displacement set to 0) because finding the maximum
  177. distance we can move is too computationally expensive. To minimize the impact of this
  178. cancellation, the vertices should be moved only by a small distance.
  179. Applying vertex moves also takes paired molecules into account\:
  180. When moves are applied to an object, all moved molecules that are paired are collected.
  181. For each of the paired molecules, we collect displacements for each
  182. of the vertices of the 'primary' wall where this molecule is located (that were provided by the user
  183. through add_vertex_move, and were possibly truncated due to collisions).
  184. Then we find the second wall where the second molecule of the pair is located.
  185. For each of the vertices of all 'secondary' walls, we collect a list of displacements
  186. that move the vertices of 'primary' walls.
  187. Then, an average displacement is computed for each vertex, and these average displacements
  188. are used to move the 'secondary' walls.
  189. When a 'primary' wall collides, its displacement is clamped or canceled. This is true even if
  190. it collides with a 'secondary' wall that would be otherwise moved. So, the displacement of the
  191. 'primary' wall will mostly just pull the 'secondary' wall, not push. Therefore it is needed
  192. that both objects are active and pull each other.
  193. This process is well commented in MCell code\:
  194. `partition.cpp <https://github.com/mcellteam/mcell/blob/master/src4/partition.cpp>`_ in functions
  195. apply_vertex_moves, apply_vertex_moves_per_object, and move_walls_with_paired_molecules.
  196. When argument collect_wall_wall_hits is True, a list of wall pairs that collided is returned,
  197. when collect_wall_wall_hits is False, an empty list is returned.
  198. examples: |
  199. tests/pymcell4_positive/1510_tetrahedron_box_collision_moving_3_verts/model.py
  200. tests/pymcell4/3200_sphere_collision_against_each_other/model.py
  201. tests/pymcell4/3150_dyn_vert_intramembrane_rxns_and_paired_mols/model.py
  202. return_type: List[WallWallHitInfo*]
  203. params:
  204. - name: collect_wall_wall_hits
  205. type: bool
  206. default: false
  207. doc: |
  208. When set to True, a list of wall pairs that collided is returned,
  209. otherwise an empty list is returned.
  210. - name: randomize_order
  211. type: bool
  212. default: true
  213. doc: |
  214. When set to True (default), the ordering of the vertex move list created by add_vertex_move
  215. calls is randomized. This allows to avoid any bias in the resulting positions of surface
  216. molecules.
  217. However, the individual vertex moves are then sorted by the object to which the vertex belongs
  218. and the moves are applied object by object for correctness. Setting this to True also radomizes the
  219. order of objects to which the vertex moves are applied.
  220. # --- molecule pairing (used by dynamic geometry) ---
  221. - name: pair_molecules
  222. doc: |
  223. Sets that two surface molecules are paired. Paired molecules bind walls together
  224. and when one wall is moved, the wall that is bound through a paired molecule is moved as well.
  225. Throws exception if the molecule ids are not surface molecules.
  226. Throws exception if the molecules are on the same object.
  227. Throws exception if any of the molecules is already paired.
  228. May be called only after model initialization.
  229. examples: tests/nutmeg4_pymcell4/2900_pair_unpair_molecules/model.py tests/pymcell4_positive/3160_dyn_vert_paired_mols_box_box/model.py
  230. params:
  231. - name: id1
  232. type: int
  233. - name: id2
  234. type: int
  235. - name: unpair_molecules
  236. doc: |
  237. Sets that two surface molecules are not paired.
  238. Throws exception if the molecule ids are not surface molecules.
  239. Throws exception if the molecules are not paired together.
  240. May be called only after model initialization.
  241. examples: tests/nutmeg4_pymcell4/2900_pair_unpair_molecules/model.py
  242. params:
  243. - name: id1
  244. type: int
  245. - name: id2
  246. type: int
  247. - name: get_paired_molecule
  248. doc: |
  249. Return id of the molecule to which the molecule with 'id' is paired.
  250. Returns ID_INVALID (-1) when the molecule is not paired.
  251. May be called only after model initialization.
  252. examples: tests/nutmeg4_pymcell4/2900_pair_unpair_molecules/model.py
  253. return_type: int
  254. params:
  255. - name: id
  256. type: int
  257. - name: get_paired_molecules
  258. examples: tests/pymcell4_positive/3170_get_paired_molecules/model.py
  259. doc: |
  260. Returns a dictionary that contains all molecules that are paired.
  261. Molecule ids are keys and the value associated with the key is the second paired molecule.
  262. The returned dictionary is a copy and any changes made to it are ignored by MCell.
  263. Note\: The reason why uint32 is used as the base type for the dictionary but type int is used
  264. everywhere else for molecule ids is only for performance reasons.
  265. return_type: Dict[uint32, uint32]
  266. # --- callbacks ---
  267. - name: register_mol_wall_hit_callback
  268. doc: |
  269. Register a callback for event when a molecule hits a wall.
  270. May be called only after model initialization because it internally uses geometry object
  271. and species ids that are set during the initialization.
  272. examples: tests/pymcell4_positive/1300_wall_hit_callback/model.py
  273. params:
  274. - name: function
  275. type: std::function<void(std::shared_ptr<MolWallHitInfo>, py::object)>
  276. doc: |
  277. Callback function to be called.
  278. The function must have two arguments MolWallHitInfo and context.
  279. Do not modify the received MolWallHitInfo object since it may be reused for other
  280. wall hit callbacks (e.g. when the first callback is for a specific geometry object and
  281. the second callback is for any geometry object).
  282. The context object (py::object type argument) is on the other hand provided
  283. to be modified and one can for instance use it to count the number of hits..
  284. - name: context
  285. type: py::object
  286. doc: |
  287. Context passed to the callback function, the callback function can store
  288. information to this object. Some context must be always passed, even when
  289. it is a useless python object.
  290. - name: object
  291. type: GeometryObject*
  292. default: unset
  293. doc: Only hits of this object will be reported, any object hit is reported when not set.
  294. internal: Extend this to Region later
  295. - name: species
  296. type: Species*
  297. default: unset
  298. doc: |
  299. Only hits of molecules of this species will be reported, any hit of volume molecules of
  300. any species is reported when this argument is not set.
  301. Sets an internal flag for this species to make sure that the species id does not change
  302. during simulation.
  303. - name: register_reaction_callback
  304. doc: |
  305. Defines a function to be called when a reaction was processed.
  306. It is allowed to do state modifications except for removing reacting molecules,
  307. they will be removed automatically after return from this callback.
  308. Unlimited number of reaction callbacks is allowed.
  309. May be called only after model initialization because it internally uses
  310. reaction rule ids that are set during the initialization.
  311. examples: tests/pymcell4_positive/1800_vol_rxn_callback/model.py
  312. params:
  313. - name: function
  314. type: std::function<bool(std::shared_ptr<ReactionInfo>, py::object)>
  315. doc: |
  316. Callback function to be called.
  317. The function must have two arguments ReactionInfo and context.
  318. Called right after a reaction occured but before the reactants were removed.
  319. It is also allowed to return a boolean value from the callbck function.
  320. If False or None is returned the reaction proceeds and reactants are removed
  321. (unless they were kept by the reaction such as with reaction A + B -> A + C).
  322. If True is returned, the reaction is cancelled, reactants are kept and products
  323. are removed.
  324. No return is needed in the callback function since Python
  325. automatically returns None that is cast to False.
  326. - name: context
  327. type: py::object
  328. doc: |
  329. Context passed to the callback function, the callback function can store
  330. information to this object. Some context must be always passed, even when
  331. it is a useless python object.
  332. internal: |
  333. In the future, One may set attributes of ReactionInfo to say how the reaction should proceed.
  334. If unchanged, reaction proceeds as it woudl without this callback.
  335. - name: reaction_rule
  336. type: ReactionRule*
  337. doc: The callback function will be called whenever this reaction rule is applied.
  338. internal: maybe also add filtering by species
  339. # --- other ---
  340. - name: load_bngl
  341. doc: |
  342. Loads sections\: molecule types, reaction rules, seed species, and observables from a BNGL file
  343. and creates objects in the current model according to it.
  344. All elementary molecule types used in the seed species section must be defined in subsystem.
  345. If an item in the seed species section does not have its compartment set,
  346. the argument default_region must be set and the molecules are released into or onto the
  347. default_region.
  348. examples: pymcell4/1400_rel_site_for_each_it/model.py
  349. params:
  350. - name: file_name
  351. type: str
  352. doc: Path to the BNGL file to be loaded.
  353. - name: observables_path_or_file
  354. type: str
  355. default: unset
  356. doc: |
  357. Directory prefix or file name where observable values will be stored.
  358. If a directory such as './react_data/seed_' + str(SEED).zfill(5) + '/' or an empty
  359. string/unset is used, each observable gets its own file and the output file format for created Count
  360. objects is CountOutputFormat.DAT.
  361. When not set, this path is used: './react_data/seed_' + str(model.config.seed).zfill(5) + '/'.
  362. If a file has a .gdat extension such as
  363. './react_data/seed_' + str(SEED).zfill(5) + '/counts.gdat', all observable are stored in this
  364. file and the output file format for created Count objects is CountOutputFormat.GDAT.
  365. Must not be empty when observables_output_format is explicitly set to CountOutputFormat.GDAT.
  366. - name: default_release_region
  367. type: Region*
  368. default: unset
  369. doc: |
  370. Used as region for releases for seed species that have no compartments specified.
  371. - name: parameter_overrides
  372. type: Dict[str, float]
  373. default: empty
  374. doc: |
  375. For each key k in the parameter_overrides, if it is defined in the BNGL's parameters section,
  376. its value is ignored and instead value parameter_overrides[k] is used.
  377. - name: observables_output_format
  378. type: CountOutputFormat
  379. default: CountOutputFormat.AUTOMATIC_FROM_EXTENSION
  380. doc: |
  381. Selection of output format. Default setting uses automatic detection
  382. based on contents of the 'observables_path_or_file' attribute.
  383. - name: export_to_bngl
  384. doc: |
  385. Exports all defined species, reaction rules and applicable observables
  386. as a BNGL file that can be then loaded by MCell4 or BioNetGen.
  387. The resulting file should be validated that it produces expected results.
  388. Many MCell features cannot be exported into BNGL and when such a feature is
  389. encountered the export fails with a RuntimeError exception.
  390. However, the export code tries to export as much as possible and one can catch
  391. the RuntimeError exception and use the possibly incomplete BNGL file anyway.
  392. params:
  393. - name: file_name
  394. type: str
  395. doc: Output file name.
  396. - name: simulation_method
  397. type: BNGSimulationMethod
  398. default: BNGSimulationMethod.ODE
  399. doc: |
  400. Selection of the BioNetGen simulation method.
  401. Selects BioNetGen action to run with the selected simulation method.
  402. For BNGSimulationMethod.NF the export is limited to a single volume and
  403. a single surface and the enerated rates use volume and surface area so that
  404. simulation with NFSim produces corect results.
  405. - name: save_checkpoint
  406. todo: provide more configurability for parameters
  407. doc: |
  408. Saves current model state as checkpoint.
  409. The default directory structure is checkpoints/seed_<SEED>/it_<ITERATION>,
  410. it can be changed by setting 'custom_dir'.
  411. If used during an iteration such as in a callback, an event is scheduled for the
  412. beginning of the next iteration. This scheduled event saves the checkpoint.
  413. examples: tests/pymcell4_positive/2700_save_checkpoint_rxn_in_box/model.py
  414. params:
  415. - name: custom_dir
  416. type: str
  417. default: unset
  418. doc: |
  419. Sets custom directory where the checkpoint will be stored.
  420. The default is 'checkpoints/seed_<SEED>/it_<ITERATION>'.
  421. - name: schedule_checkpoint
  422. todo: provide more configurability for parameters
  423. doc: |
  424. Schedules checkpoint save event that will occur when an iteration is started.
  425. This means that it will be executed right before any other events scheduled for
  426. the given iteration are executed.
  427. Can be called asynchronously at any time after initialization.
  428. examples: tests/nutmeg4_pymcell4/2760_schedule_checkpoint_async_w_timer/model.py
  429. params:
  430. - name: iteration
  431. type: uint64
  432. default: 0
  433. todo: shouldn't we specify simulated time instead?
  434. doc: |
  435. Specifies iteration number when the checkpoint save will occur.
  436. Please note that iterations are counted from 0.
  437. To schedule a checkpoint for the closest time as possible, keep the default value 0,
  438. this will schedule checkpoint for the beginning of the iteration with number current iteration + 1.
  439. If calling schedule_checkpoint from a different thread (e.g. by using threading.Timer),
  440. it is highly recommended to keep the default value 0 or choose some time that will be
  441. for sure in the future.
  442. - name: continue_simulation
  443. type: bool
  444. default: false
  445. doc: |
  446. When false, saving the checkpoint means that we want to terminate the simulation
  447. right after the save. The currently running function Model.run_iterations
  448. will not simulate any following iterations and execution will return from this function
  449. to execute the next statement which is usually 'model.end_simulation()'.
  450. When true, the checkpoint is saved and simulation continues uninterrupted.
  451. - name: custom_dir
  452. type: str
  453. default: unset
  454. doc: |
  455. Sets custom directory where the checkpoint will be stored.
  456. The default is 'checkpoints/seed_<SEED>/it_<ITERATION>'.