LCOV - code coverage report
Current view: top level - multicolvar - VolumeGradientBase.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 46 58 79.3 %
Date: 2024-10-11 08:09:47 Functions: 6 8 75.0 %

          Line data    Source code
       1             : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
       2             :    Copyright (c) 2014-2023 The plumed team
       3             :    (see the PEOPLE file at the root of the distribution for a list of names)
       4             : 
       5             :    See http://www.plumed.org for more information.
       6             : 
       7             :    This file is part of plumed, version 2.
       8             : 
       9             :    plumed is free software: you can redistribute it and/or modify
      10             :    it under the terms of the GNU Lesser General Public License as published by
      11             :    the Free Software Foundation, either version 3 of the License, or
      12             :    (at your option) any later version.
      13             : 
      14             :    plumed is distributed in the hope that it will be useful,
      15             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      16             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      17             :    GNU Lesser General Public License for more details.
      18             : 
      19             :    You should have received a copy of the GNU Lesser General Public License
      20             :    along with plumed.  If not, see <http://www.gnu.org/licenses/>.
      21             : +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
      22             : #include "VolumeGradientBase.h"
      23             : #include "core/PlumedMain.h"
      24             : #include "core/ActionSet.h"
      25             : #include "CatomPack.h"
      26             : 
      27             : namespace PLMD {
      28             : namespace multicolvar {
      29             : 
      30          37 : void VolumeGradientBase::registerKeywords( Keywords& keys ) {
      31          37 :   BridgedMultiColvarFunction::registerKeywords( keys );
      32          37 : }
      33             : 
      34          30 : VolumeGradientBase::VolumeGradientBase(const ActionOptions&ao):
      35             :   Action(ao),
      36          30 :   BridgedMultiColvarFunction(ao)
      37             : {
      38          30 : }
      39             : 
      40          30 : void VolumeGradientBase::requestAtoms( const std::vector<AtomNumber>& atoms ) {
      41          30 :   ActionAtomistic::requestAtoms(atoms); bridgeVariable=3*atoms.size();
      42             :   std::map<std::string,bool> checklabs;
      43          33 :   for(const auto & p : getDependencies() ) checklabs.insert(std::pair<std::string,bool>(p->getLabel(),false));
      44         102 :   for(const auto & p : plumed.getActionSet() ) {
      45         102 :     if( p->getLabel()==getPntrToMultiColvar()->getLabel() ) break;
      46           3 :     if( checklabs.count(p->getLabel()) ) checklabs[p->getLabel()]=true;
      47             :   }
      48          33 :   for(const auto & p : checklabs ) {
      49           3 :     if( !p.second ) error("the input for the virtual atoms used in the input for this action must appear in the input file before the input multicolvar");
      50             :   }
      51          30 :   addDependency( getPntrToMultiColvar() );
      52          30 :   tmpforces.resize( 3*atoms.size()+9 );
      53          30 : }
      54             : 
      55        2239 : void VolumeGradientBase::doJobsRequiredBeforeTaskList() {
      56        2239 :   ActionWithValue::clearDerivatives();
      57        2239 :   retrieveAtoms(); setupRegions();
      58        2239 :   ActionWithVessel::doJobsRequiredBeforeTaskList();
      59        2239 : }
      60             : 
      61       80958 : void VolumeGradientBase::completeTask( const unsigned& curr, MultiValue& invals, MultiValue& outvals ) const {
      62       80958 :   if( getPntrToMultiColvar()->isDensity() ) {
      63             :     outvals.setValue(0, 1.0); outvals.setValue(1, 1.0);
      64             :   } else {
      65             :     // Copy derivatives of the colvar and the value of the colvar
      66       37538 :     invals.copyValues( outvals );
      67       37538 :     if( derivativesAreRequired() ) invals.copyDerivatives( outvals );
      68             :   }
      69       80958 :   calculateAllVolumes( curr, outvals );
      70       80958 : }
      71             : 
      72      115758 : void VolumeGradientBase::setNumberInVolume( const unsigned& ivol, const unsigned& curr, const double& weight,
      73             :     const Vector& wdf, const Tensor& virial, const std::vector<Vector>& refders,
      74             :     MultiValue& outvals ) const {
      75             :   MultiColvarBase* mcolv=getPntrToMultiColvar();
      76      115758 :   if( !mcolv->weightHasDerivatives ) {
      77             :     outvals.setValue(ivol, weight );
      78      115758 :     if( derivativesAreRequired() ) {
      79      112725 :       CatomPack catom; mcolv->getCentralAtomPack( 0, curr, catom );
      80      253450 :       for(unsigned i=0; i<catom.getNumberOfAtomsWithDerivatives(); ++i) {
      81      140725 :         unsigned jatom=3*catom.getIndex(i);
      82      140725 :         outvals.addDerivative( ivol, jatom+0, catom.getDerivative(i,0,wdf) );
      83      140725 :         outvals.addDerivative( ivol, jatom+1, catom.getDerivative(i,1,wdf) );
      84      140725 :         outvals.addDerivative( ivol, jatom+2, catom.getDerivative(i,2,wdf) );
      85             :       }
      86      112725 :       unsigned nmder=getPntrToMultiColvar()->getNumberOfDerivatives();
      87     1465425 :       for(unsigned i=0; i<3; ++i) for(unsigned j=0; j<3; ++j) outvals.addDerivative( ivol, nmder-9+3*i+j, virial(i,j) );
      88      230310 :       for(unsigned i=0; i<refders.size(); ++i) {
      89      117585 :         unsigned iatom=nmder+3*i;
      90             : 
      91      117585 :         outvals.addDerivative( ivol, iatom+0, refders[i][0] );
      92      117585 :         outvals.addDerivative( ivol, iatom+1, refders[i][1] );
      93      117585 :         outvals.addDerivative( ivol, iatom+2, refders[i][2] );
      94             :       }
      95      112725 :     }
      96           0 :   } else if(ivol==0) {
      97           0 :     double ww=outvals.get(0); outvals.setValue(ivol,ww*weight);
      98           0 :     if( derivativesAreRequired() ) {
      99           0 :       plumed_merror("This needs testing");
     100             :       CatomPack catom; mcolv->getCentralAtomPack( 0, curr, catom );
     101             :       for(unsigned i=0; i<catom.getNumberOfAtomsWithDerivatives(); ++i) {
     102             :         unsigned jatom=3*catom.getIndex(i);
     103             :         outvals.addDerivative( ivol, jatom+0, weight*outvals.getDerivative(ivol,jatom+0) + ww*catom.getDerivative(i,0,wdf) );
     104             :         outvals.addDerivative( ivol, jatom+1, weight*outvals.getDerivative(ivol,jatom+1) + ww*catom.getDerivative(i,1,wdf) );
     105             :         outvals.addDerivative( ivol, jatom+2, weight*outvals.getDerivative(ivol,jatom+2) + ww*catom.getDerivative(i,2,wdf) );
     106             :       }
     107             :       unsigned nmder=getPntrToMultiColvar()->getNumberOfDerivatives();
     108             :       for(unsigned i=0; i<3; ++i) for(unsigned j=0; j<3; ++j) outvals.addDerivative( ivol, nmder-9+3*i+j, ww*virial(i,j) );
     109             :       for(unsigned i=0; i<refders.size(); ++i) {
     110             :         unsigned iatom=nmder+3*i;
     111             :         outvals.addDerivative( ivol, iatom+0, ww*refders[i][0] );
     112             :         outvals.addDerivative( ivol, iatom+1, ww*refders[i][1] );
     113             :         outvals.addDerivative( ivol, iatom+2, ww*refders[i][2] );
     114             :       }
     115             :     }
     116             :   } else {
     117           0 :     double ww=outvals.get(0); outvals.setValue(ivol,ww*weight);
     118           0 :     if( derivativesAreRequired() ) {
     119           0 :       plumed_merror("This needs testing");
     120             :       CatomPack catom; mcolv->getCentralAtomPack( 0, curr, catom );
     121             :       for(unsigned i=0; i<catom.getNumberOfAtomsWithDerivatives(); ++i) {
     122             :         unsigned jatom=3*catom.getIndex(i);
     123             :         outvals.addDerivative( ivol, jatom+0, ww*catom.getDerivative(i,0,wdf) );
     124             :         outvals.addDerivative( ivol, jatom+1, ww*catom.getDerivative(i,1,wdf) );
     125             :         outvals.addDerivative( ivol, jatom+2, ww*catom.getDerivative(i,2,wdf) );
     126             :       }
     127             :       unsigned nmder=getPntrToMultiColvar()->getNumberOfDerivatives();
     128             :       for(unsigned i=0; i<3; ++i) for(unsigned j=0; j<3; ++j) outvals.addDerivative( ivol, nmder-9+3*i+j, ww*virial(i,j) );
     129             :       for(unsigned i=0; i<refders.size(); ++i) {
     130             :         unsigned iatom=nmder+3*i;
     131             :         outvals.addDerivative( ivol, iatom+0, ww*refders[i][0] );
     132             :         outvals.addDerivative( ivol, iatom+1, ww*refders[i][1] );
     133             :         outvals.addDerivative( ivol, iatom+2, ww*refders[i][2] );
     134             :       }
     135             :     }
     136             :   }
     137      115758 : }
     138             : 
     139           0 : void VolumeGradientBase::addBridgeForces( const std::vector<double>& bb ) {
     140             :   plumed_dbg_assert( bb.size()==tmpforces.size()-9 );
     141             :   // Forces on local atoms
     142           0 :   for(unsigned i=0; i<bb.size(); ++i) tmpforces[i]=bb[i];
     143             :   // Virial contribution is zero
     144           0 :   for(unsigned i=bb.size(); i<bb.size()+9; ++i) tmpforces[i]=0.0;
     145           0 :   setForcesOnAtoms( tmpforces, 0 );
     146           0 : }
     147             : 
     148             : }
     149             : }

Generated by: LCOV version 1.15