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; }