MADNESS 0.10.1
Results.h
Go to the documentation of this file.
1//
2// Created by Florian Bischoff on 08.07.25.
3//
4
5#ifndef RESULTS_H
6#define RESULTS_H
7
8#include <madness/external/nlohmann_json/json.hpp>
9#include <madness/tensor/tensor_json.hpp>
10#include<string>
11
12//* base and derived classes for holding results of a calculation
13namespace madness {
14
16
17 public:
18 ResultsBase() = default;
19 virtual ~ResultsBase() = default;
20
21 /// serialize the results to a JSON object
22 virtual nlohmann::json to_json() const = 0;
23
24 virtual std::string key() const = 0;
25 };
26
27 /// holds metadata of the calculation
28
29 /// create right before the calculation starts, stop() must be called after the calculation is finished
31 public:
32
35 mpi_size= world.size();
36 }
37
38 double time_begin=0.0;
39 double time_end=0.0;
40 std::string finished_at="";
41 std::string git_hash="";
42 long mpi_size=-1;
43
44 std::string key() const override {return "metadata"; }
45
46 void stop() {
49 }
50
51 nlohmann::json to_json() const override {
52 nlohmann::json j;
53 // compute timing on-the-fly unless they have been set
54 if (time_end==0.0) {
55 j["elapsed_time"] = wall_time() - time_begin;
56 j["finished_at"] = time_tag();
57 } else {
58 j["elapsed_time"] = time_end - time_begin;
59 j["finished_at"] = finished_at;
60 }
61 j["git_hash"] = git_hash;
62 j["host"] = std::string(HOST_SYSTEM);
63 j["nthreads"] = ThreadPool::size();
64 j["mpi_size"] = mpi_size;
65 return j;
66 }
67
68 private:
69 /// borrowed from Adrian's MolDFTLib
70 std::string time_tag() const {
71 auto print_time = std::chrono::system_clock::now();
72 auto in_time_t = std::chrono::system_clock::to_time_t(print_time);
73 std::stringstream ss;
74 ss << std::put_time(std::localtime(&in_time_t), "%Y-%m-%d %X");
75 return ss.str();
76 }
77 };
78
79 /// holds convergence results of the calculation
81 double converged_for_thresh = 1.e10;
82 double converged_for_dconv = 1.e10;
83
84 public:
85 ConvergenceResults() = default;
86
87 /// construct from JSON
88 ConvergenceResults(const nlohmann::json& j) {
89 converged_for_thresh = j.value("converged_for_thresh", 1.e10);
90 converged_for_dconv = j.value("converged_for_dconv", 1.e10);
91 }
92
93 /// assignment operator from JSON
94 ConvergenceResults& operator=(const nlohmann::json& j) {
95 converged_for_thresh = j.value("converged_for_thresh", 1.e10);
96 converged_for_dconv = j.value("converged_for_dconv", 1.e10);
97 return *this;
98 }
99
100 std::string key() const override {return "convergence"; }
101
106
108 converged_for_dconv = dconv;
109 return *this;
110 }
111
112 nlohmann::json to_json() const override {
113 nlohmann::json j;
114 j["converged_for_thresh"] = converged_for_thresh;
115 j["converged_for_dconv"] = converged_for_dconv;
116 return j;
117 }
118 };
119
121 public:
122 PropertyResults() = default;
123
124 /// construct from JSON
125 PropertyResults(const nlohmann::json& j) {
126 energy= j.value("energy", 0.0);
127 if (j.count("dipole")==1) dipole = tensor_from_json<double>(j["dipole"]);
128 if (j.count("gradient")==1) gradient= tensor_from_json<double>(j["gradient"]);
129 }
130
131 double energy = 0.0;
134
135 std::string key() const override {return "properties"; }
136
137 nlohmann::json to_json() const override {
138 nlohmann::json j;
139 j["energy"] = energy;
140 j["dipole"] = tensor_to_json(dipole);
141 j["gradient"] = tensor_to_json(gradient);
142 return j;
143 }
144
145 };
146
147 class SCFResults: public ResultsBase {
148 public:
153 std::string model="scf"; // model used for the SCF calculation
154 double scf_total_energy=0.0; // total energy of the SCF calculation
156
157 SCFResults() = default;
158
159 /// construct from JSON
160 SCFResults(const nlohmann::json& j) {
161 if (j.count("scf_eigenvalues_a")>0)
162 aeps = tensor_from_json<double>(j["scf_eigenvalues_a"]);
163 if (j.count("scf_eigenvalues_b") > 0)
164 beps = tensor_from_json<double>(j["scf_eigenvalues_b"]);
165 if (j.count("scf_fock_b") > 0)
166 bfock = tensor_from_json<double>(j["scf_fock_b"]);
167 if (j.count("scf_fock_b") > 0)
168 bfock = tensor_from_json<double>(j["scf_fock_b"]);
169 if (j.count("scf_total_energy") > 0)
170 scf_total_energy = j["scf_total_energy"];
171
172 if (j.count("properties") > 0) {
173 properties = PropertyResults(j["properties"]);
174 } else {
176 }
177 }
178
179 std::string key() const override {return model; }
180
181 nlohmann::json to_json() const override {
182 nlohmann::json j;
183 j["scf_eigenvalues_a"] = tensor_to_json(aeps);
184 j["scf_fock_a"] = tensor_to_json(afock);
185 if (beps.size() > 0) j["scf_eigenvalues_b"] = tensor_to_json(beps);
186 if (bfock.size() > 0) j["scf_fock_b"] = tensor_to_json(bfock);
187 j["model"] = model;
188 j["scf_total_energy"] = scf_total_energy;
189 j["properties"] = properties.to_json();
190 return j;
191 }
192 };
193
194 class CISResults: public ResultsBase {
195 public:
197 std::string irrep; // irreducible representation
198 double omega; // excitation energy in Hartree
199 double current_error; // error in the excitation energy
200 double oscillator_strength_length; // oscillator strength
201 double oscillator_strength_velocity; // oscillator strength
202 };
203 std::vector<excitation_info> excitations;
204 long nfreeze=-1;
205 std::string model="unknown";
206
207 std::string key() const override {return model; }
208
209 CISResults() = default;
210
211 /// construct from JSON
212 CISResults(const nlohmann::json& j) {
213 if (j.count("excitations") > 0) {
214 for (const auto& ex : j["excitations"]) {
216 ei.irrep = ex.value("irrep", "");
217 ei.omega = ex.value("omega", 0.0);
218 ei.current_error = ex.value("current_error", 0.0);
219 ei.oscillator_strength_length = ex.value("oscillator_strength_length", 0.0);
220 ei.oscillator_strength_velocity = ex.value("oscillator_strength_velocity", 0.0);
221 excitations.push_back(ei);
222 }
223 }
224 nfreeze = j.value("nfreeze", -1);
225 model= j.value("model", "unknown");
226 }
227
228 /// constructor with nfreeze and model
229 CISResults(long nfreeze, const std::string& model) : nfreeze(nfreeze), model(model) {}
230
231 nlohmann::json to_json() const override {
232 nlohmann::json j;
233 for (const auto& ex : excitations) {
234 nlohmann::json ex_json;
235 ex_json["irrep"] = ex.irrep;
236 ex_json["omega"] = ex.omega;
237 ex_json["current_error"] = ex.current_error;
238 ex_json["oscillator_strength_length"] = ex.oscillator_strength_length;
239 ex_json["oscillator_strength_velocity"] = ex.oscillator_strength_velocity;
240 j["excitations"].push_back(ex_json);
241 }
242 j["nfreeze"] = nfreeze;
243 j["model"] = model;
244 return j;
245 }
246
247 };
248
249 class CC2Results: public CISResults {
250 public:
252 model="mp2";
253 }
254
255 /// construct from JSON
256 CC2Results(const nlohmann::json& j) : CISResults(j) {
257 properties = PropertyResults(j.value("properties", nlohmann::json{}));
258 model = j.value("model", "mp2");
259 correlation_energy = j.value("correlation_energy", 0.0);
260 total_energy = j.value(model+"_total_energy", 0.0);
261 }
262
263 /// constructor with nfreeze and model
264 CC2Results(long nfreeze, const std::string& model) : CISResults(nfreeze,model) {}
265
266 nlohmann::json to_json() const override {
267 nlohmann::json j;
269 j["properties"] = properties.to_json();
270 j["model"] = model;
271 j["correlation_energy"] = correlation_energy;
272 j[model+"_correlation_energy"] = correlation_energy;
273 j[model+"_total_energy"] = total_energy;
274 return j;
275 }
276
277 CC2Results& set_energies(const double scf_energy, const double corr_energy) {
278 this->correlation_energy = corr_energy;
279 this->total_energy = scf_energy + corr_energy;
280 return *this;
281 }
282
283 /// setters with chaining
284 CC2Results& set_correlation_energy(const double corr_energy) {
285 correlation_energy= corr_energy;
286 return *this;
287 }
289 this->total_energy = total_energy;
290 return *this;
291 }
293 properties = props;
294 return *this;
295 }
296 CC2Results& set_model(const std::string& model) {
297 this->model = model;
298 return *this;
299 }
300
301 PropertyResults properties; // properties of the correlated calculation
302 double correlation_energy = 0.0; // correlation energy of the correlated calculation
303 double total_energy = 0.0; // total energy of the correlated calculation
304
305 };
306
307 class ZnemoResults: public SCFResults {
308 public:
309
310 double B=0.0; // B value for the Znemo calculation
311
312 ZnemoResults() = default;
313 /// construct from JSON
314 ZnemoResults(const nlohmann::json& j) : SCFResults(j) {
315 B = j.value("B", 0.0);
316 }
317
318 nlohmann::json to_json() const override {
319 nlohmann::json j;
321 j["B"] = B;
322 return j;
323 }
324
325 };
326
327 class OEPResults: public SCFResults {
328 public:
329 double drho=0.0; // delta rho =difference to reference (=HF?) density
330 double devir14=0.0; // diagnostic parameter
331 double devir17=0.0; // diagnostic parameter
332 double Ex_vir=0.0; // local exchange energy
333 double Ex_conv=0.0; //
334 double Ex_HF=0.0; // HF exchange energy
335 double E_kin_HF=0.0;
336 double E_kin_KS=0.0; // kinetic energy of the KS reference
337 double Econv=0.0; // final energy using conventional method
338
339 OEPResults() = default;
340
341 /// construct from JSON
342 explicit OEPResults(const nlohmann::json& j) : SCFResults(j) {
343 model = j.value("model", "oaep");
344 drho = j.value("drho", 0.0);
345 devir14 = j.value("dvir14", 0.0);
346 devir17 = j.value("dvir17", 0.0);
347 Ex_vir = j.value("Ex_vir", 0.0);
348 Ex_conv = j.value("Ex_conv", 0.0);
349 Ex_HF = j.value("Ex_HF", 0.0);
350 E_kin_HF = j.value("E_kin_HF", 0.0);
351 E_kin_KS = j.value("E_kin_KS", 0.0);
352 Econv = j.value("Econv", 0.0);
353 }
354
355 nlohmann::json to_json() const override {
356 nlohmann::json j;
358 j["model"] = model;
359 j["drho"] = drho;
360 j["devir14"] = devir14;
361 j["devir17"] = devir17;
362 j["Ex_vir"] = Ex_vir;
363 j["Ex_conv"] = Ex_conv;
364 j["Ex_HF"] = Ex_HF;
365 j["E_kin_HF"] = E_kin_HF;
366 j["E_kin_KS"] = E_kin_KS;
367 j["Econv"] = Econv;
368 return j;
369 }
370
371 };
372
374 virtual nlohmann::json to_json() const {
375 MADNESS_EXCEPTION("to_json not implemented for ConvergenceResults", 1);
376 }
377
378 };
379
380
381}
382#endif //RESULTS_H
Definition test_ar.cc:141
long size() const
Returns the number of elements in the tensor.
Definition basetensor.h:138
Definition Results.h:249
CC2Results & set_energies(const double scf_energy, const double corr_energy)
Definition Results.h:277
CC2Results & set_properties(const PropertyResults &props)
Definition Results.h:292
CC2Results(const nlohmann::json &j)
construct from JSON
Definition Results.h:256
CC2Results & set_total_energy(const double total_energy)
Definition Results.h:288
CC2Results(long nfreeze, const std::string &model)
constructor with nfreeze and model
Definition Results.h:264
CC2Results & set_model(const std::string &model)
Definition Results.h:296
double correlation_energy
Definition Results.h:302
CC2Results & set_correlation_energy(const double corr_energy)
setters with chaining
Definition Results.h:284
CC2Results()
Definition Results.h:251
nlohmann::json to_json() const override
serialize the results to a JSON object
Definition Results.h:266
PropertyResults properties
Definition Results.h:301
double total_energy
Definition Results.h:303
Definition Results.h:194
std::vector< excitation_info > excitations
Definition Results.h:203
nlohmann::json to_json() const override
serialize the results to a JSON object
Definition Results.h:231
CISResults(const nlohmann::json &j)
construct from JSON
Definition Results.h:212
std::string model
Definition Results.h:205
std::string key() const override
Definition Results.h:207
long nfreeze
Definition Results.h:204
CISResults(long nfreeze, const std::string &model)
constructor with nfreeze and model
Definition Results.h:229
holds convergence results of the calculation
Definition Results.h:80
double converged_for_thresh
Definition Results.h:81
ConvergenceResults & set_converged_dconv(double dconv)
Definition Results.h:107
std::string key() const override
Definition Results.h:100
nlohmann::json to_json() const override
serialize the results to a JSON object
Definition Results.h:112
ConvergenceResults & set_converged_thresh(double thresh)
Definition Results.h:102
ConvergenceResults(const nlohmann::json &j)
construct from JSON
Definition Results.h:88
double converged_for_dconv
Definition Results.h:82
ConvergenceResults & operator=(const nlohmann::json &j)
assignment operator from JSON
Definition Results.h:94
holds metadata of the calculation
Definition Results.h:30
long mpi_size
Definition Results.h:42
MetaDataResults(World &world)
Definition Results.h:33
std::string git_hash
Definition Results.h:41
std::string finished_at
Definition Results.h:40
std::string time_tag() const
borrowed from Adrian's MolDFTLib
Definition Results.h:70
double time_begin
Definition Results.h:38
nlohmann::json to_json() const override
serialize the results to a JSON object
Definition Results.h:51
void stop()
Definition Results.h:46
double time_end
Definition Results.h:39
std::string key() const override
Definition Results.h:44
Definition Results.h:327
nlohmann::json to_json() const override
serialize the results to a JSON object
Definition Results.h:355
double Ex_vir
Definition Results.h:332
double devir14
Definition Results.h:330
double Econv
Definition Results.h:337
double Ex_HF
Definition Results.h:334
double drho
Definition Results.h:329
OEPResults(const nlohmann::json &j)
construct from JSON
Definition Results.h:342
double E_kin_KS
Definition Results.h:336
double Ex_conv
Definition Results.h:333
double E_kin_HF
Definition Results.h:335
double devir17
Definition Results.h:331
Definition Results.h:120
std::string key() const override
Definition Results.h:135
Tensor< double > gradient
Definition Results.h:133
PropertyResults(const nlohmann::json &j)
construct from JSON
Definition Results.h:125
Tensor< double > dipole
Definition Results.h:132
double energy
Definition Results.h:131
nlohmann::json to_json() const override
serialize the results to a JSON object
Definition Results.h:137
Definition Results.h:373
virtual nlohmann::json to_json() const
serialize the results to a JSON object
Definition Results.h:374
Definition Results.h:15
virtual std::string key() const =0
virtual ~ResultsBase()=default
virtual nlohmann::json to_json() const =0
serialize the results to a JSON object
Definition Results.h:147
nlohmann::json to_json() const override
serialize the results to a JSON object
Definition Results.h:181
Tensor< double > beps
Definition Results.h:150
std::string model
Definition Results.h:153
Tensor< double > aeps
Definition Results.h:149
double scf_total_energy
Definition Results.h:154
SCFResults(const nlohmann::json &j)
construct from JSON
Definition Results.h:160
Tensor< double > bfock
Definition Results.h:152
PropertyResults properties
Definition Results.h:155
Tensor< double > afock
Definition Results.h:151
std::string key() const override
Definition Results.h:179
A tensor is a multidimensional array.
Definition tensor.h:317
static std::size_t size()
Returns the number of threads in the pool.
Definition thread.h:1419
A parallel world class.
Definition world.h:132
ProcessID size() const
Returns the number of processes in this World (same as MPI_Comm_size()).
Definition world.h:330
Definition Results.h:307
nlohmann::json to_json() const override
serialize the results to a JSON object
Definition Results.h:318
double B
Definition Results.h:310
ZnemoResults(const nlohmann::json &j)
construct from JSON
Definition Results.h:314
#define MADNESS_EXCEPTION(msg, value)
Macro for throwing a MADNESS exception.
Definition madness_exception.h:119
Namespace for all elements and tools of MADNESS.
Definition DFParameters.h:10
double wall_time()
Returns the wall time in seconds relative to an arbitrary origin.
Definition timers.cc:48
static const double thresh
Definition rk.cc:45
Definition Results.h:196
double oscillator_strength_length
Definition Results.h:200
double omega
Definition Results.h:198
double oscillator_strength_velocity
Definition Results.h:201
std::string irrep
Definition Results.h:197
double current_error
Definition Results.h:199