Skip to content

Commit

Permalink
replace old uff with new uff
Browse files Browse the repository at this point in the history
Signed-off-by: Conrad Hübler <[email protected]>
  • Loading branch information
conradhuebler committed Mar 13, 2024
1 parent a32acc5 commit 827d2f5
Show file tree
Hide file tree
Showing 13 changed files with 151 additions and 184 deletions.
39 changes: 9 additions & 30 deletions src/capabilities/simplemd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -722,25 +722,20 @@ void SimpleMD::start()

void SimpleMD::Verlet(double* coord, double* grad)
{
// std::ofstream restart_file("last_stable_curcuma.json");
// auto fallback = WriteRestartInformation();
// restart_file << fallback << std::endl;

#pragma omp parallel
for (int i = 0; i < m_natoms; ++i) {
coord[3 * i + 0] = m_current_geometry[3 * i + 0] + m_dT * m_velocities[3 * i + 0] - 0.5 * grad[3 * i + 0] * m_rmass[3 * i + 0] * m_dt2;
coord[3 * i + 1] = m_current_geometry[3 * i + 1] + m_dT * m_velocities[3 * i + 1] - 0.5 * grad[3 * i + 1] * m_rmass[3 * i + 1] * m_dt2;
coord[3 * i + 2] = m_current_geometry[3 * i + 2] + m_dT * m_velocities[3 * i + 2] - 0.5 * grad[3 * i + 2] * m_rmass[3 * i + 2] * m_dt2;
m_current_geometry[3 * i + 0] = m_current_geometry[3 * i + 0] + m_dT * m_velocities[3 * i + 0] - 0.5 * grad[3 * i + 0] * m_rmass[3 * i + 0] * m_dt2;
m_current_geometry[3 * i + 1] = m_current_geometry[3 * i + 1] + m_dT * m_velocities[3 * i + 1] - 0.5 * grad[3 * i + 1] * m_rmass[3 * i + 1] * m_dt2;
m_current_geometry[3 * i + 2] = m_current_geometry[3 * i + 2] + m_dT * m_velocities[3 * i + 2] - 0.5 * grad[3 * i + 2] * m_rmass[3 * i + 2] * m_dt2;

m_velocities[3 * i + 0] = m_velocities[3 * i + 0] - 0.5 * m_dT * grad[3 * i + 0] * m_rmass[3 * i + 0];
m_velocities[3 * i + 1] = m_velocities[3 * i + 1] - 0.5 * m_dT * grad[3 * i + 1] * m_rmass[3 * i + 1];
m_velocities[3 * i + 2] = m_velocities[3 * i + 2] - 0.5 * m_dT * grad[3 * i + 2] * m_rmass[3 * i + 2];

m_current_geometry[3 * i + 0] = coord[3 * i + 0];
m_current_geometry[3 * i + 1] = coord[3 * i + 1];
m_current_geometry[3 * i + 2] = coord[3 * i + 2];
}
m_Epot = Energy(coord, grad);
double ekin = 0.0;

#pragma omp parallel
for (int i = 0; i < m_natoms; ++i) {
m_velocities[3 * i + 0] -= 0.5 * m_dT * grad[3 * i + 0] * m_rmass[3 * i + 0];
m_velocities[3 * i + 1] -= 0.5 * m_dT * grad[3 * i + 1] * m_rmass[3 * i + 1];
Expand Down Expand Up @@ -769,13 +764,11 @@ void SimpleMD::Rattle(double* coord, double* grad)
* by Richard J. Sadus
* some suff was just ignored or corrected
* like dT^3 -> dT^2 and
* updated velocities of the second atom (minus instread of plus)
* updated velocities of the second atom (minus instead of plus)
*/
// TriggerWriteRestart();

double m_dT_inverse = 1 / m_dT;
// double T_begin = 0, T_rattle_1 = 0, T_verlet = 0;
std::vector<int> moved(m_natoms, 0);
// double kin = 0;
for (int i = 0; i < m_natoms; ++i) {
coord[3 * i + 0] = m_current_geometry[3 * i + 0] + m_dT * m_velocities[3 * i + 0] - 0.5 * grad[3 * i + 0] * m_rmass[3 * i + 0] * m_dt2;
coord[3 * i + 1] = m_current_geometry[3 * i + 1] + m_dT * m_velocities[3 * i + 1] - 0.5 * grad[3 * i + 1] * m_rmass[3 * i + 1] * m_dt2;
Expand All @@ -784,9 +777,7 @@ void SimpleMD::Rattle(double* coord, double* grad)
m_velocities[3 * i + 0] -= 0.5 * m_dT * grad[3 * i + 0] * m_rmass[3 * i + 0];
m_velocities[3 * i + 1] -= 0.5 * m_dT * grad[3 * i + 1] * m_rmass[3 * i + 1];
m_velocities[3 * i + 2] -= 0.5 * m_dT * grad[3 * i + 2] * m_rmass[3 * i + 2];
// kin += m_mass[i] * (m_velocities[3 * i] * m_velocities[3 * i] + m_velocities[3 * i + 1] * m_velocities[3 * i + 1] + m_velocities[3 * i + 2] * m_velocities[3 * i + 2]);
}
// T_begin = kin / (kb_Eh * m_dof);
double iter = 0;
double max_mu = 10;

Expand Down Expand Up @@ -831,23 +822,17 @@ void SimpleMD::Rattle(double* coord, double* grad)
m_velocities[3 * j + 0] -= dx * lambda * 0.5 * m_rmass[j] * m_dT_inverse;
m_velocities[3 * j + 1] -= dy * lambda * 0.5 * m_rmass[j] * m_dT_inverse;
m_velocities[3 * j + 2] -= dz * lambda * 0.5 * m_rmass[j] * m_dT_inverse;
// std::cout << i << " " << j << " " <<lambda << " :: " ;
}
}
}
}
// std::cout << std::endl;
m_Epot = Energy(coord, grad);
double ekin = 0.0;
// kin = 0;

