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) 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 "BDSArray2DCoordsRQuad.hh"
20#include "BDSFieldValue.hh"
21
22#include "globals.hh"
23
24#include <algorithm>
25#include <cmath>
26#include <ostream>
27
28
29BDSArray2DCoordsRQuad::BDSArray2DCoordsRQuad(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 return (x - (xMin - xMax)) / xStep;
49}
50
52{
53 return (y - (yMin - yMax)) / yStep;
54}
55
56G4int BDSArray2DCoordsRQuad::NearestX(G4double x) const
57{
58 return (G4int)round(ArrayCoordsFromX(x));
59}
60
61G4int BDSArray2DCoordsRQuad::NearestY(G4double y) const
62{
63 return (G4int)round(ArrayCoordsFromY(y));
64}
65
67 G4int y,
68 G4int z,
69 G4int t) const
70{
71 if (Outside(x,y,z,t))
72 {return defaultValue;}
73
74 // Data is really
75 // A
76
77 // This interface makes it look like (0,0) bottom left, top right (2*xsize, 2*ysize)
78 // in array coords - ie int number of elements.
79 // B | A
80 // -----
81 // C | D
82
83 // factors to multiply result by - saves duplicate testing of which quadrant etc
84 G4double xr = 1;
85 G4double yr = 1;
86 G4int xi = x; // copy of x index that can be modified.
87 G4int yi = y;
88
89 // nX,nY are the number of elements in the original array this class wraps
90 // nX - 1 as for array of size 3, the flip point is index 2
91 if (x < nX - 1)
92 {
93 if (y < nY - 1)
94 {// C quadrant - coordinate flip is a subtraction in array coords
95 xi = nX - 1 - x; xr = -1; // flip
96 yi = nY - 1 - y; yr = -1; // flip
97 }
98 else
99 {// B quadrant
100 xi = nX - 1 - x; // flip
101 yi = y - nY + 1; yr = -1; // just offset index, but flip result
102 }
103 }
104 else
105 {
106 if (y < nY - 1)
107 {// D quadrant
108 xi = x - nX + 1; xr = -1;// just offset index, flip x component
109 yi = nY - 1 - y;
110 }
111 else
112 {// A quadrant
113 xi = x - nX + 1;
114 yi = y - nY + 1;
115 }
116 }
117
118 G4bool swapResult = false;
119 if (yi > xi) //lies above y = x line (even in array coords)
120 {
121 std::swap(xi, yi);
122 swapResult = true;
123 }
124
126
127 if (swapResult)
128 {
129 auto xv = returnValue.x();
130 auto yv = returnValue.y();
132 }
133
134 returnValue[0] = returnValue.x() * xr;
135 returnValue[1] = returnValue.y() * yr;
136
137 return returnValue;
138}
139
141 G4int y,
142 G4int z,
143 G4int t) const
144{
145 G4bool rx = x < 0 || x > 2*(nX-1);
146 G4bool ry = y < 0 || y > 2*(nY-1);
147 G4bool rz = z < 0 || z > nZ-1;
148 G4bool rt = t < 0 || t > nT-1;
149 return rx || ry || rz || rt;
150}
151
152std::ostream& BDSArray2DCoordsRQuad::Print(std::ostream& out) const
153{
154 std::ostream& out2 = BDSArray2DCoords::Print(out);
155
156 out2 << G4endl << "REFLECTED VERSION" << G4endl;
157
158 for (G4int t = 0; t < nT; t++)
159 {
160 for (G4int z = 0; z < nZ; z++)
161 {
162 for (G4int y = 0; y < 2*nY; y++)
163 {
164 for (G4int x = 0; x < 2*nX; x++)
165 {out2 << GetConst(x,y,z,t) << G4endl;}
166 }
167 }
168 }
169 return out2;
170}
171
172std::ostream& operator<< (std::ostream& out, BDSArray2DCoordsRQuad const &a)
173{
174 return a.Print(out);
175}
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.
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 & z() const
Accessor by name.
const T & x() const
Accessor by name.
const T & y() const
Accessor by name.