From 5296e80049a704d1ac286ea12d0166493876e152 Mon Sep 17 00:00:00 2001 From: Sophie Blondel Date: Fri, 30 Jun 2023 09:55:13 -0400 Subject: [PATCH] Saving the left rate information for clusters with sink terms (#112). --- .../include/xolotl/core/network/PSIReaction.h | 5 + .../include/xolotl/core/network/PSITraits.h | 39 +++++- .../xolotl/core/network/SinkReaction.h | 7 + .../xolotl/core/network/impl/PSIReaction.tpp | 27 +++- .../core/network/impl/PSIReactionNetwork.tpp | 127 +++++++++++++----- .../options/include/xolotl/options/IOptions.h | 14 +- .../options/include/xolotl/options/Options.h | 24 ++-- 7 files changed, 185 insertions(+), 58 deletions(-) diff --git a/xolotl/core/include/xolotl/core/network/PSIReaction.h b/xolotl/core/include/xolotl/core/network/PSIReaction.h index bd791174e..61712df5a 100644 --- a/xolotl/core/include/xolotl/core/network/PSIReaction.h +++ b/xolotl/core/include/xolotl/core/network/PSIReaction.h @@ -62,6 +62,7 @@ class PSISinkReaction : PSISinkReaction>; using Superclass::Superclass; + using IndexType = typename Superclass::IndexType; KOKKOS_INLINE_FUNCTION double @@ -70,6 +71,10 @@ class PSISinkReaction : KOKKOS_INLINE_FUNCTION double getSinkStrength(); + + KOKKOS_INLINE_FUNCTION + double + computeRate(IndexType gridIndex, double time = 0.0); }; template class PSITrapMutationReaction : diff --git a/xolotl/core/include/xolotl/core/network/PSITraits.h b/xolotl/core/include/xolotl/core/network/PSITraits.h index 740cfcef4..35e59b53f 100644 --- a/xolotl/core/include/xolotl/core/network/PSITraits.h +++ b/xolotl/core/include/xolotl/core/network/PSITraits.h @@ -296,12 +296,19 @@ struct ClusterDataExtra, MemSpace> { using NetworkType = PSIReactionNetwork; + template + using View = ViewType; + + using IndexType = detail::ReactionNetworkIndexType; + ClusterDataExtra() = default; template KOKKOS_INLINE_FUNCTION ClusterDataExtra(const ClusterDataExtra& data) : - trapMutationData(data.trapMutationData) + trapMutationData(data.trapMutationData), + leftSideRates(data.leftSideRates), + sinkMap(data.sinkMap) { } @@ -310,17 +317,45 @@ struct ClusterDataExtra, MemSpace> deepCopy(const ClusterDataExtra& data) { trapMutationData.deepCopy(data.trapMutationData); + + if (!data.leftSideRates.is_allocated()) { + return; + } + + if (!leftSideRates.is_allocated()) { + leftSideRates = create_mirror_view(data.leftSideRates); + } + deep_copy(leftSideRates, data.leftSideRates); + + if (!sinkMap.is_allocated()) { + sinkMap = create_mirror_view(data.sinkMap); + } + deep_copy(sinkMap, data.sinkMap); } std::uint64_t getDeviceMemorySize() const noexcept { - return trapMutationData.getDeviceMemorySize(); + std::uint64_t ret = 0; + ret += trapMutationData.getDeviceMemorySize(); + ret += leftSideRates.required_allocation_size(leftSideRates.extent(0)); + ret += sinkMap.required_allocation_size(sinkMap.extent(0)); + return ret; + } + + void + initialize(IndexType numClusters) + { + leftSideRates = View("Left Side Rates", numClusters); + sinkMap = View("Sink Map", numClusters); } using TrapMutationData = TrapMutationClusterData>; TrapMutationData trapMutationData; + + View leftSideRates; + View sinkMap; }; } // namespace detail } // namespace network diff --git a/xolotl/core/include/xolotl/core/network/SinkReaction.h b/xolotl/core/include/xolotl/core/network/SinkReaction.h index b0ddf0ef6..6cfc243cc 100644 --- a/xolotl/core/include/xolotl/core/network/SinkReaction.h +++ b/xolotl/core/include/xolotl/core/network/SinkReaction.h @@ -64,6 +64,13 @@ class SinkReaction : public Reaction double computeRate(IndexType gridIndex, double time = 0.0); + KOKKOS_INLINE_FUNCTION + IndexType + getReactantId() + { + return _reactant; + } + private: KOKKOS_INLINE_FUNCTION void diff --git a/xolotl/core/include/xolotl/core/network/impl/PSIReaction.tpp b/xolotl/core/include/xolotl/core/network/impl/PSIReaction.tpp index fed2208b3..4315dd9e6 100644 --- a/xolotl/core/include/xolotl/core/network/impl/PSIReaction.tpp +++ b/xolotl/core/include/xolotl/core/network/impl/PSIReaction.tpp @@ -237,9 +237,34 @@ PSISinkReaction::getSinkStrength() { constexpr double pi = ::xolotl::core::pi; double grainSize = 50000.0; // 50 um - return 1.0 / (pi * grainSize * grainSize); } + +template +KOKKOS_INLINE_FUNCTION +double +PSISinkReaction::computeRate(IndexType gridIndex, double time) +{ + auto cl = this->_clusterData->getCluster(this->_reactant); + double dc = cl.getDiffusionCoefficient(gridIndex); + + auto rate = this->_clusterData->extraData.leftSideRates; + double s_m = 0.0; + if (rate.size() > 0) { + // Look for the correct index + auto sMap = this->_clusterData->extraData.sinkMap; + auto i = 0; + for (i; i < sMap.size(); i++) { + if (sMap(i) == this->_reactant) + break; + } + s_m = rate(i) / dc; + } + + double strength = this->getSinkBias() * this->getSinkStrength() * dc; + + return strength; +} } // namespace network } // namespace core } // namespace xolotl diff --git a/xolotl/core/include/xolotl/core/network/impl/PSIReactionNetwork.tpp b/xolotl/core/include/xolotl/core/network/impl/PSIReactionNetwork.tpp index 5e7842a6a..a30ef2369 100644 --- a/xolotl/core/include/xolotl/core/network/impl/PSIReactionNetwork.tpp +++ b/xolotl/core/include/xolotl/core/network/impl/PSIReactionNetwork.tpp @@ -65,48 +65,66 @@ void PSIReactionNetwork::updateExtraClusterData( const std::vector& gridTemps, const std::vector& gridDepths) { - if (!this->_enableTrapMutation) { - return; - } - - // Check which temperature index to use - IdType tempId = 0; - for (tempId = 0; tempId < gridDepths.size(); tempId++) { - if (gridDepths[tempId] > 0.01) - break; - } + if (this->_enableTrapMutation) { + // Check which temperature index to use + IdType tempId = 0; + for (tempId = 0; tempId < gridDepths.size(); tempId++) { + if (gridDepths[tempId] > 0.01) + break; + } - _tmHandler->updateData(gridTemps[tempId]); + _tmHandler->updateData(gridTemps[tempId]); - auto& tmData = this->_clusterData.h_view().extraData.trapMutationData; + auto& tmData = this->_clusterData.h_view().extraData.trapMutationData; - using Kokkos::HostSpace; - using Kokkos::MemoryUnmanaged; + using Kokkos::HostSpace; + using Kokkos::MemoryUnmanaged; + + auto desorpInit = _tmHandler->getDesorptionInitializer(); + auto subpaving = this->_subpaving; + IndexType desorpId = this->invalidIndex(); + Kokkos::parallel_reduce( + 1, + KOKKOS_LAMBDA(std::size_t, IndexType & running) { + Composition comp{}; + comp[Species::He] = desorpInit.size; + running = static_cast(subpaving.findTileId(comp)); + }, + desorpId); + auto desorp = create_mirror_view(tmData.desorption); + desorp() = detail::Desorption{desorpInit, desorpId}; + deep_copy(tmData.desorption, desorp); + + auto depths = Kokkos::View( + _tmHandler->getDepths().data()); + deep_copy(tmData.tmDepths, depths); + + auto vSizes = + Kokkos::View( + _tmHandler->getVacancySizes().data()); + deep_copy(tmData.tmVSizes, vSizes); + + this->invalidateDataMirror(); + } - auto desorpInit = _tmHandler->getDesorptionInitializer(); - auto subpaving = this->_subpaving; - IndexType desorpId = this->invalidIndex(); - Kokkos::parallel_reduce( - 1, - KOKKOS_LAMBDA(std::size_t, IndexType & running) { - Composition comp{}; - comp[Species::He] = desorpInit.size; - running = static_cast(subpaving.findTileId(comp)); - }, - desorpId); - auto desorp = create_mirror_view(tmData.desorption); - desorp() = detail::Desorption{desorpInit, desorpId}; - deep_copy(tmData.desorption, desorp); + if (this->_enableSink) { + using SinkReactionType = typename Superclass::Traits::SinkReactionType; + auto sinkReactions = + this->_reactions.template getView(); + this->_clusterData.h_view().extraData.initialize(sinkReactions.size()); - auto depths = Kokkos::View( - _tmHandler->getDepths().data()); - deep_copy(tmData.tmDepths, depths); + this->copyClusterDataView(); - auto vSizes = Kokkos::View( - _tmHandler->getVacancySizes().data()); - deep_copy(tmData.tmVSizes, vSizes); + auto& clusterData = this->_clusterData.d_view; + Kokkos::parallel_for( + "PSIReactionNetwork::updateExtraClusterData", sinkReactions.size(), + KOKKOS_LAMBDA(IndexType i) { + clusterData().extraData.sinkMap(i) = + sinkReactions(i).getReactantId(); + }); - this->invalidateDataMirror(); + this->invalidateDataMirror(); + } } template @@ -144,6 +162,24 @@ PSIReactionNetwork::computeFluxesPreProcess( updateDesorptionLeftSideRate(concentrations, gridIndex); selectTrapMutationReactions(surfaceDepth, spacing); } + if (this->_enableSink) { + // Compute the left side rates + auto& clusterData = this->_clusterData.d_view; + Kokkos::parallel_for( + "PSIReactionNetwork::computeFluxesPreProcess", + this->_clusterData.h_view().extraData.sinkMap.size(), + KOKKOS_LAMBDA(IndexType i) { + clusterData().extraData.leftSideRates(i) = 0.0; + }); + + // Update the sink rate + using SinkReactionType = typename Superclass::Traits::SinkReactionType; + auto sinkReactions = + this->_reactions.template getView(); + Kokkos::parallel_for( + "PSIReactionNetwork::computeFluxesPreProcess", sinkReactions.size(), + KOKKOS_LAMBDA(IndexType i) { sinkReactions[i].updateRates(); }); + } } template @@ -156,6 +192,26 @@ PSIReactionNetwork::computePartialsPreProcess( updateDesorptionLeftSideRate(concentrations, gridIndex); selectTrapMutationReactions(surfaceDepth, spacing); } + if (this->_enableSink) { + // Compute the left side rates + auto& clusterData = this->_clusterData.d_view; + Kokkos::parallel_for( + "PSIReactionNetwork::computeFluxesPreProcess", + this->_clusterData.h_view().extraData.sinkMap.size(), + KOKKOS_LAMBDA(IndexType i) { + clusterData().extraData.leftSideRates(i) = + this->getLeftSideRate(concentrations, + clusterData().extraData.sinkMap(i), gridIndex); + }); + + // Update the sink rate + using SinkReactionType = typename Superclass::Traits::SinkReactionType; + auto sinkReactions = + this->_reactions.template getView(); + Kokkos::parallel_for( + "PSIReactionNetwork::computeFluxesPreProcess", sinkReactions.size(), + KOKKOS_LAMBDA(IndexType i) { sinkReactions[i].updateRates(); }); + } } template @@ -285,7 +341,6 @@ void PSIReactionNetwork::updateReactionRates(double time) { Superclass::updateReactionRates(time); - using TrapMutationReactionType = typename Superclass::Traits::TrapMutationReactionType; // TODO: is this just the local largest rate? Is it correct? diff --git a/xolotl/options/include/xolotl/options/IOptions.h b/xolotl/options/include/xolotl/options/IOptions.h index ace27a290..f01a661c9 100644 --- a/xolotl/options/include/xolotl/options/IOptions.h +++ b/xolotl/options/include/xolotl/options/IOptions.h @@ -132,13 +132,13 @@ class IOptions virtual std::string getPerfHandlerName() const = 0; - /** - * Should we write the performance report to a YAML file? - * - * @return true to enable YAML output - */ - virtual bool - usePerfOutputYAML() const = 0; + /** + * Should we write the performance report to a YAML file? + * + * @return true to enable YAML output + */ + virtual bool + usePerfOutputYAML() const = 0; /** * Obtain the name of the visualization handler to be used diff --git a/xolotl/options/include/xolotl/options/Options.h b/xolotl/options/include/xolotl/options/Options.h index 0c41d5d71..dd2031bb3 100644 --- a/xolotl/options/include/xolotl/options/Options.h +++ b/xolotl/options/include/xolotl/options/Options.h @@ -68,10 +68,10 @@ class Options : public IOptions */ std::string perfHandlerName; - /** - * Output performance report to YAML file? - */ - bool perfOutputYAMLFlag; + /** + * Output performance report to YAML file? + */ + bool perfOutputYAMLFlag; /** * Name of the viz handler @@ -406,14 +406,14 @@ class Options : public IOptions return perfHandlerName; } - /** - * \see IOptions.h - */ - bool - usePerfOutputYAML() const override - { - return perfOutputYAMLFlag; - } + /** + * \see IOptions.h + */ + bool + usePerfOutputYAML() const override + { + return perfOutputYAMLFlag; + } /** * \see IOptions.h