| 1 | /*
 | 
|---|
| 2 |  *    vmg - a versatile multigrid solver
 | 
|---|
| 3 |  *    Copyright (C) 2012 Institute for Numerical Simulation, University of Bonn
 | 
|---|
| 4 |  *
 | 
|---|
| 5 |  *  vmg is free software: you can redistribute it and/or modify
 | 
|---|
| 6 |  *  it under the terms of the GNU General Public License as published by
 | 
|---|
| 7 |  *  the Free Software Foundation, either version 3 of the License, or
 | 
|---|
| 8 |  *  (at your option) any later version.
 | 
|---|
| 9 |  *
 | 
|---|
| 10 |  *  vmg is distributed in the hope that it will be useful,
 | 
|---|
| 11 |  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
|---|
| 12 |  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
|---|
| 13 |  *  GNU General Public License for more details.
 | 
|---|
| 14 |  *
 | 
|---|
| 15 |  *  You should have received a copy of the GNU General Public License
 | 
|---|
| 16 |  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 | 
|---|
| 17 |  */
 | 
|---|
| 18 | 
 | 
|---|
| 19 | /**
 | 
|---|
| 20 |  * @file   interface_fcs.cpp
 | 
|---|
| 21 |  * @author Julian Iseringhausen <isering@ins.uni-bonn.de>
 | 
|---|
| 22 |  * @date   Mon Apr 18 12:56:20 2011
 | 
|---|
| 23 |  *
 | 
|---|
| 24 |  * @brief  VMG::InterfaceFCS
 | 
|---|
| 25 |  *
 | 
|---|
| 26 |  */
 | 
|---|
| 27 | 
 | 
|---|
| 28 | #ifdef HAVE_CONFIG_H
 | 
|---|
| 29 | #include <libvmg_config.h>
 | 
|---|
| 30 | #endif
 | 
|---|
| 31 | 
 | 
|---|
| 32 | #ifndef HAVE_MPI
 | 
|---|
| 33 | #error MPI is needed to use the Scafacos interface.
 | 
|---|
| 34 | #endif
 | 
|---|
| 35 | 
 | 
|---|
| 36 | #include <mpi.h>
 | 
|---|
| 37 | #ifdef HAVE_MARMOT
 | 
|---|
| 38 | #include <enhancempicalls.h>
 | 
|---|
| 39 | #include <sourceinfompicalls.h>
 | 
|---|
| 40 | #endif
 | 
|---|
| 41 | 
 | 
|---|
| 42 | #include "base/object.hpp"
 | 
|---|
| 43 | #include "base/timer.hpp"
 | 
|---|
| 44 | #include "comm/domain_decomposition_mpi.hpp"
 | 
|---|
| 45 | #ifdef DEBUG
 | 
|---|
| 46 | #include "comm/mpi/error_handler.hpp"
 | 
|---|
| 47 | #endif
 | 
|---|
| 48 | #include "cycles/cycle_cs_periodic.hpp"
 | 
|---|
| 49 | #include "cycles/cycle_fas_dirichlet.hpp"
 | 
|---|
| 50 | #include "discretization/discretization_poisson_fd.hpp"
 | 
|---|
| 51 | #include "discretization/discretization_poisson_fv.hpp"
 | 
|---|
| 52 | #include "level/level_operator_cs.hpp"
 | 
|---|
| 53 | #include "level/level_operator_fas.hpp"
 | 
|---|
| 54 | #include "level/stencils.hpp"
 | 
|---|
| 55 | #include "smoother/gsrb_poisson_2.hpp"
 | 
|---|
| 56 | #include "smoother/gsrb_poisson_4.hpp"
 | 
|---|
| 57 | #include "solver/givens.hpp"
 | 
|---|
| 58 | #include "solver/solver_regular.hpp"
 | 
|---|
| 59 | #include "solver/solver_singular.hpp"
 | 
|---|
| 60 | #include "units/particle/comm_mpi_particle.hpp"
 | 
|---|
| 61 | #include "units/particle/interface_fcs.h"
 | 
|---|
| 62 | #include "units/particle/interface_particles.hpp"
 | 
|---|
| 63 | 
 | 
|---|
| 64 | #include "cycles/cycle_cs_periodic_debug.hpp"
 | 
|---|
| 65 | //TODO: Remove Debug
 | 
|---|
| 66 | 
 | 
|---|
| 67 | using namespace VMG;
 | 
|---|
| 68 | 
 | 
|---|
| 69 | namespace VMGBackupSettings
 | 
