Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-17 08:53:48

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/SlotDiagnostic.hh
0006 //---------------------------------------------------------------------------//
0007 #pragma once
0008 
0009 #include <memory>
0010 #include <string>
0011 
0012 #include "corecel/cont/Span.hh"
0013 #include "corecel/data/AuxInterface.hh"
0014 #include "celeritas/global/ActionInterface.hh"
0015 
0016 namespace celeritas
0017 {
0018 //---------------------------------------------------------------------------//
0019 class AuxStateVec;
0020 class ParticleParams;
0021 
0022 //---------------------------------------------------------------------------//
0023 /*!
0024  * Print diagnostic output about what's in what slots.
0025  *
0026  * Currently this only prints the particle ID as a function of track slot,
0027  * which can later be combined with postprocessing data to print the charge of
0028  * each particle. We could in the future extend the class to use thread ID
0029  * and/or write action ID or any other ID/status instead. Special IDs are:
0030  * - \c -1 : track slot is inactive
0031  * - \c -2 : track has been flagged as an error
0032  *
0033  * A "JSON lines" file (one line per step) is opened for each stream, and a
0034  * separate file is opened once during construction to write appropriate
0035  * metadata.
0036  *
0037  * The filename base is appended with the stream ID or \c metadata. If
0038  * the filename is a directory, that directory must already exist.
0039  * For example, you could pass a filename base of \c
0040  * slot-diag- to get filenames \c slot-diag-metadata.json, \c
0041  * slot-diag-0.jsonl, etc.
0042  *
0043  * \todo Instead of writing separate files, we should probably use a
0044  * multi-stream output manager (not yet implemented) to save the result for the
0045  * end.
0046  *
0047  * \note To plot the resulting files, see \c
0048  * scripts/user/plot-slot-diagnostic.py
0049  */
0050 class SlotDiagnostic final : public CoreStepActionInterface,
0051                              public AuxParamsInterface
0052 {
0053   public:
0054     // Construct and add to core params
0055     static std::shared_ptr<SlotDiagnostic>
0056     make_and_insert(CoreParams const& core, std::string filename_base);
0057 
0058     // Construct with IDs and filename base
0059     SlotDiagnostic(ActionId action_id,
0060                    AuxId aux_id,
0061                    std::string filename_base,
0062                    size_type num_stream,
0063                    std::shared_ptr<ParticleParams const> particle);
0064 
0065     //!@{
0066     //! \name Metadata interface
0067 
0068     //! Label for the auxiliary data and action
0069     std::string_view label() const final { return sad_.label(); }
0070     // Description of the action
0071     std::string_view description() const final { return sad_.description(); }
0072     //!@}
0073 
0074     //!@{
0075     //! \name Step action interface
0076 
0077     //! Index of this class instance in the action registry
0078     ActionId action_id() const final { return sad_.action_id(); }
0079     //! Ordering of the action inside the step
0080     StepActionOrder order() const final { return StepActionOrder::user_post; }
0081     // Execute the action with host data
0082     void step(CoreParams const& params, CoreStateHost& state) const final;
0083     // Execute the action with device data
0084     void step(CoreParams const& params, CoreStateDevice& state) const final;
0085     //!@}
0086 
0087     //!@{
0088     //! \name Aux params interface
0089 
0090     //! Index of this class instance in the aux registry
0091     AuxId aux_id() const final { return aux_id_; }
0092     // Build state data for a stream
0093     UPState create_state(MemSpace m, StreamId id, size_type size) const final;
0094     //!@}
0095 
0096   private:
0097     struct State;
0098 
0099     //// DATA ////
0100 
0101     StaticActionData sad_;
0102     AuxId aux_id_;
0103     std::string filename_base_;
0104 
0105     //// HELPER FUNCTIONS ////
0106 
0107     Span<int> get_host_buffer(AuxStateVec&) const;
0108     void write_buffer(AuxStateVec&) const;
0109 };
0110 
0111 //---------------------------------------------------------------------------//
0112 }  // namespace celeritas