for (int i = 0; i < m_natoms; ++i) {
// kin += m_mass[i] * (m_velocities[3 * i] * m_velocities[3 * i] + m_velocities[3 * i + 1] * m_velocities[3 * i + 1] + m_velocities[3 * i + 2] * m_velocities[3 * i + 2]);

m_velocities[3 * i + 0] -= 0.5 * m_dT * grad[3 * i + 0] * m_rmass[3 * i + 0];
m_velocities[3 * i + 1] -= 0.5 * m_dT * grad[3 * i + 1] * m_rmass[3 * i + 1];
m_velocities[3 * i + 2] -= 0.5 * m_dT * grad[3 * i + 2] * m_rmass[3 * i + 2];
// ekin += m_mass[i] * (m_velocities[3 * i] * m_velocities[3 * i] + m_velocities[3 * i + 1] * m_velocities[3 * i + 1] + m_velocities[3 * i + 2] * m_velocities[3 * i + 2]);

m_current_geometry[3 * i + 0] = coord[3 * i + 0];
m_current_geometry[3 * i + 1] = coord[3 * i + 1];
Expand All @@ -859,8 +844,6 @@ void SimpleMD::Rattle(double* coord, double* grad)
}
m_virial_correction = 0;
iter = 0;
// T_rattle_1 = kin / (kb_Eh * m_dof);
// T_verlet = ekin / (kb_Eh * m_dof);
ekin = 0.0;
while (iter < m_rattle_maxiter) {
iter++;
Expand Down Expand Up @@ -890,7 +873,6 @@ void SimpleMD::Rattle(double* coord, double* grad)
m_velocities[3 * j + 0] -= dx * mu * m_rmass[j];
m_velocities[3 * j + 1] -= dy * mu * m_rmass[j];
m_velocities[3 * j + 2] -= dz * mu * m_rmass[j];
// std::cout << i << " " << j << " " << mu << " :: " ;
}
}
}
Expand All @@ -902,9 +884,6 @@ void SimpleMD::Rattle(double* coord, double* grad)
ekin *= 0.5;
double T = 2.0 * ekin / (kb_Eh * m_dof);
m_unstable = T > 10000 * m_T;
// std::cout << std::endl;

