LCOV - code coverage report
Current view: top level - isdb - RDC.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 171 182 94.0 %
Date: 2024-10-11 08:09:47 Functions: 10 12 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       10477 : PLUMED_REGISTER_ACTION(RDC,"RDC")
     221       10419 : PLUMED_REGISTER_ACTION(RDC,"PCS")
     222             : 
     223          31 : void RDC::registerKeywords( Keywords& keys ) {
     224          31 :   componentsAreNotOptional(keys);
     225          31 :   MetainferenceBase::registerKeywords(keys);
     226          62 :   keys.addFlag("NOPBC",false,"ignore the periodic boundary conditions when calculating distances");
     227          62 :   keys.add("numbered","ATOMS","the couple of atoms involved in each of the bonds for which you wish to calculate the RDC. "
     228             :            "Keywords like ATOMS1, ATOMS2, ATOMS3,... should be listed and one dipolar coupling will be "
     229             :            "calculated for each ATOMS keyword you specify.");
     230          62 :   keys.reset_style("ATOMS","atoms");
     231          62 :   keys.add("compulsory","GYROM","1.","Add the product of the gyromagnetic constants for the bond. ");
     232          62 :   keys.add("compulsory","SCALE","1.","Add the scaling factor to take into account concentration and other effects. ");
     233          62 :   keys.addFlag("SVD",false,"Set to TRUE if you want to back calculate using Single Value Decomposition (need GSL at compilation time).");
     234          62 :   keys.add("numbered","COUPLING","Add an experimental value for each coupling (needed by SVD and useful for \\ref STATS).");
     235          62 :   keys.addOutputComponent("rdc","default","the calculated # RDC");
     236          62 :   keys.addOutputComponent("exp","SVD/COUPLING","the experimental # RDC");
     237          62 :   keys.addOutputComponent("Sxx","SVD","Tensor component");
     238          62 :   keys.addOutputComponent("Syy","SVD","Tensor component");
     239          62 :   keys.addOutputComponent("Szz","SVD","Tensor component");
     240          62 :   keys.addOutputComponent("Sxy","SVD","Tensor component");
     241          62 :   keys.addOutputComponent("Sxz","SVD","Tensor component");
     242          62 :   keys.addOutputComponent("Syz","SVD","Tensor component");
     243          31 : }
     244             : 
     245          29 : RDC::RDC(const ActionOptions&ao):
     246             :   PLUMED_METAINF_INIT(ao),
     247          29 :   Const(1.),
     248          29 :   mu_s(1.),
     249          29 :   scale(1.),
     250          29 :   pbc(true)
     251             : {
     252          29 :   bool nopbc=!pbc;
     253          29 :   parseFlag("NOPBC",nopbc);
     254          29 :   pbc=!nopbc;
     255             : 
     256             :   const double RDCConst = 0.3356806;
     257             :   const double PCSConst = 1.0;
     258             : 
     259          29 :   if( getName().find("RDC")!=std::string::npos) { Const *= RDCConst; }
     260           0 :   else if( getName().find("PCS")!=std::string::npos) { Const *= PCSConst; }
     261             : 
     262             :   // Read in the atoms
     263             :   std::vector<AtomNumber> t, atoms;
     264          29 :   for(int i=1;; ++i ) {
     265         292 :     parseAtomList("ATOMS", i, t );
     266         146 :     if( t.empty() ) break;
     267         117 :     if( t.size()!=2 ) {
     268           0 :       std::string ss; Tools::convert(i,ss);
     269           0 :       error("ATOMS" + ss + " keyword has the wrong number of atoms");
     270             :     }
     271         117 :     atoms.push_back(t[0]);
     272         117 :     atoms.push_back(t[1]);
     273         117 :     t.resize(0);
     274         117 :   }
     275             : 
     276          29 :   const unsigned ndata = atoms.size()/2;
     277             : 
     278             :   // Read in GYROMAGNETIC constants
     279          29 :   parse("GYROM", mu_s);
     280          29 :   if(mu_s==0.) error("GYROM cannot be 0");
     281             : 
     282             :   // Read in SCALING factors
     283          29 :   parse("SCALE", scale);
     284          29 :   if(scale==0.) error("SCALE cannot be 0");
     285             : 
     286          29 :   svd=false;
     287          29 :   parseFlag("SVD",svd);
     288             : #ifndef __PLUMED_HAS_GSL
     289             :   if(svd) error("You CANNOT use SVD without GSL. Recompile PLUMED with GSL!\n");
     290             : #endif
     291          29 :   if(svd&&getDoScore()) error("It is not possible to use SVD and METAINFERENCE together");
     292             : 
     293             :   // Optionally add an experimental value
     294          29 :   coupl.resize( ndata );
     295             :   unsigned ntarget=0;
     296          82 :   for(unsigned i=0; i<ndata; ++i) {
     297         138 :     if( !parseNumbered( "COUPLING", i+1, coupl[i] ) ) break;
     298          53 :     ntarget++;
     299             :   }
     300             :   bool addexp=false;
     301          29 :   if(ntarget!=ndata && ntarget!=0) error("found wrong number of COUPLING values");
     302          29 :   if(ntarget==ndata) addexp=true;
     303          29 :   if(getDoScore()&&!addexp) error("with DOSCORE you need to set the COUPLING values");
     304          29 :   if(svd&&!addexp) error("with SVD you need to set the COUPLING values");
     305             : 
     306             : 
     307             :   // Output details of all contacts
     308          29 :   log.printf("  Gyromagnetic moment is %f. Scaling factor is %f.",mu_s,scale);
     309         146 :   for(unsigned i=0; i<ndata; ++i) {
     310         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());
     311         117 :     if(addexp) log.printf(" Experimental coupling is %f.", coupl[i]);
     312         117 :     log.printf("\n");
     313             :   }
     314             : 
     315          29 :   log<<"  Bibliography "
     316          58 :      <<plumed.cite("Camilloni C, Vendruscolo M, J. Phys. Chem. B 119, 653 (2015)")
     317         116 :      <<plumed.cite("Camilloni C, Vendruscolo M, Biochemistry 54, 7470 (2015)");
     318          87 :   log<< plumed.cite("Bonomi, Camilloni, Bioinformatics, 33, 3999 (2017)") << "\n";
     319             : 
     320             : 
     321          29 :   if(!getDoScore()&&!svd) {
     322          80 :     for(unsigned i=0; i<ndata; i++) {
     323          64 :       std::string num; Tools::convert(i,num);
     324          64 :       addComponentWithDerivatives("rdc-"+num);
     325         128 :       componentIsNotPeriodic("rdc-"+num);
     326             :     }
     327          16 :     if(addexp) {
     328           0 :       for(unsigned i=0; i<ndata; i++) {
     329           0 :         std::string num; Tools::convert(i,num);
     330           0 :         addComponent("exp-"+num);
     331           0 :         componentIsNotPeriodic("exp-"+num);
     332           0 :         Value* comp=getPntrToComponent("exp-"+num);
     333           0 :         comp->set(coupl[i]);
     334             :       }
     335             :     }
     336             :   } else {
     337          66 :     for(unsigned i=0; i<ndata; i++) {
     338          53 :       std::string num; Tools::convert(i,num);
     339          53 :       addComponentWithDerivatives("rdc-"+num);
     340         106 :       componentIsNotPeriodic("rdc-"+num);
     341             :     }
     342          66 :     for(unsigned i=0; i<ndata; i++) {
     343          53 :       std::string num; Tools::convert(i,num);
     344          53 :       addComponent("exp-"+num);
     345          53 :       componentIsNotPeriodic("exp-"+num);
     346          53 :       Value* comp=getPntrToComponent("exp-"+num);
     347          53 :       comp->set(coupl[i]);
     348             :     }
     349             :   }
     350             : 
     351          29 :   if(svd) {
     352           2 :     addComponent("Sxx"); componentIsNotPeriodic("Sxx");
     353           2 :     addComponent("Syy"); componentIsNotPeriodic("Syy");
     354           2 :     addComponent("Szz"); componentIsNotPeriodic("Szz");
     355           2 :     addComponent("Sxy"); componentIsNotPeriodic("Sxy");
     356           2 :     addComponent("Sxz"); componentIsNotPeriodic("Sxz");
     357           3 :     addComponent("Syz"); componentIsNotPeriodic("Syz");
     358             :   }
     359             : 
     360          29 :   requestAtoms(atoms, false);
     361          29 :   if(getDoScore()) {
     362          12 :     setParameters(coupl);
     363          12 :     Initialise(coupl.size());
     364             :   }
     365          29 :   setDerivatives();
     366          29 :   checkRead();
     367          29 : }
     368             : 
     369           5 : void RDC::do_svd()
     370             : {
     371             : #ifdef __PLUMED_HAS_GSL
     372           5 :   gsl_vector_unique_ptr rdc_vec(gsl_vector_alloc(coupl.size())),
     373           5 :                         S(gsl_vector_alloc(5)),
     374           5 :                         Stmp(gsl_vector_alloc(5)),
     375           5 :                         work(gsl_vector_alloc(5)),
     376           5 :                         bc(gsl_vector_alloc(coupl.size()));
     377             : 
     378           5 :   gsl_matrix_unique_ptr coef_mat(gsl_matrix_alloc(coupl.size(),5)),
     379           5 :                         A(gsl_matrix_alloc(coupl.size(),5)),
     380           5 :                         V(gsl_matrix_alloc(5,5));
     381             : 
     382           5 :   gsl_matrix_set_zero(coef_mat.get());
     383           5 :   gsl_vector_set_zero(bc.get());
     384             : 
     385             :   unsigned index=0;
     386           5 :   std::vector<double> dmax(coupl.size());
     387          30 :   for(unsigned r=0; r<getNumberOfAtoms(); r+=2) {
     388          25 :     Vector  distance;
     389          25 :     if(pbc) distance = pbcDistance(getPosition(r),getPosition(r+1));
     390           0 :     else    distance = delta(getPosition(r),getPosition(r+1));
     391          25 :     double d    = distance.modulo();
     392          25 :     double d2   = d*d;
     393          25 :     double d3   = d2*d;
     394          25 :     double id3  = 1./d3;
     395          25 :     double max  = -Const*mu_s*scale;
     396          25 :     dmax[index] = id3*max;
     397          25 :     double mu_x = distance[0]/d;
     398          25 :     double mu_y = distance[1]/d;
     399          25 :     double mu_z = distance[2]/d;
     400          25 :     gsl_vector_set(rdc_vec.get(),index,coupl[index]/dmax[index]);
     401          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));
     402          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));
     403          25 :     gsl_matrix_set(coef_mat.get(),index,2,gsl_matrix_get(coef_mat.get(),index,2)+(2.0*mu_x*mu_y));
     404          25 :     gsl_matrix_set(coef_mat.get(),index,3,gsl_matrix_get(coef_mat.get(),index,3)+(2.0*mu_x*mu_z));
     405          25 :     gsl_matrix_set(coef_mat.get(),index,4,gsl_matrix_get(coef_mat.get(),index,4)+(2.0*mu_y*mu_z));
     406          25 :     index++;
     407             :   }
     408           5 :   gsl_matrix_memcpy(A.get(),coef_mat.get());
     409           5 :   gsl_linalg_SV_decomp(A.get(), V.get(), Stmp.get(), work.get());
     410           5 :   gsl_linalg_SV_solve(A.get(), V.get(), Stmp.get(), rdc_vec.get(), S.get());
     411             :   /* tensor */
     412             :   Value* tensor;
     413           5 :   tensor=getPntrToComponent("Sxx");
     414           5 :   double Sxx = gsl_vector_get(S.get(),0);
     415             :   tensor->set(Sxx);
     416           5 :   tensor=getPntrToComponent("Syy");
     417           5 :   double Syy = gsl_vector_get(S.get(),1);
     418             :   tensor->set(Syy);
     419           5 :   tensor=getPntrToComponent("Szz");
     420           5 :   double Szz = -Sxx-Syy;
     421             :   tensor->set(Szz);
     422           5 :   tensor=getPntrToComponent("Sxy");
     423           5 :   double Sxy = gsl_vector_get(S.get(),2);
     424             :   tensor->set(Sxy);
     425           5 :   tensor=getPntrToComponent("Sxz");
     426           5 :   double Sxz = gsl_vector_get(S.get(),3);
     427             :   tensor->set(Sxz);
     428           5 :   tensor=getPntrToComponent("Syz");
     429           5 :   double Syz = gsl_vector_get(S.get(),4);
     430             :   tensor->set(Syz);
     431             : 
     432           5 :   gsl_blas_dgemv(CblasNoTrans, 1.0, coef_mat.get(), S.get(), 0., bc.get());
     433          30 :   for(index=0; index<coupl.size(); index++) {
     434          25 :     double rdc = gsl_vector_get(bc.get(),index)*dmax[index];
     435          25 :     Value* val=getPntrToComponent(index);
     436             :     val->set(rdc);
     437             :   }
     438             : #endif
     439           5 : }
     440             : 
     441        2172 : void RDC::calculate()
     442             : {
     443        2172 :   if(svd) {
     444           5 :     do_svd();
     445           5 :     return;
     446             :   }
     447             : 
     448        2167 :   const double max  = -Const*scale*mu_s;
     449             :   const unsigned N=getNumberOfAtoms();
     450        2167 :   std::vector<Vector> dRDC(N/2, Vector{0.,0.,0.});
     451             : 
     452             :   /* RDC Calculations and forces */
     453        2167 :   #pragma omp parallel num_threads(OpenMP::getNumThreads())
     454             :   {
     455             :     #pragma omp for
     456             :     for(unsigned r=0; r<N; r+=2)
     457             :     {
     458             :       const unsigned index=r/2;
     459             :       Vector  distance;
     460             :       if(pbc) distance   = pbcDistance(getPosition(r),getPosition(r+1));
     461             :       else    distance   = delta(getPosition(r),getPosition(r+1));
     462             :       const double d2    = distance.modulo2();
     463             :       const double ind   = 1./std::sqrt(d2);
     464             :       const double ind2  = 1./d2;
     465             :       const double ind3  = ind2*ind;
     466             :       const double x2    = distance[0]*distance[0]*ind2;
     467             :       const double y2    = distance[1]*distance[1]*ind2;
     468             :       const double z2    = distance[2]*distance[2]*ind2;
     469             :       const double dmax  = ind3*max;
     470             :       const double ddmax = dmax*ind2;
     471             : 
     472             :       const double rdc   = 0.5*dmax*(3.*z2-1.);
     473             :       const double prod_xy = (x2+y2-4.*z2);
     474             :       const double prod_z =  (3.*x2 + 3.*y2 - 2.*z2);
     475             : 
     476             :       dRDC[index] = -1.5*ddmax*distance;
     477             :       dRDC[index][0] *= prod_xy;
     478             :       dRDC[index][1] *= prod_xy;
     479             :       dRDC[index][2] *= prod_z;
     480             : 
     481             :       std::string num; Tools::convert(index,num);
     482             :       Value* val=getPntrToComponent("rdc-"+num);
     483             :       val->set(rdc);
     484             :       if(!getDoScore()) {
     485             :         setBoxDerivatives(val, Tensor(distance,dRDC[index]));
     486             :         setAtomsDerivatives(val, r,  dRDC[index]);
     487             :         setAtomsDerivatives(val, r+1, -dRDC[index]);
     488             :       } else setCalcData(index, rdc);
     489             :     }
     490             :   }
     491             : 
     492        2167 :   if(getDoScore()) {
     493             :     /* Metainference */
     494        1824 :     Tensor dervir;
     495        1824 :     double score = getScore();
     496             :     setScore(score);
     497             : 
     498             :     /* calculate final derivatives */
     499        1824 :     Value* val=getPntrToComponent("score");
     500        9120 :     for(unsigned r=0; r<N; r+=2)
     501             :     {
     502        7296 :       const unsigned index=r/2;
     503        7296 :       Vector  distance;
     504        7296 :       if(pbc) distance   = pbcDistance(getPosition(r),getPosition(r+1));
     505           0 :       else    distance   = delta(getPosition(r),getPosition(r+1));
     506        7296 :       const Vector der = dRDC[index]*getMetaDer(index);
     507        7296 :       dervir += Tensor(distance, der);
     508        7296 :       setAtomsDerivatives(val, r,  der);
     509        7296 :       setAtomsDerivatives(val, r+1, -der);
     510             :     }
     511        1824 :     setBoxDerivatives(val, dervir);
     512             :   }
     513             : }
     514             : 
     515         327 : void RDC::update() {
     516             :   // write status file
     517         327 :   if(getWstride()>0&& (getStep()%getWstride()==0 || getCPT()) ) writeStatus();
     518         327 : }
     519             : 
     520             : }
     521             : }

Generated by: LCOV version 1.15