Line data Source code
1 : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 : Copyright (c) 2017-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 "ActionToGetData.h" 23 : #include "ActionRegister.h" 24 : #include "PlumedMain.h" 25 : 26 : //+PLUMEDOC ANALYSIS GET 27 : /* 28 : Get data from PLUMED for another code 29 : 30 : \par Examples 31 : 32 : */ 33 : //+ENDPLUMEDOC 34 : 35 : namespace PLMD { 36 : 37 : PLUMED_REGISTER_ACTION(ActionToGetData,"GET") 38 : 39 125 : void ActionToGetData::registerKeywords(Keywords& keys) { 40 125 : Action::registerKeywords(keys); 41 125 : ActionPilot::registerKeywords(keys); 42 125 : ActionWithArguments::registerKeywords(keys); 43 250 : keys.add("compulsory","STRIDE","1","the frequency with which the quantities of interest should be stored"); 44 250 : keys.add("compulsory","TYPE","value","what do you want to collect for the value can be derivative/force"); 45 125 : keys.use("ARG"); 46 125 : keys.setValueDescription("a copy of the data in the value specified by the ARG keyword"); 47 125 : } 48 : 49 121 : ActionToGetData::ActionToGetData(const ActionOptions&ao): 50 : Action(ao), 51 : ActionPilot(ao), 52 : ActionWithArguments(ao), 53 121 : mydata(DataPassingObject::create(plumed.getRealPrecision())) { 54 : std::string type; 55 242 : parse("TYPE",type); 56 121 : if( type=="value" ) { 57 121 : gtype=val; 58 0 : } else if( type=="derivatives" ) { 59 0 : gtype=deriv; 60 0 : } else if( type=="forces" ) { 61 0 : gtype=force; 62 : } else { 63 0 : plumed_merror("cannot get " + type + " for value TYPE should be value/derivative/force"); 64 : } 65 : 66 121 : if( gtype!=val ) { 67 0 : error("not implemented functionality to pass derviatives or forces to python. Email gareth.tribello@gmail.com if you want this."); 68 : } 69 : 70 121 : if( getNumberOfArguments()!=1 ) { 71 0 : error("python interface works best when you ask for one argument at a time"); 72 : } 73 121 : if( getPntrToArgument(0)->getNumberOfValues()==0 ) { 74 0 : error("cannot get data as shape of value " + getPntrToArgument(0)->getName() + " has not been set"); 75 : } 76 121 : getPntrToArgument(0)->buildDataStore(); 77 121 : data.resize( getPntrToArgument(0)->getNumberOfValues() ); 78 121 : } 79 : 80 121 : void ActionToGetData::get_rank( const TypesafePtr & dims ) { 81 121 : if( getPntrToArgument(0)->getRank()==0 ) { 82 98 : dims.set(long(1)); 83 98 : return; 84 : } 85 23 : dims.set(long(getPntrToArgument(0)->getRank())); 86 : } 87 : 88 57 : void ActionToGetData::get_shape( const TypesafePtr & dims ) { 89 57 : if( getPntrToArgument(0)->getRank()==0 ) { 90 34 : dims.set(long(1)); 91 34 : return; 92 : } 93 23 : auto dims_=dims.get<long*>( { getPntrToArgument(0)->getRank() } ); 94 49 : for(unsigned j=0; j<getPntrToArgument(0)->getRank(); ++j) { 95 26 : dims_[j] = getPntrToArgument(0)->getShape()[j]; 96 : } 97 : } 98 : 99 121 : void ActionToGetData::set_memory( const TypesafePtr & val ) { 100 121 : mydata->setValuePointer(val,getPntrToArgument(0)->getShape(),false); 101 121 : } 102 : 103 23914 : void ActionToGetData::calculate() { 104 23914 : plumed_assert( gtype==val ); 105 23914 : mydata->setData( getPntrToArgument(0) ); 106 23914 : } 107 : 108 : }