| [d2a0f6d] | 1 | /*
 | 
|---|
 | 2 |  * Project: MoleCuilder
 | 
|---|
 | 3 |  * Description: creates and alters molecular systems
 | 
|---|
 | 4 |  * Copyright (C)  2012 University of Bonn. All rights reserved.
 | 
|---|
| [5aaa43] | 5 |  * Copyright (C)  2013 Frederik Heber. All rights reserved.
 | 
|---|
| [d2a0f6d] | 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 | 
 | 
|---|
| [8a8c8c] | 37 | #ifdef HAVE_MPI
 | 
|---|
 | 38 | #include "mpi.h"
 | 
|---|
 | 39 | #endif
 | 
|---|
 | 40 | 
 | 
|---|
| [d2a0f6d] | 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 | 
 | 
|---|
| [e9cfc4] | 46 | #include "mg.hpp"
 | 
|---|
 | 47 | #include "base/object.hpp"
 | 
|---|
 | 48 | #include "base/defs.hpp"
 | 
|---|
| [8a8c8c] | 49 | 
 | 
|---|
 | 50 | #ifdef HAVE_MPI
 | 
|---|
 | 51 | #include "comm/comm_mpi.hpp"
 | 
|---|
 | 52 | #include "comm/domain_decomposition_mpi.hpp"
 | 
|---|
 | 53 | #else
 | 
|---|
| [e9cfc4] | 54 | #include "comm/comm_serial.hpp"
 | 
|---|
| [8a8c8c] | 55 | #endif
 | 
|---|
| [ee9018] | 56 | // periodic boundary conditions
 | 
|---|
| [e9cfc4] | 57 | #include "cycles/cycle_cs_periodic.hpp"
 | 
|---|
| [e8e472b] | 58 | #include "discretization/discretization_poisson_fd.hpp"
 | 
|---|
| [ee9018] | 59 | #include "level/level_operator_cs.hpp"
 | 
|---|
 | 60 | #include "smoother/gsrb_poisson_4.hpp"
 | 
|---|
 | 61 | #include "solver/solver_singular.hpp"
 | 
|---|
 | 62 | // open boundary conditions
 | 
|---|
 | 63 | #include "cycles/cycle_fas_dirichlet.hpp"
 | 
|---|
 | 64 | #include "discretization/discretization_poisson_fv.hpp"
 | 
|---|
| [e9cfc4] | 65 | #include "grid/multigrid.hpp"
 | 
|---|
 | 66 | //#include "grid/tempgrid.hpp"
 | 
|---|
| [ee9018] | 67 | #include "level/level_operator_fas.hpp"
 | 
|---|
| [e9cfc4] | 68 | #include "level/stencils.hpp"
 | 
|---|
| [ee9018] | 69 | #include "smoother/gsrb_poisson_2.hpp"
 | 
|---|
| [e9cfc4] | 70 | #include "solver/givens.hpp"
 | 
|---|
| [ee9018] | 71 | #include "solver/solver_regular.hpp"
 | 
|---|
| [a82602] | 72 | #include "units/particle/comm_mpi_particle.hpp"
 | 
|---|
| [e9cfc4] | 73 | 
 | 
|---|
| [0990e1] | 74 | #include "CodePatterns/MemDebug.hpp"
 | 
|---|
 | 75 | 
 | 
|---|
 | 76 | #include "Jobs/VMGJob.hpp"
 | 
|---|
 | 77 | 
 | 
|---|
| [e9cfc4] | 78 | #include "LinearAlgebra/defs.hpp"
 | 
|---|
 | 79 | #include "Jobs/InterfaceVMGJob.hpp"
 | 
|---|
 | 80 | 
 | 
|---|
| [a82602] | 81 | #include "CodePatterns/Assert.hpp"
 | 
|---|
| [9ff818] | 82 | #include "CodePatterns/Log.hpp"
 | 
|---|
| [a82602] | 83 | 
 | 
|---|
| [e9cfc4] | 84 | using namespace VMG;
 | 
|---|
| [d2a0f6d] | 85 | 
 | 
|---|
| [d12d621] | 86 | VMGJob::VMGJob(
 | 
|---|
 | 87 |     const JobId_t _JobId,
 | 
|---|
| [cd77fc] | 88 |     const SamplingGrid &_density_grid,
 | 
|---|
 | 89 |     const std::vector< std::vector< double > > &_particle_positions,
 | 
|---|
| [065574] | 90 |     const std::vector< double > &_particle_charges,
 | 
|---|
| [cd2591] | 91 |     const size_t _near_field_cells,
 | 
|---|
| [b6b21a] | 92 |     const size_t _interpolation_degree,
 | 
|---|
| [e2925fd] | 93 |     const bool _DoImportParticles,
 | 
|---|
| [ee9018] | 94 |     const bool _DoPrintDebug,
 | 
|---|
| [17e4fd] | 95 |     const bool _OpenBoundaryConditions,
 | 
|---|
 | 96 |     const bool _DoSmearCharges) :
 | 
|---|
| [d2a0f6d] | 97 |   FragmentJob(_JobId),
 | 
|---|
| [442cee] | 98 |   density_grid(_density_grid),
 | 
|---|
| [cd77fc] | 99 |   particle_positions(_particle_positions),
 | 
|---|
| [2bc560] | 100 |   particle_charges(_particle_charges),
 | 
|---|
| [065574] | 101 |   near_field_cells(_near_field_cells),
 | 
|---|
| [cd2591] | 102 |   interpolation_degree(_interpolation_degree),
 | 
|---|
| [e2925fd] | 103 |   DoImportParticles(_DoImportParticles),
 | 
|---|
 | 104 |   DoPrintDebug(_DoPrintDebug),
 | 
|---|
| [ee9018] | 105 |   OpenBoundaryConditions(_OpenBoundaryConditions),
 | 
|---|
| [17e4fd] | 106 |   DoSmearCharges(_DoSmearCharges),
 | 
|---|
| [a82602] | 107 |   returndata(static_cast<const SamplingGridProperties &>(_density_grid)),
 | 
|---|
| [e2925fd] | 108 |   particles()
 | 
|---|
| [28c025] | 109 | {}
 | 
|---|
| [d2a0f6d] | 110 | 
 | 
|---|
 | 111 | VMGJob::VMGJob() :
 | 
|---|
| [065574] | 112 |   FragmentJob(JobId::IllegalJob),
 | 
|---|
| [cd2591] | 113 |   near_field_cells(3),
 | 
|---|
 | 114 |   interpolation_degree(3),
 | 
|---|
| [e2925fd] | 115 |   DoImportParticles(true),
 | 
|---|
 | 116 |   DoPrintDebug(false),
 | 
|---|
| [ee9018] | 117 |   OpenBoundaryConditions(false),
 | 
|---|
| [17e4fd] | 118 |   DoSmearCharges(false),
 | 
|---|
| [e2925fd] | 119 |   particles()
 | 
|---|
| [e089fb] | 120 | {}
 | 
|---|
 | 121 | 
 | 
|---|
 | 122 | VMGJob::~VMGJob()
 | 
|---|
 | 123 | {
 | 
|---|
 | 124 | }
 | 
|---|
 | 125 | 
 | 
|---|
 | 126 | 
 | 
|---|
 | 127 | VMGJob::particle_arrays::particle_arrays() :
 | 
|---|
| [a82602] | 128 |   num_particles(0),
 | 
|---|
 | 129 |   f(NULL),
 | 
|---|
 | 130 |   x(NULL),
 | 
|---|
 | 131 |   p(NULL),
 | 
|---|
 | 132 |   q(NULL)
 | 
|---|
| [d2a0f6d] | 133 | {}
 | 
|---|
 | 134 | 
 | 
|---|
| [e089fb] | 135 | VMGJob::particle_arrays::~particle_arrays()
 | 
|---|
| [a82602] | 136 | {
 | 
|---|
 | 137 |   delete[] f;
 | 
|---|
 | 138 |   delete[] x;
 | 
|---|
 | 139 |   delete[] p;
 | 
|---|
 | 140 |   delete[] q;
 | 
|---|
 | 141 | }
 | 
|---|
 | 142 | 
 | 
|---|
| [e089fb] | 143 | void VMGJob::particle_arrays::init(const size_t _num_particles)
 | 
|---|
| [a82602] | 144 | {
 | 
|---|
| [e089fb] | 145 |   num_particles = _num_particles;
 | 
|---|
| [a82602] | 146 |   f = new double[num_particles*3];
 | 
|---|
 | 147 |   x = new double[num_particles*3];
 | 
|---|
 | 148 |   p = new double[num_particles];
 | 
|---|
 | 149 |   q = new double[num_particles];
 | 
|---|
| [e089fb] | 150 | }
 | 
|---|
 | 151 | 
 | 
|---|
 | 152 | void VMGJob::InitVMGArrays()
 | 
|---|
 | 153 | {
 | 
|---|
 | 154 |   particles.init(particle_charges.size());
 | 
|---|
| [a82602] | 155 | 
 | 
|---|
 | 156 |   {
 | 
|---|
 | 157 |     size_t index=0;
 | 
|---|
 | 158 |     for (std::vector< std::vector< double > >::const_iterator iter = particle_positions.begin();
 | 
|---|
 | 159 |         iter != particle_positions.end(); ++iter) {
 | 
|---|
 | 160 |       for (std::vector< double >::const_iterator positer = (*iter).begin();
 | 
|---|
 | 161 |           positer != (*iter).end(); ++positer) {
 | 
|---|
| [e089fb] | 162 |         particles.f[index] = 0.;
 | 
|---|
 | 163 |         particles.x[index++] = *positer;
 | 
|---|
| [a82602] | 164 |       }
 | 
|---|
 | 165 |     }
 | 
|---|
| [e089fb] | 166 |     ASSERT( index == particles.num_particles*3,
 | 
|---|
| [a82602] | 167 |         "VMGJob::VMGJob() - too many particles or components in particle_positions.");
 | 
|---|
 | 168 |   }
 | 
|---|
 | 169 |   {
 | 
|---|
 | 170 |     size_t index = 0;
 | 
|---|
 | 171 |     for (std::vector< double >::const_iterator iter = particle_charges.begin();
 | 
|---|
 | 172 |         iter != particle_charges.end(); ++iter) {
 | 
|---|
| [e089fb] | 173 |       particles.p[index] = 0.;
 | 
|---|
 | 174 |       particles.q[index++] = *iter;
 | 
|---|
| [a82602] | 175 |     }
 | 
|---|
| [e089fb] | 176 |     ASSERT( index == particles.num_particles,
 | 
|---|
| [a82602] | 177 |         "VMGJob::VMGJob() - too many charges in particle_charges.");
 | 
|---|
 | 178 |   }
 | 
|---|
 | 179 | }
 | 
|---|
 | 180 | 
 | 
|---|
| [d2a0f6d] | 181 | 
 | 
|---|
 | 182 | FragmentResult::ptr VMGJob::Work()
 | 
|---|
 | 183 | {
 | 
|---|
| [e9cfc4] | 184 |   // initialize VMG library solver
 | 
|---|
 | 185 |   InitVMG();
 | 
|---|
 | 186 | 
 | 
|---|
 | 187 |   /*
 | 
|---|
 | 188 |    * Start the multigrid solver
 | 
|---|
 | 189 |    */
 | 
