TRIQS/triqs_modest 3.3.0
Brillouin zone summation
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#include <string_view>
11
12namespace triqs::modest {
13
14 namespace fs = std::filesystem;
15
23 template <typename InitialData, typename IterationData> class checkpoint {
24
25 fs::path _dirname;
26 long _n_iter = 0;
27 struct h5path {
28 std::string file; // absolute path to the h5 file
29 std::string group_name; // group name in the h5 file
30 };
31
32 h5path path_initial_data = h5path{_dirname.string() + "/initial_data.h5", "initial_data"};
33 std::string iteration_file_path = _dirname.string() + "/iterations.h5";
34
35 [[nodiscard]] h5path path_iteration(long n_iter) const { return {iteration_file_path, std::to_string(n_iter)}; }
36
37 void write(h5path p, auto const &x) const { h5::write(h5::file(p.file, 'a'), p.group_name, x); }
38
39 template <typename T> [[nodiscard]] T read(h5path p) const { return h5::read<T>(h5::file(p.file, 'r'), p.group_name); }
40
41 public:
46 checkpoint(std::string dirname) : _dirname{dirname} {
47 if (!fs::exists(dirname)) throw std::runtime_error{fmt::format("Checkpoint: dirname '{}' does not exist", dirname)};
48 if (!fs::is_directory(dirname)) throw std::runtime_error{fmt::format("Checkpoint: dirname '{}' exists but is not a directory", dirname)};
49
50 // Read n_iter from file : length of the iteration data in the h5 file
51 auto file = h5::file(iteration_file_path, 'r');
52 _n_iter = long(h5::group{file}.get_all_subgroup_dataset_names().size());
53 }
54
60 checkpoint(std::string dirname, InitialData const &initial_data) : _dirname{dirname} {
61 if (fs::exists(dirname)) throw std::runtime_error{fmt::format("Checkpoint: dirname '{}' already exists", dirname)};
62 fs::create_directory(dirname);
63 auto f = h5::file(path_initial_data.file, 'w');
64 auto root = h5::group{f};
65 h5::write(root, path_initial_data.group_name, initial_data);
66 // Subgroup for logging data
67 auto g = root.create_group("logging");
68 // FIXME : Writout some metadata
69 }
70
71 [[nodiscard]] std::string dirname() const { return _dirname.string(); }
72
73 [[nodiscard]] InitialData initial_data() const { return read<InitialData>(path_initial_data); }
74
75 void append(IterationData const &x) {
76 write(path_iteration(_n_iter), x);
77 _n_iter++;
78 }
79
80 [[nodiscard]] IterationData operator[](long i) const {
81 if (i < 0) i += size();
82 if (i < 0 || i >= size()) throw std::out_of_range{fmt::format("Checkpoint: index {} out of range [0, {}]", i, size())};
83 return read<IterationData>(path_iteration(i));
84 }
85
86 [[nodiscard]] long size() const { return _n_iter; }
87 };
88
89 // ===========================================================================
90 // Checkpoint data structures for DMFT loop
91 // ===========================================================================
92
93 struct initial_data {
96
99
101 // impurity_model imp_model;
102 };
103
104 // HDF5 read and write for initial_data
105 void h5_write(h5::group g, std::string const &name, initial_data const &data) {
106 auto g2 = g.create_group(name);
107 h5::write(g2, "one_body_elements_on_grid", data.obe);
108 h5::write(g2, "embedding", data.embed);
109 // h5::write(g, "impurity_model", data.imp_model); // Uncomment if impurity_model is added
110 }
111
112 void h5_read(h5::group g, std::string const &name, initial_data &data) {
113 auto g2 = g.create_group(name);
114 h5::read(g2, "one_body_elements_on_grid", data.obe);
115 h5::read(g2, "embedding", data.embed);
116 // h5::read(g, "impurity_model", data.imp_model); // Uncomment if impurity_model is added
117 }
118
119 // ------------------------------------------------------
120 using block_gf_imfreq_t = block_gf<imfreq, matrix_valued>;
121 using block_gf_imtime_t = block_gf<imtime, matrix_valued>;
122 using block_mat_t = std::vector<nda::matrix<dcomplex>>;
123
125
127 double mu;
128
130 std::vector<block_gf_imfreq_t> Sigma_imp_list;
131
134
136 std::vector<block_gf_imfreq_t> Gimp_freq_list;
137
139 std::vector<block_gf_imtime_t> Gimp_time_list;
140
142 std::vector<block_gf_imfreq_t> Sigma_dc_list;
143 };
144
145 // HDF5 read and write for iteration_data
146 void h5_write(h5::group g, std::string const &name, iteration_data const &data) {
147 auto g2 = g.create_group(name);
148 h5::write(g2, "mu", data.mu);
149
150 h5::write(g2, "Sigma_imp_list", data.Sigma_imp_list);
151 h5::write(g2, "Sigma_hartree_list", data.Sigma_hartree_list);
152 h5::write(g2, "Sigma_dc_list", data.Sigma_dc_list);
153
154 h5::write(g2, "Gimp_freq_list", data.Gimp_freq_list);
155 h5::write(g2, "Gimp_time_list", data.Gimp_time_list);
156 }
157
158 void h5_read(h5::group g, std::string const &name, iteration_data &data) {
159 auto g2 = g.open_group(name);
160 h5::read(g2, "mu", data.mu);
161
162 h5::read(g2, "Sigma_imp_list", data.Sigma_imp_list);
163 h5::read(g2, "Sigma_hartree_list", data.Sigma_hartree_list);
164 h5::read(g2, "Sigma_dc_list", data.Sigma_dc_list);
165
166 h5::read(g2, "Gimp_freq_list", data.Gimp_freq_list);
167 h5::read(g2, "Gimp_time_list", data.Gimp_time_list);
168 }
169 // ===========================================================
170
171 // Template specialization for the checkpoint class
172 // For c2py
176 template class checkpoint<initial_data, iteration_data>;
177
178} // namespace triqs::modest
A checkpoint manager for logging data after each DMFT iteration.
InitialData initial_data() const
void append(IterationData const &x)
checkpoint(std::string dirname)
Open checkpoint from an existing file (rw).
IterationData operator[](long i) const
std::string dirname() const
checkpoint(std::string dirname, InitialData const &initial_data)
Create a new checkpoint from initial_data.
The embedding class.
Definition embedding.hpp:29
block_gf< imfreq, matrix_valued > block_gf_imfreq_t
block_gf< imtime, matrix_valued > block_gf_imtime_t
void h5_write(h5::group g, std::string const &name, initial_data const &data)
std::vector< nda::matrix< dcomplex > > block_mat_t
void h5_read(h5::group g, std::string const &name, initial_data &data)
embedding embed
Embedding.
one_body_elements_on_grid obe
One body elements.
std::vector< block_gf_imtime_t > Gimp_time_list
Impurities Green's functions.
std::vector< block_gf_imfreq_t > Sigma_dc_list
Impurities self-energies.
double mu
Chemical potential.
std::vector< block_gf_imfreq_t > Gimp_freq_list
Impurities Green's functions.
block_mat_t Sigma_hartree_list
Sigma Hartree.
std::vector< block_gf_imfreq_t > Sigma_imp_list
Impurities self-energies.
A one-body elements struct where all of the underlying data exists on a fixed momentum grid.