Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-24 09:09:02

0001 //
0002 // ********************************************************************
0003 // * License and Disclaimer                                           *
0004 // *                                                                  *
0005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
0006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
0007 // * conditions of the Geant4 Software License,  included in the file *
0008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
0009 // * include a list of copyright holders.                             *
0010 // *                                                                  *
0011 // * Neither the authors of this software system, nor their employing *
0012 // * institutes,nor the agencies providing financial support for this *
0013 // * work  make  any representation or  warranty, express or implied, *
0014 // * regarding  this  software system or assume any liability for its *
0015 // * use.  Please see the license in the file  LICENSE  and URL above *
0016 // * for the full disclaimer and the limitation of liability.         *
0017 // *                                                                  *
0018 // * This  code  implementation is the result of  the  scientific and *
0019 // * technical work of the GEANT4 collaboration.                      *
0020 // * By using,  copying,  modifying or  distributing the software (or *
0021 // * any work based  on the software)  you  agree  to acknowledge its *
0022 // * use  in  resulting  scientific  publications,  and indicate your *
0023 // * acceptance of all terms of the Geant4 Software license.          *
0024 // ********************************************************************
0025 //
0026 // G4ConstRK4
0027 //
0028 // Class description:
0029 //
0030 // G4ConstRK4 performs the integration of one step with error calculation
0031 // in constant magnetic field. The integration method is the same as in
0032 // ClassicalRK4. The field value is assumed constant for the step.
0033 // This field evaluation is called only once per step.
0034 // G4ConstRK4 can be used only for magnetic fields.
0035 
0036 // Authors: J.Apostolakis, T.Nikitina (CERN), 18.09.2008
0037 // -------------------------------------------------------------------
0038 #ifndef G4CONSTRK4_HH
0039 #define G4CONSTRK4_HH
0040 
0041 #include "G4MagErrorStepper.hh"
0042 #include "G4EquationOfMotion.hh"
0043 #include "G4Mag_EqRhs.hh"
0044 
0045 /**
0046  * @brief G4ConstRK4 performs the integration of one step with error
0047  * calculation in constant magnetic field. The integration method is the
0048  * same as in ClassicalRK4. The field value is assumed constant for the step.
0049  * This field evaluation is called only once per step.
0050  * G4ConstRK4 can be used only for magnetic fields.
0051  */
0052 
0053 class G4ConstRK4 : public G4MagErrorStepper 
0054 {
0055    public:
0056 
0057     /**
0058      * Constructor for G4ConstRK4.
0059      *  @param[in] EqRhs Pointer to the provided equation of motion.
0060      *  @param[in] numberOfVariables The number of integration variables.
0061      */
0062      G4ConstRK4(G4Mag_EqRhs* EquationMotion,
0063                 G4int numberOfStateVariables=8);
0064 
0065     /**
0066      * Destructor.
0067      */
0068      ~G4ConstRK4() override;
0069 
0070     /**
0071      * Copy constructor and assignment operator not allowed.
0072      */
0073      G4ConstRK4(const G4ConstRK4&) = delete;
0074      G4ConstRK4& operator=(const G4ConstRK4&) = delete;
0075 
0076     /**
0077      * The stepper for the Runge Kutta integration.
0078      * The stepsize is fixed, with the step size given by 'h'.
0079      * Integrates ODE starting values y[0 to 6].
0080      * Outputs yout[] and its estimated error yerr[].
0081      *  @param[in] y Starting values array of integration variables.
0082      *  @param[in] dydx Derivatives array.
0083      *  @param[in] h The given step size.
0084      *  @param[out] yout Integration output.
0085      *  @param[out] yerr The estimated error.
0086      */
0087      void Stepper( const G4double y[],
0088                    const G4double dydx[],
0089                          G4double h,
0090                          G4double yout[],
0091                          G4double yerr[]  ) override;
0092 
0093     /**
0094      * Given values for the variables y[0,..,n-1] and their derivatives
0095      * dydx[0,...,n-1] known at x, uses the classical 4th Runge-Kutta
0096      * method to advance the solution over an interval h and returns the
0097      * incremented variables as yout[0,...,n-1]. The user supplies the
0098      * function RightHandSide(x,y,dydx), which returns derivatives dydx at x.
0099      * The source is routine rk4 from NRC p.712-713.
0100      *  @param[in] y Starting values array of integration variables.
0101      *  @param[in] dydx Derivatives array.
0102      *  @param[in] h The given step size.
0103      *  @param[out] yout Integration output.
0104      */
0105      void DumbStepper( const G4double yIn[],
0106                        const G4double dydx[],
0107                              G4double h,
0108                              G4double yOut[] ) override ;
0109 
0110     /**
0111      * Returns the distance from chord line.
0112      */
0113      G4double DistChord() const override;   
0114  
0115     /**
0116      * Returns the derivatives value, at position and time 'y'.
0117      *  @param[in] y The position vector plus time (x,y,z,t).
0118      *  @param[out] dydx The derivatives array.
0119      */
0120      inline void RightHandSideConst(const G4double y[], G4double dydx[] ) const;
0121 
0122     /**
0123      * Returns the field values, at position and time 'y'.
0124      *  @param[in] y The position vector plus time (x,y,z,t).
0125      *  @param[out] Field The field value in output.
0126      */
0127      inline void GetConstField(const G4double y[], G4double Field[]);
0128 
0129     /**
0130      * Returns the order, 4, of integration.
0131      */
0132      inline G4int IntegratorOrder() const override { return 4; }
0133 
0134     /**
0135      * Returns the stepper type-ID, "kConstRK4".
0136      */
0137      inline G4StepperType StepperType() const override { return kConstRK4; }
0138 
0139    private:
0140 
0141      G4ThreeVector fInitialPoint, fMidPoint, fFinalPoint;
0142      // Data stored in order to find the chord
0143      G4double *dydxm, *dydxt, *yt; // scratch space - not state 
0144      G4double *yInitial, *yMiddle, *dydxMid, *yOneStep;
0145      G4Mag_EqRhs* fEq = nullptr;
0146      G4double Field[3];
0147 };
0148 
0149 // Inline methods
0150 
0151 inline void G4ConstRK4::RightHandSideConst(const G4double y[],
0152                                                  G4double dydx[] ) const
0153 {
0154   
0155   G4double momentum_mag_square = y[3]*y[3] + y[4]*y[4] + y[5]*y[5];
0156   G4double inv_momentum_magnitude = 1.0 / std::sqrt( momentum_mag_square );
0157     
0158   G4double cof = fEq->FCof()*inv_momentum_magnitude;
0159 
0160   dydx[0] = y[3]*inv_momentum_magnitude;       //  (d/ds)x = Vx/V
0161   dydx[1] = y[4]*inv_momentum_magnitude;       //  (d/ds)y = Vy/V
0162   dydx[2] = y[5]*inv_momentum_magnitude;       //  (d/ds)z = Vz/V
0163  
0164   dydx[3] = cof*(y[4]*Field[2] - y[5]*Field[1]) ;   // Ax = a*(Vy*Bz - Vz*By)
0165   dydx[4] = cof*(y[5]*Field[0] - y[3]*Field[2]) ;   // Ay = a*(Vz*Bx - Vx*Bz)
0166   dydx[5] = cof*(y[3]*Field[1] - y[4]*Field[0]) ;   // Az = a*(Vx*By - Vy*Bx)
0167 }
0168 
0169 inline void G4ConstRK4::GetConstField(const G4double y[], G4double B[])
0170 {
0171   G4double PositionAndTime[4];
0172 
0173   PositionAndTime[0] = y[0];
0174   PositionAndTime[1] = y[1];
0175   PositionAndTime[2] = y[2];
0176   // Global Time
0177   PositionAndTime[3] = y[7];
0178   fEq -> GetFieldValue(PositionAndTime, B);
0179 }
0180 
0181 #endif