| 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 |  * VMGJob.cpp
 | 
|---|
| 26 |  *
 | 
|---|
| 27 |  *  Created on: Jul 13, 2012
 | 
|---|
| 28 |  *      Author: heber
 | 
|---|
| 29 |  */
 | 
|---|
| 30 | 
 | 
|---|
| 31 | 
 | 
|---|
| 32 | // include config.h
 | 
|---|
| 33 | #ifdef HAVE_CONFIG_H
 | 
|---|
| 34 | #include <config.h>
 | 
|---|
| 35 | #endif
 | 
|---|
| 36 | 
 | 
|---|
| 37 | #ifdef HAVE_MPI
 | 
|---|
| 38 | #include "mpi.h"
 | 
|---|
| 39 | #endif
 | 
|---|
| 40 | 
 | 
|---|
| 41 | // include headers that implement a archive in simple text format
 | 
|---|
| 42 | // otherwise BOOST_CLASS_EXPORT_IMPLEMENT has no effect
 | 
|---|
| 43 | #include <boost/archive/text_oarchive.hpp>
 | 
|---|
| 44 | #include <boost/archive/text_iarchive.hpp>
 | 
|---|
| 45 | 
 | 
|---|
| 46 | #include "mg.hpp"
 | 
|---|
| 47 | #include "base/object.hpp"
 | 
|---|
| 48 | #include "base/defs.hpp"
 | 
|---|
| 49 | 
 | 
|---|
| 50 | #ifdef HAVE_MPI
 | 
|---|
| 51 | #include "comm/comm_mpi.hpp"
 | 
|---|
| 52 | #include "comm/domain_decomposition_mpi.hpp"
 | 
|---|
| 53 | #else
 | 
|---|
| 54 | #include "comm/comm_serial.hpp"
 | 
|---|
| 55 | #endif
 | 
|---|
| 56 | #include "cycles/cycle_cs_periodic.hpp"
 | 
|---|
| 57 | #include "grid/multigrid.hpp"
 | 
|---|
| 58 | //#include "grid/tempgrid.hpp"
 | 
|---|
| 59 | #include "level/level_operator_cs.hpp"
 | 
|---|
| 60 | #include "level/stencils.hpp"
 | 
|---|
| 61 | #include "samples/discretization_poisson_fd.hpp"
 | 
|---|
| 62 | #include "smoother/gsrb_poisson_4.hpp"
 | 
|---|
| 63 | #include "solver/givens.hpp"
 | 
|---|
| 64 | //#include "solver/solver_regular.hpp"
 | 
|---|
| 65 | #include "solver/solver_singular.hpp"
 | 
|---|
| 66 | #include "units/particle/comm_mpi_particle.hpp"
 | 
|---|
| 67 | 
 | 
|---|
| 68 | #include "CodePatterns/MemDebug.hpp"
 | 
|---|
| 69 | 
 | 
|---|
| 70 | #include "Jobs/VMGJob.hpp"
 | 
|---|
| 71 | 
 | 
|---|
| 72 | #include "LinearAlgebra/defs.hpp"
 | 
|---|
| 73 | #include "Jobs/InterfaceVMGJob.hpp"
 | 
|---|
| 74 | 
 | 
|---|
| 75 | #include "CodePatterns/Assert.hpp"
 | 
|---|
| 76 | 
 | 
|---|
| 77 | using namespace VMG;
 | 
|---|
| 78 | 
 | 
|---|
| 79 | VMGJob::VMGJob(
 | 
|---|
| 80 |     const JobId_t _JobId,
 | 
|---|
| 81 |     const SamplingGrid &_density_grid,
 | 
|---|
| 82 |     const std::vector< std::vector< double > > &_particle_positions,
 | 
|---|
| 83 |     const std::vector< double > &_particle_charges,
 | 
|---|
| 84 |     const size_t _near_field_cells,
 | 
|---|
| 85 |     const size_t _interpolation_degree,
 | 
|---|
| 86 |     const bool _DoImportParticles,
 | 
|---|
| 87 |     const bool _DoPrintDebug) :
 | 
