| 1 | #cxx . . $ `<file>/ncrystal_core/include/NCrystal/internal/NCKinUtils.hh`/ifndef NCrystal_KinUtils_hh |
| 2 | #define NCrystal_KinUtils_hhcxx . . $ `ncrystal_core/include/NCrystal/internal/NCKinUtils.hh:2:9`! |
| 3 | |
| 4 | //////////////////////////////////////////////////////////////////////////////// |
| 5 | // // |
| 6 | // This file is part of NCrystal (see https://mctools.github.io/ncrystal/) // |
| 7 | // // |
| 8 | // Copyright 2015-2022 NCrystal developers // |
| 9 | // // |
| 10 | // Licensed under the Apache License, Version 2.0 (the "License"); // |
| 11 | // you may not use this file except in compliance with the License. // |
| 12 | // You may obtain a copy of the License at // |
| 13 | // // |
| 14 | // http://www.apache.org/licenses/LICENSE-2.0 // |
| 15 | // // |
| 16 | // Unless required by applicable law or agreed to in writing, software // |
| 17 | // distributed under the License is distributed on an "AS IS" BASIS, // |
| 18 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // |
| 19 | // See the License for the specific language governing permissions and // |
| 20 | // limitations under the License. // |
| 21 | // // |
| 22 | //////////////////////////////////////////////////////////////////////////////// |
| 23 | |
| 24 | #include "NCrystal/NCTypes.hh"cxx . . $ `<file>/ncrystal_core/include/NCrystal/NCTypes.hh`/ |
| 25 | |
| 26 | //Various utilities related to kinematics, including those related to the |
| 27 | //S(alpha,beta) formalism. |
| 28 | |
| 29 | namespace NCrystalcxx . . $ NCrystal/ { |
| 30 | |
| 31 | //Get deltaE and mu=cos(scattering_angle) from (alpha,beta). It is recommended |
| 32 | //to check muIsotropicAtBeta before calling this (for numerical safety, but it |
| 33 | //might also save the effort of sampling alpha). |
| 34 | PairDDcxx . . $ NCrystal/PairDD# convertAlphaBetaToDeltaEMucxx . . $ NCrystal/convertAlphaBetaToDeltaEMu(92de073cb141aacf).(double alpha, double beta, NeutronEnergycxx . . $ NCrystal/NeutronEnergy# ekin, double kT ); |
| 35 | PairDDcxx . . $ NCrystal/PairDD# convertAlphaBetaToDeltaEMucxx . . $ NCrystal/convertAlphaBetaToDeltaEMu(bb915dc568e91ac2).(PairDDcxx . . $ NCrystal/PairDD# alphabeta, NeutronEnergycxx . . $ NCrystal/NeutronEnergy# ekin, double kT ); |
| 36 | |
| 37 | //Get kinematically accessible alpha region for given ekin/kT and beta: |
| 38 | double getAlphaMinuscxx . . $ NCrystal/getAlphaMinus(d8e7e0733fa41c87).( double ekin_div_kT, double beta ); |
| 39 | double getAlphaPluscxx . . $ NCrystal/getAlphaPlus(d8e7e0733fa41c87).( double ekin_div_kT, double beta ); |
| 40 | |
| 41 | //It is faster to get both (alpha-,alpha+) at once (returns (1.0,-1.0) if not |
| 42 | //kinematically accessible, i.e. beta<-E/kT): |
| 43 | PairDDcxx . . $ NCrystal/PairDD# getAlphaLimitscxx . . $ NCrystal/getAlphaLimits(50b1a336490901e9).( double ekin_div_kT, double beta ); |
| 44 | |
| 45 | //Get { alpha-, alpha+, and alpha+ - alpha- } in numerically stable manner: |
| 46 | struct AlphaLimitsWithDiffcxx . . $ NCrystal/AlphaLimitsWithDiff# { double aminuscxx . . $ NCrystal/AlphaLimitsWithDiff#aminus., apluscxx . . $ NCrystal/AlphaLimitsWithDiff#aplus., adiffcxx . . $ NCrystal/AlphaLimitsWithDiff#adiff.; }; |
| 47 | AlphaLimitsWithDiffcxx . . $ NCrystal/AlphaLimitsWithDiff# getAlphaLimitsWithDiffcxx . . $ NCrystal/getAlphaLimitsWithDiff(c27653c8c22d9653).( double ekin_div_kT, double beta ); |
| 48 | |
| 49 | //Get kinematically accessible beta region for given ekin/kT and alpha: |
| 50 | double getBetaMinuscxx . . $ NCrystal/getBetaMinus(d8e7e0733fa41c87).( double ekin_div_kT, double alpha ); |
| 51 | double getBetaPluscxx . . $ NCrystal/getBetaPlus(d8e7e0733fa41c87).( double ekin_div_kT, double alpha ); |
| 52 | |
| 53 | //It is again faster to get both (beta-,beta+) at once: |
| 54 | PairDDcxx . . $ NCrystal/PairDD# getBetaLimitscxx . . $ NCrystal/getBetaLimits(50b1a336490901e9).( double ekin_div_kT, double alpha ); |
| 55 | |
| 56 | //Near kinematic endpoint. alpha-, alpha, and alpha+ might become numerically |
| 57 | //indistinguishable at floating point precision, but mu=cos(scattering_angle) |
| 58 | //should be sampled isotropically in this limit (since |
| 59 | //S(alpha-,beta)~=S(alpha+,beta)). For consistency, always use this function |
| 60 | //to detect this degenerate case: |
| 61 | inline bool muIsotropicAtBetacxx . . $ NCrystal/muIsotropicAtBeta(828b76dce88fac).( double beta, double ekin_div_kT ) |
| 62 | { |
| 63 | nc_assertcxx . . $ `ncrystal_core/include/NCrystal/NCException.hh:153:11`!(beta>=-ekin_div_kT); |
| 64 | nc_assertcxx . . $ `ncrystal_core/include/NCrystal/NCException.hh:153:11`!(ekin_div_kT>=0.0); |
| 65 | constexpr double lim(-1.0+1e-14); |
| 66 | return beta <= ekin_div_kT*lim; |
| 67 | } |
| 68 | |
| 69 | |
| 70 | } |
| 71 | |
| 72 | |
| 73 | //////////////////////////// |
| 74 | // Inline implementations // |
| 75 | //////////////////////////// |
| 76 | |
| 77 | namespace NCrystalcxx . . $ NCrystal/ { |
| 78 | inline PairDDcxx . . $ NCrystal/PairDD# convertAlphaBetaToDeltaEMucxx . . $ NCrystal/convertAlphaBetaToDeltaEMu(bb915dc568e91ac2).( PairDDcxx . . $ NCrystal/PairDD# alphabeta, NeutronEnergycxx . . $ NCrystal/NeutronEnergy# ekin, double kT ) |
| 79 | { |
| 80 | return convertAlphaBetaToDeltaEMucxx . . $ NCrystal/convertAlphaBetaToDeltaEMu(92de073cb141aacf).(alphabeta.first,alphabeta.second,ekin,kT); |
| 81 | } |
| 82 | |
| 83 | namespace detailcxx . . $ NCrystal/detail/ { |
| 84 | inline bool alphaMinusNeedsTaylorcxx . . $ NCrystal/detail/alphaMinusNeedsTaylor(828b76dce88fac).( double ekin_div_kT, double beta ) |
| 85 | { |
| 86 | return stdcxx . . $ std/::abs(beta) < 0.01*ekin_div_kT; |
| 87 | } |
| 88 | inline double alphaMinusTaylorcxx . . $ NCrystal/detail/alphaMinusTaylor(d8e7e0733fa41c87).( double ekin_div_kT, double beta ) { |
| 89 | nc_assertcxx . . $ `ncrystal_core/include/NCrystal/NCException.hh:153:11`!( ekin_div_kT > 0.0 ); |
| 90 | nc_assertcxx . . $ `ncrystal_core/include/NCrystal/NCException.hh:153:11`!( std::abs(beta) < 0.011*ekin_div_kT ); |
| 91 | const double x = beta / ekin_div_kT; |
| 92 | constexpr double c9 = -715./32768.; |
| 93 | constexpr double c8 = 429./16384.; |
| 94 | constexpr double c7 = -33./1024.; |
| 95 | constexpr double c6 = 21./512.; |
| 96 | constexpr double c5 = -7./128.; |
| 97 | constexpr double c4 = 5./64.; |
| 98 | constexpr double c3 = - 1./8.; |
| 99 | constexpr double c2 = 1./4.; |
| 100 | return beta*x*(c2+x*(c3+x*(c4+x*(c5+x*(c6+x*(c7+x*(c8+x*c9))))))); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | //Get kinematically accessible alpha region for given ekin/kT and beta: |
| 105 | inline PairDDcxx . . $ NCrystal/PairDD# getAlphaLimitscxx . . $ NCrystal/getAlphaLimits(50b1a336490901e9).( double ekin_div_kT, double beta ) |
| 106 | { |
| 107 | nc_assertcxx . . $ `ncrystal_core/include/NCrystal/NCException.hh:153:11`!( ekin_div_kT >= 0.0 ); |
| 108 | nc_assertcxx . . $ `ncrystal_core/include/NCrystal/NCException.hh:153:11`!( !std::isnan(beta) ); |
| 109 | const double kk = ekin_div_kT + beta; |
| 110 | if ( !(kk >= 0.0) ) { |
| 111 | //kinematically forbidden |
| 112 | return stdcxx . . $ std/::make_pair(1.0,-1.0); |
| 113 | } |
| 114 | const double a = kk + ekin_div_kT; |
| 115 | const double b = 2.0*stdcxx . . $ std/::sqrt( ekin_div_kT * kk ); |
| 116 | const double aminus = ( detailcxx . . $ NCrystal/detail/::alphaMinusNeedsTaylorcxx . . $ NCrystal/detail/alphaMinusNeedsTaylor(828b76dce88fac).( ekin_div_kT, beta ) |
| 117 | ? detailcxx . . $ NCrystal/detail/::alphaMinusTaylorcxx . . $ NCrystal/detail/alphaMinusTaylor(d8e7e0733fa41c87).( ekin_div_kT, beta ) |
| 118 | : stdcxx . . $ std/::maxcxx . . $ std/max(2a338fce5842aa6a).(0.0,a - b) ); |
| 119 | nc_assertcxx . . $ `ncrystal_core/include/NCrystal/NCException.hh:153:11`!( a+b >= aminus ); |
| 120 | return stdcxx . . $ std/::make_pair( aminus, a + b ); |
| 121 | } |
| 122 | |
| 123 | inline AlphaLimitsWithDiffcxx . . $ NCrystal/AlphaLimitsWithDiff# getAlphaLimitsWithDiffcxx . . $ NCrystal/getAlphaLimitsWithDiff(c27653c8c22d9653).( double ekin_div_kT, double beta ) |
| 124 | { |
| 125 | nc_assertcxx . . $ `ncrystal_core/include/NCrystal/NCException.hh:153:11`!( ekin_div_kT >= 0.0 ); |
| 126 | nc_assertcxx . . $ `ncrystal_core/include/NCrystal/NCException.hh:153:11`!( !std::isnan(beta) ); |
| 127 | nc_assertcxx . . $ `ncrystal_core/include/NCrystal/NCException.hh:153:11`!( beta >= -ekin_div_kT ); |
| 128 | const double kk = ekin_div_kT + beta; |
| 129 | const double a = kk + ekin_div_kT; |
| 130 | const double b = 2.0*stdcxx . . $ std/::sqrt( ekin_div_kT * kk ); |
| 131 | const double aminus = ( detailcxx . . $ NCrystal/detail/::alphaMinusNeedsTaylorcxx . . $ NCrystal/detail/alphaMinusNeedsTaylor(828b76dce88fac).( ekin_div_kT, beta ) |
| 132 | ? detailcxx . . $ NCrystal/detail/::alphaMinusTaylorcxx . . $ NCrystal/detail/alphaMinusTaylor(d8e7e0733fa41c87).( ekin_div_kT, beta ) |
| 133 | : stdcxx . . $ std/::maxcxx . . $ std/max(2a338fce5842aa6a).(0.0,a - b) ); |
| 134 | return { aminus, a+b, 2.0*b }; |
| 135 | } |
| 136 | |
| 137 | inline double getAlphaMinuscxx . . $ NCrystal/getAlphaMinus(d8e7e0733fa41c87).( double ekin_div_kT, double beta ) { |
| 138 | nc_assertcxx . . $ `ncrystal_core/include/NCrystal/NCException.hh:153:11`!(ekin_div_kT >= 0.0); |
| 139 | if ( detailcxx . . $ NCrystal/detail/::alphaMinusNeedsTaylorcxx . . $ NCrystal/detail/alphaMinusNeedsTaylor(828b76dce88fac).( ekin_div_kT, beta ) ) |
| 140 | return detailcxx . . $ NCrystal/detail/::alphaMinusTaylorcxx . . $ NCrystal/detail/alphaMinusTaylor(d8e7e0733fa41c87).( ekin_div_kT, beta ); |
| 141 | nc_assertcxx . . $ `ncrystal_core/include/NCrystal/NCException.hh:153:11`!( ekin_div_kT >= 0.0 ); |
| 142 | nc_assertcxx . . $ `ncrystal_core/include/NCrystal/NCException.hh:153:11`!( !std::isnan(beta) ); |
| 143 | const double kk = ekin_div_kT + beta; |
| 144 | nc_assertcxx . . $ `ncrystal_core/include/NCrystal/NCException.hh:153:11`!( kk >= 0.0 ); |
| 145 | const double a = kk + ekin_div_kT; |
| 146 | const double b = 2.0*stdcxx . . $ std/::sqrt( ekin_div_kT * kk ); |
| 147 | return stdcxx . . $ std/::maxcxx . . $ std/max(2a338fce5842aa6a).(0.0,a - b); |
| 148 | } |
| 149 | |
| 150 | inline double getAlphaPluscxx . . $ NCrystal/getAlphaPlus(d8e7e0733fa41c87).( double ekin_div_kT, double beta ) |
| 151 | { |
| 152 | nc_assertcxx . . $ `ncrystal_core/include/NCrystal/NCException.hh:153:11`!(ekin_div_kT >= 0.0); |
| 153 | nc_assertcxx . . $ `ncrystal_core/include/NCrystal/NCException.hh:153:11`!( ekin_div_kT >= 0.0 ); |
| 154 | nc_assertcxx . . $ `ncrystal_core/include/NCrystal/NCException.hh:153:11`!( !std::isnan(beta) ); |
| 155 | const double kk = ekin_div_kT + beta; |
| 156 | nc_assertcxx . . $ `ncrystal_core/include/NCrystal/NCException.hh:153:11`!( kk >= 0.0 ); |
| 157 | const double a = kk + ekin_div_kT; |
| 158 | const double b = 2.0*stdcxx . . $ std/::sqrt( ekin_div_kT * kk ); |
| 159 | return a + b; |
| 160 | } |
| 161 | |
| 162 | namespace detailcxx . . $ NCrystal/detail/ { |
| 163 | inline bool betaMinusNeedsTaylorcxx . . $ NCrystal/detail/betaMinusNeedsTaylor(828b76dce88fac).( double ekin_div_kT, double alpha ) |
| 164 | { |
| 165 | return stdcxx . . $ std/::abs( alpha - 4.0*ekin_div_kT) < 0.05*ekin_div_kT; |
| 166 | } |
| 167 | |
| 168 | inline double betaMinusTaylorcxx . . $ NCrystal/detail/betaMinusTaylor(d8e7e0733fa41c87).( double ekin_div_kT, double alpha ) { |
| 169 | const double x = alpha / ekin_div_kT - 4.0; |
| 170 | nc_assertcxx . . $ `ncrystal_core/include/NCrystal/NCException.hh:153:11`!( std::abs(x) < 0.051 ); |
| 171 | constexpr double c1 = 0.5; |
| 172 | constexpr double c2 = 1./32.; |
| 173 | constexpr double c3 = -1./256.; |
| 174 | constexpr double c4 = 5./8192.; |
| 175 | constexpr double c5 = -7./65536.; |
| 176 | constexpr double c6 = 21./1048576.; |
| 177 | constexpr double c7 = -33./8388608.; |
| 178 | constexpr double c8 = 429./536870912.; |
| 179 | return ekin_div_kT*(x*(c1+x*(c2+x*(c3+x*(c4+x*(c5+x*(c6+x*(c7+x*c8)))))))); |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | inline double getBetaMinuscxx . . $ NCrystal/getBetaMinus(d8e7e0733fa41c87).( double ekin_div_kT, double alpha ) |
| 184 | { |
| 185 | nc_assertcxx . . $ `ncrystal_core/include/NCrystal/NCException.hh:153:11`!(ekin_div_kT*alpha>=0); |
| 186 | return ( detailcxx . . $ NCrystal/detail/::betaMinusNeedsTaylorcxx . . $ NCrystal/detail/betaMinusNeedsTaylor(828b76dce88fac).( ekin_div_kT, alpha ) |
| 187 | ? detailcxx . . $ NCrystal/detail/::betaMinusTaylorcxx . . $ NCrystal/detail/betaMinusTaylor(d8e7e0733fa41c87).( ekin_div_kT, alpha ) |
| 188 | : ( alpha - 2*stdcxx . . $ std/::sqrt(ekin_div_kT*alpha) ) ); |
| 189 | } |
| 190 | |
| 191 | inline double getBetaPluscxx . . $ NCrystal/getBetaPlus(d8e7e0733fa41c87).( double ekin_div_kT, double alpha ) |
| 192 | { |
| 193 | nc_assertcxx . . $ `ncrystal_core/include/NCrystal/NCException.hh:153:11`!(ekin_div_kT*alpha>=0); |
| 194 | return alpha + 2*stdcxx . . $ std/::sqrt(ekin_div_kT*alpha); |
| 195 | } |
| 196 | |
| 197 | inline PairDDcxx . . $ NCrystal/PairDD# getBetaLimitscxx . . $ NCrystal/getBetaLimits(50b1a336490901e9).( double ekin_div_kT, double alpha ) |
| 198 | { |
| 199 | nc_assertcxx . . $ `ncrystal_core/include/NCrystal/NCException.hh:153:11`!(ekin_div_kT>=0); |
| 200 | nc_assertcxx . . $ `ncrystal_core/include/NCrystal/NCException.hh:153:11`!(alpha>=0); |
| 201 | const double k = 2*stdcxx . . $ std/::sqrt(ekin_div_kT*alpha); |
| 202 | return { ( detailcxx . . $ NCrystal/detail/::betaMinusNeedsTaylorcxx . . $ NCrystal/detail/betaMinusNeedsTaylor(828b76dce88fac).( ekin_div_kT, alpha ) |
| 203 | ? detailcxx . . $ NCrystal/detail/::betaMinusTaylorcxx . . $ NCrystal/detail/betaMinusTaylor(d8e7e0733fa41c87).( ekin_div_kT, alpha ) |
| 204 | : alpha - k ), alpha + k }; |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | #endif |