source: trunk/source/geometry/magneticfield/test/OtherFields/testHarmonicPolMagField.cc@ 1202

Last change on this file since 1202 was 1199, checked in by garnier, 16 years ago

nvx fichiers dans CVS

File size: 19.5 KB
Line 
1//
2// ********************************************************************
3// * License and Disclaimer *
4// * *
5// * The Geant4 software is copyright of the Copyright Holders of *
6// * the Geant4 Collaboration. It is provided under the terms and *
7// * conditions of the Geant4 Software License, included in the file *
8// * LICENSE and available at http://cern.ch/geant4/license . These *
9// * include a list of copyright holders. *
10// * *
11// * Neither the authors of this software system, nor their employing *
12// * institutes,nor the agencies providing financial support for this *
13// * work make any representation or warranty, express or implied, *
14// * regarding this software system or assume any liability for its *
15// * use. Please see the license in the file LICENSE and URL above *
16// * for the full disclaimer and the limitation of liability. *
17// * *
18// * This code implementation is the result of the scientific and *
19// * technical work of the GEANT4 collaboration. *
20// * By using, copying, modifying or distributing the software (or *
21// * any work based on the software) you agree to acknowledge its *
22// * use in resulting scientific publications, and indicate your *
23// * acceptance of all terms of the Geant4 Software license. *
24// ********************************************************************
25//
26//
27// $Id: testHarmonicPolMagField.cc,v 1.7 2006/06/29 18:26:43 gunter Exp $
28// GEANT4 tag $Name: HEAD $
29//
30//
31//
32// Started from testG4Navigator1.cc,v 1.7 1996/08/29 15:42 pkent
33// Locate & Step within simple boxlike geometry, both
34// with and without voxels. Parameterised volumes are included.
35
36#include <assert.h>
37// #include "ApproxEqual.hh"
38
39// Global defs
40#include "globals.hh"
41
42#include "G4Navigator.hh"
43
44#include "G4LogicalVolume.hh"
45#include "G4VPhysicalVolume.hh"
46#include "G4PVPlacement.hh"
47#include "G4PVParameterised.hh"
48#include "G4VPVParameterisation.hh"
49#include "G4Box.hh"
50
51#include "G4GeometryManager.hh"
52
53#include "G4RotationMatrix.hh"
54#include "G4ThreeVector.hh"
55
56#include "G4UniformMagField.hh"
57#include "G4DELPHIMagField.hh"
58#include "G4QuadrupoleMagField.hh"
59#include "G4HarmonicPolMagField.hh"
60// #include "G4LineCurrentMagField.hh"
61
62#include "G4ios.hh"
63#include <iomanip>
64
65// Sample Parameterisation
66class G4LinScale : public G4VPVParameterisation
67{
68 virtual void ComputeTransformation(const G4int n,
69 G4VPhysicalVolume* pRep) const
70 {
71 pRep->SetTranslation(G4ThreeVector(0,(n-1)*15,0));
72 }
73
74 virtual void ComputeDimensions(G4Box &pBox,
75 const G4int n,
76 const G4VPhysicalVolume* pRep) const
77 {
78 pBox.SetXHalfLength(10);
79 pBox.SetYHalfLength(5+n);
80 pBox.SetZHalfLength(5+n);
81 }
82
83 virtual void ComputeDimensions(G4Tubs &,
84 const G4int ,
85 const G4VPhysicalVolume*) const {}
86 virtual void ComputeDimensions(G4Trd &,
87 const G4int,
88 const G4VPhysicalVolume*) const {}
89 virtual void ComputeDimensions(G4Cons &,
90 const G4int ,
91 const G4VPhysicalVolume*) const {}
92 virtual void ComputeDimensions(G4Trap &,
93 const G4int ,
94 const G4VPhysicalVolume*) const {}
95 virtual void ComputeDimensions(G4Hype &,
96 const G4int ,
97 const G4VPhysicalVolume*) const {}
98 virtual void ComputeDimensions(G4Sphere &,
99 const G4int ,
100 const G4VPhysicalVolume*) const {}
101 virtual void ComputeDimensions(G4Torus &,
102 const G4int ,
103 const G4VPhysicalVolume*) const {}
104 virtual void ComputeDimensions(G4Para &,
105 const G4int ,
106 const G4VPhysicalVolume*) const {}
107};
108G4LinScale myParam;
109
110// Build simple geometry:
111// 4 small cubes + 1 slab (all G4Boxes) are positioned inside a larger cuboid
112G4VPhysicalVolume* BuildGeometry()
113{
114
115 G4Box *myHugeBox= new G4Box("huge box",15*m,15*m,25*m);
116 G4Box *myBigBox= new G4Box("big cube",10*m,10*m,10*m);
117 G4Box *mySmallBox= new G4Box("smaller cube",2.5*m,2.5*m,2.5*m);
118 G4Box *myTinyBox= new G4Box("tiny cube",.25*m,.25*m,.25*m);
119
120 // G4Box *myVariableBox=
121 new G4Box("Variable Box",10,5,5);
122
123 // World Volume
124 //
125 G4LogicalVolume *worldLog=new G4LogicalVolume(myHugeBox,0,
126 "World",0,0,0);
127 // Logical with no material,field,
128 // sensitive detector or user limits
129
130 G4PVPlacement *worldPhys=new
131 G4PVPlacement(0,G4ThreeVector(0,0,0), "World",worldLog,
132 0,false,0);
133 // Note: no mother pointer set
134
135// Create the logical Volumes
136//
137// G4LogicalVolume(*pSolid, *pMaterial, Name, *pField, *pSDetector, *pULimits)
138//
139 G4LogicalVolume *BigBoxLog=new G4LogicalVolume(myBigBox,0,
140 "Crystal Box (large)",0,0,0);
141 G4LogicalVolume *smallBoxLog=new G4LogicalVolume(mySmallBox,0,
142 "Crystal Box (small)");
143 G4LogicalVolume *tinyBoxLog=new G4LogicalVolume(myTinyBox,0,
144 "Crystal Box (tiny)");
145
146
147// Place them.
148//
149// 1) Two big boxes in the world volume
150//
151 // G4PVPlacement *BigTg1Phys=
152 new G4PVPlacement(0,G4ThreeVector(0,0,-15*m),
153 "Big Target 1",BigBoxLog,
154 worldPhys,false,0);
155 // G4PVPlacement *BigTg2Phys=
156 new G4PVPlacement(0,G4ThreeVector(0,0, 15*m),
157 "Big Target 2",BigBoxLog,
158 worldPhys,false,0);
159
160// 2) Four (medium) boxes in X & Y near the origin of the world volume
161//
162 // G4PVPlacement *MedTg3a_Phys=
163 new G4PVPlacement(0,G4ThreeVector(0, 7.5*m,0),
164 "Target 3a",smallBoxLog,
165 worldPhys,false,0);
166 // G4PVPlacement *MedTg3b_Phys=
167 new G4PVPlacement(0,G4ThreeVector(0,-7.5*m,0),
168 "Target 3b",smallBoxLog,
169 worldPhys,false,0);
170 // G4PVPlacement *MedTg3c_Phys=
171 new G4PVPlacement(0,G4ThreeVector(-7.5*m,0,0),
172 "Target 3c",smallBoxLog,
173 worldPhys,false,0);
174 // G4PVPlacement *MedTg3d_Phys=
175 new G4PVPlacement(0,G4ThreeVector( 7.5*m,0,0),
176 "Target 3d",smallBoxLog,
177 worldPhys,false,0);
178
179
180// 3) Eight small boxes around the origin of the world volume
181// (in +-X, +-Y & +-Z)
182//
183 // G4PVPlacement *SmTg4a_Phys=
184 new G4PVPlacement
185 (0,G4ThreeVector( 0.3*m, 0.3*m,0.3*m), "Target 4a",tinyBoxLog,
186 worldPhys,false,0);
187 // G4PVPlacement *SmTg4b_Phys=
188 new G4PVPlacement
189 (0,G4ThreeVector( 0.3*m,-0.3*m,0.3*m), "Target 4b",tinyBoxLog,
190 worldPhys,false,0);
191 // G4PVPlacement *SmTg4c_Phys=
192 new G4PVPlacement
193 (0,G4ThreeVector(-0.3*m,-0.3*m,0.3*m), "Target 4c",tinyBoxLog,
194 worldPhys,false,0);
195 // G4PVPlacement *SmTg4d_Phys=
196 new G4PVPlacement
197 (0,G4ThreeVector(-0.3*m, 0.3*m,0.3*m), "Target 4d",tinyBoxLog,
198 worldPhys,false,0);
199
200 // G4PVPlacement *SmTg4e_Phys=
201 new G4PVPlacement
202 (0,G4ThreeVector( 0.3*m, 0.3*m,-0.3*m), "Target 4e",tinyBoxLog,
203 worldPhys,false,0);
204 // G4PVPlacement *SmTg4f_Phys=
205 new G4PVPlacement
206 (0,G4ThreeVector( 0.3*m,-0.3*m,-0.3*m), "Target 4f",tinyBoxLog,
207 worldPhys,false,0);
208 // G4PVPlacement *SmTg4g_Phys=
209 new G4PVPlacement
210 (0,G4ThreeVector(-0.3*m,-0.3*m,-0.3*m), "Target 4g",tinyBoxLog,
211 worldPhys,false,0);
212 // G4PVPlacement *SmTg4h_Phys=
213 new G4PVPlacement
214 (0,G4ThreeVector(-0.3*m, 0.3*m,-0.3*m), "Target 4h",tinyBoxLog,
215 worldPhys,false,0);
216
217 return worldPhys;
218}
219
220#include "G4ChordFinder.hh"
221#include "G4PropagatorInField.hh"
222#include "G4MagneticField.hh"
223#include "G4FieldManager.hh"
224#include "G4TransportationManager.hh"
225#include "G4HelixExplicitEuler.hh"
226#include "G4HelixSimpleRunge.hh"
227#include "G4HelixImplicitEuler.hh"
228#include "G4ExplicitEuler.hh"
229#include "G4ImplicitEuler.hh"
230#include "G4SimpleRunge.hh"
231#include "G4SimpleHeum.hh"
232#include "G4ClassicalRK4.hh"
233#include "G4Mag_UsualEqRhs.hh"
234#include "G4CashKarpRKF45.hh"
235#include "G4RKG3_Stepper.hh"
236
237G4MagneticField* pMagField;
238
239G4FieldManager* SetupField(G4int type)
240{
241 G4FieldManager *pFieldMgr;
242 G4ChordFinder *pChordFinder;
243
244 // pMagField = new G4DELPHIMagField();
245 pMagField = new G4HarmonicPolMagField();
246
247 G4Mag_UsualEqRhs *fEquation = new G4Mag_UsualEqRhs(pMagField);
248 G4MagIntegratorStepper *pStepper;
249
250 switch ( type )
251 {
252 case 0: pStepper = new G4ExplicitEuler( fEquation ); break;
253 case 1: pStepper = new G4ImplicitEuler( fEquation ); break;
254 case 2: pStepper = new G4SimpleRunge( fEquation ); break;
255 case 3: pStepper = new G4SimpleHeum( fEquation ); break;
256 case 4: pStepper = new G4ClassicalRK4( fEquation ); break;
257 case 5: pStepper = new G4HelixExplicitEuler( fEquation ); break;
258 case 6: pStepper = new G4HelixImplicitEuler( fEquation ); break;
259 case 7: pStepper = new G4HelixSimpleRunge( fEquation ); break;
260 case 8: pStepper = new G4CashKarpRKF45( fEquation ); break;
261 case 9: pStepper = new G4RKG3_Stepper( fEquation ); break;
262 default: pStepper = 0;
263 }
264
265 pFieldMgr= G4TransportationManager::GetTransportationManager()->
266 GetFieldManager();
267
268 pFieldMgr->SetDetectorField( pMagField );
269
270 pChordFinder = new G4ChordFinder( pMagField,
271 1.0e-2 * mm,
272 pStepper);
273
274 pFieldMgr->SetChordFinder( pChordFinder );
275
276 return pFieldMgr;
277}
278
279G4PropagatorInField* SetupPropagator( G4int type)
280{
281 // G4FieldManager* fieldMgr=
282 SetupField( type) ;
283
284 // G4ChordFinder theChordFinder( &MagField, 0.05*mm ); // Default stepper
285
286 G4PropagatorInField *thePropagator =
287 G4TransportationManager::GetTransportationManager()->
288 GetPropagatorInField ();
289
290 return thePropagator;
291}
292
293// This is Done only for this test program ... the transportation does it.
294//
295void SetChargeMomentumMass(G4double charge, G4double MomentumXc, G4double Mass)
296{
297 G4ChordFinder* pChordFinder;
298
299 pChordFinder= G4TransportationManager::GetTransportationManager()->
300 GetFieldManager()->GetChordFinder();
301
302 // pMagFieldPropagator->set_magnetic_field();
303 pChordFinder->SetChargeMomentumMass(
304 charge, // charge in e+ units
305 MomentumXc, // Momentum in Mev/c ?
306 Mass );
307}
308
309//
310// Test Stepping
311//
312G4bool testG4PropagatorInField(G4VPhysicalVolume *pTopNode, G4int type)
313{
314 void report_endPV(G4ThreeVector Position,
315 G4ThreeVector UnitVelocity,
316 G4double step_len,
317 G4double physStep,
318 G4double safety,
319 G4ThreeVector EndPosition,
320 G4ThreeVector EndUnitVelocity,
321 G4int Step,
322 G4VPhysicalVolume* startVolume);
323
324 G4UniformMagField MagField(10.*tesla, 0., 0.); // Tesla Defined ?
325 G4Navigator *pNavig= G4TransportationManager::
326 GetTransportationManager()-> GetNavigatorForTracking();
327 G4PropagatorInField *pMagFieldPropagator= SetupPropagator(type);
328
329 SetChargeMomentumMass( +1., // charge in e+ units
330 0.5 * proton_mass_c2, // Momentum in Mev/c ?
331 proton_mass_c2 );
332 pNavig->SetWorldVolume(pTopNode);
333
334
335 G4VPhysicalVolume *located;
336 G4double step_len, physStep, safety;
337 G4ThreeVector xHat(1,0,0),yHat(0,1,0),zHat(0,0,1);
338 G4ThreeVector mxHat(-1,0,0),myHat(0,-1,0),mzHat(0,0,-1);
339
340 // physStep=kInfinity;
341 G4ThreeVector Position(0.,0.,0.);
342 G4ThreeVector UnitMomentum(0.,0.6,0.8);
343 G4ThreeVector EndPosition, EndUnitMomentum;
344
345//
346// Test location & Step computation
347//
348 /* assert(located->GetName()=="World"); */
349 if( std::fabs(UnitMomentum.mag() - 1.0) > 1.e-8 )
350 {
351 G4cerr << "UnitMomentum.mag() - 1.0 = " << UnitMomentum.mag() - 1.0 <<
352 G4endl;
353 }
354
355 G4cout << G4endl;
356
357 for( int iparticle=0; iparticle < 2; iparticle++ )
358 {
359 physStep= 2.5 * mm ; // millimeters
360 Position = G4ThreeVector(0.,0.,0.)
361 + iparticle * G4ThreeVector(0.2, 0.3, 0.4);
362 UnitMomentum = (G4ThreeVector(0.,0.6,0.8)
363 + (float)iparticle * G4ThreeVector(0.1, 0.2, 0.3)).unit();
364
365 G4double momentum = (0.5+iparticle*10.0) * proton_mass_c2;
366
367 G4double kineticEnergy = momentum*momentum /
368 ( std::sqrt( momentum*momentum + proton_mass_c2 * proton_mass_c2 )
369 + proton_mass_c2 );
370 G4double velocity = momentum / ( proton_mass_c2 + kineticEnergy );
371 G4double labTof= 10.0*ns, properTof= 0.1*ns;
372 G4ThreeVector Spin(1.0, 0.0, 0.0);
373 // Momentum in Mev/c ?
374 SetChargeMomentumMass(
375 +1, // charge in e+ units
376 momentum,
377 proton_mass_c2);
378 G4cout << G4endl;
379 G4cout << "Test PropagateMagField: ***********************" << G4endl
380 << " Starting New Particle with Position " << Position << G4endl
381 << " and UnitVelocity " << UnitMomentum << G4endl;
382 G4cout << " Momentum in GeV/c is "<< (0.5+iparticle*10.0)*proton_mass_c2;
383 G4cout << G4endl;
384
385
386 for( int istep=0; istep < 14; istep++ ){
387 // // G4cerr << "UnitMomentum Magnitude is " << UnitMomentum.mag() << G4endl;
388 located= pNavig->LocateGlobalPointAndSetup(Position);
389 // Is the following better ?? It would need "changes"
390 // located= pMagFieldPropagator->LocateGlobalPointAndSetup(Position);
391 // G4cerr << "Starting Step " << istep << " in volume "
392 // << located->GetName() << G4endl;
393
394 G4FieldTrack initTrack( Position,
395 UnitMomentum,
396 0.0, // starting S curve len
397 kineticEnergy,
398 proton_mass_c2,
399 velocity,
400 labTof,
401 properTof,
402 0 // or &Spin
403 );
404
405 step_len=pMagFieldPropagator->ComputeStep( initTrack,
406 physStep,
407 safety
408#ifdef G4MAG_CHECK_VOLUME
409 ,located);
410#else
411 );
412#endif
413 // --------------------
414 EndPosition= pMagFieldPropagator->EndPosition();
415 EndUnitMomentum= pMagFieldPropagator->EndMomentumDir();
416 // --------
417
418 if( std::fabs(EndUnitMomentum.mag2() - 1.0) > 1.e-8 )
419 G4cerr << "EndUnitMomentum.mag2() - 1.0 = " <<
420 EndUnitMomentum.mag2() - 1.0 << G4endl;
421
422 G4ThreeVector MoveVec = EndPosition - Position;
423 assert( MoveVec.mag() < physStep*(1.+1.e-9) );
424
425 // G4cout << " testPropagatorInField: After stepI " << istep << " : " << G4endl;
426 report_endPV(Position, UnitMomentum, step_len, physStep, safety,
427 EndPosition, EndUnitMomentum, istep, located );
428
429 assert(safety>=0);
430 pNavig->SetGeometricallyLimitedStep();
431 // pMagFieldPropagator->SetGeometricallyLimitedStep();
432
433 Position= EndPosition;
434 UnitMomentum= EndUnitMomentum;
435 physStep *= 2.;
436 } // ........................... end for ( istep )
437 } // .............................. end for ( iparticle )
438
439 return(1);
440}
441
442// int main(int argc, char** argv)
443int main(int argc, char **argv)
444{
445 G4VPhysicalVolume *myTopNode;
446 G4int type;
447 myTopNode=BuildGeometry(); // Build the geometry
448 G4GeometryManager::GetInstance()->CloseGeometry(false);
449
450 type = 8 ;
451
452 if( argc == 2 )
453 type = atoi(argv[1]);
454
455 testG4PropagatorInField(myTopNode, type);
456
457// Repeat tests but with full voxels
458 G4GeometryManager::GetInstance()->OpenGeometry();
459 G4GeometryManager::GetInstance()->CloseGeometry(true);
460
461 testG4PropagatorInField(myTopNode, type);
462
463 G4GeometryManager::GetInstance()->OpenGeometry();
464 return 0;
465}
466
467
468void report_endPV(G4ThreeVector Position,
469 G4ThreeVector UnitVelocity,
470 G4double step_len,
471 G4double physStep,
472 G4double safety,
473 G4ThreeVector EndPosition,
474 G4ThreeVector EndUnitVelocity,
475 G4int Step,
476 G4VPhysicalVolume* startVolume)
477 // G4VPhysicalVolume* endVolume)
478{
479 const G4int verboseLevel=1;
480
481 if( Step == 0 && verboseLevel <= 3 )
482 {
483 G4cout.precision(5);
484 // G4cout.setf(ios_base::fixed,ios_base::floatfield);
485 G4cout << std::setw( 5) << "Step#" << " "
486 << std::setw( 9) << "X(mm)" << " "
487 << std::setw( 9) << "Y(mm)" << " "
488 << std::setw( 9) << "Z(mm)" << " "
489 << std::setw( 7) << " N_x " << " "
490 << std::setw( 7) << " N_y " << " "
491 << std::setw( 7) << " N_z " << " "
492 // << std::setw( 9) << "KinE(MeV)" << " "
493 // << std::setw( 9) << "dE(MeV)" << " "
494 << std::setw( 9) << "StepLen" << " "
495 << std::setw( 9) << "PhsStep" << " "
496 << std::setw( 9) << "Safety" << " "
497 << std::setw(18) << "NextVolume" << " "
498 << G4endl;
499 }
500 //
501 //
502 if( verboseLevel > 3 )
503 {
504 G4cout << "End Position is " << EndPosition << G4endl
505 << " and UnitVelocity is " << EndUnitVelocity << G4endl;
506 G4cout << "Step taken was " << step_len
507 << " out of PhysicalStep= " << physStep << G4endl;
508 G4cout << "Final safety is: " << safety << G4endl;
509
510 G4cout << "Chord length = " << (EndPosition-Position).mag() << G4endl;
511 G4cout << G4endl;
512 }
513 else // if( verboseLevel > 0 )
514 {
515 G4cout.precision(3);
516 G4cout << std::setw( 5) << Step << " "
517 << std::setw( 9) << Position.x() << " "
518 << std::setw( 9) << Position.y() << " "
519 << std::setw( 9) << Position.z() << " "
520 << std::setw( 7) << EndUnitVelocity.x() << " "
521 << std::setw( 7) << EndUnitVelocity.y() << " "
522 << std::setw( 7) << EndUnitVelocity.z() << " "
523 // << std::setw( 9) << KineticEnergy << " "
524 // << std::setw( 9) << EnergyDifference << " "
525 << std::setw( 9) << step_len << " "
526 << std::setw( 9) << physStep << " "
527 << std::setw( 9) << safety << " ";
528 if( startVolume != 0) {
529 G4cout << std::setw(12) << startVolume->GetName() << " ";
530 } else {
531 G4cout << std::setw(12) << "OutOfWorld" << " ";
532 }
533
534#if 0
535 if( endVolume != 0)
536 {
537 G4cout << std::setw(12) << endVolume()->GetName() << " ";
538 }
539 else
540 {
541 G4cout << std::setw(12) << "OutOfWorld" << " ";
542 }
543#endif
544 G4cout << G4endl;
545 }
546}
547
548int readin_particle( )
549{
550 static const
551 double pmass[5] = {
552 0.00051099906 , // electron
553 0.105658389 , // muon
554 0.13956995 , // pion
555 0.493677 , // kaon
556 0.93827231 // proton
557 } ;
558 const double cSpeed = 299792458.0 ; // light speed in m/s
559 const double pi = 3.141592653589793238 ;
560 int pCharge, i ;
561 double pMomentum, pTeta, pPhi, h ;
562 G4cout<<"Enter particle type: 0 - electron, 1 - muon, 2 - pion, \n"
563 <<"3 - kaon, 4 - proton "<< G4endl ;
564 G4cin>>i ;
565 double pMass = pmass[i] ;
566 G4cout<<"Enter particle charge in units of the positron charge "<< G4endl ;
567 G4cin>>pCharge ;
568 G4cout<<"Enter particle momentum in GeV/c"<<G4endl ;
569 G4cin>>pMomentum ;
570 G4cout<<"Enter particle teta & phi in degrees"<<G4endl ;
571 G4cin>>pTeta ;
572 G4cin>>pPhi ;
573 G4cout<<"Enter particle Step in centimeters"<<G4endl ;
574 G4cin>>h ;
575
576 h *= 10.; // G4 units are in millimeters.
577
578 double betaGamma = pMomentum/pMass ;
579 double pSpeed = betaGamma*cSpeed/std::sqrt(1 + betaGamma*betaGamma) ;
580 double pEnergy = pMomentum*cSpeed/pSpeed ;
581 pEnergy *= 1.60217733e-10 ; // energy in J (SI units)
582 pTeta *= pi/180 ;
583 pPhi *= pi/180 ;
584
585#if 0
586 for(i=0;i<3;i++) ystart[i] = 0 ; // initial coordinates
587 ystart[3] = pSpeed*std::sin(pTeta)*std::cos(pPhi) ; // and speeds
588 ystart[4] = pSpeed*std::sin(pTeta)*std::sin(pPhi) ;
589 ystart[5] = pSpeed*std::cos(pTeta) ;
590#endif
591
592 return 1;
593}
594
595
Note: See TracBrowser for help on using the repository browser.