Merge branch release-5-1 into release-2016
authorMark Abraham <mark.j.abraham@gmail.com>
Wed, 22 Jun 2016 08:22:48 +0000 (10:22 +0200)
committerMark Abraham <mark.j.abraham@gmail.com>
Wed, 22 Jun 2016 08:22:48 +0000 (10:22 +0200)
Change-Id: I175bb4c5d313a33c0c95446f85b1af2128ce301c

1  2 
src/gromacs/domdec/domdec_topology.cpp
src/gromacs/gmxpreprocess/specbond.cpp

index ba47ff540bd1e34ea249db5196ccc3b16012459b,81b6f80ad43105e5836ee49b5bb2c26ca89b50f9..548e373e8564e9237308a1ace4970eccb2fe027f
@@@ -1,7 -1,7 +1,7 @@@
  /*
   * This file is part of the GROMACS molecular simulation package.
   *
-  * Copyright (c) 2006,2007,2008,2009,2010,2011,2012,2013,2014,2015, by the GROMACS development team, led by
+  * Copyright (c) 2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016, by the GROMACS development team, led by
   * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
   * and including many others, as listed in the AUTHORS file in the
   * top-level source directory and at http://www.gromacs.org.
  #include <string.h>
  
  #include <algorithm>
 +#include <string>
  
  #include "gromacs/domdec/domdec.h"
  #include "gromacs/domdec/domdec_network.h"
 -#include "gromacs/legacyheaders/chargegroup.h"
 -#include "gromacs/legacyheaders/force.h"
 -#include "gromacs/legacyheaders/gmx_ga2la.h"
 -#include "gromacs/legacyheaders/gmx_omp_nthreads.h"
 -#include "gromacs/legacyheaders/names.h"
 -#include "gromacs/legacyheaders/network.h"
 -#include "gromacs/legacyheaders/typedefs.h"
 -#include "gromacs/legacyheaders/vsite.h"
 -#include "gromacs/legacyheaders/types/commrec.h"
 +#include "gromacs/domdec/ga2la.h"
 +#include "gromacs/gmxlib/chargegroup.h"
 +#include "gromacs/gmxlib/network.h"
  #include "gromacs/math/vec.h"
 +#include "gromacs/mdlib/force.h"
 +#include "gromacs/mdlib/forcerec.h"
 +#include "gromacs/mdlib/gmx_omp_nthreads.h"
 +#include "gromacs/mdlib/vsite.h"
 +#include "gromacs/mdtypes/commrec.h"
 +#include "gromacs/mdtypes/inputrec.h"
 +#include "gromacs/mdtypes/md_enums.h"
 +#include "gromacs/mdtypes/state.h"
  #include "gromacs/pbcutil/mshift.h"
  #include "gromacs/pbcutil/pbc.h"
  #include "gromacs/topology/mtop_util.h"
  #include "gromacs/topology/topsort.h"
  #include "gromacs/utility/cstringutil.h"
 +#include "gromacs/utility/exceptions.h"
  #include "gromacs/utility/fatalerror.h"
  #include "gromacs/utility/gmxassert.h"
  #include "gromacs/utility/smalloc.h"
 +#include "gromacs/utility/stringutil.h"
  
  #include "domdec_constraints.h"
  #include "domdec_internal.h"
@@@ -107,8 -102,7 +107,8 @@@ typedef struct 
  } thread_work_t;
  
  /*! \brief Struct for the reverse topology: links bonded interactions to atomsx */
 -typedef struct gmx_reverse_top {
 +struct gmx_reverse_top_t
 +{
      //! @cond Doxygen_Suppress
      gmx_bool         bExclRequired;               /**< Do we require all exclusions to be assigned? */
      int              n_excl_at_max;               /**< The maximum number of exclusions one atom can have */
      /* Work data structures for multi-threading */
      int            nthread;           /**< The number of threads to be used */
      thread_work_t *th_work;           /**< Thread work array for local topology generation */
 -
 -    /* Pointers only used for an error message */
 -    gmx_mtop_t     *err_top_global; /**< Pointer to the global top, only used for error reporting */
 -    gmx_localtop_t *err_top_local;  /**< Pointer to the local top, only used for error reporting */
      //! @endcond
 -} gmx_reverse_top_t;
 +};
  
  /*! \brief Returns the number of atom entries for il in gmx_reverse_top_t */
  static int nral_rt(int ftype)
