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