BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
Loading...
Searching...
No Matches
BDSArray3DCoords.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 "BDSArray3DCoords.hh"
20
21#include <cmath>
22#include <ostream>
23#include <set>
24#include <vector>
25
26#include "globals.hh"
27
28BDSArray3DCoords::BDSArray3DCoords(G4int nXIn, G4int nYIn, G4int nZIn,
29 G4double xMinIn, G4double xMaxIn,
30 G4double yMinIn, G4double yMaxIn,
31 G4double zMinIn, G4double zMaxIn,
32 BDSDimensionType xDimensionIn,
33 BDSDimensionType yDimensionIn,
34 BDSDimensionType zDimensionIn):
35 BDSArray4DCoords(nXIn,nYIn,nZIn,1,
36 xMinIn,xMaxIn,
37 yMinIn,yMaxIn,
38 zMinIn,zMaxIn,
39 0, 1,
40 xDimensionIn,
41 yDimensionIn,
42 zDimensionIn)
43{
44 std::set<BDSDimensionType> allDims = {BDSDimensionType::x,
45 BDSDimensionType::y,
46 BDSDimensionType::z,
47 BDSDimensionType::t};
48 allDims.erase(xDimensionIn);
49 allDims.erase(yDimensionIn);
50 allDims.erase(zDimensionIn);
51 std::vector<BDSDimensionType> allDimsV(allDims.begin(), allDims.end());
52 tDimension = allDimsV[0];
53 BuildDimensionIndex();
54
55 BDSDimensionType tt = BDSDimensionType::t;
56 timeVarying = xDimensionIn == tt || yDimensionIn == tt || zDimensionIn == tt;
57}
58
60 G4double y,
61 G4double z,
62 BDSFieldValue (&localData)[2][2][2],
63 G4double& xFrac,
64 G4double& yFrac,
65 G4double& zFrac) const
66{
67 G4double xArrayCoords, yArrayCoords, zArrayCoords;
68 ArrayCoordsFromXYZ(x, xArrayCoords, y, yArrayCoords, z, zArrayCoords);
69 auto x1 = (G4int)std::floor(xArrayCoords);
70 auto y1 = (G4int)std::floor(yArrayCoords);
71 auto z1 = (G4int)std::floor(zArrayCoords);
72
73 xFrac = xArrayCoords - x1;
74 yFrac = yArrayCoords - y1;
75 zFrac = zArrayCoords - z1;
76
77 for (G4int i = 0; i < 2; i++)
78 {
79 for (G4int j = 0; j < 2; j++)
80 {
81 for (G4int k = 0; k < 2; k++)
82 {localData[i][j][k] = GetConst(x1+i, y1+j, z1+k);}
83 }
84 }
85}
86
88 G4double y,
89 G4double z,
90 BDSFieldValue (&localData)[4][4][4],
91 G4double& xFrac,
92 G4double& yFrac,
93 G4double& zFrac) const
94{
95 G4double xArrayCoords, yArrayCoords, zArrayCoords;
96 ArrayCoordsFromXYZ(x, xArrayCoords, y, yArrayCoords, z, zArrayCoords);
97 auto x1 = (G4int)std::floor(xArrayCoords);
98 auto y1 = (G4int)std::floor(yArrayCoords);
99 auto z1 = (G4int)std::floor(zArrayCoords);
100 xFrac = xArrayCoords - x1;
101 yFrac = yArrayCoords - y1;
102 zFrac = zArrayCoords - z1;
103
104 for (G4int i = 0; i < 4; i++)
105 {
106 for (G4int j = 0; j < 4; j++)
107 {
108 for (G4int k = 0; k < 4; k++)
109 {
110 localData[i][j][k] = GetConst(x1-1+i, y1-1+j, z1-1+k);
111 }
112 }
113 }
114}
115
117 G4double y,
118 G4double z,
119 G4double /*t*/) const
120{
121 G4int xind = NearestX(x);
122 G4int yind = NearestY(y);
123 G4int zind = NearestZ(z);
124 BDSFieldValue result = GetConst(xind, yind, zind); // here we're constructing a copy on purpose
125 return result;
126}
127
128std::ostream& operator<< (std::ostream& out, BDSArray3DCoords const &a)
129{
130 return a.Print(out);
131}
3D array with spatial mapping derived from BDSArray4DCoords.
BDSArray3DCoords()=delete
virtual void ExtractSection2x2x2(G4double x, G4double y, G4double z, BDSFieldValue(&localData)[2][2][2], G4double &xFrac, G4double &yFrac, G4double &zFrac) const
Extract 2x2x2 points lying around coordinate x.
virtual void ExtractSection4x4x4(G4double x, G4double y, G4double z, BDSFieldValue(&localData)[4][4][4], G4double &xFrac, G4double &yFrac, G4double &zFrac) const
Extract 4x4x4 points lying around coordinate x.
virtual BDSFieldValue ExtractNearest(G4double x, G4double y=0, G4double z=0, G4double t=0) const
Extract nearest field value from array. t ignored but required for overload.
Overlay of 4D array that provides uniform only spatial coordinate mapping.
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.
virtual G4int NearestZ(G4double z) const
Not much point in being both virtual and inline (in our use case) but has to be virtual.
void ArrayCoordsFromXYZ(G4double &x, G4double &xArr, G4double &y, G4double &yArr, G4double &z, G4double &zArr) const
Utility version to forward to individual function.
virtual const BDSFieldValue & GetConst(G4int x, G4int y=0, G4int z=0, G4int t=0) const
Definition: BDSArray4D.cc:46