/* 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.integrator.mcmove;

import etomica.api.IPotentialMaster;
import etomica.api.IRandom;
import etomica.atom.IAtomOriented;
import etomica.space.IOrientation;
import etomica.space.ISpace;

/**
 * Performs a rotation of an atom (not a molecule) that has an orientation coordinate.
 */
public class MCMoveRotate extends MCMoveAtom {
    
    private static final long serialVersionUID = 2L;
    private final IOrientation oldOrientation;

    private transient IOrientation iOrientation;

    public MCMoveRotate(IPotentialMaster potentialMaster, IRandom random,
    		            ISpace _space) {
        super(potentialMaster, random, _space, Math.PI/2, Math.PI, false);
        oldOrientation = _space.makeOrientation();
    }
    
    public boolean doTrial() {
        if(box.getMoleculeList().getMoleculeCount()==0) {return false;}
        atom = atomSource.getAtom();

        energyMeter.setTarget(atom);
        uOld = energyMeter.getDataAsScalar();
        iOrientation = ((IAtomOriented)atom).getOrientation(); 
        oldOrientation.E(iOrientation);  //save old orientation
        iOrientation.randomRotation(random, stepSize);
        uNew = energyMeter.getDataAsScalar();
        return true;
    }
    
    public void rejectNotify() {
        iOrientation.E(oldOrientation);
    }
}

@@ -235,16 +235,71 @@
 cmove;%0A%0A
+import etomica.api.IAtomList;%0Aimport etomica.api.IBox;%0A
 import e
@@ -453,16 +453,54 @@
 .ISpace;
+%0Aimport etomica.space3d.Orientation3D;
 %0A%0A/**%0A *
@@ -656,67 +656,8 @@
 ate 
-static final long serialVersionUID = 2L;%0A    private final 
 IOri
@@ -916,28 +916,105 @@
 false);%0A    
+%7D%0A
     
+%0A    public void setBox(IBox box) %7B%0A        super.setBox(box);%0A        if (
 oldOrientati
@@ -1020,11 +1020,352 @@
 ion 
+!
 = 
-_
+null) return;%0A        IAtomList atoms = box.getLeafList();%0A        if (atoms.getAtomCount() == 0) return;%0A        IAtomOriented atom0 = (IAtomOriented)atoms.getAtom(0);%0A        if (atom0.getOrientation() instanceof Orientation3D) %7B%0A            oldOrientation = new Orientation3D(space);%0A        %7D%0A        else %7B%0A            oldOrientation = 
 spac
@@ -1381,24 +1381,28 @@
 entation();%0A
+    
     %7D%0A    %0A 
@@ -1391,32 +1391,34 @@
 ;%0A        %7D%0A    
+%7D%0A
 %0A    public bool

make the appropriate type of orientation