CHROMA
inline_hadron_contract.cc
Go to the documentation of this file.
1 /*! \file
2  * \brief Inline hadron contraction calculations - for correlators
3  *
4  * Hadron spectrum calculations. The general version that write output
5  * into lime files.
6  */
7 
8 #include "handle.h"
14 #include "meas/glue/mesplq.h"
15 #include "util/info/unique_id.h"
16 #include "util/info/proginfo.h"
19 
20 namespace Chroma
21 {
22  // Now for all the actual work
23  namespace InlineHadronContractEnv
24  {
25  // The callback stuff
26  namespace
27  {
28  AbsInlineMeasurement* createMeasurement(XMLReader& xml_in,
29  const std::string& path)
30  {
31  return new InlineMeas(Params(xml_in, path));
32  }
33 
34  //! Local registration flag
35  bool registered = false;
36  }
37 
38  const std::string name = "HADRON_CONTRACT";
39 
40  //! Register all the factories
41  bool registerAll()
42  {
43  bool success = true;
44  if (! registered)
45  {
46  // Make sure all hadron contract function objects are registered
47  success &= HadronContractEnv::registerAll();
48 
49  // Register this object
50  success &= TheInlineMeasurementFactory::Instance().registerObject(name, createMeasurement);
51  registered = true;
52  }
53  return success;
54  }
55 
56 
57  //! Propagator input
58  void read(XMLReader& xml, const std::string& path, Params::NamedObject_t& input)
59  {
60  XMLReader inputtop(xml, path);
61 
62  read(inputtop, "gauge_id", input.gauge_id);
63  read(inputtop, "output_file", input.output_file);
64  QDPIO::cout << "Read contraction list" << std::endl;
65  input.correlators = readXMLArrayGroup(inputtop, "Contractions", "ContractionType");
66  QDPIO::cout << "Finished reading correlators" << std::endl;
67  }
68 
69  //! Propagator output
70  void write(XMLWriter& xml, const std::string& path, const Params::NamedObject_t& input)
71  {
72  push(xml, path);
73 
74  write(xml, "gauge_id", input.gauge_id);
75  write(xml, "output_file", input.output_file);
76 
77  for(int i=0; i < input.correlators.size(); ++i)
78  {
79  xml << input.correlators[i].xml;
80  }
81 
82  pop(xml);
83  }
84 
85 
86  // Param stuff
88  {
89  frequency = 0;
90  }
91 
92  Params::Params(XMLReader& xml_in, const std::string& path)
93  {
94  try
95  {
96  XMLReader paramtop(xml_in, path);
97 
98  if (paramtop.count("Frequency") == 1)
99  read(paramtop, "Frequency", frequency);
100  else
101  frequency = 1;
102 
103  // Read in the output propagator/source configuration info
104  read(paramtop, "NamedObject", named_obj);
105 
106  // Possible alternate XML file pattern
107  if (paramtop.count("xml_file") != 0)
108  {
109  read(paramtop, "xml_file", xml_file);
110  }
111  }
112  catch(const std::string& e)
113  {
114  QDPIO::cerr << __func__ << ": Caught Exception reading XML: " << e << std::endl;
115  QDP_abort(1);
116  }
117  }
118 
119 
120  void
121  Params::writeXML(XMLWriter& xml_out, const std::string& path)
122  {
123  push(xml_out, path);
124 
125  write(xml_out, "NamedObject", named_obj);
126  write(xml_out, "xml_file", xml_file);
127 
128  pop(xml_out);
129  }
130 
131 
132 
133 
134 
135  // Function call
136  void
137  InlineMeas::operator()(unsigned long update_no,
138  XMLWriter& xml_out)
139  {
140  // If xml file not empty, then use alternate
141  if (params.xml_file != "")
142  {
143  std::string xml_file = makeXMLFileName(params.xml_file, update_no);
144 
145  push(xml_out, "HadronContract");
146  write(xml_out, "update_no", update_no);
147  write(xml_out, "xml_file", xml_file);
148  pop(xml_out);
149 
150  XMLFileWriter xml(xml_file);
151  func(update_no, xml);
152  }
153  else
154  {
155  func(update_no, xml_out);
156  }
157  }
158 
159 
160  // Real work done here
161  void
162  InlineMeas::func(unsigned long update_no,
163  XMLWriter& xml_out)
164  {
165  START_CODE();
166 
167  StopWatch snoop;
168  snoop.reset();
169  snoop.start();
170 
171  // Test and grab a reference to the gauge field
172  XMLBufferWriter gauge_xml;
173  try
174  {
175  TheNamedObjMap::Instance().getData< multi1d<LatticeColorMatrix> >(params.named_obj.gauge_id);
176  TheNamedObjMap::Instance().get(params.named_obj.gauge_id).getRecordXML(gauge_xml);
177  }
178  catch( std::bad_cast )
179  {
180  QDPIO::cerr << InlineHadronContractEnv::name << ": caught dynamic cast error"
181  << std::endl;
182  QDP_abort(1);
183  }
184  catch (const std::string& e)
185  {
186  QDPIO::cerr << InlineHadronContractEnv::name << ": std::map call failed: " << e
187  << std::endl;
188  QDP_abort(1);
189  }
190  const multi1d<LatticeColorMatrix>& u =
191  TheNamedObjMap::Instance().getData< multi1d<LatticeColorMatrix> >(params.named_obj.gauge_id);
192 
193  push(xml_out, "HadronContract");
194  write(xml_out, "update_no", update_no);
195 
196  QDPIO::cout << InlineHadronContractEnv::name
197  << ": hadron contracts and spectroscopy for any type of fermion"
198  << std::endl;
199  QDPIO::cout << " Volume: " << Layout::lattSize()[0];
200  for (int i=1; i<Nd; ++i) {
201  QDPIO::cout << " x " << Layout::lattSize()[i];
202  }
203  QDPIO::cout << std::endl;
204 
205  proginfo(xml_out); // Print out basic program info
206 
207  // Write out the input
208  params.writeXML(xml_out, "Input");
209 
210  // Write out the config info
211  write(xml_out, "Config_info", gauge_xml);
212 
213  push(xml_out, "Output_version");
214  write(xml_out, "out_version", 1);
215  pop(xml_out);
216 
217  // Start the data file output
218  XMLBufferWriter file_xml;
219  push(file_xml, "HadronContract");
220  write(file_xml, "id", uniqueId()); // NOTE: new ID form
221  pop(file_xml);
222 
223  // Write the scalar data
224  QDPFileWriter qio_output(file_xml, params.named_obj.output_file,
225  QDPIO_SINGLEFILE, QDPIO_SERIAL, QDPIO_OPEN);
226 
227  // First calculate some gauge invariant observables just for info.
228  MesPlq(xml_out, "Observables", u);
229 
230  // Keep an array of all the xml output buffers
231  push(xml_out, "HadronMeasurements");
232 
233  // Now loop over the various fermion pairs
234  for(int lcorr=0; lcorr < params.named_obj.correlators.size(); ++lcorr)
235  {
236  const GroupXML_t& had_xml = params.named_obj.correlators[lcorr];
237 
238  push(xml_out, "elem");
239  write(xml_out, "Input", had_xml.xml);
240 
241  // Factory construction
242  try
243  {
244 // QDPIO::cout << "xml input = XX" << had_xml.xml << "XX" << std::endl;
245  QDPIO::cout << "Contractions for id = " << had_xml.id << std::endl;
246 
247  // Create and use the hadron 2pt object
248  std::istringstream xml_s(had_xml.xml);
249  XMLReader hadtop(xml_s);
250 
251  Handle<HadronContract> hadronContract(
252  TheHadronContractFactory::Instance().createObject(
253  had_xml.id,
254  hadtop,
255  had_xml.path));
256 
257  // Compute possibly several correlators
258  QDPIO::cout << InlineHadronContractEnv::name << ": start list" << std::endl;
259 
260  std::list< Handle<HadronContractResult_t> > hadron_cont =
261  (*hadronContract)(u, "HadronContraction", "ContractionType");
262 
263  QDPIO::cout << InlineHadronContractEnv::name << ": finished list" << std::endl;
264 
265  push(xml_out, "HadronContractions");
266 
267  // Run over the output list
268  for(std::list< Handle<HadronContractResult_t> >::const_iterator had_ptr= hadron_cont.begin();
269  had_ptr != hadron_cont.end();
270  ++had_ptr)
271  {
272  const Handle<HadronContractResult_t>& had_cont = *had_ptr;
273 
274  push(xml_out, "elem");
275 
276  // Save regression output in output file
277  // NOTE: what is under the "Diagnostic" tag is solely up
278  // to the user. You can jam whatever you want into
279  // here. It is used for the regressions to latch onto
280  // something from the output since otherwise it is all in binary.
281  // The input is written as well to give some context
282  write(xml_out, "RecordXML", had_cont->xml);
283  write(xml_out, "Diagnostic", had_cont->xml_regres);
284 
285  // Save the qio output file entry
286  write(qio_output, had_cont->xml, had_cont->bin);
287 
288  pop(xml_out); // array element
289  }
290 
291  pop(xml_out); // HadronContractions
292  }
293  catch(const std::string& e)
294  {
295  QDPIO::cerr << InlineHadronContractEnv::name << ": Caught Exception in HadronCorrelator: "
296  << e << std::endl;
297  QDP_abort(1);
298  }
299 
300  pop(xml_out); // array element
301  }
302  pop(xml_out); // HadronMeasurements
303  pop(xml_out); // HadronContract
304 
305  // Close data file
306  close(qio_output);
307 
308  snoop.stop();
309  QDPIO::cout << InlineHadronContractEnv::name << ": total time = "
310  << snoop.getTimeInSeconds()
311  << " secs" << std::endl;
312 
313  QDPIO::cout << InlineHadronContractEnv::name << ": ran successfully" << std::endl;
314 
315  END_CODE();
316  } // func
317 
318  } // namespace InlineHadronContractEnv
319 
320 } // namespace Chroma
Inline measurement factory.
Class for counted reference semantics.
Definition: handle.h:33
Inline measurement of hadron contraction functions.
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.
static T & Instance()
Definition: singleton.h:432
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.
multi1d< GroupXML_t > readXMLArrayGroup(XMLReader &xml_in, const std::string &path, const std::string &type_name)
Read group and return as a std::string.
Construct hadron correlators.
All hadron contraction constructors.
Factory for producing hadron correlator objects.
Class for counted reference semantics.
Inline hadron contractions - for correlators.
Make xml file writer.
Nd
Definition: meslate.cc:74
Named object function std::map.
static bool registered
Local registration flag.
bool registerAll()
Register all the factories.
bool registerAll()
Register all the factories.
void write(XMLWriter &xml, const std::string &path, const Params::NamedObject_t &input)
Propagator output.
void read(XMLReader &xml, const std::string &path, Params::NamedObject_t &input)
Propagator input.
Asqtad Staggered-Dirac operator.
Definition: klein_gord.cc:10
static multi1d< LatticeColorMatrix > u
push(xml_out,"Condensates")
int i
Definition: pbg5p_w.cc:55
pop(xml_out)
void MesPlq(const multi1d< LatticeColorMatrixF3 > &u, multi2d< Double > &plane_plaq, Double &link)
Definition: mesplq.cc:83
START_CODE()
::std::string string
Definition: gtest.h:1979
Print out basic info about this program.
Hold group xml and type id.
void writeXML(XMLWriter &xml_out, const std::string &path)
Generate a unique id.