|---|
| 70 | {
 | 
|---|
| 71 |   static vmg_int level = -1;
 | 
|---|
| 72 |   static vmg_int periodic[3] = {-1, -1, -1};
 | 
|---|
| 73 |   static vmg_int max_iter = -1;
 | 
|---|
| 74 |   static vmg_int smoothing_steps = -1;
 | 
|---|
| 75 |   static vmg_int cycle_type = -1;
 | 
|---|
| 76 |   static vmg_float precision = -1;
 | 
|---|
| 77 |   static vmg_float box_offset[3];
 | 
|---|
| 78 |   static vmg_float box_size = -1.0;
 | 
|---|
| 79 |   static vmg_int near_field_cells = -1;
 | 
|---|
| 80 |   static vmg_int interpolation_degree = -1;
 | 
|---|
| 81 |   static vmg_int discretization_order = -1;
 | 
|---|
| 82 |   static MPI_Comm mpi_comm;
 | 
|---|
| 83 | }
 | 
|---|
| 84 | 
 | 
|---|
| 85 | static void VMG_fcs_init(vmg_int level, vmg_int* periodic,vmg_int max_iter,
 | 
|---|
| 86 |                          vmg_int smoothing_steps, vmg_int cycle_type, vmg_float precision,
 | 
|---|
| 87 |                          vmg_float* box_offset, vmg_float box_size,
 | 
|---|
| 88 |                          vmg_int near_field_cells, vmg_int interpolation_degree,
 | 
|---|
| 89 |                          vmg_int discretization_order, MPI_Comm mpi_comm)
 | 
