MADNESS 0.10.1
FunctionIO2.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
7using namespace madness;
8
9constexpr int simple_pow(int a, int b) {
10 if (b == 0) {
11 return 1;
12 } else {
13 int result = 1;
14 for (int i = 0; i < b; i++) {
15 result *= a;
16 }
17 return result;
18 }
19}
20
21template <typename T, std::size_t NDIM> class FunctionIO {
22
23private:
25 long ndims = NDIM;
27
28public:
29 static size_t count_leaf_nodes(const Function<T, NDIM> &f) {
30 const auto &coeffs = f.get_impl()->get_coeffs();
31 size_t count = 0;
32 for (auto it = coeffs.begin(); it != coeffs.end(); ++it) {
33 const auto &key = it->first;
34 const auto &node = it->second;
35 if (node.has_coeff()) {
36 count++;
37 }
38 }
39 f.get_impl()->world.gop.sum(count);
40 return count;
41 }
43 std::ostream &out, const Key<NDIM> &key) {
44 const auto &coeffs = f.get_impl()->get_coeffs();
45 auto it = coeffs.find(key).get();
46 if (it == coeffs.end()) {
47 for (int i = 0; i < key.level(); ++i)
48 out << " ";
49 out << key << " missing --> " << coeffs.owner(key) << "\n";
50 } else {
51 const auto &node = it->second;
52 if (node.has_coeff()) {
53 auto values = f.get_impl()->coeffs2values(key, node.coeff());
54 for (int i = 0; i < key.level(); ++i)
55 out << " ";
56 out << key.level() << " ";
57 for (int i = 0; i < NDIM; ++i)
58 out << key.translation()[i] << " ";
59 out << std::endl;
60#if HAVE_GENTENSOR
61 MADNESS_EXCEPTION("FunctionIO not implemented for GenTensor", 0);
62#else
63 for (size_t i = 0; i < values.size(); i++)
64 out << values.ptr()[i] << " ";
65#endif
66 out << std::endl;
67 }
68 if (node.has_children()) {
69 for (KeyChildIterator<NDIM> kit(key); kit; ++kit) {
70 write_function_coeffs(f, out, kit.key());
71 }
72 }
73 }
74 }
75 static void write_function(const Function<T, NDIM> &f, std::ostream &out) {
76 f.reconstruct();
77 std::cout << "NUMBER OF LEAF NODES: " << count_leaf_nodes(f) << std::endl;
78
79 auto flags = out.flags();
80 auto precision = out.precision();
81 out << std::setprecision(17);
82 out << std::scientific;
83
84 if (f.get_impl()->world.rank() == 0) {
85 out << NDIM << std::endl;
86 const auto &cell = FunctionDefaults<NDIM>::get_cell();
87 for (int d = 0; d < NDIM; ++d) {
88 for (int i = 0; i < 2; ++i)
89 out << cell(d, i) << " ";
90 out << std::endl;
91 }
92 out << f.k() << std::endl;
93 out << count_leaf_nodes(f) << std::endl;
94
96 }
97 f.get_impl()->world.gop.fence();
98
99 out << std::setprecision(precision);
100 out.setf(flags);
101 }
102
103 static void read_function_coeffs(Function<T, NDIM> &f, std::istream &in,
104 int num_leaf_nodes) {
105 auto &coeffs = f.get_impl()->get_coeffs();
106
107 for (int i = 0; i < num_leaf_nodes; i++) {
108 Level n;
110 long dims[NDIM];
111 in >> n;
112 if (in.eof())
113 break;
114
115 for (int i = 0; i < NDIM; ++i) {
116 in >> l[i];
117 dims[i] = f.k();
118 }
119 Key<NDIM> key(n, l);
120
121 Tensor<T> values(NDIM, dims);
122 for (size_t i = 0; i < values.size(); i++)
123 in >> values.ptr()[i];
124 auto t = f.get_impl()->values2coeffs(key, values);
125
126 // f.get_impl()->accumulate2(t, coeffs, key);
127 coeffs.task(key, &FunctionNode<T, NDIM>::accumulate2, t, coeffs, key);
128 }
129 }
130
131 static Function<T, NDIM> read_function(World &world, std::istream &in) {
132 size_t ndim;
133 in >> ndim;
134 MADNESS_CHECK(ndim == NDIM);
135
136 Tensor<double> cell(NDIM, 2);
137 for (int d = 0; d < NDIM; ++d) {
138 for (int i = 0; i < 2; ++i)
139 in >> cell(d, i);
140 }
142
143 int k;
144 in >> k;
145 int num_leaf_nodes;
146 in >> num_leaf_nodes;
147 FunctionFactory<T, NDIM> factory(world);
148 Function<T, NDIM> f(factory.k(k).empty());
149 world.gop.fence();
150
151 read_function_coeffs(f, in, num_leaf_nodes);
152
153 f.verify_tree();
154
155 return f;
156 }
157};
158
159template <typename T, std::size_t NDIM> struct FunctionIOData {
160
161 typedef Vector<double, NDIM> coordT; ///< Type of vector holding coordinates
162 typedef int Level;
163
164 long k = 0;
165 long npts_per_box = 0;
166 std::size_t ndim = NDIM;
167 std::array<std::pair<double, double>, NDIM> cell;
168 long num_leaf_nodes{};
169 std::vector<std::array<long, NDIM + 1>> nl;
170 std::vector<std::vector<double>> values;
171 std::vector<std::vector<coordT>> coords;
172
173 FunctionIOData() = default;
174
176
178
179 f.reconstruct();
180 if (f.get_impl()->world.rank() == 0) {
182 ndim = NDIM;
183 k = f.k();
184 const auto &cell_world = FunctionDefaults<NDIM>::get_cell();
185 for (int d = 0; d < NDIM; ++d) {
186 cell[d].first = cell_world(d, 0);
187 cell[d].second = cell_world(d, 1);
188 }
189
191 }
192 f.get_impl()->world.gop.fence();
193 }
194
195 // write function coordinates for dim 1, 2, 3
197 const Key<NDIM> &key) {
200 const auto &coeffs = f.get_impl()->get_coeffs();
201 auto it = coeffs.find(key).get();
202 if (it == coeffs.end()) {
203 for (int i = 0; i < key.level(); ++i)
204 std::cout << " ";
205 std::cout << key << " missing --> " << coeffs.owner(key) << "\n";
206 } else {
207
208 auto cdata = f.get_impl()->get_cdata();
209
210 const Tensor<double> qx = cdata.quad_x;
211 const size_t npt = qx.dim(0);
212 const auto &node = it->second;
213 if (node.has_coeff()) {
214
215 const Level n = key.level();
216 const double h = std::pow(0.5, double(n));
217 coordT c; // will hold the point in user coordinates
218
219 auto node_values = f.get_impl()->coeffs2values(key, node.coeff());
220 std::array<long, NDIM + 1> key_i;
221 key_i[0] = key.level();
222 auto l = key.translation();
223 for (int i = 0; i < NDIM; ++i)
224 key_i[i + 1] = key.translation()[i];
225
226 nl.push_back(key_i);
227 coords.push_back(std::vector<coordT>());
228
229 if (NDIM == 1) {
230 for (long i = 0; i < k; ++i) {
231 c[0] = cell(0, 0) + h * cell_width[0] * (l[0] + qx(i)); // x
232 coords.back().push_back(c);
233 }
234 } else if (NDIM == 2) {
235 for (long i = 0; i < k; ++i) {
236 c[0] = cell(0, 0) + h * cell_width[0] * (l[0] + qx(i)); // x
237 for (long j = 0; j < k; ++j) {
238 c[1] = cell(1, 0) + h * cell_width[1] * (l[1] + qx(j)); // y
239 coords.back().push_back(c);
240 }
241 }
242 } else if (NDIM == 3) {
243 for (long j = 0; j < k; ++j) {
244 c[1] = cell(1, 0) + h * cell_width[1] * (l[1] + qx(j)); // y
245 for (long m = 0; m < k; ++m) {
246 c[2] = cell(2, 0) + h * cell_width[2] * (l[2] + qx(m)); // z
247 coords.back().push_back(c);
248 }
249 }
250 } else {
251 MADNESS_EXCEPTION("only NDIM <= 3 in print_grid", 0);
252 }
253
254 std::vector<double> values_i(npts_per_box);
255#if HAVE_GENTENSOR
256 MADNESS_EXCEPTION("FunctionIO not implemented for GenTensor", 0);
257#else
258 std::copy(node_values.ptr(), node_values.ptr() + npts_per_box,
259 values_i.begin());
260#endif
261 values.push_back(values_i);
262 }
263 if (node.has_children()) {
264 for (KeyChildIterator<NDIM> kit(key); kit; ++kit) {
265 initialize_func_coeffs(f, kit.key());
266 }
267 }
268 }
269 }
271 auto &coeffs = f.get_impl()->get_coeffs();
272
273 for (int i = 0; i < num_leaf_nodes; i++) {
275 long dims[NDIM];
276
277 for (int i = 0; i < NDIM; ++i) {
278 dims[i] = f.k();
279 }
280
281 auto n = nl[i][0];
282 for (int j = 0; j < NDIM; ++j) {
283 l[j] = nl[i][j + 1];
284 }
285 Key<NDIM> key(n, l);
286
287 Tensor<T> values(NDIM, dims);
288 std::copy(this->values[i].begin(), this->values[i].end(), values.ptr());
289 auto t = f.get_impl()->values2coeffs(key, values);
290
291 // f.get_impl()->accumulate2(t, coeffs, key);
292 coeffs.task(key, &FunctionNode<T, NDIM>::accumulate2, t, coeffs, key);
293 }
294 }
295
297
298 size_t ndim = this->ndim;
299 MADNESS_CHECK(ndim == NDIM);
300 Tensor<double> cell_t(NDIM, 2);
301 for (int d = 0; d < NDIM; ++d) {
302 cell_t(d, 0) = cell[d].first;
303 cell_t(d, 1) = cell[d].second;
304 }
305
307
308 FunctionFactory<T, NDIM> factory(world);
309 Function<T, NDIM> f(factory.k(k).empty());
310 world.gop.fence();
311
313
314 f.verify_tree();
315
316 return f;
317 }
318};
319
320template <typename T, std::size_t NDIM>
322 j = json{{"npts_per_box", p.npts_per_box},
323 {"num_leaf_nodes", p.num_leaf_nodes},
324 {"k", p.k},
325 {"cell", p.cell},
326 {"nl", p.nl},
327 {"ndim", p.ndim},
328 {"coords", p.coords},
329 {"values", p.values}};
330}
331
332template <typename T, std::size_t NDIM>
334 j.at("npts_per_box").get_to(p.npts_per_box);
335 j.at("k").get_to(p.k);
336 j.at("cell").get_to(p.cell);
337 j.at("num_leaf_nodes").get_to(p.num_leaf_nodes);
338 j.at("nl").get_to(p.nl);
339 j.at("values").get_to(p.values);
340 j.at("ndim").get_to(p.ndim);
341}
constexpr int simple_pow(int a, int b)
Definition FunctionIO2.h:9
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 FunctionIO2.h:75
static size_t count_leaf_nodes(const Function< T, NDIM > &f)
Definition FunctionIO2.h:29
static void write_function_coeffs(const Function< T, NDIM > &f, std::ostream &out, const Key< NDIM > &key)
Definition FunctionIO2.h:42
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 FunctionIO2.h:103
static Function< T, NDIM > read_function(World &world, std::istream &in)
Definition FunctionIO2.h:131
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 FunctionIO2.h:175
long npts_per_box
Definition FunctionIO.h:161
int Level
Definition FunctionIO2.h:162
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 FunctionIO2.h:161
FunctionIOData()=default
Function< T, NDIM > create_function(World &world)
Definition FunctionIO2.h:296
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 FunctionIO2.h:270
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