LCOV - code coverage report
Current view: top level - generic - MassChargeInput.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 48 54 88.9 %
Date: 2026-11-05 10:19:04 Functions: 3 4 75.0 %

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

Generated by: LCOV version 1.16