BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
BDSArrayOperatorValueReflect.hh
1/*
2Beam Delivery Simulation (BDSIM) Copyright (C) Royal Holloway,
3University of London 2001 - 2022.
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#ifndef BDSARRAYOPERATORVALUEREFLECT_H
20#define BDSARRAYOPERATORVALUEREFLECT_H
21#include "BDSArrayOperatorValue.hh"
22#include "BDSFieldValue.hh"
23
24#include "G4String.hh"
25#include "G4Types.hh"
26
27#include <array>
28#include <cmath>
29#include <string>
30
38{
39public:
41 BDSArrayOperatorValue("Reflect(None)"),
42 xyz{false, false, false},
43 zeroInArrayCoords{0,0,0},
44 dimensionInverted{false, false, false},
45 arrayToSpatialIndex{0,1,2,3}
46 {;}
47 BDSArrayOperatorValueReflect(const std::array<G4bool,4>& xyzt,
48 const BDSArrayInfo& arrayInfo):
50 {
51 for (G4int i = 0; i < 3; i++)
52 {xyz[i] = xyzt[i];}
53
54 arrayToSpatialIndex = arrayInfo.arrayToSpatialIndex;
55
56 G4String newName = "Reflect(";
57 for (const auto& v : xyz)
58 {newName += std::to_string(v);}
59 newName += ")";
60 name = newName;
61
62 auto zeroV = arrayInfo.zeroPoint;
63 auto nPoints = arrayInfo.nPoints;
64 for (G4int i = 0; i < 3; i++)
65 {
66 G4bool inverted = (zeroV[i] >= nPoints[i] - 1) && (nPoints[i] > 1);
67 dimensionInverted[i] = inverted;
68 zeroInArrayCoords[i] = inverted ? nPoints[i] - 1 : (G4int)std::floor(zeroV[i]);
69 }
70 }
72
74 G4int xInd,
75 G4int yInd = 0,
76 G4int zInd = 0,
77 G4int tInd = 0) const
78 {
79 std::array<G4double, 4> m = {1,1,1,1};
80 std::array<G4int, 4> inds = {xInd, yInd, zInd, tInd};
81 for (G4int i = 0; i < 3; i++)
82 {
83 if (dimensionInverted[i])
84 {m[arrayToSpatialIndex[i]] = xyz[i] && inds[i] > zeroInArrayCoords[i] ? -1.0 : 1.0;}
85 else
86 {m[arrayToSpatialIndex[i]] = xyz[i] && inds[i] < zeroInArrayCoords[i] ? -1.0 : 1.0;}
87 }
88 tInd = 4;// to retain default values and prevent compiler warnings
89 return BDSFieldValue(v.x()*m[0], v.y()*m[1], v.z()*m[2]);
90 }
91
92private:
93 std::array<G4bool,3> xyz;
94 std::array<G4int, 4> zeroInArrayCoords;
95 std::array<G4bool,4> dimensionInverted;
96 std::array<G4int, 4> arrayToSpatialIndex;
97};
98
99#endif
Simple holder of information about an array.
Definition: BDSArrayInfo.hh:35
Reflect field component in individual dimensions.
virtual BDSFieldValue Apply(BDSFieldValue v, G4int xInd, G4int yInd=0, G4int zInd=0, G4int tInd=0) const
Interface for modifying field values.
const T & z() const
Accessor by name.
const T & x() const
Accessor by name.
const T & y() const
Accessor by name.