Back to home page

EIC code displayed by LXR

 
 

    


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

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 accel/CylMapMagneticField.hh
0006 //---------------------------------------------------------------------------//
0007 #pragma once
0008 
0009 #include <memory>
0010 #include <CLHEP/Units/SystemOfUnits.h>
0011 #include <G4FieldManager.hh>
0012 #include <G4MagneticField.hh>
0013 #include <G4TransportationManager.hh>
0014 #include <corecel/Assert.hh>
0015 
0016 #include "corecel/Types.hh"
0017 #include "corecel/math/Quantity.hh"
0018 #include "geocel/g4/Convert.hh"
0019 #include "celeritas/Quantities.hh"
0020 #include "celeritas/field/CylMapField.hh"
0021 #include "celeritas/field/CylMapFieldParams.hh"
0022 
0023 namespace celeritas
0024 {
0025 //---------------------------------------------------------------------------//
0026 // Generate field input with user-defined grid
0027 CylMapFieldParams::Input
0028 MakeCylMapFieldInput(std::vector<G4double> const& r_grid,
0029                      std::vector<G4double> const& phi_values,
0030                      std::vector<G4double> const& z_grid);
0031 
0032 //---------------------------------------------------------------------------//
0033 /*!
0034  * A user magnetic field equivalent to celeritas::CylMapField.
0035  */
0036 class CylMapMagneticField : public G4MagneticField
0037 {
0038   public:
0039     //!@{
0040     //! \name Type aliases
0041     using SPConstFieldParams = std::shared_ptr<CylMapFieldParams const>;
0042     //!@}
0043 
0044   public:
0045     // Construct with CylMapFieldParams
0046     inline explicit CylMapMagneticField(SPConstFieldParams field_params);
0047 
0048     // Calculate values of the magnetic field vector
0049     inline void
0050     GetFieldValue(G4double const point[3], G4double* field) const override;
0051 
0052   private:
0053     SPConstFieldParams params_;
0054     CylMapField calc_field_;
0055 };
0056 
0057 //---------------------------------------------------------------------------//
0058 /*!
0059  * Construct with the Celeritas shared CylMapFieldParams.
0060  */
0061 CylMapMagneticField::CylMapMagneticField(SPConstFieldParams params)
0062     : params_(std::move(params))
0063     , calc_field_(CylMapField{params_->ref<MemSpace::native>()})
0064 {
0065     CELER_EXPECT(params_);
0066 }
0067 
0068 //---------------------------------------------------------------------------//
0069 /*!
0070  * Calculate the magnetic field vector at the given position.
0071  */
0072 void CylMapMagneticField::GetFieldValue(G4double const pos[3],
0073                                         G4double* field) const
0074 {
0075     // Calculate the magnetic field value in the native Celeritas unit system
0076     Real3 result = calc_field_(convert_from_geant(pos, clhep_length));
0077     for (auto i = 0; i < 3; ++i)
0078     {
0079         // Return values of the field vector in CLHEP::tesla for Geant4
0080         auto ft = native_value_to<units::FieldTesla>(result[i]);
0081         field[i] = convert_to_geant(ft.value(), CLHEP::tesla);
0082     }
0083 }
0084 
0085 //---------------------------------------------------------------------------//
0086 }  // namespace celeritas