diff --git a/doc/ADI_scheme.org b/doc/ADI_scheme.org index fbcbefb..096a02a 100644 --- a/doc/ADI_scheme.org +++ b/doc/ADI_scheme.org @@ -316,6 +316,8 @@ be rewritten: \frac{\partial C }{\partial t} & = \frac{\partial}{\partial x} \left(\alpha(x) \frac{\partial C }{\partial x} \right) \end{align} +** Discretization of the equation using chain rule + \noindent From the product rule for derivatives we obtain: #+NAME: eqn:product @@ -381,4 +383,59 @@ terms (at time level $t+1/2$) on the left hand side we obtain: \noindent If the diffusion coefficients are constant, $A_{i,j}=B_{i,j}=\alpha$ and the scheme reverts to the homogeneous -case. +case. Problem with this discretization is that the terms in $A_{ij}$ +and $B_{ij}$ can be negative depending on the derivative of the +diffusion coefficient, resulting in unphysical values for the +concentrations. + +** Direct discretization + +As noted in literature (LeVeque and Numerical Recipes) a better way is +to discretize directly the physical problem ([[eqn:hetdiff]]) at points +halfway between grid points: + +\begin{align*} +\begin{cases} +\displaystyle \alpha(x_{i+1/2}) \frac{\partial C }{\partial x}(x_{i+1/2}) & \displaystyle = \alpha_{i+1/2} \left( \frac{C_{i+1} -C_{i}}{\Delta x} \right) \\ +\displaystyle \alpha(x_{i-1/2}) \frac{\partial C }{\partial x}(x_{i-1/2}) & \displaystyle = \alpha_{i-1/2} \left( \frac{C_{i} -C_{i-1}}{\Delta x} \right) +\end{cases} +\end{align*} + +\noindent A further differentiation gives us the centered +approximation of $\frac{\partial}{\partial x} \left(\alpha(x) +\frac{\partial C }{\partial x}\right)$: + +\begin{align*} +\frac{\partial}{\partial x} \left(\alpha(x) +\frac{\partial C }{\partial x}\right)(x_i) & \simeq \frac{1}{\Delta x}\left[\alpha_{i+1/2} \left( \frac{C_{i+1} -C_{i}}{\Delta x} \right) - \alpha_{i-1/2} \left( \frac{C_{i} -C_{i-1}}{\Delta x} \right) \right]\\ +&\displaystyle =\frac{1}{\Delta x^2} \left[ \alpha_{i+1/2}C_{i+1} - (\alpha_{i+1/2}+\alpha_{i-1/2}) C_{i} + \alpha_{i-1/2}C_{i-1}\right] +\end{align*} + +\noindent The ADI scheme with this approach becomes: + +#+NAME: eqn:genADI_hetdir +\begin{equation} +\left\{ +\begin{aligned} + \frac{C^{t+1/2}_{i,j}-C^{t }_{i,j}}{\Delta t/2} = & \frac{1}{\Delta x^2} \left[ \alpha_{i+1/2,j} C^{t+1/2}_{i+1,j} - (\alpha_{i+1/2,j}+\alpha_{i-1/2,j}) C^{t+1/2}_{i,j} + \alpha_{i-1/2,j} C^{t+1/2}_{i-1,j}\right] + \\ + & \frac{1}{\Delta y^2} \left[ \alpha_{i,j+1/2}C^{t}_{i,j+1} - (\alpha_{i,j+1/2}+\alpha_{i,j-1/2}) C^t_{i} + \alpha_{i,j-1/2}C^{t}_{i,j-1}\right]\\ +\frac{C^{t+1 }_{i,j}-C^{t+1/2}_{i,j}}{\Delta t/2} = & \frac{1}{\Delta x^2} \left[ \alpha_{i+1/2,j}C^{t+1/2}_{i+1,j} - (\alpha_{i+1/2,j}+\alpha_{i-1/2,j}) C_{i} + \alpha_{i-1/2,j}C^{t+1/2}_{i-1,j}\right] + \\ + & \frac{1}{\Delta y^2} \left[ \alpha_{i,j+1/2}C^{t+1}_{i,j+1} - (\alpha_{i,j+1/2}+\alpha_{i,j-1/2}) C^{t+1}_{i,j} + \alpha_{i,j-1/2}C^{t+1}_{i,j-1}\right] +\end{aligned} +\right. +\end{equation} + +\noindent Doing the usual algebra and separating implicit from +explicit terms, the two sweeps become: + +#+NAME: eqn:sweepX_hetdir +\begin{equation} +\left\{ +\begin{aligned} +-S_x \alpha^x_{i+1/2,j} C^{t+1/2}_{i+1,j} + (1 + S_x(\alpha^x_{i+1/2,j}+ \alpha^x_{i-1/2,j}))C^{t+1/2}_{i,j} - S_x \alpha^x_{i-1/2,j} C^{t+1/2}_{i-1,j} = & \\ + S_y \alpha^y_{i,j+1/2} C^{t }_{i,j+1} + (1 - S_y(\alpha^y_{i,j+1/2}+ \alpha^y_{i,j-1/2}))C^{t }_{i,j} + S_y \alpha^y_{i,j-1/2} C^{t }_{i,j-1} & \\[1em] +-S_y \alpha^y_{i,j+1/2} C^{t+1 }_{i,j+1} + (1 + S_y(\alpha^y_{i,j+1/2}+ \alpha^y_{i,j-1/2}))C^{t+1 }_{i,j} - S_y \alpha^y_{i,j-1/2} C^{t+1 }_{i,j-1} = & \\ + S_x \alpha^x_{i+1/2,j} C^{t+1/2}_{i+1,j} + (1 - S_x(\alpha^x_{i+1/2,j}+ \alpha^x_{i-1/2,j}))C^{t+1/2}_{i,j} + S_x \alpha^x_{i-1/2,j} C^{t+1/2}_{i-1,j} +\end{aligned} +\right. +\end{equation} diff --git a/scripts/Adi2D_Reference.R b/scripts/Adi2D_Reference.R index f1d942c..ffc8212 100644 --- a/scripts/Adi2D_Reference.R +++ b/scripts/Adi2D_Reference.R @@ -1,4 +1,4 @@ -## Time-stamp: "Last modified 2022-12-20 21:17:32 delucia" +## Time-stamp: "Last modified 2022-12-21 13:47:00 delucia" ## Brutal implementation of 2D ADI scheme ## Square NxN grid with dx=dy=1 @@ -124,7 +124,7 @@ plot(adi2[[length(adi2)]], ref2, log="xy", xlab="ADI", ylab="ode.2D (reference)" abline(0,1) -## Test heterogeneous scheme +## Test heterogeneous scheme, chain rule ADIHet <- function(field, dt, iter, alpha) { if (!all.equal(dim(field), dim(alpha))) @@ -246,3 +246,104 @@ par(mfrow=c(1,2)) image(alphas) image(adih1[[length(adih1)]]) points(0.5,0.5, col="red",pch=4) + + + + +## Test heterogeneous scheme, direct discretization +ADIHetDir <- function(field, dt, iter, alpha) { + + if (!all.equal(dim(field), dim(alpha))) + stop("field and alpha are not matrix") + + ## now both field and alpha must be nx*ny matrices + nx <- ncol(field) + ny <- nrow(field) + dx <- dy <- 1 + + ## find out the center of the grid to apply conc=1 + cenx <- ceiling(nx/2) + ceny <- ceiling(ny/2) + field[cenx, ceny] <- 1 + + ## prepare containers for computations and outputs + tmpX <- tmpY <- res <- field + out <- vector(mode="list", length=iter) + + for (it in seq(1, iter)) { + for (i in seq(2, ny-1)) { + Aij <- cbind((alpha[i,]+alpha[i-1,])/2, (alpha[i,]+alpha[i+1,])/2) + Bij <- cbind((alpha[,i]+alpha[,i-1])/2, (alpha[,i]+alpha[,i+1])/2) + tmpX[i,] <- SweepByRowHetDir(i, res, dt=dt, Aij, Bij) + } + resY <- t(tmpX) + for (i in seq(2, nx-1)) + tmpY[i,] <- SweepByRowHetDir(i, resY, dt=dt, Bij, Aij) + res <- t(tmpY) + out[[it]] <- res + } + + return(out) +} + + +## Direct discretization, Workhorse function to fill A, B and solve +## for a given *row* of the grid matrix +SweepByRowHetDir <- function(i, field, dt, Aij, Bij) { + dx <- 1 ## fixed in our test + Sx <- Sy <- dt/2/dx/dx + + ## diagonal of A at once + A <- matrix(0, nrow(field), ncol(field)) + diag(A) <- 1 + Sx*(Aij[,1]+Aij[,2]) + + ## adjacent diagonals "Sx" + for (ii in seq(1, nrow(field)-1)) { + A[ii+1, ii] <- -Sx*Aij[ii,1] + A[ii, ii+1] <- -Sx*Aij[ii,2] + } + + B <- numeric(ncol(field)) + + for (ii in seq_along(B)) + B[ii] <- Sy*Bij[ii,2]*field[i+1,ii] + (1 - Sy*(Bij[ii,1]+Bij[ii,2]))*field[i, ii] + Sy*Bij[ii,1]*field[i-1,ii] + + x <- solve(A, B) + x +} + +## adi2 <- ADI(n=51, dt=10, iter=200, alpha=1E-3) +## ref2 <- DoRef(n=51, alpha=1E-3, dt=10, iter=200) + +n <- 51 +field <- matrix(0, n, n) +alphas <- matrix(1E-3*runif(n*n, 1,1.2), n, n) + + +## for (i in seq(1,nrow(alphas))) +## alphas[i,] <- seq(1E-7,1E-3, length=n) + +#diag(alphas) <- rep(1E-2, n) + +adih <- ADIHetDir(field=field, dt=10, iter=100, alpha=alphas) +adi2 <- ADI(n=n, dt=10, iter=100, alpha=1E-3) + + +par(mfrow=c(1,3)) +image(adi2[[length(adi2)]]) +image(adih[[length(adih)]]) +points(0.5,0.5, col="red",pch=4) +plot(adih1[[length(adih1)]], adi2[[length(adi2)]], pch=4, log="xy") +abline(0,1) + + +sapply(adih, sum) +sapply(adi2, sum) + +adi2 + + +par(mfrow=c(1,2)) +image(alphas) +image(adih1[[length(adih1)]]) +points(0.5,0.5, col="red",pch=4)