| [b123a5] | 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. | 
|---|
| [b123a5] | 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 |  | 
|---|
| [fbf143] | 51 | #include "Fragmentation/Summation/SetValues/SamplingGrid.hpp" | 
|---|
| [b123a5] | 52 |  | 
|---|
|  | 53 | void WindowGrid_converter::addGridOntoWindow( | 
|---|
|  | 54 | VMG::Grid &grid, | 
|---|
|  | 55 | SamplingGrid &window, | 
|---|
| [d9d028] | 56 | const double prefactor, | 
|---|
|  | 57 | const bool OpenBoundaryConditions) | 
|---|
| [b123a5] | 58 | { | 
|---|
|  | 59 | #ifndef NDEBUG | 
|---|
|  | 60 | for(size_t index=0;index<3;++index) { | 
|---|
|  | 61 | ASSERT( window.begin[index] >= window.begin_window[index], | 
|---|
|  | 62 | "InterfaceVMGJob::addGridOntoWindow() - given grid starts earlier than window in component " | 
|---|
|  | 63 | +toString(index)+"."); | 
|---|
|  | 64 | ASSERT( window.end[index] <= window.end_window[index], | 
|---|
|  | 65 | "InterfaceVMGJob::addGridOntoWindow() - given grid ends later than window in component " | 
|---|
|  | 66 | +toString(index)+"."); | 
|---|
|  | 67 | } | 
|---|
|  | 68 | #endif | 
|---|
|  | 69 | // the only issue are indices | 
|---|
|  | 70 | const size_t gridpoints_axis = window.getGridPointsPerAxis(); | 
|---|
|  | 71 | size_t pre_offset[3]; | 
|---|
|  | 72 | size_t post_offset[3]; | 
|---|
|  | 73 | size_t length[3]; | 
|---|
|  | 74 | size_t total[3]; | 
|---|
|  | 75 | const double round_offset = | 
|---|
|  | 76 | (std::numeric_limits<size_t>::round_style == std::round_toward_zero) ? | 
|---|
|  | 77 | 0.5 : 0.; // need offset to get to round_toward_nearest behavior | 
|---|
|  | 78 | for(size_t index=0;index<3;++index) { | 
|---|
|  | 79 | const double delta = (double)gridpoints_axis/(window.end[index] - window.begin[index]); | 
|---|
|  | 80 | // delta is conversion factor from box length to discrete length, i.e. number of points | 
|---|
|  | 81 | pre_offset[index] = delta*(window.begin[index] - window.begin_window[index])+round_offset; | 
|---|
|  | 82 | length[index] = delta*(window.end[index] - window.begin[index])+round_offset; | 
|---|
|  | 83 | post_offset[index] = delta*(window.end_window[index] - window.end[index])+round_offset; | 
|---|
|  | 84 | total[index] = delta*(window.end_window[index] - window.begin_window[index])+round_offset; | 
|---|
|  | 85 | // total is used as safe-guard against loss due to discrete conversion | 
|---|
|  | 86 | ASSERT( pre_offset[index]+post_offset[index]+length[index] == total[index], | 
|---|
|  | 87 | "InterfaceVMGJob::addGridOntoWindow() - pre, post, and length don't sum up to total for " | 
|---|
|  | 88 | +toString(index)+"th component."); | 
|---|
|  | 89 | } | 
|---|
|  | 90 | #ifndef NDEBUG | 
|---|
|  | 91 | const size_t calculated_size = length[0]*length[1]*length[2]; | 
|---|
|  | 92 | //  const size_t given_size = std::distance(grid_begin, grid_end); | 
|---|
|  | 93 | //  ASSERT( calculated_size == given_size, | 
|---|
|  | 94 | //      "InterfaceVMGJob::addGridOntoWindow() - not enough sampled values given: " | 
|---|
|  | 95 | //      +toString(calculated_size)+" != "+toString(given_size)+"."); | 
|---|
|  | 96 | ASSERT( calculated_size <=  window.sampled_grid.size(), | 
|---|
|  | 97 | "InterfaceVMGJob::addGridOntoWindow() - not enough sampled values available: " | 
|---|
|  | 98 | +toString(calculated_size)+" <= "+toString(window.sampled_grid.size())+"."); | 
|---|
|  | 99 | const size_t total_size = total[0]*total[1]*total[2]; | 
|---|
|  | 100 | ASSERT( total_size == window.sampled_grid.size(), | 
|---|
|  | 101 | "InterfaceVMGJob::addGridOntoWindow() - total size is not equal to number of present points: " | 
|---|
|  | 102 | +toString(total_size)+" != "+toString(window.sampled_grid.size())+"."); | 
|---|
|  | 103 | #endif | 
|---|
|  | 104 | size_t N[3]; | 
|---|
|  | 105 | SamplingGrid::sampledvalues_t::iterator griditer = window.sampled_grid.begin(); | 
|---|
|  | 106 | std::advance(griditer, pre_offset[0]*total[1]*total[2]); | 
|---|
| [d9d028] | 107 |  | 
|---|
| [b123a5] | 108 | VMG::Grid::iterator copyiter = grid.Iterators().Local().Begin(); | 
|---|
| [d9d028] | 109 | const VMG::Index size = grid.Local().Size(); | 
|---|
|  | 110 | if (OpenBoundaryConditions) | 
|---|
|  | 111 | for(N[0]=0; N[0] < size[0]/4; ++N[0]) | 
|---|
|  | 112 | for(N[1]=0; N[1] < size[1]; ++N[1]) | 
|---|
|  | 113 | for(N[2]=0; N[2] < size[2]; ++N[2]) { | 
|---|
|  | 114 | ASSERT( copyiter != grid.Iterators().Local().End(), | 
|---|
|  | 115 | "InterfaceVMGJob::addGridOntoWindow() - copyiter is already at end of window."); | 
|---|
|  | 116 | ++copyiter; | 
|---|
|  | 117 | } | 
|---|
| [b123a5] | 118 | for(N[0]=0; N[0] < length[0]; ++N[0]) { | 
|---|
|  | 119 | std::advance(griditer, pre_offset[1]*total[2]); | 
|---|
| [d9d028] | 120 | if (OpenBoundaryConditions) | 
|---|
|  | 121 | for(N[1]=0; N[1] < size[1]/4; ++N[1]) | 
|---|
|  | 122 | for(N[2]=0; N[2] < size[2]; ++N[2]) { | 
|---|
|  | 123 | ASSERT( copyiter != grid.Iterators().Local().End(), | 
|---|
|  | 124 | "InterfaceVMGJob::addGridOntoWindow() - copyiter is already at end of window."); | 
|---|
|  | 125 | ++copyiter; | 
|---|
|  | 126 | } | 
|---|
| [b123a5] | 127 | for(N[1]=0; N[1] < length[1]; ++N[1]) { | 
|---|
|  | 128 | std::advance(griditer, pre_offset[2]); | 
|---|
| [d9d028] | 129 | if (OpenBoundaryConditions) | 
|---|
|  | 130 | for(N[2]=0; N[2] < size[2]/4; ++N[2]) { | 
|---|
|  | 131 | ASSERT( copyiter != grid.Iterators().Local().End(), | 
|---|
|  | 132 | "InterfaceVMGJob::addGridOntoWindow() - copyiter is already at end of window."); | 
|---|
|  | 133 | ++copyiter; | 
|---|
|  | 134 | } | 
|---|
| [b123a5] | 135 | for(N[2]=0; N[2] < length[2]; ++N[2]) { | 
|---|
|  | 136 | ASSERT( griditer != window.sampled_grid.end(), | 
|---|
|  | 137 | "InterfaceVMGJob::addGridOntoWindow() - griditer is already at end of window."); | 
|---|
|  | 138 | ASSERT( copyiter != grid.Iterators().Local().End(), | 
|---|
| [d9d028] | 139 | "InterfaceVMGJob::addGridOntoWindow() - copyiter is already at end of window."); | 
|---|
| [b123a5] | 140 | *griditer++ += prefactor*grid(*copyiter++); | 
|---|
|  | 141 | } | 
|---|
|  | 142 | std::advance(griditer, post_offset[2]); | 
|---|
| [d9d028] | 143 | if (OpenBoundaryConditions) | 
|---|
|  | 144 | for(N[2]=0; N[2] < size[2] - size[2]/4 - length[2]; ++N[2]) { | 
|---|
|  | 145 | ASSERT( copyiter != grid.Iterators().Local().End(), | 
|---|
|  | 146 | "InterfaceVMGJob::addGridOntoWindow() - copyiter is already at end of window."); | 
|---|
|  | 147 | ++copyiter; | 
|---|
|  | 148 | } | 
|---|
| [b123a5] | 149 | } | 
|---|
|  | 150 | std::advance(griditer, post_offset[1]*total[2]); | 
|---|
| [d9d028] | 151 | if (OpenBoundaryConditions) | 
|---|
|  | 152 | for(N[1]=0; N[1] < size[1] - size[1]/4 - length[1]; ++N[1]) | 
|---|
|  | 153 | for(N[2]=0; N[2] < size[2]; ++N[2]) { | 
|---|
|  | 154 | ASSERT( copyiter != grid.Iterators().Local().End(), | 
|---|
|  | 155 | "InterfaceVMGJob::addGridOntoWindow() - copyiter is already at end of window."); | 
|---|
|  | 156 | ++copyiter; | 
|---|
|  | 157 | } | 
|---|
| [b123a5] | 158 | } | 
|---|
| [d9d028] | 159 | if (OpenBoundaryConditions) | 
|---|
|  | 160 | for(N[0]=0; N[0] < size[0] - size[0]/4 - length[0]; ++N[0]) | 
|---|
|  | 161 | for(N[1]=0; N[1] < size[1]; ++N[1]) | 
|---|
|  | 162 | for(N[2]=0; N[2] < size[2]; ++N[2]) { | 
|---|
|  | 163 | ASSERT( copyiter != grid.Iterators().Local().End(), | 
|---|
|  | 164 | "InterfaceVMGJob::addGridOntoWindow() - copyiter is already at end of window."); | 
|---|
|  | 165 | ++copyiter; | 
|---|
|  | 166 | } | 
|---|
| [b123a5] | 167 | #ifndef NDEBUG | 
|---|
|  | 168 | std::advance(griditer, post_offset[0]*total[1]*total[2]); | 
|---|
|  | 169 | ASSERT( griditer == window.sampled_grid.end(), | 
|---|
|  | 170 | "InterfaceVMGJob::addGridOntoWindow() - griditer is not at end of window."); | 
|---|
|  | 171 | ASSERT( copyiter == grid.Iterators().Local().End(), | 
|---|
|  | 172 | "InterfaceVMGJob::addGridOntoWindow() - copyiter is not at end of window."); | 
|---|
|  | 173 | #endif | 
|---|
|  | 174 | } | 
|---|
|  | 175 |  | 
|---|
|  | 176 | void WindowGrid_converter::addWindowOntoGrid( | 
|---|
|  | 177 | VMG::Grid& window, | 
|---|
|  | 178 | const SamplingGrid &grid, | 
|---|
| [27ef5c] | 179 | const double prefactor, | 
|---|
|  | 180 | const bool OpenBoundaryConditions) | 
|---|
| [b123a5] | 181 | { | 
|---|
|  | 182 | #ifndef NDEBUG | 
|---|
|  | 183 | for(size_t index=0;index<3;++index) { | 
|---|
|  | 184 | ASSERT( grid.begin_window[index] >= grid.begin[index], | 
|---|
|  | 185 | "InterfaceVMGJob::addWindowOntoGrid() - given window starts earlier than grid in component " | 
|---|
|  | 186 | +toString(index)+"."); | 
|---|
|  | 187 | ASSERT( grid.end_window[index] <= grid.end[index], | 
|---|
|  | 188 | "InterfaceVMGJob::addWindowOntoGrid() - given window ends later than grid in component " | 
|---|
|  | 189 | +toString(index)+"."); | 
|---|
|  | 190 | } | 
|---|
|  | 191 | #endif | 
|---|
|  | 192 | // the only issue are indices | 
|---|
|  | 193 | const size_t gridpoints_axis = grid.getGridPointsPerAxis(); | 
|---|
|  | 194 | size_t pre_offset[3]; | 
|---|
|  | 195 | size_t post_offset[3]; | 
|---|
|  | 196 | size_t length[3]; | 
|---|
|  | 197 | size_t total[3]; | 
|---|
|  | 198 | const double round_offset = | 
|---|
|  | 199 | (std::numeric_limits<size_t>::round_style == std::round_toward_zero) ? | 
|---|
|  | 200 | 0.5 : 0.; // need offset to get to round_toward_nearest behavior | 
|---|
|  | 201 | for(size_t index=0;index<3;++index) { | 
|---|
|  | 202 | const double delta = (double)gridpoints_axis/(grid.end[index] - grid.begin[index]); | 
|---|
|  | 203 | // delta is conversion factor from box length to discrete length, i.e. number of points | 
|---|
|  | 204 | pre_offset[index] = delta*(grid.begin_window[index] - grid.begin[index])+round_offset; | 
|---|
|  | 205 | length[index] = delta*(grid.end_window[index] - grid.begin_window[index])+round_offset; | 
|---|
|  | 206 | post_offset[index] = delta*(grid.end[index] - grid.end_window[index])+round_offset; | 
|---|
|  | 207 | total[index] = delta*(grid.end[index] - grid.begin[index])+round_offset; | 
|---|
|  | 208 | // total is used as safe-guard against loss due to discrete conversion | 
|---|
|  | 209 | ASSERT( pre_offset[index]+post_offset[index]+length[index] == total[index], | 
|---|
|  | 210 | "InterfaceVMGJob::addWindowOntoGrid() - pre, post, and length don't sum up to total for " | 
|---|
|  | 211 | +toString(index)+"th component."); | 
|---|
|  | 212 | } | 
|---|
|  | 213 | #ifndef NDEBUG | 
|---|
|  | 214 | const size_t calculated_size = length[0]*length[1]*length[2]; | 
|---|
|  | 215 | ASSERT( calculated_size == grid.sampled_grid.size(), | 
|---|
|  | 216 | "InterfaceVMGJob::addWindowOntoGrid() - not enough sampled values given: " | 
|---|
|  | 217 | +toString(calculated_size)+" != "+toString(grid.sampled_grid.size())+"."); | 
|---|
|  | 218 | //  ASSERT( calculated_size <=  given_size, | 
|---|
|  | 219 | //      "InterfaceVMGJob::addWindowOntoGrid() - not enough sampled values available: " | 
|---|
|  | 220 | //      +toString(calculated_size)+" <= "+toString(given_size)+"."); | 
|---|
|  | 221 | //  const size_t total_size = total[0]*total[1]*total[2]; | 
|---|
|  | 222 | //  ASSERT( total_size == given_size, | 
|---|
|  | 223 | //      "InterfaceVMGJob::addWindowOntoGrid() - total size is not equal to number of present points: " | 
|---|
|  | 224 | //      +toString(total_size)+" != "+toString(given_size)+"."); | 
|---|
|  | 225 | #endif | 
|---|
|  | 226 | size_t N[3]; | 
|---|
| [27ef5c] | 227 | // in open boundary case grid.Local() contains more than just the inner area. The grid | 
|---|
|  | 228 | // is actually twice as large to allow for the FV discretization to work. Hence, we first | 
|---|
|  | 229 | // have to seek our starting point ... see VMG::Grid::GetSpatialPos() | 
|---|
|  | 230 | if (OpenBoundaryConditions) { | 
|---|
|  | 231 | const VMG::Index size = window.Local().Size(); | 
|---|
|  | 232 | const VMG::Index halosize = window.Local().HaloSize1(); | 
|---|
|  | 233 | for (size_t i=0;i<3;++i) | 
|---|
|  | 234 | pre_offset[i] += size[i] / 4; | 
|---|
|  | 235 | for (size_t i=0;i<3;++i) | 
|---|
|  | 236 | total[i] = size[i]; | 
|---|
|  | 237 | for (size_t i=0;i<3;++i) | 
|---|
|  | 238 | post_offset[i] = size[i] - pre_offset[i] - length[i]; | 
|---|
|  | 239 | } | 
|---|
|  | 240 |  | 
|---|
| [b123a5] | 241 | VMG::Grid::iterator griditer = window.Iterators().Local().Begin(); | 
|---|
|  | 242 | //  griditer.advance(pre_offset[0]*total[1]*total[2]); | 
|---|
|  | 243 | for(N[0]=0; N[0] < pre_offset[0]; ++N[0]) | 
|---|
|  | 244 | for(N[1]=0; N[1] < total[1]; ++N[1]) | 
|---|
|  | 245 | for(N[2]=0; N[2] < total[2]; ++N[2]) { | 
|---|
|  | 246 | ASSERT( griditer != window.Iterators().Local().End(), | 
|---|
|  | 247 | "InterfaceVMGJob::addWindowOntoGrid() - griditer is already at end of window."); | 
|---|
|  | 248 | window(*griditer++) = 0.; | 
|---|
|  | 249 | } | 
|---|
|  | 250 | SamplingGrid::sampledvalues_t::const_iterator copyiter = grid.sampled_grid.begin(); | 
|---|
|  | 251 | for(N[0]=0; N[0] < length[0]; ++N[0]) { | 
|---|
|  | 252 | //    griditer.advance(pre_offset[1]*total[2]); | 
|---|
|  | 253 | for(N[1]=0; N[1] < pre_offset[1]; ++N[1]) | 
|---|
|  | 254 | for(N[2]=0; N[2] < total[2]; ++N[2]) { | 
|---|
|  | 255 | ASSERT( griditer != window.Iterators().Local().End(), | 
|---|
|  | 256 | "InterfaceVMGJob::addWindowOntoGrid() - griditer is already at end of window."); | 
|---|
|  | 257 | window(*griditer++) = 0.; | 
|---|
|  | 258 | } | 
|---|
|  | 259 | for(N[1]=0; N[1] < length[1]; ++N[1]) { | 
|---|
|  | 260 | //      griditer.advance(pre_offset[2]); | 
|---|
| [27ef5c] | 261 | for(N[2]=0; N[2] < pre_offset[2]; ++N[2]) | 
|---|
|  | 262 | window(*griditer++) = 0.; | 
|---|
| [b123a5] | 263 | for(N[2]=0; N[2] < length[2]; ++N[2]) { | 
|---|
|  | 264 | ASSERT( griditer != window.Iterators().Local().End(), | 
|---|
|  | 265 | "InterfaceVMGJob::addWindowOntoGrid() - griditer is already at end of window."); | 
|---|
|  | 266 | ASSERT( copyiter != grid.sampled_grid.end(), | 
|---|
|  | 267 | "InterfaceVMGJob::addWindowOntoGrid() - griditer is already at end of window."); | 
|---|
|  | 268 | window(*griditer++) += prefactor*(*copyiter++); | 
|---|
|  | 269 | } | 
|---|
|  | 270 | //      griditer.advance(post_offset[2]); | 
|---|
|  | 271 | for(N[2]=0; N[2] < post_offset[2]; ++N[2]) { | 
|---|
|  | 272 | ASSERT( griditer != window.Iterators().Local().End(), | 
|---|
|  | 273 | "InterfaceVMGJob::addWindowOntoGrid() - griditer is already at end of window."); | 
|---|
|  | 274 | window(*griditer++) = 0.; | 
|---|
|  | 275 | } | 
|---|
|  | 276 | } | 
|---|
|  | 277 | //    griditer.advance(post_offset[1]*total[2]); | 
|---|
|  | 278 | for(N[1]=0; N[1] < post_offset[1]; ++N[1]) | 
|---|
|  | 279 | for(N[2]=0; N[2] < total[2]; ++N[2]) { | 
|---|
|  | 280 | ASSERT( griditer != window.Iterators().Local().End(), | 
|---|
|  | 281 | "InterfaceVMGJob::addWindowOntoGrid() - griditer is already at end of window."); | 
|---|
|  | 282 | window(*griditer++) = 0.; | 
|---|
|  | 283 | } | 
|---|
|  | 284 | } | 
|---|
| [27ef5c] | 285 | //  griditer.advance(post_offset[0]*total[1]*total[2]); | 
|---|
| [b123a5] | 286 | for(N[0]=0; N[0] < post_offset[0]; ++N[0]) | 
|---|
|  | 287 | for(N[1]=0; N[1] < total[1]; ++N[1]) | 
|---|
|  | 288 | for(N[2]=0; N[2] < total[2]; ++N[2]) { | 
|---|
|  | 289 | ASSERT( griditer != window.Iterators().Local().End(), | 
|---|
|  | 290 | "InterfaceVMGJob::addWindowOntoGrid() - griditer is already at end of window."); | 
|---|
|  | 291 | window(*griditer++) = 0.; | 
|---|
|  | 292 | } | 
|---|
|  | 293 | #ifndef NDEBUG | 
|---|
|  | 294 | ASSERT( griditer == window.Iterators().Local().End(), | 
|---|
|  | 295 | "InterfaceVMGJob::addWindowOntoGrid() - griditer is not at end of window."); | 
|---|
|  | 296 | ASSERT( copyiter == grid.sampled_grid.end(), | 
|---|
|  | 297 | "InterfaceVMGJob::addWindowOntoGrid() - copyiter is not at end of window."); | 
|---|
|  | 298 | #endif | 
|---|
|  | 299 | //  LOG(2, "DEBUG: Grid after adding other is " << grid << "."); | 
|---|
|  | 300 | } | 
|---|