TRIQS/triqs_modest 3.3.0
Modular Electronic Structure Toolkit
Loading...
Searching...
No Matches
checkpoint.hpp
Go to the documentation of this file.
1// Copyright (c) 2025--present, The Simons Foundation
2// This file is part of TRIQS/modest and is licensed under the terms of GPLv3 or later.
3// SPDX-License-Identifier: GPL-3.0-or-later
4// See LICENSE in the root of this distribution for details.
5
6#include "downfolding.hpp"
7#include "embedding.hpp"
8#include <filesystem>
9#include <string>
10
11namespace triqs::modest {
12
13 namespace fs = std::filesystem;
14
15 // ===========================================================================
16 // Checkpoint data structures for DMFT loop
17 // ===========================================================================
18
19 using block_gf_imfreq_t = block_gf<imfreq, matrix_valued>;
20 using block_gf_imtime_t = block_gf<imtime, matrix_valued>;
21 using block_mat_t = std::vector<nda::matrix<dcomplex>>;
22
27 double mu;
28 std::vector<block_gf_imfreq_t> Sigma_imp_list;
29 std::vector<block_mat_t> Sigma_hartree_list;
30
31 C2PY_IGNORE friend void mpi_broadcast(iteration_data &x, mpi::communicator c = {}, int root = 0) {
32 mpi::broadcast(x.mu, c, root);
33 mpi::broadcast(x.Sigma_imp_list, c, root);
34 mpi::broadcast(x.Sigma_hartree_list, c, root);
35 }
36 };
37
38 // HDF5 serialization for iteration_data
39 inline void h5_write(h5::group g, std::string const &name, iteration_data const &data) {
40 auto g2 = g.create_group(name);
41 h5::write(g2, "mu", data.mu);
42 h5::write(g2, "Sigma_imp_list", data.Sigma_imp_list);
43 h5::write(g2, "Sigma_hartree_list", data.Sigma_hartree_list);
44 }
45
46 inline void h5_read(h5::group g, std::string const &name, iteration_data &data) {
47 auto g2 = g.open_group(name);
48 h5::read(g2, "mu", data.mu);
49 h5::read(g2, "Sigma_imp_list", data.Sigma_imp_list);
50 h5::read(g2, "Sigma_hartree_list", data.Sigma_hartree_list);
51 }
52
53 // ===========================================================================
54 // checkpoint
55 // ===========================================================================
56
57 template <typename IterationData> class checkpoint {
58
59 fs::path _dirname;
60 long _n_iter = 0;
61 std::string _iter_file;
62
63 struct h5path {
64 std::string file;
65 std::string group_name;
66 };
67
68 [[nodiscard]] h5path path_iteration(long n) const { return {_iter_file, std::to_string(n)}; }
69
70 template <typename T> [[nodiscard]] T read(h5path p) const { return h5::read<T>(h5::file(p.file, 'r'), p.group_name); }
71
72 public:
74 checkpoint(std::string dirname) : _dirname{dirname}, _iter_file{dirname + "/iterations.h5"} {
75 if (fs::exists(dirname)) {
76 if (!fs::is_directory(dirname))
77 throw std::runtime_error{fmt::format("Checkpoint: '{}' exists but is not a directory", dirname)};
78 auto file = h5::file(_iter_file, 'r');
79 _n_iter = long(h5::group{file}.get_all_subgroup_dataset_names().size());
80 } else {
81 fs::create_directory(dirname);
82 h5::file(_iter_file, 'w');
83 }
84 }
85
86 [[nodiscard]] std::string dirname() const { return _dirname.string(); }
87 [[nodiscard]] long size() const { return _n_iter; }
88 [[nodiscard]] bool empty() const { return _n_iter == 0; }
89
90 [[nodiscard]] IterationData last() const {
91 if (empty()) throw std::runtime_error{"Checkpoint: no iterations stored"};
92 return (*this)[-1];
93 }
94
95 void append(IterationData const &x) {
96 auto p = path_iteration(_n_iter);
97 auto f = h5::file(p.file, 'a');
98 auto root = h5::group{f};
99 h5::write(root, p.group_name, x);
100 _n_iter++;
101 }
102
103 [[nodiscard]] IterationData operator[](long i) const {
104 if (i < 0) i += size();
105 if (i < 0 || i >= size()) throw std::out_of_range{fmt::format("Checkpoint: index {} out of range [0, {})", i, size())};
106 return read<IterationData>(path_iteration(i));
107 }
108
109 // Range-based for loop support
111 checkpoint const *_cp;
112 long _idx;
113
114 public:
115 const_iterator(checkpoint const *cp, long idx) : _cp{cp}, _idx{idx} {}
116 IterationData operator*() const { return (*_cp)[_idx]; }
118 ++_idx;
119 return *this;
120 }
121 bool operator!=(const_iterator const &other) const { return _idx != other._idx; }
122 };
123
124 [[nodiscard]] const_iterator begin() const { return {this, 0}; }
125 [[nodiscard]] const_iterator end() const { return {this, _n_iter}; }
126 };
127
128 // Explicit instantiation for c2py
129 template class checkpoint<iteration_data>;
130
131} // namespace triqs::modest
const_iterator(checkpoint const *cp, long idx)
bool operator!=(const_iterator const &other) const
void append(IterationData const &x)
std::string dirname() const
IterationData operator[](long i) const
IterationData last() const
const_iterator begin() const
checkpoint(std::string dirname)
Open existing or create new checkpoint directory.
const_iterator end() const
std::vector< nda::matrix< dcomplex > > block_mat_t
void h5_read(h5::group g, std::string const &name, iteration_data &data)
void h5_write(h5::group g, std::string const &name, iteration_data const &data)
block_gf< imtime, matrix_valued > block_gf_imtime_t
block_gf< imfreq, matrix_valued > block_gf_imfreq_t
Minimal iteration data required to restart a DMFT loop.
C2PY_IGNORE friend void mpi_broadcast(iteration_data &x, mpi::communicator c={}, int root=0)
std::vector< block_mat_t > Sigma_hartree_list
std::vector< block_gf_imfreq_t > Sigma_imp_list