Skip to content

Commit 8a50f87

Browse files
committed
GMRES: Minor updates
Move function definitions to a cpp file so that the header could be included in multiple translation units. Move the class out of amrex namespace to avoid potential conflicts in the future.
1 parent 29f211e commit 8a50f87

File tree

4 files changed

+89
-91
lines changed

4 files changed

+89
-91
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#ifndef GMRES_POISSON_H_
2+
#define GMRES_POISSON_H_
3+
4+
#include <AMReX_GMRES.H>
5+
#include <AMReX_Geometry.H>
6+
#include <AMReX_MultiFab.H>
7+
8+
/**
9+
* \brief Solve Poisson's equation using amrex GMRES class
10+
*
11+
* Refer to comments in amrex/Src/LinearSolvers/AMReX_GMRES.H
12+
* for details on function implementation requirements
13+
*/
14+
class GMRESPOISSON
15+
{
16+
public:
17+
using RT = amrex::Real; // double or float
18+
using GM = amrex::GMRES<amrex::MultiFab,GMRESPOISSON>;
19+
20+
GMRESPOISSON (const amrex::BoxArray& ba, const amrex::DistributionMapping& dm, const amrex::Geometry& geom);
21+
22+
/**
23+
* \brief Solve the linear system
24+
*
25+
* \param a_sol unknowns, i.e., x in A x = b.
26+
* \param a_rhs RHS, i.e., b in A x = b.
27+
* \param a_tol_rel relative tolerance.
28+
* \param a_tol_abs absolute tolerance.
29+
*/
30+
void solve (amrex::MultiFab& a_sol, amrex::MultiFab const& a_rhs, RT a_tol_rel, RT a_tol_abs);
31+
32+
//! Sets verbosity.
33+
void setVerbose (int v) { m_gmres.setVerbose(v); }
34+
35+
//! Get the GMRES object.
36+
GM& getGMRES () { return m_gmres; }
37+
38+
//! Make MultiFab without ghost cells
39+
amrex::MultiFab makeVecRHS () const;
40+
41+
//! Make MultiFab with ghost cells and set ghost cells to zero
42+
amrex::MultiFab makeVecLHS () const;
43+
44+
RT norm2 (amrex::MultiFab const& mf) const;
45+
46+
static void scale (amrex::MultiFab& mf, RT scale_factor);
47+
48+
RT dotProduct (amrex::MultiFab const& mf1, amrex::MultiFab const& mf2) const;
49+
50+
//! lhs = 0
51+
static void setToZero (amrex::MultiFab& lhs);
52+
53+
//! lhs = rhs
54+
static void assign (amrex::MultiFab& lhs, amrex::MultiFab const& rhs);
55+
56+
//! lhs += a*rhs
57+
static void increment (amrex::MultiFab& lhs, amrex::MultiFab const& rhs, RT a);
58+
59+
//! lhs = a*rhs_a + b*rhs_b
60+
static void linComb (amrex::MultiFab& lhs, RT a, amrex::MultiFab const& rhs_a, RT b, amrex::MultiFab const& rhs_b);
61+
62+
//! lhs = L(rhs)
63+
void apply (amrex::MultiFab& lhs, amrex::MultiFab& rhs) const;
64+
65+
void precond (amrex::MultiFab& lhs, amrex::MultiFab const& rhs) const;
66+
67+
//! Control whether or not to use MLMG as preconditioner.
68+
bool usePrecond (bool new_flag) { return std::exchange(m_use_precond, new_flag); }
69+
70+
private:
71+
GM m_gmres;
72+
amrex::BoxArray m_ba;
73+
amrex::DistributionMapping m_dm;
74+
amrex::Geometry m_geom;
75+
bool m_use_precond;
76+
};
77+
78+
#endif

ExampleCodes/LinearSolvers/GMRES/Poisson/AMReX_GMRES_Poisson.H renamed to ExampleCodes/LinearSolvers/GMRES/Poisson/GMRES_Poisson.cpp

