| 1 |
// ------------------------------------------------------------------------ |
| 2 |
// |
| 3 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
// Copyright (C) 1999 - 2025 by the deal.II authors |
| 5 |
// |
| 6 |
// This file is part of the deal.II library. |
| 7 |
// |
| 8 |
// Part of the source code is dual licensed under Apache-2.0 WITH |
| 9 |
// LLVM-exception OR LGPL-2.1-or-later. Detailed license information |
| 10 |
// governing the source code and code contributions can be found in |
| 11 |
// LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II. |
| 12 |
// |
| 13 |
// ------------------------------------------------------------------------ |
| 14 |
|
| 15 |
#ifndef dealii_multigrid_templates_h |
| 16 |
#define dealii_multigrid_templates_h |
| 17 |
|
| 18 |
|
| 19 |
#include <deal.II/base/config.h> |
| 20 |
|
| 21 |
#include <deal.II/multigrid/multigrid.h> |
| 22 |
|
| 23 |
#include <boost/signals2.hpp> |
| 24 |
|
| 25 |
#include <iostream> |
| 26 |
|
| 27 |
|
| 28 |
DEAL_II_NAMESPACE_OPEN |
| 29 |
|
| 30 |
template <typename VectorType> |
| 31 |
void |
| 32 |
Multigrid<VectorType>::reinit(const unsigned int min_level, |
| 33 |
const unsigned int max_level) |
| 34 |
{ |
| 35 |
Assert(min_level >= matrix->get_minlevel(), |
| 36 |
ExcLowerRangeType<unsigned int>(min_level, matrix->get_minlevel())); |
| 37 |
Assert(max_level <= matrix->get_maxlevel(), |
| 38 |
ExcLowerRangeType<unsigned int>(matrix->get_maxlevel(), max_level)); |
| 39 |
Assert(min_level <= max_level, |
| 40 |
ExcLowerRangeType<unsigned int>(max_level, min_level)); |
| 41 |
minlevel = min_level; |
| 42 |
maxlevel = max_level; |
| 43 |
// solution, t and defect2 are resized in cycle() |
| 44 |
defect.resize(minlevel, maxlevel); |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
template <typename VectorType> |
| 50 |
void |
| 51 |
Multigrid<VectorType>::set_maxlevel(const unsigned int l) |
| 52 |
{ |
| 53 |
reinit(minlevel, l); |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
template <typename VectorType> |
| 59 |
void |
| 60 |
Multigrid<VectorType>::set_minlevel(const unsigned int l, const bool relative) |
| 61 |
{ |
| 62 |
const unsigned int new_minlevel = (relative) ? (maxlevel - l) : l; |
| 63 |
reinit(new_minlevel, maxlevel); |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
template <typename VectorType> |
| 69 |
void |
| 70 |
Multigrid<VectorType>::set_cycle(typename Multigrid<VectorType>::Cycle c) |
| 71 |
{ |
| 72 |
cycle_type = c; |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
template <typename VectorType> |
| 78 |
void |
| 79 |
Multigrid<VectorType>::set_edge_matrices( |
| 80 |
const MGMatrixBase<VectorType> &edge_out, |
| 81 |
const MGMatrixBase<VectorType> &edge_in) |
| 82 |
{ |
| 83 |
this->edge_out = &edge_out; |
| 84 |
this->edge_in = &edge_in; |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
template <typename VectorType> |
| 90 |
void |
| 91 |
Multigrid<VectorType>::set_edge_in_matrix( |
| 92 |
const MGMatrixBase<VectorType> &edge_in) |
| 93 |
{ |
| 94 |
this->edge_out = nullptr; |
| 95 |
this->edge_in = &edge_in; |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
template <typename VectorType> |
| 101 |
void |
| 102 |
Multigrid<VectorType>::set_edge_flux_matrices( |
| 103 |
const MGMatrixBase<VectorType> &edge_down, |
| 104 |
const MGMatrixBase<VectorType> &edge_up) |
| 105 |
{ |
| 106 |
this->edge_down = &edge_down; |
| 107 |
this->edge_up = &edge_up; |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
template <typename VectorType> |
| 113 |
void |
| 114 |
Multigrid<VectorType>::level_v_step(const unsigned int level) |
| 115 |
{ |
| 116 |
if (level == minlevel) |
| 117 |
{ |
| 118 |
this->signals.coarse_solve(true, level); |
| 119 |
(*coarse)(level, solution[level], defect[level]); |
| 120 |
this->signals.coarse_solve(false, level); |
| 121 |
return; |
| 122 |
} |
| 123 |
|
| 124 |
// smoothing of the residual |
| 125 |
this->signals.pre_smoother_step(true, level); |
| 126 |
pre_smooth->apply(level, solution[level], defect[level]); |
| 127 |
this->signals.pre_smoother_step(false, level); |
| 128 |
|
| 129 |
// compute residual on level, which includes the (CG) edge matrix |
| 130 |
this->signals.residual_step(true, level); |
| 131 |
matrix->vmult(level, t[level], solution[level]); |
| 132 |
if (edge_out != nullptr) |
| 133 |
{ |
| 134 |
edge_out->vmult_add(level, t[level], solution[level]); |
| 135 |
} |
| 136 |
t[level].sadd(-1.0, 1.0, defect[level]); |
| 137 |
|
| 138 |
// Get the defect on the next coarser level as part of the (DG) edge matrix |
| 139 |
// and then the main part by the restriction of the transfer |
| 140 |
if (edge_down != nullptr) |
| 141 |
{ |
| 142 |
edge_down->vmult(level, t[level - 1], solution[level]); |
| 143 |
defect[level - 1] -= t[level - 1]; |
| 144 |
} |
| 145 |
this->signals.residual_step(false, level); |
| 146 |
|
| 147 |
this->signals.restriction(true, level); |
| 148 |
transfer->restrict_and_add(level, defect[level - 1], t[level]); |
| 149 |
this->signals.restriction(false, level); |
| 150 |
|
| 151 |
// do recursion |
| 152 |
level_v_step(level - 1); |
| 153 |
|
| 154 |
// do coarse grid correction |
| 155 |
this->signals.prolongation(true, level); |
| 156 |
transfer->prolongate_and_add(level, solution[level], solution[level - 1]); |
| 157 |
this->signals.prolongation(false, level); |
| 158 |
|
| 159 |
this->signals.edge_prolongation(true, level); |
| 160 |
// get in contribution from edge matrices to the defect |
| 161 |
if (edge_in != nullptr) |
| 162 |
{ |
| 163 |
edge_in->Tvmult(level, t[level], solution[level]); |
| 164 |
defect[level] -= t[level]; |
| 165 |
} |
| 166 |
if (edge_up != nullptr) |
| 167 |
{ |
| 168 |
edge_up->Tvmult(level, t[level], solution[level - 1]); |
| 169 |
defect[level] -= t[level]; |
| 170 |
} |
| 171 |
this->signals.edge_prolongation(false, level); |
| 172 |
|
| 173 |
// post-smoothing |
| 174 |
this->signals.post_smoother_step(true, level); |
| 175 |
post_smooth->smooth(level, solution[level], defect[level]); |
| 176 |
this->signals.post_smoother_step(false, level); |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
template <typename VectorType> |
| 182 |
void |
| 183 |
Multigrid<VectorType>::level_step(const unsigned int level, Cycle cycle) |
| 184 |
{ |
| 185 |
// Combine the defect from the initial copy_to_mg with the one that has come |
| 186 |
// from the finer level by the transfer |
| 187 |
defect2[level] += defect[level]; |
| 188 |
defect[level] = typename VectorType::value_type(0.); |
| 189 |
|
| 190 |
if (level == minlevel) |
| 191 |
{ |
| 192 |
this->signals.coarse_solve(true, level); |
| 193 |
(*coarse)(level, solution[level], defect2[level]); |
| 194 |
this->signals.coarse_solve(false, level); |
| 195 |
return; |
| 196 |
} |
| 197 |
|
| 198 |
// smoothing of the residual |
| 199 |
this->signals.pre_smoother_step(true, level); |
| 200 |
pre_smooth->apply(level, solution[level], defect2[level]); |
| 201 |
this->signals.pre_smoother_step(false, level); |
| 202 |
|
| 203 |
// compute residual on level, which includes the (CG) edge matrix |
| 204 |
this->signals.residual_step(true, level); |
| 205 |
matrix->vmult(level, t[level], solution[level]); |
| 206 |
if (edge_out != nullptr) |
| 207 |
edge_out->vmult_add(level, t[level], solution[level]); |
| 208 |
t[level].sadd(-1.0, 1.0, defect2[level]); |
| 209 |
|
| 210 |
// Get the defect on the next coarser level as part of the (DG) edge matrix |
| 211 |
// and then the main part by the restriction of the transfer |
| 212 |
if (edge_down != nullptr) |
| 213 |
edge_down->vmult(level, defect2[level - 1], solution[level]); |
| 214 |
else |
| 215 |
defect2[level - 1] = typename VectorType::value_type(0.); |
| 216 |
this->signals.residual_step(false, level); |
| 217 |
|
| 218 |
this->signals.restriction(true, level); |
| 219 |
transfer->restrict_and_add(level, defect2[level - 1], t[level]); |
| 220 |
this->signals.restriction(false, level); |
| 221 |
|
| 222 |
// Every cycle starts with a recursion of its type. |
| 223 |
level_step(level - 1, cycle); |
| 224 |
|
| 225 |
// For W and F-cycle, repeat the process on the next coarser level except |
| 226 |
// for the coarse solver which we invoke just once |
| 227 |
if (level > minlevel + 1) |
| 228 |
{ |
| 229 |
// while the W-cycle repeats itself, ... |
| 230 |
if (cycle == w_cycle) |
| 231 |
level_step(level - 1, cycle); |
| 232 |
// ... the F-cycle does a V-cycle after an F-cycle, ... |
| 233 |
else if (cycle == f_cycle) |
| 234 |
level_step(level - 1, v_cycle); |
| 235 |
// ... and the V-cycle does nothing. |
| 236 |
} |
| 237 |
|
| 238 |
// do coarse grid correction |
| 239 |
this->signals.prolongation(true, level); |
| 240 |
transfer->prolongate(level, t[level], solution[level - 1]); |
| 241 |
this->signals.prolongation(false, level); |
| 242 |
|
| 243 |
this->signals.edge_prolongation(true, level); |
| 244 |
solution[level] += t[level]; |
| 245 |
|
| 246 |
// get in contribution from edge matrices to the defect |
| 247 |
if (edge_in != nullptr) |
| 248 |
{ |
| 249 |
edge_in->Tvmult(level, t[level], solution[level]); |
| 250 |
defect2[level] -= t[level]; |
| 251 |
} |
| 252 |
|
| 253 |
if (edge_up != nullptr) |
| 254 |
{ |
| 255 |
edge_up->Tvmult(level, t[level], solution[level - 1]); |
| 256 |
defect2[level] -= t[level]; |
| 257 |
} |
| 258 |
this->signals.edge_prolongation(false, level); |
| 259 |
|
| 260 |
// post-smoothing |
| 261 |
this->signals.post_smoother_step(true, level); |
| 262 |
post_smooth->smooth(level, solution[level], defect2[level]); |
| 263 |
this->signals.post_smoother_step(false, level); |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
template <typename VectorType> |
| 269 |
void |
| 270 |
Multigrid<VectorType>::cycle() |
| 271 |
{ |
| 272 |
// The defect vector has been initialized by copy_to_mg. Now adjust the |
| 273 |
// other vectors. First out a check if we have the right number of levels. |
| 274 |
if (solution.min_level() != minlevel || solution.max_level() != maxlevel) |
| 275 |
{ |
| 276 |
solution.resize(minlevel, maxlevel); |
| 277 |
t.resize(minlevel, maxlevel); |
| 278 |
} |
| 279 |
if (cycle_type != v_cycle && |
| 280 |
(defect2.min_level() != minlevel || defect2.max_level() != maxlevel)) |
| 281 |
defect2.resize(minlevel, maxlevel); |
| 282 |
|
| 283 |
// And now we go and reinit the vectors on the levels. |
| 284 |
for (unsigned int level = minlevel; level <= maxlevel; ++level) |
| 285 |
{ |
| 286 |
// the vectors for level>minlevel will be overwritten by the apply() |
| 287 |
// method of the smoother -> do not force them to be zeroed out here |
| 288 |
solution[level].reinit(defect[level], level > minlevel); |
| 289 |
t[level].reinit(defect[level], level > minlevel); |
| 290 |
if (cycle_type != v_cycle) |
| 291 |
defect2[level].reinit(defect[level]); |
| 292 |
} |
| 293 |
|
| 294 |
if (cycle_type == v_cycle) |
| 295 |
level_v_step(maxlevel); |
| 296 |
else |
| 297 |
level_step(maxlevel, cycle_type); |
| 298 |
} |
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
template <typename VectorType> |
| 303 |
void |
| 304 |
Multigrid<VectorType>::vcycle() |
| 305 |
{ |
| 306 |
// The defect vector has been initialized by copy_to_mg. Now adjust the |
| 307 |
// other vectors. First out a check if we have the right number of levels. |
| 308 |
if (solution.min_level() != minlevel || solution.max_level() != maxlevel) |
| 309 |
{ |
| 310 |
solution.resize(minlevel, maxlevel); |
| 311 |
t.resize(minlevel, maxlevel); |
| 312 |
} |
| 313 |
|
| 314 |
// And now we go and reinit the vectors on the levels. |
| 315 |
for (unsigned int level = minlevel; level <= maxlevel; ++level) |
| 316 |
{ |
| 317 |
solution[level].reinit(defect[level], level > minlevel); |
| 318 |
t[level].reinit(defect[level], level > minlevel); |
| 319 |
} |
| 320 |
level_v_step(maxlevel); |
| 321 |
} |
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
template <typename VectorType> |
| 326 |
boost::signals2::connection |
| 327 |
Multigrid<VectorType>::connect_coarse_solve( |
| 328 |
const std::function<void(const bool, const unsigned int)> &slot) |
| 329 |
{ |
| 330 |
return this->signals.coarse_solve.connect(slot); |
| 331 |
} |
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
template <typename VectorType> |
| 336 |
boost::signals2::connection |
| 337 |
Multigrid<VectorType>::connect_restriction( |
| 338 |
const std::function<void(const bool, const unsigned int)> &slot) |
| 339 |
{ |
| 340 |
return this->signals.restriction.connect(slot); |
| 341 |
} |
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
template <typename VectorType> |
| 346 |
boost::signals2::connection |
| 347 |
Multigrid<VectorType>::connect_prolongation( |
| 348 |
const std::function<void(const bool, const unsigned int)> &slot) |
| 349 |
{ |
| 350 |
return this->signals.prolongation.connect(slot); |
| 351 |
} |
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
template <typename VectorType> |
| 356 |
boost::signals2::connection |
| 357 |
Multigrid<VectorType>::connect_pre_smoother_step( |
| 358 |
const std::function<void(const bool, const unsigned int)> &slot) |
| 359 |
{ |
| 360 |
return this->signals.pre_smoother_step.connect(slot); |
| 361 |
} |
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
template <typename VectorType> |
| 366 |
boost::signals2::connection |
| 367 |
Multigrid<VectorType>::connect_post_smoother_step( |
| 368 |
const std::function<void(const bool, const unsigned int)> &slot) |
| 369 |
{ |
| 370 |
return this->signals.post_smoother_step.connect(slot); |
| 371 |
} |
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
template <typename VectorType> |
| 376 |
boost::signals2::connection |
| 377 |
Multigrid<VectorType>::connect_residual_step( |
| 378 |
const std::function<void(const bool, const unsigned int)> &slot) |
| 379 |
{ |
| 380 |
return this->signals.residual_step.connect(slot); |
| 381 |
} |
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
template <typename VectorType> |
| 386 |
boost::signals2::connection |
| 387 |
Multigrid<VectorType>::connect_edge_prolongation( |
| 388 |
const std::function<void(const bool, const unsigned int)> &slot) |
| 389 |
{ |
| 390 |
return this->signals.edge_prolongation.connect(slot); |
| 391 |
} |
| 392 |
|
| 393 |
|
| 394 |
DEAL_II_NAMESPACE_CLOSE |
| 395 |
|
| 396 |
#endif |