Bridge++  Ver. 1.3.x
fopr_Rational.cpp
Go to the documentation of this file.
1 
14 #include "fopr_Rational.h"
15 
16 #ifdef USE_PARAMETERS_FACTORY
17 #include "parameters_factory.h"
18 #endif
19 
20 #ifdef USE_FACTORY
21 namespace {
22  Fopr *create_object(Fopr *fopr)
23  {
24  return new Fopr_Rational(fopr);
25  }
26 
27 
28  bool init = Fopr::Factory_fopr::Register("Rational", create_object);
29 }
30 #endif
31 
32 //- parameter entry
33 namespace {
34  void append_entry(Parameters& param)
35  {
36  param.Register_int("number_of_poles", 0);
37  param.Register_int("exponent_numerator", 0);
38  param.Register_int("exponent_denominator", 0);
39  param.Register_double("lower_bound", 0.0);
40  param.Register_double("upper_bound", 0.0);
41  param.Register_int("maximum_number_of_iteration", 0);
42  param.Register_double("convergence_criterion_squared", 0.0);
43 
44  param.Register_string("verbose_level", "NULL");
45  }
46 
47 
48 #ifdef USE_PARAMETERS_FACTORY
49  bool init_param = ParametersFactory::Register("Fopr.Rational", append_entry);
50 #endif
51 }
52 //- end
53 
54 //- parameters class
56 //- end
57 
58 const std::string Fopr_Rational::class_name = "Fopr_Rational";
59 
60 //====================================================================
62 {
63  const string str_vlevel = params.get_string("verbose_level");
64 
65  m_vl = vout.set_verbose_level(str_vlevel);
66 
67  //- fetch and check input parameters
68  int Np, n_exp, d_exp;
69  double x_min, x_max;
70  int Niter;
71  double Stop_cond;
72 
73  int err = 0;
74  err += params.fetch_int("number_of_poles", Np);
75  err += params.fetch_int("exponent_numerator", n_exp);
76  err += params.fetch_int("exponent_denominator", d_exp);
77  err += params.fetch_double("lower_bound", x_min);
78  err += params.fetch_double("upper_bound", x_max);
79  err += params.fetch_int("maximum_number_of_iteration", Niter);
80  err += params.fetch_double("convergence_criterion_squared", Stop_cond);
81 
82  if (err) {
83  vout.crucial(m_vl, "%s: fetch error, input parameter not found.\n", class_name.c_str());
84  exit(EXIT_FAILURE);
85  }
86 
87 
88  set_parameters(Np, n_exp, d_exp, x_min, x_max, Niter, Stop_cond);
89 }
90 
91 
92 //====================================================================
93 void Fopr_Rational::set_parameters(int Np, int n_exp, int d_exp,
94  double x_min, double x_max,
95  int Niter, double Stop_cond)
96 {
97  //- print input parameters
98  vout.general(m_vl, "Parameters of %s:\n", class_name.c_str());
99  vout.general(m_vl, " Np = %d\n", Np);
100  vout.general(m_vl, " n_exp = %d\n", n_exp);
101  vout.general(m_vl, " d_exp = %d\n", d_exp);
102  vout.general(m_vl, " x_min = %10.6f\n", x_min);
103  vout.general(m_vl, " x_max = %10.6f\n", x_max);
104  vout.general(m_vl, " Niter = %d\n", Niter);
105  vout.general(m_vl, " Stop_cond = %10.6f\n", Stop_cond);
106 
107  //- range check
108  int err = 0;
109  err += ParameterCheck::non_zero(Np);
110  err += ParameterCheck::non_zero(n_exp);
111  err += ParameterCheck::non_zero(d_exp);
112  // NB. x_min,x_max=0 is allowed.
113  err += ParameterCheck::non_zero(Niter);
114  err += ParameterCheck::square_non_zero(Stop_cond);
115 
116  if (err) {
117  vout.crucial(m_vl, "%s: parameter range check failed.\n", class_name.c_str());
118  exit(EXIT_FAILURE);
119  }
120 
121  //- store values
122  m_Np = Np;
123  m_n_exp = n_exp;
124  m_d_exp = d_exp;
125  m_x_min = x_min;
126  m_x_max = x_max;
127  m_Niter = Niter;
128  m_Stop_cond = Stop_cond;
129 
130  //- post-process
131  init_parameters();
132 }
133 
134 
135 //====================================================================
137 {
138  int Nin = m_fopr->field_nin();
139  int Nvol = m_fopr->field_nvol();
140  int Nex = m_fopr->field_nex();
141 
142  int Nshift = m_Np;
143 
144  double x_min2 = m_x_min * m_x_min;
145  double x_max2 = m_x_max * m_x_max;
146 
147  m_cl.resize(m_Np);
148  m_bl.resize(m_Np);
149 
150  m_xq.resize(m_Np);
151  for (int i = 0; i < Nshift; ++i) {
152  m_xq[i].reset(Nin, Nvol, Nex);
153  }
154 
156 
157  Math_Rational rational;
158  rational.set_parameters(m_Np, m_n_exp, m_d_exp, x_min2, x_max2);
159  rational.get_parameters(m_a0, m_bl, m_cl);
160 
161  vout.general(m_vl, " a0 = %18.14f\n", m_a0);
162  for (int i = 0; i < m_Np; i++) {
163  vout.general(m_vl, " bl[%d] = %18.14f cl[%d] = %18.14f\n",
164  i, m_bl[i], i, m_cl[i]);
165  }
166 }
167 
168 
169 //====================================================================
170 void Fopr_Rational::mult(Field& v, const Field& b)
171 {
172  int Nconv;
173  double diff;
174 
175  assert(v.nin() == b.nin());
176  assert(v.nvol() == b.nvol());
177  assert(v.nex() == b.nex());
178 
179  vout.general(m_vl, " Shift solver in rational function\n");
180  vout.general(m_vl, " Number of shift values = %d\n", m_Np);
181 
182  m_fopr->set_mode("DdagD");
183  m_solver->solve(m_xq, m_cl, b, Nconv, diff);
184 
185  vout.general(m_vl, " diff(max) = %22.15e \n", diff);
186 
187  copy(v, b);
188  scal(v, m_a0); // v *= m_a0;
189  for (int i = 0; i < m_Np; i++) {
190  axpy(v, m_bl[i], m_xq[i]); // v += m_bl[i] * m_xq[i];
191  }
192 }
193 
194 
195 //====================================================================
196 double Fopr_Rational::func(double x)
197 {
198  double y = m_a0;
199 
200  for (int k = 0; k < m_Np; ++k) {
201  y += m_bl[k] / (x + m_cl[k]);
202  }
203 
204  return y;
205 }
206 
207 
208 //====================================================================
209 //============================================================END=====
void scal(Field &x, const double a)
scal(x, a): x = a * x
Definition: field.cpp:282
void get_parameters(double &norm, std::vector< double > &res, std::vector< double > &pole)
Fermion operator for rational approximation.
Definition: fopr_Rational.h:48
BridgeIO vout
Definition: bridgeIO.cpp:278
void Register_string(const string &, const string &)
Definition: parameters.cpp:351
void general(const char *format,...)
Definition: bridgeIO.cpp:65
void Register_int(const string &, const int)
Definition: parameters.cpp:330
void set_parameters(const Parameters &params)
std::vector< Field > m_xq
Definition: fopr_Rational.h:66
Container of Field-type object.
Definition: field.h:39
int nvol() const
Definition: field.h:116
Multishift Conjugate Gradient solver.
void init_parameters()
Class for parameters.
Definition: parameters.h:38
void copy(Field &y, const Field &x)
copy(y, x): y = x
Definition: field.cpp:381
void mult(Field &v, const Field &f)
multiplies fermion operator to a given field (2nd argument)
std::vector< double > m_cl
Definition: fopr_Rational.h:64
virtual int field_nin()=0
returns the on-site d.o.f. for which the fermion operator is defined.
int square_non_zero(const double v)
Definition: checker.cpp:41
int nin() const
Definition: field.h:115
double m_Stop_cond
Definition: fopr_Rational.h:58
Shiftsolver_CG * m_solver
Definition: fopr_Rational.h:61
Bridge::VerboseLevel m_vl
Definition: fopr.h:113
virtual int field_nex()=0
returns the external d.o.f. for which the fermion operator is defined.
int nex() const
Definition: field.h:117
void axpy(Field &y, const double a, const Field &x)
axpy(y, a, x): y := a * x + y
Definition: field.cpp:168
void crucial(const char *format,...)
Definition: bridgeIO.cpp:48
double func(double x)
static const std::string class_name
Definition: fopr_Rational.h:51
static bool Register(const std::string &realm, const creator_callback &cb)
void set_parameters(const Parameters &params)
int non_zero(const double v)
Definition: checker.cpp:31
virtual void set_mode(std::string mode)
setting the mode of multiplication if necessary. Default implementation here is just to avoid irrelev...
Definition: fopr.h:85
void Register_double(const string &, const double)
Definition: parameters.cpp:323
std::vector< double > m_bl
Definition: fopr_Rational.h:65
Base class of fermion operator family.
Definition: fopr.h:49
int fetch_double(const string &key, double &val) const
Definition: parameters.cpp:124
string get_string(const string &key) const
Definition: parameters.cpp:87
virtual int field_nvol()=0
returns the volume for which the fermion operator is defined.
int fetch_int(const string &key, int &val) const
Definition: parameters.cpp:141
static VerboseLevel set_verbose_level(const std::string &str)
Definition: bridgeIO.cpp:28
void solve(std::vector< Field > &solution, const std::vector< double > &shift, const Field &source, int &Nconv, double &diff)