Algorithm Search⌘K

Algorithmadvanced

Diffusion synthetic acceleration

Speeds up source iteration by correcting each iterate with a diffusion solve for the error, which captures the slowly converging smooth modes.

Time
O(I N M) worst
Space
O(M) extra
Where
I = the number of iterations; M = the number of cells; N = the number of directions
Properties
deterministicnot exactiterativenot randomized

Diffusion synthetic acceleration (DSA) makes source iteration fast in thick scattering media. Source iteration removes the error modes that vary fast in space in a few sweeps, but it damps the smooth modes only by the factor cc, the scattering ratio, per sweep. These smooth modes are the ones that the diffusion approximation describes well. So after each sweep DSA solves a diffusion equation for the error, with the change of the scattering source as its source, and adds the result to the flux. One diffusion solve costs much less than a sweep. In an infinite medium the error then falls by at most about 0.2247c0.2247c per iteration instead of cc. This holds on the discrete level only if the diffusion equation is discretized consistently with the transport scheme; a diffusion equation that is discretized on its own can make the iteration diverge in thick cells.

In plain words

Source iteration follows the particles one bounce at a time. In a thick material where almost every collision is a bounce, you need hundreds of sweeps.

The slow part of the error is smooth: it changes slowly from cell to cell. A diffusion equation gets smooth errors right, and it is cheap to solve. So after each sweep you solve a diffusion equation for the error and add that guess to the flux.

The sweep removes the rough part of the error and the diffusion solve removes the smooth part. Together they cut the error by about five times in each step, however thick the material is. The catch: the diffusion equation must be built from the same cell equations as the sweep. If it is built in another way, the two parts disagree in thick cells, and the iteration can blow up.

The problem

The model is the one-speed slab problem of the source iteration page. The slab has MM cells with the widths hjh_j, the total and scattering cross sections Σt,j\Sigma_{t,j} and Σs,j\Sigma_{s,j} in cm−1\mathrm{cm}^{-1}, the absorption Σa,j=Σt,j−Σs,j\Sigma_{a,j} = \Sigma_{t,j} - \Sigma_{s,j} and an isotropic source qjq_j. The directions and weights are the NN-point Gauss-Legendre rule with NN even, the weights sum to 2, and the equations are

μndψndx+Σt ψn=Σs ϕ+q2,ϕ=∑n=1Nwnψn,\mu_n \frac{d\psi_n}{dx} + \Sigma_t\,\psi_n = \frac{\Sigma_s\,\phi + q}{2}, \qquad \phi = \sum_{n=1}^{N} w_n\psi_n,

with diamond difference in space. Each face is vacuum or reflecting. Write ϕ=Tϕ+b\phi = T\phi + b for these equations, as on the source iteration page. The problem is to find the same solution ϕ\phi as source iteration, in far fewer sweeps, when c=Σs/Σtc = \Sigma_s/\Sigma_t is near 1 and the slab is many mean free paths thick.

The algorithm

  1. DSA(N, Σt, Σs, q, h, faces, tol, φ0)
  2. 1φ_j ← φ0_j (or 0) for every cell j
  3. 2repeat
  4. 3S_j ← (Σs_j φ_j + q_j) / 2 for every cell j ▷ the scattering source of the last iterate
  5. 4φ½ ← SWEEP(S) ▷ one transport sweep of all directions
  6. 5R_j ← Σs_j (φ½_j - φ_j) for every cell j ▷ the source of the remaining error
  7. 6f ← the solution of -d/dx (D df/dx) + Σa f = R, discretized consistently with diamond difference; D = 1/(3Σt)
  8. 7φ_new ← φ½ + f; d ← max_j |φ_new_j - φ_j|; φ ← φ_new
  9. 8until d ≤ tol · max_j |φ_j| (or d is not finite)

Line L4 is the transport sweep of the source iteration page, with the reflected values solved exactly. Line L5 is the change of the scattering source in this sweep. The error that is left, ϕ−ϕ1/2\phi - \phi_{1/2}, solves the transport equation with this source, and line L6 replaces that transport problem by a diffusion problem.

Line L6 is the part that makes DSA work. Its unknowns are the values fif_i at the cell edges. Write gj=2/(3Σt,jhj)g_j = 2/(3\Sigma_{t,j}h_j) and sj=Σa,jhj/2s_j = \Sigma_{a,j}h_j/2. The row of the inner edge ii between the cells LL and RR is

(−gL+sL)fi−1+(gL+gR+sL+sR)fi+(−gR+sR)fi+1=RLhL+RRhR.(-g_L + s_L)f_{i-1} + (g_L + g_R + s_L + s_R)f_i + (-g_R + s_R)f_{i+1} = R_Lh_L + R_Rh_R .

A face row keeps the terms of its one cell, and a vacuum face adds 1 to its diagonal entry (the Marshak condition: no particles enter). The correction in cell jj is (fj+fj+1)/2(f_j + f_{j+1})/2. These rows are the diamond difference P1 equations with the current eliminated (Theorem 2 of the proof), which is the method of Alcouffe (1977). The system is tridiagonal, symmetric and positive definite, and the Thomas algorithm solves it in O(M)O(M) operations.

The implementation also has the usual cell-centered diffusion equation as the option "inconsistent", with the face coefficient 1/(hL/(2DL)+hR/(2DR))1/(h_L/(2D_L) + h_R/(2D_R)). It is a correct discretization of diffusion, but it does not match diamond difference.

Example

An infinite medium by hand. Take Σt=1 cm−1\Sigma_t = 1\ \mathrm{cm}^{-1}, Σs=0.9 cm−1\Sigma_s = 0.9\ \mathrm{cm}^{-1}, q=1q = 1, both faces reflecting and ϕ(0)=0\phi^{(0)} = 0. Line L3 gives S=0.5S = 0.5, and the sweep gives ψ=0.5\psi = 0.5 in every direction, so ϕ1/2=1\phi_{1/2} = 1. Line L5 gives R=0.9⋅(1−0)=0.9R = 0.9 \cdot (1 - 0) = 0.9. The source is flat, so the diffusion solution has no gradient: Σaf=R\Sigma_a f = R, f=0.9/0.1=9f = 0.9/0.1 = 9. Line L7 gives ϕ=10=q/Σa\phi = 10 = q/\Sigma_a, the exact solution, after one iteration. Source iteration needs 154 iterations to reach a change of 10−810^{-8} in the same problem.

The Fourier factors. In an infinite homogeneous medium the error mode cos⁡(λΣtx)\cos(\lambda\Sigma_tx) is multiplied by ωSI\omega_{SI} in each source iteration and by ω\omega in each DSA iteration (Theorem 5 of the proof). For c=1c = 1:

λ\lambda source iteration, continuous DSA, continuous source iteration, S8S_8 DSA, S8S_8
0 1 0 1 0
0.5 0.927295 0.054838 0.927295 0.054838
1 0.785398 0.141593 0.785397 0.141589
2.525 0.472756 0.224665 0.471030 0.222127
5 0.274680 0.187642 0.254061 0.164548
20 0.076042 0.069112 0.029043 0.021761

Source iteration is slow for small λ\lambda, the smooth modes. DSA is worst at λ≈2.5\lambda \approx 2.5, where neither the sweep nor diffusion is good, and the largest factor is 0.22470.2247. The largest S8S_8 factor over all λ\lambda is 0.2223860.222386.

Thick cells. Take c=0.99c = 0.99, S8S_8, 100 equal cells, vacuum faces and q=0q = 0. The Fourier columns are the largest factor over all modes on a reflecting slab of equal cells (Theorems 5 and 7 of the proof). The measured columns are the ratio of the last two changes after 40 iterations from a rough start (300 for source iteration).

Σth\Sigma_th consistent, Fourier consistent, measured inconsistent, Fourier inconsistent, measured source iteration, measured
0.1 0.2190 0.2118 0.2152 0.2082 0.9659
1 0.2190 0.2137 0.7370 0.7161 0.9900
10 0.2190 0.2141 42.42 40.37 0.9899

The consistent scheme has the same rate for every cell size. The inconsistent one is as good for thin cells, slow for cells of one mean free path, and diverges for thick cells. Its rate for thick cells is near c/(1−c+4/(3Σt2h2))c/(1 - c + 4/(3\Sigma_t^2h^2)), which exceeds 1 once Σth>1.1664\Sigma_th > 1.1664.

