[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"
|
---|
[17e4fd] | 52 | #include "Jobs/ChargeSmearer.hpp"
|
---|
[b123a5] | 53 |
|
---|
| 54 | void WindowGrid_converter::addGridOntoWindow(
|
---|
| 55 | VMG::Grid &grid,
|
---|
| 56 | SamplingGrid &window,
|
---|
[d9d028] | 57 | const double prefactor,
|
---|
| 58 | const bool OpenBoundaryConditions)
|
---|
[b123a5] | 59 | {
|
---|
| 60 | #ifndef NDEBUG
|
---|
[336da8] | 61 | for(size_t index=0;index<NDIM;++index) {
|
---|
[b123a5] | 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
|
---|
[336da8] | 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);
|
---|
[b123a5] | 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
|
---|
[336da8] | 86 | size_t N[NDIM];
|
---|
[b123a5] | 87 | SamplingGrid::sampledvalues_t::iterator griditer = window.sampled_grid.begin();
|
---|
| 88 | std::advance(griditer, pre_offset[0]*total[1]*total[2]);
|
---|
[d9d028] | 89 |
|
---|
[b123a5] | 90 | VMG::Grid::iterator copyiter = grid.Iterators().Local().Begin();
|
---|
[d9d028] | 91 | const VMG::Index size = grid.Local().Size();
|
---|
| 92 | if (OpenBoundaryConditions)
|
---|
[a2a2f7] | 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]) {
|
---|
[d9d028] | 96 | ASSERT( copyiter != grid.Iterators().Local().End(),
|
---|
| 97 | "InterfaceVMGJob::addGridOntoWindow() - copyiter is already at end of window.");
|
---|
| 98 | ++copyiter;
|
---|
| 99 | }
|
---|
[a2a2f7] | 100 | for(N[0]=(size_t)0; N[0] < length[0]; ++N[0]) {
|
---|
[b123a5] | 101 | std::advance(griditer, pre_offset[1]*total[2]);
|
---|
[d9d028] | 102 | if (OpenBoundaryConditions)
|
---|
[a2a2f7] | 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]) {
|
---|
[d9d028] | 105 | ASSERT( copyiter != grid.Iterators().Local().End(),
|
---|
| 106 | "InterfaceVMGJob::addGridOntoWindow() - copyiter is already at end of window.");
|
---|
| 107 | ++copyiter;
|
---|
| 108 | }
|
---|
[a2a2f7] | 109 | for(N[1]=(size_t)0; N[1] < length[1]; ++N[1]) {
|
---|
[b123a5] | 110 | std::advance(griditer, pre_offset[2]);
|
---|
[d9d028] | 111 | if (OpenBoundaryConditions)
|
---|
[a2a2f7] | 112 | for(N[2]=(size_t)0; N[2] < (size_t)size[2]/(size_t)4; ++N[2]) {
|
---|
[d9d028] | 113 | ASSERT( copyiter != grid.Iterators().Local().End(),
|
---|
| 114 | "InterfaceVMGJob::addGridOntoWindow() - copyiter is already at end of window.");
|
---|
| 115 | ++copyiter;
|
---|
| 116 | }
|
---|
[a2a2f7] | 117 | for(N[2]=(size_t)0; N[2] < length[2]; ++N[2]) {
|
---|
[b123a5] | 118 | ASSERT( griditer != window.sampled_grid.end(),
|
---|
| 119 | "InterfaceVMGJob::addGridOntoWindow() - griditer is already at end of window.");
|
---|
| 120 | ASSERT( copyiter != grid.Iterators().Local().End(),
|
---|
[d9d028] | 121 | "InterfaceVMGJob::addGridOntoWindow() - copyiter is already at end of window.");
|
---|
[b123a5] | 122 | *griditer++ += prefactor*grid(*copyiter++);
|
---|
| 123 | }
|
---|
| 124 | std::advance(griditer, post_offset[2]);
|
---|
[d9d028] | 125 | if (OpenBoundaryConditions)
|
---|
[a2a2f7] | 126 | for(N[2]=(size_t)0; N[2] < (size_t)size[2] - (size_t)size[2]/(size_t)4 - length[2]; ++N[2]) {
|
---|
[d9d028] | 127 | ASSERT( copyiter != grid.Iterators().Local().End(),
|
---|
| 128 | "InterfaceVMGJob::addGridOntoWindow() - copyiter is already at end of window.");
|
---|
| 129 | ++copyiter;
|
---|
| 130 | }
|
---|
[b123a5] | 131 | }
|
---|
| 132 | std::advance(griditer, post_offset[1]*total[2]);
|
---|
[d9d028] | 133 | if (OpenBoundaryConditions)
|
---|
[a2a2f7] | 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]) {
|
---|
[d9d028] | 136 | ASSERT( copyiter != grid.Iterators().Local().End(),
|
---|
| 137 | "InterfaceVMGJob::addGridOntoWindow() - copyiter is already at end of window.");
|
---|
| 138 | ++copyiter;
|
---|
| 139 | }
|
---|
[b123a5] | 140 | }
|
---|
[d9d028] | 141 | if (OpenBoundaryConditions)
|
---|
[a2a2f7] | 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]) {
|
---|
[d9d028] | 145 | ASSERT( copyiter != grid.Iterators().Local().End(),
|
---|
| 146 | "InterfaceVMGJob::addGridOntoWindow() - copyiter is already at end of window.");
|
---|
| 147 | ++copyiter;
|
---|
| 148 | }
|
---|
[b123a5] | 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,
|
---|
[27ef5c] | 161 | const double prefactor,
|
---|
[17e4fd] | 162 | const bool OpenBoundaryConditions,
|
---|
| 163 | const bool DoSmearCharges)
|
---|
[b123a5] | 164 | {
|
---|
| 165 | #ifndef NDEBUG
|
---|
[336da8] | 166 | for(size_t index=0;index<NDIM;++index) {
|
---|
[b123a5] | 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
|
---|
[336da8] | 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);
|
---|
[b123a5] | 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
|
---|
[336da8] | 187 | size_t N[NDIM];
|
---|
[27ef5c] | 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();
|
---|
[a2a2f7] | 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();
|
---|
[b47f9c] | 197 | // this mimicks VMG::GridIndexTranslations::EndOffset()
|
---|
| 198 | const size_t off = OpenBoundaryConditions ? 1 : 0;
|
---|
[336da8] | 199 | for (size_t i=0;i<NDIM;++i)
|
---|
[a2a2f7] | 200 | pre_offset[i] += ((size_t)size[i] - off) / 4;
|
---|
[336da8] | 201 | for (size_t i=0;i<NDIM;++i)
|
---|
[a2a2f7] | 202 | total[i] = ((size_t)size[i]);
|
---|
[336da8] | 203 | for (size_t i=0;i<NDIM;++i)
|
---|
[a2a2f7] | 204 | post_offset[i] = ((size_t)size[i]) - pre_offset[i] - length[i];
|
---|
[27ef5c] | 205 | }
|
---|
| 206 |
|
---|
[445ce6] | 207 | /// reset grid to zero everywhere
|
---|
| 208 |
|
---|
[b123a5] | 209 | VMG::Grid::iterator griditer = window.Iterators().Local().Begin();
|
---|
| 210 | // griditer.advance(pre_offset[0]*total[1]*total[2]);
|
---|
[445ce6] | 211 | for(N[0]=0; N[0] < total[0]; ++N[0])
|
---|
[b123a5] | 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 | }
|
---|
[445ce6] | 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();
|
---|
[b123a5] | 227 | SamplingGrid::sampledvalues_t::const_iterator copyiter = grid.sampled_grid.begin();
|
---|
[17e4fd] | 228 | const ChargeSmearer &smearer = ChargeSmearer::getInstance();
|
---|
[445ce6] | 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 | }
|
---|
[b123a5] | 238 | for(N[0]=0; N[0] < length[0]; ++N[0]) {
|
---|
[445ce6] | 239 | // griditer += pre_offset[1] * total[2];
|
---|
[b123a5] | 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.");
|
---|
[445ce6] | 244 | griditer++;
|
---|
[b123a5] | 245 | }
|
---|
| 246 | for(N[1]=0; N[1] < length[1]; ++N[1]) {
|
---|
[445ce6] | 247 | // griditer += pre_offset[2];
|
---|
| 248 | for(N[2]=0; N[2] < pre_offset[2]; ++N[2]) {
|
---|
| 249 | griditer++;
|
---|
| 250 | }
|
---|
| 251 |
|
---|
[b123a5] | 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.");
|
---|
[17e4fd] | 257 | // either smear out charges or not.
|
---|
| 258 | if (DoSmearCharges) {
|
---|
| 259 | smearer(window, griditer++, prefactor*(*copyiter++));
|
---|
| 260 | } else {
|
---|
| 261 | window(*griditer++) += prefactor*(*copyiter++);
|
---|
| 262 | }
|
---|
[b123a5] | 263 | }
|
---|
[445ce6] | 264 | // griditer += post_offset[2];
|
---|
[b123a5] | 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.");
|
---|
[445ce6] | 268 | griditer++;
|
---|
[b123a5] | 269 | }
|
---|
| 270 | }
|
---|
[445ce6] | 271 | // griditer += post_offset[1] * total[2];
|
---|
[b123a5] | 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.");
|
---|
[445ce6] | 276 | griditer++;
|
---|
[b123a5] | 277 | }
|
---|
| 278 | }
|
---|
[445ce6] | 279 | // griditer += post_offset[0] * total[1] * total[2];
|
---|
[b123a5] | 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.");
|
---|
[445ce6] | 285 | griditer++;
|
---|
[b123a5] | 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
|
---|
[17e4fd] | 293 |
|
---|
| 294 | // LOG(2, "DEBUG: Grid after adding other is " << grid << ".");
|
---|
[b123a5] | 295 | }
|
---|