LCOV - code coverage report
Current view: top level - generic - WholeMolecules.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 77 87 88.5 %
Date: 2024-10-18 13:59:31 Functions: 5 6 83.3 %

          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 "core/ActionAtomistic.h"
      23             : #include "core/ActionPilot.h"
      24             : #include "core/ActionRegister.h"
      25             : #include "tools/Vector.h"
      26             : #include "tools/AtomNumber.h"
      27             : #include "tools/Tools.h"
      28             : #include "core/PlumedMain.h"
      29             : #include "core/ActionSet.h"
      30             : #include "core/GenericMolInfo.h"
      31             : #include "tools/OpenMP.h"
      32             : #include "tools/Tree.h"
      33             : 
      34             : #include <vector>
      35             : #include <string>
      36             : 
      37             : namespace PLMD {
      38             : namespace generic {
      39             : 
      40             : //+PLUMEDOC GENERIC WHOLEMOLECULES
      41             : /*
      42             : This action is used to rebuild molecules that can become split by the periodic boundary conditions.
      43             : 
      44             : It is similar to the ALIGN_ATOMS keyword of plumed1, and is needed since some
      45             : MD dynamics code (e.g. GROMACS) can break molecules during the calculation.
      46             : 
      47             : Running some CVs without this command can cause there to be discontinuities changes
      48             : in the CV value and artifacts in the calculations.  This command can be applied
      49             : more than once.  To see what effect is has use a variable without pbc or use
      50             : the \ref DUMPATOMS directive to output the atomic positions.
      51             : 
      52             : \attention
      53             : This directive modifies the stored position at the precise moment
      54             : it is executed. This means that only collective variables
      55             : which are below it in the input script will see the corrected positions.
      56             : As a general rule, put it at the top of the input file. Also, unless you
      57             : know exactly what you are doing, leave the default stride (1), so that
      58             : this action is performed at every MD step.
      59             : 
      60             : The way WHOLEMOLECULES modifies each of the listed entities is this:
      61             : - First atom of the list is left in place
      62             : - Each atom of the list is shifted by a lattice vectors so that it becomes as close as possible
      63             :   to the previous one, iteratively.
      64             : 
      65             : In this way, if an entity consists of a list of atoms such that consecutive atoms in the
      66             : list are always closer than half a box side the entity will become whole.
      67             : This can be usually achieved selecting consecutive atoms (1-100), but it is also possible
      68             : to skip some atoms, provided consecutive chosen atoms are close enough.
      69             : 
      70             : \par Examples
      71             : 
      72             : This command instructs plumed to reconstruct the molecule containing atoms 1-20
      73             : at every step of the calculation and dump them on a file.
      74             : 
      75             : \plumedfile
      76             : # to see the effect, one could dump the atoms as they were before molecule reconstruction:
      77             : # DUMPATOMS FILE=dump-broken.xyz ATOMS=1-20
      78             : WHOLEMOLECULES ENTITY0=1-20
      79             : DUMPATOMS FILE=dump.xyz ATOMS=1-20
      80             : \endplumedfile
      81             : 
      82             : This command instructs plumed to reconstruct two molecules containing atoms 1-20 and 30-40
      83             : 
      84             : \plumedfile
      85             : WHOLEMOLECULES ENTITY0=1-20 ENTITY1=30-40
      86             : DUMPATOMS FILE=dump.xyz ATOMS=1-20,30-40
      87             : \endplumedfile
      88             : 
      89             : This command instructs plumed to reconstruct the chain of backbone atoms in a
      90             : protein
      91             : 
      92             : \plumedfile
      93             : #SETTINGS MOLFILE=regtest/basic/rt32/helix.pdb
      94             : MOLINFO STRUCTURE=helix.pdb
      95             : WHOLEMOLECULES RESIDUES=all MOLTYPE=protein
      96             : \endplumedfile
      97             : 
      98             : */
      99             : //+ENDPLUMEDOC
     100             : 
     101             : 
     102             : class WholeMolecules:
     103             :   public ActionPilot,
     104             :   public ActionAtomistic
     105             : {
     106             :   std::vector<std::vector<std::pair<std::size_t,std::size_t> > > p_groups;
     107             :   std::vector<std::vector<std::pair<std::size_t,std::size_t> > > p_roots;
     108             :   std::vector<Vector> refs;
     109             :   bool doemst, addref;
     110             : public:
     111             :   explicit WholeMolecules(const ActionOptions&ao);
     112             :   static void registerKeywords( Keywords& keys );
     113         841 :   bool actionHasForces() override { return false; }
     114             :   void calculate() override;
     115        9888 :   void apply() override {}
     116             : };
     117             : 
     118             : PLUMED_REGISTER_ACTION(WholeMolecules,"WHOLEMOLECULES")
     119             : 
     120          80 : void WholeMolecules::registerKeywords( Keywords& keys ) {
     121          80 :   Action::registerKeywords( keys );
     122          80 :   ActionPilot::registerKeywords( keys );
     123          80 :   ActionAtomistic::registerKeywords( keys );
     124         160 :   keys.add("compulsory","STRIDE","1","the frequency with which molecules are reassembled.  Unless you are completely certain about what you are doing leave this set equal to 1!");
     125         160 :   keys.add("numbered","ENTITY","the atoms that make up a molecule that you wish to align. To specify multiple molecules use a list of ENTITY keywords: ENTITY0, ENTITY1,...");
     126         160 :   keys.reset_style("ENTITY","atoms");
     127         160 :   keys.add("residues","RESIDUES","this command specifies that the backbone atoms in a set of residues all must be aligned. It must be used in tandem with the \\ref MOLINFO "
     128             :            "action and the MOLTYPE keyword. If you wish to use all the residues from all the chains in your system you can do so by "
     129             :            "specifying all. Alternatively, if you wish to use a subset of the residues you can specify the particular residues "
     130             :            "you are interested in as a list of numbers");
     131         160 :   keys.add("optional","MOLTYPE","the type of molecule that is under study.  This is used to define the backbone atoms");
     132         160 :   keys.addFlag("EMST", false, "Define atoms sequence in entities using an Euclidean minimum spanning tree");
     133         160 :   keys.addFlag("ADDREFERENCE", false, "Define the reference position of the first atom of each entity using a PDB file");
     134          80 : }
     135             : 
     136          58 : WholeMolecules::WholeMolecules(const ActionOptions&ao):
     137             :   Action(ao),
     138             :   ActionPilot(ao),
     139             :   ActionAtomistic(ao),
     140          58 :   doemst(false), addref(false)
     141             : {
     142             :   std::vector<std::vector<AtomNumber> > groups;
     143             :   std::vector<std::vector<AtomNumber> > roots;
     144             :   // parse optional flags
     145          58 :   parseFlag("EMST", doemst);
     146         116 :   parseFlag("ADDREFERENCE", addref);
     147             : 
     148             :   // create groups from ENTITY
     149         409 :   for(int i=0;; i++) {
     150             :     std::vector<AtomNumber> group;
     151         934 :     parseAtomList("ENTITY",i,group);
     152         467 :     if( group.empty() ) break;
     153         409 :     groups.push_back(group);
     154         409 :   }
     155             : 
     156             :   // Read residues to align from MOLINFO
     157         116 :   std::vector<std::string> resstrings; parseVector("RESIDUES",resstrings);
     158          58 :   if( resstrings.size()>0 ) {
     159           0 :     if( resstrings.size()==1 ) {
     160           0 :       if( resstrings[0]=="all" ) resstrings[0]="all-ter";   // Include terminal groups in alignment
     161             :     }
     162           0 :     std::string moltype; parse("MOLTYPE",moltype);
     163           0 :     if(moltype.length()==0) error("Found RESIDUES keyword without specification of the molecule - use MOLTYPE");
     164           0 :     auto* moldat=plumed.getActionSet().selectLatest<GenericMolInfo*>(this);
     165           0 :     if( !moldat ) error("MOLINFO is required to use RESIDUES");
     166             :     std::vector< std::vector<AtomNumber> > backatoms;
     167           0 :     moldat->getBackbone( resstrings, moltype, backatoms );
     168           0 :     for(unsigned i=0; i<backatoms.size(); ++i) {
     169           0 :       groups.push_back( backatoms[i] );
     170             :     }
     171           0 :   }
     172             : 
     173             :   // check number of groups
     174          58 :   if(groups.size()==0) error("no atoms found for WHOLEMOLECULES!");
     175             : 
     176             :   // if using PDBs reorder atoms in groups based on proximity in PDB file
     177          58 :   if(doemst) {
     178           2 :     auto* moldat=plumed.getActionSet().selectLatest<GenericMolInfo*>(this);
     179           2 :     if( !moldat ) error("MOLINFO is required to use EMST");
     180             :     // initialize tree
     181           2 :     Tree tree = Tree(moldat);
     182             :     // cycle on groups and reorder atoms
     183           4 :     for(unsigned i=0; i<groups.size(); ++i) {
     184           4 :       groups[i] = tree.getTree(groups[i]);
     185             :       // store root atoms
     186           4 :       roots.push_back(tree.getRoot());
     187             :     }
     188             :   } else {
     189             :     // fill root vector with previous atom in groups
     190         463 :     for(unsigned i=0; i<groups.size(); ++i) {
     191             :       std::vector<AtomNumber> root;
     192        9863 :       for(unsigned j=0; j<groups[i].size()-1; ++j) root.push_back(groups[i][j]);
     193             :       // store root atoms
     194         407 :       roots.push_back(root);
     195             :     }
     196             :   }
     197             : 
     198             :   // adding reference if needed
     199          58 :   if(addref) {
     200           2 :     auto* moldat=plumed.getActionSet().selectLatest<GenericMolInfo*>(this);
     201           2 :     if( !moldat ) error("MOLINFO is required to use ADDREFERENCE");
     202           4 :     for(unsigned i=0; i<groups.size(); ++i) {
     203             :       // add reference position of first atom in entity
     204           4 :       refs.push_back(moldat->getPosition(groups[i][0]));
     205             :     }
     206             :   }
     207             : 
     208             :   // print out info
     209         467 :   for(unsigned i=0; i<groups.size(); ++i) {
     210         409 :     log.printf("  atoms in entity %d : ",i);
     211       10517 :     for(unsigned j=0; j<groups[i].size(); ++j) log.printf("%d ",groups[i][j].serial() );
     212         409 :     log.printf("\n");
     213         409 :     if(addref) log.printf("     with reference position : %lf %lf %lf\n",refs[i][0],refs[i][1],refs[i][2]);
     214             :   }
     215             : 
     216             :   // collect all atoms
     217             :   std::vector<AtomNumber> merge;
     218         467 :   for(unsigned i=0; i<groups.size(); ++i) {
     219         409 :     merge.insert(merge.end(),groups[i].begin(),groups[i].end());
     220             :   }
     221             : 
     222             :   // Convert groups to p_groups
     223          58 :   p_groups.resize( groups.size() );
     224         467 :   for(unsigned i=0; i<groups.size(); ++i) {
     225         409 :     p_groups[i].resize( groups[i].size() );
     226       10517 :     for(unsigned j=0; j<groups[i].size(); ++j) p_groups[i][j] = getValueIndices( groups[i][j] );
     227             :   }
     228             :   // Convert roots to p_roots
     229          58 :   p_roots.resize( roots.size() );
     230         467 :   for(unsigned i=0; i<roots.size(); ++i) {
     231         409 :     p_roots[i].resize( roots[i].size() );
     232       10108 :     for(unsigned j=0; j<roots[i].size(); ++j) p_roots[i][j] = getValueIndices( roots[i][j] );
     233             :   }
     234             : 
     235             : 
     236          58 :   checkRead();
     237          58 :   Tools::removeDuplicates(merge);
     238          58 :   requestAtoms(merge);
     239             :   doNotRetrieve();
     240             :   doNotForce();
     241          58 : }
     242             : 
     243        9888 : void WholeMolecules::calculate() {
     244       20480 :   for(unsigned i=0; i<p_groups.size(); ++i) {
     245       10592 :     Vector first = getGlobalPosition(p_groups[i][0]);
     246       10592 :     if(addref) {
     247          12 :       first = refs[i]+pbcDistance(refs[i],first);
     248          12 :       setGlobalPosition( p_groups[i][0], first );
     249             :     }
     250       10592 :     if(!doemst) {
     251      391950 :       for(unsigned j=1; j<p_groups[i].size(); ++j) {
     252      381362 :         Vector second=getGlobalPosition(p_groups[i][j]);
     253      381362 :         first = first+pbcDistance(first,second);
     254      381362 :         setGlobalPosition(p_groups[i][j], first );
     255             :       }
     256             :     } else {
     257         490 :       for(unsigned j=1; j<p_groups[i].size(); ++j) {
     258         486 :         Vector first=getGlobalPosition(p_roots[i][j-1]);
     259         486 :         Vector second=getGlobalPosition(p_groups[i][j]);
     260         486 :         second=first+pbcDistance(first,second);
     261         486 :         setGlobalPosition(p_groups[i][j], second );
     262             :       }
     263             :     }
     264             :   }
     265        9888 : }
     266             : 
     267             : 
     268             : }
     269             : }

Generated by: LCOV version 1.16