BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
Loading...
Searching...
No Matches
BDSArray4DCoordsTransformed.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 "BDSArray4DCoordsTransformed.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 BDSArray4DCoords(*arrayIn),
34 indexOperator(indexOperatorIn),
35 valueOperator(valueOperatorIn),
36 returnValue(BDSFieldValue())
37{;}
38
39BDSArray4DCoordsTransformed::~BDSArray4DCoordsTransformed()
40{
41 delete indexOperator;
42 delete valueOperator;
43}
44
46 G4double y,
47 G4double z,
48 G4double t,
49 BDSFieldValue (&localData)[2][2][2][2],
50 G4double& xFrac,
51 G4double& yFrac,
52 G4double& zFrac,
53 G4double& tFrac) const
54{
55 G4double xArrayCoords = ArrayCoordsFromX(x);
56 G4double yArrayCoords = ArrayCoordsFromY(y);
57 G4double zArrayCoords = ArrayCoordsFromZ(z);
58 G4double tArrayCoords = ArrayCoordsFromT(t);
59 auto x1 = (G4int)std::floor(xArrayCoords);
60 auto y1 = (G4int)std::floor(yArrayCoords);
61 auto z1 = (G4int)std::floor(zArrayCoords);
62 auto t1 = (G4int)std::floor(tArrayCoords);
63 xFrac = xArrayCoords - x1;
64 yFrac = yArrayCoords - y1;
65 zFrac = zArrayCoords - z1;
66 tFrac = tArrayCoords - t1;
67
68 std::array<G4int, 2> indexArrX = {x1, x1+1};
69 std::array<G4int, 2> indexArrY = {y1, y1+1};
70 std::array<G4int, 2> indexArrZ = {z1, z1+1};
71 std::array<G4int, 2> indexArrT = {t1, t1+1};
72 G4int indexOriginalX, indexOriginalY, indexOriginalZ, indexOriginalT;
73 G4int indexTransformedX, indexTransformedY, indexTransformedZ, indexTransformedT;
74 for (G4int i = 0; i < 2; i++)
75 {
76 for (G4int j = 0; j < 2; j++)
77 {
78 for (G4int k = 0; k < 2; k++)
79 {
80 for (G4int l = 0; l < 2; l++)
81 {
82 indexOriginalX = indexArrX[i];
83 indexTransformedX = indexOriginalX;
84 indexOriginalY = indexArrY[j];
85 indexTransformedY = indexOriginalY;
86 indexOriginalZ = indexArrZ[k];
87 indexTransformedZ = indexOriginalZ;
88 indexOriginalT = indexArrT[l];
89 indexTransformedT = indexOriginalT;
90 indexOperator->Apply(indexTransformedX, indexTransformedY, indexTransformedZ, indexTransformedT);
91 BDSFieldValue v = GetConst(indexTransformedX, indexTransformedY, indexTransformedZ, indexTransformedT);
92 localData[i][j][k][l] = valueOperator->Apply(v, indexOriginalX, indexOriginalY, indexOriginalZ, indexOriginalT);
93 }
94 }
95 }
96 }
97}
98
100 G4double y,
101 G4double z,
102 G4double t,
103 BDSFieldValue (&localData)[4][4][4][4],
104 G4double& xFrac,
105 G4double& yFrac,
106 G4double& zFrac,
107 G4double& tFrac) const
108{
109 G4double xArrayCoords, yArrayCoords, zArrayCoords, tArrayCoords;
110 ArrayCoordsFromXYZT(x, xArrayCoords, y, yArrayCoords, z, zArrayCoords, t, tArrayCoords);
111 auto x1 = (G4int)std::floor(xArrayCoords);
112 auto y1 = (G4int)std::floor(yArrayCoords);
113 auto z1 = (G4int)std::floor(zArrayCoords);
114 auto t1 = (G4int)std::floor(tArrayCoords);
115 xFrac = xArrayCoords - x1;
116 yFrac = yArrayCoords - y1;
117 zFrac = zArrayCoords - z1;
118 tFrac = tArrayCoords - t1;
119
120 std::array<G4int, 4> indexArrX = {x1-1, x1, x1+1, x1+2};
121 std::array<G4int, 4> indexArrY = {y1-1, y1, y1+1, y1+2};
122 std::array<G4int, 4> indexArrZ = {z1-1, z1, z1+1, z1+2};
123 std::array<G4int, 4> indexArrT = {t1-1, t1, t1+1, t1+2};
124 G4int indexOriginalX, indexOriginalY, indexOriginalZ, indexOriginalT;
125 G4int indexTransformedX, indexTransformedY, indexTransformedZ, indexTransformedT;
126 for (G4int i = 0; i < 4; i++)
127 {
128 for (G4int j = 0; j < 4; j++)
129 {
130 for (G4int k = 0; k < 4; k++)
131 {
132 for (G4int l = 0; l < 4; l++)
133 {
134 indexOriginalX = indexArrX[i];
135 indexTransformedX = indexOriginalX;
136 indexOriginalY = indexArrY[j];
137 indexTransformedY = indexOriginalY;
138 indexOriginalZ = indexArrZ[k];
139 indexTransformedZ = indexOriginalZ;
140 indexOriginalT = indexArrT[l];
141 indexTransformedT = indexOriginalT;
142 indexOperator->Apply(indexTransformedX, indexTransformedY, indexTransformedZ, indexTransformedT);
143 BDSFieldValue v = GetConst(indexTransformedX, indexTransformedY, indexTransformedZ, indexTransformedT);
144 localData[i][j][k][l] = valueOperator->Apply(v, indexOriginalX, indexOriginalY, indexOriginalZ, indexOriginalT);
145 }
146 }
147 }
148 }
149}
150
152 G4double y,
153 G4double z,
154 G4double t) const
155{
156 G4int indexOriginalX = NearestX(x);
157 G4int indexOriginalY = NearestY(y);
158 G4int indexOriginalZ = NearestY(z);
159 G4int indexOriginalT = NearestY(t);
160 G4int indexTransformedX = indexOriginalX;
161 G4int indexTransformedY = indexOriginalY;
162 G4int indexTransformedZ = indexOriginalZ;
163 G4int indexTransformedT = indexOriginalT;
164 indexOperator->Apply(indexTransformedX, indexTransformedY, indexTransformedZ, indexTransformedT);
165 BDSFieldValue v = GetConst(indexTransformedX, indexTransformedY, indexTransformedT, indexTransformedZ);
166 v = valueOperator->Apply(v, indexOriginalX, indexOriginalY, indexOriginalZ, indexOriginalT);
167 return v;
168}
169
170std::ostream& BDSArray4DCoordsTransformed::Print(std::ostream& out) const
171{
172 out << "Spatial limits are the original ones" << G4endl;
173 out << "Array index operator: " << indexOperator->Name() << G4endl;
174 out << "Array value operator: " << valueOperator->Name() << G4endl;
175 return BDSArray4DCoords::Print(out);
176}
177
178std::ostream& operator<< (std::ostream& out, BDSArray4DCoordsTransformed const &a)
179{
180 return a.Print(out);
181}
Wrapped BDSArray4DCoords with possible transformation to extend field.
BDSArray4DCoordsTransformed()=delete
No default constructor.
virtual void ExtractSection4x4x4x4(G4double x, G4double y, G4double z, G4double t, BDSFieldValue(&localData)[4][4][4][4], G4double &xFrac, G4double &yFrac, G4double &zFrac, G4double &tFrac) const
virtual BDSFieldValue ExtractNearest(G4double x, G4double y=0, G4double z=0, G4double t=0) const
virtual std::ostream & Print(std::ostream &out) const
virtual void ExtractSection2x2x2x2(G4double x, G4double y, G4double z, G4double t, BDSFieldValue(&localData)[2][2][2][2], G4double &xFrac, G4double &yFrac, G4double &zFrac, G4double &tFrac) const
Overlay of 4D array that provides uniform only spatial coordinate mapping.
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 std::ostream & Print(std::ostream &out) const
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 G4double ArrayCoordsFromT(G4double t) const
Not much point in being both virtual and inline (in our use case) but has to be virtual.
void ArrayCoordsFromXYZT(G4double &x, G4double &xArr, G4double &y, G4double &yArr, G4double &z, G4double &zArr, G4double &t, G4double &tArr) 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
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.