CHROMA
inline_propagator_s.cc
Go to the documentation of this file.
1 /*! \file
2  * \brief Inline construction of propagator
3  *
4  * Propagator calculations
5  */
6 
7 #include "fermact.h"
10 #include "meas/glue/mesplq.h"
11 #include "util/ft/sftmom.h"
12 #include "util/info/proginfo.h"
13 #include "util/info/unique_id.h"
17 
19 
20 namespace Chroma
21 {
22  //! Propagator input
23  void read(XMLReader& xml, const std::string& path, InlineStaggeredPropagatorEnv::Params::NamedObject_t& input)
24  {
25  XMLReader inputtop(xml, path);
26 
27  read(inputtop, "gauge_id", input.gauge_id);
28  read(inputtop, "source_id", input.source_id);
29  read(inputtop, "prop_id", input.prop_id);
30  }
31 
32  //! Propagator output
33  void write(XMLWriter& xml, const std::string& path, const InlineStaggeredPropagatorEnv::Params::NamedObject_t& input)
34  {
35  push(xml, path);
36 
37  write(xml, "gauge_id", input.gauge_id);
38  write(xml, "source_id", input.source_id);
39  write(xml, "prop_id", input.prop_id);
40 
41  pop(xml);
42  }
43 
44 
45  namespace InlineStaggeredPropagatorEnv
46  {
47  namespace
48  {
49  AbsInlineMeasurement* createMeasurement(XMLReader& xml_in,
50  const std::string& path)
51  {
52  return new InlineMeas(Params(xml_in, path));
53  }
54 
55  //! Local registration flag
56  bool registered = false;
57  }
58 
59  const std::string name = "PROPAGATOR_STAG";
60 
61  //! Register all the factories
62  bool registerAll()
63  {
64  bool success = true;
65  if (! registered)
66  {
68  success &= TheInlineMeasurementFactory::Instance().registerObject(name, createMeasurement);
69  registered = true;
70  }
71  return success;
72  }
73 
74 
75  // Param stuff
77 
78  Params::Params(XMLReader& xml_in, const std::string& path)
79  {
80  try
81  {
82  XMLReader paramtop(xml_in, path);
83 
84  if (paramtop.count("Frequency") == 1)
85  read(paramtop, "Frequency", frequency);
86  else
87  frequency = 1;
88 
89  // Parameters for source construction
90  read(paramtop, "Param", param);
91 
92  // Read in the output propagator/source configuration info
93  read(paramtop, "NamedObject", named_obj);
94 
95  // Possible alternate XML file pattern
96  if (paramtop.count("xml_file") != 0)
97  {
98  read(paramtop, "xml_file", xml_file);
99  }
100  }
101  catch(const std::string& e)
102  {
103  QDPIO::cerr << __func__ << ": Caught Exception reading XML: " << e << std::endl;
104  QDP_abort(1);
105  }
106  }
107 
108 
109  void
110  Params::writeXML(XMLWriter& xml_out, const std::string& path)
111  {
112  push(xml_out, path);
113 
114  write(xml_out, "Param", param);
115  write(xml_out, "NamedObject", named_obj);
116 
117  pop(xml_out);
118  }
119 
120 
121  // Function call
122  void
123  InlineMeas::operator()(unsigned long update_no,
124  XMLWriter& xml_out)
125  {
126  // If xml file not empty, then use alternate
127  if (params.xml_file != "")
128  {
129  std::string xml_file = makeXMLFileName(params.xml_file, update_no);
130 
131  push(xml_out, "propagator_stag");
132  write(xml_out, "update_no", update_no);
133  write(xml_out, "xml_file", xml_file);
134  pop(xml_out);
135 
136  XMLFileWriter xml(xml_file);
137  func(update_no, xml);
138  }
139  else
140  {
141  func(update_no, xml_out);
142  }
143  }
144 
145 
146  // Real work done here
147  void
148  InlineMeas::func(unsigned long update_no,
149  XMLWriter& xml_out)
150  {
151  START_CODE();
152 
153  StopWatch snoop;
154  snoop.reset();
155  snoop.start();
156 
157  // Test and grab a reference to the gauge field
158  XMLBufferWriter gauge_xml;
159  try
160  {
161  TheNamedObjMap::Instance().getData< multi1d<LatticeColorMatrix> >(params.named_obj.gauge_id);
162  TheNamedObjMap::Instance().get(params.named_obj.gauge_id).getRecordXML(gauge_xml);
163  }
164  catch( std::bad_cast )
165  {
166  QDPIO::cerr << name << ": caught dynamic cast error"
167  << std::endl;
168  QDP_abort(1);
169  }
170  catch (const std::string& e)
171  {
172  QDPIO::cerr << name << ": std::map call failed: " << e
173  << std::endl;
174  QDP_abort(1);
175  }
176  const multi1d<LatticeColorMatrix>& u =
177  TheNamedObjMap::Instance().getData< multi1d<LatticeColorMatrix> >(params.named_obj.gauge_id);
178 
179  push(xml_out, "propagator_stag");
180  write(xml_out, "update_no", update_no);
181 
182  QDPIO::cout << name << ": propagator calculation" << std::endl;
183 
184  proginfo(xml_out); // Print out basic program info
185 
186  // Write out the input
187  params.writeXML(xml_out, "Input");
188 
189  // Write out the config header
190  write(xml_out, "Config_info", gauge_xml);
191 
192  push(xml_out, "Output_version");
193  write(xml_out, "out_version", 1);
194  pop(xml_out);
195 
196  // Calculate some gauge invariant observables just for info.
197  MesPlq(xml_out, "Observables", u);
198 
199  //
200  // Read in the source along with relevant information.
201  //
202  XMLReader source_file_xml, source_record_xml;
203 
204  int t0;
205  int j_decay;
206  bool make_sourceP = false;
207  bool seqsourceP = false;
208  QDPIO::cout << "Snarf the source from a named buffer" << std::endl;
209  try
210  {
211  // Try the cast to see if this is a valid source
212  LatticeStaggeredPropagator& source_tmp =
213  TheNamedObjMap::Instance().getData<LatticeStaggeredPropagator>(params.named_obj.source_id);
214 
215  // Snarf the source info. This is will throw if the source_id is not there
216  TheNamedObjMap::Instance().get(params.named_obj.source_id).getFileXML(source_file_xml);
217  TheNamedObjMap::Instance().get(params.named_obj.source_id).getRecordXML(source_record_xml);
218 
219  // Try to invert this record XML into a source struct
220  // First identify what kind of source might be here
221  if (source_record_xml.count("/MakeSource") != 0)
222  {
223  PropSourceConst_t source_header;
224 
225  read(source_record_xml, "/MakeSource/PropSource", source_header);
226  j_decay = source_header.j_decay;
227  t0 = source_header.t_source;
228  make_sourceP = true;
229  }
230  else if (source_record_xml.count("/SequentialSource") != 0)
231  {
233  PropSourceConst_t source_header;
234  SeqSource_t seqsource_header;
235 
236  read(source_record_xml, "/SequentialSource/SeqSource", seqsource_header);
237  // Any source header will do for j_decay
238  read(source_record_xml, "/SequentialSource/ForwardProps/elem[1]/ForwardProp",
239  prop_header);
240  read(source_record_xml, "/SequentialSource/ForwardProps/elem[1]/PropSource",
241  source_header);
242  j_decay = source_header.j_decay;
243  t0 = seqsource_header.t_sink;
244  seqsourceP = true;
245  }
246  else
247  {
248  throw std::string("No appropriate header found");
249  }
250 
251  // Write out the source header
252  write(xml_out, "Source_file_info", source_file_xml);
253  write(xml_out, "Source_record_info", source_record_xml);
254  }
255  catch (std::bad_cast)
256  {
257  QDPIO::cerr << name << ": caught dynamic cast error"
258  << std::endl;
259  QDP_abort(1);
260  }
261  catch (const std::string& e)
262  {
263  QDPIO::cerr << name << ": error extracting source_header: " << e << std::endl;
264  QDP_abort(1);
265  }
266 
267  // Should be a valid cast now
268  const LatticeStaggeredPropagator& quark_prop_source =
269  TheNamedObjMap::Instance().getData<LatticeStaggeredPropagator>(params.named_obj.source_id);
270 
271  QDPIO::cout << "Source successfully read and parsed" << std::endl;
272 
273  // Sanity check - write out the norm2 of the source in the Nd-1 direction
274  // Use this for any possible verification
275  {
276  // Initialize the slow Fourier transform phases
277  SftMom phases(0, true, Nd-1);
278 
279  multi1d<Double> source_corr = sumMulti(localNorm2(quark_prop_source),
280  phases.getSet());
281 
282  push(xml_out, "Source_correlator");
283  write(xml_out, "source_corr", source_corr);
284  pop(xml_out);
285  }
286 
287  //
288  // Loop over the source color and spin, creating the source
289  // and calling the relevant propagator routines. The QDP
290  // terminology is that a propagator is a matrix in color
291  // and spin space
292  //
293  try
294  {
295  TheNamedObjMap::Instance().create<LatticeStaggeredPropagator>(params.named_obj.prop_id);
296  }
297  catch (std::bad_cast)
298  {
299  QDPIO::cerr << name << ": caught dynamic cast error"
300  << std::endl;
301  QDP_abort(1);
302  }
303  catch (const std::string& e)
304  {
305  QDPIO::cerr << name << ": error creating prop: " << e << std::endl;
306  QDP_abort(1);
307  }
308 
309  // Cast should be valid now
310  LatticeStaggeredPropagator& quark_propagator =
311  TheNamedObjMap::Instance().getData<LatticeStaggeredPropagator>(params.named_obj.prop_id);
312  int ncg_had = 0;
313 
314  //
315  // Initialize fermion action
316  //
317  std::istringstream xml_s(params.param.fermact.xml);
318  XMLReader fermacttop(xml_s);
319  QDPIO::cout << "FermAct = " << params.param.fermact.id << std::endl;
320 
321 
322  //
323  // Try the factories
324  //
325  bool success = false;
326 
327  if (! success)
328  {
329  try
330  {
331  StopWatch swatch;
332  swatch.reset();
333  QDPIO::cout << "Try the various factories" << std::endl;
334 
335  // Typedefs to save typing
336  typedef LatticeStaggeredFermion T;
337  typedef multi1d<LatticeColorMatrix> P;
338  typedef multi1d<LatticeColorMatrix> Q;
339 
340  // Generic Wilson-Type stuff
343  fermacttop,
345 
346 
347  Handle< FermState<T,P,Q> > state(S_f->createState(u));
348 
349  QDPIO::cout << "Suitable factory found: compute the quark prop" << std::endl;
350  swatch.start();
351  S_f->quarkProp(quark_propagator,
352  xml_out,
353  quark_prop_source,
354  state,
357  ncg_had);
358  swatch.stop();
359  QDPIO::cout << "Propagator computed: time= "
360  << swatch.getTimeInSeconds()
361  << " secs" << std::endl;
362 
363  success = true;
364  }
365  catch (const std::string& e)
366  {
367  QDPIO::cout << name << ": caught exception around quarkprop: " << e << std::endl;
368  }
369  }
370 
371 
372  if (! success)
373  {
374  QDPIO::cerr << "Error: no fermact found" << std::endl;
375  QDP_abort(1);
376  }
377 
378 
379  push(xml_out,"Relaxation_Iterations");
380  write(xml_out, "ncg_had", ncg_had);
381  pop(xml_out);
382 
383  // Sanity check - write out the propagator (pion) correlator in the Nd-1 direction
384  {
385  // Initialize the slow Fourier transform phases
386  SftMom phases(0, true, Nd-1);
387 
388  multi1d<Double> prop_corr = sumMulti(localNorm2(quark_propagator),
389  phases.getSet());
390 
391  push(xml_out, "Prop_correlator");
392  write(xml_out, "prop_corr", prop_corr);
393  pop(xml_out);
394  }
395 
396 
397  // Save the propagator info
398  try
399  {
400  QDPIO::cout << "Start writing propagator info" << std::endl;
401 
402  XMLBufferWriter file_xml;
403  push(file_xml, "propagator");
404  write(file_xml, "id", uniqueId()); // NOTE: new ID form
405  pop(file_xml);
406 
407  XMLBufferWriter record_xml;
408  if (make_sourceP)
409  {
410  XMLReader xml_tmp(source_record_xml, "/MakeSource");
411 
412  push(record_xml, "Propagator");
413  write(record_xml, "ForwardProp", params.param);
414  record_xml << xml_tmp; // write out all the stuff under MakeSource
415  pop(record_xml);
416  }
417  else if (seqsourceP)
418  {
419  XMLReader xml_tmp(source_record_xml, "/SequentialSource");
420 
421  push(record_xml, "SequentialProp");
422  write(record_xml, "SeqProp", params.param);
423  record_xml << xml_tmp; // write out all the stuff under SequentialSource
424  pop(record_xml);
425  }
426 
427  // Write the propagator xml info
428  TheNamedObjMap::Instance().get(params.named_obj.prop_id).setFileXML(file_xml);
429  TheNamedObjMap::Instance().get(params.named_obj.prop_id).setRecordXML(record_xml);
430 
431  QDPIO::cout << "Propagator successfully updated" << std::endl;
432  }
433  catch (std::bad_cast)
434  {
435  QDPIO::cerr << name << ": caught dynamic cast error"
436  << std::endl;
437  QDP_abort(1);
438  }
439  catch (const std::string& e)
440  {
441  QDPIO::cerr << name << ": error extracting prop_header: " << e << std::endl;
442  QDP_abort(1);
443  }
444 
445  pop(xml_out); // propagator
446 
447  snoop.stop();
448  QDPIO::cout << name << ": total time = "
449  << snoop.getTimeInSeconds()
450  << " secs" << std::endl;
451 
452  QDPIO::cout << name << ": ran successfully" << std::endl;
453 
454  END_CODE();
455  }
456 
457  }
458 }
Inline measurement factory.
Class for counted reference semantics.
Definition: handle.h:33
void operator()(const unsigned long update_no, XMLWriter &xml_out)
Do the measurement.
void func(const unsigned long update_no, XMLWriter &xml_out)
Do the measurement.
Fourier transform phase factor support.
Definition: sftmom.h:35
const Set & getSet() const
The set to be used in sumMulti.
Definition: sftmom.h:57
static T & Instance()
Definition: singleton.h:432
Class structure for fermion actions.
Fermion action factories.
All Staggered-type fermion actions.
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 uniqueId()
Return a unique id.
Definition: unique_id.cc:18
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.
ForwardProp_t prop_header
Inline construction of propagator.
Make xml file writer.
int j_decay
Definition: meslate.cc:22
Nd
Definition: meslate.cc:74
Named object function std::map.
static bool registered
Local registration flag.
multi1d< LatticeColorMatrix > P
bool registerAll()
Register all the factories.
bool registerAll()
Register all the factories.
Asqtad Staggered-Dirac operator.
Definition: klein_gord.cc:10
static multi1d< LatticeColorMatrix > u
LinOpSysSolverMGProtoClover::Q Q
push(xml_out,"Condensates")
LinOpSysSolverMGProtoClover::T T
pop(xml_out)
void MesPlq(const multi1d< LatticeColorMatrixF3 > &u, multi2d< Double > &plane_plaq, Double &link)
Definition: mesplq.cc:83
START_CODE()
const WilsonTypeFermAct< multi1d< LatticeFermion > > Handle< const ConnectState > state
Definition: pbg5p_w.cc:28
const WilsonTypeFermAct< multi1d< LatticeFermion > > & S_f
Definition: pbg5p_w.cc:27
::std::string string
Definition: gtest.h:1979
Print out basic info about this program.
Fourier transform phase factor support.
Propagator parameters.
Definition: qprop_io.h:75
GroupXML_t invParam
Definition: qprop_io.h:84
GroupXML_t fermact
Definition: qprop_io.h:80
QuarkSpinType quarkSpinType
Definition: qprop_io.h:77
void writeXML(XMLWriter &xml_out, const std::string &path)
struct Chroma::InlineStaggeredPropagatorEnv::Params::NamedObject_t named_obj
Propagator source construction parameters.
Definition: qprop_io.h:27
Sequential source parameters.
Definition: qprop_io.h:90
Generate a unique id.