-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a helper class to find the grid value corresponding to a sampled …
…CDF value
- Loading branch information
1 parent
9fb13d4
commit 0d187ba
Showing
4 changed files
with
149 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
//----------------------------------*-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/InverseCdfFinder.hh | ||
//---------------------------------------------------------------------------// | ||
#pragma once | ||
|
||
#include "corecel/Assert.hh" | ||
#include "corecel/Macros.hh" | ||
#include "corecel/cont/Range.hh" | ||
#include "corecel/grid/Interpolator.hh" | ||
#include "corecel/math/Algorithms.hh" | ||
|
||
namespace celeritas | ||
{ | ||
//---------------------------------------------------------------------------// | ||
/*! | ||
* Given a sampled CDF value, find the corresponding grid value. | ||
* | ||
* \tparam G Grid, e.g. \c UniformGrid or \c NonUniformGrid | ||
* \tparam C Calculate the CDF at a given grid index | ||
* | ||
* Both the input grid and the CDF must be monotonically increasing. The | ||
* sampled CDF value must be in range. | ||
*/ | ||
template<class G, class C> | ||
class InverseCdfFinder | ||
{ | ||
public: | ||
// Construct from grid and CDF calculator | ||
inline CELER_FUNCTION InverseCdfFinder(G const& grid, C&& calc_cdf); | ||
|
||
// Find and interpolate the grid value corresponding to the given CDF | ||
inline CELER_FUNCTION real_type operator()(real_type cdf) const; | ||
|
||
private: | ||
G const& grid_; | ||
C calc_cdf_; | ||
}; | ||
|
||
//---------------------------------------------------------------------------// | ||
// INLINE DEFINITIONS | ||
//---------------------------------------------------------------------------// | ||
/*! | ||
* Construct from grid and CDF calculator. | ||
*/ | ||
template<class G, class C> | ||
CELER_FUNCTION | ||
InverseCdfFinder<G, C>::InverseCdfFinder(G const& grid, C&& calc_cdf) | ||
: grid_(grid), calc_cdf_(celeritas::forward<C>(calc_cdf)) | ||
{ | ||
} | ||
|
||
//---------------------------------------------------------------------------// | ||
/*! | ||
* Find and interpolate the grid value corresponding to the given CDF. | ||
*/ | ||
template<class G, class C> | ||
CELER_FUNCTION real_type InverseCdfFinder<G, C>::operator()(real_type cdf) const | ||
{ | ||
CELER_EXPECT(cdf >= 0 && cdf < 1); | ||
|
||
// Find the grid index of the sampled CDF value | ||
Range indices(grid_.size()); | ||
auto iter = celeritas::lower_bound( | ||
indices.begin(), indices.end(), cdf, [this](size_type i, real_type c) { | ||
return calc_cdf_(i) < c; | ||
}); | ||
CELER_ASSERT(iter != indices.end()); | ||
if (calc_cdf_(*iter) != cdf) | ||
{ | ||
--iter; | ||
} | ||
size_type i = iter - indices.begin(); | ||
|
||
// Calculate the grid value corresponding to the sampled CDF value | ||
return LinearInterpolator<real_type>{ | ||
{calc_cdf_(i), grid_[i]}, {calc_cdf_(i + 1), grid_[i + 1]}}(cdf); | ||
} | ||
|
||
//---------------------------------------------------------------------------// | ||
} // namespace celeritas |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
//----------------------------------*-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/InverseCdfFinder.test.cc | ||
//---------------------------------------------------------------------------// | ||
#include "celeritas/grid/InverseCdfFinder.hh" | ||
|
||
#include "corecel/data/Collection.hh" | ||
#include "corecel/data/CollectionBuilder.hh" | ||
#include "corecel/grid/NonuniformGrid.hh" | ||
|
||
#include "celeritas_test.hh" | ||
|
||
namespace celeritas | ||
{ | ||
namespace test | ||
{ | ||
//---------------------------------------------------------------------------// | ||
|
||
class InverseCdfFinderTest : public Test | ||
{ | ||
protected: | ||
using GridT = NonuniformGrid<real_type>; | ||
|
||
void SetUp() override | ||
{ | ||
CollectionBuilder build(&data); | ||
values = build.insert_back({0.0, 0.2, 0.5, 1.5, 6.0, 10.0}); | ||
ref = data; | ||
} | ||
|
||
ItemRange<real_type> values; | ||
Collection<real_type, Ownership::value, MemSpace::host> data; | ||
Collection<real_type, Ownership::const_reference, MemSpace::host> ref; | ||
}; | ||
|
||
TEST_F(InverseCdfFinderTest, all) | ||
{ | ||
GridT grid(values, ref); | ||
std::vector<real_type> cdf{0.0, 0.1, 0.2, 0.8, 0.9, 1.0}; | ||
InverseCdfFinder find(grid, [&cdf](size_type i) { return cdf[i]; }); | ||
|
||
EXPECT_SOFT_EQ(0, find(0)); | ||
EXPECT_SOFT_EQ(0.1, find(0.05)); | ||
EXPECT_SOFT_EQ(0.2, find(0.1)); | ||
EXPECT_SOFT_EQ(1, find(0.5)); | ||
EXPECT_SOFT_EQ(6.0, find(0.9)); | ||
EXPECT_SOFT_EQ(8.0, find(0.95)); | ||
EXPECT_SOFT_EQ(9.99996, find(0.999999)); | ||
} | ||
|
||
//---------------------------------------------------------------------------// | ||
} // namespace test | ||
} // namespace celeritas |