622d076f55b1a6ba558e89e1efd6e66ab1d1bcc6
[alexxy/gromacs.git] / src / mdlib / forcerec.c
1 /* -*- mode: c; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; c-file-style: "stroustrup"; -*-
2  *
3  * 
4  *                This source code is part of
5  * 
6  *                 G   R   O   M   A   C   S
7  * 
8  *          GROningen MAchine for Chemical Simulations
9  * 
10  *                        VERSION 3.2.0
11  * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
12  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
13  * Copyright (c) 2001-2004, The GROMACS development team,
14  * check out http://www.gromacs.org for more information.
15
16  * This program is free software; you can redistribute it and/or
17  * modify it under the terms of the GNU General Public License
18  * as published by the Free Software Foundation; either version 2
19  * of the License, or (at your option) any later version.
20  * 
21  * If you want to redistribute modifications, please consider that
22  * scientific software is very special. Version control is crucial -
23  * bugs must be traceable. We will be happy to consider code for
24  * inclusion in the official distribution, but derived work must not
25  * be called official GROMACS. Details are found in the README & COPYING
26  * files - if they are missing, get the official version at www.gromacs.org.
27  * 
28  * To help us fund GROMACS development, we humbly ask that you cite
29  * the papers on the package - you can find them in the top README file.
30  * 
31  * For more info, check our website at http://www.gromacs.org
32  * 
33  * And Hey:
34  * GROwing Monsters And Cloning Shrimps
35  */
36 #ifdef HAVE_CONFIG_H
37 #include <config.h>
38 #endif
39
40 #include <math.h>
41 #include <string.h>
42 #include "sysstuff.h"
43 #include "typedefs.h"
44 #include "macros.h"
45 #include "smalloc.h"
46 #include "macros.h"
47 #include "physics.h"
48 #include "force.h"
49 #include "nonbonded.h"
50 #include "invblock.h"
51 #include "names.h"
52 #include "network.h"
53 #include "pbc.h"
54 #include "ns.h"
55 #include "mshift.h"
56 #include "txtdump.h"
57 #include "coulomb.h"
58 #include "mdrun.h"
59 #include "domdec.h"
60 #include "partdec.h"
61 #include "qmmm.h"
62 #include "copyrite.h"
63 #include "mtop_util.h"
64
65 t_forcerec *mk_forcerec(void)
66 {
67   t_forcerec *fr;
68   
69   snew(fr,1);
70   
71   return fr;
72 }
73
74 #ifdef DEBUG
75 static void pr_nbfp(FILE *fp,real *nbfp,bool bBHAM,int atnr)
76 {
77   int i,j;
78   
79   for(i=0; (i<atnr); i++) {
80     for(j=0; (j<atnr); j++) {
81       fprintf(fp,"%2d - %2d",i,j);
82       if (bBHAM)
83         fprintf(fp,"  a=%10g, b=%10g, c=%10g\n",BHAMA(nbfp,atnr,i,j),
84                 BHAMB(nbfp,atnr,i,j),BHAMC(nbfp,atnr,i,j));
85       else
86         fprintf(fp,"  c6=%10g, c12=%10g\n",C6(nbfp,atnr,i,j),
87                 C12(nbfp,atnr,i,j));
88     }
89   }
90 }
91 #endif
92
93 static real *mk_nbfp(const gmx_ffparams_t *idef,bool bBHAM)
94 {
95   real *nbfp;
96   int  i,j,k,atnr;
97   
98   atnr=idef->atnr;
99   if (bBHAM) {
100     snew(nbfp,3*atnr*atnr);
101     for(i=k=0; (i<atnr); i++) {
102       for(j=0; (j<atnr); j++,k++) {
103         BHAMA(nbfp,atnr,i,j) = idef->iparams[k].bham.a;
104         BHAMB(nbfp,atnr,i,j) = idef->iparams[k].bham.b;
105         BHAMC(nbfp,atnr,i,j) = idef->iparams[k].bham.c;
106       }
107     }
108   }
109   else {
110     snew(nbfp,2*atnr*atnr);
111     for(i=k=0; (i<atnr); i++) {
112       for(j=0; (j<atnr); j++,k++) {
113         C6(nbfp,atnr,i,j)   = idef->iparams[k].lj.c6;
114         C12(nbfp,atnr,i,j)  = idef->iparams[k].lj.c12;
115       }
116     }
117   }
118   return nbfp;
119 }
120
121 /* This routine sets fr->solvent_opt to the most common solvent in the 
122  * system, e.g. esolSPC or esolTIP4P. It will also mark each charge group in 
123  * the fr->solvent_type array with the correct type (or esolNO).
124  *
125  * Charge groups that fulfill the conditions but are not identical to the
126  * most common one will be marked as esolNO in the solvent_type array. 
127  *
128  * TIP3p is identical to SPC for these purposes, so we call it
129  * SPC in the arrays (Apologies to Bill Jorgensen ;-)
130  * 
131  * NOTE: QM particle should not
132  * become an optimized solvent. Not even if there is only one charge
133  * group in the Qm 
134  */
135
136 typedef struct 
137 {
138     int    model;          
139     int    count;
140     int    vdwtype[4];
141     real   charge[4];
142 } solvent_parameters_t;
143
144 static void
145 check_solvent_cg(const gmx_moltype_t   *molt,
146                  int                   cg0,
147                  int                   nmol,
148                  const unsigned char   *qm_grpnr,
149                  const t_grps          *qm_grps,
150                  t_forcerec *          fr,
151                  int                   *n_solvent_parameters,
152                  solvent_parameters_t  **solvent_parameters_p,
153                  int                   cginfo,
154                  int                   *cg_sp)
155 {
156     const t_blocka *  excl;
157     t_atom            *atom;
158     int               j,k;
159     int               j0,j1,nj;
160     bool              perturbed;
161     bool              has_vdw[4];
162     bool              match;
163     real              tmp_charge[4];
164     int               tmp_vdwtype[4];
165     int               tjA;
166     bool              qm;
167     solvent_parameters_t *solvent_parameters;
168
169     /* We use a list with parameters for each solvent type. 
170      * Every time we discover a new molecule that fulfills the basic 
171      * conditions for a solvent we compare with the previous entries
172      * in these lists. If the parameters are the same we just increment
173      * the counter for that type, and otherwise we create a new type
174      * based on the current molecule.
175      *
176      * Once we've finished going through all molecules we check which
177      * solvent is most common, and mark all those molecules while we
178      * clear the flag on all others.
179      */   
180
181     solvent_parameters = *solvent_parameters_p;
182
183     /* Mark the cg first as non optimized */
184     *cg_sp = -1;
185     
186     /* Check if this cg has no exclusions with atoms in other charge groups
187      * and all atoms inside the charge group excluded.
188      * We only have 3 or 4 atom solvent loops.
189      */
190     if (GET_CGINFO_EXCL_INTER(cginfo) ||
191         !GET_CGINFO_EXCL_INTRA(cginfo))
192     {
193         return;
194     }
195
196     /* Get the indices of the first atom in this charge group */
197     j0     = molt->cgs.index[cg0];
198     j1     = molt->cgs.index[cg0+1];
199     
200     /* Number of atoms in our molecule */
201     nj     = j1 - j0;
202
203     if (debug) {
204         fprintf(debug,
205                 "Moltype '%s': there are %d atoms in this charge group\n",
206                 *molt->name,nj);
207     }
208     
209     /* Check if it could be an SPC (3 atoms) or TIP4p (4) water,
210      * otherwise skip it.
211      */
212     if (nj<3 || nj>4)
213     {
214         return;
215     }
216     
217     /* Check if we are doing QM on this group */
218     qm = FALSE; 
219     if (qm_grpnr != NULL)
220     {
221         for(j=j0 ; j<j1 && !qm; j++)
222         {
223             qm = (qm_grpnr[j] < qm_grps->nr - 1);
224         }
225     }
226     /* Cannot use solvent optimization with QM */
227     if (qm)
228     {
229         return;
230     }
231     
232     atom = molt->atoms.atom;
233
234     /* Still looks like a solvent, time to check parameters */
235     
236     /* If it is perturbed (free energy) we can't use the solvent loops,
237      * so then we just skip to the next molecule.
238      */   
239     perturbed = FALSE; 
240     
241     for(j=j0; j<j1 && !perturbed; j++)
242     {
243         perturbed = PERTURBED(atom[j]);
244     }
245     
246     if (perturbed)
247     {
248         return;
249     }
250     
251     /* Now it's only a question if the VdW and charge parameters 
252      * are OK. Before doing the check we compare and see if they are 
253      * identical to a possible previous solvent type.
254      * First we assign the current types and charges.    
255      */
256     for(j=0; j<nj; j++)
257     {
258         tmp_vdwtype[j] = atom[j0+j].type;
259         tmp_charge[j]  = atom[j0+j].q;
260     } 
261     
262     /* Does it match any previous solvent type? */
263     for(k=0 ; k<*n_solvent_parameters; k++)
264     {
265         match = TRUE;
266         
267         
268         /* We can only match SPC with 3 atoms and TIP4p with 4 atoms */
269         if( (solvent_parameters[k].model==esolSPC   && nj!=3)  ||
270             (solvent_parameters[k].model==esolTIP4P && nj!=4) )
271             match = FALSE;
272         
273         /* Check that types & charges match for all atoms in molecule */
274         for(j=0 ; j<nj && match==TRUE; j++)
275         {                       
276             if (tmp_vdwtype[j] != solvent_parameters[k].vdwtype[j])
277             {
278                 match = FALSE;
279             }
280             if(tmp_charge[j] != solvent_parameters[k].charge[j])
281             {
282                 match = FALSE;
283             }
284         }
285         if (match == TRUE)
286         {
287             /* Congratulations! We have a matched solvent.
288              * Flag it with this type for later processing.
289              */
290             *cg_sp = k;
291             solvent_parameters[k].count += nmol;
292
293             /* We are done with this charge group */
294             return;
295         }
296     }
297     
298     /* If we get here, we have a tentative new solvent type.
299      * Before we add it we must check that it fulfills the requirements
300      * of the solvent optimized loops. First determine which atoms have
301      * VdW interactions.   
302      */
303     for(j=0; j<nj; j++) 
304     {
305         has_vdw[j] = FALSE;
306         tjA        = tmp_vdwtype[j];
307         
308         /* Go through all other tpes and see if any have non-zero
309          * VdW parameters when combined with this one.
310          */   
311         for(k=0; k<fr->ntype && (has_vdw[j]==FALSE); k++)
312         {
313             /* We already checked that the atoms weren't perturbed,
314              * so we only need to check state A now.
315              */ 
316             if (fr->bBHAM) 
317             {
318                 has_vdw[j] = (has_vdw[j] || 
319                               (BHAMA(fr->nbfp,fr->ntype,tjA,k) != 0.0) ||
320                               (BHAMB(fr->nbfp,fr->ntype,tjA,k) != 0.0) ||
321                               (BHAMC(fr->nbfp,fr->ntype,tjA,k) != 0.0));
322             }
323             else
324             {
325                 /* Standard LJ */
326                 has_vdw[j] = (has_vdw[j] || 
327                               (C6(fr->nbfp,fr->ntype,tjA,k)  != 0.0) ||
328                               (C12(fr->nbfp,fr->ntype,tjA,k) != 0.0));
329             }
330         }
331     }
332     
333     /* Now we know all we need to make the final check and assignment. */
334     if (nj == 3)
335     {
336         /* So, is it an SPC?
337          * For this we require thatn all atoms have charge, 
338          * the charges on atom 2 & 3 should be the same, and only
339          * atom 1 should have VdW.
340          */
341         if (has_vdw[0] == TRUE && 
342             has_vdw[1] == FALSE &&
343             has_vdw[2] == FALSE &&
344             tmp_charge[0]  != 0 &&
345             tmp_charge[1]  != 0 &&
346             tmp_charge[2]  == tmp_charge[1])
347         {
348             srenew(solvent_parameters,*n_solvent_parameters+1);
349             solvent_parameters[*n_solvent_parameters].model = esolSPC;
350             solvent_parameters[*n_solvent_parameters].count = nmol;
351             for(k=0;k<3;k++)
352             {
353                 solvent_parameters[*n_solvent_parameters].vdwtype[k] = tmp_vdwtype[k];
354                 solvent_parameters[*n_solvent_parameters].charge[k]  = tmp_charge[k];
355             }
356
357             *cg_sp = *n_solvent_parameters;
358             (*n_solvent_parameters)++;
359         }
360     }
361     else if (nj==4)
362     {
363         /* Or could it be a TIP4P?
364          * For this we require thatn atoms 2,3,4 have charge, but not atom 1. 
365          * Only atom 1 should have VdW.
366          */
367         if(has_vdw[0] == TRUE && 
368            has_vdw[1] == FALSE &&
369            has_vdw[2] == FALSE &&
370            has_vdw[3] == FALSE &&
371            tmp_charge[0]  == 0 &&
372            tmp_charge[1]  != 0 &&
373            tmp_charge[2]  == tmp_charge[1] &&
374            tmp_charge[3]  != 0)
375         {
376             srenew(solvent_parameters,*n_solvent_parameters+1);
377             solvent_parameters[*n_solvent_parameters].model = esolTIP4P;
378             solvent_parameters[*n_solvent_parameters].count = nmol;
379             for(k=0;k<4;k++)
380             {
381                 solvent_parameters[*n_solvent_parameters].vdwtype[k] = tmp_vdwtype[k];
382                 solvent_parameters[*n_solvent_parameters].charge[k]  = tmp_charge[k];
383             }
384             
385             *cg_sp = *n_solvent_parameters;
386             (*n_solvent_parameters)++;
387         }
388     }
389
390     *solvent_parameters_p = solvent_parameters;
391 }
392
393 static void
394 check_solvent(FILE *                fp,
395               const gmx_mtop_t *    mtop,
396               t_forcerec *          fr,
397               cginfo_mb_t           *cginfo_mb)
398 {
399     const t_block *   cgs;
400     const t_block *   mols;
401     const gmx_moltype_t *molt;
402     int               mb,mol,cg_mol,at_offset,cg_offset,am,cgm,i,nmol_ch,nmol;
403     int               n_solvent_parameters;
404     solvent_parameters_t *solvent_parameters;
405     int               **cg_sp;
406     int               bestsp,bestsol;
407
408     if (debug)
409     {
410         fprintf(debug,"Going to determine what solvent types we have.\n");
411     }
412
413     mols = &mtop->mols;
414
415     n_solvent_parameters = 0;
416     solvent_parameters = NULL;
417     /* Allocate temporary array for solvent type */
418     snew(cg_sp,mtop->nmolblock);
419
420     cg_offset = 0;
421     at_offset = 0;
422     for(mb=0; mb<mtop->nmolblock; mb++)
423     {
424         molt = &mtop->moltype[mtop->molblock[mb].type];
425         cgs  = &molt->cgs;
426         /* Here we have to loop over all individual molecules
427          * because we need to check for QMMM particles.
428          */
429         snew(cg_sp[mb],cginfo_mb[mb].cg_mod);
430         nmol_ch = cginfo_mb[mb].cg_mod/cgs->nr;
431         nmol    = mtop->molblock[mb].nmol/nmol_ch;
432         for(mol=0; mol<nmol_ch; mol++)
433         {
434             cgm = mol*cgs->nr;
435             am  = mol*cgs->index[cgs->nr];
436             for(cg_mol=0; cg_mol<cgs->nr; cg_mol++)
437             {
438                 check_solvent_cg(molt,cg_mol,nmol,
439                                  mtop->groups.grpnr[egcQMMM] ?
440                                  mtop->groups.grpnr[egcQMMM]+at_offset+am : 0,
441                                  &mtop->groups.grps[egcQMMM],
442                                  fr,
443                                  &n_solvent_parameters,&solvent_parameters,
444                                  cginfo_mb[mb].cginfo[cgm+cg_mol],
445                                  &cg_sp[mb][cgm+cg_mol]);
446             }
447         }
448         cg_offset += cgs->nr;
449         at_offset += cgs->index[cgs->nr];
450     }
451
452     /* Puh! We finished going through all charge groups.
453      * Now find the most common solvent model.
454      */   
455     
456     /* Most common solvent this far */
457     bestsp = -2;
458     for(i=0;i<n_solvent_parameters;i++)
459     {
460         if (bestsp == -2 ||
461             solvent_parameters[i].count > solvent_parameters[bestsp].count)
462         {
463             bestsp = i;
464         }
465     }
466     
467     if (bestsp >= 0)
468     {
469         bestsol = solvent_parameters[bestsp].model;
470     }
471     else
472     {
473         bestsol = esolNO;
474     }
475     
476 #ifdef DISABLE_WATER_NLIST
477         bestsol = esolNO;
478 #endif
479
480     fr->nWatMol = 0;
481     for(mb=0; mb<mtop->nmolblock; mb++)
482     {
483         cgs = &mtop->moltype[mtop->molblock[mb].type].cgs;
484         nmol = (mtop->molblock[mb].nmol*cgs->nr)/cginfo_mb[mb].cg_mod;
485         for(i=0; i<cginfo_mb[mb].cg_mod; i++)
486         {
487             if (cg_sp[mb][i] == bestsp)
488             {
489                 SET_CGINFO_SOLOPT(cginfo_mb[mb].cginfo[i],bestsol);
490                 fr->nWatMol += nmol;
491             }
492             else
493             {
494                 SET_CGINFO_SOLOPT(cginfo_mb[mb].cginfo[i],esolNO);
495             }
496         }
497         sfree(cg_sp[mb]);
498     }
499     sfree(cg_sp);
500     
501     if (bestsol != esolNO && fp!=NULL)
502     {
503         fprintf(fp,"\nEnabling %s-like water optimization for %d molecules.\n\n",
504                 esol_names[bestsol],
505                 solvent_parameters[bestsp].count);
506     }
507
508     sfree(solvent_parameters);
509     fr->solvent_opt = bestsol;
510 }
511
512 static cginfo_mb_t *init_cginfo_mb(FILE *fplog,const gmx_mtop_t *mtop,
513                                    t_forcerec *fr,bool bNoSolvOpt,
514                                    bool *bExcl_IntraCGAll_InterCGNone)
515 {
516     const t_block *cgs;
517     const t_blocka *excl;
518     const gmx_moltype_t *molt;
519     const gmx_molblock_t *molb;
520     cginfo_mb_t *cginfo_mb;
521     int  *cginfo;
522     int  cg_offset,a_offset,cgm,am;
523     int  mb,m,ncg_tot,cg,a0,a1,gid,ai,j,aj,excl_nalloc;
524     bool bId,*bExcl,bExclIntraAll,bExclInter;
525
526     ncg_tot = ncg_mtop(mtop);
527     snew(cginfo_mb,mtop->nmolblock);
528
529     *bExcl_IntraCGAll_InterCGNone = TRUE;
530
531     excl_nalloc = 10;
532     snew(bExcl,excl_nalloc);
533     cg_offset = 0;
534     a_offset  = 0;
535     for(mb=0; mb<mtop->nmolblock; mb++)
536     {
537         molb = &mtop->molblock[mb];
538         molt = &mtop->moltype[molb->type];
539         cgs  = &molt->cgs;
540         excl = &molt->excls;
541
542         /* Check if the cginfo is identical for all molecules in this block.
543          * If so, we only need an array of the size of one molecule.
544          * Otherwise we make an array of #mol times #cgs per molecule.
545          */
546         bId = TRUE;
547         am = 0;
548         for(m=0; m<molb->nmol; m++)
549         {
550             am = m*cgs->index[cgs->nr];
551             for(cg=0; cg<cgs->nr; cg++)
552             {
553                 a0 = cgs->index[cg];
554                 a1 = cgs->index[cg+1];
555                 if (ggrpnr(&mtop->groups,egcENER,a_offset+am+a0) !=
556                     ggrpnr(&mtop->groups,egcENER,a_offset   +a0))
557                 {
558                     bId = FALSE;
559                 }
560                 if (mtop->groups.grpnr[egcQMMM] != NULL)
561                 {
562                     for(ai=a0; ai<a1; ai++)
563                     {
564                         if (mtop->groups.grpnr[egcQMMM][a_offset+am+ai] !=
565                             mtop->groups.grpnr[egcQMMM][a_offset   +ai])
566                         {
567                             bId = FALSE;
568                         }
569                     }
570                 }
571             }
572         }
573
574         cginfo_mb[mb].cg_start = cg_offset;
575         cginfo_mb[mb].cg_end   = cg_offset + molb->nmol*cgs->nr;
576         cginfo_mb[mb].cg_mod   = (bId ? 1 : molb->nmol)*cgs->nr;
577         snew(cginfo_mb[mb].cginfo,cginfo_mb[mb].cg_mod);
578         cginfo = cginfo_mb[mb].cginfo;
579
580         for(m=0; m<(bId ? 1 : molb->nmol); m++)
581         {
582             cgm = m*cgs->nr;
583             am  = m*cgs->index[cgs->nr];
584             for(cg=0; cg<cgs->nr; cg++)
585             {
586                 a0 = cgs->index[cg];
587                 a1 = cgs->index[cg+1];
588
589                 /* Store the energy group in cginfo */
590                 gid = ggrpnr(&mtop->groups,egcENER,a_offset+am+a0);
591                 SET_CGINFO_GID(cginfo[cgm+cg],gid);
592                 
593                 /* Check the intra/inter charge group exclusions */
594                 if (a1-a0 > excl_nalloc) {
595                     excl_nalloc = a1 - a0;
596                     srenew(bExcl,excl_nalloc);
597                 }
598                 /* bExclIntraAll: all intra cg interactions excluded
599                  * bExclInter:    any inter cg interactions excluded
600                  */
601                 bExclIntraAll = TRUE;
602                 bExclInter    = FALSE;
603                 for(ai=a0; ai<a1; ai++) {
604                     /* Clear the exclusion list for atom ai */
605                     for(aj=a0; aj<a1; aj++) {
606                         bExcl[aj-a0] = FALSE;
607                     }
608                     /* Loop over all the exclusions of atom ai */
609                     for(j=excl->index[ai]; j<excl->index[ai+1]; j++)
610                     {
611                         aj = excl->a[j];
612                         if (aj < a0 || aj >= a1)
613                         {
614                             bExclInter = TRUE;
615                         }
616                         else
617                         {
618                             bExcl[aj-a0] = TRUE;
619                         }
620                     }
621                     /* Check if ai excludes a0 to a1 */
622                     for(aj=a0; aj<a1; aj++)
623                     {
624                         if (!bExcl[aj-a0])
625                         {
626                             bExclIntraAll = FALSE;
627                         }
628                     }
629                 }
630                 if (bExclIntraAll)
631                 {
632                     SET_CGINFO_EXCL_INTRA(cginfo[cgm+cg]);
633                 }
634                 if (bExclInter)
635                 {
636                     SET_CGINFO_EXCL_INTER(cginfo[cgm+cg]);
637                 }
638                 if (a1 - a0 > MAX_CHARGEGROUP_SIZE)
639                 {
640                     /* The size in cginfo is currently only read with DD */
641                     gmx_fatal(FARGS,"A charge group has size %d which is larger than the limit of %d atoms",a1-a0,MAX_CHARGEGROUP_SIZE);
642                 }
643                 SET_CGINFO_NATOMS(cginfo[cgm+cg],a1-a0);
644
645                 if (!bExclIntraAll || bExclInter)
646                 {
647                     *bExcl_IntraCGAll_InterCGNone = FALSE;
648                 }
649             }
650         }
651         cg_offset += molb->nmol*cgs->nr;
652         a_offset  += molb->nmol*cgs->index[cgs->nr];
653     }
654     sfree(bExcl);
655     
656     /* the solvent optimizer is called after the QM is initialized,
657      * because we don't want to have the QM subsystemto become an
658      * optimized solvent
659      */
660
661     check_solvent(fplog,mtop,fr,cginfo_mb);
662     
663     if (getenv("GMX_NO_SOLV_OPT"))
664     {
665         if (fplog)
666         {
667             fprintf(fplog,"Found environment variable GMX_NO_SOLV_OPT.\n"
668                     "Disabling all solvent optimization\n");
669         }
670         fr->solvent_opt = esolNO;
671     }
672     if (bNoSolvOpt)
673     {
674         fr->solvent_opt = esolNO;
675     }
676     if (!fr->solvent_opt)
677     {
678         for(mb=0; mb<mtop->nmolblock; mb++)
679         {
680             for(cg=0; cg<cginfo_mb[mb].cg_mod; cg++)
681             {
682                 SET_CGINFO_SOLOPT(cginfo_mb[mb].cginfo[cg],esolNO);
683             }
684         }
685     }
686     
687     return cginfo_mb;
688 }
689
690 static int *cginfo_expand(int nmb,cginfo_mb_t *cgi_mb)
691 {
692     int ncg,mb,cg;
693     int *cginfo;
694
695     ncg = cgi_mb[nmb-1].cg_end;
696     snew(cginfo,ncg);
697     mb = 0;
698     for(cg=0; cg<ncg; cg++)
699     {
700         while (cg >= cgi_mb[mb].cg_end)
701         {
702             mb++;
703         }
704         cginfo[cg] =
705             cgi_mb[mb].cginfo[(cg - cgi_mb[mb].cg_start) % cgi_mb[mb].cg_mod];
706     }
707
708     return cginfo;
709 }
710
711 static void set_chargesum(FILE *log,t_forcerec *fr,const gmx_mtop_t *mtop)
712 {
713     double qsum;
714     int    mb,nmol,i;
715     const t_atoms *atoms;
716     
717     qsum = 0;
718     for(mb=0; mb<mtop->nmolblock; mb++)
719     {
720         nmol  = mtop->molblock[mb].nmol;
721         atoms = &mtop->moltype[mtop->molblock[mb].type].atoms;
722         for(i=0; i<atoms->nr; i++)
723         {
724             qsum += nmol*atoms->atom[i].q;
725         }
726     }
727     fr->qsum[0] = qsum;
728     if (fr->efep != efepNO)
729     {
730         qsum = 0;
731         for(mb=0; mb<mtop->nmolblock; mb++)
732         {
733             nmol  = mtop->molblock[mb].nmol;
734             atoms = &mtop->moltype[mtop->molblock[mb].type].atoms;
735             for(i=0; i<atoms->nr; i++)
736             {
737                 qsum += nmol*atoms->atom[i].qB;
738             }
739             fr->qsum[1] = qsum;
740         }
741     }
742     else
743     {
744         fr->qsum[1] = fr->qsum[0];
745     }
746     if (log) {
747         if (fr->efep == efepNO)
748             fprintf(log,"System total charge: %.3f\n",fr->qsum[0]);
749         else
750             fprintf(log,"System total charge, top. A: %.3f top. B: %.3f\n",
751                     fr->qsum[0],fr->qsum[1]);
752     }
753 }
754
755 void update_forcerec(FILE *log,t_forcerec *fr,matrix box)
756 {
757     if (fr->eeltype == eelGRF)
758     {
759         calc_rffac(NULL,fr->eeltype,fr->epsilon_r,fr->epsilon_rf,
760                    fr->rcoulomb,fr->temp,fr->zsquare,box,
761                    &fr->kappa,&fr->k_rf,&fr->c_rf);
762     }
763 }
764
765 void set_avcsixtwelve(FILE *fplog,t_forcerec *fr,const gmx_mtop_t *mtop)
766 {
767     const t_atoms *atoms;
768     const t_blocka *excl;
769     int    mb,nmol,nmolc,i,j,tpi,tpj,j1,j2,k,n,nexcl,q;
770 #if (defined SIZEOF_LONG_LONG_INT) && (SIZEOF_LONG_LONG_INT >= 8)    
771     long long int  npair,npair_ij,tmpi,tmpj;
772 #else
773     double npair, npair_ij,tmpi,tmpj;
774 #endif
775     double csix,ctwelve;
776     int    ntp,*typecount;
777     bool   bBHAM;
778     real   *nbfp;
779
780     ntp = fr->ntype;
781     bBHAM = fr->bBHAM;
782     nbfp = fr->nbfp;
783     
784     for(q=0; q<(fr->efep==efepNO ? 1 : 2); q++) {
785         csix = 0;
786         ctwelve = 0;
787         npair = 0;
788         nexcl = 0;
789         if (!fr->n_tpi) {
790             /* Count the types so we avoid natoms^2 operations */
791             snew(typecount,ntp);
792             for(mb=0; mb<mtop->nmolblock; mb++) {
793                 nmol  = mtop->molblock[mb].nmol;
794                 atoms = &mtop->moltype[mtop->molblock[mb].type].atoms;
795                 for(i=0; i<atoms->nr; i++) {
796                     if (q == 0)
797                     {
798                         tpi = atoms->atom[i].type;
799                     }
800                     else
801                     {
802                         tpi = atoms->atom[i].typeB;
803                     }
804                     typecount[tpi] += nmol;
805                 }
806             }
807             for(tpi=0; tpi<ntp; tpi++) {
808                 for(tpj=tpi; tpj<ntp; tpj++) {
809                     tmpi = typecount[tpi];
810                     tmpj = typecount[tpj];
811                     if (tpi != tpj)
812                     {
813                         npair_ij = tmpi*tmpj;
814                     }
815                     else
816                     {
817                         npair_ij = tmpi*(tmpi - 1)/2;
818                     }
819                     if (bBHAM) {
820                         csix    += npair_ij*BHAMC(nbfp,ntp,tpi,tpj);
821                     } else {
822                         csix    += npair_ij*   C6(nbfp,ntp,tpi,tpj);
823                         ctwelve += npair_ij*  C12(nbfp,ntp,tpi,tpj);
824                     }
825                     npair += npair_ij;
826                 }
827             }
828             sfree(typecount);
829             /* Subtract the excluded pairs.
830              * The main reason for substracting exclusions is that in some cases
831              * some combinations might never occur and the parameters could have
832              * any value. These unused values should not influence the dispersion
833              * correction.
834              */
835             for(mb=0; mb<mtop->nmolblock; mb++) {
836                 nmol  = mtop->molblock[mb].nmol;
837                 atoms = &mtop->moltype[mtop->molblock[mb].type].atoms;
838                 excl  = &mtop->moltype[mtop->molblock[mb].type].excls;
839                 for(i=0; (i<atoms->nr); i++) {
840                     if (q == 0)
841                     {
842                         tpi = atoms->atom[i].type;
843                     }
844                     else
845                     {
846                         tpi = atoms->atom[i].typeB;
847                     }
848                     j1  = excl->index[i];
849                     j2  = excl->index[i+1];
850                     for(j=j1; j<j2; j++) {
851                         k = excl->a[j];
852                         if (k > i)
853                         {
854                             if (q == 0)
855                             {
856                                 tpj = atoms->atom[k].type;
857                             }
858                             else
859                             {
860                                 tpj = atoms->atom[k].typeB;
861                             }
862                             if (bBHAM) {
863                                 csix -= nmol*BHAMC(nbfp,ntp,tpi,tpj);
864                             } else {
865                                 csix    -= nmol*C6 (nbfp,ntp,tpi,tpj);
866                                 ctwelve -= nmol*C12(nbfp,ntp,tpi,tpj);
867                             }
868                             nexcl += nmol;
869                         }
870                     }
871                 }
872             }
873         } else {
874             /* Only correct for the interaction of the test particle
875              * with the rest of the system.
876              */
877             atoms = &mtop->moltype[mtop->molblock[mtop->nmolblock-1].type].atoms;
878             if (q == 0)
879             {
880                 tpi = atoms->atom[atoms->nr-1].type;
881             }
882             else
883             {
884                 tpi = atoms->atom[atoms->nr-1].typeB;
885             }
886             npair = 0;
887             for(mb=0; mb<mtop->nmolblock; mb++) {
888                 nmol  = mtop->molblock[mb].nmol;
889                 atoms = &mtop->moltype[mtop->molblock[mb].type].atoms;
890                 for(j=0; j<atoms->nr; j++) {
891                     nmolc = nmol;
892                     /* Remove the interaction of the test charge group
893                      * with itself.
894                      */
895                     if (mb == mtop->nmolblock-1 && j >= atoms->nr - fr->n_tpi)
896                     {
897                         nmolc--;
898                     }
899                     if (q == 0)
900                     {
901                         tpj = atoms->atom[j].type;
902                     }
903                     else
904                     {
905                         tpj = atoms->atom[j].typeB;
906                     }
907                     if (bBHAM)
908                     {
909                         csix    += nmolc*BHAMC(nbfp,ntp,tpi,tpj);
910                     }
911                     else
912                     {
913                         csix    += nmolc*C6 (nbfp,ntp,tpi,tpj);
914                         ctwelve += nmolc*C12(nbfp,ntp,tpi,tpj);
915                     }
916                     npair += nmolc;
917                 }
918             }
919         }
920         if (npair - nexcl <= 0 && fplog) {
921             fprintf(fplog,"\nWARNING: There are no atom pairs for dispersion correction\n\n");
922             csix     = 0;
923             ctwelve  = 0;
924         } else {
925             csix    /= npair - nexcl;
926             ctwelve /= npair - nexcl;
927         }
928         if (debug) {
929             fprintf(debug,"Counted %d exclusions\n",nexcl);
930             fprintf(debug,"Average C6 parameter is: %10g\n",(double)csix);
931             fprintf(debug,"Average C12 parameter is: %10g\n",(double)ctwelve);
932         }
933         fr->avcsix[q]    = csix;
934         fr->avctwelve[q] = ctwelve;
935     }
936     if (fplog != NULL)
937     {
938         if (fr->eDispCorr == edispcAllEner ||
939             fr->eDispCorr == edispcAllEnerPres)
940         {
941             fprintf(fplog,"Long Range LJ corr.: <C6> %10.4e, <C12> %10.4e\n",
942                     fr->avcsix[0],fr->avctwelve[0]);
943         }
944         else
945         {
946             fprintf(fplog,"Long Range LJ corr.: <C6> %10.4e\n",fr->avcsix[0]);
947         }
948     }
949 }
950
951
952 static void set_bham_b_max(FILE *fplog,t_forcerec *fr,
953                            const gmx_mtop_t *mtop)
954 {
955     const t_atoms *at1,*at2;
956     int  mt1,mt2,i,j,tpi,tpj,ntypes;
957     real b,bmin;
958     real *nbfp;
959
960     if (fplog)
961     {
962         fprintf(fplog,"Determining largest Buckingham b parameter for table\n");
963     }
964     nbfp   = fr->nbfp;
965     ntypes = fr->ntype;
966     
967     bmin           = -1;
968     fr->bham_b_max = 0;
969     for(mt1=0; mt1<mtop->nmoltype; mt1++)
970     {
971         at1 = &mtop->moltype[mt1].atoms;
972         for(i=0; (i<at1->nr); i++)
973         {
974             tpi = at1->atom[i].type;
975             if (tpi >= ntypes)
976                 gmx_fatal(FARGS,"Atomtype[%d] = %d, maximum = %d",i,tpi,ntypes);
977             
978             for(mt2=mt1; mt2<mtop->nmoltype; mt2++)
979             {
980                 at2 = &mtop->moltype[mt2].atoms;
981                 for(j=0; (j<at2->nr); j++) {
982                     tpj = at2->atom[j].type;
983                     if (tpj >= ntypes)
984                     {
985                         gmx_fatal(FARGS,"Atomtype[%d] = %d, maximum = %d",j,tpj,ntypes);
986                     }
987                     b = BHAMB(nbfp,ntypes,tpi,tpj);
988                     if (b > fr->bham_b_max)
989                     {
990                         fr->bham_b_max = b;
991                     }
992                     if ((b < bmin) || (bmin==-1))
993                     {
994                         bmin = b;
995                     }
996                 }
997             }
998         }
999     }
1000     if (fplog)
1001     {
1002         fprintf(fplog,"Buckingham b parameters, min: %g, max: %g\n",
1003                 bmin,fr->bham_b_max);
1004     }
1005 }
1006
1007 static void make_nbf_tables(FILE *fp,const output_env_t oenv,
1008                             t_forcerec *fr,real rtab,
1009                             const t_commrec *cr,
1010                             const char *tabfn,char *eg1,char *eg2,
1011                             t_nblists *nbl)
1012 {
1013   char buf[STRLEN];
1014   int i,j;
1015   void *      p_tmp;
1016
1017   if (tabfn == NULL) {
1018     if (debug)
1019       fprintf(debug,"No table file name passed, can not read table, can not do non-bonded interactions\n");
1020     return;
1021   }
1022     
1023   sprintf(buf,"%s",tabfn);
1024   if (eg1 && eg2)
1025     /* Append the two energy group names */
1026     sprintf(buf + strlen(tabfn) - strlen(ftp2ext(efXVG)) - 1,"_%s_%s.%s",
1027             eg1,eg2,ftp2ext(efXVG));
1028   nbl->tab = make_tables(fp,oenv,fr,MASTER(cr),buf,rtab,0);
1029   /* Copy the contents of the table to separate coulomb and LJ tables too,
1030    * to improve cache performance.
1031    */
1032
1033   /* For performance reasons we want
1034    * the table data to be aligned to 16-byte. This is accomplished
1035    * by allocating 16 bytes extra to a temporary pointer, and then
1036    * calculating an aligned pointer. This new pointer must not be
1037    * used in a free() call, but thankfully we're sloppy enough not
1038    * to do this...
1039    */
1040   
1041   /* 8 fp entries per vdw table point, n+1 points, and 16 bytes extra to align it. */
1042   p_tmp = malloc(8*(nbl->tab.n+1)*sizeof(real)+16);
1043   
1044   /* align it - size_t has the same same as a pointer */
1045   nbl->vdwtab = (real *) (((size_t) p_tmp + 16) & (~((size_t) 15)));  
1046
1047   /* 4 fp entries per coul table point, n+1 points, and 16 bytes extra to align it. */
1048   p_tmp = malloc(4*(nbl->tab.n+1)*sizeof(real)+16);
1049   
1050   /* align it - size_t has the same same as a pointer */
1051   nbl->coultab = (real *) (((size_t) p_tmp + 16) & (~((size_t) 15)));  
1052
1053   
1054   for(i=0; i<=nbl->tab.n; i++) {
1055     for(j=0; j<4; j++)
1056       nbl->coultab[4*i+j] = nbl->tab.tab[12*i+j];
1057     for(j=0; j<8; j++)
1058       nbl->vdwtab [8*i+j] = nbl->tab.tab[12*i+4+j];
1059   }
1060 }
1061
1062 static void count_tables(int ftype1,int ftype2,const gmx_mtop_t *mtop,
1063                          int *ncount,int **count)
1064 {
1065     const gmx_moltype_t *molt;
1066     const t_ilist *il;
1067     int mt,ftype,stride,i,j,tabnr;
1068     
1069     for(mt=0; mt<mtop->nmoltype; mt++)
1070     {
1071         molt = &mtop->moltype[mt];
1072         for(ftype=0; ftype<F_NRE; ftype++)
1073         {
1074             if (ftype == ftype1 || ftype == ftype2) {
1075                 il = &molt->ilist[ftype];
1076                 stride = 1 + NRAL(ftype);
1077                 for(i=0; i<il->nr; i+=stride) {
1078                     tabnr = mtop->ffparams.iparams[il->iatoms[i]].tab.table;
1079                     if (tabnr < 0)
1080                         gmx_fatal(FARGS,"A bonded table number is smaller than 0: %d\n",tabnr);
1081                     if (tabnr >= *ncount) {
1082                         srenew(*count,tabnr+1);
1083                         for(j=*ncount; j<tabnr+1; j++)
1084                             (*count)[j] = 0;
1085                         *ncount = tabnr+1;
1086                     }
1087                     (*count)[tabnr]++;
1088                 }
1089             }
1090         }
1091     }
1092 }
1093
1094 static bondedtable_t *make_bonded_tables(FILE *fplog,
1095                                          int ftype1,int ftype2,
1096                                          const gmx_mtop_t *mtop,
1097                                          const char *basefn,const char *tabext)
1098 {
1099     int  i,ncount,*count;
1100     char tabfn[STRLEN];
1101     bondedtable_t *tab;
1102     
1103     tab = NULL;
1104     
1105     ncount = 0;
1106     count = NULL;
1107     count_tables(ftype1,ftype2,mtop,&ncount,&count);
1108     
1109     if (ncount > 0) {
1110         snew(tab,ncount);
1111         for(i=0; i<ncount; i++) {
1112             if (count[i] > 0) {
1113                 sprintf(tabfn,"%s",basefn);
1114                 sprintf(tabfn + strlen(basefn) - strlen(ftp2ext(efXVG)) - 1,"_%s%d.%s",
1115                         tabext,i,ftp2ext(efXVG));
1116                 tab[i] = make_bonded_table(fplog,tabfn,NRAL(ftype1)-2);
1117             }
1118         }
1119         sfree(count);
1120     }
1121   
1122     return tab;
1123 }
1124
1125 void forcerec_set_ranges(t_forcerec *fr,
1126                          int ncg_home,int ncg_force,
1127                          int natoms_force,
1128                          int natoms_force_constr,int natoms_f_novirsum)
1129 {
1130     fr->cg0 = 0;
1131     fr->hcg = ncg_home;
1132
1133     /* fr->ncg_force is unused in the standard code,
1134      * but it can be useful for modified code dealing with charge groups.
1135      */
1136     fr->ncg_force           = ncg_force;
1137     fr->natoms_force        = natoms_force;
1138     fr->natoms_force_constr = natoms_force_constr;
1139
1140     if (fr->natoms_force_constr > fr->nalloc_force)
1141     {
1142         fr->nalloc_force = over_alloc_dd(fr->natoms_force_constr);
1143
1144         if (fr->bTwinRange)
1145         {
1146             srenew(fr->f_twin,fr->nalloc_force);
1147         }
1148     }
1149
1150     if (fr->bF_NoVirSum)
1151     {
1152         fr->f_novirsum_n = natoms_f_novirsum;
1153         if (fr->f_novirsum_n > fr->f_novirsum_nalloc)
1154         {
1155             fr->f_novirsum_nalloc = over_alloc_dd(fr->f_novirsum_n);
1156             srenew(fr->f_novirsum_alloc,fr->f_novirsum_nalloc);
1157         }
1158     }
1159     else
1160     {
1161         fr->f_novirsum_n = 0;
1162     }
1163 }
1164
1165 static real cutoff_inf(real cutoff)
1166 {
1167     if (cutoff == 0)
1168     {
1169         cutoff = GMX_CUTOFF_INF;
1170     }
1171
1172     return cutoff;
1173 }
1174
1175 bool can_use_allvsall(const t_inputrec *ir, const gmx_mtop_t *mtop,
1176                       bool bPrintNote,t_commrec *cr,FILE *fp)
1177 {
1178     bool bAllvsAll;
1179
1180     bAllvsAll =
1181         (
1182          ir->rlist==0            &&
1183          ir->rcoulomb==0         &&
1184          ir->rvdw==0             &&
1185          ir->ePBC==epbcNONE      &&
1186          ir->vdwtype==evdwCUT    &&
1187          ir->coulombtype==eelCUT &&
1188          ir->efep==efepNO        &&
1189          (ir->implicit_solvent == eisNO || 
1190           (ir->implicit_solvent==eisGBSA && (ir->gb_algorithm==egbSTILL || 
1191                                              ir->gb_algorithm==egbHCT   || 
1192                                              ir->gb_algorithm==egbOBC))) &&
1193          getenv("GMX_NO_ALLVSALL") == NULL
1194             );
1195     
1196     if (bAllvsAll && ir->opts.ngener > 1)
1197     {
1198         const char *note="NOTE: Can not use all-vs-all force loops, because there are multiple energy monitor groups; you might get significantly higher performance when using only a single energy monitor group.\n";
1199
1200         if (bPrintNote)
1201         {
1202             if (MASTER(cr))
1203             {
1204                 fprintf(stderr,"\n%s\n",note);
1205             }
1206             if (fp != NULL)
1207             {
1208                 fprintf(fp,"\n%s\n",note);
1209             }
1210         }
1211         bAllvsAll = FALSE;
1212     }
1213
1214     if(bAllvsAll && fp && MASTER(cr))
1215     {
1216         fprintf(fp,"\nUsing accelerated all-vs-all kernels.\n\n");
1217     }
1218     
1219     return bAllvsAll;
1220 }
1221
1222
1223 void init_forcerec(FILE *fp,
1224                    const output_env_t oenv,
1225                    t_forcerec *fr,
1226                    t_fcdata   *fcd,
1227                    const t_inputrec *ir,
1228                    const gmx_mtop_t *mtop,
1229                    const t_commrec  *cr,
1230                    matrix     box,
1231                    bool       bMolEpot,
1232                    const char *tabfn,
1233                    const char *tabpfn,
1234                    const char *tabbfn,
1235                    bool       bNoSolvOpt,
1236                    real       print_force)
1237 {
1238     int     i,j,m,natoms,ngrp,negp_pp,negptable,egi,egj;
1239     real    rtab;
1240     char    *env;
1241     double  dbl;
1242     rvec    box_size;
1243     const t_block *cgs;
1244     bool    bGenericKernelOnly;
1245     bool    bTab,bSep14tab,bNormalnblists;
1246     t_nblists *nbl;
1247     int     *nm_ind,egp_flags;
1248     
1249     fr->bDomDec = DOMAINDECOMP(cr);
1250
1251     natoms = mtop->natoms;
1252
1253     if (check_box(ir->ePBC,box))
1254     {
1255         gmx_fatal(FARGS,check_box(ir->ePBC,box));
1256     }
1257     
1258     /* Test particle insertion ? */
1259     if (EI_TPI(ir->eI)) {
1260         /* Set to the size of the molecule to be inserted (the last one) */
1261         /* Because of old style topologies, we have to use the last cg
1262          * instead of the last molecule type.
1263          */
1264         cgs = &mtop->moltype[mtop->molblock[mtop->nmolblock-1].type].cgs;
1265         fr->n_tpi = cgs->index[cgs->nr] - cgs->index[cgs->nr-1];
1266         if (fr->n_tpi != mtop->mols.index[mtop->mols.nr] - mtop->mols.index[mtop->mols.nr-1]) {
1267             gmx_fatal(FARGS,"The molecule to insert can not consist of multiple charge groups.\nMake it a single charge group.");
1268         }
1269     } else {
1270         fr->n_tpi = 0;
1271     }
1272     
1273     /* Copy the user determined parameters */
1274     fr->userint1 = ir->userint1;
1275     fr->userint2 = ir->userint2;
1276     fr->userint3 = ir->userint3;
1277     fr->userint4 = ir->userint4;
1278     fr->userreal1 = ir->userreal1;
1279     fr->userreal2 = ir->userreal2;
1280     fr->userreal3 = ir->userreal3;
1281     fr->userreal4 = ir->userreal4;
1282     
1283     /* Shell stuff */
1284     fr->fc_stepsize = ir->fc_stepsize;
1285     
1286     /* Free energy */
1287     fr->efep          = ir->efep;
1288     fr->sc_alpha      = ir->sc_alpha;
1289     fr->sc_power      = ir->sc_power;
1290     fr->sc_sigma6_def = pow(ir->sc_sigma,6);
1291     fr->sc_sigma6_min = pow(ir->sc_sigma_min,6);
1292     env = getenv("GMX_SCSIGMA_MIN");
1293     if (env != NULL)
1294     {
1295         dbl = 0;
1296         sscanf(env,"%lf",&dbl);
1297         fr->sc_sigma6_min = pow(dbl,6);
1298         if (fp)
1299         {
1300             fprintf(fp,"Setting the minimum soft core sigma to %g nm\n",dbl);
1301         }
1302     }
1303
1304     bGenericKernelOnly = FALSE;
1305     if (getenv("GMX_NB_GENERIC") != NULL)
1306     {
1307         if (fp != NULL)
1308         {
1309             fprintf(fp,
1310                     "Found environment variable GMX_NB_GENERIC.\n"
1311                     "Disabling interaction-specific nonbonded kernels.\n\n");
1312         }
1313         bGenericKernelOnly = TRUE;
1314         bNoSolvOpt         = TRUE;
1315     }
1316     
1317     fr->UseOptimizedKernels = (getenv("GMX_NOOPTIMIZEDKERNELS") == NULL);
1318     if(fp && fr->UseOptimizedKernels==FALSE)
1319     {
1320         fprintf(fp,
1321                 "\nFound environment variable GMX_NOOPTIMIZEDKERNELS.\n"
1322                 "Disabling SSE/SSE2/Altivec/ia64/Power6/Bluegene specific kernels.\n\n");
1323     }    
1324     
1325     /* Check if we can/should do all-vs-all kernels */
1326     fr->bAllvsAll       = can_use_allvsall(ir,mtop,FALSE,NULL,NULL);
1327     fr->AllvsAll_work   = NULL;
1328     fr->AllvsAll_workgb = NULL;
1329
1330     
1331     
1332     /* Neighbour searching stuff */
1333     fr->bGrid      = (ir->ns_type == ensGRID);
1334     fr->ePBC       = ir->ePBC;
1335     fr->bMolPBC    = ir->bPeriodicMols;
1336     fr->rc_scaling = ir->refcoord_scaling;
1337     copy_rvec(ir->posres_com,fr->posres_com);
1338     copy_rvec(ir->posres_comB,fr->posres_comB);
1339     fr->rlist      = cutoff_inf(ir->rlist);
1340     fr->rlistlong  = cutoff_inf(ir->rlistlong);
1341     fr->eeltype    = ir->coulombtype;
1342     fr->vdwtype    = ir->vdwtype;
1343     
1344     fr->bTwinRange = fr->rlistlong > fr->rlist;
1345     fr->bEwald     = (EEL_PME(fr->eeltype) || fr->eeltype==eelEWALD);
1346     
1347     fr->reppow     = mtop->ffparams.reppow;
1348     fr->bvdwtab    = (fr->vdwtype != evdwCUT ||
1349                       !gmx_within_tol(fr->reppow,12.0,10*GMX_DOUBLE_EPS));
1350     fr->bcoultab   = (!(fr->eeltype == eelCUT || EEL_RF(fr->eeltype)) ||
1351                       fr->eeltype == eelRF_ZERO);
1352     
1353     if (getenv("GMX_FORCE_TABLES"))
1354     {
1355         fr->bvdwtab  = TRUE;
1356         fr->bcoultab = TRUE;
1357     }
1358     
1359     if (fp) {
1360         fprintf(fp,"Table routines are used for coulomb: %s\n",bool_names[fr->bcoultab]);
1361         fprintf(fp,"Table routines are used for vdw:     %s\n",bool_names[fr->bvdwtab ]);
1362     }
1363     
1364     /* Tables are used for direct ewald sum */
1365     if(fr->bEwald)
1366     {
1367         if (EEL_PME(ir->coulombtype))
1368         {
1369             if (fp)
1370                 fprintf(fp,"Will do PME sum in reciprocal space.\n");
1371             please_cite(fp,"Essman95a");
1372             
1373             if (ir->ewald_geometry == eewg3DC)
1374             {
1375                 if (fp)
1376                 {
1377                     fprintf(fp,"Using the Ewald3DC correction for systems with a slab geometry.\n");
1378                 }
1379                 please_cite(fp,"In-Chul99a");
1380             }
1381         }
1382         fr->ewaldcoeff=calc_ewaldcoeff(ir->rcoulomb, ir->ewald_rtol);
1383         init_ewald_tab(&(fr->ewald_table), cr, ir, fp);
1384         if (fp)
1385         {
1386             fprintf(fp,"Using a Gaussian width (1/beta) of %g nm for Ewald\n",
1387                     1/fr->ewaldcoeff);
1388         }
1389     }
1390     
1391     /* Electrostatics */
1392     fr->epsilon_r  = ir->epsilon_r;
1393     fr->epsilon_rf = ir->epsilon_rf;
1394     fr->fudgeQQ    = mtop->ffparams.fudgeQQ;
1395     fr->rcoulomb_switch = ir->rcoulomb_switch;
1396     fr->rcoulomb        = cutoff_inf(ir->rcoulomb);
1397     
1398     /* Parameters for generalized RF */
1399     fr->zsquare = 0.0;
1400     fr->temp    = 0.0;
1401     
1402     if (fr->eeltype == eelGRF)
1403     {
1404         init_generalized_rf(fp,mtop,ir,fr);
1405     }
1406     else if (EEL_FULL(fr->eeltype) || (fr->eeltype == eelSHIFT) || 
1407              (fr->eeltype == eelUSER) || (fr->eeltype == eelSWITCH))
1408     {
1409         /* We must use the long range cut-off for neighboursearching...
1410          * An extra range of e.g. 0.1 nm (half the size of a charge group)
1411          * is necessary for neighboursearching. This allows diffusion 
1412          * into the cut-off range (between neighborlist updates), 
1413          * and gives more accurate forces because all atoms within the short-range
1414          * cut-off rc must be taken into account, while the ns criterium takes
1415          * only those with the center of geometry within the cut-off.
1416          * (therefore we have to add half the size of a charge group, plus
1417          * something to account for diffusion if we have nstlist > 1)
1418          */
1419         for(m=0; (m<DIM); m++)
1420             box_size[m]=box[m][m];
1421         
1422         if (fr->eeltype == eelPPPM && fr->phi == NULL)
1423             snew(fr->phi,natoms);
1424         
1425         if ((fr->eeltype==eelPPPM) || (fr->eeltype==eelPOISSON) || 
1426             (fr->eeltype == eelSHIFT && fr->rcoulomb > fr->rcoulomb_switch))
1427             set_shift_consts(fp,fr->rcoulomb_switch,fr->rcoulomb,box_size,fr);
1428     }
1429     
1430     fr->bF_NoVirSum = (EEL_FULL(fr->eeltype) ||
1431                        gmx_mtop_ftype_count(mtop,F_POSRES) > 0 ||
1432                        IR_ELEC_FIELD(*ir));
1433     
1434     /* Mask that says whether or not this NBF list should be computed */
1435     /*  if (fr->bMask == NULL) {
1436         ngrp = ir->opts.ngener*ir->opts.ngener;
1437         snew(fr->bMask,ngrp);*/
1438     /* Defaults to always */
1439     /*    for(i=0; (i<ngrp); i++)
1440           fr->bMask[i] = TRUE;
1441           }*/
1442     
1443     if (ncg_mtop(mtop) > fr->cg_nalloc && !DOMAINDECOMP(cr)) {
1444         /* Count the total number of charge groups */
1445         fr->cg_nalloc = ncg_mtop(mtop);
1446         srenew(fr->cg_cm,fr->cg_nalloc);
1447     }
1448     if (fr->shift_vec == NULL)
1449         snew(fr->shift_vec,SHIFTS);
1450     
1451     if (fr->fshift == NULL)
1452         snew(fr->fshift,SHIFTS);
1453     
1454     if (fr->nbfp == NULL) {
1455         fr->ntype = mtop->ffparams.atnr;
1456         fr->bBHAM = (mtop->ffparams.functype[0] == F_BHAM);
1457         fr->nbfp  = mk_nbfp(&mtop->ffparams,fr->bBHAM);
1458     }
1459     
1460     /* Copy the energy group exclusions */
1461     fr->egp_flags = ir->opts.egp_flags;
1462     
1463     /* Van der Waals stuff */
1464     fr->rvdw        = cutoff_inf(ir->rvdw);
1465     fr->rvdw_switch = ir->rvdw_switch;
1466     if ((fr->vdwtype != evdwCUT) && (fr->vdwtype != evdwUSER) && !fr->bBHAM) {
1467         if (fr->rvdw_switch >= fr->rvdw)
1468             gmx_fatal(FARGS,"rvdw_switch (%f) must be < rvdw (%f)",
1469                       fr->rvdw_switch,fr->rvdw);
1470         if (fp)
1471             fprintf(fp,"Using %s Lennard-Jones, switch between %g and %g nm\n",
1472                     (fr->eeltype==eelSWITCH) ? "switched":"shifted",
1473                     fr->rvdw_switch,fr->rvdw);
1474     } 
1475     
1476     if (fr->bBHAM && (fr->vdwtype == evdwSHIFT || fr->vdwtype == evdwSWITCH))
1477         gmx_fatal(FARGS,"Switch/shift interaction not supported with Buckingham");
1478     
1479     if (fp)
1480         fprintf(fp,"Cut-off's:   NS: %g   Coulomb: %g   %s: %g\n",
1481                 fr->rlist,fr->rcoulomb,fr->bBHAM ? "BHAM":"LJ",fr->rvdw);
1482     
1483     fr->eDispCorr = ir->eDispCorr;
1484     if (ir->eDispCorr != edispcNO)
1485     {
1486         set_avcsixtwelve(fp,fr,mtop);
1487     }
1488     
1489     if (fr->bBHAM)
1490     {
1491         set_bham_b_max(fp,fr,mtop);
1492     }
1493
1494     fr->bGB = (ir->implicit_solvent == eisGBSA);
1495         fr->gb_epsilon_solvent = ir->gb_epsilon_solvent;
1496
1497     /* Copy the GBSA data (radius, volume and surftens for each
1498      * atomtype) from the topology atomtype section to forcerec.
1499      */
1500     snew(fr->atype_radius,fr->ntype);
1501     snew(fr->atype_vol,fr->ntype);
1502     snew(fr->atype_surftens,fr->ntype);
1503     snew(fr->atype_gb_radius,fr->ntype);
1504     snew(fr->atype_S_hct,fr->ntype);
1505
1506     if (mtop->atomtypes.nr > 0)
1507     {
1508         for(i=0;i<fr->ntype;i++)
1509             fr->atype_radius[i] =mtop->atomtypes.radius[i];
1510         for(i=0;i<fr->ntype;i++)
1511             fr->atype_vol[i] = mtop->atomtypes.vol[i];
1512         for(i=0;i<fr->ntype;i++)
1513             fr->atype_surftens[i] = mtop->atomtypes.surftens[i];
1514         for(i=0;i<fr->ntype;i++)
1515             fr->atype_gb_radius[i] = mtop->atomtypes.gb_radius[i];
1516         for(i=0;i<fr->ntype;i++)
1517             fr->atype_S_hct[i] = mtop->atomtypes.S_hct[i];
1518     }  
1519         
1520         /* Generate the GB table if needed */
1521         if(fr->bGB)
1522         {
1523 #ifdef GMX_DOUBLE
1524                 fr->gbtabscale=2000;
1525 #else
1526                 fr->gbtabscale=500;
1527 #endif
1528                 
1529                 fr->gbtabr=100;
1530                 fr->gbtab=make_gb_table(fp,oenv,fr,tabpfn,fr->gbtabscale);
1531
1532         init_gb(&fr->born,cr,fr,ir,mtop,ir->rgbradii,ir->gb_algorithm);
1533
1534         /* Copy local gb data (for dd, this is done in dd_partition_system) */
1535         if (!DOMAINDECOMP(cr))
1536         {
1537             make_local_gb(cr,fr->born,ir->gb_algorithm);
1538         }
1539     }
1540
1541     /* Set the charge scaling */
1542     if (fr->epsilon_r != 0)
1543         fr->epsfac = ONE_4PI_EPS0/fr->epsilon_r;
1544     else
1545         /* eps = 0 is infinite dieletric: no coulomb interactions */
1546         fr->epsfac = 0;
1547     
1548     /* Reaction field constants */
1549     if (EEL_RF(fr->eeltype))
1550         calc_rffac(fp,fr->eeltype,fr->epsilon_r,fr->epsilon_rf,
1551                    fr->rcoulomb,fr->temp,fr->zsquare,box,
1552                    &fr->kappa,&fr->k_rf,&fr->c_rf);
1553     
1554     set_chargesum(fp,fr,mtop);
1555     
1556     /* if we are using LR electrostatics, and they are tabulated,
1557      * the tables will contain modified coulomb interactions.
1558      * Since we want to use the non-shifted ones for 1-4
1559      * coulombic interactions, we must have an extra set of tables.
1560      */
1561     
1562     /* Construct tables.
1563      * A little unnecessary to make both vdw and coul tables sometimes,
1564      * but what the heck... */
1565     
1566     bTab = fr->bcoultab || fr->bvdwtab;
1567
1568     bSep14tab = ((!bTab || fr->eeltype!=eelCUT || fr->vdwtype!=evdwCUT ||
1569                   fr->bBHAM) &&
1570                  (gmx_mtop_ftype_count(mtop,F_LJ14) > 0 ||
1571                   gmx_mtop_ftype_count(mtop,F_LJC14_Q) > 0 ||
1572                   gmx_mtop_ftype_count(mtop,F_LJC_PAIRS_NB) > 0));
1573
1574     negp_pp = ir->opts.ngener - ir->nwall;
1575     negptable = 0;
1576     if (!bTab) {
1577         bNormalnblists = TRUE;
1578         fr->nnblists = 1;
1579     } else {
1580         bNormalnblists = (ir->eDispCorr != edispcNO);
1581         for(egi=0; egi<negp_pp; egi++) {
1582             for(egj=egi;  egj<negp_pp; egj++) {
1583                 egp_flags = ir->opts.egp_flags[GID(egi,egj,ir->opts.ngener)];
1584                 if (!(egp_flags & EGP_EXCL)) {
1585                     if (egp_flags & EGP_TABLE) {
1586                         negptable++;
1587                     } else {
1588                         bNormalnblists = TRUE;
1589                     }
1590                 }
1591             }
1592         }
1593         if (bNormalnblists) {
1594             fr->nnblists = negptable + 1;
1595         } else {
1596             fr->nnblists = negptable;
1597         }
1598         if (fr->nnblists > 1)
1599             snew(fr->gid2nblists,ir->opts.ngener*ir->opts.ngener);
1600     }
1601     snew(fr->nblists,fr->nnblists);
1602     
1603     /* This code automatically gives table length tabext without cut-off's,
1604      * in that case grompp should already have checked that we do not need
1605      * normal tables and we only generate tables for 1-4 interactions.
1606      */
1607     rtab = ir->rlistlong + ir->tabext;
1608
1609     if (bTab) {
1610         /* make tables for ordinary interactions */
1611         if (bNormalnblists) {
1612             make_nbf_tables(fp,oenv,fr,rtab,cr,tabfn,NULL,NULL,&fr->nblists[0]);
1613             if (!bSep14tab)
1614                 fr->tab14 = fr->nblists[0].tab;
1615             m = 1;
1616         } else {
1617             m = 0;
1618         }
1619         if (negptable > 0) {
1620             /* Read the special tables for certain energy group pairs */
1621             nm_ind = mtop->groups.grps[egcENER].nm_ind;
1622             for(egi=0; egi<negp_pp; egi++) {
1623                 for(egj=egi;  egj<negp_pp; egj++) {
1624                     egp_flags = ir->opts.egp_flags[GID(egi,egj,ir->opts.ngener)];
1625                     if ((egp_flags & EGP_TABLE) && !(egp_flags & EGP_EXCL)) {
1626                         nbl = &(fr->nblists[m]);
1627                         if (fr->nnblists > 1) {
1628                             fr->gid2nblists[GID(egi,egj,ir->opts.ngener)] = m;
1629                         }
1630                         /* Read the table file with the two energy groups names appended */
1631                         make_nbf_tables(fp,oenv,fr,rtab,cr,tabfn,
1632                                         *mtop->groups.grpname[nm_ind[egi]],
1633                                         *mtop->groups.grpname[nm_ind[egj]],
1634                                         &fr->nblists[m]);
1635                         m++;
1636                     } else if (fr->nnblists > 1) {
1637                         fr->gid2nblists[GID(egi,egj,ir->opts.ngener)] = 0;
1638                     }
1639                 }
1640             }
1641         }
1642     }
1643     if (bSep14tab)
1644     {
1645         /* generate extra tables with plain Coulomb for 1-4 interactions only */
1646         fr->tab14 = make_tables(fp,oenv,fr,MASTER(cr),tabpfn,rtab,
1647                                 GMX_MAKETABLES_14ONLY);
1648     }
1649     
1650     /* Wall stuff */
1651     fr->nwall = ir->nwall;
1652     if (ir->nwall && ir->wall_type==ewtTABLE)
1653     {
1654         make_wall_tables(fp,oenv,ir,tabfn,&mtop->groups,fr);
1655     }
1656     
1657     if (fcd && tabbfn) {
1658         fcd->bondtab  = make_bonded_tables(fp,
1659                                            F_TABBONDS,F_TABBONDSNC,
1660                                            mtop,tabbfn,"b");
1661         fcd->angletab = make_bonded_tables(fp,
1662                                            F_TABANGLES,-1,
1663                                            mtop,tabbfn,"a");
1664         fcd->dihtab   = make_bonded_tables(fp,
1665                                            F_TABDIHS,-1,
1666                                            mtop,tabbfn,"d");
1667     } else {
1668         if (debug)
1669             fprintf(debug,"No fcdata or table file name passed, can not read table, can not do bonded interactions\n");
1670     }
1671     
1672     if (ir->eDispCorr != edispcNO)
1673     {
1674         calc_enervirdiff(fp,ir->eDispCorr,fr);
1675     }
1676
1677     /* QM/MM initialization if requested
1678      */
1679     if (ir->bQMMM)
1680     {
1681         fprintf(stderr,"QM/MM calculation requested.\n");
1682     }
1683     
1684     fr->bQMMM      = ir->bQMMM;   
1685     fr->qr         = mk_QMMMrec();
1686     
1687     /* Set all the static charge group info */
1688     fr->cginfo_mb = init_cginfo_mb(fp,mtop,fr,bNoSolvOpt,
1689                                    &fr->bExcl_IntraCGAll_InterCGNone);
1690     if (DOMAINDECOMP(cr)) {
1691         fr->cginfo = NULL;
1692     } else {
1693         fr->cginfo = cginfo_expand(mtop->nmolblock,fr->cginfo_mb);
1694     }
1695     
1696     if (!DOMAINDECOMP(cr))
1697     {
1698         /* When using particle decomposition, the effect of the second argument,
1699          * which sets fr->hcg, is corrected later in do_md and init_em.
1700          */
1701         forcerec_set_ranges(fr,ncg_mtop(mtop),ncg_mtop(mtop),
1702                             mtop->natoms,mtop->natoms,mtop->natoms);
1703     }
1704     
1705     fr->print_force = print_force;
1706
1707
1708     /* coarse load balancing vars */
1709     fr->t_fnbf=0.;
1710     fr->t_wait=0.;
1711     fr->timesteps=0;
1712     
1713     /* Initialize neighbor search */
1714     init_ns(fp,cr,&fr->ns,fr,mtop,box);
1715     
1716     if (cr->duty & DUTY_PP)
1717         gmx_setup_kernels(fp,bGenericKernelOnly);
1718 }
1719
1720 #define pr_real(fp,r) fprintf(fp,"%s: %e\n",#r,r)
1721 #define pr_int(fp,i)  fprintf((fp),"%s: %d\n",#i,i)
1722 #define pr_bool(fp,b) fprintf((fp),"%s: %s\n",#b,bool_names[b])
1723
1724 void pr_forcerec(FILE *fp,t_forcerec *fr,t_commrec *cr)
1725 {
1726   int i;
1727
1728   pr_real(fp,fr->rlist);
1729   pr_real(fp,fr->rcoulomb);
1730   pr_real(fp,fr->fudgeQQ);
1731   pr_bool(fp,fr->bGrid);
1732   pr_bool(fp,fr->bTwinRange);
1733   /*pr_int(fp,fr->cg0);
1734     pr_int(fp,fr->hcg);*/
1735   for(i=0; i<fr->nnblists; i++)
1736     pr_int(fp,fr->nblists[i].tab.n);
1737   pr_real(fp,fr->rcoulomb_switch);
1738   pr_real(fp,fr->rcoulomb);
1739   
1740   fflush(fp);
1741 }