Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-16 08:57:15

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 geocel/g4/Convert.hh
0006 //---------------------------------------------------------------------------//
0007 #pragma once
0008 
0009 #include <G4ThreeVector.hh>
0010 
0011 #include "corecel/Types.hh"
0012 #include "corecel/cont/Array.hh"
0013 #include "geocel/Types.hh"
0014 #include "geocel/detail/LengthUnits.hh"
0015 
0016 namespace celeritas
0017 {
0018 //---------------------------------------------------------------------------//
0019 // CONSTANTS
0020 //---------------------------------------------------------------------------//
0021 //! Value of a unit Celeritas length in the CLHEP unit system
0022 inline constexpr double clhep_length{1 / lengthunits::millimeter};
0023 
0024 //---------------------------------------------------------------------------//
0025 // FREE FUNCTIONS
0026 //---------------------------------------------------------------------------//
0027 /*!
0028  * Convert a value from Geant4/CLHEP to Celeritas native units.
0029  */
0030 template<class T>
0031 constexpr inline T convert_from_geant(T const& val, T units)
0032 {
0033     return val / units;
0034 }
0035 
0036 //---------------------------------------------------------------------------//
0037 /*!
0038  * Convert a value from Geant4 with CLHEP units.
0039  */
0040 constexpr inline double convert_from_geant(double val, double units)
0041 {
0042     return val / units;
0043 }
0044 
0045 //---------------------------------------------------------------------------//
0046 /*!
0047  * Convert a 3-vector from Geant4/CLHEP to Celeritas native units.
0048  */
0049 inline Real3 convert_from_geant(G4ThreeVector const& vec, double units)
0050 {
0051     return {static_cast<real_type>(vec[0] / units),
0052             static_cast<real_type>(vec[1] / units),
0053             static_cast<real_type>(vec[2] / units)};
0054 }
0055 
0056 //---------------------------------------------------------------------------//
0057 /*!
0058  * Convert a C array from Geant4/CLHEP to Celeritas native units.
0059  */
0060 inline Real3 convert_from_geant(double const vec[3], double units)
0061 {
0062     return {static_cast<real_type>(vec[0] / units),
0063             static_cast<real_type>(vec[1] / units),
0064             static_cast<real_type>(vec[2] / units)};
0065 }
0066 
0067 //---------------------------------------------------------------------------//
0068 /*!
0069  * Convert a native Celeritas quantity to a Geant4 value with CLHEP units.
0070  */
0071 template<class T>
0072 constexpr inline double convert_to_geant(T const& val, double units)
0073 {
0074     return val * units;
0075 }
0076 
0077 //---------------------------------------------------------------------------//
0078 /*!
0079  * Convert a native Celeritas quantity to a Geant4 value.
0080  */
0081 constexpr inline double convert_to_geant(real_type val, double units)
0082 {
0083     return double{val} * units;
0084 }
0085 
0086 //---------------------------------------------------------------------------//
0087 /*!
0088  * Convert a native Celeritas 3-vector to a Geant4 equivalent.
0089  */
0090 template<class T>
0091 inline G4ThreeVector convert_to_geant(Array<T, 3> const& arr, double units)
0092 {
0093     return {arr[0] * units, arr[1] * units, arr[2] * units};
0094 }
0095 
0096 //! \cond (CELERITAS_DOC_DEV)
0097 //---------------------------------------------------------------------------//
0098 /*!
0099  * Set y += a * x .
0100  */
0101 inline void axpy(double a, G4ThreeVector const& x, G4ThreeVector* y)
0102 {
0103     CELER_EXPECT(y);
0104     for (int i = 0; i < 3; ++i)
0105     {
0106         (*y)[i] = a * x[i] + (*y)[i];
0107     }
0108 }
0109 //! \endcond
0110 
0111 //---------------------------------------------------------------------------//
0112 }  // namespace celeritas