Solution verification

The method of nearby problems estimates the spatial discretization error of a solution on a single mesh, without a refined mesh or an analytic reference. It needs scipy (pip install "ndiffusion[nearby]").

The recipe:

  1. Solve the problem numerically.

  2. Fit a smooth curve through the numerical flux, separately within each material so the fit never differentiates across a cross-section jump.

  3. Substitute the fit into the continuous diffusion operator. What is left over is a residual source \(r = L[\phi_\text{fit}] - S\), and the fit is then the exact solution of the “nearby problem” \(L[\phi] = S + r\).

  4. Solve the nearby problem on the same mesh. Its exact solution is known - it is the fit - so \(\phi_\text{nearby} - \phi_\text{fit}\) estimates the true error \(\phi_\text{numerical} - \phi_\text{exact}\).

The second derivatives of the fit enter the leakage term, so they must be more accurate than the scheme itself. On structured meshes the fit is a quintic spline; on the unstructured mesh it is a least-squares polynomial of up to fourth degree over a stencil of same-material neighbors.

Fixed source

nearby_fixed_source takes a constructed fixed-source solver, the materials, the source, and a description of the geometry, which the solver does not expose: medium_map, edges_x and geometry in 1-D, plus edges_y in 2-D, or mesh= for the unstructured solver.

A manufactured problem with a known answer, \(\phi = \cos(\pi x / 2R)\) on a half slab:

import numpy as np
import ndiffusion as nd

D, sigma_a, R, cells = 1.0, 0.2, 10.0, 40
B = np.pi / (2 * R)
edges = np.linspace(0.0, R, cells + 1)
x = 0.5 * (edges[:-1] + edges[1:])
mmap = [0] * cells
mats = nd.materials.one_group(D=D, sigma_a=sigma_a)
exact = np.cos(B * x)
source = (D * B**2 + sigma_a) * exact

solver = nd.FixedSourceSolver(mats, mmap, edges, nd.Geometry.Slab,
                              [nd.BoundaryCondition(A=1.0, B=0.0)],
                              epsilon=1e-12, max_inner=5000)
result = nd.nearby_fixed_source(solver, mats, source, medium_map=mmap,
                                edges_x=edges, geometry=nd.Geometry.Slab)

true_error = result.numerical.flux[:, 0] - exact
estimate = result.error_estimate[:, 0]

Here the largest error is about \(1.4\times10^{-5}\), and the estimate matches it to within 0.1%.

The result holds the numerical and nearby solver results, the curve_fit, the residual and the error_estimate, all shaped (n_cells, n_groups).

k-eigenvalue

nearby_k_eigenvalue takes the k-eigenvalue solver and a fixed-source solver built on the same materials, mesh and boundary conditions. The nearby problem is solved by a power iteration that reuses the fixed-source solver for each outer step. Along with the flux estimate it returns k_curve_fit and k_nearby, and k_nearby - k_curve_fit estimates the eigenvalue error.

Given an adjoint_solver - a k-eigenvalue solver built on make_adjoint_materials(mats) with the same mesh and boundary conditions - it also returns k_error_adjoint, a one-shot estimate of the same eigenvalue error, and k_error_cells, its contribution from each cell. With nearby=False the nearby power iteration is skipped and only the one-shot estimate is formed.

On structured meshes the fits are independent in each material, so they need not agree at a material interface, and the flux estimate is most reliable away from interfaces.

Unstructured meshes

On the unstructured mesh, pass the boundary conditions the solver was built with as bc=. The curve fit is then one constrained spline per material region that keeps the flux and current continuous across interfaces and meets the boundary conditions (see The method of nearby problems):

keig = nd.KEigenSolverUnstructured2D(mats, mesh, bc, use_cg=True)
adj = nd.KEigenSolverUnstructured2D(nd.make_adjoint_materials(mats), mesh, bc, use_cg=True)
fixed = nd.FixedSourceSolverUnstructured2D(mats, mesh, bc)
r = nd.nearby_k_eigenvalue(keig, fixed, mats, mesh=mesh, bc=bc, adjoint_solver=adj)

Without bc= (or with fit="local") the older per-cell polynomial fit is used.

With method="direct" the solver’s operator is assembled as a sparse matrix (unstructured_operator) and factorized once. The nearby problem is then solved with that factorization, the one-shot eigenvalue estimate uses the exact discrete adjoint, and neither a fixed-source nor an adjoint solver is needed. On meshes of a few tens of thousands of cells this is the faster route:

r = nd.nearby_k_eigenvalue(keig, None, mats, mesh=mesh, bc=bc, method="direct")

Quantities of interest

nearby_qoi estimates the error of a normalized quantity of interest, a ratio of two reaction-rate integrals \(\int a\cdot\phi\,dV / \int b\cdot\phi\,dV\) with weights given per cell and group. For example, the absorption in a set of rodded cells per unit fission production:

G = mats.n_groups
mat = np.asarray(mesh.material_id)
sig_a = ...                                   # (n_mat, G) absorption cross sections
nusigf = np.asarray(mats.nusigf).reshape(-1, G)
num = sig_a[mat] * (mat == RODDED)[:, None]   # (n_cells, G)
den = nusigf[mat]
q = nd.nearby_qoi(r, mats, mesh, bc, num, den)
q.value, q.error_adjoint, q.error_nearby      # J(phi_h) and two estimates of its error

error_adjoint comes from a generalized adjoint solve on the exact discrete operator, error_nearby from the nearby flux (when it was computed), and cells holds the per-cell contributions to error_adjoint, a map of where the error in that quantity comes from. Stacking the weights as (n_q, n_cells, G) evaluates several quantities with one factorization.

On a layout with material corners or small enclosed regions, such as a core with rodded assemblies, the eigenvalue error is a small net of large contributions of both signs. The flux estimate stays good there, but read the eigenvalue estimate as an indicator of size, with abs(r.k_error_cells).sum() as a loose bound.

Adjoint problems

make_adjoint_materials(mats) returns the adjoint cross sections (see Cross sections). The diffusion and removal operator is self-adjoint, so any solver run on them solves the adjoint problem: the eigenvalue matches the forward one and the flux is the importance function.