| 1 | /*
 | 
|---|
| 2 |  * Project: MoleCuilder
 | 
|---|
| 3 |  * Description: creates and alters molecular systems
 | 
|---|
| 4 |  * Copyright (C)  2012 University of Bonn. All rights reserved.
 | 
|---|
| 5 |  * Copyright (C)  2013 Frederik Heber. All rights reserved.
 | 
|---|
| 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 | /*
 | 
|---|
| 25 |  * FragmentationLongRangeResults.cpp
 | 
|---|
| 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 | 
 | 
|---|
| 39 | #include "FragmentationLongRangeResults.hpp"
 | 
|---|
| 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"
 | 
|---|
| 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"
 | 
|---|
| 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 | 
 | 
|---|
| 61 | FragmentationLongRangeResults::FragmentationLongRangeResults(
 | 
|---|
| 62 |     const std::map<JobId_t,MPQCData> &fragmentData,
 | 
|---|
| 63 |     std::map<JobId_t,VMGData> &longrangeData,
 | 
|---|
| 64 |     const KeySetsContainer& _KeySet,
 | 
|---|
| 65 |     const KeySetsContainer& _ForceKeySet) :
 | 
|---|
| 66 |     KeySet(_KeySet),
 | 
|---|
| 67 |     ForceKeySet(_ForceKeySet)
 | 
|---|
| 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 |     )
 | 
|---|
| 81 | {
 | 
|---|
| 82 |   // create lookup from job nr to fragment number
 | 
|---|
| 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 |   }
 | 
|---|
| 90 | 
 | 
|---|
| 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 |   }
 | 
|---|
| 98 | }
 | 
|---|
| 99 | 
 | 
|---|
| 100 | void FragmentationLongRangeResults::operator()(
 | 
|---|
| 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 | {
 | 
|---|
| 106 |   MaxLevel = subsetmap->getMaximumSetLevel();
 | 
|---|
| 107 |   LOG(1, "INFO: Summing up results till level " << MaxLevel << ".");
 | 
|---|
| 108 |   /// convert all MPQCData to MPQCDataMap_t
 | 
|---|
| 109 |   {
 | 
|---|
| 110 |     ASSERT( ForceKeySet.KeySets.size() == fragmentData.size(),
 | 
|---|
| 111 |         "FragmentationLongRangeResults::FragmentationLongRangeResults() - ForceKeySet's KeySets and fragmentData differ in size.");
 | 
|---|
| 112 | 
 | 
|---|
| 113 |     OrthogonalSumUpPerLevel<MPQCDataGridMap_t, MPQCData, MPQCDataGridVector_t>(
 | 
|---|
| 114 |         fragmentData, MPQCMatrixNrLookup, container, subsetmap,
 | 
|---|
| 115 |         Result_Grid_fused, Result_perIndexSet_Grid);
 | 
|---|
| 116 |     OrthogonalSumUpPerLevel<MPQCDataFragmentMap_t, MPQCData, MPQCDataFragmentVector_t>(
 | 
|---|
| 117 |         fragmentData, MPQCMatrixNrLookup, container, subsetmap,
 | 
|---|
| 118 |         Result_Fragment_fused, Result_perIndexSet_Fragment);
 | 
|---|
| 119 | 
 | 
|---|
| 120 |     // multiply each short-range potential with the respective charge
 | 
|---|
| 121 |     std::map<JobId_t,MPQCData>::const_iterator mpqciter = fragmentData.begin();
 | 
|---|
| 122 |     std::map<JobId_t,VMGData>::iterator vmgiter = longrangeData.begin();
 | 
|---|
| 123 |     for (; vmgiter != longrangeData.end(); ++mpqciter, ++vmgiter) {
 | 
|---|
| 124 |       vmgiter->second.sampled_potential *= mpqciter->second.sampled_grid;
 | 
|---|
| 125 |     }
 | 
|---|
| 126 |     // then sum up
 | 
|---|
| 127 |     OrthogonalSumUpPerLevel<VMGDataMap_t, VMGData, VMGDataVector_t>(
 | 
|---|
| 128 |         longrangeData, VMGMatrixNrLookup, container, subsetmap,
 | 
|---|
| 129 |         Result_LongRange_fused, Result_perIndexSet_LongRange);
 | 
|---|
| 130 | 
 | 
|---|
| 131 |     Result_LongRangeIntegrated_fused.reserve(MaxLevel);
 | 
|---|
| 132 |     // NOTE: potential for level 1 is wrongly calculated within a molecule
 | 
|---|
| 133 |     // as saturation hydrogen are not removed on this level yet
 | 
|---|
| 134 |     for (size_t level = 1; level <= MaxLevel; ++level) {
 | 
|---|
| 135 |       // We have calculated three different contributions: e-e, e-n+n-n, and n-n.
 | 
|---|
| 136 |       // And we want to have e-e+e-n, n-n+n-e (where e-n = n-e).
 | 
|---|
| 137 |       // For each of these three contributions we have a full solution and summed
 | 
|---|
| 138 |       // up short range solutions.
 | 
|---|
| 139 | 
 | 
|---|
| 140 |       // first we obtain the full e-e energy as potential times charge on the
 | 
|---|
| 141 |       // respective level.
 | 
|---|
| 142 |       const SamplingGrid &charge_weight =
 | 
|---|
| 143 |           boost::fusion::at_key<MPQCDataFused::sampled_grid>(Result_Grid_fused[level-1]);
 | 
|---|
| 144 |       SamplingGrid full_sample_solution = fullsolutionData[level-1].sampled_potential;
 | 
|---|
| 145 |       full_sample_solution *= charge_weight;
 | 
|---|
| 146 |       double electron_solution_energy = full_sample_solution.integral();
 | 
|---|
| 147 | 
 | 
|---|
| 148 |       // then we subtract the summed-up short-range e-e interaction energy from
 | 
|---|
| 149 |       // the full solution.
 | 
|---|
| 150 |       const SamplingGrid &short_range_correction =
 | 
|---|
| 151 |           boost::fusion::at_key<VMGDataFused::sampled_potential>(Result_LongRange_fused[level-1]);
 | 
|---|
| 152 |       double electron_short_range_energy = short_range_correction.integral();
 | 
|---|
| 153 |       full_sample_solution -= short_range_correction;
 | 
|---|
| 154 |       electron_solution_energy -= electron_short_range_energy;
 | 
|---|
| 155 |       ASSERT( fabs(electron_solution_energy - full_sample_solution.integral()) < 1e-7,
 | 
|---|
| 156 |           "FragmentationLongRangeResults::operator() - integral and energy are not exchangeable.");
 | 
|---|
| 157 | 
 | 
|---|
| 158 |       // then, we obtain the e-n+n-n full solution in the same way
 | 
|---|
| 159 |       double nuclei_solution_energy = fullsolutionData[level-1].nuclei_long;
 | 
|---|
| 160 |       double nuclei_short_range_energy =
 | 
|---|
| 161 |           boost::fusion::at_key<VMGDataFused::nuclei_long>(Result_LongRange_fused[level-1]);
 | 
|---|
| 162 |       nuclei_solution_energy -= nuclei_short_range_energy;
 | 
|---|
| 163 | 
 | 
|---|
| 164 |       // and also the e-n full solution
 | 
|---|
| 165 |       double both_solution_energy = fullsolutionData[level-1].electron_long;
 | 
|---|
| 166 |       double both_short_range_energy =
 | 
|---|
| 167 |           boost::fusion::at_key<VMGDataFused::electron_long>(Result_LongRange_fused[level-1]);
 | 
|---|
| 168 |       both_solution_energy -= both_short_range_energy;
 | 
|---|
| 169 | 
 | 
|---|
| 170 |       // energies from interpolation at nuclei position has factor of 1/2 already
 | 
|---|
| 171 |       electron_solution_energy *= .5;
 | 
|---|
| 172 |       electron_short_range_energy *= .5;
 | 
|---|
| 173 | 
 | 
|---|
| 174 |       // At last, we subtract e-n from n-n+e-n for full solution and short-range
 | 
|---|
| 175 |       // correction.
 | 
|---|
| 176 |       nuclei_solution_energy -= both_solution_energy;
 | 
|---|
| 177 |       nuclei_short_range_energy -= both_short_range_energy;
 | 
|---|
| 178 | 
 | 
|---|
| 179 |       VMGDataLongRangeMap_t instance;
 | 
|---|
| 180 |       boost::fusion::at_key<VMGDataFused::electron_longrange>(instance) = electron_solution_energy;
 | 
|---|
| 181 | //      LOG(0, "Remaining long-range potential integral of level " << level << " is "
 | 
|---|
| 182 | //          << full_sample_solution.integral() << ".");
 | 
|---|
| 183 |       boost::fusion::at_key<VMGDataFused::electron_shortrange>(instance) = electron_short_range_energy;
 | 
|---|
| 184 | //      LOG(0, "Short-range correction potential integral of level " << level << " is "
 | 
|---|
| 185 | //          << short_range_correction.integral() << ".");
 | 
|---|
| 186 |       boost::fusion::at_key<VMGDataFused::mixed_longrange>(instance) = both_solution_energy;
 | 
|---|
| 187 | //      LOG(0, "Remaining long-range energy from potential integral of level " << level << " is "
 | 
|---|
| 188 | //          << full_solution_energy << ".");
 | 
|---|
| 189 |       boost::fusion::at_key<VMGDataFused::mixed_shortrange>(instance) = both_short_range_energy;
 | 
|---|
| 190 | //      LOG(0, "Short-range correction energy from potential integral of level " << level << " is "
 | 
|---|
| 191 | //          << short_range_energy << ".");
 | 
|---|
| 192 |       boost::fusion::at_key<VMGDataFused::nuclei_longrange>(instance) = nuclei_solution_energy;
 | 
|---|
| 193 | //      LOG(0, "Remaining long-range energy from potential integral of level " << level << " is "
 | 
|---|
| 194 | //          << full_solution_energy << ".");
 | 
|---|
| 195 |       boost::fusion::at_key<VMGDataFused::nuclei_shortrange>(instance) = nuclei_short_range_energy;
 | 
|---|
| 196 | //      LOG(0, "Short-range correction energy from potential integral of level " << level << " is "
 | 
|---|
| 197 | //          << short_range_energy << ".");
 | 
|---|
| 198 |       boost::fusion::at_key<VMGDataFused::total_longrange>(instance) =
 | 
|---|
| 199 |           boost::fusion::at_key<VMGDataFused::electron_longrange>(instance)
 | 
|---|
| 200 |           + 2.*boost::fusion::at_key<VMGDataFused::mixed_longrange>(instance)
 | 
|---|
| 201 |           + boost::fusion::at_key<VMGDataFused::nuclei_longrange>(instance);
 | 
|---|
| 202 |       boost::fusion::at_key<VMGDataFused::total_shortrange>(instance) =
 | 
|---|
| 203 |           boost::fusion::at_key<VMGDataFused::electron_shortrange>(instance)
 | 
|---|
| 204 |           + 2.*boost::fusion::at_key<VMGDataFused::mixed_shortrange>(instance)
 | 
|---|
| 205 |           + boost::fusion::at_key<VMGDataFused::nuclei_shortrange>(instance);
 | 
|---|
| 206 |       Result_LongRangeIntegrated_fused.push_back(instance);
 | 
|---|
| 207 |     }
 | 
|---|
| 208 | //    {
 | 
|---|
| 209 | //      //    LOG(0, "Remaining long-range energy from energy_potential is " << full_sample_solution.integral()-epotentialSummer.getFullContribution() << ".");
 | 
|---|
| 210 | //      SamplingGrid full_sample_solution = fullsolutionData.back().sampled_potential;
 | 
|---|
| 211 | //      const SamplingGrid &short_range_correction =
 | 
|---|
| 212 | //          boost::fusion::at_key<VMGDataFused::sampled_potential>(Result_LongRange_fused.back()).getFullContribution();
 | 
|---|
| 213 | //      full_sample_solution -= short_range_correction;
 | 
|---|
| 214 | //      // multiply element-wise with charge distribution
 | 
|---|
| 215 | //      LOG(0, "Remaining long-range potential integral is " << full_sample_solution.integral() << ".");
 | 
|---|
| 216 | //      LOG(0, "Short-range correction potential integral of level is " << short_range_correction.integral() << ".");
 | 
|---|
| 217 | //      LOG(0, "Remaining long-range energy from potential integral is "
 | 
|---|
| 218 | //          << full_sample_solution.integral(full_sample.back()) << ".");
 | 
|---|
| 219 | //      LOG(0, "Short-range correction energy from potential integral is "
 | 
|---|
| 220 | //          << short_range_correction.integral(full_sample.back()) << ".");
 | 
|---|
| 221 | //
 | 
|---|
| 222 | //      double e_long = fullsolutionData.back().e_long;
 | 
|---|
| 223 | //      e_long -= boost::fusion::at_key<VMGDataFused::energy_long>(Result_LongRange_fused.back()).getFullContribution();
 | 
|---|
| 224 | //      LOG(0, "Remaining long-range energy is " << e_long << ".");
 | 
|---|
| 225 | //    }
 | 
|---|
| 226 | 
 | 
|---|
| 227 |     // TODO: Extract long-range corrections to forces
 | 
|---|
| 228 |     // NOTE: potential is in atomic length units, NOT IN ANGSTROEM!
 | 
|---|
| 229 | 
 | 
|---|
| 230 |   }
 | 
|---|
| 231 | }
 | 
|---|