source: trunk/source/geometry/magneticfield/src/G4NystromRK4.cc@ 1347

Last change on this file since 1347 was 1340, checked in by garnier, 15 years ago

update ti head

File size: 7.9 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: G4NystromRK4.cc,v 1.9 2010/09/10 15:42:09 japost Exp $
28// GEANT4 tag $Name: field-V09-03-03 $
29//
30// History:
31// - Created: I.Gavrilenko 15.05.2009 (as G4AtlasRK4)
32// - Adaptations: J. Apostolakis May-Nov 2009
33// -------------------------------------------------------------------
34
35#include "G4NystromRK4.hh"
36#include <iostream>
37
38//////////////////////////////////////////////////////////////////
39// Constructor - with optional distance ( has default value)
40//////////////////////////////////////////////////////////////////
41
42G4NystromRK4::G4NystromRK4(G4Mag_EqRhs* magEqRhs, G4double distanceConstField)
43 : G4MagIntegratorStepper(magEqRhs, 6), // number of variables
44 m_fEq( magEqRhs ),
45 m_magdistance( distanceConstField ),
46 m_cof( 0.0 ),
47 m_mom( 0.0 ),
48 m_imom( 0.0 ),
49 m_cachedMom( false )
50{
51 m_fldPosition[0] = m_iPoint[0] = m_fPoint[0] = m_mPoint[0] = 9.9999999e+99 ;
52 m_fldPosition[1] = m_iPoint[1] = m_fPoint[1] = m_mPoint[1] = 9.9999999e+99 ;
53 m_fldPosition[2] = m_iPoint[2] = m_fPoint[2] = m_mPoint[2] = 9.9999999e+99 ;
54 m_fldPosition[3] = -9.9999999e+99;
55 m_lastField[0] = m_lastField[1] = m_lastField[2] = 0.0;
56
57 m_magdistance2 = distanceConstField*distanceConstField;
58}
59
60////////////////////////////////////////////////////////////////
61// Destructor
62////////////////////////////////////////////////////////////////
63
64G4NystromRK4::~G4NystromRK4()
65{
66}
67
68/////////////////////////////////////////////////////////////////////////////////
69// Integration in one step
70/////////////////////////////////////////////////////////////////////////////////
71
72void
73G4NystromRK4::Stepper
74(const G4double P[],const G4double dPdS[],G4double Step,G4double Po[],G4double Err[])
75{
76 G4double R[3] = { P[0], P[1] , P[2]};
77 G4double A[3] = {dPdS[0], dPdS[1], dPdS[2]};
78
79 m_iPoint[0]=R[0]; m_iPoint[1]=R[1]; m_iPoint[2]=R[2];
80
81 const G4double one_sixth= 1./6.;
82 G4double S = Step ;
83 G4double S5 = .5*Step ;
84 G4double S4 = .25*Step ;
85 G4double S6 = Step * one_sixth; // Step / 6.;
86
87
88 // John A added, in order to emulate effect of call to changed/derived RHS
89 // m_mom = sqrt(P[3]*P[3]+P[4]*P[4]+P[5]*P[5]);
90 // m_imom = 1./m_mom;
91 // m_cof = m_fEq->FCof()*m_imom;
92
93 // Point 1
94 //
95 G4double K1[3] = { m_imom*dPdS[3], m_imom*dPdS[4], m_imom*dPdS[5] };
96
97 // Point2
98 //
99 G4double p[4] = {R[0]+S5*(A[0]+S4*K1[0]),
100 R[1]+S5*(A[1]+S4*K1[1]),
101 R[2]+S5*(A[2]+S4*K1[2]),
102 P[7] };
103 getField(p);
104
105 G4double A2[3] = {A[0]+S5*K1[0],A[1]+S5*K1[1],A[2]+S5*K1[2]};
106 G4double K2[3] = {(A2[1]*m_lastField[2]-A2[2]*m_lastField[1])*m_cof,
107 (A2[2]*m_lastField[0]-A2[0]*m_lastField[2])*m_cof,
108 (A2[0]*m_lastField[1]-A2[1]*m_lastField[0])*m_cof};
109
110 m_mPoint[0]=p[0]; m_mPoint[1]=p[1]; m_mPoint[2]=p[2];
111
112 // Point 3 with the same magnetic field
113 //
114 G4double A3[3] = {A[0]+S5*K2[0],A[1]+S5*K2[1],A[2]+S5*K2[2]};
115 G4double K3[3] = {(A3[1]*m_lastField[2]-A3[2]*m_lastField[1])*m_cof,
116 (A3[2]*m_lastField[0]-A3[0]*m_lastField[2])*m_cof,
117 (A3[0]*m_lastField[1]-A3[1]*m_lastField[0])*m_cof};
118
119 // Point 4
120 //
121 p[0] = R[0]+S*(A[0]+S5*K3[0]);
122 p[1] = R[1]+S*(A[1]+S5*K3[1]);
123 p[2] = R[2]+S*(A[2]+S5*K3[2]);
124
125 getField(p);
126
127 G4double A4[3] = {A[0]+S*K3[0],A[1]+S*K3[1],A[2]+S*K3[2]};
128 G4double K4[3] = {(A4[1]*m_lastField[2]-A4[2]*m_lastField[1])*m_cof,
129 (A4[2]*m_lastField[0]-A4[0]*m_lastField[2])*m_cof,
130 (A4[0]*m_lastField[1]-A4[1]*m_lastField[0])*m_cof};
131
132 // New position
133 //
134 Po[0] = P[0]+S*(A[0]+S6*(K1[0]+K2[0]+K3[0]));
135 Po[1] = P[1]+S*(A[1]+S6*(K1[1]+K2[1]+K3[1]));
136 Po[2] = P[2]+S*(A[2]+S6*(K1[2]+K2[2]+K3[2]));
137
138 m_fPoint[0]=Po[0]; m_fPoint[1]=Po[1]; m_fPoint[2]=Po[2];
139
140 // New direction
141 //
142 Po[3] = A[0]+S6*(K1[0]+K4[0]+2.*(K2[0]+K3[0]));
143 Po[4] = A[1]+S6*(K1[1]+K4[1]+2.*(K2[1]+K3[1]));
144 Po[5] = A[2]+S6*(K1[2]+K4[2]+2.*(K2[2]+K3[2]));
145
146 // Errors
147 //
148 Err[3] = S*std::fabs(K1[0]-K2[0]-K3[0]+K4[0]);
149 Err[4] = S*std::fabs(K1[1]-K2[1]-K3[1]+K4[1]);
150 Err[5] = S*std::fabs(K1[2]-K2[2]-K3[2]+K4[2]);
151 Err[0] = S*Err[3] ;
152 Err[1] = S*Err[4] ;
153 Err[2] = S*Err[5] ;
154 Err[3]*= m_mom ;
155 Err[4]*= m_mom ;
156 Err[5]*= m_mom ;
157
158 // Normalize momentum
159 //
160 G4double normF = m_mom/std::sqrt(Po[3]*Po[3]+Po[4]*Po[4]+Po[5]*Po[5]);
161 Po [3]*=normF; Po[4]*=normF; Po[5]*=normF;
162
163 // Pass Energy, time unchanged -- time is not integrated !!
164 Po[6]=P[6]; Po[7]=P[7];
165}
166
167
168/////////////////////////////////////////////////////////////////////////////////
169// Estimate the maximum distance from the curve to the chord
170/////////////////////////////////////////////////////////////////////////////////
171
172G4double
173G4NystromRK4::DistChord() const
174{
175 G4double ax = m_fPoint[0]-m_iPoint[0];
176 G4double ay = m_fPoint[1]-m_iPoint[1];
177 G4double az = m_fPoint[2]-m_iPoint[2];
178 G4double dx = m_mPoint[0]-m_iPoint[0];
179 G4double dy = m_mPoint[1]-m_iPoint[1];
180 G4double dz = m_mPoint[2]-m_iPoint[2];
181 G4double d2 = (ax*ax+ay*ay+az*az) ;
182
183 if(d2!=0.) {
184 G4double s = (ax*dx+ay*dy+az*dz)/d2;
185 dx -= (s*ax) ;
186 dy -= (s*ay) ;
187 dz -= (s*az) ;
188 }
189 return std::sqrt(dx*dx+dy*dy+dz*dz);
190}
191
192/////////////////////////////////////////////////////////////////////////////////
193// Derivatives calculation - caching the momentum value
194/////////////////////////////////////////////////////////////////////////////////
195
196void
197G4NystromRK4::ComputeRightHandSide(const G4double P[],G4double dPdS[])
198{
199 G4double P4vec[4]= { P[0], P[1], P[2], P[7] }; // Time is P[7]
200 getField(P4vec);
201 m_mom = std::sqrt(P[3]*P[3]+P[4]*P[4]+P[5]*P[5]) ;
202 m_imom = 1./m_mom ;
203 m_cof = m_fEq->FCof()*m_imom ;
204 m_cachedMom = true ; // Caching the value
205 dPdS[0] = P[3]*m_imom ; // dx /ds
206 dPdS[1] = P[4]*m_imom ; // dy /ds
207 dPdS[2] = P[5]*m_imom ; // dz /ds
208 dPdS[3] = m_cof*(P[4]*m_lastField[2]-P[5]*m_lastField[1]) ; // dPx/ds
209 dPdS[4] = m_cof*(P[5]*m_lastField[0]-P[3]*m_lastField[2]) ; // dPy/ds
210 dPdS[5] = m_cof*(P[3]*m_lastField[1]-P[4]*m_lastField[0]) ; // dPz/ds
211}
Note: See TracBrowser for help on using the repository browser.