[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 "CodePatterns/MemDebug.hpp"
|
---|
| 39 |
|
---|
| 40 | #include <cmath>
|
---|
| 41 | #include <iostream>
|
---|
| 42 |
|
---|
[88c2c1] | 43 | #include "CodePatterns/Log.hpp"
|
---|
| 44 |
|
---|
[e9cfc4] | 45 | #include "base/vector.hpp"
|
---|
[cd77fc] | 46 | #include "base/math.hpp"
|
---|
[17fcbe7] | 47 | #include "comm/comm.hpp"
|
---|
[e9cfc4] | 48 | #include "grid/grid.hpp"
|
---|
| 49 | #include "grid/multigrid.hpp"
|
---|
[17fcbe7] | 50 | #include "mg.hpp"
|
---|
[e9cfc4] | 51 |
|
---|
| 52 | #include "InterfaceVMGJob.hpp"
|
---|
| 53 |
|
---|
| 54 | using namespace VMG;
|
---|
| 55 | using VMGInterfaces::InterfaceVMGJob;
|
---|
| 56 |
|
---|
[cd77fc] | 57 | InterfaceVMGJob::InterfaceVMGJob(const std::vector< double > &_sampled_input,
|
---|
[2bc560] | 58 | VMGData &_returndata,
|
---|
[cd77fc] | 59 | const std::vector< std::vector<double> > &_particle_positions,
|
---|
| 60 | const std::vector< double > &_particle_charges,
|
---|
| 61 | VMG::Boundary boundary,
|
---|
| 62 | int levelMin,
|
---|
| 63 | int levelMax,
|
---|
| 64 | const VMG::Vector &box_begin,
|
---|
| 65 | vmg_float box_end,
|
---|
| 66 | const int& near_field_cells,
|
---|
| 67 | int coarseningSteps,
|
---|
| 68 | double alpha) :
|
---|
| 69 | VMG::Interface(boundary, levelMin, levelMax,
|
---|
| 70 | box_begin, box_end, coarseningSteps, alpha),
|
---|
| 71 | spl(near_field_cells, Extent(MaxLevel()).MeshWidth().Max()),
|
---|
| 72 | sampled_input(_sampled_input),
|
---|
[2bc560] | 73 | returndata(_returndata),
|
---|
[cd77fc] | 74 | level(levelMax)
|
---|
| 75 | {
|
---|
| 76 | std::vector< std::vector<double> >::const_iterator positer = _particle_positions.begin();
|
---|
| 77 | std::vector<double>::const_iterator chargeiter = _particle_charges.begin();
|
---|
| 78 | double pos[3];
|
---|
| 79 | for (; positer != _particle_positions.end(); ++positer, ++chargeiter) {
|
---|
| 80 | ASSERT( (*positer).size() == 3,
|
---|
| 81 | "InterfaceVMGJob::InterfaceVMGJob() - particle "
|
---|
| 82 | +toString(distance(_particle_positions.begin(), positer))+" has not exactly 3 coordinates.");
|
---|
| 83 | for (size_t i=0;i<3;++i)
|
---|
| 84 | pos[i] = (*positer)[i];
|
---|
| 85 | particles.push_back(Particle::Particle(pos, *chargeiter));
|
---|
| 86 | }
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | InterfaceVMGJob::~InterfaceVMGJob()
|
---|
| 90 | {}
|
---|
| 91 |
|
---|
[e9cfc4] | 92 | void InterfaceVMGJob::ImportRightHandSide(Multigrid& multigrid)
|
---|
| 93 | {
|
---|
| 94 | Index i;
|
---|
| 95 | Vector pos;
|
---|
[cd77fc] | 96 | // VMG::TempGrid *temp_grid = new VMG::TempGrid(129, 0, 0., 1.);
|
---|
[e9cfc4] | 97 |
|
---|
| 98 | Grid& grid = multigrid(multigrid.MaxLevel());
|
---|
[17fcbe7] | 99 | //grid.ClearBoundary(); // we don't have a boundary under periodic boundary conditions
|
---|
[e9cfc4] | 100 |
|
---|
[a1436b] | 101 | // print debugging info on grid size
|
---|
| 102 | LOG(1, "INFO: Mesh has extent " << grid.Extent().MeshWidth() << ".");
|
---|
| 103 | const int gridpoints = pow(2, level);
|
---|
| 104 | LOG(1, "INFO: gridpoints on finest level are " << gridpoints << ".");
|
---|
| 105 | LOG(1, "INFO: "
|
---|
| 106 | << "X in [" << grid.Local().Begin().X() << "," << grid.Local().End().X() << "],"
|
---|
| 107 | << "Y in [" << grid.Local().Begin().Y() << "," << grid.Local().End().Y() << "],"
|
---|
| 108 | << "Z in [" << grid.Local().Begin().Z() << "," << grid.Local().End().Z() << "].");
|
---|
| 109 |
|
---|
[cd77fc] | 110 | /// 1. assign nuclei as smeared-out charges to the grid
|
---|
| 111 |
|
---|
| 112 | /*
|
---|
| 113 | * Charge assignment on the grid
|
---|
| 114 | */
|
---|
[17fcbe7] | 115 | Comm& comm = *MG::GetComm();
|
---|
[cd77fc] | 116 | Grid& particle_grid = comm.GetParticleGrid();
|
---|
| 117 |
|
---|
| 118 | particle_grid.Clear();
|
---|
| 119 |
|
---|
| 120 | assert(particle_grid.Global().LocalSize().IsComponentwiseGreater(
|
---|
| 121 | VMG::MG::GetFactory().GetObjectStorageVal<int>("PARTICLE_NEAR_FIELD_CELLS")));
|
---|
| 122 |
|
---|
[17fcbe7] | 123 | // create smeared-out particle charges on particle_grid via splines
|
---|
[4adef7] | 124 | LOG(1, "INFO: Creating particle grid for " << particles.size() << " particles.");
|
---|
[cd77fc] | 125 | for (std::list<Particle::Particle>::iterator iter = particles.begin();
|
---|
[4adef7] | 126 | iter != particles.end(); ++iter) {
|
---|
| 127 | LOG(2, "DEBUG: Current particle is at " << (*iter).Pos()
|
---|
| 128 | << " with charge " << (*iter).Charge() << ".");
|
---|
[cd77fc] | 129 | spl.SetSpline(particle_grid, *iter);
|
---|
[4adef7] | 130 | }
|
---|
| 131 |
|
---|
[cd77fc] | 132 | // Communicate charges over halo
|
---|
| 133 | comm.CommFromGhosts(particle_grid);
|
---|
| 134 |
|
---|
[a1436b] | 135 | // print nuclei grid to vtk
|
---|
| 136 | comm.PrintGrid(particle_grid, "Sampled Nuclei Density");
|
---|
[17fcbe7] | 137 |
|
---|
| 138 | // add sampled electron charge density onto grid
|
---|
| 139 | std::vector<double>::const_iterator sample_iter = sampled_input.begin();
|
---|
[a1436b] | 140 | for (Grid::iterator iter = grid.Iterators().Local().Begin();
|
---|
| 141 | iter != grid.Iterators().Local().End();
|
---|
[17fcbe7] | 142 | ++iter)
|
---|
[a1436b] | 143 | grid(*iter) = -1. * (*sample_iter++);
|
---|
[17fcbe7] | 144 | assert( sample_iter == sampled_input.end() );
|
---|
| 145 |
|
---|
[a1436b] | 146 | // print electron grid to vtk
|
---|
| 147 | comm.PrintGrid(grid, "Sampled Electron Density");
|
---|
| 148 |
|
---|
[17fcbe7] | 149 | // add particle_grid onto grid
|
---|
[cd77fc] | 150 | for (int i=0; i<grid.Local().Size().X(); ++i)
|
---|
| 151 | for (int j=0; j<grid.Local().Size().Y(); ++j)
|
---|
| 152 | for (int k=0; k<grid.Local().Size().Z(); ++k)
|
---|
| 153 | grid(grid.Local().Begin().X() + i,
|
---|
| 154 | grid.Local().Begin().Y() + j,
|
---|
[a1436b] | 155 | grid.Local().Begin().Z() + k) = 4.0 * VMG::Math::pi * (
|
---|
| 156 | grid(grid.Local().Begin().X() + i,
|
---|
| 157 | grid.Local().Begin().Y() + j,
|
---|
| 158 | grid.Local().Begin().Z() + k) +
|
---|
[cd77fc] | 159 | particle_grid.GetVal(particle_grid.Local().Begin().X() + i,
|
---|
| 160 | particle_grid.Local().Begin().Y() + j,
|
---|
[a1436b] | 161 | particle_grid.Local().Begin().Z() + k));
|
---|
[d12d621] | 162 |
|
---|
[17fcbe7] | 163 | // calculate sum over grid times h^3 as check, should be roughly zero
|
---|
| 164 | const double element_volume = grid.Extent().MeshWidth().Product();
|
---|
[e9cfc4] | 165 | double charge_sum = 0.0;
|
---|
[08cc62] | 166 | for (Grid::iterator grid_iter = grid.Iterators().Local().Begin();
|
---|
| 167 | grid_iter != grid.Iterators().Local().End();
|
---|
| 168 | ++grid_iter)
|
---|
[e9cfc4] | 169 | charge_sum += grid.GetVal(*grid_iter);
|
---|
[17fcbe7] | 170 | charge_sum = element_volume * comm.GlobalSum(charge_sum);
|
---|
| 171 | comm.PrintStringOnce("Grid charge integral: %e", charge_sum/(4.0 * VMG::Math::pi));
|
---|
[e9cfc4] | 172 |
|
---|
[a1436b] | 173 | // print total grid to vtk
|
---|
| 174 | comm.PrintGrid(grid, "Total Charge Density");
|
---|
[16b4fe] | 175 |
|
---|
[e9cfc4] | 176 | // delete temp_grid;
|
---|
| 177 | }
|
---|
| 178 |
|
---|
| 179 | void InterfaceVMGJob::ExportSolution(Grid& grid)
|
---|
| 180 | {
|
---|
| 181 | // grid now contains the sough-for potential
|
---|
[a1436b] | 182 | Comm& comm = *MG::GetComm();
|
---|
[e9cfc4] | 183 |
|
---|
| 184 | const Index begin_local = grid.Global().LocalBegin() - grid.Local().HaloSize1();
|
---|
| 185 | Index i;
|
---|
| 186 |
|
---|
[16b4fe] | 187 | // print output grid to vtk
|
---|
[a1436b] | 188 | comm.PrintGrid(grid, "Potential Solution");
|
---|
[16b4fe] | 189 |
|
---|
[17fcbe7] | 190 | // obtain sampled potential from grid
|
---|
[2bc560] | 191 | returndata.sampled_potential.sampled_grid.clear();
|
---|
[e9cfc4] | 192 | for (i.X()=grid.Local().Begin().X(); i.X()<grid.Local().End().X(); ++i.X())
|
---|
| 193 | for (i.Y()=grid.Local().Begin().Y(); i.Y()<grid.Local().End().Y(); ++i.Y())
|
---|
| 194 | for (i.Z()=grid.Local().Begin().Z(); i.Z()<grid.Local().End().Z(); ++i.Z()) {
|
---|
[2bc560] | 195 | returndata.sampled_potential.sampled_grid.push_back(grid(i));
|
---|
[e9cfc4] | 196 | }
|
---|
| 197 |
|
---|
[17fcbe7] | 198 | // calculate integral over potential as long-range energy contribution
|
---|
| 199 | const double element_volume =
|
---|
| 200 | grid.Extent().MeshWidth().X() * grid.Extent().MeshWidth().Y() * grid.Extent().MeshWidth().Z();
|
---|
[e9cfc4] | 201 | Grid::iterator grid_iter;
|
---|
| 202 | double potential_sum = 0.0;
|
---|
| 203 | for (grid_iter=grid.Iterators().Local().Begin(); grid_iter!=grid.Iterators().Local().End(); ++grid_iter)
|
---|
| 204 | potential_sum += grid.GetVal(*grid_iter);
|
---|
[17fcbe7] | 205 | potential_sum = element_volume * comm.GlobalSum(potential_sum);
|
---|
| 206 | comm.PrintStringOnce("Grid potential sum: %e", potential_sum);
|
---|
[e9cfc4] | 207 |
|
---|
| 208 | //Particle::CommMPI& comm = *dynamic_cast<Particle::CommMPI*>(MG::GetComm());
|
---|
[17fcbe7] | 209 | returndata.e_long = potential_sum;
|
---|
[e9cfc4] | 210 | }
|
---|