Iterations to convergence. With c=0.99c = 0.99, S8S_8, q=1q = 1, vacuum faces and tol=10−8\mathrm{tol} = 10^{-8}:

slab cells source iteration consistent DSA inconsistent DSA leakage at x=Lx = L
10 mean free paths 40 437 11 10 3.70841381
100 mean free paths 400 1356 10 10 5.13942747

The cells here are 0.25 mean free paths, so both DSA schemes work. In a heterogeneous slab of 60 cells with random Σt\Sigma_t in [0.5,20][0.5, 20], cc in [0.9,0.999][0.9, 0.999], widths in [0.05,2][0.05, 2] cm, one reflecting and one vacuum face, consistent DSA reaches tol=10−12\mathrm{tol} = 10^{-12} in 17 iterations, source iteration needs 2188, and inconsistent DSA overflows after 266 iterations.

Correctness

The proofs are in the proof file. If ϕ\phi is the diamond difference solution, the DSA error satisfies e↦(T+CΣs(T−I))ee \mapsto (T + C\Sigma_s(T - I))e, where CC is the diffusion solve of line L6. So the exact solution is a fixed point, and DSA converges to the same answer as source iteration whenever the spectral radius of this map is below 1 (Theorem 3). The edge system of line L6 is exactly the diamond difference P1 system with the current eliminated, and it is symmetric positive definite if some cell absorbs or some face is vacuum (Theorems 1 and 2). For S2S_2 with reflecting faces, P1 and S2S_2 are the same equations, so one DSA iteration gives the exact solution in any heterogeneous slab (Theorem 4). On equal cells with reflecting faces, the cosine modes are eigenvectors, the flat mode is removed in one step, and the eigenvalue is the factor of the SNS_N equations exact in space at λ=2tan⁡(θ/2)/(Σth)\lambda = 2\tan(\theta/2)/(\Sigma_th) (Theorem 5). From this, ∣ω∣≤3HNc<c|\omega| \le 3H_Nc < c for every cell size, with 3H8=0.37993H_8 = 0.3799 (Theorem 6). The inconsistent scheme has a different factor, which exceeds 1 for thick cells (Theorem 7).

Proof: Consistent DSA keeps the transport solution, its diffusion equation is the diamond difference P1 system, it is exact for S2 with reflection, and its Fourier factor is below the scattering ratio for every cell sizefull

We assume exact arithmetic. The slab, the cross sections and the lines L1 to L8 are those of the page. Cell jj has Σt,j>0\Sigma_{t,j} > 0, 0≤Σs,j≤Σt,j0 \le \Sigma_{s,j} \le \Sigma_{t,j}, the absorption Σa,j=Σt,j−Σs,j\Sigma_{a,j} = \Sigma_{t,j} - \Sigma_{s,j} and the width hj>0h_j > 0, and it lies between the edges jj and j+1j + 1. The directions and weights are the NN-point Gauss-Legendre rule with NN even, so the weights are positive, they sum to 2, and the rule is exact for μ2\mu^2: 12∑nwnμn2=13\tfrac12\sum_n w_n\mu_n^2 = \tfrac13.

As in the source iteration proof, line L4 with the source of line L3 is the affine map ϕ↦Tϕ+b\phi \mapsto T\phi + b. Here TT sweeps the source Σsv/2\Sigma_s v/2 with zero incoming data and the same reflection, and bb sweeps q/2q/2 with the given data. That proof shows that the sweep with exact reflection has exactly one solution (its Lemma 1) and that, if Σs,j<Σt,j\Sigma_{s,j} < \Sigma_{t,j} in every cell, the diamond difference equations have exactly one solution ϕ=Tϕ+b\phi = T\phi + b (its Theorem 3).

The diffusion equation for the correction

Write gj=2/(3Σt,jhj)g_j = 2/(3\Sigma_{t,j}h_j) and sj=Σa,jhj/2s_j = \Sigma_{a,j}h_j/2. The consistent correction of line L6 solves the edge system Af=dAf = d with the rows

(−gL+sL)fi−1+(gL+gR+sL+sR)fi+(−gR+sR)fi+1=RLhL+RRhR(1)(-g_L + s_L)f_{i-1} + (g_L + g_R + s_L + s_R)f_i + (-g_R + s_R)f_{i+1} = R_Lh_L + R_Rh_R \tag{1}

at the inner edge ii between the cells L=i−1L = i - 1 and R=iR = i. The face rows keep only the terms of the one cell next to the face, and a vacuum face adds 1 to its diagonal entry. The cell correction is (fj+fj+1)/2(f_j + f_{j+1})/2.

Theorem 1 (the edge system is symmetric positive definite). If some cell has Σa,j>0\Sigma_{a,j} > 0 or some face is vacuum, then AA is symmetric positive definite. So line L6 has exactly one solution, and the tridiagonal elimination without pivoting computes it.

Proof. Each cell adds the matrix gj(1−1−11)+sj(1111)g_j\begin{pmatrix}1 & -1\\ -1 & 1\end{pmatrix} + s_j\begin{pmatrix}1 & 1\\ 1 & 1\end{pmatrix} to the rows and columns of its two edges. This gives the entries of (1). So

fTAf=∑j[gj(fj+1−fj)2+sj(fj+fj+1)2]+∑vacuum facesfface2≥0.f^{\mathsf T}Af = \sum_j \left[g_j(f_{j+1} - f_j)^2 + s_j(f_j + f_{j+1})^2\right] + \sum_{\text{vacuum faces}} f_{\text{face}}^2 \ge 0 .

If the form is 0, then fj+1=fjf_{j+1} = f_j for every jj because gj>0g_j > 0, so ff is a constant FF. Then a cell with sj>0s_j > 0 or a vacuum face gives F=0F = 0. So AA is positive definite. The pivots of the elimination are ratios of leading principal minors, which are positive for such a matrix. □\square

Theorem 2 (the edge system is the diamond difference P1 system). Consider the P1 equations for a flux ff and a current JJ with one value at each edge, the source RjR_j in cell jj, and diamond difference in space:

Jj+1−Jj+Σa,jhj fj+fj+12=Rjhj,13(fj+1−fj)+Σt,jhj Jj+Jj+12=0,(2)J_{j+1} - J_j + \Sigma_{a,j}h_j\,\frac{f_j + f_{j+1}}{2} = R_jh_j, \qquad \frac13(f_{j+1} - f_j) + \Sigma_{t,j}h_j\,\frac{J_j + J_{j+1}}{2} = 0, \tag{2}

with J=0J = 0 at a reflecting face and the Marshak condition J0=−f0/2J_0 = -f_0/2 or JM=fM/2J_M = f_M/2 at a vacuum face. Then (f,J)(f, J) solves these equations if and only if ff solves the edge system, with JJ given by (3) below.

Proof. In cell jj, the two equations (2) give the sum and the difference of the currents at its edges: Jj+Jj+1=−gj(fj+1−fj)J_j + J_{j+1} = -g_j(f_{j+1} - f_j) and Jj+1−Jj=Rjhj−sj(fj+fj+1)J_{j+1} - J_j = R_jh_j - s_j(f_j + f_{j+1}). So the cell fixes the currents at its edges:

Jj=12[−gj(fj+1−fj)−Rjhj+sj(fj+fj+1)],Jj+1=12[−gj(fj+1−fj)+Rjhj−sj(fj+fj+1)].(3)J_j = \tfrac12\left[-g_j(f_{j+1} - f_j) - R_jh_j + s_j(f_j + f_{j+1})\right], \quad J_{j+1} = \tfrac12\left[-g_j(f_{j+1} - f_j) + R_jh_j - s_j(f_j + f_{j+1})\right]. \tag{3}

At an inner edge ii, the value from cell i−1i - 1 must equal the value from cell ii. Multiply this equation by 2 and move all terms to the left: this is (1). At a reflecting face x=0x = 0, J0=0J_0 = 0 in (3) gives (g0+s0)f0+(−g0+s0)f1=R0h0(g_0 + s_0)f_0 + (-g_0 + s_0)f_1 = R_0h_0, the face row. At a vacuum face, J0=−f0/2J_0 = -f_0/2 gives the same row with 11 added to the coefficient of f0f_0. The face x=Lx = L is the mirror case. Each step can be reversed, so the two systems have the same solutions. □\square

