%matplotlib notebook

"""
Explanation: This is the second example about diffusion. In this one, we will show that diffusion, coupled with degradation and non-uniform synthesis, can result in concentration gradients.
The code for this example is mostly identical to that of the previous example, therefore we will only explain the parts that differ from the previous example.
Preparation
End of explanation
"""


import multicell
import numpy as np

"""
Explanation: Imports
End of explanation
"""


sim = multicell.simulation_builder.generate_cell_grid_sim(20, 1, 1, 1e-3)

"""
Explanation: Problem definition
Simulation and tissue structure
End of explanation
"""


sim.register_cell_variable("a")

"""
Explanation: Biological species
End of explanation
"""


A = np.array([20] * 10 + [0] * 10)
sim.set_constants({"D_a": 100., "mu_a": 1., "A": A})

"""
Explanation: Constants
In this example, we introduce two additional constants. mu_a is the degradation - and turnover - rate of a (common to all cells) and A is a relative synthesis rate of a (20 in the first 10 cells do, 0 in the others).
End of explanation
"""


def da_dt(simulation, a, c_a, D_a, mu_a, A, adjacency_matrix, **kwargs):
    return simulation.diffusion(D_a, c_a, adjacency_matrix) - mu_a * a + mu_a * A

sim.set_ODE("a", da_dt)

"""
Explanation: Differential equations
This time, the differential equation governing a is slightly more complex, as it includes a degradation term and a synthesis term.
End of explanation
"""


sim.initialize_cell_variables()

a0 = A # Same as in the previous example

sim.set_cell_variable("a", a0)

"""
Explanation: The equation is designed so that the system has a steady state, and the total amount of a in the system at that steady state is independent of the value of mu_a, the turnover rate. We can therefore safely change the value of mu_a to modify the turnover without affecting the scale of the concentration values, which is useful for visualization purposes.
Initial conditions
End of explanation
"""


sim.set_duration(4)
sim.set_time_steps(10, "log2")

"""
Explanation: Duration
End of explanation
"""


sim.register_renderer(multicell.rendering.MatplotlibRenderer, "c_a", {"max_cmap": 20, "view": (90, -90), "axes": False})

"""
Explanation: Rendering
End of explanation
"""


sim.renderer.display("c_a")

"""
Explanation: Visualization of the initial state
End of explanation
"""


sim.simulate()

"""
Explanation: Simulation
End of explanation
"""


