// $AISD_VERSION:NumDemo.java@1.2/home/coei/arc/archive%shell3.ba.best.com$ //COPYRIGHT: Copyright (c) 1997 by Christopher K. Oei, Incorporated. //COPYRIGHT: //COPYRIGHT: http://www.chrisoei.com //COPYRIGHT: info@chrisoei.com //COPYRIGHT: //COPYRIGHT: Permission to use, display, and modify this code //COPYRIGHT: is hereby granted provided that: //COPYRIGHT: //COPYRIGHT: 1) this notice is included in its entirety //COPYRIGHT: 2) we are notified of the usage import java.applet.*; import java.awt.Graphics; import java.awt.*; import RKDemo; import Rectangle; import Line; import Potential; import Circle.*; public class NumDemo extends Applet { int width=500; //real width is 200 int height=700; // the real height is 200 int offsetx=50; int offsety=50; double x1 = 225.0; double y1 = 140; int metery = 580; int x2 = 225; int y2 = 540; int dx = 45; double rada = 100.; int go = 0; int toggE = -1; int toggB = -1; double tim = 0; double dtim=0.01; int r1 = 15; int ri = 25; public double[] x; public double[] values; public double[][] graph; static double qvalue = 0.; static double betavalue = 10.; static double lambda = 2.; int maxtim = 5; int brectcx; int brectcy; long red; long green; long blue; double squarefunc; double squarefunc1; double squarefunc2; double dx11; double dy11; double dx12; double dy12; double dx21; double dx22; double dy21; double dy22; double dx1; double dx2; double dy1; double dy2; double r; double arrowlength; double arrowlength2 = 20; double arrowlength3 = 60; double angle = 20; double radangle = angle*(Math.PI/180); double sinangle = Math.sin(radangle); double cosangle = Math.cos(radangle); double max = .00000000000000001; double dist1; double dist2; double distt; double compx, compx1, compx2; double compy, compy1, compy2; double compz, compz1, compz2; double dummy = 0.; int numline = 2; int si = 0; int ki = 0; int nmax = 24; int sii = 0; int iv; double xolder; Circle cone, ctwo, cthr; Scrollbar timeScroll = new Scrollbar(Scrollbar.HORIZONTAL,maxtim,1,10,300),betaScroll = new Scrollbar(Scrollbar.HORIZONTAL,(int)betavalue,1,0,200),qvalueScroll = new Scrollbar(Scrollbar.HORIZONTAL,(int)qvalue,1,0,100); public Label qvalueLabel = new Label("Resistance = " + qvalue), betaLabel = new Label("Dipole moment = " +betavalue), timeLabel = new Label("T = " +maxtim); Button restartbutton = new Button("Restart"), gobutton = new Button("Go"); Button toggleEbutton = new Button("Toggle E Vector Field"), toggleBbutton = new Button("Toggle B Vector Field"); public void init() { restartbutton.setForeground(Color.black); restartbutton.setBackground(Color.lightGray); gobutton.setForeground(Color.black); gobutton.setBackground(Color.lightGray); toggleEbutton.setForeground(Color.black); toggleEbutton.setBackground(Color.lightGray); toggleBbutton.setForeground(Color.black); toggleBbutton.setBackground(Color.lightGray); timeScroll.setForeground(Color.black); timeScroll.setBackground(Color.lightGray); betaScroll.setForeground(Color.black); betaScroll.setBackground(Color.lightGray); qvalueScroll.setForeground(Color.black); qvalueScroll.setBackground(Color.lightGray); setLayout(new BorderLayout()); Panel p = new Panel(); p.setLayout(new FlowLayout(FlowLayout.CENTER)); p.add(gobutton); p.add(restartbutton); p.add(toggleEbutton); p.add(toggleBbutton); add("North",p); Panel t = new Panel(); t.setLayout(new FlowLayout(FlowLayout.CENTER)); t.add(timeScroll); t.add(timeLabel); t.add(qvalueScroll); t.add(qvalueLabel); t.add(betaScroll); t.add(betaLabel); add("South",t); } public void Reset() { x1 = 225.0; y1 = 140; go = 0; tim = 0; repaint(); } public void Settvalue(String m) { maxtim = new Integer(m).intValue(); } public void Setqvalue(String m) { qvalue = new Double(m).doubleValue(); } public void Setbvalue(String m) { betavalue = new Double(m).doubleValue(); } public void SetGo() { go = 1; repaint(); } public void paint(Graphics g) { setBackground(java.awt.Color.white); // double values[] = {21.5,25.,30.}; // double values[] = {19.5,25.,30.}; // double values[] = {18.5,23.,28.}; double values[] = {12.5,16.5,21.}; x = new double[3]; graph = new double[2][(nmax+1)]; x[0]=4; x[1]=0; x[2]=0; double[] xold=new double[3]; xold[0]=x[0]; xold[1]=x[1]; xold[2]=x[2]; Rectangle rect = new Rectangle(15,10,(int)x1,(int)y1); RKDemo nint=new RKDemo(tim,x,.01); Line line=new Line(0.0,0.0,0.0,0.0); if(tim==0 && go==1) { while(tim 25.0) { dist1 = Math.sqrt(Math.pow(line.sx-x1, 2) + Math.pow(line.sy - y1, 2)); if (ij > 3) { distt=dist1; } else { distt=100.; } //compute components of dipole field with unit magnetic dipole moment compx1 = -(3.*(line.sx-x1)*(line.sy-y1)/(4.*Math.PI))/Math.pow(Math.pow(line.sx-x1,2)+Math.pow(line.sy-y1,2),2.5) ; compy1 = -((2.*Math.pow(line.sy-y1,2)-Math.pow(line.sx-x1,2))/(4.*Math.PI))/Math.pow(Math.pow(line.sx-x1,2) + Math.pow(line.sy-y1,2),2.5); //compute components of field due to ring, respectively // we compute curl numberically Potential pinto=new Potential(dummy,rada,line.sx-x1, line.sy-y2); Potential pintx=new Potential(dummy,rada,line.sx + intarc-x1, line.sy-y2); Potential pinty=new Potential(dummy,rada,line.sx-x1 , line.sy + intarc-y2); //note we are the dividing the dimensionless current by lambda beta radius radius where lambda is set to 2 above // see Force Memo #10, equation 10.28 // the expressions below is the curl of the potential in cylindrical coordinates, with x = usual rho and y = usual z signx= (line.sx-x1)/java.lang.Math.abs(line.sx-x1); compx2 = xold[1]*signx*( (pinty.Pota-pinto.Pota)/intarc )/(betavalue*betavalue*lambda*rada*rada); compy2 = -xold[1]*signx*( (pintx.Pota-pinto.Pota)/intarc + pinto.Pota/(line.sx-x1) )/(betavalue*betavalue*lambda*rada*rada); compx=compx1+compx2; compy=compy1+compy2; line.emag = Math.sqrt(Math.pow(compx,2) + Math.pow(compy,2)); line.ex = line.sx + delarc * (compx/line.emag); line.ey = line.sy + delarc * (compy/line.emag); if(si != 0) { g.setColor(java.awt.Color.blue); g.drawLine((int)line.sx, (int)line.sy, (int)line.ex, (int)line.ey); g.drawLine(225-((int)line.sx-225),(int)line.sy,225-((int)line.ex-225),(int)line.ey); } // draw arrow head on field line every once in awhile ilinc++; if (ilinc>5) { double vey,vex; vey = line.sy ; vex = line.sx ; dy1 = 1*(arrowlength2*((sinangle*compx-cosangle*compy)/line.emag)); dx1 = -1*(arrowlength2*((cosangle*compx+sinangle*compy)/line.emag)); dy2 = 1*(arrowlength2*((-sinangle*compx-cosangle*compy)/line.emag)); dx2 = -1*(arrowlength2*((cosangle*compx-sinangle*compy)/line.emag)); g.drawLine((int)vex, (int)vey, (int)(vex + dx1/2.),(int)(vey + dy1/2.)); g.drawLine((int)vex, (int)vey, (int)(vex + dx2/2.),(int)(vey + dy2/2.)); g.drawLine(450-(int)vex, (int)vey,450- (int)(vex + dx1/2.),(int)(vey + dy1/2.)); g.drawLine(450-(int)vex, (int)vey, 450-(int)(vex + dx2/2.),(int)(vey + dy2/2.)); ilinc=0; } line.sx = line.ex; line.sy = line.ey; ij++; si++; } sii++; if(sii==15) { g.setColor(java.awt.Color.white); g.fillRect(0,0,width,height); sii=0; } g.setColor(java.awt.Color.black); iv++; } // begin cut here // draw vector field for magnetic field double delarc = 13.; double intarc; intarc = delarc/3.; double signx; int ix=0; int iy=0; int numy; int numx; numx=2+ width/dx; numy=2+ height/dx; while(ix255) {red=255;} if(green>255) {green=255;} if(blue>255) {blue=255;} if(red<0) {red=0;} if(green<0) {green=0;} if(blue<0) {blue=0;} arrowlength = dx/6; line.ey = line.sy + (compy/line.emag) * dx/2.; line.ex = line.sx + dx*(compx/line.emag)/2.; dy1 = 1*(arrowlength*((sinangle*compx-cosangle*compy)/squarefunc)); dx1 = -1*(arrowlength*((cosangle*compx+sinangle*compy)/squarefunc)); dy2 = 1*(arrowlength*((-sinangle*compx-cosangle*compy)/squarefunc)); dx2 = -1*(arrowlength*((cosangle*compx-sinangle*compy)/squarefunc)); if (toggB > 0 ) { g.setColor(new Color((int)red,(int)green,(int)blue)); g.drawLine((int)line.sx, (int)line.sy, (int)line.ex, (int)line.ey); g.drawLine((int)line.ex, (int)line.ey, (int)(line.ex + dx1),(int)(line.ey + dy1)); g.drawLine((int)line.ex, (int)line.ey, (int)(line.ex + dx2),(int)(line.ey + dy2)); } // draw vector field for electric field if ( toggE > 0 ) { // compute electric field due to magnetic dipole: this is just v x B, where v is in the y-direction // so it picks out the x component of the B field due to the magnetic dipole compz1 = compx1*xold[2]*rada; //compute electric field due to ring // from B = curl A and curl E = - dB/dt, we get E = - dA/dt, so this devolves into dI/dt times // the spatial parts of the potential A (see Force Memo #6, equation 1) double Ider = RKDemo.Ideriv; // Ider is the time derivative of our dimensionless current wrt to our dimensionless time (see Force Memo #10) // System.out.println("Ider " +Ider ); compz2 = signx*Ider*pinto.Pota/(betavalue*betavalue*lambda*rada*rada); // above converts to real current, leaves time dimensionless compz=compz1+compz2; squarefunc = Math.sqrt(Math.pow(compz,2) + Math.pow(compz,2)); line.emag = squarefunc; r = line.emag/1.e-8; // System.out.println("r = " +r); red = (long)(255-r); green = (long)(255); blue = (long)(255-r); if(red>255) {red=255;} if(green>255) {green=255;} if(blue>255) {blue=255;} if(red<0) {red=0;} if(green<0) {green=0;} if(blue<0) {blue=0;} g.setColor(new Color((int)red,(int)green,(int)blue)); if(compz<0) { g.drawOval((int)line.sx-r1/2,(int)line.sy-r1/2,r1,r1); g.drawLine((int)line.sx-(r1*71/200),(int)line.sy-(r1*71/200),(int)line.sx+(r1*71/200),(int)line.sy+(r1*71/200)); g.drawLine((int)line.sx-(r1*71/200),(int)line.sy+(r1*71/200),(int)line.sx+(r1*71/200),(int)line.sy-(r1*71/200)); } else { g.drawOval((int)line.sx-r1/2,(int)line.sy-r1/2,r1,r1); g.fillOval((int)line.sx-r1/6,(int)line.sy-r1/6,r1/3,r1/3); } } iy++; } ix++; iy=0; } // end cut here // draw current in ring r = 22.5*java.lang.Math.abs(xold[1]); // System.out.println("r = " + r ); green = (long)(255-r); red = (long)(255); blue = (long)(255-r); if(red>255) {red=255;} if(green>255) {green=255;} if(blue>255) {blue=255;} if(red<0) {red=0;} if(green<0) {green=0;} if(blue<0) {blue=0;} g.setColor(new Color((int)red,(int)green,(int)blue)); line.sy=y2-12; line.sx=125.; if(xold[1]<0) { g.drawOval((int)line.sx,(int)line.sy,ri,ri); g.drawLine((int)line.sx-(ri*71/200)+ri/2,(int)line.sy-(ri*71/200)+ri/2,(int)line.sx+(ri*71/200)+ri/2,(int)line.sy+(ri*71/200)+ri/2 ); g.drawLine((int)line.sx-(ri*71/200)+ri/2,(int)line.sy+(ri*71/200)+ri/2,(int)line.sx+(ri*71/200)+ri/2,(int)line.sy-(ri*71/200)+ri/2 ); } else { g.drawOval((int)line.sx,(int)line.sy,ri,ri); g.fillOval((int)line.sx+ri/2-ri/6,(int)line.sy+ri/2-ri/6,ri/3,ri/3); } line.sx = 300.; if(xold[1]>0) { g.drawOval((int)line.sx,(int)line.sy,ri,ri); g.drawLine((int)line.sx-(ri*71/200)+ri/2,(int)line.sy-(ri*71/200)+ri/2,(int)line.sx+(ri*71/200)+ri/2,(int)line.sy+(ri*71/200)+ri/2 ); g.drawLine((int)line.sx-(ri*71/200)+ri/2,(int)line.sy+(ri*71/200)+ri/2,(int)line.sx+(ri*71/200)+ri/2,(int)line.sy-(ri*71/200)+ri/2 ); } else { g.drawOval((int)line.sx,(int)line.sy,ri,ri); g.fillOval((int)line.sx+ri/2-ri/6,(int)line.sy+ri/2-ri/6,ri/3,ri/3); } brectcx = rect.cx; brectcy = rect.cy; nint.iterate(); xold[0]=nint.x[0]; xold[1]=nint.x[1];xold[2]=nint.x[2]; tim+=dtim; xolder = xold[1] * 9; if(xolder > 45) { xolder = 45; } if(xolder < -45) { xolder = -45; } g.setColor(java.awt.Color.red); g.drawLine(438,metery+68,(438+(int)xolder),metery+15); g.setColor(java.awt.Color.black); // do line plot at bottom graph[0][ki] = 25.+(tim*40); // graph[1][ki] = 449. - Math.pow(xold[1],2); graph[1][ki] = 400. - 4.*xold[1]; g.setColor(Color.red); g.setColor(Color.white); // g.fillOval((int)graph[0][ki],(int)graph[1][ki],3,3); ki++; //System.out.println("ki = " +ki ); //System.out.println("Xcoord = " +(int)(25.+(tim*40))); g.setColor(java.awt.Color.black); // draw meter g.setColor(java.awt.Color.black); g.drawArc(390,metery,100,40,0,180); g.drawLine(390,metery+20,420,metery+70); g.drawLine(490,metery+20,460,metery+70); g.drawLine(420,metery+70,460,metery+70); g.fillOval(435,metery+65,6,6); g.drawOval(125,y2-20,(2*(int)rada),20); g.drawOval(125,y2,(2*(int)rada),20); g.drawLine(125,y2-10,125,y2+10); g.drawLine(325,y2-10,325,y2+10); g.drawOval(150,y2-20,((2*(int)rada)-50),20); g.drawOval(150,y2,((2*(int)rada)-50),20); g.drawLine(150,y2-10,150,y2+10); g.drawLine(300,y2-10,300,y2+10); // g.drawRect(25,350,450,100); if (ki <= nmax) { int bki = ki; ki = 0; while (ki < bki) { if(graph[1][ki] < 350) {g.setColor(java.awt.Color.white);} else if(graph[1][ki] > 350) {g.setColor(java.awt.Color.white);} g.fillOval((int)graph[0][ki],(int)graph[1][ki],3,3); ki++; } ki = bki; } if(ki == nmax) { ki = 0; while (ki < nmax) { graph[0][ki] = graph[0][(ki+1)]-1; graph[1][ki] = graph[1][(ki+1)]; ki++; } ki = nmax -1; } } } g.setColor(java.awt.Color.black); // draw meter g.setColor(java.awt.Color.black); g.drawArc(390,metery,100,40,0,180); g.drawLine(390,metery+20,420,metery+70); g.drawLine(490,metery+20,460,metery+70); g.drawLine(420,metery+70,460,metery+70); g.fillOval(435,metery+65,6,6); // draw the three field lines iv=0; while(iv < 3) { line.ang = values[iv]*(Math.PI/180.); line.sx = x1 - 10 * Math.sin(line.ang); line.sy = y1 - 10 * Math.cos(line.ang); distt=100.; double delarc = 13.; double intarc; int ij; double output1,output2; intarc = delarc/3.; double signx; int ilinc = 0; ij = 0; si = 0; // draw field lines from magnet, at three different angles, including field due to ring while(ij < 150 && distt > 25.0) { dist1 = Math.sqrt(Math.pow(line.sx-x1, 2) + Math.pow(line.sy - y1, 2)); if (ij > 3) { distt=dist1; } else { distt=100.; } //compute components of dipole field with unit magnetic dipole moment compx1 = -(3.*(line.sx-x1)*(line.sy-y1)/(4.*Math.PI))/Math.pow(Math.pow(line.sx-x1,2)+Math.pow(line.sy-y1,2),2.5) ; compy1 = -((2.*Math.pow(line.sy-y1,2)-Math.pow(line.sx-x1,2))/(4.*Math.PI))/Math.pow(Math.pow(line.sx-x1,2) + Math.pow(line.sy-y1,2),2.5); //compute components of field due to ring, respectively // we compute curl numberically Potential pinto=new Potential(dummy,rada,line.sx-x1, line.sy-y2); Potential pintx=new Potential(dummy,rada,line.sx + intarc-x1, line.sy-y2); Potential pinty=new Potential(dummy,rada,line.sx-x1 , line.sy + intarc-y2); //note we are the dividing the dimensionless current by lambda beta radius radius where lambda is set to 2 above // see Force Memo #10, equation 10.28 // the expressions below is the curl of the potential in cylindrical coordinates, with x = usual rho and y = usual z signx= (line.sx-x1)/java.lang.Math.abs(line.sx-x1); compx2 = xold[1]*signx*( (pinty.Pota-pinto.Pota)/intarc )/(betavalue*betavalue*lambda*rada*rada); compy2 = -xold[1]*signx*( (pintx.Pota-pinto.Pota)/intarc + pinto.Pota/(line.sx-x1) )/(betavalue*betavalue*lambda*rada*rada); compx=compx1+compx2; compy=compy1+compy2; line.emag = Math.sqrt(Math.pow(compx,2) + Math.pow(compy,2)); line.ex = line.sx + delarc * (compx/line.emag); line.ey = line.sy + delarc * (compy/line.emag); if(si != 0) { g.setColor(java.awt.Color.blue); g.drawLine((int)line.sx, (int)line.sy, (int)line.ex, (int)line.ey); g.drawLine(225-((int)line.sx-225),(int)line.sy,225-((int)line.ex-225),(int)line.ey); } // draw arrow head on field line every once in awhile ilinc++; if (ilinc>5) { double vey,vex; vey = line.sy ; vex = line.sx ; dy1 = 1*(arrowlength2*((sinangle*compx-cosangle*compy)/line.emag)); dx1 = -1*(arrowlength2*((cosangle*compx+sinangle*compy)/line.emag)); dy2 = 1*(arrowlength2*((-sinangle*compx-cosangle*compy)/line.emag)); dx2 = -1*(arrowlength2*((cosangle*compx-sinangle*compy)/line.emag)); g.drawLine((int)vex, (int)vey, (int)(vex + dx1/2.),(int)(vey + dy1/2.)); g.drawLine((int)vex, (int)vey, (int)(vex + dx2/2.),(int)(vey + dy2/2.)); g.drawLine(450-(int)vex, (int)vey,450- (int)(vex + dx1/2.),(int)(vey + dy1/2.)); g.drawLine(450-(int)vex, (int)vey, 450-(int)(vex + dx2/2.),(int)(vey + dy2/2.)); ilinc=0; } line.sx = line.ex; line.sy = line.ey; ij++; si++; } sii++; if(sii==15) { g.setColor(java.awt.Color.white); g.fillRect(0,0,width,height); sii=0; } g.setColor(java.awt.Color.black); iv++; } g.drawLine(125,y2-10,125,y2+10); g.drawLine(325,y2-10,325,y2+10); //draw ring g.drawOval(125,y2-20,(2*(int)rada),20); g.drawOval(125,y2,(2*(int)rada),20); g.drawLine(150,y2-10,150,y2+10); g.drawLine(300,y2-10,300,y2+10); g.drawOval(150,y2-20,((2*(int)rada)-50),20); g.drawOval(150,y2,((2*(int)rada)-50),20); g.drawLine(rect.cx-rect.rw,rect.cy-rect.rh+2,rect.cx-rect.rw,rect.cy+rect.rh+2); g.drawOval(rect.cx-rect.rw,rect.cy-rect.rh,(2*rect.rw),4); g.drawLine(rect.cx+rect.rw,rect.cy-rect.rh+2,rect.cx+rect.rw,rect.cy+rect.rh+2); g.drawOval(rect.cx-rect.rw,rect.cy+rect.rh,(2*rect.rw),4); // g.drawRect(25,350,450,100); } public boolean handleEvent(Event e) { Object target=e.target; if(target==timeScroll) { maxtim = timeScroll.getValue(); timeLabel.setText("T = " + maxtim); } if(target==qvalueScroll) { qvalue = qvalueScroll.getValue(); qvalueLabel.setText("Resistance = " + qvalue); } if(target==betaScroll) { betavalue = betaScroll.getValue(); betaLabel.setText("Dipole moment = " + betavalue); } if (e.id==Event.ACTION_EVENT) { if(target==restartbutton) { x1 = 225.0; y1 = 140; go = 0; tim = 0; go = 0; repaint(); } else if(target==gobutton) { go = 1; repaint(); } else if(target==toggleEbutton) { toggE = -1*toggE; repaint(); } else if(target==toggleBbutton) { toggB = -1*toggB; repaint(); } } return super.handleEvent(e); } }