   subroutine trmpi_group&
     (name,lname,code,lcode,action,laction,have_group,active,comm,rank,size)

   use trmpi
   use mpi_proc_data
   use logmod

#ifdef __MPI
   use mpi
#endif

   implicit NONE

 ! private:

   integer     :: myid
   integer     :: grp_comm, grp_rank, grp_size
   integer     :: ierr
   logical     :: iamin=.false.     

   character(len=*), intent(in)    :: name       ! group name
                                                 ! NUBEAM or TORIC or CQL3D
   integer, intent(in)             :: lname
   character(len=*), intent(in)    :: code       ! calling program
   integer, intent(in)             :: lcode
   character(len=*), intent(in)    :: action     ! request
   integer, intent(in)             :: laction
   logical, intent(out)            :: have_group ! group exists
   logical, intent(out),optional   :: active     ! active group member
   integer, intent(out),optional   :: comm       ! group communicator 
   integer, intent(out),optional   :: rank       ! group rank
   integer, intent(out),optional   :: size       ! group size 

#ifdef __MPI

   call MPI_COMM_RANK(trmpi_comm, myid, ierr )
   call infoLog('trmpi_group: check for '//name(1:lname)//' myid=',myid)

   call mpi_proc_info(name(1:lname),have_group,grp_comm,irank=grp_rank,num_procs=grp_size)
 
 ! check if we have a communicator group (test programs don't)
 !--------------
 ! GROUP EXISTS
 !--------------
   if (have_group) then
      call infoLog('trmpi_group: '//code(1:lcode)//' '//name(1:lname)//&
                   ' '//action(1:laction)//' comm =',grp_comm)
      call infoLog('trmpi_group: '//code(1:lcode)//' my '//name(1:lname)//' rank =',grp_rank)
   ! check if in group
   ! if(grp_comm .ne. -1) then
      if(grp_rank .ne. -1) then
   !--------------------
   ! REQUEST_START_GROUP
   !--------------------
         if (action(1:laction) .eq. 'REQUEST_START' ) then
            if (myid .eq. 0) then
               call trmpi_requestor( 'START_GROUP', name(1:lname) )
               call infoLog('returned from trmpi_requestor',myid)
               trmpi_comm=grp_comm
               iamin=.true.
! 08/28/12 CLF: done in trmpi_init
!               if ( name(1:lname) .eq. 'NUBEAM') then
!                  call nbi_set_comm(grp_comm)
!                  call nbi_set_myid(grp_rank)
!                  call nbi_set_numprocs(grp_size)
!               endif
            else
                call errorLog('trmpi_group: '//action(1:laction)//&
                     ' not allowed for rank =',myid)
            endif            
   !--------------------
   ! START_GROUP
   !--------------------
         else if (action(1:laction) .eq. 'START_GROUP' ) then
   !  for now all groups are first part ONLY
             trmpi_comm=grp_comm
             iamin=.true.
   !--------------------
   ! REQUEST_STOP_GROUP
   !--------------------
         else if (action(1:laction) .eq. 'REQUEST_STOP' ) then
            call trmpi_requestor( 'STOP_GROUP', name )
            trmpi_comm=MPI_COMM_WORLD
   !--------------------
   ! STOP_GROUP
   !--------------------
         else if (action(1:laction) .eq. 'STOP_GROUP' ) then
            trmpi_comm=MPI_COMM_WORLD
   !---------------
   ! INFO
   !---------------
         else if (action(1:laction) .eq. 'INFO' ) then
!  for now all groups start with parent rank 0
            iamin=.true.
         endif ! End actions
      else ! 
         iamin=.false.
         call infoLog('trmpi_group: not a member of '//name(1:lname),' myid=',myid)
      endif
   else ! no group
       call infoLog('trmpi_group: '//name(1:lname)//' group does not exist')
       iamin=.false.
   endif       

 ! return info if requested

   if (present(active)) then
      active=iamin
   endif
   if (iamin) then
      if (present(comm)) comm=grp_comm
      if (present(rank)) rank=grp_rank
      if (present(size)) size=grp_size
   endif
#endif
 end subroutine trmpi_group
