BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
Loading...
Searching...
No Matches
BDSArray3DCoordsTransformed.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 "BDSArray3DCoordsTransformed.hh"
20#include "BDSArrayOperatorIndex.hh"
21#include "BDSArrayOperatorValue.hh"
22#include "BDSFieldValue.hh"
23
24#include <array>
25#include <cmath>
26#include <ostream>
27
28#include "globals.hh"
29
31 BDSArrayOperatorIndex* indexOperatorIn,
32 BDSArrayOperatorValue* valueOperatorIn):
33 BDSArray3DCoords(*arrayIn),
34 indexOperator(indexOperatorIn),
35 valueOperator(valueOperatorIn),
36 returnValue(BDSFieldValue())
37{;}
38
39BDSArray3DCoordsTransformed::~BDSArray3DCoordsTransformed()
40{
41 delete indexOperator;
42 delete valueOperator;
43}
44
46 G4double y,
47 G4double z,
48 BDSFieldValue (&localData)[2][2][2],
49 G4double& xFrac,
50 G4double& yFrac,
51 G4double& zFrac) const
52{
53 G4double xArrayCoords, yArrayCoords, zArrayCoords;
54 ArrayCoordsFromXYZ(x, xArrayCoords, y, yArrayCoords, z, zArrayCoords);
55 auto x1 = (G4int)std::floor(xArrayCoords);
56 auto y1 = (G4int)std::floor(yArrayCoords);
57 auto z1 = (G4int)std::floor(zArrayCoords);
58 xFrac = xArrayCoords - x1;
59 yFrac = yArrayCoords - y1;
60 zFrac = zArrayCoords - z1;
61
62 std::array<G4int, 2> indexArrX = {x1, x1+1};
63 std::array<G4int, 2> indexArrY = {y1, y1+1};
64 std::array<G4int, 2> indexArrZ = {z1, z1+1};
65 G4int indexOriginalX, indexOriginalY, indexOriginalZ, indexTransformedX, indexTransformedY, indexTransformedZ;
66 for (G4int i = 0; i < 2; i++)
67 {
68 for (G4int j = 0; j < 2; j++)
69 {
70 for (G4int k = 0; k < 2; k++)
71 {
72 indexOriginalX = indexArrX[i];
73 indexTransformedX = indexOriginalX;
74 indexOriginalY = indexArrY[j];
75 indexTransformedY = indexOriginalY;
76 indexOriginalZ = indexArrZ[k];
77 indexTransformedZ = indexOriginalZ;
78 indexOperator->Apply(indexTransformedX, indexTransformedY, indexTransformedZ);
79 BDSFieldValue v = GetConst(indexTransformedX, indexTransformedY, indexTransformedZ);
80 localData[i][j][k] = valueOperator->Apply(v, indexOriginalX, indexOriginalY, indexOriginalZ);
81 }
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 = ArrayCoordsFromX(x);
95 G4double yArrayCoords = ArrayCoordsFromY(y);
96 G4double zArrayCoords = ArrayCoordsFromZ(z);
97 auto x1 = (G4int)std::floor(xArrayCoords);
98 auto y1 = (G4int)std::floor(yArrayCoords);
99 auto z1 = (G4int)std::floor(zArrayCoords);
100 xFrac = xArrayCoords - x1;
101 yFrac = yArrayCoords - y1;
102 zFrac = zArrayCoords - z1;
103
104 std::array<G4int, 4> indexArrX = {x1-1, x1, x1+1, x1+2};
105 std::array<G4int, 4> indexArrY = {y1-1, y1, y1+1, y1+2};
106 std::array<G4int, 4> indexArrZ = {z1-1, z1, z1+1, z1+2};
107 G4int indexOriginalX, indexOriginalY, indexOriginalZ, indexTransformedX, indexTransformedY, indexTransformedZ;
108 for (G4int i = 0; i < 4; i++)
109 {
110 for (G4int j = 0; j < 4; j++)
111 {
112 for (G4int k = 0; k < 4; k++)
113 {
114 indexOriginalX = indexArrX[i];
115 indexTransformedX = indexOriginalX;
116 indexOriginalY = indexArrY[j];
117 indexTransformedY = indexOriginalY;
118 indexOriginalZ = indexArrZ[k];
119 indexTransformedZ = indexOriginalZ;
120 indexOperator->Apply(indexTransformedX, indexTransformedY, indexTransformedZ);
121 BDSFieldValue v = GetConst(indexTransformedX, indexTransformedY, indexTransformedZ);
122 localData[i][j][k] = valueOperator->Apply(v, indexOriginalX, indexOriginalY, indexOriginalZ);
123 }
124 }
125 }
126}
127
129 G4double y,
130 G4double z,
131 G4double /*t*/) const
132{
133 G4int indexOriginalX = NearestX(x);
134 G4int indexOriginalY = NearestY(y);
135 G4int indexOriginalZ = NearestZ(z);
136 G4int indexTransformedX = indexOriginalX;
137 G4int indexTransformedY = indexOriginalY;
138 G4int indexTransformedZ = indexOriginalZ;
139 indexOperator->Apply(indexTransformedX, indexTransformedY, indexTransformedZ);
140 BDSFieldValue v = GetConst(indexTransformedX, indexTransformedY, indexTransformedZ);
141 v = valueOperator->Apply(v, indexOriginalX, indexOriginalY, indexOriginalZ);
142 return v;
143}
144
145std::ostream& BDSArray3DCoordsTransformed::Print(std::ostream& out) const
146{
147 out << "Spatial limits are the original ones" << G4endl;
148 out << "Array index operator: " << indexOperator->Name() << G4endl;
149 out << "Array value operator: " << valueOperator->Name() << G4endl;
150 return BDSArray3DCoords::Print(out);
151}
152
153std::ostream& operator<< (std::ostream& out, BDSArray3DCoordsTransformed const &a)
154{
155 return a.Print(out);
156}
Wrapped BDSArray3DCoords with possible transformation to extend field.
virtual void ExtractSection2x2x2(G4double x, G4double y, G4double z, BDSFieldValue(&localData)[2][2][2], G4double &xFrac, G4double &yFrac, G4double &zFrac) const
virtual BDSFieldValue ExtractNearest(G4double x, G4double y=0, G4double z=0, G4double t=0) const
BDSArray3DCoordsTransformed()=delete
No default constructor.
virtual std::ostream & Print(std::ostream &out) const
virtual void ExtractSection4x4x4(G4double x, G4double y, G4double z, BDSFieldValue(&localData)[4][4][4], G4double &xFrac, G4double &yFrac, G4double &zFrac) const
3D array with spatial mapping derived from BDSArray4DCoords.
virtual G4double ArrayCoordsFromX(G4double x) const
Not much point in being both virtual and inline (in our use case) but has to be virtual.
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 G4double ArrayCoordsFromZ(G4double z) 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 G4double ArrayCoordsFromY(G4double y) 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:46
virtual std::ostream & Print(std::ostream &out) const
Definition: BDSArray4D.cc:91
Interface for modifying by reference array indices.
virtual G4String Name() const
Supply a name of this operator for feedback to the user in print out.
virtual void Apply(G4int &, G4int &, G4int &, G4int &) const
Interface for modifying field values.
virtual BDSFieldValue Apply(BDSFieldValue v, G4int, G4int=0, G4int=0, G4int=0) const
virtual G4String Name() const
Return a name of the operator for feedback to the user in print out.