QMCPACK
WaveFunctionTester.cpp
Go to the documentation of this file.
1 //////////////////////////////////////////////////////////////////////////////////////
2 // This file is distributed under the University of Illinois/NCSA Open Source License.
3 // See LICENSE file in top directory for details.
4 //
5 // Copyright (c) 2016 Jeongnim Kim and QMCPACK developers.
6 //
7 // File developed by: Ken Esler, kpesler@gmail.com, University of Illinois at Urbana-Champaign
8 // Miguel Morales, moralessilva2@llnl.gov, Lawrence Livermore National Laboratory
9 // Jeremy McMinnis, jmcminis@gmail.com, University of Illinois at Urbana-Champaign
10 // Jeongnim Kim, jeongnim.kim@gmail.com, University of Illinois at Urbana-Champaign
11 // Mark Dewing, markdewing@gmail.com, University of Illinois at Urbana-Champaign
12 // Mark A. Berrill, berrillma@ornl.gov, Oak Ridge National Laboratory
13 //
14 // File created by: Jeongnim Kim, jeongnim.kim@gmail.com, University of Illinois at Urbana-Champaign
15 //////////////////////////////////////////////////////////////////////////////////////
16 
17 
21 #include "Message/Communicate.h"
22 #include "WaveFunctionTester.h"
24 #include "LongRange/StructFact.h"
25 #include "OhmmsData/AttributeSet.h"
26 #include "OhmmsData/ParameterSet.h"
32 #include <array>
33 #include <sstream>
34 
35 namespace qmcplusplus
36 {
38 
41  TrialWaveFunction& psi,
42  QMCHamiltonian& h,
43  ParticleSetPool& ptclPool,
45  : QMCDriver(project_data, w, psi, h, comm, "WaveFunctionTester"),
46  PtclPool(ptclPool),
47  checkRatio("no"),
48  checkClone("no"),
49  checkHamPbyP("no"),
50  wftricks("no"),
51  checkEloc("no"),
52  checkBasic("yes"),
53  checkRatioV("no"),
54  deltaParam(0.0),
55  toleranceParam(0.0),
56  outputDeltaVsError(false),
57  checkSlaterDet(true),
58  ndim(w.getLattice().ndim)
59 {
60  m_param.add(checkRatio, "ratio");
61  m_param.add(checkClone, "clone");
62  m_param.add(checkHamPbyP, "hamiltonianpbyp");
63  m_param.add(sourceName, "source");
64  m_param.add(wftricks, "orbitalutility");
65  m_param.add(checkEloc, "printEloc");
66  m_param.add(checkBasic, "basic");
67  m_param.add(checkRatioV, "virtual_move");
68  m_param.add(deltaParam, "delta");
69  m_param.add(toleranceParam, "tolerance");
71 
72  deltaR.resize(w.getTotalNum());
74  if (ndim < 3)
75  {
76  app_log() << "WF test in " << ndim << "D" << std::endl;
77  for (int iat = 0; iat < deltaR.size(); ++iat)
78  deltaR[iat][2] = 0;
79  }
80 }
81 
83 
84 /*!
85  * \brief Test the evaluation of the wavefunction, gradient and laplacian
86  by comparing to the numerical evaluation.
87  *
88  Use the finite difference formulas formulas
89  \f[
90  \nabla_i f({\bf R}) = \frac{f({\bf R+\Delta r_i}) - f({\bf R})}{2\Delta r_i}
91  \f]
92  and
93  \f[
94  \nabla_i^2 f({\bf R}) = \sum_{x,y,z} \frac{f({\bf R}+\Delta x_i)
95  - 2 f({\bf R}) + f({\bf R}-\Delta x_i)}{2\Delta x_i^2},
96  \f]
97  where \f$ f = \ln \Psi \f$ and \f$ \Delta r_i \f$ is a
98  small displacement for the ith particle.
99 */
100 
102 {
103  std::array<char, 16> fname;
104  if (std::snprintf(fname.data(), fname.size(), "wftest.%03d", OHMMS::Controller->rank()) < 0)
105  throw std::runtime_error("Error generating filename");
106  fout.open(fname.data());
107  fout.precision(15);
108 
109  app_log() << "Starting a Wavefunction tester. Additional information in " << fname.data() << std::endl;
110 
111  put(qmcNode);
112 
113  auto Rng1 = std::make_unique<RandomGenerator>();
114  H.setRandomGenerator(Rng1.get());
115  // Add to Rng so the object is eventually deleted
116  Rng.emplace_back(std::move(Rng1));
117 
118  if (checkSlaterDetOption == "no")
119  checkSlaterDet = false;
120  if (checkRatio == "yes")
121  {
122  //runRatioTest();
123  runRatioTest2();
124  }
125  else if (checkClone == "yes")
126  runCloneTest();
127  else if (checkEloc != "no")
128  printEloc();
129  else if (sourceName.size() != 0)
130  {
132  // runZeroVarianceTest();
133  }
134  else if (checkRatio == "deriv")
135  {
137  if (ndim < 3)
138  {
139  for (int iat = 0; iat < deltaR.size(); ++iat)
140  deltaR[iat][2] = 0;
141  }
142  deltaR *= 0.2;
143  runDerivTest();
145  }
146  else if (checkRatio == "derivclone")
148  else if (wftricks == "rotate")
149  throw std::runtime_error("orbitalutility \"rotation\" has been removed.");
150  else if (wftricks == "plot")
151  runNodePlot();
152  else if (checkBasic == "yes")
153  runBasicTest();
154  else if (checkRatioV == "yes")
155  runRatioV();
156  else
157  app_log() << "No wavefunction test specified" << std::endl;
158 
159  //RealType ene = H.evaluate(W);
160  //app_log() << " Energy " << ene << std::endl;
161  return true;
162 }
163 
165 {
166  app_log() << " ===== runCloneTest =====\n";
167  for (int iter = 0; iter < 4; ++iter)
168  {
169  app_log() << "Clone" << iter << std::endl;
170  auto w_clone = std::make_unique<MCWalkerConfiguration>(W);
171  auto psi_clone = Psi.makeClone(*w_clone);
172  auto h_clone = H.makeClone(*w_clone, *psi_clone);
173  h_clone->setPrimary(false);
174  int nat = W.getTotalNum();
175  MCWalkerConfiguration::PropertyContainer_t Properties(0, 0, 1, WP::MAXPROPERTIES);
176  //pick the first walker
177  MCWalkerConfiguration::Walker_t& awalker = **W.begin();
178  //copy the properties of the working walker
179  Properties = awalker.Properties;
180  W.R = awalker.R;
181  W.update();
182  ValueType logpsi1 = Psi.evaluateLog(W);
183  RealType eloc1 = H.evaluate(W);
184  w_clone->R = awalker.R;
185  w_clone->update();
186  ValueType logpsi2 = psi_clone->evaluateLog(*w_clone);
187  RealType eloc2 = h_clone->evaluate(*w_clone);
188  app_log() << "Testing walker-by-walker functions " << std::endl;
189  app_log() << "log (original) = " << logpsi1 << " energy = " << eloc1 << std::endl;
190  app_log() << "log (clone) = " << logpsi2 << " energy = " << eloc2 << std::endl;
191  app_log() << "Testing pbyp functions " << std::endl;
192  Walker_t::WFBuffer_t& wbuffer(awalker.DataSet);
193  wbuffer.clear();
194  app_log() << " Walker Buffer State current=" << wbuffer.current() << " size=" << wbuffer.size() << std::endl;
195  Psi.registerData(W, wbuffer);
196  wbuffer.allocate();
197  Psi.copyFromBuffer(W, wbuffer);
198  Psi.evaluateLog(W);
199  logpsi1 = Psi.updateBuffer(W, wbuffer, false);
200  eloc1 = H.evaluate(W);
201  app_log() << " Walker Buffer State current=" << wbuffer.current() << " size=" << wbuffer.size() << std::endl;
202  wbuffer.clear();
203  app_log() << " Walker Buffer State current=" << wbuffer.current() << " size=" << wbuffer.size() << std::endl;
204  psi_clone->registerData(W, wbuffer);
205  wbuffer.allocate();
206  Psi.copyFromBuffer(W, wbuffer);
207  Psi.evaluateLog(W);
208  logpsi2 = Psi.updateBuffer(W, wbuffer, false);
209  eloc2 = H.evaluate(*w_clone);
210  app_log() << " Walker Buffer State current=" << wbuffer.current() << " size=" << wbuffer.size() << std::endl;
211  app_log() << "log (original) = " << logpsi1 << " energy = " << eloc1 << std::endl;
212  app_log() << "log (clone) = " << logpsi2 << " energy = " << eloc2 << std::endl;
213  }
214 }
215 
217 {
218  app_log() << " ===== printEloc =====\n";
219  for (auto& [key, value] : PtclPool.getPool())
220  app_log() << "ParticelSet = " << key << std::endl;
221  // Find source ParticleSet
222  auto pit(PtclPool.getPool().find(sourceName));
223  if (pit == PtclPool.getPool().end())
224  APP_ABORT("Unknown source \"" + sourceName + "\" in printEloc in WaveFunctionTester.");
225  ParticleSet& source = *((*pit).second);
226  app_log() << "Source = " << sourceName << " " << (*pit).first << std::endl;
227  int nel = W.getTotalNum();
228  int ncenter = source.getTotalNum();
229  // int ncenter = 3;
230  // std::cout <<"number of centers: " <<source.getTotalNum() << std::endl;
231  // std::cout <<"0: " <<source.R[0] << std::endl;
232  // std::cout <<"1: " <<source.R[1] << std::endl;
233  // std::cout <<"2: " <<source.R[2] << std::endl;
234  MCWalkerConfiguration::PropertyContainer_t Properties(0, 0, 1, WP::MAXPROPERTIES);
235  ;
236  //pick the first walker
237  const MCWalkerConfiguration::Walker_t& awalker = **W.begin();
238  //copy the properties of the working walker
239  Properties = awalker.Properties;
240  W.R = awalker.R;
241  W.update();
242  //ValueType psi = Psi.evaluate(W);
243  ValueType logpsi = Psi.evaluateLog(W);
244  RealType eloc = H.evaluate(W);
245  app_log() << " Logpsi: " << logpsi << std::endl;
246  app_log() << " HamTest "
247  << " Total " << eloc << std::endl;
248  for (int i = 0; i < H.sizeOfObservables(); i++)
249  app_log() << " HamTest " << H.getObservableName(i) << " " << H.getObservable(i) << std::endl;
250  //RealType psi = Psi.evaluateLog(W);
251  //int iat=0;
252  double maxR = 1000000.0;
253  std::vector<int> closestElectron(ncenter);
254  for (int iat = 0; iat < ncenter; iat++)
255  {
256  maxR = 10000000;
257  for (int k = 0; k < nel; k++)
258  {
259  double dx = std::sqrt((W.R[k][0] - source.R[iat][0]) * (W.R[k][0] - source.R[iat][0]) +
260  (W.R[k][1] - source.R[iat][1]) * (W.R[k][1] - source.R[iat][1]) +
261  (W.R[k][2] - source.R[iat][2]) * (W.R[k][2] - source.R[iat][2]));
262  if (dx < maxR)
263  {
264  maxR = dx;
265  closestElectron[iat] = k;
266  }
267  }
268  }
269  // closestElectron[iat]=1;
270  std::ofstream out("eloc.dat");
271  double x, dx = 1.0 / 499.0;
272  for (int k = 0; k < 500; k++)
273  {
274  x = -0.5 + k * dx;
275  out << x << " ";
276  for (int iat = 0; iat < ncenter; iat++)
277  {
278  PosType tempR = W.R[closestElectron[iat]];
279  W.R[closestElectron[iat]] = source.R[iat];
280  // W.R[closestElectron[iat]]=0.0;
281  W.R[closestElectron[iat]][0] += x;
282  W.update();
283  Psi.evaluateLog(W);
284  ValueType ene = H.evaluate(W);
285  out << ene << " ";
286  W.R[closestElectron[iat]] = source.R[iat];
287  // W.R[closestElectron[iat]]=0.0;
288  W.R[closestElectron[iat]][1] += x;
289  W.update();
290  Psi.evaluateLog(W);
291  ene = H.evaluate(W);
292  out << ene << " ";
293  W.R[closestElectron[iat]] = source.R[iat];
294  // W.R[closestElectron[iat]]=0.0;
295  W.R[closestElectron[iat]][2] += x;
296  W.update();
297  Psi.evaluateLog(W);
298  ene = H.evaluate(W);
299  out << ene << " ";
300  W.R[closestElectron[iat]] = tempR;
301  }
302  out << std::endl;
303  }
304  out.close();
305 }
306 
307 
309 {
310 public:
312  {
313  FiniteDiff_LowOrder, // use simplest low-order formulas
314  FiniteDiff_Richardson // use Richardson extrapolation
315  };
317  : ndim(ndim_in), m_RichardsonSize(10), m_fd_type(fd_type)
318  {}
319 
320  const size_t ndim;
322 
324 
326  {
327  int index; // particle index
329  };
330  using PosChangeVector = std::vector<PositionChange>;
331  using ValueVector = std::vector<ValueType>;
332 
333 
334  /** Generate points to evaluate */
336 
337  /** Compute finite difference after log psi is computed for each point */
338  void computeFiniteDiff(RealType delta,
339  PosChangeVector& positions,
340  ValueVector& values,
343 
345  PosChangeVector& positions,
346  ValueVector& values,
349 
351  PosChangeVector& positions,
352  ValueVector& values,
355 };
356 
357 
359 {
360  // First position is the central point
361  PositionChange p;
362  p.index = 0;
363  p.r = W.R[0];
364  positions.push_back(p);
365 
366  int nat = W.getTotalNum();
367  for (int iat = 0; iat < nat; iat++)
368  {
369  PositionChange p;
370  p.index = iat;
371  PosType r0 = W.R[iat];
372 
373  for (int idim = 0; idim < ndim; idim++)
374  {
375  p.r = r0;
376  p.r[idim] = r0[idim] - delta;
377  positions.push_back(p);
378 
379  p.r = r0;
380  p.r[idim] = r0[idim] + delta;
381  positions.push_back(p);
382 
384  {
385  RealType dd = delta / 2;
386  for (int nr = 0; nr < m_RichardsonSize; nr++)
387  {
388  p.r = r0;
389  p.r[idim] = r0[idim] - dd;
390  positions.push_back(p);
391 
392  p.r = r0;
393  p.r[idim] = r0[idim] + dd;
394  positions.push_back(p);
395 
396  dd = dd / 2;
397  }
398  }
399  }
400  }
401 }
402 
403 
405  PosChangeVector& positions,
406  ValueVector& values,
409 {
410  assert(positions.size() == values.size());
411  if (positions.size() == 0)
412  return;
413 
414  ValueType logpsi = values[0];
415 
417  {
418  computeFiniteDiffLowOrder(delta, positions, values, G_fd, L_fd);
419  }
420  else if (m_fd_type == FiniteDiff_Richardson)
421  {
422  computeFiniteDiffRichardson(delta, positions, values, G_fd, L_fd);
423  }
424 }
425 
427  PosChangeVector& positions,
428  ValueVector& values,
431 {
432  ValueType logpsi = values[0];
433 
434  // lowest order derivative formula
435  ValueType c1 = 1.0 / delta / 2.0;
436  ValueType c2 = 1.0 / delta / delta;
437 
438  const RealType twoD(2 * ndim);
439  const int pt_per_deriv = 2; // number of points per derivative
440  for (int pt_i = 1; pt_i < values.size(); pt_i += pt_per_deriv * ndim)
441  {
442  GradType g0;
443  ValueType lap0 = 0.0;
444  for (int idim = 0; idim < ndim; idim++)
445  {
446  int idx = pt_i + idim * pt_per_deriv;
447  ValueType logpsi_m = values[idx];
448  ValueType logpsi_p = values[idx + 1];
449 
450  g0[idim] = logpsi_p - logpsi_m;
451  lap0 += logpsi_p + logpsi_m;
452  }
453 
454  int iat = positions[pt_i].index;
455  GradType g = c1 * g0;
456  G_fd[iat] = g;
457 
458  ValueType lap = c2 * (lap0 - twoD * logpsi);
459  //ValueType lap = c2*(lap0 - 2.0*OHMMS_DIM*logpsi);
460  L_fd[iat] = lap;
461  }
462 }
463 
464 // Use Richardson extrapolation to compute the derivatives.
465 // The 'delta' parameter should not be small, as with fixed
466 // order finite difference methods. The algorithm will zoom
467 // in on the right size of parameter.
469  PosChangeVector& positions,
470  ValueVector& values,
473 {
474  RealType tol = 1e-7;
475  ValueType logpsi = values[0];
476 
477  const int pt_per_deriv = 2 * (m_RichardsonSize + 1); // number of points per derivative
478  for (int pt_i = 1; pt_i < values.size(); pt_i += pt_per_deriv * ndim)
479  {
480  GradType g0;
481  GradType gmin;
482  ValueType lmin;
483  std::vector<GradType> g_base(m_RichardsonSize + 1);
484  std::vector<GradType> g_rich(m_RichardsonSize + 1);
485  std::vector<GradType> g_prev(m_RichardsonSize + 1);
486 
487  std::vector<ValueType> l_base(m_RichardsonSize + 1);
488  std::vector<ValueType> l_rich(m_RichardsonSize + 1);
489  std::vector<ValueType> l_prev(m_RichardsonSize + 1);
490 
491  // Initial gradients and Laplacians at different deltas.
492  RealType dd = delta;
493  const ValueType ctwo(2);
494  for (int inr = 0; inr < m_RichardsonSize + 1; inr++)
495  {
496  RealType twodd = 2 * dd;
497  RealType ddsq = dd * dd;
498  l_base[inr] = 0.0;
499  for (int idim = 0; idim < ndim; idim++)
500  {
501  int idx = pt_i + idim * pt_per_deriv + 2 * inr;
502  ValueType logpsi_m = values[idx];
503  ValueType logpsi_p = values[idx + 1];
504  g_base[inr][idim] = (logpsi_p - logpsi_m) / twodd;
505  l_base[inr] += (logpsi_p + logpsi_m - ctwo * logpsi) / ddsq;
506  //g_base[inr][idim] = (logpsi_p - logpsi_m)/dd/2.0;
507  //l_base[inr] += (logpsi_p + logpsi_m - 2.0*logpsi)/(dd*dd);
508  }
509  dd = dd / 2;
510  }
511 
512  // Gradient
513 
514  g_prev[0] = g_base[0];
515  RealType fac = 1;
516  bool found = false;
517  for (int inr = 1; inr < m_RichardsonSize + 1; inr++)
518  {
519  g_rich[0] = g_base[inr];
520 
521  fac *= 4;
522  for (int j = 1; j < inr + 1; j++)
523  {
524  g_rich[j] = g_rich[j - 1] + (g_rich[j - 1] - g_prev[j - 1]) / (fac - 1);
525  }
526 
527  RealType err1 = 0.0;
528  RealType norm = 0.0;
529  for (int idim = 0; idim < ndim; idim++)
530  {
531  err1 += std::abs(g_rich[inr][idim] - g_prev[inr - 1][idim]);
532  norm += std::abs(g_prev[inr - 1][idim]);
533  }
534 
535  RealType err_rel = err1 / norm;
536 
537  // Not sure about the best stopping criteria
538  if (err_rel < tol)
539  {
540  gmin = g_rich[inr];
541  found = true;
542  break;
543  }
544  g_prev = g_rich;
545  }
546 
547  if (!found)
548  {
549  gmin = g_rich[m_RichardsonSize];
550  }
551 
552 
553  // Laplacian
554  // TODO: eliminate the copied code between the gradient and Laplacian
555  // computations.
556 
557  l_prev[0] = l_base[0];
558 
559  fac = 1;
560  found = false;
561  for (int inr = 1; inr < m_RichardsonSize + 1; inr++)
562  {
563  l_rich[0] = l_base[inr];
564 
565  fac *= 4;
566  for (int j = 1; j < inr + 1; j++)
567  {
568  l_rich[j] = l_rich[j - 1] + (l_rich[j - 1] - l_prev[j - 1]) / (fac - 1);
569  }
570 
571  RealType err1 = std::abs(l_rich[inr] - l_prev[inr - 1]);
572  RealType err_rel = std::abs(err1 / l_prev[inr - 1]);
573 
574  if (err_rel < tol)
575  {
576  lmin = l_rich[inr];
577  found = true;
578  break;
579  }
580  l_prev = l_rich;
581  }
582 
583  if (!found)
584  {
585  lmin = l_rich[m_RichardsonSize];
586  }
587 
588  int iat = positions[pt_i].index;
589  G_fd[iat] = gmin;
590  L_fd[iat] = lmin;
591  }
592 }
593 
594 // Compute numerical gradient and Laplacian
596  ParticleSet::ParticleGradient& G_fd, // finite difference
598 {
601 
602  fd.finiteDifferencePoints(delta, W, positions);
603 
604  FiniteDifference::ValueVector logpsi_vals;
605  FiniteDifference::PosChangeVector::iterator it;
606 
607  for (it = positions.begin(); it != positions.end(); it++)
608  {
609  PosType r0 = W.R[it->index];
610  W.R[it->index] = it->r;
611  W.update();
612  RealType logpsi0 = Psi.evaluateLog(W);
613 #if defined(QMC_COMPLEX)
614  RealType phase0 = Psi.getPhase();
615  ValueType logpsi = std::complex<OHMMS_PRECISION>(logpsi0, phase0);
616 #else
617  ValueType logpsi = logpsi0;
618 #endif
619  logpsi_vals.push_back(logpsi);
620 
621  W.R[it->index] = r0;
622  W.update();
623  Psi.evaluateLog(W);
624  }
625 
626  fd.computeFiniteDiff(delta, positions, logpsi_vals, G_fd, L_fd);
627 }
628 
629 // Usually
630 // lower_iat = 0
631 // upper_iat = nat
633  int upper_iat,
638  std::stringstream& log,
639  int indent /* = 0 */)
640 {
641  ParticleSet::Scalar_t rel_tol = 1e-3;
642  ParticleSet::Scalar_t abs_tol = 1e-7;
643  if (toleranceParam > 0.0)
644  {
645  rel_tol = toleranceParam;
646  }
647 
648  bool all_okay = true;
649  std::string pad(4 * indent, ' ');
650 
651 
652  for (int iat = lower_iat; iat < upper_iat; iat++)
653  {
654  ParticleSet::Scalar_t L_err = std::abs(L[iat] - L_fd[iat]);
655  ParticleSet::Scalar_t L_rel_denom = std::max(std::abs(L[iat]), std::abs(L_fd[iat]));
656  ParticleSet::Scalar_t L_err_rel = std::abs(L_err / L_rel_denom);
657 
658  if (L_err_rel > rel_tol && L_err > abs_tol)
659  {
660  if (L_err_rel > rel_tol)
661  {
662  log << pad << "Finite difference Laplacian exceeds relative tolerance (" << rel_tol << ") for particle " << iat
663  << std::endl;
664  }
665  else
666  {
667  log << pad << "Finite difference Laplacian exceeds absolute tolerance (" << abs_tol << ") for particle " << iat
668  << std::endl;
669  }
670  log << pad << " Analytic = " << L[iat] << std::endl;
671  log << pad << " Finite diff = " << L_fd[iat] << std::endl;
672  log << pad << " Error = " << L_err << " Relative Error = " << L_err_rel << std::endl;
673  all_okay = false;
674  }
675 
676  ParticleSet::Scalar_t G_err_rel[OHMMS_DIM];
677  for (int idim = 0; idim < OHMMS_DIM; idim++)
678  {
679  ParticleSet::Scalar_t G_err = std::abs(G[iat][idim] - G_fd[iat][idim]);
680  ParticleSet::Scalar_t G_rel_denom = std::max(std::abs(G[iat][idim]), std::abs(G_fd[iat][idim]));
681  G_err_rel[idim] = std::abs(G_err / G[iat][idim]);
682 
683  if (G_err_rel[idim] > rel_tol && G_err > abs_tol)
684  {
685  if (G_err_rel[idim] > rel_tol)
686  {
687  log << pad << "Finite difference gradient exceeds relative tolerance (" << rel_tol << ") for particle "
688  << iat;
689  }
690  else
691  {
692  log << pad << "Finite difference gradient exceeds absolute tolerance (" << abs_tol << ") for particle "
693  << iat;
694  }
695  log << " component " << idim << std::endl;
696  log << pad << " Analytic = " << G[iat][idim] << std::endl;
697  log << pad << " Finite diff = " << G_fd[iat][idim] << std::endl;
698  log << pad << " Error = " << G_err << " Relative Error = " << G_err_rel[idim] << std::endl;
699  all_okay = false;
700  }
701  }
702 
703  fout << pad << "For particle #" << iat << " at " << W.R[iat] << std::endl;
704  fout << pad << "Gradient = " << std::setw(12) << G[iat] << std::endl;
705  fout << pad << " Finite diff = " << std::setw(12) << G_fd[iat] << std::endl;
706  fout << pad << " Error = " << std::setw(12) << G[iat] - G_fd[iat] << std::endl;
707  fout << pad << " Relative Error = ";
708  for (int idim = 0; idim < OHMMS_DIM; idim++)
709  {
710  fout << G_err_rel[idim] << " ";
711  }
712  fout << std::endl << std::endl;
713  fout << pad << "Laplacian = " << std::setw(12) << L[iat] << std::endl;
714  fout << pad << " Finite diff = " << std::setw(12) << L_fd[iat] << std::endl;
715  fout << pad << " Error = " << std::setw(12) << L[iat] - L_fd[iat] << " Relative Error = " << L_err_rel
716  << std::endl
717  << std::endl;
718  }
719  return all_okay;
720 }
721 
723  std::stringstream& fail_log,
724  bool& ignore)
725 {
726  int nat = W.getTotalNum();
727  ParticleSet::ParticleGradient G(nat), G1(nat);
728  ParticleSet::ParticleLaplacian L(nat), L1(nat);
729 
730  W.loadWalker(*W1, true);
731 
732  // compute analytic values
733  Psi.evaluateLog(W);
734  G = W.G;
735  L = W.L;
736 
737  // Use a single delta with a fairly large tolerance
738  //computeNumericalGrad(delta, G1, L1);
739 
740  RealType delta = 1.0e-4;
741  if (deltaParam > 0.0)
742  {
743  delta = deltaParam;
744  }
746 
748 
749  fd.finiteDifferencePoints(delta, W, positions);
750 
751  FiniteDifference::ValueVector logpsi_vals;
752  FiniteDifference::PosChangeVector::iterator it;
753 
754  for (it = positions.begin(); it != positions.end(); it++)
755  {
756  PosType r0 = W.R[it->index];
757  W.R[it->index] = it->r;
758  W.update();
759  RealType logpsi0 = Psi.evaluateLog(W);
760  RealType phase0 = Psi.getPhase();
761 #if defined(QMC_COMPLEX)
762  ValueType logpsi = std::complex<OHMMS_PRECISION>(logpsi0, phase0);
763 #else
764  ValueType logpsi = logpsi0;
765 #endif
766  logpsi_vals.push_back(logpsi);
767 
768  W.R[it->index] = r0;
769  W.update();
770  Psi.evaluateLog(W);
771  }
772 
773  fd.computeFiniteDiff(delta, positions, logpsi_vals, G1, L1);
774 
775  fout << "delta = " << delta << std::endl;
776 
777  // TODO - better choice of tolerance
778  // TODO - adjust delta and tolerance based on precision of wavefunction
779 
780  bool all_okay = checkGradients(0, nat, G, L, G1, L1, fail_log);
781  // RealType tol = 1e-3;
782  // if (toleranceParam> 0.0)
783  // {
784  // tol = toleranceParam;
785  // }
786 
787  for (int iorb = 0; iorb < Psi.getOrbitals().size(); iorb++)
788  {
789  auto& orb = Psi.getOrbitals()[iorb];
790 
791  ParticleSet::ParticleGradient G(nat), tmpG(nat), G1(nat);
792  ParticleSet::ParticleLaplacian L(nat), tmpL(nat), L1(nat);
793 
794 
795  LogValue logpsi1 = orb->evaluateLog(W, G, L);
796 
797  fail_log << "WaveFunctionComponent " << iorb << " " << orb->getClassName() << " log psi = " << logpsi1 << std::endl;
798 
799  FiniteDifference::ValueVector logpsi_vals;
800  FiniteDifference::PosChangeVector::iterator it;
801  for (it = positions.begin(); it != positions.end(); it++)
802  {
803  PosType r0 = W.R[it->index];
804  W.R[it->index] = it->r;
805  W.update();
807  W.makeMove(it->index, zeroR);
808 
809  LogValue logpsi0 = orb->evaluateLog(W, tmpG, tmpL);
810 #if defined(QMC_COMPLEX)
811  ValueType logpsi(logpsi0.real(), logpsi0.imag());
812 #else
813  ValueType logpsi = std::real(logpsi0);
814 #endif
815  logpsi_vals.push_back(logpsi);
816  W.rejectMove(it->index);
817 
818  W.R[it->index] = r0;
819  W.update();
820  }
821  fd.computeFiniteDiff(delta, positions, logpsi_vals, G1, L1);
822 
823  fout << " WaveFunctionComponent " << iorb << " " << orb->getClassName() << std::endl;
824 
825  if (!checkGradients(0, nat, G, L, G1, L1, fail_log, 1))
826  {
827  all_okay = false;
828  }
829 
830  if (!checkSlaterDet)
831  continue; // skip SlaterDet check if <backflow> is present
832  // DiracDeterminantWithBackflow::evaluateLog requires a call to BackflowTransformation::evaluate in its owning SlaterDetWithBackflow to work correctly.
833  SlaterDet* sd = dynamic_cast<SlaterDet*>(orb.get());
834  if (sd)
835  {
836  for (int isd = 0; isd < sd->Dets.size(); isd++)
837  {
838  ParticleSet::ParticleGradient G(nat), tmpG(nat), G1(nat);
839  ParticleSet::ParticleLaplacian L(nat), tmpL(nat), L1(nat);
840  DiracDeterminantBase& det = *sd->Dets[isd];
841  LogValue logpsi2 = det.evaluateLog(W, G, L); // this won't work with backflow
842  fail_log << " Slater Determiant " << isd << " (for particles " << det.getFirstIndex() << " to "
843  << det.getLastIndex() << ") log psi = " << logpsi2 << std::endl;
844  // Should really check the condition number on the matrix determinant.
845  // For now, just ignore values that too small.
846  if (std::real(logpsi2) < -40.0)
847  {
848  ignore = true;
849  }
850  FiniteDifference::ValueVector logpsi_vals;
851  FiniteDifference::PosChangeVector::iterator it;
852  for (it = positions.begin(); it != positions.end(); it++)
853  {
854  PosType r0 = W.R[it->index];
855  W.R[it->index] = it->r;
856  W.update();
857 
858  LogValue logpsi0 = det.evaluateLog(W, tmpG, tmpL);
859 #if defined(QMC_COMPLEX)
860  ValueType logpsi(logpsi0.real(), logpsi0.imag());
861 #else
862  ValueType logpsi = std::real(logpsi0);
863 #endif
864  logpsi_vals.push_back(logpsi);
865 
866  W.R[it->index] = r0;
867  W.update();
868  }
869  fd.computeFiniteDiff(delta, positions, logpsi_vals, G1, L1);
870 
871  if (!checkGradients(det.getFirstIndex(), det.getLastIndex(), G, L, G1, L1, fail_log, 2))
872  {
873  all_okay = false;
874  }
875 
876 #if 0
877  // Testing single particle orbitals doesn't work yet - probably something
878  // with setup after setting the position.
879  std::map<std::string, SPOSetPtr>::iterator spo_it = sd->mySPOSet.begin();
880  for (; spo_it != sd->mySPOSet.end(); spo_it++)
881  {
882  SPOSetPtr spo = spo_it->second;
883  fail_log << " SPO set = " << spo_it->first << " name = " << spo->className;
884  fail_log << " orbital set size = " << spo->size();
885  fail_log << " basis set size = " << spo->getBasisSetSize() << std::endl;
886 
887  ParticleSet::ParticleGradient G(nat), tmpG(nat), G1(nat);
888  ParticleSet::ParticleLaplacian L(nat), tmpL(nat), L1(nat);
889  RealType logpsi3 = det.evaluateLog(W, G, L);
890  FiniteDifference::ValueVector logpsi_vals;
891  FiniteDifference::PosChangeVector::iterator it;
892  for (it = positions.begin(); it != positions.end(); it++)
893  {
894  PosType r0 = W.R[it->index];
895  W.R[it->index] = it->r;
896  W.update();
898  W.makeMove(it->index,zeroR);
899 
900  SPOSet::ValueVector psi(spo->size());
901 
902  spo->evaluate(W, it->index, psi);
903  ValueType logpsi = psi[0];
904  logpsi_vals.push_back(logpsi);
905 
906  W.rejectMove(it->index);
907  W.R[it->index] = r0;
908  W.update();
909  }
910  fd.computeFiniteDiff(delta, positions, logpsi_vals, G1, L1);
911 
912  if (!checkGradients(det.getFirstIndex(), det.getLastIndex(), G, L, G1, L1, fail_log, 3))
913  {
914  all_okay = false;
915  }
916  }
917 #endif
918  }
919  }
920  }
921  return all_okay;
922 }
923 
925 {
926  int nat = W.getTotalNum();
927  fout << "Numerical gradient and Laplacian test" << std::endl;
928 
929  std::stringstream fail_log;
930  bool all_okay = true;
931  int fails = 0;
932  int nconfig = 0;
933  int nignore = 0;
934 
936  for (; Wit != W.end(); Wit++)
937  {
938  fout << "Walker # " << nconfig << std::endl;
939  std::stringstream fail_log1;
940  bool ignore = false;
941  bool this_okay = checkGradientAtConfiguration(Wit->get(), fail_log1, ignore);
942  if (ignore)
943  {
944  nignore++;
945  }
946  if (!this_okay && !ignore)
947  {
948  fail_log << "Walker # " << nconfig << std::endl;
949  fail_log << fail_log1.str();
950  fail_log << std::endl;
951  fails++;
952  all_okay = false;
953  }
954  nconfig++;
955  }
956 
957  app_log() << "Number of samples = " << nconfig << std::endl;
958  app_log() << "Number ignored (bad positions) = " << nignore << std::endl << std::endl;
959  app_log() << "Number of fails = " << fails << std::endl << std::endl;
960 
961  if (!all_okay)
962  {
963  std::string fail_name("wf_fails.dat");
964  app_log() << "More detail on finite difference failures in " << fail_name << std::endl;
965  std::ofstream eout(fail_name.c_str());
966  eout << fail_log.str();
967  eout.close();
968  }
969 
970  app_log() << "Finite difference test: " << (all_okay ? "PASS" : "FAIL") << std::endl;
971 
972  // Compute approximation error vs. delta.
973  if (outputDeltaVsError)
974  {
975  double delta = 1.0;
976  bool inputOkay = true;
977 
978  std::ofstream dout(DeltaVsError.outputFile.c_str());
979 
980  int iat = DeltaVsError.particleIndex;
982 
983  if (iat < 0 || iat >= nat)
984  {
985  dout << "# Particle index (" << iat << ") is out of range (0 - " << nat - 1 << ")" << std::endl;
986  inputOkay = false;
987  }
988 
989  if (ig < 0 || ig >= OHMMS_DIM)
990  {
991  dout << "# Gradient component index (" << ig << ") is out of range (0 - " << OHMMS_DIM - 1 << ")" << std::endl;
992  inputOkay = false;
993  }
994 
995  if (inputOkay)
996  {
997  dout << "# Particle = " << iat << " Gradient component = " << ig << std::endl;
998  dout << "#" << std::setw(11) << "delta" << std::setw(14) << "G_err_rel" << std::setw(14) << "L_err_rel"
999  << std::endl;
1000  ParticleSet::ParticleGradient G(nat), G1(nat);
1001  ParticleSet::ParticleLaplacian L(nat), L1(nat);
1002  for (int i = 0; i < 20; i++)
1003  {
1004  // compute analytic values
1005  G = W.G;
1006  L = W.L;
1007  Psi.evaluateLog(W);
1008 
1009  computeNumericalGrad(delta, G1, L1);
1010  ParticleSet::Scalar_t L_err = std::abs(L[iat] - L1[iat]);
1011  ParticleSet::Scalar_t L_err_rel = std::abs(L_err / L[iat]);
1012  ParticleSet::Scalar_t G_err = std::abs(G[iat][ig] - G1[iat][ig]);
1013  ParticleSet::Scalar_t G_err_rel = std::abs(G_err / G[iat][ig]);
1014  dout << std::setw(12) << delta;
1015  dout << std::setw(14) << std::abs(G_err_rel) << std::setw(14) << std::abs(L_err_rel);
1016  dout << std::endl;
1017  delta *= std::sqrt(0.1);
1018  }
1019  }
1020  dout.close();
1021  }
1022 
1023  fout << "Ratio test" << std::endl;
1024 
1025  RealType tol = 1e-3;
1026  RealType ratio_tol = 1e-9;
1027  bool any_ratio_fail = false;
1029  if (ndim < 3)
1030  {
1031  for (int iat = 0; iat < deltaR.size(); ++iat)
1032  deltaR[iat][2] = 0;
1033  }
1034  fout << "deltaR:" << std::endl;
1035  fout << deltaR << std::endl;
1036  fout << "Particle Ratio of Ratios Computed Ratio Internal Ratio" << std::endl;
1037  //testing ratio alone
1038  for (int iat = 0; iat < nat; iat++)
1039  {
1040  W.update();
1041  //ValueType psi_p = log(std::abs(Psi.evaluate(W)));
1042  RealType psi_p = Psi.evaluateLog(W);
1043  RealType phase_p = Psi.getPhase();
1044  W.makeMove(iat, deltaR[iat]);
1045  //W.update();
1046  ValueType aratio = Psi.calcRatio(W, iat);
1047  RealType phaseDiff = Psi.getPhaseDiff();
1048  W.rejectMove(iat);
1049  Psi.rejectMove(iat);
1050  W.R[iat] += deltaR[iat];
1051  W.update();
1052  //ValueType psi_m = log(std::abs(Psi.evaluate(W)));
1053  RealType psi_m = Psi.evaluateLog(W);
1054  RealType phase_m = Psi.getPhase();
1055 
1056 #if defined(QMC_COMPLEX)
1057  RealType ratioMag = std::exp(psi_m - psi_p);
1058  RealType dphase = phase_m - phase_p;
1059  if (phaseDiff < 0.0)
1060  phaseDiff += 2.0 * M_PI;
1061  if (phaseDiff > 2.0 * M_PI)
1062  phaseDiff -= 2.0 * M_PI;
1063  if (dphase < 0.0)
1064  dphase += 2.0 * M_PI;
1065  if (dphase > 2.0 * M_PI)
1066  dphase -= 2.0 * M_PI;
1067  ValueType ratDiff = std::complex<OHMMS_PRECISION>(ratioMag * std::cos(dphase), ratioMag * std::sin(dphase));
1068  // TODO - test complex ratio against a tolerance
1069  fout << iat << " " << aratio / ratDiff << " " << ratDiff << " " << aratio << std::endl;
1070  fout << " ratioMag " << std::abs(aratio) / ratioMag << " " << ratioMag << std::endl;
1071  fout << " PhaseDiff " << phaseDiff / dphase << " " << phaseDiff << " " << dphase << std::endl;
1072 #else
1073  RealType ratDiff = std::exp(psi_m - psi_p) * std::cos(phase_m - phase_p);
1074  fout << iat << " " << aratio / ratDiff << " " << ratDiff << " " << aratio << std::endl;
1075  if (std::abs(aratio / ratDiff - 1.0) > ratio_tol)
1076  {
1077  app_log() << "Wavefunction ratio exceeds tolerance " << tol << ") for particle " << iat << std::endl;
1078  app_log() << " Internally computed ratio = " << aratio << std::endl;
1079  app_log() << " Separately computed ratio = " << ratDiff << std::endl;
1080  any_ratio_fail = true;
1081  }
1082 #endif
1083  }
1084  app_log() << "Ratio test: " << (any_ratio_fail ? "FAIL" : "PASS") << std::endl;
1085 }
1086 
1088 {
1089 #if 0
1090  int nat = W.getTotalNum();
1091  ParticleSet::ParticleGradient Gp(nat), dGp(nat);
1092  ParticleSet::ParticleLaplacian Lp(nat), dLp(nat);
1093  bool checkHam=(checkHamPbyP == "yes");
1094  Tau=0.025;
1095  MCWalkerConfiguration::iterator it(W.begin()), it_end(W.end());
1096  while (it != it_end)
1097  {
1099  Walker_t::WFBuffer_t tbuffer;
1100  W.R = (**it).R+Tau*deltaR;
1101  (**it).R=W.R;
1102  W.update();
1103  RealType logpsi=Psi.registerData(W,tbuffer);
1104  RealType ene;
1105  if (checkHam)
1106  ene = H.registerData(W,tbuffer);
1107  else
1108  ene = H.evaluate(W);
1109  (*it)->DataSet=tbuffer;
1110  //RealType ene = H.evaluate(W);
1111  (*it)->resetProperty(logpsi,Psi.getPhase(),ene,0.0,0.0,1.0);
1112  H.saveProperty((*it)->getPropertyBase());
1113  ++it;
1114  app_log() << " HamTest " << " Total " << ene << std::endl;
1115  for (int i=0; i<H.sizeOfObservables(); i++)
1116  app_log() << " HamTest " << H.getObservableName(i) << " " << H.getObservable(i) << std::endl;
1117  }
1118  fout << " Update using drift " << std::endl;
1119  bool pbyp_mode=true;
1120  for (int iter=0; iter<4; ++iter)
1121  {
1122  int iw=0;
1123  it=W.begin();
1124  while (it != it_end)
1125  {
1126  fout << "\nStart Walker " << iw++ << std::endl;
1127  Walker_t& thisWalker(**it);
1128  W.loadWalker(thisWalker,pbyp_mode);
1129  Walker_t::WFBuffer_t& w_buffer(thisWalker.DataSet);
1130  Psi.copyFromBuffer(W,w_buffer);
1131  H.copyFromBuffer(W,w_buffer);
1132 // Psi.evaluateLog(W);
1133  RealType eold(thisWalker.Properties(WP::LOCALENERGY));
1134  RealType logpsi(thisWalker.Properties(LOGPSI));
1135  RealType emixed(eold), enew(eold);
1137  //mave a move
1138  RealType ratio_accum(1.0);
1139  for (int iat=0; iat<nat; iat++)
1140  {
1141  PosType dr(Tau*deltaR[iat]);
1142  PosType newpos(W.makeMove(iat,dr));
1143  //RealType ratio=Psi.ratio(W,iat,dGp,dLp);
1144  W.dG=0;
1145  W.dL=0;
1146  RealType ratio=Psi.ratio(W,iat,W.dG,W.dL);
1147  Gp = W.G + W.dG;
1148  //RealType enew = H.evaluatePbyP(W,iat);
1149  if (checkHam)
1150  enew = H.evaluatePbyP(W,iat);
1151  if (ratio > Random())
1152  {
1153  fout << " Accepting a move for " << iat << std::endl;
1154  fout << " Energy after a move " << enew << std::endl;
1155  W.G += W.dG;
1156  W.L += W.dL;
1157  W.acceptMove(iat);
1158  Psi.acceptMove(W,iat);
1159  if (checkHam)
1160  H.acceptMove(iat);
1161  ratio_accum *= ratio;
1162  }
1163  else
1164  {
1165  fout << " Rejecting a move for " << iat << std::endl;
1166  W.rejectMove(iat);
1167  Psi.rejectMove(iat);
1168  //H.rejectMove(iat);
1169  }
1170  }
1171  fout << " Energy after pbyp = " << H.getLocalEnergy() << std::endl;
1172  RealType newlogpsi_up = Psi.evaluateLog(W,w_buffer);
1173  W.saveWalker(thisWalker);
1174  RealType ene_up;
1175  if (checkHam)
1176  ene_up= H.evaluate(W,w_buffer);
1177  else
1178  ene_up = H.evaluate(W);
1179  Gp=W.G;
1180  Lp=W.L;
1181  W.R=thisWalker.R;
1182  W.update();
1183  RealType newlogpsi=Psi.updateBuffer(W,w_buffer,false);
1184  RealType ene = H.evaluate(W);
1185  thisWalker.resetProperty(newlogpsi,Psi.getPhase(),ene);
1186  //thisWalker.resetProperty(std::log(psi),Psi.getPhase(),ene);
1187  fout << iter << " Energy by update = "<< ene_up << " " << ene << " " << ene_up-ene << std::endl;
1188  fout << iter << " Ratio " << ratio_accum*ratio_accum
1189  << " | " << std::exp(2.0*(newlogpsi-logpsi)) << " "
1190  << ratio_accum*ratio_accum/std::exp(2.0*(newlogpsi-logpsi)) << std::endl
1191  << " new log(psi) updated " << newlogpsi_up
1192  << " new log(psi) calculated " << newlogpsi
1193  << " old log(psi) " << logpsi << std::endl;
1194  fout << " Gradients " << std::endl;
1195  for (int iat=0; iat<nat; iat++)
1196  fout << W.G[iat]-Gp[iat] << W.G[iat] << std::endl; //W.G[iat] << G[iat] << std::endl;
1197  fout << " Laplacians " << std::endl;
1198  for (int iat=0; iat<nat; iat++)
1199  fout << W.L[iat]-Lp[iat] << " " << W.L[iat] << std::endl;
1200  ++it;
1201  }
1202  }
1203  fout << " Update without drift : for VMC useDrift=\"no\"" << std::endl;
1204  for (int iter=0; iter<4; ++iter)
1205  {
1206  it=W.begin();
1207  int iw=0;
1208  while (it != it_end)
1209  {
1210  fout << "\nStart Walker " << iw++ << std::endl;
1211  Walker_t& thisWalker(**it);
1212  W.loadWalker(thisWalker,pbyp_mode);
1213  Walker_t::WFBuffer_t& w_buffer(thisWalker.DataSet);
1214  //Psi.updateBuffer(W,w_buffer,true);
1215  Psi.copyFromBuffer(W,w_buffer);
1216  RealType eold(thisWalker.Properties(WP::LOCALENERGY));
1217  RealType logpsi(thisWalker.Properties(LOGPSI));
1218  RealType emixed(eold), enew(eold);
1219  //mave a move
1220  RealType ratio_accum(1.0);
1221  for (int substep=0; substep<3; ++substep)
1222  {
1224  for (int iat=0; iat<nat; iat++)
1225  {
1226  PosType dr(Tau*deltaR[iat]);
1227  PosType newpos(W.makeMove(iat,dr));
1228  RealType ratio=Psi.ratio(W,iat);
1229  RealType prob = ratio*ratio;
1230  if (prob > Random())
1231  {
1232  fout << " Accepting a move for " << iat << std::endl;
1233  W.acceptMove(iat);
1234  Psi.acceptMove(W,iat);
1235  ratio_accum *= ratio;
1236  }
1237  else
1238  {
1239  fout << " Rejecting a move for " << iat << std::endl;
1240  W.rejectMove(iat);
1241  Psi.rejectMove(iat);
1242  }
1243  }
1244  RealType logpsi_up = Psi.updateBuffer(W,w_buffer,false);
1245  W.saveWalker(thisWalker);
1246  RealType ene = H.evaluate(W);
1247  thisWalker.resetProperty(logpsi_up,Psi.getPhase(),ene);
1248  }
1249  Gp=W.G;
1250  Lp=W.L;
1251  W.update();
1252  RealType newlogpsi=Psi.evaluateLog(W);
1253  fout << iter << " Ratio " << ratio_accum*ratio_accum
1254  << " | " << std::exp(2.0*(newlogpsi-logpsi)) << " "
1255  << ratio_accum*ratio_accum/std::exp(2.0*(newlogpsi-logpsi)) << std::endl
1256  << " new log(psi) " << newlogpsi
1257  << " old log(psi) " << logpsi << std::endl;
1258  fout << " Gradients " << std::endl;
1259  for (int iat=0; iat<nat; iat++)
1260  {
1261  fout << W.G[iat]-Gp[iat] << W.G[iat] << std::endl; //W.G[iat] << G[iat] << std::endl;
1262  }
1263  fout << " Laplacians " << std::endl;
1264  for (int iat=0; iat<nat; iat++)
1265  {
1266  fout << W.L[iat]-Lp[iat] << " " << W.L[iat] << std::endl;
1267  }
1268  ++it;
1269  }
1270  }
1271  //for(it=W.begin();it != it_end; ++it)
1272  //{
1273  // Walker_t& thisWalker(**it);
1274  // Walker_t::WFBuffer_t& w_buffer((*it)->DataSet);
1275  // w_buffer.rewind();
1276  // W.updateBuffer(**it,w_buffer);
1277  // RealType logpsi=Psi.updateBuffer(W,w_buffer,true);
1278  //}
1279 #endif
1280 }
1281 
1283 {
1284  app_log() << " ===== runRatioTest2 =====\n";
1285  int nat = W.getTotalNum();
1286  ParticleSet::ParticleGradient Gp(nat), dGp(nat);
1287  ParticleSet::ParticleLaplacian Lp(nat), dLp(nat);
1288  Tau = 0.025;
1289  MCWalkerConfiguration::iterator it(W.begin()), it_end(W.end());
1290  for (; it != it_end; ++it)
1291  {
1293  if (ndim < 3)
1294  {
1295  for (int iat = 0; iat < deltaR.size(); ++iat)
1296  deltaR[iat][2] = 0;
1297  }
1298  Walker_t::WFBuffer_t tbuffer;
1299  (**it).R += Tau * deltaR;
1300  W.loadWalker(**it, true);
1301  Psi.registerData(W, tbuffer);
1302  tbuffer.allocate();
1303  Psi.copyFromBuffer(W, tbuffer);
1304  Psi.evaluateLog(W);
1305  RealType logpsi = Psi.updateBuffer(W, tbuffer, false);
1306  RealType ene = H.evaluate(W);
1307  (*it)->DataSet = tbuffer;
1308  //RealType ene = H.evaluate(W);
1309  (*it)->resetProperty(logpsi, Psi.getPhase(), ene, 0.0, 0.0, 1.0);
1310  H.saveProperty((*it)->getPropertyBase());
1311  app_log() << " HamTest "
1312  << " Total " << ene << std::endl;
1313  for (int i = 0; i < H.sizeOfObservables(); i++)
1314  app_log() << " HamTest " << H.getObservableName(i) << " " << H.getObservable(i) << std::endl;
1315  }
1316  for (int iter = 0; iter < 20; ++iter)
1317  {
1318  int iw = 0;
1319  it = W.begin();
1320  //while(it != it_end)
1321  for (; it != it_end; ++it)
1322  {
1323  fout << "\nStart Walker " << iw++ << std::endl;
1324  Walker_t& thisWalker(**it);
1325  W.loadWalker(thisWalker, true);
1326  Walker_t::WFBuffer_t& w_buffer(thisWalker.DataSet);
1327  Psi.copyFromBuffer(W, w_buffer);
1328  RealType eold(thisWalker.Properties(WP::LOCALENERGY));
1329  RealType logpsi(thisWalker.Properties(WP::LOGPSI));
1330  Psi.evaluateLog(W);
1331  ParticleSet::ParticleGradient realGrad(W.G);
1333  if (ndim < 3)
1334  {
1335  for (int iat = 0; iat < deltaR.size(); ++iat)
1336  deltaR[iat][2] = 0;
1337  }
1338  //mave a move
1339  for (int iat = 0; iat < nat; iat++)
1340  {
1342  GradType grad_new;
1343  for (int sds = 0; sds < 3; sds++)
1344  fout << realGrad[iat][sds] - grad_now[sds] << " ";
1345  PosType dr(Tau * deltaR[iat]);
1346  W.makeMove(iat, dr);
1347  ValueType ratio2 = Psi.calcRatioGrad(W, iat, grad_new);
1348  W.rejectMove(iat);
1349  Psi.rejectMove(iat);
1350  W.makeMove(iat, dr);
1351  ValueType ratio1 = Psi.calcRatio(W, iat);
1352  //Psi.rejectMove(iat);
1353  W.rejectMove(iat);
1354  fout << " ratio1 = " << ratio1 << " ration2 = " << ratio2 << std::endl;
1355  }
1356  }
1357  }
1358  //for(it=W.begin();it != it_end; ++it)
1359  //{
1360  // Walker_t& thisWalker(**it);
1361  // Walker_t::WFBuffer_t& w_buffer((*it)->DataSet);
1362  // w_buffer.rewind();
1363  // W.updateBuffer(**it,w_buffer);
1364  // RealType logpsi=Psi.updateBuffer(W,w_buffer,true);
1365  //}
1366 }
1367 
1368 template<typename T, unsigned D>
1369 inline void randomize(ParticleAttrib<TinyVector<T, D>>& displ, T fac)
1370 {
1371  T* rv = &(displ[0][0]);
1372  assignUniformRand(rv, displ.size() * D, Random);
1373  for (int i = 0; i < displ.size() * D; ++i)
1374  rv[i] = fac * (rv[i] - 0.5);
1375 }
1376 
1378 {
1379 #if 0
1380  app_log() << "WaveFunctionTester::runRatioV " << std::endl;
1381  int nat = W.getTotalNum();
1382  Tau=0.025;
1383 
1384  //create a VP with 8 virtual moves
1385  VirtualParticleSet vp(&W,8);
1386  W.enableVirtualMoves();
1387 
1388  //cheating
1389  const ParticleSet& ions=W.DistTables[1]->origin();
1390  DistanceTable* dt_ie=W.DistTables[1];
1391  double Rmax=2.0;
1392 
1393  ParticleSet::ParticlePos sphere(8);
1394  std::vector<RealType> ratio_1(8), ratio_v(8);
1395  MCWalkerConfiguration::iterator it(W.begin()), it_end(W.end());
1396  while (it != it_end)
1397  {
1399  Walker_t::WFBuffer_t tbuffer;
1400  W.R = (**it).R+Tau*deltaR;
1401  (**it).R=W.R;
1402  W.update();
1403  RealType logpsi=Psi.registerData(W,tbuffer);
1404 
1405  W.initVirtualMoves();
1406 
1407  for(int iat=0; iat<ions.getTotalNum(); ++iat)
1408  {
1409  for(int nn=dt_ie->M[iat],iel=0; nn<dt_ie->M[iat+1]; nn++,iel++)
1410  {
1411  RealType r(dt_ie->r(nn));
1412  if(r>Rmax) continue;
1413  randomize(sphere,(RealType)0.5);
1414 
1415  for(int k=0; k<sphere.size(); ++k)
1416  {
1417  W.makeMoveOnSphere(iel,sphere[k]);
1418  ratio_1[k]=Psi.ratio(W,iel);
1419  W.rejectMove(iel);
1420  Psi.resetPhaseDiff();
1421  }
1422 
1423  vp.makeMoves(iel,sphere);
1424 
1425  Psi.evaluateRatios(vp,ratio_v);
1426 
1427  app_log() << "IAT = " << iat << " " << iel << std::endl;
1428  for(int k=0; k<sphere.size(); ++k)
1429  {
1430  app_log() << ratio_1[k]/ratio_v[k] << " " << ratio_1[k] << std::endl;
1431  }
1432  app_log() << std::endl;
1433  }
1434  }
1435  ++it;
1436  }
1437 #endif
1438 }
1439 
1441 {
1442  app_log() << " ===== runGradSourceTest =====\n";
1443  for (auto& [key, value] : PtclPool.getPool())
1444  app_log() << "ParticelSet = " << key << std::endl;
1445  // Find source ParticleSet
1446  auto pit(PtclPool.getPool().find(sourceName));
1447  app_log() << pit->first << std::endl;
1448  // if(pit == PtclPool.getPool().end())
1449  // APP_ABORT("Unknown source \"" + sourceName + "\" WaveFunctionTester.");
1450  ParticleSet& source = *((*pit).second);
1451  RealType delta = 0.00001;
1452  ValueType c1 = 1.0 / delta / 2.0;
1453  ValueType c2 = 1.0 / delta / delta;
1454  int nat = W.getTotalNum();
1456  MCWalkerConfiguration::PropertyContainer_t Properties(0, 0, 1, WP::MAXPROPERTIES);
1457  //pick the first walker
1458  const MCWalkerConfiguration::Walker_t& awalker = **W.begin();
1459  //copy the properties of the working walker
1460  Properties = awalker.Properties;
1461  //sample a new walker configuration and copy to ParticleSet::R
1462  //makeGaussRandom(deltaR);
1463  W.R = awalker.R;
1464  //W.R += deltaR;
1465  W.update();
1466  //ValueType psi = Psi.evaluate(W);
1467  ValueType logpsi = Psi.evaluateLog(W);
1468  RealType eloc = H.evaluate(W);
1469  H.auxHevaluate(W);
1470  app_log() << " HamTest "
1471  << " Total " << eloc << std::endl;
1472  for (int i = 0; i < H.sizeOfObservables(); i++)
1473  app_log() << " HamTest " << H.getObservableName(i) << " " << H.getObservable(i) << std::endl;
1474  //RealType psi = Psi.evaluateLog(W);
1475  ParticleSet::ParticleGradient G(nat), G1(nat);
1476  ParticleSet::ParticleLaplacian L(nat), L1(nat);
1477  G = W.G;
1478  L = W.L;
1479 
1480  //This code computes d/dR \ln \Psi_T using the evalGradSource method, and
1481  // by finite difference. Results are saved in grad_ion and grad_ion_FD respectively.
1482  // GRAD TEST COMPUTATION
1483  int nions = source.getTotalNum();
1484  ParticleSet::ParticleGradient grad_ion(nions), grad_ion_FD(nions);
1485  for (int iat = 0; iat < nions; iat++)
1486  {
1487  grad_ion[iat] = Psi.evalGradSource(W, source, iat);
1488  PosType rI = source.R[iat];
1489  for (int iondim = 0; iondim < 3; iondim++)
1490  {
1491  source.R[iat][iondim] = rI[iondim] + delta;
1492  source.update();
1493  W.update();
1494 
1495  ValueType log_p = Psi.evaluateLog(W);
1496 
1497  source.R[iat][iondim] = rI[iondim] - delta;
1498  source.update();
1499  W.update();
1500  ValueType log_m = Psi.evaluateLog(W);
1501 
1502  //symmetric finite difference formula for gradient.
1503  grad_ion_FD[iat][iondim] = c1 * (log_p - log_m);
1504 
1505  //reset everything to how it was.
1506  source.R[iat][iondim] = rI[iondim];
1507  source.update();
1508  W.update();
1509  }
1510  //this lastone makes sure the distance tables correspond to unperturbed source.
1511  }
1512  //END GRAD TEST COMPUTATION
1513 
1514  for (int isrc = 0; isrc < 1 /*source.getTotalNum()*/; isrc++)
1515  {
1520  for (int dim = 0; dim < OHMMS_DIM; dim++)
1521  {
1522  grad_grad[dim].resize(nat);
1523  lapl_grad[dim].resize(nat);
1524  grad_grad_FD[dim].resize(nat);
1525  lapl_grad_FD[dim].resize(nat);
1526  }
1527  Psi.evaluateLog(W);
1528  GradType grad_log = Psi.evalGradSource(W, source, isrc, grad_grad, lapl_grad);
1530  //grad_log = Psi.evalGradSource (W, source, isrc);
1531  for (int iat = 0; iat < nat; iat++)
1532  {
1533  PosType r0 = W.R[iat];
1534  GradType gFD[OHMMS_DIM];
1535  GradType lapFD = ValueType();
1536  for (int eldim = 0; eldim < 3; eldim++)
1537  {
1538  W.R[iat][eldim] = r0[eldim] + delta;
1539  W.update();
1540  ValueType log_p = Psi.evaluateLog(W);
1541  GradType gradlogpsi_p = Psi.evalGradSource(W, source, isrc);
1542  W.R[iat][eldim] = r0[eldim] - delta;
1543  W.update();
1544  ValueType log_m = Psi.evaluateLog(W);
1545  GradType gradlogpsi_m = Psi.evalGradSource(W, source, isrc);
1546  lapFD += gradlogpsi_m + gradlogpsi_p;
1547  gFD[eldim] = gradlogpsi_p - gradlogpsi_m;
1548  W.R[iat] = r0;
1549  W.update();
1550  //Psi.evaluateLog(W);
1551  }
1552  const ValueType six(6);
1553  for (int iondim = 0; iondim < OHMMS_DIM; iondim++)
1554  {
1555  for (int eldim = 0; eldim < OHMMS_DIM; eldim++)
1556  grad_grad_FD[iondim][iat][eldim] = c1 * gFD[eldim][iondim];
1557  lapl_grad_FD[iondim][iat] = c2 * (lapFD[iondim] - six * grad_log[iondim]);
1558  }
1559  }
1560  for (int dimsrc = 0; dimsrc < OHMMS_DIM; dimsrc++)
1561  {
1562  for (int iat = 0; iat < nat; iat++)
1563  {
1564  fout << "For particle #" << iat << " at " << W.R[iat] << std::endl;
1565  fout << "Grad Gradient = " << std::setw(12) << grad_grad[dimsrc][iat] << std::endl
1566  << " Finite diff = " << std::setw(12) << grad_grad_FD[dimsrc][iat] << std::endl
1567  << " Error = " << std::setw(12) << grad_grad_FD[dimsrc][iat] - grad_grad[dimsrc][iat] << std::endl
1568  << std::endl;
1569  fout << "Grad Laplacian = " << std::setw(12) << lapl_grad[dimsrc][iat] << std::endl
1570  << " Finite diff = " << std::setw(12) << lapl_grad_FD[dimsrc][iat] << std::endl
1571  << " Error = " << std::setw(12) << lapl_grad_FD[dimsrc][iat] - lapl_grad[dimsrc][iat] << std::endl
1572  << std::endl;
1573  }
1574  }
1575  fout << "==== BEGIN Ion Gradient Check ====\n";
1576  for (int iat = 0; iat < nions; iat++)
1577  {
1578  fout << "For ion #" << iat << " at " << source.R[iat] << std::endl;
1579  fout << "Gradient = " << std::setw(12) << grad_ion[iat] << std::endl
1580  << " Finite diff = " << std::setw(12) << grad_ion_FD[iat] << std::endl
1581  << " Error = " << std::setw(12) << grad_ion_FD[iat] - grad_ion[iat] << std::endl
1582  << std::endl;
1583  }
1584  fout << "==== END Ion Gradient Check ====\n";
1585  fout.flush();
1586  }
1587 }
1588 
1589 
1591 {
1592  app_log() << " ===== runZeroVarianceTest =====\n";
1593  for (auto& [key, value] : PtclPool.getPool())
1594  app_log() << "ParticelSet = " << key << std::endl;
1595  // Find source ParticleSet
1596  auto pit(PtclPool.getPool().find(sourceName));
1597  app_log() << pit->first << std::endl;
1598  // if(pit == PtclPool.getPool().end())
1599  // APP_ABORT("Unknown source \"" + sourceName + "\" WaveFunctionTester.");
1600  ParticleSet& source = *((*pit).second);
1601  int nat = W.getTotalNum();
1603  MCWalkerConfiguration::PropertyContainer_t Properties(0, 0, 1, WP::MAXPROPERTIES);
1604  ;
1605  //pick the first walker
1606  const MCWalkerConfiguration::Walker_t& awalker = **W.begin();
1607  //copy the properties of the working walker
1608  Properties = awalker.Properties;
1609  //sample a new walker configuration and copy to ParticleSet::R
1610  //makeGaussRandom(deltaR);
1611  W.R = awalker.R;
1612  //W.R += deltaR;
1613  W.update();
1614  //ValueType psi = Psi.evaluate(W);
1615  ValueType logpsi = Psi.evaluateLog(W);
1616  RealType eloc = H.evaluate(W);
1617  //RealType psi = Psi.evaluateLog(W);
1618  ParticleSet::ParticleGradient G(nat), G1(nat);
1619  ParticleSet::ParticleLaplacian L(nat), L1(nat);
1620  G = W.G;
1621  L = W.L;
1622  PosType r1(5.0, 2.62, 2.55);
1623  W.R[1] = PosType(4.313, 5.989, 4.699);
1624  W.R[2] = PosType(5.813, 4.321, 4.893);
1625  W.R[3] = PosType(4.002, 5.502, 5.381);
1626  W.R[4] = PosType(5.901, 5.121, 5.311);
1627  W.R[5] = PosType(5.808, 4.083, 5.021);
1628  W.R[6] = PosType(4.750, 5.810, 4.732);
1629  W.R[7] = PosType(4.690, 5.901, 4.989);
1630  for (int i = 1; i < 8; i++)
1631  W.R[i] -= PosType(2.5, 2.5, 2.5);
1632  std::array<char, 32> fname;
1633  if (std::snprintf(fname.data(), fname.size(), "ZVtest.%03d.dat", OHMMS::Controller->rank()) < 0)
1634  throw std::runtime_error("Error generating filename");
1635  FILE* fzout = fopen(fname.data(), "w");
1640  for (int dim = 0; dim < OHMMS_DIM; dim++)
1641  {
1642  grad_grad[dim].resize(nat);
1643  lapl_grad[dim].resize(nat);
1644  grad_grad_FD[dim].resize(nat);
1645  lapl_grad_FD[dim].resize(nat);
1646  }
1647  for (r1[0] = 0.0; r1[0] < 5.0; r1[0] += 1.0e-4)
1648  {
1649  W.R[0] = r1;
1650  fprintf(fzout, "%1.8e %1.8e %1.8e ", r1[0], r1[1], r1[2]);
1652 // ValueType psi = std::cos(Psi.getPhase())*std::exp(log);//*W.PropertyList[SIGN];
1653 #if defined(QMC_COMPLEX)
1654  RealType ratioMag = std::exp(log);
1655  ValueType psi =
1656  std::complex<OHMMS_PRECISION>(ratioMag * std::cos(Psi.getPhase()), ratioMag * std::sin(Psi.getPhase()));
1657 #else
1658  ValueType psi = std::cos(Psi.getPhase()) * std::exp(log); //*W.PropertyList[SIGN];
1659 #endif
1660  double E = H.evaluate(W);
1661  //double KE = E - W.PropertyList[LOCALPOTENTIAL];
1662  double KE = -0.5 * (Sum(W.L) + Dot(W.G, W.G));
1663 #if defined(QMC_COMPLEX)
1664  fprintf(fzout, "%16.12e %16.12e %16.12e ", psi.real(), psi.imag(), KE);
1665 #else
1666  fprintf(fzout, "%16.12e %16.12e ", psi, KE);
1667 #endif
1668  for (int isrc = 0; isrc < source.getTotalNum(); isrc++)
1669  {
1670  GradType grad_log = Psi.evalGradSource(W, source, isrc, grad_grad, lapl_grad);
1671  for (int dim = 0; dim < OHMMS_DIM; dim++)
1672  {
1673  double ZV = 0.5 * Sum(lapl_grad[dim]) + Dot(grad_grad[dim], W.G);
1674 #if defined(QMC_COMPLEX)
1675  fprintf(fzout, "%16.12e %16.12e %16.12e ", ZV, grad_log[dim].real(), grad_log[dim].imag());
1676 #else
1677  fprintf(fzout, "%16.12e %16.12e ", ZV, grad_log[dim]);
1678 #endif
1679  }
1680  }
1681  fprintf(fzout, "\n");
1682  }
1683  fclose(fzout);
1684 }
1685 
1686 
1687 bool WaveFunctionTester::put(xmlNodePtr q)
1688 {
1689  myNode = q;
1690  xmlNodePtr tcur = q->children;
1691  while (tcur != NULL)
1692  {
1693  std::string cname((const char*)(tcur->name));
1694  if (cname == "delta_output")
1695  {
1696  outputDeltaVsError = true;
1697  DeltaVsError.put(tcur);
1698  }
1699  tcur = tcur->next;
1700  }
1701 
1702  bool success = putQMCInfo(q);
1703 
1704  return success;
1705 }
1706 
1708 {
1709  app_log() << " ===== runDerivTest =====\n";
1710  app_log() << " Testing derivatives" << std::endl;
1711  int nat = W.getTotalNum();
1712  MCWalkerConfiguration::PropertyContainer_t Properties(0, 0, 1, WP::MAXPROPERTIES);
1713  //pick the first walker
1714  const MCWalkerConfiguration::Walker_t& awalker = **W.begin();
1715  //copy the properties of the working walker
1716  Properties = awalker.Properties;
1717  //sample a new walker configuration and copy to ParticleSet::R
1718  W.R = awalker.R + deltaR;
1719 
1720  fout << "Position " << std::endl << W.R << std::endl;
1721 
1722  //W.R += deltaR;
1723  W.update();
1724  //ValueType psi = Psi.evaluate(W);
1725  Psi.evaluateLog(W);
1726  RealType eloc = H.evaluate(W);
1727  app_log() << " HamTest "
1728  << " Total " << eloc << std::endl;
1729  for (int i = 0; i < H.sizeOfObservables(); i++)
1730  app_log() << " HamTest " << H.getObservableName(i) << " " << H.getObservable(i) << std::endl;
1731  //RealType psi = Psi.evaluateLog(W);
1732  ParticleSet::ParticleGradient G(nat), G1(nat);
1733  ParticleSet::ParticleLaplacian L(nat), L1(nat);
1734  G = W.G;
1735  L = W.L;
1736  fout << "Gradients" << std::endl;
1737  for (int iat = 0; iat < W.R.size(); iat++)
1738  {
1739  for (int i = 0; i < 3; i++)
1740  fout << W.G[iat][i] << " ";
1741  fout << std::endl;
1742  }
1743  fout << "Laplaians" << std::endl;
1744  for (int iat = 0; iat < W.R.size(); iat++)
1745  {
1746  fout << W.L[iat] << " ";
1747  fout << std::endl;
1748  }
1749  opt_variables_type wfVars, wfvar_prime;
1750  //build optimizables from the wavefunction
1751  wfVars.clear();
1752  Psi.checkInVariables(wfVars);
1753  wfVars.resetIndex();
1754  Psi.checkOutVariables(wfVars);
1755  wfvar_prime = wfVars;
1756  wfVars.print(fout);
1757  int Nvars = wfVars.size();
1758  Vector<ValueType> Dsaved(Nvars);
1759  Vector<ValueType> HDsaved(Nvars);
1760  std::vector<RealType> PGradient(Nvars);
1761  std::vector<RealType> HGradient(Nvars);
1762  Psi.resetParameters(wfVars);
1763  Psi.evaluateLog(W);
1764 
1765  //reuse the sphere
1766  H.setPrimary(false);
1767 
1768  eloc = H.evaluate(W);
1769  Psi.evaluateDerivatives(W, wfVars, Dsaved, HDsaved);
1770  RealType FiniteDiff = 1e-6;
1771  QMCTraits::RealType dh = 1.0 / (2.0 * FiniteDiff);
1772  for (int i = 0; i < Nvars; i++)
1773  {
1774  for (int j = 0; j < Nvars; j++)
1775  wfvar_prime[j] = wfVars[j];
1776  wfvar_prime[i] = wfVars[i] + FiniteDiff;
1777  // Psi.checkOutVariables(wfvar_prime);
1778  Psi.resetParameters(wfvar_prime);
1779  W.update();
1780  W.G = 0;
1781  W.L = 0;
1782  RealType logpsiPlus = Psi.evaluateLog(W);
1783  H.evaluate(W);
1784  RealType elocPlus = H.getLocalEnergy() - H.getLocalPotential();
1785  wfvar_prime[i] = wfVars[i] - FiniteDiff;
1786  // Psi.checkOutVariables(wfvar_prime);
1787  Psi.resetParameters(wfvar_prime);
1788  W.update();
1789  W.G = 0;
1790  W.L = 0;
1791  RealType logpsiMinus = Psi.evaluateLog(W);
1792  H.evaluate(W);
1793  RealType elocMinus = H.getLocalEnergy() - H.getLocalPotential();
1794  PGradient[i] = (logpsiPlus - logpsiMinus) * dh;
1795  HGradient[i] = (elocPlus - elocMinus) * dh;
1796  }
1797  Psi.resetParameters(wfVars);
1798  fout << std::endl << "Deriv Numeric Analytic" << std::endl;
1799  for (int i = 0; i < Nvars; i++)
1800  fout << i << " " << PGradient[i] << " " << std::real(Dsaved[i]) << " " << (PGradient[i] - std::real(Dsaved[i]))
1801  << std::endl;
1802  fout << std::endl << "Hderiv Numeric Analytic" << std::endl;
1803  for (int i = 0; i < Nvars; i++)
1804  fout << i << " " << HGradient[i] << " " << std::real(HDsaved[i]) << " " << (HGradient[i] - std::real(HDsaved[i]))
1805  << std::endl;
1806 }
1807 
1808 
1810 {
1811  app_log() << " ===== runDerivNLPPTest =====\n";
1812  std::array<char, 16> fname;
1813  if (std::snprintf(fname.data(), fname.size(), "nlpp.%03d", OHMMS::Controller->rank()) < 0)
1814  throw std::runtime_error("Error generating name");
1815 
1816  std::ofstream nlout(fname.data());
1817  nlout.precision(15);
1818 
1819  app_log() << " Testing derivatives" << std::endl;
1820  int nat = W.getTotalNum();
1821  MCWalkerConfiguration::PropertyContainer_t Properties(0, 0, 1, WP::MAXPROPERTIES);
1822  //pick the first walker
1823  const MCWalkerConfiguration::Walker_t& awalker = **W.begin();
1824  //copy the properties of the working walker
1825  Properties = awalker.Properties;
1826  //sample a new walker configuration and copy to ParticleSet::R
1827  W.R = awalker.R + deltaR;
1828 
1829  //W.R += deltaR;
1830  W.update();
1831  //ValueType psi = Psi.evaluate(W);
1832  Psi.evaluateLog(W);
1833  RealType eloc = H.evaluate(W);
1834 
1835  app_log() << " HamTest "
1836  << " Total " << eloc << std::endl;
1837  for (int i = 0; i < H.sizeOfObservables(); i++)
1838  app_log() << " HamTest " << H.getObservableName(i) << " " << H.getObservable(i) << std::endl;
1839 
1840  //RealType psi = Psi.evaluateLog(W);
1841  ParticleSet::ParticleGradient G(nat), G1(nat);
1842  ParticleSet::ParticleLaplacian L(nat), L1(nat);
1843  G = W.G;
1844  L = W.L;
1845  nlout << "Gradients" << std::endl;
1846  for (int iat = 0; iat < W.R.size(); iat++)
1847  {
1848  for (int i = 0; i < 3; i++)
1849  nlout << W.G[iat][i] << " ";
1850  nlout << std::endl;
1851  }
1852  nlout << "Laplaians" << std::endl;
1853  for (int iat = 0; iat < W.R.size(); iat++)
1854  {
1855  nlout << W.L[iat] << " ";
1856  nlout << std::endl;
1857  }
1858  opt_variables_type wfVars, wfvar_prime;
1859  //build optimizables from the wavefunction
1860  wfVars.clear();
1861  Psi.checkInVariables(wfVars);
1862  wfVars.resetIndex();
1863  Psi.checkOutVariables(wfVars);
1864  wfvar_prime = wfVars;
1865  wfVars.print(nlout);
1866  int Nvars = wfVars.size();
1867  Vector<ValueType> Dsaved(Nvars);
1868  Vector<ValueType> HDsaved(Nvars);
1869  std::vector<RealType> PGradient(Nvars);
1870  std::vector<RealType> HGradient(Nvars);
1871  Psi.resetParameters(wfVars);
1872 
1873  Psi.evaluateLog(W);
1874 
1875  //reuse the sphere for non-local pp
1876  H.setPrimary(false);
1877 
1878  std::vector<RealType> ene(4), ene_p(4), ene_m(4);
1879  Psi.evaluateDerivatives(W, wfVars, Dsaved, HDsaved);
1880 
1881  ene[0] = H.evaluateValueAndDerivatives(W, wfVars, Dsaved, HDsaved);
1882  app_log() << "Check the energy " << eloc << " " << H.getLocalEnergy() << " " << ene[0] << std::endl;
1883 
1884  RealType FiniteDiff = 1e-6;
1885  QMCTraits::RealType dh = 1.0 / (2.0 * FiniteDiff);
1886  for (int i = 0; i < Nvars; i++)
1887  {
1888  for (int j = 0; j < Nvars; j++)
1889  wfvar_prime[j] = wfVars[j];
1890  wfvar_prime[i] = wfVars[i] + FiniteDiff;
1891  Psi.resetParameters(wfvar_prime);
1892  W.update();
1893  W.G = 0;
1894  W.L = 0;
1895  RealType logpsiPlus = Psi.evaluateLog(W);
1896  RealType elocPlus = H.evaluateVariableEnergy(W, true);
1897 
1898  //H.evaluate(W);
1899  //RealType elocPlus=H.getLocalEnergy()-H.getLocalPotential();
1900 
1901  wfvar_prime[i] = wfVars[i] - FiniteDiff;
1902  Psi.resetParameters(wfvar_prime);
1903  W.update();
1904  W.G = 0;
1905  W.L = 0;
1906  RealType logpsiMinus = Psi.evaluateLog(W);
1907  RealType elocMinus = H.evaluateVariableEnergy(W, true);
1908 
1909  //H.evaluate(W);
1910  //RealType elocMinus = H.getLocalEnergy()-H.getLocalPotential();
1911 
1912  PGradient[i] = (logpsiPlus - logpsiMinus) * dh;
1913  HGradient[i] = (elocPlus - elocMinus) * dh;
1914  }
1915 
1916  nlout << std::endl << "Deriv Numeric Analytic" << std::endl;
1917  for (int i = 0; i < Nvars; i++)
1918  nlout << i << " " << PGradient[i] << " " << Dsaved[i] << " " << (PGradient[i] - Dsaved[i]) << std::endl;
1919  nlout << std::endl << "Hderiv Numeric Analytic" << std::endl;
1920  for (int i = 0; i < Nvars; i++)
1921  nlout << i << " " << HGradient[i] << " " << HDsaved[i] << " " << (HGradient[i] - HDsaved[i]) << std::endl;
1922 }
1923 
1924 
1926 {
1927  app_log() << " ===== runDerivCloneTest =====\n";
1928  app_log() << " Testing derivatives clone" << std::endl;
1929  auto Rng1 = std::make_unique<RandomGenerator>();
1930  auto Rng2 = std::make_unique<RandomGenerator>();
1931  (*Rng1) = (*Rng2);
1932  auto w_clone = std::make_unique<MCWalkerConfiguration>(W);
1933  auto psi_clone = Psi.makeClone(*w_clone);
1934  auto h_clone = H.makeClone(*w_clone, *psi_clone);
1935  h_clone->setRandomGenerator(Rng2.get());
1936  H.setRandomGenerator(Rng1.get());
1937  // Add to Rng so the object is eventually deleted
1938  Rng.emplace_back(std::move(Rng1));
1939  h_clone->setPrimary(true);
1940  int nat = W.getTotalNum();
1942  //pick the first walker
1943  const MCWalkerConfiguration::Walker_t& awalker = **W.begin();
1944  // MCWalkerConfiguration::Walker_t* bwalker = *(w_clone->begin());
1945  // bwalker->R = awalker->R;
1946  W.R = awalker.R;
1947  W.update();
1948  w_clone->R = awalker.R;
1949  w_clone->update();
1950  opt_variables_type wfVars;
1951  //build optimizables from the wavefunction
1952  // wfVars.clear();
1953  Psi.checkInVariables(wfVars);
1954  wfVars.resetIndex();
1955  Psi.checkOutVariables(wfVars);
1956  wfVars.print(fout);
1957  int Nvars = wfVars.size();
1958  opt_variables_type wfvar_prime;
1959  // wfvar_prime.insertFrom(wfVars);
1960  // wfvar_prime.clear();
1961  psi_clone->checkInVariables(wfvar_prime);
1962  wfvar_prime.resetIndex();
1963  for (int j = 0; j < Nvars; j++)
1964  wfvar_prime[j] = wfVars[j];
1965  psi_clone->checkOutVariables(wfvar_prime);
1966  wfvar_prime.print(fout);
1967  psi_clone->resetParameters(wfvar_prime);
1968  Psi.resetParameters(wfVars);
1969  Vector<ValueType> Dsaved(Nvars, 0), og_Dsaved(Nvars, 0);
1970  Vector<ValueType> HDsaved(Nvars, 0), og_HDsaved(Nvars, 0);
1971  std::vector<RealType> PGradient(Nvars, 0), og_PGradient(Nvars, 0);
1972  std::vector<RealType> HGradient(Nvars, 0), og_HGradient(Nvars, 0);
1973  ValueType logpsi2 = psi_clone->evaluateLog(*w_clone);
1974  RealType eloc2 = h_clone->evaluate(*w_clone);
1975  psi_clone->evaluateDerivatives(*w_clone, wfvar_prime, Dsaved, HDsaved);
1976  ValueType logpsi1 = Psi.evaluateLog(W);
1977  RealType eloc1 = H.evaluate(W);
1978  Psi.evaluateDerivatives(W, wfVars, og_Dsaved, og_HDsaved);
1979  app_log() << "log (original) = " << logpsi1 << " energy = " << eloc1 << std::endl;
1980  for (int i = 0; i < H.sizeOfObservables(); i++)
1981  app_log() << " HamTest " << H.getObservableName(i) << " " << H.getObservable(i) << std::endl;
1982  app_log() << "log (clone) = " << logpsi2 << " energy = " << eloc2 << std::endl;
1983  for (int i = 0; i < h_clone->sizeOfObservables(); i++)
1984  app_log() << " HamTest " << h_clone->getObservableName(i) << " " << h_clone->getObservable(i) << std::endl;
1985  // app_log()<<" Saved quantities:"<< std::endl;
1986  // for(int i=0;i<Nvars;i++) app_log()<<Dsaved[i]<<" "<<og_Dsaved[i]<<" "<<HDsaved[i]<<" "<<og_HDsaved[i]<< std::endl;
1987  RealType FiniteDiff = 1e-6;
1988  QMCTraits::RealType dh = 1.0 / (2.0 * FiniteDiff);
1989  for (int i = 0; i < Nvars; i++)
1990  {
1991  for (int j = 0; j < Nvars; j++)
1992  wfvar_prime[j] = wfVars[j];
1993  wfvar_prime[i] = wfVars[i] + FiniteDiff;
1994  psi_clone->resetParameters(wfvar_prime);
1995  w_clone->update();
1996  w_clone->G = 0;
1997  w_clone->L = 0;
1998  RealType logpsiPlus = psi_clone->evaluateLog(*w_clone);
1999  h_clone->evaluate(*w_clone);
2000  RealType elocPlus = h_clone->getLocalEnergy() - h_clone->getLocalPotential();
2001  wfvar_prime[i] = wfVars[i] - FiniteDiff;
2002  psi_clone->resetParameters(wfvar_prime);
2003  w_clone->update();
2004  w_clone->G = 0;
2005  w_clone->L = 0;
2006  RealType logpsiMinus = psi_clone->evaluateLog(*w_clone);
2007  h_clone->evaluate(*w_clone);
2008  RealType elocMinus = h_clone->getLocalEnergy() - h_clone->getLocalPotential();
2009  PGradient[i] = (logpsiPlus - logpsiMinus) * dh;
2010  HGradient[i] = (elocPlus - elocMinus) * dh;
2011  }
2012  fout << "CLONE" << std::endl;
2013  fout << std::endl << " Deriv Numeric Analytic" << std::endl;
2014  for (int i = 0; i < Nvars; i++)
2015  fout << i << " " << PGradient[i] << " " << std::real(Dsaved[i]) << " "
2016  << (PGradient[i] - std::real(Dsaved[i])) / PGradient[i] << std::endl;
2017  fout << std::endl << " Hderiv Numeric Analytic" << std::endl;
2018  for (int i = 0; i < Nvars; i++)
2019  fout << i << " " << HGradient[i] << " " << std::real(HDsaved[i]) << " "
2020  << (HGradient[i] - std::real(HDsaved[i])) / HGradient[i] << std::endl;
2021  for (int i = 0; i < Nvars; i++)
2022  {
2023  for (int j = 0; j < Nvars; j++)
2024  wfvar_prime[j] = wfVars[j];
2025  wfvar_prime[i] = wfVars[i] + FiniteDiff;
2026  Psi.resetParameters(wfvar_prime);
2027  W.update();
2028  W.G = 0;
2029  W.L = 0;
2030  RealType logpsiPlus = Psi.evaluateLog(W);
2031  H.evaluate(W);
2032  RealType elocPlus = H.getLocalEnergy() - H.getLocalPotential();
2033  wfvar_prime[i] = wfVars[i] - FiniteDiff;
2034  Psi.resetParameters(wfvar_prime);
2035  W.update();
2036  W.G = 0;
2037  W.L = 0;
2038  RealType logpsiMinus = Psi.evaluateLog(W);
2039  H.evaluate(W);
2040  RealType elocMinus = H.getLocalEnergy() - H.getLocalPotential();
2041  PGradient[i] = (logpsiPlus - logpsiMinus) * dh;
2042  HGradient[i] = (elocPlus - elocMinus) * dh;
2043  }
2044  fout << "ORIGINAL" << std::endl;
2045  fout << std::endl << " Deriv Numeric Analytic" << std::endl;
2046  for (int i = 0; i < Nvars; i++)
2047  fout << i << " " << PGradient[i] << " " << std::real(Dsaved[i]) << " "
2048  << (PGradient[i] - std::real(Dsaved[i])) / PGradient[i] << std::endl;
2049  fout << std::endl << " Hderiv Numeric Analytic" << std::endl;
2050  for (int i = 0; i < Nvars; i++)
2051  fout << i << " " << HGradient[i] << " " << std::real(HDsaved[i]) << " "
2052  << (HGradient[i] - std::real(HDsaved[i])) / HGradient[i] << std::endl;
2053 }
2054 
2056 {
2057  app_log() << " ===== runNodePlot =====\n";
2058  xmlNodePtr kids = myNode->children;
2059  std::string doEnergy("no");
2060  ParameterSet aAttrib;
2061  aAttrib.add(doEnergy, "energy");
2062  aAttrib.put(myNode);
2063  std::vector<int> Grid;
2064  while (kids != NULL)
2065  {
2066  std::string cname((const char*)(kids->name));
2067  if (cname == "grid")
2068  putContent(Grid, kids);
2069  kids = kids->next;
2070  }
2071  ParticleSet::ParticlePos R_cart(1);
2072  R_cart.setUnit(PosUnit::Cartesian);
2073  ParticleSet::ParticlePos R_unit(1);
2074  R_unit.setUnit(PosUnit::Lattice);
2075  Walker_t& thisWalker(**(W.begin()));
2076  W.loadWalker(thisWalker, true);
2077  Walker_t::WFBuffer_t& w_buffer(thisWalker.DataSet);
2078  Psi.copyFromBuffer(W, w_buffer);
2079 #if OHMMS_DIM == 2
2080  assert(Grid.size() == 2);
2081  int nat = W.getTotalNum();
2082  int nup = W.getTotalNum() / 2; //std::max(W.getSpeciesSet().findSpecies("u"),W.getSpeciesSet().findSpecies("d"));
2083  // for(int iat(0);iat<nat;iat++)
2084  // e_out<<iat<<" "<<W[0]->R[iat][0]<<" "<<W[0]->R[iat][1]<< std::endl;
2085  RealType overG0(1.0 / Grid[0]);
2086  RealType overG1(1.0 / Grid[1]);
2087  for (int iat(0); iat < nat; iat++)
2088  {
2089  W.update();
2090  std::stringstream fn;
2091  fn << RootName.c_str() << ".ratios." << iat << ".py";
2092  std::ofstream plot_out(fn.str().c_str());
2093  plot_out.precision(6);
2094  // plot_out<<"#e x y ratio"<< std::endl;
2095  R_unit[0][0] = 1.0;
2096  R_unit[0][1] = 1.0;
2097  W.convert2Cart(R_unit, R_cart);
2098  RealType xmax = R_cart[0][0];
2099  RealType ymax = R_cart[0][1];
2100  plot_out << "import matplotlib\n";
2101  plot_out << "import numpy as np\n";
2102  plot_out << "import matplotlib.cm as cm\n";
2103  plot_out << "import matplotlib.mlab as mlab\n";
2104  plot_out << "import matplotlib.pyplot as plt\n";
2105  plot_out << std::endl;
2106  plot_out << "matplotlib.rcParams['xtick.direction'] = 'out'\n";
2107  plot_out << "matplotlib.rcParams['ytick.direction'] = 'out'\n";
2108  plot_out << std::endl;
2109  plot_out << "x = np.arange(0, " << xmax << ", " << xmax * overG0 << ")\n";
2110  plot_out << "y = np.arange(0, " << ymax << ", " << ymax * overG1 << ")\n";
2111  plot_out << "X, Y = np.meshgrid(x, y)\n";
2112  plot_out << "Z = [";
2113  for (int i = 0; i < Grid[0]; i++)
2114  {
2115  plot_out << "[ ";
2116  for (int j = 0; j < Grid[1]; j++)
2117  {
2118  R_unit[0][0] = overG0 * RealType(i);
2119  R_unit[0][1] = overG1 * RealType(j);
2120  W.convert2Cart(R_unit, R_cart);
2121  PosType dr(R_cart[0] - W.R[iat]);
2122  W.makeMove(iat, dr);
2123  ValueType aratio = Psi.calcRatio(W, iat);
2124  W.rejectMove(iat);
2125  Psi.rejectMove(iat);
2126  plot_out << aratio << ", ";
2127  }
2128  plot_out << "], ";
2129  }
2130  plot_out << "]\n";
2131  plot_out << "up_y=[";
2132  for (int ix(0); ix < nup; ix++)
2133  {
2134  RealType yy(W[0]->R[ix][0]);
2135  while (yy > xmax)
2136  yy -= xmax;
2137  while (yy < 0)
2138  yy += xmax;
2139  plot_out << yy << ", ";
2140  }
2141  plot_out << "]\n";
2142  plot_out << "up_x=[";
2143  for (int ix(0); ix < nup; ix++)
2144  {
2145  RealType yy(W[0]->R[ix][1]);
2146  while (yy > ymax)
2147  yy -= ymax;
2148  while (yy < 0)
2149  yy += ymax;
2150  plot_out << yy << ", ";
2151  }
2152  plot_out << "]\n";
2153  plot_out << "dn_y=[";
2154  for (int ix(nup); ix < nat; ix++)
2155  {
2156  RealType yy(W[0]->R[ix][0]);
2157  while (yy > xmax)
2158  yy -= xmax;
2159  while (yy < 0)
2160  yy += xmax;
2161  plot_out << yy << ", ";
2162  }
2163  plot_out << "]\n";
2164  plot_out << "dn_x=[";
2165  for (int ix(nup); ix < nat; ix++)
2166  {
2167  RealType yy(W[0]->R[ix][1]);
2168  while (yy > ymax)
2169  yy -= ymax;
2170  while (yy < 0)
2171  yy += ymax;
2172  plot_out << yy << ", ";
2173  }
2174  plot_out << "]\n";
2175  plot_out << "matplotlib.rcParams['contour.negative_linestyle'] = 'solid'\n";
2176  plot_out << "plt.figure()\n";
2177  plot_out << "CS = plt.contourf(X, Y, Z, 5, cmap=cm.gray)\n";
2178  plot_out << "CS2 = plt.contour(X, Y, Z, colors='k',levels=[0])\n";
2179  plot_out << "PTu = plt.scatter(up_x,up_y, c='r', marker='o')\n";
2180  plot_out << "PTd = plt.scatter(dn_x,dn_y, c='b', marker='d')\n";
2181  plot_out << "plt.clabel(CS2, fontsize=9, inline=1)\n";
2182  plot_out << "plt.title('2D Nodal Structure')\n";
2183  plot_out << "plt.xlim(0," << ymax * (1.0 - overG1) << ")\n";
2184  plot_out << "plt.ylim(0," << xmax * (1.0 - overG0) << ")\n";
2185  fn.str("");
2186  fn << RootName.c_str() << ".ratios." << iat << ".png";
2187  plot_out << "plt.savefig('" << fn.str().c_str() << "', bbox_inches='tight', pad_inches=0.01 )\n";
2188  }
2189 #elif OHMMS_DIM == 3
2190 // assert(Grid.size()==3);
2191 //
2192 // RealType overG0(1.0/Grid[0]);
2193 // RealType overG1(1.0/Grid[1]);
2194 // RealType overG2(1.0/Grid[2]);
2195 // int iat(0);
2196 // W.update();
2197 // plot_out<<"#e x y z ratio"<< std::endl;
2198 //
2199 // for(int i=0;i<Grid[0];i++)
2200 // for(int j=0;j<Grid[1];j++)
2201 // for(int k=0;k<Grid[2];k++)
2202 // {
2203 // R_unit[iat][0]=overG0*RealType(i);
2204 // R_unit[iat][1]=overG1*RealType(j);
2205 // R_unit[iat][2]=overG2*RealType(k);
2206 // W.convert2Cart(R_unit,R_cart);
2207 // PosType dr(R_cart[iat]-W.R[iat]);
2208 //
2209 // W.makeMove(iat,dr);
2210 // RealType aratio = Psi.ratio(W,iat);
2211 // W.rejectMove(iat);
2212 // Psi.rejectMove(iat);
2213 // plot_out<<iat<<" "<<R_cart[iat][0]<<" "<<R_cart[iat][1]<<" "<<R_cart[iat][2]<<" "<<aratio<<" "<< std::endl;
2214 // }
2215 #endif
2216 }
2217 
2218 FiniteDiffErrData::FiniteDiffErrData() : particleIndex(0), gradientComponentIndex(0), outputFile("delta.dat") {}
2219 
2220 bool FiniteDiffErrData::put(xmlNodePtr q)
2221 {
2222  ParameterSet param;
2223  param.add(outputFile, "file");
2224  param.add(particleIndex, "particle_index");
2225  param.add(gradientComponentIndex, "gradient_index");
2226  bool s = param.put(q);
2227  return s;
2228 }
2229 
2230 
2231 } // namespace qmcplusplus
base class for Single-particle orbital sets
Definition: SPOSet.h:46
RealType evaluateLog(ParticleSet &P)
evalaute the log (internally gradients and laplacian) of the trial wavefunction.
void runRatioTest()
the basic ratios check
T Sum(const ParticleAttrib< T > &pa)
WaveFunctionTester(const ProjectData &project_data, MCWalkerConfiguration &w, TrialWaveFunction &psi, QMCHamiltonian &h, ParticleSetPool &ptclPool, Communicate *comm)
Constructor.
FullPrecRealType getLocalPotential()
PropertyContainer_t Properties
scalar properties of a walker
Definition: Walker.h:125
std::string getObservableName(int i) const
return the name of the i-th observable
void rejectMove(int iat)
restore to the original state
ValueType calcRatio(ParticleSet &P, int iat, ComputeType ct=ComputeType::ALL)
compute psi(R_new) / psi(R_current) ratio It returns a complex value if the wavefunction is complex...
WFBuffer_t DataSet
buffer for the data for particle-by-particle update
Definition: Walker.h:135
A set of walkers that are to be advanced by Metropolis Monte Carlo.
void copyFromBuffer(ParticleSet &P, WFBufferType &buf)
copy all the wavefunction components from buffer.
QMCTraits::RealType real
helper functions for EinsplineSetBuilder
Definition: Configuration.h:43
int rank() const
return the rank
Definition: Communicate.h:116
Tensor< T, D >::Type_t det(const Tensor< T, D > &a)
Definition: TensorOps.h:838
void evaluateDerivatives(ParticleSet &P, const opt_variables_type &optvars, Vector< ValueType > &dlogpsi, Vector< ValueType > &dhpsioverpsi)
evaluate derivatives of KE wrt optimizable varibles
QTBase::RealType RealType
Definition: Configuration.h:58
void finiteDifferencePoints(RealType delta, MCWalkerConfiguration &W, PosChangeVector &positions)
Generate points to evaluate.
std::string RootName
root of all the output files
Definition: QMCDriver.h:317
MakeReturn< UnaryNode< FnFabs, typename CreateLeaf< Vector< T1, C1 > >::Leaf_t > >::Expression_t abs(const Vector< T1, C1 > &l)
Parent class for all grids.
Definition: Grid.h:43
size_t getTotalNum() const
Definition: ParticleSet.h:493
void acceptMove(ParticleSet &P, int iat, bool safe_to_delay=false)
update the state with the new data
std::ostream & app_log()
Definition: OutputManager.h:65
xmlNodePtr qmcNode
pointer to qmc node in xml file
Definition: QMCDriver.h:310
class ProjectData
Definition: ProjectData.h:36
void saveProperty(IT first)
save the values of Hamiltonian elements to the Properties
MakeReturn< UnaryNode< FnSin, typename CreateLeaf< Vector< T1, C1 > >::Leaf_t > >::Expression_t sin(const Vector< T1, C1 > &l)
FullPrecRealType evaluateValueAndDerivatives(ParticleSet &P, const opt_variables_type &optvars, Vector< ValueType > &dlogpsi, Vector< ValueType > &dhpsioverpsi)
evaluate energy and derivatives wrt to the variables
Abstract class to manage operations on pair data between two ParticleSets.
Definition: DistanceTable.h:38
void resetParameters(const opt_variables_type &active)
Set values of parameters in each component from the global list.
A ParticleSet that handles virtual moves of a selected particle of a given physical ParticleSet Virtu...
void makeMoves(const ParticleSet &refp, int jel, const std::vector< PosType > &deltaV, bool sphere=false, int iat=-1)
move virtual particles to new postions and update distance tables
RealType getObservable(int i) const
return the value of the i-th observable
Collection of Local Energy Operators.
float real(const float &c)
real part of a scalar. Cannot be replaced by std::real due to AFQMC specific needs.
void runRatioV()
test ratios with virtual moves
void allocate()
allocate the data
Definition: PooledMemory.h:104
int size() const
return the size of the orbital set Ye: this needs to be replaced by getOrbitalSetSize(); ...
Definition: SPOSet.h:75
const std::vector< std::unique_ptr< Determinant_t > > Dets
container for the DiracDeterminants
Definition: SlaterDet.h:33
#define Random
abstract base class for QMC engines
Definition: QMCDriver.h:71
void resetIndex()
reset Index
void update(bool skipSK=false)
update the internal data
Attaches a unit to a Vector for IO.
bool run() override
Test the evaluation of the wavefunction, gradient and laplacian by comparing to the numerical evaluat...
Communicate * Controller
Global Communicator for a process.
Definition: Communicate.cpp:35
ParticleLaplacian L
laplacians of the particles
Definition: ParticleSet.h:85
RealType updateBuffer(ParticleSet &P, WFBufferType &buf, bool fromscratch=false)
update all the wavefunction components in buffer.
#define OHMMS_DIM
Definition: config.h:64
T Dot(const ParticleAttrib< TinyVector< T, D >> &pa, const ParticleAttrib< TinyVector< T, D >> &pb)
std::unique_ptr< TrialWaveFunction > makeClone(ParticleSet &tqp) const
bool put(std::istream &is) override
read from std::istream
Definition: ParameterSet.h:42
ParameterSet m_param
store any parameter that has to be read from a file
Definition: QMCDriver.h:320
for(int i=0;i< size_test;++i) CHECK(Approx(gauss_random_vals[offset_for_rs+i])
Wrapping information on parallelism.
Definition: Communicate.h:68
WaveFunctionComponent::LogValue LogValue
type definition
Specialized paritlce class for atomistic simulations.
Definition: ParticleSet.h:55
std::vector< ValueType > ValueVector
WalkerList_t::iterator iterator
FIX: a type alias of iterator for an object should not be for just one of many objects it holds...
float imag(const float &c)
imaginary part of a scalar. Cannot be replaced by std::imag due to AFQMC specific needs...
MakeReturn< UnaryNode< FnCos, typename CreateLeaf< Vector< T1, C1 > >::Leaf_t > >::Expression_t cos(const Vector< T1, C1 > &l)
class to handle a set of parameters
Definition: ParameterSet.h:27
double norm(const zVec &c)
Definition: VectorOps.h:118
WalkerProperties::Indexes WP
Definition: ParticleSet.cpp:34
QTBase::ValueType ValueType
Definition: Configuration.h:60
void makeGaussRandom(std::vector< TinyVector< T, D >> &a)
void assignUniformRand(T *restrict a, unsigned n, RG &rng)
void rejectMove(Index_t iat)
reject a proposed move in regular mode
std::vector< PositionChange > PosChangeVector
bool putQMCInfo(xmlNodePtr cur)
Parses the xml input file for parameter definitions for a single qmc simulation.
Definition: QMCDriver.cpp:399
Manage a collection of ParticleSet objects.
ParticleGradient G
gradients of the particles
Definition: ParticleSet.h:83
void convert2Cart(const ParticlePos &pin, ParticlePos &pout)
OrbitalSetTraits< ValueType >::ValueVector ValueVector
Definition: SPOSet.h:49
void saveWalker(Walker_t &awalker)
save this to awalker
class to handle a set of variables that can be modified during optimizations
Definition: VariableSet.h:49
GradType evalGradSource(ParticleSet &P, ParticleSet &source, int iat)
Returns the logarithmic gradient of the trial wave function with respect to the iat^th atom of the so...
void registerData(ParticleSet &P, WFBufferType &buf)
register all the wavefunction components in buffer.
std::vector< std::unique_ptr< WaveFunctionComponent > > const & getOrbitals()
QMCHamiltonian & H
Hamiltonian.
Definition: QMCDriver.h:329
int sizeOfObservables() const
return the size of observables
ParticlePos R
Position.
Definition: ParticleSet.h:79
QTBase::PosType PosType
Definition: Configuration.h:61
#define APP_ABORT(msg)
Widely used but deprecated fatal error macros from legacy code.
Definition: AppAbort.h:27
FullPrecRealType getLocalEnergy()
MakeReturn< UnaryNode< FnExp, typename CreateLeaf< Vector< T1, C1 > >::Leaf_t > >::Expression_t exp(const Vector< T1, C1 > &l)
void runBasicTest()
basic tests for G and L
FiniteDifference(size_t ndim_in, FiniteDiffType fd_type=FiniteDiff_Richardson)
ParticleSet::ParticlePos deltaR
void checkOutVariables(const opt_variables_type &o)
Check out optimizable variables Assign index mappings from global list (o) to local values in each co...
bool put(xmlNodePtr q) override
MCWalkerConfiguration & W
walker ensemble
Definition: QMCDriver.h:323
void loadWalker(Walker_t &awalker, bool pbyp)
load a Walker_t to the current ParticleSet
void clear()
clear the variable set
Definition: VariableSet.cpp:28
size_type size() const
return the size
Definition: VariableSet.h:88
bool putContent(T &a, xmlNodePtr cur)
replaces a&#39;s value with the first "element" in the "string" returned by XMLNodeString{cur}.
Definition: libxmldefs.h:88
MakeReturn< UnaryNode< FnLog, typename CreateLeaf< Vector< T1, C1 > >::Leaf_t > >::Expression_t log(const Vector< T1, C1 > &l)
void auxHevaluate(ParticleSet &P)
void add(PDT &aparam, const std::string &aname_in, std::vector< PDT > candidate_values={}, TagStatus status=TagStatus::OPTIONAL)
add a new parameter corresponding to an xmlNode <parameter>
size_type current() const
Definition: PooledMemory.h:76
Define determinant operators.
MakeReturn< UnaryNode< FnSqrt, typename CreateLeaf< Vector< T1, C1 > >::Leaf_t > >::Expression_t sqrt(const Vector< T1, C1 > &l)
size_type size() const
return the size of the data
Definition: PooledMemory.h:73
Class to represent a many-body trial wave function.
void makeMove(Index_t iat, const SingleParticlePos &displ, bool maybe_accept=true)
move the iat-th particle to active_pos_
GradType evalGrad(ParticleSet &P, int iat)
Indexes
an enum denoting index of physical properties
FullPrecRealType evaluate(ParticleSet &P)
evaluate Local Energy
ParticleAttrib< SingleParticlePos > ParticlePos
Definition: Configuration.h:92
TrialWaveFunction & Psi
trial function
Definition: QMCDriver.h:326
RealType Tau
timestep
Definition: QMCDriver.h:299
std::vector< std::unique_ptr< DistanceTable > > DistTables
distance tables that need to be updated by moving this ParticleSet
Definition: ParticleSet.h:630
const PoolType & getPool() const
get the Pool object
FullPrecRealType evaluateVariableEnergy(ParticleSet &P, bool free_nlpp)
evaluate energy
traits for QMC variables
Definition: Configuration.h:49
UPtrVector< RandomBase< FullPrecRealType > > Rng
Random number generators.
Definition: QMCDriver.h:341
void computeFiniteDiff(RealType delta, PosChangeVector &positions, ValueVector &values, ParticleSet::ParticleGradient &G_fd, ParticleSet::ParticleLaplacian &L_fd)
Compute finite difference after log psi is computed for each point.
iterator end()
return the last iterator, [begin(), end())
void clear()
clear the data and set Current=0
Definition: PooledMemory.h:92
void setRandomGenerator(RandomBase< FullPrecRealType > *rng)
void setPrimary(bool primary)
set PRIMARY bit of all the components
void acceptMove(Index_t iat)
accept the move and update the particle attribute by the proposed move in regular mode ...
void computeNumericalGrad(RealType delta, ParticleSet::ParticleGradient &G_fd, ParticleSet::ParticleLaplacian &L_fd)
std::unique_ptr< QMCHamiltonian > makeClone(ParticleSet &qp, TrialWaveFunction &psi) const
return a clone
void print(std::ostream &os, int leftPadSpaces=0, bool printHeader=false) const
void computeFiniteDiffLowOrder(RealType delta, PosChangeVector &positions, ValueVector &values, ParticleSet::ParticleGradient &G_fd, ParticleSet::ParticleLaplacian &L_fd)
void runCloneTest()
test clone implementations of new wavefunctions and operators
Declaration of a MCWalkerConfiguration.
A container class to represent a walker.
Definition: Walker.h:49
ValueType calcRatioGrad(ParticleSet &P, int iat, GradType &grad_iat)
compute psi(R_new) / psi(R_current) ratio and ln(psi(R_new)) gradients It returns a complex value if...
bool checkGradientAtConfiguration(MCWalkerConfiguration::Walker_t *W1, std::stringstream &fail_log, bool &ignore)
void evaluateRatios(const VirtualParticleSet &VP, std::vector< ValueType > &ratios, ComputeType ct=ComputeType::ALL)
compulte multiple ratios to handle non-local moves and other virtual moves
void resetProperty(FullPrecRealType logpsi, FullPrecRealType sigN, FullPrecRealType ene)
reset the property of a walker
Definition: Walker.h:296
void checkInVariables(opt_variables_type &o)
Check in an optimizable parameter.
ParticlePos R
The configuration vector (3N-dimensional vector to store the positions of all the particles for a sin...
Definition: Walker.h:114
void computeFiniteDiffRichardson(RealType delta, PosChangeVector &positions, ValueVector &values, ParticleSet::ParticleGradient &G_fd, ParticleSet::ParticleLaplacian &L_fd)
void randomize(ParticleAttrib< TinyVector< T, D >> &displ, T fac)
bool checkGradients(int lower_iat, int upper_iat, ParticleSet::ParticleGradient &G, ParticleSet::ParticleLaplacian &L, ParticleSet::ParticleGradient &G_fd, ParticleSet::ParticleLaplacian &L_fd, std::stringstream &log, int indent=0)
iterator begin()
return the first iterator