#ifndef OPENMM_BROOK_NONBONDED_H_
#define OPENMM_BROOK_NONBONDED_H_

/* -------------------------------------------------------------------------- *
 *                                   OpenMM                                   *
 * -------------------------------------------------------------------------- *
 * This is part of the OpenMM molecular simulation toolkit originating from   *
 * Simbios, the NIH National Center for Physics-Based Simulation of           *
 * Biological Structures at Stanford, funded under the NIH Roadmap for        *
 * Medical Research, grant U54 GM072970. See https://simtk.org.               *
 *                                                                            *
 * Portions copyright (c) 2009 Stanford University and the Authors.           *
 * Authors: Mark Friedrichs, Mike Houston                                     *
 * Contributors:                                                              *
 *                                                                            *
 * This program 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 3 of the License, or       *
 * (at your option) any later version.                                        *
 *                                                                            *
 * This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.      *
 * -------------------------------------------------------------------------- */

#include <vector>
#include <set>

#include "BrookStreamImpl.h"
#include "BrookPlatform.h"
#include "BrookCommon.h"

namespace OpenMM {

/**
 * This kernel is invoked by NonbondedForce to calculate the forces acting on the system.
 */
class BrookNonBonded : public BrookCommon {

   public:
  
     BrookNonBonded( );
  
      ~BrookNonBonded();
  
      /**
       * Return number of force streams
       * 
       * @return number of force streams
       *
       */

      int getNumberOfForceStreams( void ) const; 

      /** 
       * Get duplication factor
       * 
       * @return   duplication factor
       *
       */
      
      int getDuplicationFactor( void ) const;
      
      /** 
       * Get particle ceiling parameter
       * 
       * @return particle ceiling parameter
       *
       */
         
      int getParticleSizeCeiling( void ) const;
 
      /** 
       * Get outer loop unroll
       * 
       * @return   outer loop unroll (fixed value)
       *
       */
      
      int getOuterLoopUnroll( void ) const;
      
      /** 
       * Set outer loop unroll
       * 
       * @param  outer loop unroll (fixed value)
       *
       * @return updated outer loop unroll (fixed value)
       *
       */
      
      int setOuterLoopUnroll( int outerUnroll );
      
      /**
       * Return unrolling for inner loops
       * 
       * @return outer loop unrolling
       */

      int getInnerLoopUnroll( void ) const; 

      /**
       * Get j-stream width
       *
       * @param platform platform reference
       *
       * @return j-stream width
       */

      int getJStreamWidth( const Platform& platform ); 

      /**
       * Get j-stream width
       *
       * @return j-stream width
       */

      int getJStreamWidth( void ) const; 

      /**
       * Get j-stream height
       *
       * @param platform platform reference
       *
       * @return j-stream height
       */

      int getJStreamHeight( const Platform& platform ); 

      /**
       * Get j-stream height
       *
       * @return j-stream height
       */

      int getJStreamHeight( void ) const;

      /**
       * Get j-stream size
       * 
       * @param platform platform reference
       *
       * @return j-stream size
       */

      int getJStreamSize( const Platform& platform ); 

      /**
       * Get j-stream size
       * 
       * @return j-stream size
       */

      int getJStreamSize( void ) const; 

      /**
       * Get partial force stream width
       *
       * @param platform platform reference
       *
       * @return partial force stream width
       */

      int getPartialForceStreamWidth( const Platform& platform ); 

      /**
       * Get partial force stream width
       *
       * @return partial force stream width
       */

      int getPartialForceStreamWidth( void ) const; 

      /**
       * Get partial force stream height
       *
       * @param platform platform reference
       *
       * @return partial force stream height
       */

      int getPartialForceStreamHeight( const Platform& platform ); 

      /**
       * Get partial force stream height
       *
       * @return partial force stream height
       */

      int getPartialForceStreamHeight( void ) const;

      /**
       * Get partial force stream size
       * 
       * @param platform platform reference
       *
       * @return partial force stream size
       */

      int getPartialForceStreamSize( const Platform& platform ); 

      /**
       * Get partial force stream size
       * 
       * @return partial force stream size
       */

      int getPartialForceStreamSize( void ) const; 

      /**
       * Get partial force stream size
       * 
       * @return partial force stream size
       */

