CHROMA
inline_propagator_w.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  namespace InlinePropagatorEnv
23  {
24  namespace
25  {
26  AbsInlineMeasurement* createMeasurement(XMLReader& xml_in,
27  const std::string& path)
28  {
29  return new InlinePropagator(InlinePropagatorParams(xml_in, path));
30  }
31 
32  //! Local registration flag
33  bool registered = false;
34  }
35 
36  const std::string name = "PROPAGATOR";
37 
38  //! Register all the factories
39  bool registerAll()
40  {
41  bool success = true;
42  if (! registered)
43  {
45  success &= TheInlineMeasurementFactory::Instance().registerObject(name, createMeasurement);
46  registered = true;
47  }
48  return success;
49  }
50  } // end namespace
51 
52 
53  //! Propagator input
54  void read(XMLReader& xml, const std::string& path, InlinePropagatorParams::NamedObject_t& input)
55  {
56  XMLReader inputtop(xml, path);
57 
58  read(inputtop, "gauge_id", input.gauge_id);
59  read(inputtop, "source_id", input.source_id);
60  read(inputtop, "prop_id", input.prop_id);
61  }
62 
63  //! Propagator output
64  void write(XMLWriter& xml, const std::string& path, const InlinePropagatorParams::NamedObject_t& input)
65  {
66  push(xml, path);
67 
68  write(xml, "gauge_id", input.gauge_id);
69  write(xml, "source_id", input.source_id);
70  write(xml, "prop_id", input.prop_id);
71 
72  pop(xml);
73  }
74 
75 
76  // Param stuff
78 
80  {
81  try
82  {
83  XMLReader paramtop(xml_in, path);
84 
85  if (paramtop.count("Frequency") == 1)
86  read(paramtop, "Frequency", frequency);
87  else
88  frequency = 1;
89 
90  // Parameters for source construction
91  read(paramtop, "Param", param);
92 
93  // Read in the output propagator/source configuration info
94  read(paramtop, "NamedObject", named_obj);
95 
96  // Possible alternate XML file pattern
97  if (paramtop.count("xml_file") != 0)
98  {
99  read(paramtop, "xml_file", xml_file);
100  }
101  }
102  catch(const std::string& e)
103  {
104  QDPIO::cerr << __func__ << ": Caught Exception reading XML: " << e << std::endl;
105  QDP_abort(1);
106  }
107  }
108 
109 
110  void
111  InlinePropagatorParams::writeXML(XMLWriter& xml_out, const std::string& path)
112  {
113  push(xml_out, path);
114 
115  write(xml_out, "Param", param);
116  write(xml_out, "NamedObject", named_obj);
117 
118  pop(xml_out);
119  }
120 
121 
122  // Function call
123  void
124  InlinePropagator::operator()(unsigned long update_no,
125  XMLWriter& xml_out)
126  {
127  // If xml file not empty, then use alternate
128  if (params.xml_file != "")
129  {
130  std::string xml_file = makeXMLFileName(params.xml_file, update_no);
131 
132  push(xml_out, "propagator");
133  write(xml_out, "update_no", update_no);
134  write(xml_out, "xml_file", xml_file);
135  pop(xml_out);
136 
137  XMLFileWriter xml(xml_file);
138  func(update_no, xml);
139  }
140  else
141  {
142  func(update_no, xml_out);
143  }
144  }
145 
146 
147  // Real work done here
148  void
149  InlinePropagator::func(unsigned long update_no,
150  XMLWriter& xml_out)
151  {
152  START_CODE();
153 
154  QDPIO::cout << InlinePropagatorEnv::name << ": propagator calculation" << std::endl;
155 
156  StopWatch snoop;
157  snoop.reset();
158  snoop.start();
159 
160  // Test and grab a reference to the gauge field
161  XMLBufferWriter gauge_xml;
162  try
163  {
164  TheNamedObjMap::Instance().getData< multi1d<LatticeColorMatrix> >(params.named_obj.gauge_id);
165  TheNamedObjMap::Instance().get(params.named_obj.gauge_id).getRecordXML(gauge_xml);
166  }
167  catch( std::bad_cast )
168  {
169  QDPIO::cerr << InlinePropagatorEnv::name << ": caught dynamic cast error"
170  << std::endl;
171  QDP_abort(1);
172  }
173  catch (const std::string& e)
174  {
175  QDPIO::cerr << InlinePropagatorEnv::name << ": std::map call failed: " << e
176  << std::endl;
177  QDP_abort(1);
178  }
179  const multi1d<LatticeColorMatrix>& u =
180  TheNamedObjMap::Instance().getData< multi1d<LatticeColorMatrix> >(params.named_obj.gauge_id);
181 
182  push(xml_out, "propagator");
183  write(xml_out, "update_no", update_no);
184 
185  proginfo(xml_out); // Print out basic program info
186 
187  // Write out the input
188  params.writeXML(xml_out, "Input");
189 
190  // Write out the config header
191  write(xml_out, "Config_info", gauge_xml);
192 
193  push(xml_out, "Output_version");
194  write(xml_out, "out_version", 1);
195  pop(xml_out);
196 
197  // Calculate some gauge invariant observables just for info.
198  MesPlq(xml_out, "Observables", u);
199 
200  //
201  // Read in the source along with relevant information.
202  //
203  XMLReader source_file_xml, source_record_xml;
204 
205  // These pesky variables are needed in the quarkprop call - only chiral dudes
206  // need this stuff, but it must be there for the bleeping virtual function
207  // to live at the base class
208  int t0;
209  int j_decay;
210 
211  // Record the type of header
212  bool make_sourceP = false;
213  bool seqsourceP = false;
214 
215  QDPIO::cout << "Snarf the source from a named buffer" << std::endl;
216  try
217  {
218  // Try the cast to see if this is a valid source
219  LatticePropagator& source_tmp =
220  TheNamedObjMap::Instance().getData<LatticePropagator>(params.named_obj.source_id);
221 
222  // Snarf the source info. This is will throw if the source_id is not there
223  TheNamedObjMap::Instance().get(params.named_obj.source_id).getFileXML(source_file_xml);
224  TheNamedObjMap::Instance().get(params.named_obj.source_id).getRecordXML(source_record_xml);
225 
226  // Try to invert this record XML into a source struct
227  // First identify what kind of source might be here
228  if (source_record_xml.count("/MakeSource") != 0)
229  {
230  make_sourceP = true;
231  MakeSourceProp_t orig_header;
232  read(source_record_xml, "/MakeSource", orig_header);
233 
234  j_decay = orig_header.source_header.j_decay;
235  t0 = orig_header.source_header.t_source;
236  }
237  else if (source_record_xml.count("/SequentialSource") != 0)
238  {
239  seqsourceP = true;
240  SequentialSource_t orig_header;
241  read(source_record_xml, "/SequentialSource", orig_header);
242 
243  j_decay = orig_header.seqsource_header.j_decay;
244  t0 = orig_header.seqsource_header.t_sink; // funky, but probably not really needed
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 << InlinePropagatorEnv::name << ": caught dynamic cast error"
258  << std::endl;
259  QDP_abort(1);
260  }
261  catch (const std::string& e)
262  {
263  QDPIO::cerr << InlinePropagatorEnv::name << ": error extracting source_header: " << e << std::endl;
264  QDP_abort(1);
265  }
266 
267  // Should be a valid cast now
268  const LatticePropagator& quark_prop_source =
269  TheNamedObjMap::Instance().getData<LatticePropagator>(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<LatticePropagator>(params.named_obj.prop_id);
296  }
297  catch (std::bad_cast)
298  {
299  QDPIO::cerr << InlinePropagatorEnv::name << ": caught dynamic cast error"
300  << std::endl;
301  QDP_abort(1);
302  }
303  catch (const std::string& e)
304  {
305  QDPIO::cerr << InlinePropagatorEnv::name << ": error creating prop: " << e << std::endl;
306  QDP_abort(1);
307  }
308 
309  // Cast should be valid now
310  LatticePropagator& quark_propagator =
311  TheNamedObjMap::Instance().getData<LatticePropagator>(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 LatticeFermion T;
337  typedef multi1d<LatticeColorMatrix> P;
338  typedef multi1d<LatticeColorMatrix> Q;
339 
340  // Generic Wilson-Type stuff
343  fermacttop,
345 
346  Handle< FermState<T,P,Q> > state(S_f->createState(u));
347 
348  QDPIO::cout << "Suitable factory found: compute the quark prop" << std::endl;
349  swatch.start();
350 
351  QDPIO::cout << "Calling quarkProp" << std::endl;
352  S_f->quarkProp(quark_propagator,
353  xml_out,
354  quark_prop_source,
355  t0, j_decay,
356  state,
360  ncg_had);
361  swatch.stop();
362  QDPIO::cout << "Propagator computed: time= "
363  << swatch.getTimeInSeconds()
364  << " secs" << std::endl;
365 
366  success = true;
367  }
368  catch (const std::string& e)
369  {
370  QDPIO::cout << InlinePropagatorEnv::name << ": caught exception around quarkprop: " << e << std::endl;
371  }
372  }
373 
374 
375  if (! success)
376  {
377  QDPIO::cerr << "Error: no fermact found" << std::endl;
378  QDP_abort(1);
379  }
380 
381 
382  push(xml_out,"Relaxation_Iterations");
383  write(xml_out, "ncg_had", ncg_had);
384  pop(xml_out);
385 
386  // Sanity check - write out the propagator (pion) correlator in the Nd-1 direction
387  {
388  // Initialize the slow Fourier transform phases
389  SftMom phases(0, true, Nd-1);
390 
391  multi1d<Double> prop_corr = sumMulti(localNorm2(quark_propagator),
392  phases.getSet());
393 
394  push(xml_out, "Prop_correlator");
395  write(xml_out, "prop_corr", prop_corr);
396  pop(xml_out);
397  }
398 
399 
400  // Save the propagator info
401  try
402  {
403  QDPIO::cout << "Start writing propagator info" << std::endl;
404 
405  XMLBufferWriter file_xml;
406  push(file_xml, "propagator");
407  write(file_xml, "id", uniqueId()); // NOTE: new ID form
408  pop(file_xml);
409 
410  XMLBufferWriter record_xml;
411  if (make_sourceP)
412  {
413  MakeSourceProp_t orig_header;
414  read(source_record_xml, "/MakeSource", orig_header);
415 
416  Propagator_t new_header; // note, abandoning state_info
417  new_header.prop_header = params.param;
418  new_header.source_header = orig_header.source_header;
419  new_header.gauge_header = orig_header.gauge_header;
420  write(record_xml, "Propagator", new_header);
421  }
422  else if (seqsourceP)
423  {
424  SequentialSource_t orig_header;
425  read(source_record_xml, "/SequentialSource", orig_header);
426 
427  SequentialProp_t new_header; // note, abandoning state_info
428  new_header.seqprop_header = params.param;
429  new_header.sink_header = orig_header.sink_header;
430  new_header.seqsource_header = orig_header.seqsource_header;
431  new_header.forward_props = orig_header.forward_props;
432  new_header.gauge_header = orig_header.gauge_header;
433  write(record_xml, "SequentialProp", new_header);
434  }
435 
436  // Write the propagator xml info
437  TheNamedObjMap::Instance().get(params.named_obj.prop_id).setFileXML(file_xml);
438  TheNamedObjMap::Instance().get(params.named_obj.prop_id).setRecordXML(record_xml);
439 
440  QDPIO::cout << "Propagator successfully updated" << std::endl;
441  }
442  catch (std::bad_cast)
443  {
444  QDPIO::cerr << InlinePropagatorEnv::name << ": caught dynamic cast error"
445  << std::endl;
446  QDP_abort(1);
447  }
448  catch (const std::string& e)
449  {
450  QDPIO::cerr << InlinePropagatorEnv::name << ": error extracting prop_header: " << e << std::endl;
451  QDP_abort(1);
452  }
453 
454  pop(xml_out); // propagator
455 
456  snoop.stop();
457  QDPIO::cout << InlinePropagatorEnv::name << ": total time = "
458  << snoop.getTimeInSeconds()
459  << " secs" << std::endl;
460 
461  QDPIO::cout << InlinePropagatorEnv::name << ": ran successfully" << std::endl;
462 
463  END_CODE();
464  }
465 
466 }
Inline measurement factory.
Class for counted reference semantics.
Definition: handle.h:33
Inline propagator calculation.
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.
InlinePropagatorParams params
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 Wilson-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.
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.
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::InlinePropagatorParams::NamedObject_t named_obj
Mega structure holding a propagator source.
Definition: qprop_io.h:103
std::string gauge_header
Definition: qprop_io.h:105
PropSourceConst_t source_header
Definition: qprop_io.h:104
Mega structure holding a full forward prop (not sink smeared)
Definition: qprop_io.h:111
ChromaProp_t prop_header
Definition: qprop_io.h:112
std::string gauge_header
Definition: qprop_io.h:114
PropSourceConst_t source_header
Definition: qprop_io.h:113
Mega structure holding a full sequential prop.
Definition: qprop_io.h:140
ChromaProp_t seqprop_header
Definition: qprop_io.h:141
PropSinkSmear_t sink_header
Definition: qprop_io.h:142
multi1d< ForwardProp_t > forward_props
Definition: qprop_io.h:144
std::string gauge_header
Definition: qprop_io.h:145
SeqSource_t seqsource_header
Definition: qprop_io.h:143
Mega structure holding a full sequential source.
Definition: qprop_io.h:130
PropSinkSmear_t sink_header
Definition: qprop_io.h:131
multi1d< ForwardProp_t > forward_props
Definition: qprop_io.h:133
SeqSource_t seqsource_header
Definition: qprop_io.h:132
std::string gauge_header
Definition: qprop_io.h:134
Generate a unique id.