| 1 | ////////////////////////////////////////////////////////////////////////////////cxx . . $ `<file>/ncrystal_core/src/NCSANSUtils.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/NCSANSUtils.hh"cxx . . $ `<file>/ncrystal_core/include/NCrystal/internal/NCSANSUtils.hh`/ |
| 22 | #include "NCrystal/internal/NCMath.hh"cxx . . $ `<file>/ncrystal_core/include/NCrystal/internal/NCMath.hh`/ |
| 23 | #include "NCrystal/NCInfo.hh"cxx . . $ `<file>/ncrystal_core/include/NCrystal/NCInfo.hh`/ |
| 24 | #include "NCrystal/internal/NCCfgManip.hh"cxx . . $ `<file>/ncrystal_core/include/NCrystal/internal/NCCfgManip.hh`/ |
| 25 | |
| 26 | namespace NC = NCrystal; |
| 27 | |
| 28 | namespace NCrystalcxx . . $ NCrystal/ { |
| 29 | namespacecxx . . $ NCrystal/`$anonymous_namespace_ncrystal_core/src/NCSANSUtils.cc`/ { |
| 30 | |
| 31 | const Infocxx . . $ NCrystal/Info#::CustomSectionDatacxx . . $ NCrystal/Info#CustomSectionData#* getATypePhaseDatacxx . . $ NCrystal/`$anonymous_namespace_ncrystal_core/src/NCSANSUtils.cc`/getATypePhaseData(5c2d02c4a344e271).( const Infocxx . . $ NCrystal/Info#& info, const stdcxx . . $ std/::stringcxx . . $ std/string#& customsectionname ) |
| 32 | { |
| 33 | //NB: We don't currently allow A-type phases (the ones with the custom data) to be themselves multiphase. |
| 34 | nc_assert_alwayscxx . . $ `ncrystal_core/include/NCrystal/NCException.hh:146:9`!(!info.isMultiPhase()); |
| 35 | auto n = info.countCustomSectionscxx . . $ NCrystal/Info#countCustomSections(f0a3e59d959dd0c2).(customsectionname); |
| 36 | if ( n>1 ) |
| 37 | NCRYSTAL_THROW2cxx . . $ `ncrystal_core/include/NCrystal/NCException.hh:110:9`!(BadInput,"Info object (from "<<info.getDataSourceName() |
| 38 | <<") contains more than one @CUSTOM_"<<customsectionname<<" section"); |
| 39 | //Respect if sub-phases have ";sans=0" (to e.g. allow usage of a nanodiamond subphase where sans is disabled). |
| 40 | return ( ( n && Cfgcxx . . $ NCrystal/Cfg/::CfgManipcxx . . $ NCrystal/Cfg/CfgManip#::get_sanscxx . . $ NCrystal/Cfg/CfgManip#get_sans(10829b163c78f60).(info.getCfgDatacxx . . $ NCrystal/Info#getCfgData(cbb5a0ec4b12042d).()) ) |
| 41 | ? &info.getCustomSectioncxx . . $ NCrystal/Info#getCustomSection(286949c9cb37c9a3).(customsectionname) |
| 42 | : nullptr ); |
| 43 | } |
| 44 | |
| 45 | using CSPDResultsVectcxx . . $ NCrystal/`$anonymous_namespace_ncrystal_core/src/NCSANSUtils.cc`/CSPDResultsVect# = SmallVectorcxx . . $ NCrystal/SmallVector#<CustomSansPluginDatacxx . . $ NCrystal/CustomSansPluginData#,2>; |
| 46 | enum class SearchModecxx . . $ NCrystal/`$anonymous_namespace_ncrystal_core/src/NCSANSUtils.cc`/SearchMode# { Normalcxx . . $ NCrystal/`$anonymous_namespace_ncrystal_core/src/NCSANSUtils.cc`/SearchMode#Normal., JustCheckIfPresentcxx . . $ NCrystal/`$anonymous_namespace_ncrystal_core/src/NCSANSUtils.cc`/SearchMode#JustCheckIfPresent. }; |
| 47 | |
| 48 | CSPDResultsVectcxx . . $ NCrystal/`$anonymous_namespace_ncrystal_core/src/NCSANSUtils.cc`/CSPDResultsVect# extractCDSANSHelpercxx . . $ NCrystal/`$anonymous_namespace_ncrystal_core/src/NCSANSUtils.cc`/extractCDSANSHelper(84230b3047b44af0).( const Infocxx . . $ NCrystal/Info#::PhaseListcxx . . $ NCrystal/Info#PhaseList#& phases, |
| 49 | const stdcxx . . $ std/::stringcxx . . $ std/string#& customsectionname, |
| 50 | double overallScaleFactor = 1.0, |
| 51 | SearchModecxx . . $ NCrystal/`$anonymous_namespace_ncrystal_core/src/NCSANSUtils.cc`/SearchMode# searchMode = SearchModecxx . . $ NCrystal/`$anonymous_namespace_ncrystal_core/src/NCSANSUtils.cc`/SearchMode#::Normalcxx . . $ NCrystal/`$anonymous_namespace_ncrystal_core/src/NCSANSUtils.cc`/SearchMode#Normal. ) |
| 52 | { |
| 53 | CSPDResultsVectcxx . . $ NCrystal/`$anonymous_namespace_ncrystal_core/src/NCSANSUtils.cc`/CSPDResultsVect# rescxx . . $ NCrystal/SmallVector#SmallVector(ced63f7c635d850d).; |
| 54 | |
| 55 | //Total number density normalisation: |
| 56 | StableSumcxx . . $ NCrystal/StableSum# sum_nd; |
| 57 | for ( auto& ph : phases ) |
| 58 | sum_nd.addcxx . . $ NCrystal/StableSum#add(ba8836b59fb42830).( ph.first * ph.second->cxx . . $ NCrystal/shared_obj#`operator->`(8e614c7de0b1573e).getNumberDensitycxx . . $ NCrystal/Info#getNumberDensity(f548113181a3314a).().dblcxx . . $ NCrystal/EncapsulatedValue#dbl(683ba587bcaad6a5).() ); |
| 59 | nc_assertcxx . . $ `ncrystal_core/include/NCrystal/NCException.hh:153:11`!( sum_nd.sum() > 0.0 ); |
| 60 | const double inv_total_nd = 1.0 / sum_nd.sumcxx . . $ NCrystal/StableSum#sum(21b6c25e127938e2).(); |
| 61 | |
| 62 | |
| 63 | //Loop through phases, looking for A-type (the ones with the custom data) |
| 64 | //and B-type (the ones right after) pairs. Additionally, dive down |
| 65 | //recursively into any phases which are themselves multiphase: |
| 66 | |
| 67 | auto it = phases.begincxx . . $ std/vector#begin(d1e1699016b9d7e6).(); |
| 68 | const auto itE = phases.endcxx . . $ std/vector#end(d1e1699016b9d7e6).(); |
| 69 | struct ATypecxx . . $ NCrystal/`$anonymous_namespace_ncrystal_core/src/NCSANSUtils.cc`/extractCDSANSHelper(84230b3047b44af0).AType# { |
| 70 | ScatLenDensitycxx . . $ NCrystal/ScatLenDensity# sldcxx . . $ NCrystal/`$anonymous_namespace_ncrystal_core/src/NCSANSUtils.cc`/extractCDSANSHelper(84230b3047b44af0).AType#sld.; |
| 71 | NumberDensitycxx . . $ NCrystal/NumberDensity# ndcxx . . $ NCrystal/`$anonymous_namespace_ncrystal_core/src/NCSANSUtils.cc`/extractCDSANSHelper(84230b3047b44af0).AType#nd.; |
| 72 | double volfraccxx . . $ NCrystal/`$anonymous_namespace_ncrystal_core/src/NCSANSUtils.cc`/extractCDSANSHelper(84230b3047b44af0).AType#volfrac.; |
| 73 | const Infocxx . . $ NCrystal/Info#::CustomSectionDatacxx . . $ NCrystal/Info#CustomSectionData#* customDatacxx . . $ NCrystal/`$anonymous_namespace_ncrystal_core/src/NCSANSUtils.cc`/extractCDSANSHelper(84230b3047b44af0).AType#customData. = nullptr; |
| 74 | }; |
| 75 | Optionalcxx . . $ NCrystal/Optional#<ATypecxx . . $ NCrystal/`$anonymous_namespace_ncrystal_core/src/NCSANSUtils.cc`/extractCDSANSHelper(84230b3047b44af0).AType#> currentATypecxx . . $ NCrystal/Optional#Optional(ced63f7c635d850d).; |
| 76 | decltype(it) itATypeData = itE; |
| 77 | for ( ; it!=itE; ++it ) { |
| 78 | double info_volfrac = it->first; |
| 79 | const Infocxx . . $ NCrystal/Info#& info_ph = *cxx . . $ NCrystal/shared_obj#`operator*`(2042464fc1fea03e).it->second; |
| 80 | |
| 81 | auto aTypeCData = info_ph.isMultiPhasecxx . . $ NCrystal/Info#isMultiPhase(50ce9a9e25b4a850).() ? nullptr : getATypePhaseDatacxx . . $ NCrystal/`$anonymous_namespace_ncrystal_core/src/NCSANSUtils.cc`/getATypePhaseData(5c2d02c4a344e271).(it->second,customsectionname); |
| 82 | if ( currentAType.has_valuecxx . . $ NCrystal/Optional#has_value(3482b152b9333168).() ) { |
| 83 | if ( aTypeCData ) |
| 84 | NCRYSTAL_THROW2cxx . . $ `ncrystal_core/include/NCrystal/NCException.hh:110:9`!(BadInput,"Two consecutive entries in the phase list both contain @CUSTOM_"<<customsectionname<<" sections"); |
| 85 | nc_assertcxx . . $ `ncrystal_core/include/NCrystal/NCException.hh:153:11`!(currentAType.value().customData!=nullptr); |
| 86 | const double fracA = currentAType.valuecxx . . $ NCrystal/Optional#value(80ef4fb0f39170b5).().volfraccxx . . $ NCrystal/`$anonymous_namespace_ncrystal_core/src/NCSANSUtils.cc`/extractCDSANSHelper(84230b3047b44af0).AType#volfrac.; |
| 87 | const double fracB = info_volfrac; |
| 88 | nc_assert_alwayscxx . . $ `ncrystal_core/include/NCrystal/NCException.hh:146:9`!( fracA > 0.0 && fracB > 0.0 ); |
| 89 | const auto sldA = currentAType.valuecxx . . $ NCrystal/Optional#value(80ef4fb0f39170b5).().sldcxx . . $ NCrystal/`$anonymous_namespace_ncrystal_core/src/NCSANSUtils.cc`/extractCDSANSHelper(84230b3047b44af0).AType#sld.; |
| 90 | const auto sldB = info_ph.getSLDcxx . . $ NCrystal/Info#getSLD(13502084af802655).(); |
| 91 | const auto drho = sldA.contrastcxx . . $ NCrystal/ScatLenDensity#contrast(9c4481fcabc40146).( sldB ); |
| 92 | const auto numberDensity = NumberDensitycxx . . $ NCrystal/NumberDensity#{ fracA * currentAType.valuecxx . . $ NCrystal/Optional#value(80ef4fb0f39170b5).().ndcxx . . $ NCrystal/`$anonymous_namespace_ncrystal_core/src/NCSANSUtils.cc`/extractCDSANSHelper(84230b3047b44af0).AType#nd..dblcxx . . $ NCrystal/EncapsulatedValue#dbl(683ba587bcaad6a5).() + fracB * info_ph.getNumberDensitycxx . . $ NCrystal/Info#getNumberDensity(f548113181a3314a).().dblcxx . . $ NCrystal/EncapsulatedValue#dbl(683ba587bcaad6a5).() }; |
| 93 | |
| 94 | //For phi, we simply assume that the scattering objects are the |
| 95 | //smaller (in terms of volume) of the two involved phases. After all, |
| 96 | //the theoretical SANS models are anyway unlikely to work for samples |
| 97 | //where the scattering objects occupy more volumes than the solution |
| 98 | //(e.g. if >50% of the volume is occupied by spheres it is unlikely |
| 99 | //that approximations regarding diluteness etc. would still hold). |
| 100 | double phi = stdcxx . . $ std/::mincxx . . $ std/min(2a338fce5842aa6a).<double>( fracA, fracB ) / ( fracA+fracB ); |
| 101 | |
| 102 | //Absorp any extra scale factors (<1.0 if the two phases in question |
| 103 | //are not the only phases) into phi: |
| 104 | phi *= overallScaleFactor; |
| 105 | if ( phases.sizecxx . . $ std/vector#size(c9b1e749b64d5edc).() != 2 ) |
| 106 | phi *= numberDensity.dblcxx . . $ NCrystal/EncapsulatedValue#dbl(2395dd9bcca88783).() * inv_total_nd; |
| 107 | |
| 108 | //Record the entry (if not obviously vanishing): |
| 109 | auto sansScaleFactor = SANSScaleFactorcxx . . $ NCrystal/SANSScaleFactor#SANSScaleFactor(a467ad407dd80171).( drho, numberDensity, phi ); |
| 110 | if ( sansScaleFactor.dblcxx . . $ NCrystal/EncapsulatedValue#dbl(683ba587bcaad6a5).() > 0 ) { |
| 111 | res.push_backcxx . . $ NCrystal/SmallVector#push_back(fe7a8a4d1eddb70).( { sansScaleFactor, {} } ); |
| 112 | if ( searchMode != SearchModecxx . . $ NCrystal/`$anonymous_namespace_ncrystal_core/src/NCSANSUtils.cc`/SearchMode#::JustCheckIfPresentcxx . . $ NCrystal/`$anonymous_namespace_ncrystal_core/src/NCSANSUtils.cc`/SearchMode#JustCheckIfPresent. ) |
| 113 | res.backcxx . . $ NCrystal/SmallVector#back(80ef4fb0f39170b5).().customDatacxx . . $ NCrystal/CustomSansPluginData#customData. =cxx . . $ std/vector#`operator=`(e6b68e61becad66f). *(currentAType.valuecxx . . $ NCrystal/Optional#value(80ef4fb0f39170b5).().customDatacxx . . $ NCrystal/`$anonymous_namespace_ncrystal_core/src/NCSANSUtils.cc`/extractCDSANSHelper(84230b3047b44af0).AType#customData.);//copy the whole thing, no life-time hassle for plugin developers |
| 114 | } |
| 115 | currentAType =cxx . . $ NCrystal/Optional#`operator=`(f491e5e41fcfed39). NullOptcxx . . $ NCrystal/NullOpt.; |
| 116 | } else { |
| 117 | if ( aTypeCData ) { |
| 118 | itATypeData = it; |
| 119 | currentAType =cxx . . $ NCrystal/Optional#`operator=`(cb95957a70e0a527). ATypecxx . . $ NCrystal/`$anonymous_namespace_ncrystal_core/src/NCSANSUtils.cc`/extractCDSANSHelper(84230b3047b44af0).AType#{}; |
| 120 | currentAType.valuecxx . . $ NCrystal/Optional#value(80ef4fb0f39170b5).().sldcxx . . $ NCrystal/`$anonymous_namespace_ncrystal_core/src/NCSANSUtils.cc`/extractCDSANSHelper(84230b3047b44af0).AType#sld. =cxx . . $ NCrystal/ScatLenDensity#`operator=`(d50c1c0143ff9b14). info_ph.getSLDcxx . . $ NCrystal/Info#getSLD(13502084af802655).(); |
| 121 | currentAType.valuecxx . . $ NCrystal/Optional#value(80ef4fb0f39170b5).().ndcxx . . $ NCrystal/`$anonymous_namespace_ncrystal_core/src/NCSANSUtils.cc`/extractCDSANSHelper(84230b3047b44af0).AType#nd. =cxx . . $ NCrystal/NumberDensity#`operator=`(c32902712942f053). info_ph.getNumberDensitycxx . . $ NCrystal/Info#getNumberDensity(f548113181a3314a).(); |
| 122 | currentAType.valuecxx . . $ NCrystal/Optional#value(80ef4fb0f39170b5).().volfraccxx . . $ NCrystal/`$anonymous_namespace_ncrystal_core/src/NCSANSUtils.cc`/extractCDSANSHelper(84230b3047b44af0).AType#volfrac. = info_volfrac; |
| 123 | currentAType.valuecxx . . $ NCrystal/Optional#value(80ef4fb0f39170b5).().customDatacxx . . $ NCrystal/`$anonymous_namespace_ncrystal_core/src/NCSANSUtils.cc`/extractCDSANSHelper(84230b3047b44af0).AType#customData. = aTypeCData; |
| 124 | } |
| 125 | } |
| 126 | if ( info_ph.isMultiPhasecxx . . $ NCrystal/Info#isMultiPhase(50ce9a9e25b4a850).() ) { |
| 127 | //Search recursively for relevant entries as well, so plugin continues |
| 128 | //to work when material is used in a composition. |
| 129 | auto subv = extractCDSANSHelpercxx . . $ NCrystal/`$anonymous_namespace_ncrystal_core/src/NCSANSUtils.cc`/extractCDSANSHelper(84230b3047b44af0).( info_ph.getPhasescxx . . $ NCrystal/Info#getPhases(17c010ce7fff4a1d).(), |
| 130 | customsectionname, |
| 131 | overallScaleFactor * ( info_volfrac * info_ph.getNumberDensitycxx . . $ NCrystal/Info#getNumberDensity(f548113181a3314a).().dblcxx . . $ NCrystal/EncapsulatedValue#dbl(683ba587bcaad6a5).() * inv_total_nd ), |
| 132 | searchMode ); |
| 133 | for ( auto& e : subv ) |
| 134 | res.push_backcxx . . $ NCrystal/SmallVector#push_back(fe7a8a4d1eddb70).( stdcxx . . $ std/::move(e) ); |
| 135 | } |
| 136 | |
| 137 | } |
| 138 | if ( currentAType.has_valuecxx . . $ NCrystal/Optional#has_value(3482b152b9333168).()) |
| 139 | NCRYSTAL_THROW2cxx . . $ `ncrystal_core/include/NCrystal/NCException.hh:110:9`!(BadInput,"The phase with a @CUSTOM_"<<customsectionname |
| 140 | <<" section must always be followed by another phase (which provides the contrast)."); |
| 141 | |
| 142 | return rescxx . . $ NCrystal/SmallVector#SmallVector(bba552b83b562564).; |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | bool NC::hasCustomDataForSANSPlugincxx . . $ NCrystal/hasCustomDataForSANSPlugin(fffab82593f75f37).( const Infocxx . . $ NCrystal/Info#& info, const stdcxx . . $ std/::stringcxx . . $ std/string#& customsectionname ) |
| 148 | { |
| 149 | nc_assert_alwayscxx . . $ `ncrystal_core/include/NCrystal/NCException.hh:146:9`!(!customsectionname.empty()); |
| 150 | return ( info.isMultiPhasecxx . . $ NCrystal/Info#isMultiPhase(50ce9a9e25b4a850).() |
| 151 | && !extractCDSANSHelpercxx . . $ NCrystal/`$anonymous_namespace_ncrystal_core/src/NCSANSUtils.cc`/extractCDSANSHelper(84230b3047b44af0).( info.getPhasescxx . . $ NCrystal/Info#getPhases(17c010ce7fff4a1d).(), customsectionname, 1.0, SearchModecxx . . $ NCrystal/`$anonymous_namespace_ncrystal_core/src/NCSANSUtils.cc`/SearchMode#::JustCheckIfPresentcxx . . $ NCrystal/`$anonymous_namespace_ncrystal_core/src/NCSANSUtils.cc`/SearchMode#JustCheckIfPresent. ).emptycxx . . $ NCrystal/SmallVector#empty(3482b152b9333168).() ); |
| 152 | } |
| 153 | |
| 154 | stdcxx . . $ std/::vectorcxx . . $ std/vector#<NC::CustomSansPluginDatacxx . . $ NCrystal/CustomSansPluginData#> NC::extractCustomDataForSANSPlugincxx . . $ NCrystal/extractCustomDataForSANSPlugin(d4fbb2c35b2e9902).( const Infocxx . . $ NCrystal/Info#& info, |
| 155 | const stdcxx . . $ std/::stringcxx . . $ std/string#& customsectionname ) |
| 156 | { |
| 157 | nc_assert_alwayscxx . . $ `ncrystal_core/include/NCrystal/NCException.hh:146:9`!(!customsectionname.empty()); |
| 158 | stdcxx . . $ std/::vectorcxx . . $ std/vector#<NC::CustomSansPluginDatacxx . . $ NCrystal/CustomSansPluginData#> rescxx . . $ std/vector#vector(49f6e7a06ebc5aa8).; |
| 159 | if (!info.isMultiPhasecxx . . $ NCrystal/Info#isMultiPhase(50ce9a9e25b4a850).()) |
| 160 | return rescxx . . $ std/vector#vector(6f559d22b039ccdb).; |
| 161 | auto v = extractCDSANSHelpercxx . . $ NCrystal/`$anonymous_namespace_ncrystal_core/src/NCSANSUtils.cc`/extractCDSANSHelper(84230b3047b44af0).( info.getPhasescxx . . $ NCrystal/Info#getPhases(17c010ce7fff4a1d).(), |
| 162 | customsectionname ); |
| 163 | if ( !v.emptycxx . . $ NCrystal/SmallVector#empty(3482b152b9333168).() ) { |
| 164 | res.reservecxx . . $ std/vector#reserve(2c44cd906ae4e829).(v.sizecxx . . $ NCrystal/SmallVector#size(251b5e0f3277503e).()); |
| 165 | for ( auto& e : v ) |
| 166 | res.push_backcxx . . $ std/vector#push_back(fe7a8a4d1eddb70).( stdcxx . . $ std/::move(e) ); |
| 167 | } |
| 168 | |
| 169 | return rescxx . . $ std/vector#vector(6f559d22b039ccdb).; |
| 170 | } |