      /**
       * Get exclusion stream width
       * 
       * @return exclusion stream width
       */

      int getExclusionStreamWidth( void ) const; 

      /**
       * Get exclusion stream size
       * 
       * @return exclusion stream size
       */

      int getExclusionStreamSize( void ) const; 

      /** 
       * Get exclusion stream 
       *
       * @return  exclusion stream
       *
       */
      
      BrookFloatStreamInternal* getExclusionStream( void ) const;
      
      /** 
       * Get vdw stream 
       *
       * @return  vdw stream
       *
       */
      
      BrookFloatStreamInternal* getOuterVdwStream( void ) const;
      
      /** 
       * Get charge stream 
       *
       * @return  charge stream
       *
       */
      
      BrookFloatStreamInternal* getChargeStream( void ) const;
      
      /** 
       * Get sigma-eps stream 
       *
       * @return  sigma-eps stream
       *
       */
      
      BrookFloatStreamInternal* getInnerSigmaStream( void ) const;
      
      /** 
       * Get epsilon stream 
       *
       * @return  epsilon stream
       *
       */
      
      BrookFloatStreamInternal* getInnerEpsilonStream( void ) const;
      
      /** 
       * Get force streams 
       *
       * @return  force streams
       *
       */
      
      BrookFloatStreamInternal** getForceStreams( void );
      
      /** 
       * Compute forces
       * 
       */
      
      void computeForces( BrookStreamImpl& positionStream, BrookStreamImpl& forceStream );
      
      /** 
       * Return true if force[index] stream is set 
       *
       * @return  true  if index is valid && force[index] stream is set; else false
       *
       */
      
      int isForceStreamSet( int index ) const;
      
      /* 
       * Setup of nonbonded ixns
       *
       * @param numberOfParticles         number of particles
       * @param nonbondedParameters   vector of nonbonded parameters [particleI][0=c6]
       *                                                             [particleI][1=c12]
       *                                                             [particleI][2=charge]
       * @param platform              Brook platform
       * @param log                   optional Log file reference
       *
       * @return nonzero value if error
       * */
      
      int setup( int numberOfParticles, const std::vector<std::vector<double> >& nonbondedParameters,
                 const std::vector<std::set<int> >& exclusions,  const Platform& platform );
      
      /** 
       * Set duplication factor
       * 
       * @param   duplication factor
       *
       * @return  DefaultReturnValue
       *
       */
      
      int setDuplicationFactor( int duplicationFactor );
      
      /* 
       * Get contents of object
       *
       * @param level of dump
       *
       * @return string containing contents
       *
       * */
      
      std::string getContentsString( int level = 0 ) const;

   private:
   
      // fixed number of force streams

      static const int NumberOfForceStreams     = 4;

      // streams indices

      enum { 
              ExclusionStream,
              OuterVdwStream,
              ChargeStream,
              InnerSigmaStream,
              InnerEpsilonStream,
              LastStreamIndex
           };

      // particle ceiling

      int _particleSizeCeiling;

      // unroll in i/j dimensions

      int _outerUnroll;
      int _innerUnroll;

      // duplication factor

      int _duplicationFactor;

      // force stream width

      int _partialForceStreamWidth;
      int _partialForceStreamHeight;
      int _partialForceStreamSize;

      // exclusions stream dimensions

      int _exclusionStreamWidth;
      int _exclusionStreamHeight;
      int _exclusionStreamSize;

      // j-stream dimensions

      int _jStreamWidth;
      int _jStreamHeight;
      int _jStreamSize;

      // internal streams

      BrookFloatStreamInternal* _nonbondedStreams[LastStreamIndex];
      BrookFloatStreamInternal* _nonbondedForceStreams[NumberOfForceStreams];

      /* 
       * Setup of stream dimensions for exclusion stream
       *
       * @param particleStreamSize        particle stream size
       * @param particleStreamWidth       particle stream width
       *
       * @return ErrorReturnValue if error, else DefaultReturnValue
       *
       * */
      
      int _initializeExclusionStreamSize( int particleStreamSize, int particleStreamWidth );
      
      /** 
       * Initialize exclusion stream dimensions and stream
       * 
       * @param platform                  platform
       *
       * @return nonzero value if error
       *
       */
      
