CHROMA
inline_meson_matelem_colorvec_w.cc
Go to the documentation of this file.
1 /*! \file
2  * \brief Inline measurement of meson operators via colorstd::vector matrix elements
3  */
4 
5 
6 
7 #include "handle.h"
12 #include "meas/smear/displace.h"
13 #include "meas/glue/mesplq.h"
15 #include "util/ferm/key_val_db.h"
16 #include "util/ft/sftmom.h"
17 #include "util/info/proginfo.h"
19 
21 
22 #define COLORVEC_MATELEM_TYPE_ZERO 0
23 #define COLORVEC_MATELEM_TYPE_ONE 1
24 #define COLORVEC_MATELEM_TYPE_MONE -1
25 #define COLORVEC_MATELEM_TYPE_GENERIC 10
26 
27 namespace Chroma
28 {
29  /*!
30  * \ingroup hadron
31  *
32  * @{
33  */
34  namespace InlineMesonMatElemColorVecEnv
35  {
36  // Reader for input parameters
37  void read(XMLReader& xml, const std::string& path, InlineMesonMatElemColorVecEnv::Params::Param_t& param)
38  {
39  XMLReader paramtop(xml, path);
40 
41  int version;
42  read(paramtop, "version", version);
43 
44  switch (version)
45  {
46  case 1:
47  /**************************************************************************/
48  param.mom2_min = 0;
49  param.mom_list.resize(0);
50  break;
51 
52  case 2:
53  /**************************************************************************/
54  read(paramtop, "mom2_min", param.mom2_min);
55  param.mom_list.resize(0);
56  break;
57 
58  case 3:
59  /**************************************************************************/
60  read(paramtop, "mom2_min", param.mom2_min);
61  read(paramtop, "mom_list", param.mom_list);
62  break;
63 
64  default :
65  /**************************************************************************/
66 
67  QDPIO::cerr << "Input parameter version " << version << " unsupported." << std::endl;
68  QDP_abort(1);
69  }
70 
71  read(paramtop, "mom2_max", param.mom2_max);
72  read(paramtop, "displacement_length", param.displacement_length);
73  read(paramtop, "displacement_list", param.displacement_list);
74  read(paramtop, "num_vecs", param.num_vecs);
75  read(paramtop, "decay_dir", param.decay_dir);
76  read(paramtop, "orthog_basis", param.orthog_basis);
77 
78  param.link_smearing = readXMLGroup(paramtop, "LinkSmearing", "LinkSmearingType");
79  }
80 
81 
82  // Writer for input parameters
83  void write(XMLWriter& xml, const std::string& path, const InlineMesonMatElemColorVecEnv::Params::Param_t& param)
84  {
85  push(xml, path);
86 
87  int version = 3;
88 
89  write(xml, "version", version);
90  write(xml, "mom2_min", param.mom2_min);
91  write(xml, "mom2_max", param.mom2_max);
92  write(xml, "mom_list", param.mom_list);
93  write(xml, "displacement_length", param.displacement_length);
94  write(xml, "displacement_list", param.displacement_list);
95  write(xml, "num_vecs", param.num_vecs);
96  write(xml, "decay_dir", param.decay_dir);
97  write(xml, "orthog_basis", param.orthog_basis);
98  xml << param.link_smearing.xml;
99 
100  pop(xml);
101  }
102 
103  //! Read named objects
105  {
106  XMLReader inputtop(xml, path);
107 
108  read(inputtop, "gauge_id", input.gauge_id);
109  read(inputtop, "colorvec_id", input.colorvec_id);
110  read(inputtop, "meson_op_file", input.meson_op_file);
111  }
112 
113  //! Write named objects
114  void write(XMLWriter& xml, const std::string& path, const InlineMesonMatElemColorVecEnv::Params::NamedObject_t& input)
115  {
116  push(xml, path);
117 
118  write(xml, "gauge_id", input.gauge_id);
119  write(xml, "colorvec_id", input.colorvec_id);
120  write(xml, "meson_op_file", input.meson_op_file);
121 
122  pop(xml);
123  }
124 
125  // Writer for input parameters
126  void write(XMLWriter& xml, const std::string& path, const InlineMesonMatElemColorVecEnv::Params& param)
127  {
128  param.writeXML(xml, path);
129  }
130  }
131 
132 
133  namespace InlineMesonMatElemColorVecEnv
134  {
135  // Anonymous namespace for registration
136  namespace
137  {
138  AbsInlineMeasurement* createMeasurement(XMLReader& xml_in,
139  const std::string& path)
140  {
141  return new InlineMeas(Params(xml_in, path));
142  }
143 
144  //! Local registration flag
145  bool registered = false;
146  }
147 
148  const std::string name = "MESON_MATELEM_COLORVEC";
149 
150  //! Register all the factories
151  bool registerAll()
152  {
153  bool success = true;
154  if (! registered)
155  {
156  success &= LinkSmearingEnv::registerAll();
157  success &= TheInlineMeasurementFactory::Instance().registerObject(name, createMeasurement);
158  registered = true;
159  }
160  return success;
161  }
162 
163 
164  //! Anonymous namespace
165  /*! Diagnostic stuff */
166  namespace
167  {
168  StandardOutputStream& operator<<(StandardOutputStream& os, const multi1d<int>& d)
169  {
170  if (d.size() > 0)
171  {
172  os << d[0];
173 
174  for(int i=1; i < d.size(); ++i)
175  os << " " << d[i];
176  }
177 
178  return os;
179  }
180  }
181 
182 
183  //----------------------------------------------------------------------------
184  // Param stuff
186  {
187  frequency = 0;
188  param.mom2_min = 0;
189  param.mom2_max = 0;
190  param.mom_list.resize(0);
191  }
192 
193  Params::Params(XMLReader& xml_in, const std::string& path)
194  {
195  try
196  {
197  XMLReader paramtop(xml_in, path);
198 
199  if (paramtop.count("Frequency") == 1)
200  read(paramtop, "Frequency", frequency);
201  else
202  frequency = 1;
203 
204  // Read program parameters
205  read(paramtop, "Param", param);
206 
207  // Read in the output propagator/source configuration info
208  read(paramtop, "NamedObject", named_obj);
209 
210  // Possible alternate XML file pattern
211  if (paramtop.count("xml_file") != 0)
212  {
213  read(paramtop, "xml_file", xml_file);
214  }
215  }
216  catch(const std::string& e)
217  {
218  QDPIO::cerr << __func__ << ": Caught Exception reading XML: " << e << std::endl;
219  QDP_abort(1);
220  }
221  }
222 
223 
224  void
225  Params::writeXML(XMLWriter& xml_out, const std::string& path) const
226  {
227  push(xml_out, path);
228 
229  // Parameters for source construction
230  write(xml_out, "Param", param);
231 
232  // Write out the output propagator/source configuration info
233  write(xml_out, "NamedObject", named_obj);
234 
235  pop(xml_out);
236  }
237 
238 
239  //----------------------------------------------------------------------------
240  //! Meson operator
242  {
243  int t_slice; /*!< Meson operator time slice */
244  multi1d<int> displacement; /*!< Displacement dirs of right colorstd::vector */
245  multi1d<int> mom; /*!< D-1 momentum of this operator */
246  };
247 
248  //! Meson operator
250  {
251  int type_of_data; /*!< Flag indicating type of data (maybe trivial) */
252  multi2d<ComplexD> op; /*!< Colorstd::vector source and sink with momentum projection */
253  };
254 
255 
256  //----------------------------------------------------------------------------
257  //! Holds key and value as temporaries
259  {
262  };
263 
264 
265  //----------------------------------------------------------------------------
266  //! KeyMesonElementalOperator reader
267  void read(BinaryReader& bin, KeyMesonElementalOperator_t& param)
268  {
269  read(bin, param.t_slice);
270  read(bin, param.displacement);
271  read(bin, param.mom);
272  }
273 
274  //! MesonElementalOperator write
275  void write(BinaryWriter& bin, const KeyMesonElementalOperator_t& param)
276  {
277  write(bin, param.t_slice);
278  write(bin, param.displacement);
279  write(bin, param.mom);
280  }
281 
282  //! MesonElementalOperator reader
283  void read(XMLReader& xml, const std::string& path, KeyMesonElementalOperator_t& param)
284  {
285  XMLReader paramtop(xml, path);
286 
287  read(paramtop, "t_slice", param.t_slice);
288  read(paramtop, "displacement", param.displacement);
289  read(paramtop, "mom", param.mom);
290  }
291 
292  //! MesonElementalOperator writer
293  void write(XMLWriter& xml, const std::string& path, const KeyMesonElementalOperator_t& param)
294  {
295  push(xml, path);
296 
297  write(xml, "t_slice", param.t_slice);
298  write(xml, "displacement", param.displacement);
299  write(xml, "mom", param.mom);
300 
301  pop(xml);
302  }
303 
304 
305  //----------------------------------------------------------------------------
306  //! MesonElementalOperator reader
307  void read(BinaryReader& bin, ValMesonElementalOperator_t& param)
308  {
309  read(bin, param.type_of_data);
310 
311  int n;
312  read(bin, n); // the size is always written, even if 0
313  param.op.resize(n,n);
314 
315  for(int i=0; i < n; ++i)
316  {
317  for(int j=0; j < n; ++j)
318  {
319  read(bin, param.op(i,j));
320  }
321  }
322  }
323 
324  //! MesonElementalOperator write
325  void write(BinaryWriter& bin, const ValMesonElementalOperator_t& param)
326  {
327  write(bin, param.type_of_data);
328 
329  int n = param.op.size1(); // all sizes the same
330  write(bin, n);
331  for(int i=0; i < n; ++i)
332  {
333  for(int j=0; j < n; ++j)
334  {
335  write(bin, param.op(i,j));
336  }
337  }
338  }
339 
340 
341  //----------------------------------------------------------------------------
342  //! Normalize just one displacement array
343  multi1d<int> normDisp(const multi1d<int>& orig)
344  {
345  START_CODE();
346 
347  multi1d<int> disp;
348  multi1d<int> empty;
349  multi1d<int> no_disp(1); no_disp[0] = 0;
350 
351  // NOTE: a no-displacement is recorded as a zero-length array
352  // Convert a length one array with no displacement into a no-displacement array
353  if (orig.size() == 1)
354  {
355  if (orig == no_disp)
356  disp = empty;
357  else
358  disp = orig;
359  }
360  else
361  {
362  disp = orig;
363  }
364 
365  END_CODE();
366 
367  return disp;
368  } // void normDisp
369 
370 
371  //-------------------------------------------------------------------------------
372  // Function call
373  void
374  InlineMeas::operator()(unsigned long update_no,
375  XMLWriter& xml_out)
376  {
377  // If xml file not empty, then use alternate
378  if (params.xml_file != "")
379  {
380  std::string xml_file = makeXMLFileName(params.xml_file, update_no);
381 
382  push(xml_out, "MesonMatElemColorVec");
383  write(xml_out, "update_no", update_no);
384  write(xml_out, "xml_file", xml_file);
385  pop(xml_out);
386 
387  XMLFileWriter xml(xml_file);
388  func(update_no, xml);
389  }
390  else
391  {
392  func(update_no, xml_out);
393  }
394  }
395 
396 
397  // Function call
398  void
399  InlineMeas::func(unsigned long update_no,
400  XMLWriter& xml_out)
401  {
402  START_CODE();
403 
404  StopWatch snoop;
405  snoop.reset();
406  snoop.start();
407 
408 
409  StopWatch swiss;
410 
411  // Test and grab a reference to the gauge field
412  XMLBufferWriter gauge_xml;
413  try
414  {
415  TheNamedObjMap::Instance().getData< multi1d<LatticeColorMatrix> >(params.named_obj.gauge_id);
416  TheNamedObjMap::Instance().get(params.named_obj.gauge_id).getRecordXML(gauge_xml);
417 
418  // NB We are just checking this is here.
420  }
421  catch( std::bad_cast )
422  {
423  QDPIO::cerr << name << ": caught dynamic cast error" << std::endl;
424  QDP_abort(1);
425  }
426  catch (const std::string& e)
427  {
428  QDPIO::cerr << name << ": std::map call failed: " << e << std::endl;
429  QDP_abort(1);
430  }
431 
432  // Cast should be valid now
433  const multi1d<LatticeColorMatrix>& u =
434  TheNamedObjMap::Instance().getData< multi1d<LatticeColorMatrix> >(params.named_obj.gauge_id);
435 
436  const MapObject<int,EVPair<LatticeColorVector> >& eigen_source =
438 
439  // Sanity check
440  if (params.param.num_vecs > eigen_source.size())
441  {
442  QDPIO::cerr << name << ": number of available eigenvectors is too small\n";
443  QDP_abort(1);
444  }
445 
446  push(xml_out, "MesonMatElemColorVec");
447  write(xml_out, "update_no", update_no);
448 
449  QDPIO::cout << name << ": Meson color-std::vector matrix element" << std::endl;
450 
451  proginfo(xml_out); // Print out basic program info
452 
453  // Write out the input
454  params.writeXML(xml_out, "Input");
455 
456  // Write out the config info
457  write(xml_out, "Config_info", gauge_xml);
458 
459  push(xml_out, "Output_version");
460  write(xml_out, "out_version", 1);
461  pop(xml_out);
462 
463  //First calculate some gauge invariant observables just for info.
464  //This is really cheap.
465  MesPlq(xml_out, "Observables", u);
466 
467  //
468  // Initialize the slow Fourier transform phases
469  //
470  SftMom phases(params.param.mom2_max, false, params.param.decay_dir);
471 
472  //
473  // If a list of momenta has been specified only need phases corresponding to these
474  //
475  if (params.param.mom_list.size() > 0)
476  {
477  int num_mom = params.param.mom_list.size();
478  int mom_size = params.param.mom_list[0].size();
479  multi2d<int> moms(num_mom,mom_size);
480  for(int i = 0; i < num_mom; i++)
481  {
482  moms[i] = params.param.mom_list[i];
483  }
484  SftMom temp_phases(moms, params.param.decay_dir);
485  phases = temp_phases;
486  params.param.mom2_min = 0;
487  }
488 
489  //
490  // Smear the gauge field if needed
491  //
492  multi1d<LatticeColorMatrix> u_smr = u;
493 
494  try
495  {
496  std::istringstream xml_l(params.param.link_smearing.xml);
497  XMLReader linktop(xml_l);
498  QDPIO::cout << "Link smearing type = " << params.param.link_smearing.id << std::endl;
499 
500 
502  linkSmearing(TheLinkSmearingFactory::Instance().createObject(params.param.link_smearing.id,
503  linktop,
505 
506  (*linkSmearing)(u_smr);
507  }
508  catch(const std::string& e)
509  {
510  QDPIO::cerr << name << ": Caught Exception link smearing: " << e << std::endl;
511  QDP_abort(1);
512  }
513 
514  // Record the smeared observables
515  MesPlq(xml_out, "Smeared_Observables", u_smr);
516 
517  //
518  // DB storage
519  //
520  BinaryStoreDB< SerialDBKey<KeyMesonElementalOperator_t>, SerialDBData<ValMesonElementalOperator_t> >
521  qdp_db;
522 
523  // Open the file, and write the meta-data and the binary for this operator
524  if (! qdp_db.fileExists(params.named_obj.meson_op_file))
525  {
526  XMLBufferWriter file_xml;
527 
528  push(file_xml, "DBMetaData");
529  write(file_xml, "id", std::string("mesonElemOp"));
530  write(file_xml, "lattSize", QDP::Layout::lattSize());
531 // write(file_xml, "blockSize", params.param.block_size);
532  write(file_xml, "decay_dir", params.param.decay_dir);
533  proginfo(file_xml); // Print out basic program info
534  write(file_xml, "Params", params.param);
535  write(file_xml, "Op_Info", params.param.displacement_list);
536  write(file_xml, "Config_info", gauge_xml);
537  write(file_xml, "Weights", getEigenValues(eigen_source, params.param.num_vecs));
538  pop(file_xml);
539 
540  std::string file_str(file_xml.str());
541  qdp_db.setMaxUserInfoLen(file_str.size());
542 
543  qdp_db.open(params.named_obj.meson_op_file, O_RDWR | O_CREAT, 0664);
544 
545  qdp_db.insertUserdata(file_str);
546  }
547  else
548  {
549  qdp_db.open(params.named_obj.meson_op_file, O_RDWR, 0664);
550  }
551 
552 
553  // Keep track of no displacements and zero momentum
554  multi1d<int> no_displacement;
555  multi1d<int> zero_mom(3); zero_mom = 0;
556 
557  //
558  // Meson operators
559  //
560  // The creation and annihilation operators are the same without the
561  // spin matrices.
562  //
563  QDPIO::cout << "Building meson operators" << std::endl;
564 
565  push(xml_out, "ElementalOps");
566 
567 
568  // Loop over all time slices for the source. This is the same
569  // as the subsets for phases
570 
571  // Loop over each operator
572  for(int l=0; l < params.param.displacement_list.size(); ++l)
573  {
574  StopWatch watch;
575 
576  QDPIO::cout << "Elemental operator: op = " << l << std::endl;
577 
578  // Make sure displacement is something sensible
579  multi1d<int> disp = normDisp(params.param.displacement_list[l]);
580 
581  QDPIO::cout << "displacement = " << disp << std::endl;
582 
583  // Build the operator
584  swiss.reset();
585  swiss.start();
586 
587  // Big loop over the momentum projection
588  for(int mom_num = 0 ; mom_num < phases.numMom() ; ++mom_num)
589  {
590  if ( norm2(phases.numToMom(mom_num)) < params.param.mom2_min ) continue;
591 
592  // The keys for the spin and displacements for this particular elemental operator
593  // No displacement for left colorstd::vector, only displace right colorstd::vector
594  // Invert the time - make it an independent key
595  multi1d<KeyValMesonElementalOperator_t> buf(phases.numSubsets());
596  for(int t=0; t < phases.numSubsets(); ++t)
597  {
598  buf[t].key.key().t_slice = t;
599  buf[t].key.key().mom = phases.numToMom(mom_num);
600  buf[t].key.key().displacement = disp; // only right colorstd::vector
601  buf[t].val.data().op.resize(params.param.num_vecs,params.param.num_vecs);
602 
603  if ( params.param.orthog_basis &&
604  (phases.numToMom(mom_num)) == zero_mom &&
605  (disp == no_displacement) )
606  {
607  buf[t].val.data().type_of_data = COLORVEC_MATELEM_TYPE_ONE;
608  }
609  else
610  {
611  buf[t].val.data().type_of_data = COLORVEC_MATELEM_TYPE_GENERIC;
612  }
613  }
614 
615  for(int j = 0 ; j < params.param.num_vecs; ++j)
616  {
617  // Displace the right std::vector and multiply by the momentum phase
618  EVPair<LatticeColorVector> tmpvec; eigen_source.get(j,tmpvec);
619  LatticeColorVector shift_vec = phases[mom_num] * displace(u_smr,
620  tmpvec.eigenVector,
622  disp);
623 
624  for(int i = 0 ; i < params.param.num_vecs; ++i)
625  {
626  watch.reset();
627  watch.start();
628 
629  // Contract over color indices
630  // Do the relevant quark contraction
631  EVPair<LatticeColorVector> tmpvec; eigen_source.get(i,tmpvec);
632  LatticeComplex lop = localInnerProduct(tmpvec.eigenVector, shift_vec);
633 
634  // Slow fourier-transform
635  multi1d<ComplexD> op_sum = sumMulti(lop, phases.getSet());
636 
637  watch.stop();
638 
639  for(int t=0; t < op_sum.size(); ++t)
640  {
641  buf[t].val.data().op(i,j) = op_sum[t];
642  }
643 
644 // write(xml_out, "elem", key.key()); // debugging
645  } // end for j
646  } // end for i
647 
648  QDPIO::cout << "insert: mom= " << phases.numToMom(mom_num) << " displacement= " << disp << std::endl;
649  for(int t=0; t < phases.numSubsets(); ++t)
650  {
651  qdp_db.insert(buf[t].key, buf[t].val);
652  }
653 
654  } // mom_num
655 
656  swiss.stop();
657 
658  QDPIO::cout << "Meson operator= " << l
659  << " time= "
660  << swiss.getTimeInSeconds()
661  << " secs" << std::endl;
662 
663  } // for l
664 
665  pop(xml_out); // ElementalOps
666 
667  // Close the namelist output file XMLDAT
668  pop(xml_out); // MesonMatElemColorVector
669 
670  snoop.stop();
671  QDPIO::cout << name << ": total time = "
672  << snoop.getTimeInSeconds()
673  << " secs" << std::endl;
674 
675  QDPIO::cout << name << ": ran successfully" << std::endl;
676 
677  END_CODE();
678  } // func
679  } // namespace InlineMesonMatElemColorVecEnv
680 
681  /*! @} */ // end of group hadron
682 
683 } // namespace Chroma
684 
685 
Inline measurement factory.
Class for counted reference semantics.
Definition: handle.h:33
void func(const unsigned long update_no, XMLWriter &xml_out)
Do the measurement.
void operator()(const unsigned long update_no, XMLWriter &xml_out)
Do the measurement.
Serializable value harness.
Definition: key_val_db.h:69
Serializable key harness.
Definition: key_val_db.h:21
Fourier transform phase factor support.
Definition: sftmom.h:35
int numSubsets() const
Number of subsets - length in decay direction.
Definition: sftmom.h:63
multi1d< int > numToMom(int mom_num) const
Convert momenta id to actual array of momenta.
Definition: sftmom.h:78
int numMom() const
Number of momenta.
Definition: sftmom.h:60
const Set & getSet() const
The set to be used in sumMulti.
Definition: sftmom.h:57
static T & Instance()
Definition: singleton.h:432
Parallel transport a lattice field.
void proginfo(XMLWriter &xml)
Print out basic information about this program.
Definition: proginfo.cc:24
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.
T displace(const multi1d< LatticeColorMatrix > &u, const T &psi, int length, int dir, const Subset &sub)
Apply a displacement operator to a lattice field.
Definition: displace.cc:41
Class for counted reference semantics.
MODS_t & eigen_source
Eigenvectors.
#define COLORVEC_MATELEM_TYPE_GENERIC
#define COLORVEC_MATELEM_TYPE_ONE
Inline measurement of meson operators via colorstd::vector matrix elements.
Key and values for DB.
unsigned j
Definition: ldumul_w.cc:35
unsigned n
Definition: ldumul_w.cc:36
Make xml file writer.
int t
Definition: meslate.cc:37
Named object function std::map.
static bool registered
Local registration flag.
multi1d< int > normDisp(const multi1d< int > &orig)
Normalize just one displacement array.
void write(XMLWriter &xml, const std::string &path, const InlineMesonMatElemColorVecEnv::Params::Param_t &param)
void read(XMLReader &xml, const std::string &path, InlineMesonMatElemColorVecEnv::Params::Param_t &param)
QDPSubTypeTrait< typename BinaryReturn< C1, C2, FnLocalInnerProduct >::Type_t >::Type_t localInnerProduct(const QDPSubType< T1, C1 > &l, const QDPType< T2, C2 > &r)
bool registerAll()
Register all the factories.
Asqtad Staggered-Dirac operator.
Definition: klein_gord.cc:10
QDP::StandardOutputStream & operator<<(QDP::StandardOutputStream &s, const multi1d< int > &d)
Definition: npr_vertex_w.cc:12
static multi1d< LatticeColorMatrix > u
push(xml_out,"Condensates")
int i
Definition: pbg5p_w.cc:55
multi1d< SubsetVectorWeight_t > getEigenValues(const MapObject< int, EVPair< LatticeColorVector > > &eigen_source, int num_vecs)
pop(xml_out)
void MesPlq(const multi1d< LatticeColorMatrixF3 > &u, multi2d< Double > &plane_plaq, Double &link)
Definition: mesplq.cc:83
DComplex d
Definition: invbicg.cc:99
START_CODE()
::std::string string
Definition: gtest.h:1979
int l
Definition: pade_trln_w.cc:111
Print out basic info about this program.
Fourier transform phase factor support.
A Pair type.
void writeXML(XMLWriter &xml_out, const std::string &path) const
Holds of vectors and weights.