BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
Loading...
Searching...
No Matches
BDSFieldMagUndulator.cc
1/*
2Beam Delivery Simulation (BDSIM) Copyright (C) BDSIM Collaboration, 2001 - 2026.
3
4This file is part of BDSIM.
5
6BDSIM is free software: you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published
8by the Free Software Foundation version 3 of the License.
9
10BDSIM is distributed in the hope that it will be useful, but
11WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with BDSIM. If not, see <http://www.gnu.org/licenses/>.
17*/
18#include "BDSDebug.hh"
19#include "BDSFieldMagUndulator.hh"
20#include "BDSMagnetStrength.hh"
21#include "BDSUtilities.hh"
22
23#include "globals.hh" // geant4 types / globals
24#include "G4ThreeVector.hh"
25
26#include "CLHEP/Units/PhysicalConstants.h"
27#include "CLHEP/Units/SystemOfUnits.h"
28
29#include <cmath>
30
32 G4double beamPipeRadiusIn):
33 beamPipeRadius(beamPipeRadiusIn)
34{
35 wavenumber = (CLHEP::twopi)/(*strength)["length"];
36 B = (*strength)["field"];// / CLHEP::tesla;
37 finiteStrength = BDS::IsFinite(B);
38 limit = 3 * B; // limit for validity of this
39#ifdef BDSDEBUG
40 G4cout << __METHOD_NAME__ << "B = " << B << G4endl;
41 G4cout << __METHOD_NAME__ << "B_limit = " << limit << G4endl;
42#endif
43}
44
45G4ThreeVector BDSFieldMagUndulator::GetField(const G4ThreeVector& position,
46 const G4double /*t*/) const
47{
48 G4ThreeVector field;
49
50 if (std::abs(position.x()) > beamPipeRadius || std::abs(position.y()) > beamPipeRadius)
51 {return G4ThreeVector(0,0,0);}
52
53 G4double yFactor = std::cosh(position.y() * wavenumber);
54 if (yFactor > 5)
55 {yFactor = 5;}
56 G4double zFactor = std::sinh(position.y() * wavenumber);
57 if (zFactor > 5)
58 {zFactor = 5;}
59
60 field[0] = 0;
61 field[1] = B * std::cos(position.z() * wavenumber) * yFactor;
62 field[2] = -B * std::sin(position.z() * wavenumber) * zFactor;
63
64 // hyperbolic functions can tend to infinity if we happen to query a
65 // very large point limits -> a) the max magnitude and b) no nans
66 if (field[1] > limit || std::isnan(field[1]))
67 {field[1] = limit;}
68 if (field[2] > limit || std::isnan(field[2]))
69 {field[2] = limit;}
70
71 return field;
72}
G4double wavenumber
The undulator wavenumber.
BDSFieldMagUndulator()
Private default constructor to force use of supplied constructor.
G4double beamPipeRadius
Cache of beam pipe radius to know maximum valid extent of field.
virtual G4ThreeVector GetField(const G4ThreeVector &position, const G4double t=0) const
Access the field value.
G4double B
The peak field.
Efficient storage of magnet strengths.
G4bool IsFinite(G4double value, G4double tolerance=std::numeric_limits< double >::epsilon())