LCOV - code coverage report
Current view: top level - gridtools - Gradient.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 59 59 100.0 %
Date: 2026-11-05 10:19:04 Functions: 2 3 66.7 %

          Line data    Source code
       1             : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
       2             :    Copyright (c) 2016-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/ActionShortcut.h"
      23             : #include "core/ActionRegister.h"
      24             : 
      25             : //+PLUMEDOC MCOLVAR GRADIENT
      26             : /*
      27             : Calculate the gradient of an input grid
      28             : 
      29             : This shortcut implements the gradient CV that was used to drive nucleation and that is described in the paper cited below. A description that explains how
      30             : this CV is evaluated and used can be found in the paper.
      31             : 
      32             : An example input for computing and printing the gradient CV that is discussed in the paper is shown below:
      33             : 
      34             : ```plumed
      35             : s1xyz: GRADIENT ATOMS=1-50 ORIGIN=1 DIR=xyz NBINS=4 SIGMA=1.0
      36             : PRINT ARG=s1xyz FILE=colvar
      37             : ```
      38             : 
      39             : */
      40             : //+ENDPLUMEDOC
      41             : 
      42             : namespace PLMD {
      43             : namespace gridtools {
      44             : 
      45             : class Gradient : public ActionShortcut {
      46             : public:
      47             :   static void registerKeywords(Keywords& keys);
      48             :   explicit Gradient(const ActionOptions&);
      49             : };
      50             : 
      51             : PLUMED_REGISTER_ACTION(Gradient,"GRADIENT")
      52             : 
      53          11 : void Gradient::registerKeywords( Keywords& keys ) {
      54          11 :   ActionShortcut::registerKeywords( keys );
      55          11 :   keys.add("compulsory","ORIGIN","we will use the position of this atom as the origin in our calculation");
      56          11 :   keys.add("compulsory","NBINS","number of bins to use in each direction for the calculation of the gradient");
      57          11 :   keys.add("compulsory","DIR","xyz","the directions in which we are calculating the graident.  Should be x, y, z, xy, xz, yz or xyz");
      58          11 :   keys.add("compulsory","SIGMA","the width of the function to be used for kernel density estimation");
      59          11 :   keys.add("compulsory","KERNEL","gaussian-bin","the type of kernel function to be used in the grids");
      60          11 :   keys.add("compulsory","ATOMS","calculate the gradient of these atoms");
      61          22 :   keys.setValueDescription("scalar","the desired gradient");
      62          11 :   keys.addDOI("10.1021/ct4002027");
      63          11 :   keys.needsAction("DISTANCES");
      64          11 :   keys.needsAction("KDE");
      65          11 :   keys.needsAction("INTERPOLATE_GRID");
      66          11 :   keys.needsAction("CUSTOM");
      67          11 :   keys.needsAction("SUM");
      68          11 :   keys.needsAction("COMBINE");
      69          11 : }
      70             : 
      71           7 : Gradient::Gradient(const ActionOptions&ao):
      72             :   Action(ao),
      73           7 :   ActionShortcut(ao) {
      74             :   std::string atom_str;
      75          14 :   parse("ATOMS",atom_str);
      76             :   std::string dir;
      77          14 :   parse("DIR",dir);
      78             :   std::string origin_str;
      79          14 :   parse("ORIGIN",origin_str);
      80             :   std::string nbin_str;
      81          14 :   parse("NBINS",nbin_str);
      82             :   std::string band_str;
      83          14 :   parse("SIGMA",band_str);
      84             :   std::string kernel_str;
      85           7 :   parse("KERNEL",kernel_str);
      86             :   // First get positions of all atoms relative to origin
      87          21 :   readInputLine( getShortcutLabel() + "_dist: DISTANCES ORIGIN=" + origin_str + " ATOMS=" + atom_str + " COMPONENTS");
      88             :   // Now constrcut the histograms
      89           7 :   if( dir=="x" || dir=="xy" || dir=="xz" || dir=="xyz" ) {
      90          16 :     readInputLine( getShortcutLabel() + "_xhisto: KDE ARG=" + getShortcutLabel() + "_dist.x GRID_BIN=" + nbin_str + " KERNEL=" + kernel_str + " BANDWIDTH=" + band_str );
      91           4 :     std::string thislab = getShortcutLabel() + "_xgrad";
      92           4 :     if( dir=="x" ) {
      93           1 :       thislab = getShortcutLabel();
      94             :     }
      95           8 :     readInputLine( thislab + "_shift: INTERPOLATE_GRID ARG=" + getShortcutLabel() + "_xhisto INTERPOLATION_TYPE=ceiling GRID_BIN=" + nbin_str );
      96          12 :     readInputLine( thislab + "_x2: CUSTOM ARG=" + getShortcutLabel() + "_xhisto," + thislab + "_shift FUNC=(x-y)*(x-y) PERIODIC=NO");
      97          12 :     readInputLine( thislab + ": SUM ARG=" + thislab + "_x2 PERIODIC=NO");
      98             :   }
      99           7 :   if( dir=="y" || dir=="xy" || dir=="yz" || dir=="xyz" ) {
     100          16 :     readInputLine( getShortcutLabel() + "_yhisto: KDE ARG=" + getShortcutLabel() + "_dist.y GRID_BIN=" + nbin_str + " KERNEL=" + kernel_str + " BANDWIDTH=" + band_str );
     101           4 :     std::string thislab = getShortcutLabel() + "_ygrad";
     102           4 :     if( dir=="y" ) {
     103           1 :       thislab = getShortcutLabel();
     104             :     }
     105           8 :     readInputLine( thislab + "_shift: INTERPOLATE_GRID ARG=" + getShortcutLabel() + "_yhisto INTERPOLATION_TYPE=ceiling GRID_BIN=" + nbin_str );
     106          12 :     readInputLine( thislab + "_x2: CUSTOM ARG=" + getShortcutLabel() + "_yhisto," + thislab + "_shift FUNC=(x-y)*(x-y) PERIODIC=NO");
     107          12 :     readInputLine( thislab + ": SUM ARG=" + thislab + "_x2 PERIODIC=NO");
     108             :   }
     109           7 :   if( dir=="z" || dir=="yz" || dir=="xz" || dir=="xyz" ) {
     110          16 :     readInputLine( getShortcutLabel() + "_zhisto: KDE ARG=" + getShortcutLabel() + "_dist.z GRID_BIN=" + nbin_str + " KERNEL=" + kernel_str + " BANDWIDTH=" + band_str );
     111           4 :     std::string thislab = getShortcutLabel() + "_zgrad";
     112           4 :     if( dir=="z" ) {
     113           1 :       thislab = getShortcutLabel();
     114             :     }
     115           8 :     readInputLine( thislab + "_shift: INTERPOLATE_GRID ARG=" + getShortcutLabel() + "_zhisto INTERPOLATION_TYPE=ceiling GRID_BIN=" + nbin_str );
     116          12 :     readInputLine( thislab + "_x2: CUSTOM ARG=" + getShortcutLabel() + "_zhisto," + thislab + "_shift FUNC=(x-y)*(x-y) PERIODIC=NO");
     117          12 :     readInputLine( thislab + ": SUM ARG=" + thislab + "_x2 PERIODIC=NO");
     118             :   }
     119           7 :   if( dir=="xy" ) {
     120           4 :     readInputLine( getShortcutLabel() + ": COMBINE ARG=" + getShortcutLabel() + "_xgrad," + getShortcutLabel() + "_ygrad PERIODIC=NO");
     121           6 :   } else if( dir=="xz" ) {
     122           4 :     readInputLine( getShortcutLabel() + ": COMBINE ARG=" + getShortcutLabel() + "_xgrad," + getShortcutLabel() + "_zgrad PERIODIC=NO");
     123           5 :   } else if( dir=="yz" ) {
     124           4 :     readInputLine( getShortcutLabel() + ": COMBINE ARG=" + getShortcutLabel() + "_ygrad," + getShortcutLabel() + "_zgrad PERIODIC=NO");
     125           4 :   } else if( dir=="xyz" ) {
     126           5 :     readInputLine( getShortcutLabel() + ": COMBINE ARG=" + getShortcutLabel() + "_xgrad," + getShortcutLabel() + "_ygrad," + getShortcutLabel() + "_zgrad PERIODIC=NO");
     127             :   }
     128           7 : }
     129             : 
     130             : 
     131             : 
     132             : }
     133             : }

Generated by: LCOV version 1.16