File indexing completed on 2026-11-07 10:11:32
0001
0002
0003
0004
0005
0006
0007 #pragma once
0008
0009 #include <memory>
0010 #include <G4MagneticField.hh>
0011
0012 #include "corecel/Assert.hh"
0013 #include "corecel/Types.hh"
0014 #include "corecel/cont/Range.hh"
0015 #include "corecel/cont/Span.hh"
0016 #include "geocel/g4/Convert.hh"
0017 #include "celeritas/Quantities.hh"
0018
0019 namespace celeritas
0020 {
0021
0022
0023
0024
0025
0026
0027
0028 template<class P, class F>
0029 class MagneticField : public G4MagneticField
0030 {
0031 public:
0032
0033
0034 using SPConstParams = std::shared_ptr<P const>;
0035
0036
0037 public:
0038
0039 inline explicit MagneticField(SPConstParams params);
0040
0041
0042 inline void GetFieldValue(G4double const xyzt[4],
0043 G4double* field) const override;
0044
0045 private:
0046 SPConstParams params_;
0047 };
0048
0049
0050
0051
0052
0053
0054
0055 template<class P, class F>
0056 MagneticField<P, F>::MagneticField(SPConstParams params)
0057 : params_(std::move(params))
0058 {
0059 CELER_EXPECT(params_);
0060 }
0061
0062
0063
0064
0065
0066 template<class P, class F>
0067 void MagneticField<P, F>::GetFieldValue(G4double const xyzt[4],
0068 G4double* field) const
0069 {
0070 F calc_field{params_->host_ref()};
0071
0072
0073 Span<G4double const, 3> pos(xyzt, 3);
0074
0075
0076 Real3 field_native = calc_field(
0077 native_from_geant<units::ClhepLength, real_type>(to_array(pos)));
0078 for (auto i : range(3))
0079 {
0080
0081 field[i] = native_to_geant<units::ClhepField>(field_native[i]);
0082 }
0083 }
0084
0085
0086 }