package etomica.action;
import etomica.Atom;
import etomica.Space;
import etomica.data.DataSourceVelocityAverage;
import etomica.space.Vector;

/**
 * Sets the velocity of an atom to a specified vector value.  If applied
 * to a molecule, works with the average velocity of the atoms it comprises.
 */
public class AtomActionAccelerateTo extends AtomActionAdapter {
    
    private final Vector dr;
    private final Vector targetVelocity;
    private final AtomGroupAction atomAccelerator;
    private final AtomGroupAction velocityMeter;

    /**
     * Creates new action with atom position defined by its
     * center of mass (via DataSourceCOM).
     * @param space
     */
    public AtomActionAccelerateTo(Space space) {
        dr = space.makeVector();
        targetVelocity = space.makeVector();
        atomAccelerator = new AtomGroupAction(new AtomActionAccelerateBy(space));
        velocityMeter = new AtomGroupAction(new DataSourceVelocityAverage(space));
    }
    
    public void actionPerformed(Atom atom) {
        velocityMeter.actionPerformed(atom);
        Vector currentVelocity = ((DataSourceVelocityAverage)velocityMeter.getAction()).getVelocityAverage();
        dr.Ev1Mv2(targetVelocity, currentVelocity);
        ((AtomActionAccelerateBy)atomAccelerator.getAction()).setAccelerationVector(dr);
        atomAccelerator.actionPerformed(atom);
    }
       
    /**
     * @return Returns the targetVelocity, the velocity that the
     * atom will be accelerated to by this action.
     */
    public Vector getTargetVelocity() {
        return targetVelocity;
    }
    /**
     * @param destination The velocity to set.  A local copy
     * is made of the given vector.
     */
    public void setTargetVelocity(Vector targetVelocity) {
        this.targetVelocity.E(targetVelocity);
    }
}
@@ -107,16 +107,57 @@
 verage;%0A
+import etomica.space.ICoordinateKinetic;%0A
 import e
@@ -292,15 +292,21 @@
 the 
+mass-
 average
+d
  vel
@@ -336,16 +336,121 @@
 omprises
+,%0A * and adds a constant velocity to all atoms to make the mass-averaged velocity%0A * equal a target value
 .%0A */%0Apu
@@ -730,99 +730,38 @@
 ith 
-atom position defined by its%0A     * center of mass (via DataSourceCOM).%0A     * @param space
+target velocity equal to zero.
 %0A   
@@ -1076,49 +1076,333 @@
     
-public void actionPerformed(Atom atom) %7B%0A
+/**%0A     * Adds velocity vector to the atom (or to all atoms forming it) %0A     * so that its mass-average velocity equals the target velocity.%0A     */%0A    public void actionPerformed(Atom atom) %7B%0A        if(atom.type.isLeaf()) %7B%0A            ((ICoordinateKinetic)atom.coord).velocity().E(targetVelocity);%0A        %7D else %7B%0A    
     
@@ -1438,24 +1438,28 @@
 rmed(atom);%0A
+    
         Vect
@@ -1564,16 +1564,20 @@
         
+    
 dr.Ev1Mv
@@ -1620,16 +1620,20 @@
         
+ 
+   
 ((AtomAc
@@ -1705,32 +1705,36 @@
 or(dr);%0A        
+    
 atomAccelerator.
@@ -1752,24 +1752,34 @@
 rmed(atom);%0A
+        %7D%0A
     %7D%0A      
@@ -1831,24 +1831,37 @@
 locity, the 
+mass-average 
 velocity tha

fix up comments