Loading...
Searching...
No Matches
init.hpp
Go to the documentation of this file.
1#pragma once
2
3/**
4 * BerkeleyGWExporter — orchestrates BerkeleyGW mean-field export.
5 *
6 * Writes WFN (wavefunctions), then RHO (charge density), then attempts
7 * vxc.dat (exchange-correlation matrix elements), VXC (the exchange-correlation
8 * potential) and VSC (the local Kohn-Sham potential), and -- when the record
9 * carries projectors, which only
10 * `control.mean_field_export_kleinman_bylander_projectors` asks for -- VKB (the
11 * nonlocal pseudopotential). VSC and VKB are what ParaBands needs to extend WFN
12 * with more bands; VXC is what sigma reads under `dont_use_vxcdat`. vxc.dat is written for
13 * the supported case (spin-unpolarized, non-gamma-only -- see
14 * exchange_correlation.hpp for the G-space convolution this uses and the
15 * exact scope restrictions); outside that scope, or if the SCF state does
16 * not carry a usable G-space Vxc, write_to_file() throws a clear, specific
17 * error. That attempt is caught and logged here so an out-of-scope or
18 * incomplete vxc.dat does not take down an otherwise-successful export. VXC,
19 * VSC and VKB are attempted the same way, for the same reason.
20 * WFN's own spin-polarized (nspin>1) rejection (see wavefunction.hpp) is
21 * deliberately NOT caught here -- unlike vxc.dat, a spin-polarized run
22 * silently getting RHO without WFN is not a useful partial artifact for a
23 * BGW workflow, so that case fails the whole export loudly instead.
24 */
25
26#include <filesystem>
27#include <string>
28#include <system_error>
29
30#include "io/logger.hpp"
38
39namespace q3 {
40
42 public:
44 const std::string& directory) const override {
45 std::filesystem::create_directories(directory);
46
47 BerkeleyGWWavefunctionFile wavefunction_file(record);
48 write_or_remove(directory + "/WFN",
49 [&](const std::string& path) { wavefunction_file.write_to_file(path); });
50
51 BerkeleyGWChargeDensityFile charge_density_file(record);
52 write_or_remove(directory + "/RHO",
53 [&](const std::string& path) { charge_density_file.write_to_file(path); });
54
55 BerkeleyGWExchangeCorrelationFile exchange_correlation_file(record);
56 try {
57 write_or_remove(directory + "/vxc.dat", [&](const std::string& path) {
58 exchange_correlation_file.write_to_file(path);
59 });
60 } catch (const std::runtime_error& error) {
61 log::warn(" vxc.dat not written: {}", error.what());
62 }
63
64 BerkeleyGWExchangeCorrelationPotentialFile exchange_correlation_potential_file(record);
65 write_or_warn(directory + "/VXC", "VXC", [&](const std::string& path) {
66 exchange_correlation_potential_file.write_to_file(path);
67 });
68
69 BerkeleyGWSelfConsistentPotentialFile self_consistent_potential_file(record);
70 write_or_warn(directory + "/VSC", "VSC", [&](const std::string& path) {
71 self_consistent_potential_file.write_to_file(path);
72 });
73
74 if (record.projector_source) {
75 BerkeleyGWKleinmanBylanderProjectorFile projector_file(record);
76 write_or_warn(directory + "/VKB", "VKB", [&](const std::string& path) {
77 projector_file.write_to_file(path);
78 });
79 }
80 }
81
82 [[nodiscard]] std::string name() const override {
83 return "berkeleygw";
84 }
85
86 private:
87 /**
88 * Write one file, and remove it if writing fails part-way.
89 *
90 * A file is opened before its header is built, so a refusal there used to
91 * leave a truncated WFN on disk. A later BerkeleyGW step, or a person,
92 * could take it for the export's output. No file is better than one that
93 * stops after its first record.
94 */
95 template <typename Writer>
96 static void write_or_remove(const std::string& path, const Writer& write) {
97 try {
98 write(path);
99 } catch (...) {
100 std::error_code ignored;
101 std::filesystem::remove(path, ignored);
102 throw;
103 }
104 }
105
106 /// write_or_remove(), with a refusal logged instead of ending the export.
107 template <typename Writer>
108 static void
109 write_or_warn(const std::string& path, const std::string& name, const Writer& write) {
110 try {
111 write_or_remove(path, write);
112 } catch (const std::runtime_error& error) {
113 log::warn(" {} not written: {}", name, error.what());
114 }
115 }
116};
117
118} // namespace q3
Writes VXC from a mean-field record; see the file comment.
void write_to_file(const std::string &file_path) const
Writes the file, or throws – before creating it – when the record cannot supply it.
std::string name() const override
Format name as accepted by the input ("none", "berkeleygw", ...).
Definition init.hpp:82
void export_to_directory(const MeanFieldRecord &record, const std::string &directory) const override
Definition init.hpp:43
Writes VKB from a mean-field record that carries projectors; see the file comment.
void write_to_file(const std::string &file_path) const
Writes the file, or throws – before creating it – when the record carries no projectors.
Writes VSC from a mean-field record; see the file comment.
void write_to_file(const std::string &file_path) const
void warn(spdlog::format_string_t< Args... > fmt, Args &&... args)
Definition logger.hpp:187
void error(spdlog::format_string_t< Args... > fmt, Args &&... args)
Definition logger.hpp:192
std::shared_ptr< const MeanFieldProjectorSource > projector_source
Definition init.hpp:182