| 1 |
/****************************************************************************** |
| 2 |
This source file is part of the Avogadro project. |
| 3 |
This source code is released under the 3-Clause BSD License, (see "LICENSE"). |
| 4 |
******************************************************************************/ |
| 5 |
|
| 6 |
#include "chargemodel.h" |
| 7 |
|
| 8 |
#include <Eigen/src/Core/util/Meta.h> |
| 9 |
#include <avogadro/core/array.h> |
| 10 |
#include <avogadro/core/molecule.h> |
| 11 |
|
| 12 |
#include <cstddef> |
| 13 |
#include <iostream> |
| 14 |
|
| 15 |
namespace Avogadro { |
| 16 |
|
| 17 |
using Core::Array; |
| 18 |
using Core::Molecule; |
| 19 |
|
| 20 |
namespace Calc { |
| 21 |
|
| 22 |
#ifndef M_PI |
| 23 |
constexpr double M_PI = 3.14159265358979323846; |
| 24 |
#endif |
| 25 |
|
| 26 |
Vector3 ChargeModel::dipoleMoment(const Molecule& mol) const |
| 27 |
{ |
| 28 |
if (mol.atomCount() < 2) |
| 29 |
return Vector3(0.0, 0.0, 0.0); |
| 30 |
|
| 31 |
// default is to get the set of partial atomic charges |
| 32 |
// (some models might do something more sophisticated) |
| 33 |
const MatrixX charges = partialCharges(mol); |
| 34 |
// also get the positions of the atoms |
| 35 |
const Array<Vector3> positions = mol.atomPositions3d(); |
| 36 |
|
| 37 |
Vector3 dipole(0.0, 0.0, 0.0); |
| 38 |
if (static_cast<std::size_t>(charges.rows()) != positions.size()) |
| 39 |
std::cout << "Error: charges " << charges.rows() << " != positions " |
| 40 |
<< positions.size() << std::endl; |
| 41 |
|
| 42 |
for (Eigen::Index i = 0; i < charges.size(); ++i) |
| 43 |
dipole += charges(i, 0) * positions[i]; |
| 44 |
|
| 45 |
dipole *= 4.80320471257; // convert to Debye from electron-Angstrom |
| 46 |
|
| 47 |
return dipole; |
| 48 |
} |
| 49 |
|
| 50 |
double ChargeModel::potential(Molecule& mol, const Vector3& point) const |
| 51 |
{ |
| 52 |
// default is to get the set of partial atomic charges |
| 53 |
const MatrixX charges = partialCharges(mol); |
| 54 |
// also get the positions of the atoms |
| 55 |
const Array<Vector3> positions = mol.atomPositions3d(); |
| 56 |
|
| 57 |
// @todo: this is naive and inefficient |
| 58 |
// calculate the atoms within a cutoff distance |
| 59 |
// and sum the potentials |
| 60 |
|
| 61 |
// note this is usually multithreaded by the caller |
| 62 |
// but more efficient methods can be implemented |
| 63 |
double potential = 0.0; |
| 64 |
for (unsigned int i = 0; i < charges.size(); ++i) { |
| 65 |
double distance = (positions[i] - point).norm(); |
| 66 |
if (distance > 0.01) { |
| 67 |
// drop small distances to avoid overflow |
| 68 |
potential += charges(i, 0) / distance; |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
return potential / m_dielectric; |
| 73 |
} |
| 74 |
|
| 75 |
Array<double> ChargeModel::potentials(Core::Molecule& mol, |
| 76 |
const Array<Vector3>& points) const |
| 77 |
{ |
| 78 |
// This is naive and slow, but can be re-implemented by methods |
| 79 |
// for batching |
| 80 |
Array<double> potentials(points.size(), 0.0); |
| 81 |
for (unsigned int i = 0; i < points.size(); ++i) |
| 82 |
potentials[i] = potential(mol, points[i]); |
| 83 |
return potentials; |
| 84 |
} |
| 85 |
|
| 86 |
void ChargeModel::appendError(const std::string& errorString, |
| 87 |
bool newLine) const |
| 88 |
{ |
| 89 |
m_error += errorString; |
| 90 |
if (newLine) |
| 91 |
m_error += "\n"; |
| 92 |
} |
| 93 |
|
| 94 |
} // namespace Calc |
| 95 |
} // namespace Avogadro |