LCOV - code coverage report
Current view: top level - multicolvar - VolumeAround.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 59 61 96.7 %
Date: 2026-03-30 13:16:06 Functions: 7 8 87.5 %

          Line data    Source code
       1             : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
       2             :    Copyright (c) 2013-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 "ActionVolume.h"
      25             : 
      26             : //+PLUMEDOC VOLUMES AROUND
      27             : /*
      28             : 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.
      29             : 
      30             : Each of the base quantities calculated by a multicolvar can can be assigned to a particular point in three
      31             : dimensional space. For example, if we have the coordination numbers for all the atoms in the
      32             : system each coordination number can be assumed to lie on the position of the central atom.
      33             : Because each base quantity can be assigned to a particular point in space we can calculate functions of the
      34             : distribution of base quantities in a particular part of the box by using:
      35             : 
      36             : \f[
      37             : \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) }
      38             : \f]
      39             : 
      40             : 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$.
      41             : The function \f$ w(x_i,y_i,z_i) \f$ measures whether or not the system is in the subregion of interest. It
      42             : is equal to:
      43             : 
      44             : \f[
      45             : w(x_i,y_i,z_i) = \int_{xl}^{xu} \int_{yl}^{yu} \int_{zl}^{zu} \textrm{d}x\textrm{d}y\textrm{d}z K\left( \frac{x - x_i}{\sigma} \right)K\left( \frac{y - y_i}{\sigma} \right)K\left( \frac{z - z_i}{\sigma} \right)
      46             : \f]
      47             : 
      48             : where \f$K\f$ is one of the kernel functions described on \ref histogrambead and \f$\sigma\f$ is a bandwidth parameter.
      49             : 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.
      50             : 
      51             : When AROUND is used with the \ref DENSITY action the number of atoms in the specified region is calculated
      52             : 
      53             : \par Examples
      54             : 
      55             : The following commands tell plumed to calculate the average coordination number for the atoms
      56             : that have x (in fractional coordinates) within 2.0 nm of the com of mass c1. The final value will be labeled s.mean.
      57             : \plumedfile
      58             : COM ATOMS=1-100 LABEL=c1
      59             : COORDINATIONNUMBER SPECIES=1-100 R_0=1.0 LABEL=c
      60             : AROUND DATA=c ATOM=c1 XLOWER=-2.0 XUPPER=2.0 SIGMA=0.1 MEAN LABEL=s
      61             : \endplumedfile
      62             : 
      63             : */
      64             : //+ENDPLUMEDOC
      65             : 
      66             : namespace PLMD {
      67             : namespace multicolvar {
      68             : 
      69             : class VolumeAround : public ActionVolume {
      70             : private:
      71             :   Vector origin;
      72             :   bool dox, doy, doz;
      73             :   double xlow, xhigh;
      74             :   double ylow, yhigh;
      75             :   double zlow, zhigh;
      76             : public:
      77             :   static void registerKeywords( Keywords& keys );
      78             :   explicit VolumeAround(const ActionOptions& ao);
      79             :   void setupRegions() override;
      80             :   double calculateNumberInside( const Vector& cpos, Vector& derivatives, Tensor& vir, std::vector<Vector>& refders ) const override;
      81             : };
      82             : 
      83       13829 : PLUMED_REGISTER_ACTION(VolumeAround,"AROUND")
      84             : 
      85          26 : void VolumeAround::registerKeywords( Keywords& keys ) {
      86          26 :   ActionVolume::registerKeywords( keys );
      87          52 :   keys.add("atoms","ATOM","the atom whose vicinity we are interested in examining");
      88          52 :   keys.add("compulsory","XLOWER","0.0","the lower boundary in x relative to the x coordinate of the atom (0 indicates use full extent of box).");
      89          52 :   keys.add("compulsory","XUPPER","0.0","the upper boundary in x relative to the x coordinate of the atom (0 indicates use full extent of box).");
      90          52 :   keys.add("compulsory","YLOWER","0.0","the lower boundary in y relative to the y coordinate of the atom (0 indicates use full extent of box).");
      91          52 :   keys.add("compulsory","YUPPER","0.0","the upper boundary in y relative to the y coordinate of the atom (0 indicates use full extent of box).");
      92          52 :   keys.add("compulsory","ZLOWER","0.0","the lower boundary in z relative to the z coordinate of the atom (0 indicates use full extent of box).");
      93          52 :   keys.add("compulsory","ZUPPER","0.0","the upper boundary in z relative to the z coordinate of the atom (0 indicates use full extent of box).");
      94          26 : }
      95             : 
      96          22 : VolumeAround::VolumeAround(const ActionOptions& ao):
      97             :   Action(ao),
      98          22 :   ActionVolume(ao) {
      99             :   std::vector<AtomNumber> atom;
     100          44 :   parseAtomList("ATOM",atom);
     101          22 :   if( atom.size()!=1 ) {
     102           0 :     error("should only be one atom specified");
     103             :   }
     104          22 :   log.printf("  boundaries for region are calculated based on positions of atom : %d\n",atom[0].serial() );
     105             : 
     106          22 :   dox=true;
     107          22 :   parse("XLOWER",xlow);
     108          22 :   parse("XUPPER",xhigh);
     109          22 :   doy=true;
     110          22 :   parse("YLOWER",ylow);
     111          22 :   parse("YUPPER",yhigh);
     112          22 :   doz=true;
     113          22 :   parse("ZLOWER",zlow);
     114          22 :   parse("ZUPPER",zhigh);
     115          22 :   if( xlow==0.0 && xhigh==0.0 ) {
     116           6 :     dox=false;
     117             :   }
     118          22 :   if( ylow==0.0 && yhigh==0.0 ) {
     119          14 :     doy=false;
     120             :   }
     121          22 :   if( zlow==0.0 && zhigh==0.0 ) {
     122          14 :     doz=false;
     123             :   }
     124          22 :   if( !dox && !doy && !doz ) {
     125           0 :     error("no subregion defined use XLOWER, XUPPER, YLOWER, YUPPER, ZLOWER, ZUPPER");
     126             :   }
     127          22 :   log.printf("  boundaries for region (region of interest about atom) : x %f %f, y %f %f, z %f %f \n",xlow,xhigh,ylow,yhigh,zlow,zhigh);
     128          22 :   checkRead();
     129          22 :   requestAtoms(atom);
     130          22 : }
     131             : 
     132         317 : void VolumeAround::setupRegions() { }
     133             : 
     134       56393 : double VolumeAround::calculateNumberInside( const Vector& cpos, Vector& derivatives, Tensor& vir, std::vector<Vector>& refders ) const {
     135             :   // Setup the histogram bead
     136       56393 :   HistogramBead bead;
     137             :   bead.isNotPeriodic();
     138       56393 :   bead.setKernelType( getKernelType() );
     139             : 
     140             :   // Calculate position of atom wrt to origin
     141       56393 :   Vector fpos=pbcDistance( getPosition(0), cpos );
     142             :   double xcontr, ycontr, zcontr, xder, yder, zder;
     143       56393 :   if( dox ) {
     144       32393 :     bead.set( xlow, xhigh, getSigma() );
     145       32393 :     xcontr=bead.calculate( fpos[0], xder );
     146             :   } else {
     147             :     xcontr=1.;
     148       24000 :     xder=0.;
     149             :   }
     150       56393 :   if( doy ) {
     151       32000 :     bead.set( ylow, yhigh, getSigma() );
     152       32000 :     ycontr=bead.calculate( fpos[1], yder );
     153             :   } else {
     154             :     ycontr=1.;
     155       24393 :     yder=0.;
     156             :   }
     157       56393 :   if( doz ) {
     158       32000 :     bead.set( zlow, zhigh, getSigma() );
     159       32000 :     zcontr=bead.calculate( fpos[2], zder );
     160             :   } else {
     161             :     zcontr=1.;
     162       24393 :     zder=0.;
     163             :   }
     164       56393 :   derivatives[0]=xder*ycontr*zcontr;
     165       56393 :   derivatives[1]=xcontr*yder*zcontr;
     166       56393 :   derivatives[2]=xcontr*ycontr*zder;
     167             :   // Add derivatives wrt to position of origin atom
     168       56393 :   refders[0] = -derivatives;
     169             :   // Add virial contribution
     170       56393 :   vir -= Tensor(fpos,derivatives);
     171       56393 :   return xcontr*ycontr*zcontr;
     172             : }
     173             : 
     174             : }
     175             : }

Generated by: LCOV version 1.16