|---|
| 90 | {
 | 
|---|
| 91 |   VMGBackupSettings::level = level;
 | 
|---|
| 92 |   std::memcpy(VMGBackupSettings::periodic, periodic, 3*sizeof(vmg_int));
 | 
|---|
| 93 |   VMGBackupSettings::max_iter = max_iter;
 | 
|---|
| 94 |   VMGBackupSettings::smoothing_steps = smoothing_steps;
 | 
|---|
| 95 |   VMGBackupSettings::cycle_type = cycle_type;
 | 
|---|
| 96 |   VMGBackupSettings::precision = precision;
 | 
|---|
| 97 |   std::memcpy(VMGBackupSettings::box_offset, box_offset, 3*sizeof(vmg_float));
 | 
|---|
| 98 |   VMGBackupSettings::box_size = box_size;
 | 
|---|
| 99 |   VMGBackupSettings::near_field_cells = near_field_cells;
 | 
|---|
| 100 |   VMGBackupSettings::interpolation_degree = interpolation_degree;
 | 
|---|
| 101 |   VMGBackupSettings::discretization_order = discretization_order;
 | 
|---|
| 102 |   VMGBackupSettings::mpi_comm = mpi_comm;
 | 
|---|
| 103 | 
 | 
|---|
| 104 | #ifdef DEBUG
 | 
|---|
| 105 |   MPI_Errhandler mpiErrorHandler;
 | 
|---|
| 106 |   MPI_Comm_create_errhandler(VMG::MPI::ConvertToException, &mpiErrorHandler);
 | 
|---|
| 107 |   MPI_Comm_set_errhandler(mpi_comm, mpiErrorHandler);
 | 
|---|
| 108 | #endif
 | 
|---|
| 109 | 
 | 
|---|
| 110 |   const Boundary boundary(periodic[0] ? Periodic : Open,
 | 
|---|
| 111 |                           periodic[1] ? Periodic : Open,
 | 
|---|
| 112 |                           periodic[2] ? Periodic : Open);
 | 
|---|
| 113 | 
 | 
|---|
| 114 |   const bool singular = boundary[0] == Periodic &&
 | 
|---|
| 115 |                         boundary[1] == Periodic &&
 | 
|---|
| 116 |                         boundary[2] == Periodic;
 | 
|---|
| 117 | 
 | 
|---|
| 118 |   /*
 | 
|---|
| 119 |    * Choose multigrid components
 | 
|---|
| 120 |    */
 | 
|---|
| 121 |   if (singular) {
 | 
|---|
| 122 | 
 | 
|---|
| 123 |     new Particle::CommMPI(boundary, new DomainDecompositionMPI(), mpi_comm);
 | 
|---|
| 124 |     new DiscretizationPoissonFD(discretization_order);
 | 
|---|
| 125 |     new InterfaceParticles(boundary, 2, level, Vector(box_offset), box_size, near_field_cells);
 | 
|---|
| 126 |     new LevelOperatorCS(Stencils::RestrictionFullWeight, Stencils::InterpolationTrilinear);
 | 
|---|
| 127 |     new Givens<SolverSingular>();
 | 
|---|
| 128 |     new CycleCSPeriodic(cycle_type);
 | 
|---|
| 129 | 
 | 
|---|
| 130 |   }else {
 | 
|---|
| 131 | 
 | 
|---|
| 132 |     new Particle::CommMPI(boundary, new DomainDecompositionMPI(), mpi_comm);
 | 
|---|
| 133 |     new DiscretizationPoissonFV(discretization_order);
 | 
|---|
| 134 |     new InterfaceParticles(boundary, 2, level, Vector(box_offset), box_size, near_field_cells, 9, 1.6);
 | 
|---|
| 135 |     new LevelOperatorFAS(Stencils::RestrictionFullWeight, Stencils::Injection, Stencils::InterpolationTrilinear);
 | 
|---|
| 136 |     new Givens<SolverRegular>();
 | 
|---|
| 137 |     new CycleFASDirichlet(cycle_type);
 | 
|---|
| 138 | 
 | 
|---|
| 139 |   }
 | 
|---|
| 140 | 
 | 
|---|
| 141 |   /*
 | 
|---|
| 142 |    * Use Gauss-Seidel Red-Black ordering
 | 
|---|
| 143 |    */
 | 
|---|
| 144 |   if (discretization_order == 2)
 | 
|---|
| 145 |     new GaussSeidelRBPoisson2();
 | 
|---|
| 146 |   else
 | 
|---|
| 147 |     new GaussSeidelRBPoisson4();
 | 
|---|
| 148 | 
 | 
|---|
| 149 |   /*
 | 
|---|
| 150 |    * Register required parameters
 | 
|---|
| 151 |    */
 | 
|---|
| 152 |   new ObjectStorage<int>("PRESMOOTHSTEPS", smoothing_steps);
 | 
|---|
| 153 |   new ObjectStorage<int>("POSTSMOOTHSTEPS", smoothing_steps);
 | 
|---|
| 154 |   new ObjectStorage<vmg_float>("PRECISION", precision);
 | 
|---|
| 155 |   new ObjectStorage<int>("MAX_ITERATION", max_iter);
 | 
|---|
| 156 |   new ObjectStorage<int>("PARTICLE_NEAR_FIELD_CELLS", near_field_cells);
 | 
|---|
| 157 |   new ObjectStorage<int>("PARTICLE_INTERPOLATION_DEGREE", interpolation_degree);
 | 
|---|
| 158 | 
 | 
|---|
| 159 |   /*
 | 
|---|
| 160 |    * Post init
 | 
|---|
| 161 |    */
 | 
|---|
| 162 |   MG::PostInit();
 | 
|---|
| 163 | 
 | 
|---|
| 164 |   /*
 | 
|---|
| 165 |    * Check whether the library is correctly initialized now.
 | 
|---|
| 166 |    */
 | 
|---|
| 167 |   MG::IsInitialized();
 | 
|---|
| 168 | }
 | 
|---|
| 169 | 
 | 
|---|
| 170 | void VMG_fcs_setup(vmg_int level, vmg_int* periodic, vmg_int max_iter,
 | 
|---|
| 171 |                    vmg_int smoothing_steps, vmg_int cycle_type, vmg_float precision,
 | 
|---|
| 172 |                    vmg_float* box_offset, vmg_float box_size,
 | 
|---|
| 173 |                    vmg_int near_field_cells, vmg_int interpolation_degree,
 | 
|---|
| 174 |                    vmg_int discretization_order, MPI_Comm mpi_comm)
 | 
