Skip to content

Commit

Permalink
Add generic grid builder and refactor builder code that uses generic …
Browse files Browse the repository at this point in the history
…grids (celeritas-project#1157)

* Refactor particle insertion to use helper class
* Add size attribute to interp
* Initialize grid record
* Deduplicate value grids
* Revert "Deduplicate value grids": The in-place modification for elemental CDFs does not work!!
* IWYU and order backend data after main data
* Rename ValueGridType file
* Add generic grid builder
* Remove unused generic value grid builder
* Remove support for anything but linear/linear
* Use grid builder for cerenkov, neutron data
* Refactor livermore xs inserter
* Fix missing inline
* Fix single precision build
* Address review feedback
  • Loading branch information
sethrj committed Mar 20, 2024
1 parent 7f3b53a commit d9e23f1
Show file tree
Hide file tree
Showing 44 changed files with 496 additions and 350 deletions.
3 changes: 2 additions & 1 deletion src/celeritas/CMakeLists.txt
Expand Up @@ -42,9 +42,10 @@ list(APPEND SOURCES
global/Stepper.cc
global/detail/ActionSequence.cc
global/detail/PinnedAllocator.cc
grid/GenericGridBuilder.cc
grid/ValueGridBuilder.cc
grid/ValueGridData.cc
grid/ValueGridInserter.cc
grid/ValueGridType.cc
grid/VectorUtils.cc
io/AtomicRelaxationReader.cc
io/ImportData.cc
Expand Down
10 changes: 10 additions & 0 deletions src/celeritas/Types.cc
Expand Up @@ -42,6 +42,16 @@ UnitSystem to_unit_system(std::string const& s)
return from_string(s);
}

//---------------------------------------------------------------------------//
/*!
* Get a string corresponding to an interpolation.
*/
char const* to_cstring(Interp value)
{
static EnumStringMapper<Interp> const to_cstring_impl{"linear", "log"};
return to_cstring_impl(value);
}

//---------------------------------------------------------------------------//
/*!
* Get a string corresponding to a state of matter.
Expand Down
6 changes: 5 additions & 1 deletion src/celeritas/Types.hh
Expand Up @@ -94,7 +94,8 @@ enum class UnitSystem
enum class Interp
{
linear,
log
log,
size_
};

//---------------------------------------------------------------------------//
Expand Down Expand Up @@ -195,6 +196,9 @@ struct StepLimit
// Get a string corresponding to a unit system
char const* to_cstring(UnitSystem);

// Get a string corresponding to an interpolation
char const* to_cstring(Interp);

// Get a unit system corresponding to a string
UnitSystem to_unit_system(std::string const& s);

Expand Down
8 changes: 5 additions & 3 deletions src/celeritas/em/data/LivermorePEData.hh
Expand Up @@ -100,26 +100,28 @@ struct LivermorePEXsData

//// MEMBER DATA ////

Items<real_type> reals;
Items<LivermoreSubshell> shells;
ElementItems<LivermoreElement> elements;

// Backend data
Items<real_type> reals;

//// MEMBER FUNCTIONS ////

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

//! Assign from another set of data
template<Ownership W2, MemSpace M2>
LivermorePEXsData& operator=(LivermorePEXsData<W2, M2> const& other)
{
CELER_EXPECT(other);
reals = other.reals;
shells = other.shells;
elements = other.elements;
reals = other.reals;
return *this;
}
};
Expand Down
83 changes: 4 additions & 79 deletions src/celeritas/em/model/LivermorePEModel.cc
Expand Up @@ -13,8 +13,6 @@

#include "celeritas_config.h"
#include "corecel/cont/Range.hh"
#include "corecel/data/Collection.hh"
#include "corecel/data/CollectionBuilder.hh"
#include "celeritas/em/data/LivermorePEData.hh"
#include "celeritas/em/executor/LivermorePEExecutor.hh"
#include "celeritas/global/ActionLauncher.hh"
Expand All @@ -23,14 +21,15 @@
#include "celeritas/global/TrackExecutor.hh"
#include "celeritas/grid/XsGridData.hh"
#include "celeritas/io/ImportLivermorePE.hh"
#include "celeritas/io/ImportPhysicsVector.hh"
#include "celeritas/mat/ElementView.hh"
#include "celeritas/mat/MaterialParams.hh"
#include "celeritas/phys/InteractionApplier.hh"
#include "celeritas/phys/PDGNumber.hh"
#include "celeritas/phys/ParticleParams.hh"
#include "celeritas/phys/ParticleView.hh"

#include "detail/LivermoreXsInserter.hh"

namespace celeritas
{
//---------------------------------------------------------------------------//
Expand Down Expand Up @@ -63,11 +62,11 @@ LivermorePEModel::LivermorePEModel(ActionId id,
particles.get(host_data.ids.electron).mass());

// Load Livermore cross section data
make_builder(&host_data.xs.elements).reserve(materials.num_elements());
detail::LivermoreXsInserter insert_element(&host_data.xs);
for (auto el_id : range(ElementId{materials.num_elements()}))
{
AtomicNumber z = materials.get(el_id).atomic_number();
this->append_element(load_data(z), &host_data.xs);
insert_element(load_data(z));
}
CELER_ASSERT(host_data.xs.elements.size() == materials.num_elements());

Expand Down Expand Up @@ -134,79 +133,5 @@ ActionId LivermorePEModel::action_id() const
return this->host_ref().ids.action;
}

//---------------------------------------------------------------------------//
/*!
* Construct cross section data for a single element.
*/
void LivermorePEModel::append_element(ImportLivermorePE const& inp,
HostXsData* xs) const
{
CELER_EXPECT(!inp.shells.empty());
if constexpr (CELERITAS_DEBUG)
{
CELER_EXPECT(inp.thresh_lo <= inp.thresh_hi);
for (auto const& shell : inp.shells)
{
CELER_EXPECT(shell.param_lo.size() == 6);
CELER_EXPECT(shell.param_hi.size() == 6);
CELER_EXPECT(shell.binding_energy <= inp.thresh_lo);
}
}

auto reals = make_builder(&xs->reals);

LivermoreElement el;

// Add tabulated total cross sections
el.xs_lo.grid = reals.insert_back(inp.xs_lo.x.begin(), inp.xs_lo.x.end());
el.xs_lo.value = reals.insert_back(inp.xs_lo.y.begin(), inp.xs_lo.y.end());
el.xs_lo.grid_interp = Interp::linear;
el.xs_lo.value_interp = Interp::linear;
el.xs_hi.grid = reals.insert_back(inp.xs_hi.x.begin(), inp.xs_hi.x.end());
el.xs_hi.value = reals.insert_back(inp.xs_hi.y.begin(), inp.xs_hi.y.end());
el.xs_hi.grid_interp = Interp::linear;
el.xs_hi.value_interp = Interp::linear; // TODO: spline

// Add energy thresholds for using low and high xs parameterization
el.thresh_lo = MevEnergy(inp.thresh_lo);
el.thresh_hi = MevEnergy(inp.thresh_hi);

// Allocate subshell data
std::vector<LivermoreSubshell> shells(inp.shells.size());

// Add subshell data
for (auto i : range(inp.shells.size()))
{
// Ionization energy
shells[i].binding_energy = MevEnergy(inp.shells[i].binding_energy);

// Tabulated subshell cross section
shells[i].xs.grid = reals.insert_back(inp.shells[i].energy.begin(),
inp.shells[i].energy.end());
shells[i].xs.value = reals.insert_back(inp.shells[i].xs.begin(),
inp.shells[i].xs.end());
shells[i].xs.grid_interp = Interp::linear;
shells[i].xs.value_interp = Interp::linear;

// Subshell cross section fit parameters
std::copy(inp.shells[i].param_lo.begin(),
inp.shells[i].param_lo.end(),
shells[i].param[0].begin());
std::copy(inp.shells[i].param_hi.begin(),
inp.shells[i].param_hi.end(),
shells[i].param[1].begin());

CELER_ASSERT(shells[i]);
}
el.shells
= make_builder(&xs->shells).insert_back(shells.begin(), shells.end());

// Add the elemental data
CELER_ASSERT(el);
make_builder(&xs->elements).push_back(el);

CELER_ENSURE(el.shells.size() == inp.shells.size());
}

//---------------------------------------------------------------------------//
} // namespace celeritas
5 changes: 0 additions & 5 deletions src/celeritas/em/model/LivermorePEModel.hh
Expand Up @@ -29,7 +29,6 @@ class LivermorePEModel final : public Model
{
public:
//!@{
using MevEnergy = units::MevEnergy;
using ReadData = std::function<ImportLivermorePE(AtomicNumber)>;
using HostRef = LivermorePEHostRef;
using DeviceRef = LivermorePEDeviceRef;
Expand Down Expand Up @@ -75,10 +74,6 @@ class LivermorePEModel final : public Model
private:
// Host/device storage and reference
CollectionMirror<LivermorePEData> data_;

using HostXsData = HostVal<LivermorePEXsData>;
void
append_element(ImportLivermorePE const& inp, HostXsData* xs_data) const;
};

