| 1 |
// Copyright (c) 2010-2025, Lawrence Livermore National Security, LLC. Produced |
| 2 |
// at the Lawrence Livermore National Laboratory. All Rights reserved. See files |
| 3 |
// LICENSE and NOTICE for details. LLNL-CODE-806117. |
| 4 |
// |
| 5 |
// This file is part of the MFEM library. For more information and source code |
| 6 |
// availability visit https://mfem.org. |
| 7 |
// |
| 8 |
// MFEM is free software; you can redistribute it and/or modify it under the |
| 9 |
// terms of the BSD-3 license. We welcome feedback and contributions, see file |
| 10 |
// CONTRIBUTING.md for details. |
| 11 |
|
| 12 |
#include "ds-common.hpp" |
| 13 |
|
| 14 |
using namespace std; |
| 15 |
using namespace mfem; |
| 16 |
|
| 17 |
namespace ds_common |
| 18 |
{ |
| 19 |
|
| 20 |
int MONITOR_DIGITS = 20; |
| 21 |
int MG_MAX_ITER = 10; |
| 22 |
real_t MG_REL_TOL = std::sqrt(1e-10); |
| 23 |
|
| 24 |
int dim = 0; |
| 25 |
int space_dim = 0; |
| 26 |
real_t freq = 1.0; |
| 27 |
real_t kappa = 1.0; |
| 28 |
|
| 29 |
// Custom monitor that prints a csv-formatted file |
| 30 |
DataMonitor::DataMonitor(string file_name, int ndigits) |
| 31 |
: os(file_name), |
| 32 |
precision(ndigits) |
| 33 |
{ |
| 34 |
if (Mpi::Root()) |
| 35 |
{ |
| 36 |
mfem::out << "Saving iterations into: " << file_name << endl; |
| 37 |
} |
| 38 |
os << "it,res,sol" << endl; |
| 39 |
os << fixed << setprecision(precision); |
| 40 |
} |
| 41 |
|
| 42 |
void DataMonitor::MonitorResidual(int it, real_t norm, const Vector &x, |
| 43 |
bool final) |
| 44 |
{ |
| 45 |
os << it << "," << norm << ","; |
| 46 |
} |
| 47 |
|
| 48 |
void DataMonitor::MonitorSolution(int it, real_t norm, const Vector &x, |
| 49 |
bool final) |
| 50 |
{ |
| 51 |
os << norm << endl; |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
// Abs-L(1) general geometric multigrid method, derived from GeometricMultigrid |
| 56 |
AbsL1GeometricMultigrid::AbsL1GeometricMultigrid( |
| 57 |
ParFiniteElementSpaceHierarchy& fes_hierarchy, |
| 58 |
Array<int>& ess_bdr, |
| 59 |
IntegratorType it, |
| 60 |
SolverType st, |
| 61 |
AssemblyLevel al) |
| 62 |
: GeometricMultigrid(fes_hierarchy, ess_bdr), |
| 63 |
integrator_type(it), |
| 64 |
solver_type(st), |
| 65 |
assembly_level(al), |
| 66 |
coarse_pc(nullptr), |
| 67 |
one(1.0) |
| 68 |
{ |
| 69 |
// BilinearForm::FormSystemMatrix does not handle the ownership of A_l. |
| 70 |
// GeometricMultigrid owns the forms, and deletes them. |
| 71 |
mg_owned = !(AssemblyLevel::LEGACY == assembly_level); |
| 72 |
|
| 73 |
ConstructCoarseOperatorAndSolver(fes_hierarchy.GetFESpaceAtLevel(0)); |
| 74 |
for (int l = 1; l < fes_hierarchy.GetNumLevels(); ++l) |
| 75 |
{ |
| 76 |
ConstructOperatorAndSmoother(fes_hierarchy.GetFESpaceAtLevel(l), l); |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
void AbsL1GeometricMultigrid::ConstructCoarseOperatorAndSolver( |
| 81 |
ParFiniteElementSpace& coarse_fespace) |
| 82 |
{ |
| 83 |
ConstructBilinearForm(coarse_fespace); |
| 84 |
|
| 85 |
OperatorPtr coarse_mat; |
| 86 |
coarse_mat.SetType(Operator::ANY_TYPE); |
| 87 |
bfs[0]->FormSystemMatrix(*essentialTrueDofs[0], coarse_mat); |
| 88 |
coarse_mat.SetOperatorOwner(false); |
| 89 |
|
| 90 |
// Create smoother |
| 91 |
Vector local_ones(coarse_mat->Height()); |
| 92 |
Vector result(coarse_mat->Height()); |
| 93 |
|
| 94 |
local_ones = 1.0; |
| 95 |
coarse_mat->AbsMult(local_ones, result); |
| 96 |
|
| 97 |
coarse_pc = new OperatorJacobiSmoother(result, *essentialTrueDofs[0]); |
| 98 |
|
| 99 |
Solver* coarse_solver = nullptr; |
| 100 |
switch (solver_type) |
| 101 |
{ |
| 102 |
case sli: |
| 103 |
coarse_solver = new SLISolver(MPI_COMM_WORLD); |
| 104 |
break; |
| 105 |
case cg: |
| 106 |
coarse_solver = new CGSolver(MPI_COMM_WORLD); |
| 107 |
break; |
| 108 |
default: |
| 109 |
mfem_error("Invalid solver type!"); |
| 110 |
} |
| 111 |
coarse_solver->SetOperator(*coarse_mat); |
| 112 |
|
| 113 |
IterativeSolver *it_solver = dynamic_cast<IterativeSolver*>(coarse_solver); |
| 114 |
if (it_solver) |
| 115 |
{ |
| 116 |
it_solver->SetRelTol(MG_REL_TOL); |
| 117 |
it_solver->SetMaxIter(MG_MAX_ITER); |
| 118 |
it_solver->SetPrintLevel(-1); |
| 119 |
it_solver->SetPreconditioner(*coarse_pc); |
| 120 |
} |
| 121 |
|
| 122 |
AddLevel(coarse_mat.Ptr(), coarse_solver, mg_owned, true); |
| 123 |
} |
| 124 |
|
| 125 |
void AbsL1GeometricMultigrid::ConstructOperatorAndSmoother( |
| 126 |
ParFiniteElementSpace& fespace, int level) |
| 127 |
{ |
| 128 |
const Array<int> &ess_tdof_list = *essentialTrueDofs[level]; |
| 129 |
ConstructBilinearForm(fespace); |
| 130 |
|
| 131 |
OperatorPtr level_mat; |
| 132 |
level_mat.SetType(Operator::ANY_TYPE); |
| 133 |
bfs.Last()->FormSystemMatrix(ess_tdof_list, level_mat); |
| 134 |
level_mat.SetOperatorOwner(false); |
| 135 |
|
| 136 |
// Create smoother |
| 137 |
Vector local_ones(level_mat->Height()); |
| 138 |
Vector result(level_mat->Height()); |
| 139 |
|
| 140 |
local_ones = 1.0; |
| 141 |
level_mat->AbsMult(local_ones, result); |
| 142 |
|
| 143 |
Solver* smoother = new OperatorJacobiSmoother(result, ess_tdof_list); |
| 144 |
|
| 145 |
AddLevel(level_mat.Ptr(), smoother, mg_owned, true); |
| 146 |
} |
| 147 |
|
| 148 |
void AbsL1GeometricMultigrid::ConstructBilinearForm( |
| 149 |
ParFiniteElementSpace &fespace) |
| 150 |
{ |
| 151 |
ParBilinearForm* form = new ParBilinearForm(&fespace); |
| 152 |
form->SetAssemblyLevel(assembly_level); |
| 153 |
switch (integrator_type) |
| 154 |
{ |
| 155 |
case mass: |
| 156 |
form->AddDomainIntegrator(new MassIntegrator(one)); |
| 157 |
break; |
| 158 |
case diffusion: |
| 159 |
form->AddDomainIntegrator(new DiffusionIntegrator(one)); |
| 160 |
break; |
| 161 |
case maxwell: |
| 162 |
form->AddDomainIntegrator(new CurlCurlIntegrator(one)); |
| 163 |
form->AddDomainIntegrator(new VectorFEMassIntegrator(one)); |
| 164 |
break; |
| 165 |
default: |
| 166 |
mfem_error("Invalid integrator type! Check ParBilinearForm"); |
| 167 |
} |
| 168 |
form->Assemble(); |
| 169 |
bfs.Append(form); |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
void AssembleElementLpqJacobiDiag(ParBilinearForm& form, real_t p, real_t q, |
| 174 |
Vector& diag) |
| 175 |
{ |
| 176 |
ParBilinearForm temp_form(form.ParFESpace()); |
| 177 |
temp_form.AllocateMatrix(); |
| 178 |
for (int i = 0; i < form.ParFESpace()->GetNE(); ++i) |
| 179 |
{ |
| 180 |
DenseMatrix emat_i; |
| 181 |
form.ComputeElementMatrix(i, emat_i); |
| 182 |
Vector right(emat_i.Height()); |
| 183 |
Vector temp(emat_i.Height()); |
| 184 |
Vector left(emat_i.Height()); |
| 185 |
|
| 186 |
DenseMatrix temp_emat_i = emat_i; |
| 187 |
for (int j = 0; j < emat_i.Height(); ++j) |
| 188 |
{ |
| 189 |
for (int k = 0; k < emat_i.Width(); ++k) |
| 190 |
{ |
| 191 |
temp_emat_i(j, k) = std::pow(std::abs(emat_i(j, k)), p); |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
if (q!=0.0) |
| 196 |
{ |
| 197 |
emat_i.GetDiag(right); |
| 198 |
right.Abs(); |
| 199 |
right.Pow(q); |
| 200 |
} |
| 201 |
else |
| 202 |
{ |
| 203 |
right = 1.0; |
| 204 |
} |
| 205 |
|
| 206 |
temp_emat_i.Mult(right, temp); |
| 207 |
|
| 208 |
if (1.0 + q - p!= 0.0) |
| 209 |
{ |
| 210 |
emat_i.GetDiag(left); |
| 211 |
left.Abs(); |
| 212 |
left.Pow(1.0 + q - p); |
| 213 |
left *= temp; |
| 214 |
} |
| 215 |
else |
| 216 |
{ |
| 217 |
left = temp; |
| 218 |
} |
| 219 |
|
| 220 |
temp_emat_i.Clear(); |
| 221 |
temp_emat_i.Diag(left.GetData(), left.Size()); |
| 222 |
temp_form.AssembleElementMatrix(i, temp_emat_i, 1); |
| 223 |
} |
| 224 |
temp_form.Finalize(); |
| 225 |
auto mat = temp_form.ParallelAssemble(); |
| 226 |
mat->AssembleDiagonal(diag); |
| 227 |
delete mat; |
| 228 |
} |
| 229 |
|
| 230 |
real_t diffusion_solution(const Vector &x) |
| 231 |
{ |
| 232 |
if (dim == 3) |
| 233 |
{ |
| 234 |
return sin(kappa * x(0)) * sin(kappa * x(1)) * sin(kappa * x(2)) + 1.0; |
| 235 |
} |
| 236 |
else |
| 237 |
{ |
| 238 |
return sin(kappa * x(0)) * sin(kappa * x(1)) + 1.0; |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
real_t diffusion_source(const Vector &x) |
| 243 |
{ |
| 244 |
if (dim == 3) |
| 245 |
{ |
| 246 |
return dim * kappa * kappa * sin(kappa * x(0)) * sin(kappa * x(1)) * |
| 247 |
sin(kappa * x(2)); |
| 248 |
} |
| 249 |
else |
| 250 |
{ |
| 251 |
return dim * kappa * kappa * sin(kappa * x(0)) * sin(kappa * x(1)); |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
void maxwell_solution(const Vector &x, Vector &u) |
| 256 |
{ |
| 257 |
if (dim == 3) |
| 258 |
{ |
| 259 |
u(0) = sin(kappa * x(1)); |
| 260 |
u(1) = sin(kappa * x(2)); |
| 261 |
u(2) = sin(kappa * x(0)); |
| 262 |
} |
| 263 |
else |
| 264 |
{ |
| 265 |
u(0) = sin(kappa * x(1)); |
| 266 |
u(1) = sin(kappa * x(0)); |
| 267 |
if (x.Size() == 3) { u(2) = 0.0; } |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
void maxwell_source(const Vector &x, Vector &f) |
| 272 |
{ |
| 273 |
if (dim == 3) |
| 274 |
{ |
| 275 |
f(0) = (1. + kappa * kappa) * sin(kappa * x(1)); |
| 276 |
f(1) = (1. + kappa * kappa) * sin(kappa * x(2)); |
| 277 |
f(2) = (1. + kappa * kappa) * sin(kappa * x(0)); |
| 278 |
} |
| 279 |
else |
| 280 |
{ |
| 281 |
f(0) = (1. + kappa * kappa) * sin(kappa * x(1)); |
| 282 |
f(1) = (1. + kappa * kappa) * sin(kappa * x(0)); |
| 283 |
if (x.Size() == 3) { f(2) = 0.0; } |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
} // end namespace ds_common |