From e059ffa16575f5effb52d3bc403151f8d903e6f5 Mon Sep 17 00:00:00 2001 From: Giovanni Bussi <giovanni.bussi@gmail.com> Date: Tue, 15 Oct 2013 15:19:55 +0200 Subject: [PATCH] Added possibility to import ndx files Closes #14 --- CHANGES/v2.1.txt | 1 + regtest/basic/rt2/plumed.dat | 20 ++++++++++++-- regtest/basic/rt2/test.ndx | 6 +++++ src/generic/Group.cpp | 51 ++++++++++++++++++++++++++++++++++++ 4 files changed, 76 insertions(+), 2 deletions(-) create mode 100644 regtest/basic/rt2/test.ndx diff --git a/CHANGES/v2.1.txt b/CHANGES/v2.1.txt index 4820665e2..c1ae623c0 100644 --- a/CHANGES/v2.1.txt +++ b/CHANGES/v2.1.txt @@ -6,6 +6,7 @@ Includes all fixes in branch 2.0 (see \ref CHANGES-2-0) up to 2.0.1 Changes from version 2.0 which are relevant for users: - Added option PRECISION to set number of digits in DUMPATOMS +- Added NDX_FILE and NDX_GROUP to action GROUP, allowing to import atom lists from ndx files - Several optimizations in the following actions: WHOLEMOLECULES, ... - Faster atom scattering with domain decomposition. diff --git a/regtest/basic/rt2/plumed.dat b/regtest/basic/rt2/plumed.dat index 1ac8927aa..d504175d4 100644 --- a/regtest/basic/rt2/plumed.dat +++ b/regtest/basic/rt2/plumed.dat @@ -1,12 +1,21 @@ g1: GROUP ATOMS=1-9:2,2-10:2 g2: GROUP ATOMS=30-40,5 -c1: COM ATOMS=g1 -c2: COM ATOMS=g2 +ndx1: GROUP NDX_FILE=test.ndx +ndx2: GROUP NDX_FILE=test.ndx NDX_GROUP=second + +c1: COM ATOMS=g1 +c2: COM ATOMS=g2 +c1n: COM ATOMS=ndx1 +c2n: COM ATOMS=ndx2 d1: DISTANCE ATOMS=1,10 d2: DISTANCE ATOMS=c1,50 d3: DISTANCE ATOMS=50,c2 +d1n: DISTANCE ATOMS=1,10 +d2n: DISTANCE ATOMS=c1n,50 +d3n: DISTANCE ATOMS=50,c2n + RESTRAINT ARG=d2 AT=0.9 KAPPA=3.0 PRINT ... @@ -15,5 +24,12 @@ PRINT ... FILE=COLVAR FMT=%6.3f ... PRINT +PRINT ... + STRIDE=1 + ARG=d1n,d2n,d3n + FILE=COLVARN FMT=%6.3f +... PRINT + + ENDPLUMED diff --git a/regtest/basic/rt2/test.ndx b/regtest/basic/rt2/test.ndx new file mode 100644 index 000000000..510d0f972 --- /dev/null +++ b/regtest/basic/rt2/test.ndx @@ -0,0 +1,6 @@ +[ first ] +1 3 5 7 9 +2 4 6 8 10 +[ second ] +30 31 32 33 34 35 36 37 38 39 40 +5 diff --git a/src/generic/Group.cpp b/src/generic/Group.cpp index e82824344..e99b23ac6 100644 --- a/src/generic/Group.cpp +++ b/src/generic/Group.cpp @@ -23,6 +23,10 @@ #include "core/ActionRegister.h" #include "core/ActionAtomistic.h" #include "core/Atoms.h" +#include "tools/IFile.h" +#include "tools/Tools.h" +#include <string> +#include <vector> using namespace std; @@ -38,6 +42,9 @@ Notice that this command just creates a shortcut, and does not imply any real ca It is just convenient to better organize input files. Might be used in combination with the \ref INCLUDE command so as to store long group definitions in a separate files. +Finally, lists can be imported from ndx files (GROMACS format). Use 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). + \par Examples This command create a group of atoms containing atoms 1,4,7,11 and 14 (labeled 'o'), and another containing @@ -61,6 +68,15 @@ c: COORDINATION GROUPA=o GROUPB=h R_0=0.3 (see also \ref INCLUDE and \ref COORDINATION). The groups.dat file could be very long and include lists of thousand atoms without cluttering the main plumed.dat file. +A GROMACS index file can also be imported +\verbatim +# import group named 'protein' from file index.ndx +pro: GROUP NDX_FILE=index.ndx NDX_GROUP=protein +# dump all the atoms of the protein on a trajectory file +DUMPATOMS ATOMS=pro FILE=traj.gro +\endverbatim +(see also \ref DUMPATOMS) + */ //+ENDPLUMEDOC @@ -84,6 +100,39 @@ Group::Group(const ActionOptions&ao): { vector<AtomNumber> atoms; parseAtomList("ATOMS",atoms); + std::string ndxfile,ndxgroup; + parse("NDX_FILE",ndxfile); + parse("NDX_GROUP",ndxgroup); + if(ndxfile.length()>0 && atoms.size()>0) error("either use explicit atom list or import from index file"); + if(ndxfile.length()==0 && ndxgroup.size()>0) error("NDX_GROUP can be only used is NDX_FILE is also used"); + + if(ndxfile.length()>0){ + if(ndxgroup.size()>0) log<<" importing group '"+ndxgroup+"'"; + else log<<" importing first group"; + log<<" from index file "<<ndxfile<<"\n"; + + IFile ifile; + ifile.open(ndxfile); + std::string line; + std::string groupname; + bool firstgroup=true; + bool groupfound=false; + while(ifile.getline(line)){ + std::vector<std::string> words=Tools::getWords(line); + if(words.size()>=3 && words[0]=="[" && words[2]=="]"){ + if(groupname.length()>0) firstgroup=false; + groupname=words[1]; + if(groupname==ndxgroup || ndxgroup.length()==0) groupfound=true; + } else if(groupname==ndxgroup || (firstgroup && ndxgroup.length()==0)){ + for(unsigned i=0;i<words.size();i++){ + AtomNumber at; Tools::convert(words[i],at); + atoms.push_back(at); + } + } + } + if(!groupfound) error("group has not been found in index file"); + } + this->atoms.insertGroup(getLabel(),atoms); log.printf(" of atoms "); for(unsigned i=0;i<atoms.size();i++) log.printf(" %d",atoms[i].serial()); @@ -94,6 +143,8 @@ void Group::registerKeywords( Keywords& keys ){ Action::registerKeywords( keys ); ActionAtomistic::registerKeywords( keys ); keys.add("atoms", "ATOMS", "the numerical indexes for the set of atoms in the group"); + keys.add("optional", "NDX_FILE", "the name of index file (gromacs syntax)"); + keys.add("optional", "NDX_GROUP", "the name of the group to be imported (gromacs syntax) - first group found is used by default"); } Group::~Group(){ -- GitLab