Skip to content

Commit

Permalink
Remove unnecessary structs and refactor NeutronElasticData
Browse files Browse the repository at this point in the history
  • Loading branch information
whokion committed Feb 26, 2024
1 parent 1bcb597 commit ecd803c
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 106 deletions.
86 changes: 13 additions & 73 deletions src/celeritas/neutron/data/NeutronElasticData.hh
Original file line number Diff line number Diff line change
Expand Up @@ -60,99 +60,37 @@ struct NeutronElasticIds

//---------------------------------------------------------------------------//
/*!
* Microscopic cross section data for the neutron elastic interaction.
* Device data for creating an interactor.
*/
template<Ownership W, MemSpace M>
struct NeutronElasticXsData
struct NeutronElasticData
{
using XsUnits = units::Native; // [len^2]

template<class T>
using Items = Collection<T, W, M>;
template<class T>
using ElementItems = Collection<T, W, M, ElementId>;

//// MEMBER DATA ////

Items<real_type> reals;
ElementItems<GenericGridData> elements;

//// MEMBER FUNCTIONS ////

//! Whether all data are assigned and valid
explicit CELER_FUNCTION operator bool() const
{
return !reals.empty() && !elements.empty();
}

//! Assign from another set of data
template<Ownership W2, MemSpace M2>
NeutronElasticXsData& operator=(NeutronElasticXsData<W2, M2> const& other)
{
CELER_EXPECT(other);
reals = other.reals;
elements = other.elements;
return *this;
}
};

//---------------------------------------------------------------------------//
/*!
* A-dependent data for the differential cross section (momentum transfer) of
* the CHIPS neutron-nucleus elastic model.
*/
template<Ownership W, MemSpace M>
struct NeutronElasticDiffXsData
{
template<class T>
using IsotopeItems = Collection<T, W, M, IsotopeId>;

//// MEMBER DATA ////

IsotopeItems<ChipsDiffXsCoefficients> coeffs;

//// MEMBER FUNCTIONS ////

//! Whether all data are assigned and valid
explicit CELER_FUNCTION operator bool() const
{
return !coeffs.empty() && !coeffs.empty();
}

//! Assign from another set of data
template<Ownership W2, MemSpace M2>
NeutronElasticDiffXsData&
operator=(NeutronElasticDiffXsData<W2, M2> const& other)
{
CELER_EXPECT(other);
coeffs = other.coeffs;
return *this;
}
};