|---|
| 88 |   FragmentJob(_JobId),
 | 
|---|
| 89 |   density_grid(_density_grid),
 | 
|---|
| 90 |   particle_positions(_particle_positions),
 | 
|---|
| 91 |   particle_charges(_particle_charges),
 | 
|---|
| 92 |   near_field_cells(_near_field_cells),
 | 
|---|
| 93 |   interpolation_degree(_interpolation_degree),
 | 
|---|
| 94 |   DoImportParticles(_DoImportParticles),
 | 
|---|
| 95 |   DoPrintDebug(_DoPrintDebug),
 | 
|---|
| 96 |   returndata(static_cast<const SamplingGridProperties &>(_density_grid)),
 | 
|---|
| 97 |   particles()
 | 
|---|
| 98 | {}
 | 
|---|
| 99 | 
 | 
|---|
| 100 | VMGJob::VMGJob() :
 | 
|---|
| 101 |   FragmentJob(JobId::IllegalJob),
 | 
|---|
| 102 |   near_field_cells(3),
 | 
|---|
| 103 |   interpolation_degree(3),
 | 
|---|
| 104 |   DoImportParticles(true),
 | 
|---|
| 105 |   DoPrintDebug(false),
 | 
|---|
| 106 |   particles()
 | 
|---|
| 107 | {}
 | 
|---|
| 108 | 
 | 
|---|
| 109 | VMGJob::~VMGJob()
 | 
|---|
| 110 | {
 | 
|---|
| 111 | }
 | 
|---|
| 112 | 
 | 
|---|
| 113 | 
 | 
|---|
| 114 | VMGJob::particle_arrays::particle_arrays() :
 | 
|---|
| 115 |   num_particles(0),
 | 
|---|
| 116 |   f(NULL),
 | 
|---|
| 117 |   x(NULL),
 | 
|---|
| 118 |   p(NULL),
 | 
|---|
| 119 |   q(NULL)
 | 
|---|
| 120 | {}
 | 
|---|
| 121 | 
 | 
|---|
| 122 | VMGJob::particle_arrays::~particle_arrays()
 | 
|---|
| 123 | {
 | 
|---|
| 124 |   delete[] f;
 | 
|---|
| 125 |   delete[] x;
 | 
|---|
| 126 |   delete[] p;
 | 
|---|
| 127 |   delete[] q;
 | 
|---|
| 128 | }
 | 
|---|
| 129 | 
 | 
|---|
| 130 | void VMGJob::particle_arrays::init(const size_t _num_particles)
 | 
|---|
| 131 | {
 | 
|---|
| 132 |   num_particles = _num_particles;
 | 
|---|
| 133 |   f = new double[num_particles*3];
 | 
|---|
| 134 |   x = new double[num_particles*3];
 | 
|---|
| 135 |   p = new double[num_particles];
 | 
|---|
| 136 |   q = new double[num_particles];
 | 
|---|
| 137 | }
 | 
|---|
| 138 | 
 | 
|---|
| 139 | void VMGJob::InitVMGArrays()
 | 
