source: src/Fragmentation/Automation/VMGFragmentController.cpp@ 213f9d

Fix_FitPartialCharges
Last change on this file since 213f9d was 213f9d, checked in by Frederik Heber <heber@…>, 9 years ago

Extracted VMGFragmentController::getFullPositions().

  • Property mode set to 100644
File size: 7.9 KB
Line 
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 */
60static 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
73static 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
92void 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 &registry = 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
142bool VMGFragmentController::createLongRangeJobs(
143 const std::map<JobId_t, MPQCData> &fragmentData,
144 const std::vector<SamplingGrid> &full_sampled_grid,
145 const size_t near_field_cells,
146 const size_t interpolation_degree,
147 const SampleParticles_t _SampleParticles,
148 const TreatGrid_t _TreatGrid,
149 const MPQCData::DoValenceOnly_t _DoValenceOnly,
150 const bool _DoPrintDebug,
151 const bool _OpenBoundaryConditions,
152 const bool _DoSmearCharges,
153 const bool _UseImplicitCharges)
154{
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
163 std::vector<FragmentJob::ptr> jobs;
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
166 for (std::map<JobId_t, MPQCData>::const_iterator iter = fragmentData.begin();
167 iter != fragmentData.end(); ++iter) {
168 const JobId_t next_id = getAvailableId();
169 const MPQCData &data = iter->second;
170 const size_t increased_nfc = getFragmentNearFieldCells(
171 data.sampled_grid, max_full_delta, near_field_cells);
172 LOG(1, "INFO: Creating VMGJob with " << data.sampled_grid
173 << " gridpoints and " << data.charges.size() << " particle charges, using "
174 << increased_nfc << " near field cells.");
175 FragmentJob::ptr testJob(
176 new VMGJob(
177 next_id,
178 _TreatGrid == DoTreatGrid ?
179 data.sampled_grid :
180 SamplingGrid(data.sampled_grid.begin, data.sampled_grid.end, data.sampled_grid.level),
181 data.positions,
182 data.charges,
183 increased_nfc,
184 interpolation_degree,
185 _SampleParticles == DoSampleParticles,
186 _DoPrintDebug,
187 _OpenBoundaryConditions,
188 _DoSmearCharges) );
189 jobs.push_back(testJob);
190 }
191
192 /// prepare positions and charges of full system
193 std::vector< std::vector<double> > positions;
194 std::vector<double> charges;
195 getFullPositions(positions, charges, _UseImplicitCharges, _DoValenceOnly);
196
197 /// and submit full job
198 for(std::vector<SamplingGrid>::const_iterator iter = full_sampled_grid.begin();
199 iter != full_sampled_grid.end();
200 ++iter) {
201 const SamplingGrid &grid = *iter;
202 const JobId_t next_id = getAvailableId();
203 LOG(1, "INFO: Creating full VMGJob with " << *iter
204 << " gridpoints and " << charges.size() << " particle charges.");
205 FragmentJob::ptr testJob(
206 new VMGJob(
207 next_id,
208 _TreatGrid == DoTreatGrid ?
209 grid :
210 SamplingGrid(grid.begin, grid.end, grid.level),
211 positions,
212 charges,
213 near_field_cells,
214 interpolation_degree,
215 _SampleParticles == DoSampleParticles,
216 _DoPrintDebug,
217 _OpenBoundaryConditions,
218 _DoSmearCharges) );
219 jobs.push_back(testJob);
220 }
221
222 /// then send jobs to controller
223 addJobs(jobs);
224 sendJobs(host, port);
225 RunService("Adding VMGJobs");
226
227 return true;
228}
Note: See TracBrowser for help on using the repository browser.