TRIQS/nda 2.0.0
Multi-dimensional array library for C++
Loading...
Searching...
No Matches
ex5.cpp
1#include <nda/nda.hpp>
2#include <nda/h5.hpp>
3#include <h5/h5.hpp>
4#include <iostream>
5#include <string>
6#include <tuple>
7
8int main() {
9 // HDF5 file
10 h5::file file("ex5.h5", 'w');
11
12 // original array
13 auto A = nda::array<int, 2>(5, 5);
14 for (int i = 0; auto &x : A) x = i++;
15 std::cout << "A = " << A << std::endl;
16
17 // write the array to the file
18 h5::write(file, "A", A);
19
20 // write a view to HDF5
21 h5::write(file, "A_v", A(nda::range(0, 5, 2), nda::range(0, 5, 2)));
22
23 // read a dataset into an array
24 auto A_r = nda::array<int, 2>();
25 h5::read(file, "A", A_r);
26 std::cout << "A_r = " << A_r << std::endl;
27
28 // read a dataset into a view
29 auto B = nda::zeros<int>(5, 5);
30 auto B_v = B(nda::range(1, 4), nda::range(0, 5, 2));
31 h5::read(file, "A_v", B_v);
32 std::cout << "B = " << B << std::endl;
33
34 // prepare a dataset
35 h5::write(file, "B", nda::zeros<int>(5, 5));
36
37 // a slice that specifies every other row of "B"
38 auto slice_r024 = std::make_tuple(nda::range(0, 5, 2), nda::range::all);
39
40 // write the first 3 rows of A to the slice
41 h5::write(file, "B", A(nda::range(0, 3), nda::range::all), slice_r024);
42
43 // write the last 2 rows of A to the empty rows in "B"
44 auto slice_r13 = std::make_tuple(nda::range(1, 5, 2), nda::range::all);
45 h5::write(file, "B", A(nda::range(3, 5), nda::range::all), slice_r13);
46
47 // read every other row of "A" into the first 3 rows of C
48 auto C = nda::zeros<int>(5, 5);
49 auto C_r012 = C(nda::range(0, 3), nda::range::all);
50 h5::read(file, "A", C_r012, slice_r024);
51 std::cout << "C = " << C << std::endl;
52
53 // read rows 1 and 3 of "A" into the empty 2 rows of C
54 auto C_r13 = C(nda::range(3, 5), nda::range::all);
55 h5::read(file, "A", C_r13, slice_r13);
56 std::cout << "C = " << C << std::endl;
57
58 // write an array of strings
59 auto S = nda::array<std::string, 1>{"Hi", "my", "name", "is", "John"};
60 h5::write(file, "S", S);
61
62 // read an array of strings
63 auto S_r = nda::array<std::string, 1>();
64 h5::read(file, "S", S_r);
65 std::cout << "S_r = " << S_r << std::endl;
66
67 // write an array of integer arrays
69 h5::write(file, "I", I);
70
71 // read an array of integer arrays
72 auto I_r = nda::array<nda::array<int, 1>, 1>();
73 h5::read(file, "I", I_r);
74 std::cout << "I_r = " << I_r << std::endl;
75}
auto zeros(std::array< Int, Rank > const &shape)
Make an array of the given shape on the given address space and zero-initialize it.
basic_array< ValueType, Rank, Layout, 'A', ContainerPolicy > array
Alias template of an nda::basic_array with an 'A' algebra.
Provides HDF5 support for the nda library.
Includes all relevant headers for the core nda library.