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 "Function.h" 23 : #include "tools/OpenMP.h" 24 : #include "tools/Communicator.h" 25 : 26 : namespace PLMD { 27 : namespace function { 28 : 29 746 : void Function::registerKeywords(Keywords& keys) { 30 746 : Action::registerKeywords(keys); 31 746 : ActionWithValue::registerKeywords(keys); 32 746 : ActionWithArguments::registerKeywords(keys); 33 1492 : 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"); 34 746 : } 35 : 36 686 : Function::Function(const ActionOptions&ao): 37 : Action(ao), 38 : ActionWithValue(ao), 39 686 : ActionWithArguments(ao) { 40 686 : } 41 : 42 588 : void Function::addValueWithDerivatives() { 43 588 : plumed_massert( getNumberOfArguments()!=0, "for functions you must requestArguments before adding values"); 44 588 : ActionWithValue::addValueWithDerivatives(); 45 588 : getPntrToValue()->resizeDerivatives(getNumberOfArguments()); 46 : 47 1176 : if( keywords.exists("PERIODIC") ) { 48 : std::vector<std::string> period; 49 1170 : parseVector("PERIODIC",period); 50 1167 : if(period.size()==1 && period[0]=="NO") { 51 582 : setNotPeriodic(); 52 3 : } else if(period.size()==2) { 53 3 : setPeriodic(period[0],period[1]); 54 : } else { 55 0 : error("missing PERIODIC keyword"); 56 : } 57 585 : } 58 588 : } 59 : 60 3173 : void Function::addComponentWithDerivatives( const std::string& name ) { 61 3173 : plumed_massert( getNumberOfArguments()!=0, "for functions you must requestArguments before adding values"); 62 3173 : ActionWithValue::addComponentWithDerivatives(name); 63 3173 : getPntrToComponent(name)->resizeDerivatives(getNumberOfArguments()); 64 3173 : } 65 : 66 226509 : void Function::apply() { 67 226509 : const unsigned noa=getNumberOfArguments(); 68 226509 : const unsigned ncp=getNumberOfComponents(); 69 226509 : const unsigned cgs=comm.Get_size(); 70 : 71 226509 : std::vector<double> f(noa,0.0); 72 : 73 : unsigned stride=1; 74 : unsigned rank=0; 75 226509 : if(ncp>4*cgs) { 76 6 : stride=comm.Get_size(); 77 6 : rank=comm.Get_rank(); 78 : } 79 : 80 226509 : unsigned at_least_one_forced=0; 81 226509 : #pragma omp parallel num_threads(OpenMP::getNumThreads()) shared(f) 82 : { 83 : std::vector<double> omp_f(noa,0.0); 84 : std::vector<double> forces(noa); 85 : #pragma omp for reduction( + : at_least_one_forced) 86 : for(unsigned i=rank; i<ncp; i+=stride) { 87 : if(getPntrToComponent(i)->applyForce(forces)) { 88 : at_least_one_forced+=1; 89 : for(unsigned j=0; j<noa; j++) { 90 : omp_f[j]+=forces[j]; 91 : } 92 : } 93 : } 94 : #pragma omp critical 95 : for(unsigned j=0; j<noa; j++) { 96 : f[j]+=omp_f[j]; 97 : } 98 : } 99 : 100 226509 : if(noa>0&&ncp>4*cgs) { 101 6 : comm.Sum(&f[0],noa); 102 6 : comm.Sum(at_least_one_forced); 103 : } 104 : 105 226509 : if(at_least_one_forced>0) 106 68744 : for(unsigned i=0; i<noa; ++i) { 107 40700 : getPntrToArgument(i)->addForce(f[i]); 108 : } 109 226509 : } 110 : 111 : } 112 : }