| [28c025] | 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.
 | 
|---|
| [28c025] | 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 |  * SamplingGrid.cpp
 | 
|---|
 | 26 |  *
 | 
|---|
 | 27 |  *  Created on: 25.07.2012
 | 
|---|
 | 28 |  *      Author: heber
 | 
|---|
 | 29 |  */
 | 
|---|
 | 30 | 
 | 
|---|
 | 31 | // include config.h
 | 
|---|
 | 32 | #ifdef HAVE_CONFIG_H
 | 
|---|
 | 33 | #include <config.h>
 | 
|---|
 | 34 | #endif
 | 
|---|
 | 35 | 
 | 
|---|
 | 36 | // include headers that implement a archive in simple text format
 | 
|---|
 | 37 | // otherwise BOOST_CLASS_EXPORT_IMPLEMENT has no effect
 | 
|---|
 | 38 | #include <boost/archive/text_oarchive.hpp>
 | 
|---|
 | 39 | #include <boost/archive/text_iarchive.hpp>
 | 
|---|
 | 40 | 
 | 
|---|
 | 41 | #include "CodePatterns/MemDebug.hpp"
 | 
|---|
 | 42 | 
 | 
|---|
| [ca4b372] | 43 | #include "CodePatterns/Assert.hpp"
 | 
|---|
 | 44 | 
 | 
|---|
| [fbf143] | 45 | #include "Fragmentation/Summation/SetValues/SamplingGridProperties.hpp"
 | 
|---|
| [28c025] | 46 | 
 | 
|---|
 | 47 | SamplingGridProperties::SamplingGridProperties(
 | 
|---|
| [5b1e5e] | 48 |     const double _begin[NDIM],
 | 
|---|
 | 49 |     const double _end[NDIM],
 | 
|---|
| [28c025] | 50 |     const int _level) :
 | 
|---|
 | 51 |   level(_level)
 | 
|---|
 | 52 | {
 | 
|---|
| [5b1e5e] | 53 |   for(size_t i=0; i<NDIM; ++i) {
 | 
|---|
| [28c025] | 54 |     begin[i] = _begin[i];
 | 
|---|
| [3d9a8d] | 55 |     end[i] = _end[i];
 | 
|---|
 | 56 |   }
 | 
|---|
| [28c025] | 57 | }
 | 
|---|
 | 58 | 
 | 
|---|
 | 59 | SamplingGridProperties::SamplingGridProperties(const SamplingGridProperties &_props) :
 | 
|---|
 | 60 |   level(_props.level)
 | 
|---|
 | 61 | {
 | 
|---|
| [5b1e5e] | 62 |   for(size_t i=0; i<NDIM; ++i) {
 | 
|---|
| [28c025] | 63 |     begin[i] = _props.begin[i];
 | 
|---|
| [3d9a8d] | 64 |     end[i] = _props.end[i];
 | 
|---|
 | 65 |   }
 | 
|---|
| [28c025] | 66 | }
 | 
|---|
 | 67 | 
 | 
|---|
 | 68 | SamplingGridProperties::SamplingGridProperties() :
 | 
|---|
 | 69 |   level(0)
 | 
|---|
 | 70 | {
 | 
|---|
| [5b1e5e] | 71 |   for(size_t i=0; i<NDIM; ++i) {
 | 
|---|
| [28c025] | 72 |     begin[i] = 0.;
 | 
|---|
| [3d9a8d] | 73 |     end[i] = 0.;
 | 
|---|
 | 74 |   }
 | 
|---|
| [28c025] | 75 | }
 | 
|---|
 | 76 | 
 | 
|---|
 | 77 | SamplingGridProperties::~SamplingGridProperties()
 | 
|---|
 | 78 | {}
 | 
|---|
 | 79 | 
 | 
|---|
| [3d9a8d] | 80 | SamplingGridProperties& SamplingGridProperties::operator=(const SamplingGridProperties& other)
 | 
