KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > HEPsimulator


1 // HEPsimulator
2
// High Energy Particle Physics Simulator
3
//
4
// Written by Mark Hale (mark.hale@physics.org)
5

6 import java.awt.*;
7 import java.awt.event.*;
8 import JSci.physics.relativity.*;
9 import JSci.physics.quantum.*;
10 import JSci.physics.particles.*;
11
12 /**
13 * HEPsimulator.
14 * This is the main application class.
15 * @version 1.3
16 * @author Mark Hale (mark.hale@physics.org)
17 */

18 public final class HEPsimulator extends Frame implements Runnable JavaDoc {
19         private final Runnable JavaDoc thr=this;
20         private MenuBar mBar=new MenuBar();
21         private Menu mExp=new Menu("Experiment");
22         private Menu mRes=new Menu("Results");
23         private MenuItem mDev=new MenuItem("Device");
24         private MenuItem mSet=new MenuItem("Setup");
25         private MenuItem mCalc=new MenuItem("Calculate");
26         private DeviceDialog dlgDevice;
27         private SetupDialog dlgSetup;
28         /**
29         * The Particle Database.
30         */

31         private QuantumParticle particles[]=new QuantumParticle[38];
32
33         private QuantumParticle ingoing[]=new QuantumParticle[2];
34         private QuantumParticle outgoing[]=new QuantumParticle[2];
35         private CMframe uni;
36         /**
37         * Constructor.
38         */

39         public HEPsimulator() {
40                 super("HEP simulator");
41                 addWindowListener(new WindowAdapter() {
42                         public void windowClosing(WindowEvent evt) {
43                                 dlgDevice.dispose();
44                                 dlgSetup.dispose();
45                                 dispose();
46                                 System.exit(0);
47                         }
48                 });
49                 mDev.addActionListener(new ActionListener() {
50                         public void actionPerformed(ActionEvent evt) {
51                                 dlgDevice.show();
52                         }
53                 });
54                 mSet.addActionListener(new ActionListener() {
55                         public void actionPerformed(ActionEvent evt) {
56                                 dlgSetup.show();
57                         }
58                 });
59                 mCalc.addActionListener(new ActionListener() {
60                         public void actionPerformed(ActionEvent evt) {
61                                 new Thread JavaDoc(thr).start();
62                         }
63                 });
64                 setSize(400,300);
65                 mBar.add(mExp);
66                 mExp.add(mDev);
67                 mExp.add(mSet);
68                 mBar.add(mRes);
69                 mRes.add(mCalc);
70                 setMenuBar(mBar);
71                 loadDatabase();
72                 dlgDevice=new DeviceDialog(this,dlgSetup=new SetupDialog(this,particles));
73         }
74         public static void main(String JavaDoc args[]) {
75                 HEPsimulator sim=new HEPsimulator();
76                 sim.start();
77         }
78         public void start() {
79                 show();
80         }
81         /**
82         * Performs the experiment.
83         */

84         public void run() {
85                 uni=new CMframe();
86                 ingoing[0]=dlgSetup.getParticleA();
87                 ingoing[1]=dlgSetup.getParticleB();
88                 uni.setRelCMvel(ingoing[0],ingoing[1]);
89                 uni.addParticle(ingoing[0]);
90                 if(ingoing[1]!=null)
91                         uni.addParticle(ingoing[1]);
92
93                 try {
94                         for(int i=0;i<particles.length;i++) {
95                                 outgoing[0]=(QuantumParticle) particles[i].getClass().newInstance();
96                                 for(int j=i;j<particles.length;j++) {
97                                         outgoing[1]=(QuantumParticle) particles[j].getClass().newInstance();
98                                         if(uni.conserve(outgoing[0],outgoing[1]))
99                                                 new ResultsDialog(this,ingoing,outgoing,uni.interact()).show();
100                                 }
101                         }
102                 } catch(Exception JavaDoc e) {}
103         }
104         /**
105         * Loads up the particle database.
106         */

107         private void loadDatabase() {
108 // LEPTIONS (12)
109
particles[0]=new Electron();
110                 particles[1]=new Positron();
111                 particles[2]=new ElectronNeutrino();
112                 particles[3]=new AntiElectronNeutrino();
113                 particles[4]=new Muon();
114                 particles[5]=new AntiMuon();
115                 particles[6]=new MuonNeutrino();
116                 particles[7]=new AntiMuonNeutrino();
117                 particles[8]=new Tau();
118                 particles[9]=new AntiTau();
119                 particles[10]=new TauNeutrino();
120                 particles[11]=new AntiTauNeutrino();
121 // MESONS (8)
122
particles[12]=new PiZero();
123                 particles[13]=new PiPlus();
124                 particles[14]=new PiMinus();
125                 particles[15]=new KPlus();
126                 particles[16]=new KMinus();
127                 particles[17]=new KZero();
128                 particles[18]=new AntiKZero();
129                 particles[19]=new Eta();
130 // BARYONS (18)
131
particles[20]=new Proton();
132                 particles[21]=new AntiProton();
133                 particles[22]=new Neutron();
134                 particles[23]=new AntiNeutron();
135                 particles[24]=new Lambda();
136                 particles[25]=new AntiLambda();
137                 particles[26]=new SigmaPlus();
138                 particles[27]=new AntiSigmaPlus();
139                 particles[28]=new SigmaZero();
140                 particles[29]=new AntiSigmaZero();
141                 particles[30]=new SigmaMinus();
142                 particles[31]=new AntiSigmaMinus();
143                 particles[32]=new XiZero();
144                 particles[33]=new AntiXiZero();
145                 particles[34]=new XiMinus();
146                 particles[35]=new XiPlus();
147                 particles[36]=new OmegaMinus();
148                 particles[37]=new AntiOmegaMinus();
149         }
150 }
151
152
153 /**
154 * DeviceDialog.
155 * Sets the experimental apparatus.
156 * @version 1.3
157 * @author Mark Hale
158 */