|---|
| 140 | {
 | 
|---|
| 141 |   particles.init(particle_charges.size());
 | 
|---|
| 142 | 
 | 
|---|
| 143 |   {
 | 
|---|
| 144 |     size_t index=0;
 | 
|---|
| 145 |     for (std::vector< std::vector< double > >::const_iterator iter = particle_positions.begin();
 | 
|---|
| 146 |         iter != particle_positions.end(); ++iter) {
 | 
|---|
| 147 |       for (std::vector< double >::const_iterator positer = (*iter).begin();
 | 
|---|
| 148 |           positer != (*iter).end(); ++positer) {
 | 
|---|
| 149 |         particles.f[index] = 0.;
 | 
|---|
| 150 |         particles.x[index++] = *positer;
 | 
|---|
| 151 |       }
 | 
|---|
| 152 |     }
 | 
|---|
| 153 |     ASSERT( index == particles.num_particles*3,
 | 
|---|
| 154 |         "VMGJob::VMGJob() - too many particles or components in particle_positions.");
 | 
|---|
| 155 |   }
 | 
|---|
| 156 |   {
 | 
|---|
| 157 |     size_t index = 0;
 | 
|---|
| 158 |     for (std::vector< double >::const_iterator iter = particle_charges.begin();
 | 
|---|
| 159 |         iter != particle_charges.end(); ++iter) {
 | 
|---|
| 160 |       particles.p[index] = 0.;
 | 
|---|
| 161 |       particles.q[index++] = *iter;
 | 
|---|
| 162 |     }
 | 
|---|
| 163 |     ASSERT( index == particles.num_particles,
 | 
|---|
| 164 |         "VMGJob::VMGJob() - too many charges in particle_charges.");
 | 
|---|
| 165 |   }
 | 
|---|
| 166 | }
 | 
|---|
| 167 | 
 | 
|---|
| 168 | 
 | 
|---|
| 169 | FragmentResult::ptr VMGJob::Work()
 | 
|---|
| 170 | {
 | 
|---|
| 171 |   // initialize VMG library solver
 | 
|---|
| 172 |   InitVMG();
 | 
|---|
| 173 | 
 | 
|---|
| 174 |   /*
 | 
|---|
| 175 |    * Start the multigrid solver
 | 
|---|
| 176 |    */
 | 
|---|
| 177 |   MG::Solve();
 | 
|---|
| 178 | 
 | 
|---|
| 179 |   /// create and fill result object
 | 
|---|
| 180 |   // place into returnstream
 | 
|---|
| 181 |   std::stringstream returnstream;
 | 
|---|
| 182 |   boost::archive::text_oarchive oa(returnstream);
 | 
|---|
| 183 |   oa << returndata;
 | 
|---|
| 184 | 
 | 
|---|
| 185 |   FragmentResult::ptr ptr( new FragmentResult(getId(), returnstream.str()) );
 | 
|---|
| 186 | 
 | 
|---|
| 187 |   /*
 | 
|---|
| 188 |    * Delete all data.
 | 
|---|
| 189 |    */
 | 
|---|
| 190 |   MG::Destroy();
 | 
|---|
| 191 | 
 | 
|---|
| 192 |   return ptr;
 | 
|---|
| 193 | }
 | 
|---|
| 194 | 
 | 
|---|
| 195 | /** Initialization of VMG library.
 | 
|---|
| 196 |  *
 | 
|---|
| 197 |  * The contents is heavily inspired from interface_fcs.cpp: VMG_fcs_init() of
 | 
|---|
| 198 |  * the ScaFaCoS project.
 | 
|---|
| 199 |  *
 | 
|---|
| 200 |  */
 | 
|---|
| 201 | void VMGJob::InitVMG()
 | 
