| [ffe057] | 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. | 
|---|
| [ffe057] | 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 | * VMGFragmentController.cpp | 
|---|
|  | 26 | * | 
|---|
|  | 27 | *  Created on: Aug 27, 2012 | 
|---|
|  | 28 | *      Author: heber | 
|---|
|  | 29 | */ | 
|---|
|  | 30 |  | 
|---|
|  | 31 |  | 
|---|
|  | 32 | // include config.h | 
|---|
|  | 33 | #ifdef HAVE_CONFIG_H | 
|---|
|  | 34 | #include <config.h> | 
|---|
|  | 35 | #endif | 
|---|
|  | 36 |  | 
|---|
|  | 37 | // boost asio needs specific operator new | 
|---|
|  | 38 | #include <boost/asio.hpp> | 
|---|
|  | 39 |  | 
|---|
|  | 40 | #include "CodePatterns/MemDebug.hpp" | 
|---|
|  | 41 |  | 
|---|
|  | 42 | #include "VMGFragmentController.hpp" | 
|---|
|  | 43 |  | 
|---|
| [eaef9b] | 44 | #include "CodePatterns/Assert.hpp" | 
|---|
|  | 45 |  | 
|---|
| [ffe057] | 46 | #include "Atom/atom.hpp" | 
|---|
|  | 47 | #include "Element/element.hpp" | 
|---|
| [de84ef] | 48 | #include "Helpers/defs.hpp" | 
|---|
| [ffe057] | 49 | #include "Jobs/VMGJob.hpp" | 
|---|
|  | 50 | #include "molecule.hpp" | 
|---|
| [666e9e] | 51 | #include "Potentials/Particles/Particle.hpp" | 
|---|
|  | 52 | #include "Potentials/Particles/ParticleRegistry.hpp" | 
|---|
| [ffe057] | 53 | #include "World.hpp" | 
|---|
|  | 54 |  | 
|---|
| [6ff62c] | 55 | /** Helper function for the number of core electrons of a given element \a z. | 
|---|
|  | 56 | * | 
|---|
|  | 57 | * \param z atomic number of element | 
|---|
|  | 58 | * \return number of core electrons for this element | 
|---|
|  | 59 | */ | 
|---|
|  | 60 | static int getCoreElectrons(const int z) | 
|---|
|  | 61 | { | 
|---|
|  | 62 | int n=0; | 
|---|
|  | 63 | if (z > 2) n += 2; | 
|---|
|  | 64 | if (z > 10) n += 8; | 
|---|
|  | 65 | if (z > 18) n += 8; | 
|---|
|  | 66 | if (z > 30) n += 10; | 
|---|
|  | 67 | if (z > 36) n += 8; | 
|---|
|  | 68 | if (z > 48) n += 10; | 
|---|
|  | 69 | if (z > 54) n += 8; | 
|---|
|  | 70 | return n; | 
|---|
|  | 71 | } | 
|---|
|  | 72 |  | 
|---|
| [eaef9b] | 73 | static size_t getFragmentNearFieldCells( | 
|---|
|  | 74 | const SamplingGrid &_fragment_grid, | 
|---|
|  | 75 | const double _max_full_delta, | 
|---|
|  | 76 | const size_t full_nfc | 
|---|
|  | 77 | ) | 
|---|
|  | 78 | { | 
|---|
|  | 79 | // get delta of fragment for comparison | 
|---|
|  | 80 | const double fragment_grid_delta = | 
|---|
|  | 81 | std::max(_fragment_grid.getDeltaPerAxis(0), | 
|---|
|  | 82 | std::max(_fragment_grid.getDeltaPerAxis(1), | 
|---|
|  | 83 | _fragment_grid.getDeltaPerAxis(2))); | 
|---|
|  | 84 | const double factor = _max_full_delta/fragment_grid_delta; | 
|---|
|  | 85 | size_t increased_nfc = full_nfc * factor; | 
|---|
|  | 86 | // never let nfc get smaller than 3 | 
|---|
|  | 87 | if (increased_nfc < 3) | 
|---|
|  | 88 | increased_nfc = 3; | 
|---|
|  | 89 | return increased_nfc; | 
|---|
|  | 90 | } | 
|---|
|  | 91 |  | 
|---|
| [213f9d] | 92 | void VMGFragmentController::getFullPositions( | 
|---|
|  | 93 | std::vector< std::vector<double> > &positions, | 
|---|
|  | 94 | std::vector<double> &charges, | 
|---|
|  | 95 | const bool _UseImplicitCharges, | 
|---|
|  | 96 | const bool _DoValenceOnly) | 
|---|
|  | 97 | { | 
|---|
|  | 98 | /// \note we cannot use the summed up Fragment here, as the saturation hydrogens | 
|---|
|  | 99 | /// are in the way and cannot be sorted out properly/in a simple fashion. | 
|---|
|  | 100 | const World &world = const_cast<const World &>(World::getInstance()); | 
|---|
|  | 101 | const ParticleRegistry ®istry = const_cast<const ParticleRegistry &>(ParticleRegistry::getInstance()); | 
|---|
|  | 102 | { | 
|---|
|  | 103 | const World::ConstAtomComposite &atoms = world.getAllAtoms(); | 
|---|
|  | 104 | positions.reserve(atoms.size()); | 
|---|
|  | 105 | charges.reserve(atoms.size()); | 
|---|
|  | 106 | std::vector<double> position(3, 0.); | 
|---|
|  | 107 | for (World::ConstAtomComposite::const_iterator iter = atoms.begin(); | 
|---|
|  | 108 | iter != atoms.end(); ++iter) { | 
|---|
|  | 109 | // set position for this atom | 
|---|
|  | 110 | const Vector &pos = (*iter)->getPosition(); | 
|---|
|  | 111 | // convert positions to atomic length units | 
|---|
|  | 112 | for (size_t i=0;i<3;++i) | 
|---|
|  | 113 | position[i] = pos[i]/AtomicLengthToAngstroem; | 
|---|
|  | 114 |  | 
|---|
|  | 115 | // use partial charges ... | 
|---|
|  | 116 | const atomId_t atomid = (*iter)->getId(); | 
|---|
|  | 117 | if ((!world.isAtomSelected(atomid)) && (_UseImplicitCharges)) { | 
|---|
|  | 118 | // ... for all unselected particles ... | 
|---|
|  | 119 | std::string particlename = (*iter)->getParticleName(); | 
|---|
|  | 120 | if (particlename.empty()) | 
|---|
|  | 121 | particlename = (*iter)->getElement().getSymbol(); | 
|---|
|  | 122 | if (registry.isPresentByName(particlename)) { | 
|---|
|  | 123 | // ... that are present in ParticleRegistry | 
|---|
|  | 124 | const Particle * const particle = registry.getByName(particlename); | 
|---|
|  | 125 | LOG(3, "DEBUG: Using implicit charge " << particle->charge << " of particle " | 
|---|
|  | 126 | << particle->getName() << " for atom " << atomid); | 
|---|
|  | 127 | positions.push_back(position); | 
|---|
|  | 128 | charges.push_back(particle->charge); | 
|---|
|  | 129 | } | 
|---|
|  | 130 | } else { | 
|---|
|  | 131 | double charge = (*iter)->getElement().getAtomicNumber(); | 
|---|
|  | 132 | // subtract core electron charge from nuclei charge if only valence sampled | 
|---|
|  | 133 | if (_DoValenceOnly == MPQCData::DoSampleValenceOnly) | 
|---|
|  | 134 | charge -= getCoreElectrons(charge); | 
|---|
|  | 135 | positions.push_back(position); | 
|---|
|  | 136 | charges.push_back((double)charge); | 
|---|
|  | 137 | } | 
|---|
|  | 138 | } | 
|---|
|  | 139 | } | 
|---|
|  | 140 | } | 
|---|
|  | 141 |  | 
|---|
| [ffe057] | 142 | bool VMGFragmentController::createLongRangeJobs( | 
|---|
|  | 143 | const std::map<JobId_t, MPQCData> &fragmentData, | 
|---|
| [a2295a] | 144 | const std::vector<SamplingGrid> &full_sampled_grid, | 
|---|
| [cd2591] | 145 | const size_t near_field_cells, | 
|---|
| [6ff62c] | 146 | const size_t interpolation_degree, | 
|---|
| [e2925fd] | 147 | const SampleParticles_t _SampleParticles, | 
|---|
|  | 148 | const TreatGrid_t _TreatGrid, | 
|---|
| [b6b21a] | 149 | const MPQCData::DoValenceOnly_t _DoValenceOnly, | 
|---|
| [ee9018] | 150 | const bool _DoPrintDebug, | 
|---|
| [17e4fd] | 151 | const bool _OpenBoundaryConditions, | 
|---|
| [666e9e] | 152 | const bool _DoSmearCharges, | 
|---|
|  | 153 | const bool _UseImplicitCharges) | 
|---|
| [ffe057] | 154 | { | 
|---|
| [eaef9b] | 155 | // get max delta of full grid | 
|---|
|  | 156 | ASSERT( !full_sampled_grid.empty(), | 
|---|
|  | 157 | "VMGFragmentController::createLongRangeJobs() - given full_sampled_grid must not be empty."); | 
|---|
|  | 158 | const double max_full_delta = | 
|---|
|  | 159 | std::max(full_sampled_grid.back().getDeltaPerAxis(0), | 
|---|
|  | 160 | std::max(full_sampled_grid.back().getDeltaPerAxis(1), | 
|---|
|  | 161 | full_sampled_grid.back().getDeltaPerAxis(2))); | 
|---|
|  | 162 |  | 
|---|
| [ffe057] | 163 | std::vector<FragmentJob::ptr> jobs; | 
|---|
| [b6b21a] | 164 | /// add one job for each fragment as the short-range correction which we need | 
|---|
|  | 165 | /// to subtract from the obtained full potential to get the long-range part only | 
|---|
| [ffe057] | 166 | for (std::map<JobId_t, MPQCData>::const_iterator iter = fragmentData.begin(); | 
|---|
|  | 167 | iter != fragmentData.end(); ++iter) { | 
|---|
|  | 168 | const JobId_t next_id = getAvailableId(); | 
|---|
| [c44322] | 169 | const MPQCData &data = iter->second; | 
|---|
| [eaef9b] | 170 | const size_t increased_nfc = getFragmentNearFieldCells( | 
|---|
|  | 171 | data.sampled_grid, max_full_delta, near_field_cells); | 
|---|
| [c44322] | 172 | LOG(1, "INFO: Creating VMGJob with " << data.sampled_grid | 
|---|
| [eaef9b] | 173 | << " gridpoints and " << data.charges.size() << " particle charges, using " | 
|---|
|  | 174 | << increased_nfc << " near field cells."); | 
|---|
| [ffe057] | 175 | FragmentJob::ptr testJob( | 
|---|
| [b6b21a] | 176 | new VMGJob( | 
|---|
|  | 177 | next_id, | 
|---|
| [e2925fd] | 178 | _TreatGrid == DoTreatGrid ? | 
|---|
|  | 179 | data.sampled_grid : | 
|---|
|  | 180 | SamplingGrid(data.sampled_grid.begin, data.sampled_grid.end, data.sampled_grid.level), | 
|---|
| [b6b21a] | 181 | data.positions, | 
|---|
|  | 182 | data.charges, | 
|---|
| [eaef9b] | 183 | increased_nfc, | 
|---|
| [b6b21a] | 184 | interpolation_degree, | 
|---|
| [e2925fd] | 185 | _SampleParticles == DoSampleParticles, | 
|---|
| [ee9018] | 186 | _DoPrintDebug, | 
|---|
| [17e4fd] | 187 | _OpenBoundaryConditions, | 
|---|
|  | 188 | _DoSmearCharges) ); | 
|---|
| [ffe057] | 189 | jobs.push_back(testJob); | 
|---|
|  | 190 | } | 
|---|
|  | 191 |  | 
|---|
| [b6b21a] | 192 | /// prepare positions and charges of full system | 
|---|
| [a2295a] | 193 | std::vector< std::vector<double> > positions; | 
|---|
|  | 194 | std::vector<double> charges; | 
|---|
| [213f9d] | 195 | getFullPositions(positions, charges, _UseImplicitCharges, _DoValenceOnly); | 
|---|
| [666e9e] | 196 |  | 
|---|
| [b6b21a] | 197 | /// and submit full job | 
|---|
| [a2295a] | 198 | for(std::vector<SamplingGrid>::const_iterator iter = full_sampled_grid.begin(); | 
|---|
|  | 199 | iter != full_sampled_grid.end(); | 
|---|
|  | 200 | ++iter) { | 
|---|
| [e2925fd] | 201 | const SamplingGrid &grid = *iter; | 
|---|
| [ffe057] | 202 | const JobId_t next_id = getAvailableId(); | 
|---|
| [a2295a] | 203 | LOG(1, "INFO: Creating full VMGJob with " << *iter | 
|---|
| [ffe057] | 204 | << " gridpoints and " << charges.size() << " particle charges."); | 
|---|
|  | 205 | FragmentJob::ptr testJob( | 
|---|
| [b6b21a] | 206 | new VMGJob( | 
|---|
|  | 207 | next_id, | 
|---|
| [e2925fd] | 208 | _TreatGrid == DoTreatGrid ? | 
|---|
|  | 209 | grid : | 
|---|
|  | 210 | SamplingGrid(grid.begin, grid.end, grid.level), | 
|---|
| [b6b21a] | 211 | positions, | 
|---|
|  | 212 | charges, | 
|---|
|  | 213 | near_field_cells, | 
|---|
|  | 214 | interpolation_degree, | 
|---|
| [e2925fd] | 215 | _SampleParticles == DoSampleParticles, | 
|---|
| [ee9018] | 216 | _DoPrintDebug, | 
|---|
| [17e4fd] | 217 | _OpenBoundaryConditions, | 
|---|
|  | 218 | _DoSmearCharges) ); | 
|---|
| [ffe057] | 219 | jobs.push_back(testJob); | 
|---|
|  | 220 | } | 
|---|
|  | 221 |  | 
|---|
| [b6b21a] | 222 | /// then send jobs to controller | 
|---|
| [ffe057] | 223 | addJobs(jobs); | 
|---|
|  | 224 | sendJobs(host, port); | 
|---|
|  | 225 | RunService("Adding VMGJobs"); | 
|---|
|  | 226 |  | 
|---|
|  | 227 | return true; | 
|---|
|  | 228 | } | 
|---|