The Marshak condition is the P1 form of "no particles enter": with ψ=f/2+3Jμ/2\psi = f/2 + 3J\mu/2, the incoming current at x=0x = 0 is ∫01μψ dμ=f/4+J/2\int_0^1 \mu\psi\,d\mu = f/4 + J/2, and it is zero when J=−f/2J = -f/2.

The same fixed point

Theorem 3 (DSA keeps the transport solution). Let ϕ\phi be the diamond difference solution, ϕ=Tϕ+b\phi = T\phi + b, and let CC be the linear map from the residual RR to the correction of line L6. The error e(l)=ϕ(l)−ϕe^{(l)} = \phi^{(l)} - \phi of DSA satisfies

e(l+1)=(T+CΣs(T−I))e(l).e^{(l+1)} = \left(T + C\Sigma_s(T - I)\right)e^{(l)}.

So ϕ\phi is a fixed point of lines L3 to L7, and the iteration converges to ϕ\phi from every start if and only if the spectral radius of T+CΣs(T−I)T + C\Sigma_s(T - I) is less than 1.

Proof. Line L4 gives ϕ(l+1/2)=Tϕ(l)+b\phi^{(l+1/2)} = T\phi^{(l)} + b, so ϕ(l+1/2)−ϕ=Te(l)\phi^{(l+1/2)} - \phi = Te^{(l)}. Line L5 gives R=Σs(ϕ(l+1/2)−ϕ(l))=Σs(Te(l)−e(l))R = \Sigma_s(\phi^{(l+1/2)} - \phi^{(l)}) = \Sigma_s(Te^{(l)} - e^{(l)}). Line L7 adds CRCR and subtracts nothing else. So e(l+1)=Te(l)+CΣs(T−I)e(l)e^{(l+1)} = Te^{(l)} + C\Sigma_s(T - I)e^{(l)}. The last claim is the standard fact for a linear iteration with a fixed matrix. □\square

Theorem 4 (S2 with reflection is exact in one iteration). Let N=2N = 2, let both faces reflect, let every cell have Σs,j<Σt,j\Sigma_{s,j} < \Sigma_{t,j}, and let the cross sections and widths vary from cell to cell. Then from any ϕ(0)\phi^{(0)}, one DSA iteration gives the diamond difference solution ϕ\phi.

Proof. Let δ=ϕ−ϕ(l+1/2)\delta = \phi - \phi^{(l+1/2)}. Since ϕ=Tϕ+b\phi = T\phi + b and ϕ(l+1/2)=Tϕ(l)+b\phi^{(l+1/2)} = T\phi^{(l)} + b, we have δ=T(ϕ−ϕ(l))=Tδ+T(ϕ(l+1/2)−ϕ(l))\delta = T(\phi - \phi^{(l)}) = T\delta + T(\phi^{(l+1/2)} - \phi^{(l)}). Here T(ϕ(l+1/2)−ϕ(l))T(\phi^{(l+1/2)} - \phi^{(l)}) is the sweep of the source R/2R/2. So δ\delta is the scalar flux of the diamond difference S2S_2 problem with scattering Σs\Sigma_s, the source RR in place of qq and reflection at both faces. By the source iteration proof, this problem has exactly one solution.

The S2S_2 rule has μ=±1/3\mu = \pm 1/\sqrt3 and the weights 1 and 1. Write f=ψ++ψ−f = \psi_+ + \psi_- and J=μ(ψ+−ψ−)J = \mu(\psi_+ - \psi_-) at each edge. The diamond difference equations of the two directions in cell jj are ±μ(ψ±,j+1−ψ±,j)/hj+Σt,jψˉ±=(Σs,jfˉ+Rj)/2\pm\mu(\psi_{\pm,j+1} - \psi_{\pm,j})/h_j + \Sigma_{t,j}\bar\psi_\pm = (\Sigma_{s,j}\bar f + R_j)/2, with the cell values ψˉ±\bar\psi_\pm equal to the mean of the edge values. Their sum, times hjh_j, is the first equation (2). Their difference, times μhj\mu h_j and with μ2=1/3\mu^2 = 1/3, is the second equation (2). Reflection ψ+=ψ−\psi_+ = \psi_- is J=0J = 0. The change of variables is invertible, ψ±=(f±J/μ)/2\psi_\pm = (f \pm J/\mu)/2. So the S2S_2 problem and the P1 problem (2) are the same system. By Theorem 2 its ff solves the edge system, and its cell flux is the mean of the edge values, which is the correction of line L6. Since Σa>0\Sigma_a > 0, Theorem 1 says that this correction is unique. So CR=δCR = \delta, and line L7 gives ϕ(l+1/2)+δ=ϕ\phi^{(l+1/2)} + \delta = \phi. □\square

With vacuum faces the claim fails: S2S_2 with no incoming particles gives J0=−f0/3J_0 = -f_0/\sqrt3, while line L6 uses the Marshak value J0=−f0/2J_0 = -f_0/2. The tests measure a relative error of about 2 percent after one step in that case.

Fourier analysis

Theorem 5 (the factor of consistent DSA). Let the slab have MM equal cells of width hh with the same Σt\Sigma_t and Σs\Sigma_s, let c=Σs/Σt<1c = \Sigma_s/\Sigma_t < 1, and let both faces reflect. For k=0,…,M−1k = 0, \ldots, M - 1 let θ=πk/M\theta = \pi k/M, ej=cos⁡(θ(j+1/2))e_j = \cos(\theta(j + 1/2)) and λ=2tan⁡(θ/2)/(Σth)\lambda = 2\tan(\theta/2)/(\Sigma_th). Then ee is an eigenvector of the DSA error map of Theorem 3 with the eigenvalue

ω(λ)=ωSI(λ)−c (1−ωSI(λ))1−c+λ2/3,ωSI(λ)=c2∑nwn1+λ2μn2.\omega(\lambda) = \omega_{SI}(\lambda) - \frac{c\,(1 - \omega_{SI}(\lambda))}{1 - c + \lambda^2/3}, \qquad \omega_{SI}(\lambda) = \frac c2\sum_n \frac{w_n}{1 + \lambda^2\mu_n^2}.

Proof. By Theorem 5 of the source iteration proof, Te=ωSIeTe = \omega_{SI}e. So R=Σs(ωSI−1)e=R^ eR = \Sigma_s(\omega_{SI} - 1)e = \hat R\,e with R^=Σs(ωSI−1)\hat R = \Sigma_s(\omega_{SI} - 1). Try the edge values fi=Fcos⁡(θi)f_i = F\cos(\theta i) for i=0,…,Mi = 0, \ldots, M. All cells have the same gg and ss. Since cos⁡(θ(i−1))+cos⁡(θ(i+1))=2cos⁡θcos⁡(θi)\cos(\theta(i - 1)) + \cos(\theta(i + 1)) = 2\cos\theta\cos(\theta i), the left side of (1) is

Fcos⁡(θi)[2(g+s)+2(s−g)cos⁡θ]=Fcos⁡(θi)[4gsin⁡2(θ/2)+4scos⁡2(θ/2)],F\cos(\theta i)\left[2(g + s) + 2(s - g)\cos\theta\right] = F\cos(\theta i)\left[4g\sin^2(\theta/2) + 4s\cos^2(\theta/2)\right],

and since cos⁡(θ(i−12))+cos⁡(θ(i+12))=2cos⁡(θ/2)cos⁡(θi)\cos(\theta(i - \tfrac12)) + \cos(\theta(i + \tfrac12)) = 2\cos(\theta/2)\cos(\theta i), the right side is 2hR^cos⁡(θ/2)cos⁡(θi)2h\hat R\cos(\theta/2)\cos(\theta i). At the face i=0i = 0 both sides are half of these values with cos⁡0=1\cos 0 = 1, and at i=Mi = M they are half of them with cos⁡(θM)=(−1)k\cos(\theta M) = (-1)^k. So every row holds if

F[4gsin⁡2(θ/2)+2Σahcos⁡2(θ/2)]=2hR^cos⁡(θ/2).F\left[4g\sin^2(\theta/2) + 2\Sigma_ah\cos^2(\theta/2)\right] = 2h\hat R\cos(\theta/2).

