MADNESS 0.10.1
funcimpl.h
Go to the documentation of this file.
1/*
2 This file is part of MADNESS.
3
4 Copyright (C) 2007,2010 Oak Ridge National Laboratory
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
20 For more information please contact:
21
22 Robert J. Harrison
23 Oak Ridge National Laboratory
24 One Bethel Valley Road
25 P.O. Box 2008, MS-6367
26
27 email: harrisonrj@ornl.gov
28 tel: 865-241-3937
29 fax: 865-572-0680
30*/
31
32#ifndef MADNESS_MRA_FUNCIMPL_H__INCLUDED
33#define MADNESS_MRA_FUNCIMPL_H__INCLUDED
34
35/// \file funcimpl.h
36/// \brief Provides FunctionCommonData, FunctionImpl and FunctionFactory
37
39#include <madness/world/print.h>
40#include <madness/misc/misc.h>
43
45#include <madness/mra/indexit.h>
46#include <madness/mra/key.h>
50
51#include <madness/mra/leafop.h>
52
53#include <array>
54#include <iostream>
55#include <type_traits>
56
57namespace madness {
58 template <typename T, std::size_t NDIM>
59 class DerivativeBase;
60
61 template<typename T, std::size_t NDIM>
62 class FunctionImpl;
63
64 template<typename T, std::size_t NDIM>
65 class FunctionNode;
66
67 template<typename T, std::size_t NDIM>
68 class Function;
69
70 template<typename T, std::size_t NDIM>
71 class FunctionFactory;
72
73 template<typename T, std::size_t NDIM, std::size_t MDIM>
74 class CompositeFunctorInterface;
75
76 template<int D>
78
79}
80
81namespace madness {
82
83
84 /// A simple process map
85 template<typename keyT>
86 class SimplePmap : public WorldDCPmapInterface<keyT> {
87 private:
88 const int nproc;
90
91 public:
92 SimplePmap(World& world) : nproc(world.nproc()), me(world.rank())
93 { }
94
95 ProcessID owner(const keyT& key) const {
96 if (key.level() == 0)
97 return 0;
98 else
99 return key.hash() % nproc;
100 }
101 };
102
103 /// A pmap that locates children on odd levels with their even level parents
104 template <typename keyT>
105 class LevelPmap : public WorldDCPmapInterface<keyT> {
106 private:
107 const int nproc;
108 public:
109 LevelPmap() : nproc(0) {};
110
111 LevelPmap(World& world) : nproc(world.nproc()) {}
112
113 /// Find the owner of a given key
114 ProcessID owner(const keyT& key) const {
115 Level n = key.level();
116 if (n == 0) return 0;
117 hashT hash;
118 if (n <= 3 || (n&0x1)) hash = key.hash();
119 else hash = key.parent().hash();
120 return hash%nproc;
121 }
122 };
123
124
125 /// FunctionNode holds the coefficients, etc., at each node of the 2^NDIM-tree
126 template<typename T, std::size_t NDIM>
128 public:
131 private:
132 // Should compile OK with these volatile but there should
133 // be no need to set as volatile since the container internally
134 // stores the entire entry as volatile
135
136 coeffT _coeffs; ///< The coefficients, if any
137 double _norm_tree; ///< After norm_tree will contain norm of coefficients summed up tree
138 bool _has_children; ///< True if there are children
139 coeffT buffer; ///< The coefficients, if any
140 double dnorm=-1.0; ///< norm of the d coefficients, also defined if there are no d coefficients
141 double snorm=-1.0; ///< norm of the s coefficients
142
143 public:
144 typedef WorldContainer<Key<NDIM> , FunctionNode<T, NDIM> > dcT; ///< Type of container holding the nodes
145 /// Default constructor makes node without coeff or children
147 _coeffs(), _norm_tree(1e300), _has_children(false) {
148 }
149
150 /// Constructor from given coefficients with optional children
151
152 /// Note that only a shallow copy of the coeff are taken so
153 /// you should pass in a deep copy if you want the node to
154 /// take ownership.
155 explicit
159
160 explicit
164
165 explicit
169
172 dnorm(other.dnorm), snorm(other.snorm) {
173 }
174
177 if (this != &other) {
178 coeff() = copy(other.coeff());
179 _norm_tree = other._norm_tree;
181 dnorm=other.dnorm;
182 snorm=other.snorm;
184 }
185 return *this;
186 }
187
188 /// Copy with possible type conversion of coefficients, copying all other state
189
190 /// Choose to not overload copy and type conversion operators
191 /// so there are no automatic type conversions.
192 template<typename Q>
194 convert() const {
195 return FunctionNode<Q, NDIM> (madness::convert<Q,T>(coeff()), _norm_tree, snorm, dnorm, _has_children);
196 }
197
198 /// Returns true if there are coefficients in this node
199 bool
200 has_coeff() const {
201 return _coeffs.has_data();
202 }
203
204
205 /// Returns true if this node has children
206 bool
207 has_children() const {
208 return _has_children;
209 }
210
211 /// Returns true if this does not have children
212 bool
213 is_leaf() const {
214 return !_has_children;
215 }
216
217 /// Returns true if this node is invalid (no coeffs and no children)
218 bool
219 is_invalid() const {
220 return !(has_coeff() || has_children());
221 }
222
223 /// Returns a non-const reference to the tensor containing the coeffs
224
225 /// Returns an empty tensor if there are no coefficients.
226 coeffT&
228 MADNESS_ASSERT(_coeffs.ndim() == -1 || (_coeffs.dim(0) <= 2
229 * MAXK && _coeffs.dim(0) >= 0));
230 return const_cast<coeffT&>(_coeffs);
231 }
232
233 /// Returns a const reference to the tensor containing the coeffs
234
235 /// Returns an empty tensor if there are no coefficeints.
236 const coeffT&
237 coeff() const {
238 return const_cast<const coeffT&>(_coeffs);
239 }
240
241 /// Returns the number of coefficients in this node
242 size_t size() const {
243 return _coeffs.size();
244 }
245
246 public:
247
248 /// reduces the rank of the coefficients (if applicable)
249 void reduceRank(const double& eps) {
250 _coeffs.reduce_rank(eps);
251 }
252
253 /// Sets \c has_children attribute to value of \c flag.
254 void set_has_children(bool flag) {
255 _has_children = flag;
256 }
257
258 /// Sets \c has_children attribute to true recurring up to ensure connected
260 //madness::print(" set_chi_recu: ", key, *this);
261 //PROFILE_MEMBER_FUNC(FunctionNode); // Too fine grain for routine profiling
262 if (!(has_children() || has_coeff() || key.level()==0)) {
263 // If node already knows it has children or it has
264 // coefficients then it must already be connected to
265 // its parent. If not, the node was probably just
266 // created for this operation and must be connected to
267 // its parent.
268 Key<NDIM> parent = key.parent();
269 // Task on next line used to be TaskAttributes::hipri()) ... but deferring execution of this
270 // makes sense since it is not urgent and lazy connection will likely mean that less forwarding
271 // will happen since the upper level task will have already made the connection.
272 const_cast<dcT&>(c).task(parent, &FunctionNode<T,NDIM>::set_has_children_recursive, c, parent);
273 //const_cast<dcT&>(c).send(parent, &FunctionNode<T,NDIM>::set_has_children_recursive, c, parent);
274 //madness::print(" set_chi_recu: forwarding",key,parent);
275 }
276 _has_children = true;
277 }
278
279 /// Sets \c has_children attribute to value of \c !flag
280 void set_is_leaf(bool flag) {
281 _has_children = !flag;
282 }
283
284 /// Takes a \em shallow copy of the coeff --- same as \c this->coeff()=coeff
285 void set_coeff(const coeffT& coeffs) {
286 coeff() = coeffs;
287 if ((_coeffs.has_data()) and ((_coeffs.dim(0) < 0) || (_coeffs.dim(0)>2*MAXK))) {
288 print("set_coeff: may have a problem");
289 print("set_coeff: coeff.dim[0] =", coeffs.dim(0), ", 2* MAXK =", 2*MAXK);
290 }
291 MADNESS_ASSERT(coeffs.dim(0)<=2*MAXK && coeffs.dim(0)>=0);
292 }
293
294 /// Clears the coefficients (has_coeff() will subsequently return false)
295 void clear_coeff() {
296 coeff()=coeffT();
297 }
298
299 /// Scale the coefficients of this node
300 template <typename Q>
301 void scale(Q a) {
302 _coeffs.scale(a);
303 }
304
305 /// Sets the value of norm_tree
308 }
309
310 /// Gets the value of norm_tree
311 double get_norm_tree() const {
312 return _norm_tree;
313 }
314
315 /// return the precomputed norm of the (virtual) d coefficients
316 double get_dnorm() const {
317 return dnorm;
318 }
319
320 /// set the precomputed norm of the (virtual) s coefficients
321 void set_snorm(const double sn) {
322 snorm=sn;
323 }
324
325 /// set the precomputed norm of the (virtual) d coefficients
326 void set_dnorm(const double dn) {
327 dnorm=dn;
328 }
329
330 /// get the precomputed norm of the (virtual) s coefficients
331 double get_snorm() const {
332 return snorm;
333 }
334
336 snorm = 0.0;
337 dnorm = 0.0;
338 if (coeff().size() == 0) { ;
339 } else if (coeff().dim(0) == cdata.vk[0]) {
340 snorm = coeff().normf();
341
342 } else if (coeff().is_full_tensor()) {
343 Tensor<T> c = copy(coeff().get_tensor());
344 snorm = c(cdata.s0).normf();
345 c(cdata.s0) = 0.0;
346 dnorm = c.normf();
347
348 } else if (coeff().is_svd_tensor()) {
349 coeffT c= coeff()(cdata.s0);
350 snorm = c.normf();
351 double norm = coeff().normf();
352 dnorm = sqrt(norm * norm - snorm * snorm);
353
354 } else {
355 MADNESS_EXCEPTION("cannot use compute_dnorm", 1);
356 }
357 }
358
359
360 /// General bi-linear operation --- this = this*alpha + other*beta
361
362 /// This/other may not have coefficients. Has_children will be
363 /// true in the result if either this/other have children.
364 template <typename Q, typename R>
365 void gaxpy_inplace(const T& alpha, const FunctionNode<Q,NDIM>& other, const R& beta) {
366 //PROFILE_MEMBER_FUNC(FuncNode); // Too fine grain for routine profiling
367 if (other.has_children())
368 _has_children = true;
369 if (has_coeff()) {
370 if (other.has_coeff()) {
371 coeff().gaxpy(alpha,other.coeff(),beta);
372 }
373 else {
374 coeff().scale(alpha);
375 }
376 }
377 else if (other.has_coeff()) {
378 coeff() = other.coeff()*beta; //? Is this the correct type conversion?
379 }
380 }
381
382 /// Accumulate inplace and if necessary connect node to parent
383 void accumulate2(const tensorT& t, const typename FunctionNode<T,NDIM>::dcT& c,
384 const Key<NDIM>& key) {
385 // double cpu0=cpu_time();
386 if (has_coeff()) {
387 MADNESS_ASSERT(coeff().is_full_tensor());
388 // if (coeff().type==TT_FULL) {
389 coeff() += coeffT(t,-1.0,TT_FULL);
390 // } else {
391 // tensorT cc=coeff().full_tensor_copy();;
392 // cc += t;
393 // coeff()=coeffT(cc,args);
394 // }
395 }
396 else {
397 // No coeff and no children means the node is newly
398 // created for this operation and therefore we must
399 // tell its parent that it exists.
400 coeff() = coeffT(t,-1.0,TT_FULL);
401 // coeff() = copy(t);
402 // coeff() = coeffT(t,args);
403 if ((!_has_children) && key.level()> 0) {
404 Key<NDIM> parent = key.parent();
405 if (c.is_local(parent))
406 const_cast<dcT&>(c).send(parent, &FunctionNode<T,NDIM>::set_has_children_recursive, c, parent);
407 else
408 const_cast<dcT&>(c).task(parent, &FunctionNode<T,NDIM>::set_has_children_recursive, c, parent);
409 }
410 }
411 //double cpu1=cpu_time();
412 }
413
414
415 /// Accumulate inplace and if necessary connect node to parent
416 void accumulate(const coeffT& t, const typename FunctionNode<T,NDIM>::dcT& c,
417 const Key<NDIM>& key, const TensorArgs& args) {
418 if (has_coeff()) {
419 coeff().add_SVD(t,args.thresh);
420 if (buffer.rank()<coeff().rank()) {
421 if (buffer.has_data()) {
422 buffer.add_SVD(coeff(),args.thresh);
423 } else {
424 buffer=copy(coeff());
425 }
426 coeff()=coeffT();
427 }
428
429 } else {
430 // No coeff and no children means the node is newly
431 // created for this operation and therefore we must
432 // tell its parent that it exists.
433 coeff() = copy(t);
434 if ((!_has_children) && key.level()> 0) {
435 Key<NDIM> parent = key.parent();
436 if (c.is_local(parent))
437 const_cast<dcT&>(c).send(parent, &FunctionNode<T,NDIM>::set_has_children_recursive, c, parent);
438 else
439 const_cast<dcT&>(c).task(parent, &FunctionNode<T,NDIM>::set_has_children_recursive, c, parent);
440 }
441 }
442 }
443
444 void consolidate_buffer(const TensorArgs& args) {
445 if ((coeff().has_data()) and (buffer.has_data())) {
446 coeff().add_SVD(buffer,args.thresh);
447 } else if (buffer.has_data()) {
448 coeff()=buffer;
449 }
450 buffer=coeffT();
451 }
452
453 T trace_conj(const FunctionNode<T,NDIM>& rhs) const {
454 return this->_coeffs.trace_conj((rhs._coeffs));
455 }
456
457 template <typename Archive>
458 void serialize(Archive& ar) {
460 }
461
462 /// like operator<<(ostream&, const FunctionNode<T,NDIM>&) but
463 /// produces a sequence JSON-formatted key-value pairs
464 /// @warning enclose the output in curly braces to make
465 /// a valid JSON object
466 void print_json(std::ostream& s) const {
467 s << "\"has_coeff\":" << this->has_coeff()
468 << ",\"has_children\":" << this->has_children() << ",\"norm\":";
469 double norm = this->has_coeff() ? this->coeff().normf() : 0.0;
470 if (norm < 1e-12)
471 norm = 0.0;
472 double nt = this->get_norm_tree();
473 if (nt == 1e300)
474 nt = 0.0;
475 s << norm << ",\"norm_tree\":" << nt << ",\"snorm\":"
476 << this->get_snorm() << ",\"dnorm\":" << this->get_dnorm()
477 << ",\"rank\":" << this->coeff().rank();
478 if (this->coeff().is_assigned())
479 s << ",\"dim\":" << this->coeff().dim(0);
480 }
481
482 };
483
484 template <typename T, std::size_t NDIM>
485 std::ostream& operator<<(std::ostream& s, const FunctionNode<T,NDIM>& node) {
486 s << "(has_coeff=" << node.has_coeff() << ", has_children=" << node.has_children() << ", norm=";
487 double norm = node.has_coeff() ? node.coeff().normf() : 0.0;
488 if (norm < 1e-12)
489 norm = 0.0;
490 double nt = node.get_norm_tree();
491 if (nt == 1e300) nt = 0.0;
492 s << norm << ", norm_tree, s/dnorm =" << nt << ", " << node.get_snorm() << " " << node.get_dnorm() << "), rank="<< node.coeff().rank()<<")";
493 if (node.coeff().is_assigned()) s << " dim " << node.coeff().dim(0) << " ";
494 return s;
495 }
496
497
498 /// returns true if the result of a hartree_product is a leaf node (compute norm & error)
499 template<typename T, size_t NDIM>
501
504 long k;
505 bool do_error_leaf_op() const {return false;}
506
507 hartree_leaf_op() = default;
508 hartree_leaf_op(const implT* f, const long& k) : f(f), k(k) {}
509
510 /// no pre-determination
511 bool operator()(const Key<NDIM>& key) const {return false;}
512
513 /// no post-determination
514 bool operator()(const Key<NDIM>& key, const GenTensor<T>& coeff) const {
515 MADNESS_EXCEPTION("no post-determination in hartree_leaf_op",1);
516 return true;
517 }
518
519 /// post-determination: true if f is a leaf and the result is well-represented
520
521 /// @param[in] key the hi-dimensional key (breaks into keys for f and g)
522 /// @param[in] fcoeff coefficients of f of its appropriate key in NS form
523 /// @param[in] gcoeff coefficients of g of its appropriate key in NS form
524 bool operator()(const Key<NDIM>& key, const Tensor<T>& fcoeff, const Tensor<T>& gcoeff) const {
525
526 if (key.level()<2) return false;
527 Slice s = Slice(0,k-1);
528 std::vector<Slice> s0(NDIM/2,s);
529
530 const double tol=f->get_thresh();
531 const double thresh=f->truncate_tol(tol, key)*0.3; // custom factor to "ensure" accuracy
532 // include the wavelets in the norm, makes it much more accurate
533 const double fnorm=fcoeff.normf();
534 const double gnorm=gcoeff.normf();
535
536 // if the final norm is small, perform the hartree product and return
537 const double norm=fnorm*gnorm; // computing the outer product
538 if (norm < thresh) return true;
539
540 // norm of the scaling function coefficients
541 const double sfnorm=fcoeff(s0).normf();
542 const double sgnorm=gcoeff(s0).normf();
543
544 // get the error of both functions and of the pair function;
545 // need the abs for numerics: sfnorm might be equal fnorm.
546 const double ferror=sqrt(std::abs(fnorm*fnorm-sfnorm*sfnorm));
547 const double gerror=sqrt(std::abs(gnorm*gnorm-sgnorm*sgnorm));
548
549 // if the expected error is small, perform the hartree product and return
550 const double error=fnorm*gerror + ferror*gnorm + ferror*gerror;
551 // const double error=sqrt(fnorm*fnorm*gnorm*gnorm - sfnorm*sfnorm*sgnorm*sgnorm);
552
553 if (error < thresh) return true;
554 return false;
555 }
556 template <typename Archive> void serialize (Archive& ar) {
557 ar & f & k;
558 }
559 };
560
561 /// returns true if the result of the convolution operator op with some provided
562 /// coefficients will be small
563 template<typename T, size_t NDIM, typename opT>
564 struct op_leaf_op {
566
567 const opT* op; ///< the convolution operator
568 const implT* f; ///< the source or result function, needed for truncate_tol
569 bool do_error_leaf_op() const {return true;}
570
571 op_leaf_op() = default;
572 op_leaf_op(const opT* op, const implT* f) : op(op), f(f) {}
573
574 /// pre-determination: we can't know if this will be a leaf node before we got the final coeffs
575 bool operator()(const Key<NDIM>& key) const {return true;}
576
577 /// post-determination: return true if operator and coefficient norms are small
578 bool operator()(const Key<NDIM>& key, const GenTensor<T>& coeff) const {
579 if (key.level()<2) return false;
580 const double cnorm=coeff.normf();
581 return this->operator()(key,cnorm);
582 }
583
584 /// post-determination: return true if operator and coefficient norms are small
585 bool operator()(const Key<NDIM>& key, const double& cnorm) const {
586 if (key.level()<2) return false;
587
588 typedef Key<opT::opdim> opkeyT;
589 const opkeyT source=op->get_source_key(key);
590
591 const double thresh=f->truncate_tol(f->get_thresh(),key);
592 const std::vector<opkeyT>& disp = op->get_disp(key.level());
593 const opkeyT& d = *disp.begin(); // use the zero-displacement for screening
594 const double opnorm = op->norm(key.level(), d, source);
595 const double norm=opnorm*cnorm;
596 return norm<thresh;
597
598 }
599
600 template <typename Archive> void serialize (Archive& ar) {
601 ar & op & f;
602 }
603
604 };
605
606
607 /// returns true if the result of a hartree_product is a leaf node
608 /// criteria are error, norm and its effect on a convolution operator
609 template<typename T, size_t NDIM, size_t LDIM, typename opT>
611
614
616 const implL* g; // for use of its cdata only
617 const opT* op;
618 bool do_error_leaf_op() const {return false;}
619
621 hartree_convolute_leaf_op(const implT* f, const implL* g, const opT* op)
622 : f(f), g(g), op(op) {}
623
624 /// no pre-determination
625 bool operator()(const Key<NDIM>& key) const {return true;}
626
627 /// no post-determination
628 bool operator()(const Key<NDIM>& key, const GenTensor<T>& coeff) const {
629 MADNESS_EXCEPTION("no post-determination in hartree_convolute_leaf_op",1);
630 return true;
631 }
632
633 /// post-determination: true if f is a leaf and the result is well-represented
634
635 /// @param[in] key the hi-dimensional key (breaks into keys for f and g)
636 /// @param[in] fcoeff coefficients of f of its appropriate key in NS form
637 /// @param[in] gcoeff coefficients of g of its appropriate key in NS form
638 bool operator()(const Key<NDIM>& key, const Tensor<T>& fcoeff, const Tensor<T>& gcoeff) const {
639 // bool operator()(const Key<NDIM>& key, const GenTensor<T>& coeff) const {
640
641 if (key.level()<2) return false;
642
643 const double tol=f->get_thresh();
644 const double thresh=f->truncate_tol(tol, key);
645 // include the wavelets in the norm, makes it much more accurate
646 const double fnorm=fcoeff.normf();
647 const double gnorm=gcoeff.normf();
648
649 // norm of the scaling function coefficients
650 const double sfnorm=fcoeff(g->get_cdata().s0).normf();
651 const double sgnorm=gcoeff(g->get_cdata().s0).normf();
652
653 // if the final norm is small, perform the hartree product and return
654 const double norm=fnorm*gnorm; // computing the outer product
655 if (norm < thresh) return true;
656
657 // get the error of both functions and of the pair function
658 const double ferror=sqrt(fnorm*fnorm-sfnorm*sfnorm);
659 const double gerror=sqrt(gnorm*gnorm-sgnorm*sgnorm);
660
661 // if the expected error is small, perform the hartree product and return
662 const double error=fnorm*gerror + ferror*gnorm + ferror*gerror;
663 if (error < thresh) return true;
664
665 // now check if the norm of this and the norm of the operator are significant
666 const std::vector<Key<NDIM> >& disp = op->get_disp(key.level());
667 const Key<NDIM>& d = *disp.begin(); // use the zero-displacement for screening
668 const double opnorm = op->norm(key.level(), d, key);
669 const double final_norm=opnorm*sfnorm*sgnorm;
670 if (final_norm < thresh) return true;
671
672 return false;
673 }
674 template <typename Archive> void serialize (Archive& ar) {
675 ar & f & op;
676 }
677 };
678
679 template<typename T, size_t NDIM>
680 struct noop {
681 void operator()(const Key<NDIM>& key, const GenTensor<T>& coeff, const bool& is_leaf) const {}
682 bool operator()(const Key<NDIM>& key, const GenTensor<T>& fcoeff, const GenTensor<T>& gcoeff) const {
683 MADNESS_EXCEPTION("in noop::operator()",1);
684 return true;
685 }
686 template <typename Archive> void serialize (Archive& ar) {}
687
688 };
689
690 /// insert/replaces the coefficients into the function
691 template<typename T, std::size_t NDIM>
692 struct insert_op {
697
701 insert_op(const insert_op& other) : impl(other.impl) {}
702 void operator()(const keyT& key, const coeffT& coeff, const bool& is_leaf) const {
704 impl->get_coeffs().replace(key,nodeT(coeff,not is_leaf));
705 }
706 template <typename Archive> void serialize (Archive& ar) {
707 ar & impl;
708 }
709
710 };
711
712 /// inserts/accumulates coefficients into impl's tree
713
714 /// NOTE: will use buffer and will need consolidation after operation ended !! NOTE !!
715 template<typename T, std::size_t NDIM>
719
721 accumulate_op() = default;
723 accumulate_op(const accumulate_op& other) = default;
724 void operator()(const Key<NDIM>& key, const coeffT& coeff, const bool& is_leaf) const {
725 if (coeff.has_data())
726 impl->get_coeffs().task(key, &nodeT::accumulate, coeff, impl->get_coeffs(), key, impl->get_tensor_args());
727 }
728 template <typename Archive> void serialize (Archive& ar) {
729 ar & impl;
730 }
731
732 };
733
734
735template<size_t NDIM>
736 struct true_op {
737
738 template<typename T>
739 bool operator()(const Key<NDIM>& key, const T& t) const {return true;}
740
741 template<typename T, typename R>
742 bool operator()(const Key<NDIM>& key, const T& t, const R& r) const {return true;}
743 template <typename Archive> void serialize (Archive& ar) {}
744
745 };
746
747 /// shallow-copy, pared-down version of FunctionNode, for special purpose only
748 template<typename T, std::size_t NDIM>
749 struct ShallowNode {
753 double dnorm=-1.0;
756 : _coeffs(node.coeff()), _has_children(node.has_children()),
757 dnorm(node.get_dnorm()) {}
759 : _coeffs(node.coeff()), _has_children(node._has_children),
760 dnorm(node.dnorm) {}
761
762 const coeffT& coeff() const {return _coeffs;}
763 coeffT& coeff() {return _coeffs;}
764 bool has_children() const {return _has_children;}
765 bool is_leaf() const {return not _has_children;}
766 template <typename Archive>
767 void serialize(Archive& ar) {
768 ar & coeff() & _has_children & dnorm;
769 }
770 };
771
772
773 /// a class to track where relevant (parent) coeffs are
774
775 /// E.g. if a 6D function is composed of two 3D functions their coefficients must be tracked.
776 /// We might need coeffs from a box that does not exist, and to avoid searching for
777 /// parents we track which are their required respective boxes.
778 /// - CoeffTracker will refer either to a requested key, if it exists, or to its
779 /// outermost parent.
780 /// - Children must be made in sequential order to be able to track correctly.
781 ///
782 /// Usage: 1. make the child of a given CoeffTracker.
783 /// If the parent CoeffTracker refers to a leaf node (flag is_leaf)
784 /// the child will refer to the same node. Otherwise it will refer
785 /// to the child node.
786 /// 2. retrieve its coefficients (possible communication/ returns a Future).
787 /// Member variable key always refers to an existing node,
788 /// so we can fetch it. Once we have the node we can determine
789 /// if it has children which allows us to make a child (see 1. )
790 template<typename T, size_t NDIM>
792
796 typedef std::pair<Key<NDIM>,ShallowNode<T,NDIM> > datumT;
798
799 /// the funcimpl that has the coeffs
800 const implT* impl;
801 /// the current key, which must exists in impl
803 /// flag if key is a leaf node
805 /// the coefficients belonging to key
807 /// norm of d coefficients corresponding to key
808 double dnorm_=-1.0;
809
810 public:
811
812 /// default ctor
813 CoeffTracker() : impl(), key_(0), is_leaf_(unknown), coeff_() {} // Initialize key to avoid warnings of possible unititialied use
814
815 /// the initial ctor making the root key
817 if (impl) key_=impl->get_cdata().key0;
818 }
819
820 /// ctor with a pair<keyT,nodeT>
821 explicit CoeffTracker(const CoeffTracker& other, const datumT& datum)
822 : impl(other.impl), key_(other.key_), coeff_(datum.second.coeff()),
823 dnorm_(datum.second.dnorm) {
824 if (datum.second.is_leaf()) is_leaf_=yes;
825 else is_leaf_=no;
826 }
827
828 /// copy ctor
829 CoeffTracker(const CoeffTracker& other) : impl(other.impl), key_(other.key_),
830 is_leaf_(other.is_leaf_), coeff_(other.coeff_), dnorm_(other.dnorm_) {};
831
832 /// const reference to impl
833 const implT* get_impl() const {return impl;}
834
835 /// const reference to the coeffs
836 const coeffT& coeff() const {return coeff_;}
837
838 /// const reference to the key
839 const keyT& key() const {return key_;}
840
841 /// return the coefficients belonging to the passed-in key
842
843 /// if key equals tracked key just return the coeffs, otherwise
844 /// make the child coefficients.
845 /// @param[in] key return coeffs corresponding to this key
846 /// @return coefficients belonging to key
854
855 /// return the s and dnorm belonging to the passed-in key
856 double dnorm(const keyT& key) const {
857 if (key==key_) return dnorm_;
858 MADNESS_ASSERT(key.is_child_of(key_));
859 return 0.0;
860 }
861
862 /// const reference to is_leaf flag
863 const LeafStatus& is_leaf() const {return is_leaf_;}
864
865 /// make a child of this, ignoring the coeffs
866 CoeffTracker make_child(const keyT& child) const {
867
868 // fast return
869 if ((not impl) or impl->is_on_demand()) return CoeffTracker(*this);
870
871 // can't make a child without knowing if this is a leaf -- activate first
873
874 CoeffTracker result;
875 if (impl) {
876 result.impl=impl;
877 if (is_leaf_==yes) result.key_=key_;
878 if (is_leaf_==no) {
879 result.key_=child;
880 // check if child is direct descendent of this, but root node is special case
881 if (child.level()>0) MADNESS_ASSERT(result.key().level()==key().level()+1);
882 }
883 result.is_leaf_=unknown;
884 }
885 return result;
886 }
887
888 /// find the coefficients
889
890 /// this involves communication to a remote node
891 /// @return a Future<CoeffTracker> with the coefficients that key refers to
893
894 // fast return
895 if (not impl) return Future<CoeffTracker>(CoeffTracker());
897
898 // this will return a <keyT,nodeT> from a remote node
901
902 // construct a new CoeffTracker locally
903 return impl->world.taskq.add(*const_cast<CoeffTracker*> (this),
904 &CoeffTracker::forward_ctor,*this,datum1);
905 }
906
907 private:
908 /// taskq-compatible forwarding to the ctor
909 CoeffTracker forward_ctor(const CoeffTracker& other, const datumT& datum) const {
910 return CoeffTracker(other,datum);
911 }
912
913 public:
914 /// serialization
915 template <typename Archive> void serialize(const Archive& ar) {
916 int il=int(is_leaf_);
917 ar & impl & key_ & il & coeff_ & dnorm_;
919 }
920 };
921
922 template<typename T, std::size_t NDIM>
923 std::ostream&
924 operator<<(std::ostream& s, const CoeffTracker<T,NDIM>& ct) {
925 s << ct.key() << ct.is_leaf() << " " << ct.get_impl();
926 return s;
927 }
928
929 /// FunctionImpl holds all Function state to facilitate shallow copy semantics
930
931 /// Since Function assignment and copy constructors are shallow it
932 /// greatly simplifies maintaining consistent state to have all
933 /// (permanent) state encapsulated in a single class. The state
934 /// is shared between instances using a shared_ptr<FunctionImpl>.
935 ///
936 /// The FunctionImpl inherits all of the functionality of WorldContainer
937 /// (to store the coefficients) and WorldObject<WorldContainer> (used
938 /// for RMI and for its unqiue id).
939 ///
940 /// The class methods are public to avoid painful multiple friend template
941 /// declarations for Function and FunctionImpl ... but this trust should not be
942 /// abused ... NOTHING except FunctionImpl methods should mess with FunctionImplData.
943 /// The LB stuff might have to be an exception.
944 template <typename T, std::size_t NDIM>
945 class FunctionImpl : public WorldObject< FunctionImpl<T,NDIM> > {
946 private:
947 typedef WorldObject< FunctionImpl<T,NDIM> > woT; ///< Base class world object type
948 public:
949 typedef T typeT;
950 typedef FunctionImpl<T,NDIM> implT; ///< Type of this class (implementation)
951 typedef std::shared_ptr< FunctionImpl<T,NDIM> > pimplT; ///< pointer to this class
952 typedef Tensor<T> tensorT; ///< Type of tensor for anything but to hold coeffs
953 typedef Vector<Translation,NDIM> tranT; ///< Type of array holding translation
954 typedef Key<NDIM> keyT; ///< Type of key
955 typedef FunctionNode<T,NDIM> nodeT; ///< Type of node
956 typedef GenTensor<T> coeffT; ///< Type of tensor used to hold coeffs
957 typedef WorldContainer<keyT,nodeT> dcT; ///< Type of container holding the coefficients
958 typedef std::pair<const keyT,nodeT> datumT; ///< Type of entry in container
959 typedef Vector<double,NDIM> coordT; ///< Type of vector holding coordinates
960
961 //template <typename Q, int D> friend class Function;
962 template <typename Q, std::size_t D> friend class FunctionImpl;
963
965
966 /// getter
969 const std::vector<Vector<double,NDIM> >& get_special_points()const{return special_points;}
970
971 private:
972 int k; ///< Wavelet order
973 double thresh; ///< Screening threshold
974 int initial_level; ///< Initial level for refinement
975 int special_level; ///< Minimium level for refinement on special points
976 std::vector<Vector<double,NDIM> > special_points; ///< special points for further refinement (needed for composite functions or multiplication)
977 const Tensor<double> cell; ///< the size of the root cell in each dimension, unchangeable
978 int max_refine_level; ///< Do not refine below this level
979 int truncate_mode; ///< 0=default=(|d|<thresh), 1=(|d|<thresh/2^n), 2=(|d|<thresh/4^n);
980 bool autorefine; ///< If true, autorefine where appropriate
981 bool truncate_on_project; ///< If true projection inserts at level n-1 not n
982 TensorArgs targs; ///< type of tensor to be used in the FunctionNodes
983
985
986 std::shared_ptr< FunctionFunctorInterface<T,NDIM> > functor;
988
989 dcT coeffs; ///< The coefficients
990
991 // Disable the default copy constructor
993
994 public:
1003
1004 /// Initialize function impl from data in factory
1006 : WorldObject<implT>(factory._world)
1007 , world(factory._world)
1008 , k(factory._k)
1009 , thresh(factory._thresh)
1010 , initial_level(factory._initial_level)
1011 , special_level(factory._special_level)
1012 , special_points(factory._special_points)
1014 , max_refine_level(factory._max_refine_level)
1015 , truncate_mode(factory._truncate_mode)
1016 , autorefine(factory._autorefine)
1017 , truncate_on_project(factory._truncate_on_project)
1018 , targs(factory._thresh,FunctionDefaults<NDIM>::get_tensor_type())
1019 , cdata(FunctionCommonData<T,NDIM>::get(k))
1020 , functor(factory.get_functor())
1021 , tree_state(factory._tree_state)
1022 , coeffs(world,factory._pmap,false)
1023 //, bc(factory._bc)
1024 {
1025 // PROFILE_MEMBER_FUNC(FunctionImpl); // No need to profile this
1026 // !!! Ensure that all local state is correctly formed
1027 // before invoking process_pending for the coeffs and
1028 // for this. Otherwise, there is a race condition.
1029 MADNESS_ASSERT(k>0 && k<=MAXK);
1030
1031 bool empty = (factory._empty or is_on_demand());
1032 bool do_refine = factory._refine;
1033
1034 if (do_refine)
1035 initial_level = std::max(0,initial_level - 1);
1036
1037 if (empty) { // Do not set any coefficients at all
1038 // additional functors are only evaluated on-demand
1039 } else if (functor) { // Project function and optionally refine
1041 // set the union of the special points of functor and the ones explicitly given to FunctionFactory
1042 std::vector<coordT> functor_special_points=functor->special_points();
1043 if (!functor_special_points.empty()) special_points.insert(special_points.end(), functor_special_points.begin(), functor_special_points.end());
1044 // near special points refine as deeply as requested by the factory AND the functor
1045 special_level = std::max(special_level, functor->special_level());
1046
1047 typename dcT::const_iterator end = coeffs.end();
1048 for (typename dcT::const_iterator it=coeffs.begin(); it!=end; ++it) {
1049 if (it->second.is_leaf())
1050 woT::task(coeffs.owner(it->first), &implT::project_refine_op, it->first, do_refine,
1052 }
1053 }
1054 else { // Set as if a zero function
1055 initial_level = 1;
1057 }
1058
1060 this->process_pending();
1061 if (factory._fence && (functor || !empty)) world.gop.fence();
1062 }
1063
1064 /// Copy constructor
1065
1066 /// Allocates a \em new function in preparation for a deep copy
1067 ///
1068 /// By default takes pmap from other but can also specify a different pmap.
1069 /// Does \em not copy the coefficients ... creates an empty container.
1070 template <typename Q>
1072 const std::shared_ptr< WorldDCPmapInterface< Key<NDIM> > >& pmap,
1073 bool dozero) : FunctionImpl(other.world, other, pmap, dozero) {
1074 }
1075
1076 /// Copy constructor
1077
1078 /// Allocates a \em new function in preparation for a deep copy
1079 ///
1080 /// By default takes pmap from other but can also specify a different pmap.
1081 /// Does \em not copy the coefficients ... creates an empty container.
1082 ///
1083 /// uses a different world for the new function
1084 template <typename Q>
1086 const FunctionImpl<Q,NDIM>& other,
1087 const std::shared_ptr< WorldDCPmapInterface< Key<NDIM> > >& pmap,
1088 bool dozero)
1090 , world(world)
1091 , k(other.k)
1092 , thresh(other.thresh)
1096 , cell(other.cell)
1099 , autorefine(other.autorefine)
1101 , targs(other.targs)
1102 , cdata(FunctionCommonData<T,NDIM>::get(k))
1103 , functor()
1104 , tree_state(other.tree_state)
1105 , coeffs(world, pmap ? pmap : other.coeffs.get_pmap())
1106 {
1107 if (dozero) {
1108 initial_level = 1;
1110 //world.gop.fence(); <<<<<<<<<<<<<<<<<<<<<< needs a fence argument
1111 }
1113 this->process_pending();
1114 }
1115
1116 virtual ~FunctionImpl() { }
1117
1118 const std::shared_ptr< WorldDCPmapInterface< Key<NDIM> > >& get_pmap() const;
1119
1120 void replicate(bool fence=true) {
1121 coeffs.replicate(fence);
1122 }
1123
1124 void replicate_on_hosts(bool fence=true) {
1126 }
1127
1128 // remove all coeffs that are not local according to pmap
1129 void undo_replicate(bool fence=true) {
1130 std::list<keyT> keys;
1131 for (const auto& [key, node] : coeffs) if (not coeffs.is_local(key)) keys.push_back(key);
1132 for (const auto& key : keys) coeffs.erase(key);
1133 if (fence) world.gop.fence();
1134 }
1135
1136 void distribute(std::shared_ptr< WorldDCPmapInterface< Key<NDIM> > > newmap) const {
1137 auto currentmap=coeffs.get_pmap();
1138 currentmap->redistribute(world,newmap);
1139 }
1140
1141 /// Copy coeffs from other into self
1142
1143 /// this and other might live in different worlds
1144 template <typename Q>
1145 void copy_coeffs(const FunctionImpl<Q,NDIM>& other, bool fence) {
1146 if (world.id()==other.world.id())
1147 copy_coeffs_same_world(other,false);
1148 else
1150 if (fence) world.gop.fence();
1151 }
1152
1153 /// Copy coefficients from other funcimpl with possibly different world and on a different node
1154 template<typename Q>
1156
1157 // copy coeffs from (a subset of) other's world
1158
1159 // if other's data is distributed, we need to fetch from all ranks
1160 if (other.get_coeffs().is_distributed()) {
1161 for (ProcessID pid=0; pid<other.world.size(); ++pid) {
1162 copy_remote_coeffs_from_pid<Q>(pid, other);
1163 }
1164
1165 // if other's data is replicated, all coeffs are on the rank that owns key0
1166 } else if (other.get_coeffs().is_replicated() or other.get_coeffs().is_host_replicated()) {
1167 auto key0=other.cdata.key0;
1168 copy_remote_coeffs_from_pid<Q>(other.get_pmap()->owner(key0), other);
1169 }
1170 }
1171
1172 /// Copy coefficients from other funcimpl with possibly different world and on a different node
1173 /// to this
1174 template <typename Q>
1176 typedef FunctionImpl<Q,NDIM> implQ; ///< Type of this class (implementation)
1177 // std::vector<unsigned char> v=other.task(pid, &implQ::serialize_remote_coeffs).get();
1178 auto v=other.task(pid, &implQ::serialize_remote_coeffs);
1180 }
1181
1182 /// invoked by copy_remote_coeffs_from_pid to serialize *local* coeffs
1183 std::vector<unsigned char> serialize_remote_coeffs() {
1184 std::vector<unsigned char> v;
1186 ar & get_coeffs();
1187 return v;
1188 }
1189
1190 /// insert coeffs from vector archive into this
1191 void insert_serialized_coeffs(std::vector<unsigned char>& v) {
1193 ar & get_coeffs();
1194 }
1195
1196 /// Copy coeffs from other into self
1197 template <typename Q>
1198 void copy_coeffs_same_world(const FunctionImpl<Q,NDIM>& other, bool fence) {
1199 for (const auto& [key, node] : other.coeffs) { // iterate over all entries in other
1200 coeffs.replace(key,node. template convert<T>());
1201 }
1202 if (fence)
1203 world.gop.fence();
1204 }
1205
1206 /// perform inplace gaxpy: this = alpha*this + beta*other
1207 /// @param[in] alpha prefactor for this
1208 /// @param[in] beta prefactor for other
1209 /// @param[in] g the other function, reconstructed
1210 /// @return *this = alpha*this + beta*other, in either reconstructed or redundant_after_merge state
1211 template<typename Q, typename R>
1212 void gaxpy_inplace_reconstructed(const T& alpha, const FunctionImpl<Q,NDIM>& g, const R& beta, const bool fence) {
1213 // merge g's tree into this' tree
1214 gaxpy_inplace(alpha,g,beta,fence);
1216 // this->merge_trees(beta,g,alpha,fence);
1217 // tree is now redundant_after_merge
1218 // sum down the sum coeffs into the leafs if possible to keep the state most clean
1219 if (fence) sum_down(fence);
1220 }
1221
1222 /// merge the trees of this and other, while multiplying them with the alpha or beta, resp
1223
1224 /// first step in an inplace gaxpy operation for reconstructed functions; assuming the same
1225 /// distribution for this and other
1226
1227 /// on output, *this = alpha* *this + beta * other
1228 /// @param[in] alpha prefactor for this
1229 /// @param[in] beta prefactor for other
1230 /// @param[in] other the other function, reconstructed
1231 template<typename Q, typename R>
1232 void merge_trees(const T alpha, const FunctionImpl<Q,NDIM>& other, const R beta, const bool fence=true) {
1233 MADNESS_ASSERT(get_pmap() == other.get_pmap());
1236 }
1237
1238 /// merge the trees of this and other, while multiplying them with the alpha or beta, resp
1239
1240 /// result and rhs do not have to have the same distribution or live in the same world
1241 /// result+=alpha* this
1242 /// @param[in] alpha prefactor for this
1243 template<typename Q, typename R>
1244 void accumulate_trees(FunctionImpl<Q,NDIM>& result, const R alpha, const bool fence=true) const {
1246 }
1247
1248 /// perform: this= alpha*f + beta*g, invoked by result
1249
1250 /// f and g are reconstructed, so we can save on the compress operation,
1251 /// walk down the joint tree, and add leaf coefficients; effectively refines
1252 /// to common finest level.
1253
1254 /// nothing returned, but leaves this's tree reconstructed and as sum of f and g
1255 /// @param[in] alpha prefactor for f
1256 /// @param[in] f first addend
1257 /// @param[in] beta prefactor for g
1258 /// @param[in] g second addend
1259 void gaxpy_oop_reconstructed(const double alpha, const implT& f,
1260 const double beta, const implT& g, const bool fence);
1261
1262 /// functor for the gaxpy_inplace method
1263 template <typename Q, typename R>
1266 FunctionImpl<T,NDIM>* f; ///< prefactor for current function impl
1267 T alpha; ///< the current function impl
1268 R beta; ///< prefactor for other function impl
1269 do_gaxpy_inplace() = default;
1271 bool operator()(typename rangeT::iterator& it) const {
1272 const keyT& key = it->first;
1273 const FunctionNode<Q,NDIM>& other_node = it->second;
1274 // Use send to get write accessor and automated construction if missing
1275 f->coeffs.send(key, &nodeT:: template gaxpy_inplace<Q,R>, alpha, other_node, beta);
1276 return true;
1277 }
1278 template <typename Archive>
1279 void serialize(Archive& ar) {
1280 ar & f & alpha & beta;
1281 }
1282 };
1283
1284 /// Inplace general bilinear operation
1285
1286 /// this's world can differ from other's world
1287 /// this = alpha * this + beta * other
1288 /// @param[in] alpha prefactor for the current function impl
1289 /// @param[in] other the other function impl
1290 /// @param[in] beta prefactor for other
1291 template <typename Q, typename R>
1292 void gaxpy_inplace(const T& alpha,const FunctionImpl<Q,NDIM>& other, const R& beta, bool fence) {
1293// MADNESS_ASSERT(get_pmap() == other.get_pmap());
1294 if (alpha != T(1.0)) scale_inplace(alpha,false);
1296 typedef do_gaxpy_inplace<Q,R> opT;
1297 other.world.taskq. template for_each<rangeT,opT>(rangeT(other.coeffs.begin(), other.coeffs.end()), opT(this, T(1.0), beta));
1298 if (fence)
1299 other.world.gop.fence();
1300 }
1301
1302 // loads a function impl from persistence
1303 // @param[in] ar the archive where the function impl is stored
1304 template <typename Archive>
1305 void load(Archive& ar) {
1306 // WE RELY ON K BEING STORED FIRST
1307 int kk = 0;
1308 ar & kk;
1309
1310 MADNESS_ASSERT(kk==k);
1311
1312 // note that functor should not be (re)stored
1314 & autorefine & truncate_on_project & tree_state;//nonstandard & compressed ; //& bc;
1315
1316 ar & coeffs;
1317 world.gop.fence();
1318 }
1319
1320 // saves a function impl to persistence
1321 // @param[in] ar the archive where the function impl is to be stored
1322 template <typename Archive>
1323 void store(Archive& ar) {
1324 // WE RELY ON K BEING STORED FIRST
1325
1326 // note that functor should not be (re)stored
1328 & autorefine & truncate_on_project & tree_state;//nonstandard & compressed ; //& bc;
1329
1330 ar & coeffs;
1331 world.gop.fence();
1332 }
1333
1334 /// Returns true if the function is compressed.
1335 bool is_compressed() const;
1336
1337 /// Returns true if the function is compressed.
1338 bool is_reconstructed() const;
1339
1340 /// Returns true if the function is redundant.
1341 bool is_redundant() const;
1342
1343 /// Returns true if the function is redundant_after_merge.
1344 bool is_redundant_after_merge() const;
1345
1346 bool is_nonstandard() const;
1347
1348 bool is_nonstandard_with_leaves() const;
1349
1350 bool is_on_demand() const;
1351
1352 bool has_leaves() const;
1353
1354 void set_tree_state(const TreeState& state) {
1355 tree_state=state;
1356 }
1357
1359
1360 void set_functor(const std::shared_ptr<FunctionFunctorInterface<T,NDIM> > functor1);
1361
1362 std::shared_ptr<FunctionFunctorInterface<T,NDIM> > get_functor();
1363
1364 std::shared_ptr<FunctionFunctorInterface<T,NDIM> > get_functor() const;
1365
1366 void unset_functor();
1367
1368
1370
1372 void set_tensor_args(const TensorArgs& t);
1373
1374 double get_thresh() const;
1375
1376 /// return the simulation cell
1377 const Tensor<double>& get_cell() const { return cell; }
1378
1379 void set_thresh(double value);
1380
1381 bool get_autorefine() const;
1382
1383 void set_autorefine(bool value);
1384
1385 int get_k() const;
1386
1387 const dcT& get_coeffs() const;
1388
1389 dcT& get_coeffs();
1390
1392
1393 void accumulate_timer(const double time) const; // !!!!!!!!!!!! REDUNDANT !!!!!!!!!!!!!!!
1394
1395 void print_timer() const;
1396
1397 void reset_timer();
1398
1399 /// Adds a constant to the function. Local operation, optional fence
1400
1401 /// In scaling function basis must add value to first polyn in
1402 /// each box with appropriate scaling for level. In wavelet basis
1403 /// need only add at level zero.
1404 /// @param[in] t the scalar to be added
1405 void add_scalar_inplace(T t, bool fence);
1406
1407 /// Initialize nodes to zero function at initial_level of refinement.
1408
1409 /// Works for either basis. No communication.
1410 void insert_zero_down_to_initial_level(const keyT& key);
1411
1412 /// Truncate according to the threshold with optional global fence
1413
1414 /// If thresh<=0 the default value of this->thresh is used
1415 /// @param[in] tol the truncation tolerance
1416 void truncate(double tol, bool fence);
1417
1418 /// Returns true if after truncation this node has coefficients
1419
1420 /// Assumed to be invoked on process owning key. Possible non-blocking
1421 /// communication.
1422 /// @param[in] key the key of the current function node
1423 Future<bool> truncate_spawn(const keyT& key, double tol);
1424
1425 /// Actually do the truncate operation
1426 /// @param[in] key the key to the current function node being evaluated for truncation
1427 /// @param[in] tol the tolerance for thresholding
1428 /// @param[in] v vector of Future<bool>'s that specify whether the current nodes children have coeffs
1429 bool truncate_op(const keyT& key, double tol, const std::vector< Future<bool> >& v);
1430
1431 /// Evaluate function at quadrature points in the specified box
1432
1433 /// @param[in] key the key indicating where the quadrature points are located
1434 /// @param[in] f the interface to the elementary function
1435 /// @param[in] qx quadrature points on a level=0 box
1436 /// @param[out] fval values
1437 void fcube(const keyT& key, const FunctionFunctorInterface<T,NDIM>& f, const Tensor<double>& qx, tensorT& fval) const;
1438
1439 /// Evaluate function at quadrature points in the specified box
1440
1441 /// @param[in] key the key indicating where the quadrature points are located
1442 /// @param[in] f the interface to the elementary function
1443 /// @param[in] qx quadrature points on a level=0 box
1444 /// @param[out] fval values
1445 void fcube(const keyT& key, T (*f)(const coordT&), const Tensor<double>& qx, tensorT& fval) const;
1446
1447 /// Returns cdata.key0
1448 const keyT& key0() const;
1449
1450 /// Prints the coeffs tree of the current function impl
1451 /// @param[in] maxlevel the maximum level of the tree for printing
1452 /// @param[out] os the ostream to where the output is sent
1453 void print_tree(std::ostream& os = std::cout, Level maxlevel = 10000) const;
1454
1455 /// Functor for the do_print_tree method
1456 void do_print_tree(const keyT& key, std::ostream& os, Level maxlevel) const;
1457
1458 /// Prints the coeffs tree of the current function impl (using GraphViz)
1459 /// @param[in] maxlevel the maximum level of the tree for printing
1460 /// @param[out] os the ostream to where the output is sent
1461 void print_tree_graphviz(std::ostream& os = std::cout, Level maxlevel = 10000) const;
1462
1463 /// Functor for the do_print_tree method (using GraphViz)
1464 void do_print_tree_graphviz(const keyT& key, std::ostream& os, Level maxlevel) const;
1465
1466 /// Same as print_tree() but in JSON format
1467 /// @param[out] os the ostream to where the output is sent
1468 /// @param[in] maxlevel the maximum level of the tree for printing
1469 void print_tree_json(std::ostream& os = std::cout, Level maxlevel = 10000) const;
1470
1471 /// Functor for the do_print_tree_json method
1472 void do_print_tree_json(const keyT& key, std::multimap<Level, std::tuple<tranT, std::string>>& data, Level maxlevel) const;
1473
1474 /// convert a number [0,limit] to a hue color code [blue,red],
1475 /// or, if log is set, a number [1.e-10,limit]
1477 double limit;
1478 bool log;
1479 static double lower() {return 1.e-10;};
1481 do_convert_to_color(const double limit, const bool log) : limit(limit), log(log) {}
1482 double operator()(double val) const {
1483 double color=0.0;
1484
1485 if (log) {
1486 double val2=log10(val) - log10(lower()); // will yield >0.0
1487 double upper=log10(limit) -log10(lower());
1488 val2=0.7-(0.7/upper)*val2;
1489 color= std::max(0.0,val2);
1490 color= std::min(0.7,color);
1491 } else {
1492 double hue=0.7-(0.7/limit)*(val);
1493 color= std::max(0.0,hue);
1494 }
1495 return color;
1496 }
1497 };
1498
1499
1500 /// Print a plane ("xy", "xz", or "yz") containing the point x to file
1501
1502 /// works for all dimensions; we walk through the tree, and if a leaf node
1503 /// inside the sub-cell touches the plane we print it in pstricks format
1504 void print_plane(const std::string filename, const int xaxis, const int yaxis, const coordT& el2);
1505
1506 /// collect the data for a plot of the MRA structure locally on each node
1507
1508 /// @param[in] xaxis the x-axis in the plot (can be any axis of the MRA box)
1509 /// @param[in] yaxis the y-axis in the plot (can be any axis of the MRA box)
1510 /// @param[in] el2 needs a description
1511 /// \todo Provide a description for el2
1512 Tensor<double> print_plane_local(const int xaxis, const int yaxis, const coordT& el2);
1513
1514 /// Functor for the print_plane method
1515 /// @param[in] filename the filename for the output
1516 /// @param[in] plotinfo plotting parameters
1517 /// @param[in] xaxis the x-axis in the plot (can be any axis of the MRA box)
1518 /// @param[in] yaxis the y-axis in the plot (can be any axis of the MRA box)
1519 void do_print_plane(const std::string filename, std::vector<Tensor<double> > plotinfo,
1520 const int xaxis, const int yaxis, const coordT el2);
1521
1522 /// print the grid (the roots of the quadrature of each leaf box)
1523 /// of this function in user xyz coordinates
1524 /// @param[in] filename the filename for the output
1525 void print_grid(const std::string filename) const;
1526
1527 /// return the keys of the local leaf boxes
1528 std::vector<keyT> local_leaf_keys() const;
1529
1530 /// print the grid in xyz format
1531
1532 /// the quadrature points and the key information will be written to file,
1533 /// @param[in] filename where the quadrature points will be written to
1534 /// @param[in] keys all leaf keys
1535 void do_print_grid(const std::string filename, const std::vector<keyT>& keys) const;
1536
1537 /// read data from a grid
1538
1539 /// @param[in] keyfile file with keys and grid points for each key
1540 /// @param[in] gridfile file with grid points, w/o key, but with same ordering
1541 /// @param[in] vnuc_functor subtract the values of this functor if regularization is needed
1542 template<size_t FDIM>
1543 typename std::enable_if<NDIM==FDIM>::type
1544 read_grid(const std::string keyfile, const std::string gridfile,
1545 std::shared_ptr< FunctionFunctorInterface<double,NDIM> > vnuc_functor) {
1546
1547 std::ifstream kfile(keyfile.c_str());
1548 std::ifstream gfile(gridfile.c_str());
1549 std::string line;
1550
1551 long ndata,ndata1;
1552 if (not (std::getline(kfile,line))) MADNESS_EXCEPTION("failed reading 1st line of key data",0);
1553 if (not (std::istringstream(line) >> ndata)) MADNESS_EXCEPTION("failed reading k",0);
1554 if (not (std::getline(gfile,line))) MADNESS_EXCEPTION("failed reading 1st line of grid data",0);
1555 if (not (std::istringstream(line) >> ndata1)) MADNESS_EXCEPTION("failed reading k",0);
1556 MADNESS_CHECK(ndata==ndata1);
1557 if (not (std::getline(kfile,line))) MADNESS_EXCEPTION("failed reading 2nd line of key data",0);
1558 if (not (std::getline(gfile,line))) MADNESS_EXCEPTION("failed reading 2nd line of grid data",0);
1559
1560 // the quadrature points in simulation coordinates of the root node
1561 const Tensor<double> qx=cdata.quad_x;
1562 const size_t npt = qx.dim(0);
1563
1564 // the number of coordinates (grid point tuples) per box ({x1},{x2},{x3},..,{xNDIM})
1565 long npoints=power<NDIM>(npt);
1566 // the number of boxes
1567 long nboxes=ndata/npoints;
1568 MADNESS_ASSERT(nboxes*npoints==ndata);
1569 print("reading ",nboxes,"boxes from file",gridfile,keyfile);
1570
1571 // these will be the data
1572 Tensor<T> values(cdata.vk,false);
1573
1574 int ii=0;
1575 std::string gline,kline;
1576 // while (1) {
1577 while (std::getline(kfile,kline)) {
1578
1579 double x,y,z,x1,y1,z1,val;
1580
1581 // get the key
1582 long nn;
1583 Translation l1,l2,l3;
1584 // line looks like: # key: n l1 l2 l3
1585 kline.erase(0,7);
1586 std::stringstream(kline) >> nn >> l1 >> l2 >> l3;
1587 // kfile >> s >> nn >> l1 >> l2 >> l3;
1588 const Vector<Translation,3> ll{ l1,l2,l3 };
1589 Key<3> key(nn,ll);
1590
1591 // this is borrowed from fcube
1592 const Vector<Translation,3>& l = key.translation();
1593 const Level n = key.level();
1594 const double h = std::pow(0.5,double(n));
1595 coordT c; // will hold the point in user coordinates
1598
1599
1600 if (NDIM == 3) {
1601 for (size_t i=0; i<npt; ++i) {
1602 c[0] = cell(0,0) + h*cell_width[0]*(l[0] + qx(i)); // x
1603 for (size_t j=0; j<npt; ++j) {
1604 c[1] = cell(1,0) + h*cell_width[1]*(l[1] + qx(j)); // y
1605 for (size_t k=0; k<npt; ++k) {
1606 c[2] = cell(2,0) + h*cell_width[2]*(l[2] + qx(k)); // z
1607 // fprintf(pFile,"%18.12f %18.12f %18.12f\n",c[0],c[1],c[2]);
1608 auto& success1 = std::getline(gfile,gline); MADNESS_CHECK(success1);
1609 auto& success2 = std::getline(kfile,kline); MADNESS_CHECK(success2);
1610 std::istringstream(gline) >> x >> y >> z >> val;
1611 std::istringstream(kline) >> x1 >> y1 >> z1;
1612 MADNESS_CHECK(std::fabs(x-c[0])<1.e-4);
1613 MADNESS_CHECK(std::fabs(x1-c[0])<1.e-4);
1614 MADNESS_CHECK(std::fabs(y-c[1])<1.e-4);
1615 MADNESS_CHECK(std::fabs(y1-c[1])<1.e-4);
1616 MADNESS_CHECK(std::fabs(z-c[2])<1.e-4);
1617 MADNESS_CHECK(std::fabs(z1-c[2])<1.e-4);
1618
1619 // regularize if a functor is given
1620 if (vnuc_functor) val-=(*vnuc_functor)(c);
1621 values(i,j,k)=val;
1622 }
1623 }
1624 }
1625 } else {
1626 MADNESS_EXCEPTION("only NDIM=3 in print_grid",0);
1627 }
1628
1629 // insert the new leaf node
1630 const bool has_children=false;
1631 coeffT coeff=coeffT(this->values2coeffs(key,values),targs);
1632 nodeT node(coeff,has_children);
1633 coeffs.replace(key,node);
1635 ii++;
1636 }
1637
1638 kfile.close();
1639 gfile.close();
1640 MADNESS_CHECK(ii==nboxes);
1641
1642 }
1643
1644
1645 /// read data from a grid
1646
1647 /// @param[in] gridfile file with keys and grid points and values for each key
1648 /// @param[in] vnuc_functor subtract the values of this functor if regularization is needed
1649 template<size_t FDIM>
1650 typename std::enable_if<NDIM==FDIM>::type
1651 read_grid2(const std::string gridfile,
1652 std::shared_ptr< FunctionFunctorInterface<double,NDIM> > vnuc_functor) {
1653
1654 std::ifstream gfile(gridfile.c_str());
1655 std::string line;
1656
1657 long ndata;
1658 if (not (std::getline(gfile,line))) MADNESS_EXCEPTION("failed reading 1st line of grid data",0);
1659 if (not (std::istringstream(line) >> ndata)) MADNESS_EXCEPTION("failed reading k",0);
1660 if (not (std::getline(gfile,line))) MADNESS_EXCEPTION("failed reading 2nd line of grid data",0);
1661
1662 // the quadrature points in simulation coordinates of the root node
1663 const Tensor<double> qx=cdata.quad_x;
1664 const size_t npt = qx.dim(0);
1665
1666 // the number of coordinates (grid point tuples) per box ({x1},{x2},{x3},..,{xNDIM})
1667 long npoints=power<NDIM>(npt);
1668 // the number of boxes
1669 long nboxes=ndata/npoints;
1670 MADNESS_CHECK(nboxes*npoints==ndata);
1671 print("reading ",nboxes,"boxes from file",gridfile);
1672
1673 // these will be the data
1674 Tensor<T> values(cdata.vk,false);
1675
1676 int ii=0;
1677 std::string gline;
1678 // while (1) {
1679 while (std::getline(gfile,gline)) {
1680
1681 double x1,y1,z1,val;
1682
1683 // get the key
1684 long nn;
1685 Translation l1,l2,l3;
1686 // line looks like: # key: n l1 l2 l3
1687 gline.erase(0,7);
1688 std::stringstream(gline) >> nn >> l1 >> l2 >> l3;
1689 const Vector<Translation,3> ll{ l1,l2,l3 };
1690 Key<3> key(nn,ll);
1691
1692 // this is borrowed from fcube
1693 const Vector<Translation,3>& l = key.translation();
1694 const Level n = key.level();
1695 const double h = std::pow(0.5,double(n));
1696 coordT c; // will hold the point in user coordinates
1699
1700
1701 if (NDIM == 3) {
1702 for (int i=0; i<npt; ++i) {
1703 c[0] = cell(0,0) + h*cell_width[0]*(l[0] + qx(i)); // x
1704 for (int j=0; j<npt; ++j) {
1705 c[1] = cell(1,0) + h*cell_width[1]*(l[1] + qx(j)); // y
1706 for (int k=0; k<npt; ++k) {
1707 c[2] = cell(2,0) + h*cell_width[2]*(l[2] + qx(k)); // z
1708
1709 auto& success = std::getline(gfile,gline);
1710 MADNESS_CHECK(success);
1711 std::istringstream(gline) >> x1 >> y1 >> z1 >> val;
1712 MADNESS_CHECK(std::fabs(x1-c[0])<1.e-4);
1713 MADNESS_CHECK(std::fabs(y1-c[1])<1.e-4);
1714 MADNESS_CHECK(std::fabs(z1-c[2])<1.e-4);
1715
1716 // regularize if a functor is given
1717 if (vnuc_functor) val-=(*vnuc_functor)(c);
1718 values(i,j,k)=val;
1719 }
1720 }
1721 }
1722 } else {
1723 MADNESS_EXCEPTION("only NDIM=3 in print_grid",0);
1724 }
1725
1726 // insert the new leaf node
1727 const bool has_children=false;
1728 coeffT coeff=coeffT(this->values2coeffs(key,values),targs);
1729 nodeT node(coeff,has_children);
1730 coeffs.replace(key,node);
1731 const_cast<dcT&>(coeffs).send(key.parent(),
1733 coeffs, key.parent());
1734 ii++;
1735 }
1736
1737 gfile.close();
1738 MADNESS_CHECK(ii==nboxes);
1739
1740 }
1741
1742
1743 /// Compute by projection the scaling function coeffs in specified box
1744 /// @param[in] key the key to the current function node (box)
1745 tensorT project(const keyT& key) const;
1746
1747 /// Returns the truncation threshold according to truncate_method
1748
1749 /// here is our handwaving argument:
1750 /// this threshold will give each FunctionNode an error of less than tol. The
1751 /// total error can then be as high as sqrt(#nodes) * tol. Therefore in order
1752 /// to account for higher dimensions: divide tol by about the root of number
1753 /// of siblings (2^NDIM) that have a large error when we refine along a deep
1754 /// branch of the tree.
1755 double truncate_tol(double tol, const keyT& key) const;
1756
1757 int get_truncate_mode() const { return truncate_mode; };
1758 void set_truncate_mode(int mode) { truncate_mode = mode; };
1759
1760
1761 /// Returns patch referring to coeffs of child in parent box
1762 /// @param[in] child the key to the child function node (box)
1763 std::vector<Slice> child_patch(const keyT& child) const;
1764
1765 /// Projection with optional refinement w/ special points
1766 /// @param[in] key the key to the current function node (box)
1767 /// @param[in] do_refine should we continue refinement?
1768 /// @param[in] specialpts vector of special points in the function where we need
1769 /// to refine at a much finer level
1770 void project_refine_op(const keyT& key, bool do_refine,
1771 const std::vector<Vector<double,NDIM> >& specialpts);
1772
1773 /// Compute the Legendre scaling functions for multiplication
1774
1775 /// Evaluate parent polyn at quadrature points of a child. The prefactor of
1776 /// 2^n/2 is included. The tensor must be preallocated as phi(k,npt).
1777 /// Refer to the implementation notes for more info.
1778 /// @todo Robert please verify this comment. I don't understand this method.
1779 /// @param[in] np level of the parent function node (box)
1780 /// @param[in] nc level of the child function node (box)
1781 /// @param[in] lp translation of the parent function node (box)
1782 /// @param[in] lc translation of the child function node (box)
1783 /// @param[out] phi tensor of the legendre scaling functions
1784 void phi_for_mul(Level np, Translation lp, Level nc, Translation lc, Tensor<double>& phi) const;
1785
1786 /// Directly project parent coeffs to child coeffs
1787
1788 /// Currently used by diff, but other uses can be anticipated
1789
1790 /// @todo is this documentation correct?
1791 /// @param[in] child the key whose coeffs we are requesting
1792 /// @param[in] parent the (leaf) key of our function
1793 /// @param[in] s the (leaf) coeffs belonging to parent
1794 /// @return coeffs
1795 const coeffT parent_to_child(const coeffT& s, const keyT& parent, const keyT& child) const;
1796
1797 /// Directly project parent NS coeffs to child NS coeffs
1798
1799 /// return the NS coefficients if parent and child are the same,
1800 /// or construct sum coeffs from the parents and "add" zero wavelet coeffs
1801 /// @param[in] child the key whose coeffs we are requesting
1802 /// @param[in] parent the (leaf) key of our function
1803 /// @param[in] coeff the (leaf) coeffs belonging to parent
1804 /// @return coeffs in NS form
1805 coeffT parent_to_child_NS(const keyT& child, const keyT& parent,
1806 const coeffT& coeff) const;
1807
1808 /// Return the values when given the coeffs in scaling function basis
1809 /// @param[in] key the key of the function node (box)
1810 /// @param[in] coeff the tensor of scaling function coefficients for function node (box)
1811 /// @return function values for function node (box)
1812 template <typename Q>
1813 GenTensor<Q> coeffs2values(const keyT& key, const GenTensor<Q>& coeff) const {
1814 // PROFILE_MEMBER_FUNC(FunctionImpl); // Too fine grain for routine profiling
1815 double scale = pow(2.0,0.5*NDIM*key.level())/sqrt(FunctionDefaults<NDIM>::get_cell_volume());
1816 return transform(coeff,cdata.quad_phit).scale(scale);
1817 }
1818
1819 /// convert S or NS coeffs to values on a 2k grid of the children
1820
1821 /// equivalent to unfiltering the NS coeffs and then converting all child S-coeffs
1822 /// to values in their respective boxes. If only S coeffs are provided d coeffs are
1823 /// assumed to be zero. Reverse operation to values2NScoeffs().
1824 /// @param[in] key the key of the current S or NS coeffs, level n
1825 /// @param[in] coeff coeffs in S or NS form; if S then d coeffs are assumed zero
1826 /// @param[in] s_only sanity check to avoid unintended discard of d coeffs
1827 /// @return function values on the quadrature points of the children of child (!)
1828 template <typename Q>
1830 const bool s_only) const {
1831 // PROFILE_MEMBER_FUNC(FunctionImpl); // Too fine grain for routine profiling
1832
1833 // sanity checks
1834 MADNESS_ASSERT((coeff.dim(0)==this->get_k()) == s_only);
1835 MADNESS_ASSERT((coeff.dim(0)==this->get_k()) or (coeff.dim(0)==2*this->get_k()));
1836
1837 // this is a block-diagonal matrix with the quadrature points on the diagonal
1838 Tensor<double> quad_phit_2k(2*cdata.k,2*cdata.npt);
1839 quad_phit_2k(cdata.s[0],cdata.s[0])=cdata.quad_phit;
1840 quad_phit_2k(cdata.s[1],cdata.s[1])=cdata.quad_phit;
1841
1842 // the transformation matrix unfilters (cdata.hg) and transforms to values in one step
1843 const Tensor<double> transf = (s_only)
1844 ? inner(cdata.hg(Slice(0,k-1),_),quad_phit_2k) // S coeffs
1845 : inner(cdata.hg,quad_phit_2k); // NS coeffs
1846
1847 // increment the level since the coeffs2values part happens on level n+1
1848 const double scale = pow(2.0,0.5*NDIM*(key.level()+1))/
1850
1851 return transform(coeff,transf).scale(scale);
1852 }
1853
1854 /// Compute the function values for multiplication
1855
1856 /// Given S or NS coefficients from a parent cell, compute the value of
1857 /// the functions at the quadrature points of a child
1858 /// currently restricted to special cases
1859 /// @param[in] child key of the box in which we compute values
1860 /// @param[in] parent key of the parent box holding the coeffs
1861 /// @param[in] coeff coeffs of the parent box
1862 /// @param[in] s_only sanity check to avoid unintended discard of d coeffs
1863 /// @return function values on the quadrature points of the children of child (!)
1864 template <typename Q>
1865 GenTensor<Q> NS_fcube_for_mul(const keyT& child, const keyT& parent,
1866 const GenTensor<Q>& coeff, const bool s_only) const {
1867 // PROFILE_MEMBER_FUNC(FunctionImpl); // Too fine grain for routine profiling
1868
1869 // sanity checks
1870 MADNESS_ASSERT((coeff.dim(0)==this->get_k()) == s_only);
1871 MADNESS_ASSERT((coeff.dim(0)==this->get_k()) or (coeff.dim(0)==2*this->get_k()));
1872
1873 // fast return if possible
1874 // if (child.level()==parent.level()) return NScoeffs2values(child,coeff,s_only);
1875
1876 if (s_only) {
1877
1878 Tensor<double> quad_phi[NDIM];
1879 // tmp tensor
1880 Tensor<double> phi1(cdata.k,cdata.npt);
1881
1882 for (std::size_t d=0; d<NDIM; ++d) {
1883
1884 // input is S coeffs (dimension k), output is values on 2*npt grid points
1885 quad_phi[d]=Tensor<double>(cdata.k,2*cdata.npt);
1886
1887 // for both children of "child" evaluate the Legendre polynomials
1888 // first the left child on level n+1 and translations 2l
1889 phi_for_mul(parent.level(),parent.translation()[d],
1890 child.level()+1, 2*child.translation()[d], phi1);
1891 quad_phi[d](_,Slice(0,k-1))=phi1;
1892
1893 // next the right child on level n+1 and translations 2l+1
1894 phi_for_mul(parent.level(),parent.translation()[d],
1895 child.level()+1, 2*child.translation()[d]+1, phi1);
1896 quad_phi[d](_,Slice(k,2*k-1))=phi1;
1897 }
1898
1899 const double scale = 1.0/sqrt(FunctionDefaults<NDIM>::get_cell_volume());
1900 return general_transform(coeff,quad_phi).scale(scale);
1901 }
1902 MADNESS_EXCEPTION("you should not be here in NS_fcube_for_mul",1);
1903 return GenTensor<Q>();
1904 }
1905
1906 /// convert function values of the a child generation directly to NS coeffs
1907
1908 /// equivalent to converting the function values to 2^NDIM S coeffs and then
1909 /// filtering them to NS coeffs. Reverse operation to NScoeffs2values().
1910 /// @param[in] key key of the parent of the generation
1911 /// @param[in] values tensor holding function values of the 2^NDIM children of key
1912 /// @return NS coeffs belonging to key
1913 template <typename Q>
1914 GenTensor<Q> values2NScoeffs(const keyT& key, const GenTensor<Q>& values) const {
1915 //PROFILE_MEMBER_FUNC(FunctionImpl); // Too fine grain for routine profiling
1916
1917 // sanity checks
1918 MADNESS_ASSERT(values.dim(0)==2*this->get_k());
1919
1920 // this is a block-diagonal matrix with the quadrature points on the diagonal
1921 Tensor<double> quad_phit_2k(2*cdata.npt,2*cdata.k);
1922 quad_phit_2k(cdata.s[0],cdata.s[0])=cdata.quad_phiw;
1923 quad_phit_2k(cdata.s[1],cdata.s[1])=cdata.quad_phiw;
1924
1925 // the transformation matrix unfilters (cdata.hg) and transforms to values in one step
1926 const Tensor<double> transf=inner(quad_phit_2k,cdata.hgT);
1927
1928 // increment the level since the values2coeffs part happens on level n+1
1929 const double scale = pow(0.5,0.5*NDIM*(key.level()+1))
1931
1932 return transform(values,transf).scale(scale);
1933 }
1934
1935 /// Return the scaling function coeffs when given the function values at the quadrature points
1936 /// @param[in] key the key of the function node (box)
1937 /// @return function values for function node (box)
1938 template <typename Q>
1939 Tensor<Q> coeffs2values(const keyT& key, const Tensor<Q>& coeff) const {
1940 // PROFILE_MEMBER_FUNC(FunctionImpl); // Too fine grain for routine profiling
1941 double scale = pow(2.0,0.5*NDIM*key.level())/sqrt(FunctionDefaults<NDIM>::get_cell_volume());
1942 return transform(coeff,cdata.quad_phit).scale(scale);
1943 }
1944
1945 template <typename Q>
1946 GenTensor<Q> values2coeffs(const keyT& key, const GenTensor<Q>& values) const {
1947 // PROFILE_MEMBER_FUNC(FunctionImpl); // Too fine grain for routine profiling
1948 double scale = pow(0.5,0.5*NDIM*key.level())*sqrt(FunctionDefaults<NDIM>::get_cell_volume());
1949 return transform(values,cdata.quad_phiw).scale(scale);
1950 }
1951
1952 template <typename Q>
1953 Tensor<Q> values2coeffs(const keyT& key, const Tensor<Q>& values) const {
1954 // PROFILE_MEMBER_FUNC(FunctionImpl); // Too fine grain for routine profiling
1955 double scale = pow(0.5,0.5*NDIM*key.level())*sqrt(FunctionDefaults<NDIM>::get_cell_volume());
1956 return transform(values,cdata.quad_phiw).scale(scale);
1957 }
1958
1959 /// Compute the function values for multiplication
1960
1961 /// Given coefficients from a parent cell, compute the value of
1962 /// the functions at the quadrature points of a child
1963 /// @param[in] child the key for the child function node (box)
1964 /// @param[in] parent the key for the parent function node (box)
1965 /// @param[in] coeff the coefficients of scaling function basis of the parent box
1966 template <typename Q>
1967 Tensor<Q> fcube_for_mul(const keyT& child, const keyT& parent, const Tensor<Q>& coeff) const {
1968 // PROFILE_MEMBER_FUNC(FunctionImpl); // Too fine grain for routine profiling
1969 if (child.level() == parent.level()) {
1970 return coeffs2values(parent, coeff);
1971 }
1972 else if (child.level() < parent.level()) {
1973 MADNESS_EXCEPTION("FunctionImpl: fcube_for_mul: child-parent relationship bad?",0);
1974 }
1975 else {
1976 Tensor<double> phi[NDIM];
1977 for (std::size_t d=0; d<NDIM; ++d) {
1978 phi[d] = Tensor<double>(cdata.k,cdata.npt);
1979 phi_for_mul(parent.level(),parent.translation()[d],
1980 child.level(), child.translation()[d], phi[d]);
1981 }
1982 return general_transform(coeff,phi).scale(1.0/sqrt(FunctionDefaults<NDIM>::get_cell_volume()));;
1983 }
1984 }
1985
1986
1987 /// Compute the function values for multiplication
1988
1989 /// Given coefficients from a parent cell, compute the value of
1990 /// the functions at the quadrature points of a child
1991 /// @param[in] child the key for the child function node (box)
1992 /// @param[in] parent the key for the parent function node (box)
1993 /// @param[in] coeff the coefficients of scaling function basis of the parent box
1994 template <typename Q>
1995 GenTensor<Q> fcube_for_mul(const keyT& child, const keyT& parent, const GenTensor<Q>& coeff) const {
1996 // PROFILE_MEMBER_FUNC(FunctionImpl); // Too fine grain for routine profiling
1997 if (child.level() == parent.level()) {
1998 return coeffs2values(parent, coeff);
1999 }
2000 else if (child.level() < parent.level()) {
2001 MADNESS_EXCEPTION("FunctionImpl: fcube_for_mul: child-parent relationship bad?",0);
2002 }
2003 else {
2004 Tensor<double> phi[NDIM];
2005 for (size_t d=0; d<NDIM; d++) {
2006 phi[d] = Tensor<double>(cdata.k,cdata.npt);
2007 phi_for_mul(parent.level(),parent.translation()[d],
2008 child.level(), child.translation()[d], phi[d]);
2009 }
2010 return general_transform(coeff,phi).scale(1.0/sqrt(FunctionDefaults<NDIM>::get_cell_volume()));
2011 }
2012 }
2013
2014
2015 /// Functor for the mul method
2016 template <typename L, typename R>
2017 void do_mul(const keyT& key, const Tensor<L>& left, const std::pair< keyT, Tensor<R> >& arg) {
2018 // PROFILE_MEMBER_FUNC(FunctionImpl); // Too fine grain for routine profiling
2019 const keyT& rkey = arg.first;
2020 const Tensor<R>& rcoeff = arg.second;
2021 //madness::print("do_mul: r", rkey, rcoeff.size());
2022 Tensor<R> rcube = fcube_for_mul(key, rkey, rcoeff);
2023 //madness::print("do_mul: l", key, left.size());
2024 Tensor<L> lcube = fcube_for_mul(key, key, left);
2025
2026 Tensor<T> tcube(cdata.vk,false);
2027 TERNARY_OPTIMIZED_ITERATOR(T, tcube, L, lcube, R, rcube, *_p0 = *_p1 * *_p2;);
2028 double scale = pow(0.5,0.5*NDIM*key.level())*sqrt(FunctionDefaults<NDIM>::get_cell_volume());
2029 tcube = transform(tcube,cdata.quad_phiw).scale(scale);
2030 coeffs.replace(key, nodeT(coeffT(tcube,targs),false));
2031 }
2032
2033
2034 /// multiply the values of two coefficient tensors using a custom number of grid points
2035
2036 /// note both coefficient tensors have to refer to the same key!
2037 /// @param[in] c1 a tensor holding coefficients
2038 /// @param[in] c2 another tensor holding coeffs
2039 /// @param[in] npt number of grid points (optional, default is cdata.npt)
2040 /// @return coefficient tensor holding the product of the values of c1 and c2
2041 template<typename R>
2043 const int npt, const keyT& key) const {
2044 typedef TENSOR_RESULT_TYPE(T,R) resultT;
2045
2047
2048 // construct a tensor with the npt coeffs
2049 Tensor<T> c11(cdata2.vk), c22(cdata2.vk);
2050 c11(this->cdata.s0)=c1;
2051 c22(this->cdata.s0)=c2;
2052
2053 // it's sufficient to scale once
2054 double scale = pow(2.0,0.5*NDIM*key.level())/sqrt(FunctionDefaults<NDIM>::get_cell_volume());
2055 Tensor<T> c1value=transform(c11,cdata2.quad_phit).scale(scale);
2056 Tensor<R> c2value=transform(c22,cdata2.quad_phit);
2057 Tensor<resultT> resultvalue(cdata2.vk,false);
2058 TERNARY_OPTIMIZED_ITERATOR(resultT, resultvalue, T, c1value, R, c2value, *_p0 = *_p1 * *_p2;);
2059
2060 Tensor<resultT> result=transform(resultvalue,cdata2.quad_phiw);
2061
2062 // return a copy of the slice to have the tensor contiguous
2063 return copy(result(this->cdata.s0));
2064 }
2065
2066
2067 /// Functor for the binary_op method
2068 template <typename L, typename R, typename opT>
2069 void do_binary_op(const keyT& key, const Tensor<L>& left,
2070 const std::pair< keyT, Tensor<R> >& arg,
2071 const opT& op) {
2072 //PROFILE_MEMBER_FUNC(FunctionImpl); // Too fine grain for routine profiling
2073 const keyT& rkey = arg.first;
2074 const Tensor<R>& rcoeff = arg.second;
2075 Tensor<R> rcube = fcube_for_mul(key, rkey, rcoeff);
2076 Tensor<L> lcube = fcube_for_mul(key, key, left);
2077
2078 Tensor<T> tcube(cdata.vk,false);
2079 op(key, tcube, lcube, rcube);
2080 double scale = pow(0.5,0.5*NDIM*key.level())*sqrt(FunctionDefaults<NDIM>::get_cell_volume());
2081 tcube = transform(tcube,cdata.quad_phiw).scale(scale);
2082 coeffs.replace(key, nodeT(coeffT(tcube,targs),false));
2083 }
2084
2085 /// Invoked by result to perform result += alpha*left+beta*right in wavelet basis
2086
2087 /// Does not assume that any of result, left, right have the same distribution.
2088 /// For most purposes result will start as an empty so actually are implementing
2089 /// out of place gaxpy. If all functions have the same distribution there is
2090 /// no communication except for the optional fence.
2091 template <typename L, typename R>
2092 void gaxpy(T alpha, const FunctionImpl<L,NDIM>& left,
2093 T beta, const FunctionImpl<R,NDIM>& right, bool fence) {
2094 // Loop over local nodes in both functions. Add in left and subtract right.
2095 // Not that efficient in terms of memory bandwidth but ensures we do
2096 // not miss any nodes.
2097 typename FunctionImpl<L,NDIM>::dcT::const_iterator left_end = left.coeffs.end();
2099 it!=left_end;
2100 ++it) {
2101 const keyT& key = it->first;
2102 const typename FunctionImpl<L,NDIM>::nodeT& other_node = it->second;
2103 coeffs.send(key, &nodeT:: template gaxpy_inplace<T,L>, 1.0, other_node, alpha);
2104 }
2105 typename FunctionImpl<R,NDIM>::dcT::const_iterator right_end = right.coeffs.end();
2107 it!=right_end;
2108 ++it) {
2109 const keyT& key = it->first;
2110 const typename FunctionImpl<L,NDIM>::nodeT& other_node = it->second;
2111 coeffs.send(key, &nodeT:: template gaxpy_inplace<T,R>, 1.0, other_node, beta);
2112 }
2113 if (fence)
2114 world.gop.fence();
2115 }
2116
2117 /// Unary operation applied inplace to the coefficients WITHOUT refinement, optional fence
2118 /// @param[in] op the unary operator for the coefficients
2119 template <typename opT>
2120 void unary_op_coeff_inplace(const opT& op, bool fence) {
2121 typename dcT::iterator end = coeffs.end();
2122 for (typename dcT::iterator it=coeffs.begin(); it!=end; ++it) {
2123 const keyT& parent = it->first;
2124 nodeT& node = it->second;
2125 if (node.has_coeff()) {
2126 // op(parent, node.coeff());
2127 TensorArgs full(-1.0,TT_FULL);
2128 change_tensor_type(node.coeff(),full);
2129 op(parent, node.coeff().full_tensor());
2131 // op(parent,node);
2132 }
2133 }
2134 if (fence)
2135 world.gop.fence();
2136 }
2137
2138 /// Unary operation applied inplace to the coefficients WITHOUT refinement, optional fence
2139 /// @param[in] op the unary operator for the coefficients
2140 template <typename opT>
2141 void unary_op_node_inplace(const opT& op, bool fence) {
2142 typename dcT::iterator end = coeffs.end();
2143 for (typename dcT::iterator it=coeffs.begin(); it!=end; ++it) {
2144 const keyT& parent = it->first;
2145 nodeT& node = it->second;
2146 op(parent, node);
2147 }
2148 if (fence)
2149 world.gop.fence();
2150 }
2151
2152 /// Integrate over one particle of a two particle function and get a one particle function
2153 /// bsp \int g(1,2) \delta(2-1) d2 = f(1)
2154 /// The overall dimension of g should be even
2155
2156 /// The operator
2157 template<std::size_t LDIM>
2158 void dirac_convolution_op(const keyT &key, const nodeT &node, FunctionImpl<T,LDIM>* f) const {
2159 // fast return if the node has children (not a leaf node)
2160 if(node.has_children()) return;
2161
2162 const implT* g=this;
2163
2164 // break the 6D key into two 3D keys (may also work for every even dimension)
2165 Key<LDIM> key1, key2;
2166 key.break_apart(key1,key2);
2167
2168 // get the coefficients of the 6D function g
2169 const coeffT& g_coeff = node.coeff();
2170
2171 // get the values of the 6D function g
2172 coeffT g_values = g->coeffs2values(key,g_coeff);
2173
2174 // Determine rank and k
2175 const long rank=g_values.rank();
2176 const long maxk=f->get_k();
2177 MADNESS_ASSERT(maxk==g_coeff.dim(0));
2178
2179 // get tensors for particle 1 and 2 (U and V in SVD)
2180 tensorT vec1=copy(g_values.get_svdtensor().ref_vector(0).reshape(rank,maxk,maxk,maxk));
2181 tensorT vec2=g_values.get_svdtensor().ref_vector(1).reshape(rank,maxk,maxk,maxk);
2182 tensorT result(maxk,maxk,maxk); // should give zero tensor
2183 // Multiply the values of each U and V vector
2184 for (long i=0; i<rank; ++i) {
2185 tensorT c1=vec1(Slice(i,i),_,_,_); // shallow copy (!)
2186 tensorT c2=vec2(Slice(i,i),_,_,_);
2187 c1.emul(c2); // this changes vec1 because of shallow copy, but not the g function because of the deep copy made above
2188 double singular_value_i = g_values.get_svdtensor().weights(i);
2189 result += (singular_value_i*c1);
2190 }
2191
2192 // accumulate coefficients (since only diagonal boxes are used the coefficients get just replaced, but accumulate is needed to create the right tree structure
2193 tensorT f_coeff = f->values2coeffs(key1,result);
2194 f->coeffs.task(key1, &FunctionNode<T,LDIM>::accumulate2, f_coeff, f->coeffs, key1, TaskAttributes::hipri());
2195// coeffs.task(dest, &nodeT::accumulate2, result, coeffs, dest, TaskAttributes::hipri());
2196
2197
2198 return;
2199 }
2200
2201
2202 template<std::size_t LDIM>
2204 typename dcT::const_iterator end = this->coeffs.end();
2205 for (typename dcT::const_iterator it=this->coeffs.begin(); it!=end; ++it) {
2206 // looping through all the leaf(!) coefficients in the NDIM function ("this")
2207 const keyT& key = it->first;
2208 const FunctionNode<T,NDIM>& node = it->second;
2209 if (node.is_leaf()) {
2210 // only process the diagonal boxes
2211 Key<LDIM> key1, key2;
2212 key.break_apart(key1,key2);
2213 if(key1 == key2){
2214 ProcessID p = coeffs.owner(key);
2215 woT::task(p, &implT:: template dirac_convolution_op<LDIM>, key, node, f);
2216 }
2217 }
2218 }
2219 world.gop.fence(); // fence is necessary if trickle down is used afterwards
2220 // trickle down and undo redundand shouldnt change anything if only the diagonal elements are considered above -> check this
2221 f->trickle_down(true); // fence must be true otherwise undo_redundant will have trouble
2222// f->undo_redundant(true);
2223 f->verify_tree();
2224 //if (fence) world.gop.fence(); // unnecessary, fence is activated in undo_redundant
2225
2226 }
2227
2228
2229 /// Unary operation applied inplace to the coefficients WITHOUT refinement, optional fence
2230 /// @param[in] op the unary operator for the coefficients
2231 template <typename opT>
2232 void flo_unary_op_node_inplace(const opT& op, bool fence) {
2234// typedef do_unary_op_value_inplace<opT> xopT;
2236 if (fence) world.gop.fence();
2237 }
2238
2239 /// Unary operation applied inplace to the coefficients WITHOUT refinement, optional fence
2240 /// @param[in] op the unary operator for the coefficients
2241 template <typename opT>
2242 void flo_unary_op_node_inplace(const opT& op, bool fence) const {
2244// typedef do_unary_op_value_inplace<opT> xopT;
2246 if (fence)
2247 world.gop.fence();
2248 }
2249
2250 /// truncate tree at a certain level
2251 /// @param[in] max_level truncate tree below this level
2252 void erase(const Level& max_level);
2253
2254 /// Returns some asymmetry measure ... no comms
2255 double check_symmetry_local() const;
2256
2257 /// given an NS tree resulting from a convolution, truncate leafs if appropriate
2260 const implT* f; // for calling its member functions
2261
2263
2264 bool operator()(typename rangeT::iterator& it) const {
2265
2266 const keyT& key = it->first;
2267 nodeT& node = it->second;
2268
2269 if (node.is_leaf() and node.coeff().has_data()) {
2270 coeffT d = copy(node.coeff());
2271 d(f->cdata.s0)=0.0;
2272 const double error=d.normf();
2273 const double tol=f->truncate_tol(f->get_thresh(),key);
2274 if (error<tol) node.coeff()=copy(node.coeff()(f->cdata.s0));
2275 }
2276 return true;
2277 }
2278 template <typename Archive> void serialize(const Archive& ar) {}
2279
2280 };
2281
2282 /// remove all coefficients of internal nodes
2285
2286 /// constructor need impl for cdata
2288
2289 bool operator()(typename rangeT::iterator& it) const {
2290
2291 nodeT& node = it->second;
2292 if (node.has_children()) node.clear_coeff();
2293 return true;
2294 }
2295 template <typename Archive> void serialize(const Archive& ar) {}
2296
2297 };
2298
2299 /// remove all coefficients of leaf nodes
2302
2303 /// constructor need impl for cdata
2305
2306 bool operator()(typename rangeT::iterator& it) const {
2307 nodeT& node = it->second;
2308 if (not node.has_children()) node.clear_coeff();
2309 return true;
2310 }
2311 template <typename Archive> void serialize(const Archive& ar) {}
2312
2313 };
2314
2315
2316 /// keep only the sum coefficients in each node
2320
2321 /// constructor need impl for cdata
2323
2324 bool operator()(typename rangeT::iterator& it) const {
2325
2326 nodeT& node = it->second;
2327 coeffT s=copy(node.coeff()(impl->cdata.s0));
2328 node.coeff()=s;
2329 return true;
2330 }
2331 template <typename Archive> void serialize(const Archive& ar) {}
2332
2333 };
2334
2335
2336 /// reduce the rank of the nodes, optional fence
2339
2340 // threshold for rank reduction / SVD truncation
2342
2343 // constructor takes target precision
2344 do_reduce_rank() = default;
2346 do_reduce_rank(const double& thresh) {
2348 }
2349
2350 //
2351 bool operator()(typename rangeT::iterator& it) const {
2352
2353 nodeT& node = it->second;
2354 node.reduceRank(args.thresh);
2355 return true;
2356 }
2357 template <typename Archive> void serialize(const Archive& ar) {}
2358 };
2359
2360
2361
2362 /// check symmetry wrt particle exchange
2365 const implT* f;
2368
2369 /// return the norm of the difference of this node and its "mirror" node
2370 double operator()(typename rangeT::iterator& it) const {
2371
2372 // Temporary fix to GCC whining about out of range access for NDIM!=6
2373 if constexpr(NDIM==6) {
2374 const keyT& key = it->first;
2375 const nodeT& fnode = it->second;
2376
2377 // skip internal nodes
2378 if (fnode.has_children()) return 0.0;
2379
2380 if (f->world.size()>1) return 0.0;
2381
2382 // exchange particles
2383 std::vector<long> map(NDIM);
2384 map[0]=3; map[1]=4; map[2]=5;
2385 map[3]=0; map[4]=1; map[5]=2;
2386
2387 // make mapped key
2389 for (std::size_t i=0; i<NDIM; ++i) l[map[i]] = key.translation()[i];
2390 const keyT mapkey(key.level(),l);
2391
2392 double norm=0.0;
2393
2394
2395 // hope it's local
2396 if (f->get_coeffs().probe(mapkey)) {
2397 MADNESS_ASSERT(f->get_coeffs().probe(mapkey));
2398 const nodeT& mapnode=f->get_coeffs().find(mapkey).get()->second;
2399
2400// bool have_c1=fnode.coeff().has_data() and fnode.coeff().config().has_data();
2401// bool have_c2=mapnode.coeff().has_data() and mapnode.coeff().config().has_data();
2402 bool have_c1=fnode.coeff().has_data();
2403 bool have_c2=mapnode.coeff().has_data();
2404
2405 if (have_c1 and have_c2) {
2406 tensorT c1=fnode.coeff().full_tensor_copy();
2407 tensorT c2=mapnode.coeff().full_tensor_copy();
2408 c2 = copy(c2.mapdim(map));
2409 norm=(c1-c2).normf();
2410 } else if (have_c1) {
2411 tensorT c1=fnode.coeff().full_tensor_copy();
2412 norm=c1.normf();
2413 } else if (have_c2) {
2414 tensorT c2=mapnode.coeff().full_tensor_copy();
2415 norm=c2.normf();
2416 } else {
2417 norm=0.0;
2418 }
2419 } else {
2420 norm=fnode.coeff().normf();
2421 }
2422 return norm*norm;
2423 }
2424 else {
2425 MADNESS_EXCEPTION("ONLY FOR DIM 6!", 1);
2426 }
2427 }
2428
2429 double operator()(double a, double b) const {
2430 return (a+b);
2431 }
2432
2433 template <typename Archive> void serialize(const Archive& ar) {
2434 MADNESS_EXCEPTION("no serialization of do_check_symmetry yet",1);
2435 }
2436
2437
2438 };
2439
2440 /// merge the coefficent boxes of this into result's tree
2441
2442 /// result+= alpha*this
2443 /// this and result don't have to have the same distribution or live in the same world
2444 /// no comm, and the tree should be in an consistent state by virtue
2445 template<typename Q, typename R>
2449 T alpha=T(1.0);
2453
2454 /// return the norm of the difference of this node and its "mirror" node
2455 bool operator()(typename rangeT::iterator& it) const {
2456
2457 const keyT& key = it->first;
2458 const nodeT& node = it->second;
2459 if (node.has_coeff()) result->get_coeffs().task(key, &nodeT::accumulate,
2460 alpha*node.coeff(), result->get_coeffs(), key, result->targs);
2461 return true;
2462 }
2463
2464 template <typename Archive> void serialize(const Archive& ar) {
2465 MADNESS_EXCEPTION("no serialization of do_accumulate_trees",1);
2466 }
2467 };
2468
2469
2470 /// merge the coefficient boxes of this into other's tree
2471
2472 /// no comm, and the tree should be in an consistent state by virtue
2473 /// of FunctionNode::gaxpy_inplace
2474 template<typename Q, typename R>
2483
2484 /// return the norm of the difference of this node and its "mirror" node
2485 bool operator()(typename rangeT::iterator& it) const {
2486
2487 const keyT& key = it->first;
2488 const nodeT& fnode = it->second;
2489
2490 // if other's node exists: add this' coeffs to it
2491 // otherwise insert this' node into other's tree
2492 typename dcT::accessor acc;
2493 if (other->get_coeffs().find(acc,key)) {
2494 nodeT& gnode=acc->second;
2495 gnode.gaxpy_inplace(beta,fnode,alpha);
2496 } else {
2497 nodeT gnode=fnode;
2498 gnode.scale(alpha);
2499 other->get_coeffs().replace(key,gnode);
2500 }
2501 return true;
2502 }
2503
2504 template <typename Archive> void serialize(const Archive& ar) {
2505 MADNESS_EXCEPTION("no serialization of do_merge_trees",1);
2506 }
2507 };
2508
2509
2510 /// map this on f
2511 struct do_mapdim {
2513
2514 std::vector<long> map;
2516
2517 do_mapdim() : f(0) {};
2518 do_mapdim(const std::vector<long> map, implT& f) : map(map), f(&f) {}
2519
2520 bool operator()(typename rangeT::iterator& it) const {
2521
2522 const keyT& key = it->first;
2523 const nodeT& node = it->second;
2524
2526 for (std::size_t i=0; i<NDIM; ++i) l[map[i]] = key.translation()[i];
2527 tensorT c = node.coeff().reconstruct_tensor();
2528 if (c.size()) c = copy(c.mapdim(map));
2530 f->get_coeffs().replace(keyT(key.level(),l), nodeT(cc,node.has_children()));
2531
2532 return true;
2533 }
2534 template <typename Archive> void serialize(const Archive& ar) {
2535 MADNESS_EXCEPTION("no serialization of do_mapdim",1);
2536 }
2537
2538 };
2539
2540 /// mirror dimensions of this, write result on f
2541 struct do_mirror {
2543
2544 std::vector<long> mirror;
2546
2547 do_mirror() : f(0) {};
2548 do_mirror(const std::vector<long> mirror, implT& f) : mirror(mirror), f(&f) {}
2549
2550 bool operator()(typename rangeT::iterator& it) const {
2551
2552 const keyT& key = it->first;
2553 const nodeT& node = it->second;
2554
2555 // mirror translation index: l_new + l_old = l_max
2557 Translation lmax = (Translation(1)<<key.level()) - 1;
2558 for (std::size_t i=0; i<NDIM; ++i) {
2559 if (mirror[i]==-1) l[i]= lmax - key.translation()[i];
2560 }
2561
2562 // mirror coefficients: multiply all odd-k slices with -1
2563 tensorT c = node.coeff().full_tensor_copy();
2564 if (c.size()) {
2565 std::vector<Slice> s(___);
2566
2567 // loop over dimensions and over k
2568 for (size_t i=0; i<NDIM; ++i) {
2569 std::size_t kmax=c.dim(i);
2570 if (mirror[i]==-1) {
2571 for (size_t k=1; k<kmax; k+=2) {
2572 s[i]=Slice(k,k,1);
2573 c(s)*=(-1.0);
2574 }
2575 s[i]=_;
2576 }
2577 }
2578 }
2580 f->get_coeffs().replace(keyT(key.level(),l), nodeT(cc,node.has_children()));
2581
2582 return true;
2583 }
2584 template <typename Archive> void serialize(const Archive& ar) {
2585 MADNESS_EXCEPTION("no serialization of do_mirror",1);
2586 }
2587
2588 };
2589
2590 /// mirror dimensions of this, write result on f
2593
2594 std::vector<long> map,mirror;
2596
2598 do_map_and_mirror(const std::vector<long> map, const std::vector<long> mirror, implT& f)
2599 : map(map), mirror(mirror), f(&f) {}
2600
2601 bool operator()(typename rangeT::iterator& it) const {
2602
2603 const keyT& key = it->first;
2604 const nodeT& node = it->second;
2605
2606 tensorT c = node.coeff().full_tensor_copy();
2608
2609 // do the mapping first (if present)
2610 if (map.size()>0) {
2612 for (std::size_t i=0; i<NDIM; ++i) l1[map[i]] = l[i];
2613 std::swap(l,l1);
2614 if (c.size()) c = copy(c.mapdim(map));
2615 }
2616
2617 if (mirror.size()>0) {
2618 // mirror translation index: l_new + l_old = l_max
2620 Translation lmax = (Translation(1)<<key.level()) - 1;
2621 for (std::size_t i=0; i<NDIM; ++i) {
2622 if (mirror[i]==-1) l1[i]= lmax - l[i];
2623 }
2624 std::swap(l,l1);
2625
2626 // mirror coefficients: multiply all odd-k slices with -1
2627 if (c.size()) {
2628 std::vector<Slice> s(___);
2629
2630 // loop over dimensions and over k
2631 for (size_t i=0; i<NDIM; ++i) {
2632 std::size_t kmax=c.dim(i);
2633 if (mirror[i]==-1) {
2634 for (size_t k=1; k<kmax; k+=2) {
2635 s[i]=Slice(k,k,1);
2636 c(s)*=(-1.0);
2637 }
2638 s[i]=_;
2639 }
2640 }
2641 }
2642 }
2643
2645 f->get_coeffs().replace(keyT(key.level(),l), nodeT(cc,node.has_children()));
2646 return true;
2647 }
2648 template <typename Archive> void serialize(const Archive& ar) {
2649 MADNESS_EXCEPTION("no serialization of do_mirror",1);
2650 }
2651
2652 };
2653
2654
2655
2656 /// "put" this on g
2657 struct do_average {
2659
2661
2662 do_average() : g(0) {}
2664
2665 /// iterator it points to this
2666 bool operator()(typename rangeT::iterator& it) const {
2667
2668 const keyT& key = it->first;
2669 const nodeT& fnode = it->second;
2670
2671 // fast return if rhs has no coeff here
2672 if (fnode.has_coeff()) {
2673
2674 // check if there is a node already existing
2675 typename dcT::accessor acc;
2676 if (g->get_coeffs().find(acc,key)) {
2677 nodeT& gnode=acc->second;
2678 if (gnode.has_coeff()) gnode.coeff()+=fnode.coeff();
2679 } else {
2680 g->get_coeffs().replace(key,fnode);
2681 }
2682 }
2683
2684 return true;
2685 }
2686 template <typename Archive> void serialize(const Archive& ar) {}
2687 };
2688
2689 /// change representation of nodes' coeffs to low rank, optional fence
2692
2693 // threshold for rank reduction / SVD truncation
2696
2697 // constructor takes target precision
2699 // do_change_tensor_type(const TensorArgs& targs) : targs(targs) {}
2701
2702 //
2703 bool operator()(typename rangeT::iterator& it) const {
2704
2705 double cpu0=cpu_time();
2706 nodeT& node = it->second;
2708 double cpu1=cpu_time();
2710
2711 return true;
2712
2713 }
2714 template <typename Archive> void serialize(const Archive& ar) {}
2715 };
2716
2719
2720 // threshold for rank reduction / SVD truncation
2722
2723 // constructor takes target precision
2726 bool operator()(typename rangeT::iterator& it) const {
2727 it->second.consolidate_buffer(targs);
2728 return true;
2729 }
2730 template <typename Archive> void serialize(const Archive& ar) {}
2731 };
2732
2733
2734
2735 template <typename opT>
2739 opT op;
2741 bool operator()(typename rangeT::iterator& it) const {
2742 const keyT& key = it->first;
2743 nodeT& node = it->second;
2744 if (node.has_coeff()) {
2745 const TensorArgs full_args(-1.0,TT_FULL);
2746 change_tensor_type(node.coeff(),full_args);
2747 tensorT& t= node.coeff().full_tensor();
2748 //double before = t.normf();
2749 tensorT values = impl->fcube_for_mul(key, key, t);
2750 op(key, values);
2751 double scale = pow(0.5,0.5*NDIM*key.level())*sqrt(FunctionDefaults<NDIM>::get_cell_volume());
2752 t = transform(values,impl->cdata.quad_phiw).scale(scale);
2753 node.coeff()=coeffT(t,impl->get_tensor_args());
2754 //double after = t.normf();
2755 //madness::print("XOP:", key, before, after);
2756 }
2757 return true;
2758 }
2759 template <typename Archive> void serialize(const Archive& ar) {}
2760 };
2761
2762 template <typename Q, typename R>
2763 /// @todo I don't know what this does other than a trasform
2764 void vtransform_doit(const std::shared_ptr< FunctionImpl<R,NDIM> >& right,
2765 const Tensor<Q>& c,
2766 const std::vector< std::shared_ptr< FunctionImpl<T,NDIM> > >& vleft,
2767 double tol) {
2768 // To reduce crunch on vectors being transformed each task
2769 // does them in a random order
2770 std::vector<unsigned int> ind(vleft.size());
2771 for (unsigned int i=0; i<vleft.size(); ++i) {
2772 ind[i] = i;
2773 }
2774 for (unsigned int i=0; i<vleft.size(); ++i) {
2775 unsigned int j = RandomValue<int>()%vleft.size();
2776 std::swap(ind[i],ind[j]);
2777 }
2778
2779 typename FunctionImpl<R,NDIM>::dcT::const_iterator end = right->coeffs.end();
2780 for (typename FunctionImpl<R,NDIM>::dcT::const_iterator it=right->coeffs.begin(); it != end; ++it) {
2781 if (it->second.has_coeff()) {
2782 const Key<NDIM>& key = it->first;
2783 const GenTensor<R>& r = it->second.coeff();
2784 double norm = r.normf();
2785 double keytol = truncate_tol(tol,key);
2786
2787 for (unsigned int j=0; j<vleft.size(); ++j) {
2788 unsigned int i = ind[j]; // Random permutation
2789 if (std::abs(norm*c(i)) > keytol) {
2790 implT* left = vleft[i].get();
2791 typename dcT::accessor acc;
2792 bool newnode = left->coeffs.insert(acc,key);
2793 if (newnode && key.level()>0) {
2794 Key<NDIM> parent = key.parent();
2795 if (left->coeffs.is_local(parent))
2796 left->coeffs.send(parent, &nodeT::set_has_children_recursive, left->coeffs, parent);
2797 else
2798 left->coeffs.task(parent, &nodeT::set_has_children_recursive, left->coeffs, parent);
2799
2800 }
2801 nodeT& node = acc->second;
2802 if (!node.has_coeff())
2803 node.set_coeff(coeffT(cdata.v2k,targs));
2804 coeffT& t = node.coeff();
2805 t.gaxpy(1.0, r, c(i));
2806 }
2807 }
2808 }
2809 }
2810 }
2811
2812 /// Refine multiple functions down to the same finest level
2813
2814 /// @param v the vector of functions we are refining.
2815 /// @param key the current node.
2816 /// @param c the vector of coefficients passed from above.
2817 void refine_to_common_level(const std::vector<FunctionImpl<T,NDIM>*>& v,
2818 const std::vector<tensorT>& c,
2819 const keyT key);
2820
2821 /// Inplace operate on many functions (impl's) with an operator within a certain box
2822 /// @param[in] key the key of the current function node (box)
2823 /// @param[in] op the operator
2824 /// @param[in] v the vector of function impl's on which to be operated
2825 template <typename opT>
2826 void multiop_values_doit(const keyT& key, const opT& op, const std::vector<implT*>& v) {
2827 std::vector<tensorT> c(v.size());
2828 for (unsigned int i=0; i<v.size(); i++) {
2829 if (v[i]) {
2830 coeffT cc = coeffs2values(key, v[i]->coeffs.find(key).get()->second.coeff());
2831 c[i]=cc.full_tensor();
2832 }
2833 }
2834 tensorT r = op(key, c);
2835 coeffs.replace(key, nodeT(coeffT(values2coeffs(key, r),targs),false));
2836 }
2837
2838 /// Inplace operate on many functions (impl's) with an operator within a certain box
2839 /// Assumes all functions have been refined down to the same level
2840 /// @param[in] op the operator
2841 /// @param[in] v the vector of function impl's on which to be operated
2842 template <typename opT>
2843 void multiop_values(const opT& op, const std::vector<implT*>& v) {
2844 // rough check on refinement level (ignore non-initialized functions
2845 for (std::size_t i=1; i<v.size(); ++i) {
2846 if (v[i] and v[i-1]) {
2847 MADNESS_ASSERT(v[i]->coeffs.size()==v[i-1]->coeffs.size());
2848 }
2849 }
2850 typename dcT::iterator end = v[0]->coeffs.end();
2851 for (typename dcT::iterator it=v[0]->coeffs.begin(); it!=end; ++it) {
2852 const keyT& key = it->first;
2853 if (it->second.has_coeff())
2854 world.taskq.add(*this, &implT:: template multiop_values_doit<opT>, key, op, v);
2855 else
2856 coeffs.replace(key, nodeT(coeffT(),true));
2857 }
2858 world.gop.fence();
2859 }
2860
2861 /// Inplace operate on many functions (impl's) with an operator within a certain box
2862
2863 /// @param[in] key the key of the current function node (box)
2864 /// @param[in] op the operator
2865 /// @param[in] vin the vector of function impl's on which to be operated
2866 /// @param[out] vout the resulting vector of function impl's
2867 template <typename opT>
2868 void multi_to_multi_op_values_doit(const keyT& key, const opT& op,
2869 const std::vector<implT*>& vin, std::vector<implT*>& vout) {
2870 std::vector<tensorT> c(vin.size());
2871 for (unsigned int i=0; i<vin.size(); i++) {
2872 if (vin[i]) {
2873 coeffT cc = coeffs2values(key, vin[i]->coeffs.find(key).get()->second.coeff());
2874 c[i]=cc.full_tensor();
2875 }
2876 }
2877 std::vector<tensorT> r = op(key, c);
2878 MADNESS_ASSERT(r.size()==vout.size());
2879 for (std::size_t i=0; i<vout.size(); ++i) {
2880 vout[i]->coeffs.replace(key, nodeT(coeffT(values2coeffs(key, r[i]),targs),false));
2881 }
2882 }
2883
2884 /// Inplace operate on many functions (impl's) with an operator within a certain box
2885
2886 /// Assumes all functions have been refined down to the same level
2887 /// @param[in] op the operator
2888 /// @param[in] vin the vector of function impl's on which to be operated
2889 /// @param[out] vout the resulting vector of function impl's
2890 template <typename opT>
2891 void multi_to_multi_op_values(const opT& op, const std::vector<implT*>& vin,
2892 std::vector<implT*>& vout, const bool fence=true) {
2893 // rough check on refinement level (ignore non-initialized functions
2894 for (std::size_t i=1; i<vin.size(); ++i) {
2895 if (vin[i] and vin[i-1]) {
2896 MADNESS_ASSERT(vin[i]->coeffs.size()==vin[i-1]->coeffs.size());
2897 }
2898 }
2899 typename dcT::iterator end = vin[0]->coeffs.end();
2900 for (typename dcT::iterator it=vin[0]->coeffs.begin(); it!=end; ++it) {
2901 const keyT& key = it->first;
2902 if (it->second.has_coeff())
2903 world.taskq.add(*this, &implT:: template multi_to_multi_op_values_doit<opT>,
2904 key, op, vin, vout);
2905 else {
2906 // fill result functions with empty box in this key
2907 for (implT* it2 : vout) {
2908 it2->coeffs.replace(key, nodeT(coeffT(),true));
2909 }
2910 }
2911 }
2912 if (fence) world.gop.fence();
2913 }
2914
2915 /// Transforms a vector of functions left[i] = sum[j] right[j]*c[j,i] using sparsity
2916 /// @param[in] vright vector of functions (impl's) on which to be transformed
2917 /// @param[in] c the tensor (matrix) transformer
2918 /// @param[in] vleft vector of of the *newly* transformed functions (impl's)
2919 template <typename Q, typename R>
2920 void vtransform(const std::vector< std::shared_ptr< FunctionImpl<R,NDIM> > >& vright,
2921 const Tensor<Q>& c,
2922 const std::vector< std::shared_ptr< FunctionImpl<T,NDIM> > >& vleft,
2923 double tol,
2924 bool fence) {
2925 for (unsigned int j=0; j<vright.size(); ++j) {
2926 world.taskq.add(*this, &implT:: template vtransform_doit<Q,R>, vright[j], copy(c(j,_)), vleft, tol);
2927 }
2928 if (fence)
2929 world.gop.fence();
2930 }
2931
2932 /// Unary operation applied inplace to the values with optional refinement and fence
2933 /// @param[in] op the unary operator for the values
2934 template <typename opT>
2935 void unary_op_value_inplace(const opT& op, bool fence) {
2937 typedef do_unary_op_value_inplace<opT> xopT;
2938 world.taskq.for_each<rangeT,xopT>(rangeT(coeffs.begin(), coeffs.end()), xopT(this,op));
2939 if (fence)
2940 world.gop.fence();
2941 }
2942
2943 // Multiplication assuming same distribution and recursive descent
2944 /// Both left and right functions are in the scaling function basis
2945 /// @param[in] key the key to the current function node (box)
2946 /// @param[in] left the function impl associated with the left function
2947 /// @param[in] lcin the scaling function coefficients associated with the
2948 /// current box in the left function
2949 /// @param[in] vrightin the vector of function impl's associated with
2950 /// the vector of right functions
2951 /// @param[in] vrcin the vector scaling function coefficients associated with the
2952 /// current box in the right functions
2953 /// @param[out] vresultin the vector of resulting functions (impl's)
2954 template <typename L, typename R>
2955 void mulXXveca(const keyT& key,
2956 const FunctionImpl<L,NDIM>* left, const Tensor<L>& lcin,
2957 const std::vector<const FunctionImpl<R,NDIM>*> vrightin,
2958 const std::vector< Tensor<R> >& vrcin,
2959 const std::vector<FunctionImpl<T,NDIM>*> vresultin,
2960 double tol) {
2961 typedef typename FunctionImpl<L,NDIM>::dcT::const_iterator literT;
2962 typedef typename FunctionImpl<R,NDIM>::dcT::const_iterator riterT;
2963
2964 double lnorm = 1e99;
2965 Tensor<L> lc = lcin;
2966 if (lc.size() == 0) {
2967 literT it = left->coeffs.find(key).get();
2968 MADNESS_ASSERT(it != left->coeffs.end());
2969 lnorm = it->second.get_norm_tree();
2970 if (it->second.has_coeff())
2971 lc = it->second.coeff().full_tensor_copy();
2972 }
2973
2974 // Loop thru RHS functions seeing if anything can be multiplied
2975 std::vector<FunctionImpl<T,NDIM>*> vresult;
2976 std::vector<const FunctionImpl<R,NDIM>*> vright;
2977 std::vector< Tensor<R> > vrc;
2978 vresult.reserve(vrightin.size());
2979 vright.reserve(vrightin.size());
2980 vrc.reserve(vrightin.size());
2981
2982 for (unsigned int i=0; i<vrightin.size(); ++i) {
2983 FunctionImpl<T,NDIM>* result = vresultin[i];
2984 const FunctionImpl<R,NDIM>* right = vrightin[i];
2985 Tensor<R> rc = vrcin[i];
2986 double rnorm;
2987 if (rc.size() == 0) {
2988 riterT it = right->coeffs.find(key).get();
2989 MADNESS_ASSERT(it != right->coeffs.end());
2990 rnorm = it->second.get_norm_tree();
2991 if (it->second.has_coeff())
2992 rc = it->second.coeff().full_tensor_copy();
2993 }
2994 else {
2995 rnorm = rc.normf();
2996 }
2997
2998 if (rc.size() && lc.size()) { // Yipee!
2999 result->task(world.rank(), &implT:: template do_mul<L,R>, key, lc, std::make_pair(key,rc));
3000 }
3001 else if (tol && lnorm*rnorm < truncate_tol(tol, key)) {
3002 result->coeffs.replace(key, nodeT(coeffT(cdata.vk,targs),false)); // Zero leaf
3003 }
3004 else { // Interior node
3005 result->coeffs.replace(key, nodeT(coeffT(),true));
3006 vresult.push_back(result);
3007 vright.push_back(right);
3008 vrc.push_back(rc);
3009 }
3010 }
3011
3012 if (vresult.size()) {
3013 Tensor<L> lss;
3014 if (lc.size()) {
3015 Tensor<L> ld(cdata.v2k);
3016 ld(cdata.s0) = lc(___);
3017 lss = left->unfilter(ld);
3018 }
3019
3020 std::vector< Tensor<R> > vrss(vresult.size());
3021 for (unsigned int i=0; i<vresult.size(); ++i) {
3022 if (vrc[i].size()) {
3023 Tensor<R> rd(cdata.v2k);
3024 rd(cdata.s0) = vrc[i](___);
3025 vrss[i] = vright[i]->unfilter(rd);
3026 }
3027 }
3028
3029 for (KeyChildIterator<NDIM> kit(key); kit; ++kit) {
3030 const keyT& child = kit.key();
3031 Tensor<L> ll;
3032
3033 std::vector<Slice> cp = child_patch(child);
3034
3035 if (lc.size())
3036 ll = copy(lss(cp));
3037
3038 std::vector< Tensor<R> > vv(vresult.size());
3039 for (unsigned int i=0; i<vresult.size(); ++i) {
3040 if (vrc[i].size())
3041 vv[i] = copy(vrss[i](cp));
3042 }
3043
3044 woT::task(coeffs.owner(child), &implT:: template mulXXveca<L,R>, child, left, ll, vright, vv, vresult, tol);
3045 }
3046 }
3047 }
3048
3049 /// Multiplication using recursive descent and assuming same distribution
3050 /// Both left and right functions are in the scaling function basis
3051 /// @param[in] key the key to the current function node (box)
3052 /// @param[in] left the function impl associated with the left function
3053 /// @param[in] lcin the scaling function coefficients associated with the
3054 /// current box in the left function
3055 /// @param[in] right the function impl associated with the right function
3056 /// @param[in] rcin the scaling function coefficients associated with the
3057 /// current box in the right function
3058 template <typename L, typename R>
3059 void mulXXa(const keyT& key,
3060 const FunctionImpl<L,NDIM>* left, const Tensor<L>& lcin,
3061 const FunctionImpl<R,NDIM>* right,const Tensor<R>& rcin,
3062 double tol) {
3063 typedef typename FunctionImpl<L,NDIM>::dcT::const_iterator literT;
3064 typedef typename FunctionImpl<R,NDIM>::dcT::const_iterator riterT;
3065
3066 double lnorm=1e99, rnorm=1e99;
3067
3068 Tensor<L> lc = lcin;
3069 if (lc.size() == 0) {
3070 literT it = left->coeffs.find(key).get();
3071 MADNESS_ASSERT(it != left->coeffs.end());
3072 lnorm = it->second.get_norm_tree();
3073 if (it->second.has_coeff())
3074 lc = it->second.coeff().reconstruct_tensor();
3075 }
3076
3077 Tensor<R> rc = rcin;
3078 if (rc.size() == 0) {
3079 riterT it = right->coeffs.find(key).get();
3080 MADNESS_ASSERT(it != right->coeffs.end());
3081 rnorm = it->second.get_norm_tree();
3082 if (it->second.has_coeff())
3083 rc = it->second.coeff().reconstruct_tensor();
3084 }
3085
3086 // both nodes are leaf nodes: multiply and return
3087 if (rc.size() && lc.size()) { // Yipee!
3088 do_mul<L,R>(key, lc, std::make_pair(key,rc));
3089 return;
3090 }
3091
3092 if (tol) {
3093 if (lc.size())
3094 lnorm = lc.normf(); // Otherwise got from norm tree above
3095 if (rc.size())
3096 rnorm = rc.normf();
3097 if (lnorm*rnorm < truncate_tol(tol, key)) {
3098 coeffs.replace(key, nodeT(coeffT(cdata.vk,targs),false)); // Zero leaf node
3099 return;
3100 }
3101 }
3102
3103 // Recur down
3104 coeffs.replace(key, nodeT(coeffT(),true)); // Interior node
3105
3106 Tensor<L> lss;
3107 if (lc.size()) {
3108 Tensor<L> ld(cdata.v2k);
3109 ld(cdata.s0) = lc(___);
3110 lss = left->unfilter(ld);
3111 }
3112
3113 Tensor<R> rss;
3114 if (rc.size()) {
3115 Tensor<R> rd(cdata.v2k);
3116 rd(cdata.s0) = rc(___);
3117 rss = right->unfilter(rd);
3118 }
3119
3120 for (KeyChildIterator<NDIM> kit(key); kit; ++kit) {
3121 const keyT& child = kit.key();
3122 Tensor<L> ll;
3123 Tensor<R> rr;
3124 if (lc.size())
3125 ll = copy(lss(child_patch(child)));
3126 if (rc.size())
3127 rr = copy(rss(child_patch(child)));
3128
3129 woT::task(coeffs.owner(child), &implT:: template mulXXa<L,R>, child, left, ll, right, rr, tol);
3130 }
3131 }
3132
3133
3134 // Binary operation on values using recursive descent and assuming same distribution
3135 /// Both left and right functions are in the scaling function basis
3136 /// @param[in] key the key to the current function node (box)
3137 /// @param[in] left the function impl associated with the left function
3138 /// @param[in] lcin the scaling function coefficients associated with the
3139 /// current box in the left function
3140 /// @param[in] right the function impl associated with the right function
3141 /// @param[in] rcin the scaling function coefficients associated with the
3142 /// current box in the right function
3143 /// @param[in] op the binary operator
3144 template <typename L, typename R, typename opT>
3145 void binaryXXa(const keyT& key,
3146 const FunctionImpl<L,NDIM>* left, const Tensor<L>& lcin,
3147 const FunctionImpl<R,NDIM>* right,const Tensor<R>& rcin,
3148 const opT& op) {
3149 typedef typename FunctionImpl<L,NDIM>::dcT::const_iterator literT;
3150 typedef typename FunctionImpl<R,NDIM>::dcT::const_iterator riterT;
3151
3152 Tensor<L> lc = lcin;
3153 if (lc.size() == 0) {
3154 literT it = left->coeffs.find(key).get();
3155 MADNESS_ASSERT(it != left->coeffs.end());
3156 if (it->second.has_coeff())
3157 lc = it->second.coeff().reconstruct_tensor();
3158 }
3159
3160 Tensor<R> rc = rcin;
3161 if (rc.size() == 0) {
3162 riterT it = right->coeffs.find(key).get();
3163 MADNESS_ASSERT(it != right->coeffs.end());
3164 if (it->second.has_coeff())
3165 rc = it->second.coeff().reconstruct_tensor();
3166 }
3167
3168 if (rc.size() && lc.size()) { // Yipee!
3169 do_binary_op<L,R>(key, lc, std::make_pair(key,rc), op);
3170 return;
3171 }
3172
3173 // Recur down
3174 coeffs.replace(key, nodeT(coeffT(),true)); // Interior node
3175
3176 Tensor<L> lss;
3177 if (lc.size()) {
3178 Tensor<L> ld(cdata.v2k);
3179 ld(cdata.s0) = lc(___);
3180 lss = left->unfilter(ld);
3181 }
3182
3183 Tensor<R> rss;
3184 if (rc.size()) {
3185 Tensor<R> rd(cdata.v2k);
3186 rd(cdata.s0) = rc(___);
3187 rss = right->unfilter(rd);
3188 }
3189
3190 for (KeyChildIterator<NDIM> kit(key); kit; ++kit) {
3191 const keyT& child = kit.key();
3192 Tensor<L> ll;
3193 Tensor<R> rr;
3194 if (lc.size())
3195 ll = copy(lss(child_patch(child)));
3196 if (rc.size())
3197 rr = copy(rss(child_patch(child)));
3198
3199 woT::task(coeffs.owner(child), &implT:: template binaryXXa<L,R,opT>, child, left, ll, right, rr, op);
3200 }
3201 }
3202
3203 template <typename Q, typename opT>
3205 typedef typename opT::resultT resultT;
3207 opT op;
3208
3213
3214 Tensor<resultT> operator()(const Key<NDIM>& key, const Tensor<Q>& t) const {
3215 Tensor<Q> invalues = impl_func->coeffs2values(key, t);
3216
3217 Tensor<resultT> outvalues = op(key, invalues);
3218
3219 return impl_func->values2coeffs(key, outvalues);
3220 }
3221
3222 template <typename Archive>
3223 void serialize(Archive& ar) {
3224 ar & impl_func & op;
3225 }
3226 };
3227
3228 /// Out of place unary operation on function impl
3229 /// The skeleton algorithm should resemble something like
3230 ///
3231 /// *this = op(*func)
3232 ///
3233 /// @param[in] key the key of the current function node (box)
3234 /// @param[in] func the function impl on which to be operated
3235 /// @param[in] op the unary operator
3236 template <typename Q, typename opT>
3237 void unaryXXa(const keyT& key,
3238 const FunctionImpl<Q,NDIM>* func, const opT& op) {
3239
3240 // const Tensor<Q>& fc = func->coeffs.find(key).get()->second.full_tensor_copy();
3241 const Tensor<Q> fc = func->coeffs.find(key).get()->second.coeff().reconstruct_tensor();
3242
3243 if (fc.size() == 0) {
3244 // Recur down
3245 coeffs.replace(key, nodeT(coeffT(),true)); // Interior node
3246 for (KeyChildIterator<NDIM> kit(key); kit; ++kit) {
3247 const keyT& child = kit.key();
3248 woT::task(coeffs.owner(child), &implT:: template unaryXXa<Q,opT>, child, func, op);
3249 }
3250 }
3251 else {
3252 tensorT t=op(key,fc);
3253 coeffs.replace(key, nodeT(coeffT(t,targs),false)); // Leaf node
3254 }
3255 }
3256
3257 /// Multiplies two functions (impl's) together. Delegates to the mulXXa() method
3258 /// @param[in] left pointer to the left function impl
3259 /// @param[in] right pointer to the right function impl
3260 /// @param[in] tol numerical tolerance
3261 template <typename L, typename R>
3262 void mulXX(const FunctionImpl<L,NDIM>* left, const FunctionImpl<R,NDIM>* right, double tol, bool fence) {
3263 if (world.rank() == coeffs.owner(cdata.key0))
3264 mulXXa(cdata.key0, left, Tensor<L>(), right, Tensor<R>(), tol);
3265 if (fence)
3266 world.gop.fence();
3267
3268 //verify_tree();
3269 }
3270
3271 /// Performs binary operation on two functions (impl's). Delegates to the binaryXXa() method
3272 /// @param[in] left pointer to the left function impl
3273 /// @param[in] right pointer to the right function impl
3274 /// @param[in] op the binary operator
3275 template <typename L, typename R, typename opT>
3277 const opT& op, bool fence) {
3278 if (world.rank() == coeffs.owner(cdata.key0))
3279 binaryXXa(cdata.key0, left, Tensor<L>(), right, Tensor<R>(), op);
3280 if (fence)
3281 world.gop.fence();
3282
3283 //verify_tree();
3284 }
3285
3286 /// Performs unary operation on function impl. Delegates to the unaryXXa() method
3287 /// @param[in] func function impl of the operand
3288 /// @param[in] op the unary operator
3289 template <typename Q, typename opT>
3290 void unaryXX(const FunctionImpl<Q,NDIM>* func, const opT& op, bool fence) {
3291 if (world.rank() == coeffs.owner(cdata.key0))
3292 unaryXXa(cdata.key0, func, op);
3293 if (fence)
3294 world.gop.fence();
3295
3296 //verify_tree();
3297 }
3298
3299 /// Performs unary operation on function impl. Delegates to the unaryXXa() method
3300 /// @param[in] func function impl of the operand
3301 /// @param[in] op the unary operator
3302 template <typename Q, typename opT>
3303 void unaryXXvalues(const FunctionImpl<Q,NDIM>* func, const opT& op, bool fence) {
3304 if (world.rank() == coeffs.owner(cdata.key0))
3306 if (fence)
3307 world.gop.fence();
3308
3309 //verify_tree();
3310 }
3311
3312 /// Multiplies a function (impl) with a vector of functions (impl's). Delegates to the
3313 /// mulXXveca() method.
3314 /// @param[in] left pointer to the left function impl
3315 /// @param[in] vright vector of pointers to the right function impl's
3316 /// @param[in] tol numerical tolerance
3317 /// @param[out] vresult vector of pointers to the resulting function impl's
3318 template <typename L, typename R>
3320 const std::vector<const FunctionImpl<R,NDIM>*>& vright,
3321 const std::vector<FunctionImpl<T,NDIM>*>& vresult,
3322 double tol,
3323 bool fence) {
3324 std::vector< Tensor<R> > vr(vright.size());
3325 if (world.rank() == coeffs.owner(cdata.key0))
3326 mulXXveca(cdata.key0, left, Tensor<L>(), vright, vr, vresult, tol);
3327 if (fence)
3328 world.gop.fence();
3329 }
3330
3332
3333 mutable long box_leaf[1000];
3334 mutable long box_interior[1000];
3335
3336 // horrifically non-scalable
3337 void put_in_box(ProcessID from, long nl, long ni) const;
3338
3339 /// Prints summary of data distribution
3340 void print_info() const;
3341
3342 /// Verify tree is properly constructed ... global synchronization involved
3343
3344 /// If an inconsistency is detected, prints a message describing the error and
3345 /// then throws a madness exception.
3346 ///
3347 /// This is a reasonably quick and scalable operation that is
3348 /// useful for debugging and paranoia.
3349 void verify_tree() const;
3350
3351 /// check that parents and children are consistent
3352
3353 /// will not check proper size of coefficients
3354 /// global communication
3355 bool verify_parents_and_children() const;
3356
3357 /// check that the tree state and the coeffs are consistent
3358
3359 /// will not check existence of children and/or parents
3360 /// no communication
3361 bool verify_tree_state_local() const;
3362
3363 /// Walk up the tree returning pair(key,node) for first node with coefficients
3364
3365 /// Three possibilities.
3366 ///
3367 /// 1) The coeffs are present and returned with the key of the containing node.
3368 ///
3369 /// 2) The coeffs are further up the tree ... the request is forwarded up.
3370 ///
3371 /// 3) The coeffs are futher down the tree ... an empty tensor is returned.
3372 ///
3373 /// !! This routine is crying out for an optimization to
3374 /// manage the number of messages being sent ... presently
3375 /// each parent is fetched 2^(n*d) times where n is the no. of
3376 /// levels between the level of evaluation and the parent.
3377 /// Alternatively, reimplement multiply as a downward tree
3378 /// walk and just pass the parent down. Slightly less
3379 /// parallelism but much less communication.
3380 /// @todo Robert .... help!
3381 void sock_it_to_me(const keyT& key,
3382 const RemoteReference< FutureImpl< std::pair<keyT,coeffT> > >& ref) const;
3383 /// As above, except
3384 /// 3) The coeffs are constructed from the avg of nodes further down the tree
3385 /// @todo Robert .... help!
3386 void sock_it_to_me_too(const keyT& key,
3387 const RemoteReference< FutureImpl< std::pair<keyT,coeffT> > >& ref) const;
3388
3389 /// @todo help!
3391 const keyT& key,
3392 const coordT& plotlo, const coordT& plothi, const std::vector<long>& npt,
3393 bool eval_refine) const;
3394
3395
3396 /// Evaluate a cube/slice of points ... plotlo and plothi are already in simulation coordinates
3397 /// No communications
3398 /// @param[in] plotlo the coordinate of the starting point
3399 /// @param[in] plothi the coordinate of the ending point
3400 /// @param[in] npt the number of points in each dimension
3401 Tensor<T> eval_plot_cube(const coordT& plotlo,
3402 const coordT& plothi,
3403 const std::vector<long>& npt,
3404 const bool eval_refine = false) const;
3405
3406
3407 /// Evaluate function only if point is local returning (true,value); otherwise return (false,0.0)
3408
3409 /// maxlevel is the maximum depth to search down to --- the max local depth can be
3410 /// computed with max_local_depth();
3411 std::pair<bool,T> eval_local_only(const Vector<double,NDIM>& xin, Level maxlevel) ;
3412
3413 /// Allocation-free core of the batched eval_local_only: writes one
3414 /// (local?,value) pair per point, in input order, into results[0..npt).
3415 /// Consecutive points in the same leaf box share that box's descent and
3416 /// coefficient fetch (last-box memoization); each point is evaluated by
3417 /// the same eval_cube on the same tensor as the single-point path, so
3418 /// results are bit-for-bit identical. No communications.
3419 void eval_local_only(const Vector<double,NDIM>* xin, std::size_t npt,
3420 Level maxlevel, std::pair<bool,T>* results);
3421
3422 /// Batched eval_local_only returning a fresh vector (see the pointer
3423 /// core above for semantics).
3424 /// maxlevel is the maximum depth to search down to --- the max local depth can
3425 /// be computed with max_local_depth();
3426 std::vector<std::pair<bool,T>>
3427 eval_local_only(const std::vector<Vector<double,NDIM>>& xin, Level maxlevel) ;
3428
3429
3430 /// Evaluate the function at a point in \em simulation coordinates
3431
3432 /// Only the invoking process will get the result via the
3433 /// remote reference to a future. Active messages may be sent
3434 /// to other nodes.
3435 void eval(const Vector<double,NDIM>& xin,
3436 const keyT& keyin,
3437 const typename Future<T>::remote_refT& ref);
3438
3439 /// Get the depth of the tree at a point in \em simulation coordinates
3440
3441 /// Only the invoking process will get the result via the
3442 /// remote reference to a future. Active messages may be sent
3443 /// to other nodes.
3444 ///
3445 /// This function is a minimally-modified version of eval()
3446 void evaldepthpt(const Vector<double,NDIM>& xin,
3447 const keyT& keyin,
3448 const typename Future<Level>::remote_refT& ref);
3449
3450 /// Get the rank of leaf box of the tree at a point in \em simulation coordinates
3451
3452 /// Only the invoking process will get the result via the
3453 /// remote reference to a future. Active messages may be sent
3454 /// to other nodes.
3455 ///
3456 /// This function is a minimally-modified version of eval()
3457 void evalR(const Vector<double,NDIM>& xin,
3458 const keyT& keyin,
3459 const typename Future<long>::remote_refT& ref);
3460
3461
3462 /// Computes norm of low/high-order polyn. coeffs for autorefinement test
3463
3464 /// t is a k^d tensor. In order to screen the autorefinement
3465 /// during multiplication compute the norms of
3466 /// ... lo ... the block of t for all polynomials of order < k/2
3467 /// ... hi ... the block of t for all polynomials of order >= k/2
3468 ///
3469 /// k=5 0,1,2,3,4 --> 0,1,2 ... 3,4
3470 /// k=6 0,1,2,3,4,5 --> 0,1,2 ... 3,4,5
3471 ///
3472 /// k=number of wavelets, so k=5 means max order is 4, so max exactly
3473 /// representable squarable polynomial is of order 2.
3474 void static tnorm(const tensorT& t, double* lo, double* hi);
3475
3476 void static tnorm(const GenTensor<T>& t, double* lo, double* hi);
3477
3478 void static tnorm(const SVDTensor<T>& t, double* lo, double* hi, const int particle);
3479
3480 // This invoked if node has not been autorefined
3481 void do_square_inplace(const keyT& key);
3482
3483 // This invoked if node has been autorefined
3484 void do_square_inplace2(const keyT& parent, const keyT& child, const tensorT& parent_coeff);
3485
3486 /// Always returns false (for when autorefine is not wanted)
3487 bool noautorefine(const keyT& key, const tensorT& t) const;
3488
3489 /// Returns true if this block of coeffs needs autorefining
3490 bool autorefine_square_test(const keyT& key, const nodeT& t) const;
3491
3492 /// Pointwise squaring of function with optional global fence
3493
3494 /// If not autorefining, local computation only if not fencing.
3495 /// If autorefining, may result in asynchronous communication.
3496 void square_inplace(bool fence);
3497 void abs_inplace(bool fence);
3498 void abs_square_inplace(bool fence);
3499
3500 /// is this the same as trickle_down() ?
3501 void sum_down_spawn(const keyT& key, const coeffT& s);
3502
3503 /// After 1d push operator must sum coeffs down the tree to restore correct scaling function coefficients
3504 void sum_down(bool fence);
3505
3506 /// perform this multiplication: h(1,2) = f(1,2) * g(1)
3507 template<size_t LDIM>
3509
3510 static bool randomize() {return false;}
3514
3515 implT* h; ///< the result function h(1,2) = f(1,2) * g(1)
3518 int particle; ///< if g is g(1) or g(2)
3519
3520 multiply_op() : h(), f(), g(), particle(1) {}
3521
3522 multiply_op(implT* h1, const ctT& f1, const ctL& g1, const int particle1)
3523 : h(h1), f(f1), g(g1), particle(particle1) {};
3524
3525 /// return true if this will be a leaf node
3526
3527 /// use generalization of tnorm for a GenTensor
3528 bool screen(const coeffT& fcoeff, const coeffT& gcoeff, const keyT& key) const {
3530 MADNESS_ASSERT(fcoeff.is_svd_tensor());
3533
3534 double glo=0.0, ghi=0.0, flo=0.0, fhi=0.0;
3535 g.get_impl()->tnorm(gcoeff.get_tensor(), &glo, &ghi);
3536 g.get_impl()->tnorm(fcoeff.get_svdtensor(),&flo,&fhi,particle);
3537
3538 double total_hi=glo*fhi + ghi*flo + fhi*ghi;
3539 return (total_hi<h->truncate_tol(h->get_thresh(),key));
3540
3541 }
3542
3543 /// apply this on a FunctionNode of f and g of Key key
3544
3545 /// @param[in] key key for FunctionNode in f and g, (g: broken into particles)
3546 /// @return <this node is a leaf, coefficients of this node>
3547 std::pair<bool,coeffT> operator()(const Key<NDIM>& key) const {
3548
3549 // bool is_leaf=(not fdatum.second.has_children());
3550 // if (not is_leaf) return std::pair<bool,coeffT> (is_leaf,coeffT());
3551
3552 // break key into particles (these are the child keys, with f/gdatum come the parent keys)
3553 Key<LDIM> key1,key2;
3554 key.break_apart(key1,key2);
3555 const Key<LDIM> gkey= (particle==1) ? key1 : key2;
3556
3557 // get coefficients of the actual FunctionNode
3558 coeffT coeff1=f.get_impl()->parent_to_child(f.coeff(),f.key(),key);
3559 coeff1.normalize();
3560 const coeffT coeff2=g.get_impl()->parent_to_child(g.coeff(),g.key(),gkey);
3561
3562 // multiplication is done in TT_2D
3563 coeffT coeff1_2D=coeff1.convert(TensorArgs(h->get_thresh(),TT_2D));
3564 coeff1_2D.normalize();
3565
3566 bool is_leaf=screen(coeff1_2D,coeff2,key);
3567 if (key.level()<2) is_leaf=false;
3568
3569 coeffT hcoeff;
3570 if (is_leaf) {
3571
3572 // convert coefficients to values
3573 coeffT hvalues=f.get_impl()->coeffs2values(key,coeff1_2D);
3574 coeffT gvalues=g.get_impl()->coeffs2values(gkey,coeff2);
3575
3576 // perform multiplication
3577 coeffT result_val=h->multiply(hvalues,gvalues,particle-1);
3578
3579 hcoeff=h->values2coeffs(key,result_val);
3580
3581 // conversion on coeffs, not on values, because it implies truncation!
3582 if (not hcoeff.is_of_tensortype(h->get_tensor_type()))
3583 hcoeff=hcoeff.convert(h->get_tensor_args());
3584 }
3585
3586 return std::pair<bool,coeffT> (is_leaf,hcoeff);
3587 }
3588
3589 this_type make_child(const keyT& child) const {
3590
3591 // break key into particles
3592 Key<LDIM> key1, key2;
3593 child.break_apart(key1,key2);
3594 const Key<LDIM> gkey= (particle==1) ? key1 : key2;
3595
3596 return this_type(h,f.make_child(child),g.make_child(gkey),particle);
3597 }
3598
3600 Future<ctT> f1=f.activate();
3602 return h->world.taskq.add(detail::wrap_mem_fn(*const_cast<this_type *> (this),
3603 &this_type::forward_ctor),h,f1,g1,particle);
3604 }
3605
3606 this_type forward_ctor(implT* h1, const ctT& f1, const ctL& g1, const int particle) {
3607 return this_type(h1,f1,g1,particle);
3608 }
3609
3610 template <typename Archive> void serialize(const Archive& ar) {
3611 ar & h & f & g & particle;
3612 }
3613 };
3614
3615
3616 /// add two functions f and g: result=alpha * f + beta * g
3617 struct add_op {
3618
3621
3622 bool randomize() const {return false;}
3623
3624 /// tracking coeffs of first and second addend
3626 /// prefactor for f, g
3627 double alpha, beta;
3628
3629 add_op() = default;
3630 add_op(const ctT& f, const ctT& g, const double alpha, const double beta)
3631 : f(f), g(g), alpha(alpha), beta(beta){}
3632
3633 /// if we are at the bottom of the trees, return the sum of the coeffs
3634 std::pair<bool,coeffT> operator()(const keyT& key) const {
3635
3636 bool is_leaf=(f.is_leaf() and g.is_leaf());
3637 if (not is_leaf) return std::pair<bool,coeffT> (is_leaf,coeffT());
3638
3639 coeffT fcoeff=f.get_impl()->parent_to_child(f.coeff(),f.key(),key);
3640 coeffT gcoeff=g.get_impl()->parent_to_child(g.coeff(),g.key(),key);
3641 coeffT hcoeff=copy(fcoeff);
3642 hcoeff.gaxpy(alpha,gcoeff,beta);
3643 hcoeff.reduce_rank(f.get_impl()->get_tensor_args().thresh);
3644 return std::pair<bool,coeffT> (is_leaf,hcoeff);
3645 }
3646
3647 this_type make_child(const keyT& child) const {
3648 return this_type(f.make_child(child),g.make_child(child),alpha,beta);
3649 }
3650
3651 /// retrieve the coefficients (parent coeffs might be remote)
3653 Future<ctT> f1=f.activate();
3654 Future<ctT> g1=g.activate();
3655 return f.get_impl()->world.taskq.add(detail::wrap_mem_fn(*const_cast<this_type *> (this),
3657 }
3658
3659 /// taskq-compatible ctor
3660 this_type forward_ctor(const ctT& f1, const ctT& g1, const double alpha, const double beta) {
3661 return this_type(f1,g1,alpha,beta);
3662 }
3663
3664 template <typename Archive> void serialize(const Archive& ar) {
3665 ar & f & g & alpha & beta;
3666 }
3667
3668 };
3669
3670 /// multiply f (a pair function of NDIM) with an orbital g (LDIM=NDIM/2)
3671
3672 /// as in (with h(1,2)=*this) : h(1,2) = g(1) * f(1,2)
3673 /// use tnorm as a measure to determine if f (=*this) must be refined
3674 /// @param[in] f the NDIM function f=f(1,2)
3675 /// @param[in] g the LDIM function g(1) (or g(2))
3676 /// @param[in] particle 1 or 2, as in g(1) or g(2)
3677 template<size_t LDIM>
3678 void multiply(const implT* f, const FunctionImpl<T,LDIM>* g, const int particle) {
3679
3682
3683 typedef multiply_op<LDIM> coeff_opT;
3684 coeff_opT coeff_op(this,ff,gg,particle);
3685
3686 typedef insert_op<T,NDIM> apply_opT;
3687 apply_opT apply_op(this);
3688
3689 keyT key0=f->cdata.key0;
3690 if (world.rank() == coeffs.owner(key0)) {
3692 woT::task(p, &implT:: template forward_traverse<coeff_opT,apply_opT>, coeff_op, apply_op, key0);
3693 }
3694
3696 }
3697
3698 /// Hartree product of two LDIM functions to yield a NDIM = 2*LDIM function
3699 template<size_t LDIM, typename leaf_opT>
3700 struct hartree_op {
3701 bool randomize() const {return false;}
3702
3705
3706 implT* result; ///< where to construct the pair function
3707 ctL p1, p2; ///< tracking coeffs of the two lo-dim functions
3708 leaf_opT leaf_op; ///< determine if a given node will be a leaf node
3709
3710 // ctor
3712 hartree_op(implT* result, const ctL& p11, const ctL& p22, const leaf_opT& leaf_op)
3713 : result(result), p1(p11), p2(p22), leaf_op(leaf_op) {
3714 MADNESS_ASSERT(LDIM+LDIM==NDIM);
3715 }
3716
3717 std::pair<bool,coeffT> operator()(const Key<NDIM>& key) const {
3718
3719 // break key into particles (these are the child keys, with datum1/2 come the parent keys)
3720 Key<LDIM> key1,key2;
3721 key.break_apart(key1,key2);
3722
3723 // this returns the appropriate NS coeffs for key1 and key2 resp.
3724 const coeffT fcoeff=p1.coeff(key1);
3725 const coeffT gcoeff=p2.coeff(key2);
3726 bool is_leaf=leaf_op(key,fcoeff.full_tensor(),gcoeff.full_tensor());
3727 if (not is_leaf) return std::pair<bool,coeffT> (is_leaf,coeffT());
3728
3729 // extract the sum coeffs from the NS coeffs
3730 const coeffT s1=fcoeff(p1.get_impl()->cdata.s0);
3731 const coeffT s2=gcoeff(p2.get_impl()->cdata.s0);
3732
3733 // new coeffs are simply the hartree/kronecker/outer product --
3734 coeffT coeff=outer(s1,s2,result->get_tensor_args());
3735 // no post-determination
3736 // is_leaf=leaf_op(key,coeff);
3737 return std::pair<bool,coeffT>(is_leaf,coeff);
3738 }
3739
3740 this_type make_child(const keyT& child) const {
3741
3742 // break key into particles
3743 Key<LDIM> key1, key2;
3744 child.break_apart(key1,key2);
3745
3746 return this_type(result,p1.make_child(key1),p2.make_child(key2),leaf_op);
3747 }
3748
3750 Future<ctL> p11=p1.activate();
3751 Future<ctL> p22=p2.activate();
3752 return result->world.taskq.add(detail::wrap_mem_fn(*const_cast<this_type *> (this),
3753 &this_type::forward_ctor),result,p11,p22,leaf_op);
3754 }
3755
3756 this_type forward_ctor(implT* result1, const ctL& p11, const ctL& p22, const leaf_opT& leaf_op) {
3757 return this_type(result1,p11,p22,leaf_op);
3758 }
3759
3760 template <typename Archive> void serialize(const Archive& ar) {
3761 ar & result & p1 & p2 & leaf_op;
3762 }
3763 };
3764
3765 /// traverse a non-existing tree
3766
3767 /// part II: activate coeff_op, i.e. retrieve all the necessary remote boxes (communication)
3768 /// @param[in] coeff_op operator making the coefficients that needs activation
3769 /// @param[in] apply_op just passing thru
3770 /// @param[in] key the key we are working on
3771 template<typename coeff_opT, typename apply_opT>
3772 void forward_traverse(const coeff_opT& coeff_op, const apply_opT& apply_op, const keyT& key) const {
3774 Future<coeff_opT> active_coeff=coeff_op.activate();
3775 woT::task(world.rank(), &implT:: template traverse_tree<coeff_opT,apply_opT>, active_coeff, apply_op, key);
3776 }
3777
3778
3779 /// traverse a non-existing tree
3780
3781 /// part I: make the coefficients, process them and continue the recursion if necessary
3782 /// @param[in] coeff_op operator making the coefficients and determining them being leaves
3783 /// @param[in] apply_op operator processing the coefficients
3784 /// @param[in] key the key we are currently working on
3785 template<typename coeff_opT, typename apply_opT>
3786 void traverse_tree(const coeff_opT& coeff_op, const apply_opT& apply_op, const keyT& key) const {
3788
3789 typedef typename std::pair<bool,coeffT> argT;
3790 const argT arg=coeff_op(key);
3791 apply_op.operator()(key,arg.second,arg.first);
3792
3793 const bool has_children=(not arg.first);
3794 if (has_children) {
3795 for (KeyChildIterator<NDIM> kit(key); kit; ++kit) {
3796 const keyT& child=kit.key();
3797 coeff_opT child_op=coeff_op.make_child(child);
3798 // spawn activation where child is local
3799 ProcessID p=coeffs.owner(child);
3800
3801 void (implT::*ft)(const coeff_opT&, const apply_opT&, const keyT&) const = &implT::forward_traverse<coeff_opT,apply_opT>;
3802
3803 woT::task(p, ft, child_op, apply_op, child);
3804 }
3805 }
3806 }
3807
3808
3809 /// given two functions of LDIM, perform the Hartree/Kronecker/outer product
3810
3811 /// |Phi(1,2)> = |phi(1)> x |phi(2)>
3812 /// @param[in] p1 FunctionImpl of particle 1
3813 /// @param[in] p2 FunctionImpl of particle 2
3814 /// @param[in] leaf_op operator determining of a given box will be a leaf
3815 template<std::size_t LDIM, typename leaf_opT>
3816 void hartree_product(const std::vector<std::shared_ptr<FunctionImpl<T,LDIM>>> p1,
3817 const std::vector<std::shared_ptr<FunctionImpl<T,LDIM>>> p2,
3818 const leaf_opT& leaf_op, bool fence) {
3819 MADNESS_CHECK_THROW(p1.size()==p2.size(),"hartree_product: p1 and p2 must have the same size");
3820 for (auto& p : p1) MADNESS_CHECK(p->is_nonstandard() or p->is_nonstandard_with_leaves());
3821 for (auto& p : p2) MADNESS_CHECK(p->is_nonstandard() or p->is_nonstandard_with_leaves());
3822
3823 const keyT key0=cdata.key0;
3824
3825 for (std::size_t i=0; i<p1.size(); ++i) {
3826 if (world.rank() == this->get_coeffs().owner(key0)) {
3827
3828 // prepare the CoeffTracker
3829 CoeffTracker<T,LDIM> iap1(p1[i].get());
3830 CoeffTracker<T,LDIM> iap2(p2[i].get());
3831
3832 // the operator making the coefficients
3833 typedef hartree_op<LDIM,leaf_opT> coeff_opT;
3834 coeff_opT coeff_op(this,iap1,iap2,leaf_op);
3835
3836 // this operator simply inserts the coeffs into this' tree
3837// typedef insert_op<T,NDIM> apply_opT;
3838 typedef accumulate_op<T,NDIM> apply_opT;
3839 apply_opT apply_op(this);
3840
3841 woT::task(world.rank(), &implT:: template forward_traverse<coeff_opT,apply_opT>,
3842 coeff_op, apply_op, cdata.key0);
3843
3844 }
3845 }
3846
3848 if (fence) world.gop.fence();
3849 }
3850
3851
3852 template <typename opT, typename R>
3853 void
3855 const opT* op = pop.ptr;
3856 const Level n = key.level();
3857 const double cnorm = c.normf();
3858 const double tol = truncate_tol(thresh, key)*0.1; // ??? why this value????
3859
3861 const Translation lold = lnew[axis];
3862 const Translation maxs = Translation(1)<<n;
3863
3864 int nsmall = 0; // Counts neglected blocks to terminate s loop
3865 for (Translation s=0; s<maxs; ++s) {
3866 int maxdir = s ? 1 : -1;
3867 for (int direction=-1; direction<=maxdir; direction+=2) {
3868 lnew[axis] = lold + direction*s;
3869 if (lnew[axis] >= 0 && lnew[axis] < maxs) { // NON-ZERO BOUNDARY CONDITIONS IGNORED HERE !!!!!!!!!!!!!!!!!!!!
3870 const Tensor<typename opT::opT>& r = op->rnlij(n, s*direction, true);
3871 double Rnorm = r.normf();
3872
3873 if (Rnorm == 0.0) {
3874 return; // Hard zero means finished!
3875 }
3876
3877 if (s <= 1 || r.normf()*cnorm > tol) { // Always do kernel and neighbor
3878 nsmall = 0;
3879 tensorT result = transform_dir(c,r,axis);
3880
3881 if (result.normf() > tol*0.3) {
3882 Key<NDIM> dest(n,lnew);
3883 coeffs.task(dest, &nodeT::accumulate2, result, coeffs, dest, TaskAttributes::hipri());
3884 }
3885 }
3886 else {
3887 ++nsmall;
3888 }
3889 }
3890 else {
3891 ++nsmall;
3892 }
3893 }
3894 if (nsmall >= 4) {
3895 // If have two negligble blocks in
3896 // succession in each direction interpret
3897 // this as the operator being zero beyond
3898 break;
3899 }
3900 }
3901 }
3902
3903 template <typename opT, typename R>
3904 void
3905 apply_1d_realspace_push(const opT& op, const FunctionImpl<R,NDIM>* f, int axis, bool fence) {
3906 MADNESS_ASSERT(!f->is_compressed());
3907
3908 typedef typename FunctionImpl<R,NDIM>::dcT::const_iterator fiterT;
3909 typedef FunctionNode<R,NDIM> fnodeT;
3910 fiterT end = f->coeffs.end();
3911 ProcessID me = world.rank();
3912 for (fiterT it=f->coeffs.begin(); it!=end; ++it) {
3913 const fnodeT& node = it->second;
3914 if (node.has_coeff()) {
3915 const keyT& key = it->first;
3916 const Tensor<R>& c = node.coeff().full_tensor_copy();
3917 woT::task(me, &implT:: template apply_1d_realspace_push_op<opT,R>,
3919 }
3920 }
3921 if (fence) world.gop.fence();
3922 }
3923
3925 const implT* f,
3926 const keyT& key,
3927 const std::pair<keyT,coeffT>& left,
3928 const std::pair<keyT,coeffT>& center,
3929 const std::pair<keyT,coeffT>& right);
3930
3931 void do_diff1(const DerivativeBase<T,NDIM>* D,
3932 const implT* f,
3933 const keyT& key,
3934 const std::pair<keyT,coeffT>& left,
3935 const std::pair<keyT,coeffT>& center,
3936 const std::pair<keyT,coeffT>& right);
3937
3938 // Called by result function to differentiate f
3939 void diff(const DerivativeBase<T,NDIM>* D, const implT* f, bool fence);
3940
3941 /// Returns key of general neighbor enforcing BC
3942
3943 /// Out of volume keys are mapped to enforce the BC as follows.
3944 /// * Periodic BC map back into the volume and return the correct key
3945 /// * non-periodic BC - returns invalid() to indicate out of volume
3946 keyT neighbor(const keyT& key, const keyT& disp, const array_of_bools<NDIM>& is_periodic) const;
3947
3948 /// Returns key of general neighbor that resides in-volume
3949
3950 /// Out of volume keys are mapped to invalid()
3951 keyT neighbor_in_volume(const keyT& key, const keyT& disp) const;
3952
3953 /// find_me. Called by diff_bdry to get coefficients of boundary function
3954 Future< std::pair<keyT,coeffT> > find_me(const keyT& key) const;
3955
3956 /// return the a std::pair<key, node>, which MUST exist
3957 std::pair<Key<NDIM>,ShallowNode<T,NDIM> > find_datum(keyT key) const;
3958
3959 /// multiply the ket with a one-electron potential rr(1,2)= f(1,2)*g(1)
3960
3961 /// @param[in] val_ket function values of f(1,2)
3962 /// @param[in] val_pot function values of g(1)
3963 /// @param[in] particle if 0 then g(1), if 1 then g(2)
3964 /// @return the resulting function values
3965 coeffT multiply(const coeffT& val_ket, const coeffT& val_pot, int particle) const;
3966
3967
3968 /// given several coefficient tensors, assemble a result tensor
3969
3970 /// the result looks like: (v(1,2) + v(1) + v(2)) |ket(1,2)>
3971 /// or (v(1,2) + v(1) + v(2)) |p(1) p(2)>
3972 /// i.e. coefficients for the ket and coefficients for the two particles are
3973 /// mutually exclusive. All potential terms are optional, just pass in empty coeffs.
3974 /// @param[in] key the key of the FunctionNode to which these coeffs belong
3975 /// @param[in] coeff_ket coefficients of the ket
3976 /// @param[in] vpotential1 function values of the potential for particle 1
3977 /// @param[in] vpotential2 function values of the potential for particle 2
3978 /// @param[in] veri function values for the 2-particle potential
3979 coeffT assemble_coefficients(const keyT& key, const coeffT& coeff_ket,
3980 const coeffT& vpotential1, const coeffT& vpotential2,
3981 const tensorT& veri) const;
3982
3983
3984
3985 template<std::size_t LDIM>
3989 double error=0.0;
3990 double lo=0.0, hi=0.0, lo1=0.0, hi1=0.0, lo2=0.0, hi2=0.0;
3991
3993 pointwise_multiplier(const Key<NDIM> key, const coeffT& clhs) : coeff_lhs(clhs) {
3995 val_lhs=fcf.coeffs2values(key,coeff_lhs);
3996 error=0.0;
3998 if (coeff_lhs.is_svd_tensor()) {
4001 }
4002 }
4003
4004 /// multiply values of rhs and lhs, result on rhs, rhs and lhs are of the same dimensions
4005 tensorT operator()(const Key<NDIM> key, const tensorT& coeff_rhs) {
4006
4007 MADNESS_ASSERT(coeff_rhs.dim(0)==coeff_lhs.dim(0));
4009
4010 // the tnorm estimate is not tight enough to be efficient, better use oversampling
4011 bool use_tnorm=false;
4012 if (use_tnorm) {
4013 double rlo, rhi;
4014 implT::tnorm(coeff_rhs,&rlo,&rhi);
4015 error = hi*rlo + rhi*lo + rhi*hi;
4016 tensorT val_rhs=fcf.coeffs2values(key, coeff_rhs);
4017 val_rhs.emul(val_lhs.full_tensor());
4018 return fcf.values2coeffs(key,val_rhs);
4019 } else { // use quadrature of order k+1
4020
4021 auto& cdata=FunctionCommonData<T,NDIM>::get(coeff_rhs.dim(0)); // npt=k+1
4022 auto& cdata_npt=FunctionCommonData<T,NDIM>::get(coeff_rhs.dim(0)+oversampling); // npt=k+1
4023 FunctionCommonFunctionality<T,NDIM> fcf_hi_npt(cdata_npt);
4024
4025 // coeffs2values for rhs: k -> npt=k+1
4026 tensorT coeff1(cdata_npt.vk);
4027 coeff1(cdata.s0)=coeff_rhs; // s0 is smaller than vk!
4028 tensorT val_rhs_k1=fcf_hi_npt.coeffs2values(key,coeff1);
4029
4030 // coeffs2values for lhs: k -> npt=k+1
4031 tensorT coeff_lhs_k1(cdata_npt.vk);
4032 coeff_lhs_k1(cdata.s0)=std::as_const(coeff_lhs).full_tensor();
4033 tensorT val_lhs_k1=fcf_hi_npt.coeffs2values(key,coeff_lhs_k1);
4034
4035 // multiply
4036 val_lhs_k1.emul(val_rhs_k1);
4037
4038 // values2coeffs: npt = k+1-> k
4039 tensorT result1=fcf_hi_npt.values2coeffs(key,val_lhs_k1);
4040
4041 // extract coeffs up to k
4042 tensorT result=copy(result1(cdata.s0));
4043 result1(cdata.s0)=0.0;
4044 error=result1.normf();
4045 return result;
4046 }
4047 }
4048
4049 /// multiply values of rhs and lhs, result on rhs, rhs and lhs are of differnet dimensions
4050 coeffT operator()(const Key<NDIM> key, const tensorT& coeff_rhs, const int particle) {
4051 Key<LDIM> key1, key2;
4052 key.break_apart(key1,key2);
4053 const long k=coeff_rhs.dim(0);
4055 auto& cdata_lowdim=FunctionCommonData<T,LDIM>::get(k);
4056 FunctionCommonFunctionality<T,LDIM> fcf_lo(cdata_lowdim);
4060
4061
4062 // make hi-dim values from lo-dim coeff_rhs on npt grid points
4063 tensorT ones=tensorT(fcf_lo_npt.cdata.vk);
4064 ones=1.0;
4065
4066 tensorT coeff_rhs_npt1(fcf_lo_npt.cdata.vk);
4067 coeff_rhs_npt1(fcf_lo.cdata.s0)=coeff_rhs;
4068 tensorT val_rhs_npt1=fcf_lo_npt.coeffs2values(key1,coeff_rhs_npt1);
4069
4070 TensorArgs targs(-1.0,TT_2D);
4071 coeffT val_rhs;
4072 if (particle==1) val_rhs=outer(val_rhs_npt1,ones,targs);
4073 if (particle==2) val_rhs=outer(ones,val_rhs_npt1,targs);
4074
4075 // make values from hi-dim coeff_lhs on npt grid points
4076 coeffT coeff_lhs_k1(fcf_hi_npt.cdata.vk,coeff_lhs.tensor_type());
4077 coeff_lhs_k1(fcf_hi.cdata.s0)+=coeff_lhs;
4078 coeffT val_lhs_npt=fcf_hi_npt.coeffs2values(key,coeff_lhs_k1);
4079
4080 // multiply
4081 val_lhs_npt.emul(val_rhs);
4082
4083 // values2coeffs: npt = k+1-> k
4084 coeffT result1=fcf_hi_npt.values2coeffs(key,val_lhs_npt);
4085
4086 // extract coeffs up to k
4087 coeffT result=copy(result1(cdata.s0));
4088 result1(cdata.s0)=0.0;
4089 error=result1.normf();
4090 return result;
4091 }
4092
4093 template <typename Archive> void serialize(const Archive& ar) {
4094 ar & error & lo & lo1 & lo2 & hi & hi1& hi2 & val_lhs & coeff_lhs;
4095 }
4096
4097
4098 };
4099
4100 /// given a ket and the 1- and 2-electron potentials, construct the function V phi
4101
4102 /// small memory footstep version of Vphi_op: use the NS form to have information
4103 /// about parent and children to determine if a box is a leaf. This will require
4104 /// compression of the constituent functions, which will lead to more memory usage
4105 /// there, but will avoid oversampling of the result function.
4106 template<typename opT, size_t LDIM>
4107 struct Vphi_op_NS {
4108
4109 bool randomize() const {return true;}
4110
4114
4115 implT* result; ///< where to construct Vphi, no need to track parents
4116 opT leaf_op; ///< deciding if a given FunctionNode will be a leaf node
4117 ctT iaket; ///< the ket of a pair function (exclusive with p1, p2)
4118 ctL iap1, iap2; ///< the particles 1 and 2 (exclusive with ket)
4119 ctL iav1, iav2; ///< potentials for particles 1 and 2
4120 const implT* eri; ///< 2-particle potential, must be on-demand
4121
4122 bool have_ket() const {return iaket.get_impl();}
4123 bool have_v1() const {return iav1.get_impl();}
4124 bool have_v2() const {return iav2.get_impl();}
4125 bool have_eri() const {return eri;}
4126
4127 void accumulate_into_result(const Key<NDIM>& key, const coeffT& coeff) const {
4129 }
4130
4131 // ctor
4133 Vphi_op_NS(implT* result, const opT& leaf_op, const ctT& iaket,
4134 const ctL& iap1, const ctL& iap2, const ctL& iav1, const ctL& iav2,
4135 const implT* eri)
4137 , iav1(iav1), iav2(iav2), eri(eri) {
4138
4139 // 2-particle potential must be on-demand
4141 }
4142
4143 /// make and insert the coefficients into result's tree
4144 std::pair<bool,coeffT> operator()(const Key<NDIM>& key) const {
4145
4147 if(leaf_op.do_pre_screening()){
4148 // this means that we only construct the boxes which are leaf boxes from the other function in the leaf_op
4149 if(leaf_op.pre_screening(key)){
4150 // construct sum_coefficients, insert them and leave
4151 auto [sum_coeff, error]=make_sum_coeffs(key);
4152 accumulate_into_result(key,sum_coeff);
4153 return std::pair<bool,coeffT> (true,coeffT());
4154 }else{
4155 return continue_recursion(std::vector<bool>(1<<NDIM,false),tensorT(),key);
4156 }
4157 }
4158
4159 // this means that the function has to be completely constructed and not mirrored by another function
4160
4161 // if the initial level is not reached then this must not be a leaf box
4162 size_t il = result->get_initial_level();
4164 if(key.level()<int(il)){
4165 return continue_recursion(std::vector<bool>(1<<NDIM,false),tensorT(),key);
4166 }
4167 // if further refinement is needed (because we are at a special box, special point)
4168 // and the special_level is not reached then this must not be a leaf box
4169 if(key.level()<result->get_special_level() and leaf_op.special_refinement_needed(key)){
4170 return continue_recursion(std::vector<bool>(1<<NDIM,false),tensorT(),key);
4171 }
4172
4173 auto [sum_coeff,error]=make_sum_coeffs(key);
4174
4175 // coeffs are leaf (for whatever reason), insert into tree and stop recursion
4176 if(leaf_op.post_screening(key,sum_coeff)){
4177 accumulate_into_result(key,sum_coeff);
4178 return std::pair<bool,coeffT> (true,coeffT());
4179 }
4180
4181 // coeffs are accurate, insert into tree and stop recursion
4182 if(error<result->truncate_tol(result->get_thresh(),key)){
4183 accumulate_into_result(key,sum_coeff);
4184 return std::pair<bool,coeffT> (true,coeffT());
4185 }
4186
4187 // coeffs are inaccurate, continue recursion
4188 std::vector<bool> child_is_leaf(1<<NDIM,false);
4189 return continue_recursion(child_is_leaf,tensorT(),key);
4190 }
4191
4192
4193 /// loop over all children and either insert their sum coeffs or continue the recursion
4194
4195 /// @param[in] child_is_leaf for each child: is it a leaf?
4196 /// @param[in] coeffs coefficient tensor with 2^N sum coeffs (=unfiltered NS coeffs)
4197 /// @param[in] key the key for the NS coeffs (=parent key of the children)
4198 /// @return to avoid recursion outside this return: std::pair<is_leaf,coeff> = true,coeffT()
4199 std::pair<bool,coeffT> continue_recursion(const std::vector<bool> child_is_leaf,
4200 const tensorT& coeffs, const keyT& key) const {
4201 std::size_t i=0;
4202 for (KeyChildIterator<NDIM> kit(key); kit; ++kit, ++i) {
4203 keyT child=kit.key();
4204 bool is_leaf=child_is_leaf[i];
4205
4206 if (is_leaf) {
4207 // insert the sum coeffs
4209 iop(child,coeffT(copy(coeffs(result->child_patch(child))),result->get_tensor_args()),is_leaf);
4210 } else {
4211 this_type child_op=this->make_child(child);
4212 noop<T,NDIM> no;
4213 // spawn activation where child is local
4214 ProcessID p=result->get_coeffs().owner(child);
4215
4216 void (implT::*ft)(const Vphi_op_NS<opT,LDIM>&, const noop<T,NDIM>&, const keyT&) const = &implT:: template forward_traverse< Vphi_op_NS<opT,LDIM>, noop<T,NDIM> >;
4217 result->task(p, ft, child_op, no, child);
4218 }
4219 }
4220 // return e sum coeffs; also return always is_leaf=true:
4221 // the recursion is continued within this struct, not outside in traverse_tree!
4222 return std::pair<bool,coeffT> (true,coeffT());
4223 }
4224
4225 tensorT eri_coeffs(const keyT& key) const {
4228 if (eri->get_functor()->provides_coeff()) {
4229 return eri->get_functor()->coeff(key).full_tensor();
4230 } else {
4231 tensorT val_eri(eri->cdata.vk);
4232 eri->fcube(key,*(eri->get_functor()),eri->cdata.quad_x,val_eri);
4233 return eri->values2coeffs(key,val_eri);
4234 }
4235 }
4236
4237 /// the error is computed from the d coefficients of the constituent functions
4238
4239 /// the result is h_n = P_n(f g), computed as h_n \approx Pn(f_n g_n)
4240 /// its error is therefore
4241 /// h_n = (f g)_n = ((Pn(f) + Qn(f)) (Pn(g) + Qn(g))
4242 /// = Pn(fn gn) + Qn(fn gn) + Pn(f) Qn(g) + Qn(f) Pn(g) + Qn(f) Pn(g)
4243 /// the first term is what we compute, the second term is estimated by tnorm (in another function),
4244 /// the third to last terms are estimated in this function by e.g.: Qn(f)Pn(g) < ||Qn(f)|| ||Pn(g)||
4246 const tensorT& ceri) const {
4247 double error = 0.0;
4248 Key<LDIM> key1, key2;
4249 key.break_apart(key1,key2);
4250
4251 PROFILE_BLOCK(compute_error);
4252 double dnorm_ket, snorm_ket;
4253 if (have_ket()) {
4254 snorm_ket=iaket.coeff(key).normf();
4255 dnorm_ket=iaket.dnorm(key);
4256 } else {
4257 double s1=iap1.coeff(key1).normf();
4258 double s2=iap2.coeff(key2).normf();
4259 double d1=iap1.dnorm(key1);
4260 double d2=iap2.dnorm(key2);
4261 snorm_ket=s1*s2;
4262 dnorm_ket=s1*d2 + s2*d1 + d1*d2;
4263 }
4264
4265 if (have_v1()) {
4266 double snorm=iav1.coeff(key1).normf();
4267 double dnorm=iav1.dnorm(key1);
4268 error+=snorm*dnorm_ket + dnorm*snorm_ket + dnorm*dnorm_ket;
4269 }
4270 if (have_v2()) {
4271 double snorm=iav2.coeff(key2).normf();
4272 double dnorm=iav2.dnorm(key2);
4273 error+=snorm*dnorm_ket + dnorm*snorm_ket + dnorm*dnorm_ket;
4274 }
4275 if (have_eri()) {
4276 tensorT s_coeffs=ceri(result->cdata.s0);
4277 double snorm=s_coeffs.normf();
4278 tensorT d=copy(ceri);
4279 d(result->cdata.s0)=0.0;
4280 double dnorm=d.normf();
4281 error+=snorm*dnorm_ket + dnorm*snorm_ket + dnorm*dnorm_ket;
4282 }
4283
4284 bool no_potential=not ((have_v1() or have_v2() or have_eri()));
4285 if (no_potential) {
4286 error=dnorm_ket;
4287 }
4288 return error;
4289 }
4290
4291 /// make the sum coeffs for key
4292 std::pair<coeffT,double> make_sum_coeffs(const keyT& key) const {
4294 // break key into particles
4295 Key<LDIM> key1, key2;
4296 key.break_apart(key1,key2);
4297
4298 // bool printme=(int(key.translation()[0])==int(std::pow(key.level(),2)/2)) and
4299 // (int(key.translation()[1])==int(std::pow(key.level(),2)/2)) and
4300 // (int(key.translation()[2])==int(std::pow(key.level(),2)/2));
4301
4302// printme=false;
4303
4304 // get/make all coefficients
4305 const coeffT coeff_ket = (iaket.get_impl()) ? iaket.coeff(key)
4306 : outer(iap1.coeff(key1),iap2.coeff(key2),result->get_tensor_args());
4307 const coeffT cpot1 = (have_v1()) ? iav1.coeff(key1) : coeffT();
4308 const coeffT cpot2 = (have_v2()) ? iav2.coeff(key2) : coeffT();
4309 const tensorT ceri = (have_eri()) ? eri_coeffs(key) : tensorT();
4310
4311 // compute first part of the total error
4312 double refine_error=compute_error_from_inaccurate_refinement(key,ceri);
4313 double error=refine_error;
4314
4315 // prepare the multiplication
4316 pointwise_multiplier<LDIM> pm(key,coeff_ket);
4317
4318 // perform the multiplication, compute tnorm part of the total error
4319 coeffT cresult(result->cdata.vk,result->get_tensor_args());
4320 if (have_v1()) {
4321 cresult+=pm(key,cpot1.get_tensor(),1);
4322 error+=pm.error;
4323 }
4324 if (have_v2()) {
4325 cresult+=pm(key,cpot2.get_tensor(),2);
4326 error+=pm.error;
4327 }
4328
4329 if (have_eri()) {
4330 tensorT result1=cresult.full_tensor_copy();
4331 result1+=pm(key,copy(ceri(result->cdata.s0)));
4332 cresult=coeffT(result1,result->get_tensor_args());
4333 error+=pm.error;
4334 } else {
4336 }
4337 if ((not have_v1()) and (not have_v2()) and (not have_eri())) {
4338 cresult=coeff_ket;
4339 }
4340
4341 return std::make_pair(cresult,error);
4342 }
4343
4344 this_type make_child(const keyT& child) const {
4345
4346 // break key into particles
4347 Key<LDIM> key1, key2;
4348 child.break_apart(key1,key2);
4349
4350 return this_type(result,leaf_op,iaket.make_child(child),
4351 iap1.make_child(key1),iap2.make_child(key2),
4352 iav1.make_child(key1),iav2.make_child(key2),eri);
4353 }
4354
4356 Future<ctT> iaket1=iaket.activate();
4357 Future<ctL> iap11=iap1.activate();
4358 Future<ctL> iap21=iap2.activate();
4359 Future<ctL> iav11=iav1.activate();
4360 Future<ctL> iav21=iav2.activate();
4361 return result->world.taskq.add(detail::wrap_mem_fn(*const_cast<this_type *> (this),
4362 &this_type::forward_ctor),result,leaf_op,
4363 iaket1,iap11,iap21,iav11,iav21,eri);
4364 }
4365
4366 this_type forward_ctor(implT* result1, const opT& leaf_op, const ctT& iaket1,
4367 const ctL& iap11, const ctL& iap21, const ctL& iav11, const ctL& iav21,
4368 const implT* eri1) {
4369 return this_type(result1,leaf_op,iaket1,iap11,iap21,iav11,iav21,eri1);
4370 }
4371
4372 /// serialize this (needed for use in recursive_op)
4373 template <typename Archive> void serialize(const Archive& ar) {
4374 ar & iaket & eri & result & leaf_op & iap1 & iap2 & iav1 & iav2;
4375 }
4376 };
4377
4378 /// assemble the function V*phi using V and phi given from the functor
4379
4380 /// this function must have been constructed using the CompositeFunctorInterface.
4381 /// The interface provides one- and two-electron potentials, and the ket, which are
4382 /// assembled to give V*phi.
4383 /// @param[in] leaf_op operator to decide if a given node is a leaf node
4384 /// @param[in] fence global fence
4385 template<typename opT>
4386 void make_Vphi(const opT& leaf_op, const bool fence=true) {
4387
4388 constexpr size_t LDIM=NDIM/2;
4389 MADNESS_CHECK_THROW(NDIM==LDIM*2,"make_Vphi only works for even dimensions");
4390
4391
4392 // keep the functor available, but remove it from the result
4393 // result will return false upon is_on_demand(), which is necessary for the
4394 // CoeffTracker to track the parent coeffs correctly for error_leaf_op
4395 std::shared_ptr< FunctionFunctorInterface<T,NDIM> > func2(this->get_functor());
4396 this->unset_functor();
4397
4399 dynamic_cast<CompositeFunctorInterface<T,NDIM,LDIM>* >(&(*func2));
4401
4402 // make sure everything is in place if no fence is requested
4403 if (fence) func->make_redundant(true); // no-op if already redundant
4404 MADNESS_CHECK_THROW(func->check_redundant(),"make_Vphi requires redundant functions");
4405
4406 // loop over all functions in the functor (either ket or particles)
4407 for (auto& ket : func->impl_ket_vector) {
4408 FunctionImpl<T,NDIM>* eri=func->impl_eri.get();
4409 FunctionImpl<T,LDIM>* v1=func->impl_m1.get();
4410 FunctionImpl<T,LDIM>* v2=func->impl_m2.get();
4411 FunctionImpl<T,LDIM>* p1=nullptr;
4412 FunctionImpl<T,LDIM>* p2=nullptr;
4413 make_Vphi_only(leaf_op,ket.get(),v1,v2,p1,p2,eri,false);
4414 }
4415
4416 for (std::size_t i=0; i<func->impl_p1_vector.size(); ++i) {
4417 FunctionImpl<T,NDIM>* ket=nullptr;
4418 FunctionImpl<T,NDIM>* eri=func->impl_eri.get();
4419 FunctionImpl<T,LDIM>* v1=func->impl_m1.get();
4420 FunctionImpl<T,LDIM>* v2=func->impl_m2.get();
4421 FunctionImpl<T,LDIM>* p1=func->impl_p1_vector[i].get();
4422 FunctionImpl<T,LDIM>* p2=func->impl_p2_vector[i].get();
4423 make_Vphi_only(leaf_op,ket,v1,v2,p1,p2,eri,false);
4424 }
4425
4426 // some post-processing:
4427 // - FunctionNode::accumulate() uses buffer -> add the buffer contents to the actual coefficients
4428 // - the operation constructs sum coefficients on all scales -> sum down to get a well-defined tree-state
4429 if (fence) {
4430 world.gop.fence();
4432 sum_down(true);
4434 }
4435
4436
4437 }
4438
4439 /// assemble the function V*phi using V and phi given from the functor
4440
4441 /// this function must have been constructed using the CompositeFunctorInterface.
4442 /// The interface provides one- and two-electron potentials, and the ket, which are
4443 /// assembled to give V*phi.
4444 /// @param[in] leaf_op operator to decide if a given node is a leaf node
4445 /// @param[in] fence global fence
4446 template<typename opT, std::size_t LDIM>
4451 const bool fence=true) {
4452
4453 // prepare the CoeffTracker
4454 CoeffTracker<T,NDIM> iaket(ket);
4455 CoeffTracker<T,LDIM> iap1(p1);
4456 CoeffTracker<T,LDIM> iap2(p2);
4457 CoeffTracker<T,LDIM> iav1(v1);
4458 CoeffTracker<T,LDIM> iav2(v2);
4459
4460 // the operator making the coefficients
4461 typedef Vphi_op_NS<opT,LDIM> coeff_opT;
4462 coeff_opT coeff_op(this,leaf_op,iaket,iap1,iap2,iav1,iav2,eri);
4463
4464 // this operator simply inserts the coeffs into this' tree
4465 typedef noop<T,NDIM> apply_opT;
4466 apply_opT apply_op;
4467
4468 if (world.rank() == coeffs.owner(cdata.key0)) {
4469 woT::task(world.rank(), &implT:: template forward_traverse<coeff_opT,apply_opT>,
4470 coeff_op, apply_op, cdata.key0);
4471 }
4472
4474 if (fence) world.gop.fence();
4475
4476 }
4477
4478 /// Permute the dimensions of f according to map, result on this
4479 void mapdim(const implT& f, const std::vector<long>& map, bool fence);
4480
4481 /// mirror the dimensions of f according to map, result on this
4482 void mirror(const implT& f, const std::vector<long>& mirror, bool fence);
4483
4484 /// map and mirror the translation index and the coefficients, result on this
4485
4486 /// first map the dimensions, the mirror!
4487 /// this = mirror(map(f))
4488 void map_and_mirror(const implT& f, const std::vector<long>& map,
4489 const std::vector<long>& mirror, bool fence);
4490
4491 /// take the average of two functions, similar to: this=0.5*(this+rhs)
4492
4493 /// works in either basis and also in nonstandard form
4494 void average(const implT& rhs);
4495
4496 /// change the tensor type of the coefficients in the FunctionNode
4497
4498 /// @param[in] targs target tensor arguments (threshold and full/low rank)
4499 void change_tensor_type1(const TensorArgs& targs, bool fence);
4500
4501 /// reduce the rank of the coefficients tensors
4502
4503 /// @param[in] targs target tensor arguments (threshold and full/low rank)
4504 void reduce_rank(const double thresh, bool fence);
4505
4506
4507 /// remove all nodes with level higher than n
4508 void chop_at_level(const int n, const bool fence=true);
4509
4510 /// compute norm of s and d coefficients for all nodes
4511 void compute_snorm_and_dnorm(bool fence=true);
4512
4513 /// compute the norm of the wavelet coefficients
4516
4520
4521 bool operator()(typename rangeT::iterator& it) const {
4522 auto& node=it->second;
4523 node.recompute_snorm_and_dnorm(cdata);
4524 return true;
4525 }
4526 };
4527
4528
4529 T eval_cube(Level n, coordT& x, const tensorT& c) const;
4530
4531 /// Transform sum coefficients at level n to sums+differences at level n-1
4532
4533 /// Given scaling function coefficients s[n][l][i] and s[n][l+1][i]
4534 /// return the scaling function and wavelet coefficients at the
4535 /// coarser level. I.e., decompose Vn using Vn = Vn-1 + Wn-1.
4536 /// \code
4537 /// s_i = sum(j) h0_ij*s0_j + h1_ij*s1_j
4538 /// d_i = sum(j) g0_ij*s0_j + g1_ij*s1_j
4539 // \endcode
4540 /// Returns a new tensor and has no side effects. Works for any
4541 /// number of dimensions.
4542 ///
4543 /// No communication involved.
4544 tensorT filter(const tensorT& s) const;
4545
4546 coeffT filter(const coeffT& s) const;
4547
4548 /// Transform sums+differences at level n to sum coefficients at level n+1
4549
4550 /// Given scaling function and wavelet coefficients (s and d)
4551 /// returns the scaling function coefficients at the next finer
4552 /// level. I.e., reconstruct Vn using Vn = Vn-1 + Wn-1.
4553 /// \code
4554 /// s0 = sum(j) h0_ji*s_j + g0_ji*d_j
4555 /// s1 = sum(j) h1_ji*s_j + g1_ji*d_j
4556 /// \endcode
4557 /// Returns a new tensor and has no side effects
4558 ///
4559 /// If (sonly) ... then ss is only the scaling function coeff (and
4560 /// assume the d are zero). Works for any number of dimensions.
4561 ///
4562 /// No communication involved.
4563 tensorT unfilter(const tensorT& s) const;
4564
4565 coeffT unfilter(const coeffT& s) const;
4566
4567 /// downsample the sum coefficients of level n+1 to sum coeffs on level n
4568
4569 /// specialization of the filter method, will yield only the sum coefficients
4570 /// @param[in] key key of level n
4571 /// @param[in] v vector of sum coefficients of level n+1
4572 /// @return sum coefficients on level n in full tensor format
4573 tensorT downsample(const keyT& key, const std::vector< Future<coeffT > >& v) const;
4574
4575 /// upsample the sum coefficients of level 1 to sum coeffs on level n+1
4576
4577 /// specialization of the unfilter method, will transform only the sum coefficients
4578 /// @param[in] key key of level n+1
4579 /// @param[in] coeff sum coefficients of level n (does NOT belong to key!!)
4580 /// @return sum coefficients on level n+1
4581 coeffT upsample(const keyT& key, const coeffT& coeff) const;
4582
4583 /// Projects old function into new basis (only in reconstructed form)
4584 void project(const implT& old, bool fence);
4585
4587 bool operator()(const implT* f, const keyT& key, const nodeT& t) const {
4588 return true;
4589 }
4590 template <typename Archive> void serialize(Archive& ar) {}
4591 };
4592
4593 template <typename opT>
4594 void refine_op(const opT& op, const keyT& key) {
4595 // Must allow for someone already having autorefined the coeffs
4596 // and we get a write accessor just in case they are already executing
4597 typename dcT::accessor acc;
4598 const auto found = coeffs.find(acc,key);
4599 MADNESS_CHECK(found);
4600 nodeT& node = acc->second;
4601 if (node.has_coeff() && key.level() < max_refine_level && op(this, key, node)) {
4602 coeffT d(cdata.v2k,targs);
4603 d(cdata.s0) += copy(node.coeff());
4604 d = unfilter(d);
4605 node.clear_coeff();
4606 node.set_has_children(true);
4607 for (KeyChildIterator<NDIM> kit(key); kit; ++kit) {
4608 const keyT& child = kit.key();
4609 coeffT ss = copy(d(child_patch(child)));
4611 // coeffs.replace(child,nodeT(ss,-1.0,false).node_to_low_rank());
4612 coeffs.replace(child,nodeT(ss,-1.0,false));
4613 // Note value -1.0 for norm tree to indicate result of refinement
4614 }
4615 }
4616 }
4617
4618 template <typename opT>
4619 void refine_spawn(const opT& op, const keyT& key) {
4620 nodeT& node = coeffs.find(key).get()->second;
4621 if (node.has_children()) {
4622 for (KeyChildIterator<NDIM> kit(key); kit; ++kit)
4623 woT::task(coeffs.owner(kit.key()), &implT:: template refine_spawn<opT>, op, kit.key(), TaskAttributes::hipri());
4624 }
4625 else {
4626 woT::task(coeffs.owner(key), &implT:: template refine_op<opT>, op, key);
4627 }
4628 }
4629
4630 // Refine in real space according to local user-defined criterion
4631 template <typename opT>
4632 void refine(const opT& op, bool fence) {
4633 if (world.rank() == coeffs.owner(cdata.key0))
4634 woT::task(coeffs.owner(cdata.key0), &implT:: template refine_spawn<opT>, op, cdata.key0, TaskAttributes::hipri());
4635 if (fence)
4636 world.gop.fence();
4637 }
4638
4639 bool exists_and_has_children(const keyT& key) const;
4640
4641 bool exists_and_is_leaf(const keyT& key) const;
4642
4643
4644 void broaden_op(const keyT& key, const std::vector< Future <bool> >& v);
4645
4646 // For each local node sets value of norm tree, snorm and dnorm to 0.0
4647 void zero_norm_tree();
4648
4649 // Broaden tree
4650 void broaden(const array_of_bools<NDIM>& is_periodic, bool fence);
4651
4652 /// sum all the contributions from all scales after applying an operator in mod-NS form
4653 void trickle_down(bool fence);
4654
4655 /// sum all the contributions from all scales after applying an operator in mod-NS form
4656
4657 /// cf reconstruct_op
4658 void trickle_down_op(const keyT& key, const coeffT& s);
4659
4660 /// reconstruct this tree -- respects fence
4661 void reconstruct(bool fence);
4662
4663 void change_tree_state(const TreeState finalstate, bool fence=true);
4664
4665 // Invoked on node where key is local
4666 // void reconstruct_op(const keyT& key, const tensorT& s);
4667 void reconstruct_op(const keyT& key, const coeffT& s, const bool accumulate_NS=true);
4668
4669 /// compress the wave function
4670
4671 /// after application there will be sum coefficients at the root level,
4672 /// and difference coefficients at all other levels; furthermore:
4673 /// @param[in] nonstandard keep sum coeffs at all other levels, except leaves
4674 /// @param[in] keepleaves keep sum coeffs (but no diff coeffs) at leaves
4675 /// @param[in] redundant keep only sum coeffs at all levels, discard difference coeffs
4676// void compress(bool nonstandard, bool keepleaves, bool redundant, bool fence);
4677 void compress(const TreeState newstate, bool fence);
4678
4679 /// Invoked on node where key is local
4680 Future<std::pair<coeffT,double> > compress_spawn(const keyT& key, bool nonstandard, bool keepleaves,
4681 bool redundant1);
4682
4683 private:
4684 /// convert this to redundant, i.e. have sum coefficients on all levels
4685 void make_redundant(const bool fence);
4686 public:
4687
4688 /// convert this from redundant to standard reconstructed form
4689 void undo_redundant(const bool fence);
4690
4691 void remove_internal_coefficients(const bool fence);
4692 void remove_leaf_coefficients(const bool fence);
4693
4694
4695 /// compute for each FunctionNode the norm of the function inside that node
4696 void norm_tree(bool fence);
4697
4698 double norm_tree_op(const keyT& key, const std::vector< Future<double> >& v);
4699
4701
4702 /// truncate using a tree in reconstructed form
4703
4704 /// must be invoked where key is local
4705 Future<coeffT> truncate_reconstructed_spawn(const keyT& key, const double tol);
4706
4707 /// given the sum coefficients of all children, truncate or not
4708
4709 /// @return new sum coefficients (empty if internal, not empty, if new leaf); might delete its children
4710 coeffT truncate_reconstructed_op(const keyT& key, const std::vector< Future<coeffT > >& v, const double tol);
4711
4712 /// calculate the wavelet coefficients using the sum coefficients of all child nodes
4713
4714 /// also compute the norm tree for all nodes
4715 /// @param[in] key this's key
4716 /// @param[in] v sum coefficients of the child nodes
4717 /// @param[in] nonstandard keep the sum coefficients with the wavelet coefficients
4718 /// @param[in] redundant keep only the sum coefficients, discard the wavelet coefficients
4719 /// @return the sum coefficients
4720 std::pair<coeffT,double> compress_op(const keyT& key, const std::vector< Future<std::pair<coeffT,double>> >& v, bool nonstandard);
4721
4722
4723 /// similar to compress_op, but insert only the sum coefficients in the tree
4724
4725 /// also compute the norm tree for all nodes
4726 /// @param[in] key this's key
4727 /// @param[in] v sum coefficients of the child nodes
4728 /// @return the sum coefficients
4729 std::pair<coeffT,double> make_redundant_op(const keyT& key,const std::vector< Future<std::pair<coeffT,double> > >& v);
4730
4731 /// Changes non-standard compressed form to standard compressed form
4732 void standard(bool fence);
4733
4734 /// Changes non-standard compressed form to standard compressed form
4737
4738 // threshold for rank reduction / SVD truncation
4740
4741 // constructor takes target precision
4742 do_standard() = default;
4744
4745 //
4746 bool operator()(typename rangeT::iterator& it) const {
4747
4748 const keyT& key = it->first;
4749 nodeT& node = it->second;
4750 if (key.level()> 0 && node.has_coeff()) {
4751 if (node.has_children()) {
4752 // Zero out scaling coeffs
4753 MADNESS_ASSERT(node.coeff().dim(0)==2*impl->get_k());
4754 node.coeff()(impl->cdata.s0)=0.0;
4755 node.reduceRank(impl->targs.thresh);
4756 } else {
4757 // Deleting both scaling and wavelet coeffs
4758 node.clear_coeff();
4759 }
4760 }
4761 return true;
4762 }
4763 template <typename Archive> void serialize(const Archive& ar) {
4764 MADNESS_EXCEPTION("no serialization of do_standard",1);
4765 }
4766 };
4767
4768
4769 /// laziness
4770 template<size_t OPDIM>
4771 struct do_op_args {
4774 double tol, fac, cnorm;
4775
4776 do_op_args() = default;
4777 do_op_args(const Key<OPDIM>& key, const Key<OPDIM>& d, const keyT& dest, double tol, double fac, double cnorm)
4778 : key(key), d(d), dest(dest), tol(tol), fac(fac), cnorm(cnorm) {}
4779 template <class Archive>
4780 void serialize(Archive& ar) {
4781 ar & archive::wrap_opaque(this,1);
4782 }
4783 };
4784
4785 /// for fine-grain parallelism: call the apply method of an operator in a separate task
4786
4787 /// @param[in] op the operator working on our function
4788 /// @param[in] c full rank tensor holding the NS coefficients
4789 /// @param[in] args laziness holding norm of the coefficients, displacement, destination, ..
4790 template <typename opT, typename R, size_t OPDIM>
4791 void do_apply_kernel(const opT* op, const Tensor<R>& c, const do_op_args<OPDIM>& args) {
4792
4793 tensorT result = op->apply(args.key, args.d, c, args.tol/args.fac/args.cnorm);
4794
4795 // Screen here to reduce communication cost of negligible data
4796 // and also to ensure we don't needlessly widen the tree when
4797 // applying the operator
4798 if (result.normf()> 0.3*args.tol/args.fac) {
4800 //woT::task(world.rank(),&implT::accumulate_timer,time,TaskAttributes::hipri());
4801 // UGLY BUT ADDED THE OPTIMIZATION BACK IN HERE EXPLICITLY/
4802 if (args.dest == world.rank()) {
4803 coeffs.send(args.dest, &nodeT::accumulate, result, coeffs, args.dest);
4804 }
4805 else {
4807 }
4808 }
4809 }
4810
4811 /// same as do_apply_kernel, but use full rank tensors as input and low rank tensors as output
4812
4813 /// @param[in] op the operator working on our function
4814 /// @param[in] c full rank tensor holding the NS coefficients
4815 /// @param[in] args laziness holding norm of the coefficients, displacement, destination, ..
4816 /// @param[in] apply_targs TensorArgs with tightened threshold for accumulation
4817 /// @return nothing, but accumulate the result tensor into the destination node
4818 template <typename opT, typename R, size_t OPDIM>
4819 double do_apply_kernel2(const opT* op, const Tensor<R>& c, const do_op_args<OPDIM>& args,
4820 const TensorArgs& apply_targs) {
4821
4822 tensorT result_full = op->apply(args.key, args.d, c, args.tol/args.fac/args.cnorm);
4823 const double norm=result_full.normf();
4824
4825 // Screen here to reduce communication cost of negligible data
4826 // and also to ensure we don't needlessly widen the tree when
4827 // applying the operator
4828 // OPTIMIZATION NEEDED HERE ... CHANGING THIS TO TASK NOT SEND REMOVED
4829 // BUILTIN OPTIMIZATION TO SHORTCIRCUIT MSG IF DATA IS LOCAL
4830 if (norm > 0.3*args.tol/args.fac) {
4831
4832 small++;
4833 //double cpu0=cpu_time();
4834 coeffT result=coeffT(result_full,apply_targs);
4835 MADNESS_ASSERT(result.is_full_tensor() or result.is_svd_tensor());
4836 //double cpu1=cpu_time();
4837 //timer_lr_result.accumulate(cpu1-cpu0);
4838
4839 coeffs.task(args.dest, &nodeT::accumulate, result, coeffs, args.dest, apply_targs,
4841
4842 //woT::task(world.rank(),&implT::accumulate_timer,time,TaskAttributes::hipri());
4843 }
4844 return norm;
4845 }
4846
4847
4848
4849 /// same as do_apply_kernel2, but use low rank tensors as input and low rank tensors as output
4850
4851 /// @param[in] op the operator working on our function
4852 /// @param[in] coeff full rank tensor holding the NS coefficients
4853 /// @param[in] args laziness holding norm of the coefficients, displacement, destination, ..
4854 /// @param[in] apply_targs TensorArgs with tightened threshold for accumulation
4855 /// @return nothing, but accumulate the result tensor into the destination node
4856 template <typename opT, typename R, size_t OPDIM>
4857 double do_apply_kernel3(const opT* op, const GenTensor<R>& coeff, const do_op_args<OPDIM>& args,
4858 const TensorArgs& apply_targs) {
4859
4860 coeffT result;
4861 if (2*OPDIM==NDIM) result= op->apply2_lowdim(args.key, args.d, coeff,
4862 args.tol/args.fac/args.cnorm, args.tol/args.fac);
4863 if (OPDIM==NDIM) result = op->apply2(args.key, args.d, coeff,
4864 args.tol/args.fac/args.cnorm, args.tol/args.fac);
4865
4866 const double result_norm=result.svd_normf();
4867
4868 if (result_norm> 0.3*args.tol/args.fac) {
4869 small++;
4870
4871 double cpu0=cpu_time();
4872 if (not result.is_of_tensortype(targs.tt)) result=result.convert(targs);
4873 double cpu1=cpu_time();
4874 timer_lr_result.accumulate(cpu1-cpu0);
4875
4876 // accumulate also expects result in SVD form
4877 coeffs.task(args.dest, &nodeT::accumulate, result, coeffs, args.dest, apply_targs,
4879// woT::task(world.rank(),&implT::accumulate_timer,time,TaskAttributes::hipri());
4880
4881 }
4882 return result_norm;
4883
4884 }
4885
4886 // volume of n-dimensional sphere of radius R
4887 double vol_nsphere(int n, double R) {
4888 return std::pow(madness::constants::pi,n*0.5)*std::pow(R,n)/std::tgamma(1+0.5*n);
4889 }
4890
4891
4892 /// apply an operator on the coeffs c (at node key)
4893
4894 /// the result is accumulated inplace to this's tree at various FunctionNodes
4895 /// @param[in] op the operator to act on the source function
4896 /// @param[in] key key of the source FunctionNode of f which is processed
4897 /// @param[in] c coeffs of the FunctionNode of f which is processed
4898 template <typename opT, typename R>
4899 void do_apply(const opT* op, const keyT& key, const Tensor<R>& c) {
4901
4902 // working assumption here WAS that the operator is
4903 // isotropic and monotonically decreasing with distance
4904 // ... however, now we are using derivative Gaussian
4905 // expansions (and also non-cubic boxes) isotropic is
4906 // violated. While not strictly monotonically decreasing,
4907 // the derivative gaussian is still such that once it
4908 // becomes negligible we are in the asymptotic region.
4909
4910 typedef typename opT::keyT opkeyT;
4911 constexpr auto opdim = opT::opdim;
4912 const opkeyT source = op->get_source_key(key);
4913
4914 // Tuning here is based on observation that with
4915 // sufficiently high-order wavelet relative to the
4916 // precision, that only nearest neighbor boxes contribute,
4917 // whereas for low-order wavelets more neighbors will
4918 // contribute. Sufficiently high is picked as
4919 // k>=2-log10(eps) which is our empirical rule for
4920 // efficiency/accuracy and code instrumentation has
4921 // previously indicated that (in 3D) just unit
4922 // displacements are invoked. The error decays as R^-(k+1),
4923 // and the number of boxes increases as R^d.
4924 //
4925 // Fac is the expected number of contributions to a given
4926 // box, so the error permitted per contribution will be
4927 // tol/fac
4928
4929 // radius of shell (nearest neighbor is diameter of 3 boxes, so radius=1.5)
4930 double radius = 1.5 + 0.33 * std::max(0.0, 2 - std::log10(thresh) -
4931 k); // 0.33 was 0.5
4932 //double radius = 2.5;
4933 double fac = vol_nsphere(NDIM, radius);
4934 // previously fac=10.0 selected empirically constrained by qmprop
4935
4936 double cnorm = c.normf();
4937
4938 // BC handling:
4939 // - if operator is lattice-summed then treat this as nonperiodic (i.e. tell neighbor() to stay in simulation cell)
4940 // - if operator is NOT lattice-summed then obey BC (i.e. tell neighbor() to go outside the simulation cell along periodic dimensions)
4941 // - BUT user can force operator to treat its arguments as non-periodic (`op.set_domain_periodicity({true,true,true})`) so ... which dimensions of this function are treated as periodic by op?
4942 const array_of_bools<NDIM> func_is_treated_by_op_as_periodic =
4943 (op->particle() == 1)
4944 ? array_of_bools<NDIM>{false}.or_front(
4945 op->func_domain_is_periodic())
4946 : array_of_bools<NDIM>{false}.or_back(
4947 op->func_domain_is_periodic());
4948
4949 const auto default_real_distance_squared = [&](const auto &displacement)
4950 -> double {
4951 return displacement.real_distsq_bc(op->lattice_summed(), FunctionDefaults<NDIM>::get_cell_width());
4952 };
4953 const auto default_lattice_distance_squared = [&](const auto &displacement)
4954 -> std::uint64_t {
4955 return displacement.distsq_bc(op->lattice_summed());
4956 };
4957 const auto default_skip_predicate = [&](const auto &displacement)
4958 -> bool {
4959 return false;
4960 };
4961 const auto for_each = [&](const auto &displacements,
4962 const auto &real_distance_squared,
4963 const auto &lattice_distance_squared,
4964 const auto &skip_predicate) -> std::optional<double> {
4965
4966 // used to screen estimated and actual contributions
4967 //const double tol = truncate_tol(thresh, key);
4968 //const double tol = 0.1*truncate_tol(thresh, key);
4969 const double tol = truncate_tol(thresh, key);
4970
4971 // assume isotropic decaying kernel, screen in shell-wise fashion by
4972 // monitoring the decay of magnitude of contribution norms with the
4973 // distance ... as soon as we find a shell of displacements at least
4974 // one of each in simulation domain (see neighbor()) and
4975 // all in-domain shells produce negligible contributions, stop.
4976 // a displacement is negligible if ||op|| * ||c|| > tol / fac
4977 // where fac takes into account
4978 int nvalid = 1; // Counts #valid at each distance
4979 int nused = 1; // Counts #used at each distance
4980 std::optional<double> real_last_distsq;
4981 std::optional<std::uint64_t> lattice_last_distsq;
4982
4983 // displacements to the kernel range boundary are typically same magnitude (modulo variation)
4984 // estimate the norm of the resulting contributions and skip all if one is too small
4985 if constexpr (std::is_same_v<std::decay_t<decltype(displacements)>,BoxSurfaceDisplacementRange<opdim>>) {
4986 const auto &probing_displacement =
4987 displacements.probing_displacement();
4988 const double opnorm =
4989 op->norm(key.level(), probing_displacement, source);
4990 if (cnorm * opnorm <= tol / fac) {
4991 return {};
4992 }
4993 }
4994
4995 for (const auto& displacement: displacements) {
4996 if (skip_predicate(displacement)) continue;
4997
4998 keyT d;
4999 Key<NDIM - opdim> nullkey(key.level());
5000 MADNESS_ASSERT(op->particle() == 1 || op->particle() == 2);
5001 if (op->particle() == 1)
5002 d = displacement.merge_with(nullkey);
5003 else
5004 d = nullkey.merge_with(displacement);
5005
5006 // Screen out shells. We assume shells are grouped into shells so that the operator decays with shell index.
5007 // Shells are indexed by least distance from box to the central box.
5008 // Cells touching so much as a corner of the central box are further grouped by their lattice distance.
5009 // N.B. lattice-summed decaying kernel is periodic (i.e. does decay w.r.t. r), so loop over shells of displacements sorted by distances modulated by periodicity (Key::distsq_bc)
5010 const auto real_distsq = real_distance_squared(displacement);
5011 const std::uint64_t lattice_distsq = real_distsq ? 0 : lattice_distance_squared(displacement);
5012 if (!real_last_distsq.has_value() ||
5013 !nearlyEqual(real_distsq, *real_last_distsq) || (nearlyEqual(*real_last_distsq, 0) && lattice_distsq != *lattice_last_distsq)) { // Moved to next shell of neighbors
5014 if (nvalid > 0 && nused == 0 && (real_distsq > 0 || lattice_distsq > 1)) {
5015 // Have at least done the input box and all first
5016 // nearest neighbors, and none of the last set
5017 // of neighbors made significant contributions. Thus,
5018 // assuming monotonic decrease, we are done.
5019 break;
5020 }
5021 nused = 0;
5022 nvalid = 0;
5023 real_last_distsq = real_distsq;
5024 // After real_last_distsq > 0, we stop caring about keeping lattice_last_distsq up-to-date.
5025 lattice_last_distsq = real_distsq ? std::optional<std::uint64_t>{} : lattice_distsq;
5026 }
5027
5028 keyT dest = neighbor(key, d, func_is_treated_by_op_as_periodic);
5029 if (dest.is_valid()) {
5030 nvalid++;
5031 const double opnorm = op->norm(key.level(), displacement, source);
5032
5033 if (cnorm * opnorm > tol / fac) {
5034 tensorT result =
5035 op->apply(source, displacement, c, tol / fac / cnorm);
5036 if (result.normf() > 0.3 * tol / fac) {
5037 if (coeffs.is_local(dest))
5038 coeffs.send(dest, &nodeT::accumulate2, result, coeffs,
5039 dest);
5040 else
5041 coeffs.task(dest, &nodeT::accumulate2, result, coeffs,
5042 dest);
5043 nused++;
5044 }
5045 }
5046 }
5047 }
5048
5049 return real_last_distsq;
5050 };
5051
5052 // process "standard" displacements, screening assumes monotonic decay of the kernel
5053 // list of displacements sorted in order of increasing distance
5054 // N.B. if op is lattice-summed use periodic displacements, else use
5055 // non-periodic even if op treats any modes of this as periodic
5056 const std::vector<opkeyT> &disp = op->get_disp(key.level());
5057 const auto max_distsq_reached = for_each(disp, default_real_distance_squared, default_lattice_distance_squared, default_skip_predicate);
5058
5059 // for range-restricted kernels displacements to the boundary of the kernel range also need to be included
5060 // N.B. hard range restriction will result in slow decay of operator matrix elements for the displacements
5061 // to the range boundary, should use soft restriction or sacrifice precision
5062 if (op->range_restricted() && key.level() >= 1) {
5063
5064 std::array<std::optional<std::int64_t>, opdim> box_radius;
5065 std::array<std::optional<std::int64_t>, opdim> surface_thickness;
5066 auto &range = op->get_range();
5067 for (int d = 0; d != opdim; ++d) {
5068 if (range[d]) {
5069 box_radius[d] = range[d].N();
5070 surface_thickness[d] = range[d].finite_soft() ? 1 : 0;
5071 }
5072 }
5073
5075 // skip surface displacements that take us outside of the domain and/or were included in regular displacements
5076 // N.B. for lattice-summed axes the "filter" also maps the displacement back into the simulation cell
5077 if (max_distsq_reached)
5078 validator = BoxSurfaceDisplacementValidator<opdim>(/* is_infinite_domain= */ op->func_domain_is_periodic(), /* is_lattice_summed= */ op->lattice_summed(), range, default_real_distance_squared, *max_distsq_reached);
5079
5080 // this range iterates over the entire surface layer(s), and provides a probing displacement that can be used to screen out the entire box
5081 auto opkey = op->particle() == 1 ? key.template extract_front<opdim>() : key.template extract_front<opdim>();
5083 range_boundary_face_displacements(opkey, box_radius,
5084 surface_thickness,
5085 op->lattice_summed(),
5086 validator);
5087 for_each(
5088 range_boundary_face_displacements,
5089 // surface displacements are not screened, all are included
5090 [](const auto &displacement) -> double { return 0; },
5091 [](const auto &displacement) -> std::uint64_t { return 0; },
5092 default_skip_predicate);
5093 }
5094 }
5095
5096
5097 /// apply an operator on f to return this
5098 template <typename opT, typename R>
5099 void apply(opT& op, const FunctionImpl<R,NDIM>& f, bool fence) {
5101 MADNESS_ASSERT(!op.modified());
5102 for (const auto& [key, node]: f.coeffs) {
5103 if (node.has_coeff()) {
5104 if (node.coeff().dim(0) != k /* i.e. not a leaf */ || op.doleaves) {
5106// woT::task(p, &implT:: template do_apply<opT,R>, &op, key, node.coeff()); //.full_tensor_copy() ????? why copy ????
5107 woT::task(p, &implT:: template do_apply<opT,R>, &op, key, node.coeff().reconstruct_tensor());
5108 }
5109 }
5110 }
5111 if (fence)
5112 world.gop.fence();
5113
5115// this->compressed=true;
5116// this->nonstandard=true;
5117// this->redundant=false;
5118
5119 }
5120
5121
5122
5123 /// apply an operator on the coeffs c (at node key)
5124
5125 /// invoked by result; the result is accumulated inplace to this's tree at various FunctionNodes
5126 /// @param[in] op the operator to act on the source function
5127 /// @param[in] key key of the source FunctionNode of f which is processed (see "source")
5128 /// @param[in] coeff coeffs of FunctionNode being processed
5129 /// @param[in] do_kernel true: do the 0-disp only; false: do everything but the kernel
5130 /// @return max norm, and will modify or include new nodes in this' tree
5131 template <typename opT, typename R>
5132 double do_apply_directed_screening(const opT* op, const keyT& key, const coeffT& coeff,
5133 const bool& do_kernel) {
5135 // insert timer here
5136 typedef typename opT::keyT opkeyT;
5137
5138 // screening: contains all displacement keys that had small result norms
5139 std::list<opkeyT> blacklist;
5140
5141 constexpr auto opdim=opT::opdim;
5142 Key<NDIM-opdim> nullkey(key.level());
5143
5144 // source is that part of key that corresponds to those dimensions being processed
5145 const opkeyT source=op->get_source_key(key);
5146
5147 const double tol = truncate_tol(thresh, key);
5148
5149 // fac is the root of the number of contributing neighbors (1st shell)
5150 double fac=std::pow(3,NDIM*0.5);
5151 double cnorm = coeff.normf();
5152
5153 // for accumulation: keep slightly tighter TensorArgs
5154 TensorArgs apply_targs(targs);
5155 apply_targs.thresh=tol/fac*0.03;
5156
5157 double maxnorm=0.0;
5158
5159 // for the kernel it may be more efficient to do the convolution in full rank
5160 tensorT coeff_full;
5161 // for partial application (exchange operator) it's more efficient to
5162 // do SVD tensors instead of tensortrains, because addition in apply
5163 // can be done in full form for the specific particle
5164 coeffT coeff_SVD=coeff.convert(TensorArgs(-1.0,TT_2D));
5165#ifdef HAVE_GENTENSOR
5166 coeff_SVD.get_svdtensor().orthonormalize(tol*GenTensor<T>::fac_reduce());
5167#endif
5168
5169 // list of displacements sorted in order of increasing distance
5170 // N.B. if op is lattice-summed gives periodic displacements, else uses
5171 // non-periodic even if op treats any modes of this as periodic
5172 const std::vector<opkeyT>& disp = Displacements<opdim>().get_disp(key.level(), op->lattice_summed());
5173
5174 for (const auto& d: disp) {
5175 const int shell=d.distsq_bc(op->lattice_summed());
5176 if (do_kernel and (shell>0)) break;
5177 if ((not do_kernel) and (shell==0)) continue;
5178
5179 keyT disp1;
5180 if (op->particle()==1) disp1=d.merge_with(nullkey);
5181 else if (op->particle()==2) disp1=nullkey.merge_with(d);
5182 else {
5183 MADNESS_EXCEPTION("confused particle in operator??",1);
5184 }
5185
5186 keyT dest = neighbor_in_volume(key, disp1);
5187
5188 if (not dest.is_valid()) continue;
5189
5190 // directed screening
5191 // working assumption here is that the operator is isotropic and
5192 // monotonically decreasing with distance
5193 bool screened=false;
5194 typename std::list<opkeyT>::const_iterator it2;
5195 for (it2=blacklist.begin(); it2!=blacklist.end(); it2++) {
5196 if (d.is_farther_out_than(*it2)) {
5197 screened=true;
5198 break;
5199 }
5200 }
5201 if (not screened) {
5202
5203 double opnorm = op->norm(key.level(), d, source);
5204 double norm=0.0;
5205
5206 if (cnorm*opnorm> tol/fac) {
5207
5208 double cost_ratio=op->estimate_costs(source, d, coeff_SVD, tol/fac/cnorm, tol/fac);
5209 // cost_ratio=1.5; // force low rank
5210 // cost_ratio=0.5; // force full rank
5211
5212 if (cost_ratio>0.0) {
5213
5214 do_op_args<opdim> args(source, d, dest, tol, fac, cnorm);
5215 norm=0.0;
5216 if (cost_ratio<1.0) {
5217 if (not coeff_full.has_data()) coeff_full=coeff.full_tensor_copy();
5218 norm=do_apply_kernel2(op, coeff_full,args,apply_targs);
5219 } else {
5220 if (2*opdim==NDIM) { // apply operator on one particle only
5221 norm=do_apply_kernel3(op,coeff_SVD,args,apply_targs);
5222 } else {
5223 norm=do_apply_kernel3(op,coeff,args,apply_targs);
5224 }
5225 }
5226 maxnorm=std::max(norm,maxnorm);
5227 }
5228
5229 } else if (shell >= 12) {
5230 break; // Assumes monotonic decay beyond nearest neighbor
5231 }
5232 if (norm<0.3*tol/fac) blacklist.push_back(d);
5233 }
5234 }
5235 return maxnorm;
5236 }
5237
5238
5239 /// similar to apply, but for low rank coeffs
5240 template <typename opT, typename R>
5241 void apply_source_driven(opT& op, const FunctionImpl<R,NDIM>& f, bool fence) {
5243
5244 MADNESS_ASSERT(not op.modified());
5245 // looping through all the coefficients of the source f
5246 typename dcT::const_iterator end = f.get_coeffs().end();
5247 for (typename dcT::const_iterator it=f.get_coeffs().begin(); it!=end; ++it) {
5248
5249 const keyT& key = it->first;
5250 const coeffT& coeff = it->second.coeff();
5251
5252 if (coeff.has_data() and (coeff.rank()!=0)) {
5254 woT::task(p, &implT:: template do_apply_directed_screening<opT,R>, &op, key, coeff, true);
5255 woT::task(p, &implT:: template do_apply_directed_screening<opT,R>, &op, key, coeff, false);
5256 }
5257 }
5258 if (fence) world.gop.fence();
5260 }
5261
5262 /// after apply we need to do some cleanup;
5263
5264 /// forces fence
5265 double finalize_apply();
5266
5267 /// after summing up we need to do some cleanup;
5268
5269 /// forces fence
5270 void finalize_sum();
5271
5272 /// traverse a non-existing tree, make its coeffs and apply an operator
5273
5274 /// invoked by result
5275 /// here we use the fact that the hi-dim NS coefficients on all scales are exactly
5276 /// the outer product of the underlying low-dim functions (also in NS form),
5277 /// so we don't need to construct the full hi-dim tree and then turn it into NS form.
5278 /// @param[in] apply_op the operator acting on the NS tree
5279 /// @param[in] fimpl the funcimpl of the function of particle 1
5280 /// @param[in] gimpl the funcimpl of the function of particle 2
5281 template<typename opT, std::size_t LDIM>
5282 void recursive_apply(opT& apply_op, const FunctionImpl<T,LDIM>* fimpl,
5283 const FunctionImpl<T,LDIM>* gimpl, const bool fence) {
5284
5285 //print("IN RECUR2");
5286 const keyT& key0=cdata.key0;
5287
5288 if (world.rank() == coeffs.owner(key0)) {
5289
5290 CoeffTracker<T,LDIM> ff(fimpl);
5291 CoeffTracker<T,LDIM> gg(gimpl);
5292
5293 typedef recursive_apply_op<opT,LDIM> coeff_opT;
5294 coeff_opT coeff_op(this,ff,gg,&apply_op);
5295
5296 typedef noop<T,NDIM> apply_opT;
5297 apply_opT apply_op;
5298
5300 woT::task(p, &implT:: template forward_traverse<coeff_opT,apply_opT>, coeff_op, apply_op, key0);
5301
5302 }
5303 if (fence) world.gop.fence();
5305 }
5306
5307 /// recursive part of recursive_apply
5308 template<typename opT, std::size_t LDIM>
5310 bool randomize() const {return true;}
5311
5313
5318
5319 // ctor
5323 const opT* apply_op) : result(result), iaf(iaf), iag(iag), apply_op(apply_op)
5324 {
5325 MADNESS_ASSERT(LDIM+LDIM==NDIM);
5326 }
5328 iag(other.iag), apply_op(other.apply_op) {}
5329
5330
5331 /// make the NS-coefficients and send off the application of the operator
5332
5333 /// @return a Future<bool,coeffT>(is_leaf,coeffT())
5334 std::pair<bool,coeffT> operator()(const Key<NDIM>& key) const {
5335
5336 // World& world=result->world;
5337 // break key into particles (these are the child keys, with datum1/2 come the parent keys)
5338 Key<LDIM> key1,key2;
5339 key.break_apart(key1,key2);
5340
5341 // the lo-dim functions should be in full tensor form
5342 const tensorT fcoeff=iaf.coeff(key1).full_tensor();
5343 const tensorT gcoeff=iag.coeff(key2).full_tensor();
5344
5345 // would this be a leaf node? If so, then its sum coeffs have already been
5346 // processed by the parent node's wavelet coeffs. Therefore we won't
5347 // process it any more.
5349 bool is_leaf=leaf_op(key,fcoeff,gcoeff);
5350
5351 if (not is_leaf) {
5352 // new coeffs are simply the hartree/kronecker/outer product --
5353 const std::vector<Slice>& s0=iaf.get_impl()->cdata.s0;
5354 const coeffT coeff = (apply_op->modified())
5355 ? outer(copy(fcoeff(s0)),copy(gcoeff(s0)),result->targs)
5356 : outer(fcoeff,gcoeff,result->targs);
5357
5358 // now send off the application
5359 tensorT coeff_full;
5361 double norm0=result->do_apply_directed_screening<opT,T>(apply_op, key, coeff, true);
5362
5363 result->task(p,&implT:: template do_apply_directed_screening<opT,T>,
5364 apply_op,key,coeff,false);
5365
5366 return finalize(norm0,key,coeff);
5367
5368 } else {
5369 return std::pair<bool,coeffT> (is_leaf,coeffT());
5370 }
5371 }
5372
5373 /// sole purpose is to wait for the kernel norm, wrap it and send it back to caller
5374 std::pair<bool,coeffT> finalize(const double kernel_norm, const keyT& key,
5375 const coeffT& coeff) const {
5376 const double thresh=result->get_thresh()*0.1;
5377 bool is_leaf=(kernel_norm<result->truncate_tol(thresh,key));
5378 if (key.level()<2) is_leaf=false;
5379 return std::pair<bool,coeffT> (is_leaf,coeff);
5380 }
5381
5382
5383 this_type make_child(const keyT& child) const {
5384
5385 // break key into particles
5386 Key<LDIM> key1, key2;
5387 child.break_apart(key1,key2);
5388
5389 return this_type(result,iaf.make_child(key1),iag.make_child(key2),apply_op);
5390 }
5391
5395 return result->world.taskq.add(detail::wrap_mem_fn(*const_cast<this_type *> (this),
5396 &this_type::forward_ctor),result,f1,g1,apply_op);
5397 }
5398
5400 const opT* apply_op1) {
5401 return this_type(r,f1,g1,apply_op1);
5402 }
5403
5404 template <typename Archive> void serialize(const Archive& ar) {
5405 ar & result & iaf & iag & apply_op;
5406 }
5407 };
5408
5409 /// traverse an existing tree and apply an operator
5410
5411 /// invoked by result
5412 /// @param[in] apply_op the operator acting on the NS tree
5413 /// @param[in] fimpl the funcimpl of the source function
5414 /// @param[in] rimpl a dummy function for recursive_op to insert data
5415 template<typename opT>
5416 void recursive_apply(opT& apply_op, const implT* fimpl, implT* rimpl, const bool fence) {
5417
5418 print("IN RECUR1");
5419
5420 const keyT& key0=cdata.key0;
5421
5422 if (world.rank() == coeffs.owner(key0)) {
5423
5424 typedef recursive_apply_op2<opT> coeff_opT;
5425 coeff_opT coeff_op(this,fimpl,&apply_op);
5426
5427 typedef noop<T,NDIM> apply_opT;
5428 apply_opT apply_op;
5429
5430 woT::task(world.rank(), &implT:: template forward_traverse<coeff_opT,apply_opT>,
5431 coeff_op, apply_op, cdata.key0);
5432
5433 }
5434 if (fence) world.gop.fence();
5436 }
5437
5438 /// recursive part of recursive_apply
5439 template<typename opT>
5441 bool randomize() const {return true;}
5442
5445 typedef std::pair<bool,coeffT> argT;
5446
5447 mutable implT* result;
5448 ctT iaf; /// need this for randomization
5449 const opT* apply_op;
5450
5451 // ctor
5455
5457 iaf(other.iaf), apply_op(other.apply_op) {}
5458
5459
5460 /// send off the application of the operator
5461
5462 /// the first (core) neighbor (ie. the box itself) is processed
5463 /// immediately, all other ones are shoved into the taskq
5464 /// @return a pair<bool,coeffT>(is_leaf,coeffT())
5465 argT operator()(const Key<NDIM>& key) const {
5466
5467 const coeffT& coeff=iaf.coeff();
5468
5469 if (coeff.has_data()) {
5470
5471 // now send off the application for all neighbor boxes
5473 result->task(p,&implT:: template do_apply_directed_screening<opT,T>,
5474 apply_op, key, coeff, false);
5475
5476 // process the core box
5477 double norm0=result->do_apply_directed_screening<opT,T>(apply_op,key,coeff,true);
5478
5479 if (iaf.is_leaf()) return argT(true,coeff);
5480 return finalize(norm0,key,coeff,result);
5481
5482 } else {
5483 const bool is_leaf=true;
5484 return argT(is_leaf,coeffT());
5485 }
5486 }
5487
5488 /// sole purpose is to wait for the kernel norm, wrap it and send it back to caller
5489 argT finalize(const double kernel_norm, const keyT& key,
5490 const coeffT& coeff, const implT* r) const {
5491 const double thresh=r->get_thresh()*0.1;
5492 bool is_leaf=(kernel_norm<r->truncate_tol(thresh,key));
5493 if (key.level()<2) is_leaf=false;
5494 return argT(is_leaf,coeff);
5495 }
5496
5497
5498 this_type make_child(const keyT& child) const {
5499 return this_type(result,iaf.make_child(child),apply_op);
5500 }
5501
5502 /// retrieve the coefficients (parent coeffs might be remote)
5504 Future<ctT> f1=iaf.activate();
5505
5506// Future<ctL> g1=g.activate();
5507// return h->world.taskq.add(detail::wrap_mem_fn(*const_cast<this_type *> (this),
5508// &this_type::forward_ctor),h,f1,g1,particle);
5509
5510 return result->world.taskq.add(detail::wrap_mem_fn(*const_cast<this_type *> (this),
5511 &this_type::forward_ctor),result,f1,apply_op);
5512 }
5513
5514 /// taskq-compatible ctor
5515 this_type forward_ctor(implT* result1, const ctT& iaf1, const opT* apply_op1) {
5516 return this_type(result1,iaf1,apply_op1);
5517 }
5518
5519 template <typename Archive> void serialize(const Archive& ar) {
5520 ar & result & iaf & apply_op;
5521 }
5522 };
5523
5524 /// Returns the square of the error norm in the box labeled by key
5525
5526 /// Assumed to be invoked locally but it would be easy to eliminate
5527 /// this assumption
5528 template <typename opT>
5529 double err_box(const keyT& key, const nodeT& node, const opT& func,
5530 int npt, const Tensor<double>& qx, const Tensor<double>& quad_phit,
5531 const Tensor<double>& quad_phiw) const {
5532
5533 std::vector<long> vq(NDIM);
5534 for (std::size_t i=0; i<NDIM; ++i)
5535 vq[i] = npt;
5536 tensorT fval(vq,false), work(vq,false), result(vq,false);
5537
5538 // Compute the "exact" function in this volume at npt points
5539 // where npt is usually this->npt+1.
5540 fcube(key, func, qx, fval);
5541
5542 // Transform into the scaling function basis of order npt
5543 double scale = pow(0.5,0.5*NDIM*key.level())*sqrt(FunctionDefaults<NDIM>::get_cell_volume());
5544 fval = fast_transform(fval,quad_phiw,result,work).scale(scale);
5545
5546 // Subtract to get the error ... the original coeffs are in the order k
5547 // basis but we just computed the coeffs in the order npt(=k+1) basis
5548 // so we can either use slices or an iterator macro.
5549 const tensorT coeff = node.coeff().full_tensor();
5550 ITERATOR(coeff,fval(IND)-=coeff(IND););
5551 // flo note: we do want to keep a full tensor here!
5552
5553 // Compute the norm of what remains
5554 double err = fval.normf();
5555 return err*err;
5556 }
5557
5558 template <typename opT>
5560 const implT* impl;
5561 const opT* func;
5562 int npt;
5566 public:
5567 do_err_box() = default;
5568
5572
5575
5576 double operator()(typename dcT::const_iterator& it) const {
5577 const keyT& key = it->first;
5578 const nodeT& node = it->second;
5579 if (node.has_coeff())
5580 return impl->err_box(key, node, *func, npt, qx, quad_phit, quad_phiw);
5581 else
5582 return 0.0;
5583 }
5584
5585 double operator()(double a, double b) const {
5586 return a+b;
5587 }
5588
5589 template <typename Archive>
5590 void serialize(const Archive& ar) {
5591 MADNESS_EXCEPTION("not yet", 1);
5592 }
5593 };
5594
5595 /// Returns the sum of squares of errors from local info ... no comms
5596 template <typename opT>
5597 double errsq_local(const opT& func) const {
5599 // Make quadrature rule of higher order
5600 const int npt = cdata.npt + 1;
5601 Tensor<double> qx, qw, quad_phi, quad_phiw, quad_phit;
5602 FunctionCommonData<T,NDIM>::_init_quadrature(k+1, npt, qx, qw, quad_phi, quad_phiw, quad_phit);
5603
5606 return world.taskq.reduce< double,rangeT,do_err_box<opT> >(range,
5607 do_err_box<opT>(this, &func, npt, qx, quad_phit, quad_phiw));
5608 }
5609
5610 /// Returns \c int(f(x),x) in local volume
5611 T trace_local() const;
5612
5614 double operator()(typename dcT::const_iterator& it) const {
5615 const nodeT& node = it->second;
5616 if (node.has_coeff()) {
5617 double norm = node.coeff().normf();
5618 return norm*norm;
5619 }
5620 else {
5621 return 0.0;
5622 }
5623 }
5624
5625 double operator()(double a, double b) const {
5626 return (a+b);
5627 }
5628
5629 template <typename Archive> void serialize(const Archive& ar) {
5630 MADNESS_EXCEPTION("NOT IMPLEMENTED", 1);
5631 }
5632 };
5633
5634
5635 /// Returns the square of the local norm ... no comms
5636 double norm2sq_local() const;
5637
5638 /// compute the inner product of this range with other
5639 template<typename R>
5643 typedef TENSOR_RESULT_TYPE(T,R) resultT;
5644
5647 resultT operator()(typename dcT::const_iterator& it) const {
5648
5649 TENSOR_RESULT_TYPE(T,R) sum=0.0;
5650 const keyT& key=it->first;
5651 const nodeT& fnode = it->second;
5652 if (fnode.has_coeff()) {
5653 if (other->coeffs.probe(it->first)) {
5654 const FunctionNode<R,NDIM>& gnode = other->coeffs.find(key).get()->second;
5655 if (gnode.has_coeff()) {
5656 if (gnode.coeff().dim(0) != fnode.coeff().dim(0)) {
5657 madness::print("INNER", it->first, gnode.coeff().dim(0),fnode.coeff().dim(0));
5658 MADNESS_EXCEPTION("functions have different k or compress/reconstruct error", 0);
5659 }
5660 if (leaves_only) {
5661 if (gnode.is_leaf() or fnode.is_leaf()) {
5662 sum += fnode.coeff().trace_conj(gnode.coeff());
5663 }
5664 } else {
5665 sum += fnode.coeff().trace_conj(gnode.coeff());
5666 }
5667 }
5668 }
5669 }
5670 return sum;
5671 }
5672
5673 resultT operator()(resultT a, resultT b) const {
5674 return (a+b);
5675 }
5676
5677 template <typename Archive> void serialize(const Archive& ar) {
5678 MADNESS_EXCEPTION("NOT IMPLEMENTED", 1);
5679 }
5680 };
5681
5682 /// Returns the inner product ASSUMING same distribution
5683
5684 /// handles compressed and redundant form
5685 template <typename R>
5689 typedef TENSOR_RESULT_TYPE(T,R) resultT;
5690
5691 // make sure the states of the trees are consistent
5694 return world.taskq.reduce<resultT,rangeT,do_inner_local<R> >
5696 }
5697
5698
5699 /// compute the inner product of this range with other
5700 template<typename R>
5704 bool leaves_only=true;
5705 typedef TENSOR_RESULT_TYPE(T,R) resultT;
5706
5710 resultT operator()(typename dcT::const_iterator& it) const {
5711
5712 constexpr std::size_t LDIM=std::max(NDIM/2,std::size_t(1));
5713
5714 const keyT& key=it->first;
5715 const nodeT& fnode = it->second;
5716 if (not fnode.has_coeff()) return resultT(0.0); // probably internal nodes
5717
5718 // assuming all boxes (esp the low-dim ones) are local, i.e. the functions are replicated
5719 auto find_valid_parent = [](auto& key, auto& impl, auto&& find_valid_parent) {
5720 MADNESS_CHECK(impl->get_coeffs().owner(key)==impl->world.rank()); // make sure everything is local!
5721 if (impl->get_coeffs().probe(key)) return key;
5722 auto parentkey=key.parent();
5723 return find_valid_parent(parentkey, impl, find_valid_parent);
5724 };
5725
5726 // returns coefficients, empty if no functor present
5727 auto get_coeff = [&find_valid_parent](const auto& key, const auto& v_impl) {
5728 if ((v_impl.size()>0) and v_impl.front().get()) {
5729 auto impl=v_impl.front();
5730
5731// bool have_impl=impl.get();
5732// if (have_impl) {
5733 auto parentkey = find_valid_parent(key, impl, find_valid_parent);
5734 MADNESS_CHECK(impl->get_coeffs().probe(parentkey));
5735 typename decltype(impl->coeffs)::accessor acc;
5736 impl->get_coeffs().find(acc,parentkey);
5737 auto parentcoeff=acc->second.coeff();
5738 auto coeff=impl->parent_to_child(parentcoeff, parentkey, key);
5739 return coeff;
5740 } else {
5741 // get type of vector elements
5742 typedef typename std::decay_t<decltype(v_impl)>::value_type::element_type::typeT S;
5743// typedef typename std::decay_t<decltype(v_impl)>::value_type S;
5744 return GenTensor<S>();
5745// return GenTensor<typename std::decay_t<decltype(*impl)>::typeT>();
5746 }
5747 };
5748
5749 auto make_vector = [](auto& arg) {
5750 return std::vector<std::decay_t<decltype(arg)>>(1,arg);
5751 };
5752
5753
5754 Key<LDIM> key1,key2;
5755 key.break_apart(key1,key2);
5756
5757 auto func=dynamic_cast<CompositeFunctorInterface<R,NDIM,LDIM>* >(ket->functor.get());
5759
5760 MADNESS_CHECK_THROW(func->impl_ket_vector.size()==0 or func->impl_ket_vector.size()==1,
5761 "only one ket function supported in inner_on_demand");
5762 MADNESS_CHECK_THROW(func->impl_p1_vector.size()==0 or func->impl_p1_vector.size()==1,
5763 "only one p1 function supported in inner_on_demand");
5764 MADNESS_CHECK_THROW(func->impl_p2_vector.size()==0 or func->impl_p2_vector.size()==1,
5765 "only one p2 function supported in inner_on_demand");
5766 auto coeff_bra=fnode.coeff();
5767 auto coeff_ket=get_coeff(key,func->impl_ket_vector);
5768 auto coeff_v1=get_coeff(key1,make_vector(func->impl_m1));
5769 auto coeff_v2=get_coeff(key2,make_vector(func->impl_m2));
5770 auto coeff_p1=get_coeff(key1,func->impl_p1_vector);
5771 auto coeff_p2=get_coeff(key2,func->impl_p2_vector);
5772
5773 // construct |ket(1,2)> or |p(1)p(2)> or |p(1)p(2) ket(1,2)>
5774 double error=0.0;
5775 if (coeff_ket.has_data() and coeff_p1.has_data()) {
5776 pointwise_multiplier<LDIM> pm(key,coeff_ket);
5777 coeff_ket=pm(key,outer(coeff_p1,coeff_p2,TensorArgs(TT_FULL,-1.0)).full_tensor());
5778 error+=pm.error;
5779 } else if (coeff_ket.has_data() or coeff_p1.has_data()) {
5780 coeff_ket = (coeff_ket.has_data()) ? coeff_ket : outer(coeff_p1,coeff_p2);
5781 } else { // not ket and no p1p2
5782 MADNESS_EXCEPTION("confused ket/p1p2 in do_inner_local_on_demand",1);
5783 }
5784
5785 // construct (v(1) + v(2)) |ket(1,2)>
5786 coeffT v1v2ket;
5787 if (coeff_v1.has_data()) {
5788 pointwise_multiplier<LDIM> pm(key,coeff_ket);
5789 v1v2ket = pm(key,coeff_v1.full_tensor(), 1);
5790 error+=pm.error;
5791 v1v2ket+= pm(key,coeff_v2.full_tensor(), 2);
5792 error+=pm.error;
5793 } else {
5794 v1v2ket = coeff_ket;
5795 }
5796
5797 resultT result;
5798 if (func->impl_eri) { // project bra*ket onto eri, avoid multiplication with eri
5799 MADNESS_CHECK(func->impl_eri->get_functor()->provides_coeff());
5800 coeffT coeff_eri=func->impl_eri->get_functor()->coeff(key).full_tensor();
5801 pointwise_multiplier<LDIM> pm(key,v1v2ket);
5802 tensorT braket=pm(key,coeff_bra.full_tensor_copy().conj());
5803 error+=pm.error;
5804 if (error>1.e-3) print("error in key",key,error);
5805 result=coeff_eri.full_tensor().trace(braket);
5806
5807 } else { // no eri, project ket onto bra
5808 result=coeff_bra.full_tensor_copy().trace_conj(v1v2ket.full_tensor_copy());
5809 }
5810 return result;
5811 }
5812
5813 resultT operator()(resultT a, resultT b) const {
5814 return (a+b);
5815 }
5816
5817 template <typename Archive> void serialize(const Archive& ar) {
5818 MADNESS_EXCEPTION("NOT IMPLEMENTED", 1);
5819 }
5820 };
5821
5822 /// Returns the inner product of this with function g constructed on-the-fly
5823
5824 /// the leaf boxes of this' MRA tree defines the inner product
5825 template <typename R>
5826 TENSOR_RESULT_TYPE(T,R) inner_local_on_demand(const FunctionImpl<R,NDIM>& gimpl) const {
5829
5833 do_inner_local_on_demand<R>(this, &gimpl));
5834 }
5835
5836 /// compute the inner product of this range with other
5837 template<typename R>
5841 typedef TENSOR_RESULT_TYPE(T,R) resultT;
5842
5845 resultT operator()(typename dcT::const_iterator& it) const {
5846
5847 TENSOR_RESULT_TYPE(T,R) sum=0.0;
5848 const keyT& key=it->first;
5849 const nodeT& fnode = it->second;
5850 if (fnode.has_coeff()) {
5851 if (other->coeffs.probe(it->first)) {
5852 const FunctionNode<R,NDIM>& gnode = other->coeffs.find(key).get()->second;
5853 if (gnode.has_coeff()) {
5854 if (gnode.coeff().dim(0) != fnode.coeff().dim(0)) {
5855 madness::print("DOT", it->first, gnode.coeff().dim(0),fnode.coeff().dim(0));
5856 MADNESS_EXCEPTION("functions have different k or compress/reconstruct error", 0);
5857 }
5858 if (leaves_only) {
5859 if (gnode.is_leaf() or fnode.is_leaf()) {
5860 sum += fnode.coeff().full_tensor().trace(gnode.coeff().full_tensor());
5861 }
5862 } else {
5863 sum += fnode.coeff().full_tensor().trace(gnode.coeff().full_tensor());
5864 }
5865 }
5866 }
5867 }
5868 return sum;
5869 }
5870
5871 resultT operator()(resultT a, resultT b) const {
5872 return (a+b);
5873 }
5874
5875 template <typename Archive> void serialize(const Archive& ar) {
5876 MADNESS_EXCEPTION("NOT IMPLEMENTED", 1);
5877 }
5878 };
5879
5880 /// Returns the dot product ASSUMING same distribution
5881
5882 /// handles compressed and redundant form
5883 template <typename R>
5887 typedef TENSOR_RESULT_TYPE(T,R) resultT;
5888
5889 // make sure the states of the trees are consistent
5891 bool leaves_only=(this->is_redundant());
5892 return world.taskq.reduce<resultT,rangeT,do_dot_local<R> >
5894 }
5895
5896 /// Type of the entry in the map returned by make_key_vec_map
5897 typedef std::vector< std::pair<int,const coeffT*> > mapvecT;
5898
5899 /// Type of the map returned by make_key_vec_map
5901
5902 /// Adds keys to union of local keys with specified index
5903 void add_keys_to_map(mapT* map, int index) const {
5904 typename dcT::const_iterator end = coeffs.end();
5905 for (typename dcT::const_iterator it=coeffs.begin(); it!=end; ++it) {
5906 typename mapT::accessor acc;
5907 const keyT& key = it->first;
5908 const FunctionNode<T,NDIM>& node = it->second;
5909 if (node.has_coeff()) {
5910 [[maybe_unused]] auto inserted = map->insert(acc,key);
5911 acc->second.push_back(std::make_pair(index,&(node.coeff())));
5912 }
5913 }
5914 }
5915
5916 /// Returns map of union of local keys to vector of indexes of functions containing that key
5917
5918 /// Local concurrency and synchronization only; no communication
5919 static
5920 mapT
5921 make_key_vec_map(const std::vector<const FunctionImpl<T,NDIM>*>& v) {
5922 mapT map(100000);
5923 // This loop must be parallelized
5924 for (unsigned int i=0; i<v.size(); i++) {
5925 //v[i]->add_keys_to_map(&map,i);
5926 v[i]->world.taskq.add(*(v[i]), &FunctionImpl<T,NDIM>::add_keys_to_map, &map, int(i));
5927 }
5928 if (v.size()) v[0]->world.taskq.fence();
5929 return map;
5930 }
5931
5932#if 0
5933// Original
5934 template <typename R>
5935 static void do_inner_localX(const typename mapT::iterator lstart,
5936 const typename mapT::iterator lend,
5937 typename FunctionImpl<R,NDIM>::mapT* rmap_ptr,
5938 const bool sym,
5939 Tensor< TENSOR_RESULT_TYPE(T,R) >* result_ptr,
5940 Mutex* mutex) {
5941 Tensor< TENSOR_RESULT_TYPE(T,R) >& result = *result_ptr;
5942 Tensor< TENSOR_RESULT_TYPE(T,R) > r(result.dim(0),result.dim(1));
5943 for (typename mapT::iterator lit=lstart; lit!=lend; ++lit) {
5944 const keyT& key = lit->first;
5945 typename FunctionImpl<R,NDIM>::mapT::iterator rit=rmap_ptr->find(key);
5946 if (rit != rmap_ptr->end()) {
5947 const mapvecT& leftv = lit->second;
5948 const typename FunctionImpl<R,NDIM>::mapvecT& rightv =rit->second;
5949 const int nleft = leftv.size();
5950 const int nright= rightv.size();
5951
5952 for (int iv=0; iv<nleft; iv++) {
5953 const int i = leftv[iv].first;
5954 const GenTensor<T>* iptr = leftv[iv].second;
5955
5956 for (int jv=0; jv<nright; jv++) {
5957 const int j = rightv[jv].first;
5958 const GenTensor<R>* jptr = rightv[jv].second;
5959
5960 if (!sym || (sym && i<=j))
5961 r(i,j) += iptr->trace_conj(*jptr);
5962 }
5963 }
5964 }
5965 }
5966 mutex->lock();
5967 result += r;
5968 mutex->unlock();
5969 }
5970#else
5971 template <typename R>
5972 static void do_inner_localX(const typename mapT::iterator lstart,
5973 const typename mapT::iterator lend,
5974 typename FunctionImpl<R,NDIM>::mapT* rmap_ptr,
5975 const bool sym,
5976 Tensor< TENSOR_RESULT_TYPE(T,R) >* result_ptr,
5977 Mutex* mutex) {
5978 Tensor< TENSOR_RESULT_TYPE(T,R) >& result = *result_ptr;
5979 //Tensor< TENSOR_RESULT_TYPE(T,R) > r(result.dim(0),result.dim(1));
5980 for (typename mapT::iterator lit=lstart; lit!=lend; ++lit) {
5981 const keyT& key = lit->first;
5982 typename FunctionImpl<R,NDIM>::mapT::iterator rit=rmap_ptr->find(key);
5983 if (rit != rmap_ptr->end()) {
5984 const mapvecT& leftv = lit->second;
5985 const typename FunctionImpl<R,NDIM>::mapvecT& rightv =rit->second;
5986 const size_t nleft = leftv.size();
5987 const size_t nright= rightv.size();
5988
5989 unsigned int size = leftv[0].second->size();
5990 Tensor<T> Left(nleft, size);
5991 Tensor<R> Right(nright, size);
5992 Tensor< TENSOR_RESULT_TYPE(T,R)> r(nleft, nright);
5993 for(unsigned int iv = 0; iv < nleft; ++iv) Left(iv,_) = (*(leftv[iv].second)).full_tensor();
5994 for(unsigned int jv = 0; jv < nright; ++jv) Right(jv,_) = (*(rightv[jv].second)).full_tensor();
5995 // call mxmT from mxm.h in tensor
5996 if(TensorTypeData<T>::iscomplex) Left = Left.conj(); // Should handle complex case and leave real case alone
5997 mxmT(nleft, nright, size, r.ptr(), Left.ptr(), Right.ptr());
5998 mutex->lock();
5999 for(unsigned int iv = 0; iv < nleft; ++iv) {
6000 const int i = leftv[iv].first;
6001 for(unsigned int jv = 0; jv < nright; ++jv) {
6002 const int j = rightv[jv].first;
6003 if (!sym || (sym && i<=j)) result(i,j) += r(iv,jv);
6004 }
6005 }
6006 mutex->unlock();
6007 }
6008 }
6009 }
6010#endif
6011
6012#if 0
6013// Original
6014 template <typename R, typename = std::enable_if_t<std::is_floating_point_v<R>>>
6015 static void do_dot_localX(const typename mapT::iterator lstart,
6016 const typename mapT::iterator lend,
6017 typename FunctionImpl<R, NDIM>::mapT* rmap_ptr,
6018 const bool sym,
6019 Tensor<TENSOR_RESULT_TYPE(T, R)>* result_ptr,
6020 Mutex* mutex) {
6021 if (TensorTypeData<T>::iscomplex) MADNESS_EXCEPTION("no complex trace in LowRankTensor, sorry", 1);
6022 Tensor<TENSOR_RESULT_TYPE(T, R)>& result = *result_ptr;
6023 Tensor<TENSOR_RESULT_TYPE(T, R)> r(result.dim(0), result.dim(1));
6024 for (typename mapT::iterator lit = lstart; lit != lend; ++lit) {
6025 const keyT& key = lit->first;
6026 typename FunctionImpl<R, NDIM>::mapT::iterator rit = rmap_ptr->find(key);
6027 if (rit != rmap_ptr->end()) {
6028 const mapvecT& leftv = lit->second;
6029 const typename FunctionImpl<R, NDIM>::mapvecT& rightv = rit->second;
6030 const int nleft = leftv.size();
6031 const int nright = rightv.size();
6032
6033 for (int iv = 0; iv < nleft; iv++) {
6034 const int i = leftv[iv].first;
6035 const GenTensor<T>* iptr = leftv[iv].second;
6036
6037 for (int jv = 0; jv < nright; jv++) {
6038 const int j = rightv[jv].first;
6039 const GenTensor<R>* jptr = rightv[jv].second;
6040
6041 if (!sym || (sym && i <= j))
6042 r(i, j) += iptr->trace_conj(*jptr);
6043 }
6044 }
6045 }
6046 }
6047 mutex->lock();
6048 result += r;
6049 mutex->unlock();
6050 }
6051#else
6052 template <typename R>
6053 static void do_dot_localX(const typename mapT::iterator lstart,
6054 const typename mapT::iterator lend,
6055 typename FunctionImpl<R, NDIM>::mapT* rmap_ptr,
6056 const bool sym,
6057 Tensor<TENSOR_RESULT_TYPE(T, R)>* result_ptr,
6058 Mutex* mutex) {
6059 Tensor<TENSOR_RESULT_TYPE(T, R)>& result = *result_ptr;
6060 // Tensor<TENSOR_RESULT_TYPE(T, R)> r(result.dim(0), result.dim(1));
6061 for (typename mapT::iterator lit = lstart; lit != lend; ++lit) {
6062 const keyT& key = lit->first;
6063 typename FunctionImpl<R, NDIM>::mapT::iterator rit = rmap_ptr->find(key);
6064 if (rit != rmap_ptr->end()) {
6065 const mapvecT& leftv = lit->second;
6066 const typename FunctionImpl<R, NDIM>::mapvecT& rightv = rit->second;
6067 const size_t nleft = leftv.size();
6068 const size_t nright= rightv.size();
6069
6070 unsigned int size = leftv[0].second->size();
6071 Tensor<T> Left(nleft, size);
6072 Tensor<R> Right(nright, size);
6073 Tensor< TENSOR_RESULT_TYPE(T, R)> r(nleft, nright);
6074 for(unsigned int iv = 0; iv < nleft; ++iv) Left(iv, _) = (*(leftv[iv].second)).full_tensor();
6075 for(unsigned int jv = 0; jv < nright; ++jv) Right(jv, _) = (*(rightv[jv].second)).full_tensor();
6076 // call mxmT from mxm.h in tensor
6077 mxmT(nleft, nright, size, r.ptr(), Left.ptr(), Right.ptr());
6078 mutex->lock();
6079 for(unsigned int iv = 0; iv < nleft; ++iv) {
6080 const int i = leftv[iv].first;
6081 for(unsigned int jv = 0; jv < nright; ++jv) {
6082 const int j = rightv[jv].first;
6083 if (!sym || (sym && i <= j)) result(i, j) += r(iv, jv);
6084 }
6085 }
6086 mutex->unlock();
6087 }
6088 }
6089 }
6090#endif
6091
6092 template <typename Real>
6093 static std::enable_if_t<std::is_floating_point_v<Real>, Real> conj(const Real x) {
6094 return x;
6095 }
6096
6097 template <typename Real>
6098 static std::complex<Real> conj(const std::complex<Real>& x) {
6099 return std::conj(x);
6100 }
6101
6102 template <typename R>
6103 static Tensor< TENSOR_RESULT_TYPE(T,R) >
6104 inner_local(const std::vector<const FunctionImpl<T,NDIM>*>& left,
6105 const std::vector<const FunctionImpl<R,NDIM>*>& right,
6106 bool sym) {
6107
6108 // This is basically a sparse matrix^T * matrix product
6109 // Rij = sum(k) Aki * Bkj
6110 // where i and j index functions and k index the wavelet coeffs
6111 // eventually the goal is this structure (don't have jtile yet)
6112 //
6113 // do in parallel tiles of k (tensors of coeffs)
6114 // do tiles of j
6115 // do i
6116 // do j in jtile
6117 // do k in ktile
6118 // Rij += Aki*Bkj
6119
6120 mapT lmap = make_key_vec_map(left);
6121 typename FunctionImpl<R,NDIM>::mapT rmap;
6122 auto* rmap_ptr = (typename FunctionImpl<R,NDIM>::mapT*)(&lmap);
6123 if ((std::vector<const FunctionImpl<R,NDIM>*>*)(&left) != &right) {
6125 rmap_ptr = &rmap;
6126 }
6127
6128 size_t chunk = (lmap.size()-1)/(3*4*5)+1;
6129
6130 Tensor< TENSOR_RESULT_TYPE(T,R) > r(left.size(), right.size());
6131 Mutex mutex;
6132
6133 typename mapT::iterator lstart=lmap.begin();
6134 while (lstart != lmap.end()) {
6135 typename mapT::iterator lend = lstart;
6136 advance(lend,chunk);
6137 left[0]->world.taskq.add(&FunctionImpl<T,NDIM>::do_inner_localX<R>, lstart, lend, rmap_ptr, sym, &r, &mutex);
6138 lstart = lend;
6139 }
6140 left[0]->world.taskq.fence();
6141
6142 if (sym) {
6143 for (long i=0; i<r.dim(0); i++) {
6144 for (long j=0; j<i; j++) {
6145 TENSOR_RESULT_TYPE(T,R) sum = r(i,j)+conj(r(j,i));
6146 r(i,j) = sum;
6147 r(j,i) = conj(sum);
6148 }
6149 }
6150 }
6151 return r;
6152 }
6153
6154 template <typename R>
6155 static Tensor<TENSOR_RESULT_TYPE(T, R)>
6156 dot_local(const std::vector<const FunctionImpl<T, NDIM>*>& left,
6157 const std::vector<const FunctionImpl<R, NDIM>*>& right,
6158 bool sym) {
6159
6160 // This is basically a sparse matrix * matrix product
6161 // Rij = sum(k) Aik * Bkj
6162 // where i and j index functions and k index the wavelet coeffs
6163 // eventually the goal is this structure (don't have jtile yet)
6164 //
6165 // do in parallel tiles of k (tensors of coeffs)
6166 // do tiles of j
6167 // do i
6168 // do j in jtile
6169 // do k in ktile
6170 // Rij += Aik*Bkj
6171
6172 mapT lmap = make_key_vec_map(left);
6173 typename FunctionImpl<R, NDIM>::mapT rmap;
6174 auto* rmap_ptr = (typename FunctionImpl<R, NDIM>::mapT*)(&lmap);
6175 if ((std::vector<const FunctionImpl<R, NDIM>*>*)(&left) != &right) {
6177 rmap_ptr = &rmap;
6178 }
6179
6180 size_t chunk = (lmap.size() - 1) / (3 * 4 * 5) + 1;
6181
6182 Tensor<TENSOR_RESULT_TYPE(T, R)> r(left.size(), right.size());
6183 Mutex mutex;
6184
6185 typename mapT::iterator lstart=lmap.begin();
6186 while (lstart != lmap.end()) {
6187 typename mapT::iterator lend = lstart;
6188 advance(lend, chunk);
6189 left[0]->world.taskq.add(&FunctionImpl<T, NDIM>::do_dot_localX<R>, lstart, lend, rmap_ptr, sym, &r, &mutex);
6190 lstart = lend;
6191 }
6192 left[0]->world.taskq.fence();
6193
6194 // sym is for hermiticity
6195 if (sym) {
6196 for (long i = 0; i < r.dim(0); i++) {
6197 for (long j = 0; j < i; j++) {
6198 TENSOR_RESULT_TYPE(T, R) sum = r(i, j) + conj(r(j, i));
6199 r(i, j) = sum;
6200 r(j, i) = conj(sum);
6201 }
6202 }
6203 }
6204 return r;
6205 }
6206
6207 template <typename R>
6209 {
6210 static_assert(!std::is_same<R, int>::value &&
6211 std::is_same<R, int>::value,
6212 "Compilation failed because you wanted to know the type; see below:");
6213 }
6214
6215 /// invoked by result
6216
6217 /// contract 2 functions f(x,z) = \int g(x,y) * h(y,z) dy
6218 /// @tparam CDIM: the dimension of the contraction variable (y)
6219 /// @tparam NDIM: the dimension of the result (x,z)
6220 /// @tparam LDIM: the dimension of g(x,y)
6221 /// @tparam KDIM: the dimension of h(y,z)
6222 template<typename Q, std::size_t LDIM, typename R, std::size_t KDIM,
6223 std::size_t CDIM = (KDIM + LDIM - NDIM) / 2>
6225 const std::array<int, CDIM> v1, const std::array<int, CDIM> v2) {
6226
6227 typedef std::multimap<Key<NDIM>, std::list<Key<CDIM>>> contractionmapT;
6228 //double wall_get_lists=0.0;
6229 //double wall_recur=0.0;
6230 //double wall_contract=0.0;
6233
6234 // auto print_map = [](const auto& map) {
6235 // for (const auto& kv : map) print(kv.first,"--",kv.second);
6236 // };
6237 // logical constness, not bitwise constness
6238 FunctionImpl<Q,LDIM>& g_nc=const_cast<FunctionImpl<Q,LDIM>&>(g);
6239 FunctionImpl<R,KDIM>& h_nc=const_cast<FunctionImpl<R,KDIM>&>(h);
6240
6241 std::list<contractionmapT> all_contraction_maps;
6242 for (std::size_t n=0; n<nmax; ++n) {
6243
6244 // list of nodes with d coefficients (and their parents)
6245 //double wall0 = wall_time();
6246 auto [g_ijlist, g_jlist] = g.get_contraction_node_lists(n, v1);
6247 auto [h_ijlist, h_jlist] = h.get_contraction_node_lists(n, v2);
6248 if ((g_ijlist.size() == 0) and (h_ijlist.size() == 0)) break;
6249 //double wall1 = wall_time();
6250 //wall_get_lists += (wall1 - wall0);
6251 //wall0 = wall1;
6252// print("g_jlist");
6253// for (const auto& kv : g_jlist) print(kv.first,kv.second);
6254// print("h_jlist");
6255// for (const auto& kv : h_jlist) print(kv.first,kv.second);
6256
6257 // next lines will insert s nodes into g and h -> possible race condition!
6258 bool this_first = true; // are the remaining indices of g before those of g: f(x,z) = g(x,y) h(y,z)
6259 // CDIM, NDIM, KDIM
6260 contractionmapT contraction_map = g_nc.recur_down_for_contraction_map(
6261 g_nc.key0(), g_nc.get_coeffs().find(g_nc.key0()).get()->second, v1, v2,
6262 h_ijlist, h_jlist, this_first, thresh);
6263
6264 this_first = false;
6265 // CDIM, NDIM, LDIM
6266 auto hnode0=h_nc.get_coeffs().find(h_nc.key0()).get()->second;
6267 contractionmapT contraction_map1 = h_nc.recur_down_for_contraction_map(
6268 h_nc.key0(), hnode0, v2, v1,
6269 g_ijlist, g_jlist, this_first, thresh);
6270
6271 // will contain duplicate entries
6272 contraction_map.merge(contraction_map1);
6273 // turn multimap into a map of list
6274 auto it = contraction_map.begin();
6275 while (it != contraction_map.end()) {
6276 auto it_end = contraction_map.upper_bound(it->first);
6277 auto it2 = it;
6278 it2++;
6279 while (it2 != it_end) {
6280 it->second.splice(it->second.end(), it2->second);
6281 it2 = contraction_map.erase(it2);
6282 }
6283 it = it_end;
6284 }
6285// print("thresh ",thresh);
6286// print("contraction list size",contraction_map.size());
6287
6288 // remove all double entries
6289 for (auto& elem: contraction_map) {
6290 elem.second.sort();
6291 elem.second.unique();
6292 }
6293 //wall1 = wall_time();
6294 //wall_recur += (wall1 - wall0);
6295// if (n==2) {
6296// print("contraction map for n=", n);
6297// print_map(contraction_map);
6298// }
6299 all_contraction_maps.push_back(contraction_map);
6300
6301 long mapsize=contraction_map.size();
6302 if (mapsize==0) break;
6303 }
6304
6305
6306 // finally do the contraction
6307 for (const auto& contraction_map : all_contraction_maps) {
6308 for (const auto& key_list : contraction_map) {
6309 const Key<NDIM>& key=key_list.first;
6310 const std::list<Key<CDIM>>& list=key_list.second;
6311 woT::task(coeffs.owner(key), &implT:: template partial_inner_contract<Q,LDIM,R,KDIM>,
6312 &g,&h,v1,v2,key,list);
6313 }
6314 }
6315 }
6316
6317 /// for contraction two functions f(x,z) = \int g(x,y) h(y,z) dy
6318
6319 /// find all nodes with d coefficients and return a list of complete keys and of
6320 /// keys holding only the y dimension, also the maximum norm of all d for the j dimension
6321 /// @param[in] n the scale
6322 /// @param[in] v array holding the indices of the integration variable
6323 /// @return ijlist: list of all nodes with d coeffs; jlist: j-part of ij list only
6324 template<std::size_t CDIM>
6325 std::tuple<std::set<Key<NDIM>>, std::map<Key<CDIM>,double>>
6326 get_contraction_node_lists(const std::size_t n, const std::array<int, CDIM>& v) const {
6327
6328 const auto& cdata=get_cdata();
6329 auto has_d_coeffs = [&cdata](const coeffT& coeff) {
6330 if (coeff.has_no_data()) return false;
6331 return (coeff.dim(0)==2*cdata.k);
6332 };
6333
6334 // keys to be contracted in g
6335 std::set<Key<NDIM>> ij_list; // full key
6336 std::map<Key<CDIM>,double> j_list; // only that dimension that will be contracted
6337
6338 for (auto it=get_coeffs().begin(); it!=get_coeffs().end(); ++it) {
6339 const Key<NDIM>& key=it->first;
6340 const FunctionNode<T,NDIM>& node=it->second;
6341 if ((key.level()==int(n)) and (has_d_coeffs(node.coeff()))) {
6342 ij_list.insert(key);
6344 for (std::size_t i=0; i<CDIM; ++i) j_trans[i]=key.translation()[v[i]];
6345 Key<CDIM> jkey(n,j_trans);
6346 const double max_d_norm=j_list[jkey];
6347 j_list.insert_or_assign(jkey,std::max(max_d_norm,node.get_dnorm()));
6348 Key<CDIM> parent_jkey=jkey.parent();
6349 while (j_list.count(parent_jkey)==0) {
6350 j_list.insert({parent_jkey,1.0});
6351 parent_jkey=parent_jkey.parent();
6352 }
6353 }
6354 }
6355 return std::make_tuple(ij_list,j_list);
6356 }
6357
6358 /// make a map of all nodes that will contribute to a partial inner product
6359
6360 /// given the list of d coefficient-holding nodes of the other function:
6361 /// recur down h if snorm * dnorm > tol and key n−jx ∈ other−ij-list. Make s
6362 /// coefficients if necessary. Make list of nodes n − ijk as map(n-ik, list(j)).
6363 ///
6364 /// !! WILL ADD NEW S NODES TO THIS TREE THAT MUST BE REMOVED TO AVOID INCONSISTENT TREE STRUCTURE !!
6365 ///
6366 /// @param[in] key for recursion
6367 /// @param[in] node corresponds to key
6368 /// @param[in] v_this this' dimension that are contracted
6369 /// @param[in] v_other other's dimension that are contracted
6370 /// @param[in] ij_other_list list of nodes of the other function that will be contracted (and their parents)
6371 /// @param[in] j_other_list list of column nodes of the other function that will be contracted (and their parents)
6372 /// @param[in] max_d_norm max d coeff norm of the nodes in j_list
6373 /// @param[in] this_first are the remaining coeffs of this functions first or last in the result function
6374 /// @param[in] thresh threshold for including nodes in the contraction: snorm*dnorm > thresh
6375 /// @tparam CDIM dimension to be contracted
6376 /// @tparam ODIM dimensions of the other function
6377 /// @tparam FDIM dimensions of the final function
6378 template<std::size_t CDIM, std::size_t ODIM, std::size_t FDIM=NDIM+ODIM-2*CDIM>
6379 std::multimap<Key<FDIM>, std::list<Key<CDIM>>> recur_down_for_contraction_map(
6380 const keyT& key, const nodeT& node,
6381 const std::array<int,CDIM>& v_this,
6382 const std::array<int,CDIM>& v_other,
6383 const std::set<Key<ODIM>>& ij_other_list,
6384 const std::map<Key<CDIM>,double>& j_other_list,
6385 bool this_first, const double thresh) {
6386
6387 std::multimap<Key<FDIM>, std::list<Key<CDIM>>> contraction_map;
6388
6389 // fast return if the other function has no d coeffs
6390 if (j_other_list.empty()) return contraction_map;
6391
6392 // continue recursion if this node may be contracted with the j column
6393 // extract relevant node translations from this node
6394 const auto j_this_key=key.extract_key(v_this);
6395
6396// print("\nkey, j_this_key", key, j_this_key);
6397 const double max_d_norm=j_other_list.find(j_this_key)->second;
6398 const bool sd_norm_product_large = node.get_snorm() * max_d_norm > truncate_tol(thresh,key);
6399// print("sd_product_norm",node.get_snorm() * max_d_norm, thresh);
6400
6401 // end recursion if we have reached the final scale n
6402 // with which nodes from other will this node be contracted?
6403 bool final_scale=key.level()==ij_other_list.begin()->level();
6404 if (final_scale and sd_norm_product_large) {
6405 for (auto& other_key : ij_other_list) {
6406 const auto j_other_key=other_key.extract_key(v_other);
6407 if (j_this_key != j_other_key) continue;
6408 auto i_key=key.extract_complement_key(v_this);
6409 auto k_key=other_key.extract_complement_key(v_other);
6410// print("key, ij_other_key",key,other_key);
6411// print("i, k, j key",i_key, k_key, j_this_key);
6412 Key<FDIM> ik_key=(this_first) ? i_key.merge_with(k_key) : k_key.merge_with(i_key);
6413// print("ik_key",ik_key);
6414// MADNESS_CHECK(contraction_map.count(ik_key)==0);
6415 contraction_map.insert(std::make_pair(ik_key,std::list<Key<CDIM>>{j_this_key}));
6416 }
6417 return contraction_map;
6418 }
6419
6420 bool continue_recursion = (j_other_list.count(j_this_key)==1);
6421 if (not continue_recursion) return contraction_map;
6422
6423
6424 // continue recursion if norms are large
6425 continue_recursion = (node.has_children() or sd_norm_product_large);
6426
6427 if (continue_recursion) {
6428 // in case we need to compute children's coefficients: unfilter only once
6429 bool compute_child_s_coeffs=true;
6430 coeffT d = node.coeff();
6431// print("continuing recursion from key",key);
6432
6433 for (KeyChildIterator<NDIM> kit(key); kit; ++kit) {
6434 keyT child=kit.key();
6435 typename dcT::accessor acc;
6436
6437 // make child's s coeffs if it doesn't exist or if is has no s coeffs
6438 bool childnode_exists=get_coeffs().find(acc,child);
6439 bool need_s_coeffs= childnode_exists ? (acc->second.get_snorm()<=0.0) : true;
6440
6441 coeffT child_s_coeffs;
6442 if (need_s_coeffs) {
6443 if (compute_child_s_coeffs) {
6444 if (d.dim(0)==cdata.vk[0]) { // s coeffs only in this node
6445 coeffT d1(cdata.v2k,get_tensor_args());
6446 d1(cdata.s0)+=d;
6447 d=d1;
6448 }
6449 d = unfilter(d);
6450 compute_child_s_coeffs=false;
6451 }
6452 child_s_coeffs=copy(d(child_patch(child)));
6453 child_s_coeffs.reduce_rank(thresh);
6454 }
6455
6456 if (not childnode_exists) {
6457 get_coeffs().replace(child,nodeT(child_s_coeffs,false));
6458 get_coeffs().find(acc,child);
6459 } else if (childnode_exists and need_s_coeffs) {
6460 acc->second.coeff()=child_s_coeffs;
6461 }
6462 bool exists= get_coeffs().find(acc,child);
6463 MADNESS_CHECK(exists);
6464 nodeT& childnode = acc->second;
6465 if (need_s_coeffs) childnode.recompute_snorm_and_dnorm(get_cdata());
6466// print("recurring down to",child);
6467 contraction_map.merge(recur_down_for_contraction_map(child,childnode, v_this, v_other,
6468 ij_other_list, j_other_list, this_first, thresh));
6469// print("contraction_map.size()",contraction_map.size());
6470 }
6471
6472 }
6473
6474 return contraction_map;
6475 }
6476
6477
6478 /// tensor contraction part of partial_inner
6479
6480 /// @param[in] g rhs of the inner product
6481 /// @param[in] h lhs of the inner product
6482 /// @param[in] v1 dimensions of g to be contracted
6483 /// @param[in] v2 dimensions of h to be contracted
6484 /// @param[in] key key of result's (this) FunctionNode
6485 /// @param[in] j_key_list list of contraction index-j keys contributing to this' node
6486 template<typename Q, std::size_t LDIM, typename R, std::size_t KDIM,
6487 std::size_t CDIM = (KDIM + LDIM - NDIM) / 2>
6489 const std::array<int, CDIM> v1, const std::array<int, CDIM> v2,
6490 const Key<NDIM>& key, const std::list<Key<CDIM>>& j_key_list) {
6491
6492 Key<LDIM - CDIM> i_key;
6493 Key<KDIM - CDIM> k_key;
6494 key.break_apart(i_key, k_key);
6495
6496 coeffT result_coeff(get_cdata().v2k, get_tensor_type());
6497 for (const auto& j_key: j_key_list) {
6498
6499 auto v_complement = [](const auto& v, const auto& vc) {
6500 constexpr std::size_t VDIM = std::tuple_size<std::decay_t<decltype(v)>>::value;
6501 constexpr std::size_t VCDIM = std::tuple_size<std::decay_t<decltype(vc)>>::value;
6502 std::array<int, VCDIM> result;
6503 for (std::size_t i = 0; i < VCDIM; i++) result[i] = (v.back() + i + 1) % (VDIM + VCDIM);
6504 return result;
6505 };
6506 auto make_ij_key = [&v_complement](const auto i_key, const auto j_key, const auto& v) {
6507 constexpr std::size_t IDIM = std::decay_t<decltype(i_key)>::static_size;
6508 constexpr std::size_t JDIM = std::decay_t<decltype(j_key)>::static_size;
6509 static_assert(JDIM == std::tuple_size<std::decay_t<decltype(v)>>::value);
6510
6512 for (std::size_t i = 0; i < v.size(); ++i) l[v[i]] = j_key.translation()[i];
6513 std::array<int, IDIM> vc1;
6514 auto vc = v_complement(v, vc1);
6515 for (std::size_t i = 0; i < vc.size(); ++i) l[vc[i]] = i_key.translation()[i];
6516
6517 return Key<IDIM + JDIM>(i_key.level(), l);
6518 };
6519
6520 Key<LDIM> ij_key = make_ij_key(i_key, j_key, v1);
6521 Key<KDIM> jk_key = make_ij_key(k_key, j_key, v2);
6522
6523 MADNESS_CHECK(g->get_coeffs().probe(ij_key));
6524 MADNESS_CHECK(h->get_coeffs().probe(jk_key));
6525 const coeffT& gcoeff = g->get_coeffs().find(ij_key).get()->second.coeff();
6526 const coeffT& hcoeff = h->get_coeffs().find(jk_key).get()->second.coeff();
6527 coeffT gcoeff1, hcoeff1;
6528 if (gcoeff.dim(0) == g->get_cdata().k) {
6529 gcoeff1 = coeffT(g->get_cdata().v2k, g->get_tensor_args());
6530 gcoeff1(g->get_cdata().s0) += gcoeff;
6531 } else {
6532 gcoeff1 = gcoeff;
6533 }
6534 if (hcoeff.dim(0) == g->get_cdata().k) {
6535 hcoeff1 = coeffT(h->get_cdata().v2k, h->get_tensor_args());
6536 hcoeff1(h->get_cdata().s0) += hcoeff;
6537 } else {
6538 hcoeff1 = hcoeff;
6539 }
6540
6541 // offset: 0 for full tensor, 1 for svd representation with rand being the first dimension (r,d1,d2,d3) -> (r,d1*d2*d3)
6542 auto fuse = [](Tensor<T> tensor, const std::array<int, CDIM>& v, int offset) {
6543 for (std::size_t i = 0; i < CDIM - 1; ++i) {
6544 MADNESS_CHECK((v[i] + 1) == v[i + 1]); // make sure v is contiguous and ascending
6545 tensor = tensor.fusedim(v[0]+offset);
6546 }
6547 return tensor;
6548 };
6549
6550 // use case: partial_projection of 2-electron functions in svd representation f(1) = \int g(2) h(1,2) d2
6551 // c_i = \sum_j a_j b_ij = \sum_jr a_j b_rj b'_rj
6552 // = \sum_jr ( a_j b_rj) b'_rj )
6553 auto contract2 = [](const auto& svdcoeff, const auto& tensor, const int particle) {
6554#if HAVE_GENTENSOR
6555 const int spectator_particle=(particle+1)%2;
6556 Tensor<Q> gtensor = svdcoeff.get_svdtensor().make_vector_with_weights(particle);
6557 gtensor=gtensor.reshape(svdcoeff.rank(),gtensor.size()/svdcoeff.rank());
6558 MADNESS_CHECK(gtensor.ndim()==2);
6559 Tensor<Q> gtensor_other = svdcoeff.get_svdtensor().ref_vector(spectator_particle);
6560 Tensor<T> tmp1=inner(gtensor,tensor.flat(),1,0); // tmp1(r) = sum_j a'_(r,j) b(j)
6561 MADNESS_CHECK(tmp1.ndim()==1);
6562 Tensor<T> tmp2=inner(gtensor_other,tmp1,0,0); // tmp2(i) = sum_r a_(r,i) tmp1(r)
6563 return tmp2;
6564#else
6565 MADNESS_EXCEPTION("no partial_inner using svd without GenTensor",1);
6566 return Tensor<T>();
6567#endif
6568 };
6569
6570 if (gcoeff.is_full_tensor() and hcoeff.is_full_tensor() and result_coeff.is_full_tensor()) {
6571 // merge multiple contraction dimensions into one
6572 int offset = 0;
6573 Tensor<Q> gtensor = fuse(gcoeff1.full_tensor(), v1, offset);
6574 Tensor<R> htensor = fuse(hcoeff1.full_tensor(), v2, offset);
6575 result_coeff.full_tensor() += inner(gtensor, htensor, v1[0], v2[0]);
6576 if (key.level() > 0) {
6577 gtensor = copy(gcoeff1.full_tensor()(g->get_cdata().s0));
6578 htensor = copy(hcoeff1.full_tensor()(h->get_cdata().s0));
6579 gtensor = fuse(gtensor, v1, offset);
6580 htensor = fuse(htensor, v2, offset);
6581 result_coeff.full_tensor()(get_cdata().s0) -= inner(gtensor, htensor, v1[0], v2[0]);
6582 }
6583 }
6584
6585
6586 // use case: 2-electron functions in svd representation f(1,3) = \int g(1,2) h(2,3) d2
6587 // c_ik = \sum_j a_ij b_jk = \sum_jrr' a_ri a'_rj b_r'j b_r'k
6588 // = \sum_jrr' ( a_ri (a'_rj b_r'j) ) b_r'k
6589 // = \sum_jrr' c_r'i b_r'k
6590 else if (gcoeff.is_svd_tensor() and hcoeff.is_svd_tensor() and result_coeff.is_svd_tensor()) {
6591 MADNESS_CHECK(v1[0]==0 or v1[CDIM-1]==LDIM-1);
6592 MADNESS_CHECK(v2[0]==0 or v2[CDIM-1]==KDIM-1);
6593 int gparticle= v1[0]==0 ? 0 : 1; // which particle to integrate over
6594 int hparticle= v2[0]==0 ? 0 : 1; // which particle to integrate over
6595 // merge multiple contraction dimensions into one
6596 Tensor<Q> gtensor = gcoeff1.get_svdtensor().flat_vector_with_weights(gparticle);
6597 Tensor<Q> gtensor_other = gcoeff1.get_svdtensor().flat_vector((gparticle+1)%2);
6598 Tensor<R> htensor = hcoeff1.get_svdtensor().flat_vector_with_weights(hparticle);
6599 Tensor<R> htensor_other = hcoeff1.get_svdtensor().flat_vector((hparticle+1)%2);
6600 Tensor<T> tmp1=inner(gtensor,htensor,1,1); // tmp1(r,r') = sum_j b(r,j) a(r',j)
6601 Tensor<T> tmp2=inner(tmp1,gtensor_other,0,0); // tmp2(r',i) = sum_r tmp1(r,r') a(r,i)
6603 MADNESS_CHECK(tmp2.dim(0)==htensor_other.dim(0));
6604 w=1.0;
6605 coeffT result_tmp(get_cdata().v2k, get_tensor_type());
6606 result_tmp.get_svdtensor().set_vectors_and_weights(w,tmp2,htensor_other);
6607 if (key.level() > 0) {
6608 GenTensor<Q> gcoeff2 = copy(gcoeff1(g->get_cdata().s0));
6609 GenTensor<R> hcoeff2 = copy(hcoeff1(h->get_cdata().s0));
6610 Tensor<Q> gtensor = gcoeff2.get_svdtensor().flat_vector_with_weights(gparticle);
6611 Tensor<Q> gtensor_other = gcoeff2.get_svdtensor().flat_vector((gparticle+1)%2);
6612 Tensor<R> htensor = hcoeff2.get_svdtensor().flat_vector_with_weights(hparticle);
6613 Tensor<R> htensor_other = hcoeff2.get_svdtensor().flat_vector((hparticle+1)%2);
6614 Tensor<T> tmp1=inner(gtensor,htensor,1,1); // tmp1(r,r') = sum_j b(r,j) a(r',j)
6615 Tensor<T> tmp2=inner(tmp1,gtensor_other,0,0); // tmp2(r',i) = sum_r tmp1(r,r') a(r,i)
6617 MADNESS_CHECK(tmp2.dim(0)==htensor_other.dim(0));
6618 w=1.0;
6619 coeffT result_coeff1(get_cdata().vk, get_tensor_type());
6620 result_coeff1.get_svdtensor().set_vectors_and_weights(w,tmp2,htensor_other);
6621 result_tmp(get_cdata().s0)-=result_coeff1;
6622 }
6623 result_coeff+=result_tmp;
6624 }
6625
6626 // use case: partial_projection of 2-electron functions in svd representation f(1) = \int g(2) h(1,2) d2
6627 // c_i = \sum_j a_j b_ij = \sum_jr a_j b_rj b'_rj
6628 // = \sum_jr ( a_j b_rj) b'_rj )
6629 else if (gcoeff.is_full_tensor() and hcoeff.is_svd_tensor() and result_coeff.is_full_tensor()) {
6630 MADNESS_CHECK(v1[0]==0 and v1[CDIM-1]==LDIM-1);
6631 MADNESS_CHECK(v2[0]==0 or v2[CDIM-1]==KDIM-1);
6632 MADNESS_CHECK(LDIM==CDIM);
6633 int hparticle= v2[0]==0 ? 0 : 1; // which particle to integrate over
6634
6635 Tensor<T> r=contract2(hcoeff1,gcoeff1.full_tensor(),hparticle);
6636 if (key.level()>0) r(get_cdata().s0)-=contract2(copy(hcoeff1(h->get_cdata().s0)),copy(gcoeff.full_tensor()(g->get_cdata().s0)),hparticle);
6637 result_coeff.full_tensor()+=r;
6638 }
6639 // use case: partial_projection of 2-electron functions in svd representation f(1) = \int g(1,2) h(2) d2
6640 // c_i = \sum_j a_ij b_j = \sum_jr a_ri a'_rj b_j
6641 // = \sum_jr ( a_ri (a'_rj b_j) )
6642 else if (gcoeff.is_svd_tensor() and hcoeff.is_full_tensor() and result_coeff.is_full_tensor()) {
6643 MADNESS_CHECK(v1[0]==0 or v1[CDIM-1]==LDIM-1);
6644 MADNESS_CHECK(v2[0]==0 and v2[CDIM-1]==KDIM-1);
6645 MADNESS_CHECK(KDIM==CDIM);
6646 int gparticle= v1[0]==0 ? 0 : 1; // which particle to integrate over
6647
6648 Tensor<T> r=contract2(gcoeff1,hcoeff1.full_tensor(),gparticle);
6649 if (key.level()>0) r(get_cdata().s0)-=contract2(copy(gcoeff1(g->get_cdata().s0)),copy(hcoeff.full_tensor()(h->get_cdata().s0)),gparticle);
6650 result_coeff.full_tensor()+=r;
6651
6652 } else {
6653 MADNESS_EXCEPTION("unknown case in partial_inner_contract",1);
6654 }
6655 }
6656
6657 MADNESS_CHECK(result_coeff.is_assigned());
6658 result_coeff.reduce_rank(get_thresh());
6659
6660 if (coeffs.is_local(key))
6661 coeffs.send(key, &nodeT::accumulate, result_coeff, coeffs, key, get_tensor_args());
6662 else
6664 }
6665
6666 /// Return the inner product with an external function on a specified function node.
6667
6668 /// @param[in] key Key of the function node to compute the inner product on. (the domain of integration)
6669 /// @param[in] c Tensor of coefficients for the function at the function node given by key
6670 /// @param[in] f Reference to FunctionFunctorInterface. This is the externally provided function
6671 /// @return Returns the inner product over the domain of a single function node, no guarantee of accuracy.
6672 T inner_ext_node(keyT key, tensorT c, const std::shared_ptr< FunctionFunctorInterface<T,NDIM> > f) const {
6673 tensorT fvals = tensorT(this->cdata.vk);
6674 // Compute the value of the external function at the quadrature points.
6675 fcube(key, *(f), cdata.quad_x, fvals);
6676 // Convert quadrature point values to scaling coefficients.
6677 tensorT fc = tensorT(values2coeffs(key, fvals));
6678 // Return the inner product of the two functions' scaling coefficients.
6679 return c.trace_conj(fc);
6680 }
6681
6682 /// Call inner_ext_node recursively until convergence.
6683 /// @param[in] key Key of the function node on which to compute inner product (the domain of integration)
6684 /// @param[in] c coeffs for the function at the node given by key
6685 /// @param[in] f Reference to FunctionFunctorInterface. This is the externally provided function
6686 /// @param[in] leaf_refine boolean switch to turn on/off refinement past leaf nodes
6687 /// @param[in] old_inner the inner product on the parent function node
6688 /// @return Returns the inner product over the domain of a single function, checks for convergence.
6689 T inner_ext_recursive(keyT key, tensorT c, const std::shared_ptr< FunctionFunctorInterface<T,NDIM> > f, const bool leaf_refine, T old_inner=T(0)) const {
6690 int i = 0;
6691 tensorT c_child, inner_child;
6692 T new_inner, result = 0.0;
6693
6694 c_child = tensorT(cdata.v2k); // tensor of child coeffs
6695 inner_child = Tensor<double>(pow(2, NDIM)); // child inner products
6696
6697 // If old_inner is default value, assume this is the first call
6698 // and compute inner product on this node.
6699 if (old_inner == T(0)) {
6700 old_inner = inner_ext_node(key, c, f);
6701 }
6702
6703 if (coeffs.find(key).get()->second.has_children()) {
6704 // Since the key has children and we know the func is redundant,
6705 // Iterate over all children of this compute node, computing
6706 // the inner product on each child node. new_inner will store
6707 // the sum of these, yielding a more accurate inner product.
6708 for (KeyChildIterator<NDIM> it(key); it; ++it, ++i) {
6709 const keyT& child = it.key();
6710 tensorT cc = coeffs.find(child).get()->second.coeff().full_tensor_copy();
6711 inner_child(i) = inner_ext_node(child, cc, f);
6712 }
6713 new_inner = inner_child.sum();
6714 } else if (leaf_refine) {
6715 // We need the scaling coefficients of the numerical function
6716 // at each of the children nodes. We can't use project because
6717 // there is no guarantee that the numerical function will have
6718 // a functor. Instead, since we know we are at or below the
6719 // leaf nodes, the wavelet coefficients are zero (to within the
6720 // truncate tolerance). Thus, we can use unfilter() to
6721 // get the scaling coefficients at the next level.
6722 tensorT d = tensorT(cdata.v2k);
6723 d = T(0);
6724 d(cdata.s0) = copy(c);
6725 c_child = unfilter(d);
6726
6727 // Iterate over all children of this compute node, computing
6728 // the inner product on each child node. new_inner will store
6729 // the sum of these, yielding a more accurate inner product.
6730 for (KeyChildIterator<NDIM> it(key); it; ++it, ++i) {
6731 const keyT& child = it.key();
6732 tensorT cc = tensorT(c_child(child_patch(child)));
6733 inner_child(i) = inner_ext_node(child, cc, f);
6734 }
6735 new_inner = inner_child.sum();
6736 } else {
6737 // If we get to here, we are at the leaf nodes and the user has
6738 // specified that they do not want refinement past leaf nodes.
6739 new_inner = old_inner;
6740 }
6741
6742 // Check for convergence. If converged...yay, we're done. If not,
6743 // call inner_ext_node_recursive on each child node and accumulate
6744 // the inner product in result.
6745 // if (std::abs(new_inner - old_inner) <= truncate_tol(thresh, key)) {
6746 if (std::abs(new_inner - old_inner) <= thresh) {
6747 result = new_inner;
6748 } else {
6749 i = 0;
6750 for (KeyChildIterator<NDIM> it(key); it; ++it, ++i) {
6751 const keyT& child = it.key();
6752 tensorT cc = tensorT(c_child(child_patch(child)));
6753 result += inner_ext_recursive(child, cc, f, leaf_refine, inner_child(i));
6754 }
6755 }
6756
6757 return result;
6758 }
6759
6761 const std::shared_ptr< FunctionFunctorInterface<T, NDIM> > fref;
6762 const implT * impl;
6763 const bool leaf_refine;
6764 const bool do_leaves; ///< start with leaf nodes instead of initial_level
6765
6767 const implT * impl, const bool leaf_refine, const bool do_leaves)
6768 : fref(f), impl(impl), leaf_refine(leaf_refine), do_leaves(do_leaves) {};
6769
6770 T operator()(typename dcT::const_iterator& it) const {
6771 if (do_leaves and it->second.is_leaf()) {
6772 tensorT cc = it->second.coeff().full_tensor();
6773 return impl->inner_adaptive_recursive(it->first, cc, fref, leaf_refine, T(0));
6774 } else if ((not do_leaves) and (it->first.level() == impl->initial_level)) {
6775 tensorT cc = it->second.coeff().full_tensor();
6776 return impl->inner_ext_recursive(it->first, cc, fref, leaf_refine, T(0));
6777 } else {
6778 return 0.0;
6779 }
6780 }
6781
6782 T operator()(T a, T b) const {
6783 return (a + b);
6784 }
6785
6786 template <typename Archive> void serialize(const Archive& ar) {
6787 MADNESS_EXCEPTION("NOT IMPLEMENTED", 1);
6788 }
6789 };
6790
6791 /// Return the local part of inner product with external function ... no communication.
6792 /// @param[in] f Reference to FunctionFunctorInterface. This is the externally provided function
6793 /// @param[in] leaf_refine boolean switch to turn on/off refinement past leaf nodes
6794 /// @return Returns local part of the inner product, i.e. over the domain of all function nodes on this compute node.
6795 T inner_ext_local(const std::shared_ptr< FunctionFunctorInterface<T,NDIM> > f, const bool leaf_refine) const {
6797
6799 do_inner_ext_local_ffi(f, this, leaf_refine, false));
6800 }
6801
6802 /// Return the local part of inner product with external function ... no communication.
6803 /// @param[in] f Reference to FunctionFunctorInterface. This is the externally provided function
6804 /// @param[in] leaf_refine boolean switch to turn on/off refinement past leaf nodes
6805 /// @return Returns local part of the inner product, i.e. over the domain of all function nodes on this compute node.
6806 T inner_adaptive_local(const std::shared_ptr< FunctionFunctorInterface<T,NDIM> > f, const bool leaf_refine) const {
6808
6810 do_inner_ext_local_ffi(f, this, leaf_refine, true));
6811 }
6812
6813 /// Call inner_ext_node recursively until convergence.
6814 /// @param[in] key Key of the function node on which to compute inner product (the domain of integration)
6815 /// @param[in] c coeffs for the function at the node given by key
6816 /// @param[in] f Reference to FunctionFunctorInterface. This is the externally provided function
6817 /// @param[in] leaf_refine boolean switch to turn on/off refinement past leaf nodes
6818 /// @param[in] old_inner the inner product on the parent function node
6819 /// @return Returns the inner product over the domain of a single function, checks for convergence.
6821 const std::shared_ptr< FunctionFunctorInterface<T,NDIM> > f,
6822 const bool leaf_refine, T old_inner=T(0)) const {
6823
6824 // the inner product in the current node
6825 old_inner = inner_ext_node(key, c, f);
6826 T result=0.0;
6827
6828 // the inner product in the child nodes
6829
6830 // compute the sum coefficients of the MRA function
6831 tensorT d = tensorT(cdata.v2k);
6832 d = T(0);
6833 d(cdata.s0) = copy(c);
6834 tensorT c_child = unfilter(d);
6835
6836 // compute the inner product in the child nodes
6837 T new_inner=0.0; // child inner products
6838 for (KeyChildIterator<NDIM> it(key); it; ++it) {
6839 const keyT& child = it.key();
6840 tensorT cc = tensorT(c_child(child_patch(child)));
6841 new_inner+= inner_ext_node(child, cc, f);
6842 }
6843
6844 // continue recursion if needed
6845 const double tol=truncate_tol(thresh,key);
6846 if (leaf_refine and (std::abs(new_inner - old_inner) > tol)) {
6847 for (KeyChildIterator<NDIM> it(key); it; ++it) {
6848 const keyT& child = it.key();
6849 tensorT cc = tensorT(c_child(child_patch(child)));
6850 result += inner_adaptive_recursive(child, cc, f, leaf_refine, T(0));
6851 }
6852 } else {
6853 result = new_inner;
6854 }
6855 return result;
6856
6857 }
6858
6859
6860 /// Return the gaxpy product with an external function on a specified
6861 /// function node.
6862 /// @param[in] key Key of the function node on which to compute gaxpy
6863 /// @param[in] lc Tensor of coefficients for the function at the
6864 /// function node given by key
6865 /// @param[in] f Pointer to function of type T that takes coordT
6866 /// arguments. This is the externally provided function and
6867 /// the right argument of gaxpy.
6868 /// @param[in] alpha prefactor of c Tensor for gaxpy
6869 /// @param[in] beta prefactor of fcoeffs for gaxpy
6870 /// @return Returns coefficient tensor of the gaxpy product at specified
6871 /// key, no guarantee of accuracy.
6872 template <typename L>
6873 tensorT gaxpy_ext_node(keyT key, Tensor<L> lc, T (*f)(const coordT&), T alpha, T beta) const {
6874 // Compute the value of external function at the quadrature points.
6875 tensorT fvals = madness::fcube(key, f, cdata.quad_x);
6876 // Convert quadrature point values to scaling coefficients.
6877 tensorT fcoeffs = values2coeffs(key, fvals);
6878 // Return the inner product of the two functions' scaling coeffs.
6879 tensorT c2 = copy(lc);
6880 c2.gaxpy(alpha, fcoeffs, beta);
6881 return c2;
6882 }
6883
6884 /// Return out of place gaxpy using recursive descent.
6885 /// @param[in] key Key of the function node on which to compute gaxpy
6886 /// @param[in] left FunctionImpl, left argument of gaxpy
6887 /// @param[in] lcin coefficients of left at this node
6888 /// @param[in] c coefficients of gaxpy product at this node
6889 /// @param[in] f pointer to function of type T that takes coordT
6890 /// arguments. This is the externally provided function and
6891 /// the right argument of gaxpy.
6892 /// @param[in] alpha prefactor of left argument for gaxpy
6893 /// @param[in] beta prefactor of right argument for gaxpy
6894 /// @param[in] tol convergence tolerance...when the norm of the gaxpy's
6895 /// difference coefficients is less than tol, we are done.
6896 template <typename L>
6897 void gaxpy_ext_recursive(const keyT& key, const FunctionImpl<L,NDIM>* left,
6898 Tensor<L> lcin, tensorT c, T (*f)(const coordT&),
6899 T alpha, T beta, double tol, bool below_leaf) {
6900 typedef typename FunctionImpl<L,NDIM>::dcT::const_iterator literT;
6901
6902 // If we haven't yet reached the leaf level, check whether the
6903 // current key is a leaf node of left. If so, set below_leaf to true
6904 // and continue. If not, make this a parent, recur down, return.
6905 if (not below_leaf) {
6906 bool left_leaf = left->coeffs.find(key).get()->second.is_leaf();
6907 if (left_leaf) {
6908 below_leaf = true;
6909 } else {
6910 this->coeffs.replace(key, nodeT(coeffT(), true));
6911 for (KeyChildIterator<NDIM> it(key); it; ++it) {
6912 const keyT& child = it.key();
6913 woT::task(left->coeffs.owner(child), &implT:: template gaxpy_ext_recursive<L>,
6914 child, left, Tensor<L>(), tensorT(), f, alpha, beta, tol, below_leaf);
6915 }
6916 return;
6917 }
6918 }
6919
6920 // Compute left's coefficients if not provided
6921 Tensor<L> lc = lcin;
6922 if (lc.size() == 0) {
6923 literT it = left->coeffs.find(key).get();
6924 MADNESS_ASSERT(it != left->coeffs.end());
6925 if (it->second.has_coeff())
6926 lc = it->second.coeff().reconstruct_tensor();
6927 }
6928
6929 // Compute this node's coefficients if not provided in function call
6930 if (c.size() == 0) {
6931 c = gaxpy_ext_node(key, lc, f, alpha, beta);
6932 }
6933
6934 // We need the scaling coefficients of the numerical function at
6935 // each of the children nodes. We can't use project because there
6936 // is no guarantee that the numerical function will have a functor.
6937 // Instead, since we know we are at or below the leaf nodes, the
6938 // wavelet coefficients are zero (to within the truncate tolerance).
6939 // Thus, we can use unfilter() to get the scaling coefficients at
6940 // the next level.
6941 Tensor<L> lc_child = Tensor<L>(cdata.v2k); // left's child coeffs
6942 Tensor<L> ld = Tensor<L>(cdata.v2k);
6943 ld = L(0);
6944 ld(cdata.s0) = copy(lc);
6945 lc_child = unfilter(ld);
6946
6947 // Iterate over children of this node,
6948 // storing the gaxpy coeffs in c_child
6949 tensorT c_child = tensorT(cdata.v2k); // tensor of child coeffs
6950 for (KeyChildIterator<NDIM> it(key); it; ++it) {
6951 const keyT& child = it.key();
6952 tensorT lcoeff = tensorT(lc_child(child_patch(child)));
6953 c_child(child_patch(child)) = gaxpy_ext_node(child, lcoeff, f, alpha, beta);
6954 }
6955
6956 // Compute the difference coefficients to test for convergence.
6957 tensorT d = tensorT(cdata.v2k);
6958 d = filter(c_child);
6959 // Filter returns both s and d coefficients, so set scaling
6960 // coefficient part of d to 0 so that we take only the
6961 // norm of the difference coefficients.
6962 d(cdata.s0) = T(0);
6963 double dnorm = d.normf();
6964
6965 // Small d.normf means we've reached a good level of resolution
6966 // Store the coefficients and return.
6967 if (dnorm <= truncate_tol(tol,key)) {
6968 this->coeffs.replace(key, nodeT(coeffT(c,targs), false));
6969 } else {
6970 // Otherwise, make this a parent node and recur down
6971 this->coeffs.replace(key, nodeT(coeffT(), true)); // Interior node
6972
6973 for (KeyChildIterator<NDIM> it(key); it; ++it) {
6974 const keyT& child = it.key();
6975 tensorT child_coeff = tensorT(c_child(child_patch(child)));
6976 tensorT left_coeff = tensorT(lc_child(child_patch(child)));
6977 woT::task(left->coeffs.owner(child), &implT:: template gaxpy_ext_recursive<L>,
6978 child, left, left_coeff, child_coeff, f, alpha, beta, tol, below_leaf);
6979 }
6980 }
6981 }
6982
6983 template <typename L>
6984 void gaxpy_ext(const FunctionImpl<L,NDIM>* left, T (*f)(const coordT&), T alpha, T beta, double tol, bool fence) {
6985 if (world.rank() == coeffs.owner(cdata.key0))
6986 gaxpy_ext_recursive<L> (cdata.key0, left, Tensor<L>(), tensorT(), f, alpha, beta, tol, false);
6987 if (fence)
6988 world.gop.fence();
6989 }
6990
6991 /// project the low-dim function g on the hi-dim function f: result(x) = <this(x,y) | g(y)>
6992
6993 /// invoked by the hi-dim function, a function of NDIM+LDIM
6994
6995 /// Upon return, result matches this, with contributions on all scales
6996 /// @param[in] result lo-dim function of NDIM-LDIM \todo Should this be param[out]?
6997 /// @param[in] gimpl lo-dim function of LDIM
6998 /// @param[in] dim over which dimensions to be integrated: 0..LDIM or LDIM..LDIM+NDIM-1
6999 template<size_t LDIM>
7001 const int dim, const bool fence) {
7002
7003 const keyT& key0=cdata.key0;
7004
7005 if (world.rank() == coeffs.owner(key0)) {
7006
7007 // coeff_op will accumulate the result
7008 typedef project_out_op<LDIM> coeff_opT;
7009 coeff_opT coeff_op(this,result,CoeffTracker<T,LDIM>(gimpl),dim);
7010
7011 // don't do anything on this -- coeff_op will accumulate into result
7012 typedef noop<T,NDIM> apply_opT;
7013 apply_opT apply_op;
7014
7015 woT::task(world.rank(), &implT:: template forward_traverse<coeff_opT,apply_opT>,
7016 coeff_op, apply_op, cdata.key0);
7017
7018 }
7019 if (fence) world.gop.fence();
7020
7021 }
7022
7023
7024 /// project the low-dim function g on the hi-dim function f: result(x) = <f(x,y) | g(y)>
7025 template<size_t LDIM>
7027 bool randomize() const {return false;}
7028
7031 typedef FunctionImpl<T,NDIM-LDIM> implL1;
7032 typedef std::pair<bool,coeffT> argT;
7033
7034 const implT* fimpl; ///< the hi dim function f
7035 mutable implL1* result; ///< the low dim result function
7036 ctL iag; ///< the low dim function g
7037 int dim; ///< 0: project 0..LDIM-1, 1: project LDIM..NDIM-1
7038
7039 // ctor
7040 project_out_op() = default;
7041 project_out_op(const implT* fimpl, implL1* result, const ctL& iag, const int dim)
7042 : fimpl(fimpl), result(result), iag(iag), dim(dim) {}
7044 : fimpl(other.fimpl), result(other.result), iag(other.iag), dim(other.dim) {}
7045
7046
7047 /// do the actual contraction
7049
7050 Key<LDIM> key1,key2,dest;
7051 key.break_apart(key1,key2);
7052
7053 // make the right coefficients
7054 coeffT gcoeff;
7055 if (dim==0) {
7056 gcoeff=iag.get_impl()->parent_to_child(iag.coeff(),iag.key(),key1);
7057 dest=key2;
7058 }
7059 if (dim==1) {
7060 gcoeff=iag.get_impl()->parent_to_child(iag.coeff(),iag.key(),key2);
7061 dest=key1;
7062 }
7063
7064 MADNESS_ASSERT(fimpl->get_coeffs().probe(key)); // must be local!
7065 const nodeT& fnode=fimpl->get_coeffs().find(key).get()->second;
7066 const coeffT& fcoeff=fnode.coeff();
7067
7068 // fast return if possible
7069 if (fcoeff.has_no_data() or gcoeff.has_no_data())
7070 return Future<argT> (argT(fnode.is_leaf(),coeffT()));;
7071
7072 MADNESS_CHECK(gcoeff.is_full_tensor());
7073 tensorT final(result->cdata.vk);
7074 const int k=fcoeff.dim(0);
7075 const int k_ldim=std::pow(k,LDIM);
7076 std::vector<long> shape(LDIM, k);
7077
7078 if (fcoeff.is_full_tensor()) {
7079 // result_i = \sum_j g_j f_ji
7080 const tensorT gtensor = gcoeff.full_tensor().reshape(k_ldim);
7081 const tensorT ftensor = fcoeff.full_tensor().reshape(k_ldim,k_ldim);
7082 final=inner(gtensor,ftensor,0,dim).reshape(shape);
7083
7084 } else if (fcoeff.is_svd_tensor()) {
7085 if (fcoeff.rank()>0) {
7086
7087 // result_i = \sum_jr g_j a_rj w_r b_ri
7088 const int otherdim = (dim + 1) % 2;
7089 const tensorT gtensor = gcoeff.full_tensor().flat();
7090 const tensorT atensor = fcoeff.get_svdtensor().flat_vector(dim); // a_rj
7091 const tensorT btensor = fcoeff.get_svdtensor().flat_vector(otherdim);
7092 const tensorT gatensor = inner(gtensor, atensor, 0, 1); // ga_r
7093 tensorT weights = copy(fcoeff.get_svdtensor().weights_);
7094 weights.emul(gatensor); // ga_r * w_r
7095 // sum over all ranks of b, include new weights:
7096 // result_i = \sum_r ga_r * w_r * b_ri
7097 for (int r = 0; r < fcoeff.rank(); ++r) final += weights(r) * btensor(r, _);
7098 final = final.reshape(shape);
7099 }
7100
7101 } else {
7102 MADNESS_EXCEPTION("unsupported tensor type in project_out_op",1);
7103 }
7104
7105 // accumulate the result
7106 result->coeffs.task(dest, &FunctionNode<T,LDIM>::accumulate2, final, result->coeffs, dest, TaskAttributes::hipri());
7107
7108 return Future<argT> (argT(fnode.is_leaf(),coeffT()));
7109 }
7110
7111 this_type make_child(const keyT& child) const {
7112 Key<LDIM> key1,key2;
7113 child.break_apart(key1,key2);
7114 const Key<LDIM> gkey = (dim==0) ? key1 : key2;
7115
7116 return this_type(fimpl,result,iag.make_child(gkey),dim);
7117 }
7118
7119 /// retrieve the coefficients (parent coeffs might be remote)
7122 return result->world.taskq.add(detail::wrap_mem_fn(*const_cast<this_type *> (this),
7123 &this_type::forward_ctor),fimpl,result,g1,dim);
7124 }
7125
7126 /// taskq-compatible ctor
7127 this_type forward_ctor(const implT* fimpl1, implL1* result1, const ctL& iag1, const int dim1) {
7128 return this_type(fimpl1,result1,iag1,dim1);
7129 }
7130
7131 template <typename Archive> void serialize(const Archive& ar) {
7132 ar & result & iag & fimpl & dim;
7133 }
7134
7135 };
7136
7137
7138 /// project the low-dim function g on the hi-dim function f: this(x) = <f(x,y) | g(y)>
7139
7140 /// invoked by result, a function of NDIM
7141
7142 /// @param[in] f hi-dim function of LDIM+NDIM
7143 /// @param[in] g lo-dim function of LDIM
7144 /// @param[in] dim over which dimensions to be integrated: 0..LDIM or LDIM..LDIM+NDIM-1
7145 template<size_t LDIM>
7146 void project_out2(const FunctionImpl<T,LDIM+NDIM>* f, const FunctionImpl<T,LDIM>* g, const int dim) {
7147
7148 typedef std::pair< keyT,coeffT > pairT;
7149 typedef typename FunctionImpl<T,NDIM+LDIM>::dcT::const_iterator fiterator;
7150
7151 // loop over all nodes of hi-dim f, compute the inner products with all
7152 // appropriate nodes of g, and accumulate in result
7153 fiterator end = f->get_coeffs().end();
7154 for (fiterator it=f->get_coeffs().begin(); it!=end; ++it) {
7155 const Key<LDIM+NDIM> key=it->first;
7156 const FunctionNode<T,LDIM+NDIM> fnode=it->second;
7157 const coeffT& fcoeff=fnode.coeff();
7158
7159 if (fnode.is_leaf() and fcoeff.has_data()) {
7160
7161 // break key into particle: over key1 will be summed, over key2 will be
7162 // accumulated, or vice versa, depending on dim
7163 if (dim==0) {
7164 Key<NDIM> key1;
7165 Key<LDIM> key2;
7166 key.break_apart(key1,key2);
7167
7168 Future<pairT> result;
7169 // sock_it_to_me(key1, result.remote_ref(world));
7170 g->task(coeffs.owner(key1), &implT::sock_it_to_me, key1, result.remote_ref(world), TaskAttributes::hipri());
7171 woT::task(world.rank(),&implT:: template do_project_out<LDIM>,fcoeff,result,key1,key2,dim);
7172
7173 } else if (dim==1) {
7174 Key<LDIM> key1;
7175 Key<NDIM> key2;
7176 key.break_apart(key1,key2);
7177
7178 Future<pairT> result;
7179 // sock_it_to_me(key2, result.remote_ref(world));
7180 g->task(coeffs.owner(key2), &implT::sock_it_to_me, key2, result.remote_ref(world), TaskAttributes::hipri());
7181 woT::task(world.rank(),&implT:: template do_project_out<LDIM>,fcoeff,result,key2,key1,dim);
7182
7183 } else {
7184 MADNESS_EXCEPTION("confused dim in project_out",1);
7185 }
7186 }
7187 }
7189// this->compressed=false;
7190// this->nonstandard=false;
7191// this->redundant=true;
7192 }
7193
7194
7195 /// compute the inner product of two nodes of only some dimensions and accumulate on result
7196
7197 /// invoked by result
7198 /// @param[in] fcoeff coefficients of high dimension LDIM+NDIM
7199 /// @param[in] gpair key and coeffs of low dimension LDIM (possibly a parent node)
7200 /// @param[in] gkey key of actual low dim node (possibly the same as gpair.first, iff gnode exists)
7201 /// @param[in] dest destination node for the result
7202 /// @param[in] dim which dimensions should be contracted: 0..LDIM-1 or LDIM..NDIM+LDIM-1
7203 template<size_t LDIM>
7204 void do_project_out(const coeffT& fcoeff, const std::pair<keyT,coeffT> gpair, const keyT& gkey,
7205 const Key<NDIM>& dest, const int dim) const {
7206
7207 const coeffT gcoeff=parent_to_child(gpair.second,gpair.first,gkey);
7208
7209 // fast return if possible
7210 if (fcoeff.has_no_data() or gcoeff.has_no_data()) return;
7211
7212 // let's specialize for the time being on SVD tensors for f and full tensors of half dim for g
7214 MADNESS_ASSERT(fcoeff.tensor_type()==TT_2D);
7215 const tensorT gtensor=gcoeff.full_tensor();
7216 tensorT result(cdata.vk);
7217
7218 const int otherdim=(dim+1)%2;
7219 const int k=fcoeff.dim(0);
7220 std::vector<Slice> s(fcoeff.config().dim_per_vector()+1,_);
7221
7222 // do the actual contraction
7223 for (int r=0; r<fcoeff.rank(); ++r) {
7224 s[0]=Slice(r,r);
7225 const tensorT contracted_tensor=fcoeff.config().ref_vector(dim)(s).reshape(k,k,k);
7226 const tensorT other_tensor=fcoeff.config().ref_vector(otherdim)(s).reshape(k,k,k);
7227 const double ovlp= gtensor.trace_conj(contracted_tensor);
7228 const double fac=ovlp * fcoeff.config().weights(r);
7229 result+=fac*other_tensor;
7230 }
7231
7232 // accumulate the result
7233 coeffs.task(dest, &nodeT::accumulate2, result, coeffs, dest, TaskAttributes::hipri());
7234 }
7235
7236
7237
7238
7239 /// Returns the maximum local depth of the tree ... no communications.
7240 std::size_t max_local_depth() const;
7241
7242
7243 /// Returns the maximum depth of the tree ... collective ... global sum/broadcast
7244 std::size_t max_depth() const;
7245
7246 /// Returns the max number of nodes on a processor
7247 std::size_t max_nodes() const;
7248
7249 /// Returns the min number of nodes on a processor
7250 std::size_t min_nodes() const;
7251
7252 /// Returns the size of the tree structure of the function ... collective global sum
7253 std::size_t tree_size() const;
7254
7255 /// Returns the number of coefficients in the function for each rank
7256 std::size_t size_local() const;
7257
7258 /// Returns the number of coefficients in the function ... collective global sum
7259 std::size_t size() const;
7260
7261 /// Returns the number of coefficients in the function for this MPI rank
7262 std::size_t nCoeff_local() const;
7263
7264 /// Returns the number of coefficients in the function ... collective global sum
7265 std::size_t nCoeff() const;
7266
7267 /// Returns the number of coefficients in the function ... collective global sum
7268 std::size_t real_size() const;
7269
7270 /// print tree size and size
7271 void print_size(const std::string name) const;
7272
7273 /// print the number of configurations per node
7274 void print_stats() const;
7275
7276 /// In-place scale by a constant
7277 void scale_inplace(const T q, bool fence);
7278
7279 /// Out-of-place scale by a constant
7280 template <typename Q, typename F>
7281 void scale_oop(const Q q, const FunctionImpl<F,NDIM>& f, bool fence) {
7282 typedef typename FunctionImpl<F,NDIM>::nodeT fnodeT;
7283 typedef typename FunctionImpl<F,NDIM>::dcT fdcT;
7284 typename fdcT::const_iterator end = f.coeffs.end();
7285 for (typename fdcT::const_iterator it=f.coeffs.begin(); it!=end; ++it) {
7286 const keyT& key = it->first;
7287 const fnodeT& node = it->second;
7288
7289 if (node.has_coeff()) {
7290 coeffs.replace(key,nodeT(node.coeff()*q,node.has_children()));
7291 }
7292 else {
7293 coeffs.replace(key,nodeT(coeffT(),node.has_children()));
7294 }
7295 }
7296 if (fence)
7297 world.gop.fence();
7298 }
7299
7300 /// Hash a pointer to \c FunctionImpl
7301
7302 /// \param[in] impl pointer to a FunctionImpl
7303 /// \return The hash.
7304 inline friend hashT hash_value(const FunctionImpl<T,NDIM>* pimpl) {
7305 hashT seed = hash_value(pimpl->id().get_world_id());
7306 detail::combine_hash(seed, hash_value(pimpl->id().get_obj_id()));
7307 return seed;
7308 }
7309
7310 /// Hash a shared_ptr to \c FunctionImpl
7311
7312 /// \param[in] impl pointer to a FunctionImpl
7313 /// \return The hash.
7314 inline friend hashT hash_value(const std::shared_ptr<FunctionImpl<T,NDIM>> impl) {
7315 return hash_value(impl.get());
7316 }
7317 };
7318
7319 namespace archive {
7320 template <class Archive, class T, std::size_t NDIM>
7321 struct ArchiveLoadImpl<Archive,const FunctionImpl<T,NDIM>*> {
7322 static void load(const Archive& ar, const FunctionImpl<T,NDIM>*& ptr) {
7323 bool exists=false;
7324 ar & exists;
7325 if (exists) {
7326 uniqueidT id;
7327 ar & id;
7328 World* world = World::world_from_id(id.get_world_id());
7329 MADNESS_ASSERT(world);
7330 auto ptr_opt = world->ptr_from_id< WorldObject< FunctionImpl<T,NDIM> > >(id);
7331 if (!ptr_opt)
7332 MADNESS_EXCEPTION("FunctionImpl: remote operation attempting to use a locally uninitialized object",0);
7333 ptr = static_cast< const FunctionImpl<T,NDIM>*>(*ptr_opt);
7334 if (!ptr)
7335 MADNESS_EXCEPTION("FunctionImpl: remote operation attempting to use an unregistered object",0);
7336 } else {
7337 ptr=nullptr;
7338 }
7339 }
7340 };
7341
7342 template <class Archive, class T, std::size_t NDIM>
7343 struct ArchiveStoreImpl<Archive,const FunctionImpl<T,NDIM>*> {
7344 static void store(const Archive& ar, const FunctionImpl<T,NDIM>*const& ptr) {
7345 bool exists=(ptr) ? true : false;
7346 ar & exists;
7347 if (exists) ar & ptr->id();
7348 }
7349 };
7350
7351 template <class Archive, class T, std::size_t NDIM>
7352 struct ArchiveLoadImpl<Archive, FunctionImpl<T,NDIM>*> {
7353 static void load(const Archive& ar, FunctionImpl<T,NDIM>*& ptr) {
7354 bool exists=false;
7355 ar & exists;
7356 if (exists) {
7357 uniqueidT id;
7358 ar & id;
7359 World* world = World::world_from_id(id.get_world_id());
7360 MADNESS_ASSERT(world);
7361 auto ptr_opt = world->ptr_from_id< WorldObject< FunctionImpl<T,NDIM> > >(id);
7362 if (!ptr_opt)
7363 MADNESS_EXCEPTION("FunctionImpl: remote operation attempting to use a locally uninitialized object",0);
7364 ptr = static_cast< FunctionImpl<T,NDIM>*>(*ptr_opt);
7365 if (!ptr) {
7366 auto ids=world->get_object_ids();
7367 print(world->get_world_ids());
7368 MADNESS_EXCEPTION("FunctionImpl: remote operation attempting to use an unregistered object",0);
7369 }
7370 } else {
7371 ptr=nullptr;
7372 }
7373 }
7374 };
7375
7376 template <class Archive, class T, std::size_t NDIM>
7377 struct ArchiveStoreImpl<Archive, FunctionImpl<T,NDIM>*> {
7378 static void store(const Archive& ar, FunctionImpl<T,NDIM>*const& ptr) {
7379 bool exists=(ptr) ? true : false;
7380 ar & exists;
7381 if (exists) ar & ptr->id();
7382 // ar & ptr->id();
7383 }
7384 };
7385
7386 template <class Archive, class T, std::size_t NDIM>
7387 struct ArchiveLoadImpl<Archive, std::shared_ptr<const FunctionImpl<T,NDIM> > > {
7388 static void load(const Archive& ar, std::shared_ptr<const FunctionImpl<T,NDIM> >& ptr) {
7389 const FunctionImpl<T,NDIM>* f = nullptr;
7391 ptr.reset(f, [] (const FunctionImpl<T,NDIM> *p_) -> void {});
7392 }
7393 };
7394
7395 template <class Archive, class T, std::size_t NDIM>
7396 struct ArchiveStoreImpl<Archive, std::shared_ptr<const FunctionImpl<T,NDIM> > > {
7397 static void store(const Archive& ar, const std::shared_ptr<const FunctionImpl<T,NDIM> >& ptr) {
7399 }
7400 };
7401
7402 template <class Archive, class T, std::size_t NDIM>
7403 struct ArchiveLoadImpl<Archive, std::shared_ptr<FunctionImpl<T,NDIM> > > {
7404 static void load(const Archive& ar, std::shared_ptr<FunctionImpl<T,NDIM> >& ptr) {
7405 FunctionImpl<T,NDIM>* f = nullptr;
7407 ptr.reset(f, [] (FunctionImpl<T,NDIM> *p_) -> void {});
7408 }
7409 };
7410
7411 template <class Archive, class T, std::size_t NDIM>
7412 struct ArchiveStoreImpl<Archive, std::shared_ptr<FunctionImpl<T,NDIM> > > {
7413 static void store(const Archive& ar, const std::shared_ptr<FunctionImpl<T,NDIM> >& ptr) {
7415 }
7416 };
7417 }
7418
7419}
7420
7421#endif // MADNESS_MRA_FUNCIMPL_H__INCLUDED
double w(double t, double eps)
Definition DKops.h:22
double q(double t)
Definition DKops.h:18
This header should include pretty much everything needed for the parallel runtime.
An integer with atomic set, get, read+increment, read+decrement, and decrement+test operations.
Definition atomicint.h:126
long dim(int i) const
Returns the size of dimension i.
Definition basetensor.h:147
long ndim() const
Returns the number of dimensions in the tensor.
Definition basetensor.h:144
long size() const
Returns the number of elements in the tensor.
Definition basetensor.h:138
Definition displacements.h:332
std::function< bool(Level, const PointPattern &, std::optional< Displacement > &)> Validator
Definition displacements.h:340
Definition displacements.h:784
a class to track where relevant (parent) coeffs are
Definition funcimpl.h:791
const keyT & key() const
const reference to the key
Definition funcimpl.h:839
CoeffTracker(const CoeffTracker &other, const datumT &datum)
ctor with a pair<keyT,nodeT>
Definition funcimpl.h:821
const LeafStatus & is_leaf() const
const reference to is_leaf flag
Definition funcimpl.h:863
const implT * impl
the funcimpl that has the coeffs
Definition funcimpl.h:800
LeafStatus
Definition funcimpl.h:797
@ yes
Definition funcimpl.h:797
@ no
Definition funcimpl.h:797
@ unknown
Definition funcimpl.h:797
CoeffTracker(const CoeffTracker &other)
copy ctor
Definition funcimpl.h:829
double dnorm(const keyT &key) const
return the s and dnorm belonging to the passed-in key
Definition funcimpl.h:856
coeffT coeff_
the coefficients belonging to key
Definition funcimpl.h:806
const implT * get_impl() const
const reference to impl
Definition funcimpl.h:833
const coeffT & coeff() const
const reference to the coeffs
Definition funcimpl.h:836
keyT key_
the current key, which must exists in impl
Definition funcimpl.h:802
double dnorm_
norm of d coefficients corresponding to key
Definition funcimpl.h:808
CoeffTracker(const implT *impl)
the initial ctor making the root key
Definition funcimpl.h:816
void serialize(const Archive &ar)
serialization
Definition funcimpl.h:915
Future< CoeffTracker > activate() const
find the coefficients
Definition funcimpl.h:892
CoeffTracker()
default ctor
Definition funcimpl.h:813
GenTensor< T > coeffT
Definition funcimpl.h:795
CoeffTracker make_child(const keyT &child) const
make a child of this, ignoring the coeffs
Definition funcimpl.h:866
FunctionImpl< T, NDIM > implT
Definition funcimpl.h:793
std::pair< Key< NDIM >, ShallowNode< T, NDIM > > datumT
Definition funcimpl.h:796
CoeffTracker forward_ctor(const CoeffTracker &other, const datumT &datum) const
taskq-compatible forwarding to the ctor
Definition funcimpl.h:909
LeafStatus is_leaf_
flag if key is a leaf node
Definition funcimpl.h:804
coeffT coeff(const keyT &key) const
return the coefficients belonging to the passed-in key
Definition funcimpl.h:847
Key< NDIM > keyT
Definition funcimpl.h:794
CompositeFunctorInterface implements a wrapper of holding several functions and functors.
Definition function_interface.h:172
Definition worldhashmap.h:396
Tri-diagonal operator traversing tree primarily for derivative operator.
Definition derivative.h:73
Holds displacements for applying operators to avoid replicating for all operators.
Definition displacements.h:65
const std::vector< Key< NDIM > > & get_disp(Level n, const array_of_bools< NDIM > &kernel_lattice_sum_axes)
Definition displacements.h:236
FunctionCommonData holds all Function data common for given k.
Definition function_common_data.h:52
Tensor< double > quad_phit
transpose of quad_phi
Definition function_common_data.h:102
Tensor< double > quad_phiw
quad_phiw(i,j) = at x[i] value of w[i]*phi[j]
Definition function_common_data.h:103
std::vector< long > vk
(k,...) used to initialize Tensors
Definition function_common_data.h:93
std::vector< Slice > s0
s[0] in each dimension to get scaling coeff
Definition function_common_data.h:91
static const FunctionCommonData< T, NDIM > & get(int k)
Definition function_common_data.h:111
static void _init_quadrature(int k, int npt, Tensor< double > &quad_x, Tensor< double > &quad_w, Tensor< double > &quad_phi, Tensor< double > &quad_phiw, Tensor< double > &quad_phit)
Initialize the quadrature information.
Definition mraimpl.h:91
collect common functionality does not need to be member function of funcimpl
Definition function_common_data.h:135
const FunctionCommonData< T, NDIM > & cdata
Definition function_common_data.h:138
GenTensor< T > coeffs2values(const Key< NDIM > &key, const GenTensor< T > &coeff) const
Definition function_common_data.h:142
Tensor< T > values2coeffs(const Key< NDIM > &key, const Tensor< T > &values) const
Definition function_common_data.h:155
FunctionDefaults holds default paramaters as static class members.
Definition funcdefaults.h:100
static const double & get_thresh()
Returns the default threshold.
Definition funcdefaults.h:177
static int get_max_refine_level()
Gets the default maximum adaptive refinement level.
Definition funcdefaults.h:214
static const Tensor< double > & get_cell_width()
Returns the width of each user cell dimension.
Definition funcdefaults.h:381
static bool get_apply_randomize()
Gets the random load balancing for integral operators flag.
Definition funcdefaults.h:290
static const Tensor< double > & get_cell()
Gets the user cell for the simulation.
Definition funcdefaults.h:348
FunctionFactory implements the named-parameter idiom for Function.
Definition function_factory.h:86
bool _refine
Definition function_factory.h:99
bool _empty
Definition function_factory.h:100
bool _fence
Definition function_factory.h:103
Abstract base class interface required for functors used as input to Functions.
Definition function_interface.h:68
Definition funcimpl.h:5559
double operator()(double a, double b) const
Definition funcimpl.h:5585
const opT * func
Definition funcimpl.h:5561
Tensor< double > qx
Definition funcimpl.h:5563
double operator()(typename dcT::const_iterator &it) const
Definition funcimpl.h:5576
void serialize(const Archive &ar)
Definition funcimpl.h:5590
do_err_box(const implT *impl, const opT *func, int npt, const Tensor< double > &qx, const Tensor< double > &quad_phit, const Tensor< double > &quad_phiw)
Definition funcimpl.h:5569
int npt
Definition funcimpl.h:5562
Tensor< double > quad_phiw
Definition funcimpl.h:5565
const implT * impl
Definition funcimpl.h:5560
Tensor< double > quad_phit
Definition funcimpl.h:5564
do_err_box(const do_err_box &e)
Definition funcimpl.h:5573
FunctionImpl holds all Function state to facilitate shallow copy semantics.
Definition funcimpl.h:945
std::tuple< std::set< Key< NDIM > >, std::map< Key< CDIM >, double > > get_contraction_node_lists(const std::size_t n, const std::array< int, CDIM > &v) const
for contraction two functions f(x,z) = \int g(x,y) h(y,z) dy
Definition funcimpl.h:6326
void copy_coeffs(const FunctionImpl< Q, NDIM > &other, bool fence)
Copy coeffs from other into self.
Definition funcimpl.h:1145
bool is_nonstandard() const
Definition mraimpl.h:273
void insert_serialized_coeffs(std::vector< unsigned char > &v)
insert coeffs from vector archive into this
Definition funcimpl.h:1191
T eval_cube(Level n, coordT &x, const tensorT &c) const
Definition mraimpl.h:2034
void partial_inner_contract(const FunctionImpl< Q, LDIM > *g, const FunctionImpl< R, KDIM > *h, const std::array< int, CDIM > v1, const std::array< int, CDIM > v2, const Key< NDIM > &key, const std::list< Key< CDIM > > &j_key_list)
tensor contraction part of partial_inner
Definition funcimpl.h:6488
AtomicInt large
Definition funcimpl.h:1002
Timer timer_target_driven
Definition funcimpl.h:1000
void binaryXX(const FunctionImpl< L, NDIM > *left, const FunctionImpl< R, NDIM > *right, const opT &op, bool fence)
Definition funcimpl.h:3276
void do_apply(const opT *op, const keyT &key, const Tensor< R > &c)
apply an operator on the coeffs c (at node key)
Definition funcimpl.h:4899
void do_print_tree_graphviz(const keyT &key, std::ostream &os, Level maxlevel) const
Functor for the do_print_tree method (using GraphViz)
Definition mraimpl.h:2788
void add_keys_to_map(mapT *map, int index) const
Adds keys to union of local keys with specified index.
Definition funcimpl.h:5903
void change_tensor_type1(const TensorArgs &targs, bool fence)
change the tensor type of the coefficients in the FunctionNode
Definition mraimpl.h:1108
void gaxpy_ext_recursive(const keyT &key, const FunctionImpl< L, NDIM > *left, Tensor< L > lcin, tensorT c, T(*f)(const coordT &), T alpha, T beta, double tol, bool below_leaf)
Definition funcimpl.h:6897
int initial_level
Initial level for refinement.
Definition funcimpl.h:974
int max_refine_level
Do not refine below this level.
Definition funcimpl.h:978
double do_apply_kernel3(const opT *op, const GenTensor< R > &coeff, const do_op_args< OPDIM > &args, const TensorArgs &apply_targs)
same as do_apply_kernel2, but use low rank tensors as input and low rank tensors as output
Definition funcimpl.h:4857
void hartree_product(const std::vector< std::shared_ptr< FunctionImpl< T, LDIM > > > p1, const std::vector< std::shared_ptr< FunctionImpl< T, LDIM > > > p2, const leaf_opT &leaf_op, bool fence)
given two functions of LDIM, perform the Hartree/Kronecker/outer product
Definition funcimpl.h:3816
void traverse_tree(const coeff_opT &coeff_op, const apply_opT &apply_op, const keyT &key) const
traverse a non-existing tree
Definition funcimpl.h:3786
void do_square_inplace(const keyT &key)
int special_level
Minimium level for refinement on special points.
Definition funcimpl.h:975
void do_apply_kernel(const opT *op, const Tensor< R > &c, const do_op_args< OPDIM > &args)
for fine-grain parallelism: call the apply method of an operator in a separate task
Definition funcimpl.h:4791
double errsq_local(const opT &func) const
Returns the sum of squares of errors from local info ... no comms.
Definition funcimpl.h:5597
WorldContainer< keyT, nodeT > dcT
Type of container holding the coefficients.
Definition funcimpl.h:957
void evaldepthpt(const Vector< double, NDIM > &xin, const keyT &keyin, const typename Future< Level >::remote_refT &ref)
Get the depth of the tree at a point in simulation coordinates.
Definition mraimpl.h:3060
void scale_inplace(const T q, bool fence)
In-place scale by a constant.
Definition mraimpl.h:3231
void gaxpy_oop_reconstructed(const double alpha, const implT &f, const double beta, const implT &g, const bool fence)
perform: this= alpha*f + beta*g, invoked by result
Definition mraimpl.h:223
void unary_op_coeff_inplace(const opT &op, bool fence)
Definition funcimpl.h:2120
World & world
Definition funcimpl.h:964
void apply_1d_realspace_push_op(const archive::archive_ptr< const opT > &pop, int axis, const keyT &key, const Tensor< R > &c)
Definition funcimpl.h:3854
bool is_redundant() const
Returns true if the function is redundant.
Definition mraimpl.h:262
FunctionNode< T, NDIM > nodeT
Type of node.
Definition funcimpl.h:955
std::size_t nCoeff_local() const
Returns the number of coefficients in the function for this MPI rank.
Definition mraimpl.h:1931
void print_size(const std::string name) const
print tree size and size
Definition mraimpl.h:1950
FunctionImpl(const FunctionImpl< T, NDIM > &p)
void print_info() const
Prints summary of data distribution.
Definition mraimpl.h:833
void abs_inplace(bool fence)
Definition mraimpl.h:3243
void binaryXXa(const keyT &key, const FunctionImpl< L, NDIM > *left, const Tensor< L > &lcin, const FunctionImpl< R, NDIM > *right, const Tensor< R > &rcin, const opT &op)
Definition funcimpl.h:3145
void print_timer() const
Definition mraimpl.h:357
void evalR(const Vector< double, NDIM > &xin, const keyT &keyin, const typename Future< long >::remote_refT &ref)
Get the rank of leaf box of the tree at a point in simulation coordinates.
Definition mraimpl.h:3102
const FunctionCommonData< T, NDIM > & cdata
Definition funcimpl.h:984
void do_print_grid(const std::string filename, const std::vector< keyT > &keys) const
print the grid in xyz format
Definition mraimpl.h:584
void mulXXa(const keyT &key, const FunctionImpl< L, NDIM > *left, const Tensor< L > &lcin, const FunctionImpl< R, NDIM > *right, const Tensor< R > &rcin, double tol)
Definition funcimpl.h:3059
int get_truncate_mode() const
Definition funcimpl.h:1757
const std::vector< Vector< double, NDIM > > & get_special_points() const
Definition funcimpl.h:969
std::size_t nCoeff() const
Returns the number of coefficients in the function ... collective global sum.
Definition mraimpl.h:1941
double vol_nsphere(int n, double R)
Definition funcimpl.h:4887
keyT neighbor_in_volume(const keyT &key, const keyT &disp) const
Returns key of general neighbor that resides in-volume.
Definition mraimpl.h:3356
void compress(const TreeState newstate, bool fence)
compress the wave function
Definition mraimpl.h:1509
void do_dirac_convolution(FunctionImpl< T, LDIM > *f, bool fence) const
Definition funcimpl.h:2203
std::pair< coeffT, double > compress_op(const keyT &key, const std::vector< Future< std::pair< coeffT, double > > > &v, bool nonstandard)
calculate the wavelet coefficients using the sum coefficients of all child nodes
Definition mraimpl.h:1677
Future< bool > truncate_spawn(const keyT &key, double tol)
Returns true if after truncation this node has coefficients.
Definition mraimpl.h:2633
void print_type_in_compilation_error(R &&)
Definition funcimpl.h:6208
Future< double > norm_tree_spawn(const keyT &key)
Definition mraimpl.h:1579
std::vector< keyT > local_leaf_keys() const
return the keys of the local leaf boxes
Definition mraimpl.h:558
MADNESS_ASSERT(this->is_redundant()==g.is_redundant())
void do_print_tree(const keyT &key, std::ostream &os, Level maxlevel) const
Functor for the do_print_tree method.
Definition mraimpl.h:2706
void vtransform(const std::vector< std::shared_ptr< FunctionImpl< R, NDIM > > > &vright, const Tensor< Q > &c, const std::vector< std::shared_ptr< FunctionImpl< T, NDIM > > > &vleft, double tol, bool fence)
Definition funcimpl.h:2920
void unset_functor()
Definition mraimpl.h:312
void refine_spawn(const opT &op, const keyT &key)
Definition funcimpl.h:4619
void apply_1d_realspace_push(const opT &op, const FunctionImpl< R, NDIM > *f, int axis, bool fence)
Definition funcimpl.h:3905
void set_truncate_mode(int mode)
Definition funcimpl.h:1758
void do_print_plane(const std::string filename, std::vector< Tensor< double > > plotinfo, const int xaxis, const int yaxis, const coordT el2)
print the MRA structure
Definition mraimpl.h:499
std::pair< Key< NDIM >, ShallowNode< T, NDIM > > find_datum(keyT key) const
return the a std::pair<key, node>, which MUST exist
Definition mraimpl.h:974
void set_functor(const std::shared_ptr< FunctionFunctorInterface< T, NDIM > > functor1)
Definition mraimpl.h:293
std::enable_if< NDIM==FDIM >::type read_grid2(const std::string gridfile, std::shared_ptr< FunctionFunctorInterface< double, NDIM > > vnuc_functor)
read data from a grid
Definition funcimpl.h:1651
bool verify_tree_state_local() const
check that the tree state and the coeffs are consistent
Definition mraimpl.h:169
const std::shared_ptr< WorldDCPmapInterface< Key< NDIM > > > & get_pmap() const
Definition mraimpl.h:207
Tensor< Q > fcube_for_mul(const keyT &child, const keyT &parent, const Tensor< Q > &coeff) const
Compute the function values for multiplication.
Definition funcimpl.h:1967
Timer timer_filter
Definition funcimpl.h:998
void sock_it_to_me(const keyT &key, const RemoteReference< FutureImpl< std::pair< keyT, coeffT > > > &ref) const
Walk up the tree returning pair(key,node) for first node with coefficients.
Definition mraimpl.h:2846
void recursive_apply(opT &apply_op, const implT *fimpl, implT *rimpl, const bool fence)
traverse an existing tree and apply an operator
Definition funcimpl.h:5416
double get_thresh() const
Definition mraimpl.h:328
void trickle_down(bool fence)
sum all the contributions from all scales after applying an operator in mod-NS form
Definition mraimpl.h:1363
bool autorefine
If true, autorefine where appropriate.
Definition funcimpl.h:980
std::pair< coeffT, double > make_redundant_op(const keyT &key, const std::vector< Future< std::pair< coeffT, double > > > &v)
similar to compress_op, but insert only the sum coefficients in the tree
Definition mraimpl.h:1737
void set_autorefine(bool value)
Definition mraimpl.h:337
tensorT filter(const tensorT &s) const
Transform sum coefficients at level n to sums+differences at level n-1.
Definition mraimpl.h:1161
void chop_at_level(const int n, const bool fence=true)
remove all nodes with level higher than n
Definition mraimpl.h:1124
void unaryXXvalues(const FunctionImpl< Q, NDIM > *func, const opT &op, bool fence)
Definition funcimpl.h:3303
void partial_inner(const FunctionImpl< Q, LDIM > &g, const FunctionImpl< R, KDIM > &h, const std::array< int, CDIM > v1, const std::array< int, CDIM > v2)
invoked by result
Definition funcimpl.h:6224
TreeState tree_state
Definition funcimpl.h:987
void print_tree_json(std::ostream &os=std::cout, Level maxlevel=10000) const
Definition mraimpl.h:2726
coeffT parent_to_child_NS(const keyT &child, const keyT &parent, const coeffT &coeff) const
Directly project parent NS coeffs to child NS coeffs.
Definition mraimpl.h:707
void copy_coeffs_different_world(const FunctionImpl< Q, NDIM > &other)
Copy coefficients from other funcimpl with possibly different world and on a different node.
Definition funcimpl.h:1155
void mapdim(const implT &f, const std::vector< long > &map, bool fence)
Permute the dimensions of f according to map, result on this.
Definition mraimpl.h:1066
bool is_compressed() const
Returns true if the function is compressed.
Definition mraimpl.h:250
Vector< double, NDIM > coordT
Type of vector holding coordinates.
Definition funcimpl.h:959
void apply(opT &op, const FunctionImpl< R, NDIM > &f, bool fence)
apply an operator on f to return this
Definition funcimpl.h:5099
Tensor< T > tensorT
Type of tensor for anything but to hold coeffs.
Definition funcimpl.h:952
void mirror(const implT &f, const std::vector< long > &mirror, bool fence)
mirror the dimensions of f according to map, result on this
Definition mraimpl.h:1075
T inner_adaptive_recursive(keyT key, const tensorT &c, const std::shared_ptr< FunctionFunctorInterface< T, NDIM > > f, const bool leaf_refine, T old_inner=T(0)) const
Definition funcimpl.h:6820
void store(Archive &ar)
Definition funcimpl.h:1323
void do_binary_op(const keyT &key, const Tensor< L > &left, const std::pair< keyT, Tensor< R > > &arg, const opT &op)
Functor for the binary_op method.
Definition funcimpl.h:2069
void gaxpy_ext(const FunctionImpl< L, NDIM > *left, T(*f)(const coordT &), T alpha, T beta, double tol, bool fence)
Definition funcimpl.h:6984
void accumulate_trees(FunctionImpl< Q, NDIM > &result, const R alpha, const bool fence=true) const
merge the trees of this and other, while multiplying them with the alpha or beta, resp
Definition funcimpl.h:1244
void print_stats() const
print the number of configurations per node
Definition mraimpl.h:1978
void broaden(const array_of_bools< NDIM > &is_periodic, bool fence)
Definition mraimpl.h:1312
coeffT truncate_reconstructed_op(const keyT &key, const std::vector< Future< coeffT > > &v, const double tol)
given the sum coefficients of all children, truncate or not
Definition mraimpl.h:1626
void refine_op(const opT &op, const keyT &key)
Definition funcimpl.h:4594
static Tensor< TENSOR_RESULT_TYPE(T, R) > inner_local(const std::vector< const FunctionImpl< T, NDIM > * > &left, const std::vector< const FunctionImpl< R, NDIM > * > &right, bool sym)
Definition funcimpl.h:6104
void fcube(const keyT &key, const FunctionFunctorInterface< T, NDIM > &f, const Tensor< double > &qx, tensorT &fval) const
Evaluate function at quadrature points in the specified box.
Definition mraimpl.h:2468
Timer timer_change_tensor_type
Definition funcimpl.h:996
void forward_do_diff1(const DerivativeBase< T, NDIM > *D, const implT *f, const keyT &key, const std::pair< keyT, coeffT > &left, const std::pair< keyT, coeffT > &center, const std::pair< keyT, coeffT > &right)
Definition mraimpl.h:932
std::vector< Slice > child_patch(const keyT &child) const
Returns patch referring to coeffs of child in parent box.
Definition mraimpl.h:696
void print_tree_graphviz(std::ostream &os=std::cout, Level maxlevel=10000) const
Definition mraimpl.h:2779
void set_tree_state(const TreeState &state)
Definition funcimpl.h:1354
std::size_t min_nodes() const
Returns the min number of nodes on a processor.
Definition mraimpl.h:1882
void copy_coeffs_same_world(const FunctionImpl< Q, NDIM > &other, bool fence)
Copy coeffs from other into self.
Definition funcimpl.h:1198
std::shared_ptr< FunctionFunctorInterface< T, NDIM > > functor
Definition funcimpl.h:986
Timer timer_compress_svd
Definition funcimpl.h:999
Tensor< TENSOR_RESULT_TYPE(T, R)> mul(const Tensor< T > &c1, const Tensor< R > &c2, const int npt, const keyT &key) const
multiply the values of two coefficient tensors using a custom number of grid points
Definition funcimpl.h:2042
void make_redundant(const bool fence)
convert this to redundant, i.e. have sum coefficients on all levels
Definition mraimpl.h:1537
void load(Archive &ar)
Definition funcimpl.h:1305
std::size_t max_nodes() const
Returns the max number of nodes on a processor.
Definition mraimpl.h:1873
T inner_ext_local(const std::shared_ptr< FunctionFunctorInterface< T, NDIM > > f, const bool leaf_refine) const
Definition funcimpl.h:6795
coeffT upsample(const keyT &key, const coeffT &coeff) const
upsample the sum coefficients of level 1 to sum coeffs on level n+1
Definition mraimpl.h:1240
TensorArgs targs
type of tensor to be used in the FunctionNodes
Definition funcimpl.h:982
void flo_unary_op_node_inplace(const opT &op, bool fence)
Definition funcimpl.h:2232
std::size_t size_local() const
Returns the number of coefficients in the function for each rank.
Definition mraimpl.h:1900
GenTensor< Q > values2coeffs(const keyT &key, const GenTensor< Q > &values) const
Definition funcimpl.h:1946
void plot_cube_kernel(archive::archive_ptr< Tensor< T > > ptr, const keyT &key, const coordT &plotlo, const coordT &plothi, const std::vector< long > &npt, bool eval_refine) const
Definition mraimpl.h:3447
T trace_local() const
Returns int(f(x),x) in local volume.
Definition mraimpl.h:3285
void print_grid(const std::string filename) const
Definition mraimpl.h:542
Future< std::pair< coeffT, double > > compress_spawn(const keyT &key, bool nonstandard, bool keepleaves, bool redundant1)
Invoked on node where key is local.
Definition mraimpl.h:3384
void replicate_on_hosts(bool fence=true)
Definition funcimpl.h:1124
bool get_autorefine() const
Definition mraimpl.h:334
int k
Wavelet order.
Definition funcimpl.h:972
void vtransform_doit(const std::shared_ptr< FunctionImpl< R, NDIM > > &right, const Tensor< Q > &c, const std::vector< std::shared_ptr< FunctionImpl< T, NDIM > > > &vleft, double tol)
Definition funcimpl.h:2764
MADNESS_CHECK(this->is_reconstructed())
void phi_for_mul(Level np, Translation lp, Level nc, Translation lc, Tensor< double > &phi) const
Compute the Legendre scaling functions for multiplication.
Definition mraimpl.h:3253
Future< std::pair< keyT, coeffT > > find_me(const keyT &key) const
find_me. Called by diff_bdry to get coefficients of boundary function
Definition mraimpl.h:3371
TensorType get_tensor_type() const
Definition mraimpl.h:319
void do_project_out(const coeffT &fcoeff, const std::pair< keyT, coeffT > gpair, const keyT &gkey, const Key< NDIM > &dest, const int dim) const
compute the inner product of two nodes of only some dimensions and accumulate on result
Definition funcimpl.h:7204
void remove_leaf_coefficients(const bool fence)
Definition mraimpl.h:1531
void insert_zero_down_to_initial_level(const keyT &key)
Initialize nodes to zero function at initial_level of refinement.
Definition mraimpl.h:2602
void do_diff1(const DerivativeBase< T, NDIM > *D, const implT *f, const keyT &key, const std::pair< keyT, coeffT > &left, const std::pair< keyT, coeffT > &center, const std::pair< keyT, coeffT > &right)
Definition mraimpl.h:943
typedef TENSOR_RESULT_TYPE(T, R) resultT
void unary_op_node_inplace(const opT &op, bool fence)
Definition funcimpl.h:2141
T inner_adaptive_local(const std::shared_ptr< FunctionFunctorInterface< T, NDIM > > f, const bool leaf_refine) const
Definition funcimpl.h:6806
void do_print_tree_json(const keyT &key, std::multimap< Level, std::tuple< tranT, std::string > > &data, Level maxlevel) const
Functor for the do_print_tree_json method.
Definition mraimpl.h:2757
std::multimap< Key< FDIM >, std::list< Key< CDIM > > > recur_down_for_contraction_map(const keyT &key, const nodeT &node, const std::array< int, CDIM > &v_this, const std::array< int, CDIM > &v_other, const std::set< Key< ODIM > > &ij_other_list, const std::map< Key< CDIM >, double > &j_other_list, bool this_first, const double thresh)
make a map of all nodes that will contribute to a partial inner product
Definition funcimpl.h:6379
std::shared_ptr< FunctionImpl< T, NDIM > > pimplT
pointer to this class
Definition funcimpl.h:951
TENSOR_RESULT_TYPE(T, R) dot_local(const FunctionImpl< R
Returns the dot product ASSUMING same distribution.
void finalize_sum()
after summing up we need to do some cleanup;
Definition mraimpl.h:1830
std::enable_if< NDIM==FDIM >::type read_grid(const std::string keyfile, const std::string gridfile, std::shared_ptr< FunctionFunctorInterface< double, NDIM > > vnuc_functor)
read data from a grid
Definition funcimpl.h:1544
dcT coeffs
The coefficients.
Definition funcimpl.h:989
bool exists_and_is_leaf(const keyT &key) const
Definition mraimpl.h:1284
static std::complex< Real > conj(const std::complex< Real > &x)
Definition funcimpl.h:6098
void make_Vphi(const opT &leaf_op, const bool fence=true)
assemble the function V*phi using V and phi given from the functor
Definition funcimpl.h:4386
void unaryXX(const FunctionImpl< Q, NDIM > *func, const opT &op, bool fence)
Definition funcimpl.h:3290
std::vector< std::pair< int, const coeffT * > > mapvecT
Type of the entry in the map returned by make_key_vec_map.
Definition funcimpl.h:5897
void project_out(FunctionImpl< T, NDIM-LDIM > *result, const FunctionImpl< T, LDIM > *gimpl, const int dim, const bool fence)
project the low-dim function g on the hi-dim function f: result(x) = <this(x,y) | g(y)>
Definition funcimpl.h:7000
void verify_tree() const
Verify tree is properly constructed ... global synchronization involved.
Definition mraimpl.h:111
void do_square_inplace2(const keyT &parent, const keyT &child, const tensorT &parent_coeff)
void gaxpy_inplace_reconstructed(const T &alpha, const FunctionImpl< Q, NDIM > &g, const R &beta, const bool fence)
Definition funcimpl.h:1212
void undo_replicate(bool fence=true)
Definition funcimpl.h:1129
void set_tensor_args(const TensorArgs &t)
Definition mraimpl.h:325
GenTensor< Q > fcube_for_mul(const keyT &child, const keyT &parent, const GenTensor< Q > &coeff) const
Compute the function values for multiplication.
Definition funcimpl.h:1995
Range< typename dcT::const_iterator > rangeT
Definition funcimpl.h:5688
std::size_t real_size() const
Returns the number of coefficients in the function ... collective global sum.
Definition mraimpl.h:1918
bool exists_and_has_children(const keyT &key) const
Definition mraimpl.h:1279
void sum_down_spawn(const keyT &key, const coeffT &s)
is this the same as trickle_down() ?
Definition mraimpl.h:876
void multi_to_multi_op_values(const opT &op, const std::vector< implT * > &vin, std::vector< implT * > &vout, const bool fence=true)
Inplace operate on many functions (impl's) with an operator within a certain box.
Definition funcimpl.h:2891
long box_interior[1000]
Definition funcimpl.h:3334
keyT neighbor(const keyT &key, const keyT &disp, const array_of_bools< NDIM > &is_periodic) const
Returns key of general neighbor enforcing BC.
Definition mraimpl.h:3341
GenTensor< Q > NS_fcube_for_mul(const keyT &child, const keyT &parent, const GenTensor< Q > &coeff, const bool s_only) const
Compute the function values for multiplication.
Definition funcimpl.h:1865
rangeT range(coeffs.begin(), coeffs.end())
void norm_tree(bool fence)
compute for each FunctionNode the norm of the function inside that node
Definition mraimpl.h:1556
void gaxpy_inplace(const T &alpha, const FunctionImpl< Q, NDIM > &other, const R &beta, bool fence)
Inplace general bilinear operation.
Definition funcimpl.h:1292
const Tensor< double > cell
the size of the root cell in each dimension, unchangeable
Definition funcimpl.h:977
bool has_leaves() const
Definition mraimpl.h:288
bool verify_parents_and_children() const
check that parents and children are consistent
Definition mraimpl.h:119
void apply_source_driven(opT &op, const FunctionImpl< R, NDIM > &f, bool fence)
similar to apply, but for low rank coeffs
Definition funcimpl.h:5241
void distribute(std::shared_ptr< WorldDCPmapInterface< Key< NDIM > > > newmap) const
Definition funcimpl.h:1136
int get_special_level() const
Definition funcimpl.h:968
void reconstruct_op(const keyT &key, const coeffT &s, const bool accumulate_NS=true)
Definition mraimpl.h:2101
tensorT gaxpy_ext_node(keyT key, Tensor< L > lc, T(*f)(const coordT &), T alpha, T beta) const
Definition funcimpl.h:6873
const coeffT parent_to_child(const coeffT &s, const keyT &parent, const keyT &child) const
Directly project parent coeffs to child coeffs.
Definition mraimpl.h:3268
WorldObject< FunctionImpl< T, NDIM > > woT
Base class world object type.
Definition funcimpl.h:947
void undo_redundant(const bool fence)
convert this from redundant to standard reconstructed form
Definition mraimpl.h:1547
GenTensor< T > coeffT
Type of tensor used to hold coeffs.
Definition funcimpl.h:956
const keyT & key0() const
Returns cdata.key0.
Definition mraimpl.h:394
double finalize_apply()
after apply we need to do some cleanup;
Definition mraimpl.h:1787
bool leaves_only
Definition funcimpl.h:5693
friend hashT hash_value(const FunctionImpl< T, NDIM > *pimpl)
Hash a pointer to FunctionImpl.
Definition funcimpl.h:7304
const dcT & get_coeffs() const
Definition mraimpl.h:343
FunctionImpl(World &world, const FunctionImpl< Q, NDIM > &other, const std::shared_ptr< WorldDCPmapInterface< Key< NDIM > > > &pmap, bool dozero)
Copy constructor.
Definition funcimpl.h:1085
T inner_ext_node(keyT key, tensorT c, const std::shared_ptr< FunctionFunctorInterface< T, NDIM > > f) const
Return the inner product with an external function on a specified function node.
Definition funcimpl.h:6672
double norm2sq_local() const
Returns the square of the local norm ... no comms.
Definition mraimpl.h:1839
const FunctionCommonData< T, NDIM > & get_cdata() const
Definition mraimpl.h:349
void sum_down(bool fence)
After 1d push operator must sum coeffs down the tree to restore correct scaling function coefficients...
Definition mraimpl.h:922
T inner_ext_recursive(keyT key, tensorT c, const std::shared_ptr< FunctionFunctorInterface< T, NDIM > > f, const bool leaf_refine, T old_inner=T(0)) const
Definition funcimpl.h:6689
bool noautorefine(const keyT &key, const tensorT &t) const
Always returns false (for when autorefine is not wanted)
Definition mraimpl.h:859
double truncate_tol(double tol, const keyT &key) const
Returns the truncation threshold according to truncate_method.
Definition mraimpl.h:649
void flo_unary_op_node_inplace(const opT &op, bool fence) const
Definition funcimpl.h:2242
bool autorefine_square_test(const keyT &key, const nodeT &t) const
Returns true if this block of coeffs needs autorefining.
Definition mraimpl.h:865
void erase(const Level &max_level)
truncate tree at a certain level
Definition mraimpl.h:739
void mulXX(const FunctionImpl< L, NDIM > *left, const FunctionImpl< R, NDIM > *right, double tol, bool fence)
Definition funcimpl.h:3262
void reconstruct(bool fence)
reconstruct this tree – respects fence
Definition mraimpl.h:1477
void multiply(const implT *f, const FunctionImpl< T, LDIM > *g, const int particle)
multiply f (a pair function of NDIM) with an orbital g (LDIM=NDIM/2)
Definition funcimpl.h:3678
coeffT assemble_coefficients(const keyT &key, const coeffT &coeff_ket, const coeffT &vpotential1, const coeffT &vpotential2, const tensorT &veri) const
given several coefficient tensors, assemble a result tensor
Definition mraimpl.h:1022
static void tnorm(const tensorT &t, double *lo, double *hi)
Computes norm of low/high-order polyn. coeffs for autorefinement test.
Definition mraimpl.h:3145
std::pair< bool, T > eval_local_only(const Vector< double, NDIM > &xin, Level maxlevel)
Evaluate function only if point is local returning (true,value); otherwise return (false,...
Definition mraimpl.h:2940
std::size_t max_depth() const
Returns the maximum depth of the tree ... collective ... global sum/broadcast.
Definition mraimpl.h:1865
std::size_t size() const
Returns the number of coefficients in the function ... collective global sum.
Definition mraimpl.h:1910
void reduce_rank(const double thresh, bool fence)
reduce the rank of the coefficients tensors
Definition mraimpl.h:1116
TreeState get_tree_state() const
Definition funcimpl.h:1358
void merge_trees(const T alpha, const FunctionImpl< Q, NDIM > &other, const R beta, const bool fence=true)
merge the trees of this and other, while multiplying them with the alpha or beta, resp
Definition funcimpl.h:1232
const Tensor< double > & get_cell() const
return the simulation cell
Definition funcimpl.h:1377
std::shared_ptr< FunctionFunctorInterface< T, NDIM > > get_functor()
Definition mraimpl.h:300
double do_apply_directed_screening(const opT *op, const keyT &key, const coeffT &coeff, const bool &do_kernel)
apply an operator on the coeffs c (at node key)
Definition funcimpl.h:5132
tensorT unfilter(const tensorT &s) const
Transform sums+differences at level n to sum coefficients at level n+1.
Definition mraimpl.h:1190
int get_initial_level() const
getter
Definition funcimpl.h:967
Tensor< T > eval_plot_cube(const coordT &plotlo, const coordT &plothi, const std::vector< long > &npt, const bool eval_refine=false) const
Definition mraimpl.h:3539
virtual ~FunctionImpl()
Definition funcimpl.h:1116
Vector< Translation, NDIM > tranT
Type of array holding translation.
Definition funcimpl.h:953
void change_tree_state(const TreeState finalstate, bool fence=true)
change the tree state of this function, might or might not respect fence!
Definition mraimpl.h:1416
Future< coeffT > truncate_reconstructed_spawn(const keyT &key, const double tol)
truncate using a tree in reconstructed form
Definition mraimpl.h:1602
GenTensor< Q > coeffs2values(const keyT &key, const GenTensor< Q > &coeff) const
Definition funcimpl.h:1813
FunctionImpl(const FunctionFactory< T, NDIM > &factory)
Initialize function impl from data in factory.
Definition funcimpl.h:1005
void map_and_mirror(const implT &f, const std::vector< long > &map, const std::vector< long > &mirror, bool fence)
map and mirror the translation index and the coefficients, result on this
Definition mraimpl.h:1085
Timer timer_lr_result
Definition funcimpl.h:997
void gaxpy(T alpha, const FunctionImpl< L, NDIM > &left, T beta, const FunctionImpl< R, NDIM > &right, bool fence)
Invoked by result to perform result += alpha*left+beta*right in wavelet basis.
Definition funcimpl.h:2092
void truncate(double tol, bool fence)
Truncate according to the threshold with optional global fence.
Definition mraimpl.h:378
void do_mul(const keyT &key, const Tensor< L > &left, const std::pair< keyT, Tensor< R > > &arg)
Functor for the mul method.
Definition funcimpl.h:2017
void copy_remote_coeffs_from_pid(const ProcessID pid, const FunctionImpl< Q, NDIM > &other)
Definition funcimpl.h:1175
void project_out2(const FunctionImpl< T, LDIM+NDIM > *f, const FunctionImpl< T, LDIM > *g, const int dim)
project the low-dim function g on the hi-dim function f: this(x) = <f(x,y) | g(y)>
Definition funcimpl.h:7146
double do_apply_kernel2(const opT *op, const Tensor< R > &c, const do_op_args< OPDIM > &args, const TensorArgs &apply_targs)
same as do_apply_kernel, but use full rank tensors as input and low rank tensors as output
Definition funcimpl.h:4819
static Tensor< TENSOR_RESULT_TYPE(T, R)> dot_local(const std::vector< const FunctionImpl< T, NDIM > * > &left, const std::vector< const FunctionImpl< R, NDIM > * > &right, bool sym)
Definition funcimpl.h:6156
Tensor< Q > coeffs2values(const keyT &key, const Tensor< Q > &coeff) const
Definition funcimpl.h:1939
Tensor< Q > values2coeffs(const keyT &key, const Tensor< Q > &values) const
Definition funcimpl.h:1953
void multi_to_multi_op_values_doit(const keyT &key, const opT &op, const std::vector< implT * > &vin, std::vector< implT * > &vout)
Inplace operate on many functions (impl's) with an operator within a certain box.
Definition funcimpl.h:2868
bool is_reconstructed() const
Returns true if the function is compressed.
Definition mraimpl.h:256
void replicate(bool fence=true)
Definition funcimpl.h:1120
double norm_tree_op(const keyT &key, const std::vector< Future< double > > &v)
Definition mraimpl.h:1564
void reset_timer()
Definition mraimpl.h:366
void refine_to_common_level(const std::vector< FunctionImpl< T, NDIM > * > &v, const std::vector< tensorT > &c, const keyT key)
Refine multiple functions down to the same finest level.
Definition mraimpl.h:769
int get_k() const
Definition mraimpl.h:340
void dirac_convolution_op(const keyT &key, const nodeT &node, FunctionImpl< T, LDIM > *f) const
The operator.
Definition funcimpl.h:2158
FunctionImpl< T, NDIM > implT
Type of this class (implementation)
Definition funcimpl.h:950
void eval(const Vector< double, NDIM > &xin, const keyT &keyin, const typename Future< T >::remote_refT &ref)
Evaluate the function at a point in simulation coordinates.
Definition mraimpl.h:2896
bool truncate_op(const keyT &key, double tol, const std::vector< Future< bool > > &v)
Definition mraimpl.h:2669
void zero_norm_tree()
Definition mraimpl.h:1301
std::size_t max_local_depth() const
Returns the maximum local depth of the tree ... no communications.
Definition mraimpl.h:1851
tensorT project(const keyT &key) const
Definition mraimpl.h:2814
double thresh
Screening threshold.
Definition funcimpl.h:973
double check_symmetry_local() const
Returns some asymmetry measure ... no comms.
Definition mraimpl.h:755
Future< double > get_norm_tree_recursive(const keyT &key) const
Definition mraimpl.h:2835
bool is_redundant_after_merge() const
Returns true if the function is redundant_after_merge.
Definition mraimpl.h:268
void mulXXvec(const FunctionImpl< L, NDIM > *left, const std::vector< const FunctionImpl< R, NDIM > * > &vright, const std::vector< FunctionImpl< T, NDIM > * > &vresult, double tol, bool fence)
Definition funcimpl.h:3319
Key< NDIM > keyT
Type of key.
Definition funcimpl.h:954
friend hashT hash_value(const std::shared_ptr< FunctionImpl< T, NDIM > > impl)
Hash a shared_ptr to FunctionImpl.
Definition funcimpl.h:7314
std::vector< Vector< double, NDIM > > special_points
special points for further refinement (needed for composite functions or multiplication)
Definition funcimpl.h:976
bool truncate_on_project
If true projection inserts at level n-1 not n.
Definition funcimpl.h:981
AtomicInt small
Definition funcimpl.h:1001
static void do_dot_localX(const typename mapT::iterator lstart, const typename mapT::iterator lend, typename FunctionImpl< R, NDIM >::mapT *rmap_ptr, const bool sym, Tensor< TENSOR_RESULT_TYPE(T, R)> *result_ptr, Mutex *mutex)
Definition funcimpl.h:6053
bool is_on_demand() const
Definition mraimpl.h:283
double err_box(const keyT &key, const nodeT &node, const opT &func, int npt, const Tensor< double > &qx, const Tensor< double > &quad_phit, const Tensor< double > &quad_phiw) const
Returns the square of the error norm in the box labeled by key.
Definition funcimpl.h:5529
void accumulate_timer(const double time) const
Definition mraimpl.h:352
void trickle_down_op(const keyT &key, const coeffT &s)
sum all the contributions from all scales after applying an operator in mod-NS form
Definition mraimpl.h:1374
static void do_inner_localX(const typename mapT::iterator lstart, const typename mapT::iterator lend, typename FunctionImpl< R, NDIM >::mapT *rmap_ptr, const bool sym, Tensor< TENSOR_RESULT_TYPE(T, R) > *result_ptr, Mutex *mutex)
Definition funcimpl.h:5972
void mulXXveca(const keyT &key, const FunctionImpl< L, NDIM > *left, const Tensor< L > &lcin, const std::vector< const FunctionImpl< R, NDIM > * > vrightin, const std::vector< Tensor< R > > &vrcin, const std::vector< FunctionImpl< T, NDIM > * > vresultin, double tol)
Definition funcimpl.h:2955
void set_thresh(double value)
Definition mraimpl.h:331
Tensor< double > print_plane_local(const int xaxis, const int yaxis, const coordT &el2)
collect the data for a plot of the MRA structure locally on each node
Definition mraimpl.h:423
void sock_it_to_me_too(const keyT &key, const RemoteReference< FutureImpl< std::pair< keyT, coeffT > > > &ref) const
Definition mraimpl.h:2874
void broaden_op(const keyT &key, const std::vector< Future< bool > > &v)
Definition mraimpl.h:1290
void print_plane(const std::string filename, const int xaxis, const int yaxis, const coordT &el2)
Print a plane ("xy", "xz", or "yz") containing the point x to file.
Definition mraimpl.h:403
void print_tree(std::ostream &os=std::cout, Level maxlevel=10000) const
Definition mraimpl.h:2697
void project_refine_op(const keyT &key, bool do_refine, const std::vector< Vector< double, NDIM > > &specialpts)
Definition mraimpl.h:2480
void scale_oop(const Q q, const FunctionImpl< F, NDIM > &f, bool fence)
Out-of-place scale by a constant.
Definition funcimpl.h:7281
T typeT
Definition funcimpl.h:949
std::size_t tree_size() const
Returns the size of the tree structure of the function ... collective global sum.
Definition mraimpl.h:1891
ConcurrentHashMap< keyT, mapvecT > mapT
Type of the map returned by make_key_vec_map.
Definition funcimpl.h:5900
void add_scalar_inplace(T t, bool fence)
Adds a constant to the function. Local operation, optional fence.
Definition mraimpl.h:2561
void forward_traverse(const coeff_opT &coeff_op, const apply_opT &apply_op, const keyT &key) const
traverse a non-existing tree
Definition funcimpl.h:3772
tensorT downsample(const keyT &key, const std::vector< Future< coeffT > > &v) const
downsample the sum coefficients of level n+1 to sum coeffs on level n
Definition mraimpl.h:1210
void abs_square_inplace(bool fence)
Definition mraimpl.h:3248
FunctionImpl(const FunctionImpl< Q, NDIM > &other, const std::shared_ptr< WorldDCPmapInterface< Key< NDIM > > > &pmap, bool dozero)
Copy constructor.
Definition funcimpl.h:1071
void refine(const opT &op, bool fence)
Definition funcimpl.h:4632
static mapT make_key_vec_map(const std::vector< const FunctionImpl< T, NDIM > * > &v)
Returns map of union of local keys to vector of indexes of functions containing that key.
Definition funcimpl.h:5921
void put_in_box(ProcessID from, long nl, long ni) const
Definition mraimpl.h:824
void unary_op_value_inplace(const opT &op, bool fence)
Definition funcimpl.h:2935
std::pair< const keyT, nodeT > datumT
Type of entry in container.
Definition funcimpl.h:958
Timer timer_accumulate
Definition funcimpl.h:995
TensorArgs get_tensor_args() const
Definition mraimpl.h:322
void unaryXXa(const keyT &key, const FunctionImpl< Q, NDIM > *func, const opT &op)
Definition funcimpl.h:3237
void make_Vphi_only(const opT &leaf_op, FunctionImpl< T, NDIM > *ket, FunctionImpl< T, LDIM > *v1, FunctionImpl< T, LDIM > *v2, FunctionImpl< T, LDIM > *p1, FunctionImpl< T, LDIM > *p2, FunctionImpl< T, NDIM > *eri, const bool fence=true)
assemble the function V*phi using V and phi given from the functor
Definition funcimpl.h:4447
void average(const implT &rhs)
take the average of two functions, similar to: this=0.5*(this+rhs)
Definition mraimpl.h:1097
void recursive_apply(opT &apply_op, const FunctionImpl< T, LDIM > *fimpl, const FunctionImpl< T, LDIM > *gimpl, const bool fence)
traverse a non-existing tree, make its coeffs and apply an operator
Definition funcimpl.h:5282
void diff(const DerivativeBase< T, NDIM > *D, const implT *f, bool fence)
Definition mraimpl.h:955
void square_inplace(bool fence)
Pointwise squaring of function with optional global fence.
Definition mraimpl.h:3237
void remove_internal_coefficients(const bool fence)
Definition mraimpl.h:1526
void compute_snorm_and_dnorm(bool fence=true)
compute norm of s and d coefficients for all nodes
Definition mraimpl.h:1140
std::vector< unsigned char > serialize_remote_coeffs()
invoked by copy_remote_coeffs_from_pid to serialize local coeffs
Definition funcimpl.h:1183
long box_leaf[1000]
Definition funcimpl.h:3333
void standard(bool fence)
Changes non-standard compressed form to standard compressed form.
Definition mraimpl.h:1774
void multiop_values_doit(const keyT &key, const opT &op, const std::vector< implT * > &v)
Definition funcimpl.h:2826
bool is_nonstandard_with_leaves() const
Definition mraimpl.h:278
GenTensor< Q > values2NScoeffs(const keyT &key, const GenTensor< Q > &values) const
convert function values of the a child generation directly to NS coeffs
Definition funcimpl.h:1914
int truncate_mode
0=default=(|d|<thresh), 1=(|d|<thresh/2^n), 2=(|d|<thresh/4^n);
Definition funcimpl.h:979
void multiop_values(const opT &op, const std::vector< implT * > &v)
Definition funcimpl.h:2843
GenTensor< Q > NScoeffs2values(const keyT &key, const GenTensor< Q > &coeff, const bool s_only) const
convert S or NS coeffs to values on a 2k grid of the children
Definition funcimpl.h:1829
static std::enable_if_t< std::is_floating_point_v< Real >, Real > conj(const Real x)
Definition funcimpl.h:6093
FunctionNode holds the coefficients, etc., at each node of the 2^NDIM-tree.
Definition funcimpl.h:127
FunctionNode< Q, NDIM > convert() const
Copy with possible type conversion of coefficients, copying all other state.
Definition funcimpl.h:194
GenTensor< T > coeffT
Definition funcimpl.h:129
bool has_coeff() const
Returns true if there are coefficients in this node.
Definition funcimpl.h:200
void recompute_snorm_and_dnorm(const FunctionCommonData< T, NDIM > &cdata)
Definition funcimpl.h:335
FunctionNode(const coeffT &coeff, bool has_children=false)
Constructor from given coefficients with optional children.
Definition funcimpl.h:156
FunctionNode()
Default constructor makes node without coeff or children.
Definition funcimpl.h:146
void serialize(Archive &ar)
Definition funcimpl.h:458
void consolidate_buffer(const TensorArgs &args)
Definition funcimpl.h:444
double get_dnorm() const
return the precomputed norm of the (virtual) d coefficients
Definition funcimpl.h:316
size_t size() const
Returns the number of coefficients in this node.
Definition funcimpl.h:242
void set_has_children_recursive(const typename FunctionNode< T, NDIM >::dcT &c, const Key< NDIM > &key)
Sets has_children attribute to true recurring up to ensure connected.
Definition funcimpl.h:259
FunctionNode< T, NDIM > & operator=(const FunctionNode< T, NDIM > &other)
Definition funcimpl.h:176
double snorm
norm of the s coefficients
Definition funcimpl.h:141
void clear_coeff()
Clears the coefficients (has_coeff() will subsequently return false)
Definition funcimpl.h:295
Tensor< T > tensorT
Definition funcimpl.h:130
coeffT buffer
The coefficients, if any.
Definition funcimpl.h:139
T trace_conj(const FunctionNode< T, NDIM > &rhs) const
Definition funcimpl.h:453
void scale(Q a)
Scale the coefficients of this node.
Definition funcimpl.h:301
bool is_leaf() const
Returns true if this does not have children.
Definition funcimpl.h:213
void set_has_children(bool flag)
Sets has_children attribute to value of flag.
Definition funcimpl.h:254
void accumulate(const coeffT &t, const typename FunctionNode< T, NDIM >::dcT &c, const Key< NDIM > &key, const TensorArgs &args)
Accumulate inplace and if necessary connect node to parent.
Definition funcimpl.h:416
double get_norm_tree() const
Gets the value of norm_tree.
Definition funcimpl.h:311
bool _has_children
True if there are children.
Definition funcimpl.h:138
FunctionNode(const coeffT &coeff, double norm_tree, double snorm, double dnorm, bool has_children)
Definition funcimpl.h:166
void set_snorm(const double sn)
set the precomputed norm of the (virtual) s coefficients
Definition funcimpl.h:321
coeffT _coeffs
The coefficients, if any.
Definition funcimpl.h:136
void accumulate2(const tensorT &t, const typename FunctionNode< T, NDIM >::dcT &c, const Key< NDIM > &key)
Accumulate inplace and if necessary connect node to parent.
Definition funcimpl.h:383
void reduceRank(const double &eps)
reduces the rank of the coefficients (if applicable)
Definition funcimpl.h:249
WorldContainer< Key< NDIM >, FunctionNode< T, NDIM > > dcT
Definition funcimpl.h:144
void gaxpy_inplace(const T &alpha, const FunctionNode< Q, NDIM > &other, const R &beta)
General bi-linear operation — this = this*alpha + other*beta.
Definition funcimpl.h:365
double _norm_tree
After norm_tree will contain norm of coefficients summed up tree.
Definition funcimpl.h:137
void set_is_leaf(bool flag)
Sets has_children attribute to value of !flag.
Definition funcimpl.h:280
void print_json(std::ostream &s) const
Definition funcimpl.h:466
double get_snorm() const
get the precomputed norm of the (virtual) s coefficients
Definition funcimpl.h:331
const coeffT & coeff() const
Returns a const reference to the tensor containing the coeffs.
Definition funcimpl.h:237
FunctionNode(const coeffT &coeff, double norm_tree, bool has_children)
Definition funcimpl.h:161
bool has_children() const
Returns true if this node has children.
Definition funcimpl.h:207
void set_coeff(const coeffT &coeffs)
Takes a shallow copy of the coeff — same as this->coeff()=coeff.
Definition funcimpl.h:285
void set_dnorm(const double dn)
set the precomputed norm of the (virtual) d coefficients
Definition funcimpl.h:326
double dnorm
norm of the d coefficients, also defined if there are no d coefficients
Definition funcimpl.h:140
bool is_invalid() const
Returns true if this node is invalid (no coeffs and no children)
Definition funcimpl.h:219
FunctionNode(const FunctionNode< T, NDIM > &other)
Definition funcimpl.h:170
coeffT & coeff()
Returns a non-const reference to the tensor containing the coeffs.
Definition funcimpl.h:227
void set_norm_tree(double norm_tree)
Sets the value of norm_tree.
Definition funcimpl.h:306
Implements the functionality of futures.
Definition future.h:75
A future is a possibly yet unevaluated value.
Definition future.h:370
remote_refT remote_ref(World &world) const
Returns a structure used to pass references to another process.
Definition future.h:672
RemoteReference< FutureImpl< T > > remote_refT
Definition future.h:395
Definition lowranktensor.h:59
bool is_of_tensortype(const TensorType &tt) const
Definition gentensor.h:225
GenTensor convert(const TensorArgs &targs) const
Definition gentensor.h:198
long dim(const int i) const
return the number of entries in dimension i
Definition lowranktensor.h:391
Tensor< T > full_tensor_copy() const
Definition gentensor.h:206
long ndim() const
Definition lowranktensor.h:386
void add_SVD(const GenTensor< T > &rhs, const double &eps)
Definition gentensor.h:235
constexpr bool is_full_tensor() const
Definition gentensor.h:224
const Tensor< T > & get_tensor() const
Definition gentensor.h:203
bool has_no_data() const
Definition gentensor.h:211
void normalize()
Definition gentensor.h:218
GenTensor< T > & emul(const GenTensor< T > &other)
Inplace multiply by corresponding elements of argument Tensor.
Definition lowranktensor.h:637
float_scalar_type normf() const
Definition lowranktensor.h:406
double svd_normf() const
Definition gentensor.h:213
SRConf< T > config() const
Definition gentensor.h:237
void reduce_rank(const double &eps)
Definition gentensor.h:217
long rank() const
Definition gentensor.h:212
const Tensor< T > & full_tensor() const
Definition gentensor.h:200
long size() const
Definition lowranktensor.h:488
SVDTensor< T > & get_svdtensor()
Definition gentensor.h:228
TensorType tensor_type() const
Definition gentensor.h:221
bool has_data() const
Definition gentensor.h:210
Tensor< T > reconstruct_tensor() const
Definition gentensor.h:199
GenTensor & gaxpy(const T alpha, const GenTensor &other, const T beta)
Definition lowranktensor.h:586
bool is_assigned() const
Definition gentensor.h:209
IsSupported< TensorTypeData< Q >, GenTensor< T > & >::type scale(Q fac)
Inplace multiplication by scalar of supported type (legacy name)
Definition lowranktensor.h:426
constexpr bool is_svd_tensor() const
Definition gentensor.h:222
Iterates in lexical order thru all children of a key.
Definition key.h:548
Key is the index for a node of the 2^NDIM-tree.
Definition key.h:70
Key< NDIM+LDIM > merge_with(const Key< LDIM > &rhs) const
merge with other key (ie concatenate), use level of rhs, not of this
Definition key.h:487
Level level() const
Definition key.h:169
bool is_valid() const
Checks if a key is valid.
Definition key.h:124
hashT hash() const
Definition key.h:158
Key< NDIM-VDIM > extract_complement_key(const std::array< int, VDIM > &v) const
extract a new key with the Translations complementary to the ones indicated in the v array
Definition key.h:473
Key< VDIM > extract_key(const std::array< int, VDIM > &v) const
extract a new key with the Translations indicated in the v array
Definition key.h:465
Key parent(int generation=1) const
Returns the key of the parent.
Definition key.h:290
const Vector< Translation, NDIM > & translation() const
Definition key.h:174
void break_apart(Key< LDIM > &key1, Key< KDIM > &key2) const
break key into two low-dimensional keys
Definition key.h:424
A pmap that locates children on odd levels with their even level parents.
Definition funcimpl.h:105
LevelPmap(World &world)
Definition funcimpl.h:111
const int nproc
Definition funcimpl.h:107
LevelPmap()
Definition funcimpl.h:109
ProcessID owner(const keyT &key) const
Find the owner of a given key.
Definition funcimpl.h:114
Definition funcimpl.h:77
Mutex using pthread mutex operations.
Definition worldmutex.h:131
void unlock() const
Free a mutex owned by this thread.
Definition worldmutex.h:165
void lock() const
Acquire the mutex waiting if necessary.
Definition worldmutex.h:155
Range, vaguely a la Intel TBB, to encapsulate a random-access, STL-like start and end iterator with c...
Definition range.h:64
Simple structure used to manage references/pointers to remote instances.
Definition worldref.h:395
Definition SVDTensor.h:42
A simple process map.
Definition funcimpl.h:86
SimplePmap(World &world)
Definition funcimpl.h:92
const int nproc
Definition funcimpl.h:88
const ProcessID me
Definition funcimpl.h:89
ProcessID owner(const keyT &key) const
Maps key to processor.
Definition funcimpl.h:95
A slice defines a sub-range or patch of a dimension.
Definition slice.h:103
static TaskAttributes hipri()
Definition thread.h:456
Traits class to specify support of numeric types.
Definition type_data.h:56
A tensor is a multidimensional array.
Definition tensor.h:318
float_scalar_type normf() const
Returns the Frobenius norm of the tensor.
Definition tensor.h:1727
Tensor< T > & gaxpy(T alpha, const Tensor< T > &other, T beta)
Inplace generalized saxpy ... this = this*alpha + other*beta.
Definition tensor.h:1806
T sum() const
Returns the sum of all elements of the tensor.
Definition tensor.h:1663
Tensor< T > reshape(int ndimnew, const long *d)
Returns new view/tensor reshaping size/number of dimensions to conforming tensor.
Definition tensor.h:1385
T * ptr()
Returns a pointer to the internal data.
Definition tensor.h:1841
Tensor< T > mapdim(const std::vector< long > &map)
Returns new view/tensor permuting the dimensions.
Definition tensor.h:1625
IsSupported< TensorTypeData< Q >, Tensor< T > & >::type scale(Q x)
Inplace multiplication by scalar of supported type (legacy name)
Definition tensor.h:687
Tensor< T > & emul(const Tensor< T > &t)
Inplace multiply by corresponding elements of argument Tensor.
Definition tensor.h:1800
bool has_data() const
Definition tensor.h:1903
Tensor< T > fusedim(long i)
Returns new view/tensor fusing contiguous dimensions i and i+1.
Definition tensor.h:1588
Tensor< T > flat()
Returns new view/tensor rehshaping to flat (1-d) tensor.
Definition tensor.h:1556
Tensor< T > & conj()
Inplace complex conjugate.
Definition tensor.h:717
Definition function_common_data.h:169
void accumulate(const double time) const
accumulate timer
Definition function_common_data.h:183
A simple, fixed dimension vector.
Definition vector.h:64
Makes a distributed container with specified attributes.
Definition worlddc.h:1127
void process_pending()
Process pending messages.
Definition worlddc.h:1453
bool find(accessor &acc, const keyT &key)
Write access to LOCAL value by key. Returns true if found, false otherwise (always false for remote).
Definition worlddc.h:1274
bool probe(const keyT &key) const
Returns true if local data is immediately available (no communication)
Definition worlddc.h:1311
iterator begin()
Returns an iterator to the beginning of the local data (no communication)
Definition worlddc.h:1357
bool is_replicated() const
Definition worlddc.h:1227
ProcessID owner(const keyT &key) const
Returns processor that logically owns key (no communication)
Definition worlddc.h:1321
implT::const_iterator const_iterator
Definition worlddc.h:1135
void replicate(bool fence=true)
replicates this WorldContainer on all ProcessIDs
Definition worlddc.h:1249
void erase(const keyT &key)
Erases entry from container (non-blocking comm if remote)
Definition worlddc.h:1392
void replace(const pairT &datum)
Inserts/replaces key+value pair (non-blocking communication if key not local)
Definition worlddc.h:1261
iterator end()
Returns an iterator past the end of the local data (no communication)
Definition worlddc.h:1371
const std::shared_ptr< WorldDCPmapInterface< keyT > > & get_pmap() const
Returns shared pointer to the process mapping.
Definition worlddc.h:1429
bool insert(accessor &acc, const keyT &key)
Write access to LOCAL value by key. Returns true if inserted, false if already exists (throws if remo...
Definition worlddc.h:1288
bool is_distributed() const
Definition worlddc.h:1223
implT::iterator iterator
Definition worlddc.h:1134
std::size_t size() const
Returns the number of local entries (no communication)
Definition worlddc.h:1422
Future< REMFUTURE(MEMFUN_RETURNT(memfunT))> task(const keyT &key, memfunT memfun, const TaskAttributes &attr=TaskAttributes())
Adds task "resultT memfun()" in process owning item (non-blocking comm if remote)
Definition worlddc.h:1713
bool is_local(const keyT &key) const
Returns true if the key maps to the local processor (no communication)
Definition worlddc.h:1328
bool is_host_replicated() const
Definition worlddc.h:1231
Future< MEMFUN_RETURNT(memfunT)> send(const keyT &key, memfunT memfun)
Sends message "resultT memfun()" to item (non-blocking comm if remote)
Definition worlddc.h:1470
void replicate_on_hosts(bool fence=true)
replicates this WorldContainer on all hosts (one PID per host)
Definition worlddc.h:1255
implT::accessor accessor
Definition worlddc.h:1136
Interface to be provided by any process map.
Definition worlddc.h:122
void fence(bool debug=false)
Synchronizes all processes in communicator AND globally ensures no pending AM or tasks.
Definition worldgop.cc:176
Implements most parts of a globally addressable object (via unique ID).
Definition world_object.h:366
const uniqueidT & id() const
Returns the globally unique object ID.
Definition world_object.h:713
void process_pending()
To be called from derived constructor to process pending messages.
Definition world_object.h:658
ProcessID me
Rank of self.
Definition world_object.h:387
detail::task_result_type< memfnT >::futureT send(ProcessID dest, memfnT memfn) const
Definition world_object.h:733
detail::task_result_type< memfnT >::futureT task(ProcessID dest, memfnT memfn, const TaskAttributes &attr=TaskAttributes()) const
Sends task to derived class method returnT (this->*memfn)().
Definition world_object.h:1007
Future< bool > for_each(const rangeT &range, const opT &op)
Apply op(item) on all items in range.
Definition world_task_queue.h:572
void add(TaskInterface *t)
Add a new local task, taking ownership of the pointer.
Definition world_task_queue.h:466
Future< resultT > reduce(const rangeT &range, const opT &op)
Reduce op(item) for all items in range using op(sum,op(item)).
Definition world_task_queue.h:527
A parallel world class.
Definition world.h:132
static World * world_from_id(std::uint64_t id)
Convert a World ID to a World pointer.
Definition world.h:492
WorldTaskQueue & taskq
Task queue.
Definition world.h:206
std::vector< uniqueidT > get_object_ids() const
Returns a vector of all unique IDs in this World.
Definition world.h:468
ProcessID rank() const
Returns the process rank in this World (same as MPI_Comm_rank()).
Definition world.h:320
static std::vector< unsigned long > get_world_ids()
return a vector containing all world ids
Definition world.h:476
ProcessID size() const
Returns the number of processes in this World (same as MPI_Comm_size()).
Definition world.h:330
unsigned long id() const
Definition world.h:315
WorldGopInterface & gop
Global operations.
Definition world.h:207
std::optional< T * > ptr_from_id(uniqueidT id) const
Look up a local pointer from a world-wide unique ID.
Definition world.h:416
ProcessID random_proc()
Returns a random process number; that is, an integer in [0,world.size()).
Definition world.h:591
Wraps an archive around an STL vector for input.
Definition vector_archive.h:101
Wraps an archive around an STL vector for output.
Definition vector_archive.h:55
Wrapper for an opaque pointer for serialization purposes.
Definition archive.h:851
syntactic sugar for std::array<bool, N>
Definition array_of_bools.h:19
Class for unique global IDs.
Definition uniqueid.h:53
unsigned long get_obj_id() const
Access the object ID.
Definition uniqueid.h:97
unsigned long get_world_id() const
Access the World ID.
Definition uniqueid.h:90
static const double R
Definition csqrt.cc:46
double(* f1)(const coord_3d &)
Definition derivatives.cc:55
char * p(char *buf, const char *name, int k, int initial_level, double thresh, int order)
Definition derivatives.cc:72
static double lo
Definition dirac-hatom.cc:23
@ upper
Definition dirac-hatom.cc:15
Provides FunctionDefaults and utilities for coordinate transformation.
archive_array< unsigned char > wrap_opaque(const T *, unsigned int)
Factory function to wrap a pointer to contiguous data as an opaque (uchar) archive_array.
Definition archive.h:926
Tensor< typename Tensor< T >::scalar_type > arg(const Tensor< T > &t)
Return a new tensor holding the argument of each element of t (complex types only)
Definition tensor.h:2643
Tensor< TENSOR_RESULT_TYPE(T, Q) > & fast_transform(const Tensor< T > &t, const Tensor< Q > &c, Tensor< TENSOR_RESULT_TYPE(T, Q) > &result, Tensor< TENSOR_RESULT_TYPE(T, Q) > &workspace)
Restricted but heavily optimized form of transform()
Definition tensor.h:2460
const double beta
Definition gygi_soltion.cc:62
static const double v
Definition hatom_sf_dirac.cc:20
Provides IndexIterator.
Tensor< double > op(const Tensor< double > &x)
Definition kain.cc:508
Multidimension Key for MRA tree and associated iterators.
static double pow(const double *a, const double *b)
Definition lda.h:74
#define MADNESS_CHECK(condition)
Check a condition — even in a release build the condition is always evaluated so it can have side eff...
Definition madness_exception.h:182
#define MADNESS_EXCEPTION(msg, value)
Macro for throwing a MADNESS exception.
Definition madness_exception.h:119
#define MADNESS_ASSERT(condition)
Assert a condition that should be free of side-effects since in release builds this might be a no-op.
Definition madness_exception.h:134
#define MADNESS_CHECK_THROW(condition, msg)
Check a condition — even in a release build the condition is always evaluated so it can have side eff...
Definition madness_exception.h:207
Header to declare stuff which has not yet found a home.
constexpr double pi
Mathematical constant .
Definition constants.h:48
MemFuncWrapper< objT *, memfnT, typename result_of< memfnT >::type > wrap_mem_fn(objT &obj, memfnT memfn)
Create a member function wrapper (MemFuncWrapper) from an object and a member function pointer.
Definition mem_func_wrapper.h:251
void combine_hash(hashT &seed, hashT hash)
Internal use only.
Definition worldhash.h:248
Namespace for all elements and tools of MADNESS.
Definition DFParameters.h:10
std::ostream & operator<<(std::ostream &os, const particle< PDIM > &p)
Definition lowrankfunction.h:401
static const char * filename
Definition legendre.cc:96
static const std::vector< Slice > ___
Entire dimension.
Definition slice.h:128
static double cpu_time()
Returns the cpu time in seconds relative to an arbitrary origin.
Definition timers.h:127
GenTensor< TENSOR_RESULT_TYPE(R, Q)> general_transform(const GenTensor< R > &t, const Tensor< Q > c[])
Definition gentensor.h:274
bool nearlyEqual(double a, double b, double epsilon=1e-9)
Definition numerics.cc:11
void finalize()
Call this once at the very end of your main program instead of MPI_Finalize().
Definition world.cc:235
void norm_tree(World &world, const std::vector< Function< T, NDIM > > &v, bool fence=true)
Makes the norm tree for all functions in a vector.
Definition vmra.h:1206
std::vector< Function< TENSOR_RESULT_TYPE(T, R), NDIM > > transform(World &world, const std::vector< Function< T, NDIM > > &v, const Tensor< R > &c, bool fence=true)
Transforms a vector of functions according to new[i] = sum[j] old[j]*c[j,i].
Definition vmra.h:731
TreeState
Definition funcdefaults.h:59
@ nonstandard_after_apply
s and d coeffs, state after operator application
Definition funcdefaults.h:64
@ redundant_after_merge
s coeffs everywhere, must be summed up to yield the result
Definition funcdefaults.h:66
@ reconstructed
s coeffs at the leaves only
Definition funcdefaults.h:60
@ nonstandard
s and d coeffs in internal nodes
Definition funcdefaults.h:62
@ redundant
s coeffs everywhere
Definition funcdefaults.h:65
static Tensor< double > weights[max_npt+1]
Definition legendre.cc:99
int64_t Translation
Definition key.h:58
Key< NDIM > displacement(const Key< NDIM > &source, const Key< NDIM > &target)
given a source and a target, return the displacement in translation
Definition key.h:533
static const Slice _(0,-1, 1)
std::shared_ptr< FunctionFunctorInterface< double, 3 > > func(new opT(g))
void change_tensor_type(GenTensor< T > &t, const TensorArgs &targs)
change representation to targ.tt
Definition gentensor.h:284
int Level
Definition key.h:59
std::enable_if< std::is_base_of< ProjectorBase, projT >::value, OuterProjector< projT, projQ > >::type outer(const projT &p0, const projQ &p1)
Definition projector.h:457
int RandomValue< int >()
Random int.
Definition ran.cc:250
bool has_data(const PropertyResults &p)
Definition Results.h:413
static double pop(std::vector< double > &v)
Definition SCF.cc:115
void print(const T &t, const Ts &... ts)
Print items to std::cout (items separated by spaces) and terminate with a new line.
Definition print.h:226
Tensor< T > fcube(const Key< NDIM > &, T(*f)(const Vector< double, NDIM > &), const Tensor< double > &)
Definition mraimpl.h:2155
TensorType
low rank representations of tensors (see gentensor.h)
Definition gentensor.h:120
@ TT_2D
Definition gentensor.h:120
@ TT_FULL
Definition gentensor.h:120
NDIM & f
Definition mra.h:2604
void error(const char *msg)
Definition world.cc:142
NDIM const Function< R, NDIM > & g
Definition mra.h:2604
std::size_t hashT
The hash value type.
Definition worldhash.h:145
static const int kmax
Definition twoscale.cc:52
GenTensor< TENSOR_RESULT_TYPE(R, Q)> transform_dir(const GenTensor< R > &t, const Tensor< Q > &c, const int axis)
Definition lowranktensor.h:1106
Function< T, CCPairFunction< T, NDIM >::LDIM > inner(const CCPairFunction< T, NDIM > &c, const Function< T, CCPairFunction< T, NDIM >::LDIM > &f, const std::tuple< int, int, int > v1, const std::tuple< int, int, int > v2)
Definition ccpairfunction.h:993
void scale(World &world, std::vector< Function< T, NDIM > > &v, const std::vector< Q > &factors, bool fence=true)
Scales inplace a vector of functions by distinct values.
Definition vmra.h:847
std::string name(const FuncType &type, const int ex=-1)
Definition ccpairfunction.h:28
void mxmT(long dimi, long dimj, long dimk, T *MADNESS_RESTRICT c, const T *a, const T *b)
Matrix += Matrix * matrix transpose ... MKL interface version.
Definition mxm.h:225
Function< T, NDIM > copy(const Function< T, NDIM > &f, const std::shared_ptr< WorldDCPmapInterface< Key< NDIM > > > &pmap, bool fence=true)
Create a new copy of the function with different distribution and optional fence.
Definition mra.h:2172
static const int MAXK
The maximum wavelet order presently supported.
Definition funcdefaults.h:54
Definition mraimpl.h:51
static long abs(long a)
Definition tensor.h:219
const double cc
Definition navstokes_cosines.cc:107
static const double b
Definition nonlinschro.cc:119
static const double d
Definition nonlinschro.cc:121
static const double a
Definition nonlinschro.cc:118
Defines simple templates for printing to std::cout "a la Python".
double Q(double a)
Definition relops.cc:20
static const double c
Definition relops.cc:10
static const double L
Definition rk.cc:46
static const double thresh
Definition rk.cc:45
Definition test_ar.cc:204
Definition test_dc.cc:47
Key parent() const
Definition test_tree.cc:68
hashT hash() const
Definition test_dc.cc:54
Definition test_ccpairfunction.cc:22
given a ket and the 1- and 2-electron potentials, construct the function V phi
Definition funcimpl.h:4107
implT * result
where to construct Vphi, no need to track parents
Definition funcimpl.h:4115
bool have_v2() const
Definition funcimpl.h:4124
ctL iav1
Definition funcimpl.h:4119
Vphi_op_NS(implT *result, const opT &leaf_op, const ctT &iaket, const ctL &iap1, const ctL &iap2, const ctL &iav1, const ctL &iav2, const implT *eri)
Definition funcimpl.h:4133
ctL iap1
Definition funcimpl.h:4118
bool have_v1() const
Definition funcimpl.h:4123
std::pair< bool, coeffT > continue_recursion(const std::vector< bool > child_is_leaf, const tensorT &coeffs, const keyT &key) const
loop over all children and either insert their sum coeffs or continue the recursion
Definition funcimpl.h:4199
opT leaf_op
deciding if a given FunctionNode will be a leaf node
Definition funcimpl.h:4116
std::pair< coeffT, double > make_sum_coeffs(const keyT &key) const
make the sum coeffs for key
Definition funcimpl.h:4292
CoeffTracker< T, NDIM > ctT
Definition funcimpl.h:4112
ctL iap2
the particles 1 and 2 (exclusive with ket)
Definition funcimpl.h:4118
bool have_ket() const
Definition funcimpl.h:4122
const implT * eri
2-particle potential, must be on-demand
Definition funcimpl.h:4120
CoeffTracker< T, LDIM > ctL
Definition funcimpl.h:4113
std::pair< bool, coeffT > operator()(const Key< NDIM > &key) const
make and insert the coefficients into result's tree
Definition funcimpl.h:4144
void serialize(const Archive &ar)
serialize this (needed for use in recursive_op)
Definition funcimpl.h:4373
Vphi_op_NS< opT, LDIM > this_type
Definition funcimpl.h:4111
ctT iaket
the ket of a pair function (exclusive with p1, p2)
Definition funcimpl.h:4117
double compute_error_from_inaccurate_refinement(const keyT &key, const tensorT &ceri) const
the error is computed from the d coefficients of the constituent functions
Definition funcimpl.h:4245
void accumulate_into_result(const Key< NDIM > &key, const coeffT &coeff) const
Definition funcimpl.h:4127
this_type make_child(const keyT &child) const
Definition funcimpl.h:4344
tensorT eri_coeffs(const keyT &key) const
Definition funcimpl.h:4225
ctL iav2
potentials for particles 1 and 2
Definition funcimpl.h:4119
bool have_eri() const
Definition funcimpl.h:4125
this_type forward_ctor(implT *result1, const opT &leaf_op, const ctT &iaket1, const ctL &iap11, const ctL &iap21, const ctL &iav11, const ctL &iav21, const implT *eri1)
Definition funcimpl.h:4366
Vphi_op_NS()
Definition funcimpl.h:4132
Future< this_type > activate() const
Definition funcimpl.h:4355
bool randomize() const
Definition funcimpl.h:4109
add two functions f and g: result=alpha * f + beta * g
Definition funcimpl.h:3617
bool randomize() const
Definition funcimpl.h:3622
Future< this_type > activate() const
retrieve the coefficients (parent coeffs might be remote)
Definition funcimpl.h:3652
add_op(const ctT &f, const ctT &g, const double alpha, const double beta)
Definition funcimpl.h:3630
ctT f
tracking coeffs of first and second addend
Definition funcimpl.h:3625
double alpha
prefactor for f, g
Definition funcimpl.h:3627
add_op this_type
Definition funcimpl.h:3620
CoeffTracker< T, NDIM > ctT
Definition funcimpl.h:3619
void serialize(const Archive &ar)
Definition funcimpl.h:3664
ctT g
Definition funcimpl.h:3625
std::pair< bool, coeffT > operator()(const keyT &key) const
if we are at the bottom of the trees, return the sum of the coeffs
Definition funcimpl.h:3634
double beta
Definition funcimpl.h:3627
this_type make_child(const keyT &child) const
Definition funcimpl.h:3647
this_type forward_ctor(const ctT &f1, const ctT &g1, const double alpha, const double beta)
taskq-compatible ctor
Definition funcimpl.h:3660
opT op
Definition funcimpl.h:3207
opT::resultT resultT
Definition funcimpl.h:3205
Tensor< resultT > operator()(const Key< NDIM > &key, const Tensor< Q > &t) const
Definition funcimpl.h:3214
coeff_value_adaptor(const FunctionImpl< Q, NDIM > *impl_func, const opT &op)
Definition funcimpl.h:3210
const FunctionImpl< Q, NDIM > * impl_func
Definition funcimpl.h:3206
void serialize(Archive &ar)
Definition funcimpl.h:3223
merge the coefficent boxes of this into result's tree
Definition funcimpl.h:2446
Range< typename dcT::const_iterator > rangeT
Definition funcimpl.h:2447
void serialize(const Archive &ar)
Definition funcimpl.h:2464
FunctionImpl< Q, NDIM > * result
Definition funcimpl.h:2448
do_accumulate_trees(FunctionImpl< Q, NDIM > &result, const T alpha)
Definition funcimpl.h:2451
T alpha
Definition funcimpl.h:2449
bool operator()(typename rangeT::iterator &it) const
return the norm of the difference of this node and its "mirror" node
Definition funcimpl.h:2455
"put" this on g
Definition funcimpl.h:2657
Range< typename dcT::const_iterator > rangeT
Definition funcimpl.h:2658
void serialize(const Archive &ar)
Definition funcimpl.h:2686
implT * g
Definition funcimpl.h:2660
do_average()
Definition funcimpl.h:2662
bool operator()(typename rangeT::iterator &it) const
iterator it points to this
Definition funcimpl.h:2666
do_average(implT &g)
Definition funcimpl.h:2663
change representation of nodes' coeffs to low rank, optional fence
Definition funcimpl.h:2690
Range< typename dcT::iterator > rangeT
Definition funcimpl.h:2691
void serialize(const Archive &ar)
Definition funcimpl.h:2714
TensorArgs targs
Definition funcimpl.h:2694
do_change_tensor_type(const TensorArgs &targs, implT &g)
Definition funcimpl.h:2700
bool operator()(typename rangeT::iterator &it) const
Definition funcimpl.h:2703
implT * f
Definition funcimpl.h:2695
check symmetry wrt particle exchange
Definition funcimpl.h:2363
Range< typename dcT::const_iterator > rangeT
Definition funcimpl.h:2364
double operator()(typename rangeT::iterator &it) const
return the norm of the difference of this node and its "mirror" node
Definition funcimpl.h:2370
do_check_symmetry_local()
Definition funcimpl.h:2366
void serialize(const Archive &ar)
Definition funcimpl.h:2433
double operator()(double a, double b) const
Definition funcimpl.h:2429
do_check_symmetry_local(const implT &f)
Definition funcimpl.h:2367
const implT * f
Definition funcimpl.h:2365
compute the norm of the wavelet coefficients
Definition funcimpl.h:4514
Range< typename dcT::iterator > rangeT
Definition funcimpl.h:4515
bool operator()(typename rangeT::iterator &it) const
Definition funcimpl.h:4521
do_compute_snorm_and_dnorm(const FunctionCommonData< T, NDIM > &cdata)
Definition funcimpl.h:4518
const FunctionCommonData< T, NDIM > & cdata
Definition funcimpl.h:4517
TensorArgs targs
Definition funcimpl.h:2721
bool operator()(typename rangeT::iterator &it) const
Definition funcimpl.h:2726
Range< typename dcT::iterator > rangeT
Definition funcimpl.h:2718
do_consolidate_buffer(const TensorArgs &targs)
Definition funcimpl.h:2725
void serialize(const Archive &ar)
Definition funcimpl.h:2730
double operator()(double val) const
Definition funcimpl.h:1482
double limit
Definition funcimpl.h:1477
do_convert_to_color(const double limit, const bool log)
Definition funcimpl.h:1481
bool log
Definition funcimpl.h:1478
static double lower()
Definition funcimpl.h:1479
compute the inner product of this range with other
Definition funcimpl.h:5838
do_dot_local(const FunctionImpl< R, NDIM > *other, const bool leaves_only)
Definition funcimpl.h:5843
bool leaves_only
Definition funcimpl.h:5840
typedef TENSOR_RESULT_TYPE(T, R) resultT
resultT operator()(resultT a, resultT b) const
Definition funcimpl.h:5871
const FunctionImpl< R, NDIM > * other
Definition funcimpl.h:5839
void serialize(const Archive &ar)
Definition funcimpl.h:5875
resultT operator()(typename dcT::const_iterator &it) const
Definition funcimpl.h:5845
functor for the gaxpy_inplace method
Definition funcimpl.h:1264
FunctionImpl< T, NDIM > * f
prefactor for current function impl
Definition funcimpl.h:1266
do_gaxpy_inplace(FunctionImpl< T, NDIM > *f, T alpha, R beta)
Definition funcimpl.h:1270
bool operator()(typename rangeT::iterator &it) const
Definition funcimpl.h:1271
R beta
prefactor for other function impl
Definition funcimpl.h:1268
void serialize(Archive &ar)
Definition funcimpl.h:1279
Range< typename FunctionImpl< Q, NDIM >::dcT::const_iterator > rangeT
Definition funcimpl.h:1265
T alpha
the current function impl
Definition funcimpl.h:1267
const bool do_leaves
start with leaf nodes instead of initial_level
Definition funcimpl.h:6764
T operator()(T a, T b) const
Definition funcimpl.h:6782
do_inner_ext_local_ffi(const std::shared_ptr< FunctionFunctorInterface< T, NDIM > > f, const implT *impl, const bool leaf_refine, const bool do_leaves)
Definition funcimpl.h:6766
void serialize(const Archive &ar)
Definition funcimpl.h:6786
const bool leaf_refine
Definition funcimpl.h:6763
const std::shared_ptr< FunctionFunctorInterface< T, NDIM > > fref
Definition funcimpl.h:6761
T operator()(typename dcT::const_iterator &it) const
Definition funcimpl.h:6770
const implT * impl
Definition funcimpl.h:6762
compute the inner product of this range with other
Definition funcimpl.h:5701
const FunctionImpl< T, NDIM > * bra
Definition funcimpl.h:5702
void serialize(const Archive &ar)
Definition funcimpl.h:5817
const FunctionImpl< R, NDIM > * ket
Definition funcimpl.h:5703
bool leaves_only
Definition funcimpl.h:5704
do_inner_local_on_demand(const FunctionImpl< T, NDIM > *bra, const FunctionImpl< R, NDIM > *ket, const bool leaves_only=true)
Definition funcimpl.h:5707
resultT operator()(resultT a, resultT b) const
Definition funcimpl.h:5813
resultT operator()(typename dcT::const_iterator &it) const
Definition funcimpl.h:5710
compute the inner product of this range with other
Definition funcimpl.h:5640
resultT operator()(resultT a, resultT b) const
Definition funcimpl.h:5673
bool leaves_only
Definition funcimpl.h:5642
void serialize(const Archive &ar)
Definition funcimpl.h:5677
do_inner_local(const FunctionImpl< R, NDIM > *other, const bool leaves_only)
Definition funcimpl.h:5645
const FunctionImpl< R, NDIM > * other
Definition funcimpl.h:5641
resultT operator()(typename dcT::const_iterator &it) const
Definition funcimpl.h:5647
typedef TENSOR_RESULT_TYPE(T, R) resultT
keep only the sum coefficients in each node
Definition funcimpl.h:2317
Range< typename dcT::iterator > rangeT
Definition funcimpl.h:2318
do_keep_sum_coeffs(implT *impl)
constructor need impl for cdata
Definition funcimpl.h:2322
implT * impl
Definition funcimpl.h:2319
void serialize(const Archive &ar)
Definition funcimpl.h:2331
bool operator()(typename rangeT::iterator &it) const
Definition funcimpl.h:2324
mirror dimensions of this, write result on f
Definition funcimpl.h:2591
bool operator()(typename rangeT::iterator &it) const
Definition funcimpl.h:2601
implT * f
Definition funcimpl.h:2595
std::vector< long > mirror
Definition funcimpl.h:2594
void serialize(const Archive &ar)
Definition funcimpl.h:2648
Range< typename dcT::iterator > rangeT
Definition funcimpl.h:2592
std::vector< long > map
Definition funcimpl.h:2594
do_map_and_mirror(const std::vector< long > map, const std::vector< long > mirror, implT &f)
Definition funcimpl.h:2598
map this on f
Definition funcimpl.h:2511
do_mapdim(const std::vector< long > map, implT &f)
Definition funcimpl.h:2518
void serialize(const Archive &ar)
Definition funcimpl.h:2534
Range< typename dcT::iterator > rangeT
Definition funcimpl.h:2512
bool operator()(typename rangeT::iterator &it) const
Definition funcimpl.h:2520
std::vector< long > map
Definition funcimpl.h:2514
do_mapdim()
Definition funcimpl.h:2517
implT * f
Definition funcimpl.h:2515
merge the coefficient boxes of this into other's tree
Definition funcimpl.h:2475
bool operator()(typename rangeT::iterator &it) const
return the norm of the difference of this node and its "mirror" node
Definition funcimpl.h:2485
Range< typename dcT::const_iterator > rangeT
Definition funcimpl.h:2476
FunctionImpl< Q, NDIM > * other
Definition funcimpl.h:2477
do_merge_trees(const T alpha, const R beta, FunctionImpl< Q, NDIM > &other)
Definition funcimpl.h:2481
T alpha
Definition funcimpl.h:2478
do_merge_trees()
Definition funcimpl.h:2480
R beta
Definition funcimpl.h:2479
void serialize(const Archive &ar)
Definition funcimpl.h:2504
mirror dimensions of this, write result on f
Definition funcimpl.h:2541
bool operator()(typename rangeT::iterator &it) const
Definition funcimpl.h:2550
implT * f
Definition funcimpl.h:2545
Range< typename dcT::iterator > rangeT
Definition funcimpl.h:2542
do_mirror()
Definition funcimpl.h:2547
do_mirror(const std::vector< long > mirror, implT &f)
Definition funcimpl.h:2548
void serialize(const Archive &ar)
Definition funcimpl.h:2584
std::vector< long > mirror
Definition funcimpl.h:2544
Definition funcimpl.h:5613
double operator()(typename dcT::const_iterator &it) const
Definition funcimpl.h:5614
void serialize(const Archive &ar)
Definition funcimpl.h:5629
double operator()(double a, double b) const
Definition funcimpl.h:5625
laziness
Definition funcimpl.h:4771
void serialize(Archive &ar)
Definition funcimpl.h:4780
Key< OPDIM > d
Definition funcimpl.h:4772
Key< OPDIM > key
Definition funcimpl.h:4772
keyT dest
Definition funcimpl.h:4773
double fac
Definition funcimpl.h:4774
do_op_args(const Key< OPDIM > &key, const Key< OPDIM > &d, const keyT &dest, double tol, double fac, double cnorm)
Definition funcimpl.h:4777
double cnorm
Definition funcimpl.h:4774
double tol
Definition funcimpl.h:4774
reduce the rank of the nodes, optional fence
Definition funcimpl.h:2337
do_reduce_rank(const TensorArgs &targs)
Definition funcimpl.h:2345
TensorArgs args
Definition funcimpl.h:2341
bool operator()(typename rangeT::iterator &it) const
Definition funcimpl.h:2351
Range< typename dcT::iterator > rangeT
Definition funcimpl.h:2338
do_reduce_rank(const double &thresh)
Definition funcimpl.h:2346
void serialize(const Archive &ar)
Definition funcimpl.h:2357
Changes non-standard compressed form to standard compressed form.
Definition funcimpl.h:4735
bool operator()(typename rangeT::iterator &it) const
Definition funcimpl.h:4746
do_standard(implT *impl)
Definition funcimpl.h:4743
Range< typename dcT::iterator > rangeT
Definition funcimpl.h:4736
void serialize(const Archive &ar)
Definition funcimpl.h:4763
implT * impl
Definition funcimpl.h:4739
given an NS tree resulting from a convolution, truncate leafs if appropriate
Definition funcimpl.h:2258
void serialize(const Archive &ar)
Definition funcimpl.h:2278
const implT * f
Definition funcimpl.h:2260
bool operator()(typename rangeT::iterator &it) const
Definition funcimpl.h:2264
Range< typename dcT::iterator > rangeT
Definition funcimpl.h:2259
do_truncate_NS_leafs(const implT *f)
Definition funcimpl.h:2262
Range< typename dcT::iterator > rangeT
Definition funcimpl.h:2737
bool operator()(typename rangeT::iterator &it) const
Definition funcimpl.h:2741
implT * impl
Definition funcimpl.h:2738
void serialize(const Archive &ar)
Definition funcimpl.h:2759
do_unary_op_value_inplace(implT *impl, const opT &op)
Definition funcimpl.h:2740
Hartree product of two LDIM functions to yield a NDIM = 2*LDIM function.
Definition funcimpl.h:3700
this_type forward_ctor(implT *result1, const ctL &p11, const ctL &p22, const leaf_opT &leaf_op)
Definition funcimpl.h:3756
bool randomize() const
Definition funcimpl.h:3701
void serialize(const Archive &ar)
Definition funcimpl.h:3760
hartree_op(implT *result, const ctL &p11, const ctL &p22, const leaf_opT &leaf_op)
Definition funcimpl.h:3712
CoeffTracker< T, LDIM > ctL
Definition funcimpl.h:3704
ctL p2
tracking coeffs of the two lo-dim functions
Definition funcimpl.h:3707
leaf_opT leaf_op
determine if a given node will be a leaf node
Definition funcimpl.h:3708
hartree_op()
Definition funcimpl.h:3711
implT * result
where to construct the pair function
Definition funcimpl.h:3706
hartree_op< LDIM, leaf_opT > this_type
Definition funcimpl.h:3703
std::pair< bool, coeffT > operator()(const Key< NDIM > &key) const
Definition funcimpl.h:3717
ctL p1
Definition funcimpl.h:3707
this_type make_child(const keyT &child) const
Definition funcimpl.h:3740
Future< this_type > activate() const
Definition funcimpl.h:3749
perform this multiplication: h(1,2) = f(1,2) * g(1)
Definition funcimpl.h:3508
multiply_op()
Definition funcimpl.h:3520
ctL g
Definition funcimpl.h:3517
Future< this_type > activate() const
Definition funcimpl.h:3599
CoeffTracker< T, LDIM > ctL
Definition funcimpl.h:3512
implT * h
the result function h(1,2) = f(1,2) * g(1)
Definition funcimpl.h:3515
CoeffTracker< T, NDIM > ctT
Definition funcimpl.h:3511
std::pair< bool, coeffT > operator()(const Key< NDIM > &key) const
apply this on a FunctionNode of f and g of Key key
Definition funcimpl.h:3547
this_type forward_ctor(implT *h1, const ctT &f1, const ctL &g1, const int particle)
Definition funcimpl.h:3606
static bool randomize()
Definition funcimpl.h:3510
int particle
if g is g(1) or g(2)
Definition funcimpl.h:3518
ctT f
Definition funcimpl.h:3516
multiply_op< LDIM > this_type
Definition funcimpl.h:3513
multiply_op(implT *h1, const ctT &f1, const ctL &g1, const int particle1)
Definition funcimpl.h:3522
bool screen(const coeffT &fcoeff, const coeffT &gcoeff, const keyT &key) const
return true if this will be a leaf node
Definition funcimpl.h:3528
this_type make_child(const keyT &child) const
Definition funcimpl.h:3589
void serialize(const Archive &ar)
Definition funcimpl.h:3610
coeffT val_lhs
Definition funcimpl.h:3987
double lo
Definition funcimpl.h:3990
double lo1
Definition funcimpl.h:3990
long oversampling
Definition funcimpl.h:3988
double error
Definition funcimpl.h:3989
tensorT operator()(const Key< NDIM > key, const tensorT &coeff_rhs)
multiply values of rhs and lhs, result on rhs, rhs and lhs are of the same dimensions
Definition funcimpl.h:4005
coeffT coeff_lhs
Definition funcimpl.h:3987
void serialize(const Archive &ar)
Definition funcimpl.h:4093
double lo2
Definition funcimpl.h:3990
double hi1
Definition funcimpl.h:3990
pointwise_multiplier(const Key< NDIM > key, const coeffT &clhs)
Definition funcimpl.h:3993
coeffT operator()(const Key< NDIM > key, const tensorT &coeff_rhs, const int particle)
multiply values of rhs and lhs, result on rhs, rhs and lhs are of differnet dimensions
Definition funcimpl.h:4050
double hi2
Definition funcimpl.h:3990
double hi
Definition funcimpl.h:3990
project the low-dim function g on the hi-dim function f: result(x) = <f(x,y) | g(y)>
Definition funcimpl.h:7026
project_out_op(const implT *fimpl, implL1 *result, const ctL &iag, const int dim)
Definition funcimpl.h:7041
ctL iag
the low dim function g
Definition funcimpl.h:7036
FunctionImpl< T, NDIM-LDIM > implL1
Definition funcimpl.h:7031
Future< this_type > activate() const
retrieve the coefficients (parent coeffs might be remote)
Definition funcimpl.h:7120
std::pair< bool, coeffT > argT
Definition funcimpl.h:7032
const implT * fimpl
the hi dim function f
Definition funcimpl.h:7034
this_type forward_ctor(const implT *fimpl1, implL1 *result1, const ctL &iag1, const int dim1)
taskq-compatible ctor
Definition funcimpl.h:7127
this_type make_child(const keyT &child) const
Definition funcimpl.h:7111
project_out_op< LDIM > this_type
Definition funcimpl.h:7029
implL1 * result
the low dim result function
Definition funcimpl.h:7035
Future< argT > operator()(const Key< NDIM > &key) const
do the actual contraction
Definition funcimpl.h:7048
void serialize(const Archive &ar)
Definition funcimpl.h:7131
project_out_op(const project_out_op &other)
Definition funcimpl.h:7043
int dim
0: project 0..LDIM-1, 1: project LDIM..NDIM-1
Definition funcimpl.h:7037
bool randomize() const
Definition funcimpl.h:7027
CoeffTracker< T, LDIM > ctL
Definition funcimpl.h:7030
recursive part of recursive_apply
Definition funcimpl.h:5440
ctT iaf
Definition funcimpl.h:5448
recursive_apply_op2< opT > this_type
Definition funcimpl.h:5443
Future< this_type > activate() const
retrieve the coefficients (parent coeffs might be remote)
Definition funcimpl.h:5503
const opT * apply_op
need this for randomization
Definition funcimpl.h:5449
bool randomize() const
Definition funcimpl.h:5441
recursive_apply_op2(const recursive_apply_op2 &other)
Definition funcimpl.h:5456
void serialize(const Archive &ar)
Definition funcimpl.h:5519
argT finalize(const double kernel_norm, const keyT &key, const coeffT &coeff, const implT *r) const
sole purpose is to wait for the kernel norm, wrap it and send it back to caller
Definition funcimpl.h:5489
this_type make_child(const keyT &child) const
Definition funcimpl.h:5498
recursive_apply_op2(implT *result, const ctT &iaf, const opT *apply_op)
Definition funcimpl.h:5453
std::pair< bool, coeffT > argT
Definition funcimpl.h:5445
implT * result
Definition funcimpl.h:5447
CoeffTracker< T, NDIM > ctT
Definition funcimpl.h:5444
argT operator()(const Key< NDIM > &key) const
send off the application of the operator
Definition funcimpl.h:5465
this_type forward_ctor(implT *result1, const ctT &iaf1, const opT *apply_op1)
taskq-compatible ctor
Definition funcimpl.h:5515
recursive part of recursive_apply
Definition funcimpl.h:5309
std::pair< bool, coeffT > operator()(const Key< NDIM > &key) const
make the NS-coefficients and send off the application of the operator
Definition funcimpl.h:5334
this_type forward_ctor(implT *r, const CoeffTracker< T, LDIM > &f1, const CoeffTracker< T, LDIM > &g1, const opT *apply_op1)
Definition funcimpl.h:5399
opT * apply_op
Definition funcimpl.h:5317
recursive_apply_op(const recursive_apply_op &other)
Definition funcimpl.h:5327
recursive_apply_op< opT, LDIM > this_type
Definition funcimpl.h:5312
Future< this_type > activate() const
Definition funcimpl.h:5392
bool randomize() const
Definition funcimpl.h:5310
implT * result
Definition funcimpl.h:5314
CoeffTracker< T, LDIM > iaf
Definition funcimpl.h:5315
void serialize(const Archive &ar)
Definition funcimpl.h:5404
std::pair< bool, coeffT > finalize(const double kernel_norm, const keyT &key, const coeffT &coeff) const
sole purpose is to wait for the kernel norm, wrap it and send it back to caller
Definition funcimpl.h:5374
recursive_apply_op(implT *result, const CoeffTracker< T, LDIM > &iaf, const CoeffTracker< T, LDIM > &iag, const opT *apply_op)
Definition funcimpl.h:5321
this_type make_child(const keyT &child) const
Definition funcimpl.h:5383
CoeffTracker< T, LDIM > iag
Definition funcimpl.h:5316
remove all coefficients of internal nodes
Definition funcimpl.h:2283
Range< typename dcT::iterator > rangeT
Definition funcimpl.h:2284
remove_internal_coeffs()=default
constructor need impl for cdata
bool operator()(typename rangeT::iterator &it) const
Definition funcimpl.h:2289
void serialize(const Archive &ar)
Definition funcimpl.h:2295
remove all coefficients of leaf nodes
Definition funcimpl.h:2300
bool operator()(typename rangeT::iterator &it) const
Definition funcimpl.h:2306
remove_leaf_coeffs()=default
constructor need impl for cdata
void serialize(const Archive &ar)
Definition funcimpl.h:2311
Range< typename dcT::iterator > rangeT
Definition funcimpl.h:2301
Definition funcimpl.h:4586
void serialize(Archive &ar)
Definition funcimpl.h:4590
bool operator()(const implT *f, const keyT &key, const nodeT &t) const
Definition funcimpl.h:4587
shallow-copy, pared-down version of FunctionNode, for special purpose only
Definition funcimpl.h:749
coeffT & coeff()
Definition funcimpl.h:763
GenTensor< T > coeffT
Definition funcimpl.h:750
bool is_leaf() const
Definition funcimpl.h:765
void serialize(Archive &ar)
Definition funcimpl.h:767
ShallowNode(const ShallowNode< T, NDIM > &node)
Definition funcimpl.h:758
ShallowNode(const FunctionNode< T, NDIM > &node)
Definition funcimpl.h:755
bool has_children() const
Definition funcimpl.h:764
ShallowNode()
Definition funcimpl.h:754
bool _has_children
Definition funcimpl.h:752
double dnorm
Definition funcimpl.h:753
const coeffT & coeff() const
Definition funcimpl.h:762
coeffT _coeffs
Definition funcimpl.h:751
TensorArgs holds the arguments for creating a LowRankTensor.
Definition gentensor.h:134
double thresh
Definition gentensor.h:135
TensorType tt
Definition gentensor.h:136
inserts/accumulates coefficients into impl's tree
Definition funcimpl.h:716
FunctionImpl< T, NDIM > * impl
Definition funcimpl.h:720
FunctionNode< T, NDIM > nodeT
Definition funcimpl.h:718
accumulate_op(const accumulate_op &other)=default
void operator()(const Key< NDIM > &key, const coeffT &coeff, const bool &is_leaf) const
Definition funcimpl.h:724
void serialize(Archive &ar)
Definition funcimpl.h:728
GenTensor< T > coeffT
Definition funcimpl.h:717
accumulate_op(FunctionImpl< T, NDIM > *f)
Definition funcimpl.h:722
static void load(const Archive &ar, FunctionImpl< T, NDIM > *&ptr)
Definition funcimpl.h:7353
static void load(const Archive &ar, const FunctionImpl< T, NDIM > *&ptr)
Definition funcimpl.h:7322
static void load(const Archive &ar, std::shared_ptr< FunctionImpl< T, NDIM > > &ptr)
Definition funcimpl.h:7404
static void load(const Archive &ar, std::shared_ptr< const FunctionImpl< T, NDIM > > &ptr)
Definition funcimpl.h:7388
Default load of an object via serialize(ar, t).
Definition archive.h:667
static void load(const A &ar, const U &t)
Load an object.
Definition archive.h:679
static void store(const Archive &ar, FunctionImpl< T, NDIM > *const &ptr)
Definition funcimpl.h:7378
static void store(const Archive &ar, const FunctionImpl< T, NDIM > *const &ptr)
Definition funcimpl.h:7344
static void store(const Archive &ar, const std::shared_ptr< FunctionImpl< T, NDIM > > &ptr)
Definition funcimpl.h:7413
static void store(const Archive &ar, const std::shared_ptr< const FunctionImpl< T, NDIM > > &ptr)
Definition funcimpl.h:7397
Default store of an object via serialize(ar, t).
Definition archive.h:612
static std::enable_if_t< is_output_archive_v< A > &&!std::is_function< U >::value &&(has_member_serialize_v< U, A >||has_nonmember_serialize_v< U, A >||has_freestanding_serialize_v< U, A >||has_freestanding_default_serialize_v< U, A >), void > store(const A &ar, const U &t)
Definition archive.h:622
Definition funcimpl.h:610
void serialize(Archive &ar)
Definition funcimpl.h:674
const opT * op
Definition funcimpl.h:617
hartree_convolute_leaf_op(const implT *f, const implL *g, const opT *op)
Definition funcimpl.h:621
bool operator()(const Key< NDIM > &key) const
no pre-determination
Definition funcimpl.h:625
bool operator()(const Key< NDIM > &key, const Tensor< T > &fcoeff, const Tensor< T > &gcoeff) const
post-determination: true if f is a leaf and the result is well-represented
Definition funcimpl.h:638
const implL * g
Definition funcimpl.h:616
const FunctionImpl< T, NDIM > * f
Definition funcimpl.h:615
FunctionImpl< T, LDIM > implL
Definition funcimpl.h:613
bool do_error_leaf_op() const
Definition funcimpl.h:618
FunctionImpl< T, NDIM > implT
Definition funcimpl.h:612
bool operator()(const Key< NDIM > &key, const GenTensor< T > &coeff) const
no post-determination
Definition funcimpl.h:628
returns true if the result of a hartree_product is a leaf node (compute norm & error)
Definition funcimpl.h:500
bool do_error_leaf_op() const
Definition funcimpl.h:505
const FunctionImpl< T, NDIM > * f
Definition funcimpl.h:503
hartree_leaf_op(const implT *f, const long &k)
Definition funcimpl.h:508
long k
Definition funcimpl.h:504
void serialize(Archive &ar)
Definition funcimpl.h:556
bool operator()(const Key< NDIM > &key, const GenTensor< T > &coeff) const
no post-determination
Definition funcimpl.h:514
bool operator()(const Key< NDIM > &key, const Tensor< T > &fcoeff, const Tensor< T > &gcoeff) const
post-determination: true if f is a leaf and the result is well-represented
Definition funcimpl.h:524
bool operator()(const Key< NDIM > &key) const
no pre-determination
Definition funcimpl.h:511
FunctionImpl< T, NDIM > implT
Definition funcimpl.h:502
insert/replaces the coefficients into the function
Definition funcimpl.h:692
insert_op()
Definition funcimpl.h:699
implT * impl
Definition funcimpl.h:698
void operator()(const keyT &key, const coeffT &coeff, const bool &is_leaf) const
Definition funcimpl.h:702
FunctionNode< T, NDIM > nodeT
Definition funcimpl.h:696
Key< NDIM > keyT
Definition funcimpl.h:694
insert_op(const insert_op &other)
Definition funcimpl.h:701
FunctionImpl< T, NDIM > implT
Definition funcimpl.h:693
GenTensor< T > coeffT
Definition funcimpl.h:695
insert_op(implT *f)
Definition funcimpl.h:700
void serialize(Archive &ar)
Definition funcimpl.h:706
Definition mra.h:112
Definition funcimpl.h:680
bool operator()(const Key< NDIM > &key, const GenTensor< T > &fcoeff, const GenTensor< T > &gcoeff) const
Definition funcimpl.h:682
void serialize(Archive &ar)
Definition funcimpl.h:686
void operator()(const Key< NDIM > &key, const GenTensor< T > &coeff, const bool &is_leaf) const
Definition funcimpl.h:681
Definition funcimpl.h:564
bool operator()(const Key< NDIM > &key, const double &cnorm) const
post-determination: return true if operator and coefficient norms are small
Definition funcimpl.h:585
void serialize(Archive &ar)
Definition funcimpl.h:600
const implT * f
the source or result function, needed for truncate_tol
Definition funcimpl.h:568
op_leaf_op(const opT *op, const implT *f)
Definition funcimpl.h:572
FunctionImpl< T, NDIM > implT
Definition funcimpl.h:565
const opT * op
the convolution operator
Definition funcimpl.h:567
bool do_error_leaf_op() const
Definition funcimpl.h:569
bool operator()(const Key< NDIM > &key) const
pre-determination: we can't know if this will be a leaf node before we got the final coeffs
Definition funcimpl.h:575
bool operator()(const Key< NDIM > &key, const GenTensor< T > &coeff) const
post-determination: return true if operator and coefficient norms are small
Definition funcimpl.h:578
Definition lowrankfunction.h:336
Definition funcimpl.h:736
void serialize(Archive &ar)
Definition funcimpl.h:743
bool operator()(const Key< NDIM > &key, const T &t, const R &r) const
Definition funcimpl.h:742
bool operator()(const Key< NDIM > &key, const T &t) const
Definition funcimpl.h:739
int np
Definition tdse1d.cc:165
static const double s0
Definition tdse4.cc:83
Defines and implements most of Tensor.
#define ITERATOR(t, exp)
Definition tensor_macros.h:249
#define IND
Definition tensor_macros.h:204
#define TERNARY_OPTIMIZED_ITERATOR(X, x, Y, y, Z, z, exp)
Definition tensor_macros.h:719
AtomicInt sum
Definition test_atomicint.cc:46
double norm(const T i1)
Definition test_cloud.cc:85
int task(int i)
Definition test_runtime.cpp:4
void e()
Definition test_sig.cc:75
static double g1(const Vector< double, D > &r)
Definition test_state_archive_hdf5.cpp:34
static const double alpha
Definition testcosine.cc:10
const double offset
Definition testfuns.cc:143
constexpr std::size_t NDIM
Definition testgconv.cc:54
double h(const coord_1d &r)
Definition testgconv.cc:175
std::size_t axis
Definition testpdiff.cc:59
double source(const coordT &r)
Definition testperiodic.cc:48
#define TENSOR_RESULT_TYPE(L, R)
This macro simplifies access to TensorResultType.
Definition type_data.h:205
#define PROFILE_MEMBER_FUNC(classname)
Definition worldprofile.h:210
#define PROFILE_BLOCK(name)
Definition worldprofile.h:208
int ProcessID
Used to clearly identify process number/rank.
Definition worldtypes.h:43