Back to home page

EIC code displayed by LXR

 
 

    


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

0001 //----------------------------------*-C++-*----------------------------------//
0002 // Copyright 2020-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/field/RZMapField.hh
0007 //---------------------------------------------------------------------------//
0008 #pragma once
0009 
0010 #include <cmath>
0011 
0012 #include "corecel/Macros.hh"
0013 #include "corecel/Types.hh"
0014 #include "corecel/grid/FindInterp.hh"
0015 #include "corecel/grid/UniformGrid.hh"
0016 #include "corecel/math/Algorithms.hh"
0017 #include "celeritas/Types.hh"
0018 #include "celeritas/Units.hh"
0019 
0020 #include "RZMapFieldData.hh"
0021 
0022 namespace celeritas
0023 {
0024 //---------------------------------------------------------------------------//
0025 /*!
0026  * Evaluate the value of magnetic field based on a volume-based RZ field map.
0027  */
0028 class RZMapField
0029 {
0030   public:
0031     //!@{
0032     //! \name Type aliases
0033     using Real3 = Array<real_type, 3>;
0034     using FieldParamsRef = NativeCRef<RZMapFieldParamsData>;
0035     //!@}
0036 
0037   public:
0038     // Construct with the shared map data
0039     inline CELER_FUNCTION explicit RZMapField(FieldParamsRef const& shared);
0040 
0041     // Evaluate the magnetic field value for the given position
0042     CELER_FUNCTION
0043     inline Real3 operator()(Real3 const& pos) const;
0044 
0045   private:
0046     // Shared constant field map
0047     FieldParamsRef const& params_;
0048 
0049     UniformGrid const grid_r_;
0050     UniformGrid const grid_z_;
0051 };
0052 
0053 //---------------------------------------------------------------------------//
0054 // INLINE DEFINITIONS
0055 //---------------------------------------------------------------------------//
0056 /*!
0057  * Construct with the shared magnetic field map data.
0058  */
0059 CELER_FUNCTION
0060 RZMapField::RZMapField(FieldParamsRef const& params)
0061     : params_(params)
0062     , grid_r_(params_.grids.data_r)
0063     , grid_z_(params_.grids.data_z)
0064 {
0065 }
0066 
0067 //---------------------------------------------------------------------------//
0068 /*!
0069  * Calculate the magnetic field vector for the given position.
0070  *
0071  * This does a 2-D interpolation on the input grid and reconstructs the
0072  * magnetic field vector from the stored R and Z components of the field. The
0073  * result is in the native Celeritas unit system.
0074  */
0075 CELER_FUNCTION auto RZMapField::operator()(Real3 const& pos) const -> Real3
0076 {
0077     CELER_ENSURE(params_);
0078 
0079     Real3 value{0, 0, 0};
0080 
0081     real_type r = std::sqrt(ipow<2>(pos[0]) + ipow<2>(pos[1]));
0082 
0083     if (!params_.valid(pos[2], r))
0084         return value;
0085 
0086     // Find interpolation points for given r and z
0087     FindInterp<real_type> interp_r = find_interp<UniformGrid>(grid_r_, r);
0088     FindInterp<real_type> interp_z = find_interp<UniformGrid>(grid_z_, pos[2]);
0089 
0090     size_type ir = interp_r.index;
0091     size_type iz = interp_z.index;
0092 
0093     // z component
0094     real_type low = params_.fieldmap[params_.id(iz, ir)].value_z;
0095     real_type high = params_.fieldmap[params_.id(iz + 1, ir)].value_z;
0096     value[2] = low + (high - low) * interp_z.fraction;
0097 
0098     // x and y components
0099     low = params_.fieldmap[params_.id(iz, ir)].value_r;
0100     high = params_.fieldmap[params_.id(iz, ir + 1)].value_r;
0101     real_type tmp = (r != 0) ? (low + (high - low) * interp_r.fraction) / r
0102                              : low;
0103     value[0] = tmp * pos[0];
0104     value[1] = tmp * pos[1];
0105 
0106     return value;
0107 }
0108 
0109 //---------------------------------------------------------------------------//
0110 }  // namespace celeritas