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