| 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 | * 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 |  | 
|---|
| 44 | #include "CodePatterns/Assert.hpp" | 
|---|
| 45 |  | 
|---|
| 46 | #include "Atom/atom.hpp" | 
|---|
| 47 | #include "Element/element.hpp" | 
|---|
| 48 | #include "Helpers/defs.hpp" | 
|---|
| 49 | #include "Jobs/VMGJob.hpp" | 
|---|
| 50 | #include "molecule.hpp" | 
|---|
| 51 | #include "Potentials/Particles/Particle.hpp" | 
|---|
| 52 | #include "Potentials/Particles/ParticleRegistry.hpp" | 
|---|
| 53 | #include "World.hpp" | 
|---|
| 54 |  | 
|---|
| 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 |  | 
|---|
| 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 |  | 
|---|
| 92 | bool VMGFragmentController::createLongRangeJobs( | 
|---|
| 93 | const std::map<JobId_t, MPQCData> &fragmentData, | 
|---|
| 94 | const std::vector<SamplingGrid> &full_sampled_grid, | 
|---|
| 95 | const size_t near_field_cells, | 
|---|
| 96 | const size_t interpolation_degree, | 
|---|
| 97 | const SampleParticles_t _SampleParticles, | 
|---|
| 98 | const TreatGrid_t _TreatGrid, | 
|---|
| 99 | const MPQCData::DoValenceOnly_t _DoValenceOnly, | 
|---|
| 100 | const bool _DoPrintDebug, | 
|---|
| 101 | const bool _OpenBoundaryConditions, | 
|---|
| 102 | const bool _DoSmearCharges, | 
|---|
| 103 | const bool _UseImplicitCharges) | 
|---|
| 104 | { | 
|---|
| 105 | // get max delta of full grid | 
|---|
| 106 | ASSERT( !full_sampled_grid.empty(), | 
|---|
| 107 | "VMGFragmentController::createLongRangeJobs() - given full_sampled_grid must not be empty."); | 
|---|
| 108 | const double max_full_delta = | 
|---|
| 109 | std::max(full_sampled_grid.back().getDeltaPerAxis(0), | 
|---|
| 110 | std::max(full_sampled_grid.back().getDeltaPerAxis(1), | 
|---|
| 111 | full_sampled_grid.back().getDeltaPerAxis(2))); | 
|---|
| 112 |  | 
|---|
| 113 | std::vector<FragmentJob::ptr> jobs; | 
|---|
| 114 | /// add one job for each fragment as the short-range correction which we need | 
|---|
| 115 | /// to subtract from the obtained full potential to get the long-range part only | 
|---|
| 116 | for (std::map<JobId_t, MPQCData>::const_iterator iter = fragmentData.begin(); | 
|---|
| 117 | iter != fragmentData.end(); ++iter) { | 
|---|
| 118 | const JobId_t next_id = getAvailableId(); | 
|---|
| 119 | const MPQCData &data = iter->second; | 
|---|
| 120 | const size_t increased_nfc = getFragmentNearFieldCells( | 
|---|
| 121 | data.sampled_grid, max_full_delta, near_field_cells); | 
|---|
| 122 | LOG(1, "INFO: Creating VMGJob with " << data.sampled_grid | 
|---|
| 123 | << " gridpoints and " << data.charges.size() << " particle charges, using " | 
|---|
| 124 | << increased_nfc << " near field cells."); | 
|---|
| 125 | FragmentJob::ptr testJob( | 
|---|
| 126 | new VMGJob( | 
|---|
| 127 | next_id, | 
|---|
| 128 | _TreatGrid == DoTreatGrid ? | 
|---|
| 129 | data.sampled_grid : | 
|---|
| 130 | SamplingGrid(data.sampled_grid.begin, data.sampled_grid.end, data.sampled_grid.level), | 
|---|
| 131 | data.positions, | 
|---|
| 132 | data.charges, | 
|---|
| 133 | increased_nfc, | 
|---|
| 134 | interpolation_degree, | 
|---|
| 135 | _SampleParticles == DoSampleParticles, | 
|---|
| 136 | _DoPrintDebug, | 
|---|
| 137 | _OpenBoundaryConditions, | 
|---|
| 138 | _DoSmearCharges) ); | 
|---|
| 139 | jobs.push_back(testJob); | 
|---|
| 140 | } | 
|---|
| 141 |  | 
|---|
| 142 | /// prepare positions and charges of full system | 
|---|
| 143 | /// \note we cannot use the summed up Fragment here, as the saturation hydrogens | 
|---|
| 144 | /// are in the way and cannot be sorted out properly/in a simple fashion. | 
|---|
| 145 | std::vector< std::vector<double> > positions; | 
|---|
| 146 | std::vector<double> charges; | 
|---|
| 147 | const World &world = const_cast<const World &>(World::getInstance()); | 
|---|
| 148 | const ParticleRegistry ®istry = const_cast<const ParticleRegistry &>(ParticleRegistry::getInstance()); | 
|---|
| 149 | { | 
|---|
| 150 | const World::ConstAtomComposite &atoms = world.getAllAtoms(); | 
|---|
| 151 | positions.reserve(atoms.size()); | 
|---|
| 152 | charges.reserve(atoms.size()); | 
|---|
| 153 | std::vector<double> position(3, 0.); | 
|---|
| 154 | for (World::ConstAtomComposite::const_iterator iter = atoms.begin(); | 
|---|
| 155 | iter != atoms.end(); ++iter) { | 
|---|
| 156 | // set position for this atom | 
|---|
| 157 | const Vector &pos = (*iter)->getPosition(); | 
|---|
| 158 | // convert positions to atomic length units | 
|---|
| 159 | for (size_t i=0;i<3;++i) | 
|---|
| 160 | position[i] = pos[i]/AtomicLengthToAngstroem; | 
|---|
| 161 |  | 
|---|
| 162 | // use partial charges ... | 
|---|
| 163 | const atomId_t atomid = (*iter)->getId(); | 
|---|
| 164 | if ((!world.isAtomSelected(atomid)) && (_UseImplicitCharges)) { | 
|---|
| 165 | // ... for all unselected particles ... | 
|---|
| 166 | std::string particlename = (*iter)->getParticleName(); | 
|---|
| 167 | if (particlename.empty()) | 
|---|
| 168 | particlename = (*iter)->getElement().getSymbol(); | 
|---|
| 169 | if (registry.isPresentByName(particlename)) { | 
|---|
| 170 | // ... that are present in ParticleRegistry | 
|---|
| 171 | const Particle * const particle = registry.getByName(particlename); | 
|---|
| 172 | LOG(3, "DEBUG: Using implicit charge " << particle->charge << " of particle " | 
|---|
| 173 | << particle->getName() << " for atom " << atomid); | 
|---|
| 174 | positions.push_back(position); | 
|---|
| 175 | charges.push_back(particle->charge); | 
|---|
| 176 | } | 
|---|
| 177 | } else { | 
|---|
| 178 | double charge = (*iter)->getElement().getAtomicNumber(); | 
|---|
| 179 | // subtract core electron charge from nuclei charge if only valence sampled | 
|---|
| 180 | if (_DoValenceOnly == MPQCData::DoSampleValenceOnly) | 
|---|
| 181 | charge -= getCoreElectrons(charge); | 
|---|
| 182 | positions.push_back(position); | 
|---|
| 183 | charges.push_back((double)charge); | 
|---|
| 184 | } | 
|---|
| 185 | } | 
|---|
| 186 | } | 
|---|
| 187 | /// and submit full job | 
|---|
| 188 | for(std::vector<SamplingGrid>::const_iterator iter = full_sampled_grid.begin(); | 
|---|
| 189 | iter != full_sampled_grid.end(); | 
|---|
| 190 | ++iter) { | 
|---|
| 191 | const SamplingGrid &grid = *iter; | 
|---|
| 192 | const JobId_t next_id = getAvailableId(); | 
|---|
| 193 | LOG(1, "INFO: Creating full VMGJob with " << *iter | 
|---|
| 194 | << " gridpoints and " << charges.size() << " particle charges."); | 
|---|
| 195 | FragmentJob::ptr testJob( | 
|---|
| 196 | new VMGJob( | 
|---|
| 197 | next_id, | 
|---|
| 198 | _TreatGrid == DoTreatGrid ? | 
|---|
| 199 | grid : | 
|---|
| 200 | SamplingGrid(grid.begin, grid.end, grid.level), | 
|---|
| 201 | positions, | 
|---|
| 202 | charges, | 
|---|
| 203 | near_field_cells, | 
|---|
| 204 | interpolation_degree, | 
|---|
| 205 | _SampleParticles == DoSampleParticles, | 
|---|
| 206 | _DoPrintDebug, | 
|---|
| 207 | _OpenBoundaryConditions, | 
|---|
| 208 | _DoSmearCharges) ); | 
|---|
| 209 | jobs.push_back(testJob); | 
|---|
| 210 | } | 
|---|
| 211 |  | 
|---|
| 212 | /// then send jobs to controller | 
|---|
| 213 | addJobs(jobs); | 
|---|
| 214 | sendJobs(host, port); | 
|---|
| 215 | RunService("Adding VMGJobs"); | 
|---|
| 216 |  | 
|---|
| 217 | return true; | 
|---|
| 218 | } | 
|---|