Neutron Diffusion in Python

This notebook is an entirely self-contained solution to a basic neutron diffision equation for a reactor rx made up of a single fuel rod. The one-group diffusion equation that we will be stepping through time and space is,

$\frac{1}{v}\frac{\partial \phi}{\partial t} = D \nabla^2 \phi + (k - 1) \Sigma_a \phi + S$

where

  • $\phi$ is the neutron flux [n/cm$^2$/s],
  • $D$ is the diffusion coefficient [cm],
  • $k$ is the multiplication factor of the material [unitless],
  • $S$ is a static source term [n/cm$^2$/s], and
  • $v$ is the neutron velocity, which for thermal neutrons is 2.2e5 [cm/s]

How Does This Work?!

Here we use ITAPS / MOAB, via PyTAPS, as our underlying mesh representation. This allows us to keep a reperesentation of the mesh in-memory or dump it out to disk. The reason for using MOAB, other than that it saves us a ton of time and effort since we don't have to write our own mesh package, is that MOAB is now understood by both PyNE and yt!

PyNE is used to compute all of the nuclear data needs here and for a structured / semi-structured represntation of MOAB Hex8 meshes. yt has been supped up to understand both plain old MOAB Hex8 meshes as well as the PyNE variant. The simulation, analysis, and visulaization here takes place entirely within memory.

The software stack here is gloriously deep. Refresh yourself on this well of knowledge.

In [1]:
from itertools import product 
from pyne.mesh import Mesh
from pyne.xs.cache import XSCache
from pyne.xs.data_source import CinderDataSource, SimpleDataSource, NullDataSource
from pyne.xs.channels import sigma_a, sigma_s
from pyne.material import Material, from_atom_frac
import numpy as np
from yt.config import ytcfg; ytcfg["yt","suppressStreamLogging"] = "True"
from yt.mods import *
from itaps import iBase, iMesh
from matplotlib import animation
from JSAnimation import IPython_display
import matplotlib.pyplot as plt
from matplotlib.backends.backend_agg import FigureCanvasAgg
from IPython.display import HTML
In [2]:
xsc = XSCache([0.026e-6, 0.024e-6], (SimpleDataSource, NullDataSource))

The Laplacian

The functions in the following cell solve for the laplacian ($\nabla^2$) for any index in in the mesh using a 3 point stencil along each axis. This implements relfecting boundary conditions along the edges of the domain.

In [3]:
def lpoint(idx, n, coords, shape, m):
    lidx = list(idx)
    lidx[n] += 1 if idx[n] == 0 else -1
    left = m.structured_get_hex(*lidx)
    l = m.mesh.getVtxCoords(left)[n]
    if idx[n] == 0:
        l = 2*coords[n] - l 
    return left, l

def rpoint(idx, n, coords, shape, m):
    ridx = list(idx)
    ridx[n] += -1 if idx[n] == shape[n]-2 else 1
    right = m.structured_get_hex(*ridx)
    r = m.mesh.getVtxCoords(right)[n]
    if idx[n] == shape[n]-2:
        r = 2*coords[n] - r
    return right, r

def laplace(tag, idx, m, shape):
    ent = m.structured_get_hex(*idx)
    coords = m.mesh.getVtxCoords(ent)
    lptag = 0.0
    for n in range(3):
        left, l = lpoint(idx, n, coords, shape, m)
        right, r = rpoint(idx, n, coords, shape, m)
        c = coords[n]
        lptag += (((tag[right] - tag[ent])/(r-c)) - ((tag[ent] - tag[left])/(c-l))) / ((r-l)/2)
    return lptag

Solve in space

The timestep() fucntion sweeps through the entire mesh and computes the new flux everywhere. This opperation takes place enritely on the mesh object.

