This is the web version of EJS interface
EJS version 4.2.7
  1. Title=Magnetic Bar Field
  2. Author=Wolfgang Chrisitan, Francisco Esquembre - Universidad de Murcia, Spain
  3. Keywords=magnetic field, dipole, bar magnet
  4. Level=introductory
  5. Abstract=e-mail: fem@um.es
  6. CaptureTools=false
  7. DataTools=false
  8. LanguageTools=false
  9. EmersionSupport=false
  10. UseInterpreter=true
  11. AuxiliaryFiles=./MagneticBarField/;
  12. DetectedFiles=./MagneticBarField/MagneticBarField.html;./MagneticBarField/circle.gif;./MagneticBarField/magnet.gif;
    NameValueTypeDimension
    NameValueTypeDimension
    NameValueTypeDimension
    NameValueTypeDimension

    Init Page

    double dx=1.0/(nd-1); //set up dipoles under bar magnet for(int i=0; i<nd; i+=2) { dipoles[i][0]=-0.5+i*dx; // x position dipoles[i][1]=-.04; // y position dipoles[i+1][0]=-0.5+i*dx; // x position dipoles[i+1][1]=0.04; // y position } computeField(); cs=Math.cos(c);//FKH sc=Math.sin(c);//FKH cs1=Math.cos(-c); sc1=Math.sin(-c);

    Evol Page

    Indep. Var. Increment
    d theta /dt=omega
    d omega /dt=-k*(Math.sin(deltaTheta))-b*omega
    SolverRungeKuttaFehlberg AbsoluteTolerance=0.00001

    FixRel Page

    cta= c; cs=Math.cos(c);//FKH sc=Math.sin(c);//FKH cs1=Math.cos(-c); sc1=Math.sin(-c); xMagnettransform = xMagnet*cs+yMagnet*sc; yMagnettransform = -xMagnet*sc+yMagnet*cs; xCompasstransform = xCompass*cs+yCompass*sc; yCompasstransform = -xCompass*sc+yCompass*cs; xCompasstransform1 = xCompasstransform*cs1+yCompasstransform*sc1; yCompasstransform1 = -xCompasstransform*sc1+yCompasstransform*cs1; //double[] b = getB(xCompass,yCompass); //adjust compass values double[] b = getB(xCompass*cs+yCompass*sc,-xCompass*sc+yCompass*cs); //FKH equilibriumTheta=b[3]; fieldValue="B="+_view.format(getB(xCompasstransform, yCompasstransform)[2],"0.00"); // works if the axs follow the rotation fieldValueBx="B_{x}= "+_view.format(getB(xCompasstransform, yCompasstransform)[0],"0.00")+" T"; fieldValueBy="B_{y}= "+_view.format(getB(xCompasstransform, yCompasstransform)[1],"0.00")+" T"; // resolve to x and y fieldValueBxresolve =(getB(xCompasstransform, yCompasstransform)[0])*cs1 + (getB(xCompasstransform, yCompasstransform)[1])*sc1; fieldValueByresolve =-(getB(xCompasstransform, yCompasstransform)[0])*sc1 + (getB(xCompasstransform, yCompasstransform)[1])*cs1; fieldValueBx1="B_{x1}= "+_view.format(fieldValueBxresolve,"0.00")+" T"; fieldValueBy1="B_{y1}= "+_view.format(fieldValueByresolve,"0.00")+" T"; //test //fieldValueBx="B_{x}= "+_view.format(getB(xCompasstransform1, yCompasstransform1)[0],"0.00")+" T"; //fieldValueBy="B_{y}= "+_view.format(getB(xCompasstransform1, yCompasstransform1)[1],"0.00")+" T"; fieldValueAngle="$\\theta$= "+_view.format(getB(xCompass, yCompass)[3],"0.00") + "radian"; angledeg= getB(xCompass, yCompass)[3]*180/Math.PI; fieldValueAngledeg = "$\\theta$= "+ _view.format(angledeg,"0.0") + "$\\degree$"; angledeg1 = Math.atan2(fieldValueByresolve,fieldValueBxresolve)*180/Math.PI; fieldValueAngledeg1 = "$\\theta$= "+ _view.format(angledeg1,"0.0") + "$\\degree$"; if(omega>0)omega=Math.min(omega,Math.PI); // keeps compass from spinning wildly else omega=Math.max(omega,-Math.PI);
    public void computeField () { for (int i=0; i<nx; i++) { double x=_view.vectorField2D.indexToX(i); for (int j=0; j<ny; j++) { double y=_view.vectorField2D.indexToY(j); //double[] b=getB(x,y);// Gets the components and magnitude double[] b=getB(x*cs+y*sc,-x*sc+y*cs);//FKH //xField[i][j] = b[0]; //yField[i][j] = b[1]; xField[i][j] = b[0]*cs-b[1]*sc;//FKH yField[i][j] = b[0]*sc+b[1]*cs;//FKH magField[i][j]=b[2]; } } }
    // computes magnetic field components and magnitude from dipoles public double[] getB(double x, double y) { double[] b=new double[4]; for(int i=0; i<nd; i++) { // double dx=x-dipoles[i][0]-xMagnet; // double dy=y-dipoles[i][1]-yMagnet; double dx=x-dipoles[i][0]-(xMagnet*cs+yMagnet*sc);//FKH double dy=y-dipoles[i][1]-(-xMagnet*sc+yMagnet*cs);//FKH // double dx=x*cs+y*sc-(dipoles[i][0]*cs+dipoles[i][1]*sc)-(xMagnet*cs+yMagnet*sc);//FKH //double dy=-x*sc+y*cs-(-dipoles[i][0]*sc +dipoles[i][1]*cs)-(-xMagnet*sc+yMagnet*cs);//FKH // double dx=x*cs+y*sc-(dipoles[i][0])-(xMagnet*cs+yMagnet*sc);//FKH // double dy=-x*sc+y*cs-(+dipoles[i][1])-(-xMagnet*sc+yMagnet*cs);//FKH // double dx=x-(dipoles[i][0]*cs+dipoles[i][1]*sc)-(xMagnet*cs+yMagnet*sc);//FKH // double dy=y-(-dipoles[i][0]*sc +dipoles[i][1]*cs)-(-xMagnet*sc+yMagnet*cs);//FKH double r2=dx*dx+dy*dy; double r=Math.sqrt(r2); double r3=r2*r; double cos=dx/r; double sin=dy/r; b[0]+=m*(3*cos*cos-1)/r3; //x component b[1]+=m*3*sin*cos/r3; //y component // b[0]+=m*((3*cos*cos-1)*cs+ 3*sin*cos*sc)/r3; //x component // b[1]+=m*(-(3*cos*cos-1)*sc+ 3*sin*cos*cs)/r3; //y component if(r2==0) { b[0]=b[1]=0; } } b[2] = Math.sqrt(b[0]*b[0]+b[1]*b[1]); // magnitude b[3] = Math.atan2(b[1],b[0]); // angle return b; } /* Dipole field: B=mu/4/pi/r^3[(m*rhat)rhat-m] //vector form K=mu*M/4/pi alpha=0 Bx=K*[(3*cos(theta)^2-1)*cos(alpha)+3*sin(theta)cos(theta)*sin(alpha)]/r^3 By=K*[(3*sin(theta)^2-1)*sin(alpha)+3*sin(theta)cos(theta)*cos(alpha)]/r^3 */
    public void setLocation () { //move magnet to random location if(randomMagnetLocation){ xMagnet = (-xmax + 2*xmax*Math.random()); yMagnet = (-ymax + 2*ymax*Math.random()); } computeField(); }
    EJSVIEW: Click link to view it's content
    Control variables:(testing)
    int
    title="Bar Magnet"
    layout=border
    visible=true
    location="27,118"
    size="584,664"
    position=center
    autoscaleX=false
    autoscaleY=false
    minimumX=-xmax
    maximumX=xmax
    minimumY=-ymax
    maximumY=ymax
    square=true
    majorTicksX=false
    majorTicksY=false
    fixedGutters=false
    interiorBackground=BLACK
    minimumX=-xmax
    maximumX=xmax
    minimumY=-ymax
    maximumY=ymax
    xcomponent=xField
    ycomponent=yField
    length=0.1
    visible=!hideField
    style=RHOMBUS
    elementposition=CENTERED
    colormode=RED
    magnitude=magField
    autoscale=false
    minimum=0
    maximum=4
    visible=hideField
    x=-0.28*cs
    y=-0.28*sc
    sizeX=.56
    sizeY=.22
    transformation=c
    style=ROUND_RECTANGLE
    lineColor=GRAY
    fillColor=CYAN
    lineWidth=2
    x=0.28*cs
    y=0.28*sc
    sizeX=.56
    sizeY=.22
    transformation=c
    style=ROUND_RECTANGLE
    lineColor=GRAY
    fillColor=MAGENTA
    lineWidth=2
    x=0
    y=0
    sizeX=.56
    sizeY=.22
    transformation=c
    enabledPosition=true
    movesGroup=true
    sensitivity=0
    style=RECTANGLE
    lineColor=GRAY
    fillColor=GRAY
    lineWidth=2
    pixelSize=true
    text="Drag Me"
    font=Dialog,BOLD,12
    elementposition=SOUTH
    lineColor=YELLOW
    fillColor=YELLOW
    pixelSize=true
    text="(To Magnet Location)"
    font=Dialog,BOLD,10
    elementposition=NORTH
    lineColor=YELLOW
    fillColor=YELLOW
    x=xMagnet
    y=yMagnet
    sizeX=1.08
    sizeY=0.22
    transformation=c
    visible=!hideField
    enabledPosition=true
    sensitivity=0
    dragAction=computeField()
    imageFile="./MagneticBarField/magnet.gif"
    x=xMagnet
    y=yMagnet
    pixelSize=true
    visible=!hideField
    text="Drag me"
    font=Dialog,BOLD,14
    lineColor=YELLOW
    fillColor=YELLOW
    x=xCompass
    y=yCompass
    sizeX=0.35
    sizeY=0.35
    fillColor=LIGHTGRAY
    x=0
    trueSize=false
    sizeX=0.4
    sizeY=0.4
    imageFile="./MagneticBarField/circle.gif"
    x=0
    y=0
    sizeX=1
    sizeY=0
    scalex=0.24
    scaley=0.24
    transformation=theta+c
    style=RHOMBUS
    elementposition=CENTERED
    fillColor=RED
    extraColor=BLUE
    x=0
    y=0
    sizeX=0.028
    sizeY=0.028
    enabledPosition=true
    movesGroup=true
    sensitivity=15
    lineColor=BLACK
    fillColor=BLACK
    y=-.21
    sizeX=0.25
    sizeY=0.08
    visible=!hideField
    style=ROUND_RECTANGLE
    fillColor=YELLOW
    x=0
    y=-0.22
    pixelSize=true
    visible=!hideField
    text=%fieldValue%
    elementposition=CENTERED
    x=xCompass
    y=yCompass
    sizeX=0.35
    sizeY=0.35
    fillColor=0,255,255,50
    x=0
    y=0
    sizeX=1
    sizeY=0
    scalex=0.24
    scaley=0.24
    transformation=theta+cta
    style=RHOMBUS
    elementposition=CENTERED
    fillColor=RED
    extraColor=CYAN
    x=0
    y=0
    sizeX=0.028
    sizeY=0.028
    enabledPosition=true
    movesGroup=true
    sensitivity=15
    lineColor=BLACK
    fillColor=BLACK
    x=-0.3
    y=-.17
    sizeX=1.1
    sizeY=0.50
    visible=!hideField
    style=ROUND_RECTANGLE
    elementposition=NORTH_WEST
    fillColor=255,255,0,200
    x=0
    y=-0.33
    pixelSize=true
    visible=!hideField
    text=%fieldValueBx%
    elementposition=CENTERED
    lineColor=BLUE
    fillColor=BLUE
    lineWidth=3
    x=0
    y=-0.44
    pixelSize=true
    visible=!hideField
    text=%fieldValueBy%
    elementposition=CENTERED
    lineColor=BLUE
    fillColor=BLUE
    x=0
    y=-0.22
    pixelSize=true
    visible=!hideField
    text=%fieldValue%
    elementposition=CENTERED
    lineColor=BLUE
    fillColor=BLUE
    x=0
    y=-0.55
    pixelSize=true
    visible=!hideField
    text=%fieldValueAngledeg%
    elementposition=CENTERED
    lineColor=BLUE
    fillColor=BLUE
    x=0
    trueSize=false
    sizeX=0.4
    sizeY=0.4
    imageFile="./MagneticBarField/circle.gif"
    x=+0.5
    y=-0.33
    pixelSize=true
    visible=!hideField
    text=%fieldValueBx1%
    elementposition=CENTERED
    lineColor=BLUE
    fillColor=BLUE
    lineWidth=3
    x=0.5
    y=-0.44
    pixelSize=true
    visible=!hideField
    text=%fieldValueBy1%
    elementposition=CENTERED
    lineColor=BLUE
    fillColor=BLUE
    x=0.5
    y=-0.55
    pixelSize=true
    visible=!hideField
    text=%fieldValueAngledeg1%
    elementposition=CENTERED
    lineColor=BLUE
    fillColor=BLUE
    position=south
    layout=BORDER:0,0
    borderType=LOWERED_ETCHED
    position=east
    layout=GRID:0,1,0,0
    variable=!hideField
    tooltip="Hide magnet and field."
    textOn="Hide Field"
    actionOn=hideField=true; setLocation();
    textOff="Show Field"
    actionOff=hideField=false; computeField();
    image="/org/opensourcephysics/resources/controls/images/reset.gif"
    action=_reset()
    size="33,25"
    tooltip="Reset button."
    position=west
    layout=HBOX
    visible=!hideField
    variable=randomMagnetLocation
    text="Randomize"
    position=center
    variable=ca
    minimum=-180
    maximum=180
    format="rotate magnet by c=0"
    dragaction=c=ca*a2c; cs=Math.cos(c); sc=Math.sin(c); computeField();