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