/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package etomica.potential;

import etomica.atom.IAtomList;
import etomica.molecule.IMoleculeList;

/**
 * Evaluates the energy summed over all iterated atoms. Each call to doCalculate
 * accumulates the energy of the given potential applied to the atoms produced
 * by the given iterator. Each such call accumulates a single sum, which can
 * be accessed via the getSum() method.  Sum is re-zeroed only upon a call to the
 * zeroSum() method. 
 *
 * @author David Kofke
 */
public class PotentialCalculationEnergySum implements PotentialCalculation, PotentialCalculationMolecular, java.io.Serializable {

    public static boolean debug = false;
    
    /**
	 * Adds to the energy sum the energy values obtained from application of the given potential to the
	 * atoms.
	 */
	public void doCalculation(IAtomList atoms, IPotentialAtomic potential) {
	    sum += potential.energy(atoms);
	    if (debug && Double.isInfinite(sum) || Double.isNaN(sum)) {
	        System.err.println("unhappy energy "+sum+" for "+atoms);
	        potential.energy(atoms);
	        debug = false;
	    }
	}
	
    /**
     * Adds to the energy sum the energy values obtained from application of the given potential to the
     * molecules.
     */
    public void doCalculation(IMoleculeList molecules, IPotentialMolecular potential) {
        sum += potential.energy(molecules);
        if (debug && Double.isInfinite(sum) || Double.isNaN(sum)) {
            System.err.println("unhappy energy "+sum+" for "+molecules);
            potential.energy(molecules);
            debug = false;
        }
    }
    
	/**
	 * Sets the energy sum to zero, typically to begin a new energy-sum calculation.
	 */
	public void zeroSum() {
		sum = 0.0;
	}

	/**
	 * Returns the current value of the energy sum.
	 */
	public double getSum() {
        return sum;
    }
	
    private static final long serialVersionUID = 1L;
	protected double sum = 0.0;
}

@@ -1087,20 +1087,17 @@
 toms);%0A%09
-    
+%09
 if (debu
@@ -1093,32 +1093,33 @@
 %0A%09%09if (debug && 
+(
 Double.isInfinit
@@ -1146,16 +1146,17 @@
 aN(sum))
+)
  %7B%0A%09    
@@ -1556,24 +1556,18 @@
 cules);%0A
-        
+%09%09
 if (debu
@@ -1571,16 +1571,17 @@
 ebug && 
+(
 Double.i
@@ -1616,16 +1616,17 @@
 aN(sum))
+)
  %7B%0A     

fix debug check