|
9 | 9 | #include "gfieldConventions.h" |
10 | 10 |
|
11 | 11 | // gemv |
12 | | -#include "gutilities.h" |
| 12 | +// #include "gutilities.h" |
13 | 13 |
|
14 | | -// c++ |
15 | | -#include <iostream> |
16 | | - |
17 | | -using namespace std; |
18 | 14 |
|
19 | 15 | // tells the DLL how to create a GFieldFactory |
20 | | -extern "C" GField *GFieldFactory(void) { |
21 | | - return static_cast<GField *>(new GField_MultipolesFactory); |
22 | | -} |
| 16 | +extern "C" GField* GFieldFactory(void) { return static_cast<GField*>(new GField_MultipolesFactory); } |
23 | 17 |
|
24 | 18 |
|
25 | 19 | // for now this implementation follows gemc |
26 | | -// reference of this implementation: https://uspas.fnal.gov/materials/12MSU/magnet_elements.pdf |
27 | | -void GField_MultipolesFactory::GetFieldValue(const double pos[3], G4double *bfield) const { |
28 | | - |
29 | | - G4ThreeVector x0(pos[0], pos[1], pos[2]); |
30 | | - G4ThreeVector x1(origin[0], origin[1], origin[2]); |
31 | | - G4ThreeVector x2; |
32 | | - G4ThreeVector x0_local; |
33 | | - if (rotaxis == 0) { |
34 | | - x2 = G4ThreeVector(0 * CLHEP::cm, 0 * CLHEP::cm, 1 * CLHEP::cm).rotateX(rotation_angle) + x1; |
35 | | - x0_local = (x0 - x1).rotateX(-rotation_angle); |
36 | | - } else if (rotaxis == 1) { |
37 | | - x2 = G4ThreeVector(0 * CLHEP::cm, 0 * CLHEP::cm, 1 * CLHEP::cm).rotateY(rotation_angle) + x1; |
38 | | - x0_local = (x0 - x1).rotateY(-rotation_angle); |
39 | | - } else if (rotaxis == 2) { |
40 | | - x2 = G4ThreeVector(0 * CLHEP::cm, 0 * CLHEP::cm, 1 * CLHEP::cm).rotateZ(rotation_angle) + x1; |
41 | | - x0_local = (x0 - x1).rotateZ(-rotation_angle); |
| 20 | +// references of this implementation: |
| 21 | +// - https://cds.cern.ch/record/1333874/files/1.pdf |
| 22 | +// - https://uspas.fnal.gov/materials/12MSU/magnet_elements.pdf |
| 23 | +// - https://cas.web.cern.ch/sites/default/files/lectures/bruges-2009/wolski-1.pdf |
| 24 | +// notice strength is defined at a reference radius of 1m |
| 25 | +void GField_MultipolesFactory::GetFieldValue(const double pos[3], G4double* bfield) const |
| 26 | +{ |
| 27 | + // ======= Configuration / conventions ======= |
| 28 | + // strength: Tesla at reference radius r0 for all multipole orders |
| 29 | + const double r0 = CLHEP::m; // reference radius; make this a member if you want |
| 30 | + |
| 31 | + // ======= Basic checks ======= |
| 32 | + if (pole_number < 2 || (pole_number % 2) != 0) { |
| 33 | + log->error(ERR_WRONG_POLE_NUMBER, |
| 34 | + "Pole number must be an even integer >= 2 (2=dipole,4=quadrupole,...)"); |
| 35 | + bfield[0] = bfield[1] = bfield[2] = 0.0; |
| 36 | + return; |
| 37 | + } |
| 38 | + |
| 39 | + // ======= Positions and local frame ======= |
| 40 | + const G4ThreeVector x0(pos[0], pos[1], pos[2]); // query point (lab) |
| 41 | + const G4ThreeVector x1(origin[0], origin[1], origin[2]); // magnet origin (lab) |
| 42 | + |
| 43 | + // shift to magnet-centered coordinates and "unroll" the magnet by -rotation_angle |
| 44 | + G4ThreeVector p = x0 - x1; |
| 45 | + if (rotaxis == 0) p.rotateX(-rotation_angle); |
| 46 | + else if (rotaxis == 1) p.rotateY(-rotation_angle); |
| 47 | + else if (rotaxis == 2) p.rotateZ(-rotation_angle); |
| 48 | + |
| 49 | + // Identify transverse plane (u,v) ⟂ to the axis, and the axial coordinate w (unused here) |
| 50 | + double u=0.0, v=0.0; |
| 51 | + switch (rotaxis) { |
| 52 | + case 0: u = p.y(); v = p.z(); break; // axis = X ⇒ transverse plane = (Y,Z) |
| 53 | + case 1: u = p.z(); v = p.x(); break; // axis = Y ⇒ transverse plane = (Z,X) |
| 54 | + case 2: u = p.x(); v = p.y(); break; // axis = Z ⇒ transverse plane = (X,Y) |
| 55 | + } |
| 56 | + |
| 57 | + const double r = std::hypot(u, v); // transverse radius |
| 58 | + const double phi = std::atan2(v, u); // azimuth in transverse plane |
| 59 | + |
| 60 | + // ======= Axial (solenoid-like) mode if explicitly requested ======= |
| 61 | + if (longitudinal) { |
| 62 | + // Uniform axial field aligned with rotaxis; not a multipole. |
| 63 | + G4ThreeVector B_local(0,0,0); |
| 64 | + switch (rotaxis) { |
| 65 | + case 0: B_local.setX(strength); break; |
| 66 | + case 1: B_local.setY(strength); break; |
| 67 | + case 2: B_local.setZ(strength); break; |
| 68 | + } |
| 69 | + // Roll back to lab |
| 70 | + G4ThreeVector B_lab = B_local; |
| 71 | + if (rotaxis == 0) B_lab.rotateX(+rotation_angle); |
| 72 | + else if (rotaxis == 1) B_lab.rotateY(+rotation_angle); |
| 73 | + else if (rotaxis == 2) B_lab.rotateZ(+rotation_angle); |
| 74 | + |
| 75 | + bfield[0] = B_lab.x(); |
| 76 | + bfield[1] = B_lab.y(); |
| 77 | + bfield[2] = B_lab.z(); |
| 78 | + |
| 79 | + log->info(2, "Axial field mode (solenoid-like). Strength: ", strength, |
| 80 | + " T, Field: (", bfield[0], ", ", bfield[1], ", ", bfield[2], ")"); |
| 81 | + return; |
| 82 | + } |
| 83 | + |
| 84 | + // ======= Transverse multipole (standard accelerator definition) ======= |
| 85 | + const int n = pole_number / 2; // 1=dipole, 2=quadrupole, 3=sextupole, ... |
| 86 | + const int a = n - 1; // power of r |
| 87 | + |
| 88 | + // r^a scaling with reference radius r0; for dipole (a=0) this is 1 |
| 89 | + double ra = 1.0; |
| 90 | + if (a > 0) { |
| 91 | + // If r=0 and a>0, |B|=0 (for ideal multipoles). |
| 92 | + if (r == 0.0) { |
| 93 | + bfield[0] = bfield[1] = bfield[2] = 0.0; |
| 94 | + return; |
| 95 | + } |
| 96 | + ra = std::pow(r / r0, static_cast<double>(a)); |
42 | 97 | } |
43 | 98 |
|
44 | | - G4double r = (x2 - x1).cross(x1 - x0).mag() / (x2 - x1).mag(); //distance from x0 to line x1-x2 |
45 | | - G4double phi = atan2(x0_local.y(), x0_local.x()); |
46 | | - |
47 | | - G4ThreeVector B_local; |
48 | | - if (pole_number == 2) { |
49 | | - B_local.setX(0); |
50 | | - B_local.setY(strength); |
51 | | - B_local.setZ(0); |
52 | | - } else if (pole_number > 0) { |
53 | | - int a = pole_number / 2 - 1; |
54 | | - B_local.setX(strength * pow(r / CLHEP::m, a) * sin(a * phi)); |
55 | | - B_local.setY(strength * pow(r / CLHEP::m, a) * cos(a * phi)); |
56 | | - B_local.setZ(0); |
57 | | - } else { |
58 | | - log->error(ERR_WRONG_POLE_NUMBER, "GField_MultipolesFactory::GetFieldValue: Pole number " + to_string(pole_number) + " not supported. Exiting."); |
| 99 | + // "Normal" multipole angular dependence: |
| 100 | + // Bu = strength * r^(n-1) * cos((n-1)*phi) |
| 101 | + // Bv = strength * r^(n-1) * sin((n-1)*phi) |
| 102 | + // This yields a constant transverse dipole for n=1. |
| 103 | + const double Bu = strength * ra * std::cos(a * phi); |
| 104 | + const double Bv = strength * ra * std::sin(a * phi); |
| 105 | + |
| 106 | + // Place (Bu,Bv) into the correct transverse components of B_local; axial component = 0 |
| 107 | + G4ThreeVector B_local(0,0,0); |
| 108 | + switch (rotaxis) { |
| 109 | + case 0: B_local.setY(Bu); B_local.setZ(Bv); break; // axis X → (Y,Z) |
| 110 | + case 1: B_local.setZ(Bu); B_local.setX(Bv); break; // axis Y → (Z,X) |
| 111 | + case 2: B_local.setX(Bu); B_local.setY(Bv); break; // axis Z → (X,Y) |
59 | 112 | } |
60 | 113 |
|
| 114 | + // Rotate (roll) back to lab |
61 | 115 | G4ThreeVector B_lab = B_local; |
62 | | - if (rotaxis == 0) { B_lab.rotateX(rotation_angle); } |
63 | | - else if (rotaxis == 1) { B_lab.rotateY(rotation_angle); } |
64 | | - else if (rotaxis == 2) { B_lab.rotateZ(rotation_angle); } |
| 116 | + if (rotaxis == 0) B_lab.rotateX(+rotation_angle); |
| 117 | + else if (rotaxis == 1) B_lab.rotateY(+rotation_angle); |
| 118 | + else if (rotaxis == 2) B_lab.rotateZ(+rotation_angle); |
65 | 119 |
|
66 | | - bfield[0] = B_lab.x(); |
67 | | - bfield[1] = B_lab.y(); |
68 | | - bfield[2] = B_lab.z(); |
| 120 | + // Output |
| 121 | + bfield[0] = B_lab.x(); |
| 122 | + bfield[1] = B_lab.y(); |
| 123 | + bfield[2] = B_lab.z(); |
69 | 124 |
|
70 | 125 | log->info(2, "Pole Number: ", pole_number, |
71 | | - ", Strength: ", strength, |
72 | | - ", Requested at: (", pos[0], ", ", pos[1], ", ", pos[2], ")", |
73 | | - ", Rotation angle: ", rotation_angle, |
74 | | - ", Rotation axis: ", rotaxis, |
75 | | - ", Field: (", bfield[0], ", ", bfield[1], ", ", bfield[2], ")"); |
| 126 | + ", n: ", n, |
| 127 | + ", Strength: ", strength, |
| 128 | + ", Requested at: (", pos[0], ", ", pos[1], ", ", pos[2], ")", |
| 129 | + ", Rotation angle: ", rotation_angle, |
| 130 | + ", Rotation axis: ", rotaxis, |
| 131 | + ", longitudinal: ", longitudinal, |
| 132 | + ", Field: (", bfield[0], ", ", bfield[1], ", ", bfield[2], ")"); |
76 | 133 | } |
77 | 134 |
|
| 135 | + |
78 | 136 | void GField_MultipolesFactory::load_field_definitions(GFieldDefinition gfd) { |
79 | | - gfield_definitions = gfd; |
80 | | - |
81 | | - pole_number = get_field_parameter_int("pole_number"); |
82 | | - origin[0] = get_field_parameter_double("vx"); |
83 | | - origin[1] = get_field_parameter_double("vy"); |
84 | | - origin[2] = get_field_parameter_double("vz"); |
85 | | - rotation_angle = get_field_parameter_double("rotation_angle"); |
86 | | - if( gfield_definitions.field_parameters["rotaxis"] == "X") { |
87 | | - rotaxis = 0; |
88 | | - } else if( gfield_definitions.field_parameters["rotaxis"] == "Y") { |
89 | | - rotaxis = 1; |
90 | | - } else if( gfield_definitions.field_parameters["rotaxis"] == "Z") { |
91 | | - rotaxis = 2; |
92 | | - } else { |
93 | | - log->error(ERR_WRONG_FIELD_ROTATION, "GField_MultipolesFactory::load_field_definitions: Rotation axis " + gfield_definitions.field_parameters["rotaxis"] + " not supported. Exiting."); |
94 | | - } |
95 | | - strength = get_field_parameter_double("strength"); |
| 137 | + gfield_definitions = gfd; |
| 138 | + |
| 139 | + pole_number = get_field_parameter_int("pole_number"); |
| 140 | + origin[0] = get_field_parameter_double("vx"); |
| 141 | + origin[1] = get_field_parameter_double("vy"); |
| 142 | + origin[2] = get_field_parameter_double("vz"); |
| 143 | + rotation_angle = get_field_parameter_double("rotation_angle"); |
| 144 | + auto rot_axis_option = gfield_definitions.field_parameters["rotaxis"]; |
| 145 | + longitudinal = false; |
| 146 | + if ( gfield_definitions.field_parameters["longitudinal"] == "true" ) { |
| 147 | + longitudinal = true; |
| 148 | + log->info(1, "Longitudinal field"); |
| 149 | + } else { |
| 150 | + log->info(1, "Transverse field"); |
| 151 | + } |
| 152 | + |
| 153 | + if (rot_axis_option == "X" || rot_axis_option == "x") { rotaxis = 0; } |
| 154 | + else if (rot_axis_option == "Y" || rot_axis_option == "y") { rotaxis = 1; } |
| 155 | + else if (rot_axis_option == "Z" || rot_axis_option == "z") { rotaxis = 2; } |
| 156 | + else { |
| 157 | + log->error(ERR_WRONG_FIELD_ROTATION, "GField_MultipolesFactory::load_field_definitions: Rotation axis " + gfield_definitions.field_parameters["rotaxis"] + |
| 158 | + " not supported. Exiting."); |
| 159 | + } |
| 160 | + log->info(1, "Rotation axis: ", rotaxis); |
| 161 | + strength = get_field_parameter_double("strength"); |
96 | 162 | } |
0 commit comments