| 1 | ////////////////////////////////////////////////////////////////////////////////cxx . . $ `<file>/ncrystal_core/src/NCVDOSToScatKnl.cc`/ |
| 2 | // // |
| 3 | // This file is part of NCrystal (see https://mctools.github.io/ncrystal/) // |
| 4 | // // |
| 5 | // Copyright 2015-2022 NCrystal developers // |
| 6 | // // |
| 7 | // Licensed under the Apache License, Version 2.0 (the "License"); // |
| 8 | // you may not use this file except in compliance with the License. // |
| 9 | // You may obtain a copy of the License at // |
| 10 | // // |
| 11 | // http://www.apache.org/licenses/LICENSE-2.0 // |
| 12 | // // |
| 13 | // Unless required by applicable law or agreed to in writing, software // |
| 14 | // distributed under the License is distributed on an "AS IS" BASIS, // |
| 15 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // |
| 16 | // See the License for the specific language governing permissions and // |
| 17 | // limitations under the License. // |
| 18 | // // |
| 19 | //////////////////////////////////////////////////////////////////////////////// |
| 20 | |
| 21 | #include "NCrystal/internal/NCVDOSToScatKnl.hh"cxx . . $ `<file>/ncrystal_core/include/NCrystal/internal/NCVDOSToScatKnl.hh`/ |
| 22 | #include "NCrystal/internal/NCVDOSEval.hh"cxx . . $ `<file>/ncrystal_core/include/NCrystal/internal/NCVDOSEval.hh`/ |
| 23 | #include "NCrystal/internal/NCVDOSGn.hh"cxx . . $ `<file>/ncrystal_core/include/NCrystal/internal/NCVDOSGn.hh`/ |
| 24 | #include "NCrystal/internal/NCKinUtils.hh"cxx . . $ `<file>/ncrystal_core/include/NCrystal/internal/NCKinUtils.hh`/ |
| 25 | #include "NCrystal/internal/NCString.hh"cxx . . $ `<file>/ncrystal_core/include/NCrystal/internal/NCString.hh`/ |
| 26 | #include "NCrystal/internal/NCIter.hh"cxx . . $ `<file>/ncrystal_core/include/NCrystal/internal/NCIter.hh`/ |
| 27 | #include "NCrystal/internal/NCSABUtils.hh"cxx . . $ `<file>/ncrystal_core/include/NCrystal/internal/NCSABUtils.hh`/ |
| 28 | namespace NC=NCrystal; |
| 29 | #include <iostream> |
| 30 | |
| 31 | namespace NCrystalcxx . . $ NCrystal/ { |
| 32 | |
| 33 | namespace V2SKDetailcxx . . $ NCrystal/V2SKDetail/ { |
| 34 | static bool s_verbosecxx . . $ NCrystal/V2SKDetail/s_verbose. = getenv("NCRYSTAL_DEBUG_PHONON"); |
| 35 | inline double stirlingsSeriesSum9thOrdercxx . . $ NCrystal/V2SKDetail/stirlingsSeriesSum9thOrder(447c67c6676f75c3).(double inv_n) |
| 36 | { |
| 37 | //Calculate and return the sum_{k=0}^9{ gk/x^k }, needed to estimate |
| 38 | //Stirling's series (see eq. 5.11.3 and 5.11.4 in |
| 39 | //https://dlmf.nist.gov/5.11): Thus, pass in inv_n = 1/n and get back |
| 40 | //1+1/(12n)+1/(288n^2)+... (up to terms with 1/n^9) from Stirling's |
| 41 | //series, The result must be multiplied by sqrt(2pi*n)*(n/e)^n to provide |
| 42 | //an estimate of the faculty of n ("n!"). Factors ci are taken from: |
| 43 | //https://oeis.org/A001163 and https://oeis.org/A001164 |
| 44 | // |
| 45 | //For arguments n>=9 this can be used to evaluate n! to a relative |
| 46 | //precision of O(1e-13) or better. |
| 47 | constexpr double c1 = 1./12.; |
| 48 | constexpr double c2 = 1./288.; |
| 49 | constexpr double c3 = -139/51840.; |
| 50 | constexpr double c4 = -571./2488320.; |
| 51 | constexpr double c5 = 163879./209018880.; |
| 52 | constexpr double c6 = 5246819./75246796800.; |
| 53 | constexpr double c7 = -534703531./902961561600.; |
| 54 | constexpr double c8 = -4483131259./86684309913600.; |
| 55 | constexpr double c9 = 432261921612371./514904800886784000.; |
| 56 | return 1.0 + inv_n*(c1+inv_n*(c2+inv_n*(c3+inv_n*(c4+inv_n*(c5+inv_n*(c6+inv_n*(c7+inv_n*(c8+inv_n*c9)))))))); |
| 57 | } |
| 58 | |
| 59 | constexpr double kTmsd_to_alpha2xcxx . . $ NCrystal/V2SKDetail/kTmsd_to_alpha2x(d8e7e0733fa41c87).(double kT,double msd) |
| 60 | { |
| 61 | //alpha2x is factor required to convert alpha value to x (aka "2W" in Sjolanders paper). |
| 62 | #if __cpluspluscxx . . $ `<built-in>:470:9`! >= 201402L |
| 63 | constexpr double fact = ( 2.0*const_neutron_mass_evc2cxx . . $ NCrystal/const_neutron_mass_evc2./(constant_hbarcxx . . $ NCrystal/constant_hbar.*constant_hbarcxx . . $ NCrystal/constant_hbar.) ); |
| 64 | return fact*kT*msd; |
| 65 | #else |
| 66 | //In c++11, constexpr functions can only consist of a single return statement. |
| 67 | return (( 2.0*const_neutron_mass_evc2/(constant_hbar*constant_hbar) ))*kT*msd; |
| 68 | #endif |
| 69 | } |
| 70 | |
| 71 | VectDcxx . . $ NCrystal/VectD# fillSABFromVDOScxx . . $ NCrystal/V2SKDetail/fillSABFromVDOS(a55f273e0d1da178).( const VDOSGncxx . . $ NCrystal/VDOSGn#& Gn_asym, |
| 72 | const double msd, |
| 73 | const VectDcxx . . $ NCrystal/VectD#& alphaGrid, |
| 74 | const VectDcxx . . $ NCrystal/VectD#& betaGrid, |
| 75 | ScaleGnContributionFctcxx . . $ NCrystal/ScaleGnContributionFct# scaleGnContribFct ) |
| 76 | { |
| 77 | // Evaluate S(alpha,beta) from Sjolander's II.28, recasted to alpha/beta |
| 78 | // and excluding sigma*kT/4E from the definition of S. |
| 79 | |
| 80 | const auto nalpha = alphaGrid.sizecxx . . $ std/vector#size(c9b1e749b64d5edc).(); |
| 81 | VectDcxx . . $ NCrystal/VectD# sabcxx . . $ std/vector#vector(229e4d6bd205de31).( nalpha * betaGrid.sizecxx . . $ std/vector#size(c9b1e749b64d5edc).(), 0.0); |
| 82 | const unsigned maxOrder = Gn_asym.maxOrdercxx . . $ NCrystal/VDOSGn#maxOrder(422dc87a5e0c7f13).().valuecxx . . $ NCrystal/VDOSGn#Order#value(ddd8539a2fcdd4ce).(); |
| 83 | const double kT = Gn_asym.kTcxx . . $ NCrystal/VDOSGn#kT(21b6c25e127938e2).(); |
| 84 | |
| 85 | //Alpha-dependency is contained in f(x,n)=exp(-x)*x^n/n! where |
| 86 | //x=alpha2x*alpha. For n<stirling_threshold, f(x,n) are calculated |
| 87 | //directly and recursively, using f(x,n)=f(x,n-1)*(x/n). For higher n, |
| 88 | //this becomes numerically unstable and we instead use Stirling's series |
| 89 | //to rewrite the factor of "n!" and evaluate f(x,n) directly. NB, |
| 90 | //stirling_threshold should be left at 250: it can't be raised much higher |
| 91 | //since the direct method is not valid much beyond n=250, and if it is |
| 92 | //decreased too far there will be precision (and speed) issues. |
| 93 | constexpr bool stirling_threshold = 250; |
| 94 | const double alpha2x = V2SKDetailcxx . . $ NCrystal/V2SKDetail/::kTmsd_to_alpha2xcxx . . $ NCrystal/V2SKDetail/kTmsd_to_alpha2x(d8e7e0733fa41c87).(kT,msd); |
| 95 | auto x_vals = vectorTrfcxx . . $ NCrystal/vectorTrf(452c95d32bb88636).(alphaGrid,[alpha2x](double alpha){ return alpha*alpha2x; } ); |
| 96 | auto expmhalfx_vals = vectorTrfcxx . . $ NCrystal/vectorTrf(452c95d32bb88636).(x_vals,[](double x){ return stdcxx . . $ std/::exp(-0.5*x); } ); |
| 97 | VectDcxx . . $ NCrystal/VectD# logx_vals = ( maxOrder >= stirling_threshold ? vectorTrfcxx . . $ NCrystal/vectorTrf(452c95d32bb88636).(x_vals,[](double x){ return stdcxx . . $ std/::log(x); } ) : VectDcxx . . $ std/vector#vector(49f6e7a06ebc5aa8).() ); |
| 98 | auto fxn_cache = expmhalfx_valscxx . . $ std/vector#vector(a1201200cf0efa42).;//We apply half of the exp(-x) factor before |
| 99 | //the x^n/n! factor is added, and the other |
| 100 | //half of exp(-x) we add later. This is done |
| 101 | //to extend range of valid x-values. |
| 102 | VectDcxx . . $ NCrystal/VectD# alpha_factorscxx . . $ std/vector#vector(49f6e7a06ebc5aa8).; |
| 103 | alpha_factors.resizecxx . . $ std/vector#resize(2c44cd906ae4e829).(x_vals.sizecxx . . $ std/vector#size(c9b1e749b64d5edc).()); |
| 104 | |
| 105 | //For reasons of numerical stability and efficiency, we always evaluate S |
| 106 | //first for negative beta, and obtain the values at positive beta by the |
| 107 | //identity ("detailed balance"): S(alpha,+beta)=S(alpha,-beta)*exp(-beta). |
| 108 | // |
| 109 | //Find range of betaGrid which should be flipped: |
| 110 | auto itbeta_zero = findClosestValInSortedVectorcxx . . $ NCrystal/findClosestValInSortedVector(b21f9c12279fa3e3).(betaGrid, 0.0); |
| 111 | nc_assertcxx . . $ `ncrystal_core/include/NCrystal/NCException.hh:153:11`!( *itbeta_zero == 0.0 ); |
| 112 | auto itbeta_firstflip = findClosestValInSortedVectorcxx . . $ NCrystal/findClosestValInSortedVector(b21f9c12279fa3e3).(betaGrid, -betaGrid.backcxx . . $ std/vector#back(af0004c3ce7cab93).()); |
| 113 | nc_assertcxx . . $ `ncrystal_core/include/NCrystal/NCException.hh:153:11`!( *itbeta_firstflip == -betaGrid.back() ); |
| 114 | const stdcxx . . $ std/::size_tcxx . . $ std/size_t# idx_firstflip = stdcxx . . $ std/::distance(betaGrid.cbegincxx . . $ std/vector#cbegin(d1e1699016b9d7e6).(),itbeta_firstflip); |
| 115 | const stdcxx . . $ std/::size_tcxx . . $ std/size_t# idx_zero = stdcxx . . $ std/::distance(betaGrid.cbegincxx . . $ std/vector#cbegin(d1e1699016b9d7e6).(),itbeta_zero); |
| 116 | nc_assertcxx . . $ `ncrystal_core/include/NCrystal/NCException.hh:153:11`!( idx_zero+1 <= betaGrid.size() ); |
| 117 | const VectDcxx . . $ NCrystal/VectD# betaGridNonPositivecxx . . $ std/vector#vector(13e0640cd3842f28).( betaGrid.begincxx . . $ std/vector#begin(d1e1699016b9d7e6).(), stdcxx . . $ std/::next(betaGrid.begincxx . . $ std/vector#begin(d1e1699016b9d7e6).(),idx_zero+1) );//vector instead of span to simplify code below |
| 118 | auto expbeta_vals_nonposbeta = vectorTrfcxx . . $ NCrystal/vectorTrf(452c95d32bb88636).( betaGridNonPositive, [](double beta){ return stdcxx . . $ std/::exp(beta); } ); |
| 119 | |
| 120 | //Now we loop and fill the S-table. First over phonon order, n, next over |
| 121 | //beta and finally alpha. We take care to keep as many calculations as |
| 122 | //possible in the outer loops, while avoiding unnecessarily repeating |
| 123 | //calculations or utilising enormous memory caches. |
| 124 | |
| 125 | for ( unsigned n = 1; n <= maxOrder; ++n) { |
| 126 | |
| 127 | const double contribScaleFactor = scaleGnContribFct ? scaleGnContribFct(cxx . . $ std/function#`operator()`(39e0826ffb540481).n) : 1.0; |
| 128 | nc_assertcxx . . $ `ncrystal_core/include/NCrystal/NCException.hh:153:11`!(contribScaleFactor>=0.0); |
| 129 | |
| 130 | //Prepare for Gn(beta)-evaluations: |
| 131 | auto betakT_Range = Gn_asym.eRangecxx . . $ NCrystal/VDOSGn#eRange(92d16a05c63f5098).(ncxx . . $ NCrystal/VDOSGn#Order#Order(cb2f890c2fdad230).); |
| 132 | const double invn = 1.0/n; |
| 133 | |
| 134 | //Preparation of f(x)=exp(-x)*x^n/n! is more tricky, due to reasons of |
| 135 | //efficiency and numerical issues. As explained above, for orders below |
| 136 | //stirling_threshold, we build up recursively, and above we use |
| 137 | //Stirling's formula: |
| 138 | if ( n < stirling_threshold ) { |
| 139 | //for reasonably low orders we can build up slowly and cheaply (leaving |
| 140 | //out of fxn_cache a final factor of expmhalfx for numerical stability): |
| 141 | for (auto x : enumeratecxx . . $ NCrystal/enumerate(cc3b7692454c18fc).(x_vals) ) |
| 142 | vectAtcxx . . $ NCrystal/vectAt(bc217a45c22e0a7b).(fxn_cache,x.idxcxx . . $ NCrystal/detail/EnumIter#Entry#idx.) *= x.valcxx . . $ NCrystal/detail/EnumIter#Entry#val.*invn; |
| 143 | for (auto fxn : enumeratecxx . . $ NCrystal/enumerate(cc3b7692454c18fc).(fxn_cache) ) |
| 144 | vectAtcxx . . $ NCrystal/vectAt(bc217a45c22e0a7b).(alpha_factors,fxn.idxcxx . . $ NCrystal/detail/EnumIter#Entry#idx.) = fxn.valcxx . . $ NCrystal/detail/EnumIter#Entry#val. * vectAtcxx . . $ NCrystal/vectAt(bc217a45c22e0a7b).(expmhalfx_vals,fxn.idxcxx . . $ NCrystal/detail/EnumIter#Entry#idx.) * kT; |
| 145 | } else { |
| 146 | //Very high order phonons, f(x) is non-zero at very high values of x, but |
| 147 | //exp(-0.5*x) becomes 0, precluding the direct/cheap evaluation. Instead |
| 148 | //we evaluate directly, with the help of Stirling's series for the |
| 149 | //factorial, n!. Everything is suitably rearranged so cancellations happen |
| 150 | //before the exponential is evaluated. |
| 151 | const double gn = V2SKDetailcxx . . $ NCrystal/V2SKDetail/::stirlingsSeriesSum9thOrdercxx . . $ NCrystal/V2SKDetail/stirlingsSeriesSum9thOrder(447c67c6676f75c3).(invn); |
| 152 | const double fact= kT * kInvSqrt2Picxx . . $ NCrystal/kInvSqrt2Pi./(stdcxx . . $ std/::sqrt(n)*gn); |
| 153 | if (!fact) |
| 154 | continue;//nothing can contribute at this order (should not really happen?) |
| 155 | const double logn = stdcxx . . $ std/::log(n); |
| 156 | for (auto x : enumeratecxx . . $ NCrystal/enumerate(cc3b7692454c18fc).(x_vals) ) { |
| 157 | const double exparg = n * ( vectAtcxx . . $ NCrystal/vectAt(bc217a45c22e0a7b).(logx_vals,x.idxcxx . . $ NCrystal/detail/EnumIter#Entry#idx.) - logn + 1.0 ) - x.valcxx . . $ NCrystal/detail/EnumIter#Entry#val.; |
| 158 | vectAtcxx . . $ NCrystal/vectAt(bc217a45c22e0a7b).(alpha_factors,x.idxcxx . . $ NCrystal/detail/EnumIter#Entry#idx.) = fact * stdcxx . . $ std/::exp(exparg); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | for ( auto beta : enumeratecxx . . $ NCrystal/enumerate(cc3b7692454c18fc).(betaGridNonPositive) ) { |
| 163 | //Can evaluate more precisely at negative beta values and simply flip + |
| 164 | //apply detailed balance factor for positive. |
| 165 | nc_assertcxx . . $ `ncrystal_core/include/NCrystal/NCException.hh:153:11`!( beta.val<=0.0 ); |
| 166 | double energy = beta.valcxx . . $ NCrystal/detail/EnumIter#Entry#val. * kT; |
| 167 | if (!valueInIntervalcxx . . $ NCrystal/valueInInterval(8592303b96deed7f).(betakT_Range,energy)) |
| 168 | continue;//Gn(beta) zero here. |
| 169 | |
| 170 | double expMbeta(0.0); |
| 171 | stdcxx . . $ std/::size_tcxx . . $ std/size_t# posbeta_idx(0); |
| 172 | if ( beta.idxcxx . . $ NCrystal/detail/EnumIter#Entry#idx. >= idx_firstflip ) { |
| 173 | posbeta_idx = idx_zero + ( idx_zero - beta.idxcxx . . $ NCrystal/detail/EnumIter#Entry#idx. ); |
| 174 | expMbeta = vectAtcxx . . $ NCrystal/vectAt(bc217a45c22e0a7b).(expbeta_vals_nonposbeta,beta.idxcxx . . $ NCrystal/detail/EnumIter#Entry#idx.); |
| 175 | } |
| 176 | |
| 177 | const double Gn_asym_eval = contribScaleFactor * Gn_asym.evalcxx . . $ NCrystal/VDOSGn#eval(7be0da03ec6eef0e).(ncxx . . $ NCrystal/VDOSGn#Order#Order(cb2f890c2fdad230).,energy); |
| 178 | if ( !(Gn_asym_eval>0.0) ) |
| 179 | continue; |
| 180 | #if 0 |
| 181 | //readable version of innermost loop: |
| 182 | const auto offset_alpharow = beta.idx*nalpha; |
| 183 | const auto offset_alpharow_posbeta = posbeta_idx*nalpha; |
| 184 | for ( auto alpha_fact : enumerate(alpha_factors) ) { |
| 185 | if ( !(alpha_fact.val>0.0) ) |
| 186 | continue; |
| 187 | double contrib_S_negbeta = alpha_fact.val * Gn_asym_eval; |
| 188 | |
| 189 | //Negative beta: |
| 190 | vectAt( sab, offset_alpharow + alpha_fact.idx ) += contrib_S_negbeta; |
| 191 | if ( expMbeta ) { |
| 192 | //Expand to beta>0 by first copying S-values, |
| 193 | //S(alpha,+beta)=S(alpha,-beta) and then applying the detailed |
| 194 | //balance factor, exp(-beta/2) twice to the values at beta>0, thus |
| 195 | //ensuring in the end that all entries end up with a correct factor: |
| 196 | vectAt( sab, offset_alpharow_posbeta + alpha_fact.idx ) += contrib_S_negbeta * expMbeta; |
| 197 | } |
| 198 | }//alpha loop |
| 199 | #else |
| 200 | //Attempt to super-streamline this innermost loop: |
| 201 | nc_assertcxx . . $ `ncrystal_core/include/NCrystal/NCException.hh:153:11`!(nalpha==alpha_factors.size()); |
| 202 | double * itAlphaFactB = &alpha_factors[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).0]; |
| 203 | double * itAlphaFact = itAlphaFactB; |
| 204 | double * itAlphaFactE = itAlphaFactB + nalpha; |
| 205 | //Alpha-factors increase and then decrease. Thus, if we first skip |
| 206 | //over any initial zeros in the alpha factors, we can break (rather |
| 207 | //than just continue) in the final loop whenever we see a zero. |
| 208 | |
| 209 | //First skip over any initial zeros in alpha-factors: |
| 210 | while( !(*itAlphaFact>0.0) && itAlphaFact!=itAlphaFactE ) |
| 211 | ++itAlphaFact; |
| 212 | double * itSAB = &sab[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).0] + (beta.idxcxx . . $ NCrystal/detail/EnumIter#Entry#idx.*nalpha + (itAlphaFact-itAlphaFactB)); |
| 213 | if ( expMbeta) { |
| 214 | double * itSAB_posbeta = &sab[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).0] + (posbeta_idx*nalpha + (itAlphaFact-itAlphaFactB)); |
| 215 | for (;itAlphaFact!=itAlphaFactE;++itAlphaFact,++itSAB,++itSAB_posbeta) { |
| 216 | if ( !(*itAlphaFact>0.0) ) |
| 217 | break; |
| 218 | double contrib_S_negbeta = *itAlphaFact * Gn_asym_eval; |
| 219 | *itSAB += contrib_S_negbeta; |
| 220 | *itSAB_posbeta += contrib_S_negbeta*expMbeta; |
| 221 | }//alpha loop where +-|beta| is available |
| 222 | } else { |
| 223 | for (;itAlphaFact!=itAlphaFactE;++itAlphaFact,++itSAB) { |
| 224 | if ( !(*itAlphaFact>0.0) ) |
| 225 | break; |
| 226 | *itSAB += *itAlphaFact * Gn_asym_eval; |
| 227 | }//alpha loop where only -|beta| is available |
| 228 | } |
| 229 | #endif |
| 230 | }//beta loop |
| 231 | }//phonon order loop |
| 232 | |
| 233 | return sabcxx . . $ std/vector#vector(6f559d22b039ccdb).; |
| 234 | } |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | //Interval where f(x) = x^n*exp(-x) is above eps*fpeak: |
| 239 | NC::PairDDcxx . . $ NCrystal/PairDD# NC::rangeXNexpMXcxx . . $ NCrystal/rangeXNexpMX(e3f0dbde301bb72f).(unsigned n, double eps, double accuracy ) { |
| 240 | nc_assertcxx . . $ `ncrystal_core/include/NCrystal/NCException.hh:153:11`!(eps>0.0&&eps<1.0&&eps>1e-200&&n>0&&accuracy>0&&accuracy<=1e-2); |
| 241 | //The function f(x) = x^n*exp(-x) peaks at x=n and falls off on both |
| 242 | //sides. Returns the two solutions to f(x)= f(n)*eps, describing the central |
| 243 | //range around x=n where the function is higher than eps times the peak |
| 244 | //value. |
| 245 | |
| 246 | //Must solve for x: |
| 247 | // x^n*exp(-x) = eps*[n^n*exp(-n)] |
| 248 | //Raise to 1/n power and get: |
| 249 | // x*exp(-x/n) = eps^(1/n)*n*exp(-1) |
| 250 | //<=> (x/n)*exp(-x/n) = (1/e)*eps^(1/n) = k |
| 251 | // |
| 252 | //Which can be solved numerically for x/n: |
| 253 | const double fn = static_cast<double>(n); |
| 254 | const double k = kInvEcxx . . $ NCrystal/kInvE. * stdcxx . . $ std/::pow(eps,1.0/fn); |
| 255 | auto f = [k](double y) { return y*stdcxx . . $ std/::exp(-y)-k; }; |
| 256 | return { fn*findRoot2cxx . . $ NCrystal/findRoot2(62e4799cbe2a8fbb).( f, 0.0, 1.0, accuracy ), |
| 257 | fn*findRoot2cxx . . $ NCrystal/findRoot2(62e4799cbe2a8fbb).( f, 1.0, 700.0, accuracy ) }; |
| 258 | } |
| 259 | |
| 260 | NC::PairDDcxx . . $ NCrystal/PairDD# NC::findExtremeSABPointWithinAlphaPlusCurvecxx . . $ NCrystal/findExtremeSABPointWithinAlphaPlusCurve(b39fe339a200bd0e).(double E_div_kT, PairDDcxx . . $ NCrystal/PairDD# alphaRange, PairDDcxx . . $ NCrystal/PairDD# betaRange) |
| 261 | { |
| 262 | //Find the extreme (as in highest alpha, lowest beta) kinematically |
| 263 | //accessible point in the provided rectangular region in (alpha,beta) space |
| 264 | //for a neutron with energy/kT= Emax_div_kT. Returns {-1,-1} in case no point |
| 265 | //is accessible. Note that we on purpose consider only the kinematic edge |
| 266 | //given by the alpha+(beta) and beta=-E/kT curves, ignoring the alpha-(beta) |
| 267 | //curve. |
| 268 | nc_assertcxx . . $ `ncrystal_core/include/NCrystal/NCException.hh:153:11`!( alphaRange.second > alphaRange.first ); |
| 269 | nc_assertcxx . . $ `ncrystal_core/include/NCrystal/NCException.hh:153:11`!( alphaRange.first >= 0.0 ); |
| 270 | nc_assertcxx . . $ `ncrystal_core/include/NCrystal/NCException.hh:153:11`!( betaRange.second > betaRange.first ); |
| 271 | nc_assertcxx . . $ `ncrystal_core/include/NCrystal/NCException.hh:153:11`!( E_div_kT > 0.0 ); |
| 272 | |
| 273 | NCRYSTAL_DEBUGONLYcxx . . $ `ncrystal_core/include/NCrystal/NCDefs.hh:610:11`!(const bool should_be_accessible = sabPointWithinAlphaPlusCurve(E_div_kT,alphaRange.first,betaRange.second)); |
| 274 | |
| 275 | if ( betaRange.second <= -E_div_kT ) { |
| 276 | nc_assertcxx . . $ `ncrystal_core/include/NCrystal/NCException.hh:153:11`!(!should_be_accessible); |
| 277 | return { -1.0, -1.0 };//no accessible points in region |
| 278 | } |
| 279 | |
| 280 | auto alphaPlus = [E_div_kT](double beta) |
| 281 | { |
| 282 | nc_assertcxx . . $ `ncrystal_core/include/NCrystal/NCException.hh:153:11`!( beta >= -E_div_kT ); |
| 283 | return 2*E_div_kT + beta + 2 * stdcxx . . $ std/::sqrt( E_div_kT * ( E_div_kT + beta ) ); |
| 284 | }; |
| 285 | const double apb1 = alphaPlus(cxx . . $ NCrystal/findExtremeSABPointWithinAlphaPlusCurve(b39fe339a200bd0e).$anonymous_type_0#`operator()`(7e3a3c6b59947c6d).betaRange.second); |
| 286 | if ( apb1 <= alphaRange.first ) { |
| 287 | nc_assertcxx . . $ `ncrystal_core/include/NCrystal/NCException.hh:153:11`!(!should_be_accessible); |
| 288 | return { -1.0, -1.0 };//no accessible points in region |
| 289 | } |
| 290 | |
| 291 | nc_assertcxx . . $ `ncrystal_core/include/NCrystal/NCException.hh:153:11`!(should_be_accessible); |
| 292 | |
| 293 | //Clip lower beta range at -E/kT: |
| 294 | betaRange.first = ncmaxcxx . . $ NCrystal/ncmax(d8e7e0733fa41c87).( betaRange.first, -E_div_kT ); |
| 295 | |
| 296 | const double apb0 = alphaPlus(cxx . . $ NCrystal/findExtremeSABPointWithinAlphaPlusCurve(b39fe339a200bd0e).$anonymous_type_0#`operator()`(7e3a3c6b59947c6d).betaRange.first); |
| 297 | if ( apb0 >= alphaRange.second ) |
| 298 | return { alphaRange.second, betaRange.first };//entire rectangle is accessible |
| 299 | |
| 300 | //Cut away excess reach of rectangular region along alpha: |
| 301 | alphaRange.second = ncmincxx . . $ NCrystal/ncmin(d8e7e0733fa41c87).(alphaRange.second,apb1); |
| 302 | |
| 303 | //Cut away excess reach of rectangular region along beta: |
| 304 | if ( apb0 < alphaRange.first ) { |
| 305 | //The next formula follows from inverting the formula |
| 306 | //alphaPlus(betaRange.first) = alphaRange.first: |
| 307 | betaRange.first = alphaRange.first - 2.0 * stdcxx . . $ std/::sqrt( E_div_kT * alphaRange.first ); |
| 308 | nc_assertcxx . . $ `ncrystal_core/include/NCrystal/NCException.hh:153:11`!( floateq( alphaPlus(betaRange.first), alphaRange.first ) ); |
| 309 | } |
| 310 | |
| 311 | //Rectangular region has no excess now, result is given by its extreme |
| 312 | //corner: |
| 313 | return { alphaRange.second, betaRange.first }; |
| 314 | } |
| 315 | |
| 316 | bool NC::sabPointWithinAlphaPlusCurvecxx . . $ NCrystal/sabPointWithinAlphaPlusCurve(c3c51e0645da350e).(double E_div_kT, double alpha, double beta ) |
| 317 | { |
| 318 | //Same as findExtremeSABPointWithinAlphaPlusCurve, but only testing whether or |
| 319 | //not a given single point is accessible, in the sense that it has beta>=-E/kT |
| 320 | //and alpha<alpha+(beta). Note that this deliberately ignores the alpha-(beta) |
| 321 | //curve. |
| 322 | nc_assertcxx . . $ `ncrystal_core/include/NCrystal/NCException.hh:153:11`!( alpha >= 0.0 ); |
| 323 | nc_assertcxx . . $ `ncrystal_core/include/NCrystal/NCException.hh:153:11`!( E_div_kT > 0.0 ); |
| 324 | const double c = E_div_kT; |
| 325 | const double cpb = c + beta; |
| 326 | if ( cpb < 0.0 ) |
| 327 | return false; |
| 328 | //With c=E/kT, we check the alpha+ condition: |
| 329 | // alpha+(beta) = 2*c + beta + 2 * sqrt( c * ( c + beta ) ). |
| 330 | // So "within" means: alpha+(beta) >= alpha <=> sqrt( c * ( c + beta ) ) >= (alpha-beta)/2 - c |
| 331 | const double t = 0.5 * ( alpha - beta ) - c; |
| 332 | return t <= 0.0 || c*cpb >= t*t; |
| 333 | } |
| 334 | |
| 335 | NC::VectDcxx . . $ NCrystal/VectD# NC::setupAlphaGridcxx . . $ NCrystal/setupAlphaGrid(9ca19a1f3c97ba11).( double kT, double msd, double alphaMax, unsigned npts ) |
| 336 | { |
| 337 | nc_assertcxx . . $ `ncrystal_core/include/NCrystal/NCException.hh:153:11`!(npts>=20); |
| 338 | |
| 339 | //Strategy (region 0 ensures proper binning for upscattering of ultra-low |
| 340 | //energy neutrons, and are simply merged onto the grid pts from the other 3 |
| 341 | //regions at the end, using the finalise_grid function further down): |
| 342 | // |
| 343 | // Region 0 (N=15% of pts): x=linspace from x=1e-3 to x=alpha_upscatmax*alpha2x |
| 344 | // Region 1 (N=29% of pts): x=1e-50 + linspace[N-1] from x=1e-10 to x=1.0 (including 1.0). |
| 345 | // Region 2 (N=23% of pts): linspace[N+2] from x=1.0 to x=15.0 (excluding both endpoints). |
| 346 | // Region 3 (rest, N~=33% of pts): geomspace[N+1] from x=15.0 to final limit (including 15.0). |
| 347 | // |
| 348 | //We give special treatment to cases where alphaMax is not above alphaG15Maxx. |
| 349 | // |
| 350 | //alpha_upscatmax is roughly 10, but slightly higher or lower depending on |
| 351 | //number of points. |
| 352 | |
| 353 | const double alpha2x = V2SKDetailcxx . . $ NCrystal/V2SKDetail/::kTmsd_to_alpha2xcxx . . $ NCrystal/V2SKDetail/kTmsd_to_alpha2x(d8e7e0733fa41c87).(kT,msd); |
| 354 | const double x2alpha = 1.0 / alpha2x; |
| 355 | const double alphaMin = x2alpha*1e-50; |
| 356 | const double alphaMin2 = x2alpha*1e-10; |
| 357 | const double alphaG1Maxx = x2alpha*1.0; |
| 358 | const double alphaG15Maxx = x2alpha*15.0; |
| 359 | |
| 360 | unsigned n0 = static_cast<unsigned>(npts*0.15+0.5); |
| 361 | unsigned n1 = static_cast<unsigned>(npts*0.29+0.5); |
| 362 | unsigned n2 = static_cast<unsigned>(npts*0.23+0.5); |
| 363 | unsigned n3 = npts-(n1+n2+n0); |
| 364 | unsigned npts_123 = n1+n2+n3; |
| 365 | nc_assertcxx . . $ `ncrystal_core/include/NCrystal/NCException.hh:153:11`!(n1>=3&&n2>=3&&n3>=3&&n0>=3&&n0+n1+n2+n3==npts); |
| 366 | |
| 367 | const double alpha_upscatmax = (n0<10?6.0:(n0>50?14.0:10.0)); |
| 368 | const auto grid_region0 = linspacecxx . . $ NCrystal/linspace(f3436739aeb888dd).( ncmincxx . . $ NCrystal/ncmin(d8e7e0733fa41c87).(1e-3,alphaMax*0.01), |
| 369 | ncmincxx . . $ NCrystal/ncmin(d8e7e0733fa41c87).(alpha_upscatmax,alphaMax*0.99), |
| 370 | n0 ); |
| 371 | |
| 372 | auto finalise_grid = [&grid_region0,npts](const VectDcxx . . $ NCrystal/VectD#& grid) -> VectDcxx . . $ NCrystal/VectD# |
| 373 | { |
| 374 | nc_assert_alwayscxx . . $ `ncrystal_core/include/NCrystal/NCException.hh:146:9`!(grid.size()+grid_region0.size()==npts); |
| 375 | |
| 376 | //We want to merge the grid_region0 points into the |
| 377 | //grid points. However, the grid_region0 points will be |
| 378 | //moved a bit to not accidentally coincide with |
| 379 | //existing points. Thus, for each grid_region0 pt, find |
| 380 | //the two neighbouring values (looking in *both* grid |
| 381 | //and grid_region0). Place the point exactly between |
| 382 | //those two. I.e. just merge+sort all numbers, but make |
| 383 | //sure we know which are from region0. Those will be |
| 384 | //placed halfway between neighbours afterwards. |
| 385 | stdcxx . . $ std/::vectorcxx . . $ std/vector#<stdcxx . . $ std/::pair<double,bool>> all_mergedcxx . . $ std/vector#vector(49f6e7a06ebc5aa8).; |
| 386 | for (auto e: grid) |
| 387 | all_merged.emplace_backcxx . . $ std/vector#emplace_back(a1bcdfdabc3a37df).(e,false); |
| 388 | for (auto e: grid_region0) |
| 389 | all_merged.emplace_backcxx . . $ std/vector#emplace_back(a1bcdfdabc3a37df).(e,true); |
| 390 | stdcxx . . $ std/::stable_sortcxx . . $ std/stable_sort(249673c20e2b8d5).(all_merged.begincxx . . $ std/vector#begin(55a6de06420d4903).(),all_merged.endcxx . . $ std/vector#end(55a6de06420d4903).()); |
| 391 | auto it = stdcxx . . $ std/::next(all_merged.begincxx . . $ std/vector#begin(55a6de06420d4903).()); |
| 392 | auto itLast = stdcxx . . $ std/::prev(all_merged.endcxx . . $ std/vector#end(55a6de06420d4903).()); |
| 393 | for (;it!=itLast;++it) { |
| 394 | if ( it->second == false ) |
| 395 | continue; |
| 396 | it->first = 0.5*(stdcxx . . $ std/::prev(it)->first+stdcxx . . $ std/::next(it)->first); |
| 397 | } |
| 398 | VectDcxx . . $ NCrystal/VectD# outcxx . . $ std/vector#vector(49f6e7a06ebc5aa8).; |
| 399 | out.reservecxx . . $ std/vector#reserve(2c44cd906ae4e829).(npts); |
| 400 | for (const auto& e: all_merged) |
| 401 | out.push_backcxx . . $ std/vector#push_back(77d9f17553126ecb).(e.first); |
| 402 | nc_assert_alwayscxx . . $ `ncrystal_core/include/NCrystal/NCException.hh:146:9`!(nc_is_grid(out)); |
| 403 | nc_assert_alwayscxx . . $ `ncrystal_core/include/NCrystal/NCException.hh:146:9`!(out.size()==npts); |
| 404 | return outcxx . . $ std/vector#vector(6f559d22b039ccdb).; |
| 405 | }; |
| 406 | |
| 407 | if ( alphaMax <= alphaMin*100.0 ) { |
| 408 | //special case #1, insanely small alphaMax |
| 409 | return finalise_grid(cxx . . $ NCrystal/setupAlphaGrid(9ca19a1f3c97ba11).$anonymous_type_1#`operator()`(be3d5686be3109ae).linspacecxx . . $ NCrystal/linspace(f3436739aeb888dd).(alphaMax*0.001,alphaMax,npts_123)); |
| 410 | } |
| 411 | |
| 412 | VectDcxx . . $ NCrystal/VectD# gridcxx . . $ std/vector#vector(49f6e7a06ebc5aa8).; |
| 413 | grid.reservecxx . . $ std/vector#reserve(2c44cd906ae4e829).(npts_123); |
| 414 | grid.push_backcxx . . $ std/vector#push_back(77d9f17553126ecb).(alphaMin); |
| 415 | if ( alphaMax <= alphaG1Maxx*10.0 ) { |
| 416 | //special case #2, small alphaMax. |
| 417 | vectorAppendcxx . . $ NCrystal/vectorAppend(e2bc72a82ed8a7ce).(grid, linspacecxx . . $ NCrystal/linspace(f3436739aeb888dd).(alphaMin2,alphaMax,npts_123-1) ); |
| 418 | nc_assertcxx . . $ `ncrystal_core/include/NCrystal/NCException.hh:153:11`!( grid.size() == npts_123 && nc_is_grid(grid) ); |
| 419 | return finalise_grid(cxx . . $ NCrystal/setupAlphaGrid(9ca19a1f3c97ba11).$anonymous_type_1#`operator()`(be3d5686be3109ae).grid); |
| 420 | } |
| 421 | |
| 422 | //Region 1: |
| 423 | vectorAppendcxx . . $ NCrystal/vectorAppend(e2bc72a82ed8a7ce).(grid, linspacecxx . . $ NCrystal/linspace(f3436739aeb888dd).(alphaMin2,alphaG1Maxx,n1-1) ); |
| 424 | |
| 425 | //Region 2: |
| 426 | if ( alphaMax < 2.0 * alphaG15Maxx) { |
| 427 | //Special case #3: somewhat small alphaMax => absorb region3 into region 2 |
| 428 | auto ls2 = linspacecxx . . $ NCrystal/linspace(f3436739aeb888dd).(alphaG1Maxx,alphaMax,n2+n3+2); |
| 429 | grid.insertcxx . . $ std/vector#insert(134ef7853f0b3ebe).( grid.endcxx . . $ std/vector#end(55a6de06420d4903).(), stdcxx . . $ std/::next(ls2.begincxx . . $ std/vector#begin(55a6de06420d4903).()), stdcxx . . $ std/::prev(ls2.endcxx . . $ std/vector#end(55a6de06420d4903).()) ); |
| 430 | nc_assertcxx . . $ `ncrystal_core/include/NCrystal/NCException.hh:153:11`!( grid.size() == npts_123 && nc_is_grid(grid) ); |
| 431 | return finalise_grid(cxx . . $ NCrystal/setupAlphaGrid(9ca19a1f3c97ba11).$anonymous_type_1#`operator()`(be3d5686be3109ae).grid); |
| 432 | } |
| 433 | auto ls2 = linspacecxx . . $ NCrystal/linspace(f3436739aeb888dd).(alphaG1Maxx,alphaG15Maxx,n2+2); |
| 434 | grid.insertcxx . . $ std/vector#insert(134ef7853f0b3ebe).( grid.endcxx . . $ std/vector#end(55a6de06420d4903).(), stdcxx . . $ std/::next(ls2.begincxx . . $ std/vector#begin(55a6de06420d4903).()), stdcxx . . $ std/::prev(ls2.endcxx . . $ std/vector#end(55a6de06420d4903).()) ); |
| 435 | //Region 3: |
| 436 | vectorAppendcxx . . $ NCrystal/vectorAppend(e2bc72a82ed8a7ce).(grid, geomspacecxx . . $ NCrystal/geomspace(f3436739aeb888dd).(alphaG15Maxx,alphaMax,n3) ); |
| 437 | nc_assertcxx . . $ `ncrystal_core/include/NCrystal/NCException.hh:153:11`!( grid.size() == npts_123 && nc_is_grid(grid) ); |
| 438 | return finalise_grid(cxx . . $ NCrystal/setupAlphaGrid(9ca19a1f3c97ba11).$anonymous_type_1#`operator()`(be3d5686be3109ae).grid); |
| 439 | } |
| 440 | |
| 441 | NC::VectDcxx . . $ NCrystal/VectD# NC::setupBetaGridcxx . . $ NCrystal/setupBetaGrid(dd420c3d5da75090).( const NC::VDOSGncxx . . $ NCrystal/VDOSGn#& Gn, double betaMax, unsigned vdoslux, unsigned override_nbins ) |
| 442 | { |
| 443 | nc_assertcxx . . $ `ncrystal_core/include/NCrystal/NCException.hh:153:11`!(Gn.maxOrder().value()>=1); |
| 444 | nc_assert_alwayscxx . . $ `ncrystal_core/include/NCrystal/NCException.hh:146:9`!(vdoslux<=5); |
| 445 | nc_assert_alwayscxx . . $ `ncrystal_core/include/NCrystal/NCException.hh:146:9`!(betaMax>0.0); |
| 446 | |
| 447 | //Reach of 1-phonon and 3-phonon spectrums (converted E->beta): |
| 448 | const double invkT = 1.0/Gn.kTcxx . . $ NCrystal/VDOSGn#kT(21b6c25e127938e2).(); |
| 449 | const double G1 = ncabscxx . . $ NCrystal/ncabs(447c67c6676f75c3).(Gn.eRangecxx . . $ NCrystal/VDOSGn#eRange(92d16a05c63f5098).(1cxx . . $ NCrystal/VDOSGn#Order#Order(d4f767463ce0a6b3).).first*invkT); |
| 450 | nc_assertcxx . . $ `ncrystal_core/include/NCrystal/NCException.hh:153:11`!(G1>0.0); |
| 451 | double G3 = ncabscxx . . $ NCrystal/ncabs(447c67c6676f75c3).(Gn.eRangecxx . . $ NCrystal/VDOSGn#eRange(92d16a05c63f5098).( stdcxx . . $ NCrystal/VDOSGn#Order#Order(cb2f890c2fdad230).::mincxx . . $ std/min(2a338fce5842aa6a).<unsigned>(3,Gn.maxOrdercxx . . $ NCrystal/VDOSGn#maxOrder(422dc87a5e0c7f13).().valuecxx . . $ NCrystal/VDOSGn#Order#value(ddd8539a2fcdd4ce).()) ).first*invkT);//nb: actually =G1 or =G2 in case of maxOrder<3. |
| 452 | nc_assertcxx . . $ `ncrystal_core/include/NCrystal/NCException.hh:153:11`!( Gn.maxOrder().value()==1 || G3>G1 ); |
| 453 | if (G1==G3) |
| 454 | G3 = G1*1.0001; |
| 455 | |
| 456 | |
| 457 | //Construct betagrid which will run from [-betaMax,D] with |
| 458 | //0<D<=betaMax. Assuming betaMax is large enough, D will always be large |
| 459 | //enough to encompass the first phonon order, and energy transfers of 20*kT, |
| 460 | //whichever is larger (at luxlevel 4 and 5 the 20*kT limit is increased to |
| 461 | //30*kT and 40*kT respectively). |
| 462 | // |
| 463 | //We divide pts in regions: |
| 464 | //Region0: The single point beta=0. |
| 465 | //Region1: [-G1,0) (all points flipped and duplicated in (0,G1]). |
| 466 | //Region2: [-D,-G1) (all points flipped and duplicated in (G1,D]). |
| 467 | //Region3: [-betaMax,-D) (no duplication of points). Must be odd number of points. |
| 468 | // |
| 469 | //always encompass single-phonons, and clip G3/D to betaMax if needed, to |
| 470 | //ensure no regions will have 0 range: |
| 471 | betaMax = ncmaxcxx . . $ NCrystal/ncmax(d8e7e0733fa41c87).(betaMax,G1*1.01); |
| 472 | G3 = ncmincxx . . $ NCrystal/ncmin(d8e7e0733fa41c87).(G3,betaMax*0.9999); |
| 473 | const double D = ncmincxx . . $ NCrystal/ncmin(d8e7e0733fa41c87).(betaMax*0.9999,ncmaxcxx . . $ NCrystal/ncmax(d8e7e0733fa41c87).(G3,stdcxx . . $ std/::maxcxx . . $ std/max(2a338fce5842aa6a).<int>(2,vdoslux-1)*10.0)); |
| 474 | nc_assertcxx . . $ `ncrystal_core/include/NCrystal/NCException.hh:153:11`!( D>0.0 && G3 > G1 && D>=G3 && D<=betaMax ); |
| 475 | |
| 476 | //How many points to use? The total is given as 100 * 2^(vdoslux), meaning |
| 477 | //100 for vdoslux=0, 200 for vdoslux=1, and so on up to 3200 for vdoslux=5: |
| 478 | const unsigned ntotal = override_nbins ? override_nbins : 100*(1<<vdoslux);//100 * 2^(vdoslux). |
| 479 | |
| 480 | VectDcxx . . $ NCrystal/VectD# gridcxx . . $ std/vector#vector(49f6e7a06ebc5aa8).; |
| 481 | grid.reservecxx . . $ std/vector#reserve(2c44cd906ae4e829).(ntotal); |
| 482 | |
| 483 | //First do a bit of pre-analysis for the G1 spectrum in Region1: |
| 484 | //Here we don't just space the points uniformly, but try to place them to |
| 485 | //get the single-phonon curve best described. Additionally, we use 20% (but |
| 486 | //at least 5) of the points to put some very small values around |
| 487 | //beta=0. This is not needed to describe the actual data, but is used to |
| 488 | //reduce artefacts in some integration/sampling algorithms (ideally those |
| 489 | //algs should instead be updated to avoid such artefacts all by |
| 490 | //themselves...). |
| 491 | |
| 492 | auto rawspec_G1 = Gncxx . . $ std/vector#vector(a1201200cf0efa42)..getRawSpectrumcxx . . $ NCrystal/VDOSGn#getRawSpectrum(3318a66a553cb639).(1cxx . . $ NCrystal/VDOSGn#Order#Order(d4f767463ce0a6b3).); |
| 493 | auto erange_G1 = Gn.eRangecxx . . $ NCrystal/VDOSGn#eRange(92d16a05c63f5098).(1cxx . . $ NCrystal/VDOSGn#Order#Order(d4f767463ce0a6b3).); |
| 494 | //Convert E->beta: |
| 495 | erange_G1.first *= invkT; |
| 496 | erange_G1.second *= invkT; |
| 497 | nc_assertcxx . . $ `ncrystal_core/include/NCrystal/NCException.hh:153:11`!(floateq(erange_G1.first,-G1)); |
| 498 | auto evals_G1 = linspacecxx . . $ NCrystal/linspace(f3436739aeb888dd).(erange_G1.first,erange_G1.second,rawspec_G1.sizecxx . . $ std/vector#size(c9b1e749b64d5edc).()); |
| 499 | nc_assert_alwayscxx . . $ `ncrystal_core/include/NCrystal/NCException.hh:146:9`!(evals_G1.front()<0.0); |
| 500 | nc_assert_alwayscxx . . $ `ncrystal_core/include/NCrystal/NCException.hh:146:9`!(evals_G1.back()>0.0); |
| 501 | //Ignore positive part and 0 (NB: value representing zero can actually be |
| 502 | //slightly non-zero due to numerical issues, hence need for epsilon): |
| 503 | const double epsilon = -0.1*Gn.binWidthcxx . . $ NCrystal/VDOSGn#binWidth(68eab0ef407f1fcc).(1cxx . . $ NCrystal/VDOSGn#Order#Order(d4f767463ce0a6b3).); |
| 504 | nc_assert_alwayscxx . . $ `ncrystal_core/include/NCrystal/NCException.hh:146:9`!(evals_G1.front()<epsilon); |
| 505 | while ( evals_G1.backcxx . . $ std/vector#back(6baabfa6fc7b3a8e).()>epsilon ) |
| 506 | evals_G1.pop_backcxx . . $ std/vector#pop_back(ced63f7c635d850d).(); |
| 507 | nc_assertcxx . . $ `ncrystal_core/include/NCrystal/NCException.hh:153:11`!(evals_G1.size()>=2); |
| 508 | rawspec_G1.resizecxx . . $ std/vector#resize(2c44cd906ae4e829).(evals_G1.sizecxx . . $ std/vector#size(c9b1e749b64d5edc).()); |
| 509 | |
| 510 | //The absolute maximum number of points which can be useful in region 1 is |
| 511 | //evals_G1.size() plus a bit for fine-grained grid near beta=0: |
| 512 | const unsigned n1_max = static_cast<unsigned>(evals_G1.sizecxx . . $ std/vector#size(c9b1e749b64d5edc).()*1.25+6.5) & ~1;//& ~1 ensures even |
| 513 | nc_assertcxx . . $ `ncrystal_core/include/NCrystal/NCException.hh:153:11`!(n1_max>=8); |
| 514 | |
| 515 | //Determine how many pts to assign to the different regions, depending on the |
| 516 | //length of those regions: |
| 517 | |
| 518 | const unsigned n0 = 1;//the beta=0 point |
| 519 | |
| 520 | //Length of beta-scale covered by various regions: |
| 521 | nc_assert_alwayscxx . . $ `ncrystal_core/include/NCrystal/NCException.hh:146:9`!(D>G1); |
| 522 | nc_assert_alwayscxx . . $ `ncrystal_core/include/NCrystal/NCException.hh:146:9`!(betaMax>D); |
| 523 | double L1 = 2*G1; |
| 524 | double L2 = 2*(D-G1); |
| 525 | double L3 = betaMax-D; |
| 526 | //Ad-hoc boost of central parts where we expect more variation: |
| 527 | L1 *= 4; |
| 528 | L2 *= 2; |
| 529 | double Lnorm = 1.0/(L1+L2+L3); |
| 530 | double f1 = ncmaxcxx . . $ NCrystal/ncmax(d8e7e0733fa41c87).(0.2,L1*Lnorm); |
| 531 | double f2 = L2*Lnorm; |
| 532 | if (f1+f2>0.99) { |
| 533 | double tmp = 0.99/(f1+f2); |
| 534 | f1 *= tmp; |
| 535 | f2 *= tmp; |
| 536 | } |
| 537 | |
| 538 | unsigned n1 = stdcxx . . $ std/::mincxx . . $ std/min(2a338fce5842aa6a).<unsigned>(n1_max,static_cast<unsigned>(f1*ntotal+0.5) / 2);//div 2 due to duplication |
| 539 | unsigned n2 = static_cast<unsigned>(f2*ntotal+0.5) / 2;//div 2 due to duplication |
| 540 | n1 = stdcxx . . $ std/::maxcxx . . $ std/max(2a338fce5842aa6a).<unsigned>(15,n1); |
| 541 | n2 = stdcxx . . $ std/::maxcxx . . $ std/max(2a338fce5842aa6a).<unsigned>(1,n2); |
| 542 | unsigned n012; |
| 543 | do { |
| 544 | n012 = 2*(n1+n2)+n0; |
| 545 | if (n012 >= ntotal - 1) { |
| 546 | if (n2>n1) |
| 547 | --n2; |
| 548 | else |
| 549 | --n1; |
| 550 | } |
| 551 | else |
| 552 | break; |
| 553 | } while (true); |
| 554 | nc_assert_alwayscxx . . $ `ncrystal_core/include/NCrystal/NCException.hh:146:9`!(n1>=10); |
| 555 | |
| 556 | unsigned n3 = ntotal-n012; |
| 557 | |
| 558 | nc_assertcxx . . $ `ncrystal_core/include/NCrystal/NCException.hh:153:11`!(n0==1); |
| 559 | nc_assertcxx . . $ `ncrystal_core/include/NCrystal/NCException.hh:153:11`!(n1>=10); |
| 560 | nc_assertcxx . . $ `ncrystal_core/include/NCrystal/NCException.hh:153:11`!(n2>=1); |
| 561 | nc_assertcxx . . $ `ncrystal_core/include/NCrystal/NCException.hh:153:11`!(n3>=1); |
| 562 | nc_assertcxx . . $ `ncrystal_core/include/NCrystal/NCException.hh:153:11`!( ntotal == n0 + n3 + 2 * ( n1 + n2) ); |
| 563 | nc_assert_alwayscxx . . $ `ncrystal_core/include/NCrystal/NCException.hh:146:9`!(n1 <= ntotal ); |
| 564 | nc_assert_alwayscxx . . $ `ncrystal_core/include/NCrystal/NCException.hh:146:9`!(n2 <= ntotal ); |
| 565 | nc_assert_alwayscxx . . $ `ncrystal_core/include/NCrystal/NCException.hh:146:9`!(n3 <= ntotal ); |
| 566 | nc_assert_alwayscxx . . $ `ncrystal_core/include/NCrystal/NCException.hh:146:9`!(D>G1); |
| 567 | nc_assert_alwayscxx . . $ `ncrystal_core/include/NCrystal/NCException.hh:146:9`!(betaMax>D); |
| 568 | |
| 569 | //Time to add values to the grid! First Region3: |
| 570 | { |
| 571 | auto vals = linspacecxx . . $ NCrystal/linspace(f3436739aeb888dd).(-betaMax, -D, n3+1); |
| 572 | grid.insertcxx . . $ std/vector#insert(134ef7853f0b3ebe).(grid.begincxx . . $ std/vector#begin(55a6de06420d4903).(),vals.begincxx . . $ std/vector#begin(55a6de06420d4903).(),stdcxx . . $ std/::prev(vals.endcxx . . $ std/vector#end(55a6de06420d4903).())); |
| 573 | nc_assertcxx . . $ `ncrystal_core/include/NCrystal/NCException.hh:153:11`!(grid.size()==n3); |
| 574 | } |
| 575 | |
| 576 | const auto idx_R2Start = grid.sizecxx . . $ std/vector#size(c9b1e749b64d5edc).(); |
| 577 | |
| 578 | //Then Region 2 - negative values. |
| 579 | { |
| 580 | auto vals_r2 = linspacecxx . . $ NCrystal/linspace(f3436739aeb888dd).(-D,-G1,n2+1); |
| 581 | vals_r2.resizecxx . . $ std/vector#resize(2c44cd906ae4e829).(vals_r2.sizecxx . . $ std/vector#size(c9b1e749b64d5edc).()-1); |
| 582 | vectorAppendcxx . . $ NCrystal/vectorAppend(e2bc72a82ed8a7ce).(grid,vals_r2); |
| 583 | } |
| 584 | |
| 585 | //Now Region 1 - negative values. |
| 586 | { |
| 587 | unsigned n1_near0 = stdcxx . . $ std/::maxcxx . . $ std/max(2a338fce5842aa6a).<unsigned>(5,static_cast<unsigned>(n1*0.2+0.5)); |
| 588 | unsigned n1_spectrum = n1-n1_near0; |
| 589 | if (n1_spectrum >= evals_G1.sizecxx . . $ std/vector#size(c9b1e749b64d5edc).()) { |
| 590 | //Great: can keep all points in G1 spectrum: |
| 591 | n1_spectrum = evals_G1.sizecxx . . $ std/vector#size(c9b1e749b64d5edc).(); |
| 592 | n1_near0 = n1 - n1_spectrum; |
| 593 | } else { |
| 594 | //Remove least important points from G1 spectrum: |
| 595 | #if 0 |
| 596 | //Old way, just run reducePtsInDistribution. This might leave huge gaps if |
| 597 | //for instance VDOS is zero over a wide internal region: |
| 598 | std::tie(evals_G1,rawspec_G1) = reducePtsInDistribution( evals_G1,rawspec_G1, n1_spectrum ); |
| 599 | #else |
| 600 | //New way, avoid leaving huge gaps without pts. Doing so can leave large |
| 601 | //gaps in the beta points, which when can lead to numerical artifacts when |
| 602 | //a scattering kernel is later integrated and sampled. If at some point |
| 603 | //(TODO!) we implement better integration/sampling algorithms, we can |
| 604 | //hopefully stop doing this - and perhaps also stop needing the n1_near |
| 605 | //values. |
| 606 | unsigned n_for_gaps = ( ( n1_spectrum > 30 && evals_G1.sizecxx . . $ std/vector#size(c9b1e749b64d5edc).()-n1_spectrum > 10 ) |
| 607 | ? stdcxx . . $ std/::maxcxx . . $ std/max(2a338fce5842aa6a).<unsigned>(5,static_cast<unsigned>(n1_spectrum*0.1+0.5)) |
| 608 | : 0 ); |
| 609 | n1_spectrum -= n_for_gaps; |
| 610 | const double G1binwidth = evals_G1.atcxx . . $ std/vector#at(428bb168810b9e52).(1)-evals_G1.atcxx . . $ std/vector#at(428bb168810b9e52).(0); |
| 611 | stdcxx . . $ std/::tiecxx . . $ std/tie(988772b69a7ca893).(evals_G1,rawspec_G1) =cxx . . $ std/tuple#`operator=`(18d19790a4ab438c). reducePtsInDistributioncxx . . $ NCrystal/reducePtsInDistribution(e6118e4a4b517f4e).( evals_G1,rawspec_G1, n1_spectrum ); |
| 612 | if ( n_for_gaps > 0 ) { |
| 613 | struct Gapcxx . . $ NCrystal/setupBetaGrid(dd420c3d5da75090).Gap# { |
| 614 | Gapcxx . . $ NCrystal/setupBetaGrid(dd420c3d5da75090).Gap#Gap(bc12c9ec15586e6a).(double bbb0, double bbb1) |
| 615 | : b0cxx . . $ NCrystal/setupBetaGrid(dd420c3d5da75090).Gap#b0.(bbb0), b1cxx . . $ NCrystal/setupBetaGrid(dd420c3d5da75090).Gap#b1.(bbb1) |
| 616 | {} |
| 617 | double b0cxx . . $ NCrystal/setupBetaGrid(dd420c3d5da75090).Gap#b0., b1cxx . . $ NCrystal/setupBetaGrid(dd420c3d5da75090).Gap#b1.;//edges of gap |
| 618 | unsigned ncxx . . $ NCrystal/setupBetaGrid(dd420c3d5da75090).Gap#n. = 0;//number of points allocated to fill the gap |
| 619 | bool operatorcxx . . $ NCrystal/setupBetaGrid(dd420c3d5da75090).Gap#`operator<`(13006628221404eb).<( const Gapcxx . . $ NCrystal/setupBetaGrid(dd420c3d5da75090).Gap#& o ) const { |
| 620 | double a = ( b1cxx . . $ NCrystal/setupBetaGrid(dd420c3d5da75090).Gap#b1. - b0cxx . . $ NCrystal/setupBetaGrid(dd420c3d5da75090).Gap#b0. ) / ( ncxx . . $ NCrystal/setupBetaGrid(dd420c3d5da75090).Gap#n. + 1 ); |
| 621 | double b = ( o.b1cxx . . $ NCrystal/setupBetaGrid(dd420c3d5da75090).Gap#b1. - o.b0cxx . . $ NCrystal/setupBetaGrid(dd420c3d5da75090).Gap#b0. ) / ( o.ncxx . . $ NCrystal/setupBetaGrid(dd420c3d5da75090).Gap#n. + 1 ); |
| 622 | if ( floateqcxx . . $ NCrystal/floateq(ad3a29c0330224a3).(a,b,1e-13,1e-13) ) |
| 623 | return b0cxx . . $ NCrystal/setupBetaGrid(dd420c3d5da75090).Gap#b0. > o.b0cxx . . $ NCrystal/setupBetaGrid(dd420c3d5da75090).Gap#b0.; |
| 624 | return a > b;//reverse sort |
| 625 | } |
| 626 | }; |
| 627 | //Find all gaps: |
| 628 | stdcxx . . $ std/::vectorcxx . . $ std/vector#<Gapcxx . . $ NCrystal/setupBetaGrid(dd420c3d5da75090).Gap#> gapscxx . . $ std/vector#vector(49f6e7a06ebc5aa8).; |
| 629 | gaps.reservecxx . . $ std/vector#reserve(2c44cd906ae4e829).(256); |
| 630 | double dmin = 1.5 * G1binwidth; |
| 631 | auto it = evals_G1.begincxx . . $ std/vector#begin(55a6de06420d4903).(); |
| 632 | auto itLast = stdcxx . . $ std/::prev(evals_G1.endcxx . . $ std/vector#end(55a6de06420d4903).()); |
| 633 | for ( ; it != itLast; ++it ) { |
| 634 | double dd = *stdcxx . . $ std/::next(it)-*it; |
| 635 | if ( dd > dmin ) |
| 636 | gaps.emplace_backcxx . . $ std/vector#emplace_back(a1bcdfdabc3a37df).( *it, *stdcxx . . $ std/::next(it) ); |
| 637 | } |
| 638 | //Allocate pts one by one into largest remaining gap: |
| 639 | while (n_for_gaps>0 && !gaps.emptycxx . . $ std/vector#empty(3482b152b9333168).() ) { |
| 640 | stdcxx . . $ std/::stable_sortcxx . . $ std/stable_sort(249673c20e2b8d5).(gaps.begincxx . . $ std/vector#begin(55a6de06420d4903).(),gaps.endcxx . . $ std/vector#end(55a6de06420d4903).()); |
| 641 | gaps.frontcxx . . $ std/vector#front(6baabfa6fc7b3a8e).().ncxx . . $ NCrystal/setupBetaGrid(dd420c3d5da75090).Gap#n. += 1; |
| 642 | --n_for_gaps; |
| 643 | } |
| 644 | //Transfer points in gaps to evals_G1: |
| 645 | for ( const auto& g : gaps ) { |
| 646 | if ( g.ncxx . . $ NCrystal/setupBetaGrid(dd420c3d5da75090).Gap#n. > 0 ) { |
| 647 | double bw = (g.b1cxx . . $ NCrystal/setupBetaGrid(dd420c3d5da75090).Gap#b1.-g.b0cxx . . $ NCrystal/setupBetaGrid(dd420c3d5da75090).Gap#b0.)/(g.ncxx . . $ NCrystal/setupBetaGrid(dd420c3d5da75090).Gap#n.+1.0); |
| 648 | for ( auto i : ncrangecxx . . $ NCrystal/ncrange(852f5944255b128f).(g.ncxx . . $ NCrystal/setupBetaGrid(dd420c3d5da75090).Gap#n.) ) { |
| 649 | evals_G1.push_backcxx . . $ std/vector#push_back(fe7a8a4d1eddb70).( g.b0cxx . . $ NCrystal/setupBetaGrid(dd420c3d5da75090).Gap#b0. + (i+1)*bw ); |
| 650 | } |
| 651 | } |
| 652 | } |
| 653 | stdcxx . . $ std/::sortcxx . . $ std/sort(249673c20e2b8d5).(evals_G1.begincxx . . $ std/vector#begin(55a6de06420d4903).(),evals_G1.endcxx . . $ std/vector#end(55a6de06420d4903).()); |
| 654 | } |
| 655 | if ( n_for_gaps > 0 ) { |
| 656 | //unused gap filler points, put somewhere else: |
| 657 | n1_near0 += n_for_gaps; |
| 658 | } |
| 659 | #endif |
| 660 | } |
| 661 | |
| 662 | for ( auto e : evals_G1 ) |
| 663 | grid.push_backcxx . . $ std/vector#push_back(77d9f17553126ecb).(e); |
| 664 | |
| 665 | //To reduce artefacts in sampling/integration algs, add points near 0: |
| 666 | nc_assertcxx . . $ `ncrystal_core/include/NCrystal/NCException.hh:153:11`!( n1_near0 > 0 ); |
| 667 | nc_assertcxx . . $ `ncrystal_core/include/NCrystal/NCException.hh:153:11`!( grid.back() < 0.0 ); |
| 668 | auto v = geomspacecxx . . $ NCrystal/geomspace(f3436739aeb888dd).(ncmincxx . . $ NCrystal/ncmin(d8e7e0733fa41c87).(1e-50,-0.001*grid.backcxx . . $ std/vector#back(6baabfa6fc7b3a8e).()),-grid.backcxx . . $ std/vector#back(6baabfa6fc7b3a8e).()*0.1,n1_near0); |
| 669 | stdcxx . . $ std/::reversecxx . . $ std/reverse(249673c20e2b8d5).(v.begincxx . . $ std/vector#begin(55a6de06420d4903).(),v.endcxx . . $ std/vector#end(55a6de06420d4903).()); |
| 670 | for (auto e: v) |
| 671 | grid.push_backcxx . . $ std/vector#push_back(fe7a8a4d1eddb70).( -e ); |
| 672 | } |
| 673 | const auto idx_R1Back = grid.sizecxx . . $ std/vector#size(c9b1e749b64d5edc).()-1; |
| 674 | |
| 675 | //Then Region0, just the beta=0 point: |
| 676 | grid.push_backcxx . . $ std/vector#push_back(fe7a8a4d1eddb70).( 0.0 ); |
| 677 | |
| 678 | //Now replay region1 and region2 in flipped reverse order to generate |
| 679 | //positive beta values: |
| 680 | for ( auto i = idx_R1Back; i>=idx_R2Start; --i ) |
| 681 | grid.push_backcxx . . $ std/vector#push_back(fe7a8a4d1eddb70).( -vectAtcxx . . $ NCrystal/vectAt(bc217a45c22e0a7b).(grid,i) ); |
| 682 | |
| 683 | nc_assertcxx . . $ `ncrystal_core/include/NCrystal/NCException.hh:153:11`!( grid.front()==-betaMax ); |
| 684 | nc_assertcxx . . $ `ncrystal_core/include/NCrystal/NCException.hh:153:11`!( grid.back()==D ); |
| 685 | nc_assertcxx . . $ `ncrystal_core/include/NCrystal/NCException.hh:153:11`!( grid.size() == ntotal ); |
| 686 | nc_assertcxx . . $ `ncrystal_core/include/NCrystal/NCException.hh:153:11`!( nc_is_grid(grid) ); |
| 687 | return gridcxx . . $ std/vector#vector(6f559d22b039ccdb).; |
| 688 | } |
| 689 | |
| 690 | NC::ScatKnlDatacxx . . $ NCrystal/ScatKnlData# NC::createScatteringKernelcxx . . $ NCrystal/createScatteringKernel(df30a0497c822e3d).( const VDOSDatacxx . . $ NCrystal/VDOSData#& vdosdata, |
| 691 | unsigned vdoslux, |
| 692 | double targetEmax_requested, |
| 693 | VDOSGncxx . . $ NCrystal/VDOSGn#::TruncAndThinningParamscxx . . $ NCrystal/VDOSGn#TruncAndThinningParams# ttpars, |
| 694 | ScaleGnContributionFctcxx . . $ NCrystal/ScaleGnContributionFct# scaleGnContributionFct ) |
| 695 | { |
| 696 | //Hidden unofficial env-vars used for special debugging purposes: |
| 697 | auto getEnvInt = [](const char* name) { auto ev = getenv(name); return ev ? str2intcxx . . $ NCrystal/str2int(d52d3fef35838f95).(evcxx . . $ NCrystal/StrView#StrView(115afe5929091c94).) : 0; }; |
| 698 | auto getEnvDbl = [](const char* name) { auto ev = getenv(name); return ev ? str2dblcxx . . $ NCrystal/str2dbl(f35e4c19cd4153ad).(evcxx . . $ NCrystal/StrView#StrView(115afe5929091c94).) : 0.0; }; |
| 699 | const unsigned override_max_order = getEnvInt(cxx . . $ NCrystal/createScatteringKernel(df30a0497c822e3d).$anonymous_type_2#`operator()`(78f035161ffa9fe2)."NCRYSTAL_HACK_MAXORDER"); |
| 700 | const double override_alphamax = getEnvDbl(cxx . . $ NCrystal/createScatteringKernel(df30a0497c822e3d).$anonymous_type_3#`operator()`(3ed2397bf9a28c0)."NCRYSTAL_HACK_ALPHAMAX"); |
| 701 | const double override_betamax = getEnvDbl(cxx . . $ NCrystal/createScatteringKernel(df30a0497c822e3d).$anonymous_type_3#`operator()`(3ed2397bf9a28c0)."NCRYSTAL_HACK_BETAMAX"); |
| 702 | const unsigned override_nbins = getEnvInt(cxx . . $ NCrystal/createScatteringKernel(df30a0497c822e3d).$anonymous_type_2#`operator()`(78f035161ffa9fe2)."NCRYSTAL_HACK_NBINS"); |
| 703 | |
| 704 | //Which Emax should we target (i.e. aim to cover the kinematic reachable area |
| 705 | //for neutrons of that energy): |
| 706 | nc_assert_alwayscxx . . $ `ncrystal_core/include/NCrystal/NCException.hh:146:9`!( vdoslux <= 5 ); |
| 707 | nc_assert_alwayscxx . . $ `ncrystal_core/include/NCrystal/NCException.hh:146:9`!(targetEmax_requested>=0.0); |
| 708 | constexpr double lux2emax[6] = { 0.5, 1.0, 3.0, 5.0, 8.0, 12.0 };//Emax in eV for vdosluxs 0 to 5 |
| 709 | nc_assert_alwayscxx . . $ `ncrystal_core/include/NCrystal/NCException.hh:146:9`!( vdoslux < 6 ); |
| 710 | double targetEmax = targetEmax_requested>0.0 ? targetEmax_requested : lux2emax[vdoslux]; |
| 711 | |
| 712 | if (V2SKDetailcxx . . $ NCrystal/V2SKDetail/::s_verbosecxx . . $ NCrystal/V2SKDetail/s_verbose.) |
| 713 | stdcxx . . $ std/::coutcxx . . $ std/cout.<<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7)."NCrystal::VDOS2SK initialising with T="<<cxx . . $ NCrystal/`operator<<`(5b2d821ce67365f7).vdosdata.temperaturecxx . . $ NCrystal/VDOSData#temperature(36406a9e08e0f484).()<<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7).", vdoslux="<<cxx . . $ std/basic_ostream#`operator<<`(b4c055c363850233).vdoslux |
| 714 | <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7).", aiming for Emax="<<cxx . . $ std/basic_ostream#`operator<<`(968942d578224538).targetEmax<<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7)."eV"<<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7).(targetEmax_requested>0.0?" (as requested)":"")<<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7).", ..."<<cxx . . $ std/basic_ostream#`operator<<`(f46f7e25851d787a).stdcxx . . $ std/::endlcxx . . $ std/endl(345bf97a19cc83a9).; |
| 715 | |
| 716 | //Initialise evaluators: |
| 717 | VDOSEvalcxx . . $ NCrystal/VDOSEval# vdosevalcxx . . $ NCrystal/VDOSEval#VDOSEval(57d782bacb0f31cb).(vdosdata); |
| 718 | const double kT = vdoseval.kTcxx . . $ NCrystal/VDOSEval#kT(21b6c25e127938e2).(); |
| 719 | const double invkT = 1.0/kT; |
| 720 | const double gamma0 = vdoseval.calcGamma0cxx . . $ NCrystal/VDOSEval#calcGamma0(21b6c25e127938e2).(); |
| 721 | const double msd = vdoseval.getMSDcxx . . $ NCrystal/VDOSEval#getMSD(7e3a3c6b59947c6d).( gamma0 ); |
| 722 | double targetEmax_div_kT = targetEmax*invkT; |
| 723 | unsigned max_phonon_order = stdcxx . . $ std/::maxcxx . . $ std/max(2a338fce5842aa6a).<unsigned>(override_max_order,4); |
| 724 | VDOSGncxx . . $ NCrystal/VDOSGn# Gn_asymcxx . . $ NCrystal/VDOSGn#VDOSGn(c83571f59f0a6cc3).(vdoseval,ttpars); |
| 725 | Gn_asym.growMaxOrdercxx . . $ NCrystal/VDOSGn#growMaxOrder(f8e8bdb904e7aa5f).(max_phonon_ordercxx . . $ NCrystal/VDOSGn#Order#Order(cb2f890c2fdad230).); |
| 726 | |
| 727 | //What are the highest phonon order we allow? When user requested given target |
| 728 | //Emax, allow higher order expansions to accommodate. Otherwise, keep low to |
| 729 | //keep default initialisation times reasonable (with special extreme settings |
| 730 | //for vdoslux 0 and 5). |
| 731 | unsigned order_limit = 1000; |
| 732 | if ( targetEmax_requested>0.0 || vdoslux==5 ) |
| 733 | order_limit *= 10; |
| 734 | if ( vdoslux==0 ) |
| 735 | order_limit /= 10; |
| 736 | |
| 737 | const double emax_lowest_allowed = ( targetEmax_requested>0.0 ? targetEmax_requested : 1e-15 ); |
| 738 | |
| 739 | //Now increase order dynamically until the last order only has contributions |
| 740 | //to S(alpha,beta) outside the kinematic reach of Emax: |
| 741 | const double relcontriblvl = stdcxx . . $ std/::pow(10.0,-(3.0+2.0*vdoslux));//e.g.: 1e-3 for vdoslux 0, 1e-9 for vdoslux 3, 1e-13 for vdoslux 5 |
| 742 | const double x2alpha = 1.0 / V2SKDetailcxx . . $ NCrystal/V2SKDetail/::kTmsd_to_alpha2xcxx . . $ NCrystal/V2SKDetail/kTmsd_to_alpha2x(d8e7e0733fa41c87).(kT,msd); |
| 743 | auto findAlphaBetaRangeOfOrder = [&Gn_asym,x2alpha,invkT,relcontriblvl](unsigned n) { |
| 744 | auto eRange = Gn_asym.eRangecxx . . $ NCrystal/VDOSGn#eRange(25841f4212881612).(ncxx . . $ NCrystal/VDOSGn#Order#Order(cb2f890c2fdad230)., relcontriblvl); |
| 745 | PairDDcxx . . $ NCrystal/PairDD# betaRange( eRange.first * invkT, eRange.second * invkT ); |
| 746 | auto xRange = rangeXNexpMXcxx . . $ NCrystal/rangeXNexpMX(e3f0dbde301bb72f).( n, relcontriblvl ); |
| 747 | PairDDcxx . . $ NCrystal/PairDD# alphaRange( xRange.first * x2alpha, xRange.second * x2alpha ); |
| 748 | return stdcxx . . $ std/::make_pair(alphaRange,betaRange); |
| 749 | }; |
| 750 | while (true) { |
| 751 | if (override_max_order>0) |
| 752 | break; |
| 753 | Gn_asym.growMaxOrdercxx . . $ NCrystal/VDOSGn#growMaxOrder(f8e8bdb904e7aa5f).(max_phonon_ordercxx . . $ NCrystal/VDOSGn#Order#Order(cb2f890c2fdad230).); |
| 754 | PairDDcxx . . $ NCrystal/PairDD# alphaRange, betaRange; |
| 755 | stdcxx . . $ std/::tiecxx . . $ std/tie(988772b69a7ca893).(alphaRange, betaRange) =cxx . . $ std/tuple#`operator=`(18d19790a4ab438c). findAlphaBetaRangeOfOrder(cxx . . $ NCrystal/createScatteringKernel(df30a0497c822e3d).$anonymous_type_4#`operator()`(296613aaae2da969).Gn_asym.maxOrdercxx . . $ NCrystal/VDOSGn#maxOrder(422dc87a5e0c7f13).().valuecxx . . $ NCrystal/VDOSGn#Order#value(ddd8539a2fcdd4ce).()); |
| 756 | if (sabPointWithinAlphaPlusCurvecxx . . $ NCrystal/sabPointWithinAlphaPlusCurve(c3c51e0645da350e).(targetEmax_div_kT,alphaRange.first,betaRange.second)) { |
| 757 | ++max_phonon_order;//could consider larger stepsize, but need to carefully check usage in the following |
| 758 | } else { |
| 759 | break; |
| 760 | } |
| 761 | if (max_phonon_order>order_limit) { |
| 762 | //Too slow - unfeasible to fill out S(alpha,beta) all the way out to the |
| 763 | //kinematic curve for E=targetEmax. In this case it is better to reduce |
| 764 | //targetEmax, to at least get a consistent table (and hope the free-gas |
| 765 | //extrapolation mechanisms will be adequate already at this lower |
| 766 | //threshold). |
| 767 | double targetEmax_reduced = targetEmax; |
| 768 | do { |
| 769 | targetEmax_reduced *= 0.99; |
| 770 | if ( targetEmax_reduced < emax_lowest_allowed ) |
| 771 | NCRYSTAL_THROW2cxx . . $ `ncrystal_core/include/NCrystal/NCException.hh:110:9`!(CalcError,"VDOS expansion too slow - can not reach E="<<emax_lowest_allowed |
| 772 | <<"eV after "<<order_limit<<" phonon convolutions (likely causes: either the target energy" |
| 773 | " value is too high, vdoslux too low, the temperature too high, or the VDOS is very unusual)."); |
| 774 | } while (sabPointWithinAlphaPlusCurvecxx . . $ NCrystal/sabPointWithinAlphaPlusCurve(c3c51e0645da350e).(targetEmax_reduced*invkT,alphaRange.first,betaRange.second)); |
| 775 | if (V2SKDetailcxx . . $ NCrystal/V2SKDetail/::s_verbosecxx . . $ NCrystal/V2SKDetail/s_verbose.) |
| 776 | stdcxx . . $ std/::coutcxx . . $ std/cout.<<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7)."NCrystal::VDOS2SK Could only reach Emax="<<cxx . . $ std/basic_ostream#`operator<<`(968942d578224538).targetEmax_reduced<<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7)."eV and not the requested Emax="<<cxx . . $ std/basic_ostream#`operator<<`(968942d578224538).targetEmax<<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7)."K"<<cxx . . $ std/basic_ostream#`operator<<`(f46f7e25851d787a).stdcxx . . $ std/::endlcxx . . $ std/endl(345bf97a19cc83a9).; |
| 777 | targetEmax_div_kT = targetEmax_reduced * invkT; |
| 778 | targetEmax = targetEmax_reduced; |
| 779 | break; |
| 780 | } |
| 781 | } |
| 782 | nc_assert_alwayscxx . . $ `ncrystal_core/include/NCrystal/NCException.hh:146:9`!( targetEmax_requested==0.0 || targetEmax_requested == targetEmax ); |
| 783 | Gn_asym.growMaxOrdercxx . . $ NCrystal/VDOSGn#growMaxOrder(f8e8bdb904e7aa5f).(max_phonon_ordercxx . . $ NCrystal/VDOSGn#Order#Order(cb2f890c2fdad230).); |
| 784 | |
| 785 | //Ok, we now know how many orders we need to reach targetEmax. Next step is to |
| 786 | //look at the contribution of each order insided the kinematic reach of |
| 787 | //targetEmax, and use it to determine alpha/beta limits: |
| 788 | |
| 789 | double betaMin = 0.0; |
| 790 | double alphaMax = 0.0; |
| 791 | for ( unsigned n = 1; n<=max_phonon_order; ++n ) { |
| 792 | PairDDcxx . . $ NCrystal/PairDD# alphaRange, betaRange; |
| 793 | stdcxx . . $ std/::tiecxx . . $ std/tie(988772b69a7ca893).(alphaRange, betaRange) =cxx . . $ std/tuple#`operator=`(18d19790a4ab438c). findAlphaBetaRangeOfOrder(cxx . . $ NCrystal/createScatteringKernel(df30a0497c822e3d).$anonymous_type_4#`operator()`(296613aaae2da969).n); |
| 794 | auto ep = findExtremeSABPointWithinAlphaPlusCurvecxx . . $ NCrystal/findExtremeSABPointWithinAlphaPlusCurve(b39fe339a200bd0e).(targetEmax_div_kT, alphaRange, betaRange); |
| 795 | alphaMax = ncmaxcxx . . $ NCrystal/ncmax(d8e7e0733fa41c87).(alphaMax,ep.first); |
| 796 | betaMin = ncmincxx . . $ NCrystal/ncmin(d8e7e0733fa41c87).(betaMin,ep.second); |
| 797 | } |
| 798 | nc_assert_alwayscxx . . $ `ncrystal_core/include/NCrystal/NCException.hh:146:9`!(betaMin<0.0 && alphaMax > 0.0); |
| 799 | double upper_beta = -betaMin*1.01; |
| 800 | double upper_alpha = alphaMax*1.01; |
| 801 | if (override_alphamax) |
| 802 | upper_alpha = override_alphamax; |
| 803 | if (override_betamax) |
| 804 | upper_beta = override_betamax; |
| 805 | nc_assert_alwayscxx . . $ `ncrystal_core/include/NCrystal/NCException.hh:146:9`!( upper_beta>0.0 && upper_alpha>0.0 ); |
| 806 | |
| 807 | //Ok, time to setup the alpha/beta grids. The grid-spacing is not even, rather |
| 808 | //it attempts to best accomodate features of the distributions: |
| 809 | VectDcxx . . $ NCrystal/VectD# betaGrid = setupBetaGridcxx . . $ NCrystal/setupBetaGrid(dd420c3d5da75090).( Gn_asym, upper_beta, vdoslux, override_nbins ); |
| 810 | const unsigned alpha_size= ( override_nbins ? override_nbins : betaGrid.sizecxx . . $ std/vector#size(c9b1e749b64d5edc).()/2 ); |
| 811 | VectDcxx . . $ NCrystal/VectD# alphaGrid = setupAlphaGridcxx . . $ NCrystal/setupAlphaGrid(9ca19a1f3c97ba11).( kT, msd, upper_alpha, alpha_size ); |
| 812 | |
| 813 | //All done, now all that remains is to go through the (alpha,beta) pts in the |
| 814 | //grid and use Sjolander's II.28 equation to calculate S(alpha,beta) there as |
| 815 | //the sum of individual phonon orders: |
| 816 | auto sab = V2SKDetailcxx . . $ NCrystal/V2SKDetail/::fillSABFromVDOScxx . . $ NCrystal/V2SKDetail/fillSABFromVDOS(a55f273e0d1da178).( Gn_asym, msd, alphaGrid, betaGrid, scaleGnContributionFctcxx . . $ std/function#function(ec20bd20f4acba43). ); |
| 817 | |
| 818 | if (V2SKDetailcxx . . $ NCrystal/V2SKDetail/::s_verbosecxx . . $ NCrystal/V2SKDetail/s_verbose.) |
| 819 | stdcxx . . $ std/::coutcxx . . $ std/cout.<<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7)."NCrystal::VDOS2SK created SK with vdos expansion order N="<<cxx . . $ std/basic_ostream#`operator<<`(b4c055c363850233).max_phonon_order |
| 820 | <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7).", Emax="<<cxx . . $ std/basic_ostream#`operator<<`(968942d578224538).targetEmax<<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7)."eV, nalpha="<<cxx . . $ std/basic_ostream#`operator<<`(b09cd8ec4365c7ed).alphaGrid.sizecxx . . $ std/vector#size(c9b1e749b64d5edc).()<<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). " nbeta="<<cxx . . $ std/basic_ostream#`operator<<`(b09cd8ec4365c7ed).betaGrid.sizecxx . . $ std/vector#size(c9b1e749b64d5edc).()<<cxx . . $ std/basic_ostream#`operator<<`(f46f7e25851d787a).stdcxx . . $ std/::endlcxx . . $ std/endl(345bf97a19cc83a9).; |
| 821 | |
| 822 | ScatKnlDatacxx . . $ NCrystal/ScatKnlData# out; |
| 823 | out.alphaGridcxx . . $ NCrystal/ScatKnlData#alphaGrid. =cxx . . $ std/vector#`operator=`(1104576815caab1a). stdcxx . . $ std/::move(alphaGrid); |
| 824 | out.betaGridcxx . . $ NCrystal/ScatKnlData#betaGrid. =cxx . . $ std/vector#`operator=`(1104576815caab1a). stdcxx . . $ std/::move(betaGrid); |
| 825 | out.sabcxx . . $ NCrystal/ScatKnlData#sab. =cxx . . $ std/vector#`operator=`(1104576815caab1a). stdcxx . . $ std/::move(sab); |
| 826 | out.temperaturecxx . . $ NCrystal/ScatKnlData#temperature. =cxx . . $ NCrystal/Temperature#`operator=`(bbdab48d357cc2fa). vdoseval.temperaturecxx . . $ NCrystal/VDOSEval#temperature(36406a9e08e0f484).(); |
| 827 | out.boundXScxx . . $ NCrystal/ScatKnlData#boundXS. =cxx . . $ NCrystal/SigmaBound#`operator=`(4082ceb412330073). vdosdata.boundXScxx . . $ NCrystal/VDOSData#boundXS(de95084053c28635).(); |
| 828 | out.elementMassAMUcxx . . $ NCrystal/ScatKnlData#elementMassAMU. =cxx . . $ NCrystal/AtomMass#`operator=`(a24f046f0c3f35b5). vdosdata.elementMassAMUcxx . . $ NCrystal/VDOSData#elementMassAMU(4b88a82f62fd500a).(); |
| 829 | out.knltypecxx . . $ NCrystal/ScatKnlData#knltype. = ScatKnlDatacxx . . $ NCrystal/ScatKnlData#::KnlTypecxx . . $ NCrystal/ScatKnlData#KnlType#::SABcxx . . $ NCrystal/ScatKnlData#KnlType#SAB.; |
| 830 | out.suggestedEmaxcxx . . $ NCrystal/ScatKnlData#suggestedEmax. = targetEmax; |
| 831 | out.betaGridOptimisedcxx . . $ NCrystal/ScatKnlData#betaGridOptimised. = true;//prevent beta-thickening code upon conversion to SABData |
| 832 | return out; |
| 833 | } |