159 class DeviceDialog extends Dialog {
160         private Button ok=new Button("OK");
161         private SetupDialog dlgRel;
162         private CheckboxGroup cbg=new CheckboxGroup();
163         private Checkbox cbChamber=new Checkbox("Bubble chamber",cbg,true);
164         private Checkbox cbAccelerator=new Checkbox("Particle accelerator",cbg,false);
165         public DeviceDialog(Frame f,SetupDialog sd) {
166                 super(f,"Device Selection",true);
167                 addWindowListener(new WindowAdapter() {
168                         public void windowClosing(WindowEvent evt) {
169                                 setVisible(false);
170                         }
171                 });
172                 cbChamber.addItemListener(new ItemListener() {
173                         public void itemStateChanged(ItemEvent evt) {
174                                 dlgRel.particleB.setEnabled(false);
175                                 dlgRel.energyB.setEnabled(false);
176                         }
177                 });
178                 cbAccelerator.addItemListener(new ItemListener() {
179                         public void itemStateChanged(ItemEvent evt) {
180                                 dlgRel.particleB.setEnabled(true);
181                                 dlgRel.energyB.setEnabled(true);
182                         }
183                 });
184                 ok.addActionListener(new ActionListener() {
185                         public void actionPerformed(ActionEvent evt) {
186                                 dispose();
187                         }
188                 });
189                 setSize(200,120);
190                 dlgRel=sd;
191                 setLayout(new GridLayout(3,1));
192                 add(cbChamber);
193                 add(cbAccelerator);
194                 add(ok);
195                 dlgRel.particleB.setEnabled(false);
196                 dlgRel.energyB.setEnabled(false);
197         }
198 }
199
200
201 /**
202 * SetupDialog.
203 * Sets the experimental parameters.
204 * @version 1.3
205 * @author Mark Hale
206 */

