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