In EJS :
1. You need to provide X[],Y[] for shapeSet. How do you related X[],Y[] to dipole[][]???
i.e. The [u]Pos X[/u] and [u]Pos Y[/u] should be an one-dimension array like X[20] or Y[20] (a double array)

And in the following loop you have: i+=2 so half of the index for dipoles[0][i] were not defined.
[code]
double dy=1.0/(nd-1);    //set up dipoles under bar magnet
for(int i=0; i < n d; i+=2) {
  dipoles[0][ i ]=-0.5+i*dy;
}
[/code]
By the way what is the meaning of dipoles[][]?
Why the dimension is dipoles[20][20]? instead of dipoles[20][2]?