207 class SetupDialog extends Dialog {
208         private Button ok=new Button("OK");
209         public Choice particleA=new Choice();
210         public Choice particleB=new Choice();
211         public TextField energyA,energyB;
212         private QuantumParticle array[];
213         public SetupDialog(Frame f,QuantumParticle qpArray[]) {
214                 super(f,"Device Settings",true);
215                 array=qpArray;
216                 energyA=new TextField(Double.toString(array[0].restMass()));
217                 energyB=new TextField(Double.toString(array[0].restMass()));
218                 addWindowListener(new WindowAdapter() {
219                         public void windowClosing(WindowEvent evt) {
220                                 setVisible(false);
221                         }
222                 });
223                 particleA.addItemListener(new ItemListener() {
224                         public void itemStateChanged(ItemEvent evt) {
225                                 energyA.setText(Double.toString(array[particleA.getSelectedIndex()].restMass()));
226                         }
227                 });
228                 particleB.addItemListener(new ItemListener() {
229                         public void itemStateChanged(ItemEvent evt) {
230                                 energyB.setText(Double.toString(array[particleB.getSelectedIndex()].restMass()));
231                         }
232                 });
233                 energyA.addActionListener(new ActionListener() {
234                         public void actionPerformed(ActionEvent evt) {
235                                 if(Double.valueOf(energyA.getText()).doubleValue() < array[particleA.getSelectedIndex()].restMass())
236                                         energyA.setText(Double.toString(array[particleA.getSelectedIndex()].restMass()));
237                         }
238                 });
239                 energyB.addActionListener(new ActionListener() {
240                         public void actionPerformed(ActionEvent evt) {
241                                 if(Double.valueOf(energyB.getText()).doubleValue() < array[particleB.getSelectedIndex()].restMass())
242                                         energyB.setText(Double.toString(array[particleB.getSelectedIndex()].restMass()));
243                         }
244                 });
245                 ok.addActionListener(new ActionListener() {
246                         public void actionPerformed(ActionEvent evt) {
247                                 dispose();
248                         }
249                 });
250                 setSize(350,130);
251                 GridBagLayout gb=new GridBagLayout();
252                 GridBagConstraints c=new GridBagConstraints();
253                 for(int n=0;n<array.length;n++) {
254                         particleA.addItem(array[n].toString());
255                         particleB.addItem(array[n].toString());
256                 }
257                 setLayout(gb);
258                 c.anchor=GridBagConstraints.CENTER;
259                 c.gridwidth=GridBagConstraints.RELATIVE;
260                 gb.setConstraints(particleA,c);
261                 add(particleA);
262                 c.gridwidth=GridBagConstraints.REMAINDER;
263                 gb.setConstraints(particleB,c);
264                 add(particleB);
265                 c.gridwidth=GridBagConstraints.RELATIVE;
266                 gb.setConstraints(energyA,c);
267                 add(energyA);
268                 c.gridwidth=GridBagConstraints.REMAINDER;
269                 gb.setConstraints(energyB,c);
270                 add(energyB);
271                 c.gridwidth=GridBagConstraints.REMAINDER;
272                 gb.setConstraints(ok,c);
273                 add(ok);
274         }
275         public QuantumParticle getParticleA() {
276                 QuantumParticle temp;
277                 try {
278                         temp=(QuantumParticle) array[particleA.getSelectedIndex()].getClass().newInstance();
279                 } catch(Exception JavaDoc e) {
280                         temp=null;
281                 }
282                 double energy=Double.valueOf(energyA.getText()).doubleValue();
283                 double mass=temp.restMass();
284                 temp.momentum=new Rank1Tensor(energy,Math.sqrt(energy*energy-mass*mass),0.0,0.0);
285                 return temp;
286         }
287         public QuantumParticle getParticleB() {
288                 QuantumParticle temp;
289                 if(particleB.isEnabled()) {
290                         try {
291                                 temp=(QuantumParticle) array[particleB.getSelectedIndex()].getClass().newInstance();
292                         } catch(Exception JavaDoc e) {
293                                 temp=null;
294                         }
295                         double energy=Double.valueOf(energyB.getText()).doubleValue();
296                         double mass=temp.restMass();
297                         temp.momentum=new Rank1Tensor(energy,-Math.sqrt(energy*energy-mass*mass),0.0,0.0);
298                 } else
299                         temp=null;
300                 return temp;
301         }
302 }
303
304
305 /**
306 * ResultsDialog.
307 * Displays the results of an experiment.
308 * @version 1.3
309 * @author Mark Hale
310 */

311 class ResultsDialog extends Dialog {
312         private Button ok=new Button("OK");
313         public ResultsDialog(Frame f,QuantumParticle in[],QuantumParticle out[],String JavaDoc interact) {
314                 super(f,"Results",false);
315                 addWindowListener(new WindowAdapter() {
316                         public void windowClosing(WindowEvent evt) {
317                                 dispose();
318                         }
319                 });
320                 ok.addActionListener(new ActionListener() {
321                         public void actionPerformed(ActionEvent evt) {
322                                 dispose();
323                         }
324                 });
325                 setSize(300,300);
326                 setLayout(new GridLayout(9,1));
327                 if(in[1]==null)
328                         add(new Label(in[0].toString()+"------>"));
329                 else
330                         add(new Label(in[0].toString()+" ---> <--- "+in[1].toString()));
331                 add(new Label(out[0].toString()));
332                 add(new Label("Energy = "+Double.toString(out[0].momentum.getComponent(0))));
333                 add(new Label("Momentum = "+Double.toString(out[0].momentum.getComponent(1))));
334                 add(new Label(out[1].toString()));
335                 add(new Label("Energy = "+Double.toString(out[1].momentum.getComponent(0))));
336                 add(new Label("Momentum = "+Double.toString(out[1].momentum.getComponent(1))));
337                 add(new Label("Interaction was the "+interact));
338                 add(ok);
339         }
340 }
341
342 /**
343 * CMframe.
344 * Defines a class representing the Centre of Mass frame.
345 * @version 1.3
346 * @author Mark Hale
347 */

