3/3 of old-style casting
[alexxy/gromacs.git] / src / gromacs / mdlib / qmmm.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,2018, 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 #include "gmxpre.h"
38
39 #include "qmmm.h"
40
41 #include "config.h"
42
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46
47 #include <cmath>
48
49 #include <algorithm>
50
51 #include "gromacs/domdec/domdec_struct.h"
52 #include "gromacs/fileio/confio.h"
53 #include "gromacs/gmxlib/network.h"
54 #include "gromacs/gmxlib/nrnb.h"
55 #include "gromacs/math/functions.h"
56 #include "gromacs/math/units.h"
57 #include "gromacs/math/vec.h"
58 #include "gromacs/mdlib/ns.h"
59 #include "gromacs/mdtypes/commrec.h"
60 #include "gromacs/mdtypes/forcerec.h"
61 #include "gromacs/mdtypes/inputrec.h"
62 #include "gromacs/mdtypes/md_enums.h"
63 #include "gromacs/mdtypes/mdatom.h"
64 #include "gromacs/mdtypes/nblist.h"
65 #include "gromacs/pbcutil/ishift.h"
66 #include "gromacs/pbcutil/pbc.h"
67 #include "gromacs/topology/mtop_lookup.h"
68 #include "gromacs/topology/mtop_util.h"
69 #include "gromacs/topology/topology.h"
70 #include "gromacs/utility/fatalerror.h"
71 #include "gromacs/utility/smalloc.h"
72
73 /* declarations of the interfaces to the QM packages. The _SH indicate
74  * the QM interfaces can be used for Surface Hopping simulations
75  */
76 #if GMX_QMMM_GAMESS
77 /* GAMESS interface */
78
79 void
80 init_gamess(const t_commrec *cr, t_QMrec *qm, t_MMrec *mm);
81
82 real
83 call_gamess(const t_forcerec *fr,
84             t_QMrec *qm, t_MMrec *mm, rvec f[], rvec fshift[]);
85
86 #elif GMX_QMMM_MOPAC
87 /* MOPAC interface */
88
89 void
90 init_mopac(t_QMrec *qm);
91
92 real
93 call_mopac(t_QMrec *qm, t_MMrec *mm, rvec f[], rvec fshift[]);
94
95 real
96 call_mopac_SH(t_QMrec *qm, t_MMrec *mm, rvec f[], rvec fshift[]);
97
98 #elif GMX_QMMM_GAUSSIAN
99 /* GAUSSIAN interface */
100
101 void
102 init_gaussian(t_QMrec *qm);
103
104 real
105 call_gaussian_SH(const t_forcerec *fr, t_QMrec *qm, t_MMrec *mm, rvec f[], rvec fshift[]);
106
107 real
108 call_gaussian(const t_forcerec *fr, t_QMrec *qm, t_MMrec *mm, rvec f[], rvec fshift[]);
109
110 #elif GMX_QMMM_ORCA
111 #include "gromacs/mdlib/qm_orca.h"
112 #endif
113
114 #if GMX_QMMM
115 /* this struct and these comparison functions are needed for creating
116  * a QMMM input for the QM routines from the QMMM neighbor list.
117  */
118
119 typedef struct {
120     int      j;
121     int      shift;
122 } t_j_particle;
123
124 static bool struct_comp(const t_j_particle &a, const t_j_particle &b)
125 {
126     return a.j < b.j;
127 }
128
129 static real call_QMroutine(const t_commrec gmx_unused *cr, const t_forcerec gmx_unused *fr, t_QMrec gmx_unused *qm,
130                            t_MMrec gmx_unused *mm, rvec gmx_unused f[], rvec gmx_unused fshift[])
131 {
132     /* makes a call to the requested QM routine (qm->QMmethod)
133      * Note that f is actually the gradient, i.e. -f
134      */
135     /* do a semi-empiprical calculation */
136
137     if (qm->QMmethod < eQMmethodRHF && !(mm->nrMMatoms))
138     {
139 #if GMX_QMMM_MOPAC
140         if (qm->bSH)
141         {
142             return call_mopac_SH(qm, mm, f, fshift);
143         }
144         else
145         {
146             return call_mopac(qm, mm, f, fshift);
147         }
148 #else
149         gmx_fatal(FARGS, "Semi-empirical QM only supported with Mopac.");
150 #endif
151     }
152     else
153     {
154         /* do an ab-initio calculation */
155         if (qm->bSH && qm->QMmethod == eQMmethodCASSCF)
156         {
157 #if GMX_QMMM_GAUSSIAN
158             return call_gaussian_SH(fr, qm, mm, f, fshift);
159 #else
160             gmx_fatal(FARGS, "Ab-initio Surface-hopping only supported with Gaussian.");
161 #endif
162         }
163         else
164         {
165 #if GMX_QMMM_GAMESS
166             return call_gamess(fr, qm, mm, f, fshift);
167 #elif GMX_QMMM_GAUSSIAN
168             return call_gaussian(fr, qm, mm, f, fshift);
169 #elif GMX_QMMM_ORCA
170             return call_orca(fr, qm, mm, f, fshift);
171 #else
172             gmx_fatal(FARGS, "Ab-initio calculation only supported with Gamess, Gaussian or ORCA.");
173 #endif
174         }
175     }
176 }
177
178 static void init_QMroutine(const t_commrec gmx_unused *cr, t_QMrec gmx_unused *qm, t_MMrec gmx_unused *mm)
179 {
180     /* makes a call to the requested QM routine (qm->QMmethod)
181      */
182     if (qm->QMmethod < eQMmethodRHF)
183     {
184 #if GMX_QMMM_MOPAC
185         /* do a semi-empiprical calculation */
186         init_mopac(qm);
187 #else
188         gmx_fatal(FARGS, "Semi-empirical QM only supported with Mopac.");
189 #endif
190     }
191     else
192     {
193         /* do an ab-initio calculation */
194 #if GMX_QMMM_GAMESS
195         init_gamess(cr, qm, mm);
196 #elif GMX_QMMM_GAUSSIAN
197         init_gaussian(qm);
198 #elif GMX_QMMM_ORCA
199         init_orca(qm);
200 #else
201         gmx_fatal(FARGS, "Ab-initio calculation only supported with Gamess, Gaussian or ORCA.");
202 #endif
203     }
204 } /* init_QMroutine */
205
206 static void update_QMMM_coord(const rvec *x, const t_forcerec *fr, t_QMrec *qm, t_MMrec *mm)
207 {
208     /* shifts the QM and MM particles into the central box and stores
209      * these shifted coordinates in the coordinate arrays of the
210      * QMMMrec. These coordinates are passed on the QM subroutines.
211      */
212     int
213         i;
214
215     /* shift the QM atoms into the central box
216      */
217     for (i = 0; i < qm->nrQMatoms; i++)
218     {
219         rvec_sub(x[qm->indexQM[i]], fr->shift_vec[qm->shiftQM[i]], qm->xQM[i]);
220     }
221     /* also shift the MM atoms into the central box, if any
222      */
223     for (i = 0; i < mm->nrMMatoms; i++)
224     {
225         rvec_sub(x[mm->indexMM[i]], fr->shift_vec[mm->shiftMM[i]], mm->xMM[i]);
226     }
227 } /* update_QMMM_coord */
228
229 /* end of QMMM subroutines */
230
231 /* QMMM core routines */
232
233 static t_QMrec *mk_QMrec(void)
234 {
235     t_QMrec *qm;
236     snew(qm, 1);
237     return qm;
238 } /* mk_QMrec */
239
240 static t_MMrec *mk_MMrec(void)
241 {
242     t_MMrec *mm;
243     snew(mm, 1);
244     return mm;
245 } /* mk_MMrec */
246
247 static void init_QMrec(int grpnr, t_QMrec *qm, int nr, const int *atomarray,
248                        gmx_mtop_t *mtop, t_inputrec *ir)
249 {
250     /* fills the t_QMrec struct of QM group grpnr
251      */
252
253     qm->nrQMatoms = nr;
254     snew(qm->xQM, nr);
255     snew(qm->indexQM, nr);
256     snew(qm->shiftQM, nr); /* the shifts */
257     for (int i = 0; i < nr; i++)
258     {
259         qm->indexQM[i] = atomarray[i];
260     }
261
262     snew(qm->atomicnumberQM, nr);
263     int molb = 0;
264     for (int i = 0; i < qm->nrQMatoms; i++)
265     {
266         const t_atom &atom = mtopGetAtomParameters(mtop, qm->indexQM[i], &molb);
267         qm->nelectrons       += mtop->atomtypes.atomnumber[atom.type];
268         qm->atomicnumberQM[i] = mtop->atomtypes.atomnumber[atom.type];
269     }
270
271     qm->QMcharge       = ir->opts.QMcharge[grpnr];
272     qm->multiplicity   = ir->opts.QMmult[grpnr];
273     qm->nelectrons    -= ir->opts.QMcharge[grpnr];
274
275     qm->QMmethod       = ir->opts.QMmethod[grpnr];
276     qm->QMbasis        = ir->opts.QMbasis[grpnr];
277     /* trajectory surface hopping setup (Gaussian only) */
278     qm->bSH            = ir->opts.bSH[grpnr];
279     qm->CASorbitals    = ir->opts.CASorbitals[grpnr];
280     qm->CASelectrons   = ir->opts.CASelectrons[grpnr];
281     qm->SAsteps        = ir->opts.SAsteps[grpnr];
282     qm->SAon           = ir->opts.SAon[grpnr];
283     qm->SAoff          = ir->opts.SAoff[grpnr];
284     /* hack to prevent gaussian from reinitializing all the time */
285     qm->nQMcpus        = 0; /* number of CPU's to be used by g01, is set
286                              * upon initializing gaussian
287                              * (init_gaussian()
288                              */
289     /* print the current layer to allow users to check their input */
290     fprintf(stderr, "Layer %d\nnr of QM atoms %d\n", grpnr, nr);
291     fprintf(stderr, "QMlevel: %s/%s\n\n",
292             eQMmethod_names[qm->QMmethod], eQMbasis_names[qm->QMbasis]);
293 } /* init_QMrec */
294
295 static t_QMrec *copy_QMrec(t_QMrec *qm)
296 {
297     /* copies the contents of qm into a new t_QMrec struct */
298     t_QMrec
299        *qmcopy;
300     int
301         i;
302
303     qmcopy            = mk_QMrec();
304     qmcopy->nrQMatoms = qm->nrQMatoms;
305     snew(qmcopy->xQM, qmcopy->nrQMatoms);
306     snew(qmcopy->indexQM, qmcopy->nrQMatoms);
307     snew(qmcopy->atomicnumberQM, qm->nrQMatoms);
308     snew(qmcopy->shiftQM, qmcopy->nrQMatoms); /* the shifts */
309     for (i = 0; i < qmcopy->nrQMatoms; i++)
310     {
311         qmcopy->shiftQM[i]        = qm->shiftQM[i];
312         qmcopy->indexQM[i]        = qm->indexQM[i];
313         qmcopy->atomicnumberQM[i] = qm->atomicnumberQM[i];
314     }
315     qmcopy->nelectrons   = qm->nelectrons;
316     qmcopy->multiplicity = qm->multiplicity;
317     qmcopy->QMcharge     = qm->QMcharge;
318     qmcopy->nelectrons   = qm->nelectrons;
319     qmcopy->QMmethod     = qm->QMmethod;
320     qmcopy->QMbasis      = qm->QMbasis;
321     /* trajectory surface hopping setup (Gaussian only) */
322     qmcopy->bSH          = qm->bSH;
323     qmcopy->CASorbitals  = qm->CASorbitals;
324     qmcopy->CASelectrons = qm->CASelectrons;
325     qmcopy->SAsteps      = qm->SAsteps;
326     qmcopy->SAon         = qm->SAon;
327     qmcopy->SAoff        = qm->SAoff;
328
329     /* Gaussian init. variables */
330     qmcopy->nQMcpus      = qm->nQMcpus;
331     for (i = 0; i < DIM; i++)
332     {
333         qmcopy->SHbasis[i] = qm->SHbasis[i];
334     }
335     qmcopy->QMmem        = qm->QMmem;
336     qmcopy->accuracy     = qm->accuracy;
337     qmcopy->cpmcscf      = qm->cpmcscf;
338     qmcopy->SAstep       = qm->SAstep;
339
340     return(qmcopy);
341
342 } /*copy_QMrec */
343
344 t_QMMMrec *mk_QMMMrec(void)
345 {
346
347     t_QMMMrec *qr;
348
349     snew(qr, 1);
350
351     return qr;
352
353 } /* mk_QMMMrec */
354
355 void init_QMMMrec(const t_commrec  *cr,
356                   gmx_mtop_t       *mtop,
357                   t_inputrec       *ir,
358                   const t_forcerec *fr)
359 {
360     /* we put the atomsnumbers of atoms that belong to the QMMM group in
361      * an array that will be copied later to QMMMrec->indexQM[..]. Also
362      * it will be used to create an QMMMrec->bQMMM index array that
363      * simply contains true/false for QM and MM (the other) atoms.
364      */
365
366     gmx_groups_t            *groups;
367     int                     *qm_arr = nullptr, vsite, ai, aj;
368     int                      qm_max = 0, qm_nr = 0, i, j, jmax, k, l, nrvsite2 = 0;
369     t_QMMMrec               *qr;
370     t_MMrec                 *mm;
371     t_iatom                 *iatoms;
372     gmx_mtop_atomloop_all_t  aloop;
373     gmx_mtop_ilistloop_all_t iloop;
374     int                      a_offset;
375     const t_ilist           *ilist_mol;
376
377     if (ir->cutoff_scheme != ecutsGROUP)
378     {
379         gmx_fatal(FARGS, "QMMM is currently only supported with cutoff-scheme=group");
380     }
381     if (!EI_DYNAMICS(ir->eI))
382     {
383         gmx_fatal(FARGS, "QMMM is only supported with dynamics");
384     }
385
386     /* issue a fatal if the user wants to run with more than one node */
387     if (PAR(cr))
388     {
389         gmx_fatal(FARGS, "QM/MM does not work in parallel, use a single rank instead\n");
390     }
391
392     /* Make a local copy of the QMMMrec */
393     qr = fr->qr;
394
395     /* bQMMM[..] is an array containing TRUE/FALSE for atoms that are
396      * QM/not QM. We first set all elemenst at false. Afterwards we use
397      * the qm_arr (=MMrec->indexQM) to changes the elements
398      * corresponding to the QM atoms at TRUE.  */
399
400     qr->QMMMscheme     = ir->QMMMscheme;
401
402     /* we take the possibility into account that a user has
403      * defined more than one QM group:
404      */
405     /* an ugly work-around in case there is only one group In this case
406      * the whole system is treated as QM. Otherwise the second group is
407      * always the rest of the total system and is treated as MM.
408      */
409
410     /* small problem if there is only QM.... so no MM */
411
412     jmax = ir->opts.ngQM;
413
414     if (qr->QMMMscheme == eQMMMschemeoniom)
415     {
416         qr->nrQMlayers = jmax;
417     }
418     else
419     {
420         qr->nrQMlayers = 1;
421     }
422
423     groups = &mtop->groups;
424
425     /* there are jmax groups of QM atoms. In case of multiple QM groups
426      * I assume that the users wants to do ONIOM. However, maybe it
427      * should also be possible to define more than one QM subsystem with
428      * independent neighbourlists. I have to think about
429      * that.. 11-11-2003
430      */
431     snew(qr->qm, jmax);
432     for (j = 0; j < jmax; j++)
433     {
434         /* new layer */
435         aloop = gmx_mtop_atomloop_all_init(mtop);
436         const t_atom *atom;
437         while (gmx_mtop_atomloop_all_next(aloop, &i, &atom))
438         {
439             if (qm_nr >= qm_max)
440             {
441                 qm_max += 1000;
442                 srenew(qm_arr, qm_max);
443             }
444             if (ggrpnr(groups, egcQMMM, i) == j)
445             {
446                 /* hack for tip4p */
447                 qm_arr[qm_nr++] = i;
448             }
449         }
450         if (qr->QMMMscheme == eQMMMschemeoniom)
451         {
452             /* add the atoms to the bQMMM array
453              */
454
455             /* I assume that users specify the QM groups from small to
456              * big(ger) in the mdp file
457              */
458             qr->qm[j] = mk_QMrec();
459             /* we need to throw out link atoms that in the previous layer
460              * existed to separate this QMlayer from the previous
461              * QMlayer. We use the iatoms array in the idef for that
462              * purpose. If all atoms defining the current Link Atom (Dummy2)
463              * are part of the current QM layer it needs to be removed from
464              * qm_arr[].  */
465
466             iloop = gmx_mtop_ilistloop_all_init(mtop);
467             while (gmx_mtop_ilistloop_all_next(iloop, &ilist_mol, &a_offset))
468             {
469                 nrvsite2 = ilist_mol[F_VSITE2].nr;
470                 iatoms   = ilist_mol[F_VSITE2].iatoms;
471
472                 for (k = 0; k < nrvsite2; k += 4)
473                 {
474                     vsite = a_offset + iatoms[k+1]; /* the vsite         */
475                     ai    = a_offset + iatoms[k+2]; /* constructing atom */
476                     aj    = a_offset + iatoms[k+3]; /* constructing atom */
477                     if (ggrpnr(groups, egcQMMM, vsite) == ggrpnr(groups, egcQMMM, ai)
478                         &&
479                         ggrpnr(groups, egcQMMM, vsite) == ggrpnr(groups, egcQMMM, aj))
480                     {
481                         /* this dummy link atom needs to be removed from the qm_arr
482                          * before making the QMrec of this layer!
483                          */
484                         for (i = 0; i < qm_nr; i++)
485                         {
486                             if (qm_arr[i] == vsite)
487                             {
488                                 /* drop the element */
489                                 for (l = i; l < qm_nr; l++)
490                                 {
491                                     qm_arr[l] = qm_arr[l+1];
492                                 }
493                                 qm_nr--;
494                             }
495                         }
496                     }
497                 }
498             }
499
500             /* store QM atoms in this layer in the QMrec and initialise layer
501              */
502             init_QMrec(j, qr->qm[j], qm_nr, qm_arr, mtop, ir);
503         }
504     }
505     if (qr->QMMMscheme != eQMMMschemeoniom)
506     {
507
508         /* standard QMMM, all layers are merged together so there is one QM
509          * subsystem and one MM subsystem.
510          * Also we set the charges to zero in mtop to prevent the innerloops
511          * from doubly counting the electostatic QM MM interaction
512          * TODO: Consider doing this in grompp instead.
513          */
514
515         int molb = 0;
516         for (k = 0; k < qm_nr; k++)
517         {
518             int     indexInMolecule;
519             mtopGetMolblockIndex(mtop, qm_arr[k], &molb, nullptr, &indexInMolecule);
520             t_atom *atom = &mtop->moltype[mtop->molblock[molb].type].atoms.atom[indexInMolecule];
521             atom->q  = 0.0;
522             atom->qB = 0.0;
523         }
524         qr->qm[0] = mk_QMrec();
525         /* store QM atoms in the QMrec and initialise
526          */
527         init_QMrec(0, qr->qm[0], qm_nr, qm_arr, mtop, ir);
528
529         /* MM rec creation */
530         mm               = mk_MMrec();
531         mm->scalefactor  = ir->scalefactor;
532         mm->nrMMatoms    = (mtop->natoms)-(qr->qm[0]->nrQMatoms); /* rest of the atoms */
533         qr->mm           = mm;
534     }
535     else /* ONIOM */
536     {    /* MM rec creation */
537         mm               = mk_MMrec();
538         mm->scalefactor  = ir->scalefactor;
539         mm->nrMMatoms    = 0;
540         qr->mm           = mm;
541     }
542
543     /* these variables get updated in the update QMMMrec */
544
545     if (qr->nrQMlayers == 1)
546     {
547         /* with only one layer there is only one initialisation
548          * needed. Multilayer is a bit more complicated as it requires
549          * re-initialisation at every step of the simulation. This is due
550          * to the use of COMMON blocks in the fortran QM subroutines.
551          */
552         if (qr->qm[0]->QMmethod < eQMmethodRHF)
553         {
554 #if GMX_QMMM_MOPAC
555             /* semi-empiprical 1-layer ONIOM calculation requested (mopac93) */
556             init_mopac(qr->qm[0]);
557 #else
558             gmx_fatal(FARGS, "Semi-empirical QM only supported with Mopac.");
559 #endif
560         }
561         else
562         {
563             /* ab initio calculation requested (gamess/gaussian/ORCA) */
564 #if GMX_QMMM_GAMESS
565             init_gamess(cr, qr->qm[0], qr->mm);
566 #elif GMX_QMMM_GAUSSIAN
567             init_gaussian(qr->qm[0]);
568 #elif GMX_QMMM_ORCA
569             init_orca(qr->qm[0]);
570 #else
571             gmx_fatal(FARGS, "Ab-initio calculation only supported with Gamess, Gaussian or ORCA.");
572 #endif
573         }
574     }
575 } /* init_QMMMrec */
576
577 void update_QMMMrec(const t_commrec  *cr,
578                     const t_forcerec *fr,
579                     const rvec       *x,
580                     const t_mdatoms  *md,
581                     const matrix      box)
582 {
583     /* updates the coordinates of both QM atoms and MM atoms and stores
584      * them in the QMMMrec.
585      *
586      * NOTE: is NOT yet working if there are no PBC. Also in ns.c, simple
587      * ns needs to be fixed!
588      */
589     int
590         mm_max = 0, mm_nr = 0, mm_nr_new, i, j, is, k, shift;
591     t_j_particle
592        *mm_j_particles = nullptr, *qm_i_particles = nullptr;
593     t_QMMMrec
594        *qr;
595     t_nblist
596        *QMMMlist;
597     rvec
598         dx;
599     ivec
600         crd;
601     t_QMrec
602        *qm;
603     t_MMrec
604        *mm;
605     t_pbc
606         pbc;
607     int
608        *parallelMMarray = nullptr;
609
610     /* every cpu has this array. On every processor we fill this array
611      * with 1's and 0's. 1's indicate the atoms is a QM atom on the
612      * current cpu in a later stage these arrays are all summed. indexes
613      * > 0 indicate the atom is a QM atom. Every node therefore knows
614      * whcih atoms are part of the QM subsystem.
615      */
616     /* copy some pointers */
617     qr          = fr->qr;
618     mm          = qr->mm;
619     QMMMlist    = fr->QMMMlist;
620
621     /*  init_pbc(box);  needs to be called first, see pbc.h */
622     ivec null_ivec;
623     clear_ivec(null_ivec);
624     set_pbc_dd(&pbc, fr->ePBC, DOMAINDECOMP(cr) ? cr->dd->nc : null_ivec,
625                FALSE, box);
626     /* only in standard (normal) QMMM we need the neighbouring MM
627      * particles to provide a electric field of point charges for the QM
628      * atoms.
629      */
630     if (qr->QMMMscheme == eQMMMschemenormal) /* also implies 1 QM-layer */
631     {
632         /* we NOW create/update a number of QMMMrec entries:
633          *
634          * 1) the shiftQM, containing the shifts of the QM atoms
635          *
636          * 2) the indexMM array, containing the index of the MM atoms
637          *
638          * 3) the shiftMM, containing the shifts of the MM atoms
639          *
640          * 4) the shifted coordinates of the MM atoms
641          *
642          * the shifts are used for computing virial of the QM/MM particles.
643          */
644         qm = qr->qm[0]; /* in case of normal QMMM, there is only one group */
645         snew(qm_i_particles, QMMMlist->nri);
646         if (QMMMlist->nri)
647         {
648             qm_i_particles[0].shift = XYZ2IS(0, 0, 0);
649             for (i = 0; i < QMMMlist->nri; i++)
650             {
651                 qm_i_particles[i].j     = QMMMlist->iinr[i];
652
653                 if (i)
654                 {
655                     qm_i_particles[i].shift = pbc_dx_aiuc(&pbc, x[QMMMlist->iinr[0]],
656                                                           x[QMMMlist->iinr[i]], dx);
657
658                 }
659                 /* However, since nri >= nrQMatoms, we do a quicksort, and throw
660                  * out double, triple, etc. entries later, as we do for the MM
661                  * list too.
662                  */
663
664                 /* compute the shift for the MM j-particles with respect to
665                  * the QM i-particle and store them.
666                  */
667
668                 crd[0] = IS2X(QMMMlist->shift[i]) + IS2X(qm_i_particles[i].shift);
669                 crd[1] = IS2Y(QMMMlist->shift[i]) + IS2Y(qm_i_particles[i].shift);
670                 crd[2] = IS2Z(QMMMlist->shift[i]) + IS2Z(qm_i_particles[i].shift);
671                 is     = XYZ2IS(crd[0], crd[1], crd[2]);
672                 for (j = QMMMlist->jindex[i];
673                      j < QMMMlist->jindex[i+1];
674                      j++)
675                 {
676                     if (mm_nr >= mm_max)
677                     {
678                         mm_max += 1000;
679                         srenew(mm_j_particles, mm_max);
680                     }
681
682                     mm_j_particles[mm_nr].j     = QMMMlist->jjnr[j];
683                     mm_j_particles[mm_nr].shift = is;
684                     mm_nr++;
685                 }
686             }
687
688             /* quicksort QM and MM shift arrays and throw away multiple entries */
689
690
691
692             std::sort(qm_i_particles, qm_i_particles+QMMMlist->nri, struct_comp);
693             /* The mm_j_particles argument to qsort is not allowed to be NULL */
694             if (mm_nr > 0)
695             {
696                 std::sort(mm_j_particles, mm_j_particles+mm_nr, struct_comp);
697             }
698             /* remove multiples in the QM shift array, since in init_QMMM() we
699              * went through the atom numbers from 0 to md.nr, the order sorted
700              * here matches the one of QMindex already.
701              */
702             j = 0;
703             for (i = 0; i < QMMMlist->nri; i++)
704             {
705                 if (i == 0 || qm_i_particles[i].j != qm_i_particles[i-1].j)
706                 {
707                     qm_i_particles[j++] = qm_i_particles[i];
708                 }
709             }
710             mm_nr_new = 0;
711             /* Remove double entries for the MM array.
712              * Also remove mm atoms that have no charges!
713              * actually this is already done in the ns.c
714              */
715             for (i = 0; i < mm_nr; i++)
716             {
717                 if ((i == 0 || mm_j_particles[i].j != mm_j_particles[i-1].j)
718                     && !md->bQM[mm_j_particles[i].j]
719                     && (md->chargeA[mm_j_particles[i].j]
720                         || (md->chargeB && md->chargeB[mm_j_particles[i].j])))
721                 {
722                     mm_j_particles[mm_nr_new++] = mm_j_particles[i];
723                 }
724             }
725             mm_nr = mm_nr_new;
726             /* store the data retrieved above into the QMMMrec
727              */
728             k = 0;
729             /* Keep the compiler happy,
730              * shift will always be set in the loop for i=0
731              */
732             shift = 0;
733             for (i = 0; i < qm->nrQMatoms; i++)
734             {
735                 /* not all qm particles might have appeared as i
736                  * particles. They might have been part of the same charge
737                  * group for instance.
738                  */
739                 if (qm->indexQM[i] == qm_i_particles[k].j)
740                 {
741                     shift = qm_i_particles[k++].shift;
742                 }
743                 /* use previous shift, assuming they belong the same charge
744                  * group anyway,
745                  */
746
747                 qm->shiftQM[i] = shift;
748             }
749         }
750         /* parallel excecution */
751         if (PAR(cr))
752         {
753             snew(parallelMMarray, 2*(md->nr));
754             /* only MM particles have a 1 at their atomnumber. The second part
755              * of the array contains the shifts. Thus:
756              * p[i]=1/0 depending on wether atomnumber i is a MM particle in the QM
757              * step or not. p[i+md->nr] is the shift of atomnumber i.
758              */
759             for (i = 0; i < 2*(md->nr); i++)
760             {
761                 parallelMMarray[i] = 0;
762             }
763
764             for (i = 0; i < mm_nr; i++)
765             {
766                 parallelMMarray[mm_j_particles[i].j]          = 1;
767                 parallelMMarray[mm_j_particles[i].j+(md->nr)] = mm_j_particles[i].shift;
768             }
769             gmx_sumi(md->nr, parallelMMarray, cr);
770             mm_nr = 0;
771
772             mm_max = 0;
773             for (i = 0; i < md->nr; i++)
774             {
775                 if (parallelMMarray[i])
776                 {
777                     if (mm_nr >= mm_max)
778                     {
779                         mm_max += 1000;
780                         srenew(mm->indexMM, mm_max);
781                         srenew(mm->shiftMM, mm_max);
782                     }
783                     mm->indexMM[mm_nr]   = i;
784                     mm->shiftMM[mm_nr++] = parallelMMarray[i+md->nr]/parallelMMarray[i];
785                 }
786             }
787             mm->nrMMatoms = mm_nr;
788             free(parallelMMarray);
789         }
790         /* serial execution */
791         else
792         {
793             mm->nrMMatoms = mm_nr;
794             srenew(mm->shiftMM, mm_nr);
795             srenew(mm->indexMM, mm_nr);
796             for (i = 0; i < mm_nr; i++)
797             {
798                 mm->indexMM[i] = mm_j_particles[i].j;
799                 mm->shiftMM[i] = mm_j_particles[i].shift;
800             }
801
802         }
803         /* (re) allocate memory for the MM coordiate array. The QM
804          * coordinate array was already allocated in init_QMMM, and is
805          * only (re)filled in the update_QMMM_coordinates routine
806          */
807         srenew(mm->xMM, mm->nrMMatoms);
808         /* now we (re) fill the array that contains the MM charges with
809          * the forcefield charges. If requested, these charges will be
810          * scaled by a factor
811          */
812         srenew(mm->MMcharges, mm->nrMMatoms);
813         for (i = 0; i < mm->nrMMatoms; i++) /* no free energy yet */
814         {
815             mm->MMcharges[i] = md->chargeA[mm->indexMM[i]]*mm->scalefactor;
816         }
817         /* the next routine fills the coordinate fields in the QMMM rec of
818          * both the qunatum atoms and the MM atoms, using the shifts
819          * calculated above.
820          */
821
822         update_QMMM_coord(x, fr, qr->qm[0], qr->mm);
823         free(qm_i_particles);
824         free(mm_j_particles);
825     }
826     else /* ONIOM */ /* ????? */
827     {
828         mm->nrMMatoms = 0;
829         /* do for each layer */
830         for (j = 0; j < qr->nrQMlayers; j++)
831         {
832             qm             = qr->qm[j];
833             qm->shiftQM[0] = XYZ2IS(0, 0, 0);
834             for (i = 1; i < qm->nrQMatoms; i++)
835             {
836                 qm->shiftQM[i] = pbc_dx_aiuc(&pbc, x[qm->indexQM[0]], x[qm->indexQM[i]],
837                                              dx);
838             }
839             update_QMMM_coord(x, fr, qm, mm);
840         }
841     }
842 } /* update_QMMM_rec */
843
844 real calculate_QMMM(const t_commrec  *cr,
845                     rvec              f[],
846                     const t_forcerec *fr)
847 {
848     real
849         QMener = 0.0;
850     /* a selection for the QM package depending on which is requested
851      * (Gaussian, GAMESS-UK, MOPAC or ORCA) needs to be implemented here. Now
852      * it works through defines.... Not so nice yet
853      */
854     t_QMMMrec
855     *qr;
856     t_QMrec
857     *qm, *qm2;
858     t_MMrec
859     *mm = nullptr;
860     rvec
861     *forces  = nullptr, *fshift = nullptr,
862     *forces2 = nullptr, *fshift2 = nullptr; /* needed for multilayer ONIOM */
863     int
864         i, j, k;
865     /* make a local copy the QMMMrec pointer
866      */
867     qr = fr->qr;
868     mm = qr->mm;
869
870     /* now different procedures are carried out for one layer ONION and
871      * normal QMMM on one hand and multilayer oniom on the other
872      */
873     if (qr->QMMMscheme == eQMMMschemenormal || qr->nrQMlayers == 1)
874     {
875         qm = qr->qm[0];
876         snew(forces, (qm->nrQMatoms+mm->nrMMatoms));
877         snew(fshift, (qm->nrQMatoms+mm->nrMMatoms));
878         QMener = call_QMroutine(cr, fr, qm, mm, forces, fshift);
879         for (i = 0; i < qm->nrQMatoms; i++)
880         {
881             for (j = 0; j < DIM; j++)
882             {
883                 f[qm->indexQM[i]][j]          -= forces[i][j];
884                 fr->fshift[qm->shiftQM[i]][j] += fshift[i][j];
885             }
886         }
887         for (i = 0; i < mm->nrMMatoms; i++)
888         {
889             for (j = 0; j < DIM; j++)
890             {
891                 f[mm->indexMM[i]][j]          -= forces[qm->nrQMatoms+i][j];
892                 fr->fshift[mm->shiftMM[i]][j] += fshift[qm->nrQMatoms+i][j];
893             }
894
895         }
896         free(forces);
897         free(fshift);
898     }
899     else                                       /* Multi-layer ONIOM */
900     {
901         for (i = 0; i < qr->nrQMlayers-1; i++) /* last layer is special */
902         {
903             qm  = qr->qm[i];
904             qm2 = copy_QMrec(qr->qm[i+1]);
905
906             qm2->nrQMatoms = qm->nrQMatoms;
907
908             for (j = 0; j < qm2->nrQMatoms; j++)
909             {
910                 for (k = 0; k < DIM; k++)
911                 {
912                     qm2->xQM[j][k]       = qm->xQM[j][k];
913                 }
914                 qm2->indexQM[j]        = qm->indexQM[j];
915                 qm2->atomicnumberQM[j] = qm->atomicnumberQM[j];
916                 qm2->shiftQM[j]        = qm->shiftQM[j];
917             }
918
919             qm2->QMcharge = qm->QMcharge;
920             /* this layer at the higher level of theory */
921             srenew(forces, qm->nrQMatoms);
922             srenew(fshift, qm->nrQMatoms);
923             /* we need to re-initialize the QMroutine every step... */
924             init_QMroutine(cr, qm, mm);
925             QMener += call_QMroutine(cr, fr, qm, mm, forces, fshift);
926
927             /* this layer at the lower level of theory */
928             srenew(forces2, qm->nrQMatoms);
929             srenew(fshift2, qm->nrQMatoms);
930             init_QMroutine(cr, qm2, mm);
931             QMener -= call_QMroutine(cr, fr, qm2, mm, forces2, fshift2);
932             /* E = E1high-E1low The next layer includes the current layer at
933              * the lower level of theory, which provides + E2low
934              * this is similar for gradients
935              */
936             for (i = 0; i < qm->nrQMatoms; i++)
937             {
938                 for (j = 0; j < DIM; j++)
939                 {
940                     f[qm->indexQM[i]][j]          -= (forces[i][j]-forces2[i][j]);
941                     fr->fshift[qm->shiftQM[i]][j] += (fshift[i][j]-fshift2[i][j]);
942                 }
943             }
944             free(qm2);
945         }
946         /* now the last layer still needs to be done: */
947         qm      = qr->qm[qr->nrQMlayers-1]; /* C counts from 0 */
948         init_QMroutine(cr, qm, mm);
949         srenew(forces, qm->nrQMatoms);
950         srenew(fshift, qm->nrQMatoms);
951         QMener += call_QMroutine(cr, fr, qm, mm, forces, fshift);
952         for (i = 0; i < qm->nrQMatoms; i++)
953         {
954             for (j = 0; j < DIM; j++)
955             {
956                 f[qm->indexQM[i]][j]          -= forces[i][j];
957                 fr->fshift[qm->shiftQM[i]][j] += fshift[i][j];
958             }
959         }
960         free(forces);
961         free(fshift);
962         free(forces2);
963         free(fshift2);
964     }
965     return(QMener);
966 } /* calculate_QMMM */
967 #else
968 real calculate_QMMM(const t_commrec  * /*unused*/,
969                     rvec             * /*unused*/,
970                     const t_forcerec * /*unused*/)
971 {
972     gmx_incons("Compiled without QMMM");
973 }
974 t_QMMMrec *mk_QMMMrec(void)
975 {
976     return nullptr;
977 }
978 void init_QMMMrec(const t_commrec  * /*unused*/,
979                   gmx_mtop_t       * /*unused*/,
980                   t_inputrec       * /*unused*/,
981                   const t_forcerec * /*unused*/)
982 {
983     gmx_incons("Compiled without QMMM");
984 }
985 void update_QMMMrec(const t_commrec  * /*unused*/,
986                     const t_forcerec * /*unused*/,
987                     const rvec       * /*unused*/,
988                     const t_mdatoms  * /*unused*/,
989                     const matrix       /*unused*/)
990 {
991     gmx_incons("Compiled without QMMM");
992 }
993 #endif
994
995 /* end of QMMM core routines */