| [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"
|
|---|
| [952298] | 54 | #include "smoother/gsrb_poisson_2.hpp"
|
|---|
| 55 | #include "smoother/gsrb_poisson_4.hpp"
|
|---|
| [48b662] | 56 | #include "solver/givens.hpp"
|
|---|
| [dfed1c] | 57 | #include "solver/solver_regular.hpp"
|
|---|
| 58 | #include "solver/solver_singular.hpp"
|
|---|
| [f003a9] | 59 | #include "units/particle/comm_mpi_particle.hpp"
|
|---|
| 60 | #include "units/particle/interface_fcs.h"
|
|---|
| 61 | #include "units/particle/interface_particles.hpp"
|
|---|
| 62 |
|
|---|
| [48b662] | 63 |
|
|---|
| 64 | using namespace VMG;
|
|---|
| 65 |
|
|---|
| [dfed1c] | 66 | namespace VMGBackupSettings
|
|---|
| [48b662] | 67 | {
|
|---|
| [dfed1c] | 68 | static vmg_int level = -1;
|
|---|
| 69 | static vmg_int periodic[3] = {-1, -1, -1};
|
|---|
| 70 | static vmg_int max_iter = -1;
|
|---|
| 71 | static vmg_int smoothing_steps = -1;
|
|---|
| [728149] | 72 | static vmg_int cycle_type = -1;
|
|---|
| [dfed1c] | 73 | static vmg_float precision = -1;
|
|---|
| 74 | static vmg_float box_offset[3];
|
|---|
| 75 | static vmg_float box_size = -1.0;
|
|---|
| 76 | static vmg_int near_field_cells = -1;
|
|---|
| [a150d0] | 77 | static vmg_int interpolation_degree = -1;
|
|---|
| [4571da] | 78 | static vmg_int discretization_order = -1;
|
|---|
| [dfed1c] | 79 | static MPI_Comm mpi_comm;
|
|---|
| 80 | }
|
|---|
| [48b662] | 81 |
|
|---|
| [dfed1c] | 82 | static void VMG_fcs_init(vmg_int level, vmg_int* periodic,vmg_int max_iter,
|
|---|
| [728149] | 83 | vmg_int smoothing_steps, vmg_int cycle_type, vmg_float precision,
|
|---|
| [dfed1c] | 84 | vmg_float* box_offset, vmg_float box_size,
|
|---|
| [a150d0] | 85 | vmg_int near_field_cells, vmg_int interpolation_degree,
|
|---|
| [4571da] | 86 | vmg_int discretization_order, MPI_Comm mpi_comm)
|
|---|
| [dfed1c] | 87 | {
|
|---|
| 88 | VMGBackupSettings::level = level;
|
|---|
| 89 | std::memcpy(VMGBackupSettings::periodic, periodic, 3*sizeof(vmg_int));
|
|---|
| 90 | VMGBackupSettings::max_iter = max_iter;
|
|---|
| 91 | VMGBackupSettings::smoothing_steps = smoothing_steps;
|
|---|
| [728149] | 92 | VMGBackupSettings::cycle_type = cycle_type;
|
|---|
| [dfed1c] | 93 | VMGBackupSettings::precision = precision;
|
|---|
| 94 | std::memcpy(VMGBackupSettings::box_offset, box_offset, 3*sizeof(vmg_float));
|
|---|
| 95 | VMGBackupSettings::box_size = box_size;
|
|---|
| 96 | VMGBackupSettings::near_field_cells = near_field_cells;
|
|---|
| [a150d0] | 97 | VMGBackupSettings::interpolation_degree = interpolation_degree;
|
|---|
| [4571da] | 98 | VMGBackupSettings::discretization_order = discretization_order;
|
|---|
| [dfed1c] | 99 | VMGBackupSettings::mpi_comm = mpi_comm;
|
|---|
| 100 |
|
|---|
| [379700] | 101 | #ifdef DEBUG
|
|---|
| [ac6d04] | 102 | MPI_Errhandler mpiErrorHandler;
|
|---|
| 103 | MPI_Comm_create_errhandler(VMG::MPI::ConvertToException, &mpiErrorHandler);
|
|---|
| [2a5451] | 104 | MPI_Comm_set_errhandler(mpi_comm, mpiErrorHandler);
|
|---|
| [379700] | 105 | #endif
|
|---|
| [ac6d04] | 106 |
|
|---|
| [dfed1c] | 107 | const Boundary boundary(periodic[0] ? Periodic : Open,
|
|---|
| 108 | periodic[1] ? Periodic : Open,
|
|---|
| 109 | periodic[2] ? Periodic : Open);
|
|---|
| 110 |
|
|---|
| 111 | const bool singular = periodic[0] * periodic[1] * periodic[2];
|
|---|
| [48b662] | 112 |
|
|---|
| [a24b80] | 113 | Comm* comm;
|
|---|
| 114 | Discretization* discretization;
|
|---|
| [ac6d04] | 115 | Interface* interface;
|
|---|
| [a24b80] | 116 | LevelOperator* lop;
|
|---|
| 117 | Smoother* smoother;
|
|---|
| 118 | Solver* solver;
|
|---|
| [48b662] | 119 |
|
|---|
| 120 | /*
|
|---|
| [a24b80] | 121 | * Choose multigrid components
|
|---|
| [48b662] | 122 | */
|
|---|
| [a24b80] | 123 | if (singular) {
|
|---|
| [48b662] | 124 |
|
|---|
| [a24b80] | 125 | comm = new Particle::CommMPI(boundary, new DomainDecompositionMPI(), mpi_comm);
|
|---|
| 126 | discretization = new DiscretizationPoissonFD(discretization_order);
|
|---|
| 127 | interface = new InterfaceParticles(boundary, 2, level, Vector(box_offset), box_size, near_field_cells, 0, 1.0);
|
|---|
| [ac6d04] | 128 | lop = new LevelOperatorCS(Stencils::RestrictionFullWeight, Stencils::InterpolationTrilinear);
|
|---|
| [a24b80] | 129 | solver = new Givens<SolverSingular>();
|
|---|
| 130 |
|
|---|
| 131 | Techniques::SetCorrectionSchemePeriodic(interface->MinLevel(), interface->MaxLevel(), cycle_type);
|
|---|
| [48b662] | 132 |
|
|---|
| [952298] | 133 | }else {
|
|---|
| [a24b80] | 134 |
|
|---|
| 135 | comm = new Particle::CommMPI(boundary, new DomainDecompositionMPI(), mpi_comm);
|
|---|
| 136 | discretization = new DiscretizationPoissonFV(discretization_order);
|
|---|
| 137 | interface = new InterfaceParticles(boundary, 2, level, Vector(box_offset), box_size, near_field_cells, 2, 1.6);
|
|---|
| 138 | lop = new LevelOperatorFAS(Stencils::RestrictionFullWeight, Stencils::Injection, Stencils::InterpolationTrilinear);
|
|---|
| 139 | solver = new Givens<SolverRegular>();
|
|---|
| 140 |
|
|---|
| 141 | Techniques::SetFullApproximationSchemeDirichlet(interface->MinLevel(), interface->MaxLevel(), cycle_type);
|
|---|
| 142 |
|
|---|
| [952298] | 143 | }
|
|---|
| [48b662] | 144 |
|
|---|
| 145 | /*
|
|---|
| [a24b80] | 146 | * Use Gauss-Seidel Red-Black ordering
|
|---|
| [48b662] | 147 | */
|
|---|
| [a24b80] | 148 | if (discretization_order == 2)
|
|---|
| 149 | smoother = new GaussSeidelRBPoisson2();
|
|---|
| 150 | else
|
|---|
| 151 | smoother = new GaussSeidelRBPoisson4();
|
|---|
| [48b662] | 152 |
|
|---|
| 153 | /*
|
|---|
| [a24b80] | 154 | * Register multigrid components
|
|---|
| [48b662] | 155 | */
|
|---|
| [a24b80] | 156 | comm->Register("COMM");
|
|---|
| 157 | discretization->Register("DISCRETIZATION");
|
|---|
| 158 | interface->Register("INTERFACE");
|
|---|
| 159 | lop->Register("LEVEL_OPERATOR");
|
|---|
| 160 | smoother->Register("SMOOTHER");
|
|---|
| 161 | solver->Register("SOLVER");
|
|---|
| [48b662] | 162 |
|
|---|
| 163 | /*
|
|---|
| 164 | * Register required parameters
|
|---|
| 165 | */
|
|---|
| 166 | new ObjectStorage<int>("PRESMOOTHSTEPS", smoothing_steps);
|
|---|
| 167 | new ObjectStorage<int>("POSTSMOOTHSTEPS", smoothing_steps);
|
|---|
| 168 | new ObjectStorage<vmg_float>("PRECISION", precision);
|
|---|
| 169 | new ObjectStorage<int>("MAX_ITERATION", max_iter);
|
|---|
| [dfed1c] | 170 | new ObjectStorage<int>("PARTICLE_NEAR_FIELD_CELLS", near_field_cells);
|
|---|
| [a150d0] | 171 | new ObjectStorage<int>("PARTICLE_INTERPOLATION_DEGREE", interpolation_degree);
|
|---|
| [dfed1c] | 172 |
|
|---|
| [48b662] | 173 | /*
|
|---|
| [a24b80] | 174 | * Post init
|
|---|
| [48b662] | 175 | */
|
|---|
| [a24b80] | 176 | MG::PostInit();
|
|---|
| [894a5f] | 177 |
|
|---|
| 178 | /*
|
|---|
| [a24b80] | 179 | * Check whether the library is correctly initialized now.
|
|---|
| [894a5f] | 180 | */
|
|---|
| [a24b80] | 181 | MG::IsInitialized();
|
|---|
| [48b662] | 182 | }
|
|---|
| 183 |
|
|---|
| [dfed1c] | 184 | void VMG_fcs_setup(vmg_int level, vmg_int* periodic, vmg_int max_iter,
|
|---|
| [728149] | 185 | vmg_int smoothing_steps, vmg_int cycle_type, vmg_float precision,
|
|---|
| [dfed1c] | 186 | vmg_float* box_offset, vmg_float box_size,
|
|---|
| [a150d0] | 187 | vmg_int near_field_cells, vmg_int interpolation_degree,
|
|---|
| [4571da] | 188 | vmg_int discretization_order, MPI_Comm mpi_comm)
|
|---|
| [dfed1c] | 189 | {
|
|---|
| 190 | if (VMGBackupSettings::level != level ||
|
|---|
| 191 | VMGBackupSettings::periodic[0] != periodic[0] ||
|
|---|
| 192 | VMGBackupSettings::periodic[1] != periodic[1] ||
|
|---|
| 193 | VMGBackupSettings::periodic[2] != periodic[2] ||
|
|---|
| 194 | VMGBackupSettings::max_iter != max_iter ||
|
|---|
| 195 | VMGBackupSettings::smoothing_steps != smoothing_steps ||
|
|---|
| [728149] | 196 | VMGBackupSettings::cycle_type != cycle_type ||
|
|---|
| [dfed1c] | 197 | VMGBackupSettings::precision != precision ||
|
|---|
| 198 | VMGBackupSettings::box_offset[0] != box_offset[0] ||
|
|---|
| 199 | VMGBackupSettings::box_offset[1] != box_offset[1] ||
|
|---|
| 200 | VMGBackupSettings::box_offset[2] != box_offset[2] ||
|
|---|
| 201 | VMGBackupSettings::box_size != box_size ||
|
|---|
| 202 | VMGBackupSettings::near_field_cells != near_field_cells ||
|
|---|
| [a150d0] | 203 | VMGBackupSettings::interpolation_degree != interpolation_degree ||
|
|---|
| [4571da] | 204 | VMGBackupSettings::discretization_order != discretization_order ||
|
|---|
| [dfed1c] | 205 | VMGBackupSettings::mpi_comm != mpi_comm) {
|
|---|
| 206 |
|
|---|
| 207 | VMG_fcs_destroy();
|
|---|
| 208 | VMG_fcs_init(level, periodic, max_iter,
|
|---|
| [728149] | 209 | smoothing_steps, cycle_type, precision,
|
|---|
| [dfed1c] | 210 | box_offset, box_size, near_field_cells,
|
|---|
| [4571da] | 211 | interpolation_degree, discretization_order,
|
|---|
| 212 | mpi_comm);
|
|---|
| [dfed1c] | 213 |
|
|---|
| 214 | }
|
|---|
| 215 | }
|
|---|
| 216 |
|
|---|
| [ac6d04] | 217 | int VMG_fcs_check()
|
|---|
| 218 | {
|
|---|
| 219 | const int& near_field_cells = MG::GetFactory().GetObjectStorageVal<int>("PARTICLE_NEAR_FIELD_CELLS");
|
|---|
| 220 | const Multigrid& multigrid = *MG::GetRhs();
|
|---|
| 221 | const Grid& grid = multigrid(multigrid.MaxLevel());
|
|---|
| 222 |
|
|---|
| [7dd744] | 223 | int error_code;
|
|---|
| 224 |
|
|---|
| [ac6d04] | 225 | if (!grid.Global().LocalSize().IsComponentwiseGreater(near_field_cells))
|
|---|
| [7dd744] | 226 | error_code = 1;
|
|---|
| 227 | else
|
|---|
| 228 | error_code = 0;
|
|---|
| 229 |
|
|---|
| 230 | error_code = MG::GetComm()->GlobalMax(error_code);
|
|---|
| [ac6d04] | 231 |
|
|---|
| [7dd744] | 232 | return error_code;
|
|---|
| [ac6d04] | 233 | }
|
|---|
| 234 |
|
|---|
| [dfed1c] | 235 | void VMG_fcs_run(vmg_float* x, vmg_float* q, vmg_float* p, vmg_float* f, vmg_int num_particles_local)
|
|---|
| [48b662] | 236 | {
|
|---|
| 237 | /*
|
|---|
| 238 | * Register parameters for later use.
|
|---|
| 239 | */
|
|---|
| [dfed1c] | 240 | new ObjectStorage<vmg_float*>("PARTICLE_POS_ARRAY", x);
|
|---|
| 241 | new ObjectStorage<vmg_float*>("PARTICLE_CHARGE_ARRAY", q);
|
|---|
| 242 | new ObjectStorage<vmg_float*>("PARTICLE_POTENTIAL_ARRAY", p);
|
|---|
| [716da7] | 243 | new ObjectStorage<vmg_float*>("PARTICLE_FIELD_ARRAY", f);
|
|---|
| [dfed1c] | 244 | new ObjectStorage<vmg_int>("PARTICLE_NUM_LOCAL", num_particles_local);
|
|---|
| [48b662] | 245 |
|
|---|
| 246 | /*
|
|---|
| 247 | * Start the multigrid solver
|
|---|
| 248 | */
|
|---|
| 249 | MG::Solve();
|
|---|
| [894a5f] | 250 |
|
|---|
| 251 | #ifdef DEBUG_MEASURE_TIME
|
|---|
| 252 | Timer::Print();
|
|---|
| 253 | #endif
|
|---|
| [48b662] | 254 | }
|
|---|
| 255 |
|
|---|
| [dfed1c] | 256 | void VMG_fcs_print_timer()
|
|---|
| 257 | {
|
|---|
| 258 | Timer::Print();
|
|---|
| 259 | }
|
|---|
| 260 |
|
|---|
| 261 | void VMG_fcs_destroy(void)
|
|---|
| [48b662] | 262 | {
|
|---|
| 263 | /*
|
|---|
| 264 | * Delete all data.
|
|---|
| 265 | */
|
|---|
| 266 | MG::Destroy();
|
|---|
| 267 | }
|
|---|