MADNESS 0.10.1
FunctionIOHDF5.h
Go to the documentation of this file.
1#include "ccpairfunction.h"
2#include "funcdefaults.h"
4#include <iostream>
5#include <madness/mra/mra.h>
6#include <h5cpp/h5cpp.hpp>
7
8using namespace madness;
9
10constexpr int simple_pow(int a, int b) {
11 if (b == 0) {
12 return 1;
13 } else {
14 int result = 1;
15 for (int i = 0; i < b; i++) {
16 result *= a;
17 }
18 return result;
19 }
20}
21
22template <typename T, std::size_t NDIM> class FunctionIO {
23
24private:
26 long ndims = NDIM;
28
29public:
30 static size_t count_leaf_nodes(const Function<T, NDIM> &f) {
31 const auto &coeffs = f.get_impl()->get_coeffs();
32 size_t count = 0;
33 for (auto it = coeffs.begin(); it != coeffs.end(); ++it) {
34 const auto &key = it->first;
35 const auto &node = it->second;
36 if (node.has_coeff()) {
37 count++;
38 }
39 }
40 f.get_impl()->world.gop.sum(count);
41 return count;
42 }
44 std::ostream &out, const Key<NDIM> &key) {
45 const auto &coeffs = f.get_impl()->get_coeffs();
46 auto it = coeffs.find(key).get();
47 if (it == coeffs.end()) {
48 for (int i = 0; i < key.level(); ++i)
49 out << " ";
50 out << key << " missing --> " << coeffs.owner(key) << "\n";
51 } else {
52 const auto &node = it->second;
53 if (node.has_coeff()) {
54 auto values = f.get_impl()->coeffs2values(key, node.coeff());
55 for (int i = 0; i < key.level(); ++i)
56 out << " ";
57 out << key.level() << " ";
58 for (int i = 0; i < NDIM; ++i)
59 out << key.translation()[i] << " ";
60 out << std::endl;
61 for (size_t i = 0; i < values.size(); i++)
62 out << values.ptr()[i] << " ";
63 out << std::endl;
64 }
65 if (node.has_children()) {
66 for (KeyChildIterator<NDIM> kit(key); kit; ++kit) {
67 write_function_coeffs(f, out, kit.key());
68 }
69 }
70 }
71 }
72 static void write_function(const Function<T, NDIM> &f, std::ostream &out) {
73 f.reconstruct();
74 std::cout << "NUMBER OF LEAF NODES: " << count_leaf_nodes(f) << std::endl;
75
76 auto flags = out.flags();
77 auto precision = out.precision();
78 out << std::setprecision(17);
79 out << std::scientific;
80
81 if (f.get_impl()->world.rank() == 0) {
82 out << NDIM << std::endl;
83 const auto &cell = FunctionDefaults<NDIM>::get_cell();
84 for (int d = 0; d < NDIM; ++d) {
85 for (int i = 0; i < 2; ++i)
86 out << cell(d, i) << " ";
87 out << std::endl;
88 }
89 out << f.k() << std::endl;
90 out << count_leaf_nodes(f) << std::endl;
91
93 }
94 f.get_impl()->world.gop.fence();
95
96 out << std::setprecision(precision);
97 out.setf(flags);
98 }
99
100 static void read_function_coeffs(Function<T, NDIM> &f, std::istream &in,
101 int num_leaf_nodes) {
102 auto &coeffs = f.get_impl()->get_coeffs();
103
104 for (int i = 0; i < num_leaf_nodes; i++) {
105 Level n;
107 long dims[NDIM];
108 in >> n;
109 if (in.eof())
110 break;
111
112 for (int i = 0; i < NDIM; ++i) {
113 in >> l[i];
114 dims[i] = f.k();
115 }
116 Key<NDIM> key(n, l);
117
118 Tensor<T> values(NDIM, dims);
119 for (size_t i = 0; i < values.size(); i++)
120 in >> values.ptr()[i];
121 auto t = f.get_impl()->values2coeffs(key, values);
122
123 // f.get_impl()->accumulate2(t, coeffs, key);
124 coeffs.task(key, &FunctionNode<T, NDIM>::accumulate2, t, coeffs, key);
125 }
126 }
127
128 static Function<T, NDIM> read_function(World &world, std::istream &in) {
129 size_t ndim;
130 in >> ndim;
131 MADNESS_CHECK(ndim == NDIM);
132
133 Tensor<double> cell(NDIM, 2);
134 for (int d = 0; d < NDIM; ++d) {
135 for (int i = 0; i < 2; ++i)
136 in >> cell(d, i);
137 }
139
140 int k;
141 in >> k;
142 int num_leaf_nodes;
143 in >> num_leaf_nodes;
144 FunctionFactory<T, NDIM> factory(world);
145 Function<T, NDIM> f(factory.k(k).empty());
146 world.gop.fence();
147
148 read_function_coeffs(f, in, num_leaf_nodes);
149
150 f.verify_tree();
151
152 return f;
153 }
154};
155
156template <typename T, std::size_t NDIM> struct FunctionIOData {
157
158 typedef Vector<double, NDIM> coordT; ///< Type of vector holding coordinates
159 typedef int Level;
160
161 long k = 0;
162 long npts_per_box = 0;
163 std::size_t ndim = NDIM;
164 std::array<std::pair<double, double>, NDIM> cell;
165 long num_leaf_nodes{};
166 std::vector<std::array<long, NDIM + 1>> nl;
167 std::vector<std::vector<double>> values;
168 std::vector<std::vector<coordT>> coords;
169
170 FunctionIOData() = default;
171
173
175
176 f.reconstruct();
177 if (f.get_impl()->world.rank() == 0) {
179 ndim = NDIM;
180 k = f.k();
181 const auto &cell_world = FunctionDefaults<NDIM>::get_cell();
182 for (int d = 0; d < NDIM; ++d) {
183 cell[d].first = cell_world(d, 0);
184 cell[d].second = cell_world(d, 1);
185 }
186
188 }
189 f.get_impl()->world.gop.fence();
190 }
191
193 const Key<NDIM> &key) {
196 const auto &coeffs = f.get_impl()->get_coeffs();
197 auto it = coeffs.find(key).get();
198 if (it == coeffs.end()) {
199 for (int i = 0; i < key.level(); ++i)
200 std::cout << " ";
201 std::cout << key << " missing --> " << coeffs.owner(key) << "\n";
202 } else {
203
204 auto cdata = f.get_impl()->get_cdata();
205
206 const Tensor<double> qx = cdata.quad_x;
207 const size_t npt = qx.dim(0);
208 const auto &node = it->second;
209 if (node.has_coeff()) {
210
211 const Level n = key.level();
212 const double h = std::pow(0.5, double(n));
213 coordT c; // will hold the point in user coordinates
214
215 auto node_values = f.get_impl()->coeffs2values(key, node.coeff());
216 std::array<long, NDIM + 1> key_i;
217 key_i[0] = key.level();
218 auto l = key.translation();
219 for (int i = 0; i < NDIM; ++i)
220 key_i[i + 1] = key.translation()[i];
221
222 nl.push_back(key_i);
223 coords.push_back(std::vector<coordT>());
224
225 if (NDIM == 3) {
226 for (size_t i = 0; i < k; ++i) {
227 c[0] = cell(0, 0) + h * cell_width[0] * (l[0] + qx(i)); // x
228 for (size_t j = 0; j < k; ++j) {
229 c[1] = cell(1, 0) + h * cell_width[1] * (l[1] + qx(j)); // y
230 for (size_t m = 0; m < k; ++m) {
231 c[2] = cell(2, 0) + h * cell_width[2] * (l[2] + qx(m)); // z
232 coords.back().push_back(c);
233 }
234 }
235 }
236 } else {
237 MADNESS_EXCEPTION("only NDIM=3 in print_grid", 0);
238 }
239
240 std::vector<double> values_i(npts_per_box);
241 std::copy(node_values.ptr(), node_values.ptr() + npts_per_box,
242 values_i.begin());
243 values.push_back(values_i);
244 }
245 if (node.has_children()) {
246 for (KeyChildIterator<NDIM> kit(key); kit; ++kit) {
247 initialize_func_coeffs(f, kit.key());
248 }
249 }
250 }
251 }
253 auto &coeffs = f.get_impl()->get_coeffs();
254
255 for (int i = 0; i < num_leaf_nodes; i++) {
257 long dims[NDIM];
258
259 for (int i = 0; i < NDIM; ++i) {
260 dims[i] = f.k();
261 }
262
263 auto n = nl[i][0];
264 for (int j = 0; j < NDIM; ++j) {
265 l[j] = nl[i][j + 1];
266 }
267 Key<NDIM> key(n, l);
268
269 Tensor<T> values(NDIM, dims);
270 std::copy(this->values[i].begin(), this->values[i].end(), values.ptr());
271 auto t = f.get_impl()->values2coeffs(key, values);
272
273 // f.get_impl()->accumulate2(t, coeffs, key);
274 coeffs.task(key, &FunctionNode<T, NDIM>::accumulate2, t, coeffs, key);
275 }
276 }
277
279
280 size_t ndim = this->ndim;
281 MADNESS_CHECK(ndim == NDIM);
282 Tensor<double> cell_t(NDIM, 2);
283 for (int d = 0; d < NDIM; ++d) {
284 cell_t(d, 0) = cell[d].first;
285 cell_t(d, 1) = cell[d].second;
286 }
287
289
290 FunctionFactory<T, NDIM> factory(world);
291 Function<T, NDIM> f(factory.k(k).empty());
292 world.gop.fence();
293
295
296 f.verify_tree();
297
298 return f;
299 }
300};
301
302template <typename T, std::size_t NDIM>
304 j = json{{"npts_per_box", p.npts_per_box},
305 {"k", p.k},
306 {"cell", p.cell},
307 {"num_leaf_nodes", p.num_leaf_nodes},
308 {"coords", p.coords},
309 {"nl", p.nl},
310 {"ndim", p.ndim},
311 {"values", p.values}};
312}
313
314template <typename T, std::size_t NDIM>
316 j.at("npts_per_box").get_to(p.npts_per_box);
317 j.at("k").get_to(p.k);
318 j.at("cell").get_to(p.cell);
319 j.at("num_leaf_nodes").get_to(p.num_leaf_nodes);
320 j.at("nl").get_to(p.nl);
321 j.at("values").get_to(p.values);
322 j.at("ndim").get_to(p.ndim);
323}
constexpr int simple_pow(int a, int b)
Definition FunctionIOHDF5.h:10
Definition FunctionIO.h:20
static void write_function(const Function< T, NDIM > &f, std::ostream &out)
Definition FunctionIOHDF5.h:72
static size_t count_leaf_nodes(const Function< T, NDIM > &f)
Definition FunctionIOHDF5.h:30
static void write_function_coeffs(const Function< T, NDIM > &f, std::ostream &out, const Key< NDIM > &key)
Definition FunctionIOHDF5.h:43
long npts_per_box
Definition FunctionIO.h:25
long k
Definition FunctionIO.h:23
static void read_function_coeffs(Function< T, NDIM > &f, std::istream &in, int num_leaf_nodes)
Definition FunctionIOHDF5.h:100
static Function< T, NDIM > read_function(World &world, std::istream &in)
Definition FunctionIOHDF5.h:128
long ndims
Definition FunctionIO.h:24
long dim(int i) const
Returns the size of dimension i.
Definition basetensor.h:147
long size() const
Returns the number of elements in the tensor.
Definition basetensor.h:138
static int get_k()
Returns the default wavelet order.
Definition funcdefaults.h:163
static const Tensor< double > & get_cell_width()
Returns the width of each user cell dimension.
Definition funcdefaults.h:369
static void set_cell(const Tensor< double > &value)
Gets the user cell for the simulation.
Definition funcdefaults.h:354
static const Tensor< double > & get_cell()
Gets the user cell for the simulation.
Definition funcdefaults.h:347
FunctionFactory implements the named-parameter idiom for Function.
Definition function_factory.h:86
virtual FunctionFactory & k(int k)
Definition function_factory.h:193
FunctionFactory & empty()
Definition function_factory.h:246
FunctionNode holds the coefficients, etc., at each node of the 2^NDIM-tree.
Definition funcimpl.h:127
A multiresolution adaptive numerical function.
Definition mra.h:139
Iterates in lexical order thru all children of a key.
Definition key.h:466
Key is the index for a node of the 2^NDIM-tree.
Definition key.h:69
Level level() const
Definition key.h:168
const Vector< Translation, NDIM > & translation() const
Definition key.h:173
A tensor is a multidimensional array.
Definition tensor.h:317
T * ptr()
Returns a pointer to the internal data.
Definition tensor.h:1825
A simple, fixed dimension vector.
Definition vector.h:64
void fence(bool debug=false)
Synchronizes all processes in communicator AND globally ensures no pending AM or tasks.
Definition worldgop.cc:161
A parallel world class.
Definition world.h:132
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.
#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
Main include file for MADNESS and defines Function interface.
Namespace for all elements and tools of MADNESS.
Definition DFParameters.h:10
int Level
Definition key.h:58
NDIM & f
Definition mra.h:2481
void to_json(nlohmann::json &j)
void from_json(const nlohmann::json &, ResponseParameters &p)
Definition response_parameters.cpp:8
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 double c
Definition relops.cc:10
static const double m
Definition relops.cc:9
nlohmann::json json
Definition response_parameters.cpp:6
Definition FunctionIO.h:158
std::vector< std::array< long, NDIM+1 > > nl
Definition FunctionIO.h:165
FunctionIOData(const Function< T, NDIM > &f)
Definition FunctionIOHDF5.h:172
long npts_per_box
Definition FunctionIO.h:161
int Level
Definition FunctionIOHDF5.h:159
std::array< std::pair< double, double >, NDIM > cell
Definition FunctionIO.h:163
std::vector< std::vector< coordT > > coords
Definition FunctionIO2.h:171
Vector< double, NDIM > coordT
Type of vector holding coordinates.
Definition FunctionIOHDF5.h:158
FunctionIOData()=default
Function< T, NDIM > create_function(World &world)
Definition FunctionIOHDF5.h:278
std::size_t ndim
Definition FunctionIO.h:162
long num_leaf_nodes
Definition FunctionIO.h:164
long k
Definition FunctionIO.h:160
void set_function_coeffs(Function< T, NDIM > &f, int num_leaf_nodes)
Definition FunctionIOHDF5.h:252
void initialize_func_coeffs(const Function< T, NDIM > &f, const Key< NDIM > &key)
Definition FunctionIO.h:190
std::vector< std::vector< double > > values
Definition FunctionIO.h:166
constexpr std::size_t NDIM
Definition testgconv.cc:54
double h(const coord_1d &r)
Definition testgconv.cc:175