package etomica.potential;

import etomica.api.IAtomList;
import etomica.api.IBoundary;
import etomica.api.IBox;
import etomica.api.IMolecule;
import etomica.api.IMoleculeList;
import etomica.api.IPotentialAtomic;
import etomica.api.IPotentialMolecular;
import etomica.api.IVector;
import etomica.api.IVectorMutable;
import etomica.atom.DipoleSource;
import etomica.atom.IAtomOriented;
import etomica.space.ISpace;
import etomica.space.Space;
import etomica.space.Tensor;
import etomica.util.numerical.BesselFunction;

public class PotentialCalculationPhiSumHeisenberg implements PotentialCalculationMolecular {
	 protected final IVectorMutable ei,ej;
	 protected IVectorMutable dr;
	 protected double secondDerivativeSum= 0;
	 protected DipoleSource dipoleSource;
	 
	 protected  double Q ,mu,J,bt;

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

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

		Tensor[] t = potentialSecondDerivative.secondDerivative(atoms);
		
		IAtomOriented atom1 = (IAtomOriented)atoms.getAtom(0);
    	IAtomOriented atom2 = (IAtomOriented)atoms.getAtom(1);
    	
    	ei.E(atom1.getOrientation().getDirection());
    	ej.E(atom2.getOrientation().getDirection());
    	

		double s1 = ei.getX(1);
		double s2 = ej.getX(1);


		secondDerivativeSum += 2*t[0].component(0, 0)*s1*s2 + t[1].component(0, 0)*s1*s1
				+t[2].component(0, 0)*s2*s2;

//		System.out.println( t[0].component(0, 0) + " " + t[1].component(0, 0) + " " + t[2].component(0, 0));
//		System.out.println("secondDerivative = " + secondDerivativeSum);
//		System.exit(2);
	}

	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;
    }
	

}

@@ -765,43 +765,8 @@
 %0A%09 %0A
-%09 protected  double Q ,mu,J,bt;%0A%0A%09 
 %0A%09pu
@@ -1372,22 +1372,16 @@
 tom(1);%0A
-    %09%0A
     %09ei.
@@ -1476,14 +1476,74 @@
 ));%0A
-    %09%0A
+%0A%09%09double c1 = ei.getX(0);//cost1%0A%09%09double c2 = ej.getX(0);//cost2
 %0A%09%09d
@@ -1564,16 +1564,23 @@
 getX(1);
+//sint1
 %0A%09%09doubl
@@ -1601,320 +1601,511 @@
 (1);
-%0A%0A%0A%09%09secondDerivativeSum += 2*t%5B0%5D.component(0, 0)*s1*s2 + t%5B1%5D.component(0, 0)*s1*s1%0A%09%09%09%09+t%5B2%5D.component(0, 0)*s2*s2;%0A%0A//%09%09System.out.println( t%5B0%5D.component(0, 0) + %22 %22 + t%5B1%5D.component(0, 0) + %22 %22 + t%5B2%5D.component(0, 0));%0A//%09%09System.out.println(%22secondDerivative = %22 + secondDerivativeSum);%0A//%09%09System.exit(2)
+//sint2%0A%0A%09%09//ij phi_ij = -J*(1+c1*c2/s1/s2); J would be multiplied in meter%0A%09%09//-J Sin%5Bt1%5D Sin%5Bt2%5D (Cos%5Bt1%5D Cos%5Bt2%5D + Sin%5Bt1%5D Sin%5Bt2%5D)%0A//%09%09secondDerivativeSum += -2.0*(s1*s2+c1*c2)*s1*s2;%0A%0A%09%09//ii phi_ii = J*s2%5E3/s1; J would be multiplied in meter%0A%09%09//J Sin%5Bt1%5D Sin%5Bt2%5D%0A%09%09//jj phi_jj = J*s1%5E3/s2; J would be multiplied in meter%0A%09%09//J Sin%5Bt1%5D Sin%5Bt2%5D%0A%09%09//ii and jj is the same%0A//%09%09secondDerivativeSum += 2.0*s1*s2;%0A%0A%09%09//or you could combine ij ii and jj%0A%09%09secondDerivativeSum += -2.0*(s1*s2+c1*c2-1)*s1*s2
 ;%0A%09%7D

simplify phi Sum