votca 2026-dev
Loading...
Searching...
No Matches
crfit.cc
Go to the documentation of this file.
1/*
2 * Copyright 2009-2026 The VOTCA Development Team
3 * (http://www.votca.org)
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License")
6 *
7 * You may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 */
19
20// Standard includes
21#include <filesystem>
22#include <fstream>
23#include <iomanip>
24
25// Third party includes
26#include <boost/format.hpp>
27
28// VOTCA includes
30
31// Local VOTCA includes
33#include "votca/xtp/orbitals.h"
34
35// Local private VOTCA includes
36#include "crfit.h"
37
38namespace votca {
39namespace xtp {
40
42 xyzfile_ = job_name_ + ".xyz";
43 dftoptions_ = options.get(".dftpackage");
44 if (dftoptions_.get("name").as<std::string>() != "xtp") {
45 throw std::runtime_error("crfit: needs the xtp dftpackage");
46 }
47 const std::string method = options.get(".method").as<std::string>();
48 if (method != "analytic" && method != "finite_differences") {
49 throw std::runtime_error(
50 "crfit: method must be analytic or finite_differences");
51 }
52 fit_.analytic = (method == "analytic");
53 fit_.tolerance = options.get(".tolerance").as<double>();
54 fit_.step = options.get(".step").as<double>();
55 scf_error_ = options.get(".scf_error").as<double>();
56 fit_.use_inversion = options.get(".inversion").as<bool>();
57 output_ = options.ifExistsReturnElseReturnDefault<std::string>(
58 ".output", job_name_ + "_cr");
59 if (options.exists(".mpsfile")) {
60 mpsfile_ = options.get(".mpsfile").as<std::string>();
61 }
62 // each SCF at least as tight as scf_error (the differences divide by step)
63 tools::Property& conv = dftoptions_.get("xtpdft.convergence");
64 const double error = conv.get("error").as<double>();
65 auto str = [](double x) {
66 std::stringstream s;
67 s << std::setprecision(17) << x;
68 return s.str();
69 };
70 if (error > scf_error_) {
71 conv.set("error", str(scf_error_));
72 }
73 const double energy = conv.get("energy").as<double>();
74 if (energy > 1e-2 * scf_error_) {
75 conv.set("energy", str(1e-2 * scf_error_));
76 }
77}
78
79bool CRFit::Run() {
80 log_.setReportLevel(Log::current_level);
81 log_.setMultithreading(true);
82 log_.setCommonPreface("\n... ...");
83
84 Orbitals orb0;
86 const Index charge = dftoptions_.get("charge").as<Index>();
87 const Index spin = dftoptions_.get("spin").as<Index>();
88 if (spin != 1) {
89 throw std::runtime_error(
90 "crfit: closed-shell molecules only (spin 1), got spin " +
91 std::to_string(spin));
92 }
93 orb0.setChargeAndSpin(charge, spin);
94 const QMMolecule& mol = orb0.QMAtoms();
95 const Index n_atoms = mol.size();
96
98 << TimeStamp() << " Charge-response fit of " << n_atoms << " atoms from "
99 << xyzfile_ << std::flush;
102
103 const double A3 = std::pow(tools::conv::bohr2ang, 3);
104 auto eig = [&](const Eigen::Matrix3d& m) {
105 Eigen::SelfAdjointEigenSolver<Eigen::Matrix3d> es(m,
106 Eigen::EigenvaluesOnly);
107 return Eigen::Vector3d(es.eigenvalues() * A3);
108 };
110 << std::setprecision(4) << TimeStamp()
111 << " alpha eigenvalues [A^3]: " << eig(r.alpha).transpose()
112 << "\n charge flow X^T Pi X: " << eig(r.alpha_ct).transpose()
113 << "\n residual: " << eig(r.alpha_res).transpose()
114 << "\n Pi: asymmetry before symmetrizing " << r.pi_asymmetry
115 << ", eigenvalues " << r.pi_eigenvalues(1) << " .. "
116 << r.pi_eigenvalues(n_atoms - 1) << " e^2/Hartree" << std::flush;
117
118 WriteCrm(mol, r);
119 WriteXml(mol, r);
120 if (!mpsfile_.empty()) {
121 WriteMps(mol, r);
122 }
123 return true;
124}
125
126std::string CRFit::MethodLabel() const {
127 std::stringstream s;
128 if (fit_.analytic) {
129 s << "coupled-perturbed KS, tolerance " << fit_.tolerance;
130 } else {
131 s << "finite differences, step " << fit_.step;
132 }
133 return s.str();
134}
135
137 const ChargeResponseFit::Result& r) const {
138 const std::string file = output_ + ".crm";
139 std::ofstream ofs(file);
140 if (!ofs.is_open()) {
141 throw std::runtime_error("crfit: cannot write " + file);
142 }
143 ofs << "! charge-response matrix (Hirshfeld), "
144 << dftoptions_.get("functional").as<std::string>() << "/"
145 << dftoptions_.get("basisset").as<std::string>() << ", from " << xyzfile_
146 << " (xtp_tools -e crfit, " << MethodLabel() << ")\n";
147 ofs << "! dq = -Pi phi; Pi in e^2/Hartree, atom order as below (Angstrom)\n";
148 ofs << mol.size() << "\n";
149 for (const QMAtom& atom : mol) {
150 const Eigen::Vector3d p = atom.getPos() * tools::conv::bohr2ang;
151 ofs << boost::format("%-2s %12.6f %12.6f %12.6f\n") % atom.getElement() %
152 p(0) % p(1) % p(2);
153 }
154 for (Index j = 0; j < r.pi.rows(); ++j) {
155 for (Index k = 0; k < r.pi.cols(); ++k) {
156 ofs << boost::format("%+.10e") % r.pi(j, k)
157 << (k + 1 < r.pi.cols() ? " " : "\n");
158 }
159 }
160 XTP_LOG(Log::error, log_) << TimeStamp() << " Wrote " << file << std::flush;
161}
162
163namespace {
164std::string Number(double x) { return (boost::format("%.10e") % x).str(); }
165
166std::string Numbers(const Eigen::VectorXd& v) {
167 std::string s;
168 for (Index i = 0; i < v.size(); ++i) {
169 s += (i ? " " : "") + Number(v(i));
170 }
171 return s;
172}
173
174// symmetric 3x3 tensor as attributes xx xy xz yy yz zz
175void AddTensor(tools::Property& parent, const std::string& name,
176 const Eigen::Matrix3d& m, const std::string& units) {
177 tools::Property& t = parent.add(name, "");
178 t.setAttribute("units", units);
179 const char* labels[3] = {"x", "y", "z"};
180 for (Index i = 0; i < 3; ++i) {
181 for (Index j = i; j < 3; ++j) {
182 t.setAttribute(std::string(labels[i]) + labels[j],
183 Number(0.5 * (m(i, j) + m(j, i))));
184 }
185 }
186}
187} // namespace
188
190 const ChargeResponseFit::Result& r) const {
191 const std::string file = output_ + ".xml";
192 std::ofstream ofs(file);
193 if (!ofs.is_open()) {
194 throw std::runtime_error("crfit: cannot write " + file);
195 }
196 const double A3 = std::pow(tools::conv::bohr2ang, 3);
197 tools::Property root;
198 tools::Property& out = root.add("crfit", "");
199 out.setAttribute("xyz", xyzfile_);
200 out.setAttribute("functional",
201 dftoptions_.get("functional").as<std::string>());
202 out.setAttribute("basisset", dftoptions_.get("basisset").as<std::string>());
203 out.setAttribute("charge", dftoptions_.get("charge").as<Index>());
204 out.setAttribute("method", "xtp_tools -e crfit (" + MethodLabel() + ")");
205
206 tools::Property& atoms = out.add("atoms", "");
207 atoms.setAttribute("units", "bohr");
208 for (Index k = 0; k < mol.size(); ++k) {
209 tools::Property& atom = atoms.add("atom", "");
210 atom.setAttribute("index", k);
211 atom.setAttribute("element", mol[k].getElement());
212 const Eigen::Vector3d p = mol[k].getPos();
213 atom.setAttribute("x", Number(p(0)));
214 atom.setAttribute("y", Number(p(1)));
215 atom.setAttribute("z", Number(p(2)));
216 atom.setAttribute("residual_weight", Number(r.res_weights(k)));
217 AddTensor(atom, "alpha_res", r.alpha_res_atoms[std::size_t(k)], "bohr^3");
218 }
219
220 AddTensor(out, "alpha_dft", r.alpha, "bohr^3");
221 AddTensor(out, "alpha_dft", r.alpha * A3, "Angstrom^3");
222 AddTensor(out, "alpha_ct", r.alpha_ct * A3, "Angstrom^3");
223 AddTensor(out, "alpha_res", r.alpha_res * A3, "Angstrom^3");
224
225 tools::Property& pi = out.add("Pi", "");
226 pi.setAttribute("units", "e^2/Hartree");
227 pi.setAttribute("asymmetry", Number(r.pi_asymmetry));
228 pi.setAttribute("eigenvalues", Numbers(r.pi_eigenvalues));
229 for (Index j = 0; j < r.pi.rows(); ++j) {
230 tools::Property& row =
231 pi.add("row", Numbers(Eigen::VectorXd(r.pi.row(j).transpose())));
232 row.setAttribute("atom", j);
233 }
234 ofs << out;
235 XTP_LOG(Log::error, log_) << TimeStamp() << " Wrote " << file << std::flush;
236}
237
239 const ChargeResponseFit::Result& r) const {
240 PolarSegment tmpl("template", 0);
242 if (tmpl.size() != mol.size()) {
243 throw std::runtime_error(
244 "crfit: " + mpsfile_ + " has " + std::to_string(tmpl.size()) +
245 " sites, the molecule " + std::to_string(mol.size()) + " atoms");
246 }
247 Eigen::MatrixX3d P(mol.size(), 3), Q(mol.size(), 3);
248 for (Index k = 0; k < mol.size(); ++k) {
249 if (tmpl[k].getElement() != mol[k].getElement()) {
250 throw std::runtime_error("crfit: site " + std::to_string(k) + " of " +
251 mpsfile_ + " is " + tmpl[k].getElement() +
252 ", atom " + std::to_string(k) + " is " +
253 mol[k].getElement());
254 }
255 P.row(k) = tmpl[k].getPos().transpose();
256 Q.row(k) = mol[k].getPos().transpose();
257 }
258 // rigid alignment (Kabsch) of the fit geometry onto the template, to
259 // rotate the residual tensors into the template frame
260 const Eigen::MatrixX3d P0 = P.rowwise() - P.colwise().mean();
261 const Eigen::MatrixX3d Q0 = Q.rowwise() - Q.colwise().mean();
262 Eigen::JacobiSVD<Eigen::Matrix3d> svd(
263 Q0.transpose() * P0, Eigen::ComputeFullU | Eigen::ComputeFullV);
264 Eigen::Matrix3d D = Eigen::Matrix3d::Identity();
265 D(2, 2) = (svd.matrixU() * svd.matrixV().transpose()).determinant() < 0 ? -1.0
266 : 1.0;
267 const Eigen::Matrix3d U =
268 svd.matrixV() * D * svd.matrixU().transpose(); // fit -> template
269 const double rmsd =
270 std::sqrt((Q0 * U.transpose() - P0).rowwise().squaredNorm().mean());
271 if (rmsd * tools::conv::bohr2ang > 0.05) {
273 << TimeStamp() << " WARNING: template and fit geometry differ by "
274 << rmsd * tools::conv::bohr2ang << " A rms after alignment"
275 << std::flush;
276 }
277 for (Index k = 0; k < mol.size(); ++k) {
278 tmpl[k].setpolarization(U * r.alpha_res_atoms[std::size_t(k)] *
279 U.transpose());
280 }
281 const std::string file = output_ + ".mps";
282 tmpl.WriteMPS(file, "CRFIT (permanent multipoles of " +
283 std::filesystem::path(mpsfile_).filename().string() +
284 ", residual polarizabilities of the fit)");
285 XTP_LOG(Log::error, log_) << TimeStamp() << " Wrote " << file << std::flush;
286}
287
288} // namespace xtp
289} // namespace votca
class to manage program options with xml serialization functionality
Definition property.h:55
Property & add(const std::string &key, const std::string &value)
add a new property to structure
Definition property.cc:108
Property & get(const std::string &key)
get existing property
Definition property.cc:79
bool exists(const std::string &key) const
check whether property exists
Definition property.cc:122
T as() const
return value as type
Definition property.h:283
T ifExistsReturnElseReturnDefault(const std::string &key, T defaultvalue) const
Definition property.h:332
void setAttribute(const std::string &attribute, const T &value)
set an attribute
Definition property.h:325
const Eigen::Vector3d & getPos() const
Logger log_
Definition crfit.h:57
void WriteMps(const QMMolecule &mol, const ChargeResponseFit::Result &r) const
Definition crfit.cc:238
tools::Property dftoptions_
Definition crfit.h:58
std::string mpsfile_
Definition crfit.h:60
std::string xyzfile_
Definition crfit.h:59
std::string MethodLabel() const
Definition crfit.cc:126
bool Run() final
Definition crfit.cc:79
ChargeResponseFit::FitOptions fit_
Definition crfit.h:62
void ParseOptions(const tools::Property &options) final
Definition crfit.cc:41
std::string output_
Definition crfit.h:61
void WriteXml(const QMMolecule &mol, const ChargeResponseFit::Result &r) const
Definition crfit.cc:189
double scf_error_
Definition crfit.h:63
void WriteCrm(const QMMolecule &mol, const ChargeResponseFit::Result &r) const
Definition crfit.cc:136
static Result Fit(const tools::Property &dftoptions, Orbitals &orb, const FitOptions &fit, Logger &log)
void LoadFromFile(std::string filename)
void WriteMPS(std::string filename, std::string header) const
Container for molecular orbitals and derived one-particle data.
Definition orbitals.h:47
const QMMolecule & QMAtoms() const
Return read-only access to the molecular geometry.
Definition orbitals.h:262
void setChargeAndSpin(Index charge, Index spin)
Definition orbitals.h:246
container for QM atoms
Definition qmatom.h:37
void LoadFromFile(std::string filename)
Definition qmmolecule.cc:45
std::string job_name_
Definition qmtool.h:50
Timestamp returns the current time as a string Example: cout << TimeStamp().
Definition logger.h:224
#define XTP_LOG(level, log)
Definition logger.h:40
const double bohr2ang
Definition constants.h:49
Charge transport classes.
Definition ERIs.h:28
ClassicalSegment< PolarSite > PolarSegment
Provides a means for comparing floating point numbers.
Definition basebead.h:33
Eigen::Index Index
Definition types.h:26
static Level current_level
Definition globals.h:30
std::vector< Eigen::Matrix3d > alpha_res_atoms