| [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 | 
 | 
|---|
| [ffe057] | 92 | bool VMGFragmentController::createLongRangeJobs(
 | 
|---|
 | 93 |     const std::map<JobId_t, MPQCData> &fragmentData,
 | 
|---|
| [a2295a] | 94 |     const std::vector<SamplingGrid> &full_sampled_grid,
 | 
|---|
| [cd2591] | 95 |     const size_t near_field_cells,
 | 
|---|
| [6ff62c] | 96 |     const size_t interpolation_degree,
 | 
|---|
| [e2925fd] | 97 |     const SampleParticles_t _SampleParticles,
 | 
|---|
 | 98 |     const TreatGrid_t _TreatGrid,
 | 
|---|
| [b6b21a] | 99 |     const MPQCData::DoValenceOnly_t _DoValenceOnly,
 | 
|---|
| [ee9018] | 100 |     const bool _DoPrintDebug,
 | 
|---|
| [17e4fd] | 101 |     const bool _OpenBoundaryConditions,
 | 
|---|
| [666e9e] | 102 |     const bool _DoSmearCharges,
 | 
|---|
 | 103 |     const bool _UseImplicitCharges)
 | 
|---|
| [ffe057] | 104 | {
 | 
|---|
| [eaef9b] | 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 | 
 | 
|---|
| [ffe057] | 113 |   std::vector<FragmentJob::ptr> jobs;
 | 
|---|
| [b6b21a] | 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
 | 
|---|
| [ffe057] | 116 |   for (std::map<JobId_t, MPQCData>::const_iterator iter = fragmentData.begin();
 | 
|---|
 | 117 |       iter != fragmentData.end(); ++iter) {
 | 
|---|
 | 118 |     const JobId_t next_id = getAvailableId();
 | 
|---|
| [c44322] | 119 |     const MPQCData &data = iter->second;
 | 
|---|
| [eaef9b] | 120 |     const size_t increased_nfc = getFragmentNearFieldCells(
 | 
|---|
 | 121 |         data.sampled_grid, max_full_delta, near_field_cells);
 | 
|---|
| [c44322] | 122 |     LOG(1, "INFO: Creating VMGJob with " << data.sampled_grid
 | 
|---|
| [eaef9b] | 123 |         << " gridpoints and " << data.charges.size() << " particle charges, using "
 | 
|---|
 | 124 |         << increased_nfc << " near field cells.");
 | 
|---|
| [ffe057] | 125 |     FragmentJob::ptr testJob(
 | 
|---|
| [b6b21a] | 126 |         new VMGJob(
 | 
|---|
 | 127 |             next_id,
 | 
|---|
| [e2925fd] | 128 |             _TreatGrid == DoTreatGrid ?
 | 
|---|
 | 129 |                 data.sampled_grid :
 | 
|---|
 | 130 |                 SamplingGrid(data.sampled_grid.begin, data.sampled_grid.end, data.sampled_grid.level),
 | 
|---|
| [b6b21a] | 131 |             data.positions,
 | 
|---|
 | 132 |             data.charges,
 | 
|---|
| [eaef9b] | 133 |             increased_nfc,
 | 
|---|
| [b6b21a] | 134 |             interpolation_degree,
 | 
|---|
| [e2925fd] | 135 |             _SampleParticles == DoSampleParticles,
 | 
|---|
| [ee9018] | 136 |             _DoPrintDebug,
 | 
|---|
| [17e4fd] | 137 |             _OpenBoundaryConditions,
 | 
|---|
 | 138 |             _DoSmearCharges) );
 | 
|---|
| [ffe057] | 139 |     jobs.push_back(testJob);
 | 
|---|
 | 140 |   }
 | 
|---|
 | 141 | 
 | 
|---|
| [b6b21a] | 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.
 | 
|---|
| [a2295a] | 145 |   std::vector< std::vector<double> > positions;
 | 
|---|
 | 146 |   std::vector<double> charges;
 | 
|---|
| [666e9e] | 147 |   const World &world = const_cast<const World &>(World::getInstance());
 | 
|---|
 | 148 |   const ParticleRegistry ®istry = const_cast<const ParticleRegistry &>(ParticleRegistry::getInstance());
 | 
|---|
| [ffe057] | 149 |   {
 | 
|---|
| [666e9e] | 150 |     const World::ConstAtomComposite &atoms = world.getAllAtoms();
 | 
|---|
| [ffe057] | 151 |     positions.reserve(atoms.size());
 | 
|---|
 | 152 |     charges.reserve(atoms.size());
 | 
|---|
 | 153 |     std::vector<double> position(3, 0.);
 | 
|---|
| [a58c16] | 154 |     for (World::ConstAtomComposite::const_iterator iter = atoms.begin();
 | 
|---|
| [ffe057] | 155 |         iter != atoms.end(); ++iter) {
 | 
|---|
| [666e9e] | 156 |       // set position for this atom
 | 
|---|
| [ffe057] | 157 |       const Vector &pos = (*iter)->getPosition();
 | 
|---|
| [de84ef] | 158 |       // convert positions to atomic length units
 | 
|---|
| [666e9e] | 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 ...
 | 
|---|
| [4f42c9] | 166 |         std::string particlename = (*iter)->getParticleName();
 | 
|---|
 | 167 |         if (particlename.empty())
 | 
|---|
 | 168 |           particlename = (*iter)->getElement().getSymbol();
 | 
|---|
 | 169 |         if (registry.isPresentByName(particlename)) {
 | 
|---|
| [666e9e] | 170 |           // ... that are present in ParticleRegistry
 | 
|---|
| [4f42c9] | 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);
 | 
|---|
| [666e9e] | 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 |       }
 | 
|---|
| [ffe057] | 185 |     }
 | 
|---|
| [a2295a] | 186 |   }
 | 
|---|
| [b6b21a] | 187 |   /// and submit full job
 | 
|---|
| [a2295a] | 188 |   for(std::vector<SamplingGrid>::const_iterator iter = full_sampled_grid.begin();
 | 
|---|
 | 189 |       iter != full_sampled_grid.end();
 | 
|---|
 | 190 |       ++iter) {
 | 
|---|
| [e2925fd] | 191 |     const SamplingGrid &grid = *iter;
 | 
|---|
| [ffe057] | 192 |     const JobId_t next_id = getAvailableId();
 | 
|---|
| [a2295a] | 193 |     LOG(1, "INFO: Creating full VMGJob with " << *iter
 | 
|---|
| [ffe057] | 194 |         << " gridpoints and " << charges.size() << " particle charges.");
 | 
|---|
 | 195 |     FragmentJob::ptr testJob(
 | 
|---|
| [b6b21a] | 196 |         new VMGJob(
 | 
|---|
 | 197 |             next_id,
 | 
|---|
| [e2925fd] | 198 |             _TreatGrid == DoTreatGrid ?
 | 
|---|
 | 199 |                 grid :
 | 
|---|
 | 200 |                 SamplingGrid(grid.begin, grid.end, grid.level),
 | 
|---|
| [b6b21a] | 201 |             positions,
 | 
|---|
 | 202 |             charges,
 | 
|---|
 | 203 |             near_field_cells,
 | 
|---|
 | 204 |             interpolation_degree,
 | 
|---|
| [e2925fd] | 205 |             _SampleParticles == DoSampleParticles,
 | 
|---|
| [ee9018] | 206 |             _DoPrintDebug,
 | 
|---|
| [17e4fd] | 207 |             _OpenBoundaryConditions,
 | 
|---|
 | 208 |             _DoSmearCharges) );
 | 
|---|
| [ffe057] | 209 |     jobs.push_back(testJob);
 | 
|---|
 | 210 |   }
 | 
|---|
 | 211 | 
 | 
|---|
| [b6b21a] | 212 |   /// then send jobs to controller
 | 
|---|
| [ffe057] | 213 |   addJobs(jobs);
 | 
|---|
 | 214 |   sendJobs(host, port);
 | 
|---|
 | 215 |   RunService("Adding VMGJobs");
 | 
|---|
 | 216 | 
 | 
|---|
 | 217 |   return true;
 | 
|---|
 | 218 | }
 | 
|---|