MADNESS 0.10.1
derivative.h
Go to the documentation of this file.
1/*
2 This file is part of MADNESS.
3
4 Copyright (C) 2007,2010 Oak Ridge National Laboratory
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
20 For more information please contact:
21
22 Robert J. Harrison
23 Oak Ridge National Laboratory
24 One Bethel Valley Road
25 P.O. Box 2008, MS-6367
26
27 email: harrisonrj@ornl.gov
28 tel: 865-241-3937
29 fax: 865-572-0680
30*/
31
32#ifndef MADNESS_DERIVATIVE_H__INCLUDED
33#define MADNESS_DERIVATIVE_H__INCLUDED
34
35#include <iostream>
36#include <iomanip>
37#include <fstream>
40#include <madness/world/print.h>
41#include <madness/misc/misc.h>
42
45
46#include <madness/mra/key.h>
48
49
50/// \file mra/derivative.h
51/// \brief Declaration and initialization of tree traversal functions and generic derivative
52/// \ingroup mra
53
54namespace madness {
55
56 template<typename T, std::size_t NDIM>
57 class FunctionNode;
58
59 template<typename T, std::size_t NDIM>
60 class Function;
61
62}
63
64
65
66namespace madness {
67
68
69/// Tri-diagonal operator traversing tree primarily for derivative operator
70
71 /// \ingroup mra
72 template <typename T, std::size_t NDIM>
73 class DerivativeBase : public WorldObject< DerivativeBase<T, NDIM> > {
75 protected:
77 const std::size_t axis ; ///< Axis along which the operation is performed
78 const int k ; ///< Number of wavelets of the function
80 const std::vector<long> vk; ///< (k,...) used to initialize Tensors
81
82 public:
83 friend class FunctionImpl<T, NDIM>;
84
85 typedef Tensor<T> tensorT ; ///< regular tensors, like rm, etc
86 typedef GenTensor<T> coeffT ; ///< holding the node's coeffs (possibly low rank)
87 typedef Key<NDIM> keyT ;
88 typedef std::pair<keyT,coeffT> argT ;
93
94 /// Spawn `diff`'s per-node tasks from the task pool instead of the main thread.
95
96 /// The same tasks are produced, so the result is unchanged. Per instance and off by default,
97 /// since it only pays off when many functions are differentiated at once.
98 bool parallel_submit_ = false;
99
102 , world(world)
103 , axis(axis)
104 , k(k)
105 , bc(bc)
106 , vk(NDIM,k)
107 {
108 // No! Cannot process incoming messages until the *derived* class is constructed.
109 // this->process_pending();
110 }
111
112 virtual ~DerivativeBase() { }
113
114 void forward_do_diff1(const implT* f, implT* df, const keyT& key,
115 const argT& left,
116 const argT& center,
117 const argT& right) const {
118
119 const dcT& coeffs = f->get_coeffs();
120 ProcessID owner = coeffs.owner(key);
121
122 if (owner == world.rank()) {
123 if (!left.second.has_data()) {
125 f, df, key, find_neighbor(f, key,-1), center, right,
127 }
128 else if (!right.second.has_data()) {
130 f, df, key, left, center, find_neighbor(f, key,1),
132 }
133 // Boundary node
134 else if (left.first.is_invalid() || right.first.is_invalid()) {
136 f, df, key, left, center, right);
137 }
138 // Interior node
139 else {
141 f, df, key, left, center, right);
142 }
143 }
144 else {
146 this, f, key, left, center, right, TaskAttributes::hipri());
147 }
148 }
149
150 void do_diff1(const implT* f, implT* df, const keyT& key,
151 const argT& left,
152 const argT& center,
153 const argT& right) const {
155
156// if (left.second.size()==0 || right.second.size()==0) {
157 if ((!left.second.has_data()) || (!right.second.has_data())) {
158 // One of the neighbors is below us in the tree ... recur down
159 df->get_coeffs().replace(key,nodeT(coeffT(),true));
160 for (KeyChildIterator<NDIM> kit(key); kit; ++kit) {
161 const keyT& child = kit.key();
162 if ((child.translation()[axis]&1) == 0) {
163 // leftmost child automatically has right sibling
164 forward_do_diff1(f, df, child, left, center, center);
165 }
166 else {
167 // rightmost child automatically has left sibling
168 forward_do_diff1(f, df, child, center, center, right);
169 }
170 }
171 }
172 else {
173 forward_do_diff1(f, df, key, left, center, right);
174 }
175 }
176
177 virtual void do_diff2b(const implT* f, implT* df, const keyT& key,
178 const argT& left,
179 const argT& center,
180 const argT& right) const = 0;
181
182 virtual void do_diff2i(const implT* f, implT* df, const keyT& key,
183 const argT& left,
184 const argT& center,
185 const argT& right) const = 0;
186
187
188 /// Differentiate w.r.t. given coordinate (x=0, y=1, ...) with optional fence
189
190 /// Returns a new function with the same distribution
192 operator()(const functionT& f, bool fence=true) const {
193 if (VERIFY_TREE) f.verify_tree();
194 if (fence) f.change_tree_state(reconstructed);
195 MADNESS_CHECK_THROW(f.is_reconstructed(),"diff: trying to diff a compressed function without fencing");
196
197 functionT df;
198 df.set_impl(f,false);
199
200 df.get_impl()->diff(this, f.get_impl().get(), fence);
201 return df;
202 }
203
204
205 static bool enforce_bc(int bc_left, int bc_right, Level n, Translation& l) {
206 Translation two2n = 1ul << n;
207 if (l < 0) {
209 return false; // f=0 BC, or no BC, or nonzero f BC, or zero deriv BC, or nonzero deriv BC
210 }
211 else if (bc_left == BC_PERIODIC) {
212 l += two2n; // Periodic BC
213 MADNESS_ASSERT(bc_left == bc_right); //check that both BCs are periodic
214 }
215 else {
216 MADNESS_EXCEPTION("enforce_bc: confused left BC?",bc_left);
217 }
218 }
219 else if (l >= two2n) {
221 return false; // f=0 BC, or no BC, or nonzero f BC, or zero deriv BC, or nonzero deriv BC
222 }
223 else if (bc_right == BC_PERIODIC) {
224 l -= two2n; // Periodic BC
225 MADNESS_ASSERT(bc_left == bc_right); //check that both BCs are periodic
226 }
227 else {
228 MADNESS_EXCEPTION("enforce_bc: confused BC right?",bc_right);
229 }
230 }
231 return true;
232 }
233
234 Key<NDIM> neighbor(const keyT& key, int step) const {
236 l[axis] += step;
237 if (!enforce_bc(bc(axis,0), bc(axis,1), key.level(), l[axis])) {
238 return keyT::invalid();
239 }
240 else {
241 return keyT(key.level(),l);
242 }
243 }
244
245 /// Push f's remote same-level neighbor coefficients to the ranks that will need them.
246
247 /// Node M is the neighbor of M-1 and M+1, so its owner pushes it to the owners of those keys
248 /// without being asked, one batched message per destination. This is the axis-specific
249 /// staging policy for `FunctionImpl`'s halo table; `halo_clear()` frees the result. The
250 /// pushes arrive as tasks, so the fence is what separates staging from differentiating.
251 void stage_halo(const implT* f, bool fence = true) const {
252 const dcT& coeffs = f->get_coeffs();
253 std::map<ProcessID, std::vector<argT> > out;
254 for (const auto& [key, node] : coeffs) {
255 for (int step : {-1, 1}) {
256 keyT consumer = neighbor(key, step);
257 if (consumer.is_invalid()) continue; // domain boundary: no consumer there
258 ProcessID d = coeffs.owner(consumer);
259 if (d == world.rank()) continue; // local consumer: the pull is cheap
260 out[d].push_back(argT(key, node.has_coeff() ? node.coeff() : coeffT()));
261 }
262 }
263 for (auto& kv : out)
264 f->task(kv.first, &implT::receive_halo, kv.second, TaskAttributes::hipri());
265 if (fence) world.gop.fence();
266 }
267
269 find_neighbor(const implT* f, const Key<NDIM>& key, int step) const {
270 keyT neigh = neighbor(key, step);
271 if (neigh.is_invalid()) {
272 return Future<argT>(argT(neigh,coeffT(vk,f->get_tensor_args()))); // Zero bc
273 }
274 else {
275 // hit: same-level leaf or interior node. miss: neighbor is coarser, walk up below.
276 coeffT c;
277 if (f->halo_probe(neigh, c)) return Future<argT>(argT(neigh, c));
278 Future<argT> result;
279 if (f->get_coeffs().is_local(neigh))
280 f->send(f->get_coeffs().owner(neigh), &implT::sock_it_to_me, neigh, result.remote_ref(world));
281 else
282 f->task(f->get_coeffs().owner(neigh), &implT::sock_it_to_me, neigh, result.remote_ref(world), TaskAttributes::hipri());
283 return result;
284 }
285 }
286
287
288 /// Body of `FunctionImpl::diff`'s submission loop, as a functor for `taskq.for_each`.
289 struct submit_op {
292 const implT* f;
294 submit_op(const DerivativeBase<T,NDIM>* D=nullptr, const implT* f=nullptr, implT* df=nullptr)
295 : D(D), f(f), df(df) {}
296 bool operator()(typename rangeT::iterator& it) const {
297 const keyT& key = it->first;
298 const nodeT& node = it->second;
299 if (node.has_coeff()) {
300 Future<argT> left = D->find_neighbor(f, key, -1);
301 argT center(key, node.coeff());
302 Future<argT> right = D->find_neighbor(f, key, 1);
303 df->world.taskq.add(*df, &implT::do_diff1, D, f, key, left, center, right, TaskAttributes::hipri());
304 }
305 else {
306 df->get_coeffs().replace(key, nodeT(coeffT(), true)); // empty internal node
307 }
308 return true;
309 }
310 template <typename Archive> void serialize(const Archive& ar) {}
311 };
312
313 /// Parallel form of `FunctionImpl::diff`'s submission loop; the caller owns the fence.
314 void submit_diff_tasks(const implT* f, implT* df) const {
317 rangeT(f->get_coeffs().begin(), f->get_coeffs().end()), submit_op(this, f, df));
318 }
319
320 template <typename Archive> void serialize(const Archive& ar) const {
321 throw "NOT IMPLEMENTED";
322 }
323
324 }; // End of the DerivativeBase class
325
326
327 /// Implements derivatives operators with variety of boundary conditions on simulation domain
328 template <typename T, std::size_t NDIM>
329 class Derivative : public DerivativeBase<T, NDIM> {
330 private:
332
333 public:
335 typedef GenTensor<T> coeffT ; ///< holding the node's coeffs (possibly low rank)
336 typedef Key<NDIM> keyT ;
337 typedef std::pair<keyT,coeffT> argT ;
342
343 private:
344 const functionT g1; ///< Function describing the boundary condition on the right side
345 const functionT g2; ///< Function describing the boundary condition on the left side
346
349
350 // Tensors for holding the modified coefficients
351 Tensor<double> rm, r0, rp ; ///< Blocks of the derivative operator
352 Tensor<double> rmt, r0t, rpt ; ///< Blocks of the derivative operator, transposed
353 Tensor<double> left_rm, left_r0 ; ///< Blocks of the derivative for the left boundary
354 Tensor<double> left_rmt, left_r0t ; ///< Blocks of the derivative for the left boundary
355 Tensor<double> right_r0, right_rp; ///< Blocks of the derivative for the right boundary
356 Tensor<double> right_r0t, right_rpt; ///< Blocks of the derivative for the right boundary
357 Tensor<double> bv_left, bv_right ; ///< Blocks of the derivative operator for the boundary contribution
358
359
360 // Tensors for the bspline smoothed central difference operator
367
368 void do_diff2b(const implT* f, implT* df, const keyT& key,
369 const argT& left,
370 const argT& center,
371 const argT& right) const {
373 double lev = (double) key.level();
374
375 coeffT d;
376
377 //left boundary
378 if (l[this->axis] == 0) {
379
380 coeffT tensor_right=df->parent_to_child(right.second, right.first, this->neighbor(key,1));
381 coeffT tensor_center=df->parent_to_child(center.second, center.first, key);
382
385 }
386 else {
387
388 coeffT tensor_left=df->parent_to_child(left.second, left.first, this->neighbor(key,-1));
389 coeffT tensor_center=df->parent_to_child(center.second, center.first, key);
390
393 }
394
395 double fac = FunctionDefaults<NDIM>::get_rcell_width()[this->axis]*pow(2.0,lev);
396 if (is_second) fac *= fac;
397 else if (is_third) fac *= fac*fac;
398
399 d.scale(fac);
400 d.reduce_rank(df->get_thresh());
401 df->get_coeffs().replace(key,nodeT(d,false));
402
403
404 // This is the boundary contribution (formally in BoundaryDerivative)
405 int bc_left = this->bc(this->axis,0);
406 int bc_right = this->bc(this->axis,1);
407
410 //left boundary
411 if (l[this->axis] == 0) {
412 if (bc_left != BC_PERIODIC && bc_left != BC_FREE && bc_left != BC_ZERO && bc_left != BC_ZERONEUMANN) {
413 bf = copy(bv_left);
414 found_argT = g1.get_impl()->find_me(key);
415 }
416 else {
417 return;
418 }
419 }
420 else { //right boundary
422 bf = copy(bv_right);
423 found_argT = g2.get_impl()->find_me(key);
424 }
425 else {
426 return;
427 }
428 }
429#ifdef HAVE_PARSEC
430 std::cerr << "FATAL ERROR: PaRSEC does not support recursive task execution but Derivative::do_diff2b requires this. Use a different backend" << std::endl;
431 abort();
432#endif
433 const auto& found_argT_value = found_argT.get(); // do not recursively execute tasks to avoid making PaRSEC sad
435
436 //if (this->bc.get_bc().dim(0) == 1) {
437 if (NDIM == 1) {
438 bdry_t = gcoeffs[0]*bf;
439 }
440 else {
441 tensorT slice_aid(this->k); //vector of zeros
442 slice_aid[0] = 1;
443 tensorT tmp = inner(slice_aid, gcoeffs, 0, this->axis);
444 bdry_t = outer(bf,tmp);
445 if (this->axis) bdry_t = copy(bdry_t.cycledim(this->axis,0,this->axis)); // make it contiguous
446 }
448
449 if (l[this->axis]==0) {
450 if (bc_left == BC_DIRICHLET)
451 bdry_t.scale( pow(2.0,lev));
452 else if (bc_left ==BC_NEUMANN)
454 }
455 else {
456 if (bc_right == BC_DIRICHLET)
457 bdry_t.scale( pow(2.0,lev));
458 else if (bc_right ==BC_NEUMANN)
460 }
461
462 bdry_t += d.full_tensor_copy();;
463 df->get_coeffs().replace(key,nodeT(coeffT(bdry_t,df->get_thresh(),df->get_tensor_type()),false));
464 }
465
466 void do_diff2i(const implT* f, implT*df, const keyT& key,
467 const argT& left,
468 const argT& center,
469 const argT& right) const
470 {
471//#if !HAVE_GENTENSOR
472// coeffT d = madness::inner(rp,
473// df->parent_to_child(left.second, left.first, baseT::neighbor(key,-1)).swapdim(this->axis,0),
474// 1, 0);
475// inner_result(r0,
476// df->parent_to_child(center.second, center.first, key).swapdim(this->axis,0),
477// 1, 0, d);
478// inner_result(rm,
479// df->parent_to_child(right.second, right.first, baseT::neighbor(key,1)).swapdim(this->axis,0),
480// 1, 0, d);
481// // flo thinks this is wrong for higher dimensions -- need to cycledim
482// if (this->axis) d = copy(d.swapdim(this->axis,0)); // make it contiguous
483// d.scale(FunctionDefaults<NDIM>::get_rcell_width()[this->axis]*pow(2.0,(double) key.level()));
484// df->get_coeffs().replace(key,nodeT(d,false));
485//
486//#else
487 coeffT tensor_left=df->parent_to_child(left.second, left.first, this->neighbor(key,-1));
488 coeffT tensor_center=df->parent_to_child(center.second, center.first, key);
489 coeffT tensor_right=df->parent_to_child(right.second, right.first, this->neighbor(key,1));
490
494
495 double fac = FunctionDefaults<NDIM>::get_rcell_width()[this->axis]*pow(2.0,(double) key.level());
496 if (is_second) fac *= fac;
497 else if (is_third) fac *= fac*fac;
498
499 d.scale(fac);
500 d.reduce_rank(df->get_thresh());
501 df->get_coeffs().replace(key,nodeT(d,false));
502
503//#endif
504
505 }
506
508 is_second = false;
509 is_third = false;
510
511 r0 = Tensor<double>(this->k,this->k);
512 rp = Tensor<double>(this->k,this->k);
513 rm = Tensor<double>(this->k,this->k);
514
515 left_rm = Tensor<double>(this->k,this->k);
516 left_r0 = Tensor<double>(this->k,this->k);
517
518 right_r0 = Tensor<double>(this->k,this->k);
519 right_rp = Tensor<double>(this->k,this->k);
520
521 // These are the coefficients for the boundary contribution
522 bv_left = Tensor<double>(this->k);
523 bv_right = Tensor<double>(this->k);
524
525 int bc_left = this->bc(this->axis,0);
526 int bc_right = this->bc(this->axis,1);
527
528 double kphase = -1.0;
529 if (this->k%2 == 0) kphase = 1.0;
530 double iphase = 1.0;
531 for (int i=0; i<this->k; ++i) {
532 double jphase = 1.0;
533 for (int j=0; j<this->k; ++j) {
534 double gammaij = sqrt(double((2*i+1)*(2*j+1)));
535 double Kij;
536 if (((i-j)>0) && (((i-j)%2)==1))
537 Kij = 2.0;
538 else
539 Kij = 0.0;
540
541 r0(i,j) = 0.5*(1.0 - iphase*jphase - 2.0*Kij)*gammaij;
542 rm(i,j) = 0.5*jphase*gammaij;
543 rp(i,j) =-0.5*iphase*gammaij;
544
545 // Constraints on the derivative
547 left_rm(i,j) = jphase*gammaij*0.5*(1.0 + iphase*kphase/this->k);
548
549 double phi_tmpj_left = 0;
550
551 for (int l=0; l<this->k; ++l) {
552 double gammalj = sqrt(double((2*l+1)*(2*j+1)));
553 double Klj;
554
555 if (((l-j)>0) && (((l-j)%2)==1)) Klj = 2.0;
556 else Klj = 0.0;
557
558 phi_tmpj_left += sqrt(double(2*l+1))*Klj*gammalj;
559 }
561 left_r0(i,j) = (0.5*(1.0 + iphase*kphase/this->k) - Kij)*gammaij + iphase*sqrt(double(2*i+1))*phi_tmpj_left/pow(this->k,2.);
562 }
563 else if (bc_left == BC_ZERO || bc_left == BC_DIRICHLET || bc_left == BC_FREE) {
564 left_rm(i,j) = rm(i,j);
565
566 // B.C. with a function
567 if (bc_left == BC_ZERO || bc_left == BC_DIRICHLET)
568 left_r0(i,j) = (0.5 - Kij)*gammaij;
569
570 // No B.C.
571 else if (bc_left == BC_FREE)
572 left_r0(i,j) = (0.5 - iphase*jphase - Kij)*gammaij;
573 }
574
575 // Constraints on the derivative
577 right_rp(i,j) = -0.5*(iphase + kphase / this->k)*gammaij;
578
579 double phi_tmpj_right = 0;
580 for (int l=0; l<this->k; ++l) {
581 double gammalj = sqrt(double((2*l+1)*(2*j+1)));
582 double Klj;
583 if (((l-j)>0) && (((l-j)%2)==1)) Klj = 2.0;
584 else Klj = 0.0;
585 phi_tmpj_right += sqrt(double(2*l+1))*Klj*gammalj;
586 }
587 right_r0(i,j) = -(0.5*jphase*(iphase+ kphase/this->k) + Kij)*gammaij + sqrt(double(2*i+1))*phi_tmpj_right/pow(this->k,2.);
588 }
589 else if (bc_right == BC_ZERO || bc_right == BC_FREE || bc_right == BC_DIRICHLET) {
590 right_rp(i,j) = rp(i,j);
591
592 // Zero BC
594 right_r0(i,j) = -(0.5*iphase*jphase + Kij)*gammaij;
595
596 // No BC
597 else if (bc_right == BC_FREE)
598 right_r0(i,j) = (1.0 - 0.5*iphase*jphase - Kij)*gammaij;
599
600 }
601
602 jphase = -jphase;
603 }
604 iphase = -iphase;
605 }
606
607 // Coefficients for the boundary contributions
608 iphase = 1.0;
609 for (int i=0; i<this->k; ++i) {
610 iphase = -iphase;
611
612 if (bc_left == BC_DIRICHLET)
613 bv_left(i) = iphase*sqrt(double(2*i+1)); // vector for left dirichlet BC
614 else if(bc_left == BC_NEUMANN)
615 bv_left(i) = -iphase*sqrt(double(2*i+1))/pow(this->k,2.); // vector for left deriv BC
616 else
617 bv_left(i) = 0.0;
618
619 if (bc_right == BC_DIRICHLET)
620 bv_right(i) = sqrt(double(2*i+1)); // vector for right dirichlet BC
621 else if (bc_right == BC_NEUMANN)
622 bv_right(i) = sqrt(double(2*i+1))/pow(this->k,2.); // vector for right deriv BC
623 else
624 bv_right(i) = 0.0;
625 }
626
627 r0t = transpose(r0);
628 rpt = transpose(rp);
629 rmt = transpose(rm);
630
633
636
637 //print(rm.normf(),r0.normf(),rp.normf(),left_rm.normf(),left_r0.normf(),right_r0.normf(),right_rp.normf(),bv_left.normf(),bv_right.normf());
638 }
639
640 public:
641 typedef T opT;
642
643 /// Constructs a derivative operator
644
645 /// @param world The world
646 /// @param axis The direction to differentiate
647 /// @param bc Boundary conditions (default from FunctionDefaults)
648 /// @param g1 Function providing left boundary value (default empty)
649 /// @param g2 Function providing right boundary value (default empty)
650 /// @param k Wavelet order (default from FunctionDefaults)
652 std::size_t axis,
654 const functionT g1=functionT(),
655 const functionT g2=functionT(),
657 : DerivativeBase<T, NDIM>(world, axis, k, bc)
658 , g1(g1)
659 , g2(g2)
660 {
663 g1.reconstruct();
664 g2.reconstruct();
665
666 this->process_pending();
667 }
668
669 virtual ~Derivative() { }
670
671 void set_is_first() {is_second = false; is_third = false;}
672 void set_is_second() {is_second = true; is_third=false;}
673 void set_is_third() {is_second = false; is_third = true;}
674
677 if(k > 18) throw "Bspline derivatives are only available up to k=18";
678 std::string filename = get_mra_data_dir() + "/b-spline-deriv1.txt";
680 }
681
684 if(k > 18) throw "Bspline derivatives are only available up to k=18";
685 std::string filename = get_mra_data_dir() + "/b-spline-deriv2.txt";
687 }
688
691 if(k > 18) throw "Bspline derivatives are only available up to k=18";
692 std::string filename = get_mra_data_dir() + "/b-spline-deriv3.txt";
694 }
695
696 void set_ble1() {
698 if(k > 15) throw "BLE derivatives are only available up to k=15";
699 std::string filename = get_mra_data_dir() + "/ble-first.txt";
701 }
702
703 void set_ble2() {
705 if(k > 15) throw "BLE derivatives are only available up to k=15";
706 std::string filename = get_mra_data_dir() + "/ble-second.txt";
708 }
709
710 void read_from_file(const std::string& filename, unsigned int order = 1) {
711
712 Tensor<double> r0_bsp(this->k,this->k);
713 Tensor<double> rp_bsp(this->k,this->k);
714 Tensor<double> rm_bsp(this->k,this->k);
715
716 std::ifstream f(filename);
717 bool found=false;
718
719 for (int m; f >> m; ) {
720 if (m == this->k) {
721 for (int i=0; i<m; i++)
722 for (int j=0; j<m; j++)
723 MADNESS_CHECK(f >> rp_bsp(i,j));
724 for (int i=0; i<m; i++)
725 for (int j=0; j<m; j++)
726 MADNESS_CHECK(f >> r0_bsp(i,j));
727 for (int i=0; i<m; i++)
728 for (int j=0; j<m; j++)
729 MADNESS_CHECK(f >> rm_bsp(i,j));
730 found = true;
731 break;
732 }
733 else {
734 double junk;
735 for (int i=0; i<3*m*m; i++)
736 MADNESS_CHECK(f >> junk);
737 }
738 }
743
745
747
749
750 // Get scaling factor right for higher order derivatives
751 if (order == 1) {
752 set_is_first();
753 }
754 else if(order == 2) {
756 }
757 else if(order == 3) {
758 set_is_third();
759 }
760 }
761 };
762
763
764 /// Convenience function returning derivative operator with free-space boundary conditions
765 template <typename T, std::size_t NDIM>
770
771
772 /// Conveinence function returning derivative operator with periodic boundary conditions
773 template <typename T, std::size_t NDIM>
778
779 /// Applies derivative operator to function (for syntactic equivalence to integral operator apply)
780 template <typename T, std::size_t NDIM>
782 apply(const Derivative<T,NDIM>& D, const Function<T,NDIM>& f, bool fence=true) {
783 return D(f,fence);
784 }
785
786 /// Convenience function returning vector of derivative operators implementing grad (\f$ \nabla \f$)
787
788 /// This will only work for BC_ZERO, BC_PERIODIC, BC_FREE and
789 /// BC_ZERONEUMANN since we are not passing in any boundary
790 /// functions.
791 template <typename T, std::size_t NDIM>
792 std::vector< std::shared_ptr< Derivative<T,NDIM> > >
796 std::vector< std::shared_ptr< Derivative<T,NDIM> > > r(NDIM);
797 for (std::size_t d=0; d<NDIM; ++d) {
798 MADNESS_CHECK(bc(d,0)!=BC_DIRICHLET && bc(d,1)!=BC_DIRICHLET);
799 MADNESS_CHECK(bc(d,0)!=BC_NEUMANN && bc(d,1)!=BC_NEUMANN);
800 r[d].reset(new Derivative<T,NDIM>(world,d,bc,Function<T,NDIM>(),Function<T,NDIM>(),k));
801 }
802 return r;
803 }
804
805
806 namespace archive {
807 template <class Archive, class T, std::size_t NDIM>
809 static void load(const Archive& ar, const DerivativeBase<T,NDIM>*& ptr) {
811 ar & p;
812 ptr = static_cast< const DerivativeBase<T,NDIM>* >(p);
813 }
814 };
815
816 template <class Archive, class T, std::size_t NDIM>
818 static void store(const Archive& ar, const DerivativeBase<T,NDIM>* const & ptr) {
819 ar & ptr->id();
820 }
821 };
822 }
823
824} // End of the madness namespace
825
826#endif // MADNESS_MRA_DERIVATIVE_H_INCLUDED
This header should include pretty much everything needed for the parallel runtime.
This class is used to specify boundary conditions for all operators.
Definition bc.h:72
Tri-diagonal operator traversing tree primarily for derivative operator.
Definition derivative.h:73
void submit_diff_tasks(const implT *f, implT *df) const
Parallel form of FunctionImpl::diff's submission loop; the caller owns the fence.
Definition derivative.h:314
void do_diff1(const implT *f, implT *df, const keyT &key, const argT &left, const argT &center, const argT &right) const
Definition derivative.h:150
GenTensor< T > coeffT
holding the node's coeffs (possibly low rank)
Definition derivative.h:86
static bool enforce_bc(int bc_left, int bc_right, Level n, Translation &l)
Definition derivative.h:205
DerivativeBase(World &world, std::size_t axis, int k, BoundaryConditions< NDIM > bc)
Definition derivative.h:100
Key< NDIM > keyT
Definition derivative.h:87
const BoundaryConditions< NDIM > bc
Definition derivative.h:79
Tensor< T > tensorT
regular tensors, like rm, etc
Definition derivative.h:85
const std::vector< long > vk
(k,...) used to initialize Tensors
Definition derivative.h:80
Key< NDIM > neighbor(const keyT &key, int step) const
Definition derivative.h:234
WorldContainer< Key< NDIM >, FunctionNode< T, NDIM > > dcT
Definition derivative.h:91
virtual ~DerivativeBase()
Definition derivative.h:112
FunctionImpl< T, NDIM > implT
Definition derivative.h:89
FunctionNode< T, NDIM > nodeT
Definition derivative.h:92
Function< T, NDIM > functionT
Definition derivative.h:90
void forward_do_diff1(const implT *f, implT *df, const keyT &key, const argT &left, const argT &center, const argT &right) const
Definition derivative.h:114
const int k
Number of wavelets of the function.
Definition derivative.h:78
WorldObject< DerivativeBase< T, NDIM > > woT
Definition derivative.h:74
void serialize(const Archive &ar) const
Definition derivative.h:320
Future< argT > find_neighbor(const implT *f, const Key< NDIM > &key, int step) const
Definition derivative.h:269
Function< T, NDIM > operator()(const functionT &f, bool fence=true) const
Differentiate w.r.t. given coordinate (x=0, y=1, ...) with optional fence.
Definition derivative.h:192
void stage_halo(const implT *f, bool fence=true) const
Push f's remote same-level neighbor coefficients to the ranks that will need them.
Definition derivative.h:251
virtual void do_diff2i(const implT *f, implT *df, const keyT &key, const argT &left, const argT &center, const argT &right) const =0
const std::size_t axis
Axis along which the operation is performed.
Definition derivative.h:77
World & world
Definition derivative.h:76
virtual void do_diff2b(const implT *f, implT *df, const keyT &key, const argT &left, const argT &center, const argT &right) const =0
bool parallel_submit_
Spawn diff's per-node tasks from the task pool instead of the main thread.
Definition derivative.h:98
std::pair< keyT, coeffT > argT
Definition derivative.h:88
Implements derivatives operators with variety of boundary conditions on simulation domain.
Definition derivative.h:329
Tensor< double > right_r0t
Definition derivative.h:356
void set_ble2()
Definition derivative.h:703
Tensor< double > rmt
Definition derivative.h:352
Tensor< double > bv_left
Definition derivative.h:357
void set_bspline1()
Definition derivative.h:675
Tensor< double > r0
Definition derivative.h:351
Tensor< double > rp_bsp
Definition derivative.h:363
bool is_second
Definition derivative.h:347
Tensor< double > right_rp
Blocks of the derivative for the right boundary.
Definition derivative.h:355
void set_is_second()
Definition derivative.h:672
Derivative(World &world, std::size_t axis, const BoundaryConditions< NDIM > &bc=FunctionDefaults< NDIM >::get_bc(), const functionT g1=functionT(), const functionT g2=functionT(), int k=FunctionDefaults< NDIM >::get_k())
Constructs a derivative operator.
Definition derivative.h:651
Function< T, NDIM > functionT
Definition derivative.h:339
Tensor< double > r0t
Definition derivative.h:352
std::pair< keyT, coeffT > argT
Definition derivative.h:337
FunctionImpl< T, NDIM > implT
Definition derivative.h:338
Tensor< double > right_rpt
Blocks of the derivative for the right boundary.
Definition derivative.h:356
Tensor< double > left_rmt
Definition derivative.h:354
Tensor< double > rp_bsp_t
Definition derivative.h:366
virtual ~Derivative()
Definition derivative.h:669
GenTensor< T > coeffT
holding the node's coeffs (possibly low rank)
Definition derivative.h:335
const functionT g2
Function describing the boundary condition on the left side.
Definition derivative.h:345
void read_from_file(const std::string &filename, unsigned int order=1)
Definition derivative.h:710
Tensor< double > rp
Blocks of the derivative operator.
Definition derivative.h:351
void do_diff2i(const implT *f, implT *df, const keyT &key, const argT &left, const argT &center, const argT &right) const
Definition derivative.h:466
bool is_third
Definition derivative.h:348
void set_bspline3()
Definition derivative.h:689
Tensor< double > rm_bsp
Definition derivative.h:362
void set_bspline2()
Definition derivative.h:682
Tensor< double > rm
Definition derivative.h:351
void initCoefficients()
Definition derivative.h:507
Tensor< double > left_r0
Blocks of the derivative for the left boundary.
Definition derivative.h:353
Tensor< double > rpt
Blocks of the derivative operator, transposed.
Definition derivative.h:352
void do_diff2b(const implT *f, implT *df, const keyT &key, const argT &left, const argT &center, const argT &right) const
Definition derivative.h:368
void set_is_third()
Definition derivative.h:673
T opT
Definition derivative.h:641
Tensor< double > rm_bsp_t
Definition derivative.h:365
Tensor< double > left_r0t
Blocks of the derivative for the left boundary.
Definition derivative.h:354
Tensor< T > tensorT
Definition derivative.h:334
void set_is_first()
Definition derivative.h:671
FunctionNode< T, NDIM > nodeT
Definition derivative.h:341
Tensor< double > bv_right
Blocks of the derivative operator for the boundary contribution.
Definition derivative.h:357
Key< NDIM > keyT
Definition derivative.h:336
const functionT g1
Function describing the boundary condition on the right side.
Definition derivative.h:344
void set_ble1()
Definition derivative.h:696
Tensor< double > left_rm
Definition derivative.h:353
WorldContainer< Key< NDIM >, FunctionNode< T, NDIM > > dcT
Definition derivative.h:340
Tensor< double > r0_bsp
Definition derivative.h:361
Tensor< double > right_r0
Definition derivative.h:355
DerivativeBase< T, NDIM > baseT
Definition derivative.h:331
Tensor< double > r0_bsp_t
Definition derivative.h:364
FunctionDefaults holds default paramaters as static class members.
Definition funcdefaults.h:100
static int get_k()
Returns the default wavelet order.
Definition funcdefaults.h:164
static const Tensor< double > & get_rcell_width()
Returns the reciprocal of the width of each user cell dimension.
Definition funcdefaults.h:386
FunctionImpl holds all Function state to facilitate shallow copy semantics.
Definition funcimpl.h:968
World & world
Definition funcimpl.h:987
void sock_it_to_me(const keyT &key, const RemoteReference< FutureImpl< std::pair< keyT, coeffT > > > &ref) const
Walk up the tree returning pair(key,node) for first node with coefficients.
Definition mraimpl.h:2859
double get_thresh() const
Definition mraimpl.h:328
void receive_halo(const std::vector< std::pair< keyT, coeffT > > &buf) const
Insert pushed neighbor nodes into the halo; runs as a task, concurrently with other pushes.
Definition funcimpl.h:1050
TensorType get_tensor_type() const
Definition mraimpl.h:319
void do_diff1(const DerivativeBase< T, NDIM > *D, const implT *f, const keyT &key, const std::pair< keyT, coeffT > &left, const std::pair< keyT, coeffT > &center, const std::pair< keyT, coeffT > &right)
Definition mraimpl.h:943
const coeffT parent_to_child(const coeffT &s, const keyT &parent, const keyT &child) const
Directly project parent coeffs to child coeffs.
Definition mraimpl.h:3281
const dcT & get_coeffs() const
Definition mraimpl.h:343
FunctionNode holds the coefficients, etc., at each node of the 2^NDIM-tree.
Definition funcimpl.h:136
A multiresolution adaptive numerical function.
Definition mra.h:144
const std::shared_ptr< FunctionImpl< T, NDIM > > & get_impl() const
Returns a shared-pointer to the implementation.
Definition mra.h:724
const Function< T, NDIM > & reconstruct(bool fence=true) const
Reconstructs the function, transforming into scaling function basis. Possible non-blocking comm.
Definition mra.h:916
void set_impl(const std::shared_ptr< FunctionImpl< T, NDIM > > &impl)
Replace current FunctionImpl with provided new one.
Definition mra.h:731
A future is a possibly yet unevaluated value.
Definition future.h:370
remote_refT remote_ref(World &world) const
Returns a structure used to pass references to another process.
Definition future.h:672
Definition lowranktensor.h:59
Tensor< T > full_tensor_copy() const
Definition gentensor.h:206
Iterates in lexical order thru all children of a key.
Definition key.h:548
Key is the index for a node of the 2^NDIM-tree.
Definition key.h:70
Level level() const
Definition key.h:169
const Vector< Translation, NDIM > & translation() const
Definition key.h:174
static Key< NDIM > invalid()
Returns an invalid key.
Definition key.h:110
Range, vaguely a la Intel TBB, to encapsulate a random-access, STL-like start and end iterator with c...
Definition range.h:64
iteratorT iterator
Alias for the iterator type.
Definition range.h:71
static TaskAttributes hipri()
Definition thread.h:456
A tensor is a multidimensional array.
Definition tensor.h:318
A simple, fixed dimension vector.
Definition vector.h:64
Makes a distributed container with specified attributes.
Definition worlddc.h:1127
ProcessID owner(const keyT &key) const
Returns processor that logically owns key (no communication)
Definition worlddc.h:1321
void replace(const pairT &datum)
Inserts/replaces key+value pair (non-blocking communication if key not local)
Definition worlddc.h:1261
void fence(bool debug=false)
Synchronizes all processes in communicator AND globally ensures no pending AM or tasks.
Definition worldgop.cc:176
Implements most parts of a globally addressable object (via unique ID).
Definition world_object.h:366
const uniqueidT & id() const
Returns the globally unique object ID.
Definition world_object.h:713
void process_pending()
To be called from derived constructor to process pending messages.
Definition world_object.h:658
detail::task_result_type< memfnT >::futureT task(ProcessID dest, memfnT memfn, const TaskAttributes &attr=TaskAttributes()) const
Sends task to derived class method returnT (this->*memfn)().
Definition world_object.h:1007
void add(TaskInterface *t)
Add a new local task, taking ownership of the pointer.
Definition world_task_queue.h:466
A parallel world class.
Definition world.h:132
WorldTaskQueue & taskq
Task queue.
Definition world.h:206
ProcessID rank() const
Returns the process rank in this World (same as MPI_Comm_rank()).
Definition world.h:320
WorldGopInterface & gop
Global operations.
Definition world.h:207
char * p(char *buf, const char *name, int k, int initial_level, double thresh, int order)
Definition derivatives.cc:72
Provides FunctionDefaults and utilities for coordinate transformation.
Tensor< T > transpose(const Tensor< T > &t)
Returns a new deep copy of the transpose of the input tensor.
Definition tensor.h:2035
Multidimension Key for MRA tree and associated iterators.
static double pow(const double *a, const double *b)
Definition lda.h:74
#define MADNESS_CHECK(condition)
Check a condition — even in a release build the condition is always evaluated so it can have side eff...
Definition madness_exception.h:182
#define MADNESS_EXCEPTION(msg, value)
Macro for throwing a MADNESS exception.
Definition madness_exception.h:119
#define MADNESS_ASSERT(condition)
Assert a condition that should be free of side-effects since in release builds this might be a no-op.
Definition madness_exception.h:134
#define MADNESS_CHECK_THROW(condition, msg)
Check a condition — even in a release build the condition is always evaluated so it can have side eff...
Definition madness_exception.h:207
Header to declare stuff which has not yet found a home.
static const bool VERIFY_TREE
Definition mra.h:57
Namespace for all elements and tools of MADNESS.
Definition DFParameters.h:10
@ BC_DIRICHLET
Definition bc.h:54
@ BC_NEUMANN
Definition bc.h:56
@ BC_ZERO
Definition bc.h:51
@ BC_PERIODIC
Definition bc.h:52
@ BC_ZERONEUMANN
Definition bc.h:55
@ BC_FREE
Definition bc.h:53
static const char * filename
Definition legendre.cc:96
@ reconstructed
s coeffs at the leaves only
Definition funcdefaults.h:60
int64_t Translation
Definition key.h:58
std::vector< std::shared_ptr< Derivative< T, NDIM > > > gradient_operator(World &world, const BoundaryConditions< NDIM > &bc=FunctionDefaults< NDIM >::get_bc(), int k=FunctionDefaults< NDIM >::get_k())
Convenience function returning vector of derivative operators implementing grad ( )
Definition derivative.h:793
Derivative< T, NDIM > periodic_derivative(World &world, int axis, int k=FunctionDefaults< NDIM >::get_k())
Conveinence function returning derivative operator with periodic boundary conditions.
Definition derivative.h:775
int Level
Definition key.h:59
std::enable_if< std::is_base_of< ProjectorBase, projT >::value, OuterProjector< projT, projQ > >::type outer(const projT &p0, const projQ &p1)
Definition projector.h:457
std::string get_mra_data_dir()
Definition startup.cc:209
Derivative< T, NDIM > free_space_derivative(World &world, int axis, int k=FunctionDefaults< NDIM >::get_k())
Convenience function returning derivative operator with free-space boundary conditions.
Definition derivative.h:767
NDIM & f
Definition mra.h:2619
GenTensor< TENSOR_RESULT_TYPE(R, Q)> transform_dir(const GenTensor< R > &t, const Tensor< Q > &c, const int axis)
Definition lowranktensor.h:1106
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
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
static XNonlinearSolver< std::vector< Function< T, NDIM > >, T, vector_function_allocator< T, NDIM > > nonlinear_vector_solver(World &world, const long nvec)
Definition nonlinsol.h:371
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:2187
static const double d
Definition nonlinschro.cc:121
Defines simple templates for printing to std::cout "a la Python".
static const double c
Definition relops.cc:10
static const double m
Definition relops.cc:9
static const long k
Definition rk.cc:44
Definition test_ar.cc:204
Body of FunctionImpl::diff's submission loop, as a functor for taskq.for_each.
Definition derivative.h:289
submit_op(const DerivativeBase< T, NDIM > *D=nullptr, const implT *f=nullptr, implT *df=nullptr)
Definition derivative.h:294
Range< typename dcT::const_iterator > rangeT
Definition derivative.h:290
void serialize(const Archive &ar)
Definition derivative.h:310
const DerivativeBase< T, NDIM > * D
Definition derivative.h:291
const implT * f
Definition derivative.h:292
bool operator()(typename rangeT::iterator &it) const
Definition derivative.h:296
implT * df
Definition derivative.h:293
static void load(const Archive &ar, const DerivativeBase< T, NDIM > *&ptr)
Definition derivative.h:809
Default load of an object via serialize(ar, t).
Definition archive.h:667
static void store(const Archive &ar, const DerivativeBase< T, NDIM > *const &ptr)
Definition derivative.h:818
Default store of an object via serialize(ar, t).
Definition archive.h:612
Defines and implements most of Tensor.
constexpr std::size_t NDIM
Definition testgconv.cc:54
std::size_t axis
Definition testpdiff.cc:59
Implements WorldContainer.
int ProcessID
Used to clearly identify process number/rank.
Definition worldtypes.h:43