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) 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 "BDSArray4D.hh"
20#include "BDSDebug.hh"
21#include "BDSException.hh"
22#include "BDSFieldValue.hh"
23
24#include "globals.hh" // geant4 types / globals
25
26#include <ostream>
27#include <string>
28#include <vector>
29
30
31BDSArray4D::BDSArray4D(G4int nXIn, G4int nYIn, G4int nZIn, G4int nTIn):
32 nX(nXIn), nY(nYIn), nZ(nZIn), nT(nTIn),
33 defaultValue(BDSFieldValue()),
34 data(std::vector<BDSFieldValue>(nTIn*nZIn*nYIn*nXIn))
35{;}
36
38 G4int y,
39 G4int z,
40 G4int t)
41{
42 OutsideWarn(x,y,z,t); // keep as a warning as can't assign to invalid index
43 return data[t*nZ*nY*nX + z*nY*nX + y*nX + x];
44}
45
47 G4int y,
48 G4int z,
49 G4int t) const
50{
51 if (Outside(x,y,z,t))
52 {return defaultValue;}
53 return data[t*nZ*nY*nX + z*nY*nX + y*nX + x];
54}
55
57 G4int y,
58 G4int z,
59 G4int t) const
60{
61 return GetConst(x,y,z,t);
62}
63
64G4bool BDSArray4D::Outside(G4int x,
65 G4int y,
66 G4int z,
67 G4int t) const
68{
69 G4bool rx = x < 0 || x > nX-1;
70 G4bool ry = y < 0 || y > nY-1;
71 G4bool rz = z < 0 || z > nZ-1;
72 G4bool rt = t < 0 || t > nT-1;
73 return rx || ry || rz || rt;
74}
75
77 G4int y,
78 G4int z,
79 G4int t) const
80{
81 if (Outside(x,y,z,t))
82 {
83 throw BDSException(__METHOD_NAME__, "(" +
84 std::to_string(x) + ", " +
85 std::to_string(y) + ", " +
86 std::to_string(z) + ", " +
87 std::to_string(t) + ") is outside array");
88 }
89}
90
91std::ostream& BDSArray4D::Print(std::ostream& out) const
92{
93 out << "#x = " << nX << ", ";
94 out << "#y = " << nY << ", ";
95 out << "#z = " << nZ << ", ";
96 out << "#t = " << nT << G4endl;
97
98 for (G4int t = 0; t < nT; t++)
99 {
100 for (G4int z = 0; z < nZ; z++)
101 {
102 for (G4int y = 0; y < nY; y++)
103 {
104 for (G4int x = 0; x < nX; x++)
105 {out << BDSArray4D::GetConst(x,y,z,t) << G4endl;}
106 }
107 }
108 }
109 return out;
110}
111
112std::ostream& operator<< (std::ostream& out, BDSArray4D const &a)
113{
114 return a.Print(out);
115}
4D array and base class for 3,2 & 1D arrays.
Definition: BDSArray4D.hh:51
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:76
std::vector< BDSFieldValue > data
A 1D array representing all the data.
Definition: BDSArray4D.hh:131
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:64
virtual BDSFieldValue & operator()(G4int x, G4int y=0, G4int z=0, G4int t=0)
Setter & (technically, a non-const) accessor.
Definition: BDSArray4D.cc:37
const G4int nZ
Dimension.
Definition: BDSArray4D.hh:122
BDSFieldValue defaultValue
Need to store a default value so it can be return by reference.
Definition: BDSArray4D.hh:127
const G4int nY
Dimension.
Definition: BDSArray4D.hh:121
virtual const BDSFieldValue & GetConst(G4int x, G4int y=0, G4int z=0, G4int t=0) const
Definition: BDSArray4D.cc:46
const G4int nX
Dimension.
Definition: BDSArray4D.hh:120
virtual std::ostream & Print(std::ostream &out) const
Definition: BDSArray4D.cc:91
BDSArray4D()=delete
const G4int nT
Dimension.
Definition: BDSArray4D.hh:123
General exception with possible name of object and message.
Definition: BDSException.hh:35