| 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 |
|
|---|
| 62 | const double ExportGraph_ToJobs::log_two(log(2.));
|
|---|
| 63 |
|
|---|
| 64 | ExportGraph_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 |
|
|---|
| 75 | ExportGraph_ToJobs::~ExportGraph_ToJobs()
|
|---|
| 76 | {}
|
|---|
| 77 |
|
|---|
| 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 |
|
|---|
| 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 | {
|
|---|
| 206 | double max_domain_meshwidth = _max_meshwidth;
|
|---|
| 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);
|
|---|
| 211 | // we may never make level smaller (needs to be as large as domain's)
|
|---|
| 212 | ASSERT( desired_level >= 0,
|
|---|
| 213 | "ExportGraph_ToJobs::setAcceptableFragmentLevel() - should never get negative extra level.");
|
|---|
| 214 | if (desired_level > 0)
|
|---|
| 215 | _grid.level += desired_level;
|
|---|
| 216 | LOG(4, "DEBUG: Fragment requires " << desired_level
|
|---|
| 217 | << " additional grid levels to reach at least " << _max_meshwidth
|
|---|
| 218 | << " from " << max_domain_meshwidth);
|
|---|
| 219 | }
|
|---|
| 220 |
|
|---|
| 221 | bool ExportGraph_ToJobs::operator()()
|
|---|
| 222 | {
|
|---|
| 223 | std::vector<FragmentJob::ptr> jobs;
|
|---|
| 224 | KeySetsContainer KeySets;
|
|---|
| 225 | KeySetsContainer FullKeySets;
|
|---|
| 226 | jobs.reserve(TotalGraph.size());
|
|---|
| 227 | LOG(1, "INFO: Creating " << TotalGraph.size() << " possible bond fragmentation jobs.");
|
|---|
| 228 |
|
|---|
| 229 | // gather info about the domain
|
|---|
| 230 | const SamplingGridProperties grid(getDomainGrid(level));
|
|---|
| 231 | const ParserTypes jobtype =
|
|---|
| 232 | FormatParserStorage::getInstance().getTypeFromName("mpqc");
|
|---|
| 233 |
|
|---|
| 234 | // go through all fragments, output to stream and create job therefrom
|
|---|
| 235 | ExportGraph::SaturatedFragment_ptr CurrentFragment = getNextFragment();
|
|---|
| 236 | for (; (CurrentFragment != NULL) && (CurrentFragment->getKeySet() != ExportGraph::EmptySet);
|
|---|
| 237 | CurrentFragment = getNextFragment()) {
|
|---|
| 238 | const KeySet &set = CurrentFragment->getKeySet();
|
|---|
| 239 | LOG(2, "INFO: Creating bond fragment job for set " << set << ".");
|
|---|
| 240 |
|
|---|
| 241 | SamplingGridProperties fragment_window(grid);
|
|---|
| 242 | if (max_meshwidth != 0.) {
|
|---|
| 243 | // gather extent of the fragment in atomic length(!)
|
|---|
| 244 | std::pair<Vector, Vector> minmax = CurrentFragment->getRoughBoundingBox();
|
|---|
| 245 | minmax.first *= 1./AtomicLengthToAngstroem;
|
|---|
| 246 | minmax.second *= 1./AtomicLengthToAngstroem;
|
|---|
| 247 |
|
|---|
| 248 | /** we need to find the begin and end points of the smaller grid
|
|---|
| 249 | * otherwise we would calculate the whole domain with an incrased grid level with just
|
|---|
| 250 | * a small window (which is only used for sampling and storing; vmg uses full grid).
|
|---|
| 251 | * Hence, we need to make the grid smaller and such that it is still in a power of two
|
|---|
| 252 | * and coinciding with the grid points of the global grid.
|
|---|
| 253 | */
|
|---|
| 254 | fragment_window = getGridExtentsForGivenBoundingBox(minmax, grid, minimum_empty_boundary);
|
|---|
| 255 | LOG(4, "DEBUG: Fragment " << CurrentFragment->getKeySet() << " has window from "
|
|---|
| 256 | << Vector(fragment_window.begin) << " to " << Vector(fragment_window.end)
|
|---|
| 257 | << " with total level portion of " << fragment_window.level );
|
|---|
| 258 |
|
|---|
| 259 | // next we need to check how much we need to increase from the grid level for
|
|---|
| 260 | // the total domain to achieve at least maximum mesh width
|
|---|
| 261 | setAcceptableFragmentLevel(fragment_window, max_meshwidth/AtomicLengthToAngstroem);
|
|---|
| 262 | }
|
|---|
| 263 |
|
|---|
| 264 | // store config in stream
|
|---|
| 265 | {
|
|---|
| 266 | std::stringstream output;
|
|---|
| 267 | // save to stream
|
|---|
| 268 | CurrentFragment->OutputConfig(output, jobtype);
|
|---|
| 269 | // create job and insert
|
|---|
| 270 | FragmentJob::ptr testJob(
|
|---|
| 271 | #ifdef HAVE_JOBMARKET
|
|---|
| 272 | new MPQCJob(
|
|---|
| 273 | JobId::IllegalJob,
|
|---|
| 274 | output.str(),
|
|---|
| 275 | fragment_window.begin, fragment_window.end, fragment_window.level)
|
|---|
| 276 | #else
|
|---|
| 277 | new MPQCCommandJob(output.str(), JobId::IllegalJob)
|
|---|
| 278 | #endif
|
|---|
| 279 | );
|
|---|
| 280 | testJob->setPriority(CurrentFragment->getKeySet().size());
|
|---|
| 281 | jobs.push_back(testJob);
|
|---|
| 282 |
|
|---|
| 283 | // order is the same as the number of non-hydrogen atoms
|
|---|
| 284 | const KeySet &keyset = CurrentFragment->getKeySet();
|
|---|
| 285 | const size_t order = keyset.size();
|
|---|
| 286 | const KeySet &fullmolecule = CurrentFragment->getFullMolecule();
|
|---|
| 287 | const KeySet &saturationhydrogens = CurrentFragment->getSaturationHydrogens();
|
|---|
| 288 | KeySetsContainer::IntVector indices(keyset.begin(), keyset.end());
|
|---|
| 289 | KeySetsContainer::IntVector forceindices(fullmolecule.begin(), fullmolecule.end());
|
|---|
| 290 | {
|
|---|
| 291 | // replace all saturated hydrogen indices by "-1"
|
|---|
| 292 | for (KeySetsContainer::IntVector::iterator iter = forceindices.begin();
|
|---|
| 293 | iter != forceindices.end();
|
|---|
| 294 | ++iter)
|
|---|
| 295 | if (saturationhydrogens.find(*iter) != saturationhydrogens.end())
|
|---|
| 296 | *iter = -1;
|
|---|
| 297 | }
|
|---|
| 298 | KeySets.insert(indices, order);
|
|---|
| 299 | FullKeySets.insert(forceindices, order);
|
|---|
| 300 | }
|
|---|
| 301 | // store force index reference file
|
|---|
| 302 | // explicitly release fragment
|
|---|
| 303 | CurrentFragment.reset();
|
|---|
| 304 | }
|
|---|
| 305 | if (CurrentFragment == NULL) {
|
|---|
| 306 | ELOG(1, "Some error while obtaining the next fragment occured.");
|
|---|
| 307 | return false;
|
|---|
| 308 | }
|
|---|
| 309 |
|
|---|
| 310 | // push final jobs
|
|---|
| 311 | FragmentJobQueue::getInstance().addJobs(jobs, KeySets, FullKeySets);
|
|---|
| 312 |
|
|---|
| 313 | return true;
|
|---|
| 314 | }
|
|---|