//---------------------------------------------------------------------------//
/*!
* Device data for creating an interactor.
*/
template<Ownership W, MemSpace M>
struct NeutronElasticData
{
//! Model and particle IDs
NeutronElasticIds ids;

//! Particle mass * c^2 [MeV]
units::MevMass neutron_mass;

//! Neutron elastic cross section (G4PARTICLEXS/neutron/elZ) data
NeutronElasticXsData<W, M> xs;

//! Neutron elastic differential cross section coefficients
NeutronElasticDiffXsData<W, M> diff_xs;
//! Microscopic (element) cross section data (G4PARTICLEXS/neutron/elZ)
Items<real_type> reals;
ElementItems<GenericGridData> micro_xs;

//// MEMBER FUNCTIONS ////
//! A-dependent coefficients for the momentum transfer) of the CHIPS model
IsotopeItems<ChipsDiffXsCoefficients> coeffs;

//! Model's minimum and maximum energy limit [MeV]
static CELER_CONSTEXPR_FUNCTION units::MevEnergy mim_valid_energy()
static CELER_CONSTEXPR_FUNCTION units::MevEnergy min_valid_energy()
{
return units::MevEnergy{1e-5};
}
Expand All @@ -165,7 +103,8 @@ struct NeutronElasticData
//! Whether the data are assigned
explicit CELER_FUNCTION operator bool() const
{
return ids && neutron_mass > zero_quantity();
return ids && neutron_mass > zero_quantity() && !reals.empty()
&& !micro_xs.empty() && !coeffs.empty();
}

//! Assign from another set of data
Expand All @@ -175,8 +114,9 @@ struct NeutronElasticData
CELER_EXPECT(other);
ids = other.ids;
neutron_mass = other.neutron_mass;
xs = other.xs;
diff_xs = other.diff_xs;
reals = other.reals;
micro_xs = other.micro_xs;
coeffs = other.coeffs;
return *this;
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ CELER_FUNCTION
MomentumTransferSampler::MomentumTransferSampler(NeutronElasticRef const& shared,
IsotopeView const& target,
Momentum neutron_p)
: par_(shared.diff_xs.coeffs[target.isotope_id()].par)
: par_(shared.coeffs[target.isotope_id()].par)
, neutron_mass_(shared.neutron_mass)
, target_mass_(target.nuclear_mass())
, a_(target.atomic_mass_number())
Expand Down
36 changes: 18 additions & 18 deletions src/celeritas/neutron/model/ChipsNeutronElasticModel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,22 +50,22 @@ ChipsNeutronElasticModel::ChipsNeutronElasticModel(
data.neutron_mass = particles.get(data.ids.neutron).mass();

// Load neutron elastic cross section data
make_builder(&data.xs.elements).reserve(materials.num_elements());
make_builder(&data.micro_xs).reserve(materials.num_elements());
for (auto el_id : range(ElementId{materials.num_elements()}))
{
AtomicNumber z = materials.get(el_id).atomic_number();
this->append_xs(load_data(z), &data.xs);
this->append_xs(load_data(z), &data);
}
CELER_ASSERT(data.xs.elements.size() == materials.num_elements());
CELER_ASSERT(data.micro_xs.size() == materials.num_elements());

// Add A(Z,N)-dependent coefficients of the CHIPS elastic interaction
make_builder(&data.diff_xs.coeffs).reserve(materials.num_isotopes());
make_builder(&data.coeffs).reserve(materials.num_isotopes());
for (auto iso_id : range(IsotopeId{materials.num_isotopes()}))
{
AtomicMassNumber a = materials.get(iso_id).atomic_mass_number();
this->append_coeffs(a, &data.diff_xs);
this->append_coeffs(a, &data);
}
CELER_ASSERT(data.diff_xs.coeffs.size() == materials.num_isotopes());
CELER_ASSERT(data.coeffs.size() == materials.num_isotopes());

// Move to mirrored data, copying to device
mirror_ = CollectionMirror<NeutronElasticData>{std::move(data)};
Expand All @@ -80,7 +80,7 @@ auto ChipsNeutronElasticModel::applicability() const -> SetApplicability
{
Applicability neutron_applic;
neutron_applic.particle = this->host_ref().ids.neutron;
neutron_applic.lower = this->host_ref().mim_valid_energy();
neutron_applic.lower = this->host_ref().min_valid_energy();
neutron_applic.upper = this->host_ref().max_valid_energy();

return {neutron_applic};
Expand Down Expand Up @@ -134,20 +134,20 @@ ActionId ChipsNeutronElasticModel::action_id() const
* Construct interaction cross section data for a single element.
*/
void ChipsNeutronElasticModel::append_xs(ImportPhysicsVector const& inp,
HostXsData* xs) const
HostXsData* data) const
{
auto reals = make_builder(&xs->reals);
GenericGridData data;
auto reals = make_builder(&data->reals);
GenericGridData micro_xs;

// Add the tabulated interaction cross section from input
data.grid = reals.insert_back(inp.x.begin(), inp.x.end());
data.value = reals.insert_back(inp.y.begin(), inp.y.end());
data.grid_interp = Interp::linear;
data.value_interp = Interp::linear;
micro_xs.grid = reals.insert_back(inp.x.begin(), inp.x.end());
micro_xs.value = reals.insert_back(inp.y.begin(), inp.y.end());
micro_xs.grid_interp = Interp::linear;
micro_xs.value_interp = Interp::linear;

// Add xs data
CELER_ASSERT(data);
make_builder(&xs->elements).push_back(data);
// Add micro xs data
CELER_ASSERT(micro_xs);
make_builder(&data->micro_xs).push_back(micro_xs);
}

//---------------------------------------------------------------------------//
Expand All @@ -166,7 +166,7 @@ void ChipsNeutronElasticModel::append_xs(ImportPhysicsVector const& inp,
* Wellisch, Eur. Phys. J. A9 (2001).
*/
void ChipsNeutronElasticModel::append_coeffs(AtomicMassNumber A,
HostDiffXsData* data) const
HostXsData* data) const
{
ChipsDiffXsCoefficients coeffs;

Expand Down
6 changes: 2 additions & 4 deletions src/celeritas/neutron/model/ChipsNeutronElasticModel.hh
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,11 @@ class ChipsNeutronElasticModel final : public Model

//// TYPES ////

using HostXsData = HostVal<NeutronElasticXsData>;
using HostDiffXsData = HostVal<NeutronElasticDiffXsData>;
using HostXsData = HostVal<NeutronElasticData>;

//// HELPER FUNCTIONS ////

void append_xs(ImportPhysicsVector const& inp, HostXsData* xs_data) const;
void append_coeffs(AtomicMassNumber A, HostDiffXsData* Q2_data) const;
void append_coeffs(AtomicMassNumber A, HostXsData* xs_data) const;
};

//---------------------------------------------------------------------------//
Expand Down
10 changes: 5 additions & 5 deletions src/celeritas/neutron/xs/NeutronElasticMicroXsCalculator.hh
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace celeritas
{
//---------------------------------------------------------------------------//
/*!
* Calculate neutron elastic cross sections form NeutronElasticXsData
* Calculate neutron elastic cross sections from NeutronElasticXsData
*/
class NeutronElasticMicroXsCalculator
{
Expand Down Expand Up @@ -68,11 +68,11 @@ real_type NeutronElasticMicroXsCalculator::operator()(ElementId el_id) const
{
CELER_EXPECT(el_id);

// Check element cross section data
auto const& xs = shared_.xs;
GenericGridData grid = xs.elements[el_id];
// Get element cross section data
GenericGridData grid = shared_.micro_xs[el_id];

GenericCalculator calc_xs(grid, xs.reals);
// Calculate micro cross section at the given energy
GenericCalculator calc_xs(grid, shared_.reals);
real_type result = calc_xs(inc_energy_.value());

return result;
Expand Down
9 changes: 4 additions & 5 deletions test/celeritas/neutron/NeutronElastic.test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@ TEST_F(NeutronElasticTest, micro_xs)

// Check the size of the element cross section data (G4PARTICLEXS4.0)
NeutronElasticRef shared = model_->host_ref();
auto const& xs = shared.xs;
GenericGridData grid = xs.elements[el_id];
GenericGridData grid = shared.micro_xs[el_id];
EXPECT_EQ(grid.grid.size(), 181);

// Microscopic cross section (\f$ mm^{2} \f$) in [1e-05:1e+4] (MeV)
Expand Down Expand Up @@ -134,10 +133,10 @@ TEST_F(NeutronElasticTest, diff_xs_coeffs)
{
// Get A-dependent parameters of CHIPS differential cross sections used
// for sampling the momentum transfer.
auto const& diff_xs = model_->host_ref().diff_xs;
auto const& coeffs = model_->host_ref().coeffs;

// Set the target isotope: He4 (36 parameters for light nuclei, A < 6.5)
ChipsDiffXsCoefficients he4_coeff = diff_xs.coeffs[IsotopeId{1}];
ChipsDiffXsCoefficients he4_coeff = coeffs[IsotopeId{1}];
EXPECT_EQ(he4_coeff.par.size(), 42);
EXPECT_EQ(he4_coeff.par[0], 16000);
EXPECT_EQ(he4_coeff.par[10], 26.99741289550769);
Expand All @@ -147,7 +146,7 @@ TEST_F(NeutronElasticTest, diff_xs_coeffs)
EXPECT_EQ(he4_coeff.par[36], 0);

// Set the target isotope: Cu63 (42 parameters for heavy nuclei, A > 6.5)
ChipsDiffXsCoefficients cu63_coeff = diff_xs.coeffs[IsotopeId{2}];
ChipsDiffXsCoefficients cu63_coeff = coeffs[IsotopeId{2}];
EXPECT_EQ(cu63_coeff.par.size(), 42);
EXPECT_EQ(cu63_coeff.par[0], 527.781478797624);
EXPECT_EQ(cu63_coeff.par[10], 9.842761904761872);
Expand Down

0 comments on commit ecd803c

Please sign in to comment.