Line data Source code
1 : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 : Copyright (c) 2011-2019 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 "Function.h" 23 : #include "tools/OpenMP.h" 24 : #include "tools/Communicator.h" 25 : 26 : using namespace std; 27 : namespace PLMD { 28 : namespace function { 29 : 30 512 : void Function::registerKeywords(Keywords& keys) { 31 512 : Action::registerKeywords(keys); 32 512 : ActionWithValue::registerKeywords(keys); 33 512 : ActionWithArguments::registerKeywords(keys); 34 2048 : keys.reserve("compulsory","PERIODIC","if the output of your function is periodic then you should specify the periodicity of the function. If the output is not periodic you must state this using PERIODIC=NO"); 35 512 : } 36 : 37 500 : Function::Function(const ActionOptions&ao): 38 : Action(ao), 39 : ActionWithValue(ao), 40 500 : ActionWithArguments(ao) 41 : { 42 500 : } 43 : 44 414 : void Function::addValueWithDerivatives() { 45 414 : plumed_massert( getNumberOfArguments()!=0, "for functions you must requestArguments before adding values"); 46 414 : ActionWithValue::addValueWithDerivatives(); 47 414 : getPntrToValue()->resizeDerivatives(getNumberOfArguments()); 48 : 49 828 : if( keywords.exists("PERIODIC") ) { 50 : std::vector<std::string> period; 51 822 : parseVector("PERIODIC",period); 52 819 : if(period.size()==1 && period[0]=="NO") { 53 408 : setNotPeriodic(); 54 3 : } else if(period.size()==2) { 55 3 : setPeriodic(period[0],period[1]); 56 0 : } else error("missing PERIODIC keyword"); 57 : } 58 414 : } 59 : 60 3159 : void Function::addComponentWithDerivatives( const std::string& name ) { 61 3159 : plumed_massert( getNumberOfArguments()!=0, "for functions you must requestArguments before adding values"); 62 3159 : ActionWithValue::addComponentWithDerivatives(name); 63 3159 : getPntrToComponent(name)->resizeDerivatives(getNumberOfArguments()); 64 3159 : } 65 : 66 34758 : void Function::apply() 67 : { 68 34758 : const unsigned noa=getNumberOfArguments(); 69 34758 : const unsigned ncp=getNumberOfComponents(); 70 34758 : const unsigned cgs=comm.Get_size(); 71 : 72 34758 : vector<double> f(noa,0.0); 73 : 74 : unsigned stride=1; 75 : unsigned rank=0; 76 34758 : if(ncp>4*cgs) { 77 6 : stride=comm.Get_size(); 78 6 : rank=comm.Get_rank(); 79 : } 80 : 81 34758 : unsigned at_least_one_forced=0; 82 103053 : #pragma omp parallel num_threads(OpenMP::getNumThreads()) shared(f) 83 : { 84 68295 : vector<double> omp_f(noa,0.0); 85 69510 : vector<double> forces(noa); 86 69449 : #pragma omp for reduction( + : at_least_one_forced) 87 : for(unsigned i=rank; i<ncp; i+=stride) { 88 37889 : if(getPntrToComponent(i)->applyForce(forces)) { 89 26511 : at_least_one_forced+=1; 90 2093323 : for(unsigned j=0; j<noa; j++) omp_f[j]+=forces[j]; 91 : } 92 : } 93 139032 : #pragma omp critical 94 374046 : for(unsigned j=0; j<noa; j++) f[j]+=omp_f[j]; 95 : } 96 : 97 34764 : if(noa>0&&ncp>4*cgs) { comm.Sum(&f[0],noa); comm.Sum(at_least_one_forced); } 98 : 99 109340 : if(at_least_one_forced>0) for(unsigned i=0; i<noa; ++i) getPntrToArgument(i)->addForce(f[i]); 100 34758 : } 101 : 102 : } 103 5874 : }