22 #ifndef QMCPLUSPLUS_BSPLINE_FUNCTOR_H 23 #define QMCPLUSPLUS_BSPLINE_FUNCTOR_H 43 template<
typename REAL>
44 struct BsplineFunctor :
public OptimizableFunctorBase
48 static constexpr
Real A0 = -1.0 / 6.0,
A1 = 3.0 / 6.0,
A2 = -3.0 / 6.0,
A3 = 1.0 / 6.0;
49 static constexpr
Real A4 = 3.0 / 6.0,
A5 = -6.0 / 6.0,
A6 = 0.0 / 6.0,
A7 = 4.0 / 6.0;
50 static constexpr
Real A8 = -3.0 / 6.0,
A9 = 3.0 / 6.0,
A10 = 3.0 / 6.0,
A11 = 1.0 / 6.0;
51 static constexpr
Real A12 = 1.0 / 6.0,
A13 = 0.0 / 6.0,
A14 = 0.0 / 6.0,
A15 = 0.0 / 6.0;
105 int numKnots = numCoefs - 2;
109 spline_coefs_ = std::make_shared<Vector<Real, OffloadAllocator<Real>>>(numCoefs);
118 const int numKnots = numCoefs - 2;
122 for (
int i = 0; i < coefs.size(); i++)
147 const REAL* _distArray,
148 REAL* restrict _valArray,
149 REAL* restrict _gradArray,
150 REAL* restrict _laplArray,
151 REAL* restrict distArrayCompressed,
152 int* restrict distIndices)
const;
173 const int num_groups,
195 const REAL* restrict _distArray,
196 REAL* restrict distArrayCompressed)
const;
220 const int dist_stride,
231 Real sCoef0 = coefs[i + 0];
232 Real sCoef1 = coefs[i + 1];
233 Real sCoef2 = coefs[i + 2];
234 Real sCoef3 = coefs[i + 3];
236 return (sCoef0 * (((
A0 * t +
A1) * t +
A2) * t +
A3) + sCoef1 * (((
A4 * t +
A5) * t +
A6) * t +
A7) +
259 Real sCoef0 = coefs[i + 0];
260 Real sCoef1 = coefs[i + 1];
261 Real sCoef2 = coefs[i + 2];
262 Real sCoef3 = coefs[i + 3];
272 Real u = (sCoef0 * (((
A0 * t +
A1) * t +
A2) * t +
A3) + sCoef1 * (((
A4 * t +
A5) * t +
A6) * t +
A7) +
293 dudr = d2udr2 = d3udr3 = 0.0;
314 (coefs[i + 0] * (
d3A0 * tp[0] +
d3A1 * tp[1] +
d3A2 * tp[2] +
d3A3 * tp[3]) +
315 coefs[i + 1] * (
d3A4 * tp[0] +
d3A5 * tp[1] +
d3A6 * tp[2] +
d3A7 * tp[3]) +
319 (coefs[i + 0] * (
d2A0 * tp[0] +
d2A1 * tp[1] +
d2A2 * tp[2] +
d2A3 * tp[3]) +
320 coefs[i + 1] * (
d2A4 * tp[0] +
d2A5 * tp[1] +
d2A6 * tp[2] +
d2A7 * tp[3]) +
324 (coefs[i + 0] * (
dA0 * tp[0] +
dA1 * tp[1] +
dA2 * tp[2] +
dA3 * tp[3]) +
325 coefs[i + 1] * (
dA4 * tp[0] +
dA5 * tp[1] +
dA6 * tp[2] +
dA7 * tp[3]) +
326 coefs[i + 2] * (
dA8 * tp[0] +
dA9 * tp[1] +
dA10 * tp[2] +
dA11 * tp[3]) +
327 coefs[i + 3] * (
dA12 * tp[0] +
dA13 * tp[1] +
dA14 * tp[2] +
dA15 * tp[3]));
337 return (coefs[i + 0] * (
A0 * tp[0] +
A1 * tp[1] +
A2 * tp[2] +
A3 * tp[3]) +
338 coefs[i + 1] * (
A4 * tp[0] +
A5 * tp[1] +
A6 * tp[2] +
A7 * tp[3]) +
339 coefs[i + 2] * (
A8 * tp[0] +
A9 * tp[1] +
A10 * tp[2] +
A11 * tp[3]) +
340 coefs[i + 3] * (
A12 * tp[0] +
A13 * tp[1] +
A14 * tp[2] +
A15 * tp[3]));
363 const std::vector<bool>& isAccepted,
364 const int num_groups,
407 int imin = std::max(i, 1);
409 for (
int n = imin;
n < imax; ++
n)
455 v[0] =
A0 * tp[0] +
A1 * tp[1] +
A2 * tp[2] +
A3 * tp[3];
456 v[1] =
A4 * tp[0] +
A5 * tp[1] +
A6 * tp[2] +
A7 * tp[3];
457 v[2] =
A8 * tp[0] +
A9 * tp[1] +
A10 * tp[2] +
A11 * tp[3];
458 v[3] =
A12 * tp[0] +
A13 * tp[1] +
A14 * tp[2] +
A15 * tp[3];
459 int imin = std::max(i, 1);
461 int n = imin - 1, j = imin - i;
462 while (
n < imax && j < 4)
489 bool put(xmlNodePtr cur)
override 498 rAttrib.
add(radius,
"rcut");
499 rAttrib.
add(radius,
"cutoff");
504 app_log() <<
" Jastrow cutoff unspecified. Setting to Wigner-Seitz radius = " <<
cutoff_radius << std::endl;
509 APP_ABORT(
" Jastrow cutoff unspecified. Cutoff must be given when using open boundary conditions");
515 APP_ABORT(
" The Jastrow cutoff specified should not be larger than Wigner-Seitz radius.");
519 app_log() <<
" The Jastrow cutoff specified is slightly larger than the Wigner-Seitz radius.";
527 PRE.
error(
"You must specify a positive number of parameters for the Bspline jastrow function.",
true);
534 xmlNodePtr xmlCoefs = cur->xmlChildrenNode;
535 while (xmlCoefs != NULL)
537 std::string cname((
const char*)xmlCoefs->name);
538 if (cname ==
"coefficients")
540 std::string type(
"0"), id(
"0");
543 cAttrib.
add(
id,
"id");
544 cAttrib.
add(type,
"type");
546 cAttrib.
put(xmlCoefs);
549 PRE.
error(
"Unknown correlation type " + type +
" in BsplineFunctor." +
"Resetting to \"Array\"");
550 xmlNewProp(xmlCoefs, (
const xmlChar*)
"type", (
const xmlChar*)
"Array");
552 std::vector<Real> params;
558 app_log() <<
" Changing number of Bspline parameters from " << params.size() <<
" to " <<
NumParams 559 <<
". Performing fit:\n";
561 const int numPoints = 500;
564 tmp_func.resize(params.size());
565 tmp_func.Parameters = params;
567 std::vector<Real> y(numPoints);
569 std::vector<TinyVector<Real, 3>> derivs(
NumParams);
570 for (
int i = 0; i < numPoints; i++)
573 y[i] = tmp_func.evaluate(r);
576 basis(i, j) = derivs[j][0];
580 app_log() <<
"New parameters are:\n";
594 std::stringstream sstr;
595 sstr <<
id <<
"_" << i;
598 int left_pad_space = 5;
602 xmlCoefs = xmlCoefs->next;
608 return zeros > 1.0e-12;
612 std::vector<Real>& x,
613 std::vector<Real>& y,
625 PRE.
error(
"You must specify a positive number of parameters for the Bspline jastrow function.",
true);
627 app_log() <<
"Initializing BsplineFunctor from array. \n";
634 std::vector<TinyVector<Real, 3>> derivs(
NumParams);
635 for (
int i = 0; i < npts; i++)
640 PRE.
error(
"Error in BsplineFunctor::initialize: r > cutoff_radius.",
true);
644 basis(i, j) = derivs[j][0];
648 app_log() <<
"New parameters are:\n";
651 #if !defined(QMC_BUILD_SANDBOX_ONLY) 657 std::stringstream sstr;
658 sstr <<
id <<
"_" << i;
667 app_log() <<
"Parameters of BsplineFunctor id:" <<
id <<
" are not being optimized.\n";
722 template<
typename REAL>
726 const REAL* restrict _distArray,
727 REAL* restrict distArrayCompressed)
const 729 const Real* restrict distArray = _distArray + iStart;
733 const int iLimit = iEnd - iStart;
735 #pragma vector always 736 for (
int jat = 0; jat < iLimit; jat++)
738 Real r = distArray[jat];
740 if (r < cutoff_radius && iStart + jat != iat)
741 distArrayCompressed[iCount++] = distArray[jat];
745 auto& coefs = *spline_coefs_;
746 const int max_index = getMaxIndex();
747 #pragma omp simd reduction(+ : d) 748 for (
int jat = 0; jat < iCount; jat++)
750 Real r = distArrayCompressed[jat];
755 Real d1 = coefs[i + 0] * (((A0 * t + A1) * t + A2) * t + A3);
756 Real d2 = coefs[i + 1] * (((A4 * t + A5) * t + A6) * t + A7);
757 Real d3 = coefs[i + 2] * (((A8 * t + A9) * t + A10) * t + A11);
758 Real d4 = coefs[i + 3] * (((A12 * t + A13) * t + A14) * t + A15);
759 d += (d1 + d2 + d3 + d4);
764 template<
typename REAL>
768 const REAL* _distArray,
769 REAL* restrict _valArray,
770 REAL* restrict _gradArray,
771 REAL* restrict _laplArray,
772 REAL* restrict distArrayCompressed,
773 int* restrict distIndices)
const 775 Real dSquareDeltaRinv = DeltaRInv * DeltaRInv;
776 constexpr
Real cOne(1);
783 int iLimit = iEnd - iStart;
784 const REAL* distArray = _distArray + iStart;
785 REAL* valArray = _valArray + iStart;
786 REAL* gradArray = _gradArray + iStart;
787 REAL* laplArray = _laplArray + iStart;
789 #pragma vector always 790 for (
int jat = 0; jat < iLimit; jat++)
792 Real r = distArray[jat];
793 if (r < cutoff_radius && iStart + jat != iat)
795 distIndices[iCount] = jat;
796 distArrayCompressed[iCount] = r;
801 auto& coefs = *spline_coefs_;
802 const int max_index = getMaxIndex();
804 for (
int j = 0; j < iCount; j++)
806 Real r = distArrayCompressed[j];
807 int iScatter = distIndices[j];
808 Real rinv = cOne / r;
814 Real sCoef0 = coefs[iGather + 0];
815 Real sCoef1 = coefs[iGather + 1];
816 Real sCoef2 = coefs[iGather + 2];
817 Real sCoef3 = coefs[iGather + 3];
819 laplArray[iScatter] = dSquareDeltaRinv *
820 (sCoef0 * (d2A2 * t + d2A3) + sCoef1 * (d2A6 * t + d2A7) + sCoef2 * (d2A10 * t + d2A11) +
821 sCoef3 * (d2A14 * t + d2A15));
823 gradArray[iScatter] = DeltaRInv * rinv *
824 (sCoef0 * ((dA1 * t + dA2) * t + dA3) + sCoef1 * ((dA5 * t + dA6) * t + dA7) +
825 sCoef2 * ((dA9 * t + dA10) * t + dA11) + sCoef3 * ((dA13 * t + dA14) * t + dA15));
828 (sCoef0 * (((A0 * t + A1) * t + A2) * t + A3) + sCoef1 * (((A4 * t + A5) * t + A6) * t + A7) +
829 sCoef2 * (((A8 * t + A9) * t + A10) * t + A11) + sCoef3 * (((A12 * t + A13) * t + A14) * t + A15));
REAL evaluateV(const int iat, const int iStart, const int iEnd, const REAL *restrict _distArray, REAL *restrict distArrayCompressed) const
evaluate sum of the pair potentials for [iStart,iEnd)
Real evaluate(Real r, Real rinv)
helper functions for EinsplineSetBuilder
BsplineFunctor class for the Jastrows REAL is the real type used by offload target, it is the correct type for the mw data pointers and is also used to coerce/implicitly convert the Real type inherited OptimizableFunctorBase into that buffer if offload is off this happens too but is just an implementation quirk.
static constexpr Real d2A8
void setIndexDefault()
set default Indices, namely all the variables are active
static constexpr Real dA6
static constexpr Real d2A7
Real evaluate(Real r, Real &dudr, Real &d2udr2)
std::ostream & app_summary()
bool put(xmlNodePtr cur)
assign attributes to the set
static constexpr Real d2A9
void evaluateAll(Real r, Real rinv)
static constexpr Real dA3
static constexpr Real d2A13
OptimizableFunctorBase * makeClone() const override
create a clone of this object
static constexpr Real dA12
static constexpr Real d2A10
static constexpr Real d2A14
Real df(Real r) override
evaluate the first derivative
static constexpr Real A10
static constexpr Real dA9
static constexpr Real d2A5
std::vector< Real > Parameters
void reportStatus(std::ostream &os) override
print the state, e.g., optimizables
static constexpr Real d3A9
Define a base class for one-dimensional functions with optimizable variables.
static constexpr Real A12
static constexpr Real d3A10
int getMaxIndex() const
return the max allowed beginning index to access spline coefficients
static constexpr Real d3A6
static constexpr Real d3A5
static constexpr Real d3A14
static void mw_evaluateVGL(const int iat, const int num_groups, const BsplineFunctor *const functors[], const int n_src, const int *grp_ids, const int nw, REAL *mw_vgl, const int n_padded, const REAL *mw_dist, REAL *mw_cur_allu, Vector< char, OffloadPinnedAllocator< char >> &transfer_buffer)
compute value, gradient and laplacian for target particles This more than just a batched call of eval...
static constexpr Real d3A7
static constexpr Real d2A2
static void mw_updateVGL(const int iat, const std::vector< bool > &isAccepted, const int num_groups, const BsplineFunctor *const functors[], const int n_src, const int *grp_ids, const int nw, REAL *mw_vgl, const int n_padded, const REAL *mw_dist, REAL *mw_allUat, REAL *mw_cur_allu, Vector< char, OffloadPinnedAllocator< char >> &transfer_buffer)
update value, gradient and laplacian for target particles It serves multile walkers and handles updat...
BsplineFunctor(const std::string &my_name, Real cusp=0.0)
constructor
void setCusp(Real c) override
empty virtual function to help builder classes
static constexpr Real dA7
static void mw_evaluateV(const int num_groups, const BsplineFunctor *const functors[], const int n_src, const int *grp_ids, const int num_pairs, const int *ref_at, const REAL *mw_dist, const int dist_stride, REAL *mw_vals, Vector< char, OffloadPinnedAllocator< char >> &transfer_buffer)
compute value for target-source particle pair potentials This more than just a batched call of evalua...
static constexpr Real dA10
std::vector< std::string > ParameterNames
void insert(const std::string &vname, real_type v, bool enable=true, int type=OTHER_P)
Final class and should not be derived.
class to handle a set of attributes of an xmlNode
static constexpr Real d3A0
declaration of ProgressReportEngine
OptimizableFunctorBase::real_type Real
static constexpr Real d3A8
static constexpr Real d3A4
void checkInVariablesExclusive(opt_variables_type &active) override
check in variational parameters to the global list of parameters used by the optimizer.
Real evaluate(Real r, Real &dudr, Real &d2udr2, Real &d3udr3)
static constexpr Real d2A3
class to handle a set of variables that can be modified during optimizations
int where(int i) const
return the locator of the i-th Index
static constexpr Real d2A15
#define APP_ABORT(msg)
Widely used but deprecated fatal error macros from legacy code.
static constexpr Real d3A15
std::vector< TinyVector< Real, 3 > > SplineDerivs
static constexpr Real dA8
OMPallocator is an allocator with fused device and dualspace allocator functionality.
static constexpr Real d2A1
real_type cutoff_radius
maximum cutoff
void resetParametersExclusive(const opt_variables_type &active) override
reset the parameters during optimizations.
Base class for any functor with optimizable parameters.
bool evaluateDerivatives(Real r, std::vector< Real > &derivs) override
static constexpr Real d3A3
bool putContent(T &a, xmlNodePtr cur)
replaces a's value with the first "element" in the "string" returned by XMLNodeString{cur}.
static constexpr Real dA11
static constexpr Real dA0
void initialize(int numPoints, std::vector< Real > &x, std::vector< Real > &y, REAL cusp, REAL rcut, std::string &id, std::string &optimize)
Declaraton of Vector<T,Alloc> Manage memory through Alloc directly and allow referencing an existing ...
static constexpr Real dA1
bool put(xmlNodePtr cur) override
process xmlnode and registers variables to optimize
static Real evaluate_impl(Real r, const Real *coefs, const Real DeltaRInv, const int max_index, Real &dudr, Real &d2udr2)
void error(const std::string &msg, bool fatal=false)
static constexpr Real d3A1
void checkOutVariables(const opt_variables_type &active) override
check out variational optimizable variables
static constexpr Real dA5
static constexpr Real dA4
static Real evaluate_impl(Real r, const Real *coefs, const Real DeltaRInv, const int max_index)
void evaluateVGL(const int iat, const int iStart, const int iEnd, const REAL *_distArray, REAL *restrict _valArray, REAL *restrict _gradArray, REAL *restrict _laplArray, REAL *restrict distArrayCompressed, int *restrict distIndices) const
compute value, first and second derivatives for [iStart, iEnd) pairs
static constexpr Real d3A13
static constexpr Real A11
static constexpr Real dA2
static constexpr Real d2A0
static constexpr Real d2A6
static constexpr Real d2A11
#define ASSUME_ALIGNED(x)
void insertFrom(const VariableSet &input)
insert a VariableSet to the list
void getSplineBound(const T x, const int nmax, int &ind, TRESIDUAL &dx)
break x into the integer part and residual part and apply bounds
Real f(Real r) override
evaluate the value at r
static constexpr Real d3A11
static constexpr Real dA14
void setPeriodic(bool p) override
empty virtual function to help builder classes
Real evaluate(Real r) const
static constexpr Real A13
bool evaluateDerivatives(Real r, std::vector< TinyVector< Real, 3 >> &derivs) override
void add(PDT &aparam, const std::string &aname, std::vector< PDT > candidate_values={}, TagStatus status=TagStatus::OPTIONAL)
add a new attribute
void print(std::ostream &os, int leftPadSpaces=0, bool printHeader=false) const
optimize::VariableSet::real_type real_type
typedef for real values
int getIndex(const std::string &vname) const
return the Index vaule for the named parameter
static constexpr Real d2A12
static constexpr Real d3A2
void LinearFit(std::vector< T > &y, Matrix< T > &A, std::vector< T > &coefs)
std::shared_ptr< Vector< Real, OffloadAllocator< Real > > > spline_coefs_
void reset() override
reset coefs from Parameters
static constexpr Real d2A4
static constexpr Real dA13
static constexpr Real A15
opt_variables_type myVars
set of variables to be optimized
static constexpr Real d3A12
static constexpr Real dA15
static constexpr Real A14