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 6 : void MassChargeInput::registerKeywords(Keywords& keys) { 53 6 : ActionShortcut::registerKeywords( keys ); 54 12 : keys.add("optional","FILE","input file that contains the masses and charges that should be used"); 55 12 : keys.add("compulsory","PDBFILE","a pdb file that contains the masses and charges of the atoms in the beta and occupancy columns"); 56 12 : keys.addOutputComponent("mass","default","the masses of the atoms in the system"); 57 12 : keys.addOutputComponent("charges","default","the masses of the atoms in the system"); 58 6 : keys.needsAction("CONSTANT"); 59 6 : } 60 : 61 2 : MassChargeInput::MassChargeInput(const ActionOptions& ao): 62 : Action(ao), 63 2 : ActionShortcut(ao) { 64 2 : const ActionSet& actionset(plumed.getActionSet()); 65 18 : for(const auto & p : actionset) { 66 : // check that all the preceding actions are ActionSetup 67 16 : if( !dynamic_cast<ActionSetup*>(p.get()) && !dynamic_cast<ActionForInterface*>(p.get()) && !dynamic_cast<ActionAnyorder*>(p.get()) ) { 68 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."); 69 16 : } else if( (p.get())->getName()=="READMASSCHARGE" ) { 70 0 : error("should only be one READMASSCHARGE action in the input file"); 71 : } 72 : } 73 : // Check for correct number of atoms 74 : unsigned natoms=0; 75 2 : std::vector<ActionToPutData*> inputs=plumed.getActionSet().select<ActionToPutData*>(); 76 16 : for(const auto & pp : inputs ) { 77 28 : if( pp->getRole()=="x" ) { 78 2 : natoms = (pp->copyOutput(0))->getShape()[0]; 79 : } 80 : } 81 : std::string input; 82 2 : parse("FILE",input); 83 2 : std::vector<double> masses( natoms ), charges( natoms ); 84 2 : if( input.length()>0 ) { 85 1 : log.printf(" reading masses and charges from file named %s \n", input.c_str() ); 86 1 : IFile ifile; 87 1 : ifile.open( input ); 88 : int index; 89 : double mass; 90 : double charge; 91 218 : while(ifile.scanField("index",index).scanField("mass",mass).scanField("charge",charge).scanField()) { 92 108 : if( index>=natoms ) { 93 0 : error("indices of atoms in input file are too large"); 94 : } 95 108 : masses[index]=mass; 96 108 : charges[index]=charge; 97 : } 98 1 : ifile.close(); 99 1 : } else { 100 : std::string pdbinpt; 101 1 : parse("PDBFILE",pdbinpt); 102 1 : PDB pdb; 103 1 : log.printf(" reading masses and charges from pdb file named %s \n", pdbinpt.c_str() ); 104 1 : if( !pdb.read(pdbinpt, false, 1.0 ) ) { 105 0 : error("error reading pdb file containing masses and charges"); 106 : } 107 1 : if( natoms!=pdb.size() ) { 108 0 : error("mismatch between number of atoms passed from MD code and number of atoms in PDB file"); 109 : } 110 1 : masses = pdb.getOccupancy(); 111 1 : charges = pdb.getBeta(); 112 1 : } 113 : 114 : // Now get masses and charges 115 : std::string nnn, qstr, mstr; 116 2 : Tools::convert( masses[0], mstr ); 117 2 : Tools::convert( charges[0], qstr ); 118 216 : for(unsigned i=1; i<natoms; ++i) { 119 214 : Tools::convert( masses[i], nnn ); 120 428 : mstr += "," + nnn; 121 214 : Tools::convert( charges[i], nnn ); 122 428 : qstr += "," + nnn; 123 : } 124 : // And create constant actions to hold masses and charges 125 4 : readInputLine( getShortcutLabel() + "_mass: CONSTANT VALUES=" + mstr ); 126 4 : readInputLine( getShortcutLabel() + "_charges: CONSTANT VALUES=" + qstr ); 127 2 : } 128 : 129 : } 130 : }