LCOV - code coverage report
Current view: top level - multicolvar - VolumeInCylinder.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 50 52 96.2 %
Date: 2018-12-19 07:49:13 Functions: 11 12 91.7 %

          Line data    Source code
       1             : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
       2             :    Copyright (c) 2015-2018 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/SwitchingFunction.h"
      25             : #include "ActionVolume.h"
      26             : 
      27             : //+PLUMEDOC VOLUMES INCYLINDER
      28             : /*
      29             : This quantity can be used to calculate functions of the distribution of collective
      30             : variables for the atoms that lie in a particular, user-specified part of of the cell.
      31             : 
      32             : Each of the base quantities calculated by a multicolvar can can be assigned to a particular point in three
      33             : dimensional space. For example, if we have the coordination numbers for all the atoms in the
      34             : system each coordination number can be assumed to lie on the position of the central atom.
      35             : Because each base quantity can be assigned to a particular point in space we can calculate functions of the
      36             : distribution of base quantities in a particular part of the box by using:
      37             : 
      38             : \f[
      39             : \overline{s}_{\tau} = \frac{ \sum_i f(s_i) w(x_i,y_i,z_i) }{ \sum_i w(x_i,y_i,z_i) }
      40             : \f]
      41             : 
      42             : where the sum is over the collective variables, \f$s_i\f$, each of which can be thought to be at \f$ (x_i,y_i,z_i)\f$.
      43             : The function \f$ w(x_i,y_i,z_i) \f$ measures whether or not the system is in the subregion of interest. It
      44             : is equal to:
      45             : 
      46             : \f[
      47             : w(x_i,y_i,z_i) =
      48             : \f]
      49             : 
      50             : where \f$\sigma\f$ is a \ref switchingfunction.
      51             : The function \f$(s_i)\f$ can be any of the usual LESS_THAN, MORE_THAN, WITHIN etc that are used in all other multicolvars.
      52             : 
      53             : When INCYLINDER is used with the \ref DENSITY action the number of atoms in the specified region is calculated
      54             : 
      55             : \par Examples
      56             : 
      57             : The input below can be use to calculate the average coordination numbers for those atoms that are within a cylindrical tube
      58             : of radius 1.5 nm that is centered on the position of atom 101 and that has its long axis parallel to the z-axis.
      59             : 
      60             : \verbatim
      61             : c1: COORDINATIONNUMBER SPECIES=1-100 SWITCH={RATIONAL R_0=0.1}
      62             : d2: INCYLINDER ATOM=101 DATA=d1 DIRECTION=Z RADIUS={TANH R_0=1.5} SIGMA=0.1 LOWER=-0.1 UPPER=0.1 MEAN
      63             : PRINT ARG=d2.* FILE=colvar
      64             : \endverbatim
      65             : 
      66             : */
      67             : //+ENDPLUMEDOC
      68             : 
      69             : namespace PLMD {
      70             : namespace multicolvar {
      71             : 
      72           2 : class VolumeInCylinder : public ActionVolume {
      73             : private:
      74             :   bool docylinder;
      75             :   Vector origin;
      76             :   HistogramBead bead;
      77             :   std::vector<unsigned> dir;
      78             :   SwitchingFunction switchingFunction;
      79             : public:
      80             :   static void registerKeywords( Keywords& keys );
      81             :   explicit VolumeInCylinder (const ActionOptions& ao);
      82             :   void setupRegions();
      83             :   double calculateNumberInside( const Vector& cpos, Vector& derivatives, Tensor& vir, std::vector<Vector>& refders ) const ;
      84             : };
      85             : 
      86        2524 : PLUMED_REGISTER_ACTION(VolumeInCylinder,"INCYLINDER")
      87             : 
      88           2 : void VolumeInCylinder::registerKeywords( Keywords& keys ) {
      89           2 :   ActionVolume::registerKeywords( keys );
      90           2 :   keys.add("atoms","ATOM","the atom whose vicinity we are interested in examining");
      91           2 :   keys.add("compulsory","DIRECTION","the direction of the long axis of the cylinder. Must be x, y or z");
      92           2 :   keys.add("compulsory","RADIUS","a switching function that gives the extent of the cyclinder in the plane perpendicular to the direction");
      93           2 :   keys.add("compulsory","LOWER","0.0","the lower boundary on the direction parallel to the long axis of the cylinder");
      94           2 :   keys.add("compulsory","UPPER","0.0","the upper boundary on the direction parallel to the long axis of the cylinder");
      95           2 :   keys.reset_style("SIGMA","optional");
      96           2 : }
      97             : 
      98           1 : VolumeInCylinder::VolumeInCylinder(const ActionOptions& ao):
      99             :   Action(ao),
     100             :   ActionVolume(ao),
     101           1 :   docylinder(false)
     102             : {
     103           1 :   std::vector<AtomNumber> atom;
     104           1 :   parseAtomList("ATOM",atom);
     105           1 :   if( atom.size()!=1 ) error("should only be one atom specified");
     106           1 :   log.printf("  center of cylinder is at position of atom : %d\n",atom[0].serial() );
     107             : 
     108           2 :   std::string sdir; parse("DIRECTION",sdir);
     109           1 :   if( sdir=="X") {dir.push_back(1); dir.push_back(2); dir.push_back(0); }
     110           1 :   else if( sdir=="Y") {dir.push_back(0); dir.push_back(2); dir.push_back(1); }
     111           1 :   else if( sdir=="Z") {dir.push_back(0); dir.push_back(1); dir.push_back(2); }
     112           0 :   else { error(sdir + "is not a valid direction.  Should be X, Y or Z"); }
     113           1 :   log.printf("  cylinder's long axis is along %s axis\n",sdir.c_str() );
     114             : 
     115           2 :   std::string sw, errors; parse("RADIUS",sw);
     116           1 :   if(sw.length()==0) error("missing RADIUS keyword");
     117           1 :   switchingFunction.set(sw,errors);
     118           1 :   if( errors.length()!=0 ) error("problem reading RADIUS keyword : " + errors );
     119           1 :   log.printf("  radius of cylinder is given by %s \n", ( switchingFunction.description() ).c_str() );
     120             : 
     121           1 :   double min, max; parse("LOWER",min); parse("UPPER",max);
     122           1 :   if( min!=0.0 ||  max!=0.0 ) {
     123           1 :     if( min>max ) error("minimum of cylinder should be less than maximum");
     124           1 :     docylinder=true;
     125           1 :     log.printf("  cylinder extends from %f to %f along the %s axis\n",min,max,sdir.c_str() );
     126           1 :     bead.isNotPeriodic(); bead.setKernelType( getKernelType() ); bead.set( min, max, getSigma() );
     127             :   }
     128             : 
     129           2 :   checkRead(); requestAtoms(atom);
     130           1 : }
     131             : 
     132          20 : void VolumeInCylinder::setupRegions() { }
     133             : 
     134        3992 : double VolumeInCylinder::calculateNumberInside( const Vector& cpos, Vector& derivatives, Tensor& vir, std::vector<Vector>& refders ) const {
     135             :   // Calculate position of atom wrt to origin
     136        3992 :   Vector fpos=pbcDistance( getPosition(0), cpos );
     137             : 
     138             :   double vcylinder, dcylinder;
     139        3997 :   if( docylinder ) {
     140        3997 :     vcylinder=bead.calculate( fpos[dir[2]], dcylinder );
     141             :   } else {
     142           0 :     vcylinder=1.0; dcylinder=0.0;
     143             :   }
     144             : 
     145        3995 :   const double dd = fpos[dir[0]]*fpos[dir[0]] + fpos[dir[1]]*fpos[dir[1]];
     146        3993 :   double dfunc, vswitch = switchingFunction.calculateSqr( dd, dfunc );
     147        3995 :   derivatives.zero(); double value=vswitch*vcylinder;
     148        3999 :   derivatives[dir[0]]=vcylinder*dfunc*fpos[dir[0]];
     149        3995 :   derivatives[dir[1]]=vcylinder*dfunc*fpos[dir[1]];
     150        3999 :   derivatives[dir[2]]=vswitch*dcylinder;
     151             :   // Add derivatives wrt to position of origin atom
     152        4000 :   refders[0] = -derivatives;
     153             :   // Add virial contribution
     154        4000 :   vir -= Tensor(fpos,derivatives);
     155        3998 :   return value;
     156             : }
     157             : 
     158             : }
     159        2523 : }

Generated by: LCOV version 1.13