/*****************************************************
**
**  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 <stdlib.h>
#include <string.h>
#include "shi.h"
#include "shiStats.h"


//count the number of particles which have optimal energetics - a cheap and cheerful order parameter
int shiStats::numberBuried( shi *shiState ) {

    int    i,j, ij, ii, jj;
    int    n;

    n     =  0;
    for( ij = 0; ij < shiState->nUpSpins; ij++ ){

            i = shiState->upSpinXpos[ij];
            j = shiState->upSpinYpos[ij];

            ii = i + 1;
            if( ii >= shiState->xSize )               ii -= shiState->xSize;
            if( shiState->spins[ii][j] == SPIN_DOWN ) continue; 

            jj = j + 1;
            if( jj > shiState->ySize - 1 )            jj -= shiState->ySize;
            if( shiState->spins[i][jj] == SPIN_DOWN ) continue; 

            ii = i - 1;
            if( ii < 0 )                              ii += shiState->xSize;
            if( shiState->spins[ii][j] == SPIN_DOWN ) continue; 

            jj = j - 1;
            if( jj < 0 )                              jj += shiState->ySize;
            if( shiState->spins[i][jj] != SPIN_DOWN ) n++; 
                
    }
    
    return( n );
}



