source: src/Fragmentation/Exporters/ExportGraph_ToJobs.cpp@ f8dae2

Fix_IndependentFragmentGrids
Last change on this file since f8dae2 was f8dae2, checked in by Frederik Heber <heber@…>, 8 years ago

DROPME: Added lots of debugging to ExportGraph_ToJobs.

  • Property mode set to 100644
File size: 13.5 KB
Line 
1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
4 * Copyright (C) 2011 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 * ExportGraph_ToJobs.cpp
26 *
27 * Created on: 08.03.2012
28 * Author: heber
29 */
30
31// include config.h
32#ifdef HAVE_CONFIG_H
33#include <config.h>
34#endif
35
36// boost asio required before MemDebug due to placement new
37#include <boost/asio.hpp>
38
39#include "CodePatterns/MemDebug.hpp"
40
41#include "Fragmentation/Exporters/ExportGraph_ToJobs.hpp"
42
43#include <algorithm>
44#include <cmath>
45#include <limits>
46
47#include "CodePatterns/Log.hpp"
48
49#include "Box.hpp"
50#include "Fragmentation/KeySet.hpp"
51#include "Fragmentation/Automation/FragmentJobQueue.hpp"
52#include "Helpers/defs.hpp"
53#ifdef HAVE_JOBMARKET
54#include "Jobs/MPQCJob.hpp"
55#else
56#include "Jobs/MPQCCommandJob.hpp"
57#endif
58#include "LinearAlgebra/RealSpaceMatrix.hpp"
59#include "Parser/FormatParserStorage.hpp"
60#include "World.hpp"
61
62const double ExportGraph_ToJobs::log_two(log(2.));
63
64ExportGraph_ToJobs::ExportGraph_ToJobs(
65 const Graph &_graph,
66 const enum HydrogenTreatment _treatment,
67 const enum HydrogenSaturation _saturation,
68 const SaturatedFragment::GlobalSaturationPositions_t &_globalsaturationpositions) :
69 ExportGraph(_graph, _treatment, _saturation,_globalsaturationpositions),
70 level(5),
71 max_meshwidth(0.),
72 minimum_empty_boundary(5./AtomicLengthToAngstroem)
73{}
74
75ExportGraph_ToJobs::~ExportGraph_ToJobs()
76{}
77
78SamplingGridProperties ExportGraph_ToJobs::getDomainGrid(
79 const int _level)
80{
81 double domain_begin[NDIM] = { 0., 0., 0. };
82 RealSpaceMatrix M = World::getInstance().getDomain().getM();
83 M *= 1./AtomicLengthToAngstroem; // scale to atomic length units
84 const double size = std::max( std::max(M.at(0,0), M.at(1,1)), M.at(2,2));
85 double domain_end[NDIM] = { size, size, size };
86 SamplingGridProperties grid(domain_begin, domain_end, _level);
87 return grid;
88}
89
90SamplingGridProperties ExportGraph_ToJobs::getGridExtentsForGivenBoundingBox(
91 const std::pair<Vector, Vector> &_minmax,
92 const SamplingGridProperties &_domain,
93 const double & _minimum_empty_boundary
94 )
95{
96 // return value
97 SamplingGridProperties fragment_window;
98
99 /// determine center of fragment
100 const Vector center = .5*(_minmax.first + _minmax.second);
101 LOG(2, "DEBUG: center of fragment is at " << center);
102
103 /// associate center to its containing grid cell (defined by boundary points)
104 Vector lower_center;
105 Vector higher_center;
106 bool equal_components[NDIM] = { false, false, false };
107 for (size_t i=0;i<NDIM;++i) {
108 lower_center[i] = _domain.getNearestLowerGridPoint(center[i], i);
109 higher_center[i] = _domain.getNearestHigherGridPoint(center[i], i);
110 equal_components[i] =
111 fabs(lower_center[i] - higher_center[i]) < std::numeric_limits<double>::epsilon()*1e4;
112 }
113 LOG(2, "DEBUG: lower_center is " << lower_center << ", higher_center is " << higher_center);
114
115 // fashion min max into cubic box extents of at least extent plus empty
116 // boundary and at most three times the extent
117 const Vector extent = _minmax.second - _minmax.first;
118 double greatest_extent = extent[extent.GreatestComponent()];
119 LOG(2, "DEBUG: extent of fragment is " << extent << " with greatest extent " << greatest_extent);
120 if (greatest_extent > _minimum_empty_boundary)
121 greatest_extent *= 3.;
122 else
123 greatest_extent += 2.* _minimum_empty_boundary;
124 const Vector total(
125 _domain.getTotalLengthPerAxis(0),
126 _domain.getTotalLengthPerAxis(1),
127 _domain.getTotalLengthPerAxis(2)
128 );
129 const double greatest_total = total[total.GreatestComponent()];
130 greatest_extent = std::min(greatest_extent, greatest_total);
131 LOG(2, "DEBUG: Using boundary of " << greatest_extent);
132
133 /// increase levels around this center to find the matching window
134 const double delta[NDIM] = {
135 _domain.getDeltaPerAxis(0),
136 _domain.getDeltaPerAxis(1),
137 _domain.getDeltaPerAxis(2)
138 };
139 LOG(2, "DEBUG: delta is " << Vector(delta));
140 const double greatest_delta = std::max(delta[0], std::max(delta[1], delta[2]));
141
142 fragment_window.level = (int)ceil(log(greatest_extent/greatest_delta+1)/log_two);
143 const size_t half_fragment_gridpoints = 1 << (fragment_window.level-1);
144 const size_t domain_gridpoints = _domain.getGridPointsPerAxis();
145 int begin_index[NDIM];
146 int end_index[NDIM];
147 int begin_steps[NDIM] = { 0, 0, 0 };
148 int end_steps[NDIM] = { 0, 0, 0 };
149 for (size_t i=0;i<NDIM;++i) {
150 const unsigned int lower_index = round(lower_center[i]/delta[i]);
151 const unsigned int higher_index = round(higher_center[i]/delta[i]);
152 LOG(3, "DEBUG: center is within indices [" << lower_index << ", " << higher_index << "]");
153 begin_index[i] = lower_index - (half_fragment_gridpoints-1);
154 end_index[i] = higher_index + (half_fragment_gridpoints+(unsigned int)(equal_components[i]));
155 LOG(3, "DEBUG: Intermediate begin end indices for axis #" << i << " are ["
156 << begin_index[i] << ", " << end_index[i] << "]");
157 if (begin_index[i] < 0)
158 begin_steps[i] = -begin_index[i];
159 if (end_index[i] > (int)domain_gridpoints)
160 end_steps[i] = end_index[i] - domain_gridpoints;
161 if ((begin_steps[i]+end_steps[i]+end_index[i]-begin_index[i] > (int)domain_gridpoints)
162 || ((begin_steps[i] != 0) && (end_steps[i] != 0))) {
163 begin_index[i] = 0;
164 end_index[i] = domain_gridpoints;
165 begin_steps[i] = 0;
166 end_steps[i] = 0;
167 }
168 LOG(3, "DEBUG: Resulting final indices [" << begin_index[i] << ", "
169 << end_index[i] << "] and step indices for axis #" << i << " are ["
170 << begin_steps[i] << ", " << end_steps[i] << "]");
171 }
172 for (size_t i=0;i<NDIM;++i) {
173 fragment_window.begin[i] = (begin_index[i]+begin_steps[i]-end_steps[i]) * delta[i];
174 fragment_window.end[i] = (end_index[i]-end_steps[i]+begin_steps[i]) * delta[i];
175 }
176 LOG(2, "DEBUG: fragment begin is " << Vector(fragment_window.begin)
177 << ", fragment end is " << Vector(fragment_window.end));
178
179 /// check whether window is large enough, if not yet continue
180#ifndef NDEBUG
181 for (size_t i=0;i<NDIM;++i) {
182 const double window_length = fragment_window.end[i] - fragment_window.begin[i];
183 ASSERT (((greatest_total != greatest_extent) && (greatest_extent+delta[i] <= window_length))
184 || ((greatest_total == greatest_extent) && (greatest_extent <= window_length)),
185 "ExportGraph_ToJobs::getGridExtentsForGivenBoundingBox() - level "
186 +toString(fragment_window.level)+" is insufficient to place fragment inside box "
187 +toString(_minmax.first)+" <-> "+toString(_minmax.second));
188 }
189#endif
190#ifndef NDEBUG
191 for (size_t i=0;i<NDIM;++i) {
192 ASSERT( fragment_window.begin[i] >= _domain.begin[i],
193 "ExportGraph_ToJobs::getGridExtentsForGivenBoundingBox() - fragment begin "
194 +toString(fragment_window.begin[i])+" is smaller than that of domain "
195 +toString(_domain.begin[i]));
196 ASSERT( fragment_window.end[i] <= _domain.end[i],
197 "ExportGraph_ToJobs::getGridExtentsForGivenBoundingBox() - fragment end "
198 +toString(fragment_window.end[i])+" is smaller than that of domain "
199 +toString(_domain.end[i]));
200 }
201#endif
202 if (DoLog(3))
203 for (size_t i=0;i<NDIM;++i)
204 LOG(3, "DEBUG: We have " << (fragment_window.end[i] - fragment_window.begin[i])/delta[i]
205 << " gridpoints on axis " << i);
206
207 return fragment_window;
208}
209
210void ExportGraph_ToJobs::setAcceptableFragmentLevel(
211 SamplingGridProperties &_grid,
212 const double &_max_meshwidth
213 )
214{
215 double max_domain_meshwidth = 0.;
216 for (size_t i=0;i<NDIM;++i)
217 max_domain_meshwidth = std::max(max_domain_meshwidth, _grid.getDeltaPerAxis(i));
218 // find power of 2 that's just greater than that ratio of given over desired mesh width
219 const int desired_level = ceil(log(max_domain_meshwidth / _max_meshwidth)/log_two);
220 _grid.level += desired_level;
221 if (_grid.level < 2) {
222 ELOG(1, "ExportGraph_ToJobs::setAcceptableFragmentLevel() - fragment grid level of 1 or less? "
223 << "max-meshwidth " << _max_meshwidth << " chosen too large w.r.t delta of "
224 << max_domain_meshwidth << "? Setting to minimum of 2.");
225 _grid.level = 2;
226 } else {
227 LOG(4, "DEBUG: Fragment requires " << desired_level
228 << " additional grid levels to reach at least " << _max_meshwidth
229 << " from " << max_domain_meshwidth);
230 }
231}
232
233bool ExportGraph_ToJobs::operator()()
234{
235 std::vector<FragmentJob::ptr> jobs;
236 KeySetsContainer KeySets;
237 KeySetsContainer FullKeySets;
238 jobs.reserve(TotalGraph.size());
239 LOG(1, "INFO: Creating " << TotalGraph.size() << " possible bond fragmentation jobs.");
240
241 // checking that max-meshwidth is not smaller than grid spacing of global level
242 const SamplingGridProperties grid(getDomainGrid(level));
243
244 // gather info about the domain
245 const double global_grid_delta =
246 std::max(grid.getDeltaPerAxis(0),
247 std::max(grid.getDeltaPerAxis(1),
248 grid.getDeltaPerAxis(2)));
249 if (global_grid_delta < max_meshwidth) {
250 ELOG(2, "Desired max meswidth " << max_meshwidth
251 << " is larger than grid spacing of global grid " << global_grid_delta << ".\n"
252 << "We cannot use coarser grids in fragment level, equating mesh width to global delta.");
253 max_meshwidth = global_grid_delta;
254 }
255
256 // set parser to use for writing job configurations
257 const ParserTypes jobtype =
258 FormatParserStorage::getInstance().getTypeFromName("mpqc");
259
260 // go through all fragments, output to stream and create job therefrom
261 ExportGraph::SaturatedFragment_ptr CurrentFragment = getNextFragment();
262 for (; (CurrentFragment != NULL) && (CurrentFragment->getKeySet() != ExportGraph::EmptySet);
263 CurrentFragment = getNextFragment()) {
264 const KeySet &set = CurrentFragment->getKeySet();
265 LOG(2, "INFO: Creating bond fragment job for set " << set << ".");
266
267 SamplingGridProperties fragment_window(grid);
268 if (max_meshwidth != 0.) {
269 // gather extent of the fragment in atomic length(!)
270 std::pair<Vector, Vector> minmax = CurrentFragment->getRoughBoundingBox();
271 minmax.first *= 1./AtomicLengthToAngstroem;
272 minmax.second *= 1./AtomicLengthToAngstroem;
273
274 /** we need to find the begin and end points of the smaller grid
275 * otherwise we would calculate the whole domain with an incrased grid level with just
276 * a small window (which is only used for sampling and storing; vmg uses full grid).
277 * Hence, we need to make the grid smaller and such that it is still in a power of two
278 * and coinciding with the grid points of the global grid.
279 */
280 fragment_window = getGridExtentsForGivenBoundingBox(minmax, grid, minimum_empty_boundary);
281 LOG(4, "DEBUG: Fragment " << CurrentFragment->getKeySet() << " has window from "
282 << Vector(fragment_window.begin) << " to " << Vector(fragment_window.end)
283 << " with total level portion of " << fragment_window.level );
284
285 // next we need to check how much we need to increase from the grid level for
286 // the total domain to achieve at least maximum mesh width
287 setAcceptableFragmentLevel(fragment_window, max_meshwidth/AtomicLengthToAngstroem);
288 }
289
290 // store config in stream
291 {
292 std::stringstream output;
293 // save to stream
294 CurrentFragment->OutputConfig(output, jobtype);
295 // create job and insert
296 FragmentJob::ptr testJob(
297#ifdef HAVE_JOBMARKET
298 new MPQCJob(
299 JobId::IllegalJob,
300 output.str(),
301 fragment_window.begin, fragment_window.end, fragment_window.level)
302#else
303 new MPQCCommandJob(output.str(), JobId::IllegalJob)
304#endif
305 );
306 testJob->setPriority(CurrentFragment->getKeySet().size());
307 jobs.push_back(testJob);
308
309 // order is the same as the number of non-hydrogen atoms
310 const KeySet &keyset = CurrentFragment->getKeySet();
311 const size_t order = keyset.size();
312 const KeySet &fullmolecule = CurrentFragment->getFullMolecule();
313 const KeySet &saturationhydrogens = CurrentFragment->getSaturationHydrogens();
314 KeySetsContainer::IntVector indices(keyset.begin(), keyset.end());
315 KeySetsContainer::IntVector forceindices(fullmolecule.begin(), fullmolecule.end());
316 {
317 // replace all saturated hydrogen indices by "-1"
318 for (KeySetsContainer::IntVector::iterator iter = forceindices.begin();
319 iter != forceindices.end();
320 ++iter)
321 if (saturationhydrogens.find(*iter) != saturationhydrogens.end())
322 *iter = -1;
323 }
324 KeySets.insert(indices, order);
325 FullKeySets.insert(forceindices, order);
326 }
327 // store force index reference file
328 // explicitly release fragment
329 CurrentFragment.reset();
330 }
331 if (CurrentFragment == NULL) {
332 ELOG(1, "Some error while obtaining the next fragment occured.");
333 return false;
334 }
335
336 // push final jobs
337 FragmentJobQueue::getInstance().addJobs(jobs, KeySets, FullKeySets);
338
339 return true;
340}
Note: See TracBrowser for help on using the repository browser.