Grid 0.7.0
SolverModules.h
Go to the documentation of this file.
1/*************************************************************************************
2
3Grid physics library, www.github.com/paboyle/Grid
4
5Source file: ./lib/qcd/modules/SolverModules.h
6
7Copyright (C) 2016
8
9Author: Guido Cossu <guido.cossu@ed.ac.uk>
10
11This program is free software; you can redistribute it and/or modify
12it under the terms of the GNU General Public License as published by
13the Free Software Foundation; either version 2 of the License, or
14(at your option) any later version.
15
16This program is distributed in the hope that it will be useful,
17but WITHOUT ANY WARRANTY; without even the implied warranty of
18MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19GNU General Public License for more details.
20
21You should have received a copy of the GNU General Public License along
22with this program; if not, write to the Free Software Foundation, Inc.,
2351 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24
25See the full license in the file "LICENSE" in the top level distribution
26directory
27*************************************************************************************/
28 /* END LEGAL */
29#ifndef SOLVER_MODULES_H
30#define SOLVER_MODULES_H
31
33
35// Operator Functions (Solvers)
37
38template <template <typename> class SolverType, class Field, class SPar>
40 : public Parametrized<SPar>,
41 public HMCModuleBase<OperatorFunction<Field> > {
42public:
44 typedef typename Base::Product Product;
45
46 std::unique_ptr< SolverType<Field> > SolverPtr;
47
48 SolverModule(SPar Par) : Parametrized<SPar>(Par) {}
49
50 template <class ReaderClass>
51 SolverModule(Reader<ReaderClass>& Reader) : Parametrized<SPar>(Reader){};
52
53 virtual void print_parameters(){
54 std::cout << this->Par_ << std::endl;
55 }
56
58 if (!SolverPtr) initialize();
59
60 return SolverPtr.get();
61 }
62
63private:
64 virtual void initialize() = 0;
65};
66
67
68// Factory
69template <char const *str, class Field, class ReaderClass >
71 : public Factory < HMCModuleBase<OperatorFunction<Field> > , Reader<ReaderClass> > {
72public:
73 // use SINGLETON FUNCTOR MACRO HERE
74 typedef Reader<ReaderClass> TheReader;
75
77 void operator=(const HMC_SolverModuleFactory& e) = delete;
80 return e;
81 }
82
83private:
84 HMC_SolverModuleFactory(void) = default;
85 std::string obj_type() const {
86 return std::string(str);
87 }
88};
89
90
91
92class SolverParameters : Serializable {
93public:
95 RealD, tolerance,
96 RealD, max_iterations);
97 // add error on no convergence?
98};
99
100
101class SolverObjName: Serializable {
102public:
104 std::string, name,
105 SolverParameters, parameters);
106
107};
108
109
110
111template <class Field >
112class ConjugateGradientModule: public SolverModule<ConjugateGradient, Field, SolverParameters> {
114 using SolverBase::SolverBase; // for constructors
115
116 // acquire resource
117 virtual void initialize(){
118 this->SolverPtr.reset(new ConjugateGradient<Field>(this->Par_.tolerance, this->Par_.max_iterations, true));
119 }
120};
121
122template <class Field >
123class BiCGSTABModule: public SolverModule<BiCGSTAB, Field, SolverParameters> {
125 using SolverBase::SolverBase; // for constructors
126
127 // acquire resource
128 virtual void initialize(){
129 this->SolverPtr.reset(new BiCGSTAB<Field>(this->Par_.tolerance, this->Par_.max_iterations, true));
130 }
131};
132
133template <class Field >
134class ConjugateResidualModule: public SolverModule<ConjugateResidual, Field, SolverParameters> {
136 using SolverBase::SolverBase; // for constructors
137
138 // acquire resource
139 virtual void initialize(){
140 this->SolverPtr.reset(new ConjugateResidual<Field>(this->Par_.tolerance, this->Par_.max_iterations));
141 }
142
143};
144
145extern char solver_string[];
146
148
149#endif //SOLVER_MODULES_H
#define NAMESPACE_BEGIN(A)
Definition Namespace.h:35
#define NAMESPACE_END(A)
Definition Namespace.h:36
double RealD
Definition Simd.h:61
char solver_string[]
SolverModule< BiCGSTAB, Field, SolverParameters > SolverBase
virtual void initialize()
virtual void initialize()
SolverModule< ConjugateGradient, Field, SolverParameters > SolverBase
SolverModule< ConjugateResidual, Field, SolverParameters > SolverBase
virtual void initialize()
OperatorFunction< Field > Product
Definition Modules.h:97
Reader< ReaderClass > TheReader
HMC_SolverModuleFactory(void)=default
std::string obj_type() const
HMC_SolverModuleFactory(const HMC_SolverModuleFactory &e)=delete
static HMC_SolverModuleFactory & getInstance(void)
void operator=(const HMC_SolverModuleFactory &e)=delete
Parametrized(Parameters Par)
Definition Modules.h:51
virtual void print_parameters()
Product * getPtr()
HMCModuleBase< OperatorFunction< Field > > Base
std::unique_ptr< ConjugateGradient< Field > > SolverPtr
SolverModule(SPar Par)
virtual void initialize()=0
SolverModule(Reader< ReaderClass > &Reader)
Base::Product Product
GRID_SERIALIZABLE_CLASS_MEMBERS(SolverObjName, std::string, name, SolverParameters, parameters)
GRID_SERIALIZABLE_CLASS_MEMBERS(SolverParameters, RealD, tolerance, RealD, max_iterations)