forked from QMCPACK/qmcpack
/
RadialJastrowBuilder.cpp
576 lines (529 loc) · 18.4 KB
/
RadialJastrowBuilder.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
//////////////////////////////////////////////////////////////////////////////////////
// This file is distributed under the University of Illinois/NCSA Open Source License.
// See LICENSE file in top directory for details.
//
// Copyright (c) 2016 Jeongnim Kim and QMCPACK developers.
//
// File developed by: Luke Shulenburger, lshulen@sandia.gov, Sandia National Laboratories
// Jaron T. Krogel, krogeljt@ornl.gov, Oak Ridge National Laboratory
//
// File created by: Luke Shulenburger, lshulen@sandia.gov, Sandia National Laboratories
//////////////////////////////////////////////////////////////////////////////////////
#include "RadialJastrowBuilder.h"
#if defined(ENABLE_SOA)
#include "QMCWaveFunctions/Jastrow/J1OrbitalSoA.h"
#include "QMCWaveFunctions/Jastrow/J2OrbitalSoA.h"
#else
#include "QMCWaveFunctions/Jastrow/OneBodyJastrowOrbital.h"
#include "QMCWaveFunctions/Jastrow/TwoBodyJastrowOrbital.h"
#endif
#if defined(QMC_CUDA)
#if defined(ENABLE_SOA)
#include "QMCWaveFunctions/Jastrow/OneBodyJastrowOrbitalBspline.h"
#include "QMCWaveFunctions/Jastrow/TwoBodyJastrowOrbitalBspline.h"
#else
#include "QMCWaveFunctions/Jastrow/OneBodyJastrowOrbitalBsplineAoS.h"
#include "QMCWaveFunctions/Jastrow/TwoBodyJastrowOrbitalBsplineAoS.h"
#endif
#endif
#include "QMCWaveFunctions/Jastrow/DiffOneBodyJastrowOrbital.h"
#include "QMCWaveFunctions/Jastrow/DiffOneBodySpinJastrowOrbital.h"
#include "QMCWaveFunctions/Jastrow/DiffTwoBodyJastrowOrbital.h"
#include "QMCWaveFunctions/Jastrow/OneBodySpinJastrowOrbital.h"
#include "QMCWaveFunctions/Jastrow/RPAJastrow.h"
#include "LongRange/LRHandlerBase.h"
#include "QMCWaveFunctions/Jastrow/SplineFunctors.h"
#include "QMCWaveFunctions/Jastrow/LRBreakupUtilities.h"
#include "LongRange/LRRPAHandlerTemp.h"
#include "QMCWaveFunctions/Jastrow/LRBreakupUtilities.h"
#include "QMCWaveFunctions/Jastrow/BsplineFunctor.h"
#include "QMCWaveFunctions/Jastrow/PadeFunctors.h"
#include "QMCWaveFunctions/Jastrow/ShortRangeCuspFunctor.h"
#include "QMCWaveFunctions/Jastrow/UserFunctor.h"
#include <iostream>
namespace qmcplusplus
{
RadialJastrowBuilder::RadialJastrowBuilder(ParticleSet& target, TrialWaveFunction& psi, ParticleSet& source)
: WaveFunctionComponentBuilder(target, psi), SourcePtcl(&source)
{
ClassName = "RadialJastrowBuilder";
NameOpt = "0";
TypeOpt = "unknown";
Jastfunction = "unknown";
SpinOpt = "no";
}
RadialJastrowBuilder::RadialJastrowBuilder(ParticleSet& target, TrialWaveFunction& psi)
: WaveFunctionComponentBuilder(target, psi), SourcePtcl(NULL)
{
ClassName = "RadialJastrowBuilder";
NameOpt = "0";
TypeOpt = "unknown";
Jastfunction = "unknown";
SpinOpt = "no";
}
// helper method for dealing with functor incompatible with Open Boundaries
void RadialJastrowBuilder::guardAgainstOBC()
{
if (targetPtcl.Lattice.SuperCellEnum == SUPERCELL_OPEN)
{
app_error() << Jastfunction << " relies on the total density for its form\n";
app_error() << "but open boundary conditions are requested. Please choose other forms of Jastrow\n";
}
}
// helper method for dealing with functor incompatible with PBC
void RadialJastrowBuilder::guardAgainstPBC()
{
if (targetPtcl.Lattice.SuperCellEnum != SUPERCELL_OPEN)
{
app_error() << Jastfunction << " does not support a cutoff, but is requested with\n";
app_error() << "periodic boundary conditions, please choose other forms of Jastrow\n";
}
}
// quick helper class to allow use of RPA
class RPAFunctor
{};
// helper class to simplify and localize ugly ifdef stuff for types
template<class RadFuncType>
class JastrowTypeHelper
{
public:
#if defined(ENABLE_SOA)
using J1OrbitalType = J1OrbitalSoA<RadFuncType>;
using J2OrbitalType = J2OrbitalSoA<RadFuncType>;
#else
using J1OrbitalType = OneBodyJastrowOrbital<RadFuncType>;
using J2OrbitalType = TwoBodyJastrowOrbital<RadFuncType>;
#endif
using DiffJ1OrbitalType = DiffOneBodyJastrowOrbital<RadFuncType>;
using DiffJ2OrbitalType = DiffTwoBodyJastrowOrbital<RadFuncType>;
// spin polarized J1
using J1OrbitalTypeSpin = OneBodySpinJastrowOrbital<RadFuncType>;
using DiffJ1OrbitalTypeSpin = DiffOneBodyJastrowOrbital<RadFuncType>;
};
template<>
class JastrowTypeHelper<BsplineFunctor<RadialJastrowBuilder::RealType>>
{
public:
using RadFuncType = BsplineFunctor<RadialJastrowBuilder::RealType>;
#if defined(QMC_CUDA) && defined(ENABLE_SOA)
using J1OrbitalType = OneBodyJastrowOrbitalBspline<RadFuncType>;
using J2OrbitalType = TwoBodyJastrowOrbitalBspline<RadFuncType>;
#endif
#if defined(QMC_CUDA) && !defined(ENABLE_SOA)
using J1OrbitalType = OneBodyJastrowOrbitalBsplineAoS;
using J2OrbitalType = TwoBodyJastrowOrbitalBsplineAoS;
#endif
#if !defined(QMC_CUDA) && defined(ENABLE_SOA)
using J1OrbitalType = J1OrbitalSoA<RadFuncType>;
using J2OrbitalType = J2OrbitalSoA<RadFuncType>;
#endif
#if !defined(QMC_CUDA) && !defined(ENABLE_SOA)
using J1OrbitalType = OneBodyJastrowOrbital<RadFuncType>;
using J2OrbitalType = TwoBodyJastrowOrbital<RadFuncType>;
#endif
using DiffJ1OrbitalType = DiffOneBodyJastrowOrbital<RadFuncType>;
using DiffJ2OrbitalType = DiffTwoBodyJastrowOrbital<RadFuncType>;
// spin polarized J1
using J1OrbitalTypeSpin = OneBodySpinJastrowOrbital<RadFuncType>;
using DiffJ1OrbitalTypeSpin = DiffOneBodyJastrowOrbital<RadFuncType>;
};
template<class RadFuncType>
void RadialJastrowBuilder::initTwoBodyFunctor(RadFuncType& functor, double fac)
{}
template<>
void RadialJastrowBuilder::initTwoBodyFunctor(BsplineFunctor<RealType>& bfunc, double fac)
{
if (targetPtcl.Lattice.SuperCellEnum == SUPERCELL_OPEN) // for open systems, do nothing
{
return;
}
app_log() << " Initializing Two-Body with RPA Jastrow " << std::endl;
std::vector<RealType> rpaValues;
int npts = bfunc.NumParams;
if (rpaValues.empty())
{
rpaValues.resize(npts);
LRRPAHandlerTemp<RPABreakup<RealType>, LPQHIBasis> rpa(targetPtcl, -1.0);
rpa.Breakup(targetPtcl, -1.0);
RealType dr = bfunc.cutoff_radius / static_cast<RealType>(npts);
RealType r = 0;
for (int i = 0; i < npts; i++)
{
rpaValues[i] = rpa.evaluate(r, 1.0 / r); //y[i]=fac*rpa.evaluate(r,1.0/r);
r += dr;
}
}
RealType last = rpaValues[npts - 1];
for (int i = 0; i < npts; i++)
bfunc.Parameters[i] = fac * (rpaValues[i] - last);
bfunc.reset();
}
template<class RadFuncType>
bool RadialJastrowBuilder::createJ2(xmlNodePtr cur)
{
ReportEngine PRE(ClassName, "createJ2(xmlNodePtr)");
using RT = typename RadFuncType::real_type;
using J2OrbitalType = typename JastrowTypeHelper<RadFuncType>::J2OrbitalType;
using DiffJ2OrbitalType = typename JastrowTypeHelper<RadFuncType>::DiffJ2OrbitalType;
std::string j2name = "J2_" + Jastfunction;
SpeciesSet& species(targetPtcl.getSpeciesSet());
int taskid = (targetPsi.is_manager()) ? targetPsi.getGroupID() : -1;
auto* J2 = new J2OrbitalType(targetPtcl, taskid);
auto* dJ2 = new DiffJ2OrbitalType(targetPtcl);
std::string init_mode("0");
{
OhmmsAttributeSet hAttrib;
hAttrib.add(init_mode, "init");
hAttrib.put(cur);
}
cur = cur->xmlChildrenNode;
while (cur != NULL)
{
std::string cname((const char*)cur->name);
if (cname == "correlation")
{
OhmmsAttributeSet rAttrib;
RealType cusp = -1e10;
std::string spA(species.speciesName[0]);
std::string spB(species.speciesName[0]);
std::string pairType("0");
rAttrib.add(spA, "speciesA");
rAttrib.add(spB, "speciesB");
rAttrib.add(pairType, "pairType");
rAttrib.add(cusp, "cusp");
rAttrib.put(cur);
if (pairType[0] == '0')
{
pairType = spA + spB;
}
else
{
PRE.warning("pairType is deprecated. Use speciesA/speciesB");
//overwrite the species
spA = pairType[0];
spB = pairType[1];
}
int ia = species.findSpecies(spA);
int ib = species.findSpecies(spB);
int chargeInd = species.addAttribute("charge");
std::string illegal_species;
if (ia == species.size())
illegal_species = spA;
if (ib == species.size())
{
if (illegal_species.size())
illegal_species += " and ";
illegal_species += spB;
}
if (illegal_species.size())
PRE.error("species " + illegal_species + " requested for Jastrow " + j2name +
" does not exist in ParticleSet " + targetPtcl.getName(),
true);
if (ia == ib && (targetPtcl.last(ia) - targetPtcl.first(ia) == 1))
PRE.error("Failed to add " + spA + spB + " correlation for only 1 " + spA +
" particle. Please remove it from two-body Jastrow.",
true);
if (cusp < -1e6)
{
RealType qq = species(chargeInd, ia) * species(chargeInd, ib);
cusp = (ia == ib) ? -0.25 * qq : -0.5 * qq;
}
app_summary() << " Radial function for species: " << spA << " - " << spB << std::endl;
app_debug() << " RadialJastrowBuilder adds a functor with cusp = " << cusp << std::endl;
auto* functor = new RadFuncType();
functor->setCusp(cusp);
functor->setPeriodic(targetPtcl.Lattice.SuperCellEnum != SUPERCELL_OPEN);
functor->cutoff_radius = targetPtcl.Lattice.WignerSeitzRadius;
bool functor_initialized = functor->put(cur);
if (!functor_initialized && init_mode == "rpa")
{
initTwoBodyFunctor(*functor, -cusp / 0.5);
}
app_summary() << std::endl;
J2->addFunc(ia, ib, functor);
dJ2->addFunc(ia, ib, functor);
if (qmc_common.io_node)
{
char fname[32];
if (qmc_common.mpi_groups > 1)
sprintf(fname, "J2.%s.g%03d.dat", pairType.c_str(), taskid);
else
sprintf(fname, "J2.%s.dat", pairType.c_str());
std::ofstream os(fname);
print(*functor, os);
}
}
cur = cur->next;
}
J2->dPsi = dJ2;
targetPsi.addOrbital(J2, j2name.c_str());
J2->setOptimizable(true);
return true;
}
// specialiation for J2 RPA jastrow.
template<>
bool RadialJastrowBuilder::createJ2<RPAFunctor>(xmlNodePtr cur)
{
RPAJastrow* rpajastrow = new RPAJastrow(targetPtcl, targetPsi.is_manager());
rpajastrow->put(cur);
targetPsi.addOrbital(rpajastrow, NameOpt);
return true;
}
template<class RadFuncType>
bool RadialJastrowBuilder::createJ1(xmlNodePtr cur)
{
ReportEngine PRE(ClassName, "createJ1(xmlNodePtr)");
using RT = typename RadFuncType::real_type;
using J1OrbitalType = typename JastrowTypeHelper<RadFuncType>::J1OrbitalType;
using DiffJ1OrbitalType = typename JastrowTypeHelper<RadFuncType>::DiffJ1OrbitalType;
int taskid = targetPsi.getGroupID();
J1OrbitalType* J1 = new J1OrbitalType(*SourcePtcl, targetPtcl);
DiffJ1OrbitalType* dJ1 = new DiffJ1OrbitalType(*SourcePtcl, targetPtcl);
xmlNodePtr kids = cur->xmlChildrenNode;
// Find the number of the source species
SpeciesSet& sSet = SourcePtcl->getSpeciesSet();
SpeciesSet& tSet = targetPtcl.getSpeciesSet();
bool success = false;
bool Opt(true);
std::string jname = "J1_" + Jastfunction;
while (kids != NULL)
{
std::string kidsname = (char*)kids->name;
tolower(kidsname);
if (kidsname == "correlation")
{
std::string speciesA;
std::string speciesB;
RealType cusp(0);
OhmmsAttributeSet rAttrib;
rAttrib.add(speciesA, "elementType");
rAttrib.add(speciesA, "speciesA");
rAttrib.add(speciesB, "speciesB");
rAttrib.add(cusp, "cusp");
rAttrib.put(kids);
auto* functor = new RadFuncType();
functor->setPeriodic(SourcePtcl->Lattice.SuperCellEnum != SUPERCELL_OPEN);
functor->cutoff_radius = targetPtcl.Lattice.WignerSeitzRadius;
functor->setCusp(cusp);
const int ig = sSet.findSpecies(speciesA);
const int jg = speciesB.size() ? tSet.findSpecies(speciesB) : -1;
if (ig == sSet.getTotalNum())
{
PRE.error("species " + speciesA + " requested for Jastrow " + jname + " does not exist in ParticleSet " +
SourcePtcl->getName(),
true);
}
if (jg == tSet.getTotalNum())
{
PRE.error("species " + speciesB + " requested for Jastrow " + jname + " does not exist in ParticleSet " +
targetPtcl.getName(),
true);
}
app_summary() << " Radial function for element: " << speciesA << std::endl;
functor->put(kids);
app_summary() << std::endl;
J1->addFunc(ig, functor, jg);
dJ1->addFunc(ig, functor, jg);
success = true;
if (qmc_common.io_node)
{
char fname[128];
if (qmc_common.mpi_groups > 1)
{
if (speciesB.size())
sprintf(fname, "%s.%s%s.g%03d.dat", jname.c_str(), speciesA.c_str(), speciesB.c_str(), taskid);
else
sprintf(fname, "%s.%s.g%03d.dat", jname.c_str(), speciesA.c_str(), taskid);
}
else
{
if (speciesB.size())
sprintf(fname, "%s.%s%s.dat", jname.c_str(), speciesA.c_str(), speciesB.c_str());
else
sprintf(fname, "%s.%s.dat", jname.c_str(), speciesA.c_str());
}
std::ofstream os(fname);
print(*functor, os);
}
}
kids = kids->next;
}
if (success)
{
J1->dPsi = dJ1;
targetPsi.addOrbital(J1, jname.c_str());
J1->setOptimizable(Opt);
return true;
}
else
{
PRE.warning("BsplineJastrowBuilder failed to add an One-Body Jastrow.");
delete J1;
delete dJ1;
return false;
}
}
// specialiation for J1 RPA jastrow. Note that the long range part is not implemented
template<>
bool RadialJastrowBuilder::createJ1<RPAFunctor>(xmlNodePtr cur)
{
using RT = RealType;
using SplineEngineType = CubicBspline<RT, LINEAR_1DGRID, FIRSTDERIV_CONSTRAINTS>;
using RadFunctorType = CubicSplineSingle<RT, SplineEngineType>;
using GridType = LinearGrid<RT>;
using HandlerType = LRHandlerBase;
#if defined(ENABLE_SOA)
using J1OrbitalType = J1OrbitalSoA<RadFunctorType>;
#endif
#if !defined(ENABLE_SOA)
using J1OrbitalType = OneBodyJastrowOrbital<RadFunctorType>;
#endif
using DiffJ1OrbitalType = DiffOneBodyJastrowOrbital<RadFunctorType>;
/*
using J1OrbitalType = JastrowTypeHelper<RT, RadFunctorType>::J1OrbitalType;
using DiffJ1OrbitalType = JastrowTypeHelper<RT, RadFunctorType>::DiffJ1OrbitalType;
*/
std::string MyName = "Jep";
std::string rpafunc = "RPA";
OhmmsAttributeSet a;
a.add(MyName, "name");
a.add(rpafunc, "function");
a.put(cur);
ParameterSet params;
RealType Rs(-1.0);
RealType Kc(-1.0);
params.add(Rs, "rs", "double");
params.add(Kc, "kc", "double");
params.put(cur);
bool Opt(true);
HandlerType* myHandler;
if (Rs < 0)
{
Rs = std::pow(3.0 / 4.0 / M_PI * targetPtcl.Lattice.Volume / static_cast<RealType>(targetPtcl.getTotalNum()),
1.0 / 3.0);
}
if (Kc < 0)
{
Kc = 1e-6;
}
if (rpafunc == "RPA")
{
myHandler = new LRRPAHandlerTemp<EPRPABreakup<RealType>, LPQHIBasis>(targetPtcl, Kc);
app_log() << " using e-p RPA" << std::endl;
}
else if (rpafunc == "dRPA")
{
myHandler = new LRRPAHandlerTemp<derivEPRPABreakup<RealType>, LPQHIBasis>(targetPtcl, Kc);
app_log() << " using e-p derivRPA" << std::endl;
}
myHandler->Breakup(targetPtcl, Rs);
RT Rcut = myHandler->get_rc() - 0.1;
GridType* myGrid = new GridType;
int npts = static_cast<int>(Rcut / 0.01) + 1;
myGrid->set(0, Rcut, npts);
//create the numerical functor
RadFunctorType* nfunc = new RadFunctorType;
ShortRangePartAdapter<RT>* SRA = new ShortRangePartAdapter<RT>(myHandler);
SRA->setRmax(Rcut);
nfunc->initialize(SRA, myGrid);
J1OrbitalType* J1 = new J1OrbitalType(*SourcePtcl, targetPtcl);
DiffJ1OrbitalType* dJ1 = new DiffJ1OrbitalType(*SourcePtcl, targetPtcl);
SpeciesSet& sSet = SourcePtcl->getSpeciesSet();
for (int ig = 0; ig < sSet.getTotalNum(); ig++)
{
J1->addFunc(ig, nfunc);
dJ1->addFunc(ig, nfunc);
}
J1->dPsi = dJ1;
std::string jname = "J1_" + Jastfunction;
targetPsi.addOrbital(J1, jname.c_str());
J1->setOptimizable(Opt);
return true;
}
bool RadialJastrowBuilder::put(xmlNodePtr cur)
{
ReportEngine PRE(ClassName, "put(xmlNodePtr)");
OhmmsAttributeSet aAttrib;
aAttrib.add(NameOpt, "name");
aAttrib.add(TypeOpt, "type");
aAttrib.add(Jastfunction, "function");
aAttrib.add(SpinOpt, "spin");
aAttrib.put(cur);
tolower(NameOpt);
tolower(TypeOpt);
tolower(Jastfunction);
tolower(SpinOpt);
bool success = false;
SpeciesSet& species(targetPtcl.getSpeciesSet());
int chargeInd = species.addAttribute("charge");
if (TypeOpt.find("one") < TypeOpt.size())
{
// it's a one body jastrow factor
if (Jastfunction == "bspline")
{
success = createJ1<BsplineFunctor<RealType>>(cur);
}
else if (Jastfunction == "pade")
{
guardAgainstPBC();
success = createJ1<PadeFunctor<RealType>>(cur);
}
else if (Jastfunction == "shortrangecusp")
{
//guardAgainstPBC(); // is this needed?
success = createJ1<ShortRangeCuspFunctor<RealType>>(cur);
}
else if (Jastfunction == "user")
{
success = createJ1<UserFunctor<RealType>>(cur);
}
else if (Jastfunction == "rpa")
{
#if !(OHMMS_DIM == 3)
app_error() << "RPA for one-body jastrow is only available for 3D\n";
#endif
guardAgainstOBC();
#if defined(ENABLE_SOA)
app_error() << "one body RPA jastrow is not compatible with SOA at the moment\n";
success = false;
#else
success = createJ1<RPAFunctor>(cur);
#endif
}
else
{
app_error() << "Unknown one jastrow body function: " << Jastfunction << ".\n";
}
}
else if (TypeOpt.find("two") < TypeOpt.size())
{
// it's a two body jastrow factor
if (Jastfunction == "bspline")
{
success = createJ2<BsplineFunctor<RealType>>(cur);
}
else if (Jastfunction == "pade")
{
guardAgainstPBC();
success = createJ2<PadeFunctor<RealType>>(cur);
}
else if (Jastfunction == "user")
{
success = createJ2<UserFunctor<RealType>>(cur);
}
else if (Jastfunction == "rpa" || Jastfunction == "yukawa")
{
#if !(OHMMS_DIM == 3)
app_error() << "RPA for one-body jastrow is only available for 3D\n";
#endif
guardAgainstOBC();
success = createJ2<RPAFunctor>(cur);
}
else
{
app_error() << "Unknown two body jastrow function: " << Jastfunction << ".\n";
}
}
return success;
}
} // namespace qmcplusplus