| 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 | *
|
|---|
| 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 | * InterfaceVMGJob.cpp
|
|---|
| 26 | *
|
|---|
| 27 | * Created on: 10.06.2012
|
|---|
| 28 | * Author: Frederik Heber
|
|---|
| 29 | */
|
|---|
| 30 |
|
|---|
| 31 | #ifdef HAVE_CONFIG_H
|
|---|
| 32 | #include <config.h>
|
|---|
| 33 | #endif
|
|---|
| 34 |
|
|---|
| 35 | #ifdef HAVE_MPI
|
|---|
| 36 | #include "mpi.h"
|
|---|
| 37 | #endif
|
|---|
| 38 |
|
|---|
| 39 | #include "base/vector.hpp"
|
|---|
| 40 | #include "base/math.hpp"
|
|---|
| 41 | #include "comm/comm.hpp"
|
|---|
| 42 | #include "grid/grid.hpp"
|
|---|
| 43 | #include "grid/multigrid.hpp"
|
|---|
| 44 | #include "units/particle/comm_mpi_particle.hpp"
|
|---|
| 45 | #include "units/particle/interpolation.hpp"
|
|---|
| 46 | #include "units/particle/linked_cell_list.hpp"
|
|---|
| 47 | #include "mg.hpp"
|
|---|
| 48 |
|
|---|
| 49 | #include "InterfaceVMGJob.hpp"
|
|---|
| 50 |
|
|---|
| 51 | #include "CodePatterns/MemDebug.hpp"
|
|---|
| 52 |
|
|---|
| 53 | #include <cmath>
|
|---|
| 54 | #include <iostream>
|
|---|
| 55 | #include <limits>
|
|---|
| 56 |
|
|---|
| 57 | #include "CodePatterns/Log.hpp"
|
|---|
| 58 |
|
|---|
| 59 | #include "Jobs/WindowGrid_converter.hpp"
|
|---|
| 60 |
|
|---|
| 61 | using namespace VMG;
|
|---|
| 62 | using VMGInterfaces::InterfaceVMGJob;
|
|---|
| 63 |
|
|---|
| 64 | InterfaceVMGJob::InterfaceVMGJob(const SamplingGrid &_sampled_input,
|
|---|
| 65 | VMGData &_returndata,
|
|---|
| 66 | const std::vector< std::vector<double> > &_particle_positions,
|
|---|
| 67 | const std::vector< double > &_particle_charges,
|
|---|
| 68 | VMG::Boundary boundary,
|
|---|
| 69 | int levelMin,
|
|---|
| 70 | int levelMax,
|
|---|
| 71 | const VMG::Vector &_box_begin,
|
|---|
| 72 | vmg_float _box_end,
|
|---|
| 73 | const int& near_field_cells,
|
|---|
| 74 | const ImportParticles_t _ImportParticles,
|
|---|
| 75 | const bool _DoPrintDebug,
|
|---|
| 76 | int coarseningSteps,
|
|---|
| 77 | double alpha) :
|
|---|
| 78 | VMG::Interface(boundary, levelMin, levelMax,
|
|---|
| 79 | _box_begin, _box_end, coarseningSteps, alpha),
|
|---|
| 80 | spl(near_field_cells, Extent(MaxLevel()).MeshWidth().Max()),
|
|---|
| 81 | sampled_input(_sampled_input),
|
|---|
| 82 | returndata(_returndata),
|
|---|
| 83 | level(levelMax),
|
|---|
| 84 | ImportParticles(_ImportParticles),
|
|---|
| 85 | DoPrintDebug(_DoPrintDebug)
|
|---|
| 86 | {
|
|---|
| 87 | for (size_t i=0;i<3;++i) {
|
|---|
| 88 | box_begin[i] = _box_begin[i];
|
|---|
| 89 | box_end[i] = _box_end;
|
|---|
| 90 | }
|
|---|
| 91 | std::vector< std::vector<double> >::const_iterator positer = _particle_positions.begin();
|
|---|
| 92 | std::vector<double>::const_iterator chargeiter = _particle_charges.begin();
|
|---|
| 93 | double pos[3];
|
|---|
| 94 | for (; positer != _particle_positions.end(); ++positer, ++chargeiter) {
|
|---|
| 95 | ASSERT( (*positer).size() == 3,
|
|---|
| 96 | "InterfaceVMGJob::InterfaceVMGJob() - particle "
|
|---|
| 97 | +toString(distance(_particle_positions.begin(), positer))+" has not exactly 3 coordinates.");
|
|---|
| 98 | for (size_t i=0;i<3;++i)
|
|---|
| 99 | pos[i] = (*positer)[i];
|
|---|
| 100 | particles.push_back(Particle::Particle(pos, *chargeiter));
|
|---|
| 101 | }
|
|---|
| 102 | }
|
|---|
| 103 |
|
|---|
| 104 | void InterfaceVMGJob::ImportRightHandSide(Multigrid& multigrid)
|
|---|
| 105 | {
|
|---|
| 106 | Index i;
|
|---|
| 107 | Vector pos;
|
|---|
| 108 | // VMG::TempGrid *temp_grid = new VMG::TempGrid(129, 0, 0., 1.);
|
|---|
| 109 |
|
|---|
| 110 | Grid& grid = multigrid(multigrid.MaxLevel());
|
|---|
| 111 | grid.Clear();
|
|---|
| 112 | //grid.ClearBoundary(); // we don't have a boundary under periodic boundary conditions
|
|---|
| 113 |
|
|---|
| 114 | // print debugging info on grid size
|
|---|
| 115 | LOG(1, "INFO: Mesh has extent " << grid.Extent().MeshWidth() << ".");
|
|---|
| 116 | const int gridpoints = pow(2, level);
|
|---|
| 117 | LOG(1, "INFO: gridpoints on finest level are " << gridpoints << ".");
|
|---|
| 118 | LOG(1, "INFO: "
|
|---|
| 119 | << "X in [" << grid.Local().Begin().X() << "," << grid.Local().End().X() << "],"
|
|---|
| 120 | << "Y in [" << grid.Local().Begin().Y() << "," << grid.Local().End().Y() << "],"
|
|---|
| 121 | << "Z in [" << grid.Local().Begin().Z() << "," << grid.Local().End().Z() << "].");
|
|---|
| 122 |
|
|---|
| 123 | /// 1. assign nuclei as smeared-out charges to the grid
|
|---|
| 124 |
|
|---|
| 125 | /*
|
|---|
| 126 | * Charge assignment on the grid
|
|---|
| 127 | */
|
|---|
| 128 | Particle::CommMPI& comm = *dynamic_cast<Particle::CommMPI*>(MG::GetComm());
|
|---|
| 129 | Grid& particle_grid = comm.GetParticleGrid();
|
|---|
| 130 | particle_grid.Clear();
|
|---|
| 131 |
|
|---|
| 132 | // distribute particles
|
|---|
| 133 | particles.clear();
|
|---|
| 134 | comm.CommParticles(grid, particles);
|
|---|
| 135 |
|
|---|
| 136 | assert(particle_grid.Global().LocalSize().IsComponentwiseGreater(
|
|---|
| 137 | VMG::MG::GetFactory().GetObjectStorageVal<int>("PARTICLE_NEAR_FIELD_CELLS")));
|
|---|
| 138 |
|
|---|
| 139 | if (ImportParticles == DoImportParticles) {
|
|---|
| 140 | // create smeared-out particle charges on particle_grid via splines
|
|---|
| 141 | LOG(1, "INFO: Creating particle grid for " << particles.size() << " particles.");
|
|---|
| 142 | for (std::list<Particle::Particle>::iterator iter = particles.begin();
|
|---|
| 143 | iter != particles.end(); ++iter) {
|
|---|
| 144 | LOG(2, "DEBUG: Current particle is at " << (*iter).Pos()
|
|---|
| 145 | << " with charge " << (*iter).Charge() << ".");
|
|---|
| 146 | spl.SetSpline(particle_grid, *iter);
|
|---|
| 147 | }
|
|---|
| 148 | }
|
|---|
| 149 |
|
|---|
| 150 | // Communicate charges over halo
|
|---|
| 151 | comm.CommFromGhosts(particle_grid);
|
|---|
| 152 |
|
|---|
| 153 | if (DoPrintDebug) {
|
|---|
| 154 | // print nuclei grid to vtk
|
|---|
| 155 | comm.PrintGrid(particle_grid, "Sampled Nuclei Density");
|
|---|
| 156 | }
|
|---|
| 157 |
|
|---|
| 158 | // add sampled electron charge density onto grid
|
|---|
| 159 | WindowGrid_converter::addWindowOntoGrid(
|
|---|
| 160 | grid,
|
|---|
| 161 | sampled_input,
|
|---|
| 162 | 1.);
|
|---|
| 163 |
|
|---|
| 164 | if (DoPrintDebug) {
|
|---|
| 165 | // print electron grid to vtk
|
|---|
| 166 | comm.PrintGrid(grid, "Sampled Electron Density");
|
|---|
| 167 | }
|
|---|
| 168 |
|
|---|
| 169 | // add particle_grid onto grid
|
|---|
| 170 | for (int i=0; i<grid.Local().Size().X(); ++i)
|
|---|
| 171 | for (int j=0; j<grid.Local().Size().Y(); ++j)
|
|---|
| 172 | for (int k=0; k<grid.Local().Size().Z(); ++k)
|
|---|
| 173 | grid(grid.Local().Begin().X() + i,
|
|---|
| 174 | grid.Local().Begin().Y() + j,
|
|---|
| 175 | grid.Local().Begin().Z() + k) = 4.0 * VMG::Math::pi * (
|
|---|
| 176 | grid(grid.Local().Begin().X() + i,
|
|---|
| 177 | grid.Local().Begin().Y() + j,
|
|---|
| 178 | grid.Local().Begin().Z() + k) +
|
|---|
| 179 | particle_grid.GetVal(particle_grid.Local().Begin().X() + i,
|
|---|
| 180 | particle_grid.Local().Begin().Y() + j,
|
|---|
| 181 | particle_grid.Local().Begin().Z() + k));
|
|---|
| 182 |
|
|---|
| 183 | // calculate sum over grid times h^3 as check, should be roughly zero
|
|---|
| 184 | const double element_volume = grid.Extent().MeshWidth().Product();
|
|---|
| 185 | double charge_sum = 0.0;
|
|---|
| 186 | for (Grid::iterator grid_iter = grid.Iterators().Local().Begin();
|
|---|
| 187 | grid_iter != grid.Iterators().Local().End();
|
|---|
| 188 | ++grid_iter)
|
|---|
| 189 | charge_sum += grid.GetVal(*grid_iter);
|
|---|
| 190 | charge_sum = element_volume * comm.GlobalSum(charge_sum);
|
|---|
| 191 | comm.PrintStringOnce("Grid charge integral: %e", charge_sum/(4.0 * VMG::Math::pi));
|
|---|
| 192 |
|
|---|
| 193 | if (DoPrintDebug) {
|
|---|
| 194 | // print total grid to vtk
|
|---|
| 195 | comm.PrintGrid(grid, "Total Charge Density");
|
|---|
| 196 | }
|
|---|
| 197 |
|
|---|
| 198 | // delete temp_grid;
|
|---|
| 199 | }
|
|---|
| 200 |
|
|---|
| 201 | void InterfaceVMGJob::ExportSolution(Grid& grid)
|
|---|
| 202 | {
|
|---|
| 203 | /// sample the obtained potential to evaluate with the electron charge density
|
|---|
| 204 |
|
|---|
| 205 | // grid now contains the sough-for potential
|
|---|
| 206 | //Comm& comm = *MG::GetComm();
|
|---|
| 207 | Particle::CommMPI& comm = *dynamic_cast<Particle::CommMPI*>(MG::GetComm());
|
|---|
| 208 |
|
|---|
| 209 | const Index begin_local = grid.Global().LocalBegin() - grid.Local().HaloSize1();
|
|---|
| 210 | Index i;
|
|---|
| 211 |
|
|---|
| 212 | if (DoPrintDebug) {
|
|---|
| 213 | // print output grid to vtk
|
|---|
| 214 | comm.PrintGrid(grid, "Potential Solution");
|
|---|
| 215 | }
|
|---|
| 216 |
|
|---|
| 217 | // obtain sampled potential from grid
|
|---|
| 218 | returndata.sampled_potential.setWindow(
|
|---|
| 219 | box_begin,
|
|---|
| 220 | box_end
|
|---|
| 221 | );
|
|---|
| 222 | WindowGrid_converter::addGridOntoWindow(
|
|---|
| 223 | grid,
|
|---|
| 224 | returndata.sampled_potential,
|
|---|
| 225 | +1.
|
|---|
| 226 | );
|
|---|
| 227 |
|
|---|
| 228 | // calculate integral over potential as long-range energy contribution
|
|---|
| 229 | const double element_volume =
|
|---|
| 230 | grid.Extent().MeshWidth().X() * grid.Extent().MeshWidth().Y() * grid.Extent().MeshWidth().Z();
|
|---|
| 231 | Grid::iterator grid_iter;
|
|---|
| 232 | double potential_sum = 0.0;
|
|---|
| 233 | for (grid_iter=grid.Iterators().Local().Begin(); grid_iter!=grid.Iterators().Local().End(); ++grid_iter)
|
|---|
| 234 | potential_sum += grid.GetVal(*grid_iter);
|
|---|
| 235 | potential_sum = element_volume * comm.GlobalSum(potential_sum);
|
|---|
| 236 | comm.PrintStringOnce("Grid potential sum: %e", potential_sum);
|
|---|
| 237 |
|
|---|
| 238 | {
|
|---|
| 239 | Grid::iterator grid_iter = grid.Iterators().Local().Begin();
|
|---|
| 240 | comm.PrintStringOnce("Grid potential at (0,0,0): %e", grid.GetVal(*grid_iter));
|
|---|
| 241 | }
|
|---|
| 242 |
|
|---|
| 243 | //Particle::CommMPI& comm = *dynamic_cast<Particle::CommMPI*>(MG::GetComm()); returndata.e_long = potential_sum;
|
|---|
| 244 |
|
|---|
| 245 | /// Calculate potential energy of nuclei
|
|---|
| 246 |
|
|---|
| 247 | vmg_float e = 0.0;
|
|---|
| 248 | vmg_float e_long = 0.0;
|
|---|
| 249 | vmg_float e_self = 0.0;
|
|---|
| 250 | vmg_float e_short_peak = 0.0;
|
|---|
| 251 | vmg_float e_short_spline = 0.0;
|
|---|
| 252 |
|
|---|
| 253 | Factory& factory = MG::GetFactory();
|
|---|
| 254 |
|
|---|
| 255 | /*
|
|---|
| 256 | * Get parameters and arrays
|
|---|
| 257 | */
|
|---|
| 258 | const vmg_int& near_field_cells = factory.GetObjectStorageVal<int>("PARTICLE_NEAR_FIELD_CELLS");
|
|---|
| 259 | const vmg_int& interpolation_degree = factory.GetObjectStorageVal<int>("PARTICLE_INTERPOLATION_DEGREE");
|
|---|
| 260 |
|
|---|
| 261 | Particle::Interpolation ip(interpolation_degree);
|
|---|
| 262 |
|
|---|
| 263 | const vmg_float r_cut = near_field_cells * grid.Extent().MeshWidth().Max();
|
|---|
| 264 |
|
|---|
| 265 | /*
|
|---|
| 266 | * Copy potential values to a grid with sufficiently large halo size.
|
|---|
| 267 | * This may be optimized in future.
|
|---|
| 268 | * The parameters of this grid have been set in the import step.
|
|---|
| 269 | */
|
|---|
| 270 | Grid& particle_grid = comm.GetParticleGrid();
|
|---|
| 271 |
|
|---|
| 272 | for (i.X()=0; i.X()<grid.Local().Size().X(); ++i.X())
|
|---|
| 273 | for (i.Y()=0; i.Y()<grid.Local().Size().Y(); ++i.Y())
|
|---|
| 274 | for (i.Z()=0; i.Z()<grid.Local().Size().Z(); ++i.Z())
|
|---|
| 275 | particle_grid(i + particle_grid.Local().Begin()) = grid.GetVal(i + grid.Local().Begin());
|
|---|
| 276 |
|
|---|
| 277 | comm.CommToGhosts(particle_grid);
|
|---|
| 278 |
|
|---|
| 279 | /*
|
|---|
| 280 | * Compute potentials
|
|---|
| 281 | */
|
|---|
| 282 | Particle::LinkedCellList lc(particles, near_field_cells, grid);
|
|---|
| 283 | Particle::LinkedCellList::iterator p1, p2;
|
|---|
| 284 | Grid::iterator iter;
|
|---|
| 285 |
|
|---|
| 286 | comm.CommLCListToGhosts(lc);
|
|---|
| 287 |
|
|---|
| 288 | for (int i=lc.Local().Begin().X(); i<lc.Local().End().X(); ++i)
|
|---|
| 289 | for (int j=lc.Local().Begin().Y(); j<lc.Local().End().Y(); ++j)
|
|---|
| 290 | for (int k=lc.Local().Begin().Z(); k<lc.Local().End().Z(); ++k) {
|
|---|
| 291 |
|
|---|
| 292 | if (lc(i,j,k).size() > 0)
|
|---|
| 293 | ip.ComputeCoefficients(particle_grid, Index(i,j,k) - lc.Local().Begin() + particle_grid.Local().Begin());
|
|---|
| 294 |
|
|---|
| 295 | for (p1=lc(i,j,k).begin(); p1!=lc(i,j,k).end(); ++p1) {
|
|---|
| 296 |
|
|---|
| 297 | // Interpolate long-range part of potential and electric field
|
|---|
| 298 | ip.Evaluate(**p1);
|
|---|
| 299 |
|
|---|
| 300 | // Subtract self-induced potential
|
|---|
| 301 | (*p1)->Pot() -= (*p1)->Charge() * spl.GetAntiDerivativeAtZero();
|
|---|
| 302 |
|
|---|
| 303 | e_long += 0.5 * (*p1)->Charge() * ip.EvaluatePotentialLR(**p1);
|
|---|
| 304 | e_self += 0.5 * (*p1)->Charge() * (*p1)->Charge() * spl.GetAntiDerivativeAtZero();
|
|---|
| 305 |
|
|---|
| 306 | for (int dx=-1*near_field_cells; dx<=near_field_cells; ++dx)
|
|---|
| 307 | for (int dy=-1*near_field_cells; dy<=near_field_cells; ++dy)
|
|---|
| 308 | for (int dz=-1*near_field_cells; dz<=near_field_cells; ++dz) {
|
|---|
| 309 |
|
|---|
| 310 | for (p2=lc(i+dx,j+dy,k+dz).begin(); p2!=lc(i+dx,j+dy,k+dz).end(); ++p2)
|
|---|
| 311 |
|
|---|
| 312 | if (*p1 != *p2) {
|
|---|
| 313 |
|
|---|
| 314 | const Vector dir = (*p1)->Pos() - (*p2)->Pos();
|
|---|
| 315 | const vmg_float length = dir.Length();
|
|---|
| 316 |
|
|---|
| 317 | if (length < r_cut) {
|
|---|
| 318 |
|
|---|
| 319 | (*p1)->Pot() += (*p2)->Charge() / length * (1.0 + spl.EvaluatePotential(length));
|
|---|
| 320 | (*p1)->Field() += (*p2)->Charge() * dir * spl.EvaluateField(length);
|
|---|
| 321 |
|
|---|
| 322 | e_short_peak += 0.5 * (*p1)->Charge() * (*p2)->Charge() / length;
|
|---|
| 323 | e_short_spline += 0.5 * (*p1)->Charge() * (*p2)->Charge() / length * spl.EvaluatePotential(length);
|
|---|
| 324 | }
|
|---|
| 325 | }
|
|---|
| 326 | }
|
|---|
| 327 | }
|
|---|
| 328 | }
|
|---|
| 329 |
|
|---|
| 330 | /* Remove average force term */
|
|---|
| 331 | if (!particles.empty()) {
|
|---|
| 332 | Vector average_force = 0.0;
|
|---|
| 333 | for (std::list<Particle::Particle>::const_iterator iter=particles.begin(); iter!=particles.end(); ++iter)
|
|---|
| 334 | average_force += iter->Charge() * iter->Field();
|
|---|
| 335 | const vmg_int& npl = MG::GetFactory().GetObjectStorageVal<vmg_int>("PARTICLE_NUM_LOCAL");
|
|---|
| 336 | const vmg_int num_particles_global = comm.GlobalSum(npl);
|
|---|
| 337 | average_force /= num_particles_global;
|
|---|
| 338 | comm.GlobalSumArray(average_force.vec(), 3);
|
|---|
| 339 | for (std::list<Particle::Particle>::iterator iter=particles.begin(); iter!=particles.end(); ++iter)
|
|---|
| 340 | iter->Field() -= average_force / iter->Charge();
|
|---|
| 341 | }
|
|---|
| 342 |
|
|---|
| 343 | comm.CommParticlesBack(particles);
|
|---|
| 344 |
|
|---|
| 345 | vmg_float* q = factory.GetObjectStorageArray<vmg_float>("PARTICLE_CHARGE_ARRAY");
|
|---|
| 346 | const vmg_int& num_particles_local = factory.GetObjectStorageVal<vmg_int>("PARTICLE_NUM_LOCAL");
|
|---|
| 347 | const vmg_float* p = factory.GetObjectStorageArray<vmg_float>("PARTICLE_POTENTIAL_ARRAY");
|
|---|
| 348 | // const vmg_float* f = factory.GetObjectStorageArray<vmg_float>("PARTICLE_FIELD_ARRAY");
|
|---|
| 349 |
|
|---|
| 350 |
|
|---|
| 351 | e_long = comm.GlobalSumRoot(e_long);
|
|---|
| 352 | e_short_peak = comm.GlobalSumRoot(e_short_peak);
|
|---|
| 353 | e_short_spline = comm.GlobalSumRoot(e_short_spline);
|
|---|
| 354 | e_self = comm.GlobalSumRoot(e_self);
|
|---|
| 355 |
|
|---|
| 356 | for (int j=0; j<num_particles_local; ++j)
|
|---|
| 357 | e += 0.5 * p[j] * q[j];
|
|---|
| 358 | e = comm.GlobalSumRoot(e);
|
|---|
| 359 |
|
|---|
| 360 | comm.PrintStringOnce("E_long: %e", e_long);
|
|---|
| 361 | comm.PrintStringOnce("E_short_peak: %e", e_short_peak);
|
|---|
| 362 | comm.PrintStringOnce("E_short_spline: %e", e_short_spline);
|
|---|
| 363 | comm.PrintStringOnce("E_self: %e", e_self);
|
|---|
| 364 | comm.PrintStringOnce("E_total: %e", e);
|
|---|
| 365 | comm.PrintStringOnce("E_total*: %e", e_long + e_short_peak + e_short_spline - e_self);
|
|---|
| 366 |
|
|---|
| 367 | returndata.nuclei_long = e_long;
|
|---|
| 368 | returndata.electron_long = e_long;
|
|---|
| 369 | }
|
|---|