MADNESS 0.10.1
supbound.h
Go to the documentation of this file.
1#ifndef MADNESS_MRA_SUPBOUND_H__INCLUDED
2#define MADNESS_MRA_SUPBOUND_H__INCLUDED
3
4/// \file supbound.h
5/// \brief Bounds on the largest magnitude of a function inside a box, from its scaling coefficients.
6///
7/// Inside the box (n, l) a function with scaling coefficients c is, in user coordinates,
8///
9/// f(x) = 2^{NDIM n/2} V^{-1/2} sum_i c_i prod_d phi_{i_d}(2^n x_d - l_d)
10///
11/// (x in simulation coordinates, V the cell volume; FunctionCommonData::coeffs2values), and the normalized
12/// Legendre scaling functions phi_i(t) = sqrt(2i+1) P_i(2t-1) peak at sqrt(2i+1) on the box faces. Hence
13///
14/// sup_box |f| <= 2^{NDIM n/2} V^{-1/2} sum_i |c_i| prod_d sqrt(2 i_d + 1),
15///
16/// rigorously, at k^NDIM multiply-adds per box. The bound lies above the box's rms magnitude
17/// ||c|| / sqrt(V_box) by a factor between 1 (a constant) and k^NDIM; for a function that is smooth across the
18/// box it is close to 1, and it is the larger the more f varies inside the box.
19///
20/// SupBoundMap holds that bound for the boxes of a tree: a leaf's from its coefficients, an interior node's as
21/// the max over its children (the sup over a union is the max of the sups), and a box below a leaf takes the
22/// leaf's (the sup over a sub-box is at most the sup over the box). An interior node's own coefficients (a
23/// redundant tree) are a projection of f and bound nothing, so only the leaves' coefficients are read.
24/// FunctionImpl::sup_bound_map() builds the map for a function.
25
26#include <madness/mra/key.h>
30
31#include <cmath>
32#include <unordered_map>
33
34namespace madness {
35
36 /// Bounds on sup|f| per box of a function's tree (supbound.h)
37 template <std::size_t NDIM>
39 public:
40 typedef std::unordered_map<Key<NDIM>, double, Hash<Key<NDIM>>> mapT;
41
42 /// the answer for a box the map holds no bound for
43 static constexpr double unbounded() { return 1e300; }
44
45 /// the weights of the leaf bound, prod_d sqrt(2 i_d + 1), for k coefficients per dimension
46 static Tensor<double> leaf_weights(const int k) {
47 Tensor<double> w1(k);
48 for (int i = 0; i < k; ++i) w1(i) = std::sqrt(2.0 * i + 1.0);
49 Tensor<double> w = w1;
50 for (std::size_t d = 1; d < NDIM; ++d) w = outer(w, w1);
51 return w;
52 }
53
54 /// A bound on sup|f| over the box `key`, from f's scaling coefficients `c` there and the weights of
55 /// leaf_weights(k). No coefficients means f vanishes in the box.
56 template <typename T>
57 static double leaf_bound(const Key<NDIM>& key, const Tensor<T>& c, const Tensor<double>& weights) {
58 if (c.size() == 0) return 0.0;
59 MADNESS_CHECK_THROW(c.size() == weights.size(), "SupBoundMap: coefficients and weights differ in size");
60 const Tensor<T> cc = c.iscontiguous() ? c : copy(c);
61 const T* p = cc.ptr();
62 const double* w = weights.ptr();
63 double s = 0.0;
64 for (long i = 0; i < cc.size(); ++i) s += std::abs(p[i]) * w[i];
65 return s * std::pow(2.0, 0.5 * NDIM * key.level()) / std::sqrt(FunctionDefaults<NDIM>::get_cell_volume());
66 }
67
68 SupBoundMap() = default;
69 explicit SupBoundMap(mapT bound) : bound_(std::move(bound)) {}
70
71 /// The bound for the box `key`: its own, or the leaf's it lies below; unbounded() for a box the map
72 /// holds neither for. `below_leaf`, if given, is set when the answer is a leaf's above `key`.
73 double operator()(Key<NDIM> key, bool* below_leaf = nullptr) const {
74 for (bool below = false;; below = true) {
75 const auto it = bound_.find(key);
76 if (it != bound_.end()) {
77 if (below_leaf) *below_leaf = below;
78 return it->second;
79 }
80 if (key.level() == 0) return unbounded();
81 key = key.parent();
82 }
83 }
84
85 /// the number of boxes held
86 std::size_t size() const { return bound_.size(); }
87
88 private:
90 };
91
92} // namespace madness
93
94#endif // MADNESS_MRA_SUPBOUND_H__INCLUDED
double w(double t, double eps)
Definition DKops.h:22
static double get_cell_volume()
Returns the volume of the user cell.
Definition funcdefaults.h:410
Key is the index for a node of the 2^NDIM-tree.
Definition key.h:71
Level level() const
Definition key.h:170
Key parent(int generation=1) const
Returns the key of the parent.
Definition key.h:366
static Tensor< double > leaf_weights(const int k)
the weights of the leaf bound, prod_d sqrt(2 i_d + 1), for k coefficients per dimension
Definition supbound.h:46
static constexpr double unbounded()
the answer for a box the map holds no bound for
Definition supbound.h:43
SupBoundMap(mapT bound)
Definition supbound.h:69
static double leaf_bound(const Key< NDIM > &key, const Tensor< T > &c, const Tensor< double > &weights)
Definition supbound.h:57
double operator()(Key< NDIM > key, bool *below_leaf=nullptr) const
Definition supbound.h:73
std::unordered_map< Key< NDIM >, double, Hash< Key< NDIM > > > mapT
Definition supbound.h:40
std::size_t size() const
the number of boxes held
Definition supbound.h:86
mapT bound_
Definition supbound.h:89
A tensor is a multidimensional array.
Definition tensor.h:313
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.
Multidimension Key for MRA tree and associated iterators.
#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
Namespace for all elements and tools of MADNESS.
Definition DFConvergence.h:9
int FunctionDefaults< NDIM >::k
Definition mraimpl.h:3878
static Tensor< double > weights[max_npt+1]
Definition legendre.cc:99
std::enable_if< std::is_base_of< ProjectorBase, projT >::value, OuterProjector< projT, projQ > >::type outer(const projT &p0, const projQ &p1)
Definition projector.h:476
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:2239
Definition mraimpl.h:53
static long abs(long a)
Definition tensor.h:214
const double cc
Definition navstokes_cosines.cc:107
static const double d
Definition nonlinschro.cc:121
static const double c
Definition relops.cc:10
Hash functor.
Definition worldhash.h:234
Defines and implements most of Tensor.
constexpr std::size_t NDIM
Definition testgconv.cc:54
Defines hash functions for use in distributed containers.