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) 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 "BDSArray2DCoords.hh"
20#include "BDSExtent.hh"
21
22#include <array>
23#include <cmath>
24#include <limits>
25#include <ostream>
26#include <set>
27#include <vector>
28
29#include "globals.hh"
30
31BDSArray2DCoords::BDSArray2DCoords(G4int nXIn, G4int nYIn,
32 G4double xMinIn, G4double xMaxIn,
33 G4double yMinIn, G4double yMaxIn,
34 BDSDimensionType xDimensionIn,
35 BDSDimensionType yDimensionIn):
36 BDSArray3DCoords(nXIn,nYIn,1,
37 xMinIn,xMaxIn,
38 yMinIn,yMaxIn,
39 0, 1,
40 xDimensionIn,
41 yDimensionIn)
42{
43 std::set<BDSDimensionType> allDims = {BDSDimensionType::x,
44 BDSDimensionType::y,
45 BDSDimensionType::z,
46 BDSDimensionType::t};
47 allDims.erase(xDimensionIn);
48 allDims.erase(yDimensionIn);
49 std::array<BDSDimensionType*, 2> vars = {&zDimension, &tDimension};
50 std::vector<BDSDimensionType> unusedDims(allDims.begin(), allDims.end());
51 for (G4int i = 0; i < 2; i++)
52 {*(vars[i]) = unusedDims[i];}
53 BuildDimensionIndex();
54
55 BDSDimensionType tt = BDSDimensionType::t;
56 timeVarying = xDimensionIn == tt || yDimensionIn == tt;
57}
58
60 G4double y,
61 BDSFieldValue (&localData)[2][2],
62 G4double& xFrac,
63 G4double& yFrac) const
64{
65 G4double xarr, yarr;
66 ArrayCoordsFromXY(x, xarr, y, yarr);
67
68 auto x1 = (G4int)std::floor(xarr);
69 auto y1 = (G4int)std::floor(yarr);
70
71 xFrac = xarr - x1;
72 yFrac = yarr - y1;
73
74 for (G4int i = 0; i < 2; i++)
75 {
76 for (G4int j = 0; j < 2; j++)
77 {localData[i][j] = GetConst(x1+i, y1+j);}
78 }
79}
80
82 G4double y,
83 BDSFieldValue (&localData)[4][4],
84 G4double& xFrac,
85 G4double& yFrac) const
86{
87 G4double xarr, yarr;
88 ArrayCoordsFromXY(x, xarr, y, yarr);
89
90 // Array indices will look like this where point lies in unit (in array coords)
91 // square between 11,12,22,21.
92
93 // 03 13 23 33
94 // 02 12 22 32
95 // 01 11 21 31
96 // 00 10 20 30
97
98 auto x1 = (G4int)std::floor(xarr);
99 auto y1 = (G4int)std::floor(yarr);
100
101 xFrac = xarr - x1;
102 yFrac = yarr - y1;
103
104 for (G4int i = 0; i < 4; i++)
105 {
106 for (G4int j = 0; j < 4; j++)
107 {localData[i][j] = GetConst(x1-1+i, y1-1+j);}
108 }
109}
110
112 G4double y,
113 G4double /*z*/,
114 G4double /*t*/) const
115{
116 G4int xind = NearestX(x);
117 G4int yind = NearestY(y);
118 BDSFieldValue result = GetConst(xind, yind); // here we're constructing a copy on purpose
119 return result;
120}
121
122std::ostream& operator<< (std::ostream& out, BDSArray2DCoords const &a)
123{
124 return a.Print(out);
125}
126
128{
129 G4double limitMax = std::numeric_limits<double>::max();
130 G4double limitMin = std::numeric_limits<double>::lowest();
131 return BDSExtent(xMin, xMax, yMin, yMax, limitMin, limitMax);
132}
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:46
Holder for +- extents in 3 dimensions.
Definition: BDSExtent.hh:39