In [4]:
def timestep(m, dt):
    nx = len(m.structured_get_divisions("x"))
    ny = len(m.structured_get_divisions("y"))
    nz = len(m.structured_get_divisions("z"))
    shape = (nx, ny, nz)
    D = m.mesh.getTagHandle("D")
    k = m.mesh.getTagHandle("k")
    S = m.mesh.getTagHandle("S")
    Sigma_a = m.mesh.getTagHandle("Sigma_a")
    phi = m.mesh.getTagHandle("phi")
    phi_next = m.mesh.getTagHandle("phi_next")
    for idx in product(*[range(xyz-1) for xyz in shape]):
        ent = m.structured_get_hex(*idx)
        phi_next[ent] = (max(D[ent] * laplace(phi, idx, m, shape) + 
                                    (k[ent] - 1.0) * Sigma_a[ent] * phi[ent], 0.0) + S[ent])*dt*2.2e5 + phi[ent]
    ents = m.mesh.getEntities(iBase.Type.region)
    phi[ents] = phi_next[ents]

Solve in time

The render() function steps through time calling the timestep() function and then creating an image. The images that are generated are then dumped into a movie.

In [5]:
def render(m, dt, axis="z", field="phi", frames=100):
    pf = PyneMoabHex8StaticOutput(m)
    s = SlicePlot(pf, axis, field, origin='native')
    fig = s.plots['gas', field].figure
    fig.canvas = FigureCanvasAgg(fig)
    axim = fig.axes[0].images[0]

    def init():
        axim = s.plots['gas', 'phi'].image
        return axim

    def animate(i):
        s = SlicePlot(pf, axis, field, origin='native')
        axim.set_data(s._frb['gas', field])
        timestep(m, dt)
        return axim

    return animation.FuncAnimation(fig, animate, init_func=init, frames=frames, interval=100, blit=False)

Reactor

This setups up a simple light water reactor fuel pin in a water cell. Note that our cells are allowed to have varing aspect ratios. This alows us to be corsely refined inside of the pin, finely refined around the edge of the pin, and then have a differernt coarse refinement out in the coolant.

In [6]:
def isinrod(ent, rx, radius=0.4):
    """returns whether an entity is in a control rod"""
    coord = rx.mesh.getVtxCoords(ent)
    return (coord[0]**2 + coord[1]**2) <= radius**2

def create_reactor(multfact=1.0, radius=0.4):
    fuel = from_atom_frac({'U235': 0.045, 'U238': 0.955, 'O16': 2.0}, density=10.7)
    cool = from_atom_frac({'H1': 2.0, 'O16': 1.0}, density=1.0)
    xpoints = [0.0, 0.075, 0.15, 0.225] + list(np.arange(0.3, 0.7, 0.025)) + list(np.arange(0.7, 1.201, 0.05))
    ypoints = xpoints
    zpoints = np.linspace(0.0, 1.0, 8)
    rx = Mesh(structured_coords=[xpoints, ypoints, zpoints], structured=True)
    D = rx.mesh.createTag("D", 1, float)
    k = rx.mesh.createTag("k", 1, float)
    S = rx.mesh.createTag('S', 1, float)
    Sigma_a = rx.mesh.createTag("Sigma_a", 1, float)
    phi = rx.mesh.createTag("phi", 1, float)
    phi_next = rx.mesh.createTag("phi_next", 1, float)
    for ent in rx.structured_iterate_hex("xyz"):
        if isinrod(ent, rx, radius=radius):
            D[ent] = 1.0 / (3.0 * fuel.density * 1e-24 * sigma_s(fuel, xs_cache=xsc))
            Sigma_a[ent] = fuel.density * 1e-24 * sigma_a(fuel, xs_cache=xsc)
            phi[ent] = 4e14
            k[ent] = multfact
        else:
            D[ent] = 1.0 / (3.0 * cool.density * 1e-24 * sigma_s(cool, xs_cache=xsc))
            Sigma_a[ent] = cool.density * 1e-24 * sigma_a(cool, xs_cache=xsc)
            r2 = (rx.mesh.getVtxCoords(ent)[:2]**2).sum()
            phi[ent] = 4e14 * radius**2 / r2 if r2 < 0.7**2 else 0.0
            k[ent] = 0.0
        S[ent] = 0.0
        phi_next[ent] = 0.0
    return rx
In [7]:
rx = create_reactor()
render(rx, dt=2.5e-31, frames=100)
Out[7]:


Once Loop Reflect