Line data Source code
1 : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 : Copyright (c) 2016-2020 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 "GridPrintingBase.h" 23 : #include "core/PlumedMain.h" 24 : #include "core/ActionSet.h" 25 : #include "vesselbase/ActionWithVessel.h" 26 : 27 : namespace PLMD { 28 : namespace gridtools { 29 : 30 58 : void GridPrintingBase::registerKeywords( Keywords& keys ) { 31 58 : Action::registerKeywords( keys ); ActionPilot::registerKeywords( keys ); 32 232 : keys.add("compulsory","GRID","the action that creates the grid you would like to output"); 33 290 : keys.add("compulsory","STRIDE","0","the frequency with which the grid should be output to the file. The default " 34 : "value of 0 ensures that the grid is only output at the end of the trajectory"); 35 290 : keys.add("compulsory","FILE","density","the file on which to write the grid."); 36 290 : keys.add("compulsory","REPLICA","0","the replicas for which you would like to output this information"); 37 232 : keys.add("optional","FMT","the format that should be used to output real numbers"); 38 58 : } 39 : 40 55 : GridPrintingBase::GridPrintingBase(const ActionOptions&ao): 41 : Action(ao), 42 : ActionPilot(ao), 43 : fmt("%f"), 44 110 : output_for_all_replicas(false) 45 : { 46 110 : std::string mlab; parse("GRID",mlab); 47 110 : vesselbase::ActionWithVessel* mves= plumed.getActionSet().selectWithLabel<vesselbase::ActionWithVessel*>(mlab); 48 55 : if(!mves) error("action labelled " + mlab + " does not exist or does not have vessels"); 49 55 : addDependency(mves); 50 : 51 55 : for(unsigned i=0; i<mves->getNumberOfVessels(); ++i) { 52 55 : ingrid=dynamic_cast<GridVessel*>( mves->getPntrToVessel(i) ); 53 55 : if( ingrid ) break; 54 : } 55 55 : if( !ingrid ) error("input action does not calculate a grid"); 56 : 57 110 : parse("FILE",filename); 58 55 : if(filename.length()==0) error("name out output file was not specified"); 59 165 : log.printf(" outputting grid calculated by action %s to file named %s",mves->getLabel().c_str(), filename.c_str() ); 60 110 : if( keywords.exists("FMT") ) { 61 159 : parse("FMT",fmt); log.printf(" with format %s \n", fmt.c_str() ); 62 : } else { 63 2 : log.printf("\n"); 64 : } 65 165 : std::vector<std::string> rep_data; parseVector("REPLICA",rep_data); 66 55 : if( rep_data.size()==1 ) { 67 55 : if( rep_data[0]=="all" ) output_for_all_replicas=true; 68 : else { 69 110 : preps.resize(1); Tools::convert( rep_data[0], preps[0] ); 70 : } 71 : } else { 72 0 : preps.resize( rep_data.size() ); 73 0 : for(unsigned i=0; i<rep_data.size(); ++i) Tools::convert( rep_data[i], preps[i] ); 74 : } 75 55 : } 76 : 77 68 : void GridPrintingBase::update() { 78 68 : if( !output_for_all_replicas ) { 79 68 : bool found=false; unsigned myrep=plumed.multi_sim_comm.Get_rank(); 80 136 : for(unsigned i=0; i<preps.size(); ++i) { 81 68 : if( myrep==preps[i] ) { found=true; break; } 82 : } 83 93 : if( !found ) return; 84 : } 85 68 : if( getStep()==0 || getStride()==0 ) return ; 86 : 87 86 : OFile ofile; ofile.link(*this); 88 86 : ofile.setBackupString("analysis"); 89 43 : ofile.open( filename ); printGrid( ofile ); 90 : } 91 : 92 55 : void GridPrintingBase::runFinalJobs() { 93 55 : if( !output_for_all_replicas ) { 94 55 : bool found=false; unsigned myrep=plumed.multi_sim_comm.Get_rank(); 95 125 : for(unsigned i=0; i<preps.size(); ++i) { 96 55 : if( myrep==preps[i] ) { found=true; break; } 97 : } 98 85 : if( !found ) return; 99 : } 100 50 : if( getStride()>0 ) return; 101 : 102 50 : OFile ofile; ofile.link(*this); 103 25 : ofile.open( filename ); printGrid( ofile ); 104 : } 105 : 106 : } 107 5517 : }