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/ActionRegister.h" 25 : 26 : namespace PLMD { 27 : namespace generic { 28 : 29 : //+PLUMEDOC PRINTANALYSIS PRINT 30 : /* 31 : Print quantities to a file. 32 : 33 : This directive can be used multiple times 34 : in the input so you can print files with different strides or print different quantities 35 : to different files. You can control the buffering of output using the \subpage FLUSH keyword. 36 : Output file is either appended or backed up depending on the presence of the \ref RESTART action. 37 : A per-action `RESTART` keyword can be used as well. 38 : 39 : Notice that printing happens in the so-called "update" phase. This implies that printing 40 : is affected by the presence of \ref UPDATE_IF actions. In addition, one might decide to start 41 : and stop printing at preassigned values of time using the `UPDATE_FROM` and `UPDATE_UNTIL` keywords. 42 : Keep into account that even on steps when the action is not updated (and thus the file is not printed) 43 : the argument will be activated. In other words, if you use `UPDATE_FROM` to start printing at a given time, 44 : the collective variables this PRINT statement depends on will be computed also before that time. 45 : 46 : \par Examples 47 : 48 : The following input instructs plumed to print the distance between atoms 3 and 5 on a file 49 : called COLVAR every 10 steps, and the distance and total energy on a file called COLVAR_ALL 50 : every 1000 steps. 51 : \plumedfile 52 : # compute distance: 53 : distance: DISTANCE ATOMS=2,5 54 : # compute total energy (potential) 55 : energy: ENERGY 56 : # print distance on a file 57 : PRINT ARG=distance STRIDE=10 FILE=COLVAR 58 : # print both variables on another file 59 : PRINT ARG=distance,energy STRIDE=1000 FILE=COLVAR_ALL 60 : \endplumedfile 61 : 62 : Notice that \ref DISTANCE and \ref ENERGY are computed respectively every 10 and 1000 steps, that is 63 : only when required. 64 : 65 : */ 66 : //+ENDPLUMEDOC 67 : 68 : class Print : 69 : public ActionPilot, 70 : public ActionWithArguments 71 : { 72 : std::string file; 73 : OFile ofile; 74 : std::string fmt; 75 : // small internal utility 76 : ///////////////////////////////////////// 77 : // these are crazy things just for debug: 78 : // they allow to change regularly the 79 : // printed argument 80 : int rotate; 81 : int rotateCountdown; 82 : int rotateLast; 83 : std::vector<Value*> rotateArguments; 84 : ///////////////////////////////////////// 85 : public: 86 212144 : void calculate() override {} 87 : void prepare() override; 88 : explicit Print(const ActionOptions&); 89 : static void registerKeywords(Keywords& keys); 90 212026 : void apply() override {} 91 : void update() override; 92 : ~Print(); 93 : }; 94 : 95 12005 : PLUMED_REGISTER_ACTION(Print,"PRINT") 96 : 97 794 : void Print::registerKeywords(Keywords& keys) { 98 794 : Action::registerKeywords(keys); 99 794 : ActionPilot::registerKeywords(keys); 100 794 : ActionWithArguments::registerKeywords(keys); 101 794 : keys.use("ARG"); 102 1588 : keys.add("compulsory","STRIDE","1","the frequency with which the quantities of interest should be output"); 103 1588 : keys.add("optional","FILE","the name of the file on which to output these quantities"); 104 1588 : keys.add("optional","FMT","the format that should be used to output real numbers"); 105 1588 : keys.add("hidden","_ROTATE","some funky thing implemented by GBussi"); 106 794 : keys.use("RESTART"); 107 794 : keys.use("UPDATE_FROM"); 108 794 : keys.use("UPDATE_UNTIL"); 109 794 : } 110 : 111 793 : Print::Print(const ActionOptions&ao): 112 : Action(ao), 113 : ActionPilot(ao), 114 : ActionWithArguments(ao), 115 793 : fmt("%f"), 116 1586 : rotate(0) 117 : { 118 793 : ofile.link(*this); 119 1586 : parse("FILE",file); 120 793 : if(file.length()>0) { 121 793 : ofile.open(file); 122 793 : log.printf(" on file %s\n",file.c_str()); 123 : } else { 124 0 : log.printf(" on plumed log file\n"); 125 0 : ofile.link(log); 126 : } 127 793 : parse("FMT",fmt); 128 793 : fmt=" "+fmt; 129 793 : log.printf(" with format %s\n",fmt.c_str()); 130 6100 : for(unsigned i=0; i<getNumberOfArguments(); ++i) ofile.setupPrintValue( getPntrToArgument(i) ); 131 : ///////////////////////////////////////// 132 : // these are crazy things just for debug: 133 : // they allow to change regularly the 134 : // printed argument 135 793 : parse("_ROTATE",rotate); 136 793 : if(rotate>0) { 137 1 : rotateCountdown=rotate; 138 4 : for(unsigned i=0; i<getNumberOfArguments(); ++i) rotateArguments.push_back( getPntrToArgument(i) ); 139 1 : std::vector<Value*> a(1,rotateArguments[0]); 140 1 : requestArguments(std::vector<Value*>(1,rotateArguments[0])); 141 1 : rotateLast=0; 142 : } 143 : ///////////////////////////////////////// 144 793 : checkRead(); 145 793 : } 146 : 147 212267 : void Print::prepare() { 148 : ///////////////////////////////////////// 149 : // these are crazy things just for debug: 150 : // they allow to change regularly the 151 : // printed argument 152 212267 : if(rotate>0) { 153 5 : rotateCountdown--; 154 5 : if(rotateCountdown==0) { 155 2 : rotateCountdown=rotate; 156 2 : rotateLast++; 157 2 : rotateLast%=rotateArguments.size(); 158 4 : requestArguments(std::vector<Value*>(1,rotateArguments[rotateLast])); 159 : } 160 : } 161 : ///////////////////////////////////////// 162 212267 : } 163 : 164 211407 : void Print::update() { 165 211407 : ofile.fmtField(" %f"); 166 211407 : ofile.printField("time",getTime()); 167 742140 : for(unsigned i=0; i<getNumberOfArguments(); i++) { 168 530733 : ofile.fmtField(fmt); 169 530733 : ofile.printField( getPntrToArgument(i), getArgument(i) ); 170 : } 171 211407 : ofile.printField(); 172 211407 : } 173 : 174 1586 : Print::~Print() { 175 2379 : } 176 : 177 : } 178 : 179 : 180 : }