LCOV - code coverage report
Current view: top level - isdb - NOE.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 102 104 98.1 %
Date: 2024-10-11 08:09:47 Functions: 7 8 87.5 %

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

Generated by: LCOV version 1.15