|
| 1 | +#include <complex> |
| 2 | +#include <iomanip> |
| 3 | +#include <iostream> |
| 4 | +#include <madness/mra/mra.h> |
| 5 | +#include <memory> |
| 6 | + |
| 7 | +using namespace madness; |
| 8 | + |
| 9 | +static const size_t D = 2; |
| 10 | +typedef Vector<double, D> coordT; |
| 11 | +typedef Key<D> keyT; |
| 12 | +typedef double dataT;// was std::complex<double> |
| 13 | +typedef std::shared_ptr<FunctionFunctorInterface<dataT, D>> functorT; |
| 14 | +typedef Function<dataT, D> functionT; |
| 15 | +typedef FunctionFactory<dataT, D> factoryT; |
| 16 | +typedef SeparatedConvolution<dataT, D> operatorT; |
| 17 | + |
| 18 | +static const double L = 4.0; |
| 19 | +static const long k = 5; // wavelet order |
| 20 | +static const double thresh = 1e-3;// precision |
| 21 | + |
| 22 | +static dataT f(const coordT &r) { |
| 23 | + double R = r.normf(); |
| 24 | + return std::exp(-R * R); |
| 25 | +} |
| 26 | + |
| 27 | +template<typename T, std::size_t NDIM> |
| 28 | +void write_function_coeffs(const Function<T, NDIM> &f, std::ostream &out, const Key<NDIM> &key) { |
| 29 | + const auto &coeffs = f.get_impl()->get_coeffs(); |
| 30 | + auto it = coeffs.find(key).get(); |
| 31 | + if (it == coeffs.end()) { |
| 32 | + for (int i = 0; i < key.level(); ++i) out << " "; |
| 33 | + out << key << " missing --> " << coeffs.owner(key) << "\n"; |
| 34 | + } else { |
| 35 | + const auto &node = it->second; |
| 36 | + if (node.has_coeff()) { |
| 37 | + auto values = f.get_impl()->coeffs2values(key, node.coeff()); |
| 38 | + for (int i = 0; i < key.level(); ++i) out << " "; |
| 39 | + out << key.level() << " "; |
| 40 | + for (int i = 0; i < NDIM; ++i) out << key.translation()[i] << " "; |
| 41 | + out << std::endl; |
| 42 | + for (size_t i = 0; i < values.size(); i++) out << values.ptr()[i] << " "; |
| 43 | + out << std::endl; |
| 44 | + } |
| 45 | + if (node.has_children()) { |
| 46 | + for (KeyChildIterator<NDIM> kit(key); kit; ++kit) { write_function_coeffs<T, NDIM>(f, out, kit.key()); } |
| 47 | + } |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +template<typename T, std::size_t NDIM> |
| 52 | +size_t count_leaf_nodes(const Function<T, NDIM> &f) { |
| 53 | + const auto &coeffs = f.get_impl()->get_coeffs(); |
| 54 | + size_t count = 0; |
| 55 | + for (auto it = coeffs.begin(); it != coeffs.end(); ++it) { |
| 56 | + const auto &key = it->first; |
| 57 | + const auto &node = it->second; |
| 58 | + if (node.has_coeff()) { count++; } |
| 59 | + } |
| 60 | + f.get_impl()->world.gop.sum(count); |
| 61 | + return count; |
| 62 | +} |
| 63 | + |
| 64 | +template<typename T, std::size_t NDIM> |
| 65 | +void write_function(const Function<T, NDIM> &f, std::ostream &out) { |
| 66 | + f.reconstruct(); |
| 67 | + std::cout << "NUMBER OF LEAF NODES: " << count_leaf_nodes(f) << std::endl; |
| 68 | + |
| 69 | + auto flags = out.flags(); |
| 70 | + auto precision = out.precision(); |
| 71 | + out << std::setprecision(17); |
| 72 | + out << std::scientific; |
| 73 | + |
| 74 | + if (f.get_impl()->world.rank() == 0) { |
| 75 | + out << NDIM << std::endl; |
| 76 | + const auto &cell = FunctionDefaults<NDIM>::get_cell(); |
| 77 | + for (int d = 0; d < NDIM; ++d) { |
| 78 | + for (int i = 0; i < 2; ++i) out << cell(d, i) << " "; |
| 79 | + out << std::endl; |
| 80 | + } |
| 81 | + out << f.k() << std::endl; |
| 82 | + out << count_leaf_nodes(f) << std::endl; |
| 83 | + |
| 84 | + write_function_coeffs(f, out, Key<NDIM>(0)); |
| 85 | + } |
| 86 | + f.get_impl()->world.gop.fence(); |
| 87 | + |
| 88 | + out << std::setprecision(precision); |
| 89 | + out.setf(flags); |
| 90 | +} |
| 91 | + |
| 92 | +template<typename T, std::size_t NDIM> |
| 93 | +void read_function_coeffs(Function<T, NDIM> &f, std::istream &in) { |
| 94 | + auto &coeffs = f.get_impl()->get_coeffs(); |
| 95 | + |
| 96 | + while (true) { |
| 97 | + Level n; |
| 98 | + Vector<Translation, NDIM> l; |
| 99 | + long dims[NDIM]; |
| 100 | + in >> n; |
| 101 | + if (in.eof()) break; |
| 102 | + |
| 103 | + for (int i = 0; i < NDIM; ++i) { |
| 104 | + in >> l[i]; |
| 105 | + dims[i] = f.k(); |
| 106 | + } |
| 107 | + Key<NDIM> key(n, l); |
| 108 | + |
| 109 | + Tensor<T> values(NDIM, dims); |
| 110 | + for (size_t i = 0; i < values.size(); i++) in >> values.ptr()[i]; |
| 111 | + auto t = f.get_impl()->values2coeffs(key, values); |
| 112 | + |
| 113 | + // f.get_impl()->accumulate2(t, coeffs, key); |
| 114 | + coeffs.task(key, &FunctionNode<T, NDIM>::accumulate2, t, coeffs, key); |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +template<typename T, std::size_t NDIM> |
| 119 | +Function<T, NDIM> read_function(World &world, std::istream &in) { |
| 120 | + size_t ndim; |
| 121 | + in >> ndim; |
| 122 | + MADNESS_CHECK(ndim == NDIM); |
| 123 | + |
| 124 | + Tensor<double> cell(NDIM, 2); |
| 125 | + for (int d = 0; d < NDIM; ++d) { |
| 126 | + for (int i = 0; i < 2; ++i) in >> cell(d, i); |
| 127 | + } |
| 128 | + FunctionDefaults<NDIM>::set_cell(cell); |
| 129 | + |
| 130 | + int k; |
| 131 | + in >> k; |
| 132 | + FunctionFactory<T, NDIM> factory(world); |
| 133 | + Function<T, NDIM> f(factory.k(k).empty()); |
| 134 | + world.gop.fence(); |
| 135 | + |
| 136 | + read_function_coeffs(f, in); |
| 137 | + |
| 138 | + f.verify_tree(); |
| 139 | + |
| 140 | + return f; |
| 141 | +} |
| 142 | + |
| 143 | +void test(World &world) { |
| 144 | + functionT fun = factoryT(world).f(f); |
| 145 | + fun.truncate(); |
| 146 | + |
| 147 | + { |
| 148 | + double norm = fun.norm2(); |
| 149 | + if (world.rank() == 0) std::cout << "norm = " << norm << std::endl; |
| 150 | + std::ofstream out("fun.dat", std::ios::out); |
| 151 | + write_function(fun, out); |
| 152 | + out.close(); |
| 153 | + // fun.print_tree(); |
| 154 | + } |
| 155 | + |
| 156 | + { |
| 157 | + std::ifstream in("fun.dat", std::ios::in); |
| 158 | + functionT fun2 = read_function<dataT, D>(world, in); |
| 159 | + double norm = fun2.norm2(); |
| 160 | + if (world.rank() == 0) std::cout << "norm = " << norm << std::endl; |
| 161 | + // write_function(fun2,std::cout); |
| 162 | + // fun2.print_tree(); |
| 163 | + double err = (fun - fun2).norm2(); |
| 164 | + if (world.rank() == 0) std::cout << "error = " << err << std::endl; |
| 165 | + } |
| 166 | +} |
| 167 | + |
| 168 | +int main(int argc, char **argv) { |
| 169 | + World &world = initialize(argc, argv); |
| 170 | + startup(world, argc, argv); |
| 171 | + std::cout.precision(6); |
| 172 | + |
| 173 | + FunctionDefaults<D>::set_k(k); |
| 174 | + FunctionDefaults<D>::set_thresh(thresh); |
| 175 | + FunctionDefaults<D>::set_refine(true); |
| 176 | + FunctionDefaults<D>::set_initial_level(2); |
| 177 | + FunctionDefaults<D>::set_truncate_mode(0); |
| 178 | + FunctionDefaults<D>::set_cubic_cell(-L / 2, L / 2); |
| 179 | + |
| 180 | + test(world); |
| 181 | + |
| 182 | + world.gop.fence(); |
| 183 | + finalize(); |
| 184 | + return 0; |
| 185 | +} |
0 commit comments