MADNESS 0.10.1
vmra.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 $Id$
32*/
33#ifndef MADNESS_MRA_VMRA_H__INCLUDED
34#define MADNESS_MRA_VMRA_H__INCLUDED
35
36/*!
37 \file vmra.h
38 \brief Defines operations on vectors of Functions
39 \ingroup mra
40
41 This file defines a number of operations on vectors of functions.
42 Assume v is a vector of NDIM-D functions of a certain type.
43
44
45 Operations on array of functions
46
47 *) copying: deep copying of vectors of functions to vector of functions
48 \code
49 vector2 = copy(world, vector1,fence);
50 \endcode
51
52 *) compress: convert multiwavelet representation to legendre representation
53 \code
54 compress(world, vector, fence);
55 \endcode
56
57 *) reconstruct: convert representation to multiwavelets
58 \code
59 reconstruct(world, vector, fence);
60 \endcode
61
62 *) make_nonstandard: convert to non-standard form
63 \code
64 make_nonstandard(world, v, fence);
65 \endcode
66
67 *) standard: convert to standard form
68 \code
69 standard(world, v, fence);
70 \endcode
71
72 *) truncate: truncating vectors of functions to desired precision
73 \code
74 truncate(world, v, tolerance, fence);
75 \endcode
76
77
78 *) zero function: create a vector of zero functions of length n
79 \code
80 v=zero(world, n);
81 \endcode
82
83 *) transform: transform a representation from one basis to another
84 \code
85 transform(world, vector, tensor, tolerance, fence )
86 \endcode
87
88 Setting thresh-hold for precision
89
90 *) set_thresh: setting a finite thresh-hold for a vector of functions
91 \code
92 void set_thresh(World& world, std::vector< Function<T,NDIM> >& v, double thresh, bool fence=true);
93 \endcode
94
95 Arithmetic Operations on arrays of functions
96
97 *) conjugation: conjugate a vector of complex functions
98
99 *) add
100 *) sub
101 *) mul
102 - mul_sparse
103 *) square
104 *) gaxpy
105 *) apply
106
107 Norms, inner-products, blas-1 like operations on vectors of functions
108
109 *) inner
110 *) matrix_inner
111 *) norm_tree
112 *) normalize
113 *) norm2
114 - norm2s
115 *) scale(world, v, alpha);
116
117
118
119
120*/
121
122#include <madness/mra/mra.h>
125#include <cstdio>
126#include <algorithm>
127
128namespace madness {
129
130
131 /// get tree state of a vector of functions
132
133 /// @return TreeState::unknown if the vector is empty or if the functions have different tree states
134 template <typename T, std::size_t NDIM>
136 if (v.size()==0) return TreeState::unknown;
137 // return unknown if any function is not initialized
138 if (std::any_of(v.begin(), v.end(), [](const Function<T,NDIM>& f) {return not f.is_initialized();})) {
139 return TreeState::unknown;
140 }
141 TreeState state=v[0].get_impl()->get_tree_state();
142 for (const auto& f : v) {
143 if (f.get_impl()->get_tree_state()!=state) state=TreeState::unknown;
144 }
145 return state;
146 }
147
148 /// Compress a vector of functions
149 template <typename T, std::size_t NDIM>
150 void compress(World& world,
151 const std::vector< Function<T,NDIM> >& v,
152 bool fence=true) {
153 PROFILE_BLOCK(Vcompress);
155 }
156
157
158 /// reconstruct a vector of functions
159
160 /// implies fence
161 /// return v for chaining
162 template <typename T, std::size_t NDIM>
163 const std::vector< Function<T,NDIM> >& reconstruct(const std::vector< Function<T,NDIM> >& v) {
165 }
166
167 /// compress a vector of functions
168
169 /// implies fence
170 /// return v for chaining
171 template <typename T, std::size_t NDIM>
172 const std::vector< Function<T,NDIM> >& compress(const std::vector< Function<T,NDIM> >& v) {
174 }
175
176 /// Reconstruct a vector of functions
177 template <typename T, std::size_t NDIM>
178 void reconstruct(World& world,
179 const std::vector< Function<T,NDIM> >& v,
180 bool fence=true) {
181 PROFILE_BLOCK(Vreconstruct);
183 }
184
185 /// change tree_state of a vector of functions to redundant
186 template <typename T, std::size_t NDIM>
188 const std::vector< Function<T,NDIM> >& v,
189 bool fence=true) {
190
191 PROFILE_BLOCK(Vcompress);
193 }
194
195 /// refine the functions according to the autorefine criteria
196 template <typename T, std::size_t NDIM>
197 void refine(World& world, const std::vector<Function<T,NDIM> >& vf,
198 bool fence=true) {
199 for (const auto& f : vf) f.refine(false);
200 if (fence) world.gop.fence();
201 }
202
203 /// refine all functions to a common (finest) level
204
205 /// if functions are not initialized (impl==NULL) they are ignored
206 template <typename T, std::size_t NDIM>
207 void refine_to_common_level(World& world, std::vector<Function<T,NDIM> >& vf,
208 bool fence=true) {
209
210 reconstruct(world,vf);
212 std::vector<FunctionImpl<T,NDIM>*> v_ptr;
213
214 // push initialized function pointers into the vector v_ptr
215 for (unsigned int i=0; i<vf.size(); ++i) {
216 if (vf[i].is_initialized()) v_ptr.push_back(vf[i].get_impl().get());
217 }
218
219 // sort and remove duplicates to not confuse the refining function
220 std::sort(v_ptr.begin(),v_ptr.end());
221 typename std::vector<FunctionImpl<T, NDIM>*>::iterator it;
222 it = std::unique(v_ptr.begin(), v_ptr.end());
223 v_ptr.resize( std::distance(v_ptr.begin(),it) );
224
225 std::vector< Tensor<T> > c(v_ptr.size());
226 v_ptr[0]->refine_to_common_level(v_ptr, c, key0);
227 if (fence) v_ptr[0]->world.gop.fence();
228 if (VERIFY_TREE)
229 for (unsigned int i=0; i<vf.size(); i++) vf[i].verify_tree();
230 }
231
232 /// Generates non-standard form of a vector of functions
233 template <typename T, std::size_t NDIM>
235 std::vector< Function<T,NDIM> >& v,
236 bool fence= true) {
237 PROFILE_BLOCK(Vnonstandard);
239 }
240
241
242 /// Generates standard form of a vector of functions
243 template <typename T, std::size_t NDIM>
244 void standard(World& world,
245 std::vector< Function<T,NDIM> >& v,
246 bool fence=true) {
247 PROFILE_BLOCK(Vstandard);
249 }
250
251
252 /// change tree state of the functions
253
254 /// might not respect fence
255 /// @return v for chaining
256 template <typename T, std::size_t NDIM>
257 const std::vector<Function<T,NDIM>>& change_tree_state(const std::vector<Function<T,NDIM>>& v,
258 const TreeState finalstate,
259 const bool fence=true) {
260 // fast return
261 if (v.size()==0) return v;
262 if (get_tree_state(v)==finalstate) return v;
263
264 // find initialized function with world
265 Function<T,NDIM> dummy;
266 for (const auto& f : v)
267 if (f.is_initialized()) {
268 dummy=f;
269 break;
270 }
271 if (not dummy.is_initialized()) return v;
272 World& world=dummy.world();
273
274
275 // if a tree state cannot directly be changed to finalstate, we need to go via intermediate
276 auto change_initial_to_intermediate =[](const std::vector<Function<T,NDIM>>& v,
277 const TreeState initialstate,
278 const TreeState intermediatestate) {
279 int must_fence=0;
280 for (auto& f : v) {
281 if (f.is_initialized() and f.get_impl()->get_tree_state()==initialstate) {
282 f.change_tree_state(intermediatestate,false);
283 must_fence=1;
284 }
285 }
286 return must_fence;
287 };
288
289 int do_fence=0;
290 if (finalstate==compressed) {
291 do_fence+=change_initial_to_intermediate(v,redundant,TreeState::reconstructed);
292 }
293 if (finalstate==nonstandard) {
294 do_fence+=change_initial_to_intermediate(v,compressed,TreeState::reconstructed);
295 do_fence+=change_initial_to_intermediate(v,redundant,TreeState::reconstructed);
296 }
297 if (finalstate==nonstandard_with_leaves) {
298 do_fence+=change_initial_to_intermediate(v,compressed,TreeState::reconstructed);
299 do_fence+=change_initial_to_intermediate(v,nonstandard,TreeState::reconstructed);
300 do_fence+=change_initial_to_intermediate(v,redundant,TreeState::reconstructed);
301 }
302 if (finalstate==redundant) {
303 do_fence+=change_initial_to_intermediate(v,compressed,TreeState::reconstructed);
304 do_fence+=change_initial_to_intermediate(v,nonstandard,TreeState::reconstructed);
305 do_fence+=change_initial_to_intermediate(v,nonstandard_with_leaves,TreeState::reconstructed);
306 }
307 if (do_fence>0) world.gop.fence();
308
309 for (unsigned int i=0; i<v.size(); ++i) v[i].change_tree_state(finalstate,fence);
310 if (fence) world.gop.fence();
311
312 return v;
313 }
314
315 /// ensure v has the requested tree state, change the tree state of v if necessary and no fence is given
316 template<typename T, std::size_t NDIM>
318 const TreeState state, bool fence) {
319 // fast return
320 if (get_tree_state(v)==state) return true;;
321
322 // if there is a fence we can simply change the tree state, might be a no-op
323 if (fence) change_tree_state(v,state,true);
324
325 // check success, throw if not
326 bool ok=get_tree_state(v)==state;
327 if (not ok) {
328 print("ensure_tree_state_respecting_fence failed");
329 throw std::runtime_error("ensure_tree_state_respecting_fence failed");
330 }
331 return ok;
332 }
333
334 /// Truncates a vector of functions
335 template <typename T, std::size_t NDIM>
336 void truncate(World& world,
337 std::vector< Function<T,NDIM> >& v,
338 double tol=0.0,
339 bool fence=true) {
340 PROFILE_BLOCK(Vtruncate);
341
342 // truncate in compressed form only for low-dimensional functions
343 // compression is very expensive if low-rank tensor approximations are used
344 if (NDIM<4) compress(world, v);
345
346 for (auto& vv: v) {
347 vv.truncate(tol, false);
348 }
349
350 if (fence) world.gop.fence();
351 }
352
353 /// Truncates a vector of functions
354
355 /// @return the truncated vector for chaining
356 template <typename T, std::size_t NDIM>
357 std::vector< Function<T,NDIM> > truncate(std::vector< Function<T,NDIM> > v,
358 double tol=0.0, bool fence=true) {
359 if (v.size()>0) truncate(v[0].world(),v,tol,fence);
360 return v;
361 }
362
363 /// reduces the tensor rank of the coefficient tensor (if applicable)
364
365 /// @return the vector for chaining
366 template <typename T, std::size_t NDIM>
367 std::vector< Function<T,NDIM> > reduce_rank(std::vector< Function<T,NDIM> > v,
368 double thresh=0.0, bool fence=true) {
369 if (v.size()==0) return v;
370 for (auto& vv : v) vv.reduce_rank(thresh,false);
371 if (fence) v[0].world().gop.fence();
372 return v;
373 }
374
375
376 /// Pre-stages the neighbor coefficients that differentiating v with each of grad will need
377
378 /// Differentiating then serves those neighbors locally instead of fetching them one at a time.
379 /// One halo per function holds every operator's pushes, so it pays when several functions are
380 /// differentiated together. `clear_halo` frees them afterwards.
381 template <typename T, std::size_t NDIM>
382 void stage_halo(World& world,
383 const std::vector< std::shared_ptr< Derivative<T,NDIM> > >& grad,
384 const std::vector< Function<T,NDIM> >& v,
385 bool fence=true)
386 {
387 for (const auto& f : v) MADNESS_CHECK(f.is_reconstructed());
388 for (const auto& D : grad)
389 for (const auto& f : v) D->stage_halo(f.get_impl().get(), false);
390 if (fence) world.gop.fence();
391 }
392
393 /// Discards the neighbor halos staged on v
394
395 /// Requires a quiescent window: it frees tables the derivative may still be reading.
396 template <typename T, std::size_t NDIM>
397 void clear_halo(const std::vector< Function<T,NDIM> >& v)
398 {
399 for (const auto& f : v) f.get_impl()->halo_clear();
400 }
401
402 /// Applies a derivative operator to a vector of functions
403 template <typename T, std::size_t NDIM>
404 std::vector< Function<T,NDIM> >
405 apply(World& world,
406 const Derivative<T,NDIM>& D,
407 const std::vector< Function<T,NDIM> >& v,
408 bool fence=true)
409 {
410 reconstruct(world, v);
411 std::vector< Function<T,NDIM> > df(v.size());
412 for (unsigned int i=0; i<v.size(); ++i) {
413 df[i] = D(v[i],false);
414 }
415 if (fence) world.gop.fence();
416 return df;
417 }
418
419 /// Generates a vector of zero functions with a given tree state
420 template <typename T, std::size_t NDIM>
421 std::vector< Function<T,NDIM> >
422 zero_functions_tree_state(World& world, int n, const TreeState state, bool fence=true) {
423 std::vector< Function<T,NDIM> > r(n);
424 for (int i=0; i<n; ++i) {
425 if (state==compressed)
426 r[i] = Function<T,NDIM>(FunctionFactory<T,NDIM>(world).fence(false).compressed(true).initial_level(1));
427 else if (state==reconstructed)
428 r[i] = Function<T,NDIM>(FunctionFactory<T,NDIM>(world).fence(false));
429 else {
430 print("zero_functions_tree_state: unknown tree state");
431 throw std::runtime_error("zero_functions_tree_state: unknown tree state");
432 }
433 }
434
435 if (n && fence) world.gop.fence();
436 return r;
437
438 }
439
440 /// Generates a vector of zero functions (reconstructed)
441 template <typename T, std::size_t NDIM>
442 std::vector< Function<T,NDIM> >
443 zero_functions(World& world, int n, bool fence=true) {
444 return zero_functions_tree_state<T,NDIM>(world,n,reconstructed,fence);
445 }
446
447 /// Generates a vector of zero functions (compressed)
448 template <typename T, std::size_t NDIM>
449 std::vector< Function<T,NDIM> >
450 zero_functions_compressed(World& world, int n, bool fence=true) {
451 return zero_functions_tree_state<T,NDIM>(world,n,compressed,fence);
452 }
453
454 /// Generates a vector of zero functions, either compressed or reconstructed, depending on tensor type
455 template <typename T, std::size_t NDIM>
456 std::vector< Function<T,NDIM> >
457 zero_functions_auto_tree_state(World& world, int n, bool fence=true) {
459 return zero_functions_tree_state<T,NDIM>(world,n,state,fence);
460 }
461
462
463
464 /// orthonormalize the vectors
465 template<typename T, std::size_t NDIM>
466 std::vector<Function<T,NDIM>> orthonormalize(const std::vector<Function<T,NDIM> >& vf_in) {
467 if (vf_in.size()==0) return std::vector<Function<T,NDIM>>();
468 World& world=vf_in.front().world();
469 auto vf=copy(world,vf_in);
470 normalize(world,vf);
471 if (vf.size()==1) return copy(world,vf_in);
472 double maxq;
473 double trantol=0.0;
474 auto Q2=[](const Tensor<T>& s) {
475 Tensor<T> Q = -0.5*s;
476 for (int i=0; i<s.dim(0); ++i) Q(i,i) += 1.5;
477 return Q;
478 };
479
480 do {
481 Tensor<T> Q = Q2(matrix_inner(world, vf, vf));
482 maxq=0.0;
483 for (int i=0; i<Q.dim(0); ++i)
484 for (int j=0; j<i; ++j)
485 maxq = std::max(maxq,std::abs(Q(i,j)));
486
487 vf = transform(world, vf, Q, trantol, true);
488 truncate(world, vf);
489
490 } while (maxq>0.01);
491 normalize(world,vf);
492 return vf;
493 }
494
495
496 /// symmetric orthonormalization (see e.g. Szabo/Ostlund)
497
498 /// @param[in] the vector to orthonormalize
499 /// @param[in] overlap matrix
500 template <typename T, std::size_t NDIM>
501 std::vector<Function<T,NDIM> > orthonormalize_symmetric(
502 const std::vector<Function<T,NDIM> >& v,
503 const Tensor<T>& ovlp,
504 double lindep = 1e-12) {
505 if(v.empty()) return v;
506
507 World& world = v.front().world();
508 const size_t n = v.size();
509
510 Tensor<T> U;
512 syev(ovlp, U, s);
513 lindep *= s(s.size() - 1); // eigenvalues are in ascending order
514
515 // transform s to s^{-1/2} in-place
516 int rank = 0, nlindep = 0;
517 for(size_t i = 0; i < n; ++i) {
518 const auto s_i = s(i);
519 s(i) = 1.0 / sqrt(s_i);
520 (s_i > lindep) ? rank++ : nlindep++;
521 }
522 MADNESS_ASSERT(size_t(nlindep + rank) == n);
523
524 // warn of linearly dependent vectors and values
525 if (nlindep > 0) {
526 if (world.rank() == 0)
527 print("WARNING: linear dependencies detected in ", nlindep,
528 " functions, rank = ", rank);
529 }
530
531 // save Ut before U gets modified with s^{-1/2}
532 const Tensor<T> Ut = conj_transpose(U);
533
534 for(size_t i = 0; i < n; ++i){
535 for(size_t j = 0; j < n; ++j){
536 U(i, j) = U(i, j) * s(j);
537 }
538 }
539
540 Tensor<T> X = inner(U, Ut, 1, 0);
541
542 return transform(world, v, X);
543 }
544
545 /// convenience routine for symmetric orthonormalization (see e.g. Szabo/Ostlund)
546 /// overlap matrix is calculated
547 /// @param[in] the vector to orthonormalize
548 template <typename T, std::size_t NDIM>
549 std::vector<Function<T,NDIM> > orthonormalize_symmetric(const std::vector<Function<T,NDIM> >& v,
550 double lindep = 1e-12){
551 if(v.empty()) return v;
552
553 Tensor<T> ovlp = matrix_inner(v.front().world(), v, v, /* sym= */ true);
554
555 return orthonormalize_symmetric(v, ovlp, lindep);
556 }
557
558 /// canonical orthonormalization (see e.g. Szabo/Ostlund)
559 /// @param[in] the vector to orthonormalize
560 /// @param[in] overlap matrix
561 /// @param[in] lindep linear dependency threshold relative to largest eigenvalue
562 template <typename T, std::size_t NDIM>
563 std::vector<Function<T,NDIM> > orthonormalize_canonical(
564 const std::vector<Function<T,NDIM> >& v,
565 const Tensor<T>& ovlp,
566 double lindep = 1e-12) {
567 if(v.empty()) return v;
568
569 World& world = v.front().world();
570 const size_t n = v.size();
571
572 Tensor<T> U;
574 syev(ovlp, U, s);
575 lindep *= s(s.size() - 1); // eigenvalues are in ascending order
576
577 // transform s to s^{-1/2} in-place
578 size_t rank = 0, nlindep = 0;
579 for(size_t i = 0; i < n; ++i) {
580 const auto s_i = s(i);
581 if (s_i > lindep) {
582 s(i) = 1.0 / sqrt(s_i);
583 rank++;
584 } else {
585 nlindep++;
586 }
587 }
588 MADNESS_ASSERT(size_t(nlindep + rank) == n);
589
590 // remove linearly dependent vectors and values
591 if (nlindep > 0) {
592 if (world.rank() == 0)
593 print("Linear dependencies detected: removed ", nlindep,
594 " functions, rank = ", rank);
595 U = U(_, Slice(nlindep, -1));
596 s = s(Slice(nlindep, -1));
597 }
598
599 // modify U in-place, U is now transformation matrix (U * s^{-1/2})
600 for(size_t i = 0; i < n; ++i){
601 for(size_t j = 0; j < rank; ++j){
602 U(i, j) = U(i, j) * s(j);
603 }
604 }
605
606 return transform(world, v, U);
607 }
608
609 /// convenience routine for canonical routine for symmetric orthonormalization (see e.g. Szabo/Ostlund)
610 /// overlap matrix is calculated
611 /// @param[in] the vector to orthonormalize
612 template <typename T, std::size_t NDIM>
613 std::vector<Function<T,NDIM> > orthonormalize_canonical(const std::vector<Function<T,NDIM> >& v,
614 double lindep = 1e-12){
615 if(v.empty()) return v;
616
617 Tensor<T> ovlp = matrix_inner(v.front().world(), v, v, /* sym= */ true);
618
619 return orthonormalize_canonical(v, ovlp, lindep);
620 }
621
622 /// cholesky orthonormalization without pivoting
623 /// @param[in] the vector to orthonormalize
624 /// @param[in] overlap matrix, destroyed on return!
625 template <typename T, std::size_t NDIM>
626 std::vector<Function<T,NDIM> > orthonormalize_cd(
627 const std::vector<Function<T,NDIM> >& v,
628 Tensor<T>& ovlp) {
629
630 if (v.empty()) return v;
631
632 cholesky(ovlp); // destroys ovlp and gives back Upper ∆ Matrix from CD
633
634 Tensor<T> L = transpose(ovlp);
635 Tensor<T> Linv = inverse(L);
636 Tensor<T> U = transpose(Linv);
637
638 World& world=v.front().world();
639 return transform(world, v, U);
640
641 }
642
643 /// convenience routine for cholesky orthonormalization without pivoting
644 /// @param[in] the vector to orthonormalize
645 /// @param[in] overlap matrix
646 template <typename T, std::size_t NDIM>
647 std::vector<Function<T,NDIM> > orthonormalize_cd(const std::vector<Function<T,NDIM> >& v){
648 if(v.empty()) return v;
649
650 World& world=v.front().world();
651 Tensor<T> ovlp = matrix_inner(world, v, v, /* sym= */ true);
652
653 return orthonormalize_cd(v,ovlp);
654 }
655
656 /// @param[in] the vector to orthonormalize
657 /// @param[in] overlap matrix, will be destroyed on return!
658 /// @param[in] tolerance for numerical rank reduction
659 /// @param[out] pivoting vector, no allocation on input needed
660 /// @param[out] rank
661 /// @return orthonormalized vector (may or may not be truncated)
662 template <typename T, std::size_t NDIM>
663 std::vector<Function<T,NDIM> > orthonormalize_rrcd(
664 const std::vector<Function<T,NDIM> >& v,
665 Tensor<T>& ovlp,
666 const double tol,
667 Tensor<integer>& piv,
668 int& rank) {
669
670 if (v.empty()) {
671 return v;
672 }
673
674 rr_cholesky(ovlp,tol,piv,rank); // destroys ovlp and gives back Upper ∆ Matrix from CCD
675
676 // rearrange and truncate the functions according to the pivoting of the rr_cholesky
677 std::vector<Function<T,NDIM> > pv(rank);
678 for(integer i=0;i<rank;++i){
679 pv[i]=v[piv[i]];
680 }
681 ovlp=ovlp(Slice(0,rank-1),Slice(0,rank-1));
682
683 Tensor<T> L = transpose(ovlp);
684 Tensor<T> Linv = inverse(L);
685 Tensor<T> U = transpose(Linv);
686
687 World& world=v.front().world();
688 return transform(world, pv, U);
689 }
690
691 /// convenience routine for orthonormalize_cholesky: orthonormalize_cholesky without information on pivoting and rank
692 /// @param[in] the vector to orthonormalize
693 /// @param[in] overlap matrix
694 /// @param[in] tolerance for numerical rank reduction
695 template <typename T, std::size_t NDIM>
696 std::vector<Function<T,NDIM> > orthonormalize_rrcd(const std::vector<Function<T,NDIM> >& v, Tensor<T> ovlp , const double tol) {
697 Tensor<integer> piv;
698 int rank;
699 return orthonormalize_rrcd(v,ovlp,tol,piv,rank);
700 }
701
702 /// convenience routine for orthonormalize_cholesky: computes the overlap matrix and then calls orthonormalize_cholesky
703 /// @param[in] the vector to orthonormalize
704 /// @param[in] tolerance for numerical rank reduction
705 template <typename T, std::size_t NDIM>
706 std::vector<Function<T,NDIM> > orthonormalize_rrcd(const std::vector<Function<T,NDIM> >& v, const double tol) {
707 if (v.empty()) {
708 return v;
709 }
710 // compute overlap
711 World& world=v.front().world();
712 Tensor<T> ovlp = matrix_inner(world, v, v, /* sym= */ true);
713 return orthonormalize_rrcd(v,ovlp,tol);
714 }
715
716 /// combine two vectors
717 template <typename T, std::size_t NDIM>
718 std::vector<Function<T,NDIM> > append(const std::vector<Function<T,NDIM> > & lhs, const std::vector<Function<T,NDIM> > & rhs){
719 std::vector<Function<T,NDIM> > v=lhs;
720 for (std::size_t i = 0; i < rhs.size(); ++i) v.push_back(rhs[i]);
721 return v;
722 }
723
724 template <typename T, std::size_t NDIM>
725 std::vector<Function<T,NDIM> > flatten(const std::vector< std::vector<Function<T,NDIM> > >& vv){
726 std::vector<Function<T,NDIM> >result;
727 for(const auto& x:vv) result=append(result,x);
728 return result;
729 }
730
731 template<typename T, std::size_t NDIM>
732 std::vector<std::shared_ptr<FunctionImpl<T,NDIM>>> get_impl(const std::vector<Function<T,NDIM>>& v) {
733 std::vector<std::shared_ptr<FunctionImpl<T,NDIM>>> result;
734 for (auto& f : v) result.push_back(f.get_impl());
735 return result;
736 }
737
738 template<typename T, std::size_t NDIM>
739 void set_impl(std::vector<Function<T,NDIM>>& v, const std::vector<std::shared_ptr<FunctionImpl<T,NDIM>>> vimpl) {
740 MADNESS_CHECK(vimpl.size()==v.size());
741 for (std::size_t i=0; i<vimpl.size(); ++i) v[i].set_impl(vimpl[i]);
742 }
743
744 template<typename T, std::size_t NDIM>
745 std::vector<Function<T,NDIM>> impl2function(const std::vector<std::shared_ptr<FunctionImpl<T,NDIM>>> vimpl) {
746 std::vector<Function<T,NDIM>> v(vimpl.size());
747 for (std::size_t i=0; i<vimpl.size(); ++i) v[i].set_impl(vimpl[i]);
748 return v;
749 }
750
751
752 /// Transforms a vector of functions according to new[i] = sum[j] old[j]*c[j,i]
753
754 /// Uses sparsity in the transformation matrix --- set small elements to
755 /// zero to take advantage of this.
756 template <typename T, typename R, std::size_t NDIM>
757 std::vector< Function<TENSOR_RESULT_TYPE(T,R),NDIM> >
759 const std::vector< Function<T,NDIM> >& v,
760 const Tensor<R>& c,
761 bool fence=true) {
762
763 PROFILE_BLOCK(Vtransformsp);
764 typedef TENSOR_RESULT_TYPE(T,R) resultT;
765 int n = v.size(); // n is the old dimension
766 int m = c.dim(1); // m is the new dimension
767 MADNESS_CHECK(n==c.dim(0));
768
769 std::vector< Function<resultT,NDIM> > vc = zero_functions_compressed<resultT,NDIM>(world, m);
770 compress(world, v);
771
772 for (int i=0; i<m; ++i) {
773 for (int j=0; j<n; ++j) {
774 if (c(j,i) != R(0.0)) vc[i].gaxpy(resultT(1.0),v[j],resultT(c(j,i)),false);
775 }
776 }
777
778 if (fence) world.gop.fence();
779 return vc;
780 }
781
782 /// Transforms a vector of functions according to new[i] = sum[j] old[j]*c[j,i]
783
784 /// all trees are in reconstructed state, final trees have to be summed down if no fence is present
785 template <typename T, typename R, std::size_t NDIM>
786 std::vector< Function<TENSOR_RESULT_TYPE(T,R),NDIM> >
788 const std::vector< Function<T,NDIM> >& v,
789 const Tensor<R>& c,
790 bool fence=true) {
791
792 PROFILE_BLOCK(Vtransformsp);
793 typedef TENSOR_RESULT_TYPE(T,R) resultT;
794 int n = v.size(); // n is the old dimension
795 int m = c.dim(1); // m is the new dimension
796 MADNESS_CHECK(n==c.dim(0));
797
798 // if we fence set the right tree state here, otherwise it has to be correct from the start.
800 for (const auto& vv : v) MADNESS_CHECK_THROW(
801 vv.get_impl()->get_tree_state()==reconstructed,"trees have to be reconstructed in transform_reconstructed");
802
803 std::vector< Function<resultT,NDIM> > result = zero_functions<resultT,NDIM>(world, m);
804
805 for (int i=0; i<m; ++i) {
806 result[i].get_impl()->set_tree_state(redundant_after_merge);
807 for (int j=0; j<n; ++j) {
808 if (c(j,i) != R(0.0)) v[j].get_impl()->accumulate_trees(*(result[i].get_impl()),resultT(c(j,i)),true);
809 }
810 }
811
812 // if we fence we can as well finish the job here. Otherwise no harm done, as the tree state is well-defined.
813 if (fence) {
814 world.gop.fence();
815 // for (auto& r : vc) r.sum_down(false);
816 for (auto& r : result) r.get_impl()->finalize_sum();
817 world.gop.fence();
818 }
819 return result;
820 }
821
822 /// this version of transform uses Function::vtransform and screens
823 /// using both elements of `c` and `v`
824 template <typename L, typename R, std::size_t NDIM>
825 std::vector< Function<TENSOR_RESULT_TYPE(L,R),NDIM> >
826 transform(World& world, const std::vector< Function<L,NDIM> >& v,
827 const Tensor<R>& c, double tol, bool fence=true) {
828 PROFILE_BLOCK(Vtransform);
829 MADNESS_ASSERT(v.size() == (unsigned int)(c.dim(0)));
830
831 std::vector< Function<TENSOR_RESULT_TYPE(L,R),NDIM> > vresult
832 = zero_functions_compressed<TENSOR_RESULT_TYPE(L,R),NDIM>(world, c.dim(1));
833
834 compress(world, v, true);
835 vresult[0].vtransform(v, c, vresult, tol, fence);
836 return vresult;
837 }
838
839 template <typename T, typename R, std::size_t NDIM>
840 std::vector< Function<TENSOR_RESULT_TYPE(T,R),NDIM> >
842 const std::vector< Function<T,NDIM> >& v,
843 const DistributedMatrix<R>& c,
844 bool fence=true) {
846
847 typedef TENSOR_RESULT_TYPE(T,R) resultT;
848 long n = v.size(); // n is the old dimension
849 long m = c.rowdim(); // m is the new dimension
850 MADNESS_ASSERT(n==c.coldim());
851
852 // new(i) = sum(j) old(j) c(j,i)
853
854 Tensor<T> tmp(n,m);
855 c.copy_to_replicated(tmp); // for debugging
856 tmp = transpose(tmp);
857
858 std::vector< Function<resultT,NDIM> > vc = zero_functions_compressed<resultT,NDIM>(world, m);
859 compress(world, v);
860
861 for (int i=0; i<m; ++i) {
862 for (int j=0; j<n; ++j) {
863 if (tmp(j,i) != R(0.0)) vc[i].gaxpy(1.0,v[j],tmp(j,i),false);
864 }
865 }
866
867 if (fence) world.gop.fence();
868 return vc;
869 }
870
871
872 /// Scales inplace a vector of functions by distinct values
873 template <typename T, typename Q, std::size_t NDIM>
874 void scale(World& world,
875 std::vector< Function<T,NDIM> >& v,
876 const std::vector<Q>& factors,
877 bool fence=true) {
878 PROFILE_BLOCK(Vscale);
879 for (unsigned int i=0; i<v.size(); ++i) v[i].scale(factors[i],false);
880 if (fence) world.gop.fence();
881 }
882
883 /// Scales inplace a vector of functions by the same
884 template <typename T, typename Q, std::size_t NDIM>
885 void scale(World& world,
886 std::vector< Function<T,NDIM> >& v,
887 const Q factor,
888 bool fence=true) {
889 PROFILE_BLOCK(Vscale);
890 for (unsigned int i=0; i<v.size(); ++i) v[i].scale(factor,false);
891 if (fence) world.gop.fence();
892 }
893
894 /// Computes the 2-norms of a vector of functions
895 template <typename T, std::size_t NDIM>
896 std::vector<double> norm2s(World& world,
897 const std::vector< Function<T,NDIM> >& v) {
898 PROFILE_BLOCK(Vnorm2);
899 std::vector<double> norms(v.size());
901 for (unsigned int i=0; i<v.size(); ++i) norms[i] = v[i].norm2sq_local();
902 world.gop.sum(&norms[0], norms.size());
903 for (unsigned int i=0; i<v.size(); ++i) norms[i] = sqrt(norms[i]);
904 world.gop.fence();
905 return norms;
906 }
907 /// Computes the 2-norms of a vector of functions
908 template <typename T, std::size_t NDIM>
909 Tensor<double> norm2s_T(World& world, const std::vector<Function<T, NDIM>>& v) {
910 PROFILE_BLOCK(Vnorm2);
911 Tensor<double> norms(v.size());
913 for (unsigned int i = 0; i < v.size(); ++i) norms[i] = v[i].norm2sq_local();
914 world.gop.sum(&norms[0], norms.size());
915 for (unsigned int i = 0; i < v.size(); ++i) norms[i] = sqrt(norms[i]);
916 world.gop.fence();
917 return norms;
918 }
919
920 /// Computes the 2-norm of a vector of functions
921 template <typename T, std::size_t NDIM>
922 double norm2(World& world,const std::vector< Function<T,NDIM> >& v) {
923 PROFILE_BLOCK(Vnorm2);
924 if (v.size()==0) return 0.0;
926 std::vector<double> norms(v.size());
927 for (unsigned int i=0; i<v.size(); ++i) norms[i] = v[i].norm2sq_local();
928 world.gop.sum(&norms[0], norms.size());
929 for (unsigned int i=1; i<v.size(); ++i) norms[0] += norms[i];
930 world.gop.fence();
931 return sqrt(norms[0]);
932 }
933
934 inline double conj(double x) {
935 return x;
936 }
937
938 inline double conj(float x) {
939 return x;
940 }
941
942// !!! FIXME: this task is broken because FunctionImpl::inner_local forces a
943// future on return from WorldTaskQueue::reduce, which will causes a deadlock if
944// run inside a task. This behavior must be changed before this task can be used
945// again.
946//
947// template <typename T, typename R, std::size_t NDIM>
948// struct MatrixInnerTask : public TaskInterface {
949// Tensor<TENSOR_RESULT_TYPE(T,R)> result; // Must be a copy
950// const Function<T,NDIM>& f;
951// const std::vector< Function<R,NDIM> >& g;
952// long jtop;
953//
954// MatrixInnerTask(const Tensor<TENSOR_RESULT_TYPE(T,R)>& result,
955// const Function<T,NDIM>& f,
956// const std::vector< Function<R,NDIM> >& g,
957// long jtop)
958// : result(result), f(f), g(g), jtop(jtop) {}
959//
960// void run(World& world) {
961// for (long j=0; j<jtop; ++j) {
962// result(j) = f.inner_local(g[j]);
963// }
964// }
965//
966// private:
967// /// Get the task id
968//
969// /// \param id The id to set for this task
970// virtual void get_id(std::pair<void*,unsigned short>& id) const {
971// PoolTaskInterface::make_id(id, *this);
972// }
973// }; // struct MatrixInnerTask
974
975
976
977 template <typename T, std::size_t NDIM>
979 const std::vector< Function<T,NDIM> >& f,
980 const std::vector< Function<T,NDIM> >& g,
981 bool sym=false)
982 {
985 const int64_t n = A.coldim();
986 const int64_t m = A.rowdim();
987 MADNESS_ASSERT(int64_t(f.size()) == n && int64_t(g.size()) == m);
988
989 // Assume we can always create an ichunk*jchunk matrix locally
990 const int ichunk = 1000;
991 const int jchunk = 1000; // 1000*1000*8 = 8 MBytes
992 for (int64_t ilo=0; ilo<n; ilo+=ichunk) {
993 int64_t ihi = std::min(ilo + ichunk, n);
994 std::vector< Function<T,NDIM> > ivec(f.begin()+ilo, f.begin()+ihi);
995 for (int64_t jlo=0; jlo<m; jlo+=jchunk) {
996 int64_t jhi = std::min(jlo + jchunk, m);
997 std::vector< Function<T,NDIM> > jvec(g.begin()+jlo, g.begin()+jhi);
998
999 Tensor<T> P = matrix_inner(A.get_world(), ivec, jvec);
1000 A.copy_from_replicated_patch(ilo, ihi - 1, jlo, jhi - 1, P);
1001 }
1002 }
1003 return A;
1004 }
1005
1006 /// Computes the matrix inner product of two function vectors - q(i,j) = inner(f[i],g[j])
1007
1008 /// For complex types symmetric is interpreted as Hermitian.
1009
1010 /// The current parallel loop is non-optimal but functional.
1011 template <typename T, typename R, std::size_t NDIM>
1013 const std::vector< Function<T,NDIM> >& f,
1014 const std::vector< Function<R,NDIM> >& g,
1015 bool sym=false)
1016 {
1017 world.gop.fence();
1018 auto tensor_type = [](const std::vector<Function<T,NDIM>>& v) {
1019 return v.front().get_impl()->get_tensor_type();
1020 };
1021 TreeState operating_state=tensor_type(f)==TT_FULL ? compressed : redundant;
1022 ensure_tree_state_respecting_fence(f,operating_state,true);
1023 ensure_tree_state_respecting_fence(g,operating_state,true);
1024
1025 std::vector<const FunctionImpl<T,NDIM>*> left(f.size());
1026 std::vector<const FunctionImpl<R,NDIM>*> right(g.size());
1027 for (unsigned int i=0; i<f.size(); i++) left[i] = f[i].get_impl().get();
1028 for (unsigned int i=0; i<g.size(); i++) right[i]= g[i].get_impl().get();
1029
1031
1032 world.gop.fence();
1033 world.gop.sum(r.ptr(),f.size()*g.size());
1034
1035 return r;
1036 }
1037
1038 /// Computes the matrix inner product of two function vectors - q(i,j) = inner(f[i],g[j])
1039
1040 /// For complex types symmetric is interpreted as Hermitian.
1041 ///
1042 /// The current parallel loop is non-optimal but functional.
1043 template <typename T, typename R, std::size_t NDIM>
1045 const std::vector< Function<T,NDIM> >& f,
1046 const std::vector< Function<R,NDIM> >& g,
1047 bool sym=false) {
1048 PROFILE_BLOCK(Vmatrix_inner);
1049 long n=f.size(), m=g.size();
1050 Tensor< TENSOR_RESULT_TYPE(T,R) > r(n,m);
1051 if (sym) MADNESS_ASSERT(n==m);
1052
1053 world.gop.fence();
1054 compress(world, f);
1055 if ((void*)(&f) != (void*)(&g)) compress(world, g);
1056
1057 for (long i=0; i<n; ++i) {
1058 long jtop = m;
1059 if (sym) jtop = i+1;
1060 for (long j=0; j<jtop; ++j) {
1061 r(i,j) = f[i].inner_local(g[j]);
1062 if (sym) r(j,i) = conj(r(i,j));
1063 }
1064 }
1065
1066// for (long i=n-1; i>=0; --i) {
1067// long jtop = m;
1068// if (sym) jtop = i+1;
1069// world.taskq.add(new MatrixInnerTask<T,R,NDIM>(r(i,_), f[i], g, jtop));
1070// }
1071 world.gop.fence();
1072 world.gop.sum(r.ptr(),n*m);
1073
1074// if (sym) {
1075// for (int i=0; i<n; ++i) {
1076// for (int j=0; j<i; ++j) {
1077// r(j,i) = conj(r(i,j));
1078// }
1079// }
1080// }
1081 return r;
1082 }
1083
1084 /// Computes the element-wise inner product of two function vectors - q(i) = inner(f[i],g[i])
1085
1086 /// works in reconstructed or compressed state, state is chosen based on TensorType
1087 template <typename T, typename R, std::size_t NDIM>
1089 const std::vector< Function<T,NDIM> >& f,
1090 const std::vector< Function<R,NDIM> >& g) {
1091 PROFILE_BLOCK(Vinnervv);
1092 long n=f.size(), m=g.size();
1093 MADNESS_CHECK(n==m);
1094 Tensor< TENSOR_RESULT_TYPE(T,R) > r(n);
1095 if (n==0) return r;
1096
1097 auto tensor_type = [](const std::vector<Function<T,NDIM>>& v) {
1098 return v.front().get_impl()->get_tensor_type();
1099 };
1100 TreeState operating_state=tensor_type(f)==TT_FULL ? compressed : redundant;
1101 ensure_tree_state_respecting_fence(f,operating_state,true);
1102 ensure_tree_state_respecting_fence(g,operating_state,true);
1103
1104 for (long i=0; i<n; ++i) r(i) = f[i].inner_local(g[i]);
1105
1106 world.taskq.fence();
1107 world.gop.sum(r.ptr(),n);
1108 world.gop.fence();
1109 return r;
1110 }
1111
1112
1113 /// Computes the inner product of a function with a function vector - q(i) = inner(f,g[i])
1114
1115 /// works in reconstructed or compressed state, state is chosen based on TensorType
1116 template <typename T, typename R, std::size_t NDIM>
1118 const Function<T,NDIM>& f,
1119 const std::vector< Function<R,NDIM> >& g) {
1120 PROFILE_BLOCK(Vinner);
1121 long n=g.size();
1122 Tensor< TENSOR_RESULT_TYPE(T,R) > r(n);
1123
1124 auto tensor_type = [](const std::vector<Function<T,NDIM>>& v) {
1125 return v.front().get_impl()->get_tensor_type();
1126 };
1127 TreeState operating_state=tensor_type(g)==TT_FULL ? compressed : redundant;
1128 f.change_tree_state(operating_state,false);
1129 ensure_tree_state_respecting_fence(g,operating_state,true);
1130 world.gop.fence();
1131
1132 for (long i=0; i<n; ++i) {
1133 r(i) = f.inner_local(g[i]);
1134 }
1135
1136 world.taskq.fence();
1137 world.gop.sum(r.ptr(),n);
1138 world.gop.fence();
1139 return r;
1140 }
1141
1142 /// inner function with right signature for the nonlinear solver
1143 /// this is needed for the KAIN solvers and other functions
1144 template <typename T, typename R, std::size_t NDIM>
1145 TENSOR_RESULT_TYPE(T,R) inner( const std::vector< Function<T,NDIM> >& f,
1146 const std::vector< Function<R,NDIM> >& g){
1147 MADNESS_ASSERT(f.size()==g.size());
1148 if(f.empty()) return 0.0;
1149 else return inner(f[0].world(),f,g).sum();
1150 }
1151
1152
1153 /// Multiplies a function against a vector of functions --- q[i] = a * v[i]
1154 template <typename T, typename R, std::size_t NDIM>
1155 std::vector< Function<TENSOR_RESULT_TYPE(T,R), NDIM> >
1156 mul(World& world,
1157 const Function<T,NDIM>& a,
1158 const std::vector< Function<R,NDIM> >& v,
1159 bool fence=true) {
1160 PROFILE_BLOCK(Vmul);
1161 make_redundant(world, v, false);
1162 a.make_redundant(false);
1163 world.gop.fence();
1164 return vmulXX(a, v, 0.0, fence);
1165 }
1166
1167 /// Multiplies a function against a vector of functions using sparsity of a and v[i] --- q[i] = a * v[i]
1168 ///
1169 /// Box pairs whose estimated contribution falls below the tolerance are skipped instead
1170 /// of being multiplied. Both inputs are made redundant; the screening reads their
1171 /// norm_tree and dnorm_tree.
1172 ///
1173 /// Leaves both inputs in redundant form. Function is a shallow handle, so this is visible
1174 /// to the caller: logically const, not bitwise const. Converting back is not free, so a
1175 /// caller that reuses the operands afterwards must do it itself.
1176 ///
1177 /// @param[in] tol target absolute accuracy of the product; the safety margin is applied
1178 /// internally (FunctionImpl::MUL_SCREENING_SAFETY), so pass the accuracy
1179 /// wanted, not a pre-scaled value. tol=0 multiplies exactly. The criterion
1180 /// estimates the neglected cross terms rather than bounding them: the error
1181 /// tracks tol up to a measured O(1-20) constant and decays as ~tol^0.75
1182 /// rather than ~tol (see test_mul_sparse.cc). The meaning differs from the
1183 /// earlier norm_tree-based screen, so a previously tuned value needs
1184 /// re-checking.
1185 /// @param[in] do_make_redundant if false, both inputs must already be redundant
1186 template <typename T, typename R, std::size_t NDIM>
1187 std::vector< Function<TENSOR_RESULT_TYPE(T,R), NDIM> >
1189 const Function<T,NDIM>& a,
1190 const std::vector< Function<R,NDIM> >& v,
1191 double tol,
1192 bool fence=true,
1193 bool do_make_redundant=true) {
1194 PROFILE_BLOCK(Vmulsp);
1195 if (do_make_redundant) {
1196 try {
1199 } catch (...) {
1200 print("could not respect fence in mul_sparse");
1201 a.make_redundant(false);
1202 make_redundant(world, v, false);
1203 world.gop.fence();
1204 }
1205 } else if (!v.empty()) {
1206 MADNESS_CHECK_THROW(a.get_impl()->get_tree_state() == TreeState::redundant,
1207 "mul_sparse: left input must be redundant when do_make_redundant=false");
1209 "mul_sparse: right inputs must be redundant when do_make_redundant=false");
1210 }
1211 return vmulXX(a, v, tol, fence);
1212 }
1213
1214 /// Multiplies two vectors of functions using sparsity of a[i] and b[i] --- q[i] = a[i] * b[i]
1215 ///
1216 /// Box pairs whose estimated contribution falls below the tolerance are skipped instead
1217 /// of being multiplied. Both inputs are made redundant; the screening reads their
1218 /// norm_tree and dnorm_tree.
1219 ///
1220 /// Leaves both inputs in redundant form. Function is a shallow handle, so this is visible
1221 /// to the caller: logically const, not bitwise const. Converting back is not free, so a
1222 /// caller that reuses the operands afterwards must do it itself.
1223 ///
1224 /// @param[in] tol target absolute accuracy of the product; the safety margin is applied
1225 /// internally (FunctionImpl::MUL_SCREENING_SAFETY), so pass the accuracy
1226 /// wanted, not a pre-scaled value. tol=0 multiplies exactly. The criterion
1227 /// estimates the neglected cross terms rather than bounding them: the error
1228 /// tracks tol up to a measured O(1-20) constant and decays as ~tol^0.75
1229 /// rather than ~tol (see test_mul_sparse.cc). The meaning differs from the
1230 /// earlier norm_tree-based screen, so a previously tuned value needs
1231 /// re-checking.
1232 /// @param[in] do_make_redundant if false, both inputs must already be redundant
1233 template <typename T, typename R, std::size_t NDIM>
1234 std::vector< Function<TENSOR_RESULT_TYPE(T,R), NDIM> >
1236 const std::vector< Function<T,NDIM> >& a,
1237 const std::vector< Function<R,NDIM> >& b,
1238 double tol,
1239 bool fence=true,
1240 bool do_make_redundant=true) {
1241 PROFILE_BLOCK(Vmulvv);
1242 if (do_make_redundant) {
1243 try {
1246 } catch (...) {
1247 print("could not respect fence in mul_sparse");
1248 make_redundant(world, a, false);
1249 make_redundant(world, b, false);
1250 world.gop.fence();
1251 }
1252 }
1253 std::vector< Function<TENSOR_RESULT_TYPE(T,R),NDIM> > q(a.size());
1254 for (unsigned int i=0; i<a.size(); ++i) {
1255 q[i] = mul_sparse(a[i], b[i], tol, false, false);
1256 }
1257 if (fence) world.gop.fence();
1258 return q;
1259 }
1260
1261
1262 /// Outer product of a vector of functions with a vector of functions using sparsity
1263
1264 /// \tparam T type parameter for first factor
1265 /// \tparam R type parameter for second factor
1266 /// \tparam NDIM dimension of first and second factors
1267 /// \param world the world
1268 /// \param f first vector of functions
1269 /// \param g second vector of functions
1270 /// \param tol target absolute accuracy of each product; see mul_sparse for the
1271 /// semantics, including the internal safety margin and tol=0
1272 /// \param fence force fence (will always fence if necessary)
1273 /// \param symm if true, only compute f(i) * g(j) for j<=i
1274 /// \return fg(i,j) = f(i) * g(j), as a vector of vectors
1275 template <typename T, typename R, std::size_t NDIM>
1276 std::vector<std::vector<Function<TENSOR_RESULT_TYPE(T, R), NDIM> > >
1278 const std::vector<Function<R, NDIM> > &f,
1279 const std::vector<Function<R, NDIM> > &g,
1280 double tol,
1281 bool fence = true,
1282 bool symm = false) {
1283 PROFILE_BLOCK(Vmulsp);
1284 bool same=(&f == &g);
1285 make_redundant(world, f, false);
1286 if (not same) make_redundant(world, g, false);
1287 world.gop.fence();
1288
1289 std::vector<std::vector<Function<R,NDIM> > >result(f.size());
1290 std::vector<Function<R,NDIM>> g_i;
1291 for (int64_t i=f.size()-1; i>=0; --i) {
1292 if (!symm)
1293 result[i]= vmulXX(f[i], g, tol, false);
1294 else {
1295 if (g_i.empty()) g_i = g;
1296 g_i.resize(i+1); // this shrinks g_i down to single function for i=0
1297 result[i]= vmulXX(f[i], g_i, tol, false);
1298 }
1299 }
1300 if (fence) world.gop.fence();
1301 return result;
1302 }
1303
1304 /// Makes the norm tree for all functions in a vector
1305 template <typename T, std::size_t NDIM>
1306 void norm_tree(World& world,
1307 const std::vector< Function<T,NDIM> >& v,
1308 bool fence=true)
1309 {
1310 PROFILE_BLOCK(Vnorm_tree);
1311 for (unsigned int i=0; i<v.size(); ++i) {
1312 v[i].norm_tree(false);
1313 }
1314 if (fence) world.gop.fence();
1315 }
1316
1317 /// Multiplies two vectors of functions q[i] = a[i] * b[i]; see mul_sparse to screen
1318 template <typename T, typename R, std::size_t NDIM>
1319 std::vector< Function<TENSOR_RESULT_TYPE(T,R), NDIM> >
1320 mul(World& world,
1321 const std::vector< Function<T,NDIM> >& a,
1322 const std::vector< Function<R,NDIM> >& b,
1323 bool fence=true,
1324 bool do_make_redundant=true) {
1325 PROFILE_BLOCK(Vmulvv);
1326 if (do_make_redundant) {
1327 try {
1330 } catch (...) {
1331 print("could not respect fence in mul");
1332 make_redundant(world, a, false);
1333 make_redundant(world, b, false);
1334 world.gop.fence();
1335 }
1336 }
1337 std::vector< Function<TENSOR_RESULT_TYPE(T,R),NDIM> > q(a.size());
1338 for (unsigned int i=0; i<a.size(); ++i) {
1339 q[i] = mul(a[i], b[i], false, false);
1340 }
1341 if (fence) world.gop.fence();
1342 return q;
1343 }
1344
1345
1346 /// multiply a high-dimensional function with a low-dimensional function
1347
1348 /// @param[in] f NDIM function of NDIM dimensions
1349 /// @param[in] g LDIM function of LDIM
1350 /// @param[in] v dimension indices of f to multiply
1351 /// @return h[i](0,1,2,3) = f(0,1,2,3) * g[i](1,2,3) for v={1,2,3}
1352 template<typename T, std::size_t NDIM, std::size_t LDIM>
1353 std::vector<Function<T,NDIM> > partial_mul(const Function<T,NDIM> f, const std::vector<Function<T,LDIM> > g,
1354 const int particle) {
1355
1356 World& world=f.world();
1357 std::vector<Function<T,NDIM> > result(g.size());
1358 for (auto& r : result) r.set_impl(f, false);
1359
1360 FunctionImpl<T,NDIM>* fimpl=f.get_impl().get();
1361// fimpl->make_redundant(false);
1362 fimpl->change_tree_state(redundant,false);
1363 make_redundant(world,g,false);
1364 world.gop.fence();
1365
1366 for (std::size_t i=0; i<result.size(); ++i) {
1367 FunctionImpl<T,LDIM>* gimpl=g[i].get_impl().get();
1368 result[i].get_impl()->multiply(fimpl,gimpl,particle); // stupid naming inconsistency
1369 }
1370 world.gop.fence();
1371
1372 fimpl->undo_redundant(false);
1373 for (auto& ig : g) ig.get_impl()->undo_redundant(false);
1374 world.gop.fence();
1375 return result;
1376 }
1377
1378 template<typename T, std::size_t NDIM, std::size_t LDIM>
1379 std::vector<Function<T,NDIM> > multiply(const Function<T,NDIM> f, const std::vector<Function<T,LDIM> > g,
1380 const std::tuple<int,int,int> v) {
1381 return partial_mul<T,NDIM,LDIM>(f,g,std::array<int,3>({std::get<0>(v),std::get<1>(v),std::get<2>(v)}));
1382 }
1383
1384
1385/// Computes the square of a vector of functions --- q[i] = v[i]**2
1386 template <typename T, std::size_t NDIM>
1387 std::vector< Function<T,NDIM> >
1389 const std::vector< Function<T,NDIM> >& v,
1390 bool fence=true) {
1391 return mul<T,T,NDIM>(world, v, v, fence);
1392// std::vector< Function<T,NDIM> > vsq(v.size());
1393// for (unsigned int i=0; i<v.size(); ++i) {
1394// vsq[i] = square(v[i], false);
1395// }
1396// if (fence) world.gop.fence();
1397// return vsq;
1398 }
1399
1400
1401 /// Computes the square of a vector of functions --- q[i] = abs(v[i])**2
1402 template <typename T, std::size_t NDIM>
1403 std::vector< Function<typename Tensor<T>::scalar_type,NDIM> >
1404 abssq(World& world,
1405 const std::vector< Function<T,NDIM> >& v,
1406 bool fence=true) {
1407 typedef typename Tensor<T>::scalar_type scalartype;
1408 reconstruct(world,v);
1409 std::vector<Function<scalartype,NDIM> > result(v.size());
1410 for (size_t i=0; i<v.size(); ++i) result[i]=abs_square(v[i],false);
1411 if (fence) world.gop.fence();
1412 return result;
1413 }
1414
1415
1416 /// Sets the threshold in a vector of functions
1417 template <typename T, std::size_t NDIM>
1418 void set_thresh(World& world, std::vector< Function<T,NDIM> >& v, double thresh, bool fence=true) {
1419 for (unsigned int j=0; j<v.size(); ++j) {
1420 v[j].set_thresh(thresh,false);
1421 }
1422 if (fence) world.gop.fence();
1423 }
1424
1425 /// Returns the complex conjugate of the vector of functions
1426 template <typename T, std::size_t NDIM>
1427 std::vector< Function<T,NDIM> >
1428 conj(World& world,
1429 const std::vector< Function<T,NDIM> >& v,
1430 bool fence=true) {
1431 PROFILE_BLOCK(Vconj);
1432 std::vector< Function<T,NDIM> > r = copy(world, v); // Currently don't have oop conj
1433 for (unsigned int i=0; i<v.size(); ++i) {
1434 r[i].conj(false);
1435 }
1436 if (fence) world.gop.fence();
1437 return r;
1438 }
1439
1440 /// Returns a deep copy of a vector of functions
1441 template <typename T, typename R, std::size_t NDIM>
1442 std::vector< Function<R,NDIM> > convert(World& world,
1443 const std::vector< Function<T,NDIM> >& v, bool fence=true) {
1444 PROFILE_BLOCK(Vcopy);
1445 std::vector< Function<R,NDIM> > r(v.size());
1446 for (unsigned int i=0; i<v.size(); ++i) {
1447 r[i] = convert<T,R,NDIM>(v[i], false);
1448 }
1449 if (fence) world.gop.fence();
1450 return r;
1451 }
1452
1453
1454 /// Returns a deep copy of a vector of functions
1455 template <typename T, std::size_t NDIM>
1456 std::vector< Function<T,NDIM> >
1457 copy(World& world,
1458 const std::vector< Function<T,NDIM> >& v,
1459 bool fence=true) {
1460 PROFILE_BLOCK(Vcopy);
1461 std::vector< Function<T,NDIM> > r(v.size());
1462 for (unsigned int i=0; i<v.size(); ++i) {
1463 r[i] = copy(v[i], false);
1464 }
1465 if (fence) world.gop.fence();
1466 return r;
1467 }
1468
1469
1470 /// Returns a deep copy of a vector of functions
1471 template <typename T, std::size_t NDIM>
1472 std::vector< Function<T,NDIM> >
1473 copy(const std::vector< Function<T,NDIM> >& v, bool fence=true) {
1474 PROFILE_BLOCK(Vcopy);
1475 std::vector< Function<T,NDIM> > r(v.size());
1476 if (v.size()>0) r=copy(v.front().world(),v,fence);
1477 return r;
1478 }
1479
1480 /// Returns a vector of `n` deep copies of a function
1481 template <typename T, std::size_t NDIM>
1482 std::vector< Function<T,NDIM> >
1484 const Function<T,NDIM>& v,
1485 const unsigned int n,
1486 bool fence=true) {
1487 PROFILE_BLOCK(Vcopy1);
1488 std::vector< Function<T,NDIM> > r(n);
1489 for (unsigned int i=0; i<n; ++i) {
1490 r[i] = copy(v, false);
1491 }
1492 if (fence) world.gop.fence();
1493 return r;
1494 }
1495
1496 /// Create a new copy of the function with different distribution and optional
1497 /// fence
1498
1499 /// Works in either basis. Different distributions imply
1500 /// asynchronous communication and the optional fence is
1501 /// collective.
1502 //
1503 /// Returns a deep copy of a vector of functions
1504
1505 template <typename T, std::size_t NDIM>
1506 std::vector<Function<T, NDIM>> copy(World& world,
1507 const std::vector<Function<T, NDIM>>& v,
1508 const std::shared_ptr<WorldDCPmapInterface<Key<NDIM>>>& pmap,
1509 bool fence = true) {
1510 PROFILE_BLOCK(Vcopy);
1511 std::vector<Function<T, NDIM>> r(v.size());
1512 for (unsigned int i = 0; i < v.size(); ++i) {
1513 r[i] = copy(v[i], pmap, false);
1514 }
1515 if (fence) world.gop.fence();
1516 return r;
1517 }
1518
1519 /// owner[j] = j % nranks. For redistribute_to_batches.
1520 inline std::vector<ProcessID> assign_round_robin(std::size_t nfunc, int nranks) {
1521 MADNESS_CHECK(nranks > 0);
1522 std::vector<ProcessID> owner(nfunc);
1523 for (std::size_t j = 0; j < nfunc; ++j) owner[j] = ProcessID(j % std::size_t(nranks));
1524 return owner;
1525 }
1526
1527 /// Cost-balanced assignment: descending cost, each function to the least-loaded
1528 /// rank (LPT greedy). Deterministic for a replicated cost[] (stable sort, index
1529 /// tie-break), so owner[] agrees across ranks. For redistribute_to_batches.
1530 /// @param[in] cost per-function cost proxy (>= 0), identical on every rank
1531 inline std::vector<ProcessID> assign_cost_aware(const std::vector<double>& cost, int nranks) {
1532 MADNESS_CHECK(nranks > 0);
1533 const std::size_t nfunc = cost.size();
1534 std::vector<ProcessID> owner(nfunc);
1535 std::vector<std::size_t> order(nfunc);
1536 for (std::size_t j = 0; j < nfunc; ++j) order[j] = j;
1537 // descending cost; ascending index breaks ties -> stable and reproducible
1538 std::stable_sort(order.begin(), order.end(),
1539 [&](std::size_t a, std::size_t b) { return cost[a] > cost[b]; });
1540 std::vector<double> load(std::size_t(nranks), 0.0);
1541 for (std::size_t k = 0; k < nfunc; ++k) {
1542 int best = 0; // least-loaded rank; smallest
1543 for (int r = 1; r < nranks; ++r) // rank index breaks ties
1544 if (load[std::size_t(r)] < load[std::size_t(best)]) best = r;
1545 const std::size_t j = order[k];
1546 owner[j] = ProcessID(best);
1547 load[std::size_t(best)] += std::max(1.0, cost[j]); // floor: zero-cost funcs still rotate
1548 }
1549 return owner;
1550 }
1551
1552 /// Global coefficient count per function -- one reduction, identical on every rank,
1553 /// so safe for a deterministic assignment. A proxy for convolution cost; does not
1554 /// predict result-tree refinement.
1555 template <typename T, std::size_t NDIM>
1556 std::vector<double> function_costs(World& world, const std::vector<Function<T, NDIM>>& v) {
1557 std::vector<double> cost(v.size(), 0.0);
1558 for (std::size_t j = 0; j < v.size(); ++j) cost[j] = double(v[j].size_local());
1559 if (!v.empty()) world.gop.sum(cost.data(), cost.size());
1560 return cost;
1561 }
1562
1563 /// Move each v[j] so its whole tree lives on rank owner[j], via the coalesced
1564 /// WorldContainer transport (bulk AMs, erase-after-copy: streams, no 2x transient).
1565 /// State-preserving. Each function gets its OWN single-owner pmap (Key<NDIM> is
1566 /// function-agnostic), and the pmap outlives the call -- until redistributed again,
1567 /// every operation on v[j] runs on rank owner[j] alone.
1568 ///
1569 /// @param[in,out] v functions to localize (moved in place)
1570 /// @param[in] owner destination rank per function; MUST be identical on every
1571 /// rank (checked collectively)
1572 /// @param[in] cap_bytes soft cap per message (0 => ~1 MiB, sized for the default
1573 /// MAD_BUFFER_SIZE; lower it if that buffer was shrunk)
1574 /// @param[in] rotate stagger destinations to reduce incast
1575 template <typename T, std::size_t NDIM>
1577 std::vector<Function<T, NDIM>>& v,
1578 const std::vector<ProcessID>& owner,
1579 std::size_t cap_bytes = 0,
1580 bool rotate = true) {
1581 MADNESS_CHECK(owner.size() == v.size());
1582 if (v.empty()) return;
1583
1584 // owner[] must agree across ranks -- divergence silently corrupts ownership
1585 {
1586 long h = 0;
1587 for (std::size_t j = 0; j < owner.size(); ++j) h += long(owner[j]) * long(j + 1);
1588 long hmax = h, hmin = h;
1589 world.gop.max(hmax);
1590 world.gop.min(hmin);
1591 MADNESS_CHECK(hmax == hmin);
1592 }
1593
1594 // chunk cap in #boxes; box size bounded by the functions' own k, not
1595 // FunctionDefaults (v may carry its own k)
1596 if (cap_bytes == 0) cap_bytes = 1024 * 1024; // ~1 MiB, under the default RMI buffer
1597 long kmax = 1;
1598 for (const auto& f : v) kmax = std::max(kmax, long(f.k()));
1599 std::size_t box_bytes = sizeof(T);
1600 for (std::size_t d = 0; d < NDIM; ++d) box_bytes *= std::size_t(2 * kmax);
1601 const std::size_t cap_boxes = std::max<std::size_t>(1, cap_bytes / box_bytes);
1602
1603 // fence, phase1, fence, phase2, fence: the middle fence is REQUIRED -- phase1
1604 // iterates the ConcurrentHashMap and needs a quiescent window (see worlddc.h)
1605 world.gop.fence();
1606 for (std::size_t j = 0; j < v.size(); ++j) {
1607 auto pmap = std::shared_ptr<WorldDCPmapInterface<Key<NDIM>>>(
1608 new WorldDCSingleOwnerPmap<Key<NDIM>>(owner[j]));
1609 v[j].get_impl()->get_coeffs().redistribute_coalesced_phase1(pmap);
1610 }
1611 world.gop.fence();
1612 for (std::size_t j = 0; j < v.size(); ++j)
1613 v[j].get_impl()->get_coeffs().redistribute_coalesced_phase2(cap_boxes, rotate);
1614 world.gop.fence();
1615 }
1616
1617 /// Returns new vector of functions --- q[i] = a[i] + b[i]
1618 template <typename T, typename R, std::size_t NDIM>
1619 std::vector< Function<TENSOR_RESULT_TYPE(T,R), NDIM> >
1620 add(World& world,
1621 const std::vector< Function<T,NDIM> >& a,
1622 const std::vector< Function<R,NDIM> >& b,
1623 bool fence=true) {
1624 PROFILE_BLOCK(Vadd);
1625 MADNESS_ASSERT(a.size() == b.size());
1626 compress(world, a);
1627 compress(world, b);
1628
1629 std::vector< Function<TENSOR_RESULT_TYPE(T,R),NDIM> > r(a.size());
1630 for (unsigned int i=0; i<a.size(); ++i) {
1631 r[i] = add(a[i], b[i], false);
1632 }
1633 if (fence) world.gop.fence();
1634 return r;
1635 }
1636
1637 /// Returns new vector of functions --- q[i] = a + b[i]
1638 template <typename T, typename R, std::size_t NDIM>
1639 std::vector< Function<TENSOR_RESULT_TYPE(T,R), NDIM> >
1640 add(World& world,
1641 const Function<T,NDIM> & a,
1642 const std::vector< Function<R,NDIM> >& b,
1643 bool fence=true) {
1644 PROFILE_BLOCK(Vadd1);
1645 a.compress();
1646 compress(world, b);
1647
1648 std::vector< Function<TENSOR_RESULT_TYPE(T,R),NDIM> > r(b.size());
1649 for (unsigned int i=0; i<b.size(); ++i) {
1650 r[i] = add(a, b[i], false);
1651 }
1652 if (fence) world.gop.fence();
1653 return r;
1654 }
1655 template <typename T, typename R, std::size_t NDIM>
1656 inline std::vector< Function<TENSOR_RESULT_TYPE(T,R), NDIM> >
1657 add(World& world,
1658 const std::vector< Function<R,NDIM> >& b,
1659 const Function<T,NDIM> & a,
1660 bool fence=true) {
1661 return add(world, a, b, fence);
1662 }
1663
1664 /// Returns new vector of functions --- q[i] = a[i] - b[i]
1665 template <typename T, typename R, std::size_t NDIM>
1666 std::vector< Function<TENSOR_RESULT_TYPE(T,R), NDIM> >
1667 sub(World& world,
1668 const std::vector< Function<T,NDIM> >& a,
1669 const std::vector< Function<R,NDIM> >& b,
1670 bool fence=true) {
1671 PROFILE_BLOCK(Vsub);
1672 MADNESS_ASSERT(a.size() == b.size());
1673 compress(world, a);
1674 compress(world, b);
1675
1676 std::vector< Function<TENSOR_RESULT_TYPE(T,R),NDIM> > r(a.size());
1677 for (unsigned int i=0; i<a.size(); ++i) {
1678 r[i] = sub(a[i], b[i], false);
1679 }
1680 if (fence) world.gop.fence();
1681 return r;
1682 }
1683
1684 /// Returns new function --- q = sum_i f[i]
1685 template <typename T, std::size_t NDIM>
1686 Function<T, NDIM> sum(World& world, const std::vector<Function<T,NDIM> >& f,
1687 bool fence=true) {
1688
1689 compress(world, f);
1691
1692 for (unsigned int i=0; i<f.size(); ++i) r.gaxpy(1.0,f[i],1.0,false);
1693 if (fence) world.gop.fence();
1694 return r;
1695 }
1696
1697 template <typename T, std::size_t NDIM>
1699 const std::vector<Function<T, NDIM>>& f,
1700 const std::vector<Function<T, NDIM>>& g,
1701 bool sym=false)
1702 {
1705 const int64_t n = A.coldim();
1706 const int64_t m = A.rowdim();
1707 MADNESS_ASSERT(int64_t(f.size()) == n && int64_t(g.size()) == m);
1708
1709 // Assume we can always create an ichunk*jchunk matrix locally
1710 const int ichunk = 1000;
1711 const int jchunk = 1000; // 1000*1000*8 = 8 MBytes
1712 for (int64_t ilo = 0; ilo < n; ilo += ichunk) {
1713 int64_t ihi = std::min(ilo + ichunk, n);
1714 std::vector<Function<T, NDIM>> ivec(f.begin() + ilo, f.begin() + ihi);
1715 for (int64_t jlo = 0; jlo < m; jlo += jchunk) {
1716 int64_t jhi = std::min(jlo + jchunk, m);
1717 std::vector<Function<T, NDIM>> jvec(g.begin() + jlo, g.begin() + jhi);
1718
1719 Tensor<T> P = matrix_dot(A.get_world(), ivec, jvec, sym);
1720 A.copy_from_replicated_patch(ilo, ihi - 1, jlo, jhi - 1, P);
1721 }
1722 }
1723 return A;
1724 }
1725
1726 /// Computes the matrix dot product of two function vectors - q(i,j) = dot(f[i],g[j])
1727
1728 /// For complex types symmetric is interpreted as Hermitian.
1729 ///
1730 /// The current parallel loop is non-optimal but functional.
1731 template <typename T, typename R, std::size_t NDIM>
1733 const std::vector<Function<T, NDIM>>& f,
1734 const std::vector<Function<R, NDIM>>& g,
1735 bool sym=false)
1736 {
1737 world.gop.fence();
1738 compress(world, f);
1739 // if ((void*)(&f) != (void*)(&g)) compress(world, g);
1740 compress(world, g);
1741
1742 std::vector<const FunctionImpl<T, NDIM>*> left(f.size());
1743 std::vector<const FunctionImpl<R, NDIM>*> right(g.size());
1744 for (unsigned int i = 0; i < f.size(); i++) left[i] = f[i].get_impl().get();
1745 for (unsigned int i = 0; i < g.size(); i++) right[i] = g[i].get_impl().get();
1746
1748
1749 world.gop.fence();
1750 world.gop.sum(r.ptr(), f.size() * g.size());
1751
1752 return r;
1753 }
1754
1755 /// Computes the matrix dot product of two function vectors - q(i,j) = dot(f[i],g[j])
1756
1757 /// For complex types symmetric is interpreted as Hermitian.
1758 ///
1759 /// The current parallel loop is non-optimal but functional.
1760 template <typename T, typename R, std::size_t NDIM>
1762 const std::vector< Function<T,NDIM> >& f,
1763 const std::vector< Function<R,NDIM> >& g,
1764 bool sym=false) {
1765 PROFILE_BLOCK(Vmatrix_dot);
1766 long n=f.size(), m=g.size();
1767 Tensor< TENSOR_RESULT_TYPE(T,R) > r(n,m);
1768 if (sym) MADNESS_ASSERT(n==m);
1769
1770 world.gop.fence();
1771 compress(world, f);
1772 if ((void*)(&f) != (void*)(&g)) compress(world, g);
1773
1774 for (long i=0; i<n; ++i) {
1775 long jtop = m;
1776 if (sym) jtop = i+1;
1777 for (long j=0; j<jtop; ++j) {
1778 if (sym) {
1779 r(j,i) = f[i].dot_local(g[j]);
1780 if (i != j)
1781 r(i,j) = conj(r(j,i));
1782 } else
1783 r(i,j) = f[i].dot_local(g[j]);
1784 }
1785 }
1786
1787 world.gop.fence();
1788 world.gop.sum(r.ptr(),n*m);
1789
1790 return r;
1791 }
1792
1793 /// Multiplies and sums two vectors of functions r = \sum_i a[i] * b[i]
1794 template <typename T, typename R, std::size_t NDIM>
1795 Function<TENSOR_RESULT_TYPE(T,R), NDIM>
1797 const std::vector< Function<T,NDIM> >& a,
1798 const std::vector< Function<R,NDIM> >& b,
1799 double tol,
1800 bool fence=true,
1801 bool do_make_redundant=true) {
1802 MADNESS_CHECK(a.size()==b.size());
1803 return sum(world,mul_sparse(world,a,b,tol,/*fence=*/true,do_make_redundant),fence);
1804 }
1805
1806 /// Multiplies and sums two vectors of functions r = \sum_i a[i] * b[i]; see dot_sparse for screening
1807 template <typename T, typename R, std::size_t NDIM>
1808 Function<TENSOR_RESULT_TYPE(T,R), NDIM>
1809 dot(World& world,
1810 const std::vector< Function<T,NDIM> >& a,
1811 const std::vector< Function<R,NDIM> >& b,
1812 bool fence=true,
1813 bool do_make_redundant=true) {
1814 MADNESS_CHECK(a.size()==b.size());
1815 return sum(world,mul(world,a,b,/*fence=*/true,do_make_redundant),fence);
1816 }
1817
1818 /// out-of-place gaxpy for two vectors: result[i] = alpha * a[i] + beta * b[i]
1819 template <typename T, typename Q, typename R, std::size_t NDIM>
1820 std::vector<Function<TENSOR_RESULT_TYPE(Q,TENSOR_RESULT_TYPE(T,R)),NDIM> >
1822 const std::vector< Function<T,NDIM> >& a,
1823 Q beta,
1824 const std::vector< Function<R,NDIM> >& b,
1825 bool fence=true) {
1826
1827 MADNESS_ASSERT(a.size() == b.size());
1828 typedef TENSOR_RESULT_TYPE(Q,TENSOR_RESULT_TYPE(T,R)) resultT;
1829 if (a.size()==0) return std::vector<Function<resultT,NDIM> >();
1830
1831 auto tensor_type = [](const std::vector<Function<T,NDIM>>& v) {
1832 return v.front().get_impl()->get_tensor_type();
1833 };
1834
1835 // gaxpy can be done either in reconstructed or in compressed state
1836 World& world=a[0].world();
1837 std::vector<Function<resultT,NDIM> > result(a.size());
1838
1839 TreeState operating_state=tensor_type(a)==TT_FULL ? compressed : reconstructed;
1840 try {
1841 ensure_tree_state_respecting_fence(a,operating_state,fence);
1842 ensure_tree_state_respecting_fence(b,operating_state,fence);
1843 } catch (...) {
1844 print("could not respect fence in gaxpy");
1845 change_tree_state(a,operating_state,true);
1846 change_tree_state(b,operating_state,true);
1847 }
1848
1849 if (operating_state==compressed) {
1850 for (unsigned int i=0; i<a.size(); ++i) result[i]=gaxpy_oop(alpha, a[i], beta, b[i], false);
1851 } else {
1852 for (unsigned int i=0; i<a.size(); ++i) result[i]=gaxpy_oop_reconstructed(alpha, a[i], beta, b[i], false);
1853 }
1854
1855 if (fence) world.gop.fence();
1856 return result;
1857 }
1858
1859
1860 /// out-of-place gaxpy for a vectors and a function: result[i] = alpha * a[i] + beta * b
1861 template <typename T, typename Q, typename R, std::size_t NDIM>
1862 std::vector<Function<TENSOR_RESULT_TYPE(Q,TENSOR_RESULT_TYPE(T,R)),NDIM> >
1864 const std::vector< Function<T,NDIM> >& a,
1865 Q beta,
1866 const Function<R,NDIM>& b,
1867 bool fence=true) {
1868
1869 typedef TENSOR_RESULT_TYPE(Q,TENSOR_RESULT_TYPE(T,R)) resultT;
1870 if (a.size()==0) return std::vector<Function<resultT,NDIM> >();
1871
1872 World& world=a[0].world();
1873 try {
1875 // ensure_tree_state_respecting_fence({b},compressed,fence);
1877 } catch (...) {
1878 print("could not respect fence in gaxpy_oop");
1879 compress(world,a);
1880 b.compress();
1881 }
1882 std::vector<Function<resultT,NDIM> > result(a.size());
1883 for (unsigned int i=0; i<a.size(); ++i) {
1884 result[i]=gaxpy_oop(alpha, a[i], beta, b, false);
1885 }
1886 if (fence) world.gop.fence();
1887 return result;
1888 }
1889
1890
1891 /// Generalized A*X+Y for vectors of functions ---- a[i] = alpha*a[i] + beta*b[i]
1892 template <typename T, typename Q, typename R, std::size_t NDIM>
1893 void gaxpy(Q alpha, std::vector<Function<T,NDIM>>& a, Q beta, const std::vector<Function<R,NDIM>>& b, const bool fence) {
1894 if (a.size() == 0) return;
1895 World& world=a.front().world();
1896 gaxpy(world,alpha,a,beta,b,fence);
1897 }
1898
1899 /// Generalized A*X+Y for vectors of functions ---- a[i] = alpha*a[i] + beta*b[i]
1900 template <typename T, typename Q, typename R, std::size_t NDIM>
1901 void gaxpy(World& world,
1902 Q alpha,
1903 std::vector< Function<T,NDIM> >& a,
1904 Q beta,
1905 const std::vector< Function<R,NDIM> >& b,
1906 bool fence=true) {
1907 PROFILE_BLOCK(Vgaxpy);
1908 MADNESS_ASSERT(a.size() == b.size());
1909 if (a.empty()) return;
1910
1911 auto tensor_type = [](const std::vector<Function<T,NDIM>>& v) {
1912 return v.front().get_impl()->get_tensor_type();
1913 };
1914
1915 // gaxpy can be done either in reconstructed or in compressed state
1916 bool do_in_reconstructed_state=tensor_type(a)!=TT_FULL;
1917 TreeState operating_state=do_in_reconstructed_state ? reconstructed : compressed;
1918
1919 if (operating_state==compressed) {
1920 // this is strict: both vectors have to be compressed
1921 try {
1922 ensure_tree_state_respecting_fence(a,operating_state,fence);
1923 ensure_tree_state_respecting_fence(b,operating_state,fence);
1924 } catch (...) {
1925 print("could not respect fence in gaxpy");
1926 change_tree_state(a,operating_state,true);
1927 change_tree_state(b,operating_state,true);
1928 }
1929 MADNESS_CHECK_THROW(get_tree_state(a)==get_tree_state(b),"gaxpy requires same tree state for all functions");
1930 MADNESS_CHECK_THROW(get_tree_state(a)==operating_state,"gaxpy requires reconstructed/compressed tree state for all functions");
1931 } else {
1932 // both vectors can be reconstructed or redundant_after_merge, and they don't have to be the same
1933 TreeState astate=get_tree_state(a);
1934 TreeState bstate=get_tree_state(b);
1935 if (not (astate==reconstructed or astate==redundant_after_merge)) {
1936 try {
1937 ensure_tree_state_respecting_fence(a,operating_state,fence);
1938 } catch (...) {
1939 print("could not respect fence in gaxpy for a");
1941 }
1942 }
1943 if (not (bstate==reconstructed or bstate==redundant_after_merge)) {
1944 try {
1945 ensure_tree_state_respecting_fence(b,operating_state,fence);
1946 } catch (...) {
1947 print("could not respect fence in gaxpy for b");
1949 }
1950 }
1951 }
1952
1953 // finally do the work
1954 for (unsigned int i=0; i<a.size(); ++i) {
1955 a[i].gaxpy(alpha, b[i], beta, false);
1956 }
1957 if (fence and (get_tree_state(a)==redundant_after_merge)) {
1958 for (unsigned int i=0; i<a.size(); ++i) a[i].get_impl()->finalize_sum();
1959 }
1960
1961 if (fence) world.gop.fence();
1962 }
1963
1964
1965 /// Applies a vector of operators to a vector of functions --- q[i] = apply(op[i],f[i])
1966 template <typename opT, typename R, std::size_t NDIM>
1967 std::vector< Function<TENSOR_RESULT_TYPE(typename opT::opT,R), NDIM> >
1968 apply(World& world,
1969 const std::vector< std::shared_ptr<opT> >& op,
1970 const std::vector< Function<R,NDIM> > f) {
1971
1972 PROFILE_BLOCK(Vapplyv);
1973 MADNESS_ASSERT(f.size()==op.size());
1974
1975 std::vector< Function<R,NDIM> >& ncf = *const_cast< std::vector< Function<R,NDIM> >* >(&f);
1976
1977// reconstruct(world, f);
1978 make_nonstandard(world, ncf);
1979
1980 std::vector< Function<TENSOR_RESULT_TYPE(typename opT::opT,R), NDIM> > result(f.size());
1981 for (unsigned int i=0; i<f.size(); ++i) {
1982 result[i] = apply_only(*op[i], f[i], false);
1983 result[i].get_impl()->set_tree_state(nonstandard_after_apply);
1984 }
1985
1986 world.gop.fence();
1987
1988 standard(world, ncf, false); // restores promise of logical constness
1989 reconstruct(result);
1990 world.gop.fence();
1991
1992 return result;
1993 }
1994
1995
1996 /// Applies an operator to a vector of functions --- q[i] = apply(op,f[i])
1997 template <typename T, typename R, std::size_t NDIM, std::size_t KDIM>
1998 std::vector< Function<TENSOR_RESULT_TYPE(T,R), NDIM> >
2000 const std::vector< Function<R,NDIM> > f) {
2001 return apply(op.get_world(),op,f);
2002 }
2003
2004
2005 /// Applies an operator to a vector of functions --- q[i] = apply(op,f[i])
2006 template <typename T, typename R, std::size_t NDIM, std::size_t KDIM>
2007 std::vector< Function<TENSOR_RESULT_TYPE(T,R), NDIM> >
2008 apply(World& world,
2010 const std::vector< Function<R,NDIM> > f) {
2011 PROFILE_BLOCK(Vapply);
2012
2013 std::vector< Function<R,NDIM> >& ncf = *const_cast< std::vector< Function<R,NDIM> >* >(&f);
2014 bool print_timings=(NDIM==6) and (world.rank()==0) and op.print_timings;
2015
2016 double wall0=wall_time();
2017// reconstruct(world, f);
2018 make_nonstandard(world, ncf);
2019 double wall1=wall_time();
2020 if (print_timings) printf("timer: %20.20s %8.2fs\n", "make_nonstandard", wall1-wall0);
2021
2022 std::vector< Function<TENSOR_RESULT_TYPE(T,R), NDIM> > result(f.size());
2023 for (unsigned int i=0; i<f.size(); ++i) {
2024 result[i] = apply_only(op, f[i], false);
2025 }
2026
2027 world.gop.fence();
2028
2029 // restores promise of logical constness
2030 if (op.destructive()) {
2031 for (auto& ff : ncf) ff.clear(false);
2032 world.gop.fence();
2033 } else {
2034 reconstruct(world,f);
2035 }
2036
2037 // svd-tensor requires some cleanup after apply
2038 if (result[0].get_impl()->get_tensor_type()==TT_2D) {
2039 for (auto& r : result) r.get_impl()->finalize_apply();
2040 }
2041
2042 if (print_timings) {
2043 for (auto& r : result) r.get_impl()->print_timer();
2044 op.print_timer();
2045 }
2046 reconstruct(world, result);
2047
2048 return result;
2049 }
2050
2051 /// Normalizes a vector of functions --- v[i] = v[i].scale(1.0/v[i].norm2())
2052 template <typename T, std::size_t NDIM>
2053 void normalize(World& world, std::vector< Function<T,NDIM> >& v, bool fence=true) {
2054 PROFILE_BLOCK(Vnormalize);
2055 std::vector<double> nn = norm2s(world, v);
2056 for (unsigned int i=0; i<v.size(); ++i) v[i].scale(1.0/nn[i],false);
2057 if (fence) world.gop.fence();
2058 }
2059
2060 template <typename T, std::size_t NDIM>
2061 void print_size(World &world, const std::vector<Function<T,NDIM> > &v, const std::string &msg = "vectorfunction" ){
2062 if(v.empty()){
2063 if(world.rank()==0) std::cout << "print_size: " << msg << " is empty" << std::endl;
2064 }else if(v.size()==1){
2065 v.front().print_size(msg);
2066 }else{
2067 for(auto x:v){
2068 // print("impl",x.get_impl().get());
2069 x.print_size(msg);
2070 }
2071 }
2072 }
2073
2074 /// return the size of a vector of functions for each rank
2075 template <typename T, std::size_t NDIM>
2076 double get_size_local(World& world, const std::vector< Function<T,NDIM> >& v){
2077 double size=0.0;
2078 for(auto x:v){
2079 if (x.is_initialized()) size+=x.size_local();
2080 }
2081 const double d=sizeof(T);
2082 const double fac=1024*1024*1024;
2083 return size/fac*d;
2084 }
2085
2086 /// return the size of a function for each rank
2087 template <typename T, std::size_t NDIM>
2089 return get_size_local(f.world(),std::vector<Function<T,NDIM> >(1,f));
2090 }
2091
2092
2093 // gives back the size in GB
2094 template <typename T, std::size_t NDIM>
2095 double get_size(World& world, const std::vector< Function<T,NDIM> >& v){
2096
2097 if (v.empty()) return 0.0;
2098
2099 const double d=sizeof(T);
2100 const double fac=1024*1024*1024;
2101
2102 double size=0.0;
2103 for(unsigned int i=0;i<v.size();i++){
2104 if (v[i].is_initialized()) size+=v[i].size();
2105 }
2106
2107 return size/fac*d;
2108
2109 }
2110
2111 // gives back the size in GB
2112 template <typename T, std::size_t NDIM>
2113 double get_size(const Function<T,NDIM> & f){
2114 const double d=sizeof(T);
2115 const double fac=1024*1024*1024;
2116 double size=f.size();
2117 return size/fac*d;
2118 }
2119
2120 /// apply op on the input vector yielding an output vector of functions
2121
2122 /// @param[in] op the operator working on vin
2123 /// @param[in] vin vector of input Functions; needs to be refined to common level!
2124 /// @return vector of output Functions vout = op(vin)
2125 template <typename T, typename opT, std::size_t NDIM>
2126 std::vector<Function<T,NDIM> > multi_to_multi_op_values(const opT& op,
2127 const std::vector< Function<T,NDIM> >& vin,
2128 const bool fence=true) {
2129 MADNESS_ASSERT(vin.size()>0);
2130 MADNESS_ASSERT(vin[0].is_initialized()); // might be changed
2131 World& world=vin[0].world();
2132 Function<T,NDIM> dummy;
2133 dummy.set_impl(vin[0], false);
2134 std::vector<Function<T,NDIM> > vout=zero_functions<T,NDIM>(world, op.get_result_size());
2135 for (auto& out : vout) out.set_impl(vin[0],false);
2136 dummy.multi_to_multi_op_values(op, vin, vout, fence);
2137 return vout;
2138 }
2139
2140
2141
2142
2143 // convenience operators
2144
2145 /// result[i] = a[i] + b[i]
2146 template <typename T, std::size_t NDIM>
2147 std::vector<Function<T,NDIM> > operator+(const std::vector<Function<T,NDIM> >& lhs,
2148 const std::vector<Function<T,NDIM>>& rhs) {
2149 MADNESS_CHECK(lhs.size() == rhs.size());
2150 return gaxpy_oop(1.0,lhs,1.0,rhs);
2151 }
2152
2153 /// result[i] = a[i] - b[i]
2154 template <typename T, std::size_t NDIM>
2155 std::vector<Function<T,NDIM> > operator-(const std::vector<Function<T,NDIM> >& lhs,
2156 const std::vector<Function<T,NDIM> >& rhs) {
2157 MADNESS_CHECK(lhs.size() == rhs.size());
2158 return gaxpy_oop(1.0,lhs,-1.0,rhs);
2159 }
2160
2161 /// result[i] = a[i] + b
2162 template <typename T, std::size_t NDIM>
2163 std::vector<Function<T,NDIM> > operator+(const std::vector<Function<T,NDIM> >& lhs,
2164 const Function<T,NDIM>& rhs) {
2165 // MADNESS_CHECK(lhs.size() == rhs.size()); // no!!
2166 return gaxpy_oop(1.0,lhs,1.0,rhs);
2167 }
2168
2169 /// result[i] = a[i] - b
2170 template <typename T, std::size_t NDIM>
2171 std::vector<Function<T,NDIM> > operator-(const std::vector<Function<T,NDIM> >& lhs,
2172 const Function<T,NDIM>& rhs) {
2173 // MADNESS_CHECK(lhs.size() == rhs.size()); // no
2174 return gaxpy_oop(1.0,lhs,-1.0,rhs);
2175 }
2176
2177 /// result[i] = a + b[i]
2178 template <typename T, std::size_t NDIM>
2179 std::vector<Function<T,NDIM> > operator+(const Function<T,NDIM>& lhs,
2180 const std::vector<Function<T,NDIM> >& rhs) {
2181 // MADNESS_CHECK(lhs.size() == rhs.size()); // no
2182 return gaxpy_oop(1.0,rhs,1.0,lhs);
2183 }
2184
2185 /// result[i] = a - b[i]
2186 template <typename T, std::size_t NDIM>
2187 std::vector<Function<T,NDIM> > operator-(const Function<T,NDIM>& lhs,
2188 const std::vector<Function<T,NDIM> >& rhs) {
2189// MADNESS_CHECK(lhs.size() == rhs.size()); // no
2190 return gaxpy_oop(-1.0,rhs,1.0,lhs);
2191 }
2192
2193
2194 template <typename T, typename R, std::size_t NDIM>
2195 std::vector<Function<TENSOR_RESULT_TYPE(T,R),NDIM> > operator*(const R fac,
2196 const std::vector<Function<T,NDIM> >& rhs) {
2197 if (rhs.size()>0) {
2198 std::vector<Function<T,NDIM> > tmp=copy(rhs[0].world(),rhs);
2199 scale(tmp[0].world(),tmp,TENSOR_RESULT_TYPE(T,R)(fac));
2200 return tmp;
2201 }
2202 return std::vector<Function<TENSOR_RESULT_TYPE(T,R),NDIM> >();
2203 }
2204
2205 template <typename T, typename R, std::size_t NDIM>
2206 std::vector<Function<T,NDIM> > operator*(const std::vector<Function<T,NDIM> >& rhs,
2207 const R fac) {
2208 if (rhs.size()>0) {
2209 std::vector<Function<TENSOR_RESULT_TYPE(T,R),NDIM> > tmp=copy(rhs[0].world(),rhs);
2210 scale(tmp[0].world(),tmp,TENSOR_RESULT_TYPE(T,R)(fac));
2211 return tmp;
2212 }
2213 return std::vector<Function<TENSOR_RESULT_TYPE(T,R),NDIM> >();
2214 }
2215
2216 /// multiply a vector of functions with a function: r[i] = v[i] * a
2217 template <typename T, typename R, std::size_t NDIM>
2219 const std::vector<Function<R,NDIM> >& v) {
2220 if (v.size()>0) return mul(v[0].world(),a,v,true);
2221 return std::vector<Function<TENSOR_RESULT_TYPE(T,R),NDIM> >();
2222 }
2223
2224
2225 /// multiply a vector of functions with a function: r[i] = a * v[i]
2226 template <typename T, typename R, std::size_t NDIM>
2227 std::vector<Function<TENSOR_RESULT_TYPE(T,R),NDIM> > operator*(const std::vector<Function<T,NDIM> >& v,
2228 const Function<R,NDIM>& a) {
2229 if (v.size()>0) return mul(v[0].world(),a,v,true);
2230 return std::vector<Function<TENSOR_RESULT_TYPE(T,R),NDIM> >();
2231 }
2232
2233
2234 template <typename T, std::size_t NDIM>
2235 std::vector<Function<T,NDIM> > operator+=(std::vector<Function<T,NDIM> >& lhs, const std::vector<Function<T,NDIM> >& rhs) {
2236 MADNESS_CHECK(lhs.size() == rhs.size());
2237 if (lhs.size() > 0) gaxpy(lhs.front().world(), 1.0, lhs, 1.0, rhs);
2238 return lhs;
2239 }
2240
2241 template <typename T, std::size_t NDIM>
2242 std::vector<Function<T,NDIM> > operator-=(std::vector<Function<T,NDIM> >& lhs,
2243 const std::vector<Function<T,NDIM> >& rhs) {
2244 MADNESS_CHECK(lhs.size() == rhs.size());
2245 if (lhs.size() > 0) gaxpy(lhs.front().world(), 1.0, lhs, -1.0, rhs);
2246 return lhs;
2247 }
2248
2249 /// return the real parts of the vector's function (if complex)
2250 template <typename T, std::size_t NDIM>
2251 std::vector<Function<typename Tensor<T>::scalar_type,NDIM> >
2252 real(const std::vector<Function<T,NDIM> >& v, bool fence=true) {
2253 std::vector<Function<typename Tensor<T>::scalar_type,NDIM> > result(v.size());
2254 for (std::size_t i=0; i<v.size(); ++i) result[i]=real(v[i],false);
2255 if (fence and result.size()>0) result[0].world().gop.fence();
2256 return result;
2257 }
2258
2259 /// return the imaginary parts of the vector's function (if complex)
2260 template <typename T, std::size_t NDIM>
2261 std::vector<Function<typename Tensor<T>::scalar_type,NDIM> >
2262 imag(const std::vector<Function<T,NDIM> >& v, bool fence=true) {
2263 std::vector<Function<typename Tensor<T>::scalar_type,NDIM> > result(v.size());
2264 for (std::size_t i=0; i<v.size(); ++i) result[i]=imag(v[i],false);
2265 if (fence and result.size()>0) result[0].world().gop.fence();
2266 return result;
2267 }
2268
2269 /// shorthand gradient operator
2270
2271 /// returns the differentiated function f in all NDIM directions
2272 /// @param[in] f the function on which the grad operator works on
2273 /// @param[in] refine refinement before diff'ing makes the result more accurate
2274 /// @param[in] fence fence after completion; if reconstruction is needed always fence
2275 /// @return the vector \frac{\partial}{\partial x_i} f
2276 template <typename T, std::size_t NDIM>
2277 std::vector<Function<T,NDIM> > grad(const Function<T,NDIM>& f,
2278 bool refine=false, bool fence=true) {
2279
2280 World& world=f.world();
2281 f.reconstruct();
2282 if (refine) f.refine(); // refine to make result more precise
2283
2284 std::vector< std::shared_ptr< Derivative<T,NDIM> > > grad=
2285 gradient_operator<T,NDIM>(world);
2286
2287 std::vector<Function<T,NDIM> > result(NDIM);
2288 for (size_t i=0; i<NDIM; ++i) result[i]=apply(*(grad[i]),f,false);
2289 if (fence) world.gop.fence();
2290 return result;
2291 }
2292
2293 // BLM first derivative
2294 template <typename T, std::size_t NDIM>
2295 std::vector<Function<T,NDIM> > grad_ble_one(const Function<T,NDIM>& f,
2296 bool refine=false, bool fence=true) {
2297
2298 World& world=f.world();
2299 f.reconstruct();
2300 if (refine) f.refine(); // refine to make result more precise
2301
2302 std::vector< std::shared_ptr< Derivative<T,NDIM> > > grad=
2303 gradient_operator<T,NDIM>(world);
2304
2305 // Read in new coeff for each operator
2306 for (unsigned int i=0; i<NDIM; ++i) (*grad[i]).set_ble1();
2307
2308 std::vector<Function<T,NDIM> > result(NDIM);
2309 for (unsigned int i=0; i<NDIM; ++i) result[i]=apply(*(grad[i]),f,false);
2310 if (fence) world.gop.fence();
2311 return result;
2312 }
2313
2314 // BLM second derivative
2315 template <typename T, std::size_t NDIM>
2316 std::vector<Function<T,NDIM> > grad_ble_two(const Function<T,NDIM>& f,
2317 bool refine=false, bool fence=true) {
2318
2319 World& world=f.world();
2320 f.reconstruct();
2321 if (refine) f.refine(); // refine to make result more precise
2322
2323 std::vector< std::shared_ptr< Derivative<T,NDIM> > > grad=
2324 gradient_operator<T,NDIM>(world);
2325
2326 // Read in new coeff for each operator
2327 for (unsigned int i=0; i<NDIM; ++i) (*grad[i]).set_ble2();
2328
2329 std::vector<Function<T,NDIM> > result(NDIM);
2330 for (unsigned int i=0; i<NDIM; ++i) result[i]=apply(*(grad[i]),f,false);
2331 if (fence) world.gop.fence();
2332 return result;
2333 }
2334
2335 // Bspline first derivative
2336 template <typename T, std::size_t NDIM>
2337 std::vector<Function<T,NDIM> > grad_bspline_one(const Function<T,NDIM>& f,
2338 bool refine=false, bool fence=true) {
2339
2340 World& world=f.world();
2341 f.reconstruct();
2342 if (refine) f.refine(); // refine to make result more precise
2343
2344 std::vector< std::shared_ptr< Derivative<T,NDIM> > > grad=
2345 gradient_operator<T,NDIM>(world);
2346
2347 // Read in new coeff for each operator
2348 for (unsigned int i=0; i<NDIM; ++i) (*grad[i]).set_bspline1();
2349
2350 std::vector<Function<T,NDIM> > result(NDIM);
2351 for (unsigned int i=0; i<NDIM; ++i) result[i]=apply(*(grad[i]),f,false);
2352 if (fence) world.gop.fence();
2353 return result;
2354 }
2355
2356 // Bpsline second derivative
2357 template <typename T, std::size_t NDIM>
2358 std::vector<Function<T,NDIM> > grad_bpsline_two(const Function<T,NDIM>& f,
2359 bool refine=false, bool fence=true) {
2360
2361 World& world=f.world();
2362 f.reconstruct();
2363 if (refine) f.refine(); // refine to make result more precise
2364
2365 std::vector< std::shared_ptr< Derivative<T,NDIM> > > grad=
2366 gradient_operator<T,NDIM>(world);
2367
2368 // Read in new coeff for each operator
2369 for (unsigned int i=0; i<NDIM; ++i) (*grad[i]).set_bspline2();
2370
2371 std::vector<Function<T,NDIM> > result(NDIM);
2372 for (unsigned int i=0; i<NDIM; ++i) result[i]=apply(*(grad[i]),f,false);
2373 if (fence) world.gop.fence();
2374 return result;
2375 }
2376
2377 // Bspline third derivative
2378 template <typename T, std::size_t NDIM>
2379 std::vector<Function<T,NDIM> > grad_bspline_three(const Function<T,NDIM>& f,
2380 bool refine=false, bool fence=true) {
2381
2382 World& world=f.world();
2383 f.reconstruct();
2384 if (refine) f.refine(); // refine to make result more precise
2385
2386 std::vector< std::shared_ptr< Derivative<T,NDIM> > > grad=
2387 gradient_operator<T,NDIM>(world);
2388
2389 // Read in new coeff for each operator
2390 for (unsigned int i=0; i<NDIM; ++i) (*grad[i]).set_bspline3();
2391
2392 std::vector<Function<T,NDIM> > result(NDIM);
2393 for (unsigned int i=0; i<NDIM; ++i) result[i]=apply(*(grad[i]),f,false);
2394 if (fence) world.gop.fence();
2395 return result;
2396 }
2397
2398
2399
2400 /// shorthand div operator
2401
2402 /// returns the dot product of nabla with a vector f
2403 /// @param[in] f the vector of functions on which the div operator works on
2404 /// @param[in] refine refinement before diff'ing makes the result more accurate
2405 /// @param[in] fence fence after completion; currently always fences
2406 /// @return the vector \frac{\partial}{\partial x_i} f
2407 /// TODO: add this to operator fusion
2408 template <typename T, std::size_t NDIM>
2410 bool do_refine=false, bool fence=true) {
2411
2412 MADNESS_ASSERT(v.size()>0);
2413 World& world=v[0].world();
2414 reconstruct(world,v);
2415 if (do_refine) refine(world,v); // refine to make result more precise
2416
2417 std::vector< std::shared_ptr< Derivative<T,NDIM> > > grad=
2418 gradient_operator<T,NDIM>(world);
2419
2420 std::vector<Function<T,NDIM> > result(NDIM);
2421 for (size_t i=0; i<NDIM; ++i) result[i]=apply(*(grad[i]),v[i],false);
2422 world.gop.fence();
2423 return sum(world,result,fence);
2424 }
2425
2426 /// shorthand rot operator
2427
2428 /// returns the cross product of nabla with a vector f
2429 /// @param[in] f the vector of functions on which the rot operator works on
2430 /// @param[in] refine refinement before diff'ing makes the result more accurate
2431 /// @param[in] fence fence after completion; currently always fences
2432 /// @return the vector \frac{\partial}{\partial x_i} f
2433 /// TODO: add this to operator fusion
2434 template <typename T, std::size_t NDIM>
2435 std::vector<Function<T,NDIM> > rot(const std::vector<Function<T,NDIM> >& v,
2436 bool do_refine=false, bool fence=true) {
2437
2438 MADNESS_ASSERT(v.size()==3);
2439 World& world=v[0].world();
2440 reconstruct(world,v);
2441 if (do_refine) refine(world,v); // refine to make result more precise
2442
2443 std::vector< std::shared_ptr< Derivative<T,NDIM> > > grad=
2444 gradient_operator<T,NDIM>(world);
2445
2446 std::vector<Function<T,NDIM> > d(NDIM),dd(NDIM);
2447 d[0]=apply(*(grad[1]),v[2],false); // Dy z
2448 d[1]=apply(*(grad[2]),v[0],false); // Dz x
2449 d[2]=apply(*(grad[0]),v[1],false); // Dx y
2450 dd[0]=apply(*(grad[2]),v[1],false); // Dz y
2451 dd[1]=apply(*(grad[0]),v[2],false); // Dx z
2452 dd[2]=apply(*(grad[1]),v[0],false); // Dy x
2453 world.gop.fence();
2454
2455 compress(world,d,false);
2456 compress(world,dd,false);
2457 world.gop.fence();
2458 d[0].gaxpy(1.0,dd[0],-1.0,false);
2459 d[1].gaxpy(1.0,dd[1],-1.0,false);
2460 d[2].gaxpy(1.0,dd[2],-1.0,false);
2461
2462 world.gop.fence();
2463 reconstruct(d);
2464 return d;
2465 }
2466
2467 /// shorthand cross operator
2468
2469 /// returns the cross product of vectors f and g
2470 /// @param[in] f the vector of functions on which the rot operator works on
2471 /// @param[in] g the vector of functions on which the rot operator works on
2472 /// @param[in] fence fence after completion; currently always fences
2473 /// @return the vector \frac{\partial}{\partial x_i} f, in redundant state
2474 /// TODO: add this to operator fusion
2475 template <typename T, typename R, std::size_t NDIM>
2476 std::vector<Function<TENSOR_RESULT_TYPE(T,R),NDIM> > cross(const std::vector<Function<T,NDIM> >& f,
2477 const std::vector<Function<R,NDIM> >& g,
2478 bool do_refine=false, bool fence=true) {
2479
2480 MADNESS_ASSERT(f.size()==3);
2481 MADNESS_ASSERT(g.size()==3);
2482 World& world=f[0].world();
2485
2486 std::vector<Function<TENSOR_RESULT_TYPE(T,R),NDIM> > d(f.size()),dd(f.size());
2487
2488 d[0]=mul(f[1],g[2],false);
2489 d[1]=mul(f[2],g[0],false);
2490 d[2]=mul(f[0],g[1],false);
2491
2492 dd[0]=mul(f[2],g[1],false);
2493 dd[1]=mul(f[0],g[2],false);
2494 dd[2]=mul(f[1],g[0],false);
2495 world.gop.fence();
2496
2497 compress(world,d,false);
2498 compress(world,dd,false);
2499 world.gop.fence();
2500
2501 d[0].gaxpy(1.0,dd[0],-1.0,false);
2502 d[1].gaxpy(1.0,dd[1],-1.0,false);
2503 d[2].gaxpy(1.0,dd[2],-1.0,false);
2504 world.gop.fence();
2505
2506 make_redundant(world, d);
2507 return d;
2508 }
2509
2510 template<typename T, std::size_t NDIM>
2511 void load_balance(World& world, std::vector<Function<T,NDIM> >& vf) {
2512
2513 struct LBCost {
2514 LBCost() = default;
2515 double operator()(const Key<NDIM>& key, const FunctionNode<T,NDIM>& node) const {
2516 return node.coeff().size();
2517 }
2518 };
2519
2520 LoadBalanceDeux<6> lb(world);
2521 for (const auto& f : vf) lb.add_tree(f, LBCost());
2523
2524 }
2525
2526 /// load a vector of functions
2527 template<typename T, size_t NDIM>
2528 void load_function(World& world, std::vector<Function<T,NDIM> >& f,
2529 const std::string name) {
2530 if (world.rank()==0) print("loading vector of functions",name);
2532 std::size_t fsize=0;
2533 ar & fsize;
2534 f.resize(fsize);
2535 for (std::size_t i=0; i<fsize; ++i) ar & f[i];
2536 }
2537
2538 /// save a vector of functions
2539 template<typename T, size_t NDIM>
2540 void save_function(const std::vector<Function<T,NDIM> >& f, const std::string name) {
2541 if (f.size()>0) {
2542 World& world=f.front().world();
2543 if (world.rank()==0) print("saving vector of functions",name);
2545 std::size_t fsize=f.size();
2546 ar & fsize;
2547 for (std::size_t i=0; i<fsize; ++i) ar & f[i];
2548 }
2549 }
2550
2551
2552}
2553#endif // MADNESS_MRA_VMRA_H__INCLUDED
double q(double t)
Definition DKops.h:18
Definition test_ar.cc:118
long size() const
Returns the number of elements in the tensor.
Definition basetensor.h:138
Implements derivatives operators with variety of boundary conditions on simulation domain.
Definition derivative.h:329
Definition distributed_matrix.h:68
Manages data associated with a row/column/block distributed array.
Definition distributed_matrix.h:388
static void redistribute(World &world, const std::shared_ptr< WorldDCPmapInterface< Key< NDIM > > > &newpmap)
Sets the default process map and redistributes all functions using the old map.
Definition funcdefaults.h:442
static TensorType get_tensor_type()
Returns the default tensor type.
Definition funcdefaults.h:323
FunctionFactory implements the named-parameter idiom for Function.
Definition function_factory.h:86
FunctionFactory & compressed(bool value=true)
Definition function_factory.h:168
FunctionImpl holds all Function state to facilitate shallow copy semantics.
Definition funcimpl.h:970
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:6242
void undo_redundant(const bool fence)
convert this from redundant to standard reconstructed form
Definition mraimpl.h:1559
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:3798
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:1421
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:6294
FunctionNode holds the coefficients, etc., at each node of the 2^NDIM-tree.
Definition funcimpl.h:136
coeffT & coeff()
Returns a non-const reference to the tensor containing the coeffs.
Definition funcimpl.h:237
A multiresolution adaptive numerical function.
Definition mra.h:144
World & world() const
Returns the world.
Definition mra.h:758
Function< T, NDIM > & gaxpy(const T &alpha, const Function< Q, NDIM > &other, const R &beta, bool fence=true)
Inplace, general bi-linear operation in wavelet basis. No communication except for optional fence.
Definition mra.h:1124
void set_impl(const std::shared_ptr< FunctionImpl< T, NDIM > > &impl)
Replace current FunctionImpl with provided new one.
Definition mra.h:731
void multi_to_multi_op_values(const opT &op, const std::vector< Function< T, NDIM > > &vin, std::vector< Function< T, NDIM > > &vout, const bool fence=true)
apply op on the input vector yielding an output vector of functions
Definition mra.h:1701
bool is_initialized() const
Returns true if the function is initialized.
Definition mra.h:172
long size() const
Definition lowranktensor.h:488
Key is the index for a node of the 2^NDIM-tree.
Definition key.h:70
Definition lbdeux.h:233
std::shared_ptr< WorldDCPmapInterface< keyT > > load_balance(double fac=1.0, bool printstuff=false)
Actually does the partitioning of the tree.
Definition lbdeux.h:390
void add_tree(const Function< T, NDIM > &f, const costT &costfn, bool fence=false)
Accumulates cost from a function.
Definition lbdeux.h:294
Convolutions in separated form (including Gaussian)
Definition operator.h:139
A slice defines a sub-range or patch of a dimension.
Definition slice.h:103
A tensor is a multidimensional array.
Definition tensor.h:318
TensorTypeData< T >::scalar_type scalar_type
C++ typename of the real type associated with a complex type.
Definition tensor.h:410
T * ptr()
Returns a pointer to the internal data.
Definition tensor.h:1841
A simple, fixed dimension vector.
Definition vector.h:64
Interface to be provided by any process map.
Definition worlddc.h:125
Definition worlddc.h:309
void max(T *buf, size_t nelem)
Inplace global max while still processing AM & tasks.
Definition worldgop.h:902
void fence(bool debug=false)
Synchronizes all processes in communicator AND globally ensures no pending AM or tasks.
Definition worldgop.cc:176
void min(T *buf, size_t nelem)
Inplace global min while still processing AM & tasks.
Definition worldgop.h:896
void sum(T *buf, size_t nelem)
Inplace global sum while still processing AM & tasks.
Definition worldgop.h:890
void fence()
Returns after all local tasks have completed.
Definition world_task_queue.h:1384
A parallel world class.
Definition world.h:134
WorldTaskQueue & taskq
Task queue.
Definition world.h:215
ProcessID rank() const
Returns the process rank in this World (same as MPI_Comm_rank()).
Definition world.h:344
ProcessID size() const
Returns the number of processes in this World (same as MPI_Comm_size()).
Definition world.h:354
WorldGopInterface & gop
Global operations.
Definition world.h:216
An archive for storing local or parallel data, wrapping a BinaryFstreamInputArchive.
Definition parallel_archive.h:366
An archive for storing local or parallel data wrapping a BinaryFstreamOutputArchive.
Definition parallel_archive.h:321
int integer
Definition crayio.c:25
static const double R
Definition csqrt.cc:46
Declaration and initialization of tree traversal functions and generic derivative.
Tensor< T > conj_transpose(const Tensor< T > &t)
Returns a new deep copy of the complex conjugate transpose of the input tensor.
Definition tensor.h:2044
Tensor< T > transpose(const Tensor< T > &t)
Returns a new deep copy of the transpose of the input tensor.
Definition tensor.h:2035
const double beta
Definition gygi_soltion.cc:62
static const double v
Definition hatom_sf_dirac.cc:20
Tensor< double > op(const Tensor< double > &x)
Definition kain.cc:508
#define rot(x, k)
Definition lookup3.c:72
#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_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
Main include file for MADNESS and defines Function interface.
static const bool VERIFY_TREE
Definition mra.h:57
Namespace for all elements and tools of MADNESS.
Definition DFParameters.h:13
void save_function(const std::vector< Function< T, NDIM > > &f, const std::string name)
save a vector of functions
Definition vmra.h:2540
bool ensure_tree_state_respecting_fence(const std::vector< Function< T, NDIM > > &v, const TreeState state, bool fence)
ensure v has the requested tree state, change the tree state of v if necessary and no fence is given
Definition vmra.h:317
void rr_cholesky(Tensor< T > &A, typename Tensor< T >::scalar_type tol, Tensor< integer > &piv, int &rank)
Compute the rank-revealing Cholesky factorization.
Definition lapack.cc:1203
void make_redundant(World &world, const std::vector< Function< T, NDIM > > &v, bool fence=true)
change tree_state of a vector of functions to redundant
Definition vmra.h:187
std::vector< Function< T, NDIM > > orthonormalize_rrcd(const std::vector< Function< T, NDIM > > &v, Tensor< T > &ovlp, const double tol, Tensor< integer > &piv, int &rank)
Definition vmra.h:663
Function< double, NDIM > abssq(const Function< double_complex, NDIM > &z, bool fence=true)
Returns a new function that is the square of the absolute value of the input.
Definition mra.h:2917
Function< TENSOR_RESULT_TYPE(L, R), NDIM > gaxpy_oop(TENSOR_RESULT_TYPE(L, R) alpha, const Function< L, NDIM > &left, TENSOR_RESULT_TYPE(L, R) beta, const Function< R, NDIM > &right, bool fence=true)
Returns new function alpha*left + beta*right optional fence and no automatic compression.
Definition mra.h:2099
Function< typename TensorTypeData< Q >::scalar_type, NDIM > abs_square(const Function< Q, NDIM > &func)
Definition complexfun.h:121
std::vector< double > function_costs(World &world, const std::vector< Function< T, NDIM > > &v)
Definition vmra.h:1556
Function< T, NDIM > square(const Function< T, NDIM > &f, bool fence=true)
Create a new function that is the square of f - global comm only if not reconstructed.
Definition mra.h:2885
Function< TENSOR_RESULT_TYPE(L, R), NDIM > sub(const Function< L, NDIM > &left, const Function< R, NDIM > &right, bool fence=true)
Same as operator- but with optional fence and no automatic compression.
Definition mra.h:2154
std::vector< ProcessID > assign_round_robin(std::size_t nfunc, int nranks)
owner[j] = j % nranks. For redistribute_to_batches.
Definition vmra.h:1520
std::vector< Function< T, NDIM > > reduce_rank(std::vector< Function< T, NDIM > > v, double thresh=0.0, bool fence=true)
reduces the tensor rank of the coefficient tensor (if applicable)
Definition vmra.h:367
Tensor< double > norm2s_T(World &world, const std::vector< Function< T, NDIM > > &v)
Computes the 2-norms of a vector of functions.
Definition vmra.h:909
std::vector< double > norm2s(World &world, const std::vector< Function< T, NDIM > > &v)
Computes the 2-norms of a vector of functions.
Definition vmra.h:896
Function< TENSOR_RESULT_TYPE(T, R), NDIM > dot_sparse(World &world, const std::vector< Function< T, NDIM > > &a, const std::vector< Function< R, NDIM > > &b, double tol, bool fence=true, bool do_make_redundant=true)
Multiplies and sums two vectors of functions r = \sum_i a[i] * b[i].
Definition vmra.h:1796
std::vector< Function< T, NDIM > > grad_bspline_one(const Function< T, NDIM > &f, bool refine=false, bool fence=true)
Definition vmra.h:2337
void set_impl(std::vector< Function< T, NDIM > > &v, const std::vector< std::shared_ptr< FunctionImpl< T, NDIM > > > vimpl)
Definition vmra.h:739
Function< Q, NDIM > convert(const Function< T, NDIM > &f, bool fence=true)
Type conversion implies a deep copy. No communication except for optional fence.
Definition mra.h:2232
Function< TENSOR_RESULT_TYPE(Q, T), NDIM > mul(const Q alpha, const Function< T, NDIM > &f, bool fence=true)
Returns new function equal to alpha*f(x) with optional fence.
Definition mra.h:1884
Function< TENSOR_RESULT_TYPE(T, R), NDIM > dot(World &world, const std::vector< Function< T, NDIM > > &a, const std::vector< Function< R, NDIM > > &b, bool fence=true, bool do_make_redundant=true)
Multiplies and sums two vectors of functions r = \sum_i a[i] * b[i]; see dot_sparse for screening.
Definition vmra.h:1809
std::vector< Function< T, NDIM > > orthonormalize_symmetric(const std::vector< Function< T, NDIM > > &v, const Tensor< T > &ovlp, double lindep=1e-12)
symmetric orthonormalization (see e.g. Szabo/Ostlund)
Definition vmra.h:501
std::vector< std::shared_ptr< FunctionImpl< T, NDIM > > > get_impl(const std::vector< Function< T, NDIM > > &v)
Definition vmra.h:732
Function< T, NDIM > div(const std::vector< Function< T, NDIM > > &v, bool do_refine=false, bool fence=true)
shorthand div operator
Definition vmra.h:2409
std::vector< Function< T, NDIM > > orthonormalize_cd(const std::vector< Function< T, NDIM > > &v, Tensor< T > &ovlp)
Definition vmra.h:626
std::vector< Function< T, NDIM > > copy_n(World &world, const Function< T, NDIM > &v, const unsigned int n, bool fence=true)
Returns a vector of n deep copies of a function.
Definition vmra.h:1483
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:1306
tensorT Q2(const tensorT &s)
Given overlap matrix, return rotation with 2nd order error to orthonormalize the vectors.
Definition SCF.cc:139
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:758
std::vector< Function< TENSOR_RESULT_TYPE(T, R), NDIM > > cross(const std::vector< Function< T, NDIM > > &f, const std::vector< Function< R, NDIM > > &g, bool do_refine=false, bool fence=true)
shorthand cross operator
Definition vmra.h:2476
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
@ unknown
Definition funcdefaults.h:68
@ compressed
d coeffs in internal nodes, s and d coeffs at the root, empty leaves may be present
Definition funcdefaults.h:61
@ redundant
s coeffs everywhere
Definition funcdefaults.h:65
@ nonstandard_with_leaves
like nonstandard, with s coeffs at the leaves
Definition funcdefaults.h:63
Function< T, NDIM > conj(const Function< T, NDIM > &f, bool fence=true)
Return the complex conjugate of the input function with the same distribution and optional fence.
Definition mra.h:2246
void cholesky(Tensor< T > &A)
Compute the Cholesky factorization.
Definition lapack.cc:1174
std::vector< std::vector< Function< TENSOR_RESULT_TYPE(T, R), NDIM > > > matrix_mul_sparse(World &world, const std::vector< Function< R, NDIM > > &f, const std::vector< Function< R, NDIM > > &g, double tol, bool fence=true, bool symm=false)
Outer product of a vector of functions with a vector of functions using sparsity.
Definition vmra.h:1277
void standard(World &world, std::vector< Function< T, NDIM > > &v, bool fence=true)
Generates standard form of a vector of functions.
Definition vmra.h:244
void truncate(World &world, std::vector< Function< T, NDIM > > &v, double tol=0.0, bool fence=true)
Truncates a vector of functions.
Definition vmra.h:336
void compress(World &world, const std::vector< Function< T, NDIM > > &v, bool fence=true)
Compress a vector of functions.
Definition vmra.h:150
const std::vector< Function< T, NDIM > > & reconstruct(const std::vector< Function< T, NDIM > > &v)
reconstruct a vector of functions
Definition vmra.h:163
std::vector< Function< T, NDIM > > impl2function(const std::vector< std::shared_ptr< FunctionImpl< T, NDIM > > > vimpl)
Definition vmra.h:745
std::vector< Function< T, NDIM > > grad_bpsline_two(const Function< T, NDIM > &f, bool refine=false, bool fence=true)
Definition vmra.h:2358
void set_thresh(World &world, std::vector< Function< T, NDIM > > &v, double thresh, bool fence=true)
Sets the threshold in a vector of functions.
Definition vmra.h:1418
double norm2(World &world, const std::vector< Function< T, NDIM > > &v)
Computes the 2-norm of a vector of functions.
Definition vmra.h:922
std::vector< ProcessID > assign_cost_aware(const std::vector< double > &cost, int nranks)
Definition vmra.h:1531
std::vector< Function< T, NDIM > > flatten(const std::vector< std::vector< Function< T, NDIM > > > &vv)
Definition vmra.h:725
std::vector< CCPairFunction< T, NDIM > > operator*(const double fac, const std::vector< CCPairFunction< T, NDIM > > &arg)
Definition ccpairfunction.h:1089
static void verify_tree(World &world, const std::vector< Function< T, NDIM > > &v)
Definition SCF.cc:76
std::vector< Function< T, NDIM > > multi_to_multi_op_values(const opT &op, const std::vector< Function< T, NDIM > > &vin, const bool fence=true)
apply op on the input vector yielding an output vector of functions
Definition vmra.h:2126
static const Slice _(0,-1, 1)
Tensor< T > inverse(const Tensor< T > &a_in)
invert general square matrix A
Definition lapack.cc:832
void load_balance(const real_function_6d &f, const bool leaf)
do some load-balancing
Definition madness/chem/mp2.cc:70
TreeState get_tree_state(const Function< T, NDIM > &f)
get tree state of a function
Definition mra.h:2933
std::vector< CCPairFunction< T, NDIM > > operator-(const std::vector< CCPairFunction< T, NDIM > > c1, const std::vector< CCPairFunction< T, NDIM > > &c2)
Definition ccpairfunction.h:1060
std::vector< Function< T, NDIM > > partial_mul(const Function< T, NDIM > f, const std::vector< Function< T, LDIM > > g, const int particle)
multiply a high-dimensional function with a low-dimensional function
Definition vmra.h:1353
Function< T, NDIM > gaxpy_oop_reconstructed(const double alpha, const Function< T, NDIM > &left, const double beta, const Function< T, NDIM > &right, const bool fence=true)
Returns new function alpha*left + beta*right optional fence, having both addends reconstructed.
Definition mra.h:2117
std::vector< Function< TENSOR_RESULT_TYPE(T, R), NDIM > > transform_reconstructed(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:787
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:227
std::vector< Function< T, NDIM > > append(const std::vector< Function< T, NDIM > > &lhs, const std::vector< Function< T, NDIM > > &rhs)
combine two vectors
Definition vmra.h:718
void redistribute_to_batches(World &world, std::vector< Function< T, NDIM > > &v, const std::vector< ProcessID > &owner, std::size_t cap_bytes=0, bool rotate=true)
Definition vmra.h:1576
@ TT_2D
Definition gentensor.h:120
@ TT_FULL
Definition gentensor.h:120
void refine(World &world, const std::vector< Function< T, NDIM > > &vf, bool fence=true)
refine the functions according to the autorefine criteria
Definition vmra.h:197
void print_size(World &world, const std::vector< Function< T, NDIM > > &v, const std::string &msg="vectorfunction")
Definition vmra.h:2061
NDIM & f
Definition mra.h:2620
Function< TENSOR_RESULT_TYPE(L, R), NDIM > add(const Function< L, NDIM > &left, const Function< R, NDIM > &right, bool fence=true)
Same as operator+ but with optional fence and no automatic compression.
Definition mra.h:2109
const Function< T, NDIM > & change_tree_state(const Function< T, NDIM > &f, const TreeState finalstate, bool fence=true)
change tree state of a function
Definition mra.h:2946
std::vector< CCPairFunction< T, NDIM > > & operator-=(std::vector< CCPairFunction< T, NDIM > > &rhs, const std::vector< CCPairFunction< T, NDIM > > &lhs)
Definition ccpairfunction.h:1082
std::vector< Function< T, NDIM > > orthonormalize(const std::vector< Function< T, NDIM > > &vf_in)
orthonormalize the vectors
Definition vmra.h:466
NDIM const Function< R, NDIM > & g
Definition mra.h:2620
double wall_time()
Returns the wall time in seconds relative to an arbitrary origin.
Definition timers.cc:48
std::vector< Function< T, NDIM > > grad_ble_one(const Function< T, NDIM > &f, bool refine=false, bool fence=true)
Definition vmra.h:2295
Function< TENSOR_RESULT_TYPE(typename opT::opT, R), NDIM > apply_only(const opT &op, const Function< R, NDIM > &f, bool fence=true)
Apply operator ONLY in non-standard form - required other steps missing !!
Definition mra.h:2320
std::vector< Function< T, NDIM > > grad_bspline_three(const Function< T, NDIM > &f, bool refine=false, bool fence=true)
Definition vmra.h:2379
static const int kmax
Definition twoscale.cc:52
std::vector< Function< T, NDIM > > zero_functions_compressed(World &world, int n, bool fence=true)
Generates a vector of zero functions (compressed)
Definition vmra.h:450
double imag(double x)
Definition complexfun.h:56
void load_function(World &world, std::vector< Function< T, NDIM > > &f, const std::string name)
load a vector of functions
Definition vmra.h:2528
std::vector< Function< TENSOR_RESULT_TYPE(L, R), D > > vmulXX(const Function< L, D > &left, const std::vector< Function< R, D > > &vright, double tol, bool fence=true)
Use the vmra/mul(...) interface instead.
Definition mra.h:1989
void refine_to_common_level(World &world, std::vector< Function< T, NDIM > > &vf, bool fence=true)
refine all functions to a common (finest) level
Definition vmra.h:207
std::vector< Function< T, NDIM > > grad_ble_two(const Function< T, NDIM > &f, bool refine=false, bool fence=true)
Definition vmra.h:2316
static bool print_timings
Definition SCF.cc:108
CCPairFunction< T, NDIM > apply(const SeparatedConvolution< T, NDIM/2 > &op, const CCPairFunction< T, NDIM > &arg)
apply the operator to the argument
Definition ccpairfunction.h:896
void normalize(World &world, std::vector< Function< T, NDIM > > &v, bool fence=true)
Normalizes a vector of functions — v[i] = v[i].scale(1.0/v[i].norm2())
Definition vmra.h:2053
void stage_halo(World &world, const std::vector< std::shared_ptr< Derivative< T, NDIM > > > &grad, const std::vector< Function< T, NDIM > > &v, bool fence=true)
Pre-stages the neighbor coefficients that differentiating v with each of grad will need.
Definition vmra.h:382
std::vector< Function< T, NDIM > > zero_functions(World &world, int n, bool fence=true)
Generates a vector of zero functions (reconstructed)
Definition vmra.h:443
std::vector< CCPairFunction< T, NDIM > > operator+(const std::vector< CCPairFunction< T, NDIM > > c1, const std::vector< CCPairFunction< T, NDIM > > &c2)
Definition ccpairfunction.h:1052
Function< TENSOR_RESULT_TYPE(L, R), NDIM > mul_sparse(const Function< L, NDIM > &left, const Function< R, NDIM > &right, double tol, bool fence=true, bool do_make_redundant=true)
Sparse multiplication; the scalar interface redirects to the vector one in vmra.h.
Definition mra.h:1929
std::vector< Function< T, NDIM > > grad(const Function< T, NDIM > &f, bool refine=false, bool fence=true)
shorthand gradient operator
Definition vmra.h:2277
Function< T, CCPairFunction< T, NDIM >::LDIM > inner(const CCPairFunction< T, NDIM > &c, const Function< T, CCPairFunction< T, NDIM >::LDIM > &f, const std::tuple< int, int, int > v1, const std::tuple< int, int, int > v2)
Definition ccpairfunction.h:993
Function< T, NDIM > multiply(const Function< T, NDIM > f, const Function< T, LDIM > g, const int particle, const bool fence=true)
multiply a high-dimensional function with a low-dimensional function
Definition mra.h:2573
void scale(World &world, std::vector< Function< T, NDIM > > &v, const std::vector< Q > &factors, bool fence=true)
Scales inplace a vector of functions by distinct values.
Definition vmra.h:874
std::vector< Function< T, NDIM > > zero_functions_auto_tree_state(World &world, int n, bool fence=true)
Generates a vector of zero functions, either compressed or reconstructed, depending on tensor type.
Definition vmra.h:457
DistributedMatrix< T > matrix_dot(const DistributedMatrixDistribution &d, const std::vector< Function< T, NDIM > > &f, const std::vector< Function< T, NDIM > > &g, bool sym=false)
Definition vmra.h:1698
void load(Function< T, NDIM > &f, const std::string name)
Definition mra.h:2984
std::vector< Function< T, NDIM > > zero_functions_tree_state(World &world, int n, const TreeState state, bool fence=true)
Generates a vector of zero functions with a given tree state.
Definition vmra.h:422
double real(double x)
Definition complexfun.h:52
std::vector< CCPairFunction< T, NDIM > > & operator+=(std::vector< CCPairFunction< T, NDIM > > &lhs, const CCPairFunction< T, NDIM > &rhs)
Definition ccpairfunction.h:1068
@ same
same atoms at the same places
std::vector< Function< T, NDIM > > orthonormalize_canonical(const std::vector< Function< T, NDIM > > &v, const Tensor< T > &ovlp, double lindep=1e-12)
Definition vmra.h:563
Tensor< TENSOR_RESULT_TYPE(T, R) > matrix_dot_old(World &world, const std::vector< Function< T, NDIM > > &f, const std::vector< Function< R, NDIM > > &g, bool sym=false)
Computes the matrix dot product of two function vectors - q(i,j) = dot(f[i],g[j])
Definition vmra.h:1761
std::string name(const FuncType &type, const int ex=-1)
Definition ccpairfunction.h:28
void clear_halo(const std::vector< Function< T, NDIM > > &v)
Discards the neighbor halos staged on v.
Definition vmra.h:397
void matrix_inner(DistributedMatrix< T > &A, const std::vector< Function< T, NDIM > > &f, const std::vector< Function< T, NDIM > > &g, bool sym=false)
Definition distpm.cc:46
double get_size(World &world, const std::vector< Function< T, NDIM > > &v)
Definition vmra.h:2095
double get_size_local(World &world, const std::vector< Function< T, NDIM > > &v)
return the size of a vector of functions for each rank
Definition vmra.h:2076
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:2185
void syev(const Tensor< T > &A, Tensor< T > &V, Tensor< typename Tensor< T >::scalar_type > &e)
Real-symmetric or complex-Hermitian eigenproblem.
Definition lapack.cc:969
Tensor< TENSOR_RESULT_TYPE(T, R) > matrix_inner_old(World &world, const std::vector< Function< T, NDIM > > &f, const std::vector< Function< R, NDIM > > &g, bool sym=false)
Computes the matrix inner product of two function vectors - q(i,j) = inner(f[i],g[j])
Definition vmra.h:1044
void make_nonstandard(World &world, std::vector< Function< T, NDIM > > &v, bool fence=true)
Generates non-standard form of a vector of functions.
Definition vmra.h:234
void gaxpy(const double a, ScalarResult< T > &left, const double b, const T &right, const bool fence=true)
the result type of a macrotask must implement gaxpy
Definition macrotaskq.h:244
int distance(const madness::Hash_private::HashIterator< hashT > &it, const madness::Hash_private::HashIterator< hashT > &jt)
Definition worldhashmap.h:616
static long abs(long a)
Definition tensor.h:219
static const double b
Definition nonlinschro.cc:119
static const double d
Definition nonlinschro.cc:121
static const double a
Definition nonlinschro.cc:118
static const size_t nfunc
Definition pcr.cc:63
double Q(double a)
Definition relops.cc:20
static const double c
Definition relops.cc:10
static const double m
Definition relops.cc:9
static const double L
Definition rk.cc:46
static const double thresh
Definition rk.cc:45
static const long k
Definition rk.cc:44
Definition test_ar.cc:204
Definition mp2.h:63
double operator()(const Key< 6 > &key, const FunctionNode< double, 6 > &node) const
Definition mp2.h:70
Definition lowrankfunction.h:336
Definition dirac-hatom.cc:112
std::string ok(const bool b)
Definition test6.cc:43
AtomicInt sum
Definition test_atomicint.cc:46
int P
Definition test_binsorter.cc:9
void e()
Definition test_sig.cc:75
static const double alpha
Definition testcosine.cc:10
constexpr std::size_t NDIM
Definition testgconv.cc:54
double h(const coord_1d &r)
Definition testgconv.cc:175
#define TENSOR_RESULT_TYPE(L, R)
This macro simplifies access to TensorResultType.
Definition type_data.h:205
#define PROFILE_FUNC
Definition worldprofile.h:209
#define PROFILE_BLOCK(name)
Definition worldprofile.h:208
int ProcessID
Used to clearly identify process number/rank.
Definition worldtypes.h:43