<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Tutorial 1 (Model)
This is a tutorial for E-Cell4. Here, we represent the way of modeling with E-Cell 4.
Species
We'll show you how to create Species.
Step1:  each expression describes a Species named A or B.
A describes not a specific molecule, but a type of molecule.
Caution
Species name(we call this Serial) has a number of naming rules,
The naming requires attention to use special symbol '()', '.', '_', numbers, and space.
<hr>
You can add attributes to Species,
Step2:  The 1st argument for set_attribute is the name of attribute.
The 2nd argument is the value.
Both have got to be string.
Radius, diffusion coefficient, location are frequently used, so there is a shortcut for this.
Step3:  When you want to inspect the Species attributes, please write as follows.
Step4:  ReactionRule
ReactionRule describes the transition of the molecule types from Reactants to Products. ReactionRule requires at least a kinetic rate attribute, this has to be number.
Step5:  Now you created a reaction from A to B. In this reaction definition, you don't need to assign values to Species.
You can also create a binding reaction as follows
Step6:  A Binding A and B creates C. There are utility functions for binding and unbinding ReactionRules.
Step7:  When you want to inspect the ReactionRule, please use as_string function.
Step8:  NetworkModel
Now you have created components for Model, next we register these components on Model.
Step9:  To add a Species, use add_species_attribute.
To add a ReactionRule, use add_reaction_rule.
Now you created a Model for simple association and dissociation.
To inspect a Model, use species_attributes, reaction_rules, and num_reaction_rules().
Step10:  NetworkModel also contains Species attributes. These attributes are indispensable for particle and lattice simulations, but not necessarily needed for gillespie and ode.
NetworkModel attributes a Species based on the registered Species.
A simple simulation with E-Cell4
Yet we have not explained World and Simulator, we show you a simple simulation result.
E-Cell4 has a utility function named run_simulation for simple demo.
This function runs on an environment installed IPython Notebook and matplotlib.
Step11:  run_simulation records the number of molecules for time t and plots the time-series simulation result.
In this case, we recorded the simulation for 10 seconds per 0.1 second.
The second argument is initial value. We started the simulation from the 60 C molecules.
volume is the volume for this simulation, and we set a network model to model.
You can also plot stochastic simulation result.
Step12:  You can simalate a model with different methods like this.
E-Cell system completely separates the model and simulation methods.
Special model notation in E-Cell4
We created Species and ReactionRule, then set them to NetworkModel and simulated it.
But the model description is cumbersome even in simple association and dissociation.
So E-Cell4 has shortcuts to describe this model.
Here we show a shortcut about ReactionRule.
Before using the shortcut, please remove the reactant Species A and B we created in global scope above.
(You don't need to remove the product C)
Step13:  Please use with statement for E-Cell4 special notation.
You can use special notation under this with scope.
Please remember to write () after reaction_rules.
The syntax speaks for itself. The number after separator | is kinetic constant.
The syntax must be valid Python, so take care in using line breaks.
If you do NOT del A and del B, A + B is expanded to Species("A") + Species("B").
This causes an error like TypeError
Step14:  $$\frac{\mathrm{d[A]}}{\mathrm{d}t}=\frac{\mathrm{d[B]}}{\mathrm{d}t}=-0.01\mathrm{[A][B]}+0.3\mathrm{[C]}\
\frac{\mathrm{d[C]}}{\mathrm{d}t}=+0.01\mathrm{[A][B]}-0.3\mathrm{[C]}$$
In reversible reaction, you need to set two kinetic constant after the separator.
When you do NOT set the model to run_simulation function, run_simulation calls get_model() automatically. So in this case we skipped.
Notations about synthesis and degradation
In the absence of left or right side of molecules like synthesis or degradation,
you may write the model like
python
with reaction_rules()
<ASSISTANT_TASK:>
Python Code:
from ecell4 import *
A = Species("A")
B = Species("B")
Explanation: Tutorial 1 (Model)
This is a tutorial for E-Cell4. Here, we represent the way of modeling with E-Cell 4.
Species
We'll show you how to create Species.
End of explanation
A = Species("A")
A.set_attribute("radius", "0.005")
A.set_attribute("D", "1")
A.set_attribute("location", "cytoplasm")
Explanation: each expression describes a Species named A or B.
A describes not a specific molecule, but a type of molecule.
Caution
Species name(we call this Serial) has a number of naming rules,
The naming requires attention to use special symbol '()', '.', '_', numbers, and space.
<hr>
You can add attributes to Species,
End of explanation
A = Species("A", "0.005", "1", "cytoplasm")  # XXX: serial, radius, D, location
Explanation: The 1st argument for set_attribute is the name of attribute.
The 2nd argument is the value.
Both have got to be string.
Radius, diffusion coefficient, location are frequently used, so there is a shortcut for this.
End of explanation
print(A.serial())  # will return 'A'
print(A.get_attribute("D"))  # will return '1'
Explanation: When you want to inspect the Species attributes, please write as follows.
End of explanation
rr = ReactionRule()
rr.add_reactant(Species("A"))
rr.add_product(Species("B"))
rr.set_k(1.0)
Explanation: ReactionRule
ReactionRule describes the transition of the molecule types from Reactants to Products. ReactionRule requires at least a kinetic rate attribute, this has to be number.
End of explanation
rr = ReactionRule()
rr.add_reactant(Species("A"))
rr.add_reactant(Species("B"))
rr.add_product(Species("C"))
rr.set_k(1.0)
Explanation: Now you created a reaction from A to B. In this reaction definition, you don't need to assign values to Species.
You can also create a binding reaction as follows
End of explanation
rr1 = create_unimolecular_reaction_rule(Species("A"), Species("B"), 1.0)
rr2 = create_binding_reaction_rule(Species("A"), Species("B"), Species("C"), 1.0)
rr3 = create_binding_reaction_rule(Species("C"), Species("A"), Species("B"), 1.5)
Explanation: A Binding A and B creates C. There are utility functions for binding and unbinding ReactionRules.
End of explanation
print(rr3.as_string())  # will return 'C+A>B|1.5'
Explanation: When you want to inspect the ReactionRule, please use as_string function.
End of explanation
sp1 = Species("A", "0.005", "1")
sp2 = Species("B", "0.005", "1")
sp3 = Species("C", "0.01", "0.5")
rr1 = create_binding_reaction_rule(Species("A"), Species(b"B"), Species("C"), 0.01)
rr2 = create_unbinding_reaction_rule(Species("C"), Species("A"), Species("B"), 0.3)
m = NetworkModel()
m.add_species_attribute(sp1)
m.add_species_attribute(sp2)
m.add_species_attribute(sp3)
m.add_reaction_rule(rr1)
m.add_reaction_rule(rr2)
Explanation: NetworkModel
Now you have created components for Model, next we register these components on Model.
End of explanation
print(m.species_attributes())
print(m.reaction_rules())
m.num_reaction_rules()
# will return like:
# [<ecell4.core.Species object at 0x7f36443bfa98>, <ecell4.core.Species object at 0x7f36443bfab0>, <ecell4.core.Species object at 0x7f36443bfac8>]
# [<ecell4.core.ReactionRule object at 0x7f36443bfa98>, <ecell4.core.ReactionRule object at 0x7f36443bfab0>]
# 2
Explanation: To add a Species, use add_species_attribute.
To add a ReactionRule, use add_reaction_rule.
Now you created a Model for simple association and dissociation.
To inspect a Model, use species_attributes, reaction_rules, and num_reaction_rules().
End of explanation
%matplotlib inline
# XXX: 'm' is a NetworkModel, which is described in Model tutorial (http://nbviewer.ipython.org/github/ecell/ecell4/blob/develop/ipynb/Tutorials/ModelBasics.ipynb) .
import numpy
t = numpy.linspace(0, 10, 100)  # [0.0, 0.1, 0.2, ..., 9.9, 10.0]
y = run_simulation(t, {'C': 60}, volume=1.0, model=m)
Explanation: NetworkModel also contains Species attributes. These attributes are indispensable for particle and lattice simulations, but not necessarily needed for gillespie and ode.
NetworkModel attributes a Species based on the registered Species.
A simple simulation with E-Cell4
Yet we have not explained World and Simulator, we show you a simple simulation result.
E-Cell4 has a utility function named run_simulation for simple demo.
This function runs on an environment installed IPython Notebook and matplotlib.
End of explanation
y = run_simulation(t, {'C': 60}, volume=1.0, model=m, solver='gillespie')
Explanation: run_simulation records the number of molecules for time t and plots the time-series simulation result.
In this case, we recorded the simulation for 10 seconds per 0.1 second.
The second argument is initial value. We started the simulation from the 60 C molecules.
volume is the volume for this simulation, and we set a network model to model.
You can also plot stochastic simulation result.
End of explanation
del A
del B
with reaction_rules():
    A + B > C | 0.01  # equivalent to create_binding_reaction_rule
    C > A + B | 0.3   # equivalent to create_unbinding_reaction_rule
m = get_model()
Explanation: You can simalate a model with different methods like this.
E-Cell system completely separates the model and simulation methods.
Special model notation in E-Cell4
We created Species and ReactionRule, then set them to NetworkModel and simulated it.
But the model description is cumbersome even in simple association and dissociation.
So E-Cell4 has shortcuts to describe this model.
Here we show a shortcut about ReactionRule.
Before using the shortcut, please remove the reactant Species A and B we created in global scope above.
(You don't need to remove the product C)
End of explanation
with reaction_rules():
    A + B == C | (0.01, 0.3)
y = run_simulation(numpy.linspace(0, 10, 100), {'C': 60}, volume=1.0)
Explanation: Please use with statement for E-Cell4 special notation.
You can use special notation under this with scope.
Please remember to write () after reaction_rules.
The syntax speaks for itself. The number after separator | is kinetic constant.
The syntax must be valid Python, so take care in using line breaks.
If you do NOT del A and del B, A + B is expanded to Species("A") + Species("B").
This causes an error like TypeError: unsupported operand type(s) for +: 'ecell4.core.Species' and 'ecell4.core.Species'.
Do not forget to del A and del B if you created Species A and B above.
For reversible reaction, please use ==
End of explanation
with reaction_rules():
    A > ~A | 1.0  # XXX: create_degradation_reaction_rule
    ~A > A | 1.0  # XXX: create_synthesis_reaction_rule
Explanation: $$\frac{\mathrm{d[A]}}{\mathrm{d}t}=\frac{\mathrm{d[B]}}{\mathrm{d}t}=-0.01\mathrm{[A][B]}+0.3\mathrm{[C]}\
\frac{\mathrm{d[C]}}{\mathrm{d}t}=+0.01\mathrm{[A][B]}-0.3\mathrm{[C]}$$
In reversible reaction, you need to set two kinetic constant after the separator.
When you do NOT set the model to run_simulation function, run_simulation calls get_model() automatically. So in this case we skipped.
Notations about synthesis and degradation
In the absence of left or right side of molecules like synthesis or degradation,
you may write the model like
python
with reaction_rules():
    A &gt; | 1.0  # XXX: will throw SyntaxError
    &gt; A | 1.0  # XXX: will throw SyntaxError
but this must return SyntaxError: invalid syntax
For synthesis and degradation, please use special character ~.
~ sets the following molecule stoichiometric coefficient 0.
End of explanation
<END_TASK>