Since 0≤θ<π0 \le \theta < \pi, cos⁡(θ/2)>0\cos(\theta/2) > 0 and the bracket is positive, so this fixes FF. By Theorem 1 (here Σa>0\Sigma_a > 0) it is the solution. The cell correction is F(cos⁡(θj)+cos⁡(θ(j+1)))/2=Fcos⁡(θ/2) ejF(\cos(\theta j) + \cos(\theta(j + 1)))/2 = F\cos(\theta/2)\,e_j. Divide the equation for FF by cos⁡(θ/2)\cos(\theta/2) and use g=2/(3Σth)g = 2/(3\Sigma_th):

Fcos⁡(θ/2)=2hR^8tan⁡2(θ/2)/(3Σth)+2Σah=R^Σt(λ2/3+1−c)=c(ωSI−1)λ2/3+1−c.F\cos(\theta/2) = \frac{2h\hat R}{8\tan^2(\theta/2)/(3\Sigma_th) + 2\Sigma_ah} = \frac{\hat R}{\Sigma_t(\lambda^2/3 + 1 - c)} = \frac{c(\omega_{SI} - 1)}{\lambda^2/3 + 1 - c}.

Line L7 adds this to ωSIe\omega_{SI}e. □\square

The MM vectors ee are orthogonal, so they are all the eigenvectors in this case, and the spectral radius is the largest ∣ω∣|\omega| over the MM values of θ\theta.

Corollary (the flat mode). For k=0k = 0, λ=0\lambda = 0 and ωSI=c\omega_{SI} = c, so ω=c−c(1−c)/(1−c)=0\omega = c - c(1 - c)/(1 - c) = 0. The mode that source iteration damps the least, by the factor cc, is removed in one step. In particular, in a homogeneous slab with both faces reflecting and a flat source, one DSA iteration from a flat start gives the exact solution.

Write a(λ)=12∑nwn/(1+λ2μn2)a(\lambda) = \tfrac12\sum_n w_n/(1 + \lambda^2\mu_n^2), so ωSI=ca\omega_{SI} = ca. A short computation gives the second form

ω(λ)=c[a(λ)(1+λ2/3)−1]1−c+λ2/3.(4)\omega(\lambda) = \frac{c\left[a(\lambda)(1 + \lambda^2/3) - 1\right]}{1 - c + \lambda^2/3}. \tag{4}

The factor of the diamond difference scheme is the factor of the SNS_N equations exact in space, evaluated at λ=2tan⁡(θ/2)/(Σth)\lambda = 2\tan(\theta/2)/(\Sigma_th). As θ\theta runs over [0,π)[0, \pi), λ\lambda runs over [0,∞)[0, \infty) for every cell size. As N→∞N \to \infty, a(λ)→arctan⁡(λ)/λa(\lambda) \to \arctan(\lambda)/\lambda, which gives the factor of the continuous problem.

Theorem 6 (a bound for every cell size). Let HN=12∑μn2<1/3wn(1/3−μn2)H_N = \tfrac12\sum_{\mu_n^2 < 1/3} w_n(1/3 - \mu_n^2). Then HN<1/3H_N < 1/3, and for every λ≥0\lambda \ge 0 and c≤1c \le 1 (with λ>0\lambda > 0 if c=1c = 1),

∣ω(λ)∣≤3HN c<c.|\omega(\lambda)| \le 3H_N\,c < c .

So consistent DSA on equal cells with reflecting faces converges for every cell thickness Σth\Sigma_th, with a rate that does not depend on it.

Proof. Since 12∑nwn=1\tfrac12\sum_n w_n = 1,

a(λ)(1+λ2/3)−1=12∑nwn(1+λ2/3)−(1+λ2μn2)1+λ2μn2=λ2 p(λ),p(λ)=12∑nwn1/3−μn21+λ2μn2.a(\lambda)(1 + \lambda^2/3) - 1 = \frac12\sum_n w_n\frac{(1 + \lambda^2/3) - (1 + \lambda^2\mu_n^2)}{1 + \lambda^2\mu_n^2} = \lambda^2\,p(\lambda), \qquad p(\lambda) = \frac12\sum_n w_n\frac{1/3 - \mu_n^2}{1 + \lambda^2\mu_n^2}.

The terms with μn2<1/3\mu_n^2 < 1/3 are positive and at most 12wn(1/3−μn2)\tfrac12 w_n(1/3 - \mu_n^2), so p≤HNp \le H_N. The terms with μn2>1/3\mu_n^2 > 1/3 are negative and at least −12wn(μn2−1/3)-\tfrac12 w_n(\mu_n^2 - 1/3). Since the rule is exact for μ2\mu^2, 12∑nwn(1/3−μn2)=0\tfrac12\sum_n w_n(1/3 - \mu_n^2) = 0, so the sum of the numbers 12wn(μn2−1/3)\tfrac12 w_n(\mu_n^2 - 1/3) over μn2>1/3\mu_n^2 > 1/3 is HNH_N, and p≥−HNp \ge -H_N. With (4) and 1−c+λ2/3≥λ2/31 - c + \lambda^2/3 \ge \lambda^2/3, ∣ω∣≤cλ2HN/(λ2/3)=3HNc|\omega| \le c\lambda^2H_N/(\lambda^2/3) = 3H_Nc. For λ=0\lambda = 0 and c<1c < 1, ω=0\omega = 0. Finally, HN≤13⋅12∑μn2<1/3wn≤13H_N \le \tfrac13\cdot\tfrac12\sum_{\mu_n^2 < 1/3} w_n \le \tfrac13, and equality would need all the weight at μ=0\mu = 0; for NN even no node is 0, so HN<1/3H_N < 1/3. □\square

For N=2N = 2, μ2=1/3\mu^2 = 1/3, so H2=0H_2 = 0 and ω≡0\omega \equiv 0, which agrees with Theorem 4. The values of 3HN3H_N are 0.4260 for S4S_4, 0.3799 for S8S_8 and 0.3862 for S16S_{16}, and 2/(33)=0.38492/(3\sqrt3) = 0.3849 for the continuous rule. The true maximum of ∣ω∣|\omega| is smaller. For the continuous factor with c=1c = 1, the maximum over a fine grid of λ\lambda is 0.22470.2247 near λ=2.5\lambda = 2.5; this value is a computation, not part of the theorem.

Theorem 7 (the inconsistent scheme). With the same slab and modes, the cell-centered correction of the page (face coefficient D/hD/h between equal cells, no current at a reflecting face) has the eigenvalue

ωI(θ)=ωSI(λ)−c (1−ωSI(λ))1−c+4sin⁡2(θ/2)/(3τ2),τ=Σth.\omega_I(\theta) = \omega_{SI}(\lambda) - \frac{c\,(1 - \omega_{SI}(\lambda))}{1 - c + 4\sin^2(\theta/2)/(3\tau^2)}, \qquad \tau = \Sigma_th .

If c>1/2c > 1/2 and τ>2/3(2c−1)\tau > 2/\sqrt{3(2c - 1)}, then ∣ωI∣>1|\omega_I| > 1 for θ\theta close enough to π\pi, so the iteration diverges on fine enough meshes of thick cells.

Proof. The cell row is Σahfj+(D/h)(2fj−fj−1−fj+1)=Rjh\Sigma_ahf_j + (D/h)(2f_j - f_{j-1} - f_{j+1}) = R_jh with D=1/(3Σt)D = 1/(3\Sigma_t). At a reflecting face the missing neighbor has the same value as the cell, which holds for ee because cos⁡(−θ/2)=cos⁡(θ/2)\cos(-\theta/2) = \cos(\theta/2) and cos⁡(θ(M+12))=cos⁡(θ(M−12))\cos(\theta(M + \tfrac12)) = \cos(\theta(M - \tfrac12)) for θ=πk/M\theta = \pi k/M. With f=f^ef = \hat fe every row becomes f^[Σah+4Dsin⁡2(θ/2)/h]=R^h\hat f[\Sigma_ah + 4D\sin^2(\theta/2)/h] = \hat Rh, so f^=R^/(Σt(1−c+4sin⁡2(θ/2)/(3τ2)))\hat f = \hat R/(\Sigma_t(1 - c + 4\sin^2(\theta/2)/(3\tau^2))), and R^=Σs(ωSI−1)\hat R = \Sigma_s(\omega_{SI} - 1) as before. As θ→π\theta \to \pi, λ→∞\lambda \to \infty and ωSI→0\omega_{SI} \to 0 because no node is 0, so ωI→−c/(1−c+4/(3τ2))\omega_I \to -c/(1 - c + 4/(3\tau^2)). This has absolute value greater than 1 exactly when τ2>4/(3(2c−1))\tau^2 > 4/(3(2c - 1)). By continuity, ∣ωI∣>1|\omega_I| > 1 for θ\theta near π\pi, and these values of θ=πk/M\theta = \pi k/M occur once MM is large. □\square

