LCOV - code coverage report
Current view: top level - function - Function.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 49 51 96.1 %
Date: 2018-12-19 07:49:13 Functions: 8 9 88.9 %

          Line data    Source code
       1             : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
       2             :    Copyright (c) 2011-2018 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             : 
      25             : using namespace std;
      26             : namespace PLMD {
      27             : namespace function {
      28             : 
      29         162 : void Function::registerKeywords(Keywords& keys) {
      30         162 :   Action::registerKeywords(keys);
      31         162 :   ActionWithValue::registerKeywords(keys);
      32         162 :   ActionWithArguments::registerKeywords(keys);
      33         162 :   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         162 : }
      35             : 
      36         152 : Function::Function(const ActionOptions&ao):
      37             :   Action(ao),
      38             :   ActionWithValue(ao),
      39         152 :   ActionWithArguments(ao)
      40             : {
      41         152 : }
      42             : 
      43          87 : void Function::addValueWithDerivatives() {
      44          87 :   plumed_massert( getNumberOfArguments()!=0, "for functions you must requestArguments before adding values");
      45          87 :   ActionWithValue::addValueWithDerivatives();
      46          87 :   getPntrToValue()->resizeDerivatives(getNumberOfArguments());
      47             : 
      48          87 :   if( keywords.exists("PERIODIC") ) {
      49          86 :     std::vector<std::string> period;
      50          86 :     parseVector("PERIODIC",period);
      51          86 :     if(period.size()==1 && period[0]=="NO") {
      52          83 :       setNotPeriodic();
      53           3 :     } else if(period.size()==2) {
      54           3 :       setPeriodic(period[0],period[1]);
      55           0 :     } else error("missing PERIODIC keyword");
      56             :   }
      57          87 : }
      58             : 
      59        3119 : void Function::addComponentWithDerivatives( const std::string& name ) {
      60        3119 :   plumed_massert( getNumberOfArguments()!=0, "for functions you must requestArguments before adding values");
      61        3119 :   ActionWithValue::addComponentWithDerivatives(name);
      62        3119 :   getPntrToComponent(name)->resizeDerivatives(getNumberOfArguments());
      63        3119 : }
      64             : 
      65        1994 : void Function::apply()
      66             : {
      67        1994 :   const unsigned noa=getNumberOfArguments();
      68        1994 :   const unsigned ncp=getNumberOfComponents();
      69        1994 :   const unsigned cgs=comm.Get_size();
      70             : 
      71        1994 :   vector<double> f(noa,0.0);
      72             : 
      73        1994 :   unsigned stride=1;
      74        1994 :   unsigned rank=0;
      75        1994 :   if(ncp>4*cgs) {
      76           6 :     stride=comm.Get_size();
      77           6 :     rank=comm.Get_rank();
      78             :   }
      79             : 
      80        1994 :   unsigned at_least_one_forced=0;
      81        5845 :   #pragma omp parallel num_threads(OpenMP::getNumThreads()) shared(f)
      82             :   {
      83        3851 :     vector<double> omp_f(noa,0.0);
      84        7976 :     vector<double> forces(noa);
      85        3988 :     #pragma omp for reduction( + : at_least_one_forced)
      86             :     for(unsigned i=rank; i<ncp; i+=stride) {
      87           0 :       if(getPntrToComponent(i)->applyForce(forces)) {
      88       13212 :         at_least_one_forced+=1;
      89       13212 :         for(unsigned j=0; j<noa; j++) omp_f[j]+=forces[j];
      90             :       }
      91             :     }
      92        7976 :     #pragma omp critical
      93        7975 :     for(unsigned j=0; j<noa; j++) f[j]+=omp_f[j];
      94             :   }
      95             : 
      96        1994 :   if(noa>0&&ncp>4*cgs) { comm.Sum(&f[0],noa); comm.Sum(at_least_one_forced); }
      97             : 
      98        1994 :   if(at_least_one_forced>0) for(unsigned i=0; i<noa; ++i) getPntrToArgument(i)->addForce(f[i]);
      99        1994 : }
     100             : 
     101             : }
     102        2523 : }

Generated by: LCOV version 1.13