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/ActionPilot.h" 23 : #include "core/ActionWithArguments.h" 24 : #include "core/ActionAtomistic.h" 25 : #include "core/ActionRegister.h" 26 : #include "tools/CheckInRange.h" 27 : 28 : //+PLUMEDOC PRINTANALYSIS PRINT_NDX 29 : /* 30 : Print an ndx file 31 : 32 : \par Examples 33 : 34 : */ 35 : //+ENDPLUMEDOC 36 : 37 : namespace PLMD { 38 : namespace generic { 39 : 40 : class PrintNDX : 41 : public ActionPilot, 42 : public ActionAtomistic, 43 : public ActionWithArguments { 44 : std::string file; 45 : OFile ofile; 46 : CheckInRange bounds; 47 : public: 48 4 : void calculate() override {} 49 : std::string writeInGraph() const override; 50 : explicit PrintNDX(const ActionOptions&); 51 : static void registerKeywords(Keywords& keys); 52 0 : bool actionHasForces() override { 53 0 : return false; 54 : } 55 0 : void calculateNumericalDerivatives( ActionWithValue* a=NULL ) override { 56 0 : plumed_error(); 57 : } 58 : void lockRequests() override; 59 : void unlockRequests() override; 60 4 : void apply() override {} 61 : void update() override; 62 12 : ~PrintNDX() {} 63 : }; 64 : 65 : PLUMED_REGISTER_ACTION(PrintNDX,"PRINT_NDX") 66 : 67 10 : void PrintNDX::registerKeywords(Keywords& keys) { 68 10 : Action::registerKeywords(keys); 69 10 : ActionPilot::registerKeywords(keys); 70 10 : ActionAtomistic::registerKeywords( keys ); 71 10 : ActionWithArguments::registerKeywords(keys); 72 10 : keys.use("ARG"); 73 20 : keys.add("atoms","ATOMS","the list of atoms that have the corresponding arguments"); 74 20 : keys.add("compulsory","STRIDE","1","the frequency with which the quantities of interest should be output"); 75 20 : keys.add("optional","FILE","the name of the file on which to output these quantities"); 76 20 : keys.add("optional","LESS_THAN_OR_EQUAL","when printing with arguments that are vectors only print components of vectors have a value less than or equal to this value"); 77 20 : keys.add("optional","GREATER_THAN_OR_EQUAL","when printing with arguments that are vectors only print components of vectors have a value greater than or equal to this value"); 78 10 : keys.use("RESTART"); 79 10 : keys.use("UPDATE_FROM"); 80 10 : keys.use("UPDATE_UNTIL"); 81 10 : } 82 : 83 4 : PrintNDX::PrintNDX(const ActionOptions&ao): 84 : Action(ao), 85 : ActionPilot(ao), 86 : ActionAtomistic(ao), 87 4 : ActionWithArguments(ao) { 88 4 : ofile.link(*this); 89 8 : parse("FILE",file); 90 4 : if(file.length()>0) { 91 4 : ofile.open(file); 92 4 : log.printf(" on file %s\n",file.c_str()); 93 : } else { 94 0 : log.printf(" on plumed log file\n"); 95 0 : ofile.link(log); 96 : } 97 : std::vector<AtomNumber> all_atoms; 98 8 : parseAtomList("ATOMS",all_atoms); 99 4 : std::vector<std::string> argnames( getNumberOfArguments() ); 100 4 : requestAtoms( all_atoms, false ); 101 8 : for(unsigned i=0; i<getNumberOfArguments(); ++i) { 102 4 : if( getPntrToArgument(i)->getRank()!=1 || getPntrToArgument(i)->hasDerivatives() ) { 103 0 : error("arguments for print ndx should be vector"); 104 : } 105 4 : if( getPntrToArgument(i)->getShape()[0]!=all_atoms.size() ) { 106 0 : error("mismatch between number of arguments and number of input atoms"); 107 : } 108 4 : getPntrToArgument(i)->buildDataStore(true); 109 : argnames[i] = getPntrToArgument(i)->getName(); 110 : } 111 4 : log.printf(" printing ndx file containing indices of atoms that have arguments in ranges prescribed below \n"); 112 4 : log.printf(" full set of atom indices investigated are : "); 113 7988 : for(unsigned int i=0; i<all_atoms.size(); ++i) { 114 7984 : if ( (i+1) % 25 == 0 ) { 115 316 : log.printf(" \n"); 116 : } 117 7984 : log.printf(" %d", all_atoms[i].serial()); 118 : } 119 4 : log.printf("\n"); 120 : std::vector<std::string> str_upper, str_lower; 121 : std::string errors; 122 4 : parseVector("LESS_THAN_OR_EQUAL",str_upper); 123 8 : parseVector("GREATER_THAN_OR_EQUAL",str_lower); 124 4 : if( !bounds.setBounds( getNumberOfArguments(), str_lower, str_upper, errors ) ) { 125 0 : error( errors ); 126 : } 127 4 : if( bounds.wereSet() ) { 128 8 : log.printf(" %s \n", bounds.report( argnames ).c_str() ); 129 : } 130 4 : checkRead(); 131 8 : } 132 : 133 0 : std::string PrintNDX::writeInGraph() const { 134 0 : return getName() + "\n" + "FILE=" + file; 135 : } 136 : 137 4 : void PrintNDX::lockRequests() { 138 : ActionWithArguments::lockRequests(); 139 : ActionAtomistic::lockRequests(); 140 4 : } 141 : 142 4 : void PrintNDX::unlockRequests() { 143 : ActionWithArguments::unlockRequests(); 144 : ActionAtomistic::unlockRequests(); 145 4 : } 146 : 147 4 : void PrintNDX::update() { 148 : unsigned n=0; 149 4 : std::vector<double> argvals( getNumberOfArguments() ); 150 4 : ofile.printf("[ %s step %d ] \n", getLabel().c_str(), getStep() ); 151 7988 : for(unsigned i=0; i<getNumberOfAtoms(); ++i) { 152 15968 : for(unsigned j=0; j<getNumberOfArguments(); ++j) { 153 7984 : argvals[j] = getPntrToArgument(j)->get(i); 154 : } 155 7984 : if( bounds.check( argvals ) ) { 156 156 : ofile.printf("%6d", getAbsoluteIndexes()[i].serial() ); 157 156 : n++; 158 156 : if( n%15==0 ) { 159 8 : ofile.printf("\n"); 160 : } 161 : } 162 : } 163 4 : ofile.printf("\n"); 164 4 : } 165 : 166 : } 167 : 168 : 169 : }