|---|
 | 190 |   MG::Solve();
 | 
|---|
 | 191 | 
 | 
|---|
 | 192 |   /// create and fill result object
 | 
|---|
| [442cee] | 193 |   // place into returnstream
 | 
|---|
 | 194 |   std::stringstream returnstream;
 | 
|---|
| [ae68b5] | 195 |   {
 | 
|---|
 | 196 |     const VMGData archivedata(returndata);
 | 
|---|
 | 197 |     boost::archive::text_oarchive oa(returnstream);
 | 
|---|
 | 198 |     oa << archivedata;
 | 
|---|
 | 199 |   }
 | 
|---|
| [442cee] | 200 | 
 | 
|---|
 | 201 |   FragmentResult::ptr ptr( new FragmentResult(getId(), returnstream.str()) );
 | 
|---|
| [9ff818] | 202 |   if (returndata.precision != 0.) {
 | 
|---|
| [7debff] | 203 |     ptr->exitflag =
 | 
|---|
 | 204 |         ((returndata.relative_residual < returndata.precision)
 | 
|---|
 | 205 |         || (returndata.residual < returndata.precision)) ? 0 : 1;
 | 
|---|
| [9ff818] | 206 |     if (ptr->exitflag != 0)
 | 
|---|
 | 207 |       ELOG(1, "Job #" << ptr->getId() << " failed to reach desired accuracy.");
 | 
|---|
 | 208 |   } else {
 | 
|---|
 | 209 |     LOG(3, "INFO: No precision returned from VMG job, not checking.");
 | 
|---|
 | 210 |   }
 | 