//---------------------------------------------------------------------------//
Expand Down
127 changes: 127 additions & 0 deletions src/celeritas/em/model/detail/LivermoreXsInserter.hh
@@ -0,0 +1,127 @@
//----------------------------------*-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/em/model/detail/LivermoreXsInserter.hh
//---------------------------------------------------------------------------//
#pragma once

#include "corecel/data/CollectionBuilder.hh"
#include "celeritas/em/data/LivermorePEData.hh"
#include "celeritas/grid/GenericGridBuilder.hh"
#include "celeritas/io/ImportLivermorePE.hh"
#include "celeritas/io/ImportPhysicsVector.hh"

namespace celeritas
{
namespace detail
{
//---------------------------------------------------------------------------//
/*!
* Construct Livermore Photoelectric cross section data from imported data.
*/
class LivermoreXsInserter
{
public:
//!@{
//! \name Type aliases
using Data = HostVal<LivermorePEXsData>;
//!@}

public:
// Construct with pointer to host data
explicit inline LivermoreXsInserter(Data* data);

// Construct cross section data for a single element
inline void operator()(ImportLivermorePE const& inp);

private:
GenericGridBuilder build_grid_;

CollectionBuilder<LivermoreSubshell> shells_;
CollectionBuilder<LivermoreElement, MemSpace::host, ElementId> elements_;
};

//---------------------------------------------------------------------------//
// INLINE DEFINITIONS
//---------------------------------------------------------------------------//
/*!
* Construct with data.
*/
LivermoreXsInserter::LivermoreXsInserter(Data* data)
: build_grid_{&data->reals}
, shells_{&data->shells}
, elements_{&data->elements}
{
CELER_EXPECT(data);
}

//---------------------------------------------------------------------------//
/*!
* Construct cross section data for a single element.
*/
void LivermoreXsInserter::operator()(ImportLivermorePE const& inp)
{
CELER_EXPECT(!inp.shells.empty());
if constexpr (CELERITAS_DEBUG)
{
CELER_EXPECT(inp.thresh_lo <= inp.thresh_hi);
for (auto const& shell : inp.shells)
{
CELER_EXPECT(shell.param_lo.size() == 6);
CELER_EXPECT(shell.param_hi.size() == 6);
CELER_EXPECT(shell.binding_energy <= inp.thresh_lo);
}
}
using units::MevEnergy;

LivermoreElement el;

// Add tabulated total cross sections
if (inp.xs_lo)
{
// Z < 3 have no low-energy cross sections
el.xs_lo = build_grid_(inp.xs_lo);
}
el.xs_hi = build_grid_(inp.xs_hi);

// Add energy thresholds for using low and high xs parameterization
el.thresh_lo = MevEnergy(inp.thresh_lo);
el.thresh_hi = MevEnergy(inp.thresh_hi);

// Allocate subshell data
std::vector<LivermoreSubshell> shells(inp.shells.size());

// Add subshell data
for (auto i : range(inp.shells.size()))
{
// Ionization energy
shells[i].binding_energy = MevEnergy(inp.shells[i].binding_energy);

// Tabulated subshell cross section
shells[i].xs = build_grid_(make_span(inp.shells[i].energy),
make_span(inp.shells[i].xs));

// Subshell cross section fit parameters
std::copy(inp.shells[i].param_lo.begin(),
inp.shells[i].param_lo.end(),
shells[i].param[0].begin());
std::copy(inp.shells[i].param_hi.begin(),
inp.shells[i].param_hi.end(),
shells[i].param[1].begin());

CELER_ASSERT(shells[i]);
}
el.shells = shells_.insert_back(shells.begin(), shells.end());

// Add the elemental data
CELER_ASSERT(el);
elements_.push_back(el);

CELER_ENSURE(el.shells.size() == inp.shells.size());
}

//---------------------------------------------------------------------------//
} // namespace detail
} // namespace celeritas
2 changes: 1 addition & 1 deletion src/celeritas/em/msc/detail/UrbanMscHelper.hh
Expand Up @@ -17,7 +17,7 @@
#include "celeritas/grid/EnergyLossCalculator.hh"
#include "celeritas/grid/InverseRangeCalculator.hh"
#include "celeritas/grid/RangeCalculator.hh"
#include "celeritas/grid/ValueGridData.hh"
#include "celeritas/grid/ValueGridType.hh"
#include "celeritas/grid/XsCalculator.hh"
#include "celeritas/phys/ParticleTrackView.hh"
#include "celeritas/phys/PhysicsTrackView.hh"
Expand Down
2 changes: 1 addition & 1 deletion src/celeritas/em/process/EPlusAnnihilationProcess.cc
Expand Up @@ -14,7 +14,7 @@
#include "corecel/cont/Range.hh"
#include "celeritas/em/model/EPlusGGModel.hh"
#include "celeritas/grid/ValueGridBuilder.hh"
#include "celeritas/grid/ValueGridData.hh"
#include "celeritas/grid/ValueGridType.hh"
#include "celeritas/phys/Model.hh"
#include "celeritas/phys/PDGNumber.hh"

Expand Down
2 changes: 2 additions & 0 deletions src/celeritas/grid/GenericCalculator.hh
Expand Up @@ -22,6 +22,8 @@ namespace celeritas
//---------------------------------------------------------------------------//
/*!
* Find and interpolate values on a nonuniform grid.
*
* Out-of-bounds values are snapped to the closest grid points.
*/
class GenericCalculator
{
Expand Down

0 comments on commit d9e23f1

Please sign in to comment.