double SimpleRMSD::calc( const std::vector<Vector>& pos, ReferenceValuePack& myder, const bool& squared ) const { if( myder.getAtomsDisplacementVector().size()!=pos.size() ) myder.getAtomsDisplacementVector().resize( pos.size() ); double d=myrmsd.simpleAlignment( getAlign(), getDisplace(), pos, getReferencePositions(), myder.getAtomVector(), myder.getAtomsDisplacementVector(), squared ); myder.clear(); for(unsigned i=0;i<pos.size();++i) myder.setAtomDerivatives( i, myder.getAtomVector()[i] ); if( !myder.updateComplete() ) myder.updateDynamicLists(); return d; }
double MultiDomainRMSD::projectAtomicDisplacementOnVector( const bool& normalized, const std::vector<Vector>& vecs, ReferenceValuePack& mypack ) const { double totd=0.; std::vector<Vector> tvecs; mypack.clear(); MultiValue tvals( 1, mypack.getNumberOfDerivatives() ); ReferenceValuePack tder( 0, getNumberOfAtoms(), tvals ); for(unsigned i=0; i<domains.size(); ++i) { // Must extract appropriate positions here tvecs.resize( blocks[i+1] - blocks[i] ); domains[i]->setupPCAStorage( tder ); if( tder.centeredpos.size()>0 ) { for(unsigned p=0; p<3; ++p) for(unsigned q=0; q<3; ++q) tder.DRotDPos(p,q).resize( tvecs.size() ); } // Extract information from storage pack and put in local pack if( tder.centeredpos.size()>0 ) tder.rot[0]=mypack.rot[i]; unsigned n=0; for(unsigned j=blocks[i]; j<blocks[i+1]; ++j) { tder.setAtomIndex(n,j); tvecs[n] = vecs[j]; tder.displacement[n]=mypack.displacement[j] / weights[i]; if( tder.centeredpos.size()>0 ) { tder.centeredpos[n]=mypack.centeredpos[j]; for(unsigned p=0; p<3; ++p) for(unsigned q=0; q<3; ++q) tder.DRotDPos(p,q)[n]=mypack.DRotDPos(p,q)[j]; } n++; } for(unsigned k=n; k<getNumberOfAtoms(); ++k) tder.setAtomIndex(k,3*vecs.size()+10); // Do the calculations totd += weights[i]*domains[i]->projectAtomicDisplacementOnVector( normalized, tvecs, tder ); // And derivatives mypack.copyScaledDerivatives( 0, weights[i], tvals ); } if( !mypack.updateComplete() ) mypack.updateDynamicLists(); return totd; }
double OptimalRMSD::projectAtomicDisplacementOnVector( const bool& normalized, const std::vector<Vector>& vecs, ReferenceValuePack& mypack ) const { plumed_dbg_assert( mypack.calcUsingPCAOption() ); double proj=0.0; mypack.clear(); for(unsigned i=0; i<vecs.size(); ++i) { proj += dotProduct( mypack.getAtomsDisplacementVector()[i], vecs[i] ); } for(unsigned a=0; a<3; a++) { for(unsigned b=0; b<3; b++) { for(unsigned iat=0; iat<getNumberOfAtoms(); iat++) { double tmp1=0.; for(unsigned n=0; n<getNumberOfAtoms(); n++) tmp1+=mypack.centeredpos[n][b]*vecs[n][a]; if( normalized ) mypack.addAtomDerivatives( iat, getDisplace()[iat]*mypack.DRotDPos[a][b][iat]*tmp1 ); else mypack.addAtomDerivatives( iat, mypack.DRotDPos[a][b][iat]*tmp1 ); } } } Tensor trot=mypack.rot[0].transpose(); Vector v1; v1.zero(); double prefactor = 1. / static_cast<double>( getNumberOfAtoms() ); for(unsigned n=0; n<getNumberOfAtoms(); n++) v1+=prefactor*matmul(trot,vecs[n]); if( normalized ) { for(unsigned iat=0; iat<getNumberOfAtoms(); iat++) mypack.addAtomDerivatives( iat, getDisplace()[iat]*(matmul(trot,vecs[iat]) - v1) ); } else { for(unsigned iat=0; iat<getNumberOfAtoms(); iat++) mypack.addAtomDerivatives( iat, (matmul(trot,vecs[iat]) - v1) ); } if( !mypack.updateComplete() ) mypack.updateDynamicLists(); return proj; }
double SimpleRMSD::projectAtomicDisplacementOnVector( const std::vector<Vector>& vecs, const std::vector<Vector>& pos, ReferenceValuePack& mypack ) const { plumed_dbg_assert( mypack.calcUsingPCAOption() ); Vector comder; comder.zero(); for(unsigned j=0;j<pos.size();++j){ for(unsigned k=0;k<3;++k) comder[k] += getAlign()[j]*vecs[j][k]; } double proj=0; mypack.clear(); for(unsigned j=0;j<pos.size();++j){ for(unsigned k=0;k<3;++k){ proj += vecs[j][k]*mypack.getAtomsDisplacementVector()[j][k]; } mypack.setAtomDerivatives( j, vecs[j] - comder ); } if( !mypack.updateComplete() ) mypack.updateDynamicLists(); return proj; }
double OptimalRMSD::calc( const std::vector<Vector>& pos, ReferenceValuePack& myder, const bool& squared ) const { double d; if( myder.calcUsingPCAOption() ) { std::vector<Vector> centeredreference( getNumberOfAtoms () ); d=myrmsd.calc_PCAelements(pos,myder.getAtomVector(),myder.rot[0],myder.DRotDPos,myder.getAtomsDisplacementVector(),myder.centeredpos,centeredreference,squared); unsigned nat = pos.size(); for(unsigned i=0; i<nat; ++i) { myder.getAtomsDisplacementVector()[i] -= getReferencePosition(i); myder.getAtomsDisplacementVector()[i] *= getDisplace()[i]; } } else if( fast ) { if( getAlign()==getDisplace() ) d=myrmsd.optimalAlignment<false,true>(getAlign(),getDisplace(),pos,getReferencePositions(),myder.getAtomVector(),squared); else d=myrmsd.optimalAlignment<false,false>(getAlign(),getDisplace(),pos,getReferencePositions(),myder.getAtomVector(),squared); } else { if( getAlign()==getDisplace() ) d=myrmsd.optimalAlignment<true,true>(getAlign(),getDisplace(),pos,getReferencePositions(),myder.getAtomVector(),squared); else d=myrmsd.optimalAlignment<true,false>(getAlign(),getDisplace(),pos,getReferencePositions(),myder.getAtomVector(),squared); } myder.clear(); for(unsigned i=0; i<pos.size(); ++i) myder.setAtomDerivatives( i, myder.getAtomVector()[i] ); if( !myder.updateComplete() ) myder.updateDynamicLists(); return d; }
double MultiDomainRMSD::calculate( const std::vector<Vector>& pos, const Pbc& pbc, ReferenceValuePack& myder, const bool& squared ) const { double totd=0.; Tensor tvirial; std::vector<Vector> mypos; MultiValue tvals( 1, 3*pos.size()+9 ); ReferenceValuePack tder( 0, getNumberOfAtoms(), tvals ); myder.clear(); for(unsigned i=0; i<domains.size(); ++i) { // Must extract appropriate positions here mypos.resize( blocks[i+1] - blocks[i] ); if( myder.calcUsingPCAOption() ) domains[i]->setupPCAStorage( tder ); unsigned n=0; for(unsigned j=blocks[i]; j<blocks[i+1]; ++j) { tder.setAtomIndex(n,j); mypos[n]=pos[j]; n++; } for(unsigned k=n; k<getNumberOfAtoms(); ++k) tder.setAtomIndex(k,3*pos.size()+10); // This actually does the calculation totd += weights[i]*domains[i]->calculate( mypos, pbc, tder, true ); // Now merge the derivative myder.copyScaledDerivatives( 0, weights[i], tvals ); // If PCA copy PCA stuff if( myder.calcUsingPCAOption() ) { unsigned n=0; if( tder.centeredpos.size()>0 ) myder.rot[i]=tder.rot[0]; for(unsigned j=blocks[i]; j<blocks[i+1]; ++j) { myder.displacement[j]=weights[i]*tder.displacement[n]; // Multiplication by weights here ensures that normalisation is done correctly if( tder.centeredpos.size()>0 ) { myder.centeredpos[j]=tder.centeredpos[n]; for(unsigned p=0; p<3; ++p) for(unsigned q=0; q<3; ++q) myder.DRotDPos(p,q)[j]=tder.DRotDPos(p,q)[n]; } n++; } } // Make sure virial status is set correctly in output derivative pack // This is only done here so I do this by using class friendship if( tder.virialWasSet() ) myder.boxWasSet=true; } if( !myder.updateComplete() ) myder.updateDynamicLists(); if( !squared ) { totd=sqrt(totd); double xx=0.5/totd; myder.scaleAllDerivatives( xx ); } return totd; }
double DRMSD::calc( const std::vector<Vector>& pos, const Pbc& pbc, ReferenceValuePack& myder, const bool& squared ) const { plumed_dbg_assert(!targets.empty()); Vector distance; myder.clear(); double drmsd=0.; for(std::map< std::pair <unsigned,unsigned> , double>::const_iterator it=targets.begin();it!=targets.end();++it){ const unsigned i=getAtomIndex( it->first.first ); const unsigned j=getAtomIndex( it->first.second ); if(nopbc) distance=delta( pos[i] , pos[j] ); else distance=pbc.distance( pos[i] , pos[j] ); const double len = distance.modulo(); const double diff = len - it->second; const double der = diff / len; drmsd += diff * diff; myder.addAtomDerivatives( i, -der * distance ); myder.addAtomDerivatives( j, der * distance ); myder.addBoxDerivatives( - der * Tensor(distance,distance) ); } const double inpairs = 1./static_cast<double>(targets.size()); double idrmsd; if(squared){ drmsd = drmsd * inpairs; idrmsd = 2.0 * inpairs; } else { drmsd = sqrt( drmsd * inpairs ); idrmsd = inpairs / drmsd ; } myder.scaleAllDerivatives( idrmsd ); return drmsd; }