Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New forcefield framework #1370

Merged
merged 24 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
de67512
Import stable version of CppNumericalSolvers
ghutchis Jul 11, 2022
2639c2a
Initial import of previous code
ghutchis Jul 11, 2022
1fc4979
More work
ghutchis Jul 11, 2022
d193b1f
Add template for FLAME minimizer
ghutchis Jul 11, 2022
4ffafc4
Remove qDebug
ghutchis Jul 11, 2022
ca85bd7
Merge Lennard-Jones parameters into a header
ghutchis Jul 12, 2022
a374e03
Compiling
ghutchis Sep 2, 2022
6897bb9
Changeup freeze atoms (now in constraint class) for mask
ghutchis Sep 17, 2022
2ca9ed1
Merge branch 'master' of github.com:openchemistry/avogadrolibs into n…
ghutchis Sep 4, 2023
cab91f3
Move thirdparty headers to sensible paths
ghutchis Sep 4, 2023
de2c387
Fix freeze / unfreeze compilation
ghutchis Sep 4, 2023
0dc412a
More fixes
ghutchis Sep 5, 2023
ed4f59e
Merge branch 'master' of github.com:OpenChemistry/avogadrolibs into n…
ghutchis Oct 8, 2023
f33a553
Compiles
ghutchis Oct 11, 2023
05bd338
Add support for unit cell optimizing through minimum distances
ghutchis Oct 12, 2023
d73b186
Remove include paths
ghutchis Oct 12, 2023
dfad831
More cleanups, including a manager class to filter methods
ghutchis Oct 14, 2023
29b73db
Implement a script interface - currently just energies
ghutchis Oct 14, 2023
3381b92
Handles energies through scripts - still need gradients
ghutchis Oct 21, 2023
78696ba
Add initial obmm support
ghutchis Oct 22, 2023
10d7d40
Continue working on scripts
ghutchis Oct 23, 2023
f2e416f
Fixed problem - gradients went the wrong way
ghutchis Oct 23, 2023
2f947b5
Fix formatting, tweak UI including configuration dialog
ghutchis Oct 23, 2023
5298261
Ensure m_molecule is always initialized
ghutchis Oct 23, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion avogadro/calc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,21 @@ avogadro_headers(Calc
chargemodel.h
chargemanager.h
defaultmodel.h
energycalculator.h
energymanager.h
lennardjones.h
)

target_sources(Calc PRIVATE
chargemodel.cpp
chargemanager.cpp
defaultmodel.cpp
energycalculator.cpp
energymanager.cpp
lennardjones.cpp
)

avogadro_add_library(Calc)

target_link_libraries(Calc
PUBLIC Avogadro::Core)
PUBLIC Avogadro::Core cppoptlib)
10 changes: 4 additions & 6 deletions avogadro/calc/chargemanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#include <algorithm>
#include <memory>