|---|
| [e9cfc4] | 211 | 
 | 
|---|
 | 212 |   /*
 | 
|---|
 | 213 |    * Delete all data.
 | 
|---|
 | 214 |    */
 | 
|---|
 | 215 |   MG::Destroy();
 | 
|---|
 | 216 | 
 | 
|---|
| [d2a0f6d] | 217 |   return ptr;
 | 
|---|
 | 218 | }
 | 
|---|
 | 219 | 
 | 
|---|
| [e9cfc4] | 220 | /** Initialization of VMG library.
 | 
|---|
 | 221 |  *
 | 
|---|
 | 222 |  * The contents is heavily inspired from interface_fcs.cpp: VMG_fcs_init() of
 | 
|---|
 | 223 |  * the ScaFaCoS project.
 | 
|---|
 | 224 |  *
 | 
|---|
 | 225 |  */
 | 
|---|
 | 226 | void VMGJob::InitVMG()
 | 
|---|
 | 227 | {
 | 
|---|
| [ee9018] | 228 |   Boundary *boundary = NULL;
 | 
|---|
 | 229 |   if (OpenBoundaryConditions)
 | 
|---|
 | 230 |     boundary = new Boundary(Open, Open, Open);
 | 
|---|
 | 231 |   else
 | 
|---|
 | 232 |     boundary = new Boundary(Periodic, Periodic, Periodic);
 | 
|---|
| [e9cfc4] | 233 | 
 | 
|---|
 | 234 |   /*
 | 
|---|
 | 235 |    * Choose multigrid components (self-registering)
 | 
|---|
 | 236 |    */
 | 
|---|
| [ee9018] | 237 | 
 | 
|---|
| [8a8c8c] | 238 | #ifdef HAVE_MPI
 | 
|---|
| [ee9018] | 239 |   new Particle::CommMPI(*boundary, new DomainDecompositionMPI());
 | 
|---|
| [8a8c8c] | 240 | #else
 | 
|---|
| [ee9018] | 241 |   new CommSerial(*boundary);
 | 
|---|
| [8a8c8c] | 242 | #endif
 | 
|---|
| [ee9018] | 243 |   if (OpenBoundaryConditions)
 | 
|---|
 | 244 |     new DiscretizationPoissonFV(2);
 | 
|---|
 | 245 |   else
 | 
|---|
 | 246 |     new DiscretizationPoissonFD(4);
 | 
|---|
| [e9cfc4] | 247 |   new VMGInterfaces::InterfaceVMGJob(
 | 
|---|
| [8f3cdd] | 248 |       density_grid,
 | 
|---|
| [2bc560] | 249 |       returndata,
 | 
|---|
| [cd77fc] | 250 |       particle_positions,
 | 
|---|
 | 251 |       particle_charges,
 | 
|---|
| [ee9018] | 252 |       *boundary,
 | 
|---|
| [e9cfc4] | 253 |       2,
 | 
|---|
| [28c025] | 254 |       density_grid.level,
 | 
|---|
 | 255 |       Vector(density_grid.begin),
 | 
|---|
| [3d9a8d] | 256 |       density_grid.end[0]-density_grid.begin[0],
 | 
|---|
| [b6b21a] | 257 |       near_field_cells,
 | 
|---|
| [e2925fd] | 258 |       DoImportParticles ?
 | 
|---|
 | 259 |           VMGInterfaces::InterfaceVMGJob::DoImportParticles
 | 
|---|
 | 260 |           : VMGInterfaces::InterfaceVMGJob::DontImportParticles,
 | 
|---|
| [17e4fd] | 261 |       DoPrintDebug,
 | 
|---|
 | 262 |       DoSmearCharges);
 | 
|---|
| [e9cfc4] | 263 |   const int cycle_type = 1; // V-type
 | 
|---|
| [ee9018] | 264 |   if (OpenBoundaryConditions) {
 | 
|---|
 | 265 |     new LevelOperatorFAS(Stencils::RestrictionFullWeight, Stencils::Injection, Stencils::InterpolationTrilinear);
 | 
|---|
 | 266 |     new Givens<SolverRegular>();
 | 
|---|
 | 267 |     new CycleFASDirichlet(cycle_type);
 | 
|---|
 | 268 |     new GaussSeidelRBPoisson2();
 | 
|---|
 | 269 |   } else {
 | 
|---|
 | 270 |     new LevelOperatorCS(Stencils::RestrictionFullWeight, Stencils::InterpolationTrilinear);
 | 
|---|
 | 271 |     new Givens<SolverSingular>();
 | 
|---|
 | 272 |     new CycleCSPeriodic(cycle_type);
 | 
|---|
 | 273 |     new GaussSeidelRBPoisson4();
 | 
|---|
 | 274 |   }
 | 
|---|
 | 275 |   delete boundary;
 | 
|---|
| [e9cfc4] | 276 | 
 | 
|---|
 | 277 |   /*
 | 
|---|
 | 278 |    * Register required parameters
 | 
|---|
 | 279 |    */
 | 
|---|
 | 280 |   new ObjectStorage<int>("PRESMOOTHSTEPS", 3);
 | 
|---|
 | 281 |   new ObjectStorage<int>("POSTSMOOTHSTEPS", 3);
 | 
|---|
 | 282 |   new ObjectStorage<vmg_float>("PRECISION", 1.0e-10);
 | 
|---|
 | 283 |   new ObjectStorage<int>("MAX_ITERATION", 15);
 | 
|---|
| [cd77fc] | 284 |   new ObjectStorage<int>("PARTICLE_NEAR_FIELD_CELLS", near_field_cells);
 | 
|---|
| [cd2591] | 285 |   new ObjectStorage<int>("PARTICLE_INTERPOLATION_DEGREE", interpolation_degree);
 | 
|---|
| [e9cfc4] | 286 | 
 | 
|---|
| [a82602] | 287 |   // first initialize f,x,p,q,... from STL vectors
 | 
|---|
 | 288 |   InitVMGArrays();
 | 
|---|
 | 289 |   // then initialize as objects with VMG's storage
 | 
|---|
| [e089fb] | 290 |   new ObjectStorage<vmg_float*>("PARTICLE_POS_ARRAY", particles.x);
 | 
|---|
 | 291 |   new ObjectStorage<vmg_float*>("PARTICLE_CHARGE_ARRAY", particles.q);
 | 
|---|
 | 292 |   new ObjectStorage<vmg_float*>("PARTICLE_POTENTIAL_ARRAY", particles.p);
 | 
|---|
 | 293 |   new ObjectStorage<vmg_float*>("PARTICLE_FIELD_ARRAY", particles.f);
 | 
|---|
 | 294 |   new ObjectStorage<vmg_int>("PARTICLE_NUM_LOCAL", particles.num_particles);
 | 
|---|
| [a82602] | 295 | 
 | 
|---|
| [e9cfc4] | 296 |   /*
 | 
|---|
 | 297 |    * Post init
 | 
|---|
 | 298 |    */
 | 
|---|
 | 299 |   MG::PostInit();
 | 
|---|
 | 300 | 
 | 
|---|
 | 301 |   /*
 | 
|---|
 | 302 |    * Check whether the library is correctly initialized now.
 | 
|---|
 | 303 |    */
 | 
|---|
 | 304 |   MG::IsInitialized();
 | 
|---|
 | 305 | }
 | 
|---|
 | 306 | 
 | 
|---|
| [d2a0f6d] | 307 | // we need to explicitly instantiate the serialization functions as
 | 
|---|
 | 308 | // its is only serialized through its base class FragmentJob
 | 
|---|
 | 309 | BOOST_CLASS_EXPORT_IMPLEMENT(VMGJob)
 | 
|---|