BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
Loading...
Searching...
No Matches
BDSArray2DCoords.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 "BDSArray2DCoords.hh"
19#include "BDSExtent.hh"
20
21#include <array>
22#include <cmath>
23#include <limits>
24#include <ostream>
25#include <set>
26#include <vector>
27
28#include "globals.hh"
29
30BDSArray2DCoords::BDSArray2DCoords(G4int nXIn, G4int nYIn,
31 G4double xMinIn, G4double xMaxIn,
32 G4double yMinIn, G4double yMaxIn,
33 BDSDimensionType xDimensionIn,
34 BDSDimensionType yDimensionIn):
35 BDSArray3DCoords(nXIn,nYIn,1,
36 xMinIn,xMaxIn,
37 yMinIn,yMaxIn,
38 0, 1,
39 xDimensionIn,
40 yDimensionIn)
41{
42 std::set<BDSDimensionType> allDims = {BDSDimensionType::x,
43 BDSDimensionType::y,
44 BDSDimensionType::z,
45 BDSDimensionType::t};
46 allDims.erase(xDimensionIn);
47 allDims.erase(yDimensionIn);
48 std::array<BDSDimensionType*, 2> vars = {&zDimension, &tDimension};
49 std::vector<BDSDimensionType> unusedDims(allDims.begin(), allDims.end());
50 for (G4int i = 0; i < 2; i++)
51 {*(vars[i]) = unusedDims[i];}
52 BuildDimensionIndex();
53
54 BDSDimensionType tt = BDSDimensionType::t;
55 timeVarying = xDimensionIn == tt || yDimensionIn == tt;
56}
57
59 G4double y,
60 BDSFieldValue (&localData)[2][2],
61 G4double& xFrac,
62 G4double& yFrac) const
63{
64 G4double xarr, yarr;
65 ArrayCoordsFromXY(x, xarr, y, yarr);
66
67 auto x1 = (G4int)std::floor(xarr);
68 auto y1 = (G4int)std::floor(yarr);
69
70 xFrac = xarr - x1;
71 yFrac = yarr - y1;
72
73 for (G4int i = 0; i < 2; i++)
74 {
75 for (G4int j = 0; j < 2; j++)
76 {localData[i][j] = GetConst(x1+i, y1+j);}
77 }
78}
79
81 G4double y,
82 BDSFieldValue (&localData)[4][4],
83 G4double& xFrac,
84 G4double& yFrac) const
85{
86 G4double xarr, yarr;
87 ArrayCoordsFromXY(x, xarr, y, yarr);
88
89 // Array indices will look like this where point lies in unit (in array coords)
90 // square between 11,12,22,21.
91
92 // 03 13 23 33
93 // 02 12 22 32
94 // 01 11 21 31
95 // 00 10 20 30
96
97 auto x1 = (G4int)std::floor(xarr);
98 auto y1 = (G4int)std::floor(yarr);
99
100 xFrac = xarr - x1;
101 yFrac = yarr - y1;
102
103 for (G4int i = 0; i < 4; i++)
104 {
105 for (G4int j = 0; j < 4; j++)
106 {localData[i][j] = GetConst(x1-1+i, y1-1+j);}
107 }
108}
109
111 G4double y,
112 G4double /*z*/,
113 G4double /*t*/) const
114{
115 G4int xind = NearestX(x);
116 G4int yind = NearestY(y);
117 BDSFieldValue result = GetConst(xind, yind); // here we're constructing a copy on purpose
118 return result;
119}
120
121std::ostream& operator<< (std::ostream& out, BDSArray2DCoords const &a)
122{
123 return a.Print(out);
124}
125
127{
128 G4double limitMax = std::numeric_limits<double>::max();
129 G4double limitMin = std::numeric_limits<double>::lowest();
130 return BDSExtent(xMin, xMax, yMin, yMax, limitMin, limitMax);
131}
2D array with spatial mapping derived from BDSArray4DCoords.
virtual void ExtractSection4x4(G4double x, G4double y, BDSFieldValue(&localData)[4][4], G4double &xFrac, G4double &yFrac) const
Extract 4x4 points lying around coordinate x.
virtual BDSExtent Extent() const
virtual BDSFieldValue ExtractNearest(G4double x, G4double y=0, G4double z=0, G4double t=0) const
Extract nearest field value from array. z,t ignored but required for overload.
BDSArray2DCoords()=delete
virtual void ExtractSection2x2(G4double x, G4double y, BDSFieldValue(&localData)[2][2], G4double &xFrac, G4double &yFrac) const
Extract 2x2 points lying around coordinate x.
3D array with spatial mapping derived from BDSArray4DCoords.
G4double xMin
Dimension parameter - protected for derived class access.
void ArrayCoordsFromXY(G4double &x, G4double &xArr, G4double &y, G4double &yArr) const
Utility version to forward to individual function.
G4double yMin
Dimension parameter - protected for derived class access.
virtual std::ostream & Print(std::ostream &out) const
virtual G4int NearestY(G4double y) const
Not much point in being both virtual and inline (in our use case) but has to be virtual.
virtual G4int NearestX(G4double x) const
Not much point in being both virtual and inline (in our use case) but has to be virtual.
G4double yMax
Dimension parameter - protected for derived class access.
G4double xMax
Dimension parameter - protected for derived class access.
virtual const BDSFieldValue & GetConst(G4int x, G4int y=0, G4int z=0, G4int t=0) const
Definition BDSArray4D.cc:45
Holder for +- extents in 3 dimensions.
Definition BDSExtent.hh:38