@@@ -160,7 -158,7 +160,7 @@@ static gmx_bool dd_check_ftype(int ftyp
  }
  
  /*! \brief Print a header on error messages */
 -static void print_error_header(FILE *fplog, char *moltypename, int nprint)
 +static void print_error_header(FILE *fplog, const char *moltypename, int nprint)
  {
      fprintf(fplog, "\nMolecule type '%s'\n", moltypename);
      fprintf(stderr, "\nMolecule type '%s'\n", moltypename);
  
  /*! \brief Help print error output when interactions are missing */
  static void print_missing_interactions_mb(FILE *fplog, t_commrec *cr,
 -                                          gmx_reverse_top_t *rt,
 -                                          char *moltypename,
 -                                          reverse_ilist_t *ril,
 +                                          const gmx_reverse_top_t *rt,
 +                                          const char *moltypename,
 +                                          const reverse_ilist_t *ril,
                                            int a_start, int a_end,
                                            int nat_mol, int nmol,
 -                                          t_idef *idef)
 +                                          const t_idef *idef)
  {
 -    int      nril_mol, *assigned, *gatindex;
 -    int      ftype, ftype_j, nral, i, j_mol, j, a0, a0_mol, mol, a;
 -    int      nprint;
 -    t_ilist *il;
 -    t_iatom *ia;
 -    gmx_bool bFound;
 -
 -    nril_mol = ril->index[nat_mol];
 +    int *assigned;
 +    int  nril_mol = ril->index[nat_mol];
      snew(assigned, nmol*nril_mol);
  
 -    gatindex = cr->dd->gatindex;
 -    for (ftype = 0; ftype < F_NRE; ftype++)
 +    int *gatindex = cr->dd->gatindex;
 +    for (int ftype = 0; ftype < F_NRE; ftype++)
      {
          if (dd_check_ftype(ftype, rt->bBCheck, rt->bConstr, rt->bSettle))
          {
 -            nral = NRAL(ftype);
 -            il   = &idef->il[ftype];
 -            ia   = il->iatoms;
 -            for (i = 0; i < il->nr; i += 1+nral)
 +            int            nral = NRAL(ftype);
 +            const t_ilist *il   = &idef->il[ftype];
 +            const t_iatom *ia   = il->iatoms;
 +            for (int i = 0; i < il->nr; i += 1+nral)
              {
 -                a0     = gatindex[ia[1]];
 +                int a0 = gatindex[ia[1]];
                  /* Check if this interaction is in
                   * the currently checked molblock.
                   */
                  if (a0 >= a_start && a0 < a_end)
                  {
 -                    mol    = (a0 - a_start)/nat_mol;
 -                    a0_mol = (a0 - a_start) - mol*nat_mol;
 -                    j_mol  = ril->index[a0_mol];
 -                    bFound = FALSE;
 -                    while (j_mol < ril->index[a0_mol+1] && !bFound)
 +                    int  mol    = (a0 - a_start)/nat_mol;
 +                    int  a0_mol = (a0 - a_start) - mol*nat_mol;
 +                    int  j_mol  = ril->index[a0_mol];
 +                    bool found  = false;
 +                    while (j_mol < ril->index[a0_mol+1] && !found)
                      {
 -                        j       = mol*nril_mol + j_mol;
 -                        ftype_j = ril->il[j_mol];
 +                        int j       = mol*nril_mol + j_mol;
 +                        int ftype_j = ril->il[j_mol];
                          /* Here we need to check if this interaction has
                           * not already been assigned, since we could have
                           * multiply defined interactions.
                              assigned[j] == 0)
                          {
                              /* Check the atoms */
 -                            bFound = TRUE;
 -                            for (a = 0; a < nral; a++)
 +                            found = true;
 +                            for (int a = 0; a < nral; a++)
                              {
                                  if (gatindex[ia[1+a]] !=
                                      a_start + mol*nat_mol + ril->il[j_mol+2+a])
                                  {
 -                                    bFound = FALSE;
 +                                    found = false;
                                  }
                              }
 -                            if (bFound)
 +                            if (found)
                              {
                                  assigned[j] = 1;
                              }
                          }
                          j_mol += 2 + nral_rt(ftype_j);
                      }
 -                    if (!bFound)
 +                    if (!found)
                      {
                          gmx_incons("Some interactions seem to be assigned multiple times");
                      }
  
      gmx_sumi(nmol*nril_mol, assigned, cr);
  
 -    nprint = 10;
 -    i      = 0;
 -    for (mol = 0; mol < nmol; mol++)
 +    int nprint = 10;
 +    int i      = 0;
 +    for (int mol = 0; mol < nmol; mol++)
      {
 -        j_mol = 0;
 +        int j_mol = 0;
          while (j_mol < nril_mol)
          {
 -            ftype = ril->il[j_mol];
 -            nral  = NRAL(ftype);
 -            j     = mol*nril_mol + j_mol;
 +            int ftype = ril->il[j_mol];
 +            int nral  = NRAL(ftype);
 +            int j     = mol*nril_mol + j_mol;
              if (assigned[j] == 0 &&
                  !(interaction_function[ftype].flags & IF_VSITE))
              {
                              interaction_function[ftype].longname);
                      fprintf(stderr, "%20s atoms",
                              interaction_function[ftype].longname);
 +                    int a;
                      for (a = 0; a < nral; a++)
                      {
                          fprintf(fplog, "%5d", ril->il[j_mol+2+a]+1);
  
  /*! \brief Help print error output when interactions are missing */
  static void print_missing_interactions_atoms(FILE *fplog, t_commrec *cr,
 -                                             gmx_mtop_t *mtop, t_idef *idef)
 +                                             const gmx_mtop_t *mtop,
 +                                             const t_idef *idef)
  {
 -    int                mb, a_start, a_end;
 -    gmx_molblock_t    *molb;
 -    gmx_reverse_top_t *rt;
 +    int                      mb, a_start, a_end;
 +    const gmx_molblock_t    *molb;
 +    const gmx_reverse_top_t *rt;
  
      rt = cr->dd->reverse_top;
  
      }
  }
  
 -void dd_print_missing_interactions(FILE *fplog, t_commrec *cr, int local_count,  gmx_mtop_t *top_global, t_state *state_local)
 +void dd_print_missing_interactions(FILE *fplog, t_commrec *cr,
 +                                   int local_count,
 +                                   const gmx_mtop_t *top_global,
 +                                   const gmx_localtop_t *top_local,
 +                                   t_state *state_local)
  {
      int             ndiff_tot, cl[F_NRE], n, ndiff, rest_global, rest_local;
      int             ftype, nral;
      char            buf[STRLEN];
      gmx_domdec_t   *dd;
 -    gmx_mtop_t     *err_top_global;
 -    gmx_localtop_t *err_top_local;
  
      dd = cr->dd;
  
 -    err_top_global = dd->reverse_top->err_top_global;
 -    err_top_local  = dd->reverse_top->err_top_local;
 -
      if (fplog)
      {
          fprintf(fplog, "\nNot all bonded interactions have been properly assigned to the domain decomposition cells\n");
      for (ftype = 0; ftype < F_NRE; ftype++)
      {
          nral      = NRAL(ftype);
 -        cl[ftype] = err_top_local->idef.il[ftype].nr/(1+nral);
 +        cl[ftype] = top_local->idef.il[ftype].nr/(1+nral);
      }
  
      gmx_sumi(F_NRE, cl, cr);
                  || (dd->reverse_top->bConstr && ftype == F_CONSTR)
                  || (dd->reverse_top->bSettle && ftype == F_SETTLE))
              {
 -                n    = gmx_mtop_ftype_count(err_top_global, ftype);
 +                n    = gmx_mtop_ftype_count(top_global, ftype);
                  if (ftype == F_CONSTR)
                  {
 -                    n += gmx_mtop_ftype_count(err_top_global, F_CONSTRNC);
 +                    n += gmx_mtop_ftype_count(top_global, F_CONSTRNC);
                  }
                  ndiff = cl[ftype] - n;
                  if (ndiff != 0)
          }
      }
  
 -    print_missing_interactions_atoms(fplog, cr, err_top_global,
 -                                     &err_top_local->idef);
 +    print_missing_interactions_atoms(fplog, cr, top_global, &top_local->idef);
      write_dd_pdb("dd_dump_err", 0, "dump", top_global, cr,
                   -1, state_local->x, state_local->box);
 -    if (DDMASTER(dd))
 +
 +    std::string errorMessage;
 +
 +    if (ndiff_tot > 0)
      {
 -        if (ndiff_tot > 0)
 -        {
 -            gmx_incons("One or more interactions were multiple assigned in the domain decompostion");
 -        }
 -        else
 -        {
 -            gmx_fatal(FARGS, "%d of the %d bonded interactions could not be calculated because some atoms involved moved further apart than the multi-body cut-off distance (%g nm) or the two-body cut-off distance (%g nm), see option -rdd, for pairs and tabulated bonds also see option -ddcheck", -ndiff_tot, cr->dd->nbonded_global, dd_cutoff_multibody(cr->dd), dd_cutoff_twobody(cr->dd));
 -        }
 +        errorMessage = "One or more interactions were assigned to multiple domains of the domain decompostion. Please report this bug.";
 +    }
 +    else
 +    {
 +        errorMessage = gmx::formatString("%d of the %d bonded interactions could not be calculated because some atoms involved moved further apart than the multi-body cut-off distance (%g nm) or the two-body cut-off distance (%g nm), see option -rdd, for pairs and tabulated bonds also see option -ddcheck", -ndiff_tot, cr->dd->nbonded_global, dd_cutoff_multibody(dd), dd_cutoff_twobody(dd));
      }
 +    gmx_fatal_collective(FARGS, cr->mpi_comm_mygroup, MASTER(cr), errorMessage.c_str());
  }
  
  /*! \brief Return global topology molecule information for global atom index \p i_gl */
@@@ -504,8 -507,9 +504,8 @@@ static void count_excls(const t_block *
  }
  
  /*! \brief Run the reverse ilist generation and store it when \p bAssign = TRUE */
 -static int low_make_reverse_ilist(const t_ilist *il_mt,
 -                                  const t_atom *atom,
 -                                  int **vsite_pbc, /* should be const */
 +static int low_make_reverse_ilist(const t_ilist *il_mt, const t_atom *atom,
 +                                  const int * const * vsite_pbc,
                                    int *count,
                                    gmx_bool bConstr, gmx_bool bSettle,
                                    gmx_bool bBCheck,
  {
      int            ftype, nral, i, j, nlink, link;
      const t_ilist *il;
 -    t_iatom       *ia;
 -    atom_id        a;
 +    const t_iatom *ia;
 +    int            a;
      int            nint;
      gmx_bool       bVSite;
  
  /*! \brief Make the reverse ilist: a list of bonded interactions linked to atoms */
  static int make_reverse_ilist(const t_ilist *ilist,
                                const t_atoms *atoms,
 -                              int **vsite_pbc, /* should be const (C issue) */
 +                              const int * const * vsite_pbc,
                                gmx_bool bConstr, gmx_bool bSettle,
                                gmx_bool bBCheck,
                                gmx_bool bLinkToAllAtoms,
@@@ -655,8 -659,8 +655,8 @@@ static void destroy_reverse_ilist(rever
  }
  
  /*! \brief Generate the reverse topology */
 -static gmx_reverse_top_t *make_reverse_top(gmx_mtop_t *mtop, gmx_bool bFE,
 -                                           int ***vsite_pbc_molt,
 +static gmx_reverse_top_t *make_reverse_top(const gmx_mtop_t *mtop, gmx_bool bFE,
 +                                           const int * const * const * vsite_pbc_molt,
                                             gmx_bool bConstr, gmx_bool bSettle,
                                             gmx_bool bBCheck, int *nint)
  {
  }
  
  void dd_make_reverse_top(FILE *fplog,
 -                         gmx_domdec_t *dd, gmx_mtop_t *mtop,
 -                         gmx_vsite_t *vsite,
 -                         t_inputrec *ir, gmx_bool bBCheck)
 +                         gmx_domdec_t *dd, const gmx_mtop_t *mtop,
 +                         const gmx_vsite_t *vsite,
 +                         const t_inputrec *ir, gmx_bool bBCheck)
  {
      if (fplog)
      {
       * excluded pair should appear exactly once.
       */
      rt->bExclRequired = (ir->cutoff_scheme == ecutsGROUP &&
 -                         IR_EXCL_FORCES(*ir));
 +                         inputrecExclForces(ir));
  
      int nexcl, mb;
  
   * atom-indexing organization code with the ifunc-adding code, so that
   * they can see that nral is the same value. */
  static gmx_inline void
 -add_ifunc_for_vsites(t_iatom *tiatoms, gmx_ga2la_t ga2la,
 +add_ifunc_for_vsites(t_iatom *tiatoms, gmx_ga2la_t *ga2la,
                       int nral, gmx_bool bHomeA,
                       int a, int a_gl, int a_mol,
                       const t_iatom *iatoms,
@@@ -1022,7 -1026,7 +1022,7 @@@ static void add_fbposres(int mol, int a
  }
  
  /*! \brief Store a virtual site interaction, complex because of PBC and recursion */
 -static void add_vsite(gmx_ga2la_t ga2la, const int *index, const int *rtil,
 +static void add_vsite(gmx_ga2la_t *ga2la, const int *index, const int *rtil,
                        int ftype, int nral,
                        gmx_bool bHomeA, int a, int a_gl, int a_mol,
                        const t_iatom *iatoms,
@@@ -1258,34 -1262,46 +1258,46 @@@ static void combine_idef(t_idef *dest, 
  
                  ild->nr += ils->nr;
              }
-         }
-     }
-     /* Position restraints need an additional treatment */
-     if (dest->il[F_POSRES].nr > 0)
-     {
-         int n, s, i;
  
-         n = dest->il[F_POSRES].nr/2;
-         if (n > dest->iparams_posres_nalloc)
-         {
-             dest->iparams_posres_nalloc = over_alloc_large(n);
-             srenew(dest->iparams_posres, dest->iparams_posres_nalloc);
-         }
-         /* Set n to the number of original position restraints in dest */
-         for (s = 1; s < nsrc; s++)
-         {
-             n -= src[s].idef.il[F_POSRES].nr/2;
-         }
-         for (s = 1; s < nsrc; s++)
-         {
-             for (i = 0; i < src[s].idef.il[F_POSRES].nr/2; i++)
+             /* Position restraints need an additional treatment */
+             if (ftype == F_POSRES || ftype == F_FBPOSRES)
              {
-                 /* Correct the index into iparams_posres */
-                 dest->il[F_POSRES].iatoms[n*2] = n;
-                 /* Copy the position restraint force parameters */
-                 dest->iparams_posres[n] = src[s].idef.iparams_posres[i];
-                 n++;
+                 int nposres = dest->il[ftype].nr/2;
+                 if (nposres > dest->iparams_posres_nalloc)
+                 {
+                     dest->iparams_posres_nalloc = over_alloc_large(nposres);
+                     srenew(dest->iparams_posres, dest->iparams_posres_nalloc);
+                 }
+                 /* Set nposres to the number of original position restraints in dest */
+                 for (int s = 1; s < nsrc; s++)
+                 {
+                     nposres -= src[s].idef.il[ftype].nr/2;
+                 }
+                 for (int s = 1; s < nsrc; s++)
+                 {
+                     if (ftype == F_POSRES)
+                     {
+                         for (int i = 0; i < src[s].idef.il[ftype].nr/2; i++)
+                         {
+                             /* Correct the index into iparams_posres */
+                             dest->il[ftype].iatoms[nposres*2] = nposres;
+                             /* Copy the position restraint force parameters */
+                             dest->iparams_posres[nposres] = src[s].idef.iparams_posres[i];
+                             nposres++;
+                         }
+                     }
+                     else
+                     {
+                         for (int i = 0; i < src[s].idef.il[ftype].nr/2; i++)
+                         {
+                             /* Correct the index into iparams_fbposres */
+                             dest->il[ftype].iatoms[nposres*2] = nposres;
+                             /* Copy the position restraint force parameters */
+                             dest->iparams_fbposres[nposres] = src[s].idef.iparams_fbposres[i];
+                             nposres++;
+                         }
+                     }
+                 }
              }
          }
      }
@@@ -1645,11 -1661,11 +1657,11 @@@ static int make_exclusions_zone_cg(gmx_
                                     int iz,
                                     int cg_start, int cg_end)
  {
 -    int             n_excl_at_max, n, count, jla0, jla1, jla;
 -    int             cg, la0, la1, la, a_gl, mb, mt, mol, a_mol, j, aj_mol;
 -    const t_blocka *excls;
 -    gmx_ga2la_t     ga2la;
 -    int             cell;
 +    int               n_excl_at_max, n, count, jla0, jla1, jla;
 +    int               cg, la0, la1, la, a_gl, mb, mt, mol, a_mol, j, aj_mol;
 +    const t_blocka   *excls;
 +    gmx_ga2la_t      *ga2la;
 +    int               cell;
  
      ga2la = dd->ga2la;
  
@@@ -1782,9 -1798,9 +1794,9 @@@ static void make_exclusions_zone(gmx_do
                                   int iz,
                                   int at_start, int at_end)
  {
 -    gmx_ga2la_t ga2la;
 -    int         jla0, jla1;
 -    int         n_excl_at_max, n, at;
 +    gmx_ga2la_t *ga2la;
 +    int          jla0, jla1;
 +    int          n_excl_at_max, n, at;
  
      ga2la = dd->ga2la;
  
@@@ -1980,92 -1996,88 +1992,92 @@@ static int make_local_bondeds_excls(gmx
  #pragma omp parallel for num_threads(rt->nthread) schedule(static)
          for (thread = 0; thread < rt->nthread; thread++)
          {
 -            int       cg0t, cg1t;
 -            t_idef   *idef_t;
 -            int     **vsite_pbc;
 -            int      *vsite_pbc_nalloc;
 -            t_blocka *excl_t;
 -
 -            cg0t = cg0 + ((cg1 - cg0)* thread   )/rt->nthread;
 -            cg1t = cg0 + ((cg1 - cg0)*(thread+1))/rt->nthread;
 -
 -            if (thread == 0)
 +            try
              {
 -                idef_t = idef;
 -            }
 -            else
 -            {
 -                idef_t = &rt->th_work[thread].idef;
 -                clear_idef(idef_t);
 -            }
 +                int       cg0t, cg1t;
 +                t_idef   *idef_t;
 +                int     **vsite_pbc;
 +                int      *vsite_pbc_nalloc;
 +                t_blocka *excl_t;
 +
 +                cg0t = cg0 + ((cg1 - cg0)* thread   )/rt->nthread;
 +                cg1t = cg0 + ((cg1 - cg0)*(thread+1))/rt->nthread;
  
 -            if (vsite && vsite->bHaveChargeGroups && vsite->n_intercg_vsite > 0)
 -            {
                  if (thread == 0)
                  {
 -                    vsite_pbc        = vsite->vsite_pbc_loc;
 -                    vsite_pbc_nalloc = vsite->vsite_pbc_loc_nalloc;
 +                    idef_t = idef;
                  }
                  else
                  {
 -                    vsite_pbc        = rt->th_work[thread].vsite_pbc;
 -                    vsite_pbc_nalloc = rt->th_work[thread].vsite_pbc_nalloc;
 +                    idef_t = &rt->th_work[thread].idef;
 +                    clear_idef(idef_t);
                  }
 -            }
 -            else
 -            {
 -                vsite_pbc        = NULL;
 -                vsite_pbc_nalloc = NULL;
 -            }
  
 -            rt->th_work[thread].nbonded =
 -                make_bondeds_zone(dd, zones,
 -                                  mtop->molblock,
 -                                  bRCheckMB, rcheck, bRCheck2B, rc2,
 -                                  la2lc, pbc_null, cg_cm, idef->iparams,
 -                                  idef_t,
 -                                  vsite_pbc, vsite_pbc_nalloc,
 -                                  izone,
 -                                  dd->cgindex[cg0t], dd->cgindex[cg1t]);
 -
 -            if (izone < nzone_excl)
 -            {
 -                if (thread == 0)
 +                if (vsite && vsite->bHaveChargeGroups && vsite->n_intercg_vsite > 0)
                  {
 -                    excl_t = lexcls;
 +                    if (thread == 0)
 +                    {
 +                        vsite_pbc        = vsite->vsite_pbc_loc;
 +                        vsite_pbc_nalloc = vsite->vsite_pbc_loc_nalloc;
 +                    }
 +                    else
 +                    {
 +                        vsite_pbc        = rt->th_work[thread].vsite_pbc;
 +                        vsite_pbc_nalloc = rt->th_work[thread].vsite_pbc_nalloc;
 +                    }
                  }
                  else
                  {
 -                    excl_t      = &rt->th_work[thread].excl;
 -                    excl_t->nr  = 0;
 -                    excl_t->nra = 0;
 +                    vsite_pbc        = NULL;
 +                    vsite_pbc_nalloc = NULL;
                  }
  
 -                if (dd->cgindex[dd->ncg_tot] == dd->ncg_tot &&
 -                    !rt->bExclRequired)
 -                {
 -                    /* No charge groups and no distance check required */
 -                    make_exclusions_zone(dd, zones,
 -                                         mtop->moltype, cginfo,
 -                                         excl_t,
 -                                         izone,
 -                                         cg0t, cg1t);
 -                }
 -                else
 +                rt->th_work[thread].nbonded =
 +                    make_bondeds_zone(dd, zones,
 +                                      mtop->molblock,
 +                                      bRCheckMB, rcheck, bRCheck2B, rc2,
 +                                      la2lc, pbc_null, cg_cm, idef->iparams,
 +                                      idef_t,
 +                                      vsite_pbc, vsite_pbc_nalloc,
 +                                      izone,
 +                                      dd->cgindex[cg0t], dd->cgindex[cg1t]);
 +
 +                if (izone < nzone_excl)
                  {
 -                    rt->th_work[thread].excl_count =
 -                        make_exclusions_zone_cg(dd, zones,
 -                                                mtop->moltype, bRCheck2B, rc2,
 -                                                la2lc, pbc_null, cg_cm, cginfo,
 -                                                excl_t,
 -                                                izone,
 -                                                cg0t, cg1t);
 +                    if (thread == 0)
 +                    {
 +                        excl_t = lexcls;
 +                    }
 +                    else
 +                    {
 +                        excl_t      = &rt->th_work[thread].excl;
 +                        excl_t->nr  = 0;
 +                        excl_t->nra = 0;
 +                    }
 +
 +                    if (dd->cgindex[dd->ncg_tot] == dd->ncg_tot &&
 +                        !rt->bExclRequired)
 +                    {
 +                        /* No charge groups and no distance check required */
 +                        make_exclusions_zone(dd, zones,
 +                                             mtop->moltype, cginfo,
 +                                             excl_t,
 +                                             izone,
 +                                             cg0t, cg1t);
 +                    }
 +                    else
 +                    {
 +                        rt->th_work[thread].excl_count =
 +                            make_exclusions_zone_cg(dd, zones,
 +                                                    mtop->moltype, bRCheck2B, rc2,
 +                                                    la2lc, pbc_null, cg_cm, cginfo,
 +                                                    excl_t,
 +                                                    izone,
 +                                                    cg0t, cg1t);
 +                    }
                  }
              }
 +            GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR;
          }
  
          if (rt->nthread > 1)
@@@ -2122,7 -2134,7 +2134,7 @@@ void dd_make_local_top(gmx_domdec_t *dd
                         t_forcerec *fr,
                         rvec *cgcm_or_x,
                         gmx_vsite_t *vsite,
 -                       gmx_mtop_t *mtop, gmx_localtop_t *ltop)
 +                       const gmx_mtop_t *mtop, gmx_localtop_t *ltop)
  {
      gmx_bool bRCheckMB, bRCheck2B;
      real     rc = -1;
              make_la2lc(dd);
              if (fr->bMolPBC)
              {
 -                set_pbc_dd(&pbc, fr->ePBC, dd, TRUE, box);
 -                pbc_null = &pbc;
 +                pbc_null = set_pbc_dd(&pbc, fr->ePBC, dd->nc, TRUE, box);
              }
              else
              {
      }
  
      ltop->atomtypes  = mtop->atomtypes;
 -
 -    /* For an error message only */
 -    dd->reverse_top->err_top_global = mtop;
 -    dd->reverse_top->err_top_local  = ltop;
  }
  
 -void dd_sort_local_top(gmx_domdec_t *dd, t_mdatoms *mdatoms,
 +void dd_sort_local_top(gmx_domdec_t *dd, const t_mdatoms *mdatoms,
                         gmx_localtop_t *ltop)
  {
      if (dd->reverse_top->ilsort == ilsortNO_FE)
      }
  }
  
 -gmx_localtop_t *dd_init_local_top(gmx_mtop_t *top_global)
 +gmx_localtop_t *dd_init_local_top(const gmx_mtop_t *top_global)
  {
      gmx_localtop_t *top;
      int             i;
@@@ -2317,7 -2334,7 +2329,7 @@@ static int *make_at2cg(t_block *cgs
      return at2cg;
  }
  
 -t_blocka *make_charge_group_links(gmx_mtop_t *mtop, gmx_domdec_t *dd,
 +t_blocka *make_charge_group_links(const gmx_mtop_t *mtop, gmx_domdec_t *dd,
                                    cginfo_mb_t *cginfo_mb)
  {
      gmx_bool            bExclRequired;
@@@ -2582,7 -2599,7 +2594,7 @@@ static void bonded_cg_distance_mol(gmx_
  /*! \brief Set the distance, function type and atom indices for the longest atom distance involved in intermolecular interactions for two-body and multi-body bonded interactions */
  static void bonded_distance_intermol(const t_ilist *ilists_intermol,
                                       gmx_bool bBCheck,
 -                                     rvec *x, int ePBC, matrix box,
 +                                     const rvec *x, int ePBC, matrix box,
                                       bonded_distance_t *bd_2b,
                                       bonded_distance_t *bd_mb)
  {
  }
  
  //! Compute charge group centers of mass for molecule \p molt
 -static void get_cgcm_mol(gmx_moltype_t *molt, gmx_ffparams_t *ffparams,
 +static void get_cgcm_mol(const gmx_moltype_t *molt,
 +                         const gmx_ffparams_t *ffparams,
                           int ePBC, t_graph *graph, matrix box,
 -                         gmx_vsite_t *vsite,
 -                         rvec *x, rvec *xs, rvec *cg_cm)
 +                         const gmx_vsite_t *vsite,
 +                         const rvec *x, rvec *xs, rvec *cg_cm)
  {
      int n, i;
  
@@@ -2691,9 -2707,8 +2703,9 @@@ static int have_vsite_molt(gmx_moltype_
  }
  
  void dd_bonded_cg_distance(FILE *fplog,
 -                           gmx_mtop_t *mtop,
 -                           t_inputrec *ir, rvec *x, matrix box,
 +                           const gmx_mtop_t *mtop,
 +                           const t_inputrec *ir,
 +                           const rvec *x, matrix box,
                             gmx_bool bBCheck,
                             real *r_2b, real *r_mb)
  {
      bonded_distance_t  bd_2b = { 0, -1, -1, -1 };
      bonded_distance_t  bd_mb = { 0, -1, -1, -1 };
  
 -    bExclRequired = IR_EXCL_FORCES(*ir);
 +    bExclRequired = inputrecExclForces(ir);
  
      vsite = init_vsite(mtop, NULL, TRUE);
  
index 265e7d4f9330271684ebb16e12f6fd6a84e58f3c,0000000000000000000000000000000000000000..74145c5cd244299318a357287c7716edebd75fed
mode 100644,000000..100644
--- /dev/null
@@@ -1,386 -1,0 +1,387 @@@
-  * Copyright (c) 2013,2014,2015, by the GROMACS development team, led by
 +/*
 + * This file is part of the GROMACS molecular simulation package.
 + *
 + * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
 + * Copyright (c) 2001-2004, The GROMACS development team.
-                 if (is_bond(nsb, sb, pdba, ai, aj, d[i][j], &index_sb, &bSwap))
++ * Copyright (c) 2013,2014,2015,2016, by the GROMACS development team, led by
 + * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
 + * and including many others, as listed in the AUTHORS file in the
 + * top-level source directory and at http://www.gromacs.org.
 + *
 + * GROMACS is free software; you can redistribute it and/or
 + * modify it under the terms of the GNU Lesser General Public License
 + * as published by the Free Software Foundation; either version 2.1
 + * of the License, or (at your option) any later version.
 + *
 + * GROMACS is distributed in the hope that it will be useful,
 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 + * Lesser General Public License for more details.
 + *
 + * You should have received a copy of the GNU Lesser General Public
 + * License along with GROMACS; if not, see
 + * http://www.gnu.org/licenses, or write to the Free Software Foundation,
 + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
 + *
 + * If you want to redistribute modifications to GROMACS, please
 + * consider that scientific software is very special. Version
 + * control is crucial - bugs must be traceable. We will be happy to
 + * consider code for inclusion in the official distribution, but
 + * derived work must not be called official GROMACS. Details are found
 + * in the README & COPYING files - if they are missing, get the
 + * official version at http://www.gromacs.org.
 + *
 + * To help us fund GROMACS development, we humbly ask that you cite
 + * the research papers on the package. Check out http://www.gromacs.org.
 + */
 +#include "gmxpre.h"
 +
 +#include "specbond.h"
 +
 +#include <ctype.h>
 +#include <string.h>
 +
 +#include <cmath>
 +
 +#include <algorithm>
 +
 +#include "gromacs/fileio/pdbio.h"
 +#include "gromacs/gmxpreprocess/pdb2top.h"
 +#include "gromacs/math/vec.h"
 +#include "gromacs/utility/cstringutil.h"
 +#include "gromacs/utility/fatalerror.h"
 +#include "gromacs/utility/smalloc.h"
 +#include "gromacs/utility/strdb.h"
 +
 +gmx_bool yesno(void)
 +{
 +    char c;
 +
 +    do
 +    {
 +        c = toupper(fgetc(stdin));
 +    }
 +    while ((c != 'Y') && (c != 'N'));
 +
 +    return (c == 'Y');
 +}
 +
 +t_specbond *get_specbonds(int *nspecbond)
 +{
 +    const char  *sbfile = "specbond.dat";
 +
 +    t_specbond  *sb = NULL;
 +    char         r1buf[32], r2buf[32], a1buf[32], a2buf[32], nr1buf[32], nr2buf[32];
 +    double       length;
 +    int          nb1, nb2;
 +    char       **lines;
 +    int          nlines, i, n;
 +
 +    nlines = get_lines(sbfile, &lines);
 +    if (nlines > 0)
 +    {
 +        snew(sb, nlines);
 +    }
 +
 +    n = 0;
 +    for (i = 0; (i < nlines); i++)
 +    {
 +        if (sscanf(lines[i], "%s%s%d%s%s%d%lf%s%s",
 +                   r1buf, a1buf, &nb1, r2buf, a2buf, &nb2, &length, nr1buf, nr2buf) != 9)
 +        {
 +            fprintf(stderr, "Invalid line '%s' in %s\n", lines[i], sbfile);
 +        }
 +        else
 +        {
 +            sb[n].res1    = gmx_strdup(r1buf);
 +            sb[n].res2    = gmx_strdup(r2buf);
 +            sb[n].newres1 = gmx_strdup(nr1buf);
 +            sb[n].newres2 = gmx_strdup(nr2buf);
 +            sb[n].atom1   = gmx_strdup(a1buf);
 +            sb[n].atom2   = gmx_strdup(a2buf);
 +            sb[n].nbond1  = nb1;
 +            sb[n].nbond2  = nb2;
 +            sb[n].length  = length;
 +            n++;
 +        }
 +        sfree(lines[i]);
 +    }
 +    if (nlines > 0)
 +    {
 +        sfree(lines);
 +    }
 +    fprintf(stderr, "%d out of %d lines of %s converted successfully\n",
 +            n, nlines, sbfile);
 +
 +    *nspecbond = n;
 +
 +    return sb;
 +}
 +
 +void done_specbonds(int nsb, t_specbond sb[])
 +{
 +    int i;
 +
 +    for (i = 0; (i < nsb); i++)
 +    {
 +        sfree(sb[i].res1);
 +        sfree(sb[i].res2);
 +        sfree(sb[i].atom1);
 +        sfree(sb[i].atom2);
 +        sfree(sb[i].newres1);
 +        sfree(sb[i].newres2);
 +    }
 +}
 +
 +static gmx_bool is_special(int nsb, t_specbond sb[], char *res, char *atom)
 +{
 +    int i;
 +
 +    for (i = 0; (i < nsb); i++)
 +    {
 +        if (((strncmp(sb[i].res1, res, 3) == 0) &&
 +             (gmx_strcasecmp(sb[i].atom1, atom) == 0)) ||
 +            ((strncmp(sb[i].res2, res, 3) == 0) &&
 +             (gmx_strcasecmp(sb[i].atom2, atom) == 0)))
 +        {
 +            return TRUE;
 +        }
 +    }
 +    return FALSE;
 +}
 +
 +static gmx_bool is_bond(int nsb, t_specbond sb[], t_atoms *pdba, int a1, int a2,
 +                        real d, int *index_sb, gmx_bool *bSwap)
 +{
 +    int   i;
 +    char *at1, *at2, *res1, *res2;
 +
 +    at1  = *pdba->atomname[a1];
 +    at2  = *pdba->atomname[a2];
 +    res1 = *pdba->resinfo[pdba->atom[a1].resind].name;
 +    res2 = *pdba->resinfo[pdba->atom[a2].resind].name;
 +
 +    if (debug)
 +    {
 +        fprintf(stderr, "Checking %s-%d %s-%d and %s-%d %s-%d: %g ",
 +                res1, pdba->resinfo[pdba->atom[a1].resind].nr, at1, a1+1,
 +                res2, pdba->resinfo[pdba->atom[a2].resind].nr, at2, a2+1, d);
 +    }
 +
 +    for (i = 0; (i < nsb); i++)
 +    {
 +        *index_sb = i;
 +        if (((strncmp(sb[i].res1, res1, 3) == 0)  &&
 +             (gmx_strcasecmp(sb[i].atom1, at1) == 0) &&
 +             (strncmp(sb[i].res2, res2, 3) == 0)  &&
 +             (gmx_strcasecmp(sb[i].atom2, at2) == 0)))
 +        {
 +            *bSwap = FALSE;
 +            if ((0.9*sb[i].length < d) && (1.1*sb[i].length > d))
 +            {
 +                if (debug)
 +                {
 +                    fprintf(stderr, "%g\n", sb[i].length);
 +                }
 +                return TRUE;
 +            }
 +        }
 +        if (((strncmp(sb[i].res1, res2, 3) == 0)  &&
 +             (gmx_strcasecmp(sb[i].atom1, at2) == 0) &&
 +             (strncmp(sb[i].res2, res1, 3) == 0)  &&
 +             (gmx_strcasecmp(sb[i].atom2, at1) == 0)))
 +        {
 +            *bSwap = TRUE;
 +            if ((0.9*sb[i].length < d) && (1.1*sb[i].length > d))
 +            {
 +                if (debug)
 +                {
 +                    fprintf(stderr, "%g\n", sb[i].length);
 +                }
 +                return TRUE;
 +            }
 +        }
 +    }
 +    if (debug)
 +    {
 +        fprintf(stderr, "\n");
 +    }
 +    return FALSE;
 +}
 +
 +static void rename_1res(t_atoms *pdba, int resind, char *newres, gmx_bool bVerbose)
 +{
 +    if (bVerbose)
 +    {
 +        printf("Using rtp entry %s for %s %d\n",
 +               newres,
 +               *pdba->resinfo[resind].name,
 +               pdba->resinfo[resind].nr);
 +    }
 +    /* this used to free *resname, which messes up the symtab! */
 +    snew(pdba->resinfo[resind].rtp, 1);
 +    *pdba->resinfo[resind].rtp = gmx_strdup(newres);
 +}
 +
 +int mk_specbonds(t_atoms *pdba, rvec x[], gmx_bool bInteractive,
 +                 t_ssbond **specbonds, gmx_bool bVerbose)
 +{
 +    t_specbond *sb    = NULL;
 +    t_ssbond   *bonds = NULL;
 +    int         nsb;
 +    int         nspec, nbonds;
 +    int        *specp, *sgp;
 +    gmx_bool    bDoit, bSwap;
 +    int         i, j, b, e, e2;
 +    int         ai, aj, index_sb;
 +    real      **d;
 +    char        buf[10];
 +
 +    nbonds = 0;
 +    sb     = get_specbonds(&nsb);
 +
 +    if (nsb > 0)
 +    {
 +        snew(specp, pdba->nr);
 +        snew(sgp, pdba->nr);
 +
 +        nspec = 0;
 +        for (i = 0; (i < pdba->nr); i++)
 +        {
 +            /* Check if this atom is special and if it is not a double atom
 +             * in the input that still needs to be removed.
 +             */
 +            if (is_special(nsb, sb, *pdba->resinfo[pdba->atom[i].resind].name,
 +                           *pdba->atomname[i]) &&
 +                !(nspec > 0 &&
 +                  pdba->atom[sgp[nspec-1]].resind == pdba->atom[i].resind &&
 +                  gmx_strcasecmp(*pdba->atomname[sgp[nspec-1]],
 +                                 *pdba->atomname[i]) == 0))
 +            {
 +                specp[nspec] = pdba->atom[i].resind;
 +                sgp[nspec]   = i;
 +                nspec++;
 +            }
 +        }
 +        /* distance matrix d[nspec][nspec] */
 +        snew(d, nspec);
 +        for (i = 0; (i < nspec); i++)
 +        {
 +            snew(d[i], nspec);
 +        }
 +
 +        for (i = 0; (i < nspec); i++)
 +        {
 +            ai = sgp[i];
 +            for (j = 0; (j < nspec); j++)
 +            {
 +                aj      = sgp[j];
 +                d[i][j] = std::sqrt(distance2(x[ai], x[aj]));
 +            }
 +        }
 +        if (nspec > 1)
 +        {
 +#define MAXCOL 7
 +            fprintf(stderr, "Special Atom Distance matrix:\n");
 +            for (b = 0; (b < nspec); b += MAXCOL)
 +            {
 +                /* print resname/number column headings */
 +                fprintf(stderr, "%8s%8s", "", "");
 +                e = std::min(b+MAXCOL, nspec-1);
 +                for (i = b; (i < e); i++)
 +                {
 +                    sprintf(buf, "%s%d", *pdba->resinfo[pdba->atom[sgp[i]].resind].name,
 +                            pdba->resinfo[specp[i]].nr);
 +                    fprintf(stderr, "%8s", buf);
 +                }
 +                fprintf(stderr, "\n");
 +                /* print atomname/number column headings */
 +                fprintf(stderr, "%8s%8s", "", "");
 +                e = std::min(b+MAXCOL, nspec-1);
 +                for (i = b; (i < e); i++)
 +                {
 +                    sprintf(buf, "%s%d", *pdba->atomname[sgp[i]], sgp[i]+1);
 +                    fprintf(stderr, "%8s", buf);
 +                }
 +                fprintf(stderr, "\n");
 +                /* print matrix */
 +                e = std::min(b+MAXCOL, nspec);
 +                for (i = b+1; (i < nspec); i++)
 +                {
 +                    sprintf(buf, "%s%d", *pdba->resinfo[pdba->atom[sgp[i]].resind].name,
 +                            pdba->resinfo[specp[i]].nr);
 +                    fprintf(stderr, "%8s", buf);
 +                    sprintf(buf, "%s%d", *pdba->atomname[sgp[i]], sgp[i]+1);
 +                    fprintf(stderr, "%8s", buf);
 +                    e2 = std::min(i, e);
 +                    for (j = b; (j < e2); j++)
 +                    {
 +                        fprintf(stderr, " %7.3f", d[i][j]);
 +                    }
 +                    fprintf(stderr, "\n");
 +                }
 +            }
 +        }
 +
 +        snew(bonds, nspec);
 +
 +        for (i = 0; (i < nspec); i++)
 +        {
 +            ai = sgp[i];
 +            for (j = i+1; (j < nspec); j++)
 +            {
 +                aj = sgp[j];
++                /* Ensure creation of at most nspec special bonds to avoid overflowing bonds[] */
++                if (nbonds < nspec && is_bond(nsb, sb, pdba, ai, aj, d[i][j], &index_sb, &bSwap))
 +                {
 +                    fprintf(stderr, "%s %s-%d %s-%d and %s-%d %s-%d%s",
 +                            bInteractive ? "Link" : "Linking",
 +                            *pdba->resinfo[pdba->atom[ai].resind].name,
 +                            pdba->resinfo[specp[i]].nr,
 +                            *pdba->atomname[ai], ai+1,
 +                            *pdba->resinfo[pdba->atom[aj].resind].name,
 +                            pdba->resinfo[specp[j]].nr,
 +                            *pdba->atomname[aj], aj+1,
 +                            bInteractive ? " (y/n) ?" : "...\n");
 +                    bDoit = bInteractive ? yesno() : TRUE;
 +
 +                    if (bDoit)
 +                    {
 +                        /* Store the residue numbers in the bonds array */
 +                        bonds[nbonds].res1 = specp[i];
 +                        bonds[nbonds].res2 = specp[j];
 +                        bonds[nbonds].a1   = gmx_strdup(*pdba->atomname[ai]);
 +                        bonds[nbonds].a2   = gmx_strdup(*pdba->atomname[aj]);
 +                        /* rename residues */
 +                        if (bSwap)
 +                        {
 +                            rename_1res(pdba, specp[i], sb[index_sb].newres2, bVerbose);
 +                            rename_1res(pdba, specp[j], sb[index_sb].newres1, bVerbose);
 +                        }
 +                        else
 +                        {
 +                            rename_1res(pdba, specp[i], sb[index_sb].newres1, bVerbose);
 +                            rename_1res(pdba, specp[j], sb[index_sb].newres2, bVerbose);
 +                        }
 +                        nbonds++;
 +                    }
 +                }
 +            }
 +        }
 +
 +        for (i = 0; (i < nspec); i++)
 +        {
 +            sfree(d[i]);
 +        }
 +        sfree(d);
 +        sfree(sgp);
 +        sfree(specp);
 +
 +        done_specbonds(nsb, sb);
 +        sfree(sb);
 +    }
 +
 +    *specbonds = bonds;
 +
 +    return nbonds;
 +}