34namespace fs = std::filesystem;
36std::string lower(std::string s) {
37 std::transform(s.begin(), s.end(), s.begin(),
38 [](
unsigned char c) { return std::tolower(c); });
42std::mutex &format_mutex() {
46std::string &input_fmt() {
50thread_local std::string g_last_cell_warning;
53 std::vector<double> values;
57ConfigCell &config_cell() {
63Matrix3d matrix_from_values(
const std::vector<double> &v) {
70 for (
int i = 0; i < 9; ++i) {
71 box(i / 3, i % 3) = v[
static_cast<size_t>(i)];
79std::string chemfiles_format(
const std::string &fmt) {
80 const auto f = lower(fmt);
84 if (f ==
"xyz" || f ==
"extxyz") {
96 if (f ==
"data" || f ==
"lammps" || f ==
"lammps-data" || f ==
"lmp") {
105 if (f ==
"poscar" || f ==
"vasp") {
112std::string format_for_path(
const std::string &filename,
113 const std::string &format) {
114 if (!format.empty()) {
115 return chemfiles_format(format);
117 const auto ext = lower(fs::path(filename).extension().
string());
118 if (ext ==
".extxyz") {
121 if (ext ==
".data" || ext ==
".lmp") {
122 return "LAMMPS Data";
124 const auto name = lower(fs::path(filename).filename().
string());
125 if (name ==
"poscar" || name ==
"contcar" || ext ==
".vasp") {
133bool mask_from_property(
const chemfiles::Property &p,
bool frozen_when_true,
134 std::array<bool, 3> &mask) {
135 auto flip = [&](
bool v) {
return frozen_when_true ? v : !v; };
137 case chemfiles::Property::BOOL: {
138 const bool v = flip(p.as_bool());
142 case chemfiles::Property::DOUBLE: {
143 const bool v = flip(p.as_double() != 0.0);
147 case chemfiles::Property::VECTOR3D: {
148 const auto v = p.as_vector3d();
149 mask = {flip(v[0] != 0.0), flip(v[1] != 0.0), flip(v[2] != 0.0)};
157void quiet_chemfiles() {
158 static std::once_flag once;
159 std::call_once(once, [] {
160 chemfiles::set_warning_callback(
161 [](
const std::string &msg) {
EONC_LOG_DEBUG(
"chemfiles: {}", msg); });
177 const auto name = lower(fs::path(std::string(filename)).filename().
string());
178 for (
const char *ext : {
".con",
".convel"}) {
179 const std::string e(ext);
180 if (name.ends_with(e) || name.ends_with(e +
".gz") ||
181 name.ends_with(e +
".zst")) {
189 const fs::path path{std::string(filename)};
190 const auto name = lower(path.filename().string());
191 if (name ==
"poscar" || name ==
"contcar") {
194 static const char *
const kExt[] = {
195 ".xyz",
".extxyz",
".pdb",
".cif",
".mmcif",
".gro",
".data",
196 ".lmp",
".mol2",
".sdf",
".vasp",
".xtc",
".trr",
".dcd",
197 ".nc",
".arc",
".cml",
".cssr",
".mmtf",
".tng"};
198 const auto ext = lower(path.extension().string());
199 return std::find(std::begin(kExt), std::end(kExt), ext) != std::end(kExt);
203 fmt = lower(std::move(fmt));
204 const std::lock_guard<std::mutex> g(format_mutex());
205 input_fmt() = (fmt ==
"con") ? std::string() : fmt;
211 std::vector<double> out;
212 std::istringstream in(text);
217 out.push_back(std::stod(tok, &used));
218 if (used != tok.size()) {
219 throw std::invalid_argument(tok);
221 }
catch (
const std::exception &) {
222 throw std::invalid_argument(
"[Main] cell: '" + tok +
"' is not a number");
225 if (!out.empty() && out.size() != 3 && out.size() != 9) {
226 throw std::invalid_argument(
227 "[Main] cell takes 3 numbers (a b c) or 9 (a 3x3 lattice), got " +
228 std::to_string(out.size()));
234 if (!values.empty() && values.size() != 3 && values.size() != 9) {
235 throw std::invalid_argument(
"[Main] cell takes 3 or 9 numbers");
237 const std::lock_guard<std::mutex> g(format_mutex());
238 config_cell().values = values;
239 config_cell().pbc =
pbc;
243 if (!(angstrom >= 0.0)) {
244 throw std::invalid_argument(
"[Main] default_vacuum must be >= 0");
246 const std::lock_guard<std::mutex> g(format_mutex());
247 config_cell().vacuum = angstrom;
253 bool pbc,
double vacuum) {
255 if (!(vacuum >= 0.0)) {
256 throw std::invalid_argument(
"[Main] default_vacuum must be >= 0");
269 const auto name = lower(fs::path(filename).filename().
string());
270 if (name.ends_with(
".convel") || name.ends_with(
".convel.gz") ||
271 name.ends_with(
".convel.zst")) {
280 const std::string &format) {
289 chemfiles::Trajectory traj(filename,
'r',
290 format_for_path(filename, format));
291 if (traj.nsteps() == 0) {
295 const chemfiles::Frame frame = traj.read();
297 g_last_cell_warning.clear();
298 const auto &cell = frame.cell();
299 const bool file_has_cell = cell.shape() != chemfiles::UnitCell::INFINITE;
300 const auto n =
static_cast<Eigen::Index
>(frame.size());
301 m.
resize(
static_cast<long>(n));
305 VectorXi atomic_nrs(n);
306 const auto &pos = frame.positions();
307 for (Eigen::Index i = 0; i < n; ++i) {
308 const auto &atom = frame[
static_cast<size_t>(i)];
309 const auto z = atom.atomic_number();
311 EONC_LOG_ERROR(
"{}: atom {} has type '{}', which is not an element",
312 filename, i, atom.type());
315 atomic_nrs(i) =
static_cast<int>(*z);
316 masses(i) = atom.mass();
317 for (
int k = 0; k < 3; ++k) {
318 positions(i, k) = pos[
static_cast<size_t>(i)][k];
320 std::array<bool, 3> mask{
false,
false,
false};
321 if (
auto p = atom.get(
"fixed")) {
322 mask_from_property(*p,
true, mask);
323 }
else if (
auto p = atom.get(
"move_mask")) {
324 mask_from_property(*p,
false, mask);
327 m.
setAtomIndex(
static_cast<long>(i),
static_cast<std::int64_t
>(i));
330 std::vector<long> identity(
static_cast<size_t>(n));
331 for (Eigen::Index i = 0; i < n; ++i) {
332 identity[
static_cast<size_t>(i)] =
static_cast<long>(i);
337 bool periodic =
true;
338 const ConfigCell cfg = [] {
339 const std::lock_guard<std::mutex> g(format_mutex());
340 return config_cell();
344 const auto cm = cell.matrix();
345 for (
int i = 0; i < 3; ++i) {
346 for (
int j = 0; j < 3; ++j) {
347 box(i, j) = cm[j][i];
350 }
else if (!cfg.values.empty()) {
351 box = matrix_from_values(cfg.values);
355 Eigen::RowVector3d lo = Eigen::RowVector3d::Zero();
356 Eigen::RowVector3d hi = Eigen::RowVector3d::Zero();
358 lo = positions.colwise().minCoeff();
359 hi = positions.colwise().maxCoeff();
361 for (
int k = 0; k < 3; ++k) {
362 box(k, k) = (hi(k) - lo(k)) + 2.0 * cfg.vacuum;
364 if (box(k, k) <= 0.0) {
368 positions.rowwise() -= (lo - Eigen::RowVector3d::Constant(cfg.vacuum));
370 g_last_cell_warning = std::format(
371 "WARNING: {} has no unit cell and [Main] cell is not set. Using a "
372 "non-periodic orthorhombic box of {:.3f} x {:.3f} x {:.3f} A "
373 "(atom bounding box plus {} A vacuum per side, [Main] "
374 "default_vacuum); atoms were shifted into it. Set [Main] cell and "
375 "[Main] pbc, or add a cell to the file, for a periodic system.",
376 filename, box(0, 0), box(1, 1), box(2, 2), cfg.vacuum);
392 bool have_velocities =
false;
393 if (
const auto vel = frame.velocities()) {
394 have_velocities =
true;
395 for (Eigen::Index i = 0; i < n; ++i) {
396 for (
int k = 0; k < 3; ++k) {
397 v(i, k) = (*vel)[
static_cast<size_t>(i)][k];
402 for (Eigen::Index i = 0; i < n; ++i) {
403 const auto &atom = frame[
static_cast<size_t>(i)];
404 const auto prop = atom.get(
"velo");
405 if (!prop || prop->kind() != chemfiles::Property::VECTOR3D) {
408 have_velocities =
true;
409 const auto vec = prop->as_vector3d();
410 for (
int k = 0; k < 3; ++k) {
411 v(i, k) = vec[
static_cast<size_t>(k)];
415 if (have_velocities) {
420 }
catch (
const std::exception &e) {
421 EONC_LOG_ERROR(
"Failed to read {} with chemfiles: {}", filename, e.what());
429 const std::string &) {
430 EONC_LOG_ERROR(
"Cannot read {}: this eonclient was built without chemfiles. "
431 "Install chemfiles-lib and reconfigure with "
432 "-Dwith_chemfiles=enabled, or convert the file to .con.",