LCOV - code coverage report
Current view: top level - isdb - RDC.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 169 180 93.9 %
Date: 2024-10-18 14:00:25 Functions: 5 6 83.3 %

          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/Pbc.h"
      25             : 
      26             : #ifdef __PLUMED_HAS_GSL
      27             : #include <gsl/gsl_vector.h>
      28             : #include <gsl/gsl_matrix.h>
      29             : #include <gsl/gsl_linalg.h>
      30             : #include <gsl/gsl_blas.h>
      31             : #endif
      32             : 
      33             : namespace PLMD {
      34             : namespace isdb {
      35             : 
      36             : //+PLUMEDOC ISDB_COLVAR RDC
      37             : /*
      38             : Calculates the (Residual) Dipolar Coupling between two atoms.
      39             : 
      40             : The Dipolar Coupling between two nuclei depends on the \f$\theta\f$ angle between
      41             : the inter-nuclear vector and the external magnetic field.
      42             : 
      43             : \f[
      44             : D=D_{max}0.5(3\cos^2(\theta)-1)
      45             : \f]
      46             : 
      47             : where
      48             : 
      49             : \f[
      50             : D_{max}=-\mu_0\gamma_1\gamma_2h/(8\pi^3r^3)
      51             : \f]
      52             : 
      53             : that is the maximal value of the dipolar coupling for the two nuclear spins with gyromagnetic ratio \f$\gamma\f$.
      54             : \f$\mu\f$ is the magnetic constant and h is the Planck constant.
      55             : 
      56             : Common Gyromagnetic Ratios (C.G.S)
      57             : - H(1) 26.7513
      58             : - C(13) 6.7261
      59             : - N(15) -2.7116
      60             : and their products (this is what is given in input using the keyword GYROM)
      61             : - N-H -72.5388
      62             : - C-H 179.9319
      63             : - C-N -18.2385
      64             : - C-C 45.2404
      65             : 
      66             : In isotropic media DCs average to zero because of the rotational
      67             : averaging, but when the rotational symmetry is broken, either through the introduction of an alignment medium or for molecules
      68             : with highly anisotropic paramagnetic susceptibility, then the average of the DCs is not zero and it is possible to measure a Residual Dipolar Coupling (RDCs).
      69             : 
      70             : This collective variable calculates the Dipolar Coupling for a set of couple of atoms using
      71             : the above definition.
      72             : 
      73             : In a standard MD simulation the average over time of the DC should then be zero. If one wants to model the meaning of a set of measured RDCs it is possible to try to solve the following problem: "what is the distribution of structures and orientations that reproduce the measured RDCs".
      74             : 
      75             : This collective variable can then be use to break the rotational symmetry of a simulation by imposing that the average of the DCs over the conformational ensemble must be equal to the measured RDCs \cite Camilloni:2015ka . Since measured RDCs are also a function of the fraction of aligned molecules in the sample it is better to compare them modulo a constant or looking at the correlation.
      76             : 
      77             : Alternatively if the molecule is rigid it is possible to use the experimental data to calculate the alignment tensor and the use that to back calculate the RDCs, this is what is usually call the Single Value Decomposition approach. In this case the code rely on the
      78             : a set of function from the GNU Scientific Library (GSL). (With SVD forces are not currently implemented).
      79             : 
      80             : Replica-Averaged simulations can be performed using RDCs, \ref ENSEMBLE, \ref STATS and \ref RESTRAINT .
      81             : \ref METAINFERENCE can be activated using DOSCORE and the other relevant keywords.
      82             : 
      83             : Additional material and examples can be also found in the tutorial \ref isdb-1
      84             : 
      85             : \par Examples
      86             : In the following example five N-H RDCs are defined and averaged over multiple replicas,
      87             : their correlation is then calculated with respect to a set of experimental data and restrained.
      88             : In addition, and only for analysis purposes, the same RDCs each single conformation are calculated
      89             : using a Single Value Decomposition algorithm, then averaged and again compared with the experimental data.
      90             : 
      91             : \plumedfile
      92             : #SETTINGS NREPLICAS=2
      93             : RDC ...
      94             : GYROM=-72.5388
      95             : SCALE=0.001
      96             : ATOMS1=20,21
      97             : ATOMS2=37,38
      98             : ATOMS3=56,57
      99             : ATOMS4=76,77
     100             : ATOMS5=92,93
     101             : LABEL=nh
     102             : ... RDC
     103             : 
     104             : erdc: ENSEMBLE ARG=nh.*
     105             : 
     106             : st: STATS ARG=erdc.* PARAMETERS=8.17,-8.271,-10.489,-9.871,-9.152
     107             : 
     108             : rdce: RESTRAINT ARG=st.corr KAPPA=0. SLOPE=-25000.0 AT=1.
     109             : 
     110             : RDC ...
     111             : GYROM=-72.5388
     112             : SVD
     113             : ATOMS1=20,21 COUPLING1=8.17
     114             : ATOMS2=37,38 COUPLING2=-8.271
     115             : ATOMS3=56,57 COUPLING3=-10.489
     116             : ATOMS4=76,77 COUPLING4=-9.871
     117             : ATOMS5=92,93 COUPLING5=-9.152
     118             : LABEL=svd
     119             : ... RDC
     120             : 
     121             : esvd: ENSEMBLE ARG=(svd\.rdc-.*)
     122             : 
     123             : st_svd: STATS ARG=esvd.* PARAMETERS=8.17,-8.271,-10.489,-9.871,-9.152
     124             : 
     125             : PRINT ARG=st.corr,st_svd.corr,rdce.bias FILE=colvar
     126             : \endplumedfile
     127             : 
     128             : */
     129             : //+ENDPLUMEDOC
     130             : 
     131             : //+PLUMEDOC ISDB_COLVAR PCS
     132             : /*
     133             : Calculates the Pseudo-contact shift of a nucleus determined by the presence of a metal ion susceptible to anisotropic magnetization.
     134             : 
     135             : The PCS of an atomic nucleus depends on the \f$\theta\f$ angle between the vector from the spin-label to the nucleus
     136             :  and the external magnetic field and the module of the vector itself \cite Camilloni:2015jf . While in principle the averaging
     137             : resulting from the tumbling should remove the pseudo-contact shift, in presence of the NMR magnetic field the magnetically anisotropic molecule bound to system will break the rotational symmetry does resulting in measurable values for the PCS and RDC.
     138             : 
     139             : PCS values can also be calculated using a Single Value Decomposition approach, in this case the code rely on the
     140             : a set of function from the GNU Scientific Library (GSL). (With SVD forces are not currently implemented).
     141             : 
     142             : Replica-Averaged simulations can be performed using PCS values, \ref ENSEMBLE, \ref STATS and \ref RESTRAINT .
     143             : Metainference simulations can be performed with this CV and \ref METAINFERENCE .
     144             : 
     145             : \par Examples
     146             : 
     147             : In the following example five PCS values are defined and their correlation with
     148             : respect to a set of experimental data is calculated and restrained. In addition,
     149             : and only for analysis purposes, the same PCS values are calculated using a Single Value
     150             : Decomposition algorithm.
     151             : 
     152             : \plumedfile
     153             : PCS ...
     154             : ATOMS1=20,21
     155             : ATOMS2=20,38
     156             : ATOMS3=20,57
     157             : ATOMS4=20,77
     158             : ATOMS5=20,93
     159             : LABEL=nh
     160             : ... PCS
     161             : 
     162             : enh: ENSEMBLE ARG=nh.*
     163             : 
     164             : st: STATS ARG=enh.* PARAMETERS=8.17,-8.271,-10.489,-9.871,-9.152
     165             : 
     166             : pcse: RESTRAINT ARG=st.corr KAPPA=0. SLOPE=-25000.0 AT=1.
     167             : 
     168             : PRINT ARG=st.corr,pcse.bias FILE=colvar
     169             : \endplumedfile
     170             : 
     171             : */
     172             : //+ENDPLUMEDOC
     173             : 
     174             : class RDC :
     175             :   public MetainferenceBase
     176             : {
     177             : private:
     178             :   double         Const;
     179             :   double         mu_s;
     180             :   double         scale;
     181             :   std::vector<double> coupl;
     182             :   bool           svd;
     183             :   bool           pbc;
     184             : 
     185             : #ifdef __PLUMED_HAS_GSL
     186             : /// Auxiliary class to delete a gsl_vector.
     187             : /// If used somewhere else we can move it.
     188             :   struct gsl_vector_deleter {
     189             :     void operator()(gsl_vector* p) {
     190          25 :       gsl_vector_free(p);
     191          25 :     }
     192             :   };
     193             : 
     194             : /// unique_ptr to a gsl_vector.
     195             : /// Gets deleted when going out of scope.
     196             :   typedef std::unique_ptr<gsl_vector,gsl_vector_deleter> gsl_vector_unique_ptr;
     197             : 
     198             : /// Auxiliary class to delete a gsl_matrix.
     199             : /// If used somewhere else we can move it.
     200             :   struct gsl_matrix_deleter {
     201             :     void operator()(gsl_matrix* p) {
     202          15 :       gsl_matrix_free(p);
     203          15 :     }
     204             :   };
     205             : 
     206             : /// unique_ptr to a gsl_matrix.
     207             : /// Gets deleted when going out of scope.
     208             :   typedef std::unique_ptr<gsl_matrix,gsl_matrix_deleter> gsl_matrix_unique_ptr;
     209             : #endif
     210             : 
     211             : 
     212             :   void do_svd();
     213             : public:
     214             :   explicit RDC(const ActionOptions&);
     215             :   static void registerKeywords( Keywords& keys );
     216             :   void calculate() override;
     217             :   void update() override;
     218             : };
     219             : 
     220             : PLUMED_REGISTER_ACTION(RDC,"RDC")
     221             : PLUMED_REGISTER_ACTION(RDC,"PCS")
     222             : 
     223          33 : void RDC::registerKeywords( Keywords& keys ) {
     224          33 :   MetainferenceBase::registerKeywords(keys);
     225          66 :   keys.addFlag("NOPBC",false,"ignore the periodic boundary conditions when calculating distances");
     226          66 :   keys.add("numbered","ATOMS","the couple of atoms involved in each of the bonds for which you wish to calculate the RDC. "
     227             :            "Keywords like ATOMS1, ATOMS2, ATOMS3,... should be listed and one dipolar coupling will be "
     228             :            "calculated for each ATOMS keyword you specify.");
     229          66 :   keys.reset_style("ATOMS","atoms");
     230          66 :   keys.add("compulsory","GYROM","1.","Add the product of the gyromagnetic constants for the bond. ");
     231          66 :   keys.add("compulsory","SCALE","1.","Add the scaling factor to take into account concentration and other effects. ");
     232          66 :   keys.addFlag("SVD",false,"Set to TRUE if you want to back calculate using Single Value Decomposition (need GSL at compilation time).");
     233          66 :   keys.add("numbered","COUPLING","Add an experimental value for each coupling (needed by SVD and useful for STATS).");
     234          66 :   keys.addOutputComponent("rdc","default","the calculated # RDC");
     235          66 :   keys.addOutputComponent("exp","SVD/COUPLING","the experimental # RDC");
     236          66 :   keys.addOutputComponent("Sxx","SVD","Tensor component");
     237          66 :   keys.addOutputComponent("Syy","SVD","Tensor component");
     238          66 :   keys.addOutputComponent("Szz","SVD","Tensor component");
     239          66 :   keys.addOutputComponent("Sxy","SVD","Tensor component");
     240          66 :   keys.addOutputComponent("Sxz","SVD","Tensor component");
     241          66 :   keys.addOutputComponent("Syz","SVD","Tensor component");
     242          33 : }
     243             : 
     244          29 : RDC::RDC(const ActionOptions&ao):
     245             :   PLUMED_METAINF_INIT(ao),
     246          29 :   Const(1.),
     247          29 :   mu_s(1.),
     248          29 :   scale(1.),
     249          29 :   pbc(true)
     250             : {
     251          29 :   bool nopbc=!pbc;
     252          29 :   parseFlag("NOPBC",nopbc);
     253          29 :   pbc=!nopbc;
     254             : 
     255             :   const double RDCConst = 0.3356806;
     256             :   const double PCSConst = 1.0;
     257             : 
     258          29 :   if( getName().find("RDC")!=std::string::npos) { Const *= RDCConst; }
     259           0 :   else if( getName().find("PCS")!=std::string::npos) { Const *= PCSConst; }
     260             : 
     261             :   // Read in the atoms
     262             :   std::vector<AtomNumber> t, atoms;
     263          29 :   for(int i=1;; ++i ) {
     264         292 :     parseAtomList("ATOMS", i, t );
     265         146 :     if( t.empty() ) break;
     266         117 :     if( t.size()!=2 ) {
     267           0 :       std::string ss; Tools::convert(i,ss);
     268           0 :       error("ATOMS" + ss + " keyword has the wrong number of atoms");
     269             :     }
     270         117 :     atoms.push_back(t[0]);
     271         117 :     atoms.push_back(t[1]);
     272         117 :     t.resize(0);
     273         117 :   }
     274             : 
     275          29 :   const unsigned ndata = atoms.size()/2;
     276             : 
     277             :   // Read in GYROMAGNETIC constants
     278          29 :   parse("GYROM", mu_s);
     279          29 :   if(mu_s==0.) error("GYROM cannot be 0");
     280             : 
     281             :   // Read in SCALING factors
     282          29 :   parse("SCALE", scale);
     283          29 :   if(scale==0.) error("SCALE cannot be 0");
     284             : 
     285          29 :   svd=false;
     286          29 :   parseFlag("SVD",svd);
     287             : #ifndef __PLUMED_HAS_GSL
     288             :   if(svd) error("You CANNOT use SVD without GSL. Recompile PLUMED with GSL!\n");
     289             : #endif
     290          29 :   if(svd&&getDoScore()) error("It is not possible to use SVD and METAINFERENCE together");
     291             : 
     292             :   // Optionally add an experimental value
     293          29 :   coupl.resize( ndata );
     294             :   unsigned ntarget=0;
     295          82 :   for(unsigned i=0; i<ndata; ++i) {
     296         138 :     if( !parseNumbered( "COUPLING", i+1, coupl[i] ) ) break;
     297          53 :     ntarget++;
     298             :   }
     299             :   bool addexp=false;
     300          29 :   if(ntarget!=ndata && ntarget!=0) error("found wrong number of COUPLING values");
     301          29 :   if(ntarget==ndata) addexp=true;
     302          29 :   if(getDoScore()&&!addexp) error("with DOSCORE you need to set the COUPLING values");
     303          29 :   if(svd&&!addexp) error("with SVD you need to set the COUPLING values");
     304             : 
     305             : 
     306             :   // Output details of all contacts
     307          29 :   log.printf("  Gyromagnetic moment is %f. Scaling factor is %f.",mu_s,scale);
     308         146 :   for(unsigned i=0; i<ndata; ++i) {
     309         117 :     log.printf("  The %uth Bond Dipolar Coupling is calculated from atoms : %d %d.", i+1, atoms[2*i].serial(), atoms[2*i+1].serial());
     310         117 :     if(addexp) log.printf(" Experimental coupling is %f.", coupl[i]);
     311         117 :     log.printf("\n");
     312             :   }
     313             : 
     314          29 :   log<<"  Bibliography "
     315          58 :      <<plumed.cite("Camilloni C, Vendruscolo M, J. Phys. Chem. B 119, 653 (2015)")
     316          87 :      <<plumed.cite("Camilloni C, Vendruscolo M, Biochemistry 54, 7470 (2015)");
     317          58 :   log<< plumed.cite("Bonomi, Camilloni, Bioinformatics, 33, 3999 (2017)") << "\n";
     318             : 
     319             : 
     320          29 :   if(!getDoScore()&&!svd) {
     321          80 :     for(unsigned i=0; i<ndata; i++) {
     322          64 :       std::string num; Tools::convert(i,num);
     323         128 :       addComponentWithDerivatives("rdc-"+num);
     324         128 :       componentIsNotPeriodic("rdc-"+num);
     325             :     }
     326          16 :     if(addexp) {
     327           0 :       for(unsigned i=0; i<ndata; i++) {
     328           0 :         std::string num; Tools::convert(i,num);
     329           0 :         addComponent("exp-"+num);
     330           0 :         componentIsNotPeriodic("exp-"+num);
     331           0 :         Value* comp=getPntrToComponent("exp-"+num);
     332           0 :         comp->set(coupl[i]);
     333             :       }
     334             :     }
     335             :   } else {
     336          66 :     for(unsigned i=0; i<ndata; i++) {
     337          53 :       std::string num; Tools::convert(i,num);
     338         106 :       addComponentWithDerivatives("rdc-"+num);
     339         106 :       componentIsNotPeriodic("rdc-"+num);
     340             :     }
     341          66 :     for(unsigned i=0; i<ndata; i++) {
     342          53 :       std::string num; Tools::convert(i,num);
     343         106 :       addComponent("exp-"+num);
     344          53 :       componentIsNotPeriodic("exp-"+num);
     345          53 :       Value* comp=getPntrToComponent("exp-"+num);
     346          53 :       comp->set(coupl[i]);
     347             :     }
     348             :   }
     349             : 
     350          29 :   if(svd) {
     351           3 :     addComponent("Sxx"); componentIsNotPeriodic("Sxx");
     352           3 :     addComponent("Syy"); componentIsNotPeriodic("Syy");
     353           3 :     addComponent("Szz"); componentIsNotPeriodic("Szz");
     354           3 :     addComponent("Sxy"); componentIsNotPeriodic("Sxy");
     355           3 :     addComponent("Sxz"); componentIsNotPeriodic("Sxz");
     356           3 :     addComponent("Syz"); componentIsNotPeriodic("Syz");
     357             :   }
     358             : 
     359          29 :   requestAtoms(atoms, false);
     360          29 :   if(getDoScore()) {
     361          12 :     setParameters(coupl);
     362          12 :     Initialise(coupl.size());
     363             :   }
     364          29 :   setDerivatives();
     365          29 :   checkRead();
     366          29 : }
     367             : 
     368           5 : void RDC::do_svd()
     369             : {
     370             : #ifdef __PLUMED_HAS_GSL
     371           5 :   gsl_vector_unique_ptr rdc_vec(gsl_vector_alloc(coupl.size())),
     372           5 :                         S(gsl_vector_alloc(5)),
     373           5 :                         Stmp(gsl_vector_alloc(5)),
     374           5 :                         work(gsl_vector_alloc(5)),
     375           5 :                         bc(gsl_vector_alloc(coupl.size()));
     376             : 
     377           5 :   gsl_matrix_unique_ptr coef_mat(gsl_matrix_alloc(coupl.size(),5)),
     378           5 :                         A(gsl_matrix_alloc(coupl.size(),5)),
     379           5 :                         V(gsl_matrix_alloc(5,5));
     380             : 
     381           5 :   gsl_matrix_set_zero(coef_mat.get());
     382           5 :   gsl_vector_set_zero(bc.get());
     383             : 
     384             :   unsigned index=0;
     385           5 :   std::vector<double> dmax(coupl.size());
     386          30 :   for(unsigned r=0; r<getNumberOfAtoms(); r+=2) {
     387          25 :     Vector  distance;
     388          25 :     if(pbc) distance = pbcDistance(getPosition(r),getPosition(r+1));
     389           0 :     else    distance = delta(getPosition(r),getPosition(r+1));
     390          25 :     double d    = distance.modulo();
     391          25 :     double d2   = d*d;
     392          25 :     double d3   = d2*d;
     393          25 :     double id3  = 1./d3;
     394          25 :     double max  = -Const*mu_s*scale;
     395          25 :     dmax[index] = id3*max;
     396          25 :     double mu_x = distance[0]/d;
     397          25 :     double mu_y = distance[1]/d;
     398          25 :     double mu_z = distance[2]/d;
     399          25 :     gsl_vector_set(rdc_vec.get(),index,coupl[index]/dmax[index]);
     400          25 :     gsl_matrix_set(coef_mat.get(),index,0,gsl_matrix_get(coef_mat.get(),index,0)+(mu_x*mu_x-mu_z*mu_z));
     401          25 :     gsl_matrix_set(coef_mat.get(),index,1,gsl_matrix_get(coef_mat.get(),index,1)+(mu_y*mu_y-mu_z*mu_z));
     402          25 :     gsl_matrix_set(coef_mat.get(),index,2,gsl_matrix_get(coef_mat.get(),index,2)+(2.0*mu_x*mu_y));
     403          25 :     gsl_matrix_set(coef_mat.get(),index,3,gsl_matrix_get(coef_mat.get(),index,3)+(2.0*mu_x*mu_z));
     404          25 :     gsl_matrix_set(coef_mat.get(),index,4,gsl_matrix_get(coef_mat.get(),index,4)+(2.0*mu_y*mu_z));
     405          25 :     index++;
     406             :   }
     407           5 :   gsl_matrix_memcpy(A.get(),coef_mat.get());
     408           5 :   gsl_linalg_SV_decomp(A.get(), V.get(), Stmp.get(), work.get());
     409           5 :   gsl_linalg_SV_solve(A.get(), V.get(), Stmp.get(), rdc_vec.get(), S.get());
     410             :   /* tensor */
     411             :   Value* tensor;
     412           5 :   tensor=getPntrToComponent("Sxx");
     413           5 :   double Sxx = gsl_vector_get(S.get(),0);
     414             :   tensor->set(Sxx);
     415           5 :   tensor=getPntrToComponent("Syy");
     416           5 :   double Syy = gsl_vector_get(S.get(),1);
     417             :   tensor->set(Syy);
     418           5 :   tensor=getPntrToComponent("Szz");
     419           5 :   double Szz = -Sxx-Syy;
     420             :   tensor->set(Szz);
     421           5 :   tensor=getPntrToComponent("Sxy");
     422           5 :   double Sxy = gsl_vector_get(S.get(),2);
     423             :   tensor->set(Sxy);
     424           5 :   tensor=getPntrToComponent("Sxz");
     425           5 :   double Sxz = gsl_vector_get(S.get(),3);
     426             :   tensor->set(Sxz);
     427           5 :   tensor=getPntrToComponent("Syz");
     428           5 :   double Syz = gsl_vector_get(S.get(),4);
     429             :   tensor->set(Syz);
     430             : 
     431           5 :   gsl_blas_dgemv(CblasNoTrans, 1.0, coef_mat.get(), S.get(), 0., bc.get());
     432          30 :   for(index=0; index<coupl.size(); index++) {
     433          25 :     double rdc = gsl_vector_get(bc.get(),index)*dmax[index];
     434          25 :     Value* val=getPntrToComponent(index);
     435             :     val->set(rdc);
     436             :   }
     437             : #endif
     438           5 : }
     439             : 
     440        2172 : void RDC::calculate()
     441             : {
     442        2172 :   if(svd) {
     443           5 :     do_svd();
     444           5 :     return;
     445             :   }
     446             : 
     447        2167 :   const double max  = -Const*scale*mu_s;
     448             :   const unsigned N=getNumberOfAtoms();
     449        2167 :   std::vector<Vector> dRDC(N/2, Vector{0.,0.,0.});
     450             : 
     451             :   /* RDC Calculations and forces */
     452        2167 :   #pragma omp parallel num_threads(OpenMP::getNumThreads())
     453             :   {
     454             :     #pragma omp for
     455             :     for(unsigned r=0; r<N; r+=2)
     456             :     {
     457             :       const unsigned index=r/2;
     458             :       Vector  distance;
     459             :       if(pbc) distance   = pbcDistance(getPosition(r),getPosition(r+1));
     460             :       else    distance   = delta(getPosition(r),getPosition(r+1));
     461             :       const double d2    = distance.modulo2();
     462             :       const double ind   = 1./std::sqrt(d2);
     463             :       const double ind2  = 1./d2;
     464             :       const double ind3  = ind2*ind;
     465             :       const double x2    = distance[0]*distance[0]*ind2;
     466             :       const double y2    = distance[1]*distance[1]*ind2;
     467             :       const double z2    = distance[2]*distance[2]*ind2;
     468             :       const double dmax  = ind3*max;
     469             :       const double ddmax = dmax*ind2;
     470             : 
     471             :       const double rdc   = 0.5*dmax*(3.*z2-1.);
     472             :       const double prod_xy = (x2+y2-4.*z2);
     473             :       const double prod_z =  (3.*x2 + 3.*y2 - 2.*z2);
     474             : 
     475             :       dRDC[index] = -1.5*ddmax*distance;
     476             :       dRDC[index][0] *= prod_xy;
     477             :       dRDC[index][1] *= prod_xy;
     478             :       dRDC[index][2] *= prod_z;
     479             : 
     480             :       std::string num; Tools::convert(index,num);
     481             :       Value* val=getPntrToComponent("rdc-"+num);
     482             :       val->set(rdc);
     483             :       if(!getDoScore()) {
     484             :         setBoxDerivatives(val, Tensor(distance,dRDC[index]));
     485             :         setAtomsDerivatives(val, r,  dRDC[index]);
     486             :         setAtomsDerivatives(val, r+1, -dRDC[index]);
     487             :       } else setCalcData(index, rdc);
     488             :     }
     489             :   }
     490             : 
     491        2167 :   if(getDoScore()) {
     492             :     /* Metainference */
     493        1824 :     Tensor dervir;
     494        1824 :     double score = getScore();
     495        1824 :     setScore(score);
     496             : 
     497             :     /* calculate final derivatives */
     498        1824 :     Value* val=getPntrToComponent("score");
     499        9120 :     for(unsigned r=0; r<N; r+=2)
     500             :     {
     501        7296 :       const unsigned index=r/2;
     502        7296 :       Vector  distance;
     503        7296 :       if(pbc) distance   = pbcDistance(getPosition(r),getPosition(r+1));
     504           0 :       else    distance   = delta(getPosition(r),getPosition(r+1));
     505        7296 :       const Vector der = dRDC[index]*getMetaDer(index);
     506        7296 :       dervir += Tensor(distance, der);
     507        7296 :       setAtomsDerivatives(val, r,  der);
     508        7296 :       setAtomsDerivatives(val, r+1, -der);
     509             :     }
     510        1824 :     setBoxDerivatives(val, dervir);
     511             :   }
     512             : }
     513             : 
     514         327 : void RDC::update() {
     515             :   // write status file
     516         327 :   if(getWstride()>0&& (getStep()%getWstride()==0 || getCPT()) ) writeStatus();
     517         327 : }
     518             : 
     519             : }
     520             : }

Generated by: LCOV version 1.16