Lecture 12: Physical modelling ============================== Before this class you should: .. include:: prep12.txt Before next class you should: .. include:: prep13.txt Note Taker: Syed Mahdi Rehan. Lecture Summary ------------------- This lecture explored physical modelling through code examples, focusing on diffusion, reaction-diffusion, percolation, and fractals. We first started with simple diffusion, then introduced reacting chemicals, and simulated how they interact. We then analyzed percolation, investigating the probability of water reaching the bottom and how the critical value is affected by grid size. Finally, fractal dimensions in percolation models were examined, using cellular automata as well. Using these concepts, we elaborated on reaction-diffusion models, simulating cell mitosis, and understood the mathematics behind the patterns. They further illustrate how parameter variations create diverse patterns and discuss fractals, dimensions, and critical points in complex systems, using code to demonstrate these concepts. Physical Modelling Overview ---------------------------- * **Overview:** Physical modelling involves using cellular automata to simulate real-world phenomena. These simulations can represent chemical diffusion, reaction, percolation, and fractal geometry. Diffusion ---------- * **Definition:** Diffusion is the process where particles move from an area of high concentration to an area of low concentration. * **Visualisation:** Can be visualized at two levels. * **Microscopic Level:** Random movement of individual particles. * **Macroscopic Level:** Smooth transition from high to low concentration. * **Cellular Automaton Modelling:** In Cellular Automation, diffusion can be modelled by comparing the concentration of a cell with its neighbours. * A higher concentration cell spreads chemical to lower concentration neighbours, and the cell will absorb chemicals. * Lower concentration neighbours absorb chemical from the higher concentration cell, and the cell will spread its chemicals out. * **Kernel:** The Kernel is a key part of the Diffusion model. It computes the difference between the average concentration of a cell's neighbourhood and the cell's own concentration. .. code-block:: python concentration += diffusion_rate * concentration_difference * **Implementation of Diffusion Using Cellular Automata:** .. code-block:: python # The diffusion process is modeled as a 2D cellular automaton from scipy.signal import correlate2d from Cell2D import Cell2D, draw_array class Diffusion(Cell2D): "Diffusion Cellular Automaton." kernel = np.array([[0, 1, 0], [1,-4, 1], [0, 1, 0]]) def __init__(self, n, r=0.1): """Initializes the attributes: n: number of rows r: diffusion rate constant """ self.r = r self.array = np.zeros((n, n), float) def add_cells(self, row, col, *strings): """Adds cells at the given location: row: top row index col: left col index strings: list of strings of 0s and 1s """ for i, s in enumerate(strings): self.array[row+i, col:col+len(s)] = np.array([int(x) for x in s]) Reaction-Diffusion -------------------- * **Model Description:** This model involves two chemicals, A and B, interacting with each other. * Chemical A is fed into the system. * Chemical B is removed from the system. * Reaction: Two B molecules consume one A molecule to create another B molecule. * **Key Parameters:** * :math:`r_a`: Diffusion rate of chemical A. * :math:`r_b`: Diffusion rate of chemical B. * :math:`f`: Feed rate of chemical A. * :math:`k`: Kill rate of chemical B. * **Term Explanations:** * :math:`AB^2`: Represents the chance of reaction between one A and two B molecules. * :math:`f(1-A)`: Represents the feeding of A into the system. * :math:`(k+f)B`: Represents the removal of B from the system. * **Patterns:** Different parameters can lead to different patterns, such as: * Animal skin patterns (e.g., zebra stripes, leopard spots). * Turing patterns (from Alan Turing's model of morphogenesis). * Cell mitosis: Occurs due to stable states with high concentrations of A or B, and interesting behaviours at the borders between these states. * **Implementation in Python:** .. code-block:: python class ReactionDiffusion(Diffusion): def step(self): "Executes one time step of the simulation." c = correlate2d(self.array, self.kernel, mode='same') self.array += self.r * c # To run a simulation rd = ReactionDiffusion(n=100, r=0.1) rd.add_cells(50, 50, "111", "010", "111") for i in range(100): rd.step() rd.draw() Percolation ------------- * **Definition:** Percolation models the movement of a fluid through a semi-porous material. * **Examples:** * Oil in rock formations * Water making paper wet * Coffee percolation * Epidemics * Networks of electrical resistors * **2D Cellular Automaton:** * Cell state represents porous (1) or non-porous (0), and wet (5). * If a cell is porous and has at least one wet neighbour, it becomes wet during each step. * Simulation runs until a fixed point is reached where no cells change state. * If water reaches the bottom row, the model has a percolating cluster. * **Probability:** The probability of a percolating cluster can be estimated by running many simulations. * **Critical Point:** * The value of **q** (probability of a cell being porous) around which there is a rapid change in behaviour. * Near the critical point, the system displays critical phenomena. * Critical phenomena refer to the behavior of physical systems during phase transitions, such as the point where water turns into vapor or a magnetic material loses its magnetism. Fractals --------- * **Definition:** Geometric shapes that exhibit self-similarity at different scales. * **Occurrence:** Often found in complex systems near their critical points. * **Fractal Dimension:** * Measure of how the size of an object scales with its linear measure. * Simple geometric objects have integer dimensions (1 for a line, 2 for a square, 3 for a cube). * Fractals have non-integer dimensions between these integer values. * **Measurement:** The fractal dimension of a percolation model can be measured by assessing how the number of wet cells scales as the size of a bounding box increases. Additional Notes ------------------ * The Cell1D object may create artifacts at the boundaries, which the Wrap1D object can address. * The forest fire model is another cellular automaton example that can produce fractals. * In the reaction-diffusion model, the kernel includes diagonal elements with lower weights due to their distance from the centre cell. * Diagonal elements have lower weights to reflect the longer path and increased resistance associated with diagonal movement. This ensures the model correctly prioritizes horizontal and vertical diffusion paths for greater accuracy. * In the percolation model, the '5' used to represent the wet state is based on having a 4-cell neighbourhood.