CHROMA
inline_eigbnds.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 /*! \file
3  * \brief Inline measurements for eigenvalue bounds
4  *
5  * Measure eigenvalue bounds of M^dag*M
6  */
7 
10 #include "meas/eig/eig_spec.h"
14 
17 
18 namespace Chroma
19 {
20 
21  namespace InlineEigBndsMdagMEnv
22  {
23  namespace
24  {
25  AbsInlineMeasurement* createMeasurement(XMLReader& xml_in,
26  const std::string& path)
27  {
28  return new InlineEigBndsMdagM(InlineEigBndsMdagMParams(xml_in, path));
29  }
30 
31  //! Local registration flag
32  bool registered = false;
33  }
34 
35  const std::string name = "EIGBNDSMDAGM";
36 
37  //! Register all the factories
38  bool registerAll()
39  {
40  bool success = true;
41  if (! registered)
42  {
43  success &= TheInlineMeasurementFactory::Instance().registerObject(name,
44  createMeasurement);
45  registered = true;
46  }
47  return success;
48  }
49  }
50 
51 
52  //! Ritz input
53  void read(XMLReader& xml, const std::string& path,
55  {
56  XMLReader paramtop(xml, path);
57 
58  read(paramtop, "RsdR", param.RsdR);
59  read(paramtop, "RsdA", param.RsdA);
60  if( paramtop.count("RsdRHi") == 0 ) {
61  param.RsdRHi = param.RsdR;
62  }
63  else {
64  read(paramtop, "RsdRHi", param.RsdRHi);
65  }
66  if( paramtop.count("RsdAHi") == 0 ) {
67  param.RsdAHi = param.RsdA;
68  }
69  else {
70  read(paramtop, "RsdAHi", param.RsdAHi);
71  }
72  read(paramtop, "RsdZero", param.RsdZero);
73  read(paramtop, "ProjApsiP", param.ProjApsiP);
74  read(paramtop, "Nmin", param.Nmin);
75  read(paramtop, "MaxCG", param.MaxCG);
76  read(paramtop, "Nrenorm", param.Nrenorm);
77  if( paramtop.count("Neig")==1 ) {
78  read(paramtop, "Neig", param.Neig);
79  }
80  else {
81  param.Neig=1;
82  }
83  }
84 
85  //! Ritz output
86  void write(XMLWriter& xml, const std::string& path,
88  {
89  push(xml, path);
90 
91  write(xml, "RsdR", param.RsdR);
92  write(xml, "RsdRHi", param.RsdRHi);
93  write(xml, "RsdA", param.RsdA);
94  write(xml, "RsdAHi", param.RsdAHi);
95  write(xml, "RsdZero", param.RsdZero);
96  write(xml, "ProjApsiP", param.ProjApsiP);
97  write(xml, "Nmin", param.Nmin);
98  write(xml, "MaxCG", param.MaxCG);
99  write(xml, "Nrenorm", param.Nrenorm);
100  if( param.Neig != 1 ) {
101  write(xml, "Neig", param.Neig);
102  }
103  pop(xml);
104  }
105 
106 
107  //! Object buffer
108  void write(XMLWriter& xml, const std::string& path, const InlineEigBndsMdagMParams::NamedObject_t& input)
109  {
110  push(xml, path);
111 
112  write(xml, "gauge_id", input.gauge_id);
113 
114  pop(xml);
115  }
116 
117 
118  //! Object buffer
119  void read(XMLReader& xml, const std::string& path, InlineEigBndsMdagMParams::NamedObject_t& input)
120  {
121  XMLReader inputtop(xml, path);
122 
123  read(inputtop, "gauge_id", input.gauge_id);
124  }
125 
126 
127  // Param stuff
129  {
130  frequency = 0;
131  }
132 
134  {
135  try
136  {
137  XMLReader paramtop(xml_in, path);
138 
139  read(paramtop, "Frequency", frequency);
140  read(paramtop, "Ritz", ritz);
141 
142  if (paramtop.count("usePV") == 1)
143  read(paramtop, "usePV", usePV);
144  else
145  usePV = false;
146 
147  ferm_act = readXMLGroup(paramtop, "FermionAction", "FermAct");
148 
149  // Ids
150  read(paramtop, "NamedObject", named_obj);
151 
152  // Possible alternate XML file pattern
153  if (paramtop.count("xml_file") != 0)
154  {
155  read(paramtop, "xml_file", xml_file);
156  }
157  }
158  catch(const std::string& e)
159  {
160  QDPIO::cerr << "Caught Exception reading XML: " << e << std::endl;
161  QDP_abort(1);
162  }
163  }
164 
165 
166  // Writer
167  void
168  InlineEigBndsMdagMParams::write(XMLWriter& xml, const std::string& path)
169  {
170  push(xml, path);
171 
172  QDP::write(xml, "Frequency", frequency);
173  QDP::write(xml, "usePV", usePV);
174  xml << ferm_act.xml;
175  Chroma::write(xml, "Ritz", ritz);
176 
177  // Ids
178  Chroma::write(xml, "NamedObject", named_obj);
179 
180  pop(xml);
181  }
182 
183 
184  // Constructor
186  {
187  std::istringstream is(params.ferm_act.xml);
188  XMLReader fermact_reader(is);
189 
190  try
191  {
192  // Construct the factory
194  fermact_reader,
196  }
197  catch(const std::string& s) {
198  QDPIO::cerr << __func__ << ": caught exception constructing fermact: " << s << std::endl;
199  QDP_abort(1);
200  }
201 
202  }
203 
204  // "Do" helper on a 4D action
205  void
207  unsigned long update_no,
208  XMLWriter& xml_out)
209  {
210  QDPIO::cout << "4D eig bnds" << std::endl;
211 
212  push(xml_out, "EigBndsMdagM");
213  write(xml_out, "update_no", update_no);
214  params.write(xml_out, "Input");
215 
216  int n_eig = params.ritz.Neig;
217  multi1d<Real> lambda(n_eig);
218  multi1d<Real> check_norm(n_eig);
219  multi1d<LatticeFermion> psi(n_eig);
220  int n_CG_count;
221 
222  for(int i =0; i < n_eig; i++)
223  gaussian(psi[i]);
224 
225  QDPIO::cout << "Look for lowest ev" << std::endl;
226 
227  push(xml_out,"LowestEv");
228  EigSpecRitzCG(*MM,
229  lambda,
230  psi,
231  n_eig,
233  params.ritz.Nmin,
234  params.ritz.MaxCG,
235  params.ritz.RsdR,
236  params.ritz.RsdA,
239  n_CG_count,
240  xml_out);
241  pop(xml_out); // LowestEv
242 
243  QDPIO::cout << "Look for highest ev" << std::endl;
245 
246  // Look for highest ev
247  n_eig = 1;
248  psi.resize(n_eig);
249  lambda.resize(n_eig);
250  for(int i =0; i < n_eig; i++)
251  gaussian(psi[i]);
252 
253  push(xml_out,"HighestEv");
254  EigSpecRitzCG(*MinusMM,
255  lambda,
256  psi,
257  n_eig,
259  params.ritz.Nmin,
260  params.ritz.MaxCG,
265  n_CG_count,
266  xml_out);
267  pop(xml_out); // HighestEv
268 
269  pop(xml_out); // pop("EigBndsMdagM");
270  }
271 
272 
273 
274  // "Do" helper on a 5D action
275  void
277  unsigned long update_no,
278  XMLWriter& xml_out)
279  {
280  QDPIO::cout << "5D eig bnds" << std::endl;
281 
282  push(xml_out, "EigBndsMdagM");
283  write(xml_out, "update_no", update_no);
284  params.write(xml_out, "Input");
285 
286  int n_eig = 1;
287  const int N5 = MM->size();
288  multi1d<Real> lambda(n_eig);
289  multi1d<Real> check_norm(n_eig);
290  multi2d<LatticeFermion> psi(n_eig, N5);
291  int n_CG_count;
292 
293  for(int i=0; i < n_eig; i++)
294  for(int n=0; n < N5; n++)
295  gaussian(psi[i][n]);
296 
297  QDPIO::cout << "Look for lowest ev" << std::endl;
298 
299  push(xml_out,"LowestEv");
300  EigSpecRitzCG(*MM,
301  lambda,
302  psi,
303  n_eig,
305  params.ritz.Nmin,
306  params.ritz.MaxCG,
307  params.ritz.RsdR,
308  params.ritz.RsdA,
311  n_CG_count,
312  xml_out);
313  pop(xml_out); // LowestEv
314 
315  {
316  multi1d<Double> prof(N5);
317  for(int n=0; n < N5; n++)
318  prof[n] = norm2(psi[0][n], MM->subset());
319 
320  write(xml_out, "EigenVectorProfile", prof);
321 
322  for(int n=0; n < N5; n++)
323  prof[n] = innerProductReal(psi[0][n], chiralProjectPlus(psi[0][n]), MM->subset());
324 
325  write(xml_out, "EigenVectorProfilePlus", prof);
326 
327  for(int n=0; n < N5; n++)
328  prof[n] = innerProductReal(psi[0][n], chiralProjectMinus(psi[0][n]), MM->subset());
329 
330  write(xml_out, "EigenVectorProfileMinus", prof);
331  }
332 
333  QDPIO::cout << "Look for highest ev" << std::endl;
335 
336  // Look for highest ev
337  for(int i=0; i < n_eig; i++)
338  for(int n=0; n < N5; n++)
339  gaussian(psi[i][n]);
340 
341  push(xml_out,"HighestEv");
342  EigSpecRitzCG(*MinusMM,
343  lambda,
344  psi,
345  n_eig,
347  params.ritz.Nmin,
348  params.ritz.MaxCG,
353  n_CG_count,
354  xml_out);
355  pop(xml_out); // HighestEv
356 
357  pop(xml_out); // pop("EigBndsMdagM");
358  }
359 
360 
361 
362  // Function call
363  void
364  InlineEigBndsMdagM::operator()(unsigned long update_no,
365  XMLWriter& xml_out)
366  {
367  // If xml file not empty, then use alternate
368  if (params.xml_file != "")
369  {
370  std::string xml_file = makeXMLFileName(params.xml_file, update_no);
371 
372  push(xml_out, "EigBndsMdagM");
373  write(xml_out, "update_no", update_no);
374  write(xml_out, "xml_file", xml_file);
375  pop(xml_out);
376 
377  XMLFileWriter xml(xml_file);
378  func(update_no, xml);
379  }
380  else
381  {
382  func(update_no, xml_out);
383  }
384  }
385 
386 
387 
388  // The "do" function
389  void
390  InlineEigBndsMdagM::func(unsigned long update_no,
391  XMLWriter& xml_out)
392  {
393  START_CODE();
394 
395  QDPIO::cout << InlineEigBndsMdagMEnv::name << ": MdagM eigenvalue bounds" << std::endl;
396 
397  StopWatch snoop;
398  snoop.reset();
399  snoop.start();
400 
401  // Typedefs to save typing
402  typedef LatticeFermion T;
403  typedef multi1d<LatticeColorMatrix> P;
404  typedef multi1d<LatticeColorMatrix> Q;
405 
406  // Check success of the downcast
407  // Possible actions
408  FermAct4D<T,P,Q>* S_4d =
409  dynamic_cast< FermAct4D<T,P,Q>*>(fermact.operator->()); // get pointer
410 
411  // Possible actions
412  FermAct5D<T,P,Q>* S_5d =
413  dynamic_cast< FermAct5D<T,P,Q>*>(fermact.operator->()); // get pointer
414 
415 
416  multi1d<LatticeColorMatrix> u;
417  try
418  {
419  // Grab the object
420  u = TheNamedObjMap::Instance().getData< multi1d<LatticeColorMatrix> >(params.named_obj.gauge_id);
421  }
422  catch (std::bad_cast)
423  {
424  QDPIO::cerr << InlineEigBndsMdagMEnv::name << ": cast error"
425  << std::endl;
426  QDP_abort(1);
427  }
428  catch (const std::string& e)
429  {
430  QDPIO::cerr << InlineEigBndsMdagMEnv::name << ": error message: " << e
431  << std::endl;
432  QDP_abort(1);
433  }
434 
435 
436  Handle< FermState<T,P,Q> > connect_state(fermact->createState(u));
437 
438  if (S_4d != 0)
439  {
440  Handle< LinearOperator<LatticeFermion> > MM(S_4d->lMdagM(connect_state));
441  this->do4d(MM, update_no, xml_out);
442  }
443  else if (S_5d != 0)
444  {
445  if (! params.usePV)
446  {
447  //! Find evs of base operator
448  Handle< LinearOperatorArray<LatticeFermion> > MM(S_5d->lMdagM(connect_state));
449  this->do5d(MM, update_no, xml_out);
450  }
451  else
452  {
453  //! Find evs of PV operator
455  MM(new MdagMLinOpArray<LatticeFermion>(S_5d->linOpPV(connect_state)));
456 
457  this->do5d(MM, update_no, xml_out);
458  }
459  }
460  else
461  {
462  QDPIO::cerr << InlineEigBndsMdagMEnv::name << ": no suitable cast found" << std::endl;
463  QDP_abort(1);
464  }
465 
466  snoop.stop();
467  QDPIO::cout << InlineEigBndsMdagMEnv::name << ": total time = "
468  << snoop.getTimeInSeconds()
469  << " secs" << std::endl;
470 
471  QDPIO::cout << InlineEigBndsMdagMEnv::name << ": ran successfully" << std::endl;
472 
473  END_CODE();
474  }
475 
476 }
Inline measurement factory.
Base class for quadratic matter actions (e.g., fermions)
Definition: fermact.h:176
virtual LinearOperator< T > * lMdagM(Handle< FermState< T, P, Q > > state) const =0
Produce a linear operator M^dag.M for this action.
Base class of quadratic matter actions (e.g., fermions) living in an extra dimension.
Definition: fermact.h:265
Class for counted reference semantics.
Definition: handle.h:33
Inline measurement of eigenvalue bounds of M^dag*M.
InlineEigBndsMdagM(const InlineEigBndsMdagMParams &p)
void do4d(Handle< LinearOperator< LatticeFermion > > MM, unsigned long update_no, XMLWriter &xml_out)
4D
InlineEigBndsMdagMParams params
void do5d(Handle< LinearOperatorArray< LatticeFermion > > MM, unsigned long update_no, XMLWriter &xml_out)
5D
void func(const unsigned long update_no, XMLWriter &xml_out)
Do the measurement.
Handle< FermionAction< LatticeFermion, multi1d< LatticeColorMatrix >, multi1d< LatticeColorMatrix > > > fermact
void operator()(unsigned long update_no, XMLWriter &xml_out)
Do the measurement.
Linear Operator to arrays.
Definition: linearop.h:61
M^dag.M linear operator over arrays.
Definition: lmdagm.h:67
static T & Instance()
Definition: singleton.h:432
Partial specialization of scaled M operator over arrays.
Definition: lopscl.h:111
Scaled Linear Operator.
Definition: lopscl.h:22
Fermion action factories.
void EigSpecRitzCG(const LinearOperator< LatticeFermion > &M, multi1d< Real > &lambda_H, multi1d< LatticeFermion > &psi, int n_eig, int n_renorm, int n_min, int MaxCG, const Real &Rsd_r, const Real &Rsd_a, const Real &zero_cutoff, const bool ProjApsiP, int &n_cg_tot, XMLWriter &xml_out)
Compute low lying eigenvalues of the hermitian H.
Definition: eig_spec.cc:41
void read(XMLReader &xml, const std::string &path, AsqtadFermActParams &param)
Read parameters.
void write(XMLWriter &xml, const std::string &path, const AsqtadFermActParams &param)
Writer parameters.
std::string makeXMLFileName(std::string xml_file, unsigned long update_no)
Return a xml file name for inline measurements.
GroupXML_t readXMLGroup(XMLReader &xml_in, const std::string &path, const std::string &type_name)
Read group and return as a std::string.
Inline measurements for eigenvalue bounds.
Params params
unsigned n
Definition: ldumul_w.cc:36
Make xml file writer.
Named object function std::map.
static bool registered
Local registration flag.
multi1d< LatticeColorMatrix > P
bool registerAll()
Register all the factories.
Asqtad Staggered-Dirac operator.
Definition: klein_gord.cc:10
gaussian(aux)
static multi1d< LatticeColorMatrix > u
LinOpSysSolverMGProtoClover::Q Q
push(xml_out,"Condensates")
LinOpSysSolverMGProtoClover::T T
int i
Definition: pbg5p_w.cc:55
LatticeFermion psi
Definition: mespbg5p_w.cc:35
pop(xml_out)
void write(XMLWriter &xml, const std::string &path, const InlineEigBndsMdagMParams::NamedObject_t &input)
Object buffer.
START_CODE()
multi1d< LatticeFermion > s(Ncb)
::std::string string
Definition: gtest.h:1979
Struct for parameters needed for a Ritz type solve.
struct Chroma::InlineEigBndsMdagMParams::RitzParams_t ritz
struct Chroma::InlineEigBndsMdagMParams::NamedObject_t named_obj
void write(XMLWriter &xml_in, const std::string &path)