|---|
| 175 | {
 | 
|---|
| 176 |   if (VMGBackupSettings::level != level ||
 | 
|---|
| 177 |       VMGBackupSettings::periodic[0] != periodic[0] ||
 | 
|---|
| 178 |       VMGBackupSettings::periodic[1] != periodic[1] ||
 | 
|---|
| 179 |       VMGBackupSettings::periodic[2] != periodic[2] ||
 | 
|---|
| 180 |       VMGBackupSettings::max_iter != max_iter ||
 | 
|---|
| 181 |       VMGBackupSettings::smoothing_steps != smoothing_steps ||
 | 
|---|
| 182 |       VMGBackupSettings::cycle_type != cycle_type ||
 | 
|---|
| 183 |       VMGBackupSettings::precision != precision ||
 | 
|---|
| 184 |       VMGBackupSettings::box_offset[0] != box_offset[0] ||
 | 
|---|
| 185 |       VMGBackupSettings::box_offset[1] != box_offset[1] ||
 | 
|---|
| 186 |       VMGBackupSettings::box_offset[2] != box_offset[2] ||
 | 
|---|
| 187 |       VMGBackupSettings::box_size != box_size ||
 | 
|---|
| 188 |       VMGBackupSettings::near_field_cells != near_field_cells ||
 | 
|---|
| 189 |       VMGBackupSettings::interpolation_degree != interpolation_degree ||
 | 
|---|
| 190 |       VMGBackupSettings::discretization_order != discretization_order ||
 | 
|---|
| 191 |       VMGBackupSettings::mpi_comm != mpi_comm) {
 | 
|---|
| 192 | 
 | 
|---|
| 193 |     VMG_fcs_destroy();
 | 
|---|
| 194 |     VMG_fcs_init(level, periodic, max_iter,
 | 
|---|
| 195 |                  smoothing_steps, cycle_type, precision,
 | 
|---|
| 196 |                  box_offset, box_size, near_field_cells,
 | 
|---|
| 197 |                  interpolation_degree, discretization_order,
 | 
|---|
| 198 |                  mpi_comm);
 | 
|---|
| 199 | 
 | 
|---|
| 200 |   }
 | 
|---|
| 201 | }
 | 
|---|
| 202 | 
 | 
|---|
| 203 | int VMG_fcs_check()
 | 
|---|
| 204 | {
 | 
|---|
| 205 |   const int& near_field_cells = MG::GetFactory().GetObjectStorageVal<int>("PARTICLE_NEAR_FIELD_CELLS");
 | 
|---|
| 206 |   const Multigrid& multigrid = *MG::GetRhs();
 | 
|---|
| 207 |   const Grid& grid = multigrid(multigrid.MaxLevel());
 | 
|---|
| 208 | 
 | 
|---|
| 209 |   vmg_int error_code = 0;
 | 
|---|
| 210 | 
 | 
|---|
| 211 |   if (!grid.Global().LocalSize().IsComponentwiseGreater(near_field_cells))
 | 
|---|
| 212 |     error_code = 1;
 | 
|---|
| 213 | 
 | 
|---|
| 214 |   return MG::GetComm()->GlobalMax(error_code);
 | 
|---|
| 215 | }
 | 
|---|
| 216 | 
 | 
|---|
| 217 | void VMG_fcs_run(vmg_float* x, vmg_float* q, vmg_float* p, vmg_float* f, vmg_int num_particles_local)
 | 
|---|
| 218 | {
 | 
|---|
| 219 |   /*
 | 
|---|
| 220 |    * Register parameters for later use.
 | 
|---|
| 221 |    */
 | 
|---|
| 222 |   new ObjectStorage<vmg_float*>("PARTICLE_POS_ARRAY", x);
 | 
|---|
| 223 |   new ObjectStorage<vmg_float*>("PARTICLE_CHARGE_ARRAY", q);
 | 
|---|
| 224 |   new ObjectStorage<vmg_float*>("PARTICLE_POTENTIAL_ARRAY", p);
 | 
|---|
| 225 |   new ObjectStorage<vmg_float*>("PARTICLE_FIELD_ARRAY", f);
 | 
|---|
| 226 |   new ObjectStorage<vmg_int>("PARTICLE_NUM_LOCAL", num_particles_local);
 | 
|---|
| 227 | 
 | 
|---|
| 228 |   /*
 | 
|---|
| 229 |    * Start the multigrid solver
 | 
|---|
| 230 |    */
 | 
|---|
| 231 |   MG::Solve();
 | 
|---|
| 232 | 
 | 
|---|
| 233 |   Timer::Print();
 | 
|---|
| 234 | }
 | 
|---|
| 235 | 
 | 
|---|
| 236 | void VMG_fcs_print_timer()
 | 
|---|
| 237 | {
 | 
|---|
| 238 |   Timer::Print();
 | 
|---|
| 239 | }
 | 
|---|
| 240 | 
 | 
|---|
| 241 | void VMG_fcs_destroy(void)
 | 
|---|
| 242 | {
 | 
|---|
| 243 |   /*
 | 
|---|
| 244 |    * Delete all data.
 | 
|---|
| 245 |    */
 | 
|---|
| 246 |   MG::Destroy();
 | 
|---|
| 247 | }
 | 
|---|