pyopensn.response.ResponseEvaluator
- class pyopensn.response.ResponseEvaluator
Response evaluator by folding sources against adjoint solutions.
Wrapper of
opensn::ResponseEvaluator.The workflow for this utility is constructed to minimize the file reading necessary for evaluations. To begin, one should add all adjoint solutions that are desired for response computations as named data sets. Then, one should define the different forward source configurations of interest. With this, the user can now iterate over the source configurations and convolve them against all available adjoint data sets.
- ClearForwardSources(self: pyopensn.response.ResponseEvaluator) None
Clear the existing forward sources from the response evaluator.
- EvaluateResponse(self: pyopensn.response.ResponseEvaluator, data_name: str) float
Evaluate a response by folding the currently defined forward sources against the adjoint flux moments and angular fluxes of a data set added with
SetOptions(data=...)orSetDataOptions().For adjoint data \(\phi^\dagger, \psi^\dagger\) this returns \(R = \int \phi^\dagger q\,dV + W \sum_{n:\,\Omega_n\cdot n < 0} w_n \int_{\partial D} |\Omega_n\cdot n| \psi^\dagger_n \psi^{\text{in}}_n\,dA\) over the material, point, volumetric, and boundary sources. The boundary sum runs over the incoming directions of each boundary face, \(w_n\) is the quadrature weight, and \(W\) is the sum of the quadrature weights (1 for OpenSn quadratures, applied per groupset). For data from a fixed-source adjoint solve, this equals the forward response to the adjoint source that produced the data, for a forward problem with the same mesh, cross sections, quadrature, and vacuum and reflecting boundaries. A k-eigenvalue adjoint has an arbitrary normalization and gives no detector response. Volumetric sources are integrated with the consistent mass matrix, as in the forward solve. Point and volumetric sources are evaluated at the problem’s current time, and sources that are inactive then are skipped. In RZ geometry the result includes the \(2\pi\) azimuthal factor. This call is collective.
- Parameters:
data_name (str) – Name of the data set holding the adjoint flux moments (
flux_moments) and, for boundary sources, adjoint angular fluxes (angular_fluxes).- Returns:
The response, identical on every rank.
- Return type:
float
- Raises:
ValueError – If no data set has this name.
RuntimeError – If the data set lacks the flux moments or angular fluxes the defined sources require.
- EvaluateSurfaceResponse(self: pyopensn.response.ResponseEvaluator, fwd_data_name: str, adj_data_name: str, surfaces: list[str] = []) dict
Evaluate the surface inner products of forward and adjoint surface angular fluxes.
Both data sets must have been added with
SetOptions(data=...)using thesurface_angular_fluxesprefix. Faces are matched by location, so the two data sets may have been written by different numbers of ranks and partitions, and neither needs to match the partitioning of the current run.For a surface \(S\) with faces \(f\), the response carried across it is
\[R = W \sum_{f \in S} \sum_{d \in D_f} \mu_{f,d}\, w_d \sum_{ij} M_{f,ij} \sum_g \psi_{f,i,d,g}\, \psi^\dagger_{f,j,d,g},\]with \(\mu_{f,d} = \Omega_d \cdot n_f\), \(w_d\) the quadrature weight, \(W\) the sum of the quadrature weights (1 for OpenSn quadratures, applied per groupset), \(M_{f,ij}\) the mass matrix of face \(f\), and \(\psi_{f,i,d,g}\) and \(\psi^\dagger_{f,j,d,g}\) the forward and adjoint angular fluxes at nodes \(i\) and \(j\) of face \(f\).
For a boundary, \(n_f\) is the outward normal and \(D_f\) holds every direction. For the outflow directions (\(\mu_{f,d} > 0\)), the forward flux is that of the face’s cell and the adjoint flux is the adjoint boundary-condition value (0 for a vacuum boundary), which is upwind for the adjoint because it travels along \(-\Omega_d\). For the inflow directions, the forward flux is the forward boundary-condition value (0 for a vacuum boundary) and the adjoint flux that of the face’s cell. This is the boundary term of the discrete adjoint identity, so a boundary response is exact for the discretization, like an interior surface response: its
outgoingpart is the response to the particles leaving through the boundary (for a detector outside the domain, represented by an adjoint boundary source, the detector response), and itsincomingpart minus the response to a forward boundary source on it. On a reflecting boundary both sides use the reflected angular flux of the final iterate, so its response is zero to the iteration tolerance.The surface files record whether each tag is a boundary or one side of an interior surface, so a boundary is evaluated as a boundary whatever its name, including names ending in
_uor_d.An interior surface is stored under tags
<name>_uand<name>_d, one per side. Its faces are those of the_uside, each matched to the_dface at the same location, and \(n_f\) is the outward normal of the side the surface is requested by: the_unormal (the positive axis) for<name>or<name>_u, and the_dnormal (the negative axis) for<name>_d, which gives the same response with the opposite sign. \(D_f\) holds every direction. The forward flux is taken from the side of the face upwind of \(\Omega_d\), the side it leaves. The adjoint flux is taken from the downwind side, which is upwind for the adjoint because it travels along \(-\Omega_d\).When an interior surface separates the forward source from the detector (the region the adjoint source scores), its response equals the detector response if \(n_f\) points from the source side to the detector side, and the negative of it otherwise. Request the surface by the tag of the side the source is on to get the positive value:
<name>_uwhen the source is on the negative side of the surface,<name>_dwhen it is on the positive side. A surface that does not separate them gives zero.Each response is split by the sign of \(\mu_{f,d}\):
outgoingsums the directions with \(\mu_{f,d} > 0\), which cross along \(n_f\), andincomingthose with \(\mu_{f,d} < 0\), which cross back;totalis their sum, \(R\). For an interior surfacex20requested along its_unormal (+x),outgoingis the part carried in the +x direction andincomingminus the part carried in the -x direction. Requested asx20_d, the two parts swap and change sign.Forward and adjoint directions are paired by equal direction vectors, and the nodes of matched faces by location. The responses are also logged at verbosity 1. This call is collective. The adjoint data must come from an adjoint problem, which OpenSn supports only in Cartesian geometry, so surface responses are not available for curvilinear (RZ) problems.
- Parameters:
fwd_data_name (str) – Name of the forward data set. It must have been written from a forward problem.
adj_data_name (str) – Name of the adjoint data set. It must have been written from an adjoint problem.
surfaces (List[str], default=[]) – Surfaces to evaluate: boundary names, and interior surface names, bare (
<name>, along the_unormal) or with the tag of the side whose normal to use (<name>_uor<name>_d). Each surface may be requested once, under one of its names. If empty, every surface in the forward data is evaluated, in name order, interior surfaces along their_unormals.
- Returns:
For each surface, under the name it was requested by and in the requested order, a dictionary with keys
outgoing,incoming, andtotal(their sum, the response carried across the surface). Sum thetotalvalues to combine surfaces.- Return type:
Dict[str, Dict[str, float]]
- Raises:
ValueError – If a data set or surface is unknown, a surface is requested more than once, a required tag is missing from the adjoint data or recorded there as a different kind of surface, or the data sets have incompatible quadratures, groupsets, adjoint flags, or face keys.
RuntimeError – If the forward and adjoint data do not cover the same faces.
- SetDataOptions(self: pyopensn.response.ResponseEvaluator, **kwargs) None
Add a named data set to the response evaluator. This call is collective.
- Parameters:
name (str (required)) – Data set name used by
EvaluateResponse()andEvaluateSurfaceResponse(). It must not already be in use.file_prefixes (Dict (required)) –
- Prefix map with optional keys:
- flux_moments: str
Prefix for the adjoint flux-moment files that
EvaluateResponse()folds against the forward sources. Written with the current rank count and partitioning.
- angular_fluxes: str
Prefix for the adjoint angular-flux files needed for boundary sources. Written with the current rank count and partitioning.
- surface_angular_fluxes: str
Prefix for the files written by
WriteSurfaceAngularFluxes, used byEvaluateSurfaceResponse(). All files of the prefix are read, whatever number of ranks wrote them, and their faces are distributed over the current ranks. Any rank may read any file, so the files must be on a file system that every rank can access.
Flux-moment and angular-flux files must also match the problem’s quadrature and groups. Adjoint data must be in physical orientation, as written after its solver finished. Forward data is also accepted.
surfaces (List[str], default=[]) – Surfaces to read. Required with
surface_angular_fluxes, and rejected without it. Each entry is a boundary name or an interior surface name, which reads both stored sides<name>_uand<name>_d. A single side’s tag is rejected unless its other side is also requested, because a surface response needs both sides.
- Raises:
ValueError – If the name is already used, or
surfacesandsurface_angular_fluxesare not given together.RuntimeError – If a file cannot be read, does not match the problem, or holds adjoint data in solver orientation.
- SetOptions(self: pyopensn.response.ResponseEvaluator, **kwargs) None
Set options for the response evaluator for adding data sets and defining forward sources.
This call is collective. Every rank must give the same options, because adding data sets and material sources communicates over all ranks.
- Parameters:
data (List[Dict], default=[]) – A list of dictionaries containing data set specifications, each as accepted by
SetDataOptions().clear_sources (bool, default=False) – A flag to clear existing sources.
sources (Dict, default={}) –
- Dictionary describing forward sources. Supported keys:
- material: List[Dict], default=[]
- Each dictionary supports:
- block_id: int (required)
Block id for the material source.
- strength: List[float] (required)
Group-wise source strength (length must match number of groups).
- point: List[pyopensn.source.PointSource], default=[]
Point source handles.
- volumetric: List[pyopensn.source.VolumetricSource], default=[]
Volumetric source handles.
- boundary: List[Dict], default=[]
- Boundary source dictionaries. Each dictionary supports:
- name: str (required)
Boundary name that identifies the specific boundary.
- type: {‘isotropic’} (required)
Boundary type. Only isotropic boundary sources are supported.
- group_strength: List[float] (required)
Isotropic incident angular flux per group.
A boundary may have only one boundary source.
- SetSourceOptions(self: pyopensn.response.ResponseEvaluator, **kwargs) None
Add forward sources from the options block to the response evaluator.
This call is collective. Every rank must add the same sources in the same order, because a material source counts its cells over all ranks.
- Parameters:
material (List[Dict], default=[]) –
- Each dictionary supports:
- block_id: int (required)
Block id for the material source.
- strength: List[float] (required)
Group-wise source strength (length must match number of groups).
point (List[pyopensn.source.PointSource], default=[]) – Point source handles.
volumetric (List[pyopensn.source.VolumetricSource], default=[]) – Volumetric source handles.
boundary (List[Dict], default=[]) –
- Boundary source dictionaries. Each dictionary supports:
- name: str (required)
Boundary name that identifies the specific boundary.
- type: {‘isotropic’} (required)
Boundary type. Only isotropic boundary sources are supported.
- group_strength: List[float] (required)
Isotropic incident angular flux per group.
A boundary may have only one boundary source.
- __init__(self: pyopensn.response.ResponseEvaluator, **kwargs) None
Construct a response evaluator object.
This call is collective. Every rank must give the same options, because adding data sets and material sources communicates over all ranks.
- Parameters:
problem (pyopensn.solver.DiscreteOrdinatesProblem) – A handle to an existing LBS problem.
options (Dict) –
- The specification of data sets and forward sources to use. Supported keys:
- data: List[Dict], default=[]
Data set specifications, as for
SetDataOptions().
- clear_sources: bool, default=False
If true, clears any existing forward sources.
- sources: Dict, default={}
- Dictionary describing forward sources. Supported keys:
- material: List[Dict], default=[]
- Each dictionary supports:
- block_id: int (required)
Block id for the material source.
- strength: List[float] (required)
Group-wise source strength (length must match number of groups).
- point: List[pyopensn.source.PointSource], default=[]
Point source handles.
- volumetric: List[pyopensn.source.VolumetricSource], default=[]
Volumetric source handles.
- boundary: List[Dict], default=[]
- Boundary source dictionaries. Each dictionary supports:
- name: str (required)
Boundary name that identifies the specific boundary.
- type: {‘isotropic’} (required)
Boundary type. Only isotropic boundary sources are supported.
- group_strength: List[float] (required)
Isotropic incident angular flux per group.
A boundary may have only one boundary source.