| [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), | 
|---|
|  | 67 | ForceKeySet(_ForceKeySet) | 
|---|
| [5281ff] | 68 | { | 
|---|
|  | 69 | initLookups(fragmentData, longrangeData); | 
|---|
|  | 70 |  | 
|---|
|  | 71 | // convert KeySetContainer to IndexSetContainer | 
|---|
|  | 72 | container.reset(new IndexSetContainer(KeySet)); | 
|---|
|  | 73 | // create the map of all keysets | 
|---|
|  | 74 | subsetmap.reset(new SubsetMap(*container)); | 
|---|
|  | 75 | } | 
|---|
|  | 76 |  | 
|---|
|  | 77 | void FragmentationLongRangeResults::initLookups( | 
|---|
|  | 78 | const std::map<JobId_t,MPQCData> &fragmentData, | 
|---|
|  | 79 | std::map<JobId_t,VMGData> &longrangeData | 
|---|
|  | 80 | ) | 
|---|
| [376a3b] | 81 | { | 
|---|
|  | 82 | // create lookup from job nr to fragment number | 
|---|
| [e0ae58d] | 83 | { | 
|---|
|  | 84 | size_t MPQCFragmentCounter = 0; | 
|---|
|  | 85 | std::vector<bool> ValueMask(fragmentData.size(), true); | 
|---|
|  | 86 | const std::vector<JobId_t> mpqcjobids = extractJobIds<MPQCData>(fragmentData); | 
|---|
|  | 87 | MPQCMatrixNrLookup = | 
|---|
|  | 88 | createMatrixNrLookup(mpqcjobids, MPQCFragmentCounter, ValueMask); | 
|---|
|  | 89 | } | 
|---|
| [376a3b] | 90 |  | 
|---|
| [e0ae58d] | 91 | { | 
|---|
|  | 92 | size_t VMGFragmentCounter = 0; | 
|---|
|  | 93 | std::vector<bool> ValueMask(longrangeData.size(), true); | 
|---|
|  | 94 | const std::vector<JobId_t> vmgjobids = extractJobIds<VMGData>(longrangeData); | 
|---|
|  | 95 | VMGMatrixNrLookup = | 
|---|
|  | 96 | createMatrixNrLookup(vmgjobids, VMGFragmentCounter, ValueMask); | 
|---|
|  | 97 | } | 
|---|
| [2dd305] | 98 | } | 
|---|
| [376a3b] | 99 |  | 
|---|
| [0cd8cf] | 100 | void FragmentationLongRangeResults::operator()( | 
|---|
| [2dd305] | 101 | const std::map<JobId_t,MPQCData> &fragmentData, | 
|---|
|  | 102 | std::map<JobId_t,VMGData> &longrangeData, | 
|---|
|  | 103 | const std::vector<VMGData> &fullsolutionData, | 
|---|
|  | 104 | const std::vector<SamplingGrid> &full_sample) | 
|---|
|  | 105 | { | 
|---|
| [19c50e] | 106 | MaxLevel = subsetmap->getMaximumSetLevel(); | 
|---|
|  | 107 | LOG(1, "INFO: Summing up results till level " << MaxLevel << "."); | 
|---|
| [376a3b] | 108 | /// convert all MPQCData to MPQCDataMap_t | 
|---|
|  | 109 | { | 
|---|
|  | 110 | ASSERT( ForceKeySet.KeySets.size() == fragmentData.size(), | 
|---|
| [0cd8cf] | 111 | "FragmentationLongRangeResults::FragmentationLongRangeResults() - ForceKeySet's KeySets and fragmentData differ in size."); | 
|---|
| [376a3b] | 112 |  | 
|---|
| [b8f0b25] | 113 | OrthogonalSumUpPerLevel<MPQCDataGridMap_t, MPQCData, MPQCDataGridVector_t>( | 
|---|
|  | 114 | fragmentData, MPQCMatrixNrLookup, container, subsetmap, | 
|---|
|  | 115 | Result_Grid_fused, Result_perIndexSet_Grid); | 
|---|
| [5a5196] | 116 |  | 
|---|
|  | 117 | // multiply each short-range potential with the respective charge | 
|---|
|  | 118 | std::map<JobId_t,MPQCData>::const_iterator mpqciter = fragmentData.begin(); | 
|---|
|  | 119 | std::map<JobId_t,VMGData>::iterator vmgiter = longrangeData.begin(); | 
|---|
|  | 120 | for (; vmgiter != longrangeData.end(); ++mpqciter, ++vmgiter) { | 
|---|
|  | 121 | vmgiter->second.sampled_potential *= mpqciter->second.sampled_grid; | 
|---|
|  | 122 | } | 
|---|
|  | 123 | // then sum up | 
|---|
| [b8f0b25] | 124 | OrthogonalSumUpPerLevel<VMGDataMap_t, VMGData, VMGDataVector_t>( | 
|---|
|  | 125 | longrangeData, VMGMatrixNrLookup, container, subsetmap, | 
|---|
|  | 126 | Result_LongRange_fused, Result_perIndexSet_LongRange); | 
|---|
| [376a3b] | 127 |  | 
|---|
|  | 128 | Result_LongRangeIntegrated_fused.reserve(MaxLevel); | 
|---|
| [d29b31] | 129 | // NOTE: potential for level 1 is wrongly calculated within a molecule | 
|---|
|  | 130 | // as saturation hydrogen are not removed on this level yet | 
|---|
|  | 131 | for (size_t level = 1; level <= MaxLevel; ++level) { | 
|---|
| [e2925fd] | 132 | // We have calculated three different contributions: e-e, e-n+n-n, and n-n. | 
|---|
|  | 133 | // And we want to have e-e+e-n, n-n+n-e (where e-n = n-e). | 
|---|
|  | 134 | // For each of these three contributions we have a full solution and summed | 
|---|
|  | 135 | // up short range solutions. | 
|---|
|  | 136 |  | 
|---|
|  | 137 | // first we obtain the full e-e energy as potential times charge on the | 
|---|
|  | 138 | // respective level. | 
|---|
| [376a3b] | 139 | const SamplingGrid &charge_weight = | 
|---|
|  | 140 | boost::fusion::at_key<MPQCDataFused::sampled_grid>(Result_Grid_fused[level-1]); | 
|---|
| [d29b31] | 141 | SamplingGrid full_sample_solution = fullsolutionData[level-1].sampled_potential; | 
|---|
| [67fec1] | 142 | full_sample_solution *= charge_weight; | 
|---|
| [e2925fd] | 143 | double electron_solution_energy = full_sample_solution.integral(); | 
|---|
|  | 144 |  | 
|---|
|  | 145 | // then we subtract the summed-up short-range e-e interaction energy from | 
|---|
|  | 146 | // the full solution. | 
|---|
| [376a3b] | 147 | const SamplingGrid &short_range_correction = | 
|---|
|  | 148 | boost::fusion::at_key<VMGDataFused::sampled_potential>(Result_LongRange_fused[level-1]); | 
|---|
| [e2925fd] | 149 | double electron_short_range_energy = short_range_correction.integral(); | 
|---|
| [376a3b] | 150 | full_sample_solution -= short_range_correction; | 
|---|
| [e2925fd] | 151 | electron_solution_energy -= electron_short_range_energy; | 
|---|
|  | 152 | ASSERT( fabs(electron_solution_energy - full_sample_solution.integral()) < 1e-7, | 
|---|
|  | 153 | "FragmentationLongRangeResults::operator() - integral and energy are not exchangeable."); | 
|---|
|  | 154 |  | 
|---|
|  | 155 | // then, we obtain the e-n+n-n full solution in the same way | 
|---|
| [d29b31] | 156 | double nuclei_solution_energy = fullsolutionData[level-1].nuclei_long; | 
|---|
| [e2925fd] | 157 | double nuclei_short_range_energy = | 
|---|
|  | 158 | boost::fusion::at_key<VMGDataFused::nuclei_long>(Result_LongRange_fused[level-1]); | 
|---|
|  | 159 | nuclei_solution_energy -= nuclei_short_range_energy; | 
|---|
|  | 160 |  | 
|---|
|  | 161 | // and also the e-n full solution | 
|---|
| [d29b31] | 162 | double both_solution_energy = fullsolutionData[level-1].electron_long; | 
|---|
| [e2925fd] | 163 | double both_short_range_energy = | 
|---|
|  | 164 | boost::fusion::at_key<VMGDataFused::electron_long>(Result_LongRange_fused[level-1]); | 
|---|
|  | 165 | both_solution_energy -= both_short_range_energy; | 
|---|
|  | 166 |  | 
|---|
|  | 167 | // energies from interpolation at nuclei position has factor of 1/2 already | 
|---|
|  | 168 | electron_solution_energy *= .5; | 
|---|
|  | 169 | electron_short_range_energy *= .5; | 
|---|
|  | 170 |  | 
|---|
|  | 171 | // At last, we subtract e-n from n-n+e-n for full solution and short-range | 
|---|
|  | 172 | // correction. | 
|---|
|  | 173 | nuclei_solution_energy -= both_solution_energy; | 
|---|
|  | 174 | nuclei_short_range_energy -= both_short_range_energy; | 
|---|
| [376a3b] | 175 |  | 
|---|
|  | 176 | VMGDataLongRangeMap_t instance; | 
|---|
| [e2925fd] | 177 | boost::fusion::at_key<VMGDataFused::electron_longrange>(instance) = electron_solution_energy; | 
|---|
| [67fec1] | 178 | //      LOG(0, "Remaining long-range potential integral of level " << level << " is " | 
|---|
|  | 179 | //          << full_sample_solution.integral() << "."); | 
|---|
| [e2925fd] | 180 | boost::fusion::at_key<VMGDataFused::electron_shortrange>(instance) = electron_short_range_energy; | 
|---|
| [67fec1] | 181 | //      LOG(0, "Short-range correction potential integral of level " << level << " is " | 
|---|
|  | 182 | //          << short_range_correction.integral() << "."); | 
|---|
| [e2925fd] | 183 | boost::fusion::at_key<VMGDataFused::mixed_longrange>(instance) = both_solution_energy; | 
|---|
|  | 184 | //      LOG(0, "Remaining long-range energy from potential integral of level " << level << " is " | 
|---|
|  | 185 | //          << full_solution_energy << "."); | 
|---|
|  | 186 | boost::fusion::at_key<VMGDataFused::mixed_shortrange>(instance) = both_short_range_energy; | 
|---|
|  | 187 | //      LOG(0, "Short-range correction energy from potential integral of level " << level << " is " | 
|---|
|  | 188 | //          << short_range_energy << "."); | 
|---|
|  | 189 | boost::fusion::at_key<VMGDataFused::nuclei_longrange>(instance) = nuclei_solution_energy; | 
|---|
| [67fec1] | 190 | //      LOG(0, "Remaining long-range energy from potential integral of level " << level << " is " | 
|---|
|  | 191 | //          << full_solution_energy << "."); | 
|---|
| [e2925fd] | 192 | boost::fusion::at_key<VMGDataFused::nuclei_shortrange>(instance) = nuclei_short_range_energy; | 
|---|
| [67fec1] | 193 | //      LOG(0, "Short-range correction energy from potential integral of level " << level << " is " | 
|---|
|  | 194 | //          << short_range_energy << "."); | 
|---|
|  | 195 | boost::fusion::at_key<VMGDataFused::total_longrange>(instance) = | 
|---|
| [e2925fd] | 196 | boost::fusion::at_key<VMGDataFused::electron_longrange>(instance) | 
|---|
|  | 197 | + 2.*boost::fusion::at_key<VMGDataFused::mixed_longrange>(instance) | 
|---|
|  | 198 | + boost::fusion::at_key<VMGDataFused::nuclei_longrange>(instance); | 
|---|
| [67fec1] | 199 | boost::fusion::at_key<VMGDataFused::total_shortrange>(instance) = | 
|---|
| [e2925fd] | 200 | boost::fusion::at_key<VMGDataFused::electron_shortrange>(instance) | 
|---|
|  | 201 | + 2.*boost::fusion::at_key<VMGDataFused::mixed_shortrange>(instance) | 
|---|
|  | 202 | + boost::fusion::at_key<VMGDataFused::nuclei_shortrange>(instance); | 
|---|
| [376a3b] | 203 | Result_LongRangeIntegrated_fused.push_back(instance); | 
|---|
|  | 204 | } | 
|---|
|  | 205 | //    { | 
|---|
|  | 206 | //      //    LOG(0, "Remaining long-range energy from energy_potential is " << full_sample_solution.integral()-epotentialSummer.getFullContribution() << "."); | 
|---|
|  | 207 | //      SamplingGrid full_sample_solution = fullsolutionData.back().sampled_potential; | 
|---|
|  | 208 | //      const SamplingGrid &short_range_correction = | 
|---|
|  | 209 | //          boost::fusion::at_key<VMGDataFused::sampled_potential>(Result_LongRange_fused.back()).getFullContribution(); | 
|---|
|  | 210 | //      full_sample_solution -= short_range_correction; | 
|---|
|  | 211 | //      // multiply element-wise with charge distribution | 
|---|
|  | 212 | //      LOG(0, "Remaining long-range potential integral is " << full_sample_solution.integral() << "."); | 
|---|
|  | 213 | //      LOG(0, "Short-range correction potential integral of level is " << short_range_correction.integral() << "."); | 
|---|
|  | 214 | //      LOG(0, "Remaining long-range energy from potential integral is " | 
|---|
|  | 215 | //          << full_sample_solution.integral(full_sample.back()) << "."); | 
|---|
|  | 216 | //      LOG(0, "Short-range correction energy from potential integral is " | 
|---|
|  | 217 | //          << short_range_correction.integral(full_sample.back()) << "."); | 
|---|
|  | 218 | // | 
|---|
|  | 219 | //      double e_long = fullsolutionData.back().e_long; | 
|---|
|  | 220 | //      e_long -= boost::fusion::at_key<VMGDataFused::energy_long>(Result_LongRange_fused.back()).getFullContribution(); | 
|---|
|  | 221 | //      LOG(0, "Remaining long-range energy is " << e_long << "."); | 
|---|
|  | 222 | //    } | 
|---|
|  | 223 |  | 
|---|
|  | 224 | // TODO: Extract long-range corrections to forces | 
|---|
|  | 225 | // NOTE: potential is in atomic length units, NOT IN ANGSTROEM! | 
|---|
|  | 226 |  | 
|---|
|  | 227 | } | 
|---|
|  | 228 | } | 
|---|