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) 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 "BDSArray3DCoords.hh"
19
20#include <cmath>
21#include <ostream>
22#include <set>
23#include <vector>
24
25#include "globals.hh"
26
27BDSArray3DCoords::BDSArray3DCoords(G4int nXIn, G4int nYIn, G4int nZIn,
28 G4double xMinIn, G4double xMaxIn,
29 G4double yMinIn, G4double yMaxIn,
30 G4double zMinIn, G4double zMaxIn,
31 BDSDimensionType xDimensionIn,
32 BDSDimensionType yDimensionIn,
33 BDSDimensionType zDimensionIn):
34 BDSArray4DCoords(nXIn,nYIn,nZIn,1,
35 xMinIn,xMaxIn,
36 yMinIn,yMaxIn,
37 zMinIn,zMaxIn,
38 0, 1,
39 xDimensionIn,
40 yDimensionIn,
41 zDimensionIn)
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 allDims.erase(zDimensionIn);
50 std::vector<BDSDimensionType> allDimsV(allDims.begin(), allDims.end());
51 tDimension = allDimsV[0];
52 BuildDimensionIndex();
53
54 BDSDimensionType tt = BDSDimensionType::t;
55 timeVarying = xDimensionIn == tt || yDimensionIn == tt || zDimensionIn == tt;
56}
57
59 G4double y,
60 G4double z,
61 BDSFieldValue (&localData)[2][2][2],
62 G4double& xFrac,
63 G4double& yFrac,
64 G4double& zFrac) const
65{
66 G4double xArrayCoords, yArrayCoords, zArrayCoords;
67 ArrayCoordsFromXYZ(x, xArrayCoords, y, yArrayCoords, z, zArrayCoords);
68 auto x1 = (G4int)std::floor(xArrayCoords);
69 auto y1 = (G4int)std::floor(yArrayCoords);
70 auto z1 = (G4int)std::floor(zArrayCoords);
71
72 xFrac = xArrayCoords - x1;
73 yFrac = yArrayCoords - y1;
74 zFrac = zArrayCoords - z1;
75
76 for (G4int i = 0; i < 2; i++)
77 {
78 for (G4int j = 0; j < 2; j++)
79 {
80 for (G4int k = 0; k < 2; k++)
81 {localData[i][j][k] = GetConst(x1+i, y1+j, z1+k);}
82 }
83 }
84}
85
87 G4double y,
88 G4double z,
89 BDSFieldValue (&localData)[4][4][4],
90 G4double& xFrac,
91 G4double& yFrac,
92 G4double& zFrac) const
93{
94 G4double xArrayCoords, yArrayCoords, zArrayCoords;
95 ArrayCoordsFromXYZ(x, xArrayCoords, y, yArrayCoords, z, zArrayCoords);
96 auto x1 = (G4int)std::floor(xArrayCoords);
97 auto y1 = (G4int)std::floor(yArrayCoords);
98 auto z1 = (G4int)std::floor(zArrayCoords);
99 xFrac = xArrayCoords - x1;
100 yFrac = yArrayCoords - y1;
101 zFrac = zArrayCoords - z1;
102
103 for (G4int i = 0; i < 4; i++)
104 {
105 for (G4int j = 0; j < 4; j++)
106 {
107 for (G4int k = 0; k < 4; k++)
108 {
109 localData[i][j][k] = GetConst(x1-1+i, y1-1+j, z1-1+k);
110 }
111 }
112 }
113}
114
116 G4double y,
117 G4double z,
118 G4double /*t*/) const
119{
120 G4int xind = NearestX(x);
121 G4int yind = NearestY(y);
122 G4int zind = NearestZ(z);
123 BDSFieldValue result = GetConst(xind, yind, zind); // here we're constructing a copy on purpose
124 return result;
125}
126
127std::ostream& operator<< (std::ostream& out, BDSArray3DCoords const &a)
128{
129 return a.Print(out);
130}
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:45