Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:54:49

0001 //----------------------------------*-C++-*----------------------------------//
0002 // Copyright 2023-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 corecel/math/Turn.hh
0007 //---------------------------------------------------------------------------//
0008 #pragma once
0009 
0010 #include <cmath>
0011 
0012 #include "corecel/Types.hh"
0013 
0014 #include "Algorithms.hh"
0015 #include "Quantity.hh"
0016 
0017 namespace celeritas
0018 {
0019 //---------------------------------------------------------------------------//
0020 //! Unit for 2*pi radians
0021 struct TwoPi
0022 {
0023     static real_type value() { return 2 * static_cast<real_type>(m_pi); }
0024     //! Text label for output
0025     static char const* label() { return "tr"; }
0026 };
0027 
0028 //---------------------------------------------------------------------------//
0029 //! Unit for pi/2 radians
0030 struct HalfPi
0031 {
0032     static real_type value() { return static_cast<real_type>(m_pi / 2); }
0033     //! Text label for output
0034     static char const* label() { return "qtr"; }
0035 };
0036 
0037 //---------------------------------------------------------------------------//
0038 /*!
0039  * Quantity denoting a full turn.
0040  *
0041  * Turns are a useful way of representing angles without the historical
0042  * arbitrariness of degrees or the roundoff errors of radians. See, for
0043  * example, https://www.computerenhance.com/p/turns-are-better-than-radians .
0044  */
0045 using Turn = Quantity<TwoPi, real_type>;
0046 
0047 //---------------------------------------------------------------------------//
0048 //! Quantity for an integer number of turns for axis swapping
0049 using QuarterTurn = Quantity<HalfPi, int>;
0050 
0051 //---------------------------------------------------------------------------//
0052 //!@{
0053 //! Special overrides for math functions for more precise arithmetic
0054 CELER_FORCEINLINE_FUNCTION real_type sin(Turn r)
0055 {
0056     return sinpi(r.value() * 2);
0057 }
0058 
0059 CELER_FORCEINLINE_FUNCTION real_type cos(Turn r)
0060 {
0061     return cospi(r.value() * 2);
0062 }
0063 
0064 CELER_FORCEINLINE_FUNCTION void sincos(Turn r, real_type* sinv, real_type* cosv)
0065 {
0066     return sincospi(r.value() * 2, sinv, cosv);
0067 }
0068 
0069 CELER_FORCEINLINE_FUNCTION int cos(QuarterTurn r)
0070 {
0071     constexpr int cosval[] = {1, 0, -1, 0};
0072     return cosval[std::abs(r.value()) % 4];
0073 }
0074 
0075 CELER_FORCEINLINE_FUNCTION int sin(QuarterTurn r)
0076 {
0077     // Define in terms of the symmetric "cos"
0078     return cos(QuarterTurn{r.value() - 1});
0079 }
0080 
0081 CELER_FORCEINLINE_FUNCTION void sincos(QuarterTurn r, int* sinv, int* cosv)
0082 {
0083     *sinv = sin(r);
0084     *cosv = cos(r);
0085 }
0086 //!@}
0087 
0088 //---------------------------------------------------------------------------//
0089 }  // namespace celeritas