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) 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#include "BDSArray4DCoordsTransformed.hh"
19#include "BDSArrayOperatorIndex.hh"
20#include "BDSArrayOperatorValue.hh"
21#include "BDSFieldValue.hh"
22
23#include <array>
24#include <cmath>
25#include <ostream>
26
27#include "globals.hh"
28
30 BDSArrayOperatorIndex* indexOperatorIn,
31 BDSArrayOperatorValue* valueOperatorIn):
32 BDSArray4DCoords(*arrayIn),
33 indexOperator(indexOperatorIn),
34 valueOperator(valueOperatorIn),
35 returnValue(BDSFieldValue())
36{;}
37
38BDSArray4DCoordsTransformed::~BDSArray4DCoordsTransformed()
39{
40 delete indexOperator;
41 delete valueOperator;
42}
43
45 G4double y,
46 G4double z,
47 G4double t,
48 BDSFieldValue (&localData)[2][2][2][2],
49 G4double& xFrac,
50 G4double& yFrac,
51 G4double& zFrac,
52 G4double& tFrac) const
53{
54 G4double xArrayCoords = ArrayCoordsFromX(x);
55 G4double yArrayCoords = ArrayCoordsFromY(y);
56 G4double zArrayCoords = ArrayCoordsFromZ(z);
57 G4double tArrayCoords = ArrayCoordsFromT(t);
58 auto x1 = (G4int)std::floor(xArrayCoords);
59 auto y1 = (G4int)std::floor(yArrayCoords);
60 auto z1 = (G4int)std::floor(zArrayCoords);
61 auto t1 = (G4int)std::floor(tArrayCoords);
62 xFrac = xArrayCoords - x1;
63 yFrac = yArrayCoords - y1;
64 zFrac = zArrayCoords - z1;
65 tFrac = tArrayCoords - t1;
66
67 std::array<G4int, 2> indexArrX = {x1, x1+1};
68 std::array<G4int, 2> indexArrY = {y1, y1+1};
69 std::array<G4int, 2> indexArrZ = {z1, z1+1};
70 std::array<G4int, 2> indexArrT = {t1, t1+1};
71 G4int indexOriginalX, indexOriginalY, indexOriginalZ, indexOriginalT;
72 G4int indexTransformedX, indexTransformedY, indexTransformedZ, indexTransformedT;
73 for (G4int i = 0; i < 2; i++)
74 {
75 for (G4int j = 0; j < 2; j++)
76 {
77 for (G4int k = 0; k < 2; k++)
78 {
79 for (G4int l = 0; l < 2; l++)
80 {
81 indexOriginalX = indexArrX[i];
82 indexTransformedX = indexOriginalX;
83 indexOriginalY = indexArrY[j];
84 indexTransformedY = indexOriginalY;
85 indexOriginalZ = indexArrZ[k];
86 indexTransformedZ = indexOriginalZ;
87 indexOriginalT = indexArrT[l];
88 indexTransformedT = indexOriginalT;
89 indexOperator->Apply(indexTransformedX, indexTransformedY, indexTransformedZ, indexTransformedT);
90 BDSFieldValue v = GetConst(indexTransformedX, indexTransformedY, indexTransformedZ, indexTransformedT);
91 localData[i][j][k][l] = valueOperator->Apply(v, indexOriginalX, indexOriginalY, indexOriginalZ, indexOriginalT);
92 }
93 }
94 }
95 }
96}
97
99 G4double y,
100 G4double z,
101 G4double t,
102 BDSFieldValue (&localData)[4][4][4][4],
103 G4double& xFrac,
104 G4double& yFrac,
105 G4double& zFrac,
106 G4double& tFrac) const
107{
108 G4double xArrayCoords, yArrayCoords, zArrayCoords, tArrayCoords;
109 ArrayCoordsFromXYZT(x, xArrayCoords, y, yArrayCoords, z, zArrayCoords, t, tArrayCoords);
110 auto x1 = (G4int)std::floor(xArrayCoords);
111 auto y1 = (G4int)std::floor(yArrayCoords);
112 auto z1 = (G4int)std::floor(zArrayCoords);
113 auto t1 = (G4int)std::floor(tArrayCoords);
114 xFrac = xArrayCoords - x1;
115 yFrac = yArrayCoords - y1;
116 zFrac = zArrayCoords - z1;
117 tFrac = tArrayCoords - t1;
118
119 std::array<G4int, 4> indexArrX = {x1-1, x1, x1+1, x1+2};
120 std::array<G4int, 4> indexArrY = {y1-1, y1, y1+1, y1+2};
121 std::array<G4int, 4> indexArrZ = {z1-1, z1, z1+1, z1+2};
122 std::array<G4int, 4> indexArrT = {t1-1, t1, t1+1, t1+2};
123 G4int indexOriginalX, indexOriginalY, indexOriginalZ, indexOriginalT;
124 G4int indexTransformedX, indexTransformedY, indexTransformedZ, indexTransformedT;
125 for (G4int i = 0; i < 4; i++)
126 {
127 for (G4int j = 0; j < 4; j++)
128 {
129 for (G4int k = 0; k < 4; k++)
130 {
131 for (G4int l = 0; l < 4; l++)
132 {
133 indexOriginalX = indexArrX[i];
134 indexTransformedX = indexOriginalX;
135 indexOriginalY = indexArrY[j];
136 indexTransformedY = indexOriginalY;
137 indexOriginalZ = indexArrZ[k];
138 indexTransformedZ = indexOriginalZ;
139 indexOriginalT = indexArrT[l];
140 indexTransformedT = indexOriginalT;
141 indexOperator->Apply(indexTransformedX, indexTransformedY, indexTransformedZ, indexTransformedT);
142 BDSFieldValue v = GetConst(indexTransformedX, indexTransformedY, indexTransformedZ, indexTransformedT);
143 localData[i][j][k][l] = valueOperator->Apply(v, indexOriginalX, indexOriginalY, indexOriginalZ, indexOriginalT);
144 }
145 }
146 }
147 }
148}
149
151 G4double y,
152 G4double z,
153 G4double t) const
154{
155 G4int indexOriginalX = NearestX(x);
156 G4int indexOriginalY = NearestY(y);
157 G4int indexOriginalZ = NearestY(z);
158 G4int indexOriginalT = NearestY(t);
159 G4int indexTransformedX = indexOriginalX;
160 G4int indexTransformedY = indexOriginalY;
161 G4int indexTransformedZ = indexOriginalZ;
162 G4int indexTransformedT = indexOriginalT;
163 indexOperator->Apply(indexTransformedX, indexTransformedY, indexTransformedZ, indexTransformedT);
164 BDSFieldValue v = GetConst(indexTransformedX, indexTransformedY, indexTransformedT, indexTransformedZ);
165 v = valueOperator->Apply(v, indexOriginalX, indexOriginalY, indexOriginalZ, indexOriginalT);
166 return v;
167}
168
169std::ostream& BDSArray4DCoordsTransformed::Print(std::ostream& out) const
170{
171 out << "Spatial limits are the original ones" << G4endl;
172 out << "Array index operator: " << indexOperator->Name() << G4endl;
173 out << "Array value operator: " << valueOperator->Name() << G4endl;
174 return BDSArray4DCoords::Print(out);
175}
176
177std::ostream& operator<< (std::ostream& out, BDSArray4DCoordsTransformed const &a)
178{
179 return a.Print(out);
180}
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:45
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.