namespace Avogadro::Calc {

ChargeManager& ChargeManager::instance()
Expand Down Expand Up @@ -88,7 +87,7 @@ ChargeManager::ChargeManager()
ChargeManager::~ChargeManager()
{
// Delete the models that were loaded.
for (auto & m_model : m_models) {
for (auto& m_model : m_models) {
delete m_model;
}
m_models.clear();
Expand All @@ -102,7 +101,6 @@ std::set<std::string> ChargeManager::identifiersForMolecule(

// check our models for compatibility
for (auto m_model : m_models) {

// We check that every element in the molecule
// is handled by the model
auto mask = m_model->elements() & molecule.elements();
Expand All @@ -113,8 +111,8 @@ std::set<std::string> ChargeManager::identifiersForMolecule(
return identifiers;
}

MatrixX ChargeManager::partialCharges(
const std::string& identifier, Core::Molecule& molecule) const
MatrixX ChargeManager::partialCharges(const std::string& identifier,
Core::Molecule& molecule) const
{
// first check if the type is found in the molecule
// (i.e., read from a file not computed dynamically)
Expand Down Expand Up @@ -181,4 +179,4 @@ Core::Array<double> ChargeManager::potentials(
return model->potentials(molecule, points);
}

} // namespace Avogadro
} // namespace Avogadro::Calc
14 changes: 2 additions & 12 deletions avogadro/calc/chargemanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class AVOGADROCALC_EXPORT ChargeManager

/**
* @brief Register a new charge model with the manager.
* @param format An instance of the format to manage, the manager assumes
* @param model An instance of the model to manage, the manager assumes
* ownership of the object passed in.
* @return True on success, false on failure.
*/
Expand All @@ -80,16 +80,6 @@ class AVOGADROCALC_EXPORT ChargeManager
*/
bool removeModel(const std::string& identifier);

/**
* New instance of the model for the specified @p identifier. Ownership
* is passed to the caller.
* @param identifier The unique identifier of the format.
* @return Instance of the format, nullptr if not found. Ownership passes to
* the
* caller.
*/
ChargeModel* newModelFromIdentifier(const std::string& identifier) const;

/**
* Get a list of all loaded identifiers
*/
Expand All @@ -111,7 +101,7 @@ class AVOGADROCALC_EXPORT ChargeManager
* @return atomic partial charges for the molecule, or 0.0 if undefined
*/
MatrixX partialCharges(const std::string& identifier,
Core::Molecule& mol) const;
Core::Molecule& mol) const;

/**
* @return the potential at the point for the molecule, or 0.0 if the model is
Expand Down
7 changes: 4 additions & 3 deletions avogadro/calc/chargemodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ double ChargeModel::potential(Molecule& mol, const Vector3& point) const
double distance = (positions[i] - point).norm();
if (distance > 0.01) {
// drop small distances to avoid overflow
potential += charges(i,0) / distance;
potential += charges(i, 0) / distance;
}
}

Expand All @@ -52,12 +52,13 @@ Array<double> ChargeModel::potentials(Core::Molecule& mol,
// This is naive and slow, but can be re-implemented by methods
// for batching
Array<double> potentials(points.size(), 0.0);
for(unsigned int i = 0; i < points.size(); ++i)
for (unsigned int i = 0; i < points.size(); ++i)
potentials[i] = potential(mol, points[i]);
return potentials;
}

void ChargeModel::appendError(const std::string& errorString, bool newLine) const
void ChargeModel::appendError(const std::string& errorString,
bool newLine) const
{
m_error += errorString;
if (newLine)
Expand Down
36 changes: 36 additions & 0 deletions avogadro/calc/energycalculator.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/******************************************************************************
This source file is part of the Avogadro project.
This source code is released under the 3-Clause BSD License, (see "LICENSE").
******************************************************************************/

#include "energycalculator.h"

#include <iostream>

namespace Avogadro::Calc {

void EnergyCalculator::gradient(const TVector& x, TVector& grad)
{
finiteGradient(x, grad);
cleanGradients(grad);
}

void EnergyCalculator::cleanGradients(TVector& grad)
{
unsigned int size = grad.rows();
// check for overflows -- in case of divide by zero, etc.
for (unsigned int i = 0; i < size; ++i) {
if (!std::isfinite(grad[i]) || std::isnan(grad[i])) {
grad[i] = 0.0;
}
}

// freeze any masked atoms or coordinates
if (m_mask.rows() == size)
grad = grad.cwiseProduct(m_mask);
else
std::cerr << "Error: mask size " << m_mask.rows() << " " << grad.rows()
<< std::endl;
}

} // namespace Avogadro::Calc
127 changes: 127 additions & 0 deletions avogadro/calc/energycalculator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/******************************************************************************
This source file is part of the Avogadro project.
This source code is released under the 3-Clause BSD License, (see "LICENSE").
******************************************************************************/

#ifndef AVOGADRO_CALC_ENERGYCALCULATOR_H
#define AVOGADRO_CALC_ENERGYCALCULATOR_H

#include "avogadrocalcexport.h"

#include <avogadro/core/molecule.h>
#include <avogadro/core/variantmap.h>
#include <avogadro/core/vector.h>

#include <cppoptlib/problem.h>

namespace Avogadro {
namespace Core {
class Molecule;
}

namespace Calc {

class AVOGADROCALC_EXPORT EnergyCalculator : public cppoptlib::Problem<Real>
{
public:
EnergyCalculator() {}
~EnergyCalculator() {}

/**
* Create a new instance of the model. Ownership passes to the
* caller.
*/
virtual EnergyCalculator* newInstance() const = 0;

/**
* @return a unique identifier for this calculator.
*/
virtual std::string identifier() const = 0;

/**
* @return A short translatable name for this method (e.g., MMFF94, UFF, etc.)
*/
virtual std::string name() const = 0;

/**
* @return a description of the method
*/
virtual std::string description() const = 0;

/**
* Called to set the configuration (e.g., for a GUI options dialog)
*/
virtual bool setConfiguration(Core::VariantMap& config) { return true; }

/**
* @brief Indicate if your method only treats a subset of elements
* @return an element mask corresponding to the defined subset
*/
virtual Core::Molecule::ElementMask elements() const = 0;

/**
* @brief Indicate if your method can handle unit cells
* @return true if unit cells are supported
*/
virtual bool acceptsUnitCell() const { return false; }

/**
* @brief Indicate if your method can handle ions
* Many methods only treat neutral systems, either
* a neutral molecule or a neutral unit cell.
*
* @return true if ions are supported
*/
virtual bool acceptsIons() const { return false; }

/**
* @brief Indicate if your method can handle radicals
* Most methods only treat closed-shell molecules.
* @return true if radicals are supported
*/
virtual bool acceptsRadicals() const { return false; }

/**
* Calculate the gradients for this method, defaulting to numerical
* finite-difference methods
*/
virtual void gradient(const TVector& x, TVector& grad) override;

/**
* Called to 'clean' gradients @param grad (e.g., for constraints)
*/
void cleanGradients(TVector& grad);

/**
* Called to update the "frozen" mask (e.g., during editing)
*/
void setMask(TVector mask) { m_mask = mask; }

/**
* @return the frozen atoms mask
*/
TVector mask() const { return m_mask; }

/**
* Called when the current molecule changes.
*/
virtual void setMolecule(Core::Molecule* mol) = 0;

protected:
/**
* @brief Append an error to the error string for the model.
* @param errorString The error to be added.
* @param newLine Add a new line after the error string?
*/
void appendError(const std::string& errorString, bool newLine = true) const;

TVector m_mask; // optimize or frozen atom mask

private:
mutable std::string m_error;
};

} // end namespace Calc
} // end namespace Avogadro

#endif // AVOGADRO_CALC_ENERGYCALCULATOR_H
Loading