LCOV - code coverage report
Current view: top level - gridtools - Gradient.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 58 58 100.0 %
Date: 2025-04-08 21:11:17 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             : \par Examples
      30             : 
      31             : */
      32             : //+ENDPLUMEDOC
      33             : 
      34             : namespace PLMD {
      35             : namespace gridtools {
      36             : 
      37             : class Gradient : public ActionShortcut {
      38             : public:
      39             :   static void registerKeywords(Keywords& keys);
      40             :   explicit Gradient(const ActionOptions&);
      41             : };
      42             : 
      43             : PLUMED_REGISTER_ACTION(Gradient,"GRADIENT")
      44             : 
      45           9 : void Gradient::registerKeywords( Keywords& keys ) {
      46           9 :   ActionShortcut::registerKeywords( keys );
      47           9 :   keys.add("compulsory","ORIGIN","we will use the position of this atom as the origin in our calculation");
      48           9 :   keys.add("compulsory","NBINS","number of bins to use in each direction for the calculation of the gradient");
      49           9 :   keys.add("compulsory","DIR","xyz","the directions in which we are calculating the graident.  Should be x, y, z, xy, xz, yz or xyz");
      50           9 :   keys.add("compulsory","SIGMA","the width of the function to be used for kernel density estimation");
      51           9 :   keys.add("compulsory","KERNEL","gaussian-bin","the type of kernel function to be used in the grids");
      52           9 :   keys.add("compulsory","ATOMS","calculate the gradient of these atoms");
      53          18 :   keys.setValueDescription("scalar","the desired gradient");
      54           9 :   keys.needsAction("DISTANCES");
      55           9 :   keys.needsAction("KDE");
      56           9 :   keys.needsAction("INTERPOLATE_GRID");
      57           9 :   keys.needsAction("CUSTOM");
      58           9 :   keys.needsAction("SUM_GRID");
      59           9 :   keys.needsAction("COMBINE");
      60           9 : }
      61             : 
      62           7 : Gradient::Gradient(const ActionOptions&ao):
      63             :   Action(ao),
      64           7 :   ActionShortcut(ao) {
      65             :   std::string atom_str;
      66          14 :   parse("ATOMS",atom_str);
      67             :   std::string dir;
      68          14 :   parse("DIR",dir);
      69             :   std::string origin_str;
      70          14 :   parse("ORIGIN",origin_str);
      71             :   std::string nbin_str;
      72          14 :   parse("NBINS",nbin_str);
      73             :   std::string band_str;
      74          14 :   parse("SIGMA",band_str);
      75             :   std::string kernel_str;
      76           7 :   parse("KERNEL",kernel_str);
      77             :   // First get positions of all atoms relative to origin
      78          14 :   readInputLine( getShortcutLabel() + "_dist: DISTANCES ORIGIN=" + origin_str + " ATOMS=" + atom_str + " COMPONENTS");
      79             :   // Now constrcut the histograms
      80          22 :   if( dir=="x" || dir=="xy" || dir=="xz" || dir=="xyz" ) {
      81           8 :     readInputLine( getShortcutLabel() + "_xhisto: KDE ARG=" + getShortcutLabel() + "_dist.x GRID_BIN=" + nbin_str + " KERNEL=" + kernel_str + " BANDWIDTH=" + band_str );
      82           4 :     std::string thislab = getShortcutLabel() + "_xgrad";
      83           4 :     if( dir=="x" ) {
      84           1 :       thislab = getShortcutLabel();
      85             :     }
      86           8 :     readInputLine( thislab + "_shift: INTERPOLATE_GRID ARG=" + getShortcutLabel() + "_xhisto INTERPOLATION_TYPE=ceiling MIDPOINTS");
      87           8 :     readInputLine( thislab + "_x2: CUSTOM ARG=" + getShortcutLabel() + "_xhisto," + thislab + "_shift FUNC=(x-y)*(x-y) PERIODIC=NO");
      88           8 :     readInputLine( thislab + ": SUM_GRID ARG=" + thislab + "_x2 PERIODIC=NO");
      89             :   }
      90          22 :   if( dir=="y" || dir=="xy" || dir=="yz" || dir=="xyz" ) {
      91           8 :     readInputLine( getShortcutLabel() + "_yhisto: KDE ARG=" + getShortcutLabel() + "_dist.y GRID_BIN=" + nbin_str + " KERNEL=" + kernel_str + " BANDWIDTH=" + band_str );
      92           4 :     std::string thislab = getShortcutLabel() + "_ygrad";
      93           4 :     if( dir=="y" ) {
      94           1 :       thislab = getShortcutLabel();
      95             :     }
      96           8 :     readInputLine( thislab + "_shift: INTERPOLATE_GRID ARG=" + getShortcutLabel() + "_yhisto INTERPOLATION_TYPE=ceiling MIDPOINTS");
      97           8 :     readInputLine( thislab + "_x2: CUSTOM ARG=" + getShortcutLabel() + "_yhisto," + thislab + "_shift FUNC=(x-y)*(x-y) PERIODIC=NO");
      98           8 :     readInputLine( thislab + ": SUM_GRID ARG=" + thislab + "_x2 PERIODIC=NO");
      99             :   }
     100          22 :   if( dir=="z" || dir=="yz" || dir=="xz" || dir=="xyz" ) {
     101           8 :     readInputLine( getShortcutLabel() + "_zhisto: KDE ARG=" + getShortcutLabel() + "_dist.z GRID_BIN=" + nbin_str + " KERNEL=" + kernel_str + " BANDWIDTH=" + band_str );
     102           4 :     std::string thislab = getShortcutLabel() + "_zgrad";
     103           4 :     if( dir=="z" ) {
     104           1 :       thislab = getShortcutLabel();
     105             :     }
     106           8 :     readInputLine( thislab + "_shift: INTERPOLATE_GRID ARG=" + getShortcutLabel() + "_zhisto INTERPOLATION_TYPE=ceiling MIDPOINTS");
     107           8 :     readInputLine( thislab + "_x2: CUSTOM ARG=" + getShortcutLabel() + "_zhisto," + thislab + "_shift FUNC=(x-y)*(x-y) PERIODIC=NO");
     108           8 :     readInputLine( thislab + ": SUM_GRID ARG=" + thislab + "_x2 PERIODIC=NO");
     109             :   }
     110           7 :   if( dir=="xy" ) {
     111           2 :     readInputLine( getShortcutLabel() + ": COMBINE ARG=" + getShortcutLabel() + "_xgrad," + getShortcutLabel() + "_ygrad PERIODIC=NO");
     112           6 :   } else if( dir=="xz" ) {
     113           2 :     readInputLine( getShortcutLabel() + ": COMBINE ARG=" + getShortcutLabel() + "_xgrad," + getShortcutLabel() + "_zgrad PERIODIC=NO");
     114           5 :   } else if( dir=="yz" ) {
     115           2 :     readInputLine( getShortcutLabel() + ": COMBINE ARG=" + getShortcutLabel() + "_ygrad," + getShortcutLabel() + "_zgrad PERIODIC=NO");
     116           4 :   } else if( dir=="xyz" ) {
     117           2 :     readInputLine( getShortcutLabel() + ": COMBINE ARG=" + getShortcutLabel() + "_xgrad," + getShortcutLabel() + "_ygrad," + getShortcutLabel() + "_zgrad PERIODIC=NO");
     118             :   }
     119           7 : }
     120             : 
     121             : 
     122             : 
     123             : }
     124             : }

Generated by: LCOV version 1.16