/*****************************************************
**
**  S-PRES Copyright (c) 2010
**
**  Joshua T Berryman
**
**  This file is part of S-PRES
**
**  S-PRES is Free software: you can redistribute it and/or modify
**  it under the terms of the GNU General Public License as published by
**  the Free Software Foundation, either version 3 of the License, or
**  (at your option) any later version.
**
**  S-PRES 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 General Public License for more details.
**
**  You should have received a copy of the GNU General Public License
**  along with this program.  If not, see <http://www.gnu.org/licenses/>.
**
**  If you make use of this program for research 
**  resulting in a publication, please cite:
**
**  Joshua T Berryman and Tanja Schilling, 
**  Sampling rare events in nonequilibrium and nonstationary systems. 
**  
**  J Chem Phys, 133(24):244101, Dec 2010.
**
******************************************************/
#include <iostream>
#include <string>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <cstring>
#include <limits.h>
#include "../../spres/src/spres.h"



extern float ran2(long *idum);


int spres::assignRcBin( double rc ) {

    int     bin;
    double interval, fraction;

#ifdef ALLEN_FRENKEL_FLUX_CALC
    // (rc < rcBinMin) is bin 0     (state A in the parlance of Allen & Frenkel)
    // (rc > rcBinMax) is bin (n-1) (state B in Allen & Frenkel)

    //because of this definition 
    //the bin assignment code has to be a bit fiddly.
    if( rc < rcBinMin ){
        return( 0 );
    }
    else if( rc > rcBinMax ){
        return(  nRcBins - 1 );
    }
    interval = rcBinMax - rcBinMin;
    fraction = (rc - rcBinMin) / interval;
    bin      = int(fraction * nRcBins);

    if( bin == 0 ){
        return( 1 );
    }
    else if( bin >= nRcBins - 1 ){
        return( nRcBins - 2 );
    }
#else
    interval = rcBinMax - rcBinMin;
    fraction = (rc - rcBinMin) / interval;
    bin      = int(fraction * nRcBins);
    if( bin <= 0 ){
        return( 0 );
    }
    else if( bin >= nRcBins - 1 ){
        return( nRcBins - 1 );
    }
#endif

    return( bin );

}



