LCOV - code coverage report
Current view: top level - function - LocalEnsemble.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 34 36 94.4 %
Date: 2026-03-30 13:16:06 Functions: 6 7 85.7 %

          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 "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             :   unsigned ens_dim;
      79             :   unsigned narg;
      80             : public:
      81             :   explicit LocalEnsemble(const ActionOptions&);
      82             :   void     calculate() override;
      83             :   static void registerKeywords(Keywords& keys);
      84             : };
      85             : 
      86             : 
      87       13789 : PLUMED_REGISTER_ACTION(LocalEnsemble,"LOCALENSEMBLE")
      88             : 
      89           6 : void LocalEnsemble::registerKeywords(Keywords& keys) {
      90           6 :   Function::registerKeywords(keys);
      91           6 :   keys.use("ARG");
      92          12 :   keys.add("compulsory","NUM","the number of local replicas");
      93           6 :   useCustomisableComponents(keys);
      94           6 : }
      95             : 
      96           2 : LocalEnsemble::LocalEnsemble(const ActionOptions&ao):
      97             :   Action(ao),
      98             :   Function(ao),
      99           2 :   ens_dim(0) {
     100           2 :   parse("NUM",ens_dim);
     101           2 :   if(ens_dim==0) {
     102           0 :     error("NUM should be greater or equal to 1");
     103             :   }
     104             : 
     105             :   std::vector<Value*> arg;
     106             :   int oldsize=-1;
     107           8 :   for(unsigned i=1; i<=ens_dim; ++i ) {
     108             :     std::vector<Value*> larg;
     109          12 :     if(!parseArgumentList("ARG",i,larg)) {
     110             :       break;
     111             :     }
     112          18 :     for(unsigned j=0; j<larg.size(); j++) {
     113          12 :       arg.push_back(larg[j]);
     114             :     }
     115           6 :     if(oldsize!=-1&&oldsize!=static_cast<int>(larg.size())) {
     116           0 :       error("In LOCALENSEMBLE you should have the same number of arguments for each ARG keyword");
     117             :     }
     118           6 :     oldsize = larg.size();
     119           6 :     if(!larg.empty()) {
     120           6 :       log.printf("  with arguments %u: ", i);
     121          18 :       for(unsigned j=0; j<larg.size(); j++) {
     122          12 :         log.printf(" %s",larg[j]->getName().c_str());
     123             :       }
     124           6 :       log.printf("\n");
     125             :     }
     126             :   }
     127           2 :   requestArguments(arg);
     128           2 :   narg = arg.size()/ens_dim;
     129             : 
     130             :   // these are the averages
     131           6 :   for(unsigned i=0; i<narg; i++) {
     132           4 :     std::string s=getPntrToArgument(i)->getName();
     133           4 :     addComponentWithDerivatives(s);
     134           4 :     getPntrToComponent(i)->setNotPeriodic();
     135             :   }
     136             : 
     137           2 :   log.printf("  averaging over %u replicas.\n", ens_dim);
     138           2 : }
     139             : 
     140          40 : void LocalEnsemble::calculate() {
     141          40 :   const double fact = 1.0/static_cast<double>(ens_dim);
     142          40 :   #pragma omp parallel for num_threads(OpenMP::getNumThreads())
     143             :   for(unsigned i=0; i<narg; ++i) {
     144             :     double mean = 0.;
     145             :     Value* v=getPntrToComponent(i);
     146             :     for(unsigned j=0; j<ens_dim; ++j) {
     147             :       const unsigned index = j*narg+i;
     148             :       setDerivative(v, index, fact);
     149             :       mean += fact*getArgument(index);
     150             :     }
     151             :     v->set(mean);
     152             :   }
     153          40 : }
     154             : 
     155             : }
     156             : }
     157             : 
     158             : 

Generated by: LCOV version 1.16