Skip to content

Commit

Permalink
Filter material from (#2750)
Browse files Browse the repository at this point in the history
Co-authored-by: Patrick Shriwise <[email protected]>
Co-authored-by: Paul Romano <[email protected]>
  • Loading branch information
3 people authored Nov 9, 2023
1 parent 910d1df commit a3695c7
Show file tree
Hide file tree
Showing 12 changed files with 99 additions and 10 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,7 @@ list(APPEND libopenmc_SOURCES
src/tallies/filter_energyfunc.cpp
src/tallies/filter_legendre.cpp
src/tallies/filter_material.cpp
src/tallies/filter_materialfrom.cpp
src/tallies/filter_mesh.cpp
src/tallies/filter_meshsurface.cpp
src/tallies/filter_mu.cpp
Expand Down
1 change: 1 addition & 0 deletions docs/source/pythonapi/base.rst
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ Constructing Tallies
openmc.Filter
openmc.UniverseFilter
openmc.MaterialFilter
openmc.MaterialFromFilter
openmc.CellFilter
openmc.CellFromFilter
openmc.CellBornFilter
Expand Down
1 change: 1 addition & 0 deletions include/openmc/particle_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,7 @@ class ParticleData {
int& material() { return material_; }
const int& material() const { return material_; }
int& material_last() { return material_last_; }
const int& material_last() const { return material_last_; }

BoundaryInfo& boundary() { return boundary_; }

Expand Down
1 change: 1 addition & 0 deletions include/openmc/tallies/filter.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ enum class FilterType {
ENERGY_OUT,
LEGENDRE,
MATERIAL,
MATERIALFROM,
MESH,
MESH_SURFACE,
MU,
Expand Down
2 changes: 1 addition & 1 deletion include/openmc/tallies/filter_material.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class MaterialFilter : public Filter {

void set_materials(gsl::span<const int32_t> materials);

private:
protected:
//----------------------------------------------------------------------------
// Data members

Expand Down
29 changes: 29 additions & 0 deletions include/openmc/tallies/filter_materialfrom.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#ifndef OPENMC_TALLIES_FILTER_MATERIALFROM_H
#define OPENMC_TALLIES_FILTER_MATERIALFROM_H

#include <string>

#include "openmc/tallies/filter_material.h"

namespace openmc {

//==============================================================================
//! Specifies which material particles exit when crossing a surface.
//==============================================================================

class MaterialFromFilter : public MaterialFilter {
public:
//----------------------------------------------------------------------------
// Methods

std::string type_str() const override { return "materialfrom"; }
FilterType type() const override { return FilterType::MATERIALFROM; }

void get_all_bins(const Particle& p, TallyEstimator estimator,
FilterMatch& match) const override;

std::string text_label(int bin) const override;
};

} // namespace openmc
#endif // OPENMC_TALLIES_FILTER_MATERIALFROM_H
32 changes: 28 additions & 4 deletions openmc/filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
_FILTER_TYPES = (
'universe', 'material', 'cell', 'cellborn', 'surface', 'mesh', 'energy',
'energyout', 'mu', 'polar', 'azimuthal', 'distribcell', 'delayedgroup',
'energyfunction', 'cellfrom', 'legendre', 'spatiallegendre',
'energyfunction', 'cellfrom', 'materialfrom', 'legendre', 'spatiallegendre',
'sphericalharmonics', 'zernike', 'zernikeradial', 'particle', 'cellinstance',
'collision', 'time'
)
Expand Down Expand Up @@ -451,7 +451,7 @@ class UniverseFilter(WithIDFilter):
Parameters
----------
bins : openmc.UniverseBase, int, or iterable thereof
The Universes to tally. Either openmc.UniverseBase objects or their
The Universes to tally. Either :class:`openmc.UniverseBase` objects or their
Integral ID numbers can be used.
filter_id : int
Unique identifier for the filter
Expand All @@ -475,7 +475,31 @@ class MaterialFilter(WithIDFilter):
Parameters
----------
bins : openmc.Material, Integral, or iterable thereof
The Materials to tally. Either openmc.Material objects or their
The material(s) to tally. Either :class:`openmc.Material` objects or their
Integral ID numbers can be used.
filter_id : int
Unique identifier for the filter
Attributes
----------
bins : Iterable of Integral
openmc.Material IDs.
id : int
Unique identifier for the filter
num_bins : Integral
The number of filter bins
"""
expected_type = Material


class MaterialFromFilter(WithIDFilter):
"""Bins tally event locations based on the Material they occurred in.
Parameters
----------
bins : openmc.Material, Integral, or iterable thereof
The material(s) to tally. Either :class:`openmc.Material` objects or their
Integral ID numbers can be used.
filter_id : int
Unique identifier for the filter
Expand All @@ -499,7 +523,7 @@ class CellFilter(WithIDFilter):
Parameters
----------
bins : openmc.Cell, int, or iterable thereof
The cells to tally. Either openmc.Cell objects or their ID numbers can
The cells to tally. Either :class:`openmc.Cell` objects or their ID numbers can
be used.
filter_id : int
Unique identifier for the filter
Expand Down
7 changes: 6 additions & 1 deletion openmc/lib/filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
'Filter', 'AzimuthalFilter', 'CellFilter', 'CellbornFilter', 'CellfromFilter',
'CellInstanceFilter', 'CollisionFilter', 'DistribcellFilter', 'DelayedGroupFilter',
'EnergyFilter', 'EnergyoutFilter', 'EnergyFunctionFilter', 'LegendreFilter',
'MaterialFilter', 'MeshFilter', 'MeshSurfaceFilter', 'MuFilter', 'ParticleFilter',
'MaterialFilter', 'MaterialFromFilter', 'MeshFilter', 'MeshSurfaceFilter', 'MuFilter', 'ParticleFilter',
'PolarFilter', 'SphericalHarmonicsFilter', 'SpatialLegendreFilter', 'SurfaceFilter',
'UniverseFilter', 'ZernikeFilter', 'ZernikeRadialFilter', 'filters'
]
Expand Down Expand Up @@ -342,6 +342,10 @@ def bins(self, materials):
_dll.openmc_material_filter_set_bins(self._index, n, bins)


class MaterialFromFilter(Filter):
filter_type = 'materialfrom'


class MeshFilter(Filter):
filter_type = 'mesh'

Expand Down Expand Up @@ -501,6 +505,7 @@ class ZernikeRadialFilter(ZernikeFilter):
'energyfunction': EnergyFunctionFilter,
'legendre': LegendreFilter,
'material': MaterialFilter,
'materialfrom': MaterialFromFilter,
'mesh': MeshFilter,
'meshsurface': MeshSurfaceFilter,
'mu': MuFilter,
Expand Down
3 changes: 3 additions & 0 deletions src/tallies/filter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "openmc/tallies/filter_energyfunc.h"
#include "openmc/tallies/filter_legendre.h"
#include "openmc/tallies/filter_material.h"
#include "openmc/tallies/filter_materialfrom.h"
#include "openmc/tallies/filter_mesh.h"
#include "openmc/tallies/filter_meshsurface.h"
#include "openmc/tallies/filter_mu.h"
Expand Down Expand Up @@ -121,6 +122,8 @@ Filter* Filter::create(const std::string& type, int32_t id)
return Filter::create<LegendreFilter>(id);
} else if (type == "material") {
return Filter::create<MaterialFilter>(id);
} else if (type == "materialfrom") {
return Filter::create<MaterialFromFilter>(id);
} else if (type == "mesh") {
return Filter::create<MeshFilter>(id);
} else if (type == "meshsurface") {
Expand Down
24 changes: 24 additions & 0 deletions src/tallies/filter_materialfrom.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include "openmc/tallies/filter_materialfrom.h"

#include "openmc/cell.h"
#include "openmc/material.h"

namespace openmc {

void MaterialFromFilter::get_all_bins(
const Particle& p, TallyEstimator estimator, FilterMatch& match) const
{
auto search = map_.find(p.material_last());
if (search != map_.end()) {
match.bins_.push_back(search->second);
match.weights_.push_back(1.0);
}
}

std::string MaterialFromFilter::text_label(int bin) const
{
return "Material from " +
std::to_string(model::materials[materials_[bin]]->id_);
}

} // namespace openmc
2 changes: 1 addition & 1 deletion tests/regression_tests/surface_tally/inputs_true.dat
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
<filter id="4" type="surface">
<bins>1</bins>
</filter>
<filter id="7" type="cellfrom">
<filter id="7" type="materialfrom">
<bins>2</bins>
</filter>
<filter id="8" type="cell">
Expand Down
6 changes: 3 additions & 3 deletions tests/regression_tests/surface_tally/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,19 +106,19 @@ def __init__(self, *args, **kwargs):

# Create partial current tallies from water to fuel
# Filters
cell_from_filter = openmc.CellFromFilter(water)
mat_from_filter = openmc.MaterialFromFilter(borated_water)
cell_filter = openmc.CellFilter(fuel)

# Cell to cell filters for partial current
cell_to_cell_tally = openmc.Tally(name=str('water_to_fuel_1'))
cell_to_cell_tally.filters = [cell_from_filter, cell_filter, \
cell_to_cell_tally.filters = [mat_from_filter, cell_filter, \
energy_filter, polar_filter, azimuthal_filter]
cell_to_cell_tally.scores = ['current']
tallies_file.append(cell_to_cell_tally)

# Cell from + surface filters for partial current
cell_to_cell_tally = openmc.Tally(name=str('water_to_fuel_2'))
cell_to_cell_tally.filters = [cell_from_filter, surface_filter, \
cell_to_cell_tally.filters = [mat_from_filter, surface_filter, \
energy_filter, polar_filter, azimuthal_filter]
cell_to_cell_tally.scores = ['current']
tallies_file.append(cell_to_cell_tally)
Expand Down

0 comments on commit a3695c7

Please sign in to comment.