// std::cout << T_begin << " " << T_rattle_1 << " " << T_verlet << " " << T << std::endl;
m_T = T;
}

Expand Down Expand Up @@ -1268,7 +1247,7 @@ double SimpleMD::CleanEnergy(const double* coord, double* grad)

double SimpleMD::FastEnergy(const double* coord, double* grad)
{
m_interface->updateGeometry(coord);
m_interface->updateGeometry(m_current_geometry);

double Energy = m_interface->CalculateEnergy(true);
m_interface->getGradient(grad);
Expand Down
21 changes: 20 additions & 1 deletion src/core/dftd3interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ void DFTD3Interface::CreateParameter()

void DFTD3Interface::UpdateParameters(const json& controller)
{
#pragma message("remove and make consistent")

json parameter = MergeJson(DFTD3Settings, controller);
m_d3_a1 = parameter["d_a1"];
m_d3_a2 = parameter["d_a2"];
Expand All @@ -116,7 +118,24 @@ void DFTD3Interface::UpdateParameters(const json& controller)
m_d3_s9 = parameter["d_s9"];
CreateParameter();

PrintParameter();
// PrintParameter();
}

void DFTD3Interface::UpdateParametersD3(const json& controller)
{
#pragma message("remove and make consistent")
json parameter = MergeJson(DFTD3Settings, controller);
m_d3_a1 = parameter["d3_a1"];
m_d3_a2 = parameter["d3_a2"];
m_d3_alp = parameter["d3_alp"];

m_d3_s6 = parameter["d3_s6"];
m_d3_s8 = parameter["d3_s8"];

m_d3_s9 = parameter["d3_s9"];
CreateParameter();

// PrintParameter();
}

bool DFTD3Interface::InitialiseMolecule(const std::vector<int>& atomtypes)
Expand Down
1 change: 1 addition & 0 deletions src/core/dftd3interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class DFTD3Interface {

double DFTD3Calculation(double* grad = 0);
void UpdateParameters(const json& controller);
void UpdateParametersD3(const json& controller);

void clear();

Expand Down
25 changes: 16 additions & 9 deletions src/core/energycalculator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

EnergyCalculator::EnergyCalculator(const std::string& method, const json& controller)
: m_method(method)
, m_controller(controller)
{
m_charges = []() {
return std::vector<double>{};
Expand Down Expand Up @@ -228,16 +229,17 @@ void EnergyCalculator::setMolecule(const Molecule& molecule)

} else if (std::find(m_ff_methods.begin(), m_ff_methods.end(), m_method) != m_ff_methods.end()) { //

ForceFieldGenerator ff;
ForceFieldGenerator ff(m_controller);
ff.setMolecule(molecule);
ff.Generate();
json parameters = ff.getParameter();
// std::ofstream parameterfile("uff.json");
// parameterfile << parameters;
m_forcefield->setAtomTypes(molecule.Atoms());
m_forcefield->setParameter(parameters);
m_forcefield->setAtomCount(molecule.AtomCount());
// m_forcefield->setMolecule(atoms, geom);
// m_forcefield->Initialise();
// m_forcefield->setAtomCount(molecule.AtomCount());
// m_forcefield->setMolecule(atoms, geom);
// m_forcefield->Initialise();

} else { // Fall back to UFF?
}
Expand Down Expand Up @@ -289,11 +291,6 @@ void EnergyCalculator::updateGeometry(const std::vector<double>& geometry)
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;
}
}

/*
Expand Down Expand Up @@ -329,6 +326,11 @@ void EnergyCalculator::CalculateUFF(bool gradient, bool verbose)
void EnergyCalculator::CalculateTBlite(bool gradient, bool verbose)
{
#ifdef USE_TBLITE
for (int i = 0; i < m_atoms; ++i) {
m_coord[3 * i + 0] = m_geometry(i, 0) / au;
m_coord[3 * i + 1] = m_geometry(i, 1) / au;
m_coord[3 * i + 2] = m_geometry(i, 2) / au;
}
m_tblite->UpdateMolecule(m_coord);

if (gradient) {
Expand All @@ -346,6 +348,11 @@ void EnergyCalculator::CalculateTBlite(bool gradient, bool verbose)
void EnergyCalculator::CalculateXTB(bool gradient, bool verbose)
{
#ifdef USE_XTB
for (int i = 0; i < m_atoms; ++i) {
m_coord[3 * i + 0] = m_geometry(i, 0) / au;
m_coord[3 * i + 1] = m_geometry(i, 1) / au;
m_coord[3 * i + 2] = m_geometry(i, 2) / au;
}
m_xtb->UpdateMolecule(m_coord);

if (gradient) {
Expand Down
4 changes: 2 additions & 2 deletions src/core/energycalculator.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,8 @@ class EnergyCalculator {
eigenUFF* m_uff = NULL;
QMDFF* m_qmdff = NULL;
ForceField* m_forcefield = NULL;
StringList m_uff_methods = { "uff" };
StringList m_ff_methods = { "fuff" };
StringList m_uff_methods = { "fuff" };
StringList m_ff_methods = { "uff", "uff-d3" };
StringList m_qmdff_method = { "qmdff" };
StringList m_tblite_methods = { "ipea1", "gfn1", "gfn2" };
StringList m_xtb_methods = { "gfnff", "xtb-gfn1", "xtb-gfn2" };
Expand Down
40 changes: 29 additions & 11 deletions src/core/forcefield.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,12 @@

ForceField::ForceField(const json& controller)
{
json parameter = MergeJson(UFFParameterJson, controller);

m_threadpool = new CxxThreadPool();
m_threadpool->setProgressBar(CxxThreadPool::ProgressBarType::None);
m_threads = 1;
m_threads = parameter["threads"];
m_gradient_type = parameter["gradient"];
}

void ForceField::UpdateGeometry(const Matrix& geometry)
Expand Down Expand Up @@ -65,7 +68,7 @@ void ForceField::setParameter(const json& parameters)
setDihedrals(parameters["dihedrals"]);
setInversions(parameters["inversions"]);
setvdWs(parameters["vdws"]);

m_parameters = parameters;
AutoRanges();
}

Expand Down Expand Up @@ -171,27 +174,38 @@ void ForceField::setvdWs(const json& vdws)

void ForceField::AutoRanges()
{
for (int i = 0; i < m_threads; ++i) {
ForceFieldThread* thread = new ForceFieldThread(i, m_threads);
int free_threads = m_threads;
int d3 = m_parameters["d3"];
if (d3) {
if (free_threads > 1)
free_threads--;
D3Thread* thread = new D3Thread(m_threads - 1, free_threads);
thread->setParamater(m_parameters);
thread->Initialise(m_atom_types);
m_threadpool->addThread(thread);
m_stored_threads.push_back(thread);
}
for (int i = 0; i < free_threads; ++i) {
ForceFieldThread* thread = new ForceFieldThread(i, free_threads);
thread->setGeometry(m_geometry, false);
m_threadpool->addThread(thread);
m_stored_threads.push_back(thread);
for (int j = int(i * m_bonds.size() / double(m_threads)); j < int((i + 1) * m_bonds.size() / double(m_threads)); ++j)
for (int j = int(i * m_bonds.size() / double(free_threads)); j < int((i + 1) * m_bonds.size() / double(free_threads)); ++j)
thread->addBond(m_bonds[j]);

for (int j = int(i * m_angles.size() / double(m_threads)); j < int((i + 1) * m_angles.size() / double(m_threads)); ++j)
for (int j = int(i * m_angles.size() / double(free_threads)); j < int((i + 1) * m_angles.size() / double(free_threads)); ++j)
thread->addAngle(m_angles[j]);

for (int j = int(i * m_dihedrals.size() / double(m_threads)); j < int((i + 1) * m_dihedrals.size() / double(m_threads)); ++j)
for (int j = int(i * m_dihedrals.size() / double(free_threads)); j < int((i + 1) * m_dihedrals.size() / double(free_threads)); ++j)
thread->addDihedral(m_dihedrals[j]);

for (int j = int(i * m_inversions.size() / double(m_threads)); j < int((i + 1) * m_inversions.size() / double(m_threads)); ++j)
for (int j = int(i * m_inversions.size() / double(free_threads)); j < int((i + 1) * m_inversions.size() / double(free_threads)); ++j)
thread->addInversion(m_inversions[j]);

for (int j = int(i * m_vdWs.size() / double(m_threads)); j < int((i + 1) * m_vdWs.size() / double(m_threads)); ++j)
for (int j = int(i * m_vdWs.size() / double(free_threads)); j < int((i + 1) * m_vdWs.size() / double(free_threads)); ++j)
thread->addvdW(m_vdWs[j]);

for (int j = int(i * m_EQs.size() / double(m_threads)); j < int((i + 1) * m_EQs.size() / double(m_threads)); ++j)
for (int j = int(i * m_EQs.size() / double(free_threads)); j < int((i + 1) * m_EQs.size() / double(free_threads)); ++j)
thread->addEQ(m_EQs[j]);
}
}
Expand Down Expand Up @@ -233,6 +247,7 @@ double ForceField::Calculate(bool gradient, bool verbose)
double eq_energy = 0.0;
double h4_energy = 0.0;
double hh_energy = 0.0;
#pragma omp parallel for
for (int i = 0; i < m_stored_threads.size(); ++i) {
m_stored_threads[i]->UpdateGeometry(m_geometry, gradient);
}
Expand All @@ -241,7 +256,10 @@ double ForceField::Calculate(bool gradient, bool verbose)
m_threadpool->setActiveThreadCount(m_threads);

m_threadpool->StartAndWait();
m_threadpool->setWakeUp(m_threadpool->WakeUp() / 2.0);
m_threadpool->setWakeUp(m_threadpool->WakeUp() / 2);
omp_set_num_threads(m_threads);
Eigen::setNbThreads(m_threads);
#pragma omp parallel for
for (int i = 0; i < m_stored_threads.size(); ++i) {
bond_energy += m_stored_threads[i]->BondEnergy();
angle_energy += m_stored_threads[i]->AngleEnergy();
Expand Down
15 changes: 14 additions & 1 deletion src/core/forcefield.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,23 @@
#include "json.hpp"
using json = nlohmann::json;

static const json FFJson = {
{ "threads", 1 },
{ "gradient", 1 }
};

class ForceField {

public:
ForceField(const json& controller);
~ForceField();

inline void setAtomCount(int atom) { m_natoms = atom; }
// inline void setAtomCount(int atom) { m_natoms = atom; }
inline void setAtomTypes(const std::vector<int>& atom_types)
{
m_atom_types = atom_types;
m_natoms = atom_types.size();
}
void UpdateGeometry(const Matrix& geometry);
inline void UpdateGeometry(const double* coord);
inline void UpdateGeometry(const std::vector<std::array<double, 3>>& geometry);
Expand All @@ -78,12 +88,15 @@ class ForceField {
void setvdWs(const json& vdws);

Matrix m_geometry, m_gradient;
std::vector<int> m_atom_types;
int m_natoms = 0;
int m_threads = 1;
int m_gradient_type = 1;
std::vector<Bond> m_bonds;
std::vector<Angle> m_angles;
std::vector<Dihedral> m_dihedrals;
std::vector<Inversion> m_inversions;
std::vector<vdW> m_vdWs;
std::vector<EQ> m_EQs;
json m_parameters;
};
Loading

0 comments on commit 827d2f5

Please sign in to comment.