| [fcf7f6] | 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 |
|
|---|
| [48b662] | 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 <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>
|
|---|
| [ac6d04] | 37 | #ifdef HAVE_MARMOT
|
|---|
| 38 | #include <enhancempicalls.h>
|
|---|
| 39 | #include <sourceinfompicalls.h>
|
|---|
| 40 | #endif
|
|---|
| [48b662] | 41 |
|
|---|
| 42 | #include "base/object.hpp"
|
|---|
| [dfed1c] | 43 | #include "base/timer.hpp"
|
|---|
| 44 | #include "comm/domain_decomposition_mpi.hpp"
|
|---|
| [f003a9] | 45 | #ifdef DEBUG
|
|---|
| [ac6d04] | 46 | #include "comm/mpi/error_handler.hpp"
|
|---|
| [f003a9] | 47 | #endif
|
|---|
| [48b662] | 48 | #include "level/level_operator_cs.hpp"
|
|---|
| [ac6d04] | 49 | #include "level/level_operator_fas.hpp"
|
|---|
| [b7b317] | 50 | #include "level/stencils.hpp"
|
|---|
| [48b662] | 51 | #include "samples/discretization_poisson_fd.hpp"
|
|---|
| [ac6d04] | 52 | #include "samples/discretization_poisson_fv.hpp"
|
|---|
| [48b662] | 53 | #include "samples/techniques.hpp"
|
|---|
| [4571da] | 54 | #include "smoother/gsrb.hpp"
|
|---|
| [48b662] | 55 | #ifdef HAVE_LAPACK
|
|---|
| 56 | #include "solver/dgesv.hpp"
|
|---|
| 57 | #include "solver/dsysv.hpp"
|
|---|
| 58 | #else
|
|---|
| 59 | #include "solver/givens.hpp"
|
|---|
| 60 | #endif
|
|---|
| [dfed1c] | 61 | #include "solver/solver_regular.hpp"
|
|---|
| 62 | #include "solver/solver_singular.hpp"
|
|---|
| [f003a9] | 63 | #include "units/particle/comm_mpi_particle.hpp"
|
|---|
| 64 | #include "units/particle/interface_fcs.h"
|
|---|
| 65 | #include "units/particle/interface_particles.hpp"
|
|---|
| 66 |
|
|---|
| [48b662] | 67 |
|
|---|
| 68 | using namespace VMG;
|
|---|
| 69 |
|
|---|
| [dfed1c] | 70 | namespace VMGBackupSettings
|
|---|
| [48b662] | 71 | {
|
|---|
| [dfed1c] | 72 | static vmg_int level = -1;
|
|---|
| 73 | static vmg_int periodic[3] = {-1, -1, -1};
|
|---|
| 74 | static vmg_int max_iter = -1;
|
|---|
| 75 | static vmg_int smoothing_steps = -1;
|
|---|
| [728149] | 76 | static vmg_int cycle_type = -1;
|
|---|
| [dfed1c] | 77 | static vmg_float precision = -1;
|
|---|
| 78 | static vmg_float box_offset[3];
|
|---|
| 79 | static vmg_float box_size = -1.0;
|
|---|
| 80 | static vmg_int near_field_cells = -1;
|
|---|
| [a150d0] | 81 | static vmg_int interpolation_degree = -1;
|
|---|
| [4571da] | 82 | static vmg_int discretization_order = -1;
|
|---|
| [dfed1c] | 83 | static MPI_Comm mpi_comm;
|
|---|
| 84 | }
|
|---|
| [48b662] | 85 |
|
|---|
| [dfed1c] | 86 | static void VMG_fcs_init(vmg_int level, vmg_int* periodic,vmg_int max_iter,
|
|---|
| [728149] | 87 | vmg_int smoothing_steps, vmg_int cycle_type, vmg_float precision,
|
|---|
| [dfed1c] | 88 | vmg_float* box_offset, vmg_float box_size,
|
|---|
| [a150d0] | 89 | vmg_int near_field_cells, vmg_int interpolation_degree,
|
|---|
| [4571da] | 90 | vmg_int discretization_order, MPI_Comm mpi_comm)
|
|---|
| [dfed1c] | 91 | {
|
|---|
| 92 | VMGBackupSettings::level = level;
|
|---|
| 93 | std::memcpy(VMGBackupSettings::periodic, periodic, 3*sizeof(vmg_int));
|
|---|
| 94 | VMGBackupSettings::max_iter = max_iter;
|
|---|
| 95 | VMGBackupSettings::smoothing_steps = smoothing_steps;
|
|---|
| [728149] | 96 | VMGBackupSettings::cycle_type = cycle_type;
|
|---|
| [dfed1c] | 97 | VMGBackupSettings::precision = precision;
|
|---|
| 98 | std::memcpy(VMGBackupSettings::box_offset, box_offset, 3*sizeof(vmg_float));
|
|---|
| 99 | VMGBackupSettings::box_size = box_size;
|
|---|
| 100 | VMGBackupSettings::near_field_cells = near_field_cells;
|
|---|
| [a150d0] | 101 | VMGBackupSettings::interpolation_degree = interpolation_degree;
|
|---|
| [4571da] | 102 | VMGBackupSettings::discretization_order = discretization_order;
|
|---|
| [dfed1c] | 103 | VMGBackupSettings::mpi_comm = mpi_comm;
|
|---|
| 104 |
|
|---|
| [379700] | 105 | #ifdef DEBUG
|
|---|
| [ac6d04] | 106 | MPI_Errhandler mpiErrorHandler;
|
|---|
| 107 | MPI_Comm_create_errhandler(VMG::MPI::ConvertToException, &mpiErrorHandler);
|
|---|
| 108 | MPI_Comm_set_errhandler(MPI_COMM_WORLD, mpiErrorHandler);
|
|---|
| [379700] | 109 | #endif
|
|---|
| [ac6d04] | 110 |
|
|---|
| [dfed1c] | 111 | const Boundary boundary(periodic[0] ? Periodic : Open,
|
|---|
| 112 | periodic[1] ? Periodic : Open,
|
|---|
| 113 | periodic[2] ? Periodic : Open);
|
|---|
| 114 |
|
|---|
| 115 | const bool singular = periodic[0] * periodic[1] * periodic[2];
|
|---|
| [48b662] | 116 |
|
|---|
| 117 | /*
|
|---|
| 118 | * Register communication class.
|
|---|
| 119 | */
|
|---|
| [f003a9] | 120 | Comm* comm = new Particle::CommMPI(boundary, new DomainDecompositionMPI());
|
|---|
| [48b662] | 121 | comm->Register("COMM");
|
|---|
| 122 |
|
|---|
| 123 | /*
|
|---|
| 124 | * Register particle interface.
|
|---|
| 125 | */
|
|---|
| [ac6d04] | 126 | Interface* interface;
|
|---|
| 127 | if (singular)
|
|---|
| 128 | interface = new InterfaceParticles(boundary, 2, level, Vector(box_offset), box_size, near_field_cells, 0, 1.0);
|
|---|
| 129 | else
|
|---|
| 130 | interface = new InterfaceParticles(boundary, 2, level, Vector(box_offset), box_size, near_field_cells, 2, 1.6);
|
|---|
| [48b662] | 131 | MG::SetInterface(interface, comm);
|
|---|
| 132 |
|
|---|
| 133 | /*
|
|---|
| [ac6d04] | 134 | * Define discretization of the Poisson equation.
|
|---|
| 135 | * Finite volumes for locally refined grids and
|
|---|
| 136 | * finite differences otherwise.
|
|---|
| [48b662] | 137 | */
|
|---|
| [ac6d04] | 138 | Discretization* discretization;
|
|---|
| 139 | if (singular)
|
|---|
| [4571da] | 140 | discretization = new DiscretizationPoissonFD(discretization_order);
|
|---|
| [ac6d04] | 141 | else
|
|---|
| [4571da] | 142 | discretization = new DiscretizationPoissonFV(discretization_order);
|
|---|
| [48b662] | 143 | discretization->Register("DISCRETIZATION");
|
|---|
| 144 |
|
|---|
| 145 | /*
|
|---|
| 146 | * Use a correction scheme multigrid algorithm with full weight stencils.
|
|---|
| 147 | * Since we use the Gauss-Seidel RB smoother here, this may be replaced
|
|---|
| 148 | * with a half-weight version once debugging is finished.
|
|---|
| 149 | */
|
|---|
| [ac6d04] | 150 | LevelOperator* lop;
|
|---|
| 151 | if (singular)
|
|---|
| 152 | lop = new LevelOperatorCS(Stencils::RestrictionFullWeight, Stencils::InterpolationTrilinear);
|
|---|
| 153 | else
|
|---|
| 154 | lop = new LevelOperatorFAS(Stencils::RestrictionFullWeight, Stencils::Injection, Stencils::InterpolationTrilinear);
|
|---|
| [48b662] | 155 | lop->Register("LEVEL_OPERATOR");
|
|---|
| 156 |
|
|---|
| 157 | /*
|
|---|
| 158 | * Use Gauss-Seidel Red-Black ordering
|
|---|
| 159 | */
|
|---|
| 160 | Smoother* smoother = new GaussSeidelRB();
|
|---|
| 161 | smoother->Register("SMOOTHER");
|
|---|
| 162 |
|
|---|
| 163 | /*
|
|---|
| 164 | * Use LAPACK solver when available, otherwise use Givens rotations.
|
|---|
| 165 | */
|
|---|
| 166 | #ifdef HAVE_LAPACK
|
|---|
| [dfed1c] | 167 | Solver* solver = (singular ?
|
|---|
| 168 | static_cast<Solver*>(new DSYSV<SolverSingular>()) :
|
|---|
| 169 | static_cast<Solver*>(new DGESV<SolverRegular>()));
|
|---|
| [48b662] | 170 | #else
|
|---|
| [dfed1c] | 171 | Solver* solver = (singular ?
|
|---|
| 172 | static_cast<Solver*>(new Givens<SolverSingular>()) :
|
|---|
| 173 | static_cast<Solver*>(new Givens<SolverRegular>()));
|
|---|
| [48b662] | 174 | #endif
|
|---|
| 175 | solver->Register("SOLVER");
|
|---|
| 176 |
|
|---|
| 177 | /*
|
|---|
| 178 | * Set commands for the actual multigrid cycle
|
|---|
| 179 | */
|
|---|
| [dfed1c] | 180 | if (singular)
|
|---|
| [728149] | 181 | Techniques::SetCorrectionSchemePeriodic(interface->MinLevel(), interface->MaxLevel(), cycle_type);
|
|---|
| [48b662] | 182 | else
|
|---|
| [728149] | 183 | Techniques::SetFullApproximationSchemeDirichlet(interface->MinLevel(), interface->MaxLevel(), cycle_type);
|
|---|
| [48b662] | 184 |
|
|---|
| 185 | /*
|
|---|
| 186 | * Register required parameters
|
|---|
| 187 | */
|
|---|
| 188 | new ObjectStorage<int>("PRESMOOTHSTEPS", smoothing_steps);
|
|---|
| 189 | new ObjectStorage<int>("POSTSMOOTHSTEPS", smoothing_steps);
|
|---|
| 190 | new ObjectStorage<vmg_float>("PRECISION", precision);
|
|---|
| 191 | new ObjectStorage<int>("MAX_ITERATION", max_iter);
|
|---|
| [dfed1c] | 192 | new ObjectStorage<int>("PARTICLE_NEAR_FIELD_CELLS", near_field_cells);
|
|---|
| [a150d0] | 193 | new ObjectStorage<int>("PARTICLE_INTERPOLATION_DEGREE", interpolation_degree);
|
|---|
| [dfed1c] | 194 |
|
|---|
| [48b662] | 195 | /*
|
|---|
| 196 | * Check whether the library is correctly initialized now.
|
|---|
| 197 | */
|
|---|
| 198 | MG::IsInitialized();
|
|---|
| [894a5f] | 199 |
|
|---|
| 200 | /*
|
|---|
| 201 | * Post init communication class
|
|---|
| 202 | */
|
|---|
| 203 | MG::PostInit();
|
|---|
| [48b662] | 204 | }
|
|---|
| 205 |
|
|---|
| [dfed1c] | 206 | void VMG_fcs_setup(vmg_int level, vmg_int* periodic, vmg_int max_iter,
|
|---|
| [728149] | 207 | vmg_int smoothing_steps, vmg_int cycle_type, vmg_float precision,
|
|---|
| [dfed1c] | 208 | vmg_float* box_offset, vmg_float box_size,
|
|---|
| [a150d0] | 209 | vmg_int near_field_cells, vmg_int interpolation_degree,
|
|---|
| [4571da] | 210 | vmg_int discretization_order, MPI_Comm mpi_comm)
|
|---|
| [dfed1c] | 211 | {
|
|---|
| 212 | if (VMGBackupSettings::level != level ||
|
|---|
| 213 | VMGBackupSettings::periodic[0] != periodic[0] ||
|
|---|
| 214 | VMGBackupSettings::periodic[1] != periodic[1] ||
|
|---|
| 215 | VMGBackupSettings::periodic[2] != periodic[2] ||
|
|---|
| 216 | VMGBackupSettings::max_iter != max_iter ||
|
|---|
| 217 | VMGBackupSettings::smoothing_steps != smoothing_steps ||
|
|---|
| [728149] | 218 | VMGBackupSettings::cycle_type != cycle_type ||
|
|---|
| [dfed1c] | 219 | VMGBackupSettings::precision != precision ||
|
|---|
| 220 | VMGBackupSettings::box_offset[0] != box_offset[0] ||
|
|---|
| 221 | VMGBackupSettings::box_offset[1] != box_offset[1] ||
|
|---|
| 222 | VMGBackupSettings::box_offset[2] != box_offset[2] ||
|
|---|
| 223 | VMGBackupSettings::box_size != box_size ||
|
|---|
| 224 | VMGBackupSettings::near_field_cells != near_field_cells ||
|
|---|
| [a150d0] | 225 | VMGBackupSettings::interpolation_degree != interpolation_degree ||
|
|---|
| [4571da] | 226 | VMGBackupSettings::discretization_order != discretization_order ||
|
|---|
| [dfed1c] | 227 | VMGBackupSettings::mpi_comm != mpi_comm) {
|
|---|
| 228 |
|
|---|
| 229 | VMG_fcs_destroy();
|
|---|
| 230 | VMG_fcs_init(level, periodic, max_iter,
|
|---|
| [728149] | 231 | smoothing_steps, cycle_type, precision,
|
|---|
| [dfed1c] | 232 | box_offset, box_size, near_field_cells,
|
|---|
| [4571da] | 233 | interpolation_degree, discretization_order,
|
|---|
| 234 | mpi_comm);
|
|---|
| [dfed1c] | 235 |
|
|---|
| 236 | }
|
|---|
| 237 | }
|
|---|
| 238 |
|
|---|
| [ac6d04] | 239 | int VMG_fcs_check()
|
|---|
| 240 | {
|
|---|
| 241 | const int& near_field_cells = MG::GetFactory().GetObjectStorageVal<int>("PARTICLE_NEAR_FIELD_CELLS");
|
|---|
| 242 | const Multigrid& multigrid = *MG::GetRhs();
|
|---|
| 243 | const Grid& grid = multigrid(multigrid.MaxLevel());
|
|---|
| 244 |
|
|---|
| [7dd744] | 245 | int error_code;
|
|---|
| 246 |
|
|---|
| [ac6d04] | 247 | if (!grid.Global().LocalSize().IsComponentwiseGreater(near_field_cells))
|
|---|
| [7dd744] | 248 | error_code = 1;
|
|---|
| 249 | else
|
|---|
| 250 | error_code = 0;
|
|---|
| 251 |
|
|---|
| 252 | error_code = MG::GetComm()->GlobalMax(error_code);
|
|---|
| [ac6d04] | 253 |
|
|---|
| [7dd744] | 254 | return error_code;
|
|---|
| [ac6d04] | 255 | }
|
|---|
| 256 |
|
|---|
| [dfed1c] | 257 | void VMG_fcs_run(vmg_float* x, vmg_float* q, vmg_float* p, vmg_float* f, vmg_int num_particles_local)
|
|---|
| [48b662] | 258 | {
|
|---|
| 259 | /*
|
|---|
| 260 | * Register parameters for later use.
|
|---|
| 261 | */
|
|---|
| [dfed1c] | 262 | new ObjectStorage<vmg_float*>("PARTICLE_POS_ARRAY", x);
|
|---|
| 263 | new ObjectStorage<vmg_float*>("PARTICLE_CHARGE_ARRAY", q);
|
|---|
| 264 | new ObjectStorage<vmg_float*>("PARTICLE_POTENTIAL_ARRAY", p);
|
|---|
| [716da7] | 265 | new ObjectStorage<vmg_float*>("PARTICLE_FIELD_ARRAY", f);
|
|---|
| [dfed1c] | 266 | new ObjectStorage<vmg_int>("PARTICLE_NUM_LOCAL", num_particles_local);
|
|---|
| [48b662] | 267 |
|
|---|
| 268 | /*
|
|---|
| 269 | * Start the multigrid solver
|
|---|
| 270 | */
|
|---|
| 271 | MG::Solve();
|
|---|
| [894a5f] | 272 |
|
|---|
| 273 | #ifdef DEBUG_MEASURE_TIME
|
|---|
| 274 | Timer::Print();
|
|---|
| 275 | #endif
|
|---|
| [48b662] | 276 | }
|
|---|
| 277 |
|
|---|
| [dfed1c] | 278 | void VMG_fcs_print_timer()
|
|---|
| 279 | {
|
|---|
| 280 | Timer::Print();
|
|---|
| 281 | }
|
|---|
| 282 |
|
|---|
| 283 | void VMG_fcs_destroy(void)
|
|---|
| [48b662] | 284 | {
|
|---|
| 285 | /*
|
|---|
| 286 | * Delete all data.
|
|---|
| 287 | */
|
|---|
| 288 | MG::Destroy();
|
|---|
| 289 | }
|
|---|