|---|
| 202 | {
 | 
|---|
| 203 |   // TODO: As a matter of fact should use open boundary conditions
 | 
|---|
| 204 |   const Boundary boundary(Periodic, Periodic, Periodic);
 | 
|---|
| 205 | 
 | 
|---|
| 206 |   /*
 | 
|---|
| 207 |    * Choose multigrid components (self-registering)
 | 
|---|
| 208 |    */
 | 
|---|
| 209 | #ifdef HAVE_MPI
 | 
|---|
| 210 |   new Particle::CommMPI(boundary, new DomainDecompositionMPI());
 | 
|---|
| 211 | #else
 | 
|---|
| 212 |   new CommSerial(boundary);
 | 
|---|
| 213 | #endif
 | 
|---|
| 214 |   new DiscretizationPoissonFD(4);
 | 
|---|
| 215 |   new VMGInterfaces::InterfaceVMGJob(
 | 
|---|
| 216 |       density_grid,
 | 
|---|
| 217 |       returndata,
 | 
|---|
| 218 |       particle_positions,
 | 
|---|
| 219 |       particle_charges,
 | 
|---|
| 220 |       boundary,
 | 
|---|
| 221 |       2,
 | 
|---|
| 222 |       density_grid.level,
 | 
|---|
| 223 |       Vector(density_grid.begin),
 | 
|---|
| 224 |       density_grid.end[0]-density_grid.begin[0],
 | 
|---|
| 225 |       near_field_cells,
 | 
|---|
| 226 |       DoImportParticles ?
 | 
|---|
| 227 |           VMGInterfaces::InterfaceVMGJob::DoImportParticles
 | 
|---|
| 228 |           : VMGInterfaces::InterfaceVMGJob::DontImportParticles,
 | 
|---|
| 229 |       DoPrintDebug);
 | 
|---|
| 230 |   new LevelOperatorCS(Stencils::RestrictionFullWeight, Stencils::InterpolationTrilinear);
 | 
|---|
| 231 |   new Givens<SolverSingular>();
 | 
|---|
| 232 |   const int cycle_type = 1; // V-type
 | 
|---|
| 233 |   new CycleCSPeriodic(cycle_type);
 | 
|---|
| 234 |   new GaussSeidelRBPoisson4();
 | 
|---|
| 235 | 
 | 
|---|
| 236 |   /*
 | 
|---|
| 237 |    * Register required parameters
 | 
|---|
| 238 |    */
 | 
|---|
| 239 |   new ObjectStorage<int>("PRESMOOTHSTEPS", 3);
 | 
|---|
| 240 |   new ObjectStorage<int>("POSTSMOOTHSTEPS", 3);
 | 
|---|
| 241 |   new ObjectStorage<vmg_float>("PRECISION", 1.0e-10);
 | 
|---|
| 242 |   new ObjectStorage<int>("MAX_ITERATION", 15);
 | 
|---|
| 243 |   new ObjectStorage<int>("PARTICLE_NEAR_FIELD_CELLS", near_field_cells);
 | 
|---|
| 244 |   new ObjectStorage<int>("PARTICLE_INTERPOLATION_DEGREE", interpolation_degree);
 | 
|---|
| 245 | 
 | 
|---|
| 246 |   // first initialize f,x,p,q,... from STL vectors
 | 
|---|
| 247 |   InitVMGArrays();
 | 
|---|
| 248 |   // then initialize as objects with VMG's storage
 | 
|---|
| 249 |   new ObjectStorage<vmg_float*>("PARTICLE_POS_ARRAY", particles.x);
 | 
|---|
| 250 |   new ObjectStorage<vmg_float*>("PARTICLE_CHARGE_ARRAY", particles.q);
 | 
|---|
| 251 |   new ObjectStorage<vmg_float*>("PARTICLE_POTENTIAL_ARRAY", particles.p);
 | 
|---|
| 252 |   new ObjectStorage<vmg_float*>("PARTICLE_FIELD_ARRAY", particles.f);
 | 
|---|
| 253 |   new ObjectStorage<vmg_int>("PARTICLE_NUM_LOCAL", particles.num_particles);
 | 
|---|
| 254 | 
 | 
|---|
| 255 |   /*
 | 
|---|
| 256 |    * Post init
 | 
|---|
| 257 |    */
 | 
|---|
| 258 |   MG::PostInit();
 | 
|---|
| 259 | 
 | 
|---|
| 260 |   /*
 | 
|---|
| 261 |    * Check whether the library is correctly initialized now.
 | 
|---|
| 262 |    */
 | 
|---|
| 263 |   MG::IsInitialized();
 | 
|---|
| 264 | }
 | 
|---|
| 265 | 
 | 
|---|
| 266 | // we need to explicitly instantiate the serialization functions as
 | 
|---|
| 267 | // its is only serialized through its base class FragmentJob
 | 
|---|
| 268 | BOOST_CLASS_EXPORT_IMPLEMENT(VMGJob)
 | 
|---|