Skip to content

Commit

Permalink
Rename GenericXsCalculator (#1081)
Browse files Browse the repository at this point in the history
  • Loading branch information
amandalund committed Jan 8, 2024
1 parent f5fa7b8 commit a8df93d
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 59 deletions.
2 changes: 1 addition & 1 deletion src/celeritas/em/data/LivermorePEData.hh
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include "corecel/math/Quantity.hh"
#include "celeritas/Quantities.hh"
#include "celeritas/Types.hh"
#include "celeritas/grid/XsGridData.hh"
#include "celeritas/grid/GenericGridData.hh"

namespace celeritas
{
Expand Down
4 changes: 2 additions & 2 deletions src/celeritas/em/interactor/LivermorePEInteractor.hh
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#include "celeritas/Quantities.hh"
#include "celeritas/em/data/LivermorePEData.hh"
#include "celeritas/em/xs/LivermorePEMicroXsCalculator.hh"
#include "celeritas/grid/GenericXsCalculator.hh"
#include "celeritas/grid/GenericCalculator.hh"
#include "celeritas/grid/PolyEvaluator.hh"
#include "celeritas/phys/CutoffView.hh"
#include "celeritas/phys/Interaction.hh"
Expand Down Expand Up @@ -249,7 +249,7 @@ CELER_FUNCTION SubshellId LivermorePEInteractor::sample_subshell(Engine& rng) co
}

// Use the tabulated subshell cross sections
GenericXsCalculator calc_xs(shell.xs, shared_.xs.reals);
GenericCalculator calc_xs(shell.xs, shared_.xs.reals);
xs += inv_cube_energy * calc_xs(inc_energy_);

if (xs > cutoff)
Expand Down
6 changes: 3 additions & 3 deletions src/celeritas/em/xs/LivermorePEMicroXsCalculator.hh
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include "celeritas/Quantities.hh"
#include "celeritas/Types.hh"
#include "celeritas/em/data/LivermorePEData.hh"
#include "celeritas/grid/GenericXsCalculator.hh"
#include "celeritas/grid/GenericCalculator.hh"
#include "celeritas/grid/PolyEvaluator.hh"

namespace celeritas
Expand Down Expand Up @@ -91,14 +91,14 @@ real_type LivermorePEMicroXsCalculator::operator()(ElementId el_id) const
{
// Use tabulated cross sections above K-shell energy but below energy
// limit for parameterization
GenericXsCalculator calc_xs(el.xs_hi, shared_.xs.reals);
GenericCalculator calc_xs(el.xs_hi, shared_.xs.reals);
result = ipow<3>(inv_energy) * calc_xs(energy.value());
}
else
{
CELER_ASSERT(el.xs_lo);
// Use tabulated cross sections below K-shell energy
GenericXsCalculator calc_xs(el.xs_lo, shared_.xs.reals);
GenericCalculator calc_xs(el.xs_lo, shared_.xs.reals);
result = ipow<3>(inv_energy) * calc_xs(energy.value());
}
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// See the top-level COPYRIGHT file for details.
// SPDX-License-Identifier: (Apache-2.0 OR MIT)
//---------------------------------------------------------------------------//
//! \file celeritas/grid/GenericXsCalculator.hh
//! \file celeritas/grid/GenericCalculator.hh
//---------------------------------------------------------------------------//
#pragma once

Expand All @@ -15,15 +15,15 @@
#include "corecel/grid/Interpolator.hh"
#include "corecel/grid/NonuniformGrid.hh"

#include "XsGridData.hh"
#include "GenericGridData.hh"

namespace celeritas
{
//---------------------------------------------------------------------------//
/*!
* Find and interpolate cross sections on a nonuniform grid.
* Find and interpolate values on a nonuniform grid.
*/
class GenericXsCalculator
class GenericCalculator
{
public:
//@{
Expand All @@ -35,16 +35,17 @@ class GenericXsCalculator
public:
// Construct from grid data and backend values
inline CELER_FUNCTION
GenericXsCalculator(GenericGridData const& grid, Values const& values);
GenericCalculator(GenericGridData const& grid, Values const& values);

// Find and interpolate the cross section from the given energy
inline CELER_FUNCTION real_type operator()(const real_type energy) const;
// Find and interpolate the y value from the given x value
inline CELER_FUNCTION real_type operator()(real_type x) const;

// Get the tabulated y value at a particular index.
CELER_FORCEINLINE_FUNCTION real_type operator[](size_type index) const;

private:
GenericGridData const& data_;
Values const& reals_;

CELER_FORCEINLINE_FUNCTION real_type get(size_type index) const;
};

//---------------------------------------------------------------------------//
Expand All @@ -54,56 +55,55 @@ class GenericXsCalculator
* Construct from grid data and backend values.
*/
CELER_FUNCTION
GenericXsCalculator::GenericXsCalculator(GenericGridData const& grid,
Values const& values)
GenericCalculator::GenericCalculator(GenericGridData const& grid,
Values const& values)
: data_(grid), reals_(values)
{
CELER_EXPECT(data_);
}

//---------------------------------------------------------------------------//
/*!
* Calculate the cross section at the given energy.
* Calculate the y value at the given x value.
*/
CELER_FUNCTION real_type
GenericXsCalculator::operator()(const real_type energy) const
CELER_FUNCTION real_type GenericCalculator::operator()(real_type x) const
{
NonuniformGrid<real_type> const energy_grid(data_.grid, reals_);
NonuniformGrid<real_type> const x_grid(data_.grid, reals_);

// Snap out-of-bounds values to closest grid points
size_type lower_idx;
real_type result;
if (energy <= energy_grid.front())
if (x <= x_grid.front())
{
lower_idx = 0;
result = this->get(lower_idx);
result = (*this)[lower_idx];
}
else if (energy >= energy_grid.back())
else if (x >= x_grid.back())
{
lower_idx = energy_grid.size() - 1;
result = this->get(lower_idx);
lower_idx = x_grid.size() - 1;
result = (*this)[lower_idx];
}
else
{
// Locate the energy bin
lower_idx = energy_grid.find(energy);
CELER_ASSERT(lower_idx + 1 < energy_grid.size());
// Locate the x bin
lower_idx = x_grid.find(x);
CELER_ASSERT(lower_idx + 1 < x_grid.size());

// Interpolate *linearly* on energy using the bin data.
// Interpolate *linearly* on x using the bin data.
LinearInterpolator<real_type> interpolate_xs(
{energy_grid[lower_idx], this->get(lower_idx)},
{energy_grid[lower_idx + 1], this->get(lower_idx + 1)});
result = interpolate_xs(energy);
{x_grid[lower_idx], (*this)[lower_idx]},
{x_grid[lower_idx + 1], (*this)[lower_idx + 1]});
result = interpolate_xs(x);
}

return result;
}

//---------------------------------------------------------------------------//
/*!
* Get the raw cross section data at a particular index.
* Get the tabulated y value at a particular index.
*/
CELER_FUNCTION real_type GenericXsCalculator::get(size_type index) const
CELER_FUNCTION real_type GenericCalculator::operator[](size_type index) const
{
CELER_EXPECT(index < data_.value.size());
return reals_[data_.value[index]];
Expand Down
35 changes: 35 additions & 0 deletions src/celeritas/grid/GenericGridData.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//----------------------------------*-C++-*----------------------------------//
// Copyright 2024 UT-Battelle, LLC, and other Celeritas developers.
// See the top-level COPYRIGHT file for details.
// SPDX-License-Identifier: (Apache-2.0 OR MIT)
//---------------------------------------------------------------------------//
//! \file celeritas/grid/GenericGridData.hh
//---------------------------------------------------------------------------//
#pragma once

#include "corecel/Types.hh"
#include "corecel/data/Collection.hh"
#include "celeritas/Types.hh"

namespace celeritas
{
//---------------------------------------------------------------------------//
/*!
* A generic grid of 1D data with arbitrary interpolation.
*/
struct GenericGridData
{
ItemRange<real_type> grid; //!< x grid
ItemRange<real_type> value; //!< f(x) value
Interp grid_interp; //!< Interpolation along x
Interp value_interp; //!< Interpolation along f(x)

//! Whether the interface is initialized and valid
explicit CELER_FUNCTION operator bool() const
{
return (value.size() >= 2) && grid.size() == value.size();
}
};

//---------------------------------------------------------------------------//
} // namespace celeritas
1 change: 1 addition & 0 deletions src/celeritas/grid/ValueGridInserter.hh
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "corecel/grid/UniformGridData.hh"
#include "celeritas/Types.hh"

#include "GenericGridData.hh"
#include "XsGridData.hh"

namespace celeritas
Expand Down
18 changes: 0 additions & 18 deletions src/celeritas/grid/XsGridData.hh
Original file line number Diff line number Diff line change
Expand Up @@ -49,23 +49,5 @@ struct XsGridData
}
};

