BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
Loading...
Searching...
No Matches
BDSArray4D.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 "BDSArray4D.hh"
19#include "BDSDebug.hh"
20#include "BDSException.hh"
21#include "BDSFieldValue.hh"
22
23#include "globals.hh" // geant4 types / globals
24
25#include <ostream>
26#include <string>
27#include <vector>
28
29
30BDSArray4D::BDSArray4D(G4int nXIn, G4int nYIn, G4int nZIn, G4int nTIn):
31 nX(nXIn), nY(nYIn), nZ(nZIn), nT(nTIn),
32 defaultValue(BDSFieldValue()),
33 data(std::vector<BDSFieldValue>(nTIn*nZIn*nYIn*nXIn))
34{;}
35
37 G4int y,
38 G4int z,
39 G4int t)
40{
41 OutsideWarn(x,y,z,t); // keep as a warning as can't assign to invalid index
42 return data[t*nZ*nY*nX + z*nY*nX + y*nX + x];
43}
44
46 G4int y,
47 G4int z,
48 G4int t) const
49{
50 if (Outside(x,y,z,t))
51 {return defaultValue;}
52 return data[t*nZ*nY*nX + z*nY*nX + y*nX + x];
53}
54
56 G4int y,
57 G4int z,
58 G4int t) const
59{
60 return GetConst(x,y,z,t);
61}
62
63G4bool BDSArray4D::Outside(G4int x,
64 G4int y,
65 G4int z,
66 G4int t) const
67{
68 G4bool rx = x < 0 || x > nX-1;
69 G4bool ry = y < 0 || y > nY-1;
70 G4bool rz = z < 0 || z > nZ-1;
71 G4bool rt = t < 0 || t > nT-1;
72 return rx || ry || rz || rt;
73}
74
76 G4int y,
77 G4int z,
78 G4int t) const
79{
80 if (Outside(x,y,z,t))
81 {
82 throw BDSException(__METHOD_NAME__, "(" +
83 std::to_string(x) + ", " +
84 std::to_string(y) + ", " +
85 std::to_string(z) + ", " +
86 std::to_string(t) + ") is outside array");
87 }
88}
89
90std::ostream& BDSArray4D::Print(std::ostream& out) const
91{
92 out << "#x = " << nX << ", ";
93 out << "#y = " << nY << ", ";
94 out << "#z = " << nZ << ", ";
95 out << "#t = " << nT << G4endl;
96
97 for (G4int t = 0; t < nT; t++)
98 {
99 for (G4int z = 0; z < nZ; z++)
100 {
101 for (G4int y = 0; y < nY; y++)
102 {
103 for (G4int x = 0; x < nX; x++)
104 {out << BDSArray4D::GetConst(x,y,z,t) << G4endl;}
105 }
106 }
107 }
108 return out;
109}
110
111std::ostream& operator<< (std::ostream& out, BDSArray4D const &a)
112{
113 return a.Print(out);
114}
4D array and base class for 3,2 & 1D arrays.
Definition BDSArray4D.hh:50
virtual void OutsideWarn(G4int x, G4int y, G4int z, G4int t) const
Use Outside() but warn and exit if the coordinates requested are outside the array.
Definition BDSArray4D.cc:75
std::vector< BDSFieldValue > data
A 1D array representing all the data.
virtual G4bool Outside(G4int x, G4int y, G4int z, G4int t) const
Return whether the indices are valid and lie within the array boundaries or not.
Definition BDSArray4D.cc:63
virtual BDSFieldValue & operator()(G4int x, G4int y=0, G4int z=0, G4int t=0)
Setter & (technically, a non-const) accessor.
Definition BDSArray4D.cc:36
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
BDSArray4D()=delete
const G4int nT
Dimension.
General exception with possible name of object and message.
STL namespace.