      int _initializeExclusionStream( const Platform& platform );
            
      /** 
       * Set exclusion (4x4)
       * 
       * @param i                         particle i index
       * @param j                         particle j index
       * @param exclusionStreamWidth      exclusion stream width
       * @param exclusion                 array of packed exclusions
       *
       * @return nonzero value if error
       *
       */
      
      int _setExclusion( int i, int j, int exclusionStreamWidth, BrookOpenMMFloat* exclusion );
      
      /** 
       * Initialize exclusions
       * 
       * @param exclusions                vector of sets containing exclusions (1 set entry for every particle)
       * @param platform                  platform
       *
       * @return nonzero value if error
       *
       */
      
      int _initializeExclusions( const std::vector<std::set<int> >& exclusionsVector, const Platform& platform );

      /** 
       * Initialize stream dimensions
       * 
       * @param numberOfParticles             number of particles
       * @param platform                  platform
       *
       * @return ErrorReturnValue if error, else DefaultReturnValue
       *
       */
      
      int _initializeStreamSizes( int numberOfParticles, const Platform& platform );
      
      /** 
       * Initialize stream dimensions and streams
       * 
       * @param platform                  platform
       *
       * @return nonzero value if error
       *
       */
      
      int _initializeStreams( const Platform& platform );
      
      /* 
       * Setup of j-stream dimensions
       *
       * @param particleStreamSize        particle stream size
       * @param particleStreamWidth       particle stream width
       *
       * @return ErrorReturnValue if error, else DefaultReturnValue
       *
       * @throw  OpenMMException  if jStreamWidth < 1 || innerUnroll < 1
       *
       * */
      
      int _initializeJStreamSize( int particleStreamSize, int particleStreamWidth );

      /* 
       * Setup of outer vdw stream size
       *
       * @param particleStreamSize        particle stream size
       * @param particleStreamWidth       particle stream width
       *
       * @return ErrorReturnValue if error, else DefaultReturnValue
       *
       * @throw  OpenMMException  if jStreamWidth < 1 || innerUnroll < 1
       *
       * */
      
      int _initializeOuterVdwStreamSize( int particleStreamSize, int particleStreamWidth );

      /** 
       * Set sigma & epsilon given c6 & c12 (geometric rule)
       * 
       * @param c6                        vdw c6
       * @param c12                       vdw c12
       * @param sigma                     massaged sigma
       * @param epsilon                   massaged epsilon
       *
       * @return nonzero value if error
       *
       */
      
      int _setSigmaEpsilon( double c6, double c12, double* sigma , double* epsilon );

      /** 
       * Initialize vdw & charge
       * 
       * @param exclusions                vector of sets containing exclusions (1 set entry for every particle)
       * @param platform                  platform
       *
       * @return nonzero value if error
       *
       */
      
      int _initializeVdwAndCharge( const std::vector<std::vector<double> >& nonbondedParameters, const Platform& platform );
      
      /** 
       * Initialize vdw & charge
       * 
       * @param exclusions                vector of sets containing exclusions (1 set entry for every particle)
       * @param platform                  platform
       *
       * @return nonzero value if error
       *
       */
      
      int _initializeJStreamVdw( const std::vector<std::vector<double> >& nonbondedParameters, const Platform& platform );
      
      /* 
       * Setup of stream dimensions for partial force streams
       *
       * @param particleStreamSize        particle stream size
       * @param particleStreamWidth       particle stream width
       *
       * @return ErrorReturnValue if error, else DefaultReturnValue
       *
       * */
      
      int _initializePartialForceStreamSize( int particleStreamSize, int particleStreamWidth );
      
      /* 
       * Utility to compute distances between two points
       *
       * @param idx1       index of point1 into array 
       * @param idx2       index of point2 into array 
       * @param coord      array of points p1={coord[0], coord[1], coord[2] },
       *                                   p2={coord[3], coord[4], coord[5] },
       *                                   p3={coord[6], coord[7], coord[8] }
       *
       * @return distance
       *
       * */
      
      float _computeDistance( int idx1, int idx2, float* coord );
};

} // namespace OpenMM

#endif /* OPENMM_BROOK_NONBONDED_H_ */
