Merge branch release-4-6
[alexxy/gromacs.git] / src / gromacs / gmxlib / orires.c
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5  * Copyright (c) 2001-2004, The GROMACS development team.
6  * Copyright (c) 2013,2014, by the GROMACS development team, led by
7  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
8  * and including many others, as listed in the AUTHORS file in the
9  * top-level source directory and at http://www.gromacs.org.
10  *
11  * GROMACS is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public License
13  * as published by the Free Software Foundation; either version 2.1
14  * of the License, or (at your option) any later version.
15  *
16  * GROMACS is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with GROMACS; if not, see
23  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
24  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
25  *
26  * If you want to redistribute modifications to GROMACS, please
27  * consider that scientific software is very special. Version
28  * control is crucial - bugs must be traceable. We will be happy to
29  * consider code for inclusion in the official distribution, but
30  * derived work must not be called official GROMACS. Details are found
31  * in the README & COPYING files - if they are missing, get the
32  * official version at http://www.gromacs.org.
33  *
34  * To help us fund GROMACS development, we humbly ask that you cite
35  * the research papers on the package. Check out http://www.gromacs.org.
36  */
37 #ifdef HAVE_CONFIG_H
38 #include <config.h>
39 #endif
40
41 #include "typedefs.h"
42 #include "smalloc.h"
43 #include "vec.h"
44 #include "nrjac.h"
45 #include "network.h"
46 #include "orires.h"
47 #include "do_fit.h"
48 #include "main.h"
49 #include "copyrite.h"
50 #include "pbc.h"
51 #include "mtop_util.h"
52
53 void init_orires(FILE *fplog, const gmx_mtop_t *mtop,
54                  rvec xref[],
55                  const t_inputrec *ir,
56                  const t_commrec *cr, t_oriresdata *od,
57                  t_state *state)
58 {
59     int                     i, j, d, ex, nmol, *nr_ex;
60     double                  mtot;
61     rvec                    com;
62     gmx_mtop_ilistloop_t    iloop;
63     t_ilist                *il;
64     gmx_mtop_atomloop_all_t aloop;
65     t_atom                 *atom;
66     const gmx_multisim_t   *ms;
67
68     od->nr = gmx_mtop_ftype_count(mtop, F_ORIRES);
69     if (0 == od->nr)
70     {
71         /* Not doing orientation restraints */
72         return;
73     }
74
75     if (DOMAINDECOMP(cr))
76     {
77         gmx_fatal(FARGS, "Orientation restraints do not work with more than one domain (ie. MPI rank).");
78     }
79     /* Orientation restraints */
80     if (!MASTER(cr))
81     {
82         /* Nothing to do */
83         return;
84     }
85     ms = cr->ms;
86
87     od->fc  = ir->orires_fc;
88     od->nex = 0;
89     od->S   = NULL;
90     od->M   = NULL;
91     od->eig = NULL;
92     od->v   = NULL;
93
94     nr_ex = NULL;
95
96     iloop = gmx_mtop_ilistloop_init(mtop);
97     while (gmx_mtop_ilistloop_next(iloop, &il, &nmol))
98     {
99         for (i = 0; i < il[F_ORIRES].nr; i += 3)
100         {
101             ex = mtop->ffparams.iparams[il[F_ORIRES].iatoms[i]].orires.ex;
102             if (ex >= od->nex)
103             {
104                 srenew(nr_ex, ex+1);
105                 for (j = od->nex; j < ex+1; j++)
106                 {
107                     nr_ex[j] = 0;
108                 }
109                 od->nex = ex+1;
110             }
111             nr_ex[ex]++;
112         }
113     }
114     snew(od->S, od->nex);
115     /* When not doing time averaging, the instaneous and time averaged data
116      * are indentical and the pointers can point to the same memory.
117      */
118     snew(od->Dinsl, od->nr);
119     if (ms)
120     {
121         snew(od->Dins, od->nr);
122     }
123     else
124     {
125         od->Dins = od->Dinsl;
126     }
127
128     if (ir->orires_tau == 0)
129     {
130         od->Dtav  = od->Dins;
131         od->edt   = 0.0;
132         od->edt_1 = 1.0;
133     }
134     else
135     {
136         snew(od->Dtav, od->nr);
137         od->edt   = exp(-ir->delta_t/ir->orires_tau);
138         od->edt_1 = 1.0 - od->edt;
139
140         /* Extend the state with the orires history */
141         state->flags           |= (1<<estORIRE_INITF);
142         state->hist.orire_initf = 1;
143         state->flags           |= (1<<estORIRE_DTAV);
144         state->hist.norire_Dtav = od->nr*5;
145         snew(state->hist.orire_Dtav, state->hist.norire_Dtav);
146     }
147
148     snew(od->oinsl, od->nr);
149     if (ms)
150     {
151         snew(od->oins, od->nr);
152     }
153     else
154     {
155         od->oins = od->oinsl;
156     }
157     if (ir->orires_tau == 0)
158     {
159         od->otav = od->oins;
160     }
161     else
162     {
163         snew(od->otav, od->nr);
164     }
165     snew(od->tmp, od->nex);
166     snew(od->TMP, od->nex);
167     for (ex = 0; ex < od->nex; ex++)
168     {
169         snew(od->TMP[ex], 5);
170         for (i = 0; i < 5; i++)
171         {
172             snew(od->TMP[ex][i], 5);
173         }
174     }
175
176     od->nref = 0;
177     for (i = 0; i < mtop->natoms; i++)
178     {
179         if (ggrpnr(&mtop->groups, egcORFIT, i) == 0)
180         {
181             od->nref++;
182         }
183     }
184     snew(od->mref, od->nref);
185     snew(od->xref, od->nref);
186     snew(od->xtmp, od->nref);
187
188     snew(od->eig, od->nex*12);
189
190     /* Determine the reference structure on the master node.
191      * Copy it to the other nodes after checking multi compatibility,
192      * so we are sure the subsystems match before copying.
193      */
194     clear_rvec(com);
195     mtot  = 0.0;
196     j     = 0;
197     aloop = gmx_mtop_atomloop_all_init(mtop);
198     while (gmx_mtop_atomloop_all_next(aloop, &i, &atom))
199     {
200         if (mtop->groups.grpnr[egcORFIT] == NULL ||
201             mtop->groups.grpnr[egcORFIT][i] == 0)
202         {
203             /* Not correct for free-energy with changing masses */
204             od->mref[j] = atom->m;
205             if (ms == NULL || MASTERSIM(ms))
206             {
207                 copy_rvec(xref[i], od->xref[j]);
208                 for (d = 0; d < DIM; d++)
209                 {
210                     com[d] += od->mref[j]*xref[i][d];
211                 }
212             }
213             mtot += od->mref[j];
214             j++;
215         }
216     }
217     svmul(1.0/mtot, com, com);
218     if (ms == NULL || MASTERSIM(ms))
219     {
220         for (j = 0; j < od->nref; j++)
221         {
222             rvec_dec(od->xref[j], com);
223         }
224     }
225
226     fprintf(fplog, "Found %d orientation experiments\n", od->nex);
227     for (i = 0; i < od->nex; i++)
228     {
229         fprintf(fplog, "  experiment %d has %d restraints\n", i+1, nr_ex[i]);
230     }
231
232     sfree(nr_ex);
233
234     fprintf(fplog, "  the fit group consists of %d atoms and has total mass %g\n",
235             od->nref, mtot);
236
237     if (ms)
238     {
239         fprintf(fplog, "  the orientation restraints are ensemble averaged over %d systems\n", ms->nsim);
240
241         check_multi_int(fplog, ms, od->nr,
242                         "the number of orientation restraints",
243                         FALSE);
244         check_multi_int(fplog, ms, od->nref,
245                         "the number of fit atoms for orientation restraining",
246                         FALSE);
247         check_multi_int(fplog, ms, ir->nsteps, "nsteps", FALSE);
248         /* Copy the reference coordinates from the master to the other nodes */
249         gmx_sum_sim(DIM*od->nref, od->xref[0], ms);
250     }
251
252     please_cite(fplog, "Hess2003");
253 }
254
255 void diagonalize_orires_tensors(t_oriresdata *od)
256 {
257     int           ex, i, j, nrot, ord[DIM], t;
258     matrix        S, TMP;
259
260     if (od->M == NULL)
261     {
262         snew(od->M, DIM);
263         for (i = 0; i < DIM; i++)
264         {
265             snew(od->M[i], DIM);
266         }
267         snew(od->eig_diag, DIM);
268         snew(od->v, DIM);
269         for (i = 0; i < DIM; i++)
270         {
271             snew(od->v[i], DIM);
272         }
273     }
274
275     for (ex = 0; ex < od->nex; ex++)
276     {
277         /* Rotate the S tensor back to the reference frame */
278         mmul(od->R, od->S[ex], TMP);
279         mtmul(TMP, od->R, S);
280         for (i = 0; i < DIM; i++)
281         {
282             for (j = 0; j < DIM; j++)
283             {
284                 od->M[i][j] = S[i][j];
285             }
286         }
287
288         jacobi(od->M, DIM, od->eig_diag, od->v, &nrot);
289
290         for (i = 0; i < DIM; i++)
291         {
292             ord[i] = i;
293         }
294         for (i = 0; i < DIM; i++)
295         {
296             for (j = i+1; j < DIM; j++)
297             {
298                 if (sqr(od->eig_diag[ord[j]]) > sqr(od->eig_diag[ord[i]]))
299                 {
300                     t      = ord[i];
301                     ord[i] = ord[j];
302                     ord[j] = t;
303                 }
304             }
305         }
306
307         for (i = 0; i < DIM; i++)
308         {
309             od->eig[ex*12 + i] = od->eig_diag[ord[i]];
310         }
311         for (i = 0; i < DIM; i++)
312         {
313             for (j = 0; j < DIM; j++)
314             {
315                 od->eig[ex*12 + 3 + 3*i + j] = od->v[j][ord[i]];
316             }
317         }
318     }
319 }
320
321 void print_orires_log(FILE *log, t_oriresdata *od)
322 {
323     int   ex, i;
324     real *eig;
325
326     diagonalize_orires_tensors(od);
327
328     for (ex = 0; ex < od->nex; ex++)
329     {
330         eig = od->eig + ex*12;
331         fprintf(log, "  Orientation experiment %d:\n", ex+1);
332         fprintf(log, "    order parameter: %g\n", eig[0]);
333         for (i = 0; i < DIM; i++)
334         {
335             fprintf(log, "    eig: %6.3f   %6.3f %6.3f %6.3f\n",
336                     (eig[0] != 0) ? eig[i]/eig[0] : eig[i],
337                     eig[DIM+i*DIM+XX],
338                     eig[DIM+i*DIM+YY],
339                     eig[DIM+i*DIM+ZZ]);
340         }
341         fprintf(log, "\n");
342     }
343 }
344
345 real calc_orires_dev(const gmx_multisim_t *ms,
346                      int nfa, const t_iatom forceatoms[], const t_iparams ip[],
347                      const t_mdatoms *md, const rvec x[], const t_pbc *pbc,
348                      t_fcdata *fcd, history_t *hist)
349 {
350     int              fa, d, i, j, type, ex, nref;
351     real             edt, edt_1, invn, pfac, r2, invr, corrfac, weight, wsv2, sw, dev;
352     tensor          *S, R, TMP;
353     rvec5           *Dinsl, *Dins, *Dtav, *rhs;
354     real            *mref, ***T;
355     double           mtot;
356     rvec            *xref, *xtmp, com, r_unrot, r;
357     t_oriresdata    *od;
358     gmx_bool         bTAV;
359     const real       two_thr = 2.0/3.0;
360
361     od = &(fcd->orires);
362
363     if (od->nr == 0)
364     {
365         /* This means that this is not the master node */
366         gmx_fatal(FARGS, "Orientation restraints are only supported on the master node, use less processors");
367     }
368
369     bTAV  = (od->edt != 0);
370     edt   = od->edt;
371     edt_1 = od->edt_1;
372     S     = od->S;
373     Dinsl = od->Dinsl;
374     Dins  = od->Dins;
375     Dtav  = od->Dtav;
376     T     = od->TMP;
377     rhs   = od->tmp;
378     nref  = od->nref;
379     mref  = od->mref;
380     xref  = od->xref;
381     xtmp  = od->xtmp;
382
383     if (bTAV)
384     {
385         od->exp_min_t_tau = hist->orire_initf*edt;
386
387         /* Correction factor to correct for the lack of history
388          * at short times.
389          */
390         corrfac = 1.0/(1.0 - od->exp_min_t_tau);
391     }
392     else
393     {
394         corrfac = 1.0;
395     }
396
397     if (ms)
398     {
399         invn = 1.0/ms->nsim;
400     }
401     else
402     {
403         invn = 1.0;
404     }
405
406     clear_rvec(com);
407     mtot = 0;
408     j    = 0;
409     for (i = 0; i < md->nr; i++)
410     {
411         if (md->cORF[i] == 0)
412         {
413             copy_rvec(x[i], xtmp[j]);
414             mref[j] = md->massT[i];
415             for (d = 0; d < DIM; d++)
416             {
417                 com[d] += mref[j]*xref[j][d];
418             }
419             mtot += mref[j];
420             j++;
421         }
422     }
423     svmul(1.0/mtot, com, com);
424     for (j = 0; j < nref; j++)
425     {
426         rvec_dec(xtmp[j], com);
427     }
428     /* Calculate the rotation matrix to rotate x to the reference orientation */
429     calc_fit_R(DIM, nref, mref, xref, xtmp, R);
430     copy_mat(R, od->R);
431
432     d = 0;
433     for (fa = 0; fa < nfa; fa += 3)
434     {
435         type = forceatoms[fa];
436         if (pbc)
437         {
438             pbc_dx_aiuc(pbc, x[forceatoms[fa+1]], x[forceatoms[fa+2]], r_unrot);
439         }
440         else
441         {
442             rvec_sub(x[forceatoms[fa+1]], x[forceatoms[fa+2]], r_unrot);
443         }
444         mvmul(R, r_unrot, r);
445         r2   = norm2(r);
446         invr = gmx_invsqrt(r2);
447         /* Calculate the prefactor for the D tensor, this includes the factor 3! */
448         pfac = ip[type].orires.c*invr*invr*3;
449         for (i = 0; i < ip[type].orires.power; i++)
450         {
451             pfac *= invr;
452         }
453         Dinsl[d][0] = pfac*(2*r[0]*r[0] + r[1]*r[1] - r2);
454         Dinsl[d][1] = pfac*(2*r[0]*r[1]);
455         Dinsl[d][2] = pfac*(2*r[0]*r[2]);
456         Dinsl[d][3] = pfac*(2*r[1]*r[1] + r[0]*r[0] - r2);
457         Dinsl[d][4] = pfac*(2*r[1]*r[2]);
458
459         if (ms)
460         {
461             for (i = 0; i < 5; i++)
462             {
463                 Dins[d][i] = Dinsl[d][i]*invn;
464             }
465         }
466
467         d++;
468     }
469
470     if (ms)
471     {
472         gmx_sum_sim(5*od->nr, Dins[0], ms);
473     }
474
475     /* Calculate the order tensor S for each experiment via optimization */
476     for (ex = 0; ex < od->nex; ex++)
477     {
478         for (i = 0; i < 5; i++)
479         {
480             rhs[ex][i] = 0;
481             for (j = 0; j <= i; j++)
482             {
483                 T[ex][i][j] = 0;
484             }
485         }
486     }
487     d = 0;
488     for (fa = 0; fa < nfa; fa += 3)
489     {
490         if (bTAV)
491         {
492             /* Here we update Dtav in t_fcdata using the data in history_t.
493              * Thus the results stay correct when this routine
494              * is called multiple times.
495              */
496             for (i = 0; i < 5; i++)
497             {
498                 Dtav[d][i] = edt*hist->orire_Dtav[d*5+i] + edt_1*Dins[d][i];
499             }
500         }
501
502         type   = forceatoms[fa];
503         ex     = ip[type].orires.ex;
504         weight = ip[type].orires.kfac;
505         /* Calculate the vector rhs and half the matrix T for the 5 equations */
506         for (i = 0; i < 5; i++)
507         {
508             rhs[ex][i] += Dtav[d][i]*ip[type].orires.obs*weight;
509             for (j = 0; j <= i; j++)
510             {
511                 T[ex][i][j] += Dtav[d][i]*Dtav[d][j]*weight;
512             }
513         }
514         d++;
515     }
516     /* Now we have all the data we can calculate S */
517     for (ex = 0; ex < od->nex; ex++)
518     {
519         /* Correct corrfac and copy one half of T to the other half */
520         for (i = 0; i < 5; i++)
521         {
522             rhs[ex][i]  *= corrfac;
523             T[ex][i][i] *= sqr(corrfac);
524             for (j = 0; j < i; j++)
525             {
526                 T[ex][i][j] *= sqr(corrfac);
527                 T[ex][j][i]  = T[ex][i][j];
528             }
529         }
530         m_inv_gen(T[ex], 5, T[ex]);
531         /* Calculate the orientation tensor S for this experiment */
532         S[ex][0][0] = 0;
533         S[ex][0][1] = 0;
534         S[ex][0][2] = 0;
535         S[ex][1][1] = 0;
536         S[ex][1][2] = 0;
537         for (i = 0; i < 5; i++)
538         {
539             S[ex][0][0] += 1.5*T[ex][0][i]*rhs[ex][i];
540             S[ex][0][1] += 1.5*T[ex][1][i]*rhs[ex][i];
541             S[ex][0][2] += 1.5*T[ex][2][i]*rhs[ex][i];
542             S[ex][1][1] += 1.5*T[ex][3][i]*rhs[ex][i];
543             S[ex][1][2] += 1.5*T[ex][4][i]*rhs[ex][i];
544         }
545         S[ex][1][0] = S[ex][0][1];
546         S[ex][2][0] = S[ex][0][2];
547         S[ex][2][1] = S[ex][1][2];
548         S[ex][2][2] = -S[ex][0][0] - S[ex][1][1];
549     }
550
551     wsv2 = 0;
552     sw   = 0;
553
554     d = 0;
555     for (fa = 0; fa < nfa; fa += 3)
556     {
557         type = forceatoms[fa];
558         ex   = ip[type].orires.ex;
559
560         od->otav[d] = two_thr*
561             corrfac*(S[ex][0][0]*Dtav[d][0] + S[ex][0][1]*Dtav[d][1] +
562                      S[ex][0][2]*Dtav[d][2] + S[ex][1][1]*Dtav[d][3] +
563                      S[ex][1][2]*Dtav[d][4]);
564         if (bTAV)
565         {
566             od->oins[d] = two_thr*(S[ex][0][0]*Dins[d][0] + S[ex][0][1]*Dins[d][1] +
567                                    S[ex][0][2]*Dins[d][2] + S[ex][1][1]*Dins[d][3] +
568                                    S[ex][1][2]*Dins[d][4]);
569         }
570         if (ms)
571         {
572             /* When ensemble averaging is used recalculate the local orientation
573              * for output to the energy file.
574              */
575             od->oinsl[d] = two_thr*
576                 (S[ex][0][0]*Dinsl[d][0] + S[ex][0][1]*Dinsl[d][1] +
577                  S[ex][0][2]*Dinsl[d][2] + S[ex][1][1]*Dinsl[d][3] +
578                  S[ex][1][2]*Dinsl[d][4]);
579         }
580
581         dev = od->otav[d] - ip[type].orires.obs;
582
583         wsv2 += ip[type].orires.kfac*sqr(dev);
584         sw   += ip[type].orires.kfac;
585
586         d++;
587     }
588     od->rmsdev = sqrt(wsv2/sw);
589
590     /* Rotate the S matrices back, so we get the correct grad(tr(S D)) */
591     for (ex = 0; ex < od->nex; ex++)
592     {
593         tmmul(R, S[ex], TMP);
594         mmul(TMP, R, S[ex]);
595     }
596
597     return od->rmsdev;
598
599     /* Approx. 120*nfa/3 flops */
600 }
601
602 real orires(int nfa, const t_iatom forceatoms[], const t_iparams ip[],
603             const rvec x[], rvec f[], rvec fshift[],
604             const t_pbc *pbc, const t_graph *g,
605             real gmx_unused lambda, real gmx_unused *dvdlambda,
606             const t_mdatoms gmx_unused *md, t_fcdata *fcd,
607             int gmx_unused *global_atom_index)
608 {
609     atom_id             ai, aj;
610     int                 fa, d, i, type, ex, power, ki = CENTRAL;
611     ivec                dt;
612     real                r2, invr, invr2, fc, smooth_fc, dev, devins, pfac;
613     rvec                r, Sr, fij;
614     real                vtot;
615     const t_oriresdata *od;
616     gmx_bool            bTAV;
617
618     vtot = 0;
619     od   = &(fcd->orires);
620
621     if (od->fc != 0)
622     {
623         bTAV = (od->edt != 0);
624
625         smooth_fc = od->fc;
626         if (bTAV)
627         {
628             /* Smoothly switch on the restraining when time averaging is used */
629             smooth_fc *= (1.0 - od->exp_min_t_tau);
630         }
631
632         d = 0;
633         for (fa = 0; fa < nfa; fa += 3)
634         {
635             type  = forceatoms[fa];
636             ai    = forceatoms[fa+1];
637             aj    = forceatoms[fa+2];
638             if (pbc)
639             {
640                 ki = pbc_dx_aiuc(pbc, x[ai], x[aj], r);
641             }
642             else
643             {
644                 rvec_sub(x[ai], x[aj], r);
645             }
646             r2    = norm2(r);
647             invr  = gmx_invsqrt(r2);
648             invr2 = invr*invr;
649             ex    = ip[type].orires.ex;
650             power = ip[type].orires.power;
651             fc    = smooth_fc*ip[type].orires.kfac;
652             dev   = od->otav[d] - ip[type].orires.obs;
653
654             /* NOTE:
655              * there is no real potential when time averaging is applied
656              */
657             vtot += 0.5*fc*sqr(dev);
658
659             if (bTAV)
660             {
661                 /* Calculate the force as the sqrt of tav times instantaneous */
662                 devins = od->oins[d] - ip[type].orires.obs;
663                 if (dev*devins <= 0)
664                 {
665                     dev = 0;
666                 }
667                 else
668                 {
669                     dev = sqrt(dev*devins);
670                     if (devins < 0)
671                     {
672                         dev = -dev;
673                     }
674                 }
675             }
676
677             pfac  = fc*ip[type].orires.c*invr2;
678             for (i = 0; i < power; i++)
679             {
680                 pfac *= invr;
681             }
682             mvmul(od->S[ex], r, Sr);
683             for (i = 0; i < DIM; i++)
684             {
685                 fij[i] =
686                     -pfac*dev*(4*Sr[i] - 2*(2+power)*invr2*iprod(Sr, r)*r[i]);
687             }
688
689             if (g)
690             {
691                 ivec_sub(SHIFT_IVEC(g, ai), SHIFT_IVEC(g, aj), dt);
692                 ki = IVEC2IS(dt);
693             }
694
695             for (i = 0; i < DIM; i++)
696             {
697                 f[ai][i]           += fij[i];
698                 f[aj][i]           -= fij[i];
699                 fshift[ki][i]      += fij[i];
700                 fshift[CENTRAL][i] -= fij[i];
701             }
702             d++;
703         }
704     }
705
706     return vtot;
707
708     /* Approx. 80*nfa/3 flops */
709 }
710
711 void update_orires_history(t_fcdata *fcd, history_t *hist)
712 {
713     t_oriresdata *od;
714     int           pair, i;
715
716     od = &(fcd->orires);
717     if (od->edt != 0)
718     {
719         /* Copy the new time averages that have been calculated
720          *  in calc_orires_dev.
721          */
722         hist->orire_initf = od->exp_min_t_tau;
723         for (pair = 0; pair < od->nr; pair++)
724         {
725             for (i = 0; i < 5; i++)
726             {
727                 hist->orire_Dtav[pair*5+i] = od->Dtav[pair][i];
728             }
729         }
730     }
731 }