LCOV - code coverage report
Current view: top level - generic - MassChargeInput.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 48 53 90.6 %
Date: 2025-04-08 21:11:17 Functions: 2 3 66.7 %

          Line data    Source code
       1             : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
       2             :    Copyright (c) 2012-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/ActionShortcut.h"
      23             : #include "core/ActionRegister.h"
      24             : #include "core/ActionToPutData.h"
      25             : #include "core/ActionAnyorder.h"
      26             : #include "core/ActionSetup.h"
      27             : #include "core/PlumedMain.h"
      28             : #include "core/ActionSet.h"
      29             : #include "tools/IFile.h"
      30             : #include "tools/PDB.h"
      31             : 
      32             : namespace PLMD {
      33             : namespace generic {
      34             : 
      35             : //+PLUMEDOC COLVAR READMASSCHARGE
      36             : /*
      37             : Set the masses and charges from an input PDB file.
      38             : 
      39             : You can use this command to read in the masses and charges for the atoms as shown in the example input below:
      40             : 
      41             : ```plumed
      42             : #SETTINGS INPUTFILES=regtest/basic/rt-readmasscharge2/mcinpt
      43             : mq: READMASSCHARGE FILE=regtest/basic/rt-readmasscharge2/mcinpt
      44             : ```
      45             : 
      46             : In the above example the masses and charges are in a file called `mcinpt` that contains three columns of numbers.
      47             : The first column of numbers is a numerical index, the second column is the masses and the third is the charges.
      48             : 
      49             : You can read masses and charges from a PDB file instead by using an input like the one shown below:
      50             : 
      51             : ```plumed
      52             : #SETTINGS NATOMS=108 INPUTFILES=regtest/basic/rt-readmasscharge/test.pdb
      53             : mq: READMASSCHARGE PDBFILE=regtest/basic/rt-readmasscharge/test.pdb
      54             : ```
      55             : 
      56             : In this chages masses are read from the occupancy column and charges are read from the beta column.
      57             : 
      58             : __Using this command to read in masses and charges is useful when you are postprocessing a trajectory using driver.
      59             : If you are using PLUMED as a plugin to a molecular dynamics code the masses and charges will often be passed from the
      60             : MD code to PLUMED.__
      61             : 
      62             : 
      63             : */
      64             : //+ENDPLUMEDOC
      65             : 
      66             : class MassChargeInput : public ActionShortcut {
      67             : public:
      68             :   static void registerKeywords(Keywords& keys);
      69             :   explicit MassChargeInput(const ActionOptions&);
      70             : };
      71             : 
      72             : PLUMED_REGISTER_ACTION(MassChargeInput,"READMASSCHARGE")
      73             : 
      74           4 : void MassChargeInput::registerKeywords(Keywords& keys) {
      75           4 :   ActionShortcut::registerKeywords( keys );
      76           4 :   keys.add("optional","FILE","input file that contains the masses and charges that should be used");
      77           4 :   keys.add("compulsory","PDBFILE","a pdb file that contains the masses and charges of the atoms in the beta and occupancy columns");
      78           8 :   keys.addOutputComponent("mass","default","vector","the masses of the atoms in the system");
      79           8 :   keys.addOutputComponent("charges","default","vector","the masses of the atoms in the system");
      80           4 :   keys.needsAction("CONSTANT");
      81           4 : }
      82             : 
      83           2 : MassChargeInput::MassChargeInput(const ActionOptions& ao):
      84             :   Action(ao),
      85           2 :   ActionShortcut(ao) {
      86           2 :   const ActionSet& actionset(plumed.getActionSet());
      87          18 :   for(const auto & p : actionset) {
      88             :     // check that all the preceding actions are ActionSetup
      89          16 :     if( !dynamic_cast<ActionSetup*>(p.get()) && !dynamic_cast<ActionForInterface*>(p.get()) && !dynamic_cast<ActionAnyorder*>(p.get()) ) {
      90           0 :       error("Action " + getLabel() + " is a setup action, and should be only preceded by other setup actions or by actions that can be used in any order.");
      91          16 :     } else if( (p.get())->getName()=="READMASSCHARGE" ) {
      92           0 :       error("should only be one READMASSCHARGE action in the input file");
      93             :     }
      94             :   }
      95             :   // Check for correct number of atoms
      96             :   unsigned natoms=0;
      97           2 :   std::vector<ActionToPutData*> inputs=plumed.getActionSet().select<ActionToPutData*>();
      98          16 :   for(const auto & pp : inputs ) {
      99          28 :     if( pp->getRole()=="x" ) {
     100           2 :       natoms = (pp->copyOutput(0))->getShape()[0];
     101             :     }
     102             :   }
     103             :   std::string input;
     104           2 :   parse("FILE",input);
     105           2 :   std::vector<double> masses( natoms ), charges( natoms );
     106           2 :   if( input.length()>0 ) {
     107           1 :     log.printf("   reading masses and charges from file named %s \n", input.c_str() );
     108           1 :     IFile ifile;
     109           1 :     ifile.open( input );
     110             :     int index;
     111             :     double mass;
     112             :     double charge;
     113         218 :     while(ifile.scanField("index",index).scanField("mass",mass).scanField("charge",charge).scanField()) {
     114         108 :       if( index>=natoms ) {
     115           0 :         error("indices of atoms in input file are too large");
     116             :       }
     117         108 :       masses[index]=mass;
     118         108 :       charges[index]=charge;
     119             :     }
     120           1 :     ifile.close();
     121           1 :   } else {
     122             :     std::string pdbinpt;
     123           1 :     parse("PDBFILE",pdbinpt);
     124           1 :     PDB pdb;
     125           1 :     log.printf("  reading masses and charges from pdb file named %s \n", pdbinpt.c_str() );
     126           1 :     if( !pdb.read(pdbinpt, false, 1.0 ) ) {
     127           0 :       error("error reading pdb file containing masses and charges");
     128             :     }
     129           1 :     if( natoms!=pdb.size() ) {
     130           0 :       error("mismatch between number of atoms passed from MD code and number of atoms in PDB file");
     131             :     }
     132           1 :     masses = pdb.getOccupancy();
     133           1 :     charges = pdb.getBeta();
     134           1 :   }
     135             : 
     136             :   // Now get masses and charges
     137             :   std::string nnn, qstr, mstr;
     138           2 :   Tools::convert( masses[0], mstr );
     139           2 :   Tools::convert( charges[0], qstr );
     140         216 :   for(unsigned i=1; i<natoms; ++i) {
     141         214 :     Tools::convert( masses[i], nnn );
     142         428 :     mstr += "," + nnn;
     143         214 :     Tools::convert( charges[i], nnn );
     144         428 :     qstr += "," + nnn;
     145             :   }
     146             :   // And create constant actions to hold masses and charges
     147           4 :   readInputLine( getShortcutLabel() + "_mass: CONSTANT VALUES=" + mstr );
     148           4 :   readInputLine( getShortcutLabel() + "_charges: CONSTANT VALUES=" + qstr );
     149           2 : }
     150             : 
     151             : }
     152             : }

Generated by: LCOV version 1.16