From a32acc51b662835297fd704cc3b8d841dfc3755f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Conrad=20H=C3=BCbler?= Date: Tue, 5 Mar 2024 23:22:34 +0100 Subject: [PATCH] replace some vector/array matrices with eigen matrices MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Conrad Hübler --- src/capabilities/curcumaopt.cpp | 4 +- src/capabilities/hessian.cpp | 22 ++-- src/capabilities/hessian.h | 2 +- src/core/energycalculator.cpp | 14 ++- src/core/energycalculator.h | 4 +- src/core/forcefield.cpp | 2 +- src/core/molecule.cpp | 184 ++++++++++++++++++-------------- src/core/molecule.h | 8 +- src/tools/formats.h | 24 ++++- 9 files changed, 154 insertions(+), 110 deletions(-) diff --git a/src/capabilities/curcumaopt.cpp b/src/capabilities/curcumaopt.cpp index 2b8df43..717bb40 100644 --- a/src/capabilities/curcumaopt.cpp +++ b/src/capabilities/curcumaopt.cpp @@ -134,7 +134,7 @@ void CurcumaOpt::ProcessMoleculesSerial(const std::vector& molecules) #endif if (m_hessian) { - Hessian hess(m_method, m_defaults, m_threads); + Hessian hess(m_method, m_defaults, false); hess.setMolecule(*iter); hess.CalculateHessian(m_hessian); } @@ -191,7 +191,7 @@ void CurcumaOpt::ProcessMolecules(const std::vector& molecules) Molecule* mol2 = new Molecule(thread->getMolecule()); if (m_hessian) { - Hessian hess(m_method, m_defaults, m_threads); + Hessian hess(m_method, m_defaults, false); hess.setMolecule(mol2); hess.CalculateHessian(m_hessian); } diff --git a/src/capabilities/hessian.cpp b/src/capabilities/hessian.cpp index 1d2b49b..df3aa47 100644 --- a/src/capabilities/hessian.cpp +++ b/src/capabilities/hessian.cpp @@ -68,17 +68,17 @@ void HessianThread::Numerical() m_geom_ip_jm = m_molecule.Coords(); m_geom_im_jm = m_molecule.Coords(); - m_geom_ip_jp[m_i][m_xi] += m_d; - m_geom_ip_jp[m_j][m_xj] += m_d; + m_geom_ip_jp(m_i, m_xi) += m_d; + m_geom_ip_jp(m_j, m_xj) += m_d; - m_geom_im_jp[m_i][m_xi] -= m_d; - m_geom_im_jp[m_j][m_xj] += m_d; + m_geom_im_jp(m_i, m_xi) -= m_d; + m_geom_im_jp(m_j, m_xj) += m_d; - m_geom_ip_jm[m_i][m_xi] += m_d; - m_geom_ip_jm[m_j][m_xj] -= m_d; + m_geom_ip_jm(m_i, m_xi) += m_d; + m_geom_ip_jm(m_j, m_xj) -= m_d; - m_geom_im_jm[m_i][m_xi] -= m_d; - m_geom_im_jm[m_j][m_xj] -= m_d; + m_geom_im_jm(m_i, m_xi) -= m_d; + m_geom_im_jm(m_j, m_xj) -= m_d; EnergyCalculator energy(m_method, m_controller); energy.setMolecule(m_molecule); @@ -104,7 +104,7 @@ void HessianThread::Seminumerical() m_geom_ip_jp = m_molecule.Coords(); m_geom_im_jp = m_molecule.Coords(); - m_geom_ip_jp[m_i][m_xi] += m_d; + m_geom_ip_jp(m_i, m_xi) += m_d; // std::cout << m_controller << std::endl; EnergyCalculator energy(m_method, m_controller); @@ -115,7 +115,7 @@ void HessianThread::Seminumerical() energy.CalculateEnergy(true, false); Matrix gradientp = energy.Gradient(); - m_geom_im_jp[m_i][m_xi] -= m_d; + m_geom_im_jp(m_i, m_xi) -= m_d; energy.updateGeometry(m_geom_im_jp); energy.CalculateEnergy(true, false); @@ -130,7 +130,7 @@ Hessian::Hessian(const std::string& method, const json& controller, bool silent) { UpdateController(controller); - m_threads = m_controller["threads"]; + m_threads = m_defaults["threads"]; /* Yeah, thats not really correct, but it works a bit */ if (m_method.compare("gfnff") == 0) { m_scale_functions = [](double val) -> double { diff --git a/src/capabilities/hessian.h b/src/capabilities/hessian.h index 8023da3..e993e3d 100644 --- a/src/capabilities/hessian.h +++ b/src/capabilities/hessian.h @@ -71,7 +71,7 @@ class HessianThread : public CxxThread { json m_controller, m_parameter; Molecule m_molecule; Matrix m_gradient; - std::vector> m_geom_ip_jp, m_geom_im_jp, m_geom_ip_jm, m_geom_im_jm; + Geometry m_geom_ip_jp, m_geom_im_jp, m_geom_ip_jm, m_geom_im_jm; int m_i, m_j, m_xi, m_xj; bool m_fullnumerical = true; double m_dd = 0; diff --git a/src/core/energycalculator.cpp b/src/core/energycalculator.cpp index 20a8b96..52ba59b 100644 --- a/src/core/energycalculator.cpp +++ b/src/core/energycalculator.cpp @@ -285,6 +285,18 @@ void EnergyCalculator::updateGeometry(const std::vector& geometry) } // m_containsNaN = std::isnan(m_geometry[m_atoms - 1][0]); } + +void EnergyCalculator::updateGeometry(const Matrix& geometry) +{ + m_geometry = geometry; + for (int i = 0; i < m_atoms; ++i) { + m_coord[3 * i + 0] = geometry(i, 0) / au; + m_coord[3 * i + 1] = geometry(i, 1) / au; + m_coord[3 * i + 2] = geometry(i, 2) / au; + } +} + +/* void EnergyCalculator::updateGeometry(const std::vector>& geometry) { for (int i = 0; i < m_atoms; ++i) { @@ -297,7 +309,7 @@ void EnergyCalculator::updateGeometry(const std::vector>& m_geometry(i, 2) = geometry[i][2]; } } - +*/ double EnergyCalculator::CalculateEnergy(bool gradient, bool verbose) { m_ecengine(gradient, verbose); diff --git a/src/core/energycalculator.h b/src/core/energycalculator.h index 7f0960e..6c4487b 100644 --- a/src/core/energycalculator.h +++ b/src/core/energycalculator.h @@ -56,8 +56,8 @@ class EnergyCalculator { void updateGeometry(const double* coord); void updateGeometry(const std::vector& geometry); - void updateGeometry(const std::vector>& geometry); - + // void updateGeometry(const std::vector>& geometry); + void updateGeometry(const Matrix& geometry); void updateGeometry(const Eigen::VectorXd& geometry); void getGradient(double* coord); diff --git a/src/core/forcefield.cpp b/src/core/forcefield.cpp index 27be36a..b31c39e 100644 --- a/src/core/forcefield.cpp +++ b/src/core/forcefield.cpp @@ -30,7 +30,7 @@ ForceField::ForceField(const json& controller) { m_threadpool = new CxxThreadPool(); m_threadpool->setProgressBar(CxxThreadPool::ProgressBarType::None); - m_threads = 16; + m_threads = 1; } void ForceField::UpdateGeometry(const Matrix& geometry) diff --git a/src/core/molecule.cpp b/src/core/molecule.cpp index ee1a0be..eaf0464 100644 --- a/src/core/molecule.cpp +++ b/src/core/molecule.cpp @@ -167,7 +167,7 @@ json Molecule::ExportJson() const structure["elements"] = Tools::Vector2String(m_atoms); structure["name"] = m_name; for (int i = 0; i < m_atoms.size(); ++i) { - structure["atom" + std::to_string(i)] = Tools::DoubleVector2String({ m_geometry[i][0], m_geometry[i][1], m_geometry[i][2] }); + structure["atom" + std::to_string(i)] = Tools::DoubleVector2String({ m_geometry(i, 0), m_geometry(i, 1), m_geometry(i, 2) }); } structure["charge"] = m_charge; return structure; @@ -195,9 +195,10 @@ void Molecule::ImportJson(const json& molecule) m_name = molecule["name"]; m_atoms = Tools::String2Vector(molecule["elements"]); m_charge = molecule["charge"]; + m_geometry = Eigen::MatrixXd::Zero(atoms, 3); for (int i = 0; i < atoms; ++i) { auto position = Tools::String2DoubleVec(molecule["atom" + std::to_string(i)], "|"); - m_geometry.push_back(std::array({ position[0], position[1], position[2] })); + m_geometry.row(i) = Eigen::Vector3d(position[0], position[1], position[2]); //(std::array({ position[0], position[1], position[2] })); } } void Molecule::Initialise(const int* attyp, const double* coord, const int natoms, const double charge, const int spin) @@ -205,8 +206,14 @@ void Molecule::Initialise(const int* attyp, const double* coord, const int natom m_charge = charge; m_spin = spin; Molecule mol; + m_geometry = Eigen::MatrixXd::Zero(natoms, 3); + for (int i = 0; i < natoms; ++i) { - m_geometry.push_back({ coord[3 * i], coord[3 * i + 1], coord[3 * i + 2] }); + m_geometry(i, 0) = coord[3 * i]; + m_geometry(i, 1) = coord[3 * i + 1]; + m_geometry(i, 2) = coord[3 * i + 2]; + + // m_geometry.push_back({ coord[3 * i], coord[3 * i + 1], coord[3 * i + 2] }); m_atoms.push_back(attyp[i]); m_mass += Elements::AtomicMass[attyp[i]]; } @@ -226,7 +233,7 @@ void Molecule::print_geom(bool moreinfo) const std::cout << AtomCount() << std::endl; std::cout << Name() << " " << std::setprecision(12) << Energy() << std::endl; for (int i = 0; i < AtomCount(); i++) { - printf("%s %8.5f %8.5f %8.5f\n", Elements::ElementAbbr[m_atoms[i]].c_str(), m_geometry[i][0], m_geometry[i][1], m_geometry[i][2]); + printf("%s %8.5f %8.5f %8.5f\n", Elements::ElementAbbr[m_atoms[i]].c_str(), m_geometry(i, 0), m_geometry(i, 1), m_geometry(i, 2)); } if (moreinfo) { std::cout << std::endl @@ -246,7 +253,7 @@ int Molecule::Check() const return 0; for (int i = 0; i < AtomCount(); ++i) { - if (std::isnan(m_geometry[i][0]) || std::isnan(m_geometry[i][1]) || std::isnan(m_geometry[i][2])) + if (std::isnan(m_geometry(i, 0)) || std::isnan(m_geometry(i, 1)) || std::isnan(m_geometry(i, 2))) return 2; for (int j = 0; j < i; ++j) { if (CalculateDistance(i, j) < 1e-1) @@ -273,7 +280,7 @@ void Molecule::printFragmente() for (std::size_t i = 0; i < m_fragments.size(); ++i) { for (const auto& atom : m_fragments[i]) { - printf("%s(%i) %8.5f %8.5f %8.5f\n", Elements::ElementAbbr[m_atoms[atom]].c_str(), i + 1, m_geometry[atom][0], m_geometry[atom][1], m_geometry[atom][2]); + printf("%s(%i) %8.5f %8.5f %8.5f\n", Elements::ElementAbbr[m_atoms[atom]].c_str(), i + 1, m_geometry(atom, 0), m_geometry(atom, 1), m_geometry(atom, 2)); } } } @@ -281,16 +288,20 @@ void Molecule::printFragmente() void Molecule::printAtom(int i) const { if (i < AtomCount()) - printf("%s %8.5f %8.5f %8.5f", Elements::ElementAbbr[m_atoms[i]].c_str(), m_geometry[i][0], m_geometry[i][1], m_geometry[i][2]); + printf("%s %8.5f %8.5f %8.5f", Elements::ElementAbbr[m_atoms[i]].c_str(), m_geometry(i, 0), m_geometry(i, 1), m_geometry(i, 2)); } void Molecule::InitialiseEmptyGeometry(int atoms) { + m_geometry = Eigen::MatrixXd::Zero(atoms, 3); + /* for (int i = 0; i < atoms; i++) { std::array atom = { 0, 0, 0 }; - m_geometry.push_back(atom); - } + //m_geometry.push_back(atom); + m_geometry.conservativeResize(m_geometry.rows() + 1, m_geometry.cols()); + m_geometry.row(m_geometry.rows() - 1) = Eigen::Vector3d(vector[0], vector[1], vector[2]); + }*/ m_dirty = true; } @@ -298,7 +309,10 @@ bool Molecule::addPair(const std::pair& atom) { bool exist = true; // const std::array at = { atom.second(0), atom.second(1), atom.second(2)}; - m_geometry.push_back({ atom.second(0), atom.second(1), atom.second(2) }); + m_geometry.conservativeResize(m_geometry.rows() + 1, 3); + m_geometry.row(m_geometry.rows() - 1) = atom.second; + + // m_geometry.push_back({ atom.second(0), atom.second(1), atom.second(2) }); m_atoms.push_back(atom.first); m_mass += Elements::AtomicMass[atom.first]; @@ -505,33 +519,42 @@ double Molecule::CalculateDistance(int i, int j) const if (i >= AtomCount() || j >= AtomCount()) return 0; - double x_i = m_geometry[i][0]; - double x_j = m_geometry[j][0]; + double x_i = m_geometry(i, 0); + double x_j = m_geometry(j, 0); - double y_i = m_geometry[i][1]; - double y_j = m_geometry[j][1]; + double y_i = m_geometry(i, 1); + double y_j = m_geometry(j, 1); - double z_i = m_geometry[i][2]; - double z_j = m_geometry[j][2]; + double z_i = m_geometry(i, 2); + double z_j = m_geometry(j, 2); return sqrt((((x_i - x_j) * (x_i - x_j)) + ((y_i - y_j) * (y_i - y_j)) + ((z_i - z_j) * (z_i - z_j)))); } - +/* double Molecule::DotProduct(std::array pos1, std::array pos2) const { return pos1[0]*pos2[0]+pos1[1]*pos2[1]+pos1[2]*pos2[2]; } - -double Molecule::CalculateAngle(int atom1, int atom2, int atom3) const -{ +*/ +double Molecule::CalculateAngle(int i, int j, int k) const +{ +#pragma message("how was it really, check for later") + Eigen::Vector3d rij = m_geometry.row(i) - m_geometry.row(j); + auto nij = rij / rij.norm(); + Eigen::Vector3d rkj = m_geometry.row(k) - m_geometry.row(j); + auto nkj = rkj / rkj.norm(); + double costheta = (rij.dot(rkj) / (sqrt(rij.dot(rij) * rkj.dot(rkj)))); + return acos(costheta); + /* std::array atom_0 = { m_geometry[atom2] }; // Proton std::array atom_1 = { m_geometry[atom3] }; // Acceptor std::array atom_2 = { m_geometry[atom1] }; // Donor std::array vec_1 = { atom_0[0]-atom_1[0], atom_0[1]-atom_1[1], atom_0[2]-atom_1[2] }; std::array vec_2 = { atom_0[0]-atom_2[0], atom_0[1]-atom_2[1], atom_0[2]-atom_2[2] }; - + return acos(DotProduct(vec_1,vec_2)/(sqrt(DotProduct(vec_1,vec_1)*DotProduct(vec_2,vec_2))))*360/2.0/pi; +*/ } void Molecule::ParseString(const std::string& internal, std::vector& elements) @@ -581,9 +604,9 @@ void Molecule::setAtom(const std::string& internal, int i) double x = r_ik + r_ij*cos(omega); double y = r_ij*sin(omega); double z = 0; - m_geometry[i][0] = x; - m_geometry[i][1] = y; - m_geometry[i][2] = z; + m_geometry(i, 0) = x; + m_geometry(i, 1) = y; + m_geometry(i, 2) = z; } m_dirty = true; } @@ -604,9 +627,9 @@ void Molecule::setXYZ(const std::string& internal, int i) double y = stod(elements[2]); double z = stod(elements[3]); - m_geometry[i][0] = x; - m_geometry[i][1] = y; - m_geometry[i][2] = z; + m_geometry(i, 0) = x; + m_geometry(i, 1) = y; + m_geometry(i, 2) = z; } m_dirty = true; @@ -615,7 +638,7 @@ void Molecule::setXYZ(const std::string& internal, int i) void Molecule::clear() { m_atoms.clear(); - m_geometry.clear(); + // m_geometry.clear(); m_dirty = true; } @@ -680,13 +703,7 @@ Molecule Molecule::getFragmentMolecule(int fragment) const Geometry Molecule::getGeometry(bool protons) const { if (protons) { - Geometry geometry(m_geometry.size(), 3); - for (int i = 0; i < m_geometry.size(); ++i) { - geometry(i, 0) = m_geometry[i][0]; - geometry(i, 1) = m_geometry[i][1]; - geometry(i, 2) = m_geometry[i][2]; - } - return geometry; + return m_geometry; } else { std::vector indicies; for (int i = 0; i < m_geometry.size(); ++i) { @@ -697,9 +714,9 @@ Geometry Molecule::getGeometry(bool protons) const Geometry geometry(indicies.size(), 3); int index = 0; for (int i : indicies) { - geometry(index, 0) = m_geometry[i][0]; - geometry(index, 1) = m_geometry[i][1]; - geometry(index, 2) = m_geometry[i][2]; + geometry(index, 0) = m_geometry(i, 0); + geometry(index, 1) = m_geometry(i, 1); + geometry(index, 2) = m_geometry(i, 2); index++; } return geometry; @@ -722,17 +739,17 @@ Geometry Molecule::getGeometry(const IntPair& pair, bool protons) const if (protons) { for (int i = start; i < end; ++i) { - geometry(index, 0) = m_geometry[i][0]; - geometry(index, 1) = m_geometry[i][1]; - geometry(index, 2) = m_geometry[i][2]; + geometry(index, 0) = m_geometry(i, 0); + geometry(index, 1) = m_geometry(i, 1); + geometry(index, 2) = m_geometry(i, 2); index++; } } else { for (int i = start; i < end; ++i) { if (m_atoms[i] != 1) { - geometry(index, 0) = m_geometry[i][0]; - geometry(index, 1) = m_geometry[i][1]; - geometry(index, 2) = m_geometry[i][2]; + geometry(index, 0) = m_geometry(i, 0); + geometry(index, 1) = m_geometry(i, 1); + geometry(index, 2) = m_geometry(i, 2); index++; } } @@ -746,17 +763,17 @@ Geometry Molecule::getGeometry(std::vector atoms, bool protons) const int index = 0; if (protons) { for (int i : atoms) { - geometry(index, 0) = m_geometry[i][0]; - geometry(index, 1) = m_geometry[i][1]; - geometry(index, 2) = m_geometry[i][2]; + geometry(index, 0) = m_geometry(i, 0); + geometry(index, 1) = m_geometry(i, 1); + geometry(index, 2) = m_geometry(i, 2); index++; } } else { for (int i : atoms) { if (m_atoms[i] != 1) { - geometry(index, 0) = m_geometry[i][0]; - geometry(index, 1) = m_geometry[i][1]; - geometry(index, 2) = m_geometry[i][2]; + geometry(index, 0) = m_geometry(i, 0); + geometry(index, 1) = m_geometry(i, 1); + geometry(index, 2) = m_geometry(i, 2); index++; } } @@ -778,13 +795,14 @@ bool Molecule::setGeometry(const Geometry &geometry) return false; m_dirty = true; - + m_geometry = geometry; + /* for (int i = 0; i < m_geometry.size(); ++i) { - m_geometry[i][0] = geometry(i, 0); - m_geometry[i][1] = geometry(i, 1); - m_geometry[i][2] = geometry(i, 2); + m_geometry(i,0) = geometry(i, 0); + m_geometry(i,1) = geometry(i, 1); + m_geometry(i,2) = geometry(i, 2); } - + */ return true; } @@ -797,18 +815,18 @@ bool Molecule::setGeometryByFragment(const Geometry& geometry, int fragment, boo int index = 0; if (protons) { for (int i : frag) { - m_geometry[i][0] = geometry(index, 0); - m_geometry[i][1] = geometry(index, 1); - m_geometry[i][2] = geometry(index, 2); + m_geometry(i, 0) = geometry(index, 0); + m_geometry(i, 1) = geometry(index, 1); + m_geometry(i, 2) = geometry(index, 2); index++; } } else { for (int i : frag) { if (Atom(i).first == 1) continue; - m_geometry[i][0] = geometry(index, 0); - m_geometry[i][1] = geometry(index, 1); - m_geometry[i][2] = geometry(index, 2); + m_geometry(i, 0) = geometry(index, 0); + m_geometry(i, 1) = geometry(index, 1); + m_geometry(i, 2) = geometry(index, 2); index++; } } @@ -876,9 +894,9 @@ Eigen::Vector3d Molecule::COM(bool protons, int fragment) Eigen::Vector3d com = { 0, 0, 0 }; for (int i = 0; i < m_geometry.size(); ++i) { double mass = Elements::AtomicMass[m_atoms[i]]; - com(0) += mass * m_geometry[i][0]; - com(1) += mass * m_geometry[i][1]; - com(2) += mass * m_geometry[i][2]; + com(0) += mass * m_geometry(i, 0); + com(1) += mass * m_geometry(i, 1); + com(2) += mass * m_geometry(i, 2); } com(0) /= m_mass; com(1) /= m_mass; @@ -901,9 +919,9 @@ std::vector Molecule::CalculateDipoleMoments(const std::vector for (int i : m_fragments[f]) { double m = Elements::AtomicMass[m_atoms[i]]; mass += m; - pos(0) += m * m_geometry[i][0]; - pos(1) += m * m_geometry[i][1]; - pos(2) += m * m_geometry[i][2]; + pos(0) += m * m_geometry(i, 0); + pos(1) += m * m_geometry(i, 1); + pos(2) += m * m_geometry(i, 2); } pos(0) /= mass; pos(1) /= mass; @@ -912,9 +930,9 @@ std::vector Molecule::CalculateDipoleMoments(const std::vector double scale = 3; if (scaling.size() > i) scale = scaling[i]; - dipole(0) += m_charges[i] * (m_geometry[i][0] - pos(0)) * scale; - dipole(1) += m_charges[i] * (m_geometry[i][1] - pos(1)) * scale; - dipole(2) += m_charges[i] * (m_geometry[i][2] - pos(2)) * scale; + dipole(0) += m_charges[i] * (m_geometry(i, 0) - pos(0)) * scale; + dipole(1) += m_charges[i] * (m_geometry(i, 1) - pos(1)) * scale; + dipole(2) += m_charges[i] * (m_geometry(i, 2) - pos(2)) * scale; // std::cout << scale << " "; } // std::cout << std::endl; @@ -935,9 +953,9 @@ Position Molecule::CalculateDipoleMoment(const std::vector& scaling) con for (int i = 0; i < m_geometry.size(); ++i) { double m = Elements::AtomicMass[m_atoms[i]]; mass += m; - pos(0) += m * m_geometry[i][0]; - pos(1) += m * m_geometry[i][1]; - pos(2) += m * m_geometry[i][2]; + pos(0) += m * m_geometry(i, 0); + pos(1) += m * m_geometry(i, 1); + pos(2) += m * m_geometry(i, 2); } pos(0) /= mass; pos(1) /= mass; @@ -946,9 +964,9 @@ Position Molecule::CalculateDipoleMoment(const std::vector& scaling) con double scale = 3; if (scaling.size() > i) scale = scaling[i]; - dipole(0) += m_charges[i] * (m_geometry[i][0] - pos(0)) * scale; - dipole(1) += m_charges[i] * (m_geometry[i][1] - pos(1)) * scale; - dipole(2) += m_charges[i] * (m_geometry[i][2] - pos(2)) * scale; + dipole(0) += m_charges[i] * (m_geometry(i, 0) - pos(0)) * scale; + dipole(1) += m_charges[i] * (m_geometry(i, 1) - pos(1)) * scale; + dipole(2) += m_charges[i] * (m_geometry(i, 2) - pos(2)) * scale; // std::cout << scale << " "; } // std::cout << std::endl; @@ -960,8 +978,8 @@ std::pair Molecule::GyrationRadius(bool protons, int fragment) Eigen::Vector3d com = COM(protons, fragment); double gyr = 0, gyr_mass = 0; for (int i = 0; i < m_geometry.size(); ++i) { - gyr += ((com(0) - m_geometry[i][0]) * (com(0) - m_geometry[i][0]) + (com(1) - m_geometry[i][1]) * (com(1) - m_geometry[i][1]) + (com(2) - m_geometry[i][2]) * (com(2) - m_geometry[i][2])); - gyr_mass += Elements::AtomicMass[m_atoms[i]] * ((com(0) - m_geometry[i][0]) * (com(0) - m_geometry[i][0]) + (com(1) - m_geometry[i][1]) * (com(1) - m_geometry[i][1]) + (com(2) - m_geometry[i][2]) * (com(2) - m_geometry[i][2])); + gyr += ((com(0) - m_geometry(i, 0)) * (com(0) - m_geometry(i, 0)) + (com(1) - m_geometry(i, 1)) * (com(1) - m_geometry(i, 1)) + (com(2) - m_geometry(i, 2)) * (com(2) - m_geometry(i, 2))); + gyr_mass += Elements::AtomicMass[m_atoms[i]] * ((com(0) - m_geometry(i, 0)) * (com(0) - m_geometry(i, 0)) + (com(1) - m_geometry(i, 1)) * (com(1) - m_geometry(i, 1)) + (com(2) - m_geometry(i, 2)) * (com(2) - m_geometry(i, 2))); } gyr /= double(m_geometry.size()); gyr_mass /= double(m_mass); @@ -970,7 +988,7 @@ std::pair Molecule::GyrationRadius(bool protons, int fragment) std::pair Molecule::Atom(int i) const { - return std::pair(m_atoms[i], { m_geometry[i][0], m_geometry[i][1], m_geometry[i][2] }); + return std::pair(m_atoms[i], { m_geometry(i, 0), m_geometry(i, 1), m_geometry(i, 2) }); } void Molecule::writeXYZFile(const std::string& filename) const @@ -1001,7 +1019,7 @@ std::string Molecule::Header() const std::string Molecule::Atom2String(int i) const { #ifdef GCC - return fmt::format("{} {:f} {:f} {:f}\n", Elements::ElementAbbr[m_atoms[i]].c_str(), m_geometry[i][0], m_geometry[i][1], m_geometry[i][2]); + return fmt::format("{} {:f} {:f} {:f}\n", Elements::ElementAbbr[m_atoms[i]].c_str(), m_geometry(i, 0), m_geometry(i, 1), m_geometry(i, 2)); #else return fmt::format("{} {:} {:} {:}\n", Elements::ElementAbbr[m_atoms[i]].c_str(), m_geometry[i][0], m_geometry[i][1], m_geometry[i][2]); #endif @@ -1124,9 +1142,9 @@ void Molecule::CalculateRotationalConstants() for (int i = 0; i < AtomCount(); ++i) { double m = Elements::AtomicMass[m_atoms[i]]; mass += m; - pos(0) += m * m_geometry[i][0]; - pos(1) += m * m_geometry[i][1]; - pos(2) += m * m_geometry[i][2]; + pos(0) += m * m_geometry(i, 0); + pos(1) += m * m_geometry(i, 1); + pos(2) += m * m_geometry(i, 2); } pos(0) /= mass; pos(1) /= mass; diff --git a/src/core/molecule.h b/src/core/molecule.h index 974ad0f..4ca3e94 100644 --- a/src/core/molecule.h +++ b/src/core/molecule.h @@ -43,7 +43,7 @@ struct Mol { std::string m_commentline; - std::vector> m_geometry; + Geometry m_geometry; std::vector> m_bonds; std::vector m_atoms; }; @@ -86,7 +86,7 @@ class Molecule void InitialiseConnectedMass(double scaling = 1.3, bool protons = true); inline double ConnectedMass(int atom) const { return m_connect_mass[atom]; } double CalculateAngle(int atom1, int atom2, int atom3) const; - double DotProduct(std::array pos1, std::array pos2) const; + // double DotProduct(std::array pos1, std::array pos2) const; void clear(); @@ -208,7 +208,7 @@ class Molecule std::pair DistanceMatrix() const; - std::vector> Coords() const { return m_geometry; } + Geometry Coords() const { return m_geometry; } Matrix AlignmentAxes() const { return m_alignmentAxes; } @@ -235,7 +235,7 @@ class Molecule void InitialiseEmptyGeometry(int atoms); int m_charge = 0, m_spin = 0; - std::vector> m_geometry; + Geometry m_geometry; std::vector m_atoms; std::vector m_charges; diff --git a/src/tools/formats.h b/src/tools/formats.h index f1ae051..164b766 100644 --- a/src/tools/formats.h +++ b/src/tools/formats.h @@ -104,7 +104,9 @@ inline Mol XYZString2Mol(const std::string& coord) if (i > 1) { auto pair = Line2Atoms(line); molecule.m_atoms.push_back(pair.first); - molecule.m_geometry.push_back(pair.second); + molecule.m_geometry.conservativeResize(molecule.m_geometry.rows() + 1, molecule.m_geometry.cols()); + molecule.m_geometry.row(molecule.m_geometry.rows() - 1) = Eigen::Vector3d(pair.second[0], pair.second[1], pair.second[2]); + // push_back(pair.second); } if (i - 1 == atoms) { break; @@ -132,6 +134,7 @@ inline Mol XYZ2Mol(const std::string& filename) try { molecule.m_number_atoms = stoi(line); atoms = stoi(line); + molecule.m_geometry = Eigen::MatrixXd::Zero(atoms, 3); } catch (const std::invalid_argument& arg) { atoms = 0; } @@ -142,7 +145,9 @@ inline Mol XYZ2Mol(const std::string& filename) if (i > 1) { auto pair = Line2Atoms(line); molecule.m_atoms.push_back(pair.first); - molecule.m_geometry.push_back(pair.second); + // molecule.m_geometry.push_back(pair.second); + // molecule.m_geometry.conservativeResize(molecule.m_geometry.rows() + 1, molecule.m_geometry.cols()); + molecule.m_geometry.row(molecule.m_atoms.size() - 1) = Eigen::Vector3d(pair.second[0], pair.second[1], pair.second[2]); } if (i - 1 == atoms) { break; @@ -158,6 +163,7 @@ inline Mol XYZ2Mol(const std::string& filename) inline Mol Coord2Mol(const std::string& filename) { Mol molecule; + molecule.m_geometry = Eigen::MatrixXd::Zero(0, 3); auto file = new std::ifstream(filename); for (std::string line; getline(*file, line);) { @@ -170,7 +176,9 @@ inline Mol Coord2Mol(const std::string& filename) vector[0] = std::stod(strings[0]) * au; vector[1] = std::stod(strings[1]) * au; vector[2] = std::stod(strings[2]) * au; - molecule.m_geometry.push_back(vector); + // molecule.m_geometry.push_back(vector); + molecule.m_geometry.conservativeResize(molecule.m_atoms.size(), molecule.m_geometry.cols()); + molecule.m_geometry.row(molecule.m_geometry.rows() - 1) = Eigen::Vector3d(vector[0], vector[1], vector[2]); } catch (const std::invalid_argument& arg) { } } @@ -193,7 +201,9 @@ inline Mol SDF2Mol(const std::string& filename) vector[0] = std::stod(strings[0]); vector[1] = std::stod(strings[1]); vector[2] = std::stod(strings[2]); - molecule.m_geometry.push_back(vector); + // molecule.m_geometry.push_back(vector); + molecule.m_geometry.conservativeResize(molecule.m_atoms.size(), molecule.m_geometry.cols()); + molecule.m_geometry.row(molecule.m_geometry.rows() - 1) = Eigen::Vector3d(vector[0], vector[1], vector[2]); } catch (const std::invalid_argument& arg) { } } @@ -204,6 +214,8 @@ inline Mol SDF2Mol(const std::string& filename) inline Mol Mol22Mol(const std::string& filename) { Mol molecule; + molecule.m_geometry = Eigen::MatrixXd::Zero(0, 3); + int read_atom = false, read_bond = false; auto file = new std::ifstream(filename); for (std::string line; getline(*file, line);) { @@ -216,7 +228,9 @@ inline Mol Mol22Mol(const std::string& filename) vector[0] = std::stod(strings[2]); vector[1] = std::stod(strings[3]); vector[2] = std::stod(strings[4]); - molecule.m_geometry.push_back(vector); + // molecule.m_geometry.push_back(vector); + molecule.m_geometry.conservativeResize(molecule.m_atoms.size(), molecule.m_geometry.cols()); + molecule.m_geometry.row(molecule.m_geometry.rows() - 1) = Eigen::Vector3d(vector[0], vector[1], vector[2]); } catch (const std::invalid_argument& arg) { } }