From 15dbc5c074193907fb24d9187c2e3466b566e615 Mon Sep 17 00:00:00 2001 From: Giovanni Bussi <giovanni.bussi@gmail.com> Date: Sun, 17 May 2015 08:55:35 +0200 Subject: [PATCH] Simplified virial calculation in GHOST Similarly to what happens in Colvar class, I made also for ActionWithVirtualAtom a method setBoxDerivativesNoPbc that can be used to set the virial contribution when the Pbc have not been used in the calculation of derivatives. This should make the addition of new vatom actions easier --- src/core/ActionWithVirtualAtom.cpp | 15 +++++++++++++++ src/core/ActionWithVirtualAtom.h | 7 +++++++ src/vatom/Ghost.cpp | 17 +---------------- 3 files changed, 23 insertions(+), 16 deletions(-) diff --git a/src/core/ActionWithVirtualAtom.cpp b/src/core/ActionWithVirtualAtom.cpp index f1f71a8dd..3e9ffd21a 100644 --- a/src/core/ActionWithVirtualAtom.cpp +++ b/src/core/ActionWithVirtualAtom.cpp @@ -86,6 +86,21 @@ void ActionWithVirtualAtom::setBoxDerivatives(const std::vector<Tensor> &d){ for(unsigned i=0;i<3;i++) for(unsigned j=0;j<3;j++) boxDerivatives[j][i][j]+=pos[i]; } +void ActionWithVirtualAtom::setBoxDerivativesNoPbc(){ + std::vector<Tensor> bd(3); + for(unsigned i=0;i<3;i++) for(unsigned j=0;j<3;j++) for(unsigned k=0;k<3;k++){ +// Notice that this expression is very similar to the one used in Colvar::setBoxDerivativesNoPbc(). +// Indeed, we have the negative of a sum over dependent atoms (l) of the external product between positions +// and derivatives. Notice that this only works only when Pbc have not been used to compute +// derivatives. + for(unsigned l=0;l<getNumberOfAtoms();l++){ + bd[k][i][j]-=getPosition(l)[i]*derivatives[l][j][k]; + } + } + setBoxDerivatives(bd); +} + + void ActionWithVirtualAtom::setGradientsIfNeeded(){ if(isOptionOn("GRADIENTS")) { diff --git a/src/core/ActionWithVirtualAtom.h b/src/core/ActionWithVirtualAtom.h index 69397e986..10925335e 100644 --- a/src/core/ActionWithVirtualAtom.h +++ b/src/core/ActionWithVirtualAtom.h @@ -66,6 +66,13 @@ protected: /// On the other hand if the vatom position is a non-linear function of atomic coordinates this /// should be called (see vatom::Ghost). void setBoxDerivatives(const std::vector<Tensor> &d); +/// Set box derivatives automatically. +/// It should be called after the settomsDerivatives has been used for all +/// single atoms. +/// \warning It only works for virtual atoms NOT using PBCs! +/// This implies that all atoms used + the new virtual atom should be +/// in the same periodic image. + void setBoxDerivativesNoPbc(); public: void setGradients(); const std::map<AtomNumber,Tensor> & getGradients()const; diff --git a/src/vatom/Ghost.cpp b/src/vatom/Ghost.cpp index 9d123eed3..b434e591f 100644 --- a/src/vatom/Ghost.cpp +++ b/src/vatom/Ghost.cpp @@ -90,11 +90,6 @@ void Ghost::calculate(){ Vector pos; vector<Tensor> deriv(getNumberOfAtoms()); vector<Vector> n; - Vector pp[3]; - - pp[0]=getPosition(0); - pp[1]=pp[0]+delta(getPosition(0), getPosition(1)); - pp[2]=pp[0]+delta(getPosition(0), getPosition(2)); // first versor Vector n01 = delta(getPosition(0), getPosition(1)); @@ -184,17 +179,7 @@ void Ghost::calculate(){ setAtomsDerivatives(deriv); // Virial contribution - std::vector<Tensor> bd(3); - for(unsigned i=0;i<3;i++) for(unsigned j=0;j<3;j++) for(unsigned k=0;k<3;k++){ -// Notice that this expression is very similar to the one used in Colvar::setBoxDerivativesNoPbc(). -// Indeed, we have the negative of a sum over dependent atoms (l) of the external product between positions -// and derivatives. Notice that positions here should be computed in the same periodic image -// as pos. - for(unsigned l=0;l<3;l++){ - bd[k][i][j]-=pp[l][i]*deriv[l][j][k]; - } - } - setBoxDerivatives(bd); + setBoxDerivativesNoPbc(); } } -- GitLab