Lines changed: 6 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,99 +1,24 @@
1-
#ifndef AMREX_GMRES_POISSON_H_
2-
#define AMREX_GMRES_POISSON_H_
3-
#include <AMReX_Config.H>
4-
5-
#include <AMReX_GMRES.H>
6-
#include <utility>
7-
8-
namespace amrex {
9-
10-
/**
11-
* \brief Solve Poisson's equation using amrex GMRES class
12-
*
13-
* Refer to comments in amrex/Src/LinearSolvers/AMReX_GMRES.H
14-
* for details on function implementation requirements
15-
*/
16-
class GMRESPOISSON
17-
{
18-
public:
19-
using RT = amrex::Real; // double or float
20-
using GM = GMRES<MultiFab,GMRESPOISSON>;
21-
22-
explicit GMRESPOISSON (const BoxArray& ba, const DistributionMapping& dm, const Geometry& geom);
23-
24-
/**
25-
* \brief Solve the linear system
26-
*
27-
* \param a_sol unknowns, i.e., x in A x = b.
28-
* \param a_rhs RHS, i.e., b in A x = b.
29-
* \param a_tol_rel relative tolerance.
30-
* \param a_tol_abs absolute tolerance.
31-
*/
32-
void solve (MultiFab& a_sol, MultiFab const& a_rhs, RT a_tol_rel, RT a_tol_abs);
33-
34-
//! Sets verbosity.
35-
void setVerbose (int v) { m_gmres.setVerbose(v); }
36-
37-
//! Get the GMRES object.
38-
GM& getGMRES () { return m_gmres; }
39-
40-
//! Make MultiFab without ghost cells
41-
MultiFab makeVecRHS () const;
42-
43-
//! Make MultiFab with ghost cells and set ghost cells to zero
44-
MultiFab makeVecLHS () const;
45-
46-
RT norm2 (MultiFab const& mf) const;
47-
48-
static void scale (MultiFab& mf, RT scale_factor);
49-
50-
RT dotProduct (MultiFab const& mf1, MultiFab const& mf2) const;
51-
52-
//! lhs = 0
53-
static void setToZero (MultiFab& lhs);
1+
#include "GMRES_Poisson.H"
542

55-
//! lhs = rhs
56-
static void assign (MultiFab& lhs, MultiFab const& rhs);
57-
58-
//! lhs += a*rhs
59-
static void increment (MultiFab& lhs, MultiFab const& rhs, RT a);
60-
61-
//! lhs = a*rhs_a + b*rhs_b
62-
static void linComb (MultiFab& lhs, RT a, MultiFab const& rhs_a, RT b, MultiFab const& rhs_b);
63-
64-
//! lhs = L(rhs)
65-
void apply (MultiFab& lhs, MultiFab& rhs) const;
66-
67-
void precond (MultiFab& lhs, MultiFab const& rhs) const;
68-
69-
//! Control whether or not to use MLMG as preconditioner.
70-
bool usePrecond (bool new_flag) { return std::exchange(m_use_precond, new_flag); }
71-
72-
private:
73-
GM m_gmres;
74-
BoxArray m_ba;
75-
DistributionMapping m_dm;
76-
Geometry m_geom;
77-
bool m_use_precond;
78-
};
3+
using namespace amrex;
794

805
GMRESPOISSON::GMRESPOISSON (const BoxArray& ba, const DistributionMapping& dm, const Geometry& geom)
816
: m_ba(ba), m_dm(dm), m_geom(geom)
827
{
838
m_gmres.define(*this);
849
}
8510

86-
auto GMRESPOISSON::makeVecRHS () const -> MultiFab
11+
MultiFab GMRESPOISSON::makeVecRHS () const
8712
{
8813
return MultiFab(m_ba, m_dm, 1, 0);
8914
}
9015

91-
auto GMRESPOISSON::makeVecLHS () const -> MultiFab
16+
MultiFab GMRESPOISSON::makeVecLHS () const
9217
{
9318
return MultiFab(m_ba, m_dm, 1, 1);
9419
}
9520

96-
auto GMRESPOISSON::norm2 (MultiFab const& mf) const -> RT
21+
Real GMRESPOISSON::norm2 (MultiFab const& mf) const
9722
{
9823
return mf.norm2();
9924
}
@@ -103,7 +28,7 @@ void GMRESPOISSON::scale (MultiFab& mf, RT scale_factor)
10328
mf.mult(scale_factor);
10429
}
10530

106-
auto GMRESPOISSON::dotProduct (MultiFab const& mf1, MultiFab const& mf2) const -> RT
31+
Real GMRESPOISSON::dotProduct (MultiFab const& mf1, MultiFab const& mf2) const
10732
{
10833
return MultiFab::Dot(mf1,0,mf2,0,1,0);
10934
}
@@ -209,7 +134,3 @@ void GMRESPOISSON::solve (MultiFab& a_sol, MultiFab const& a_rhs, RT a_tol_rel,
209134
{
210135
m_gmres.solve(a_sol, a_rhs, a_tol_rel, a_tol_abs);
211136
}
212-
213-
}
214-
215-
#endif
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
CEXE_sources += main.cpp
2-
CEXE_headers += AMReX_GMRES_Poisson.H
1+
CEXE_sources += main.cpp GMRES_Poisson.cpp
2+
CEXE_headers += GMRES_Poisson.H

ExampleCodes/LinearSolvers/GMRES/Poisson/main.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
* A simplified usage of the AMReX GMRES class
33
*/
44

5+
#include "GMRES_Poisson.H"
6+
57
#include <AMReX.H>
68
#include <AMReX_PlotFileUtil.H>
79
#include <AMReX_ParmParse.H>
8-
#include <AMReX_GMRES_Poisson.H>
910

1011
int main (int argc, char* argv[])
1112
{
@@ -112,7 +113,7 @@ int main (int argc, char* argv[])
112113

113114
WriteSingleLevelPlotfile("rhs", rhs, {"rhs"}, geom, 0., 0);
114115

115-
amrex::GMRESPOISSON gmres_poisson(ba,dm,geom);
116+
GMRESPOISSON gmres_poisson(ba,dm,geom);
116117

117118
// initial guess
118119
phi.setVal(0.);
@@ -127,5 +128,3 @@ int main (int argc, char* argv[])
127128
amrex::Finalize();
128129
return 0;
129130
}
130-
131-

0 commit comments

Comments
 (0)