BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
Loading...
Searching...
No Matches
BDSArray2DCoordsRQuad.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 "BDSArray2DCoordsRQuad.hh"
19#include "BDSFieldValue.hh"
20
21#include "globals.hh"
22
23#include <algorithm>
24#include <cmath>
25#include <ostream>
26
27
28BDSArray2DCoordsRQuad::BDSArray2DCoordsRQuad(BDSArray2DCoords* arrayIn):
29 BDSArray2DCoords(*arrayIn),
30 returnValue(BDSFieldValue())
31{;}
32
34 G4double y,
35 G4double z,
36 G4double t) const
37{
38 G4bool rx = x < -xMax || x > xMax;
39 G4bool ry = y < -yMax || y > yMax;
40 G4bool rz = z < zMin || z > zMax;
41 G4bool rt = t < tMin || t > tMax;
42 return rx || ry || rz || rt;
43}
44
46{
47 return (x - (xMin - xMax)) / xStep;
48}
49
51{
52 return (y - (yMin - yMax)) / yStep;
53}
54
55G4int BDSArray2DCoordsRQuad::NearestX(G4double x) const
56{
57 return (G4int)round(ArrayCoordsFromX(x));
58}
59
60G4int BDSArray2DCoordsRQuad::NearestY(G4double y) const
61{
62 return (G4int)round(ArrayCoordsFromY(y));
63}
64
66 G4int y,
67 G4int z,
68 G4int t) const
69{
70 if (Outside(x,y,z,t))
71 {return defaultValue;}
72
73 // Data is really
74 // A
75
76 // This interface makes it look like (0,0) bottom left, top right (2*xsize, 2*ysize)
77 // in array coords - ie int number of elements.
78 // B | A
79 // -----
80 // C | D
81
82 // factors to multiply result by - saves duplicate testing of which quadrant etc
83 G4double xr = 1;
84 G4double yr = 1;
85 G4int xi = x; // copy of x index that can be modified.
86 G4int yi = y;
87
88 // nX,nY are the number of elements in the original array this class wraps
89 // nX - 1 as for array of size 3, the flip point is index 2
90 if (x < nX - 1)
91 {
92 if (y < nY - 1)
93 {// C quadrant - coordinate flip is a subtraction in array coords
94 xi = nX - 1 - x; xr = -1; // flip
95 yi = nY - 1 - y; yr = -1; // flip
96 }
97 else
98 {// B quadrant
99 xi = nX - 1 - x; // flip
100 yi = y - nY + 1; yr = -1; // just offset index, but flip result
101 }
102 }
103 else
104 {
105 if (y < nY - 1)
106 {// D quadrant
107 xi = x - nX + 1; xr = -1;// just offset index, flip x component
108 yi = nY - 1 - y;
109 }
110 else
111 {// A quadrant
112 xi = x - nX + 1;
113 yi = y - nY + 1;
114 }
115 }
116
117 G4bool swapResult = false;
118 if (yi > xi) //lies above y = x line (even in array coords)
119 {
120 std::swap(xi, yi);
121 swapResult = true;
122 }
123
125
126 if (swapResult)
127 {
128 auto xv = returnValue.x();
129 auto yv = returnValue.y();
131 }
132
133 returnValue[0] = returnValue.x() * xr;
134 returnValue[1] = returnValue.y() * yr;
135
136 return returnValue;
137}
138
140 G4int y,
141 G4int z,
142 G4int t) const
143{
144 G4bool rx = x < 0 || x > 2*(nX-1);
145 G4bool ry = y < 0 || y > 2*(nY-1);
146 G4bool rz = z < 0 || z > nZ-1;
147 G4bool rt = t < 0 || t > nT-1;
148 return rx || ry || rz || rt;
149}
150
151std::ostream& BDSArray2DCoordsRQuad::Print(std::ostream& out) const
152{
153 std::ostream& out2 = BDSArray2DCoords::Print(out);
154
155 out2 << G4endl << "REFLECTED VERSION" << G4endl;
156
157 for (G4int t = 0; t < nT; t++)
158 {
159 for (G4int z = 0; z < nZ; z++)
160 {
161 for (G4int y = 0; y < 2*nY; y++)
162 {
163 for (G4int x = 0; x < 2*nX; x++)
164 {out2 << GetConst(x,y,z,t) << G4endl;}
165 }
166 }
167 }
168 return out2;
169}
170
171std::ostream& operator<< (std::ostream& out, BDSArray2DCoordsRQuad const &a)
172{
173 return a.Print(out);
174}
A wrapper to achieve 2D reflection of a minimal quadrupole field solve.
virtual G4int NearestX(G4double x) const
Overridden from BDSArray4DCoords.
virtual G4int NearestY(G4double y) const
Overridden from BDSArray4DCoords.
virtual G4double ArrayCoordsFromY(G4double y) const
Overridden from BDSArray4DCoords.
virtual const BDSFieldValue & GetConst(G4int x, G4int y, G4int z=0, G4int t=0) const
Overridden from BDSArray4D.
virtual G4double ArrayCoordsFromX(G4double x) const
Overridden from BDSArray4DCoords.
virtual G4bool Outside(G4int x, G4int y, G4int z, G4int t) const
Overridden from BDSArray4D.
virtual G4bool OutsideCoords(G4double x, G4double y, G4double z, G4double t) const
Overridden from BDSArray4DCoords.
virtual std::ostream & Print(std::ostream &out) const
2D array with spatial mapping derived from BDSArray4DCoords.
G4double xMin
Dimension parameter - protected for derived class access.
G4double yMin
Dimension parameter - protected for derived class access.
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.
BDSFieldValue defaultValue
Need to store a default value so it can be return by reference.
const G4int nY
Dimension.
virtual const BDSFieldValue & GetConst(G4int x, G4int y=0, G4int z=0, G4int t=0) const
Definition BDSArray4D.cc:45
const G4int nX
Dimension.
virtual std::ostream & Print(std::ostream &out) const
Definition BDSArray4D.cc:90
const G4int nT
Dimension.
const T & z() const
Accessor by name.
const T & x() const
Accessor by name.
const T & y() const
Accessor by name.