|---|
 | 81 | {
 | 
|---|
 | 82 |   // check for self-assignment
 | 
|---|
 | 83 |   if (this != &other) {
 | 
|---|
| [5b1e5e] | 84 |     for(size_t index=0; index<NDIM; ++index) {
 | 
|---|
| [3d9a8d] | 85 |       begin[index] = other.begin[index];
 | 
|---|
 | 86 |       end[index] = other.end[index];
 | 
|---|
 | 87 |     }
 | 
|---|
 | 88 |     level = other.level;
 | 
|---|
 | 89 |   }
 | 
|---|
 | 90 |   return *this;
 | 
|---|
 | 91 | }
 | 
|---|
 | 92 | 
 | 
|---|
| [c889b7] | 93 | bool SamplingGridProperties::operator==(const SamplingGridProperties &_props) const
 | 
|---|
 | 94 | {
 | 
|---|
 | 95 |   bool status = true;
 | 
|---|
| [5b1e5e] | 96 |   for (size_t i=0; i<NDIM; ++i) {
 | 
|---|
| [c889b7] | 97 |     status &= begin[i] == _props.begin[i];
 | 
|---|
| [3d9a8d] | 98 |     status &= end[i] == _props.end[i];
 | 
|---|
 | 99 |   }
 | 
|---|
| [c889b7] | 100 |   status &= level == _props.level;
 | 
|---|
 | 101 |   return status;
 | 
|---|
 | 102 | }
 | 
|---|
 | 103 | 
 | 
|---|
| [955051] | 104 | 
 | 
|---|
| [028790] | 105 | double SamplingGridProperties::getNearestLowerGridPoint(
 | 
|---|
 | 106 |     const double value, const size_t axis) const
 | 
|---|
 | 107 | {
 | 
|---|
 | 108 |   const double length = getTotalLengthPerAxis(axis);
 | 
|---|
 | 109 |   if ((fabs(length) < std::numeric_limits<double>::epsilon()) || (getGridPointsPerAxis() == 0))
 | 
|---|
 | 110 |     return begin[axis];
 | 
|---|
 | 111 |   if ((value - end[axis]) > -std::numeric_limits<double>::epsilon()*1.e4)
 | 
|---|
 | 112 |     return end[axis];
 | 
|---|
 | 113 |   const double offset = value - begin[axis];
 | 
|---|
 | 114 |   if (offset < std::numeric_limits<double>::epsilon()*1.e4)
 | 
|---|
 | 115 |     return begin[axis];
 | 
|---|
 | 116 |   // modify value a little if value actually has been on a grid point but
 | 
|---|
 | 117 |   // perturbed by numerical rounding: here up, as we always go lower
 | 
|---|
 | 118 |   const double factor =
 | 
|---|
 | 119 |       floor((double)getGridPointsPerAxis()*(offset/length)
 | 
|---|
 | 120 |           +std::numeric_limits<double>::epsilon()*1.e4);
 | 
|---|
 | 121 |   return begin[axis]+factor*getDeltaPerAxis(axis);
 | 
|---|
 | 122 | }
 | 
|---|
 | 123 | 
 | 
|---|
 | 124 | double SamplingGridProperties::getNearestHigherGridPoint(
 | 
|---|
 | 125 |     const double value, const size_t axis) const
 | 
|---|
 | 126 | {
 | 
|---|
 | 127 |   const double length = getTotalLengthPerAxis(axis);
 | 
|---|
 | 128 |   if ((fabs(length) < std::numeric_limits<double>::epsilon()) || (getGridPointsPerAxis() == 0))
 | 
|---|
 | 129 |     return end[axis];
 | 
|---|
 | 130 |   if ((value - end[axis]) > -std::numeric_limits<double>::epsilon()*1.e4)
 | 
|---|
 | 131 |     return end[axis];
 | 
|---|
 | 132 |   const double offset = value - begin[axis];
 | 
|---|
 | 133 |   if (offset < std::numeric_limits<double>::epsilon()*1.e4)
 | 
|---|
 | 134 |     return begin[axis];
 | 
|---|
 | 135 |   // modify value a little if value actually has been on a grid point but
 | 
|---|
 | 136 |   // perturbed by numerical rounding: here down, as we always go higher
 | 
|---|
 | 137 |   const double factor =
 | 
|---|
 | 138 |       ceil((double)getGridPointsPerAxis()*(offset/length)
 | 
|---|
 | 139 |           -std::numeric_limits<double>::epsilon()*1.e4);
 | 
|---|
 | 140 |   return begin[axis]+factor*getDeltaPerAxis(axis);
 | 
|---|
 | 141 | }
 | 