//---------------------------------------------------------------------------//
/*!
* A generic grid of 1D data with arbitrary interpolation.
*/
struct GenericGridData
{
ItemRange<real_type> grid; //!< x grid
ItemRange<real_type> value; //!< f(x) value
Interp grid_interp; //!< Interpolation along x
Interp value_interp; //!< Interpolation along f(x)

//! Whether the interface is initialized and valid
explicit CELER_FUNCTION operator bool() const
{
return (value.size() >= 2) && grid.size() == value.size();
}
};

//---------------------------------------------------------------------------//
} // namespace celeritas
2 changes: 1 addition & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ celeritas_add_test(celeritas/global/Stepper.test.cc
#-------------------------------------#
# Grid
set(CELERITASTEST_PREFIX celeritas/grid)
celeritas_add_test(celeritas/grid/GenericXsCalculator.test.cc)
celeritas_add_test(celeritas/grid/GenericCalculator.test.cc)
celeritas_add_test(celeritas/grid/GridIdFinder.test.cc)
celeritas_add_test(celeritas/grid/InverseRangeCalculator.test.cc)
celeritas_add_test(celeritas/grid/PolyEvaluator.test.cc)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
// See the top-level COPYRIGHT file for details.
// SPDX-License-Identifier: (Apache-2.0 OR MIT)
//---------------------------------------------------------------------------//
//! \file celeritas/grid/GenericXsCalculator.test.cc
//! \file celeritas/grid/GenericCalculator.test.cc
//---------------------------------------------------------------------------//
#include "celeritas/grid/GenericXsCalculator.hh"
#include "celeritas/grid/GenericCalculator.hh"

#include <algorithm>
#include <cmath>
Expand All @@ -24,7 +24,7 @@ namespace test
// TEST HARNESS
//---------------------------------------------------------------------------//

class GenericXsCalculatorTest : public CalculatorTestBase
class GenericCalculatorTest : public CalculatorTestBase
{
protected:
void SetUp() override
Expand All @@ -51,9 +51,13 @@ class GenericXsCalculatorTest : public CalculatorTestBase
// TESTS
//---------------------------------------------------------------------------//

TEST_F(GenericXsCalculatorTest, all)
TEST_F(GenericCalculatorTest, all)
{
GenericXsCalculator calc(data_, ref_);
GenericCalculator calc(data_, ref_);

// Test accessing tabulated data
EXPECT_EQ(4.0, calc[0]);
EXPECT_EQ(2.0, calc[3]);

// Test on grid points
EXPECT_SOFT_EQ(4.0, calc(1));
Expand Down

0 comments on commit a8df93d

Please sign in to comment.