
/* -------------------------------------------------------------------------- *
 *                                   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/>.      *
 * -------------------------------------------------------------------------- */

/**---------------------------------------------------------------------------------------

   This kernel calculates the linear momentum

   @param mass           stream of masses
   @param velocities     stream of velocities
   @param linearMomentum output stream of linear momentum

   --------------------------------------------------------------------------------------- */

kernel void kCalculateLinearMomentum( float mass<>, float3 velocities<>, out float3 linearMomentum<> ){
   linearMomentum = mass*velocities;
}

/**---------------------------------------------------------------------------------------

   This kernel scales the linear momentum

   @param scale              scale factor
   @param linearMomentumIn   input linearMomentum
   @param linearMomentumOut  output linearMomentum

   --------------------------------------------------------------------------------------- */

kernel void kScale( float scale, float3 linearMomentumIn<>, out float3 linearMomentumOut<> ){
   linearMomentumOut = scale*linearMomentumIn;
}

/**---------------------------------------------------------------------------------------

   This kernel calculates the total linear momentum via a reduction

   @param momentum       momentum
   @param linearMomentum total momentum

   Note: treating the output linearMomentum as non-stream did not compile, even though
   I found examples in Brook testing code that had that construct -- MSF 3/6/08

   --------------------------------------------------------------------------------------- */

reduce void kReduceLinearMomentum( float3 momentum<>, reduce float3 linearMomentum<> ){
   linearMomentum += momentum;
}

/**---------------------------------------------------------------------------------------

   This kernel calculates the total linear momentum via a reduction

   @param atomStrWidth   atom stream width
   @param numberOfAtoms  number of atoms
   @param momentum       momentum
   @param linearMomentum total momentum

   --------------------------------------------------------------------------------------- */

kernel void kSumLinearMomentum( float atomStrWidth, float numberOfAtoms, float scale, float3 momentum[][],
                                out float3 linearMomentum<> ){

   float atomCount;
   float2 atomIndex;

   atomCount      = 0.0f;
   linearMomentum = float3( 0.0f, 0.0f, 0.0f );
   atomIndex.y    =  0.0f;
   atomIndex.x    =  0.0f;

   while( atomCount < numberOfAtoms ){

      linearMomentum += momentum[atomIndex];

      atomIndex.x    += 1.0f;
      if( atomIndex.x == atomStrWidth ){
         atomIndex.x  = 0.0f; 
         atomIndex.y += 1.0f;
      }

      atomCount += 1.0f;
   } 
   linearMomentum *= scale;
}

/**---------------------------------------------------------------------------------------

   This kernel calculates the total linear momentum via a reduction

   @param momentum       momentum
   @param linearMomentum total momentum

   Note: treating the output linearMomentum as non-stream did not compile, even though
   I found examples in Brook testing code that had that construct -- MSF 3/6/08

   --------------------------------------------------------------------------------------- */

reduce void kSum( float3 array<>, reduce float3 sum<> ){
   //sum += array;
   //sum += array.x + array.y + array.z;
   //sum += float3( 1.0, 1.0, 1.0 );
/*
   sum.x += 1.0;
   sum.y += 1.0;
   sum.z += 1.0;
*/
   sum += array;
   //sum += 1.0;
}

/**---------------------------------------------------------------------------------------

   This kernel subtracts the (total linear momentum)/totalMass from the velocities 

   @param linearMomentum  normalized linear momentum
   @param velocities      stream of velocities
   @param velocitiesOut   output stream of velocities

   --------------------------------------------------------------------------------------- */

kernel void kRemoveLinearMomentum( float3 linearMomentum[], float3 velocities<>, out float3 velocitiesOut<> ){
   velocitiesOut = velocities - linearMomentum[0];
}
