[28c025] | 1 | /*
|
---|
| 2 | * Project: MoleCuilder
|
---|
| 3 | * Description: creates and alters molecular systems
|
---|
| 4 | * Copyright (C) 2012 University of Bonn. All rights reserved.
|
---|
| 5 | *
|
---|
| 6 | *
|
---|
| 7 | * This file is part of MoleCuilder.
|
---|
| 8 | *
|
---|
| 9 | * MoleCuilder is free software: you can redistribute it and/or modify
|
---|
| 10 | * it under the terms of the GNU General Public License as published by
|
---|
| 11 | * the Free Software Foundation, either version 2 of the License, or
|
---|
| 12 | * (at your option) any later version.
|
---|
| 13 | *
|
---|
| 14 | * MoleCuilder is distributed in the hope that it will be useful,
|
---|
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 17 | * GNU General Public License for more details.
|
---|
| 18 | *
|
---|
| 19 | * You should have received a copy of the GNU General Public License
|
---|
| 20 | * along with MoleCuilder. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 21 | */
|
---|
| 22 |
|
---|
| 23 | /*
|
---|
| 24 | * SamplingGrid.cpp
|
---|
| 25 | *
|
---|
| 26 | * Created on: 25.07.2012
|
---|
| 27 | * Author: heber
|
---|
| 28 | */
|
---|
| 29 |
|
---|
| 30 | // include config.h
|
---|
| 31 | #ifdef HAVE_CONFIG_H
|
---|
| 32 | #include <config.h>
|
---|
| 33 | #endif
|
---|
| 34 |
|
---|
| 35 | // include headers that implement a archive in simple text format
|
---|
| 36 | // otherwise BOOST_CLASS_EXPORT_IMPLEMENT has no effect
|
---|
| 37 | #include <boost/archive/text_oarchive.hpp>
|
---|
| 38 | #include <boost/archive/text_iarchive.hpp>
|
---|
| 39 |
|
---|
| 40 | #include "CodePatterns/MemDebug.hpp"
|
---|
| 41 |
|
---|
| 42 | #include "Jobs/Grid/SamplingGrid.hpp"
|
---|
| 43 |
|
---|
[98f8fe] | 44 | #include <limits>
|
---|
| 45 |
|
---|
[c889b7] | 46 | #include "CodePatterns/Assert.hpp"
|
---|
[cb3363] | 47 | #include "CodePatterns/Log.hpp"
|
---|
[28c025] | 48 |
|
---|
[1a00bb] | 49 | // static instances
|
---|
| 50 | const double SamplingGrid::zeroOffset[3] = { 0., 0., 0. };
|
---|
| 51 |
|
---|
| 52 | SamplingGrid::SamplingGrid() :
|
---|
| 53 | SamplingGridProperties()
|
---|
| 54 | {
|
---|
| 55 | setWindowSize(zeroOffset, zeroOffset);
|
---|
| 56 | ASSERT( getWindowGridPoints() == (size_t)0,
|
---|
| 57 | "SamplingGrid::SamplingGrid() - incorrect number of samples given for the window.");
|
---|
| 58 | }
|
---|
| 59 |
|
---|
[28c025] | 60 | SamplingGrid::SamplingGrid(const double _begin[3],
|
---|
[3d9a8d] | 61 | const double _end[3],
|
---|
[28c025] | 62 | const int _level,
|
---|
[c889b7] | 63 | const sampledvalues_t &_sampled_grid) :
|
---|
[3d9a8d] | 64 | SamplingGridProperties(_begin, _end, _level),
|
---|
[28c025] | 65 | sampled_grid(_sampled_grid)
|
---|
[1a00bb] | 66 | {
|
---|
| 67 | setWindowSize(_begin, _end);
|
---|
| 68 | ASSERT( getWindowGridPoints() == (size_t)_sampled_grid.size(),
|
---|
| 69 | "SamplingGrid::SamplingGrid() - incorrect number of samples given for the window.");
|
---|
| 70 | }
|
---|
[28c025] | 71 |
|
---|
| 72 | SamplingGrid::SamplingGrid(const SamplingGrid &_grid) :
|
---|
| 73 | SamplingGridProperties(_grid),
|
---|
| 74 | sampled_grid(_grid.sampled_grid)
|
---|
[1a00bb] | 75 | {
|
---|
| 76 | setWindowSize(_grid.begin, _grid.end);
|
---|
| 77 | }
|
---|
[28c025] | 78 |
|
---|
| 79 | SamplingGrid::SamplingGrid(const SamplingGridProperties &_props) :
|
---|
| 80 | SamplingGridProperties(_props)
|
---|
[1a00bb] | 81 | {
|
---|
| 82 | setWindowSize(_props.begin, _props.end);
|
---|
| 83 | }
|
---|
[28c025] | 84 |
|
---|
[c889b7] | 85 | SamplingGrid::SamplingGrid(
|
---|
| 86 | const SamplingGridProperties &_props,
|
---|
| 87 | const sampledvalues_t &_sampled_grid) :
|
---|
| 88 | SamplingGridProperties(_props),
|
---|
| 89 | sampled_grid(_sampled_grid)
|
---|
[1a00bb] | 90 | {
|
---|
| 91 | setWindowSize(_props.begin, _props.end);
|
---|
| 92 | ASSERT( getWindowGridPoints() == (size_t)_sampled_grid.size(),
|
---|
| 93 | "SamplingGrid::SamplingGrid() - incorrect number of samples given for the window.");
|
---|
| 94 | }
|
---|
[c889b7] | 95 |
|
---|
[28c025] | 96 | SamplingGrid::~SamplingGrid()
|
---|
| 97 | {}
|
---|
| 98 |
|
---|
[c889b7] | 99 | SamplingGrid& SamplingGrid::operator=(const SamplingGrid& other)
|
---|
| 100 | {
|
---|
| 101 | // check for self-assignment
|
---|
| 102 | if (this != &other) {
|
---|
| 103 | static_cast<SamplingGridProperties &>(*this) = other;
|
---|
[e2404f] | 104 | setWindowSize(other.begin_window, other.end_window);
|
---|
[c889b7] | 105 | sampled_grid = other.sampled_grid;
|
---|
| 106 | }
|
---|
| 107 | return *this;
|
---|
| 108 | }
|
---|
| 109 |
|
---|
[a1fcc6] | 110 | SamplingGrid& SamplingGrid::operator*=(const SamplingGrid& other)
|
---|
| 111 | {
|
---|
| 112 | // check that grids are compatible
|
---|
| 113 | if (isCompatible(other)) {
|
---|
| 114 | sampledvalues_t::iterator iter = sampled_grid.begin();
|
---|
| 115 | sampledvalues_t::const_iterator otheriter = other.sampled_grid.begin();
|
---|
| 116 | for(; iter != sampled_grid.end(); ++iter, ++otheriter )
|
---|
| 117 | *iter *= (*otheriter);
|
---|
| 118 | } else {
|
---|
| 119 | ASSERT(0, "SamplingGrid::operator*=() - superposing incompatible grids is so far not in the cards.");
|
---|
| 120 | }
|
---|
| 121 | return *this;
|
---|
| 122 | }
|
---|
| 123 |
|
---|
[c889b7] | 124 | void SamplingGrid::superposeOtherGrids(const SamplingGrid &other, const double prefactor)
|
---|
| 125 | {
|
---|
[1a00bb] | 126 | /// check that grids are compatible
|
---|
[c889b7] | 127 | if (isCompatible(other)) {
|
---|
[1a00bb] | 128 | /// get maximum of window
|
---|
| 129 | double max_begin_window[3];
|
---|
| 130 | double max_end_window[3];
|
---|
| 131 | bool doExtend = false;
|
---|
| 132 | for (size_t index=0; index<3;++index) {
|
---|
| 133 | if (begin_window[index] >= other.begin_window[index]) {
|
---|
| 134 | max_begin_window[index] = other.begin_window[index];
|
---|
| 135 | doExtend = true;
|
---|
| 136 | } else {
|
---|
| 137 | max_begin_window[index] = begin_window[index];
|
---|
| 138 | }
|
---|
| 139 | if (end_window[index] >= other.end_window[index]) {
|
---|
| 140 | max_end_window[index] = end_window[index];
|
---|
| 141 | } else {
|
---|
| 142 | max_end_window[index] = other.end_window[index];
|
---|
| 143 | doExtend = true;
|
---|
| 144 | }
|
---|
| 145 | }
|
---|
| 146 | if (doExtend)
|
---|
| 147 | extendWindow(max_begin_window, max_end_window);
|
---|
| 148 | /// and copy other into larger window, too
|
---|
| 149 | addOntoWindow(other.begin_window, other.end_window, other.sampled_grid);
|
---|
[c889b7] | 150 | // then add other's grid onto this one
|
---|
| 151 | sampledvalues_t::iterator iter = sampled_grid.begin();
|
---|
| 152 | sampledvalues_t::const_iterator otheriter = other.sampled_grid.begin();
|
---|
| 153 | for(; iter != sampled_grid.end(); ++iter, ++otheriter )
|
---|
| 154 | *iter += prefactor * (*otheriter);
|
---|
| 155 | } else {
|
---|
| 156 | ASSERT(0, "SamplingGrid::superposeOtherGrids() - superposing incompatible grids is so far not in the cards.");
|
---|
| 157 | }
|
---|
| 158 | }
|
---|
| 159 |
|
---|
[1a00bb] | 160 | const double SamplingGrid::getVolume() const
|
---|
[3d9a8d] | 161 | {
|
---|
| 162 | double volume = 1.;
|
---|
| 163 | for (size_t i=0;i<3;++i)
|
---|
| 164 | volume *= end[i]-begin[i];
|
---|
| 165 | return volume;
|
---|
| 166 | }
|
---|
| 167 |
|
---|
[1a00bb] | 168 | const double SamplingGrid::getWindowVolume() const
|
---|
| 169 | {
|
---|
| 170 | double volume = 1.;
|
---|
| 171 | for (size_t i=0;i<3;++i)
|
---|
| 172 | volume *= end_window[i]-begin_window[i];
|
---|
| 173 | return volume;
|
---|
| 174 | }
|
---|
| 175 |
|
---|
[98f8fe] | 176 | const size_t SamplingGrid::getTotalGridPoints() const
|
---|
| 177 | {
|
---|
| 178 | return pow(getGridPointsPerAxis(),3);
|
---|
| 179 | }
|
---|
| 180 |
|
---|
| 181 | const size_t SamplingGrid::getGridPointsPerAxis() const
|
---|
| 182 | {
|
---|
| 183 | return pow(2, level);
|
---|
| 184 | }
|
---|
| 185 |
|
---|
[1a00bb] | 186 | const size_t SamplingGrid::getWindowGridPoints() const
|
---|
| 187 | {
|
---|
| 188 | const double volume = getVolume();
|
---|
| 189 | if (fabs(volume) < std::numeric_limits<double>::epsilon())
|
---|
| 190 | return 0;
|
---|
| 191 | else
|
---|
| 192 | return (getWindowVolume()*getTotalGridPoints())/volume;
|
---|
| 193 | }
|
---|
| 194 |
|
---|
[cb3363] | 195 | double SamplingGrid::integral() const
|
---|
| 196 | {
|
---|
[98f8fe] | 197 | const double volume_element = getVolume()/(double)getTotalGridPoints();
|
---|
[cb3363] | 198 | double int_value = 0.;
|
---|
| 199 | for (sampledvalues_t::const_iterator iter = sampled_grid.begin();
|
---|
| 200 | iter != sampled_grid.end();
|
---|
| 201 | ++iter)
|
---|
| 202 | int_value += *iter;
|
---|
| 203 | int_value *= volume_element;
|
---|
| 204 | LOG(2, "DEBUG: SamplingGrid::integral() is " << scientific << setprecision(13) << int_value << ".");
|
---|
| 205 | return int_value;
|
---|
| 206 | }
|
---|
| 207 |
|
---|
[e72c61] | 208 | double SamplingGrid::integral(const SamplingGrid &weight) const
|
---|
| 209 | {
|
---|
| 210 | if (isCompatible(weight)) {
|
---|
[98f8fe] | 211 | const double volume_element = getVolume()/(double)getTotalGridPoints();
|
---|
[e72c61] | 212 | double int_value = 0.;
|
---|
| 213 | sampledvalues_t::const_iterator iter = sampled_grid.begin();
|
---|
| 214 | sampledvalues_t::const_iterator weightiter = weight.sampled_grid.begin();
|
---|
| 215 | for (;iter != sampled_grid.end();++iter,++weightiter)
|
---|
| 216 | int_value += (*weightiter) * (*iter);
|
---|
| 217 | int_value *= volume_element;
|
---|
| 218 | //LOG(2, "DEBUG: SamplingGrid::integral() is " << scientific << setprecision(13) << int_value << ".");
|
---|
| 219 | return int_value;
|
---|
| 220 | } else
|
---|
| 221 | return 0.;
|
---|
| 222 | }
|
---|
| 223 |
|
---|
[1a00bb] | 224 | void SamplingGrid::setWindowSize(
|
---|
| 225 | const double _begin_window[3],
|
---|
| 226 | const double _end_window[3])
|
---|
| 227 | {
|
---|
| 228 | for (size_t index=0;index<3;++index) {
|
---|
[e2404f] | 229 | ASSERT( _begin_window[index] >= begin[index],
|
---|
| 230 | "SamplingGrid::setWindowSize() - window starts earlier than domain on "
|
---|
| 231 | +toString(index)+"th component.");
|
---|
[1a00bb] | 232 | begin_window[index] = _begin_window[index];
|
---|
[e2404f] | 233 | ASSERT( _end_window[index] <= end[index],
|
---|
| 234 | "SamplingGrid::setWindowSize() - window ends later than domain on "
|
---|
| 235 | +toString(index)+"th component.");
|
---|
[1a00bb] | 236 | end_window[index] = _end_window[index];
|
---|
| 237 | }
|
---|
| 238 | }
|
---|
| 239 |
|
---|
| 240 | void SamplingGrid::setWindow(
|
---|
| 241 | const double _begin_window[3],
|
---|
| 242 | const double _end_window[3])
|
---|
| 243 | {
|
---|
| 244 | setWindowSize(_begin_window, _end_window);
|
---|
| 245 | const size_t gridpoints_window = getWindowGridPoints();
|
---|
| 246 | sampled_grid.resize(gridpoints_window, 0.);
|
---|
| 247 | }
|
---|
| 248 |
|
---|
[e2404f] | 249 | void SamplingGrid::setDomainSize(
|
---|
| 250 | const double _begin[3],
|
---|
| 251 | const double _end[3])
|
---|
[1a00bb] | 252 | {
|
---|
| 253 | for (size_t index=0;index<3;++index) {
|
---|
[e2404f] | 254 | begin[index] = _begin[index];
|
---|
| 255 | end[index] = _end[index];
|
---|
[1a00bb] | 256 | }
|
---|
| 257 | }
|
---|
| 258 |
|
---|
[e2404f] | 259 | void SamplingGrid::setDomain(
|
---|
| 260 | const double _begin[3],
|
---|
| 261 | const double _end[3])
|
---|
| 262 | {
|
---|
| 263 | setDomainSize(_begin, _end);
|
---|
| 264 | setWindowSize(_begin, _end);
|
---|
| 265 | const size_t gridpoints = getTotalGridPoints();
|
---|
| 266 | sampled_grid.resize(gridpoints, 0.);
|
---|
| 267 | }
|
---|
| 268 |
|
---|
[1a00bb] | 269 | void SamplingGrid::extendWindow(
|
---|
| 270 | const double _begin_window[3],
|
---|
| 271 | const double _end_window[3])
|
---|
| 272 | {
|
---|
| 273 | // check that we truly have to extend the window
|
---|
| 274 | #ifndef NDEBUG
|
---|
| 275 | for(size_t index=0;index < 3; ++index) {
|
---|
| 276 | ASSERT ( begin_window[index] >= _begin_window[index],
|
---|
| 277 | "SamplingGrid::extendWindow() - component "+toString(index)+
|
---|
| 278 | " of window start is greater than old value.");
|
---|
| 279 | ASSERT ( end_window[index] <= _end_window[index],
|
---|
| 280 | "SamplingGrid::extendWindow() - component "+toString(index)+
|
---|
| 281 | " of window end is less than old value.");
|
---|
| 282 | }
|
---|
| 283 | #endif
|
---|
| 284 | // copy old window size and values
|
---|
| 285 | double old_begin_window[3];
|
---|
| 286 | double old_end_window[3];
|
---|
| 287 | for(size_t index=0;index<3;++index) {
|
---|
| 288 | old_begin_window[index] = begin_window[index];
|
---|
| 289 | old_end_window[index] = end_window[index];
|
---|
| 290 | }
|
---|
| 291 | sampledvalues_t old_values(sampled_grid);
|
---|
| 292 | // now extend it ...
|
---|
| 293 | addOntoWindow(old_begin_window, old_end_window, old_values);
|
---|
| 294 | }
|
---|
| 295 |
|
---|
| 296 | void SamplingGrid::addOntoWindow(
|
---|
| 297 | const double _begin_window[3],
|
---|
| 298 | const double _end_window[3],
|
---|
| 299 | const sampledvalues_t &_sampled_grid)
|
---|
| 300 | {
|
---|
| 301 | #ifndef NDEBUG
|
---|
| 302 | for(size_t index=0;index<3;++index) {
|
---|
| 303 | ASSERT( _begin_window[index] >= begin_window[index],
|
---|
| 304 | "SamplingGrid::addOntoWindow() - given window starts earlier in component "
|
---|
| 305 | +toString(index)+".");
|
---|
| 306 | ASSERT( _end_window[index] <= end_window[index],
|
---|
| 307 | "SamplingGrid::addOntoWindow() - given window ends later in component "
|
---|
| 308 | +toString(index)+".");
|
---|
| 309 | }
|
---|
| 310 | #endif
|
---|
| 311 | // the only issue are indices
|
---|
| 312 | const size_t gridpoints_axis = pow(2, level);
|
---|
| 313 | size_t offset[3];
|
---|
| 314 | size_t length[3];
|
---|
| 315 | for(size_t index=0;index<3;++index) {
|
---|
| 316 | offset[index] = gridpoints_axis*(_begin_window[index] - begin_window[index]);
|
---|
| 317 | length[index] = gridpoints_axis*(_end_window[index] - _begin_window[index]);
|
---|
| 318 | }
|
---|
| 319 | ASSERT( length[0]*length[1]*length[2] == _sampled_grid.size(),
|
---|
| 320 | "SamplingGrid::addOntoWindow() - not enough sampled values given.");
|
---|
| 321 | size_t N[3];
|
---|
| 322 | // sampledvalues_t::iterator griditer = sampled_grid.begin();
|
---|
| 323 | sampledvalues_t::const_iterator copyiter = _sampled_grid.begin();
|
---|
| 324 | for(N[0]=offset[0]; N[0] < length[0]; ++N[0]) {
|
---|
| 325 | for(N[1]=offset[1]; N[1] < length[1]; ++N[1]) {
|
---|
| 326 | for(N[2]=offset[2]; N[2] < length[2]; ++N[2]) {
|
---|
| 327 | const size_t index =
|
---|
| 328 | (N[0]*gridpoints_axis+N[1])*gridpoints_axis+N[2];
|
---|
| 329 | sampled_grid[index] += *copyiter;
|
---|
| 330 | }
|
---|
| 331 | }
|
---|
| 332 | }
|
---|
| 333 | }
|
---|
| 334 |
|
---|
[c889b7] | 335 | std::ostream & operator<<(std::ostream &ost, const SamplingGrid& other)
|
---|
| 336 | {
|
---|
[1a00bb] | 337 | ost << "SamplingGrid";
|
---|
| 338 | ost << " starting at " << other.begin[0] << "," << other.begin[1] << "," << other.begin[2];
|
---|
| 339 | ost << " ending at " << other.end[0] << "," << other.end[1] << "," << other.end[2];
|
---|
| 340 | ost << ", window starting at " << other.begin_window[0] << "," << other.begin_window[1] << "," << other.begin_window[2];
|
---|
| 341 | ost << ", window ending at " << other.end_window[0] << "," << other.end_window[1] << "," << other.end_window[2];
|
---|
[80959a1] | 342 | ost << ", level of " << other.level;
|
---|
| 343 | ost << " and integrated value of " << other.integral();
|
---|
[c889b7] | 344 | return ost;
|
---|
| 345 | }
|
---|
| 346 |
|
---|
[beb16e] | 347 | template<> SamplingGrid ZeroInstance<SamplingGrid>()
|
---|
| 348 | {
|
---|
| 349 | SamplingGrid returnvalue;
|
---|
| 350 | return returnvalue;
|
---|
| 351 | }
|
---|
| 352 |
|
---|
[28c025] | 353 | // we need to explicitly instantiate the serialization functions as
|
---|
| 354 | // its is only serialized through its base class FragmentJob
|
---|
| 355 | BOOST_CLASS_EXPORT_IMPLEMENT(SamplingGrid)
|
---|