| 1 | /*
 | 
|---|
| 2 |  * Project: MoleCuilder
 | 
|---|
| 3 |  * Description: creates and alters molecular systems
 | 
|---|
| 4 |  * Copyright (C)  2012 University of Bonn. All rights reserved.
 | 
|---|
| 5 |  * Copyright (C)  2013 Frederik Heber. All rights reserved.
 | 
|---|
| 6 |  * Please see the COPYING file or "Copyright notice" in builder.cpp for details.
 | 
|---|
| 7 |  * 
 | 
|---|
| 8 |  *
 | 
|---|
| 9 |  *   This file is part of MoleCuilder.
 | 
|---|
| 10 |  *
 | 
|---|
| 11 |  *    MoleCuilder is free software: you can redistribute it and/or modify
 | 
|---|
| 12 |  *    it under the terms of the GNU General Public License as published by
 | 
|---|
| 13 |  *    the Free Software Foundation, either version 2 of the License, or
 | 
|---|
| 14 |  *    (at your option) any later version.
 | 
|---|
| 15 |  *
 | 
|---|
| 16 |  *    MoleCuilder is distributed in the hope that it will be useful,
 | 
|---|
| 17 |  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
|---|
| 18 |  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
|---|
| 19 |  *    GNU General Public License for more details.
 | 
|---|
| 20 |  *
 | 
|---|
| 21 |  *    You should have received a copy of the GNU General Public License
 | 
|---|
| 22 |  *    along with MoleCuilder.  If not, see <http://www.gnu.org/licenses/>. 
 | 
|---|
| 23 |  */
 | 
|---|
| 24 | 
 | 
|---|
| 25 | /*
 | 
|---|
| 26 |  * WindowGrid_converter.cpp
 | 
|---|
| 27 |  *
 | 
|---|
| 28 |  *  Created on: Dec 20, 2012
 | 
|---|
| 29 |  *      Author: heber
 | 
|---|
| 30 |  */
 | 
|---|
| 31 | 
 | 
|---|
| 32 | 
 | 
|---|
| 33 | // include config.h
 | 
|---|
| 34 | #ifdef HAVE_CONFIG_H
 | 
|---|
| 35 | #include <config.h>
 | 
|---|
| 36 | #endif
 | 
|---|
| 37 | 
 | 
|---|
| 38 | #include "grid/grid.hpp"
 | 
|---|
| 39 | 
 | 
|---|
| 40 | #include "WindowGrid_converter.hpp"
 | 
|---|
| 41 | 
 | 
|---|
| 42 | #include "CodePatterns/MemDebug.hpp"
 | 
|---|
| 43 | 
 | 
|---|
| 44 | #include <iostream>
 | 
|---|
| 45 | #include <iterator>
 | 
|---|
| 46 | #include <limits>
 | 
|---|
| 47 | 
 | 
|---|
| 48 | #include "CodePatterns/Assert.hpp"
 | 
|---|
| 49 | #include "CodePatterns/Log.hpp"
 | 
|---|
| 50 | 
 | 
|---|
| 51 | #include "Fragmentation/Summation/SetValues/SamplingGrid.hpp"
 | 
|---|
| 52 | 
 | 
|---|
| 53 | void WindowGrid_converter::addGridOntoWindow(
 | 
|---|
| 54 |     VMG::Grid &grid,
 | 
|---|
| 55 |     SamplingGrid &window,
 | 
|---|
| 56 |     const double prefactor)
 | 
