LCOV - code coverage report
Current view: top level - function - LocalEnsemble.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 31 33 93.9 %
Date: 2024-10-18 14:00:25 Functions: 3 5 60.0 %

          Line data    Source code
       1             : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
       2             :    Copyright (c) 2016-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 "core/ActionRegister.h"
      24             : #include "tools/OpenMP.h"
      25             : 
      26             : namespace PLMD {
      27             : namespace function {
      28             : 
      29             : //+PLUMEDOC FUNCTION LOCALENSEMBLE
      30             : /*
      31             : Calculates the average over multiple arguments.
      32             : 
      33             : If more than one collective variable is given for each argument then they
      34             : are averaged separately. The average is stored in a component labelled <em>label</em>.cvlabel.
      35             : 
      36             : \par Examples
      37             : 
      38             : The following input tells plumed to calculate the chemical shifts for four
      39             : different proteins in the same simulation box then average them, calculated
      40             : the sum of the squared deviation with respect to the experimental values and
      41             : applies a linear restraint.
      42             : \plumedfile
      43             : MOLINFO STRUCTURE=data/template.pdb
      44             : 
      45             : chaina: GROUP ATOMS=1-1640
      46             : chainb: GROUP ATOMS=1641-3280
      47             : chainc: GROUP ATOMS=3281-4920
      48             : chaind: GROUP ATOMS=4921-6560
      49             : 
      50             : WHOLEMOLECULES ENTITY0=chaina ENTITY1=chainb ENTITY2=chainc ENTITY3=chaind
      51             : 
      52             : csa: CS2BACKBONE ATOMS=chaina NRES=100 DATA=data/ TEMPLATE=chaina.pdb NOPBC
      53             : csb: CS2BACKBONE ATOMS=chainb NRES=100 DATA=data/ TEMPLATE=chainb.pdb NOPBC
      54             : csc: CS2BACKBONE ATOMS=chainc NRES=100 DATA=data/ TEMPLATE=chainc.pdb NOPBC
      55             : csd: CS2BACKBONE ATOMS=chaind NRES=100 DATA=data/ TEMPLATE=chaind.pdb NOPBC
      56             : 
      57             : ensca: LOCALENSEMBLE NUM=4 ARG1=(csa\.ca_.*) ARG2=(csb\.ca_.*) ARG3=(csc\.ca_.*) ARG4=(csd\.ca_.*)
      58             : enscb: LOCALENSEMBLE NUM=4 ARG1=(csa\.cb_.*) ARG2=(csb\.cb_.*) ARG3=(csc\.cb_.*) ARG4=(csd\.cb_.*)
      59             : ensco: LOCALENSEMBLE NUM=4 ARG1=(csa\.co_.*) ARG2=(csb\.co_.*) ARG3=(csc\.co_.*) ARG4=(csd\.co_.*)
      60             : enshn: LOCALENSEMBLE NUM=4 ARG1=(csa\.hn_.*) ARG2=(csb\.hn_.*) ARG3=(csc\.hn_.*) ARG4=(csd\.hn_.*)
      61             : ensnh: LOCALENSEMBLE NUM=4 ARG1=(csa\.nh_.*) ARG2=(csb\.nh_.*) ARG3=(csc\.nh_.*) ARG4=(csd\.nh_.*)
      62             : 
      63             : stca: STATS ARG=(ensca\.csa\.ca_.*) PARARG=(csa\.expca_.*) SQDEVSUM
      64             : stcb: STATS ARG=(enscb\.csa\.cb_.*) PARARG=(csa\.expcb_.*) SQDEVSUM
      65             : stco: STATS ARG=(ensco\.csa\.co_.*) PARARG=(csa\.expco_.*) SQDEVSUM
      66             : sthn: STATS ARG=(enshn\.csa\.hn_.*) PARARG=(csa\.exphn_.*) SQDEVSUM
      67             : stnh: STATS ARG=(ensnh\.csa\.nh_.*) PARARG=(csa\.expnh_.*) SQDEVSUM
      68             : 
      69             : res: RESTRAINT ARG=stca.*,stcb.*,stco.*,sthn.*,stnh.* AT=0.,0.,0.,0.,0. KAPPA=0.,0.,0.,0.,0 SLOPE=16.,16.,12.,24.,0.5
      70             : \endplumedfile
      71             : 
      72             : */
      73             : //+ENDPLUMEDOC
      74             : 
      75             : 
      76             : class LocalEnsemble :
      77             :   public Function
      78             : {
      79             :   unsigned ens_dim;
      80             :   unsigned narg;
      81             : public:
      82             :   explicit LocalEnsemble(const ActionOptions&);
      83             :   void     calculate() override;
      84             :   static void registerKeywords(Keywords& keys);
      85             :   std::string getOutputComponentDescription( const std::string& cname, const Keywords& keys ) const override ;
      86             : };
      87             : 
      88             : 
      89             : PLUMED_REGISTER_ACTION(LocalEnsemble,"LOCALENSEMBLE")
      90             : 
      91           4 : void LocalEnsemble::registerKeywords(Keywords& keys) {
      92           4 :   Function::registerKeywords(keys);
      93           4 :   keys.use("ARG");
      94           8 :   keys.add("compulsory","NUM","the number of local replicas");
      95           4 :   useCustomisableComponents(keys);
      96           4 : }
      97             : 
      98           2 : LocalEnsemble::LocalEnsemble(const ActionOptions&ao):
      99             :   Action(ao),
     100             :   Function(ao),
     101           2 :   ens_dim(0)
     102             : {
     103           2 :   parse("NUM",ens_dim);
     104           2 :   if(ens_dim==0) error("NUM should be greater or equal to 1");
     105             : 
     106             :   std::vector<Value*> arg;
     107             :   int oldsize=-1;
     108           8 :   for(unsigned i=1; i<=ens_dim; ++i ) {
     109             :     std::vector<Value*> larg;
     110          12 :     if(!parseArgumentList("ARG",i,larg)) break;
     111          18 :     for(unsigned j=0; j<larg.size(); j++) arg.push_back(larg[j]);
     112           6 :     if(oldsize!=-1&&oldsize!=static_cast<int>(larg.size())) error("In LOCALENSEMBLE you should have the same number of arguments for each ARG keyword");
     113           6 :     oldsize = larg.size();
     114           6 :     if(!larg.empty()) {
     115           6 :       log.printf("  with arguments %u: ", i);
     116          18 :       for(unsigned j=0; j<larg.size(); j++) log.printf(" %s",larg[j]->getName().c_str());
     117           6 :       log.printf("\n");
     118             :     }
     119             :   }
     120           2 :   requestArguments(arg);
     121           2 :   narg = arg.size()/ens_dim;
     122             : 
     123             :   // these are the averages
     124           6 :   for(unsigned i=0; i<narg; i++) {
     125           4 :     std::string s=getPntrToArgument(i)->getName();
     126           4 :     addComponentWithDerivatives(s);
     127           4 :     getPntrToComponent(i)->setNotPeriodic();
     128             :   }
     129             : 
     130           2 :   log.printf("  averaging over %u replicas.\n", ens_dim);
     131           2 : }
     132             : 
     133           0 : std::string LocalEnsemble::getOutputComponentDescription( const std::string& cname, const Keywords& keys ) const {
     134           0 :   return "the average for argument named " + cname;
     135             : }
     136             : 
     137          40 : void LocalEnsemble::calculate()
     138             : {
     139          40 :   const double fact = 1.0/static_cast<double>(ens_dim);
     140          40 :   #pragma omp parallel for num_threads(OpenMP::getNumThreads())
     141             :   for(unsigned i=0; i<narg; ++i) {
     142             :     double mean = 0.;
     143             :     Value* v=getPntrToComponent(i);
     144             :     for(unsigned j=0; j<ens_dim; ++j) {
     145             :       const unsigned index = j*narg+i;
     146             :       setDerivative(v, index, fact);
     147             :       mean += fact*getArgument(index);
     148             :     }
     149             :     v->set(mean);
     150             :   }
     151          40 : }
     152             : 
     153             : }
     154             : }
     155             : 
     156             : 

Generated by: LCOV version 1.16