Since 4sin⁡2(θ/2)/τ2=λ2cos⁡2(θ/2)4\sin^2(\theta/2)/\tau^2 = \lambda^2\cos^2(\theta/2), the two schemes agree for smooth modes, θ→0\theta \to 0. They differ for the modes that change sign from cell to cell. In thick cells these modes have ωSI\omega_{SI} near 0, and the consistent scheme gives them almost no correction while the inconsistent one gives them a correction of nearly −c/(1−c)-c/(1 - c) times their size. For c=0.99c = 0.99 the threshold is τ>1.1664\tau > 1.1664.

Complexity

In the real RAM model, one iteration is one sweep, O(NM)O(NM) operations, plus one tridiagonal solve, O(M)O(M) operations. So II iterations cost O(INM)O(INM), and the diffusion solve adds only a fraction of about 1/N1/N to the work of source iteration. The gain is in II. On equal cells with reflecting faces, Theorem 6 bounds the rate by 0.38c0.38c for S8S_8, so II is at most about ln⁡(tol)/ln⁡(0.38)\ln(\mathrm{tol})/\ln(0.38), which is 19 for tol=10−8\mathrm{tol} = 10^{-8}, independent of cc, MM and the cell size. The measured counts are 10 to 11, against 437 to 1356 for source iteration. The extra memory is O(M)O(M) for the residual, the tridiagonal system and the correction.

Properties

  • The same answer. DSA changes the iteration, not the equations. Its fixed point is the diamond difference solution of source iteration (Theorem 3).
  • A rate that does not depend on the scattering ratio. On equal cells with reflecting faces, the error falls by at most 0.38c0.38c per iteration for S8S_8 (Theorem 6). The measured rate is about 0.22c0.22c, which matches the Fourier maximum 0.2247c0.2247c of the continuous problem.
  • Unconditional stability. With the consistent diffusion equation, the bound holds for every cell thickness (Theorem 6). The inconsistent equation diverges once Σth>2/3(2c−1)\Sigma_th > 2/\sqrt{3(2c - 1)} on fine meshes (Theorem 7).
  • Exact for S2 with reflection. P1 and S2S_2 are the same equations, so the correction is the exact error (Theorem 4).
  • Cheap. One tridiagonal solve per sweep in a slab. In several dimensions, the diffusion solve is a sparse elliptic problem, which multigrid or conjugate gradients solve at a cost small next to a sweep, as a rule of thumb.

Variants and improvements

  • Inconsistent DSA. A diffusion equation discretized on its own, such as the cell-centered scheme of the implementation, is simpler and works for cells thinner than about one mean free path (Theorem 7).
  • Larsen's four-step method. Larsen (1982) derived consistent diffusion equations for other transport discretizations by taking the P1 moments of the discrete transport equations, as Theorem 2 does for diamond difference. For linear discontinuous and multidimensional schemes the fully consistent equation is large, and partially consistent schemes are used, as described in the review of Adams and Larsen (2002).
  • DSA as a preconditioner. Write the transport problem as (I−T)ϕ=b(I - T)\phi = b and use the diffusion solve as a preconditioner for GMRES. Warsa, Wareing and Morel (2004) found that this keeps the speed in multidimensional problems with strong material discontinuities, where plain DSA loses much of its effectiveness.
  • Other low-order operators. Transport synthetic acceleration uses a cheap low-order transport problem, such as S2S_2, in place of diffusion. Nonlinear schemes such as coarse mesh finite difference use a diffusion problem whose coefficients are updated from the transport solution. Both appear in the review of Adams and Larsen (2002).
  • Energy groups and eigenvalues. In the multigroup approximation DSA accelerates the inner iteration of each group. For the k-eigenvalue problem a similar diffusion correction accelerates the outer power iteration.

When to use it

Use DSA when source iteration is slow: cc near 1 and a slab many mean free paths thick, as in reactor cores with water, or in radiative transfer in optically thick matter. Derive the diffusion equation from the transport discretization, as in line L6, or at least check its Fourier factor for thick cells. In multidimensional problems with strong material discontinuities, use DSA as a preconditioner for a Krylov method. When the problem is thin or strongly absorbing, plain source iteration is already fast and DSA brings little.

Pitfalls

  • An inconsistent diffusion equation (line L6). A diffusion scheme that is correct for diffusion can still make DSA diverge. In the example the cell-centered scheme has the rate 40 for cells of 10 mean free paths. Test the scheme with thick cells.
  • The wrong face condition (line L6). Vacuum faces need the Marshak row. With a zero flux face condition the correction is wrong near the face, and the edge system does not match the P1 equations.
  • A singular system. With c=1c = 1 in every cell and both faces reflecting, the edge system is singular (Theorem 1), and so is the transport problem. Fix the flux level by another condition.
  • Negative flux fixups. A fixup in the sweep makes the transport scheme nonlinear and no longer consistent with line L6. As a rule of thumb, the rate then becomes worse in thick cells.
  • The sign of the residual (line L5). The source of the diffusion problem is Σs(ϕ1/2−ϕ)\Sigma_s(\phi_{1/2} - \phi), the change of the scattering source, not the change of the flux.
  • The weight convention. With weights that sum to 2, the scattering source is (Σsϕ+q)/2(\Sigma_s\phi + q)/2 and the diffusion equation is written for ϕ\phi. Other conventions need matching factors in lines L3 and L6.

Implementations

Each implementation follows the pseudocode above. A comment @line links a line of code to a line of the pseudocode. Point at a line of the pseudocode to see the matching code. The build runs the native tests of each language.

diffusion_synthetic_acceleration.pyI0RawGitHub
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/diffusion-synthetic-acceleration/. The authors are listed in prov/XAVARJ.jsonl.
"""Diffusion synthetic acceleration (DSA) of source iteration for the slab S_N equations with diamond differences.

The slab has M cells with sigma_t[j], sigma_s[j], an isotropic source q[j]
and widths dx[j]. The scalar flux is phi = integral of psi over mu in
[-1, 1], so the emission per unit mu is (sigma_s phi + q) / 2 and the
Gauss-Legendre weights sum to 2. Faces are "vacuum" or "reflect".

One DSA iteration:
    1. sweep with the source of the last iterate: phi_half
    2. the residual R_j = sigma_s,j (phi_half_j - phi_j)
    3. solve a diffusion equation for the correction f with the source R
    4. phi_new = phi_half + f

accel = "consistent": the diffusion equation for the edge values f_i is the
one obtained from the diamond difference P1 equations by eliminating the
current (the method of Alcouffe 1977). With g = 2 / (3 sigma_t dx) and the
absorption sigma_a = sigma_t - sigma_s, the row of edge i between the cells
L (left) and R (right) is

    (-g_L + sa_L dx_L / 2) f_(i-1) + (g_L + g_R + sa_L dx_L / 2 + sa_R dx_R / 2) f_i
        + (-g_R + sa_R dx_R / 2) f_(i+1) = R_L dx_L + R_R dx_R.

A vacuum face adds 1 to its diagonal entry (the Marshak condition
J_out = f / 2), and a reflecting face adds nothing. The cell correction is
the mean of the two edge values.

accel = "inconsistent": the usual cell-centered diffusion equation with the
face coefficient 1 / (dx_L / (2 D_L) + dx_R / (2 D_R)), D = 1 / (3 sigma_t).
This is a correct discretization of diffusion, but not the one consistent with
diamond difference, and it diverges for thick cells.

accel = "none": plain source iteration.
"""

import math

from scisparse import thomas


def legendre(n: int, x: float):
    if n == 0:
        return 1.0, 0.0
    p0, p1 = 1.0, x
    for k in range(2, n + 1):
        p0, p1 = p1, ((2 * k - 1) * x * p1 - (k - 1) * p0) / k
    return p1, n * (x * p1 - p0) / (x * x - 1.0)


