package etomica.potential;

import etomica.atom.IAtomList;
import etomica.atom.IAtomOriented;
import etomica.molecule.DipoleSource;
import etomica.molecule.IMoleculeList;
import etomica.space.Space;
import etomica.space.Tensor;
import etomica.space.Vector;

public class PotentialCalculationPhiSumHeisenberg implements PotentialCalculationMolecular {
    protected final Vector ei, ej;
    protected Vector dr;
    protected double secondDerivativeSum = 0;
    protected DipoleSource dipoleSource;


    public PotentialCalculationPhiSumHeisenberg(Space space) {
        dr = space.makeVector();
        ei = space.makeVector();
        ej = space.makeVector();

    }

    public void doCalculation(IAtomList atoms, IPotentialAtomic potential) {
        if (!(potential instanceof IPotentialAtomicSecondDerivative)) {
            return;
        }


        IAtomOriented atom1 = (IAtomOriented) atoms.getAtom(0);
        IAtomOriented atom2 = (IAtomOriented) atoms.getAtom(1);
        ei.E(atom1.getOrientation().getDirection());
        ej.E(atom2.getOrientation().getDirection());

        //complicated way to get secondDerivativeSum, don't need the secondDerivative from p2Spin
        IPotentialAtomicSecondDerivative potentialSecondDerivative = (IPotentialAtomicSecondDerivative) potential;
        Tensor[] t = potentialSecondDerivative.secondDerivative(atoms);
        secondDerivativeSum += 2 * t[0].component(0, 0) * (ei.dot(ej));
        secondDerivativeSum += t[1].component(0, 0);
        secondDerivativeSum += t[2].component(0, 0);

        //much easier way but need to time back coupling J in the meter and here!!!!!!!!
//		double Cos = ei.dot(ej);
//		secondDerivativeSum += -2*Cos*Cos+2*Cos;
//		double diff = 2*t[0].component(0,0)*(ei.dot(ej))+t[1].component(0,0)+t[2].component(0,0)-1.5*( -2*Cos*Cos+2*Cos);
//		System.out.println("diff = " + diff);//check these two approach is the same or not
    }

    public void doCalculation(IMoleculeList molecules, IPotentialMolecular potential) {
        if (!(potential instanceof IPotentialMolecularSecondDerivative)) {
            return;
        }
    }


    public void zeroSum() {
        secondDerivativeSum = 0.0;
    }

    /**
     * Returns the current value of the energy sum.
     */
    public double getSum() {
        return secondDerivativeSum;
    }


}

@@ -362,14 +362,8 @@
 ted 
-final 
 Vect
@@ -373,16 +373,16 @@
 ei, ej;%0A
+
     prot
@@ -842,242 +842,9 @@
 %7D%0A%0A%0A
-        IAtomOriented atom1 = (IAtomOriented) atoms.getAtom(0);%0A        IAtomOriented atom2 = (IAtomOriented) atoms.getAtom(1);%0A        ei.E(atom1.getOrientation().getDirection());%0A        ej.E(atom2.getOrientation().getDirection());
 %0A
+
 %0A   
@@ -1300,24 +1300,24 @@
 ent(0, 0);%0A%0A
-
         //mu
@@ -1393,16 +1393,250 @@
 !!!!!!!%0A
+        IAtomOriented atom1 = (IAtomOriented) atoms.getAtom(0);%0A        IAtomOriented atom2 = (IAtomOriented) atoms.getAtom(1);%0A        ei.E(atom1.getOrientation().getDirection());%0A        ej.E(atom2.getOrientation().getDirection());%0A
 //%09%09doub

Remove final for vector ei and ej