LCOV - code coverage report
Current view: top level - multicolvar - Distances.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 85 88 96.6 %
Date: 2025-11-25 13:55:50 Functions: 2 3 66.7 %

          Line data    Source code
       1             : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
       2             :    Copyright (c) 2012-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             : #include "MultiColvarShortcuts.h"
      25             : #include <string>
      26             : #include <cmath>
      27             : 
      28             : //+PLUMEDOC MCOLVAR DISTANCES
      29             : /*
      30             : Calculate the distances between multiple piars of atoms
      31             : 
      32             : \par Examples
      33             : 
      34             : */
      35             : //+ENDPLUMEDOC
      36             : 
      37             : //+PLUMEDOC MCOLVAR XDISTANCES
      38             : /*
      39             : Calculate the x components of the vectors connecting one or many pairs of atoms.
      40             : You can then calculate functions of the distribution of values such as the minimum, the number less than a certain quantity and so on.
      41             : 
      42             : \par Examples
      43             : 
      44             : The following input tells plumed to calculate the x-component of the vector connecting atom 3 to atom 5 and
      45             : the x-component of the vector connecting atom 1 to atom 2.  The minimum of these two quantities is then
      46             : printed
      47             : \plumedfile
      48             : d1: XDISTANCES ATOMS1=3,5 ATOMS2=1,2 MIN={BETA=0.1}
      49             : PRINT ARG=d1.min
      50             : \endplumedfile
      51             : (See also \ref PRINT).
      52             : 
      53             : 
      54             : The following input tells plumed to calculate the x-component of the vector connecting atom 3 to atom 5 and
      55             : the x-component of the vector connecting atom 1 to atom 2.  The number of values that are
      56             : less than 0.1nm is then printed to a file.
      57             : \plumedfile
      58             : d1: XDISTANCES ATOMS1=3,5 ATOMS2=1,2 LESS_THAN={RATIONAL R_0=0.1}
      59             : PRINT ARG=d1.lessthan
      60             : \endplumedfile
      61             : (See also \ref PRINT \ref switchingfunction).
      62             : 
      63             : The following input tells plumed to calculate the x-components of all the distinct vectors that can be created
      64             : between atoms 1, 2 and 3 (i.e. the vectors between atoms 1 and 2, atoms 1 and 3 and atoms 2 and 3).
      65             : The average of these quantities is then calculated.
      66             : \plumedfile
      67             : d1: XDISTANCES GROUP=1-3 MEAN
      68             : PRINT ARG=d1.mean
      69             : \endplumedfile
      70             : (See also \ref PRINT)
      71             : 
      72             : The following input tells plumed to calculate all the vectors connecting the the atoms in GROUPA to the atoms in GROUPB.
      73             : In other words the vector between atoms 1 and 2 and the vector between atoms 1 and 3.  The number of values
      74             : more than 0.1 is then printed to a file.
      75             : \plumedfile
      76             : d1: XDISTANCES GROUPA=1 GROUPB=2,3 MORE_THAN={RATIONAL R_0=0.1}
      77             : PRINT ARG=d1.morethan
      78             : \endplumedfile
      79             : (See also \ref PRINT \ref switchingfunction)
      80             : */
      81             : //+ENDPLUMEDOC
      82             : 
      83             : 
      84             : //+PLUMEDOC MCOLVAR YDISTANCES
      85             : /*
      86             : Calculate the y components of the vectors connecting one or many pairs of atoms.
      87             : You can then calculate functions of the distribution of values such as the minimum, the number less than a certain quantity and so on.
      88             : 
      89             : \par Examples
      90             : 
      91             : The following input tells plumed to calculate the y-component of the vector connecting atom 3 to atom 5 and
      92             : the y-component of the vector connecting atom 1 to atom 2.  The minimum of these two quantities is then
      93             : printed
      94             : \plumedfile
      95             : d1: YDISTANCES ATOMS1=3,5 ATOMS2=1,2 MIN={BETA=0.1}
      96             : PRINT ARG=d1.min
      97             : \endplumedfile
      98             : (See also \ref PRINT).
      99             : 
     100             : 
     101             : The following input tells plumed to calculate the y-component of the vector connecting atom 3 to atom 5 and
     102             : the y-component of the vector connecting atom 1 to atom 2.  The number of values that are
     103             : less than 0.1nm is then printed to a file.
     104             : \plumedfile
     105             : d1: YDISTANCES ATOMS1=3,5 ATOMS2=1,2 LESS_THAN={RATIONAL R_0=0.1}
     106             : PRINT ARG=d1.lessthan
     107             : \endplumedfile
     108             : (See also \ref PRINT \ref switchingfunction).
     109             : 
     110             : The following input tells plumed to calculate the y-components of all the distinct vectors that can be created
     111             : between atoms 1, 2 and 3 (i.e. the vectors between atoms 1 and 2, atoms 1 and 3 and atoms 2 and 3).
     112             : The average of these quantities is then calculated.
     113             : \plumedfile
     114             : d1: YDISTANCES GROUP=1-3 MEAN
     115             : PRINT ARG=d1.mean
     116             : \endplumedfile
     117             : (See also \ref PRINT)
     118             : 
     119             : The following input tells plumed to calculate all the vectors connecting the the atoms in GROUPA to the atoms in GROUPB.
     120             : In other words the vector between atoms 1 and 2 and the vector between atoms 1 and 3.  The number of values
     121             : more than 0.1 is then printed to a file.
     122             : \plumedfile
     123             : d1: YDISTANCES GROUPA=1 GROUPB=2,3 MORE_THAN={RATIONAL R_0=0.1}
     124             : PRINT ARG=d1.morethan
     125             : \endplumedfile
     126             : (See also \ref PRINT \ref switchingfunction)
     127             : 
     128             : */
     129             : //+ENDPLUMEDOC
     130             : 
     131             : //+PLUMEDOC MCOLVAR ZDISTANCES
     132             : /*
     133             : Calculate the z components of the vectors connecting one or many pairs of atoms.
     134             : You can then calculate functions of the distribution of values such as the minimum, the number less than a certain quantity and so on.
     135             : 
     136             : \par Examples
     137             : 
     138             : The following input tells plumed to calculate the z-component of the vector connecting atom 3 to atom 5 and
     139             : the z-component of the vector connecting atom 1 to atom 2.  The minimum of these two quantities is then
     140             : printed
     141             : \plumedfile
     142             : d1: ZDISTANCES ATOMS1=3,5 ATOMS2=1,2 MIN={BETA=0.1}
     143             : PRINT ARG=d1.min
     144             : \endplumedfile
     145             : (See also \ref PRINT).
     146             : 
     147             : 
     148             : The following input tells plumed to calculate the z-component of the vector connecting atom 3 to atom 5 and
     149             : the z-component of the vector connecting atom 1 to atom 2.  The number of values that are
     150             : less than 0.1nm is then printed to a file.
     151             : \plumedfile
     152             : d1: ZDISTANCES ATOMS1=3,5 ATOMS2=1,2 LESS_THAN={RATIONAL R_0=0.1}
     153             : PRINT ARG=d1.lessthan
     154             : \endplumedfile
     155             : (See also \ref PRINT \ref switchingfunction).
     156             : 
     157             : The following input tells plumed to calculate the z-components of all the distinct vectors that can be created
     158             : between atoms 1, 2 and 3 (i.e. the vectors between atoms 1 and 2, atoms 1 and 3 and atoms 2 and 3).
     159             : The average of these quantities is then calculated.
     160             : \plumedfile
     161             : d1: ZDISTANCES GROUP=1-3 MEAN
     162             : PRINT ARG=d1.mean
     163             : \endplumedfile
     164             : (See also \ref PRINT)
     165             : 
     166             : The following input tells plumed to calculate all the vectors connecting the the atoms in GROUPA to the atoms in GROUPB.
     167             : In other words the vector between atoms 1 and 2 and the vector between atoms 1 and 3.  The number of values
     168             : more than 0.1 is then printed to a file.
     169             : \plumedfile
     170             : d1: ZDISTANCES GROUPA=1 GROUPB=2,3 MORE_THAN={RATIONAL R_0=0.1}
     171             : PRINT ARG=d1.morethan
     172             : \endplumedfile
     173             : (See also \ref PRINT \ref switchingfunction)
     174             : 
     175             : */
     176             : //+ENDPLUMEDOC
     177             : 
     178             : 
     179             : namespace PLMD {
     180             : namespace multicolvar {
     181             : 
     182             : class Distances : public ActionShortcut {
     183             : public:
     184             :   static void registerKeywords(Keywords& keys);
     185             :   explicit Distances(const ActionOptions&);
     186             : };
     187             : 
     188             : PLUMED_REGISTER_ACTION(Distances,"DISTANCES")
     189             : PLUMED_REGISTER_ACTION(Distances,"XDISTANCES")
     190             : PLUMED_REGISTER_ACTION(Distances,"YDISTANCES")
     191             : PLUMED_REGISTER_ACTION(Distances,"ZDISTANCES")
     192             : 
     193         132 : void Distances::registerKeywords(Keywords& keys) {
     194         132 :   ActionShortcut::registerKeywords( keys );
     195         264 :   keys.add("atoms-1","GROUP","Calculate the distance between each distinct pair of atoms in the group");
     196         264 :   keys.add("atoms-2","GROUPA","Calculate the distances between all the atoms in GROUPA and all "
     197             :            "the atoms in GROUPB. This must be used in conjunction with GROUPB.");
     198         264 :   keys.add("atoms-2","GROUPB","Calculate the distances between all the atoms in GROUPA and all the atoms "
     199             :            "in GROUPB. This must be used in conjunction with GROUPA.");
     200         264 :   keys.add("numbered","ATOMS","the pairs of atoms that you would like to calculate the angles for");
     201         264 :   keys.addFlag("NOPBC",false,"ignore the periodic boundary conditions when calculating distances");
     202         264 :   keys.addFlag("COMPONENTS",false,"calculate the x, y and z components of the distance separately and store them as label.x, label.y and label.z");
     203         264 :   keys.addFlag("SCALED_COMPONENTS",false,"calculate the a, b and c scaled components of the distance separately and store them as label.a, label.b and label.c");
     204         264 :   keys.addFlag("LOWMEM",false,"this flag does nothing and is present only to ensure back-compatibility");
     205         264 :   keys.reset_style("ATOMS","atoms");
     206         132 :   MultiColvarShortcuts::shortcutKeywords( keys );
     207         264 :   keys.add("atoms","ORIGIN","calculate the distance of all the atoms specified using the ATOMS keyword from this point");
     208         264 :   keys.add("numbered","LOCATION","the location at which the CV is assumed to be in space");
     209         264 :   keys.reset_style("LOCATION","atoms");
     210         264 :   keys.addOutputComponent("x","COMPONENTS","the x-components of the distance vectors");
     211         264 :   keys.addOutputComponent("y","COMPONENTS","the y-components of the distance vectors");
     212         264 :   keys.addOutputComponent("z","COMPONENTS","the z-components of the distance vectors");
     213         132 :   keys.needsAction("GROUP");
     214         132 :   keys.needsAction("DISTANCE");
     215         132 :   keys.needsAction("CENTER");
     216         132 : }
     217             : 
     218          58 : Distances::Distances(const ActionOptions& ao):
     219             :   Action(ao),
     220          58 :   ActionShortcut(ao) {
     221             :   // Create distances
     222             :   bool lowmem;
     223          58 :   parseFlag("LOWMEM",lowmem);
     224          58 :   if( lowmem ) {
     225           0 :     warning("LOWMEM flag is deprecated and is no longer required for this action");
     226             :   }
     227          58 :   std::string dline = getShortcutLabel() + ": DISTANCE";
     228             :   bool nopbc;
     229          58 :   parseFlag("NOPBC",nopbc);
     230          58 :   if( nopbc ) {
     231             :     dline += " NOPBC";
     232             :   }
     233          58 :   if( getName()=="DISTANCES" ) {
     234             :     bool comp;
     235          55 :     parseFlag("COMPONENTS",comp);
     236          55 :     if( comp ) {
     237             :       dline += " COMPONENTS";
     238             :     }
     239             :     bool scomp;
     240          55 :     parseFlag("SCALED_COMPONENTS",scomp);
     241          55 :     if( scomp ) {
     242             :       dline += " SCALED_COMPONENTS";
     243             :     }
     244             :   } else {
     245             :     dline += " COMPONENTS";
     246             :   }
     247             :   // Parse origin
     248             :   std::string num, ostr;
     249         116 :   parse("ORIGIN",ostr);
     250          58 :   if( ostr.length()>0 ) {
     251             :     // Parse atoms
     252             :     std::vector<std::string> afstr;
     253          23 :     MultiColvarShortcuts::parseAtomList("ATOMS",afstr,this);
     254       17118 :     for(unsigned i=0; i<afstr.size(); ++i) {
     255       17095 :       Tools::convert( i+1, num );
     256       34190 :       dline += " ATOMS" + num + "=" + ostr + "," + afstr[i];
     257             :     }
     258          23 :   } else {
     259             :     std::vector<std::string> grp;
     260          70 :     MultiColvarShortcuts::parseAtomList("GROUP",grp,this);
     261             :     std::vector<std::string> grpa;
     262          70 :     MultiColvarShortcuts::parseAtomList("GROUPA",grpa,this);
     263          35 :     if( grp.size()>0 ) {
     264           3 :       if( grpa.size()>0 ) {
     265           0 :         error("should not be using GROUPA in tandem with GROUP");
     266             :       }
     267             :       unsigned n=0;
     268         203 :       for(unsigned i=1; i<grp.size(); ++i) {
     269       10103 :         for(unsigned j=0; j<i; ++j) {
     270             :           std::string num;
     271        9903 :           Tools::convert( n+1, num );
     272             :           n++;
     273       19806 :           dline += " ATOMS" + num + "=" + grp[i] + "," + grp[j];
     274             :         }
     275             :       }
     276          32 :     } else if( grpa.size()>0 ) {
     277             :       std::vector<std::string> grpb;
     278           2 :       MultiColvarShortcuts::parseAtomList("GROUPB",grpb,this);
     279           1 :       if( grpb.size()==0 ) {
     280           0 :         error("found GROUPA but no corresponding GROUPB");
     281             :       }
     282             :       bool printcomment=false;
     283           4 :       for(unsigned i=0; i<grpa.size(); ++i) {
     284         294 :         for(unsigned j=0; j<grpb.size(); ++j) {
     285         291 :           Tools::convert( i*grpb.size() + j + 1, num );
     286         582 :           dline += " ATOMS" + num + "=" + grpa[i] + "," + grpb[j];
     287             :         }
     288             :       }
     289           1 :     } else {
     290          31 :       std::string grpstr = getShortcutLabel() + "_grp: GROUP ATOMS=";
     291          31 :       for(unsigned i=1;; ++i) {
     292             :         std::string atstring;
     293        1844 :         parseNumbered("ATOMS",i,atstring);
     294         922 :         if( atstring.length()==0 ) {
     295             :           break;
     296             :         }
     297             :         std::string locstr;
     298        1782 :         parseNumbered("LOCATION",i,locstr);
     299         891 :         if( locstr.length()==0 ) {
     300             :           std::string num;
     301         281 :           Tools::convert( i, num );
     302         562 :           readInputLine( getShortcutLabel() + "_vatom" + num + ": CENTER ATOMS=" + atstring );
     303         281 :           if( i==1 ) {
     304          54 :             grpstr += getShortcutLabel() + "_vatom" + num;
     305             :           } else {
     306         508 :             grpstr += "," + getShortcutLabel() + "_vatom" + num;
     307             :           }
     308             :         } else {
     309         610 :           if( i==1 ) {
     310             :             grpstr += locstr;
     311             :           } else {
     312        1212 :             grpstr += "," + locstr;
     313             :           }
     314             :         }
     315             :         std::string num;
     316         891 :         Tools::convert( i, num );
     317        1782 :         dline += " ATOMS" + num + "=" + atstring;
     318         891 :       }
     319          31 :       readInputLine( grpstr );
     320             :     }
     321          35 :   }
     322          58 :   readInputLine( dline );
     323             :   // Add shortcuts to label
     324          58 :   if( getName()=="DISTANCES" ) {
     325         110 :     MultiColvarShortcuts::expandFunctions( getShortcutLabel(), getShortcutLabel(), "", this );
     326           3 :   } else if( getName()=="XDISTANCES" ) {
     327           2 :     MultiColvarShortcuts::expandFunctions( getShortcutLabel(), getShortcutLabel() + ".x", "", this );
     328           2 :   } else if( getName()=="YDISTANCES" ) {
     329           2 :     MultiColvarShortcuts::expandFunctions( getShortcutLabel(), getShortcutLabel() + ".y", "", this );
     330           1 :   } else if( getName()=="ZDISTANCES" ) {
     331           2 :     MultiColvarShortcuts::expandFunctions( getShortcutLabel(), getShortcutLabel() + ".z", "", this );
     332             :   }
     333          58 : }
     334             : 
     335             : }
     336             : }

Generated by: LCOV version 1.16