|---|
| 57 | {
 | 
|---|
| 58 | #ifndef NDEBUG
 | 
|---|
| 59 |   for(size_t index=0;index<3;++index) {
 | 
|---|
| 60 |     ASSERT( window.begin[index] >= window.begin_window[index],
 | 
|---|
| 61 |         "InterfaceVMGJob::addGridOntoWindow() - given grid starts earlier than window in component "
 | 
|---|
| 62 |         +toString(index)+".");
 | 
|---|
| 63 |     ASSERT( window.end[index] <= window.end_window[index],
 | 
|---|
| 64 |         "InterfaceVMGJob::addGridOntoWindow() - given grid ends later than window in component "
 | 
|---|
| 65 |         +toString(index)+".");
 | 
|---|
| 66 |   }
 | 
|---|
| 67 | #endif
 | 
|---|
| 68 |   // the only issue are indices
 | 
|---|
| 69 |   const size_t gridpoints_axis = window.getGridPointsPerAxis();
 | 
|---|
| 70 |   size_t pre_offset[3];
 | 
|---|
| 71 |   size_t post_offset[3];
 | 
|---|
| 72 |   size_t length[3];
 | 
|---|
| 73 |   size_t total[3];
 | 
|---|
| 74 |   const double round_offset =
 | 
|---|
| 75 |       (std::numeric_limits<size_t>::round_style == std::round_toward_zero) ?
 | 
|---|
| 76 |           0.5 : 0.; // need offset to get to round_toward_nearest behavior
 | 
|---|
| 77 |   for(size_t index=0;index<3;++index) {
 | 
|---|
| 78 |     const double delta = (double)gridpoints_axis/(window.end[index] - window.begin[index]);
 | 
|---|
| 79 |     // delta is conversion factor from box length to discrete length, i.e. number of points
 | 
|---|
| 80 |     pre_offset[index] = delta*(window.begin[index] - window.begin_window[index])+round_offset;
 | 
|---|
| 81 |     length[index] = delta*(window.end[index] - window.begin[index])+round_offset;
 | 
|---|
| 82 |     post_offset[index] = delta*(window.end_window[index] - window.end[index])+round_offset;
 | 
|---|
| 83 |     total[index] = delta*(window.end_window[index] - window.begin_window[index])+round_offset;
 | 
|---|
| 84 |     // total is used as safe-guard against loss due to discrete conversion
 | 
|---|
| 85 |     ASSERT( pre_offset[index]+post_offset[index]+length[index] == total[index],
 | 
|---|
| 86 |         "InterfaceVMGJob::addGridOntoWindow() - pre, post, and length don't sum up to total for "
 | 
|---|
| 87 |         +toString(index)+"th component.");
 | 
|---|
| 88 |   }
 | 
|---|
| 89 | #ifndef NDEBUG
 | 
|---|
| 90 |   const size_t calculated_size = length[0]*length[1]*length[2];
 | 
|---|
| 91 | //  const size_t given_size = std::distance(grid_begin, grid_end);
 | 
|---|
| 92 | //  ASSERT( calculated_size == given_size,
 | 
|---|
| 93 | //      "InterfaceVMGJob::addGridOntoWindow() - not enough sampled values given: "
 | 
|---|
| 94 | //      +toString(calculated_size)+" != "+toString(given_size)+".");
 | 
|---|
| 95 |   ASSERT( calculated_size <=  window.sampled_grid.size(),
 | 
|---|
| 96 |       "InterfaceVMGJob::addGridOntoWindow() - not enough sampled values available: "
 | 
|---|
| 97 |       +toString(calculated_size)+" <= "+toString(window.sampled_grid.size())+".");
 | 
|---|
| 98 |   const size_t total_size = total[0]*total[1]*total[2];
 | 
|---|
| 99 |   ASSERT( total_size == window.sampled_grid.size(),
 | 
|---|
| 100 |       "InterfaceVMGJob::addGridOntoWindow() - total size is not equal to number of present points: "
 | 
|---|
| 101 |       +toString(total_size)+" != "+toString(window.sampled_grid.size())+".");
 | 
|---|
| 102 | #endif
 | 
|---|
| 103 |   size_t N[3];
 | 
|---|
| 104 |   SamplingGrid::sampledvalues_t::iterator griditer = window.sampled_grid.begin();
 | 
|---|
| 105 |   std::advance(griditer, pre_offset[0]*total[1]*total[2]);
 | 
|---|
| 106 |   VMG::Grid::iterator copyiter = grid.Iterators().Local().Begin();
 | 
|---|
| 107 |   for(N[0]=0; N[0] < length[0]; ++N[0]) {
 | 
|---|
| 108 |     std::advance(griditer, pre_offset[1]*total[2]);
 | 
|---|
| 109 |     for(N[1]=0; N[1] < length[1]; ++N[1]) {
 | 
|---|
| 110 |       std::advance(griditer, pre_offset[2]);
 | 
|---|
| 111 |       for(N[2]=0; N[2] < length[2]; ++N[2]) {
 | 
|---|
| 112 |         ASSERT( griditer != window.sampled_grid.end(),
 | 
|---|
| 113 |             "InterfaceVMGJob::addGridOntoWindow() - griditer is already at end of window.");
 | 
|---|
| 114 |         ASSERT( copyiter != grid.Iterators().Local().End(),
 | 
|---|
| 115 |             "InterfaceVMGJob::addGridOntoWindow() - griditer is already at end of window.");
 | 
|---|
| 116 |         *griditer++ += prefactor*grid(*copyiter++);
 | 
|---|
| 117 |       }
 | 
|---|
| 118 |       std::advance(griditer, post_offset[2]);
 | 
|---|
| 119 |     }
 | 
|---|
| 120 |     std::advance(griditer, post_offset[1]*total[2]);
 | 
|---|
| 121 |   }
 | 
|---|
| 122 | #ifndef NDEBUG
 | 
|---|
| 123 |   std::advance(griditer, post_offset[0]*total[1]*total[2]);
 | 
|---|
| 124 |   ASSERT( griditer == window.sampled_grid.end(),
 | 
|---|
| 125 |       "InterfaceVMGJob::addGridOntoWindow() - griditer is not at end of window.");
 | 
|---|
| 126 |   ASSERT( copyiter == grid.Iterators().Local().End(),
 | 
|---|
| 127 |       "InterfaceVMGJob::addGridOntoWindow() - copyiter is not at end of window.");
 | 
|---|
| 128 | #endif
 | 
|---|
| 129 | }
 | 
