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