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