Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-11-07 10:11:32

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/field/RZMapField.covfie.hh
0006 //---------------------------------------------------------------------------//
0007 #pragma once
0008 
0009 #include "corecel/Macros.hh"
0010 #include "corecel/Types.hh"
0011 #include "corecel/cont/Array.hh"
0012 #include "corecel/math/Algorithms.hh"
0013 #include "celeritas/Types.hh"
0014 
0015 #include "RZMapFieldData.covfie.hh"  // IWYU pragma: keep
0016 
0017 #include "detail/CovfieRZFieldTraits.hh"
0018 
0019 namespace celeritas
0020 {
0021 //---------------------------------------------------------------------------//
0022 /*!
0023  * Evaluate the value of magnetic field based on a volume-based RZ field map.
0024  *
0025  * Values outside the grid are returned as zero.
0026  */
0027 class RZMapField
0028 {
0029   public:
0030     //!@{
0031     //! \name Type aliases
0032     using real_type = float;
0033     using Real3 = Array<celeritas::real_type, 3>;
0034     using ParamsRef = NativeCRef<RZMapFieldParamsData>;
0035     //!@}
0036 
0037   public:
0038     // Construct with the shared map data
0039     inline CELER_FUNCTION explicit RZMapField(ParamsRef const& shared);
0040 
0041     // Evaluate the magnetic field value for the given position
0042     CELER_FUNCTION inline Real3 operator()(Real3 const& pos) const;
0043 
0044   private:
0045     using field_view_t =
0046         typename detail::CovfieRZFieldTraits<MemSpace::native>::field_t::view_t;
0047     ParamsRef const& shared_;
0048     field_view_t const& field_;
0049 };
0050 
0051 //---------------------------------------------------------------------------//
0052 // INLINE DEFINITIONS
0053 //---------------------------------------------------------------------------//
0054 /*!
0055  * Construct with the shared magnetic field map data.
0056  */
0057 CELER_FUNCTION RZMapField::RZMapField(ParamsRef const& shared)
0058     : shared_{shared}, field_{get_rz_map_field_view(shared)}
0059 {
0060 }
0061 
0062 //---------------------------------------------------------------------------//
0063 /*!
0064  * Calculate the magnetic field vector for the given position.
0065  *
0066  * This queries the covfie 2D field at (r, z) to obtain (Br, Bz), then
0067  * projects Br onto the x and y components using the position direction.
0068  * The result is in the native Celeritas unit system.
0069  */
0070 CELER_FUNCTION auto RZMapField::operator()(Real3 const& pos) const -> Real3
0071 {
0072     using traits_t = detail::CovfieRZFieldTraits<MemSpace::native>;
0073     celeritas::real_type r = hypot(pos[0], pos[1]);
0074 
0075     // Return zero outside the grid
0076     if (r < shared_.min_r || r > shared_.max_r || pos[2] < shared_.min_z
0077         || pos[2] > shared_.max_z)
0078     {
0079         return {0, 0, 0};
0080     }
0081 
0082     auto bvec = traits_t::to_array(
0083         field_.at(static_cast<real_type>(r), static_cast<real_type>(pos[2])));
0084 
0085     // bvec = {Br, Bz}
0086     Real3 value;
0087     value[2] = bvec[1];
0088 
0089     celeritas::real_type br_over_r = (r != 0) ? bvec[0] / r
0090                                               : celeritas::real_type(0);
0091     value[0] = br_over_r * pos[0];
0092     value[1] = br_over_r * pos[1];
0093 
0094     return value;
0095 }
0096 
0097 //---------------------------------------------------------------------------//
0098 }  // namespace celeritas