QMCPACK
UserFunctor.h
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) 2019 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 A. Berrill, berrillma@ornl.gov, Oak Ridge National Laboratory
12 // Luke Shulenburger, lshulen@sandia.gov, Sandia National Laboratories
13 //
14 // File created by: Ken Esler, kpesler@gmail.com, University of Illinois at Urbana-Champaign
15 //////////////////////////////////////////////////////////////////////////////////////
16 
17 
18 /*
19  DO NOT MAKE PERMANENT EDITS IN THIS FILE
20  This file is generated from src/QMCWavefunctions/Jastrow/codegen/user_jastrow.py and UserFunctor.h.in
21 
22  To make changes, edit UserFunctor.h.in and rerun user_jastrow.py.
23 */
24 
25 
26 /** @file UserFunctor.h
27  * @brief User-defined functor
28  */
29 #ifndef QMCPLUSPLUS_USERFUNCTOR_H
30 #define QMCPLUSPLUS_USERFUNCTOR_H
31 #include "OptimizableFunctorBase.h"
32 #include "OhmmsData/AttributeSet.h"
33 #include <cmath>
34 // #include <vector>
35 #include "OhmmsPETE/TinyVector.h"
37 
38 
39 namespace qmcplusplus
40 {
41 /** Implements the function
42  * f = A*r/(B*r + 1) - A/B
43 
44  *
45  */
46 template<class T>
48 {
49  /// Is optimizable
50  bool Opt_A;
51  /// Value
53  /// Id in XML input
54  std::string ID_A;
55 
56 
57  /// Is optimizable
58  bool Opt_B;
59  /// Value
61  /// Id in XML input
62  std::string ID_B;
63 
64 
65  ///default constructor
66  UserFunctor(const std::string& my_name) : OptimizableFunctorBase(my_name) { reset(); }
67 
68  // void setCusp(real_type cusp)
69 
70  void setCusp(real_type cusp) override
71  {
72  A = cusp;
73  Opt_A = false;
74  reset();
75  }
76 
77 
78  OptimizableFunctorBase* makeClone() const override { return new UserFunctor(*this); }
79 
80  void reset() override {}
81 
82 
83  // inline real_type evaluate(real_type r) const
84 
85  inline real_type evaluate(real_type r) const { return A * r / (B * r + 1) - A / B; }
86 
87 
88  // const inline real_type evaluate(real_type r, real_type& dudr, real_type& d2udr2) const
89 
90  inline real_type evaluate(real_type r, real_type& dudr, real_type& d2udr2) const
91  {
92  dudr = -A * B * r / ((B * r + 1) * (B * r + 1)) + A / (B * r + 1);
93  d2udr2 = 2 * A * B * (B * r / (B * r + 1) - 1) / ((B * r + 1) * (B * r + 1));
94  return A * r / (B * r + 1) - A / B;
95  }
96 
97 
98  // inline real_type evaluate(real_type r, real_type& dudr, real_type& d2udr2, real_type& d3udr3) const
99 
100  inline real_type evaluate(real_type r, real_type& dudr, real_type& d2udr2, real_type& d3udr3) const
101  {
102  dudr = -A * B * r / ((B * r + 1) * (B * r + 1)) + A / (B * r + 1);
103  d2udr2 = 2 * A * B * (B * r / (B * r + 1) - 1) / ((B * r + 1) * (B * r + 1));
104  d3udr3 = 6 * A * ((B) * (B)) * (-B * r / (B * r + 1) + 1) / std::pow(B * r + 1, 3);
105  return A * r / (B * r + 1) - A / B;
106  }
107 
108 
109  inline real_type evaluateV(const int iat,
110  const int iStart,
111  const int iEnd,
112  const T* restrict _distArray,
113  T* restrict distArrayCompressed) const
114  {
115  // specialized evaluation loop?
116  real_type sum(0);
117  for (int idx = iStart; idx < iEnd; idx++)
118  if (idx != iat)
119  sum += evaluate(_distArray[idx]);
120  return sum;
121  }
122 
123  /** evaluate sum of the pair potentials FIXME
124  * @return \f$\sum u(r_j)\f$ for r_j < cutoff_radius
125  */
126  static void mw_evaluateV(const int num_groups,
127  const UserFunctor* const functors[],
128  const int n_src,
129  const int* grp_ids,
130  const int num_pairs,
131  const int* ref_at,
132  const T* mw_dist,
133  const int dist_stride,
134  T* mw_vals,
135  Vector<char, OffloadPinnedAllocator<char>>& transfer_buffer)
136  {
137  for (int ip = 0; ip < num_pairs; ip++)
138  {
139  mw_vals[ip] = 0;
140  for (int j = 0; j < n_src; j++)
141  {
142  const int ig = grp_ids[j];
143  auto& functor(*functors[ig]);
144  if (j != ref_at[ip])
145  mw_vals[ip] += functor.evaluate(mw_dist[ip * dist_stride + j]);
146  }
147  }
148  }
149 
150  inline void evaluateVGL(const int iat,
151  const int iStart,
152  const int iEnd,
153  const T* distArray,
154  T* restrict valArray,
155  T* restrict gradArray,
156  T* restrict laplArray,
157  T* restrict distArrayCompressed,
158  int* restrict distIndices) const
159  {
160  // specialized evaluation loop?
161  for (int idx = iStart; idx < iEnd; idx++)
162  {
163  valArray[idx] = evaluate(distArray[idx], gradArray[idx], laplArray[idx]);
164  gradArray[idx] /= distArray[idx];
165  }
166  if (iat >= iStart && iat < iEnd)
167  valArray[iat] = gradArray[iat] = laplArray[iat] = T(0);
168  }
169 
170  static void mw_evaluateVGL(const int iat,
171  const int num_groups,
172  const UserFunctor* const functors[],
173  const int n_src,
174  const int* grp_ids,
175  const int nw,
176  T* mw_vgl, // [nw][DIM+2]
177  const int n_padded,
178  const T* mw_dist, // [nw][DIM+1][n_padded]
179  T* mw_cur_allu, // [nw][3][n_padded]
180  Vector<char, OffloadPinnedAllocator<char>>& transfer_buffer)
181  {
182  throw std::runtime_error("UserFunctor mw_evaluateVGL not implemented!");
183  }
184 
185  inline real_type f(real_type r) override { return evaluate(r); }
186 
187  inline real_type df(real_type r) override
188  {
189  real_type dudr, d2udr2;
190  real_type res = evaluate(r, dudr, d2udr2);
191  return dudr;
192  }
193 
194  static void mw_updateVGL(const int iat,
195  const std::vector<bool>& isAccepted,
196  const int num_groups,
197  const UserFunctor* const functors[],
198  const int n_src,
199  const int* grp_ids,
200  const int nw,
201  T* mw_vgl, // [nw][DIM+2]
202  const int n_padded,
203  const T* mw_dist, // [nw][DIM+1][n_padded]
204  T* mw_allUat, // [nw][DIM+2][n_padded]
205  T* mw_cur_allu, // [nw][3][n_padded]
206  Vector<char, OffloadPinnedAllocator<char>>& transfer_buffer)
207  {
208  throw std::runtime_error("UserFunctor mw_updateVGL not implemented!");
209  }
210 
211  // inline bool evaluateDerivatives(real_type r, std::vector<TinyVector<real_type, 3>>& derivs)
212 
213  inline bool evaluateDerivatives(real_type r, std::vector<TinyVector<real_type, 3>>& derivs) override
214  {
215  int i = 0;
216 
217  if (Opt_A)
218  {
219  derivs[i][0] = r / (B * r + 1) - 1 / B;
220  derivs[i][1] = -B * r / ((B * r + 1) * (B * r + 1)) + 1.0 / (B * r + 1);
221  derivs[i][2] = 2 * B * (B * r / (B * r + 1) - 1) / ((B * r + 1) * (B * r + 1));
222  ++i;
223  }
224 
225  if (Opt_B)
226  {
227  derivs[i][0] = -A * ((r) * (r)) / ((B * r + 1) * (B * r + 1)) + A / ((B) * (B));
228  derivs[i][1] = 2 * A * B * ((r) * (r)) / std::pow(B * r + 1, 3) - 2 * A * r / ((B * r + 1) * (B * r + 1));
229  derivs[i][2] = -4 * A * B * r * (B * r / (B * r + 1) - 1) / std::pow(B * r + 1, 3) +
230  2 * A * B * (-B * ((r) * (r)) / ((B * r + 1) * (B * r + 1)) + r / (B * r + 1)) / ((B * r + 1) * (B * r + 1)) +
231  2 * A * (B * r / (B * r + 1) - 1) / ((B * r + 1) * (B * r + 1));
232  ++i;
233  }
234 
235  return true;
236  }
237 
238 
239  /// compute derivatives with respect to variational parameters
240  inline bool evaluateDerivatives(real_type r, std::vector<real_type>& derivs) override
241  {
242  int i = 0;
243 
244  if (Opt_A)
245  {
246  derivs[i] = r / (B * r + 1) - 1 / B;
247  ++i;
248  }
249 
250  if (Opt_B)
251  {
252  derivs[i] = -A * ((r) * (r)) / ((B * r + 1) * (B * r + 1)) + A / ((B) * (B));
253  ++i;
254  }
255 
256 
257  return true;
258  }
259 
260 
261  // bool put(xmlNodePtr cur)
262 
263  bool put(xmlNodePtr cur) override
264  {
265  cur = cur->xmlChildrenNode;
266  while (cur != NULL)
267  {
268  std::string cname((const char*)(cur->name));
269  if (cname == "var") //only accept var
270  {
271  std::string id_in;
272  std::string p_name;
273  OhmmsAttributeSet rAttrib;
274  rAttrib.add(id_in, "id");
275  rAttrib.add(p_name, "name");
276  rAttrib.put(cur);
277 
278  if (p_name == "A")
279  {
280  ID_A = id_in;
281  putContent(A, cur);
282  Opt_A = true;
283  }
284 
285  if (p_name == "B")
286  {
287  ID_B = id_in;
288  putContent(B, cur);
289  Opt_B = true;
290  }
291  }
292  cur = cur->next;
293  }
294  reset();
295  myVars.clear();
296 
297  if (Opt_A)
299 
300  if (Opt_B)
302 
303  return true;
304  }
305 
306 
308  {
309  active.insertFrom(myVars);
310  }
311 
312  void checkOutVariables(const opt_variables_type& active) override
313  {
314  myVars.getIndex(active);
315  }
316 
317  void resetParametersExclusive(const opt_variables_type& active) override
318  {
319  if (myVars.size())
320  {
321  int ia = myVars.where(0);
322  if (ia > -1)
323  {
324  int i = 0;
325 
326  if (Opt_A)
327  A = std::real(myVars[i++] = active[ia++]);
328 
329  if (Opt_B)
330  B = std::real(myVars[i++] = active[ia++]);
331  }
332  reset();
333  }
334  }
335 };
336 
337 
338 } // namespace qmcplusplus
339 #endif
void checkOutVariables(const opt_variables_type &active) override
check out variational optimizable variables
Definition: UserFunctor.h:312
Fixed-size array.
Definition: OhmmsTinyMeta.h:30
QMCTraits::RealType real
helper functions for EinsplineSetBuilder
Definition: Configuration.h:43
bool put(xmlNodePtr cur) override
process xmlnode and registers variables to optimize
Definition: UserFunctor.h:263
static void mw_evaluateV(const int num_groups, const UserFunctor *const functors[], const int n_src, const int *grp_ids, const int num_pairs, const int *ref_at, const T *mw_dist, const int dist_stride, T *mw_vals, Vector< char, OffloadPinnedAllocator< char >> &transfer_buffer)
evaluate sum of the pair potentials FIXME
Definition: UserFunctor.h:126
static void mw_updateVGL(const int iat, const std::vector< bool > &isAccepted, const int num_groups, const UserFunctor *const functors[], const int n_src, const int *grp_ids, const int nw, T *mw_vgl, const int n_padded, const T *mw_dist, T *mw_allUat, T *mw_cur_allu, Vector< char, OffloadPinnedAllocator< char >> &transfer_buffer)
Definition: UserFunctor.h:194
void checkInVariablesExclusive(opt_variables_type &active) override
check in variational parameters to the global list of parameters used by the optimizer.
Definition: UserFunctor.h:307
bool put(xmlNodePtr cur)
assign attributes to the set
Definition: AttributeSet.h:55
void setCusp(real_type cusp) override
empty virtual function to help builder classes
Definition: UserFunctor.h:70
real_type evaluate(real_type r, real_type &dudr, real_type &d2udr2, real_type &d3udr3) const
Definition: UserFunctor.h:100
std::string ID_A
Id in XML input.
Definition: UserFunctor.h:54
Define a base class for one-dimensional functions with optimizable variables.
bool Opt_A
Is optimizable.
Definition: UserFunctor.h:50
real_type f(real_type r) override
evaluate the value at r
Definition: UserFunctor.h:185
bool Opt_B
Is optimizable.
Definition: UserFunctor.h:58
real_type evaluate(real_type r, real_type &dudr, real_type &d2udr2) const
Definition: UserFunctor.h:90
static void mw_evaluateVGL(const int iat, const int num_groups, const UserFunctor *const functors[], const int n_src, const int *grp_ids, const int nw, T *mw_vgl, const int n_padded, const T *mw_dist, T *mw_cur_allu, Vector< char, OffloadPinnedAllocator< char >> &transfer_buffer)
Definition: UserFunctor.h:170
MakeReturn< BinaryNode< FnPow, typename CreateLeaf< Vector< T1, C1 > >::Leaf_t, typename CreateLeaf< Vector< T2, C2 > >::Leaf_t > >::Expression_t pow(const Vector< T1, C1 > &l, const Vector< T2, C2 > &r)
Implements the function f = A*r/(B*r + 1) - A/B.
Definition: UserFunctor.h:47
void evaluateVGL(const int iat, const int iStart, const int iEnd, const T *distArray, T *restrict valArray, T *restrict gradArray, T *restrict laplArray, T *restrict distArrayCompressed, int *restrict distIndices) const
Definition: UserFunctor.h:150
void insert(const std::string &vname, real_type v, bool enable=true, int type=OTHER_P)
Definition: VariableSet.h:133
OptimizableFunctorBase * makeClone() const override
create a clone of this object
Definition: UserFunctor.h:78
class to handle a set of attributes of an xmlNode
Definition: AttributeSet.h:24
void resetParametersExclusive(const opt_variables_type &active) override
reset the parameters during optimizations.
Definition: UserFunctor.h:317
class to handle a set of variables that can be modified during optimizations
Definition: VariableSet.h:49
int where(int i) const
return the locator of the i-th Index
Definition: VariableSet.h:90
real_type evaluateV(const int iat, const int iStart, const int iEnd, const T *restrict _distArray, T *restrict distArrayCompressed) const
Definition: UserFunctor.h:109
OMPallocator is an allocator with fused device and dualspace allocator functionality.
void clear()
clear the variable set
Definition: VariableSet.cpp:28
size_type size() const
return the size
Definition: VariableSet.h:88
Base class for any functor with optimizable parameters.
bool evaluateDerivatives(real_type r, std::vector< real_type > &derivs) override
compute derivatives with respect to variational parameters
Definition: UserFunctor.h:240
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
std::string ID_B
Id in XML input.
Definition: UserFunctor.h:62
real_type A
Value.
Definition: UserFunctor.h:52
UserFunctor(const std::string &my_name)
default constructor
Definition: UserFunctor.h:66
void reset() override
reset function
Definition: UserFunctor.h:80
bool evaluateDerivatives(real_type r, std::vector< TinyVector< real_type, 3 >> &derivs) override
Definition: UserFunctor.h:213
void insertFrom(const VariableSet &input)
insert a VariableSet to the list
Definition: VariableSet.cpp:37
real_type df(real_type r) override
evaluate the first derivative
Definition: UserFunctor.h:187
void add(PDT &aparam, const std::string &aname, std::vector< PDT > candidate_values={}, TagStatus status=TagStatus::OPTIONAL)
add a new attribute
Definition: AttributeSet.h:42
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
real_type evaluate(real_type r) const
Definition: UserFunctor.h:85
real_type B
Value.
Definition: UserFunctor.h:60
opt_variables_type myVars
set of variables to be optimized