Line data Source code
1 : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 : Copyright (c) 2011-2019 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 :
23 : #include "core/ActionRegister.h"
24 : #include "core/ActionAtomistic.h"
25 : #include "core/Atoms.h"
26 : #include "tools/IFile.h"
27 : #include "tools/Tools.h"
28 : #include <string>
29 : #include <vector>
30 : #include <algorithm>
31 :
32 : using namespace std;
33 :
34 : namespace PLMD {
35 : namespace generic {
36 :
37 : //+PLUMEDOC GENERIC GROUP
38 : /*
39 : 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.
40 :
41 : Atoms can be listed as comma separated numbers (i.e. `1,2,3,10,45,7,9`) , simple positive ranges
42 : (i.e. `20-40`), ranges with a stride either positive or negative (i.e. `20-40:2` or `80-50:-2`) or as
43 : comma separated combinations of all the former methods (`1,2,4,5,10-20,21-40:2,80-50:-2`).
44 :
45 : Moreover, lists can be imported from ndx files (GROMACS format). Use `NDX_FILE` to set the name of
46 : the index file and `NDX_GROUP` to set the name of the group to be imported (default is first one).
47 :
48 : It is also possible to remove atoms from a list and or sort them using keywords `REMOVE`, `SORT`, and `UNIQUE`.
49 : The flow is the following:
50 : - If `ATOMS` is present, then take the ordered list of atoms from the `ATOMS` keyword as a starting list.
51 : - If `NDX_FILE` is present, then append to it the list obtained from the gromacs group.
52 : - If `REMOVE` is present, then remove the first occurrence of each of these atoms from the list.
53 : If one tries to remove an atom that was not listed plumed adds a notice in the output.
54 : An atom that is present twice in the original list might be removed twice.
55 : - If `SORT` is present, then the resulting list is sorted by increasing serial number.
56 : - If `UNIQUE` is present, then the resulting list is sorted by increasing serial number _and_ duplicate elements are removed.
57 :
58 : Notice that this command just creates a shortcut, and does not imply any real calculation.
59 : So, having a huge group defined does not slow down your calculation in any way.
60 : It is just convenient to better organize input files. Might be used in combination with
61 : the \ref INCLUDE command so as to store long group definitions in a separate file.
62 :
63 :
64 : \par Examples
65 :
66 : This command create a group of atoms containing atoms 1, 4, 7, 11 and 14 (labeled 'o'), and another containing
67 : atoms 2, 3, 5, 6, 8, 9, 12, and 13 (labeled 'h'):
68 : \plumedfile
69 : o: GROUP ATOMS=1,4,7,11,14
70 : h: GROUP ATOMS=2,3,5,6,8,9,12,13
71 : # compute the coordination among the two groups
72 : c: COORDINATION GROUPA=o GROUPB=h R_0=0.3
73 : # same could have been obtained without GROUP, just writing:
74 : # c: COORDINATION GROUPA=1,4,7,11,14 GROUPB=2,3,5,6,8,9,12,13
75 :
76 : # print the coordination on file 'colvar'
77 : PRINT ARG=c FILE=colvar
78 : \endplumedfile
79 :
80 : Groups can be conveniently stored in a separate file.
81 : E.g. one could create a file named `groups.dat` which reads
82 : \plumedfile
83 : #SETTINGS FILENAME=groups.dat
84 : # this is groups.dat
85 : o: GROUP ATOMS=1,4,7,11,14
86 : h: GROUP ATOMS=2,3,5,6,8,9,12,13
87 : \endplumedfile
88 : and then include it in the main 'plumed.dat' file
89 : \plumedfile
90 : INCLUDE FILE=groups.dat
91 : # compute the coordination among the two groups
92 : c: COORDINATION GROUPA=o GROUPB=h R_0=0.3
93 : # print the coordination on file 'colvar'
94 : PRINT ARG=c FILE=colvar
95 : \endplumedfile
96 : The `groups.dat` file could be very long and include lists of thousand atoms without cluttering the main plumed.dat file.
97 :
98 : A GROMACS index file such as the one shown below:
99 :
100 : \auxfile{index.ndx}
101 : [ Protein ]
102 : 1 3 5 7 9
103 : 2 4 6 8 10
104 : [ Group2 ]
105 : 30 31 32 33 34 35 36 37 38 39 40
106 : 5
107 : \endauxfile
108 :
109 : can also be imported by using the GROUP keyword as shown below
110 : \plumedfile
111 : # import group named 'Protein' from file index.ndx
112 : pro: GROUP NDX_FILE=index.ndx NDX_GROUP=Protein
113 : # dump all the atoms of the protein on a trajectory file
114 : DUMPATOMS ATOMS=pro FILE=traj.gro
115 : \endplumedfile
116 :
117 : A list can be edited with `REMOVE`. For instance, if you
118 : are using a water model with three atoms per molecule, you can
119 : easily construct the list of hydrogen atoms in this manner
120 : \plumedfile
121 : # take one atom every three, that is oxygens
122 : ox: GROUP ATOMS=1-90:3
123 : # take the remaining atoms, that is hydrogens
124 : hy: GROUP ATOMS=1-90 REMOVE=ox
125 : DUMPATOMS ATOMS=ox FILE=ox.gro
126 : DUMPATOMS ATOMS=hy FILE=hy.gro
127 : \endplumedfile
128 :
129 :
130 : */
131 : //+ENDPLUMEDOC
132 :
133 : class Group:
134 : public ActionAtomistic
135 : {
136 :
137 : public:
138 : explicit Group(const ActionOptions&ao);
139 : ~Group();
140 : static void registerKeywords( Keywords& keys );
141 0 : void calculate() override {}
142 0 : void apply() override {}
143 : };
144 :
145 8040 : PLUMED_REGISTER_ACTION(Group,"GROUP")
146 :
147 104 : Group::Group(const ActionOptions&ao):
148 : Action(ao),
149 104 : ActionAtomistic(ao)
150 : {
151 : vector<AtomNumber> atoms;
152 208 : parseAtomList("ATOMS",atoms);
153 : std::string ndxfile,ndxgroup;
154 208 : parse("NDX_FILE",ndxfile);
155 208 : parse("NDX_GROUP",ndxgroup);
156 118 : if(ndxfile.length()>0 && atoms.size()>0) error("either use explicit atom list or import from index file");
157 194 : if(ndxfile.length()==0 && ndxgroup.size()>0) error("NDX_GROUP can be only used is NDX_FILE is also used");
158 :
159 104 : if(ndxfile.length()>0) {
160 53 : if(ndxgroup.size()>0) log<<" importing group '"+ndxgroup+"'";
161 1 : else log<<" importing first group";
162 14 : log<<" from index file "<<ndxfile<<"\n";
163 :
164 14 : IFile ifile;
165 14 : ifile.open(ndxfile);
166 : std::string line;
167 : std::string groupname;
168 : bool firstgroup=true;
169 : bool groupfound=false;
170 3697 : while(ifile.getline(line)) {
171 3669 : std::vector<std::string> words=Tools::getWords(line);
172 7533 : if(words.size()>=3 && words[0]=="[" && words[2]=="]") {
173 124 : if(groupname.length()>0) firstgroup=false;
174 124 : groupname=words[1];
175 235 : if(groupname==ndxgroup || ndxgroup.length()==0) groupfound=true;
176 4320 : } else if(groupname==ndxgroup || (firstgroup && ndxgroup.length()==0)) {
177 11628 : for(unsigned i=0; i<words.size(); i++) {
178 5620 : AtomNumber at; Tools::convert(words[i],at);
179 5620 : atoms.push_back(at);
180 : }
181 : }
182 3669 : }
183 28 : if(!groupfound) error("group has not been found in index file");
184 : }
185 :
186 : std::vector<AtomNumber> remove;
187 208 : parseAtomList("REMOVE",remove);
188 104 : if(remove.size()>0) {
189 : std::vector<AtomNumber> notfound;
190 : unsigned k=0;
191 2 : log<<" removing these atoms from the list:";
192 226 : for(unsigned i=0; i<remove.size(); i++) {
193 : const auto it = find(atoms.begin(),atoms.end(),remove[i]);
194 112 : if(it!=atoms.end()) {
195 111 : if(k%25==0) log<<"\n";
196 111 : log<<" "<<(*it).serial();
197 111 : k++;
198 111 : atoms.erase(it);
199 1 : } else notfound.push_back(remove[i]);
200 : }
201 2 : log<<"\n";
202 2 : if(notfound.size()>0) {
203 1 : log<<" the following atoms were not found:";
204 3 : for(unsigned i=0; i<notfound.size(); i++) log<<" "<<notfound[i].serial();
205 1 : log<<"\n";
206 : }
207 : }
208 :
209 104 : bool sortme=false;
210 208 : parseFlag("SORT",sortme);
211 104 : if(sortme) {
212 1 : log<<" atoms are sorted\n";
213 1 : sort(atoms.begin(),atoms.end());
214 : }
215 104 : bool unique=false;
216 208 : parseFlag("UNIQUE",unique);
217 104 : if(unique) {
218 1 : log<<" sorting atoms and removing duplicates\n";
219 1 : Tools::removeDuplicates(atoms);
220 : }
221 :
222 104 : this->atoms.insertGroup(getLabel(),atoms);
223 104 : log.printf(" list of atoms:");
224 42430 : for(unsigned i=0; i<atoms.size(); i++) {
225 21163 : if(i%25==0) log<<"\n";
226 21163 : log<<" "<<atoms[i].serial();
227 : }
228 104 : log.printf("\n");
229 104 : }
230 :
231 105 : void Group::registerKeywords( Keywords& keys ) {
232 105 : Action::registerKeywords( keys );
233 105 : ActionAtomistic::registerKeywords( keys );
234 420 : keys.add("atoms", "ATOMS", "the numerical indexes for the set of atoms in the group");
235 420 : keys.add("atoms", "REMOVE","remove these atoms from the list");
236 315 : keys.addFlag("SORT",false,"sort the resulting list");
237 315 : keys.addFlag("UNIQUE",false,"sort atoms and remove duplicated ones");
238 420 : keys.add("optional", "NDX_FILE", "the name of index file (gromacs syntax)");
239 420 : keys.add("optional", "NDX_GROUP", "the name of the group to be imported (gromacs syntax) - first group found is used by default");
240 105 : }
241 :
242 312 : Group::~Group() {
243 104 : atoms.removeGroup(getLabel());
244 208 : }
245 :
246 : }
247 5874 : }
248 :
|