[376a3b] | 1 | /*
|
---|
| 2 | * Project: MoleCuilder
|
---|
| 3 | * Description: creates and alters molecular systems
|
---|
| 4 | * Copyright (C) 2012 University of Bonn. All rights reserved.
|
---|
[5aaa43] | 5 | * Copyright (C) 2013 Frederik Heber. All rights reserved.
|
---|
[376a3b] | 6 | *
|
---|
| 7 | *
|
---|
| 8 | * This file is part of MoleCuilder.
|
---|
| 9 | *
|
---|
| 10 | * MoleCuilder is free software: you can redistribute it and/or modify
|
---|
| 11 | * it under the terms of the GNU General Public License as published by
|
---|
| 12 | * the Free Software Foundation, either version 2 of the License, or
|
---|
| 13 | * (at your option) any later version.
|
---|
| 14 | *
|
---|
| 15 | * MoleCuilder is distributed in the hope that it will be useful,
|
---|
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 18 | * GNU General Public License for more details.
|
---|
| 19 | *
|
---|
| 20 | * You should have received a copy of the GNU General Public License
|
---|
| 21 | * along with MoleCuilder. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 22 | */
|
---|
| 23 |
|
---|
| 24 | /*
|
---|
[0cd8cf] | 25 | * FragmentationLongRangeResults.cpp
|
---|
[376a3b] | 26 | *
|
---|
| 27 | * Created on: Aug 31, 2012
|
---|
| 28 | * Author: heber
|
---|
| 29 | */
|
---|
| 30 |
|
---|
| 31 |
|
---|
| 32 | // include config.h
|
---|
| 33 | #ifdef HAVE_CONFIG_H
|
---|
| 34 | #include <config.h>
|
---|
| 35 | #endif
|
---|
| 36 |
|
---|
| 37 | #include "CodePatterns/MemDebug.hpp"
|
---|
| 38 |
|
---|
[0cd8cf] | 39 | #include "FragmentationLongRangeResults.hpp"
|
---|
[376a3b] | 40 |
|
---|
| 41 | #include <boost/mpl/for_each.hpp>
|
---|
| 42 | #include <boost/mpl/remove.hpp>
|
---|
| 43 | #include <sstream>
|
---|
| 44 |
|
---|
| 45 | #include "CodePatterns/Assert.hpp"
|
---|
| 46 | #include "CodePatterns/Log.hpp"
|
---|
| 47 |
|
---|
| 48 | #include "Fragmentation/KeySetsContainer.hpp"
|
---|
[fbf143] | 49 | #include "Fragmentation/parseKeySetFile.hpp"
|
---|
| 50 | #include "Fragmentation/Summation/Converter/DataConverter.hpp"
|
---|
| 51 | #include "Fragmentation/Summation/Containers/createMatrixNrLookup.hpp"
|
---|
| 52 | #include "Fragmentation/Summation/Containers/extractJobIds.hpp"
|
---|
[376a3b] | 53 | #include "Fragmentation/Summation/AllLevelOrthogonalSummator.hpp"
|
---|
| 54 | #include "Fragmentation/Summation/IndexSetContainer.hpp"
|
---|
| 55 | #include "Fragmentation/Summation/OrthogonalSumUpPerLevel.hpp"
|
---|
| 56 | #include "Fragmentation/Summation/SubsetMap.hpp"
|
---|
| 57 | #include "Fragmentation/Summation/SumUpPerLevel.hpp"
|
---|
| 58 |
|
---|
| 59 | #include "Helpers/defs.hpp"
|
---|
| 60 |
|
---|
[0cd8cf] | 61 | FragmentationLongRangeResults::FragmentationLongRangeResults(
|
---|
[376a3b] | 62 | const std::map<JobId_t,MPQCData> &fragmentData,
|
---|
[5a5196] | 63 | std::map<JobId_t,VMGData> &longrangeData,
|
---|
[bae7bc] | 64 | const KeySetsContainer& _KeySet,
|
---|
| 65 | const KeySetsContainer& _ForceKeySet) :
|
---|
| 66 | KeySet(_KeySet),
|
---|
[94db13] | 67 | ForceKeySet(_ForceKeySet),
|
---|
| 68 | hasForces((!longrangeData.empty()) && (longrangeData.begin()->second.hasForces))
|
---|
[5281ff] | 69 | {
|
---|
| 70 | initLookups(fragmentData, longrangeData);
|
---|
| 71 |
|
---|
| 72 | // convert KeySetContainer to IndexSetContainer
|
---|
| 73 | container.reset(new IndexSetContainer(KeySet));
|
---|
| 74 | // create the map of all keysets
|
---|
| 75 | subsetmap.reset(new SubsetMap(*container));
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | void FragmentationLongRangeResults::initLookups(
|
---|
| 79 | const std::map<JobId_t,MPQCData> &fragmentData,
|
---|
| 80 | std::map<JobId_t,VMGData> &longrangeData
|
---|
| 81 | )
|
---|
[376a3b] | 82 | {
|
---|
| 83 | // create lookup from job nr to fragment number
|
---|
[e0ae58d] | 84 | {
|
---|
| 85 | size_t MPQCFragmentCounter = 0;
|
---|
| 86 | std::vector<bool> ValueMask(fragmentData.size(), true);
|
---|
| 87 | const std::vector<JobId_t> mpqcjobids = extractJobIds<MPQCData>(fragmentData);
|
---|
| 88 | MPQCMatrixNrLookup =
|
---|
| 89 | createMatrixNrLookup(mpqcjobids, MPQCFragmentCounter, ValueMask);
|
---|
| 90 | }
|
---|
[376a3b] | 91 |
|
---|
[e0ae58d] | 92 | {
|
---|
| 93 | size_t VMGFragmentCounter = 0;
|
---|
| 94 | std::vector<bool> ValueMask(longrangeData.size(), true);
|
---|
| 95 | const std::vector<JobId_t> vmgjobids = extractJobIds<VMGData>(longrangeData);
|
---|
| 96 | VMGMatrixNrLookup =
|
---|
| 97 | createMatrixNrLookup(vmgjobids, VMGFragmentCounter, ValueMask);
|
---|
| 98 | }
|
---|
[2dd305] | 99 | }
|
---|
[376a3b] | 100 |
|
---|
[0cd8cf] | 101 | void FragmentationLongRangeResults::operator()(
|
---|
[2dd305] | 102 | const std::map<JobId_t,MPQCData> &fragmentData,
|
---|
| 103 | std::map<JobId_t,VMGData> &longrangeData,
|
---|
| 104 | const std::vector<VMGData> &fullsolutionData,
|
---|
[0ba27c9] | 105 | const std::vector<SamplingGrid> &full_sample,
|
---|
[45f4b6b] | 106 | const SamplingGrid &_zero_globalgrid,
|
---|
[0ba27c9] | 107 | const IndexedVectors::indices_t &_implicit_charges_indices)
|
---|
[2dd305] | 108 | {
|
---|
[19c50e] | 109 | MaxLevel = subsetmap->getMaximumSetLevel();
|
---|
| 110 | LOG(1, "INFO: Summing up results till level " << MaxLevel << ".");
|
---|
[376a3b] | 111 | /// convert all MPQCData to MPQCDataMap_t
|
---|
| 112 | {
|
---|
| 113 | ASSERT( ForceKeySet.KeySets.size() == fragmentData.size(),
|
---|
[0cd8cf] | 114 | "FragmentationLongRangeResults::FragmentationLongRangeResults() - ForceKeySet's KeySets and fragmentData differ in size.");
|
---|
[376a3b] | 115 |
|
---|
[409a05] | 116 | OrthogonalSumUpPerLevel<MPQCDataGridMap_t, MPQCData, MPQCDataGridVector_t>
|
---|
| 117 | chargesummation(fragmentData);
|
---|
[45f4b6b] | 118 | // charges and potential use same grid, hence same zero instance
|
---|
| 119 | chargesummation.setZeroInstance<MPQCDataFused::sampled_grid>(_zero_globalgrid);
|
---|
[409a05] | 120 | chargesummation(MPQCMatrixNrLookup, container, subsetmap, Result_Grid_fused,
|
---|
| 121 | Result_perIndexSet_Grid);
|
---|
[5a5196] | 122 |
|
---|
| 123 | // multiply each short-range potential with the respective charge
|
---|
| 124 | std::map<JobId_t,MPQCData>::const_iterator mpqciter = fragmentData.begin();
|
---|
| 125 | std::map<JobId_t,VMGData>::iterator vmgiter = longrangeData.begin();
|
---|
| 126 | for (; vmgiter != longrangeData.end(); ++mpqciter, ++vmgiter) {
|
---|
| 127 | vmgiter->second.sampled_potential *= mpqciter->second.sampled_grid;
|
---|
| 128 | }
|
---|
| 129 | // then sum up
|
---|
[409a05] | 130 | OrthogonalSumUpPerLevel<VMGDataMap_t, VMGData, VMGDataVector_t> vmgsummation(
|
---|
| 131 | longrangeData);
|
---|
| 132 | vmgsummation(VMGMatrixNrLookup, container, subsetmap,
|
---|
[b8f0b25] | 133 | Result_LongRange_fused, Result_perIndexSet_LongRange);
|
---|
[376a3b] | 134 |
|
---|
[ff347f] | 135 | IndexedVectors::indices_t fullindices;
|
---|
[94db13] | 136 | if (hasLongRangeForces()) {
|
---|
[da098d2] | 137 | // initialize zero instance map
|
---|
| 138 | VMGDataForceMap_t ZeroInstances;
|
---|
| 139 | ZeroInstanceInitializer<VMGDataForceMap_t> initZeroInstance(ZeroInstances);
|
---|
| 140 | boost::mpl::for_each<VMGDataForceVector_t>(boost::ref(initZeroInstance));
|
---|
| 141 |
|
---|
[94db13] | 142 | // force has extra data converter (this is similar to MPQCData's forces
|
---|
| 143 | std::map<JobId_t, VMGDataForceMap_t> VMGData_Force_fused;
|
---|
| 144 | convertDatatoForceMap<VMGData, VMGDataForceMap_t, VMGDataFused>(
|
---|
| 145 | longrangeData, ForceKeySet, VMGData_Force_fused);
|
---|
| 146 | Result_ForceLongRange_fused.resize(MaxLevel); // we need the results of correct size already
|
---|
| 147 | AllLevelOrthogonalSummator<VMGDataForceMap_t> forceSummer(
|
---|
| 148 | subsetmap,
|
---|
| 149 | VMGData_Force_fused,
|
---|
| 150 | container->getContainer(),
|
---|
| 151 | VMGMatrixNrLookup,
|
---|
| 152 | Result_ForceLongRange_fused,
|
---|
[da098d2] | 153 | Result_perIndexSet_LongRange_Force,
|
---|
| 154 | ZeroInstances);
|
---|
[94db13] | 155 | boost::mpl::for_each<VMGDataForceVector_t>(boost::ref(forceSummer));
|
---|
[ff347f] | 156 | // build full force index set
|
---|
| 157 | KeySetsContainer::ArrayOfIntVectors::const_iterator arrayiter = ForceKeySet.KeySets.begin();
|
---|
| 158 | std::set<IndexedVectors::index_t> sorted_indices;
|
---|
| 159 | for (;arrayiter != ForceKeySet.KeySets.end(); ++arrayiter) {
|
---|
| 160 | sorted_indices.insert(arrayiter->begin(), arrayiter->end());
|
---|
| 161 | }
|
---|
[0ba27c9] | 162 | // add additionally those from implicit charges which are not associated to
|
---|
| 163 | // any fragment and hence are unknown so far.
|
---|
| 164 | sorted_indices.insert(_implicit_charges_indices.begin(), _implicit_charges_indices.end());
|
---|
[ff347f] | 165 | sorted_indices.erase(-1);
|
---|
| 166 | fullindices.insert(fullindices.begin(), sorted_indices.begin(), sorted_indices.end());
|
---|
[94db13] | 167 | }
|
---|
| 168 |
|
---|
[83a425] | 169 | // then sum up
|
---|
[409a05] | 170 | OrthogonalSumUpPerLevel<VMGDataGridMap_t, VMGData, VMGDataGridVector_t> vmggridsummation(
|
---|
| 171 | longrangeData);
|
---|
[45f4b6b] | 172 | vmggridsummation.setZeroInstance<VMGDataFused::sampled_potential>(_zero_globalgrid);
|
---|
| 173 | vmggridsummation.setZeroInstance<VMGDataFused::both_sampled_potential>(_zero_globalgrid);
|
---|
[409a05] | 174 | vmggridsummation(VMGMatrixNrLookup, container, subsetmap,
|
---|
[83a425] | 175 | Result_GridLongRange_fused, Result_perIndexSet_LongRange_Grid);
|
---|
| 176 |
|
---|
[376a3b] | 177 | Result_LongRangeIntegrated_fused.reserve(MaxLevel);
|
---|
[d29b31] | 178 | // NOTE: potential for level 1 is wrongly calculated within a molecule
|
---|
| 179 | // as saturation hydrogen are not removed on this level yet
|
---|
| 180 | for (size_t level = 1; level <= MaxLevel; ++level) {
|
---|
[e2925fd] | 181 | // We have calculated three different contributions: e-e, e-n+n-n, and n-n.
|
---|
| 182 | // And we want to have e-e+e-n, n-n+n-e (where e-n = n-e).
|
---|
| 183 | // For each of these three contributions we have a full solution and summed
|
---|
| 184 | // up short range solutions.
|
---|
| 185 |
|
---|
| 186 | // first we obtain the full e-e energy as potential times charge on the
|
---|
| 187 | // respective level.
|
---|
[760fa22] | 188 | const SamplingGrid &charge_weight = full_sample[level-1];
|
---|
[d29b31] | 189 | SamplingGrid full_sample_solution = fullsolutionData[level-1].sampled_potential;
|
---|
[67fec1] | 190 | full_sample_solution *= charge_weight;
|
---|
[e2925fd] | 191 | double electron_solution_energy = full_sample_solution.integral();
|
---|
| 192 |
|
---|
| 193 | // then we subtract the summed-up short-range e-e interaction energy from
|
---|
| 194 | // the full solution.
|
---|
[376a3b] | 195 | const SamplingGrid &short_range_correction =
|
---|
[83a425] | 196 | boost::fusion::at_key<VMGDataFused::sampled_potential>(Result_GridLongRange_fused[level-1]);
|
---|
[e2925fd] | 197 | double electron_short_range_energy = short_range_correction.integral();
|
---|
| 198 | electron_solution_energy -= electron_short_range_energy;
|
---|
[760fa22] | 199 | #ifndef NDEBUG
|
---|
| 200 | {
|
---|
| 201 | static const double round_offset(
|
---|
| 202 | (std::numeric_limits<size_t>::round_style == std::round_toward_zero) ?
|
---|
| 203 | 0.5 : 0.); // need offset to get to round_toward_nearest behavior
|
---|
| 204 | // we can only check equivalence if both have same level, otherwise
|
---|
| 205 | // short_range_correction.integral() has higher precision because of finger grid
|
---|
| 206 | const int surplus_level = full_sample_solution.getSurplusLevel(short_range_correction)+round_offset;
|
---|
| 207 | if (full_sample_solution.level == short_range_correction.level+surplus_level) {
|
---|
| 208 | SamplingGrid check_difference_full_solution = full_sample_solution;
|
---|
| 209 | check_difference_full_solution -= short_range_correction;
|
---|
| 210 | ASSERT( fabs(electron_solution_energy - check_difference_full_solution.integral()) < 1e-7,
|
---|
| 211 | "FragmentationLongRangeResults::operator() - integral and energy are not exchangeable.");
|
---|
| 212 | }
|
---|
| 213 | }
|
---|
| 214 | #endif
|
---|
[e2925fd] | 215 |
|
---|
| 216 | // then, we obtain the e-n+n-n full solution in the same way
|
---|
[d29b31] | 217 | double nuclei_solution_energy = fullsolutionData[level-1].nuclei_long;
|
---|
[e2925fd] | 218 | double nuclei_short_range_energy =
|
---|
| 219 | boost::fusion::at_key<VMGDataFused::nuclei_long>(Result_LongRange_fused[level-1]);
|
---|
| 220 | nuclei_solution_energy -= nuclei_short_range_energy;
|
---|
| 221 |
|
---|
| 222 | // and also the e-n full solution
|
---|
[d29b31] | 223 | double both_solution_energy = fullsolutionData[level-1].electron_long;
|
---|
[e2925fd] | 224 | double both_short_range_energy =
|
---|
| 225 | boost::fusion::at_key<VMGDataFused::electron_long>(Result_LongRange_fused[level-1]);
|
---|
| 226 | both_solution_energy -= both_short_range_energy;
|
---|
| 227 |
|
---|
| 228 | // energies from interpolation at nuclei position has factor of 1/2 already
|
---|
| 229 | electron_solution_energy *= .5;
|
---|
| 230 | electron_short_range_energy *= .5;
|
---|
| 231 |
|
---|
| 232 | // At last, we subtract e-n from n-n+e-n for full solution and short-range
|
---|
| 233 | // correction.
|
---|
| 234 | nuclei_solution_energy -= both_solution_energy;
|
---|
| 235 | nuclei_short_range_energy -= both_short_range_energy;
|
---|
[376a3b] | 236 |
|
---|
| 237 | VMGDataLongRangeMap_t instance;
|
---|
[e2925fd] | 238 | boost::fusion::at_key<VMGDataFused::electron_longrange>(instance) = electron_solution_energy;
|
---|
[67fec1] | 239 | // LOG(0, "Remaining long-range potential integral of level " << level << " is "
|
---|
| 240 | // << full_sample_solution.integral() << ".");
|
---|
[e2925fd] | 241 | boost::fusion::at_key<VMGDataFused::electron_shortrange>(instance) = electron_short_range_energy;
|
---|
[67fec1] | 242 | // LOG(0, "Short-range correction potential integral of level " << level << " is "
|
---|
| 243 | // << short_range_correction.integral() << ".");
|
---|
[e2925fd] | 244 | boost::fusion::at_key<VMGDataFused::mixed_longrange>(instance) = both_solution_energy;
|
---|
| 245 | // LOG(0, "Remaining long-range energy from potential integral of level " << level << " is "
|
---|
| 246 | // << full_solution_energy << ".");
|
---|
| 247 | boost::fusion::at_key<VMGDataFused::mixed_shortrange>(instance) = both_short_range_energy;
|
---|
| 248 | // LOG(0, "Short-range correction energy from potential integral of level " << level << " is "
|
---|
| 249 | // << short_range_energy << ".");
|
---|
| 250 | boost::fusion::at_key<VMGDataFused::nuclei_longrange>(instance) = nuclei_solution_energy;
|
---|
[67fec1] | 251 | // LOG(0, "Remaining long-range energy from potential integral of level " << level << " is "
|
---|
| 252 | // << full_solution_energy << ".");
|
---|
[e2925fd] | 253 | boost::fusion::at_key<VMGDataFused::nuclei_shortrange>(instance) = nuclei_short_range_energy;
|
---|
[67fec1] | 254 | // LOG(0, "Short-range correction energy from potential integral of level " << level << " is "
|
---|
| 255 | // << short_range_energy << ".");
|
---|
| 256 | boost::fusion::at_key<VMGDataFused::total_longrange>(instance) =
|
---|
[e2925fd] | 257 | boost::fusion::at_key<VMGDataFused::electron_longrange>(instance)
|
---|
| 258 | + 2.*boost::fusion::at_key<VMGDataFused::mixed_longrange>(instance)
|
---|
| 259 | + boost::fusion::at_key<VMGDataFused::nuclei_longrange>(instance);
|
---|
[67fec1] | 260 | boost::fusion::at_key<VMGDataFused::total_shortrange>(instance) =
|
---|
[e2925fd] | 261 | boost::fusion::at_key<VMGDataFused::electron_shortrange>(instance)
|
---|
| 262 | + 2.*boost::fusion::at_key<VMGDataFused::mixed_shortrange>(instance)
|
---|
| 263 | + boost::fusion::at_key<VMGDataFused::nuclei_shortrange>(instance);
|
---|
[376a3b] | 264 | Result_LongRangeIntegrated_fused.push_back(instance);
|
---|
[ff347f] | 265 |
|
---|
| 266 | if (hasLongRangeForces()) {
|
---|
| 267 | VMGDataLongRangeForceMap_t forceinstance;
|
---|
| 268 | IndexedVectors fullforces( fullindices, fullsolutionData[level-1].forces);
|
---|
| 269 | IndexedVectors longrangeforces =
|
---|
| 270 | boost::fusion::at_key<VMGDataFused::forces>(Result_ForceLongRange_fused[level-1]);
|
---|
| 271 | boost::fusion::at_key<VMGDataFused::forces_shortrange>(forceinstance) =
|
---|
| 272 | fullforces;
|
---|
| 273 | fullforces -= longrangeforces;
|
---|
| 274 | boost::fusion::at_key<VMGDataFused::forces_longrange>(forceinstance) =
|
---|
| 275 | fullforces;
|
---|
| 276 | Result_ForcesLongRangeIntegrated_fused.push_back(forceinstance);
|
---|
| 277 | }
|
---|
[376a3b] | 278 | }
|
---|
| 279 | // {
|
---|
| 280 | // // LOG(0, "Remaining long-range energy from energy_potential is " << full_sample_solution.integral()-epotentialSummer.getFullContribution() << ".");
|
---|
| 281 | // SamplingGrid full_sample_solution = fullsolutionData.back().sampled_potential;
|
---|
| 282 | // const SamplingGrid &short_range_correction =
|
---|
[83a425] | 283 | // boost::fusion::at_key<VMGDataFused::sampled_potential>(Result_GridLongRange_fused.back()).getFullContribution();
|
---|
[376a3b] | 284 | // full_sample_solution -= short_range_correction;
|
---|
| 285 | // // multiply element-wise with charge distribution
|
---|
| 286 | // LOG(0, "Remaining long-range potential integral is " << full_sample_solution.integral() << ".");
|
---|
| 287 | // LOG(0, "Short-range correction potential integral of level is " << short_range_correction.integral() << ".");
|
---|
| 288 | // LOG(0, "Remaining long-range energy from potential integral is "
|
---|
| 289 | // << full_sample_solution.integral(full_sample.back()) << ".");
|
---|
| 290 | // LOG(0, "Short-range correction energy from potential integral is "
|
---|
| 291 | // << short_range_correction.integral(full_sample.back()) << ".");
|
---|
| 292 | //
|
---|
| 293 | // double e_long = fullsolutionData.back().e_long;
|
---|
| 294 | // e_long -= boost::fusion::at_key<VMGDataFused::energy_long>(Result_LongRange_fused.back()).getFullContribution();
|
---|
| 295 | // LOG(0, "Remaining long-range energy is " << e_long << ".");
|
---|
| 296 | // }
|
---|
| 297 |
|
---|
| 298 | // TODO: Extract long-range corrections to forces
|
---|
| 299 | // NOTE: potential is in atomic length units, NOT IN ANGSTROEM!
|
---|
| 300 |
|
---|
| 301 | }
|
---|
| 302 | }
|
---|