LCOV - code coverage report
Current view: top level - multicolvar - VolumeInCylinder.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 47 49 95.9 %
Date: 2024-10-11 08:09:47 Functions: 7 8 87.5 %

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

Generated by: LCOV version 1.15