LCOV - code coverage report
Current view: top level - isdb - NOE.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 107 113 94.7 %
Date: 2025-03-25 09:33:27 Functions: 4 5 80.0 %

          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             : private:
      70             :   bool             pbc;
      71             :   std::vector<unsigned> nga;
      72             :   std::unique_ptr<NeighborList> nl;
      73             :   unsigned         tot_size;
      74             : public:
      75             :   static void registerKeywords( Keywords& keys );
      76             :   explicit NOE(const ActionOptions&);
      77             :   void calculate() override;
      78             :   void update() override;
      79             : };
      80             : 
      81             : PLUMED_REGISTER_ACTION(NOE,"NOE")
      82             : 
      83          19 : void NOE::registerKeywords( Keywords& keys ) {
      84          19 :   MetainferenceBase::registerKeywords(keys);
      85          19 :   keys.addFlag("NOPBC",false,"ignore the periodic boundary conditions when calculating distances");
      86          19 :   keys.add("numbered","GROUPA","the atoms involved in each of the contacts you wish to calculate. "
      87             :            "Keywords like GROUPA1, GROUPA2, GROUPA3,... should be listed and one contact will be "
      88             :            "calculated for each ATOM keyword you specify.");
      89          19 :   keys.add("numbered","GROUPB","the atoms involved in each of the contacts you wish to calculate. "
      90             :            "Keywords like GROUPB1, GROUPB2, GROUPB3,... should be listed and one contact will be "
      91             :            "calculated for each ATOM keyword you specify.");
      92          38 :   keys.reset_style("GROUPA","atoms");
      93          38 :   keys.reset_style("GROUPB","atoms");
      94          19 :   keys.add("numbered","NOEDIST","Add an experimental value for each NOE.");
      95          38 :   keys.addOutputComponent("noe","default","scalar","the # NOE");
      96          38 :   keys.addOutputComponent("exp","NOEDIST","scalar","the # NOE experimental distance");
      97          19 : }
      98             : 
      99          17 : NOE::NOE(const ActionOptions&ao):
     100             :   PLUMED_METAINF_INIT(ao),
     101          17 :   pbc(true) {
     102          17 :   bool nopbc=!pbc;
     103          17 :   parseFlag("NOPBC",nopbc);
     104          17 :   pbc=!nopbc;
     105             : 
     106             :   // Read in the atoms
     107             :   std::vector<AtomNumber> t, ga_lista, gb_lista;
     108          34 :   for(int i=1;; ++i ) {
     109         102 :     parseAtomList("GROUPA", i, t );
     110          51 :     if( t.empty() ) {
     111             :       break;
     112             :     }
     113          85 :     for(unsigned j=0; j<t.size(); j++) {
     114          51 :       ga_lista.push_back(t[j]);
     115             :     }
     116          34 :     nga.push_back(t.size());
     117          34 :     t.resize(0);
     118          34 :   }
     119             :   std::vector<unsigned> ngb;
     120          34 :   for(int i=1;; ++i ) {
     121         102 :     parseAtomList("GROUPB", i, t );
     122          51 :     if( t.empty() ) {
     123             :       break;
     124             :     }
     125          85 :     for(unsigned j=0; j<t.size(); j++) {
     126          51 :       gb_lista.push_back(t[j]);
     127             :     }
     128          34 :     ngb.push_back(t.size());
     129          34 :     if(ngb[i-1]!=nga[i-1]) {
     130           0 :       error("The same number of atoms is expected for the same GROUPA-GROUPB couple");
     131             :     }
     132          34 :     t.resize(0);
     133          34 :   }
     134          17 :   if(nga.size()!=ngb.size()) {
     135           0 :     error("There should be the same number of GROUPA and GROUPB keywords");
     136             :   }
     137             :   // Create neighbour lists
     138          34 :   nl=Tools::make_unique<NeighborList>(ga_lista,gb_lista,false,true,pbc,getPbc(),comm);
     139             : 
     140             :   // Optionally add an experimental value (like with RDCs)
     141             :   std::vector<double> noedist;
     142          17 :   noedist.resize( nga.size() );
     143             :   unsigned ntarget=0;
     144          43 :   for(unsigned i=0; i<nga.size(); ++i) {
     145          60 :     if( !parseNumbered( "NOEDIST", i+1, noedist[i] ) ) {
     146             :       break;
     147             :     }
     148          26 :     ntarget++;
     149             :   }
     150             :   bool addexp=false;
     151          17 :   if(ntarget!=nga.size() && ntarget!=0) {
     152           0 :     error("found wrong number of NOEDIST values");
     153             :   }
     154          17 :   if(ntarget==nga.size()) {
     155             :     addexp=true;
     156             :   }
     157          17 :   if(getDoScore()&&!addexp) {
     158           0 :     error("with DOSCORE you need to set the NOEDIST values");
     159             :   }
     160             : 
     161             :   // Output details of all contacts
     162             :   unsigned index=0;
     163          51 :   for(unsigned i=0; i<nga.size(); ++i) {
     164          34 :     log.printf("  The %uth NOE is calculated using %u equivalent couples of atoms\n", i, nga[i]);
     165          85 :     for(unsigned j=0; j<nga[i]; j++) {
     166          51 :       log.printf("    couple %u is %d %d.\n", j, ga_lista[index].serial(), gb_lista[index].serial() );
     167          51 :       index++;
     168             :     }
     169             :   }
     170          17 :   tot_size = index;
     171             : 
     172          17 :   if(pbc) {
     173          17 :     log.printf("  using periodic boundary conditions\n");
     174             :   } else {
     175           0 :     log.printf("  without periodic boundary conditions\n");
     176             :   }
     177             : 
     178          34 :   log << " Bibliography" << plumed.cite("Bonomi, Camilloni, Bioinformatics, 33, 3999 (2017)") << "\n";
     179             : 
     180          17 :   if(!getDoScore()) {
     181          27 :     for(unsigned i=0; i<nga.size(); i++) {
     182             :       std::string num;
     183          18 :       Tools::convert(i,num);
     184          36 :       addComponentWithDerivatives("noe-"+num);
     185          36 :       componentIsNotPeriodic("noe-"+num);
     186             :     }
     187           9 :     if(addexp) {
     188          15 :       for(unsigned i=0; i<nga.size(); i++) {
     189             :         std::string num;
     190          10 :         Tools::convert(i,num);
     191          20 :         addComponent("exp-"+num);
     192          10 :         componentIsNotPeriodic("exp-"+num);
     193          10 :         Value* comp=getPntrToComponent("exp-"+num);
     194          10 :         comp->set(noedist[i]);
     195             :       }
     196             :     }
     197             :   } else {
     198          24 :     for(unsigned i=0; i<nga.size(); i++) {
     199             :       std::string num;
     200          16 :       Tools::convert(i,num);
     201          32 :       addComponent("noe-"+num);
     202          32 :       componentIsNotPeriodic("noe-"+num);
     203             :     }
     204          24 :     for(unsigned i=0; i<nga.size(); i++) {
     205             :       std::string num;
     206          16 :       Tools::convert(i,num);
     207          32 :       addComponent("exp-"+num);
     208          16 :       componentIsNotPeriodic("exp-"+num);
     209          16 :       Value* comp=getPntrToComponent("exp-"+num);
     210          16 :       comp->set(noedist[i]);
     211             :     }
     212             :   }
     213             : 
     214          17 :   requestAtoms(nl->getFullAtomList(), false);
     215          17 :   if(getDoScore()) {
     216           8 :     setParameters(noedist);
     217           8 :     Initialise(nga.size());
     218             :   }
     219          17 :   setDerivatives();
     220          17 :   checkRead();
     221          17 : }
     222             : 
     223         528 : void NOE::calculate() {
     224         528 :   const unsigned ngasz=nga.size();
     225         528 :   std::vector<Vector> deriv(tot_size, Vector{0,0,0});
     226             : 
     227         528 :   #pragma omp parallel for num_threads(OpenMP::getNumThreads())
     228             :   for(unsigned i=0; i<ngasz; i++) {
     229             :     Tensor dervir;
     230             :     double noe=0;
     231             :     unsigned index=0;
     232             :     for(unsigned k=0; k<i; k++) {
     233             :       index+=nga[k];
     234             :     }
     235             :     std::string num;
     236             :     Tools::convert(i,num);
     237             :     Value* val=getPntrToComponent("noe-"+num);
     238             :     // cycle over equivalent atoms
     239             :     for(unsigned j=0; j<nga[i]; j++) {
     240             :       const unsigned i0=nl->getClosePair(index+j).first;
     241             :       const unsigned i1=nl->getClosePair(index+j).second;
     242             : 
     243             :       Vector distance;
     244             :       if(pbc) {
     245             :         distance=pbcDistance(getPosition(i0),getPosition(i1));
     246             :       } else {
     247             :         distance=delta(getPosition(i0),getPosition(i1));
     248             :       }
     249             : 
     250             :       const double ir2=1./distance.modulo2();
     251             :       const double ir6=ir2*ir2*ir2;
     252             :       const double ir8=6*ir6*ir2;
     253             : 
     254             :       noe += ir6;
     255             :       deriv[index+j] = ir8*distance;
     256             :       if(!getDoScore()) {
     257             :         dervir += Tensor(distance, deriv[index+j]);
     258             :         setAtomsDerivatives(val, i0,  deriv[index+j]);
     259             :         setAtomsDerivatives(val, i1, -deriv[index+j]);
     260             :       }
     261             :     }
     262             :     val->set(noe);
     263             :     if(!getDoScore()) {
     264             :       setBoxDerivatives(val, dervir);
     265             :     } else {
     266             :       setCalcData(i, noe);
     267             :     }
     268             :   }
     269             : 
     270         528 :   if(getDoScore()) {
     271             :     /* Metainference */
     272          96 :     Tensor dervir;
     273          96 :     double score = getScore();
     274          96 :     setScore(score);
     275             : 
     276             :     /* calculate final derivatives */
     277          96 :     Value* val=getPntrToComponent("score");
     278         288 :     for(unsigned i=0; i<ngasz; i++) {
     279             :       unsigned index=0;
     280         288 :       for(unsigned k=0; k<i; k++) {
     281          96 :         index+=nga[k];
     282             :       }
     283             :       // cycle over equivalent atoms
     284         480 :       for(unsigned j=0; j<nga[i]; j++) {
     285         288 :         const unsigned i0=nl->getClosePair(index+j).first;
     286         288 :         const unsigned i1=nl->getClosePair(index+j).second;
     287             : 
     288         288 :         Vector distance;
     289         288 :         if(pbc) {
     290         288 :           distance=pbcDistance(getPosition(i0),getPosition(i1));
     291             :         } else {
     292           0 :           distance=delta(getPosition(i0),getPosition(i1));
     293             :         }
     294             : 
     295         288 :         dervir += Tensor(distance,deriv[index+j]*getMetaDer(i));
     296         288 :         setAtomsDerivatives(val, i0,  deriv[index+j]*getMetaDer(i));
     297         288 :         setAtomsDerivatives(val, i1, -deriv[index+j]*getMetaDer(i));
     298             :       }
     299             :     }
     300          96 :     setBoxDerivatives(val, dervir);
     301             :   }
     302         528 : }
     303             : 
     304         204 : void NOE::update() {
     305             :   // write status file
     306         204 :   if(getWstride()>0&& (getStep()%getWstride()==0 || getCPT()) ) {
     307          17 :     writeStatus();
     308             :   }
     309         204 : }
     310             : 
     311             : }
     312             : }

Generated by: LCOV version 1.16