MRaster examples 22.0.0.0
Image Processing Library
Loading...
Searching...
No Matches
brusselator.cpp
Go to the documentation of this file.
1// -*- Mode:C++; Coding:us-ascii-unix; fill-column:158 -*-
2/*******************************************************************************************************************************************************.H.S.**/
3/**
4 @file brusselator.cpp
5 @author Mitch Richling <https://www.mitchr.me>
6 @brief Simulate the brusselator on a random initial set.@EOL
7 @keywords brusselator reaction diffusion models
8 @parblock
9 Copyright (c) 2025, Mitchell Jay Richling <https://www.mitchr.me> All rights reserved.
10
11 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
12
13 1. Redistributions of source code must retain the above copyright notice, this list of conditions, and the following disclaimer.
14
15 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions, and the following disclaimer in the documentation
16 and/or other materials provided with the distribution.
17
18 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software
19 without specific prior written permission.
20
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
26 DAMAGE.
27 @endparblock
28 @filedetails
29
30 The Brusselator is a reaction-diffusion model for an autocatalytic chemical reaction in space and time. This system was was proposed by Ilya Romanovich
31 Prigogine and Rene Lefever from Universite libre de Bruxelles -- hence the name. The model is given by the following equations:
32
33 @f[ \begin{eqnarray}
34 \frac{\partial u_1}{\partial t} & = & 1-(b+1)u_1+au_1^2u_2+d_1 \left(\frac{\partial^2 u_1}{\partial x_1^2}+\frac{\partial^2 u_1}{\partial x_2^2}\right) \\
35 \frac{\partial u_2}{\partial t} & = & bu_1-au_1^2u_2+d_2 \left(\frac{\partial^2 u_2}{\partial x_1^2}+\frac{\partial^2 u_2}{\partial x_2^2}\right)
36 \end{eqnarray} @f]
37
38 The functions @f$ u_1 @f$ and @f$ u_2 @f$ represent dimensionless concentrations of two of the reactants -- an activator and an inhibitor. The values
39 @f$ a @f$ and @f$ b @f$ represent normalized reaction rates. The values @f$ d_1 @f$ and @f$ d_2 @f$ are "diffusion coefficients", and may be
40 combined into a single value @f$ d=\frac{d_2}{d_1} @f$ like this:
41
42 @f[ \begin{eqnarray}
43 \frac{\partial u_1}{\partial t} & = & 1-(b+1)u_1+au_1^2u_2 \left(\frac{\partial^2 u_1}{\partial x_1^2}+\frac{\partial^2 u_1}{\partial x_2^2}\right) \\
44 \frac{\partial u_2}{\partial t} & = & bu_1-au_1^2u_2+d \left(\frac{\partial^2 u_2}{\partial x_1^2}+\frac{\partial^2 u_2}{\partial x_2^2}\right)
45 \end{eqnarray} @f]
46
47 In the code below, we use the following values for the parameters:
48
49 @f[ \begin{array}{lcc}
50 a & = & 9 \\
51 b & = & \frac{98}{10} \\
52 d & = & 3
53 \end{array} @f]
54
55 We use a simple finite differences method to solve the equations with a step size of @f$ \Delta t=0.007 @f$ over 3000 steps. The spatial grid has a grid
56 size of @f$ h=0.3 @f$. The spatial second derivatives at a point @f$ (x,y) @f$ are approximated with the five point stencil":
57
58 @f[ \frac{\partial^2 u_j}{\partial x_1^2}+\frac{\partial^2 u_j}{\partial x_2^2} \approx \frac{u_j(x+h, y)+u_j(x-h, y)+u_j(x, y+h)+u_j(x, y-h)-4u_j(x,y)}{h^2} @f]
59
60 The code uses four images to store the state of the system at two steps. At each time step it uses two of the images to update the other two. This doubles
61 the RAM required, but simplifies the code and eliminates the need for swapping data. At the end of the run the last updated state images are combined into
62 a 24-bit RGB image which is written to disk.
63
64*/
65/*******************************************************************************************************************************************************.H.E.**/
66/** @cond exj */
67
68//--------------------------------------------------------------------------------------------------------------------------------------------------------------
69#include "ramCanvas.hpp"
70
71typedef mjr::ramCanvas3c8b rcT;
72
73//--------------------------------------------------------------------------------------------------------------------------------------------------------------
74int main(void) {
75 std::random_device rd;
76 std::minstd_rand0 rEng(rd());
77 std::uniform_real_distribution<double> uniform_dist_double(1.0e-5, 1.0);
78 int width = 7680/16;
79 int height = 4320/16;
80
81 std::chrono::time_point<std::chrono::system_clock> startTime = std::chrono::system_clock::now();
82 rcT theRamCanvas(width, height);
83
84 std::vector<mjr::ramCanvas1c64F> imgu1{mjr::ramCanvas1c64F(width, height), mjr::ramCanvas1c64F(width, height)};
85 std::vector<mjr::ramCanvas1c64F> imgu2{mjr::ramCanvas1c64F(width, height), mjr::ramCanvas1c64F(width, height)};
86
87 mjr::ramCanvas1c64F::pointIntVecType st { {0,-1}, {0,1}, {-1,0}, {1,0}};
88
89 int N = 3000;
90 double h = 0.3;
91 double t = 0.007;
92 double a = 9.0;
93 double b = 9.8;
94 double d = 3.0;
95
96 for(rcT::coordIntType y=0;y<theRamCanvas.getNumPixY();y++)
97 for(rcT::coordIntType x=0;x<theRamCanvas.getNumPixX();x++) {
98 imgu1[0].drawPoint(x, y, uniform_dist_double(rEng));
99 imgu2[0].drawPoint(x, y, uniform_dist_double(rEng));
100 }
101
102 int i_in, i_ou;
103 double maxv = 0;
104 for(int step=0; step<N; step++) {
105 i_in = (step%2);
106 i_ou = ((step+1)%2);
107 if (0==(step%100))
108 std::cout << "STEP: " << step << std::endl;
109# pragma omp parallel for schedule(static,1)
110 for(rcT::coordIntType y=0;y<theRamCanvas.getNumPixY();y++) {
111 for(rcT::coordIntType x=0;x<theRamCanvas.getNumPixX();x++) {
112
113 double u1_sum = 0;
114 double u2_sum = 0;
115 for(mjr::ramCanvas1c64F::pointIntType const &p: st) {
116 u1_sum += imgu1[i_in].getPxColorChanWrap<0>(x+p.x, y+p.y);
117 u2_sum += imgu2[i_in].getPxColorChanWrap<0>(x+p.x, y+p.y);
118 }
119
120 double u1_c = imgu1[i_in].getPxColor(x, y).getC0();
121 double u2_c = imgu2[i_in].getPxColor(x, y).getC0();
122
123 double du1_c = t*(1.0-(b+1)*u1_c+a*u1_c*u1_c*u2_c+(u1_sum-4*u1_c)/(h*h));
124 double du2_c = t*(b*u1_c-a*u1_c*u1_c*u2_c+d*(u2_sum-4*u2_c)/(h*h));
125
126 double u1 = u1_c + du1_c;
127 double u2 = u2_c + du2_c;
128
129 maxv = std::max(maxv, std::max(u1, u2));
130
131 imgu1[i_ou].drawPoint(x, y, u1);
132 imgu2[i_ou].drawPoint(x, y, u2);
133 }
134 }
135 if (maxv > 1e10) {
136 std::cout << "Solution Failure at step " << step << std::endl;
137 return 1;
138 } else if (maxv < 0.0001) {
139 std::cout << "Zero Solution at step " << step << std::endl;
140 return 1;
141 }
142 }
143
144 imgu1[i_ou].autoHistStrech();
145 imgu2[i_ou].autoHistStrech();
146 for(int y=0;y<theRamCanvas.getNumPixY();y++)
147 for(int x=0;x<theRamCanvas.getNumPixX();x++) {
148 rcT::colorChanType r = static_cast<rcT::colorChanType>(255*imgu1[i_ou].getPxColorNC(x, y).getC0());
149 rcT::colorChanType b = static_cast<rcT::colorChanType>(255*imgu2[i_ou].getPxColorNC(x, y).getC0());
150 theRamCanvas.drawPoint(x, y, mjr::color3c8b(r, 0, b));
151 }
152 theRamCanvas.writeTIFFfile("brusselator.tiff");
153
154 std::chrono::duration<double> runTime = std::chrono::system_clock::now() - startTime;
155 std::cout << "Total Runtime " << runTime.count() << " sec" << std::endl;
156 return 0;
157}
158/** @endcond */
int main(int argc, char *argv[])