BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
Loading...
Searching...
No Matches
BDSArray2DCoordsRDipole.cc
1/*
2Beam Delivery Simulation (BDSIM) Copyright (C) Royal Holloway,
3University of London 2001 - 2023.
4
5This file is part of BDSIM.
6
7BDSIM is free software: you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published
9by the Free Software Foundation version 3 of the License.
10
11BDSIM is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with BDSIM. If not, see <http://www.gnu.org/licenses/>.
18*/
19#include "BDSArray2DCoordsRDipole.hh"
20#include "BDSFieldValue.hh"
21
22#include "globals.hh"
23
24#include <algorithm>
25#include <cmath>
26#include <ostream>
27
28
29BDSArray2DCoordsRDipole::BDSArray2DCoordsRDipole(BDSArray2DCoords* arrayIn):
30 BDSArray2DCoords(*arrayIn),
31 returnValue(BDSFieldValue())
32{;}
33
35 G4double y,
36 G4double z,
37 G4double t) const
38{
39 G4bool rx = x < -xMax || x > xMax;
40 G4bool ry = y < -yMax || y > yMax;
41 G4bool rz = z < zMin || z > zMax;
42 G4bool rt = t < tMin || t > tMax;
43 return rx || ry || rz || rt;
44}
45
47{
48 // xmin becomes -xmax
49 return (x + xMax) / xStep;
50}
51
53{
54 return (y + yMax) / yStep;
55}
56
57G4int BDSArray2DCoordsRDipole::NearestX(G4double x) const
58{
59 return (G4int)round((x+xMax)/xStep);
60}
61
62G4int BDSArray2DCoordsRDipole::NearestY(G4double y) const
63{
64 return (G4int)round((y+yMax)/yStep);
65}
66
68 G4int y,
69 G4int z,
70 G4int t) const
71{
72 if (Outside(x,y,z,t))
73 {return defaultValue;}
74
75 // Data is really
76 // A
77
78 // This interface makes it look like (0,0) bottom left, top right (2*xsize, 2*ysize)
79 // in array coords - ie int number of elements.
80 // B | A
81 // -----
82 // C | D
83
84 // factors to multiply result by - saves duplicate testing of which quadrant etc
85 G4double xr = 1;
86 G4double yr = 1;
87 G4int xi = x; // copy of x index that can be modified.
88 G4int yi = y;
89
90 if (x < nX)
91 {
92 if (y < nY)
93 {// C quadrant - coordinate flip is a subtraction in array coords
94 xi = nX - 1 - x;
95 yi = nY - 1 - y;
96 }
97 else
98 {// B quadrant
99 xi = nX - 1 - x; xr = -1; // flip
100 yi = y - nY;
101 }
102 }
103 else
104 {
105 if (y < nY)
106 {// D quadrant
107 xi = x - nX; xr = -1;// just offset index, flip x component
108 yi = nY - 1 - y;
109 }
110 else
111 {// A quadrant
112 xi = x - nX;
113 yi = y - nY;
114 }
115 }
116
118
119 returnValue[0] = returnValue.x() * xr;
120 returnValue[1] = returnValue.y() * yr;
121
122 return returnValue;
123}
124
126 G4int y,
127 G4int z,
128 G4int t) const
129{
130 G4bool rx = x < 0 || x > 2*(nX-1);
131 G4bool ry = y < 0 || y > 2*(nY-1);
132 G4bool rz = z < 0 || z > nZ-1;
133 G4bool rt = t < 0 || t > nT-1;
134 return rx || ry || rz || rt;
135}
136
137std::ostream& BDSArray2DCoordsRDipole::Print(std::ostream& out) const
138{
139 std::ostream& out2 = BDSArray2DCoords::Print(out);
140
141 out2 << G4endl << "REFLECTED VERSION" << G4endl;
142
143 for (G4int t = 0; t < nT; t++)
144 {
145 for (G4int z = 0; z < nZ; z++)
146 {
147 for (G4int y = 0; y < 2*nY; y++)
148 {
149 for (G4int x = 0; x < 2*nX; x++)
150 {out2 << GetConst(x,y,z,t) << G4endl;}
151 }
152 }
153 }
154 return out2;
155}
156
157std::ostream& operator<< (std::ostream& out, BDSArray2DCoordsRDipole const &a)
158{
159 return a.Print(out);
160}
A wrapper to achieve 2D reflection of a minimal dipole field solve.
virtual G4double ArrayCoordsFromY(G4double y) const
Overridden from BDSArray4DCoords.
virtual G4bool OutsideCoords(G4double x, G4double y, G4double z, G4double t) const
Overridden from BDSArray4DCoords.
virtual G4double ArrayCoordsFromX(G4double x) const
Overridden from BDSArray4DCoords.
virtual G4int NearestX(G4double x) const
Overridden from BDSArray4DCoords.
virtual G4bool Outside(G4int x, G4int y, G4int z, G4int t) const
Overridden from BDSArray4D.
virtual const BDSFieldValue & GetConst(G4int x, G4int y, G4int z=0, G4int t=0) const
Overridden from BDSArray4D.
virtual G4int NearestY(G4double y) const
Overridden from BDSArray4DCoords.
virtual std::ostream & Print(std::ostream &out) const
2D array with spatial mapping derived from BDSArray4DCoords.
G4double yStep
Dimension parameter - protected for derived class access.
G4double zMax
Dimension parameter - protected for derived class access.
G4double xStep
Dimension parameter - protected for derived class access.
G4double yMax
Dimension parameter - protected for derived class access.
G4double xMax
Dimension parameter - protected for derived class access.
G4double tMax
Dimension parameter - protected for derived class access.
const G4int nZ
Dimension.
Definition: BDSArray4D.hh:122
BDSFieldValue defaultValue
Need to store a default value so it can be return by reference.
Definition: BDSArray4D.hh:127
const G4int nY
Dimension.
Definition: BDSArray4D.hh:121
virtual const BDSFieldValue & GetConst(G4int x, G4int y=0, G4int z=0, G4int t=0) const
Definition: BDSArray4D.cc:46
const G4int nX
Dimension.
Definition: BDSArray4D.hh:120
virtual std::ostream & Print(std::ostream &out) const
Definition: BDSArray4D.cc:91
const G4int nT
Dimension.
Definition: BDSArray4D.hh:123
const T & x() const
Accessor by name.
const T & y() const
Accessor by name.