348 final class CMframe {
349         /**
350         * Total energy.
351         */

352         private double energy;
353         /**
354         * Quantum number totals.
355         */

356         private int B,I,Iz,Le,Lm,Lt,Q,S;
357         /**
358         * The relative velocity between the Centre
359         * of Mass frame and the lab frame.
360         */

361         private double relCMvel;
362         /**
363         * Interaction.
364         */

365         private int interaction;
366         /**
367         * Interaction codes.
368         */

369         private final static int STRONG=1,WEAK=2,EM=3;
370         /**
371         * Constructor.
372         */

373         public CMframe() {
374                 energy=B=I=Iz=Le=Lm=Lt=Q=S=0;
375         }
376         /**
377         * Checks whether Particles a and b obey the
378         * conservation laws.
379         */

380         public boolean conserve(QuantumParticle a,QuantumParticle b) {
381                 double Ei,Ej,pi,pj;
382         // work out interaction type (also check conservation of quantum numbers)
383
if(a.charge()+b.charge()==Q && a.eLeptonQN()+b.eLeptonQN()==Le &&
384                         a.muLeptonQN()+b.muLeptonQN()==Lm && a.tauLeptonQN()+b.tauLeptonQN()==Lt &&
385                         a.baryonQN()+b.baryonQN()==B && Math.abs(S-a.strangeQN()-b.strangeQN())<=1) {
386                         if(a.strangeQN()+b.strangeQN()==S && a.isospinZ()+b.isospinZ()==Iz) {
387                                 if(a.isospin()==0 && b.isospin()==0) {
388                                         // EM Interaction
389
interaction=EM;
390                                 } else {
391                                         // Strong Interaction
392
interaction=STRONG;
393                                 }
394                         } else {
395                                 // Weak Interaction
396
interaction=WEAK;
397                         }
398                 } else {
399                         interaction=0;
400                 }
401         // check quantum numbers are conserved
402
if(interaction>0) {
403         // Convert to keV for better accuracy
404
double massA=1000.0*a.restMass();
405                         double massB=1000.0*b.restMass();
406         // work out Ei,Ej,pi,pj
407
Ei=(energy*energy-massB*massB+massA*massA)/(2.0*energy);
408                         Ej=(energy*energy-massA*massA+massB*massB)/(2.0*energy);
409                         pi=(energy*energy*energy*energy +
410                                 massA*massA*massA*massA +
411                                 massB*massB*massB*massB -
412                                 2.0*massA*massA*energy*energy -
413                                 2.0*massB*massB*energy*energy -
414                                 2.0*massA*massA*massB*massB);
415         // check consistency
416
if(pi>=0.0 && Ei>=0.0 && Ej>=0.0) {
417                                 pi=Math.sqrt(pi)/(2.0*energy);
418         // Convert back to MeV and update E & p values
419
pi/=1000.0;
420                                 a.momentum=new Rank1Tensor(Ei/1000.0,pi,0.0,0.0);
421                                 b.momentum=new Rank1Tensor(Ej/1000.0,-pi,0.0,0.0);
422         // Perform Lorentz Transform
423
if(relCMvel!=0.0) {
424                                         LorentzBoost lb=new LorentzBoost(-relCMvel,0.0,0.0);
425                                         a.momentum=lb.multiply(a.momentum);
426                                         b.momentum=lb.multiply(b.momentum);
427                                 }
428                                 return true;
429                         }
430                 }
431                 return false;
432         }
433         public void addParticle(QuantumParticle qp) {
434                 B+=qp.baryonQN();
435                 I+=qp.isospin();
436                 Iz+=qp.isospinZ();
437                 Le+=qp.eLeptonQN();
438                 Lm+=qp.muLeptonQN();
439                 Lt+=qp.tauLeptonQN();
440                 Q+=qp.charge();
441                 S+=qp.strangeQN();
442
443                 LorentzBoost lb=new LorentzBoost(relCMvel,0.0,0.0);
444                 qp.momentum=lb.multiply(qp.momentum);
445
446                 energy+=qp.momentum.getComponent(0)*1000.0; // store as keV for better accuracy
447
}
448         /**
449         * Set the relative centre of mass velocity.
450         */

451         public void setRelCMvel(QuantumParticle a,QuantumParticle b) {
452                 if(b==null)
453                         relCMvel=a.momentum.getComponent(1)/a.momentum.getComponent(0);
454                 else
455                         relCMvel=(a.momentum.getComponent(1)+b.momentum.getComponent(1))/(a.momentum.getComponent(0)+b.momentum.getComponent(0));
456         }
457         /**
458         * Returns the type of interaction that took place.
459         */

460         public String JavaDoc interact() {
461                 switch(interaction) {
462                         case STRONG : return new String JavaDoc("Strong Force");
463                         case WEAK : return new String JavaDoc("Weak Force");
464                         case EM : return new String JavaDoc("Electromagnetic Force");
465                         default : return new String JavaDoc("Unknown - error");
466                 }
467         }
468 }
469
Popular Tags