TRIQS/h5 1.3.0
C++ interface to HDF5
Loading...
Searching...
No Matches
scalar.hpp
Go to the documentation of this file.
1// Copyright (c) 2019-2024 Simons Foundation
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0.txt
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14//
15// Authors: Thomas Hahn, Olivier Parcollet, Nils Wentzell
16
22#ifndef LIBH5_SCALAR_HPP
23#define LIBH5_SCALAR_HPP
24
25#include "./array_interface.hpp"
26#include "./complex.hpp"
27#include "./group.hpp"
28#include "./macros.hpp"
29#include "./object.hpp"
30
31#include <cmath>
32#include <string>
33#include <type_traits>
34
35namespace h5 {
36
37 namespace array_interface {
38
47 template <typename T>
49 return {hdf5_type<std::decay_t<T>>(), (void *)(&x), 0, is_complex_v<std::decay_t<T>>};
50 }
51
52 } // namespace array_interface
53
69 template <typename T>
70 void h5_write(group g, std::string const &name, T const &x) H5_REQUIRES(std::is_arithmetic_v<T> or is_complex_v<T> or std::is_same_v<T, dcplx_t>) {
72 }
73
84 template <typename T>
85 void h5_read(group g, std::string const &name, T &x) H5_REQUIRES(std::is_arithmetic_v<T> or is_complex_v<T> or std::is_same_v<T, dcplx_t>) {
86 // backward compatibility to read complex values stored the old way (in a subgroup)
87 if constexpr (is_complex_v<T>) {
88 if (g.has_subgroup(name)) {
89 group gr = g.open_group(name);
90 H5_ASSERT(gr.has_key("r") and gr.has_key("i"));
91 double r = NAN, i = NAN;
92 h5_read(gr, "r", r);
93 h5_read(gr, "i", i);
94 x = std::complex<double>{r, i};
95 return;
96 }
97 }
98
99 // get dataset_info
100 auto ds_info = array_interface::get_dataset_info(g, name);
101
102 // read complex values stored as a compound HDF5 datatype
103 if constexpr (is_complex_v<T>) {
104 if (hdf5_type_equal(ds_info.ty, hdf5_type<dcplx_t>())) {
105 h5_read(g, name, reinterpret_cast<dcplx_t &>(x)); // NOLINT (reinterpret_cast is safe here)
106 return;
107 }
108 }
109
110 // read scalar value
112 }
113
124 template <typename T>
125 void h5_write_attribute(object obj, std::string const &name, T const &x) H5_REQUIRES(std::is_arithmetic_v<T> or is_complex_v<T>) {
127 }
128
139 template <typename T>
140 void h5_read_attribute(object obj, std::string const &name, T &x) H5_REQUIRES(std::is_arithmetic_v<T> or is_complex_v<T>) {
142 }
143
146} // namespace h5
147
148#endif // LIBH5_SCALAR_HPP
Provides a generic interface to read/write n-dimensional arrays from/to HDF5.
A handle to an HDF5 group.
Definition group.hpp:44
group open_group(std::string const &key) const
Open a subgroup with the given key in the group.
Definition group.cpp:96
bool has_key(std::string const &key) const
Check if a link with the given key exists in the group.
Definition group.cpp:53
Provides a compound type and type traits for complex numbers.
Provides a handle to an HDF5 group and various methods to simplify the creation/opening of subgroups,...
bool hdf5_type_equal(datatype dt1, datatype dt2)
Check if two HDF5 datatypes are equal.
Definition object.cpp:198
constexpr bool is_complex_v
Boolean type trait set to true for std::complex types.
Definition complex.hpp:64
datatype hdf5_type()
Map a given C++ type to an HDF5 datatype.
Definition object.hpp:156
void read_attribute(object obj, std::string const &name, array_view v)
Read from an HDF5 attribute into an array view.
void read(group g, std::string const &name, array_view v, hyperslab sl)
Read a given hyperslab from an HDF5 dataset into an array view.
void write_attribute(object obj, std::string const &name, array_view v)
Write an array view to an HDF5 attribute.
dataset_info get_dataset_info(dataset ds)
Retrieve the shape and the h5::datatype from a dataset.
array_view array_view_from_scalar(T &x)
Create an array view for a scalar.
Definition scalar.hpp:48
void write(group g, std::string const &name, array_view const &v, bool compress)
Write an array view to an HDF5 dataset.
T h5_read(group g, std::string const &key)
Generic implementation for reading from an HDF5 dataset/subgroup.
Definition generic.hpp:51
T h5_read_attribute(object obj, std::string const &name)
Generic implementation for reading an HDF5 attribute.
Definition generic.hpp:120
void h5_write(group g, std::string const &name, T const &x) H5_REQUIRES(std
Write a scalar to an HDF5 dataset.
Definition scalar.hpp:70
void h5_write_attribute(object obj, std::string const &name, T const &x) H5_REQUIRES(std
Write a scalar to an HDF5 attribute.
Definition scalar.hpp:125
Macros used in the h5 library.
Provides a generic handle for HDF5 objects.
Struct representing a view on an n-dimensional array/dataspace.
A complex compound type consisting of two doubles to represent a complex number.
Definition complex.hpp:38