LCOV - code coverage report
Current view: top level - core - Group.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 64 67 95.5 %
Date: 2025-03-25 09:33:27 Functions: 3 4 75.0 %

          Line data    Source code
       1             : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
       2             :    Copyright (c) 2011-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 "Group.h"
      23             : #include "ActionRegister.h"
      24             : #include "PlumedMain.h"
      25             : #include "Value.h"
      26             : #include "ActionWithValue.h"
      27             : #include "ActionWithVirtualAtom.h"
      28             : #include "tools/IFile.h"
      29             : #include "tools/Tools.h"
      30             : #include <string>
      31             : #include <vector>
      32             : #include <algorithm>
      33             : 
      34             : namespace PLMD {
      35             : 
      36             : //+PLUMEDOC GENERIC GROUP
      37             : /*
      38             : Define a group of atoms so that a particular list of atoms can be referenced with a single label in definitions of CVs or virtual atoms.
      39             : 
      40             : The GROUP command can be used to define a list of atoms so that the group can be referenced in the definitions of other
      41             : CVs or virtual atoms as shown below:
      42             : 
      43             : ```plumed
      44             : o: GROUP ATOMS=1,4,7,11,14
      45             : h: GROUP ATOMS=2,3,5,6,8,9,12,13
      46             : # compute the coordination among the two groups
      47             : c: COORDINATION GROUPA=o GROUPB=h R_0=0.3
      48             : # same could have been obtained without GROUP, just writing:
      49             : # c: COORDINATION GROUPA=1,4,7,11,14 GROUPB=2,3,5,6,8,9,12,13
      50             : 
      51             : # print the coordination on file 'colvar'
      52             : PRINT ARG=c FILE=colvar
      53             : ```
      54             : 
      55             : The first group command here creates a group of atoms called `o` containing atoms 1, 4, 7, 11 and 14.
      56             : The second group command here creates a group of toms called `h` containing atoms 2, 3, 5, 6, 8, 9, 12, and 13.
      57             : 
      58             : As discussed on [this page](specifying_atoms.md) atoms in groups can be listed as comma separated numbers (i.e. `1,2,3,10,45,7,9`),
      59             : simple positive ranges (i.e. `20-40`), ranges with a stride either positive or negative (i.e. `20-40:2` or `80-50:-2`) or as
      60             : comma separated combinations of all the former methods (`1,2,4,5,10-20,21-40:2,80-50:-2`).
      61             : 
      62             : Oftentime people will store the definitions in a separate file as has been done in the following example input.
      63             : 
      64             : ```plumed
      65             : INCLUDE FILE=extras/groups.dat
      66             : # compute the coordination among the two groups
      67             : c: COORDINATION GROUPA=groupa GROUPB=groupb R_0=0.3
      68             : # print the coordination on file 'colvar'
      69             : PRINT ARG=c FILE=colvar
      70             : ```
      71             : 
      72             : Storing the groups in the extra file is particularly useful if the groups include list of thousand atoms. Putting these definitions
      73             : in a separate file ensures that the main plumed.dat file is not cluttered.  You can even use a [GROMACS index file](https://manual.gromacs.org/archive/5.0.4/online/ndx.html)
      74             : to hold the groups as illustrated in the input below:
      75             : 
      76             : ```plumed
      77             : # import group named 'Protein' from file index.ndx
      78             : pro: GROUP NDX_FILE=extras/index.ndx NDX_GROUP=Protein
      79             : # dump all the atoms of the protein on a trajectory file
      80             : DUMPATOMS ATOMS=pro FILE=traj.gro
      81             : ```
      82             : 
      83             : Notice that you use the keyword `NDX_FILE` to set the name of the index file and `NDX_GROUP` to set the name of the group to be imported (default is first one).
      84             : Further notice that starting from version 2.10 it is possible to directly use an `@ndx:` selector in the input to an action as shwn below:
      85             : 
      86             : ```plumed
      87             : DUMPATOMS ATOMS={@ndx:{extras/index.ndx Protein}} FILE=traj.gro
      88             : ```
      89             : 
      90             : Lastly notice that it is also possible to remove atoms from the list of atoms specified in a GROUP by using the keywords `REMOVE`, `SORT`, and `UNIQUE` as shown below:
      91             : 
      92             : ```plumed
      93             : # take one atom every three, that is oxygens
      94             : ox: GROUP ATOMS=1-90:3
      95             : # take the remaining atoms, that is hydrogens
      96             : hy: GROUP ATOMS=1-90 REMOVE=ox
      97             : DUMPATOMS ATOMS=ox FILE=ox.gro
      98             : DUMPATOMS ATOMS=hy FILE=hy.gro
      99             : ```
     100             : 
     101             : When you used the GROUP command the flow as follows:
     102             : 
     103             : - If `ATOMS` is present, then take the ordered list of atoms from the `ATOMS` keyword as a starting list.
     104             : - Alternatively, if `NDX_FILE` is present, use the list obtained from the gromacs group.
     105             : - If `REMOVE` is present, then remove the first occurrence of each of these atoms from the list.
     106             :   If one tries to remove an atom that was not listed plumed adds a notice in the output.
     107             :   An atom that is present twice in the original list might be removed twice.
     108             : - If `SORT` is present, then the resulting list is sorted by increasing serial number.
     109             : - If `UNIQUE` is present, then the resulting list is sorted by increasing serial number _and_ duplicate elements are removed.
     110             : 
     111             : Notice that this command just creates a shortcut, and does not imply any real calculation.
     112             : So, having a huge group defined does not slow down your calculation in any way.
     113             : It is just convenient to better organize input files.
     114             : 
     115             : */
     116             : //+ENDPLUMEDOC
     117             : 
     118             : PLUMED_REGISTER_ACTION(Group,"GROUP")
     119             : 
     120         334 : Group::Group(const ActionOptions&ao):
     121             :   Action(ao),
     122         334 :   ActionAtomistic(ao) {
     123         668 :   parseAtomList("ATOMS",atoms);
     124             :   std::string ndxfile,ndxgroup;
     125         334 :   parse("NDX_FILE",ndxfile);
     126         668 :   parse("NDX_GROUP",ndxgroup);
     127         334 :   if(ndxfile.length()>0 && atoms.size()>0) {
     128           0 :     error("either use explicit atom list or import from index file");
     129             :   }
     130         334 :   if(ndxfile.length()==0 && ndxgroup.size()>0) {
     131           0 :     error("NDX_GROUP can be only used is NDX_FILE is also used");
     132             :   }
     133             : 
     134         334 :   if(ndxfile.length()>0) {
     135             : 
     136             :     std::vector<AtomNumber> add;
     137             :     std::vector<std::string> words;
     138          46 :     words.emplace_back("@ndx: " + ndxfile + " " + ndxgroup);
     139          23 :     interpretAtomList(words,add);
     140          23 :     atoms.insert(atoms.end(),add.begin(),add.end());
     141          23 :   }
     142             : 
     143             :   std::vector<AtomNumber> remove;
     144         668 :   parseAtomList("REMOVE",remove);
     145         334 :   if(remove.size()>0) {
     146             :     std::vector<AtomNumber> notfound;
     147             :     unsigned k=0;
     148           2 :     log<<"  removing these atoms from the list:";
     149         114 :     for(unsigned i=0; i<remove.size(); i++) {
     150         112 :       const auto it = find(atoms.begin(),atoms.end(),remove[i]);
     151         112 :       if(it!=atoms.end()) {
     152         111 :         if(k%25==0) {
     153           6 :           log<<"\n";
     154             :         }
     155         111 :         log<<" "<<(*it).serial();
     156         111 :         k++;
     157             :         atoms.erase(it);
     158             :       } else {
     159           1 :         notfound.push_back(remove[i]);
     160             :       }
     161             :     }
     162           2 :     log<<"\n";
     163           2 :     if(notfound.size()>0) {
     164           1 :       log<<"  the following atoms were not found:";
     165           2 :       for(unsigned i=0; i<notfound.size(); i++) {
     166           1 :         log<<" "<<notfound[i].serial();
     167             :       }
     168           1 :       log<<"\n";
     169             :     }
     170             :   }
     171             : 
     172         334 :   bool sortme=false;
     173         334 :   parseFlag("SORT",sortme);
     174         334 :   if(sortme) {
     175           1 :     log<<"  atoms are sorted\n";
     176           1 :     sort(atoms.begin(),atoms.end());
     177             :   }
     178         334 :   bool unique=false;
     179         334 :   parseFlag("UNIQUE",unique);
     180         334 :   if(unique) {
     181           1 :     log<<"  sorting atoms and removing duplicates\n";
     182           1 :     Tools::removeDuplicates(atoms);
     183             :   }
     184             : 
     185         334 :   log.printf("  list of atoms:");
     186      312611 :   for(unsigned i=0; i<atoms.size(); i++) {
     187      312277 :     if(i%25==0) {
     188       12672 :       log<<"\n";
     189             :     }
     190      312277 :     log<<" "<<atoms[i].serial();
     191             :   }
     192         334 :   log.printf("\n");
     193         334 : }
     194             : 
     195         503 : void Group::registerKeywords( Keywords& keys ) {
     196         503 :   Action::registerKeywords( keys );
     197         503 :   ActionAtomistic::registerKeywords( keys );
     198         503 :   keys.add("atoms", "ATOMS", "the numerical indexes for the set of atoms in the group");
     199         503 :   keys.add("atoms", "REMOVE","remove these atoms from the list");
     200         503 :   keys.addFlag("SORT",false,"sort the resulting list");
     201         503 :   keys.addFlag("UNIQUE",false,"sort atoms and remove duplicated ones");
     202         503 :   keys.add("optional", "NDX_FILE", "the name of index file (gromacs syntax)");
     203         503 :   keys.add("optional", "NDX_GROUP", "the name of the group to be imported (gromacs syntax) - first group found is used by default");
     204         503 : }
     205             : 
     206         577 : std::vector<std::string> Group::getGroupAtoms() const {
     207         577 :   std::vector<std::string> atoms_str(atoms.size());
     208      387313 :   for(unsigned i=0; i<atoms.size(); ++i) {
     209      386736 :     std::pair<std::size_t,std::size_t> a = getValueIndices( atoms[i] );
     210      386736 :     if( xpos[a.first]->getNumberOfValues()==1 ) {
     211       10053 :       atoms_str[i] = (xpos[a.first]->getPntrToAction())->getLabel();
     212             :     } else {
     213      376683 :       Tools::convert( atoms[i].serial(), atoms_str[i] );
     214             :     }
     215             :   }
     216         577 :   return atoms_str;
     217           0 : }
     218             : 
     219             : }
     220             : 

Generated by: LCOV version 1.16