LCOV - code coverage report
Current view: top level - generic - WholeMolecules.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 63 78 80.8 %
Date: 2026-03-30 13:16:06 Functions: 7 8 87.5 %

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

Generated by: LCOV version 1.16