|
| 1 | +""" |
| 2 | +2D Brusselator |
| 3 | +
|
| 4 | +```math |
| 5 | +\\begin{align} |
| 6 | +\\frac{\\partial u}{\\partial t} &= 1 + u^2v - 4.4u + \\alpha(\frac{\\partial^2 u}{\\partial x^2} + \frac{\\partial^2 u}{\\partial y^2}) + f(x, y, t) |
| 7 | +\\frac{\\partial v}{\\partial t} &= 3.4u - u^2v + \\alpha(\frac{\\partial^2 u}{\\partial x^2} + \frac{\\partial^2 u}{\\partial y^2}) |
| 8 | +\\end{align} |
| 9 | +``` |
| 10 | +
|
| 11 | +where |
| 12 | +
|
| 13 | +```math |
| 14 | +f(x, y, t) = \\begin{cases} |
| 15 | +5 & \\quad \\text{if } (x-0.3)^2+(y-0.6)^2 ≤ 0.1^2 \\text{ and } t ≥ 1.1 \\\\ |
| 16 | +0 & \\quad \\text{else} |
| 17 | +\\end{cases} |
| 18 | +``` |
| 19 | +
|
| 20 | +and the initial conditions are |
| 21 | +
|
| 22 | +```math |
| 23 | +\\begin{align} |
| 24 | +u(x, y, 0) &= 22\\cdot y(1-y)^{3/2} \\\\ |
| 25 | +v(x, y, 0) &= 27\\cdot x(1-x)^{3/2} |
| 26 | +\\end{align} |
| 27 | +``` |
| 28 | +
|
| 29 | +with the periodic boundary condition |
| 30 | +
|
| 31 | +```math |
| 32 | +\\begin{align} |
| 33 | +u(x+1,y,t) &= u(x,y,t) \\\\ |
| 34 | +u(x,y+1,t) &= u(x,y,t) |
| 35 | +\\end{align} |
| 36 | +``` |
| 37 | +
|
| 38 | +From Hairer Norsett Wanner Solving Ordinary Differential Equations II - Stiff and Differential-Algebraic Problems Page 152 |
| 39 | +""" |
| 40 | +brusselator_f(x, y, t) = ifelse((((x-0.3)^2 + (y-0.6)^2) <= 0.1^2) && |
| 41 | + (t >= 1.1), 5., 0.) |
| 42 | +function limit(a, N) |
| 43 | + if a == N+1 |
| 44 | + return 1 |
| 45 | + elseif a == 0 |
| 46 | + return N |
| 47 | + else |
| 48 | + return a |
| 49 | + end |
| 50 | +end |
| 51 | +function brusselator_2d_loop(du, u, p, t) |
| 52 | + @inbounds begin |
| 53 | + A, B, α, xyd, dx, N = p |
| 54 | + α = α/dx^2 |
| 55 | + for I in CartesianRange((N, N)) |
| 56 | + x = xyd[I[1]] |
| 57 | + y = xyd[I[2]] |
| 58 | + i = I[1] |
| 59 | + j = I[2] |
| 60 | + ip1 = limit(i+1, N) |
| 61 | + im1 = limit(i-1, N) |
| 62 | + jp1 = limit(j+1, N) |
| 63 | + jm1 = limit(j-1, N) |
| 64 | + du[i,j,1] = α*(u[im1,j,1] + u[ip1,j,1] + u[i,jp1,1] + u[i,jm1,1] - 4u[i,j,1]) + |
| 65 | + B + u[i,j,1]^2*u[i,j,2] - (A + 1)*u[i,j,1] + brusselator_f(x, y, t) |
| 66 | + end |
| 67 | + for I in CartesianRange((N, N)) |
| 68 | + i = I[1] |
| 69 | + j = I[2] |
| 70 | + ip1 = limit(i+1, N) |
| 71 | + im1 = limit(i-1, N) |
| 72 | + jp1 = limit(j+1, N) |
| 73 | + jm1 = limit(j-1, N) |
| 74 | + du[i,j,2] = α*(u[im1,j,2] + u[ip1,j,2] + u[i,jp1,2] + u[i,jm1,2] - 4u[i,j,2]) + |
| 75 | + A*u[i,j,1] - u[i,j,1]^2*u[i,j,2] |
| 76 | + end |
| 77 | + end |
| 78 | +end |
| 79 | +function init_brusselator_2d(xyd) |
| 80 | + N = length(xyd) |
| 81 | + u = zeros(N, N, 2) |
| 82 | + for I in CartesianRange((N, N)) |
| 83 | + x = xyd[I[1]] |
| 84 | + y = xyd[I[2]] |
| 85 | + u[I,1] = 22*(y*(1-y))^(3/2) |
| 86 | + u[I,2] = 27*(x*(1-x))^(3/2) |
| 87 | + end |
| 88 | + u |
| 89 | +end |
| 90 | +xyd_brusselator = linspace(0,1,32) |
| 91 | +prob_ode_brusselator_2d = ODEProblem(brusselator_2d_loop, |
| 92 | + init_brusselator_2d(xyd_brusselator), |
| 93 | + (0.,11.5), |
| 94 | + (3.4, 1., 10., |
| 95 | + xyd_brusselator, step(xyd_brusselator), |
| 96 | + length(xyd_brusselator))) |
| 97 | + |
| 98 | +""" |
| 99 | +1D Brusselator |
| 100 | +
|
| 101 | +```math |
| 102 | +\\begin{align} |
| 103 | +\\frac{\\partial u}{\\partial t} &= A + u^2v - (B+1)u + \\alpha\frac{\\partial^2 u}{\\partial x^2} |
| 104 | +\\frac{\\partial v}{\\partial t} &= Bu - u^2v + \\alpha\frac{\\partial^2 u}{\\partial x^2} |
| 105 | +\\end{align} |
| 106 | +``` |
| 107 | +
|
| 108 | +and the initial conditions are |
| 109 | +
|
| 110 | +```math |
| 111 | +\\begin{align} |
| 112 | +u(x,0) &= 1+\\sin(2π x) \\\\ |
| 113 | +v(x,0) &= 3 |
| 114 | +\\end{align} |
| 115 | +``` |
| 116 | +
|
| 117 | +with the boundary condition |
| 118 | +
|
| 119 | +```math |
| 120 | +\\begin{align} |
| 121 | +u(0,t) &= u(1,t) = 1 \\\\ |
| 122 | +v(0,t) &= v(1,t) = 3 |
| 123 | +\\end{align} |
| 124 | +``` |
| 125 | +
|
| 126 | +From Hairer Norsett Wanner Solving Ordinary Differential Equations II - Stiff and Differential-Algebraic Problems Page 6 |
| 127 | +""" |
| 128 | +const N_brusselator_1d = 40 |
| 129 | +const D_brusselator_u = DerivativeOperator{Float64}(2,2,1/(N_brusselator_1d-1), |
| 130 | + N_brusselator_1d, |
| 131 | + :Dirichlet,:Dirichlet; |
| 132 | + BC=(1.,1.)) |
| 133 | +const D_brusselator_v = DerivativeOperator{Float64}(2,2,1/(N_brusselator_1d-1), |
| 134 | + N_brusselator_1d, |
| 135 | + :Dirichlet,:Dirichlet; |
| 136 | + BC=(3.,3.)) |
| 137 | +function brusselator_1d(du, u_, p, t) |
| 138 | + A, B, α, buffer = p |
| 139 | + u = @view(u_[:, 1]) |
| 140 | + v = @view(u_[:, 2]) |
| 141 | + A_mul_B!(buffer, D_brusselator_u, u) |
| 142 | + Du = buffer |
| 143 | + @. du[:, 1] = A + u^2*v - (B+1)*u + α*Du |
| 144 | + |
| 145 | + A_mul_B!(buffer, D_brusselator_v, v) |
| 146 | + Dv = buffer |
| 147 | + @. du[:, 2] = B*u - u^2*v + α*Dv |
| 148 | + nothing |
| 149 | +end |
| 150 | +function init_brusselator_1d(N) |
| 151 | + u = zeros(N, 2) |
| 152 | + x = linspace(0, 1, N) |
| 153 | + for i in 1:N |
| 154 | + u[i, 1] = 1 + sin(2pi*x[i]) |
| 155 | + u[i, 2] = 3. |
| 156 | + end |
| 157 | + u |
| 158 | +end |
| 159 | +prob_ode_brusselator_1d = ODEProblem(brusselator_1d, |
| 160 | + init_brusselator_1d(N_brusselator_1d), |
| 161 | + (0.,10.), |
| 162 | + (1., 3., 1/50, zeros(N_brusselator_1d))) |
0 commit comments