LCOV - code coverage report
Current view: top level - multicolvar - VolumeBetweenContours.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 8 53 15.1 %
Date: 2020-11-18 11:20:57 Functions: 5 13 38.5 %

          Line data    Source code
       1             : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
       2             :    Copyright (c) 2017-2019 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 "core/ActionRegister.h"
      23             : #include "tools/Pbc.h"
      24             : #include "tools/KernelFunctions.h"
      25             : #include "tools/SwitchingFunction.h"
      26             : #include "ActionVolume.h"
      27             : 
      28             : //+PLUMEDOC VOLUMES INENVELOPE
      29             : /*
      30             : This quantity can be used to calculate functions of the distribution of collective
      31             : variables for the atoms that lie in a region where the density of a certain type of atom is high.
      32             : 
      33             : This collective variable can be used to determine whether colvars are within region where the density
      34             : of a particular atom is high.  This is achieved by calculating the following function at the point where
      35             : the atom is located \f$(x,y,z)\f$:
      36             : 
      37             : \f[
      38             : w_j = 1 - \sigma\left[ \sum_{i=1}^N K\left( \frac{x-x_i}{\sigma_x},\frac{y-y_i}{\sigma_y},\frac{z-z_i}{\sigma_z} \right) \right]
      39             : \f]
      40             : 
      41             : Here \f$\sigma\f$ is a \ref switchingfunction and \f$K\f$ is a \ref kernelfunctions.  The sum runs over the atoms
      42             : specified using the ATOMS keyword and a \f$w_j\f$ value is calculated for each of the central atoms of the input
      43             : multicolvar.
      44             : 
      45             : \par Examples
      46             : 
      47             : The input below calculates a density field from the positions of atoms 1-14400.  The number of the atoms
      48             : that are specified in the DENSITY action that are within a region where the density field is greater than
      49             : 2.0 is then calculated.
      50             : 
      51             : \verbatim
      52             : d1: DENSITY SPECIES=14401-74134:3 LOWMEM
      53             : fi: INENVELOPE DATA=d1 ATOMS=1-14400 CONTOUR={RATIONAL D_0=2.0 R_0=1.0} BANDWIDTH=0.1,0.1,0.1 LOWMEM
      54             : PRINT ARG=fi,rr.* FILE=colvar
      55             : \endverbatim
      56             : 
      57             : */
      58             : //+ENDPLUMEDOC
      59             : 
      60             : namespace PLMD {
      61             : namespace multicolvar {
      62             : 
      63             : class VolumeInEnvelope : public ActionVolume {
      64             : private:
      65             :   LinkCells mylinks;
      66             :   KernelFunctions* kernel;
      67             :   std::vector<Value*> pos;
      68             :   std::vector<Vector> ltmp_pos;
      69             :   std::vector<unsigned> ltmp_ind;
      70             :   SwitchingFunction sfunc;
      71             : public:
      72             :   static void registerKeywords( Keywords& keys );
      73             :   explicit VolumeInEnvelope(const ActionOptions& ao);
      74             :   ~VolumeInEnvelope();
      75             :   void setupRegions();
      76             :   double calculateNumberInside( const Vector& cpos, Vector& derivatives, Tensor& vir, std::vector<Vector>& refders ) const ;
      77             : };
      78             : 
      79        6452 : PLUMED_REGISTER_ACTION(VolumeInEnvelope,"INENVELOPE")
      80             : 
      81           1 : void VolumeInEnvelope::registerKeywords( Keywords& keys ) {
      82           2 :   ActionVolume::registerKeywords( keys ); keys.remove("SIGMA");
      83           4 :   keys.add("atoms","ATOMS","the atom whose positions we are constructing a field from");
      84           4 :   keys.add("compulsory","BANDWIDTH","the bandwidths for kernel density esimtation");
      85           4 :   keys.add("compulsory","CONTOUR","a switching funciton that tells PLUMED how large the density should be");
      86           1 : }
      87             : 
      88           0 : VolumeInEnvelope::VolumeInEnvelope(const ActionOptions& ao):
      89             :   Action(ao),
      90             :   ActionVolume(ao),
      91           0 :   mylinks(comm)
      92             : {
      93           0 :   std::vector<AtomNumber> atoms; parseAtomList("ATOMS",atoms);
      94           0 :   log.printf("  creating density field from atoms : ");
      95           0 :   for(unsigned i=0; i<atoms.size(); ++i) log.printf("%d ",atoms[i].serial() );
      96           0 :   log.printf("\n"); ltmp_ind.resize( atoms.size() ); ltmp_pos.resize( atoms.size() );
      97           0 :   for(unsigned i=0; i<atoms.size(); ++i) ltmp_ind[i]=i;
      98             : 
      99           0 :   std::string sw, errors; parse("CONTOUR",sw);
     100           0 :   if(sw.length()==0) error("missing CONTOURkeyword");
     101           0 :   sfunc.set(sw,errors);
     102           0 :   if( errors.length()!=0 ) error("problem reading RADIUS keyword : " + errors );
     103           0 :   log.printf("  density at atom must be larger than %s \n", ( sfunc.description() ).c_str() );
     104             : 
     105           0 :   std::vector<double> pp(3,0.0), bandwidth(3); parseVector("BANDWIDTH",bandwidth);
     106           0 :   log.printf("  using %s kernel with bandwidths %f %f %f \n",getKernelType().c_str(),bandwidth[0],bandwidth[1],bandwidth[2] );
     107           0 :   kernel = new KernelFunctions( pp, bandwidth, getKernelType(), false, 1.0, true );
     108           0 :   for(unsigned i=0; i<3; ++i) { pos.push_back(new Value()); pos[i]->setNotPeriodic(); }
     109           0 :   std::vector<double> csupport( kernel->getContinuousSupport() );
     110           0 :   double maxs = csupport[0];
     111           0 :   for(unsigned i=1; i<csupport.size(); ++i) {
     112           0 :     if( csupport[i]>maxs ) maxs = csupport[i];
     113             :   }
     114           0 :   checkRead(); requestAtoms(atoms); mylinks.setCutoff( maxs );
     115           0 : }
     116             : 
     117           0 : VolumeInEnvelope::~VolumeInEnvelope() {
     118           0 :   delete kernel; for(unsigned j=0; j<3; ++j) delete pos[j];
     119           0 : }
     120             : 
     121           0 : void VolumeInEnvelope::setupRegions() {
     122           0 :   for(unsigned i=0; i<ltmp_ind.size(); ++i) { ltmp_pos[i]=getPosition(i); }
     123           0 :   mylinks.buildCellLists( ltmp_pos, ltmp_ind, getPbc() );
     124           0 : }
     125             : 
     126           0 : double VolumeInEnvelope::calculateNumberInside( const Vector& cpos, Vector& derivatives, Tensor& vir, std::vector<Vector>& refders ) const {
     127           0 :   unsigned ncells_required=0, natoms=1; std::vector<unsigned> cells_required( mylinks.getNumberOfCells() ), indices( 1 + getNumberOfAtoms() );
     128           0 :   mylinks.addRequiredCells( mylinks.findMyCell( cpos ), ncells_required, cells_required );
     129           0 :   indices[0]=getNumberOfAtoms(); mylinks.retrieveAtomsInCells( ncells_required, cells_required, natoms, indices );
     130           0 :   double value=0; std::vector<double> der(3); Vector tder;
     131           0 :   for(unsigned i=1; i<natoms; ++i) {
     132           0 :     Vector dist = getSeparation( cpos, getPosition( indices[i] ) );
     133           0 :     for(unsigned j=0; j<3; ++j) pos[j]->set( dist[j] );
     134           0 :     value += kernel->evaluate( pos, der, true );
     135           0 :     for(unsigned j=0; j<3; ++j) {
     136           0 :       derivatives[j] -= der[j]; refders[ indices[i] ][j] += der[j]; tder[j]=der[j];
     137             :     }
     138           0 :     vir -= Tensor( tder, dist );
     139             :   }
     140           0 :   double deriv, fval = sfunc.calculate( value, deriv );
     141           0 :   derivatives *= -deriv*value; vir *= -deriv*value;
     142           0 :   for(unsigned i=1; i<natoms; ++i) refders[ indices[i] ] *= -deriv*value;
     143           0 :   return 1.0 - fval;
     144             : }
     145             : 
     146             : }
     147        4839 : }

Generated by: LCOV version 1.13