Line data Source code
1 : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 : Copyright (c) 2015-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 "AnalysisBase.h" 23 : #include "reference/ReferenceAtoms.h" 24 : #include "reference/ReferenceArguments.h" 25 : #include "core/ActionRegister.h" 26 : #include "core/PlumedMain.h" 27 : 28 : namespace PLMD { 29 : namespace analysis { 30 : 31 : //+PLUMEDOC ANALYSIS OUTPUT_ANALYSIS_DATA_TO_COLVAR 32 : /* 33 : This can be used to output the data that has been stored in an Analysis object. 34 : 35 : The most useful application of this method is to output all projections of all the 36 : points that were stored in an analysis object that performs some form of dimensionality 37 : reduction. If you use the USE_DIMRED_DATA_FROM option below projections of all the 38 : stored points will be output to a file. The positions of these projections will be calculated 39 : using that dimensionality reduction algorithms out-of-sample extension algorithm. 40 : 41 : \par Examples 42 : 43 : */ 44 : //+ENDPLUMEDOC 45 : 46 : class OutputColvarFile : public AnalysisBase { 47 : private: 48 : std::string fmt; 49 : std::string filename; 50 : bool output_for_all_replicas; 51 : std::vector<unsigned> preps; 52 : std::vector<std::string> req_vals; 53 : public: 54 : static void registerKeywords( Keywords& keys ); 55 : explicit OutputColvarFile( const ActionOptions& ); 56 0 : void performTask( const unsigned&, const unsigned&, MultiValue& ) const override { 57 0 : plumed_error(); 58 : } 59 : void performAnalysis() override; 60 : }; 61 : 62 13825 : PLUMED_REGISTER_ACTION(OutputColvarFile,"OUTPUT_ANALYSIS_DATA_TO_COLVAR") 63 : 64 24 : void OutputColvarFile::registerKeywords( Keywords& keys ) { 65 24 : AnalysisBase::registerKeywords( keys ); 66 24 : keys.use("ARG"); 67 48 : keys.add("compulsory","FILE","the name of the file to output to"); 68 48 : keys.add("compulsory","REPLICA","0","the replicas for which you would like to output this information"); 69 48 : keys.add("compulsory","STRIDE","0","the frequency with which to perform the required analysis and to output the data. The default value of 0 tells plumed to use all the data"); 70 48 : keys.add("optional","FMT","the format to output the data using"); 71 24 : } 72 : 73 20 : OutputColvarFile::OutputColvarFile( const ActionOptions& ao ): 74 : Action(ao), 75 : AnalysisBase(ao), 76 40 : fmt("%f"), 77 20 : output_for_all_replicas(false) { 78 20 : parse("FILE",filename); 79 40 : parse("FMT",fmt); 80 20 : if( !getRestart() ) { 81 20 : OFile ofile; 82 20 : ofile.link(*this); 83 20 : ofile.setBackupString("analysis"); 84 20 : ofile.backupAllFiles(filename); 85 20 : } 86 20 : log.printf(" printing data to file named %s \n",filename.c_str() ); 87 20 : if( getArguments().size()==0 ) { 88 12 : std::vector<std::string> tmp_vals( my_input_data->getArgumentNames() ); 89 12 : req_vals.resize( tmp_vals.size() ); 90 18 : for(unsigned i=0; i<tmp_vals.size(); ++i) { 91 : req_vals[i]=tmp_vals[i]; 92 : } 93 12 : } else { 94 8 : req_vals.resize( getArguments().size() ); 95 24 : for(unsigned i=0; i<req_vals.size(); ++i) { 96 : req_vals[i]=getPntrToArgument(i)->getName(); 97 : } 98 : } 99 20 : if( req_vals.size()==0 ) { 100 6 : log.printf(" outputting weights from input action \n"); 101 : } else { 102 14 : log.printf(" outputting %s", req_vals[0].c_str() ); 103 22 : for(unsigned i=1; i<req_vals.size(); ++i) { 104 8 : log.printf(",", req_vals[i].c_str() ); 105 : } 106 14 : log.printf("\n"); 107 : } 108 : std::string rep_data; 109 40 : parse("REPLICA",rep_data); 110 20 : if( rep_data=="all" ) { 111 0 : output_for_all_replicas=true; 112 : } else { 113 20 : preps.resize(1); 114 20 : Tools::convert( rep_data, preps[0] ); 115 : } 116 20 : if( output_for_all_replicas ) { 117 0 : log.printf(" outputting files for all replicas \n"); 118 : } else { 119 20 : log.printf(" outputting data for replicas "); 120 40 : for(unsigned i=0; i<preps.size(); ++i) { 121 20 : log.printf("%d ", preps[i] ); 122 : } 123 : } 124 20 : } 125 : 126 24 : void OutputColvarFile::performAnalysis() { 127 24 : if( !output_for_all_replicas ) { 128 : bool found=false; 129 24 : unsigned myrep=plumed.multi_sim_comm.Get_rank(); 130 29 : for(unsigned i=0; i<preps.size(); ++i) { 131 24 : if( myrep==preps[i] ) { 132 : found=true; 133 : break; 134 : } 135 : } 136 24 : if( !found ) { 137 5 : return; 138 : } 139 : } 140 : // Output the embedding as long lists of data 141 19 : OFile gfile; 142 19 : gfile.link(*this); 143 19 : gfile.setBackupString("analysis"); 144 19 : gfile.fmtField(fmt+" "); 145 19 : gfile.open( filename ); 146 : 147 : // Print embedding coordinates 148 2599 : for(unsigned i=0; i<getNumberOfDataPoints(); ++i) { 149 2580 : const DataCollectionObject& mydata=getStoredData(i, false); 150 6989 : for(unsigned j=0; j<req_vals.size(); ++j) { 151 4409 : gfile.printField( req_vals[j], mydata.getArgumentValue(req_vals[j]) ); 152 : } 153 2580 : gfile.printField( "weight", getWeight(i) ); 154 2580 : gfile.printField(); 155 : } 156 19 : gfile.close(); 157 19 : } 158 : 159 : } 160 : }