|---|
 | 142 | 
 | 
|---|
| [cb30d9] | 143 | double SamplingGridProperties::getSurplusLevel(const SamplingGridProperties &_props) const
 | 
|---|
| [ca4b372] | 144 | {
 | 
|---|
 | 145 |   static const double log_two = log(2.);
 | 
|---|
 | 146 |   const double domain_extent =
 | 
|---|
 | 147 |       std::max(getTotalLengthPerAxis(0),
 | 
|---|
 | 148 |           std::max(getTotalLengthPerAxis(1),
 | 
|---|
 | 149 |               getTotalLengthPerAxis(2)));
 | 
|---|
 | 150 |   const double props_extent =
 | 
|---|
 | 151 |       std::max(_props.getTotalLengthPerAxis(0),
 | 
|---|
 | 152 |           std::max(_props.getTotalLengthPerAxis(1),
 | 
|---|
 | 153 |               _props.getTotalLengthPerAxis(2)));
 | 
|---|
 | 154 |   //!> states how many more levels we have because of smaller grid (assuming equal levels)
 | 
|---|
 | 155 |   const double surplus_level = log(domain_extent/props_extent)/log_two;
 | 
|---|
| [cb30d9] | 156 |   return surplus_level;
 | 
|---|
 | 157 | }
 | 
|---|
 | 158 | 
 | 
|---|
 | 159 | bool SamplingGridProperties::isCompatible(const SamplingGridProperties &_props) const
 | 
|---|
 | 160 | {
 | 
|---|
 | 161 |   // only equally sized or finer grids are compatible: we only downsample
 | 
|---|
 | 162 |   const double surplus_level = getSurplusLevel(_props);
 | 
|---|
 | 163 |   if (fabs(surplus_level - round(surplus_level)) > std::numeric_limits<double>::epsilon()*1e4)
 | 
|---|
 | 164 |     return false;
 | 
|---|
 | 165 |   if (level > (_props.level+surplus_level))
 | 
|---|
 | 166 |     return false;
 | 
|---|
 | 167 |   // check whether grid point corners coincide
 | 
|---|
 | 168 |   for (size_t i=0;i<NDIM;++i) {
 | 
|---|
 | 169 |     const double lowercorner = getNearestLowerGridPoint(_props.begin[i], i);
 | 
|---|
 | 170 |     if (fabs(lowercorner - _props.begin[i]) > std::numeric_limits<double>::epsilon()*1e4)
 | 
|---|
 | 171 |       return false;
 | 
|---|
 | 172 |     const double uppercorner = getNearestHigherGridPoint(_props.end[i], i);
 | 
|---|
 | 173 |     if (fabs(uppercorner - _props.end[i]) > std::numeric_limits<double>::epsilon()*1e4)
 | 
|---|
 | 174 |       return false;
 | 
|---|
 | 175 |   }
 | 
|---|
 | 176 |   return true;
 | 
|---|
| [ca4b372] | 177 | }
 | 
|---|
| [028790] | 178 | 
 | 
|---|
| [955051] | 179 | template<> SamplingGridProperties ZeroInstance<SamplingGridProperties>()
 | 
|---|
 | 180 | {
 | 
|---|
 | 181 |   SamplingGridProperties returnvalue;
 | 
|---|
 | 182 |   return returnvalue;
 | 
|---|
 | 183 | }
 | 
|---|
 | 184 | 
 | 
|---|
| [28c025] | 185 | // we need to explicitly instantiate the serialization functions as
 | 
|---|
 | 186 | // its is only serialized through its base class FragmentJob
 | 
|---|
 | 187 | BOOST_CLASS_EXPORT_IMPLEMENT(SamplingGridProperties)
 | 
|---|