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