LCOV - code coverage report
Current view: top level - generic - MassChargeInput.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 40 41 97.6 %
Date: 2024-10-18 13:59:31 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             : \par Examples
      40             : 
      41             : */
      42             : //+ENDPLUMEDOC
      43             : 
      44             : class MassChargeInput : public ActionShortcut {
      45             : public:
      46             :   static void registerKeywords(Keywords& keys);
      47             :   explicit MassChargeInput(const ActionOptions&);
      48             : };
      49             : 
      50             : PLUMED_REGISTER_ACTION(MassChargeInput,"READMASSCHARGE")
      51             : 
      52           4 : void MassChargeInput::registerKeywords(Keywords& keys) {
      53           4 :   ActionShortcut::registerKeywords( keys );
      54           8 :   keys.add("optional","FILE","input file that contains the masses and charges that should be used");
      55           8 :   keys.add("compulsory","PDBFILE","a pdb file that contains the masses and charges of the atoms in the beta and occupancy columns");
      56           8 :   keys.addOutputComponent("mass","default","vector","the masses of the atoms in the system");
      57           8 :   keys.addOutputComponent("charges","default","vector","the masses of the atoms in the system");
      58           4 :   keys.needsAction("CONSTANT");
      59           4 : }
      60             : 
      61           2 : MassChargeInput::MassChargeInput(const ActionOptions& ao):
      62             :   Action(ao),
      63           2 :   ActionShortcut(ao)
      64             : {
      65           2 :   const ActionSet& actionset(plumed.getActionSet());
      66          18 :   for(const auto & p : actionset) {
      67             :     // check that all the preceding actions are ActionSetup
      68          16 :     if( !dynamic_cast<ActionSetup*>(p.get()) && !dynamic_cast<ActionForInterface*>(p.get()) && !dynamic_cast<ActionAnyorder*>(p.get()) ) {
      69           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.");
      70          16 :     } else if( (p.get())->getName()=="READMASSCHARGE" ) error("should only be one READMASSCHARGE action in the input file");
      71             :   }
      72             :   // Check for correct number of atoms
      73           2 :   unsigned natoms=0; std::vector<ActionToPutData*> inputs=plumed.getActionSet().select<ActionToPutData*>();
      74          16 :   for(const auto & pp : inputs ) {
      75          28 :     if( pp->getRole()=="x" ) natoms = (pp->copyOutput(0))->getShape()[0];
      76             :   }
      77           4 :   std::string input; parse("FILE",input); std::vector<double> masses( natoms ), charges( natoms );
      78           2 :   if( input.length()>0 ) {
      79           1 :     log.printf("   reading masses and charges from file named %s \n", input.c_str() );
      80           1 :     IFile ifile; ifile.open( input ); int index; double mass; double charge;
      81         218 :     while(ifile.scanField("index",index).scanField("mass",mass).scanField("charge",charge).scanField()) {
      82         108 :       if( index>=natoms ) error("indices of atoms in input file are too large");
      83         108 :       masses[index]=mass; charges[index]=charge;
      84             :     }
      85           1 :     ifile.close();
      86           1 :   } else {
      87           2 :     std::string pdbinpt; parse("PDBFILE",pdbinpt); PDB pdb;
      88           1 :     log.printf("  reading masses and charges from pdb file named %s \n", pdbinpt.c_str() );
      89           1 :     if( !pdb.read(pdbinpt, false, 1.0 ) ) error("error reading pdb file containing masses and charges");
      90           1 :     if( natoms!=pdb.size() ) error("mismatch between number of atoms passed from MD code and number of atoms in PDB file");
      91           1 :     masses = pdb.getOccupancy(); charges = pdb.getBeta();
      92           1 :   }
      93             : 
      94             :   // Now get masses and charges
      95             :   std::string nnn, qstr, mstr;
      96           2 :   Tools::convert( masses[0], mstr );
      97           2 :   Tools::convert( charges[0], qstr );
      98         216 :   for(unsigned i=1; i<natoms; ++i) {
      99         428 :     Tools::convert( masses[i], nnn ); mstr += "," + nnn;
     100         428 :     Tools::convert( charges[i], nnn ); qstr += "," + nnn;
     101             :   }
     102             :   // And create constant actions to hold masses and charges
     103           4 :   readInputLine( getShortcutLabel() + "_mass: CONSTANT VALUES=" + mstr );
     104           4 :   readInputLine( getShortcutLabel() + "_charges: CONSTANT VALUES=" + qstr );
     105           2 : }
     106             : 
     107             : }
     108             : }

Generated by: LCOV version 1.16