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) 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 "BDSArray3DCoordsTransformed.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 BDSArray3DCoords(*arrayIn),
33 indexOperator(indexOperatorIn),
34 valueOperator(valueOperatorIn),
35 returnValue(BDSFieldValue())
36{;}
37
38BDSArray3DCoordsTransformed::~BDSArray3DCoordsTransformed()
39{
40 delete indexOperator;
41 delete valueOperator;
42}
43
45 G4double y,
46 G4double z,
47 BDSFieldValue (&localData)[2][2][2],
48 G4double& xFrac,
49 G4double& yFrac,
50 G4double& zFrac) const
51{
52 G4double xArrayCoords, yArrayCoords, zArrayCoords;
53 ArrayCoordsFromXYZ(x, xArrayCoords, y, yArrayCoords, z, zArrayCoords);
54 auto x1 = (G4int)std::floor(xArrayCoords);
55 auto y1 = (G4int)std::floor(yArrayCoords);
56 auto z1 = (G4int)std::floor(zArrayCoords);
57 xFrac = xArrayCoords - x1;
58 yFrac = yArrayCoords - y1;
59 zFrac = zArrayCoords - z1;
60
61 std::array<G4int, 2> indexArrX = {x1, x1+1};
62 std::array<G4int, 2> indexArrY = {y1, y1+1};
63 std::array<G4int, 2> indexArrZ = {z1, z1+1};
64 G4int indexOriginalX, indexOriginalY, indexOriginalZ, indexTransformedX, indexTransformedY, indexTransformedZ;
65 for (G4int i = 0; i < 2; i++)
66 {
67 for (G4int j = 0; j < 2; j++)
68 {
69 for (G4int k = 0; k < 2; k++)
70 {
71 indexOriginalX = indexArrX[i];
72 indexTransformedX = indexOriginalX;
73 indexOriginalY = indexArrY[j];
74 indexTransformedY = indexOriginalY;
75 indexOriginalZ = indexArrZ[k];
76 indexTransformedZ = indexOriginalZ;
77 indexOperator->Apply(indexTransformedX, indexTransformedY, indexTransformedZ);
78 BDSFieldValue v = GetConst(indexTransformedX, indexTransformedY, indexTransformedZ);
79 localData[i][j][k] = valueOperator->Apply(v, indexOriginalX, indexOriginalY, indexOriginalZ);
80 }
81 }
82 }
83}
84
86 G4double y,
87 G4double z,
88 BDSFieldValue (&localData)[4][4][4],
89 G4double& xFrac,
90 G4double& yFrac,
91 G4double& zFrac) const
92{
93 G4double xArrayCoords = ArrayCoordsFromX(x);
94 G4double yArrayCoords = ArrayCoordsFromY(y);
95 G4double zArrayCoords = ArrayCoordsFromZ(z);
96 auto x1 = (G4int)std::floor(xArrayCoords);
97 auto y1 = (G4int)std::floor(yArrayCoords);
98 auto z1 = (G4int)std::floor(zArrayCoords);
99 xFrac = xArrayCoords - x1;
100 yFrac = yArrayCoords - y1;
101 zFrac = zArrayCoords - z1;
102
103 std::array<G4int, 4> indexArrX = {x1-1, x1, x1+1, x1+2};
104 std::array<G4int, 4> indexArrY = {y1-1, y1, y1+1, y1+2};
105 std::array<G4int, 4> indexArrZ = {z1-1, z1, z1+1, z1+2};
106 G4int indexOriginalX, indexOriginalY, indexOriginalZ, indexTransformedX, indexTransformedY, indexTransformedZ;
107 for (G4int i = 0; i < 4; i++)
108 {
109 for (G4int j = 0; j < 4; j++)
110 {
111 for (G4int k = 0; k < 4; k++)
112 {
113 indexOriginalX = indexArrX[i];
114 indexTransformedX = indexOriginalX;
115 indexOriginalY = indexArrY[j];
116 indexTransformedY = indexOriginalY;
117 indexOriginalZ = indexArrZ[k];
118 indexTransformedZ = indexOriginalZ;
119 indexOperator->Apply(indexTransformedX, indexTransformedY, indexTransformedZ);
120 BDSFieldValue v = GetConst(indexTransformedX, indexTransformedY, indexTransformedZ);
121 localData[i][j][k] = valueOperator->Apply(v, indexOriginalX, indexOriginalY, indexOriginalZ);
122 }
123 }
124 }
125}
126
128 G4double y,
129 G4double z,
130 G4double /*t*/) const
131{
132 G4int indexOriginalX = NearestX(x);
133 G4int indexOriginalY = NearestY(y);
134 G4int indexOriginalZ = NearestZ(z);
135 G4int indexTransformedX = indexOriginalX;
136 G4int indexTransformedY = indexOriginalY;
137 G4int indexTransformedZ = indexOriginalZ;
138 indexOperator->Apply(indexTransformedX, indexTransformedY, indexTransformedZ);
139 BDSFieldValue v = GetConst(indexTransformedX, indexTransformedY, indexTransformedZ);
140 v = valueOperator->Apply(v, indexOriginalX, indexOriginalY, indexOriginalZ);
141 return v;
142}
143
144std::ostream& BDSArray3DCoordsTransformed::Print(std::ostream& out) const
145{
146 out << "Spatial limits are the original ones" << G4endl;
147 out << "Array index operator: " << indexOperator->Name() << G4endl;
148 out << "Array value operator: " << valueOperator->Name() << G4endl;
149 return BDSArray3DCoords::Print(out);
150}
151
152std::ostream& operator<< (std::ostream& out, BDSArray3DCoordsTransformed const &a)
153{
154 return a.Print(out);
155}
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:45
virtual std::ostream & Print(std::ostream &out) const
Definition BDSArray4D.cc:90
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.