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) 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 "BDSArray2DCoordsRDipole.hh"
19#include "BDSFieldValue.hh"
20
21#include "globals.hh"
22
23#include <algorithm>
24#include <cmath>
25#include <ostream>
26
27
28BDSArray2DCoordsRDipole::BDSArray2DCoordsRDipole(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 // xmin becomes -xmax
48 return (x + xMax) / xStep;
49}
50
52{
53 return (y + yMax) / yStep;
54}
55
56G4int BDSArray2DCoordsRDipole::NearestX(G4double x) const
57{
58 return (G4int)round((x+xMax)/xStep);
59}
60
61G4int BDSArray2DCoordsRDipole::NearestY(G4double y) const
62{
63 return (G4int)round((y+yMax)/yStep);
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 if (x < nX)
90 {
91 if (y < nY)
92 {// C quadrant - coordinate flip is a subtraction in array coords
93 xi = nX - 1 - x;
94 yi = nY - 1 - y;
95 }
96 else
97 {// B quadrant
98 xi = nX - 1 - x; xr = -1; // flip
99 yi = y - nY;
100 }
101 }
102 else
103 {
104 if (y < nY)
105 {// D quadrant
106 xi = x - nX; xr = -1;// just offset index, flip x component
107 yi = nY - 1 - y;
108 }
109 else
110 {// A quadrant
111 xi = x - nX;
112 yi = y - nY;
113 }
114 }
115
117
118 returnValue[0] = returnValue.x() * xr;
119 returnValue[1] = returnValue.y() * yr;
120
121 return returnValue;
122}
123
125 G4int y,
126 G4int z,
127 G4int t) const
128{
129 G4bool rx = x < 0 || x > 2*(nX-1);
130 G4bool ry = y < 0 || y > 2*(nY-1);
131 G4bool rz = z < 0 || z > nZ-1;
132 G4bool rt = t < 0 || t > nT-1;
133 return rx || ry || rz || rt;
134}
135
136std::ostream& BDSArray2DCoordsRDipole::Print(std::ostream& out) const
137{
138 std::ostream& out2 = BDSArray2DCoords::Print(out);
139
140 out2 << G4endl << "REFLECTED VERSION" << G4endl;
141
142 for (G4int t = 0; t < nT; t++)
143 {
144 for (G4int z = 0; z < nZ; z++)
145 {
146 for (G4int y = 0; y < 2*nY; y++)
147 {
148 for (G4int x = 0; x < 2*nX; x++)
149 {out2 << GetConst(x,y,z,t) << G4endl;}
150 }
151 }
152 }
153 return out2;
154}
155
156std::ostream& operator<< (std::ostream& out, BDSArray2DCoordsRDipole const &a)
157{
158 return a.Print(out);
159}
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.
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 & x() const
Accessor by name.
const T & y() const
Accessor by name.