Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-18 09:09:16

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