|---|
| 130 | 
 | 
|---|
| 131 | void WindowGrid_converter::addWindowOntoGrid(
 | 
|---|
| 132 |     VMG::Grid& window,
 | 
|---|
| 133 |     const SamplingGrid &grid,
 | 
|---|
| 134 |     const double prefactor)
 | 
|---|
| 135 | {
 | 
|---|
| 136 | #ifndef NDEBUG
 | 
|---|
| 137 |   for(size_t index=0;index<3;++index) {
 | 
|---|
| 138 |     ASSERT( grid.begin_window[index] >= grid.begin[index],
 | 
|---|
| 139 |         "InterfaceVMGJob::addWindowOntoGrid() - given window starts earlier than grid in component "
 | 
|---|
| 140 |         +toString(index)+".");
 | 
|---|
| 141 |     ASSERT( grid.end_window[index] <= grid.end[index],
 | 
|---|
| 142 |         "InterfaceVMGJob::addWindowOntoGrid() - given window ends later than grid in component "
 | 
|---|
| 143 |         +toString(index)+".");
 | 
|---|
| 144 |   }
 | 
|---|
| 145 | #endif
 | 
|---|
| 146 |   // the only issue are indices
 | 
|---|
| 147 |   const size_t gridpoints_axis = grid.getGridPointsPerAxis();
 | 
|---|
| 148 |   size_t pre_offset[3];
 | 
|---|
| 149 |   size_t post_offset[3];
 | 
|---|
| 150 |   size_t length[3];
 | 
|---|
| 151 |   size_t total[3];
 | 
|---|
| 152 |   const double round_offset =
 | 
|---|
| 153 |       (std::numeric_limits<size_t>::round_style == std::round_toward_zero) ?
 | 
|---|
| 154 |           0.5 : 0.; // need offset to get to round_toward_nearest behavior
 | 
|---|
| 155 |   for(size_t index=0;index<3;++index) {
 | 
|---|
| 156 |     const double delta = (double)gridpoints_axis/(grid.end[index] - grid.begin[index]);
 | 
|---|
| 157 |     // delta is conversion factor from box length to discrete length, i.e. number of points
 | 
|---|
| 158 |     pre_offset[index] = delta*(grid.begin_window[index] - grid.begin[index])+round_offset;
 | 
|---|
| 159 |     length[index] = delta*(grid.end_window[index] - grid.begin_window[index])+round_offset;
 | 
|---|
| 160 |     post_offset[index] = delta*(grid.end[index] - grid.end_window[index])+round_offset;
 | 
|---|
| 161 |     total[index] = delta*(grid.end[index] - grid.begin[index])+round_offset;
 | 
|---|
| 162 |     // total is used as safe-guard against loss due to discrete conversion
 | 
|---|
| 163 |     ASSERT( pre_offset[index]+post_offset[index]+length[index] == total[index],
 | 
|---|
| 164 |         "InterfaceVMGJob::addWindowOntoGrid() - pre, post, and length don't sum up to total for "
 | 
|---|
| 165 |         +toString(index)+"th component.");
 | 
|---|
| 166 |   }
 | 
|---|
| 167 | #ifndef NDEBUG
 | 
|---|
| 168 |   const size_t calculated_size = length[0]*length[1]*length[2];
 | 
|---|
| 169 |   ASSERT( calculated_size == grid.sampled_grid.size(),
 | 
|---|
| 170 |       "InterfaceVMGJob::addWindowOntoGrid() - not enough sampled values given: "
 | 
|---|
| 171 |       +toString(calculated_size)+" != "+toString(grid.sampled_grid.size())+".");
 | 
|---|
| 172 | //  ASSERT( calculated_size <=  given_size,
 | 
|---|
| 173 | //      "InterfaceVMGJob::addWindowOntoGrid() - not enough sampled values available: "
 | 
|---|
| 174 | //      +toString(calculated_size)+" <= "+toString(given_size)+".");
 | 
|---|
| 175 | //  const size_t total_size = total[0]*total[1]*total[2];
 | 
|---|
| 176 | //  ASSERT( total_size == given_size,
 | 
|---|
| 177 | //      "InterfaceVMGJob::addWindowOntoGrid() - total size is not equal to number of present points: "
 | 
|---|
| 178 | //      +toString(total_size)+" != "+toString(given_size)+".");
 | 
|---|
| 179 | #endif
 | 
|---|
| 180 |   size_t N[3];
 | 
|---|
| 181 |   VMG::Grid::iterator griditer = window.Iterators().Local().Begin();
 | 
|---|
| 182 | //  griditer.advance(pre_offset[0]*total[1]*total[2]);
 | 
|---|
| 183 |   for(N[0]=0; N[0] < pre_offset[0]; ++N[0])
 | 
|---|
| 184 |     for(N[1]=0; N[1] < total[1]; ++N[1])
 | 
|---|
| 185 |       for(N[2]=0; N[2] < total[2]; ++N[2]) {
 | 
|---|
| 186 |         ASSERT( griditer != window.Iterators().Local().End(),
 | 
|---|
| 187 |             "InterfaceVMGJob::addWindowOntoGrid() - griditer is already at end of window.");
 | 
|---|
| 188 |         window(*griditer++) = 0.;
 | 
|---|
| 189 |       }
 | 
|---|
| 190 |   SamplingGrid::sampledvalues_t::const_iterator copyiter = grid.sampled_grid.begin();
 | 
|---|
| 191 |   for(N[0]=0; N[0] < length[0]; ++N[0]) {
 | 
|---|
| 192 | //    griditer.advance(pre_offset[1]*total[2]);
 | 
|---|
| 193 |       for(N[1]=0; N[1] < pre_offset[1]; ++N[1])
 | 
|---|
| 194 |         for(N[2]=0; N[2] < total[2]; ++N[2]) {
 | 
|---|
| 195 |           ASSERT( griditer != window.Iterators().Local().End(),
 | 
|---|
| 196 |               "InterfaceVMGJob::addWindowOntoGrid() - griditer is already at end of window.");
 | 
|---|
| 197 |           window(*griditer++) = 0.;
 | 
|---|
| 198 |         }
 | 
|---|
| 199 |     for(N[1]=0; N[1] < length[1]; ++N[1]) {
 | 
|---|
| 200 | //      griditer.advance(pre_offset[2]);
 | 
|---|
| 201 |         for(N[2]=0; N[2] < pre_offset[2]; ++N[2])
 | 
|---|
| 202 |           window(*griditer++) = 0.;
 | 
|---|
| 203 |       for(N[2]=0; N[2] < length[2]; ++N[2]) {
 | 
|---|
| 204 |         ASSERT( griditer != window.Iterators().Local().End(),
 | 
|---|
| 205 |             "InterfaceVMGJob::addWindowOntoGrid() - griditer is already at end of window.");
 | 
|---|
| 206 |         ASSERT( copyiter != grid.sampled_grid.end(),
 | 
|---|
| 207 |             "InterfaceVMGJob::addWindowOntoGrid() - griditer is already at end of window.");
 | 
|---|
| 208 |         window(*griditer++) += prefactor*(*copyiter++);
 | 
|---|
| 209 |       }
 | 
|---|
| 210 | //      griditer.advance(post_offset[2]);
 | 
|---|
| 211 |       for(N[2]=0; N[2] < post_offset[2]; ++N[2]) {
 | 
|---|
| 212 |         ASSERT( griditer != window.Iterators().Local().End(),
 | 
|---|
| 213 |             "InterfaceVMGJob::addWindowOntoGrid() - griditer is already at end of window.");
 | 
|---|
| 214 |         window(*griditer++) = 0.;
 | 
|---|
| 215 |       }
 | 
|---|
| 216 |     }
 | 
|---|
| 217 | //    griditer.advance(post_offset[1]*total[2]);
 | 
|---|
| 218 |     for(N[1]=0; N[1] < post_offset[1]; ++N[1])
 | 
|---|
| 219 |       for(N[2]=0; N[2] < total[2]; ++N[2]) {
 | 
|---|
| 220 |         ASSERT( griditer != window.Iterators().Local().End(),
 | 
|---|
| 221 |             "InterfaceVMGJob::addWindowOntoGrid() - griditer is already at end of window.");
 | 
|---|
| 222 |         window(*griditer++) = 0.;
 | 
|---|
| 223 |       }
 | 
|---|
| 224 |   }
 | 
|---|
| 225 |   for(N[0]=0; N[0] < post_offset[0]; ++N[0])
 | 
|---|
| 226 |     for(N[1]=0; N[1] < total[1]; ++N[1])
 | 
|---|
| 227 |       for(N[2]=0; N[2] < total[2]; ++N[2]) {
 | 
|---|
| 228 |         ASSERT( griditer != window.Iterators().Local().End(),
 | 
|---|
| 229 |             "InterfaceVMGJob::addWindowOntoGrid() - griditer is already at end of window.");
 | 
|---|
| 230 |         window(*griditer++) = 0.;
 | 
|---|
| 231 |       }
 | 
|---|
| 232 | #ifndef NDEBUG
 | 
|---|
| 233 | //  griditer.advance(post_offset[0]*total[1]*total[2]);
 | 
|---|
| 234 |   ASSERT( griditer == window.Iterators().Local().End(),
 | 
|---|
| 235 |       "InterfaceVMGJob::addWindowOntoGrid() - griditer is not at end of window.");
 | 
|---|
| 236 |   ASSERT( copyiter == grid.sampled_grid.end(),
 | 
|---|
| 237 |       "InterfaceVMGJob::addWindowOntoGrid() - copyiter is not at end of window.");
 | 
|---|
| 238 | #endif
 | 
|---|
| 239 | //  LOG(2, "DEBUG: Grid after adding other is " << grid << ".");
 | 
|---|
| 240 | }
 | 
|---|