c
c
c     ###################################################
c     ##  COPYRIGHT (C)  1997  by  Jay William Ponder  ##
c     ##              All Rights Reserved              ##
c     ###################################################
c
c     ###########################################################
c     ##                                                       ##
c     ##  subroutine groups  --  group membership of atom set  ##
c     ##                                                       ##
c     ###########################################################
c
c
c     "groups" tests a set of atoms to see if all are members
c     of a single atom group or a pair of atom groups; if so,
c     then the correct intra- or intergroup weight is assigned
c
c
      subroutine groups (useset,weight,nset,ia,ib,ic,id,ie)
      implicit none
      include 'sizes.i'
      include 'group.i'
      integer ia,ib,ic,id,ie,nset
      integer iga,igb,igc,igd,ige
      integer gmax,gmin
      real*8 weight
      logical useset
c
c
c     check group membership for a set containing one atom
c
      if (nset .eq. 1) then
         iga = grplist(ia)
         weight = wgrp(iga,iga)
c
c     check group membership for a set containing two atoms
c
      else if (nset .eq. 2) then
         iga = grplist(ia)
         igb = grplist(ib)
         weight = wgrp(iga,igb)
c
c     check group membership for a set containing three atoms
c
      else if (nset .eq. 3) then
         iga = grplist(ia)
         igb = grplist(ib)
         igc = grplist(ic)
         if (iga.eq.igb .or. igb.eq.igc) then
            weight = wgrp(iga,igc)
         else if (iga .eq. igc) then
            weight = wgrp(iga,igb)
         else
            weight = 0.0d0
         end if
c
c     check group membership for a set containing four atoms
c
      else if (nset .eq. 4) then
         iga = grplist(ia)
         igb = grplist(ib)
         igc = grplist(ic)
         igd = grplist(id)
         gmin = min(iga,igb,igc,igd)
         gmax = max(iga,igb,igc,igd)
         if ((iga.ne.gmin .and. iga.ne.gmax) .or.
     &       (igb.ne.gmin .and. igb.ne.gmax) .or.
     &       (igc.ne.gmin .and. igc.ne.gmax) .or.
     &       (igd.ne.gmin .and. igd.ne.gmax)) then
            weight = 0.0d0
         else
            weight = wgrp(gmin,gmax)
         end if
c
c     check group membership for a set containing five atoms
c
      else if (nset .eq. 5) then
         iga = grplist(ia)
         igb = grplist(ib)
         igc = grplist(ic)
         igd = grplist(id)
         ige = grplist(ie)
         gmin = min(iga,igb,igc,igd,ige)
         gmax = max(iga,igb,igc,igd,ige)
         if ((iga.ne.gmin .and. iga.ne.gmax) .or.
     &       (igb.ne.gmin .and. igb.ne.gmax) .or.
     &       (igc.ne.gmin .and. igc.ne.gmax) .or.
     &       (igd.ne.gmin .and. igd.ne.gmax) .or.
     &       (ige.ne.gmin .and. ige.ne.gmax)) then
            weight = 0.0d0
         else
            weight = wgrp(gmin,gmax)
         end if
      end if
c
c     interaction will be used if its group has nonzero weight
c
      if (weight .eq. 0.0d0) then
         useset = .false.
      else
         useset = .true.
      end if
      return
      end
