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