LCOV - code coverage report
Current view: top level - isdb - NOE.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 127 130 97.7 %
Date: 2021-11-18 15:22:58 Functions: 12 13 92.3 %

          Line data    Source code
       1             : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
       2             :    Copyright (c) 2014-2020 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 "MetainferenceBase.h"
      23             : #include "core/ActionRegister.h"
      24             : #include "tools/NeighborList.h"
      25             : #include "tools/Pbc.h"
      26             : 
      27             : #include <string>
      28             : #include <cmath>
      29             : #include <memory>
      30             : 
      31             : using namespace std;
      32             : 
      33             : namespace PLMD {
      34             : namespace isdb {
      35             : 
      36             : //+PLUMEDOC ISDB_COLVAR NOE
      37             : /*
      38             : Calculates NOE intensities as sums of 1/r^6, also averaging over multiple equivalent atoms
      39             :  or ambiguous NOE.
      40             : 
      41             : Each NOE is defined by two groups containing the same number of atoms, distances are
      42             : calculated in pairs, transformed in 1/r^6, summed and saved as components.
      43             : 
      44             : \f[
      45             : NOE() = (\frac{1}{N_{eq}}\sum_j^{N_{eq}} (\frac{1}{r_j^6}))
      46             : \f]
      47             : 
      48             : NOE can be used to calculate a Metainference score over one or more replicas using the intrinsic implementation
      49             : of \ref METAINFERENCE that is activated by DOSCORE.
      50             : 
      51             : \par Examples
      52             : In the following examples three noes are defined, the first is calculated based on the distances
      53             : of atom 1-2 and 3-2; the second is defined by the distance 5-7 and the third by the distances
      54             : 4-15,4-16,8-15,8-16. \ref METAINFERENCE is activated using DOSCORE.
      55             : 
      56             : \plumedfile
      57             : NOE ...
      58             : GROUPA1=1,3 GROUPB1=2,2
      59             : GROUPA2=5 GROUPB2=7
      60             : GROUPA3=4,4,8,8 GROUPB3=15,16,15,16
      61             : DOSCORE
      62             : LABEL=noes
      63             : ... NOE
      64             : 
      65             : PRINT ARG=noes.* FILE=colvar
      66             : \endplumedfile
      67             : 
      68             : */
      69             : //+ENDPLUMEDOC
      70             : 
      71          33 : class NOE :
      72             :   public MetainferenceBase
      73             : {
      74             : private:
      75             :   bool             pbc;
      76             :   vector<unsigned> nga;
      77             :   std::unique_ptr<NeighborList> nl;
      78             :   unsigned         tot_size;
      79             : public:
      80             :   static void registerKeywords( Keywords& keys );
      81             :   explicit NOE(const ActionOptions&);
      82             :   virtual void calculate();
      83             :   void update();
      84             : };
      85             : 
      86        7378 : PLUMED_REGISTER_ACTION(NOE,"NOE")
      87             : 
      88          12 : void NOE::registerKeywords( Keywords& keys ) {
      89          12 :   componentsAreNotOptional(keys);
      90          12 :   useCustomisableComponents(keys);
      91          12 :   MetainferenceBase::registerKeywords(keys);
      92          36 :   keys.addFlag("NOPBC",false,"ignore the periodic boundary conditions when calculating distances");
      93          48 :   keys.add("numbered","GROUPA","the atoms involved in each of the contacts you wish to calculate. "
      94             :            "Keywords like GROUPA1, GROUPA2, GROUPA3,... should be listed and one contact will be "
      95             :            "calculated for each ATOM keyword you specify.");
      96          48 :   keys.add("numbered","GROUPB","the atoms involved in each of the contacts you wish to calculate. "
      97             :            "Keywords like GROUPB1, GROUPB2, GROUPB3,... should be listed and one contact will be "
      98             :            "calculated for each ATOM keyword you specify.");
      99          36 :   keys.reset_style("GROUPA","atoms");
     100          36 :   keys.reset_style("GROUPB","atoms");
     101          36 :   keys.addFlag("ADDEXP",false,"Set to TRUE if you want to have fixed components with the experimental reference values.");
     102          48 :   keys.add("numbered","NOEDIST","Add an experimental value for each NOE.");
     103          48 :   keys.addOutputComponent("noe","default","the # NOE");
     104          48 :   keys.addOutputComponent("exp","ADDEXP","the # NOE experimental distance");
     105          12 : }
     106             : 
     107          11 : NOE::NOE(const ActionOptions&ao):
     108             :   PLUMED_METAINF_INIT(ao),
     109          22 :   pbc(true)
     110             : {
     111          11 :   bool nopbc=!pbc;
     112          22 :   parseFlag("NOPBC",nopbc);
     113          11 :   pbc=!nopbc;
     114             : 
     115             :   // Read in the atoms
     116             :   vector<AtomNumber> t, ga_lista, gb_lista;
     117          22 :   for(int i=1;; ++i ) {
     118          66 :     parseAtomList("GROUPA", i, t );
     119          33 :     if( t.empty() ) break;
     120         143 :     for(unsigned j=0; j<t.size(); j++) ga_lista.push_back(t[j]);
     121          44 :     nga.push_back(t.size());
     122          22 :     t.resize(0);
     123          22 :   }
     124             :   vector<unsigned> ngb;
     125          22 :   for(int i=1;; ++i ) {
     126          66 :     parseAtomList("GROUPB", i, t );
     127          33 :     if( t.empty() ) break;
     128         143 :     for(unsigned j=0; j<t.size(); j++) gb_lista.push_back(t[j]);
     129          44 :     ngb.push_back(t.size());
     130          66 :     if(ngb[i-1]!=nga[i-1]) error("The same number of atoms is expected for the same GROUPA-GROUPB couple");
     131          22 :     t.resize(0);
     132          22 :   }
     133          11 :   if(nga.size()!=ngb.size()) error("There should be the same number of GROUPA and GROUPB keywords");
     134             :   // Create neighbour lists
     135          33 :   nl.reset( new NeighborList(ga_lista,gb_lista,true,pbc,getPbc()) );
     136             : 
     137          11 :   bool addexp=false;
     138          22 :   parseFlag("ADDEXP",addexp);
     139          11 :   if(getDoScore()) addexp=true;
     140             : 
     141             :   vector<double> noedist;
     142          11 :   if(addexp) {
     143           9 :     noedist.resize( nga.size() );
     144             :     unsigned ntarget=0;
     145             : 
     146          54 :     for(unsigned i=0; i<nga.size(); ++i) {
     147          36 :       if( !parseNumbered( "NOEDIST", i+1, noedist[i] ) ) break;
     148             :       ntarget++;
     149             :     }
     150           9 :     if( ntarget!=nga.size() ) error("found wrong number of NOEDIST values");
     151             :   }
     152             : 
     153             :   // Ouput details of all contacts
     154             :   unsigned index=0;
     155          88 :   for(unsigned i=0; i<nga.size(); ++i) {
     156          44 :     log.printf("  The %uth NOE is calculated using %u equivalent couples of atoms\n", i, nga[i]);
     157          88 :     for(unsigned j=0; j<nga[i]; j++) {
     158          66 :       log.printf("    couple %u is %d %d.\n", j, ga_lista[index].serial(), gb_lista[index].serial() );
     159          33 :       index++;
     160             :     }
     161             :   }
     162          11 :   tot_size = index;
     163             : 
     164          11 :   if(pbc)      log.printf("  using periodic boundary conditions\n");
     165           0 :   else         log.printf("  without periodic boundary conditions\n");
     166             : 
     167          33 :   log << " Bibliography" << plumed.cite("Bonomi, Camilloni, Bioinformatics, 33, 3999 (2017)") << "\n";
     168             : 
     169          11 :   if(!getDoScore()) {
     170          56 :     for(unsigned i=0; i<nga.size(); i++) {
     171          14 :       string num; Tools::convert(i,num);
     172          28 :       addComponentWithDerivatives("noe_"+num);
     173          28 :       componentIsNotPeriodic("noe_"+num);
     174             :     }
     175           7 :     if(addexp) {
     176          40 :       for(unsigned i=0; i<nga.size(); i++) {
     177          10 :         string num; Tools::convert(i,num);
     178          20 :         addComponent("exp_"+num);
     179          20 :         componentIsNotPeriodic("exp_"+num);
     180          20 :         Value* comp=getPntrToComponent("exp_"+num);
     181          10 :         comp->set(noedist[i]);
     182             :       }
     183             :     }
     184             :   } else {
     185          32 :     for(unsigned i=0; i<nga.size(); i++) {
     186           8 :       string num; Tools::convert(i,num);
     187          16 :       addComponent("noe_"+num);
     188          16 :       componentIsNotPeriodic("noe_"+num);
     189             :     }
     190          32 :     for(unsigned i=0; i<nga.size(); i++) {
     191           8 :       string num; Tools::convert(i,num);
     192          16 :       addComponent("exp_"+num);
     193          16 :       componentIsNotPeriodic("exp_"+num);
     194          16 :       Value* comp=getPntrToComponent("exp_"+num);
     195           8 :       comp->set(noedist[i]);
     196             :     }
     197             :   }
     198             : 
     199          11 :   requestAtoms(nl->getFullAtomList(), false);
     200          11 :   if(getDoScore()) {
     201           4 :     setParameters(noedist);
     202           4 :     Initialise(nga.size());
     203             :   }
     204          11 :   setDerivatives();
     205          11 :   checkRead();
     206          11 : }
     207             : 
     208         456 : void NOE::calculate()
     209             : {
     210         456 :   const unsigned ngasz=nga.size();
     211         456 :   vector<Vector> deriv(tot_size, Vector{0,0,0});
     212             : 
     213        1368 :   #pragma omp parallel for num_threads(OpenMP::getNumThreads())
     214             :   for(unsigned i=0; i<ngasz; i++) {
     215         912 :     Tensor dervir;
     216             :     double noe=0;
     217             :     unsigned index=0;
     218        2280 :     for(unsigned k=0; k<i; k++) index+=nga[k];
     219         912 :     string num; Tools::convert(i,num);
     220        1824 :     Value* val=getPntrToComponent("noe_"+num);
     221             :     // cycle over equivalent atoms
     222        5928 :     for(unsigned j=0; j<nga[i]; j++) {
     223        1368 :       const unsigned i0=nl->getClosePair(index+j).first;
     224        1368 :       const unsigned i1=nl->getClosePair(index+j).second;
     225             : 
     226        1368 :       Vector distance;
     227        5472 :       if(pbc) distance=pbcDistance(getPosition(i0),getPosition(i1));
     228           0 :       else    distance=delta(getPosition(i0),getPosition(i1));
     229             : 
     230        1368 :       const double ir2=1./distance.modulo2();
     231        1368 :       const double ir6=ir2*ir2*ir2;
     232        1368 :       const double ir8=6*ir6*ir2;
     233             : 
     234        1368 :       noe += ir6;
     235        2736 :       deriv[index+j] = ir8*distance;
     236        1368 :       if(!getDoScore()) {
     237        2448 :         dervir += Tensor(distance, deriv[index+j]);
     238        2448 :         setAtomsDerivatives(val, i0,  deriv[index+j]);
     239        2448 :         setAtomsDerivatives(val, i1, -deriv[index+j]);
     240             :       }
     241             :     }
     242             :     val->set(noe);
     243         912 :     if(!getDoScore()) {
     244         816 :       setBoxDerivatives(val, dervir);
     245             :     } else setCalcData(i, noe);
     246             :   }
     247             : 
     248         456 :   if(getDoScore()) {
     249             :     /* Metainference */
     250          48 :     Tensor dervir;
     251          48 :     double score = getScore();
     252             :     setScore(score);
     253             : 
     254             :     /* calculate final derivatives */
     255          96 :     Value* val=getPntrToComponent("score");
     256         240 :     for(unsigned i=0; i<ngasz; i++) {
     257             :       unsigned index=0;
     258         192 :       for(unsigned k=0; k<i; k++) index+=nga[k];
     259             :       // cycle over equivalent atoms
     260         624 :       for(unsigned j=0; j<nga[i]; j++) {
     261         144 :         const unsigned i0=nl->getClosePair(index+j).first;
     262         144 :         const unsigned i1=nl->getClosePair(index+j).second;
     263             : 
     264         144 :         Vector distance;
     265         432 :         if(pbc) distance=pbcDistance(getPosition(i0),getPosition(i1));
     266           0 :         else    distance=delta(getPosition(i0),getPosition(i1));
     267             : 
     268         288 :         dervir += Tensor(distance,deriv[index+j]*getMetaDer(i));
     269         144 :         setAtomsDerivatives(val, i0,  deriv[index+j]*getMetaDer(i));
     270         144 :         setAtomsDerivatives(val, i1, -deriv[index+j]*getMetaDer(i));
     271             :       }
     272             :     }
     273          48 :     setBoxDerivatives(val, dervir);
     274             :   }
     275         456 : }
     276             : 
     277         132 : void NOE::update() {
     278             :   // write status file
     279         264 :   if(getWstride()>0&& (getStep()%getWstride()==0 || getCPT()) ) writeStatus();
     280         132 : }
     281             : 
     282             : }
     283        5517 : }

Generated by: LCOV version 1.14