#include "AtomToMoleculeTransfer.h"
#include "ATC_Method.h"
using std::set;
namespace ATC {
SmallMoleculeCentroid::SmallMoleculeCentroid(ATC_Method * atc, PerAtomQuantity<double> * source, SmallMoleculeSet * smallMoleculeSet, PerAtomQuantity<double> * atomPositions) : AtomToSmallMoleculeTransfer<double>(atc, source, smallMoleculeSet), atomPositions_(atomPositions)
{
atomPositions_->register_dependence(this);
}
SmallMoleculeCentroid::~SmallMoleculeCentroid()
{
atomPositions_->remove_dependence(this);
}
void SmallMoleculeCentroid::reset_quantity() const
{
const LammpsInterface * lammps(atc_->lammps_interface());
const DENS_MAT & sourceMatrix(source_->quantity()); double xi[3], xj[3], xjImage[3];
const DENS_MAT & atomPosMatrix(atomPositions_->quantity());
int nLocalMol = smallMoleculeSet_->local_molecule_count();
quantity_.reset(nLocalMol,atc_->nsd());
for (int i = 0; i < nLocalMol; i++) {
const set<int> & atomsLocalMolArray = smallMoleculeSet_->atoms_by_local_molecule(i);
set<int>::const_iterator atomsLocalMolID;
double totalSourceMol = 0.0; for (atomsLocalMolID = atomsLocalMolArray.begin(); atomsLocalMolID != atomsLocalMolArray.end(); atomsLocalMolID++) {
totalSourceMol += sourceMatrix(*atomsLocalMolID,0);
} atomsLocalMolID = atomsLocalMolArray.begin();
for (int j = 0; j < atc_->nsd(); j++) {
xi[j] = atomPosMatrix(*atomsLocalMolID,j);
}
for (atomsLocalMolID = atomsLocalMolArray.begin(); atomsLocalMolID != atomsLocalMolArray.end(); atomsLocalMolID++) {
for (int j = 0; j < atc_->nsd(); j++){
xj[j] = atomPosMatrix(*atomsLocalMolID,j);
}
lammps->closest_image(xi,xj,xjImage);
for (int j = 0; j < atc_->nsd() ; j++) {
quantity_(i,j) += sourceMatrix(*atomsLocalMolID,0) * xjImage[j]/totalSourceMol;
}
}
}
}
SmallMoleculeDipoleMoment::SmallMoleculeDipoleMoment(ATC_Method * atc, PerAtomQuantity<double> * source, SmallMoleculeSet * smallMoleculeSet, PerAtomQuantity<double> * atomPositions, SmallMoleculeCentroid * centroid) : SmallMoleculeCentroid(atc, source, smallMoleculeSet, atomPositions), centroid_(centroid) {
centroid_->register_dependence(this);
}
SmallMoleculeDipoleMoment::~SmallMoleculeDipoleMoment()
{
centroid_->remove_dependence(this);
}
void SmallMoleculeDipoleMoment::reset_quantity() const
{
const LammpsInterface * lammps(atc_->lammps_interface());
const DENS_MAT & sourceMatrix(source_->quantity()); const DENS_MAT & atomPosMatrix(atomPositions_->quantity());
int nLocalMol = smallMoleculeSet_->local_molecule_count();
int nsd = atc_->nsd();
quantity_.reset(nLocalMol,nsd);
double dx[3];
const DENS_MAT & centroidMolMatrix(centroid_->quantity());
for (int i = 0; i < nLocalMol; i++) {
const set<int> & atomsLocalMolArray = smallMoleculeSet_->atoms_by_local_molecule(i);
set<int>::const_iterator atomsLocalMolID;;
for (atomsLocalMolID = atomsLocalMolArray.begin(); atomsLocalMolID != atomsLocalMolArray.end();atomsLocalMolID++) {
for (int j = 0; j < nsd; j++) {
dx[j] = atomPosMatrix(*atomsLocalMolID,j) - centroidMolMatrix(i,j);
}
lammps->minimum_image(dx[0], dx[1], dx[2]);
for(int j = 0; j < nsd; j++) {
quantity_(i,j) += sourceMatrix(*atomsLocalMolID,0) * dx[j];
}
}
}
}
SmallMoleculeQuadrupoleMoment::SmallMoleculeQuadrupoleMoment(ATC_Method * atc, PerAtomQuantity<double> * source, SmallMoleculeSet * smallMoleculeSet, PerAtomQuantity<double> * atomPositions, SmallMoleculeCentroid * centroid) : SmallMoleculeCentroid(atc, source, smallMoleculeSet, atomPositions), centroid_(centroid)
{
centroid_->register_dependence(this);
}
SmallMoleculeQuadrupoleMoment::~SmallMoleculeQuadrupoleMoment()
{
centroid_->remove_dependence(this);
}
void SmallMoleculeQuadrupoleMoment::reset_quantity() const
{
const LammpsInterface * lammps(atc_->lammps_interface());
const DENS_MAT & sourceMatrix(source_->quantity()); const DENS_MAT & atomPosMatrix(atomPositions_->quantity());
int nLocalMol = smallMoleculeSet_->local_molecule_count();
int nsd = atc_->nsd();
quantity_.reset(nLocalMol,nsd);
double dx[3];
const DENS_MAT & centroidMolMatrix(centroid_->quantity());
for (int i = 0; i < nLocalMol; i++) {
const set<int> & atomsLocalMolArray = smallMoleculeSet_->atoms_by_local_molecule(i);
set<int>::const_iterator atomsLocalMolID;;
for (atomsLocalMolID = atomsLocalMolArray.begin(); atomsLocalMolID != atomsLocalMolArray.end();atomsLocalMolID++) {
for (int j = 0; j < nsd; j++) {
dx[j] = atomPosMatrix(*atomsLocalMolID,j) - centroidMolMatrix(i,j);
}
lammps->minimum_image(dx[0], dx[1], dx[2]);
for(int j = 0; j < nsd; j++) {
quantity_(i,j) += 0.5*sourceMatrix(*atomsLocalMolID,0) * dx[j] * dx[2];
}
}
}
}
MotfShapeFunctionRestriction::MotfShapeFunctionRestriction(PerMoleculeQuantity<double> * source,
SPAR_MAN * shapeFunction) :
MatToMatTransfer<double>(source),
shapeFunction_(shapeFunction)
{
shapeFunction_->register_dependence(this);
}
MotfShapeFunctionRestriction::~MotfShapeFunctionRestriction()
{
shapeFunction_->remove_dependence(this);
}
void MotfShapeFunctionRestriction::reset_quantity() const
{
const DENS_MAT & sourceMatrix(source_->quantity());
const SPAR_MAT & shapeFunctionMatrix(shapeFunction_->quantity());
quantity_.resize(shapeFunctionMatrix.nCols(),sourceMatrix.nCols());
local_restriction(sourceMatrix,shapeFunctionMatrix);
int count = quantity_.nRows()*quantity_.nCols();
lammpsInterface_->allsum(_workspace_.ptr(),quantity_.ptr(),count);
}
void MotfShapeFunctionRestriction::local_restriction(const DENS_MAT & sourceMatrix,
const SPAR_MAT & shapeFunctionMatrix) const
{
if (sourceMatrix.nRows() > 0)
_workspace_ = shapeFunctionMatrix.transMat(sourceMatrix);
else
_workspace_.reset(quantity_.nRows(),quantity_.nCols());
}
}