def gauss_legendre(n: int):
    """Nodes in increasing order and weights of the n-point Gauss-Legendre rule on [-1, 1]."""
    nodes, weights = [0.0] * n, [0.0] * n
    for i in range((n + 1) // 2):
        x = math.cos(math.pi * (i + 0.75) / (n + 0.5))
        for _ in range(100):
            p, dp = legendre(n, x)
            step = p / dp
            x -= step
            if abs(step) < 1e-16:
                break
        p, dp = legendre(n, x)
        w = 2.0 / ((1.0 - x * x) * dp * dp)
        nodes[n - 1 - i], weights[n - 1 - i] = x, w
        nodes[i], weights[i] = -x, w
    return nodes, weights


def _dd(mu, psi, sigma_t, src, dx):
    m = len(dx)
    cells = [0.0] * m
    for j in (range(m) if mu > 0 else range(m - 1, -1, -1)):
        a = 2.0 * abs(mu) / dx[j]
        cells[j] = (src[j] + a * psi) / (sigma_t[j] + a)
        psi = 2.0 * cells[j] - psi
    return cells, psi


def sweep(mus, ws, sigma_t, src, dx, left="vacuum", right="vacuum"):
    """Scalar flux of one diamond difference sweep of all directions; reflection is exact. Returns (phi, leak_left, leak_right)."""
    n, m = len(mus), len(dx)
    phi = [0.0] * m
    leak_l = leak_r = 0.0
    for k in range(n // 2, n):
        mu, w = mus[k], ws[k]
        if left == "reflect" and right == "reflect":
            _, s1 = _dd(mu, 0.0, sigma_t, src, dx)
            _, t2 = _dd(-mu, 0.0, sigma_t, src, dx)
            r = 1.0
            for st, h in zip(sigma_t, dx):
                tau = st * h / mu
                r *= (2.0 - tau) / (2.0 + tau)
            fwd, out_r = _dd(mu, (r * s1 + t2) / (1.0 - r * r), sigma_t, src, dx)
            bwd, out_l = _dd(-mu, out_r, sigma_t, src, dx)
        elif left == "reflect":
            bwd, out_l = _dd(-mu, 0.0, sigma_t, src, dx)
            fwd, out_r = _dd(mu, out_l, sigma_t, src, dx)
        else:
            fwd, out_r = _dd(mu, 0.0, sigma_t, src, dx)
            bwd, out_l = _dd(-mu, out_r if right == "reflect" else 0.0, sigma_t, src, dx)
        for j in range(m):
            phi[j] += w * (fwd[j] + bwd[j])
        if right != "reflect":
            leak_r += w * mu * out_r
        if left != "reflect":
            leak_l += w * mu * out_l
    return phi, leak_l, leak_r


def consistent_correction(sigma_t, sigma_s, dx, res, left="vacuum", right="vacuum"):
    """Cell corrections from the edge diffusion equation with the source res (the residual sigma_s (phi_half - phi))."""
    m = len(dx)
    n = m + 1
    a, b, c, d = [0.0] * n, [0.0] * n, [0.0] * n, [0.0] * n
    for j in range(m):
        g = 2.0 / (3.0 * sigma_t[j] * dx[j])
        sa = (sigma_t[j] - sigma_s[j]) * dx[j] / 2.0
        # cell j couples its left edge j and its right edge j + 1
        b[j] += g + sa
        c[j] += -g + sa
        a[j + 1] += -g + sa
        b[j + 1] += g + sa
        d[j] += res[j] * dx[j]
        d[j + 1] += res[j] * dx[j]
    if left == "vacuum":
        b[0] += 1.0
    if right == "vacuum":
        b[m] += 1.0
    f = thomas(a, b, c, d)
    return [(f[j] + f[j + 1]) / 2.0 for j in range(m)]


def inconsistent_correction(sigma_t, sigma_s, dx, res, left="vacuum", right="vacuum"):
    """Cell corrections from the cell-centered diffusion equation with Marshak or reflecting faces."""
    m = len(dx)
    D = [1.0 / (3.0 * s) for s in sigma_t]
    a, b, c, d = [0.0] * m, [0.0] * m, [0.0] * m, [0.0] * m
    for j in range(m):
        b[j] = (sigma_t[j] - sigma_s[j]) * dx[j]
        d[j] = res[j] * dx[j]
    for j in range(m - 1):
        g = 1.0 / (dx[j] / (2.0 * D[j]) + dx[j + 1] / (2.0 * D[j + 1]))
        b[j] += g
        b[j + 1] += g
        c[j] -= g
        a[j + 1] -= g
    if left == "vacuum":
        b[0] += 1.0 / (dx[0] / (2.0 * D[0]) + 2.0)
    if right == "vacuum":
        b[-1] += 1.0 / (dx[-1] / (2.0 * D[-1]) + 2.0)
    return thomas(a, b, c, d)


def dsa(n, sigma_t, sigma_s, q, dx, left="vacuum", right="vacuum", accel="consistent",
        tol=1e-10, max_iter=10000, phi0=None):
    """Source iteration with diffusion synthetic acceleration.

    Returns a dict with phi, the leakages, the number of iterations and the list of the largest changes."""
    mus, ws = gauss_legendre(n)
    m = len(dx)
    phi = list(phi0) if phi0 is not None else [0.0] * m  # @line L1
    changes = []
    leak_l = leak_r = 0.0
    it = 0
    for it in range(1, max_iter + 1):  # @line L2
        src = [(sigma_s[j] * phi[j] + q[j]) / 2.0 for j in range(m)]  # @line L3
        half, leak_l, leak_r = sweep(mus, ws, sigma_t, src, dx, left, right)  # @line L4
        res = [sigma_s[j] * (half[j] - phi[j]) for j in range(m)]  # @line L5
        if accel == "consistent":
            corr = consistent_correction(sigma_t, sigma_s, dx, res, left, right)  # @line L6
        elif accel == "inconsistent":
            corr = inconsistent_correction(sigma_t, sigma_s, dx, res, left, right)
        else:
            corr = [0.0] * m
        new = [half[j] + corr[j] for j in range(m)]  # @line L7
        change = max(abs(new[j] - phi[j]) for j in range(m))
        phi = new
        changes.append(change)
        if not math.isfinite(change) or change <= tol * (max(abs(v) for v in phi) or 1.0):  # @line L8
            break
    return {"phi": phi, "leak_left": leak_l, "leak_right": leak_r, "iterations": it, "changes": changes}


def fourier_si(lam, c, mus=None, ws=None):
    """Source iteration factor (c / 2) sum w / (1 + lam^2 mu^2); with mus = None the exact angular integral c atan(lam) / lam."""
    if mus is None:
        return c if lam == 0.0 else c * math.atan(lam) / lam
    return 0.5 * c * sum(w / (1.0 + (lam * mu) ** 2) for mu, w in zip(mus, ws))


def fourier_dsa(lam, c, mus=None, ws=None):
    """DSA factor omega_SI - c (1 - omega_SI) / (1 - c + lam^2 / 3) for the mode with the wave number lam sigma_t."""
    if lam == 0.0:
        return 0.0  # the flat mode is removed exactly; this is also the limit for c = 1
    w = fourier_si(lam, c, mus, ws)
    return w - c * (1.0 - w) / (1.0 - c + lam * lam / 3.0)


def fourier_dsa_dd(theta, c, tau, mus, ws):
    """Consistent DSA with diamond difference on equal cells of optical thickness tau, for the mode cos(theta (j + 1/2))."""
    return fourier_dsa(2.0 * math.tan(theta / 2.0) / tau, c, mus, ws)


def fourier_dsa_inconsistent(theta, c, tau, mus, ws):
    """Inconsistent cell-centered DSA with diamond difference, for the same mode."""
    w = fourier_si(2.0 * math.tan(theta / 2.0) / tau, c, mus, ws)
    return w - c * (1.0 - w) / (1.0 - c + 4.0 * math.sin(theta / 2.0) ** 2 / (3.0 * tau * tau))


def spectral_radius_dsa(c, mus=None, ws=None, samples=4000, lam_max=50.0):
    """The largest |fourier_dsa| on a grid of lam in [0, lam_max]. Returns (radius, lam at the maximum)."""
    best, at = 0.0, 0.0
    for i in range(samples + 1):
        lam = lam_max * i / samples
        v = abs(fourier_dsa(lam, c, mus, ws))
        if v > best:
            best, at = v, lam
    return best, at
scisparse.py 189 lines
# SPDX-License-Identifier: MIT
# Shared helper of the scientific-computing pages of https://algorithm.tamnd.com/. The authors are listed in prov/.

"""Tridiagonal solvers, sparse matrices and simple iterative solvers.

A vector is a list. A sparse matrix is a list of rows, and each row is a
dict that maps a column index to a value. Entries may be int, float or
fractions.Fraction. thomas, thomas_periodic, spmatvec, from_dense and
to_dense keep Fraction entries exact. cg takes square roots and works in
float.

API (fixed; other pages copy this file byte for byte):
    thomas(a, b, c, d)             solve a tridiagonal system. Row i reads
                                   a[i] x[i-1] + b[i] x[i] + c[i] x[i+1] = d[i];
                                   a[0] and c[n-1] are not used.
    thomas_periodic(a, b, c, d)    solve a cyclic tridiagonal system: as
                                   thomas, but a[0] multiplies x[n-1] and
                                   c[n-1] multiplies x[0]. Uses the
                                   Sherman-Morrison formula. n >= 3.
    from_dense(A)                  sparse copy of a dense matrix (drops zeros)
    to_dense(A, ncols=None)        dense copy of a sparse matrix
    spmatvec(A, x)                 A times x
    poisson2d(nx, ny, dx, dy, bc="dirichlet")
                                   the matrix of MINUS the 5-point Laplacian
                                   on nx by ny unknowns, numbered
                                   k = i + nx * j (i along x, j along y).
                                   bc = "dirichlet": unknowns at the interior
                                   nodes ((i + 1) dx, (j + 1) dy) with zero
                                   values outside; the matrix is symmetric
                                   positive definite.
                                   bc = "periodic": the grid wraps around in
                                   both directions; the matrix is symmetric
                                   positive semidefinite, and its null space
                                   is the constant vectors.
                                   bc = "neumann": unknowns at cell centers,
                                   zero normal derivative at the walls (a
                                   missing neighbor drops out of the
                                   stencil); null space as for "periodic".
    cg(A, b, tol=1e-10, maxit=None, x0=None)
                                   conjugate gradient for a symmetric
                                   positive (semi)definite A. Stops when
                                   ||b - A x||_2 <= tol * ||b||_2. Returns
                                   (x, iterations). For a singular A, b must
                                   be in the range of A (for the Poisson
                                   matrices above: sum(b) = 0).
    gauss_seidel(A, b, iters, x0=None)
                                   iters forward Gauss-Seidel sweeps; every
                                   row needs a nonzero diagonal entry.
"""

import math


def thomas(a, b, c, d):
    n = len(d)
    if n == 0:
        return []
    cp = [0] * n
    dp = [0] * n
    cp[0] = c[0] / b[0] if n > 1 else 0
    dp[0] = d[0] / b[0]
    for i in range(1, n):
        m = b[i] - a[i] * cp[i - 1]
        if i < n - 1:
            cp[i] = c[i] / m
        dp[i] = (d[i] - a[i] * dp[i - 1]) / m
    x = [0] * n
    x[n - 1] = dp[n - 1]
    for i in range(n - 2, -1, -1):
        x[i] = dp[i] - cp[i] * x[i + 1]
    return x


def thomas_periodic(a, b, c, d):
    """Cyclic tridiagonal solve. A = T + u v^T with u = (gamma, 0, .., 0, c[n-1])
    and v = (1, 0, .., 0, a[0] / gamma), so T is tridiagonal."""
    n = len(d)
    if n < 3:
        raise ValueError("thomas_periodic needs n >= 3")
    gamma = -b[0]
    bb = list(b)
    bb[0] = b[0] - gamma
    bb[n - 1] = b[n - 1] - c[n - 1] * a[0] / gamma
    y = thomas(a, bb, c, d)
    u = [0] * n
    u[0] = gamma
    u[n - 1] = c[n - 1]
    z = thomas(a, bb, c, u)
    vy = y[0] + a[0] * y[n - 1] / gamma
    vz = z[0] + a[0] * z[n - 1] / gamma
    f = vy / (1 + vz)
    return [y[i] - f * z[i] for i in range(n)]


def from_dense(A):
    return [{j: v for j, v in enumerate(row) if v != 0} for row in A]


def to_dense(A, ncols=None):
    if ncols is None:
        ncols = len(A)
    out = []
    for row in A:
        r = [0] * ncols
        for j, v in row.items():
            r[j] = v
        out.append(r)
    return out


def spmatvec(A, x):
    return [sum(v * x[j] for j, v in row.items()) for row in A]


def poisson2d(nx, ny, dx, dy, bc="dirichlet"):
    if bc not in ("dirichlet", "periodic", "neumann"):
        raise ValueError("bc must be dirichlet, periodic or neumann")
    cx = 1 / (dx * dx)
    cy = 1 / (dy * dy)
    A = []
    for j in range(ny):
        for i in range(nx):
            row = {}
            k = i + nx * j
            for (ii, jj, w) in ((i - 1, j, cx), (i + 1, j, cx), (i, j - 1, cy), (i, j + 1, cy)):
                inside = 0 <= ii < nx and 0 <= jj < ny
                if bc == "periodic":
                    ii %= nx
                    jj %= ny
                    inside = True
                if bc == "neumann" and not inside:
                    continue
                row[k] = row.get(k, 0) + w
                if inside:
                    m = ii + nx * jj
                    row[m] = row.get(m, 0) - w
            A.append({m: v for m, v in row.items() if v != 0})
    return A


def cg(A, b, tol=1e-10, maxit=None, x0=None):
    n = len(b)
    if maxit is None:
        maxit = 10 * n
    x = [0.0] * n if x0 is None else [float(v) for v in x0]
    if x0 is None:
        r = [float(v) for v in b]
    else:
        ax = spmatvec(A, x)
        r = [b[i] - ax[i] for i in range(n)]
    bnorm = math.sqrt(sum(float(v) * float(v) for v in b))
    stop = tol * bnorm
    rr = sum(v * v for v in r)
    if math.sqrt(rr) <= stop:
        return x, 0
    p = list(r)
    for it in range(1, maxit + 1):
        ap = spmatvec(A, p)
        pap = sum(p[i] * ap[i] for i in range(n))
        if pap <= 0:
            return x, it
        alpha = rr / pap
        for i in range(n):
            x[i] += alpha * p[i]
            r[i] -= alpha * ap[i]
        rr_new = sum(v * v for v in r)
        if math.sqrt(rr_new) <= stop:
            return x, it
        beta = rr_new / rr
        rr = rr_new
        p = [r[i] + beta * p[i] for i in range(n)]
    return x, maxit


def gauss_seidel(A, b, iters, x0=None):
    n = len(b)
    x = [0] * n if x0 is None else list(x0)
    for _ in range(iters):
        for i in range(n):
            row = A[i]
            s = b[i]
            diag = 0
            for j, v in row.items():
                if j == i:
                    diag = v
                else:
                    s -= v * x[j]
            x[i] = s / diag
    return x
test_diffusion_synthetic_acceleration.py 117 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/diffusion-synthetic-acceleration/. The authors are listed in prov/XAVARJ.jsonl.
import math
import random
import unittest

from diffusion_synthetic_acceleration import (dsa, fourier_dsa, fourier_dsa_dd, fourier_dsa_inconsistent,
                                              gauss_legendre, spectral_radius_dsa)


def slab_rate(accel, h, c=0.99, m=100, iters=40):
    phi0 = [1.0 + 0.5 * math.sin(3.7 * j) for j in range(m)]
    r = dsa(8, [1.0] * m, [c] * m, [0.0] * m, [h] * m, accel=accel, tol=0.0, max_iter=iters, phi0=phi0)
    ch = r["changes"]
    return ch[-1] / ch[-2]


class TestFourier(unittest.TestCase):
    def test_continuous_spectral_radius(self):
        rho, lam = spectral_radius_dsa(1.0)
        self.assertAlmostEqual(rho, 0.2247, delta=1e-4)
        self.assertAlmostEqual(lam, 2.53, delta=0.02)
        rho, _ = spectral_radius_dsa(0.99)
        self.assertAlmostEqual(rho, 0.2214, delta=1e-4)
        self.assertEqual(fourier_dsa(0.0, 0.9), 0.0)

    def test_s2_dsa_is_exact(self):
        mus, ws = gauss_legendre(2)
        for lam in (0.1, 1.0, 3.0, 30.0):
            for c in (0.5, 0.99, 1.0):
                self.assertAlmostEqual(fourier_dsa(lam, c, mus, ws), 0.0, delta=1e-11)

    def test_cosine_modes_are_eigenvectors(self):
        mus, ws = gauss_legendre(8)
        m, h, c = 30, 0.7, 0.95
        for accel, factor in (("consistent", fourier_dsa_dd), ("inconsistent", fourier_dsa_inconsistent)):
            for k in (0, 1, 4, 15, 29):
                theta = math.pi * k / m
                e = [math.cos(theta * (j + 0.5)) for j in range(m)]
                r = dsa(8, [1.0] * m, [c] * m, [0.0] * m, [h] * m, "reflect", "reflect", accel,
                        tol=0.0, max_iter=1, phi0=e)
                omega = factor(theta, c, h, mus, ws)
                for j in range(m):
                    self.assertAlmostEqual(r["phi"][j], omega * e[j], places=12)

    def test_consistent_factor_is_bounded_for_all_cell_sizes(self):
        mus, ws = gauss_legendre(8)
        for tau in (0.01, 0.1, 1.0, 10.0, 100.0):
            worst = max(abs(fourier_dsa_dd(math.pi * k / 500, 0.999, tau, mus, ws)) for k in range(500))
            self.assertLess(worst, 0.2247)
        bad = max(abs(fourier_dsa_inconsistent(math.pi * k / 500, 0.99, 10.0, mus, ws)) for k in range(500))
        self.assertGreater(bad, 40.0)


class TestIteration(unittest.TestCase):
    def test_infinite_medium_in_one_step(self):
        r = dsa(8, [1.0] * 10, [0.9] * 10, [1.0] * 10, [1.0] * 10, "reflect", "reflect", tol=0.0, max_iter=1)
        for p in r["phi"]:
            self.assertAlmostEqual(p, 10.0, places=12)

    def test_s2_reflecting_slab_in_one_step(self):
        rng = random.Random(4)
        m = 25
        st = [rng.uniform(0.5, 5.0) for _ in range(m)]
        ss = [s * rng.uniform(0.8, 0.999) for s in st]
        q = [rng.uniform(0.0, 1.0) for _ in range(m)]
        dx = [rng.uniform(0.1, 2.0) for _ in range(m)]
        phi0 = [rng.uniform(0.0, 5.0) for _ in range(m)]
        exact = dsa(2, st, ss, q, dx, "reflect", "reflect", "none", tol=1e-15)["phi"]
        one = dsa(2, st, ss, q, dx, "reflect", "reflect", tol=0.0, max_iter=1, phi0=phi0)["phi"]
        for a, b in zip(one, exact):
            self.assertAlmostEqual(a, b, delta=1e-11 * b)
        # with S4, or with vacuum faces, one step is not exact
        four = dsa(4, st, ss, q, dx, "reflect", "reflect", tol=0.0, max_iter=1, phi0=phi0)["phi"]
        self.assertGreater(max(abs(a - b) / b for a, b in zip(four, exact)), 0.1)

    def test_measured_rates(self):
        for h in (0.1, 1.0, 10.0):
            self.assertLess(slab_rate("consistent", h), 0.22)
        self.assertLess(slab_rate("inconsistent", 0.1), 0.22)
        self.assertGreater(slab_rate("inconsistent", 1.0), 0.7)
        self.assertGreater(slab_rate("inconsistent", 10.0), 30.0)
        self.assertGreater(slab_rate("none", 10.0, iters=200), 0.985)

    def test_same_solution_far_fewer_iterations(self):
        m, length = 40, 10.0
        args = (8, [1.0] * m, [0.99] * m, [1.0] * m, [length / m] * m)
        si = dsa(*args, accel="none", tol=1e-8)
        ac = dsa(*args, accel="consistent", tol=1e-8)
        self.assertEqual(si["iterations"], 437)
        self.assertLessEqual(ac["iterations"], 12)
        ref = dsa(*args, accel="consistent", tol=1e-14)
        self.assertAlmostEqual(ac["leak_right"], ref["leak_right"], delta=1e-8)
        # source iteration stops much farther from the solution than its tolerance suggests
        self.assertGreater(abs(si["leak_right"] - ref["leak_right"]), 1e-7)
        absorbed = sum(0.01 * p * length / m for p in ref["phi"])
        self.assertAlmostEqual(absorbed + ref["leak_left"] + ref["leak_right"], length, places=9)

    def test_heterogeneous_slab(self):
        rng = random.Random(9)
        m = 60
        st = [rng.uniform(0.5, 20.0) for _ in range(m)]
        ss = [s * rng.uniform(0.9, 0.999) for s in st]
        q = [rng.uniform(0.0, 1.0) for _ in range(m)]
        dx = [rng.uniform(0.05, 2.0) for _ in range(m)]
        ac = dsa(8, st, ss, q, dx, "reflect", "vacuum", tol=1e-12)
        self.assertLess(ac["iterations"], 20)
        ic = dsa(8, st, ss, q, dx, "reflect", "vacuum", "inconsistent", tol=1e-12, max_iter=300)
        self.assertFalse(math.isfinite(ic["changes"][-1]))
        si = dsa(8, st, ss, q, dx, "reflect", "vacuum", "none", tol=1e-12, phi0=ac["phi"])
        self.assertLessEqual(si["iterations"], 3)
        absorbed = sum((a - b) * p * h for a, b, p, h in zip(st, ss, ac["phi"], dx))
        self.assertAlmostEqual(absorbed + ac["leak_right"], sum(x * h for x, h in zip(q, dx)), delta=1e-8)


if __name__ == "__main__":
    unittest.main()

History

Kopp proposed the synthetic method in 1963: a low-order problem corrects each transport iterate. Reed (1971) analyzed the diffusion correction by Fourier analysis for the continuous problem and found the factor 0.2247c0.2247c, but discretized forms of the method could be unstable for thick cells. Alcouffe (1977) showed that the diffusion equation must be derived from the diamond difference transport equations, and the result was stable for every cell thickness. Larsen (1982) turned this into a general procedure and proved unconditional stability in slab geometry. Adams and Larsen (2002) review the theory. Warsa, Wareing and Morel (2004) showed that DSA loses effectiveness in multidimensional problems with material discontinuities and that Krylov methods with a DSA preconditioner restore it.

References

  1. R. E. Alcouffe, Diffusion synthetic acceleration methods for the diamond-differenced discrete-ordinates equations, Nuclear Science and Engineering 64(2), 1977, 344-355, the derivation of a diffusion equation consistent with diamond difference and its stability for thick cells
  2. W. H. Reed, The effectiveness of acceleration techniques for iterative methods in transport theory, Nuclear Science and Engineering 45(3), 1971, 245-254, the Fourier analysis of the diffusion correction for the continuous problem
  3. E. W. Larsen, Unconditionally stable diffusion-synthetic acceleration methods for the slab geometry discrete ordinates equations. Part I: Theory, Nuclear Science and Engineering 82(1), 1982, 47-63, the four-step procedure that derives a consistent diffusion equation from a given transport discretization
  4. Marvin L. Adams and Edward W. Larsen, Fast iterative methods for discrete-ordinates particle transport calculations, Progress in Nuclear Energy 40(1), 2002, 3-159, the sections on diffusion synthetic acceleration, its Fourier analysis and the consistency requirement
  5. J. S. Warsa, T. A. Wareing and J. E. Morel, Krylov iterative methods and the degraded effectiveness of diffusion synthetic acceleration for multidimensional SN calculations in problems with material discontinuities, Nuclear Science and Engineering 147(3), 2004, 218-248, DSA as a preconditioner for Krylov methods and its loss of effectiveness in multidimensional problems with material discontinuities

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH1 sketch
  • a full proof of time.worst
  • a full proof of space.extra
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
Code (python)I0 not checked yet
  • the tests did not run in this build
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW2 basic
  • History with a source
  • a scene preset for the example