Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-02-22 10:31:32

0001 //----------------------------------*-C++-*----------------------------------//
0002 // Copyright 2023-2024 UT-Battelle, LLC, and other Celeritas developers.
0003 // See the top-level COPYRIGHT file for details.
0004 // SPDX-License-Identifier: (Apache-2.0 OR MIT)
0005 //---------------------------------------------------------------------------//
0006 //! \file celeritas/user/ActionDiagnostic.hh
0007 //---------------------------------------------------------------------------//
0008 #pragma once
0009 
0010 #include <map>
0011 #include <memory>
0012 #include <vector>
0013 
0014 #include "corecel/data/StreamStore.hh"
0015 #include "corecel/io/OutputInterface.hh"
0016 #include "celeritas/global/ActionInterface.hh"
0017 #include "celeritas/global/CoreTrackData.hh"
0018 
0019 #include "ParticleTallyData.hh"
0020 
0021 namespace celeritas
0022 {
0023 //---------------------------------------------------------------------------//
0024 class ParticleParams;
0025 class ActionRegistry;
0026 
0027 //---------------------------------------------------------------------------//
0028 /*!
0029  * Tally post-step actions for each particle type.
0030  *
0031  * This adds an \c action-diagnostic entry to the \c result category of the
0032  * main Celeritas output that has the number of times a post-step action was
0033  * selected, grouped by particle type. It integrates over all steps and all
0034  * events.
0035  */
0036 class ActionDiagnostic final : public CoreStepActionInterface,
0037                                public CoreBeginRunActionInterface,
0038                                public OutputInterface
0039 {
0040   public:
0041     //@{
0042     //! \name Type aliases
0043     using CoreStepActionInterface::CoreStateDevice;
0044     using CoreStepActionInterface::CoreStateHost;
0045     //@}
0046 
0047   public:
0048     //!@{
0049     //! \name Type aliases
0050     using WPConstActionRegistry = std::weak_ptr<ActionRegistry const>;
0051     using WPConstParticle = std::weak_ptr<ParticleParams const>;
0052     using MapStringCount = std::map<std::string, size_type>;
0053     using VecCount = std::vector<size_type>;
0054     using VecVecCount = std::vector<VecCount>;
0055     //!@}
0056 
0057   public:
0058     // Construct and add to core params
0059     static std::shared_ptr<ActionDiagnostic>
0060     make_and_insert(CoreParams const& core);
0061 
0062     // Construct with ID, deferring other data till later
0063     explicit ActionDiagnostic(ActionId id);
0064 
0065     // Default destructor
0066     ~ActionDiagnostic();
0067 
0068     //!@{
0069     //! \name Action interface
0070     //! ID of the action
0071     ActionId action_id() const final { return id_; }
0072     //! Short name for the action
0073     std::string_view label() const final { return "action-diagnostic"; }
0074     // Description of the action for user interaction
0075     std::string_view description() const final;
0076     //! Dependency ordering of the action
0077     StepActionOrder order() const final { return StepActionOrder::post; }
0078     //!@}
0079 
0080     //!@{
0081     //! \name BeginRunAction interface
0082     // Set host data at the beginning of a run
0083     void begin_run(CoreParams const&, CoreStateHost&) final;
0084     // Set device data at the beginning of a run
0085     void begin_run(CoreParams const&, CoreStateDevice&) final;
0086     //!@}
0087 
0088     //!@{
0089     //! \name ExplicitAction interface
0090     // Launch kernel with host data
0091     void step(CoreParams const&, CoreStateHost&) const final;
0092     // Launch kernel with device data
0093     void step(CoreParams const&, CoreStateDevice&) const final;
0094     //!@}
0095 
0096     //!@{
0097     //! \name Output interface
0098     //! Category of data to write
0099     Category category() const final { return Category::result; }
0100     // Write output to the given JSON object
0101     void output(JsonPimpl*) const final;
0102     //!@}
0103 
0104     // Get the nonzero diagnostic results accumulated over all streams
0105     MapStringCount calc_actions_map() const;
0106 
0107     // Get the diagnostic results accumulated over all streams
0108     VecVecCount calc_actions() const;
0109 
0110     // Diagnostic state data size (number of particles times number of actions)
0111     size_type state_size() const;
0112 
0113     // Reset diagnostic results
0114     void clear();
0115 
0116   private:
0117     using StoreT = StreamStore<ParticleTallyParamsData, ParticleTallyStateData>;
0118 
0119     ActionId id_;
0120 
0121     WPConstActionRegistry action_reg_;
0122     WPConstParticle particle_;
0123 
0124     mutable StoreT store_;
0125 
0126     //// HELPER METHODS ////
0127 
0128     // Build the storage for diagnostic parameters and stream-dependent states
0129     void begin_run_impl(CoreParams const&);
0130 };
0131 
0132 //---------------------------------------------------------------------------//
0133 }  // namespace celeritas