LIBINT 2.9.0
atom.h
1/*
2 * Copyright (C) 2004-2024 Edward F. Valeev
3 *
4 * This file is part of Libint library.
5 *
6 * Libint library is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * Libint library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with Libint library. If not, see <http://www.gnu.org/licenses/>.
18 *
19 */
20
21#ifndef _libint2_src_lib_libint_atom_h_
22#define _libint2_src_lib_libint_atom_h_
23
24#include <libint2/util/cxxstd.h>
25#if LIBINT2_CPLUSPLUS_STD < 2011
26#error "libint2/atom.h requires C++11 support"
27#endif
28
29#include <libint2/chemistry/elements.h>
30
31#include <array>
32#include <iostream>
33#include <sstream>
34#include <tuple>
35#include <utility>
36#include <vector>
37
38namespace libint2 {
39
40struct Atom {
41 int atomic_number;
42 double x, y, z;
43};
44inline bool operator==(const Atom &atom1, const Atom &atom2) {
45 return atom1.atomic_number == atom2.atomic_number && atom1.x == atom2.x &&
46 atom1.y == atom2.y && atom1.z == atom2.z;
47}
48
49namespace constants {
53 static constexpr double bohr_to_angstrom = 0.529177210903;
54 static constexpr double angstrom_to_bohr = 1 / bohr_to_angstrom;
55};
58 static constexpr double bohr_to_angstrom = 0.52917721067;
59 static constexpr double angstrom_to_bohr = 1 / bohr_to_angstrom;
60};
63 static constexpr double bohr_to_angstrom = 0.52917721092;
64 static constexpr double angstrom_to_bohr = 1 / bohr_to_angstrom;
65};
66} // namespace constants
67
68} // namespace libint2
69
70namespace {
71
72bool strcaseequal(const std::string &a, const std::string &b) {
73 return a.size() == b.size() &&
74 std::equal(a.begin(), a.end(), b.begin(), [](char a, char b) {
75 return ::tolower(a) == ::tolower(b);
76 });
77}
78
81inline std::tuple<std::vector<libint2::Atom>,
82 std::array<std::array<double, 3>, 3>>
83__libint2_read_dotxyz(std::istream &is, const double bohr_to_angstrom,
84 const bool pbc = false) {
85 using libint2::Atom;
86 const std::string caller =
87 std::string("libint2::read_dotxyz") + (pbc ? "_pbc" : "");
88
89 // first line = # of atoms
90 size_t natom;
91 is >> natom;
92
93 // read off the rest of first line and discard
94 std::string rest_of_line;
95 std::getline(is, rest_of_line);
96
97 // second line = comment
98 std::string comment;
99 std::getline(is, comment);
100
101 // rest of lines are atoms (and unit cell parameters, if pbc = true)
102 const auto nlines_expected = natom + (pbc ? 3 : 0);
103 std::vector<Atom> atoms(natom, Atom{0, 0.0, 0.0, 0.0});
104 std::array<std::array<double, 3>, 3> unit_cell({{{0.0, 0.0, 0.0}}});
105 bool found_abc[3] = {false, false, false};
106 for (size_t line = 0, atom_index = 0; line < nlines_expected; ++line) {
107 if (is.eof())
108 throw std::logic_error(caller + ": expected " +
109 std::to_string(nlines_expected) +
110 " sets of coordinates but only " +
111 std::to_string(line) + " received");
112
113 // read line
114 std::string linestr;
115 std::getline(is, linestr);
116 std::istringstream iss(linestr);
117 // then parse ... this handles "extended" XYZ formats
118 std::string element_symbol;
119 double x, y, z;
120 iss >> element_symbol >> x >> y >> z;
121
122 // .xyz files report Cartesian coordinates in angstroms; convert to bohr
123 const auto angstrom_to_bohr = 1 / bohr_to_angstrom;
124
125 auto assign_atom = [angstrom_to_bohr](Atom &atom, int Z, double x, double y,
126 double z) {
127 atom.atomic_number = Z;
128 atom.x = x * angstrom_to_bohr;
129 atom.y = y * angstrom_to_bohr;
130 atom.z = z * angstrom_to_bohr;
131 };
132 auto assign_xyz = [angstrom_to_bohr](std::array<double, 3> &xyz, double x,
133 double y, double z) {
134 xyz[0] = x * angstrom_to_bohr;
135 xyz[1] = y * angstrom_to_bohr;
136 xyz[2] = z * angstrom_to_bohr;
137 };
138
139 auto axis = -1;
140 // if pbc = true, look for unit cell params
141 if (pbc) {
142 if (strcaseequal("AA", element_symbol)) axis = 0;
143 if (strcaseequal("BB", element_symbol)) axis = 1;
144 if (strcaseequal("CC", element_symbol)) axis = 2;
145 if (axis != -1) {
146 if (found_abc[axis])
147 throw std::logic_error(
148 caller + ": unit cell parameter along Cartesian axis " +
149 std::to_string(axis) + " appears more than once");
150 assign_xyz(unit_cell[axis], x, y, z);
151 found_abc[axis] = true;
152 }
153 }
154
155 // .xyz files report element labels, hence convert to atomic numbers
156 if (axis == -1) {
157 int Z = -1;
158 for (const auto &e : libint2::chemistry::get_element_info()) {
159 if (strcaseequal(e.symbol, element_symbol)) {
160 Z = e.Z;
161 break;
162 }
163 }
164 if (Z == -1) {
165 std::ostringstream oss;
166 oss << caller << ": element symbol \"" << element_symbol
167 << "\" is not recognized" << std::endl;
168 throw std::logic_error(oss.str().c_str());
169 }
170
171 if (pbc &&
172 atom_index == atoms.size()) { // if PBC, check for too many atoms
173 throw std::logic_error(caller + ": too many atoms");
174 }
175 assign_atom(atoms[atom_index++], Z, x, y, z);
176 }
177 }
178
179 // make sure all 3 axes were specified
180 if (pbc) {
181 for (auto xyz = 0; xyz != 3; ++xyz)
182 if (!found_abc[xyz]) {
183 throw std::logic_error(caller +
184 ": unit cell parameter along Cartesian axis " +
185 std::to_string(xyz) + " not given");
186 }
187 }
188
189 return std::make_tuple(atoms, unit_cell);
190}
191
192} // anonymous namespace
193
194namespace libint2 {
195
196// clang-format off
208// clang-format off
209inline std::vector<Atom> read_dotxyz(
210 std::istream &is,
211 const double bohr_to_angstrom = constants::codata_2018::bohr_to_angstrom) {
212 std::vector<Atom> atoms;
213 std::tie(atoms, std::ignore) =
214 __libint2_read_dotxyz(is, bohr_to_angstrom, false);
215 return atoms;
216}
217
233inline auto read_dotxyz_pbc(
234 std::istream &is,
235 const double bohr_to_angstrom = constants::codata_2018::bohr_to_angstrom)
236 -> decltype(__libint2_read_dotxyz(is, bohr_to_angstrom, true)) {
237 return __libint2_read_dotxyz(is, bohr_to_angstrom, true);
238}
239
241std::vector<std::pair<double, std::array<double, 3>>> inline make_point_charges(
242 const std::vector<libint2::Atom> &atoms) {
243 std::vector<std::pair<double, std::array<double, 3>>> q;
244 q.reserve(atoms.size());
245 for (const auto &atom : atoms) {
246 q.emplace_back(static_cast<double>(atom.atomic_number),
247 std::array<double, 3>{{atom.x, atom.y, atom.z}});
248 }
249 return q;
250}
251
252} // namespace libint2
253
254#endif /* _libint2_src_lib_libint_atom_h_ */
Defaults definitions for various parameters assumed by Libint.
Definition algebra.cc:24
auto read_dotxyz_pbc(std::istream &is, const double bohr_to_angstrom=constants::codata_2018::bohr_to_angstrom) -> decltype(__libint2_read_dotxyz(is, bohr_to_angstrom, true))
reads the list of atoms from a file in the PBC-extended XYZ format
Definition atom.h:233
std::vector< std::pair< double, std::array< double, 3 > > > make_point_charges(const std::vector< libint2::Atom > &atoms)
converts a vector of Atoms to a vector of point charges
Definition atom.h:241
std::vector< Atom > read_dotxyz(std::istream &is, const double bohr_to_angstrom=constants::codata_2018::bohr_to_angstrom)
reads the list of atoms from a file in the standard XYZ format supported by most chemistry software (...
Definition atom.h:209
Definition atom.h:40
the 2010 CODATA reference set, available at DOI 10.1103/RevModPhys.84.1527
Definition atom.h:62
the 2014 CODATA reference set, available at DOI 10.1103/RevModPhys.88.035009
Definition atom.h:57
the 2018 CODATA reference set, available at https://physics.nist.gov/cuu/pdf/wall_2018....
Definition atom.h:52