BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
Loading...
Searching...
No Matches
BDSArray2DCoordsTransformed.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 "BDSArray2DCoordsTransformed.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 BDSArray2DCoords(*arrayIn),
33 indexOperator(indexOperatorIn),
34 valueOperator(valueOperatorIn),
35 returnValue(BDSFieldValue())
36{;}
37
38BDSArray2DCoordsTransformed::~BDSArray2DCoordsTransformed()
39{
40 delete indexOperator;
41 delete valueOperator;
42}
43
45 G4double y,
46 BDSFieldValue (&localData)[2][2],
47 G4double& xFrac,
48 G4double& yFrac) const
49{
50 G4double xArrayCoords, yArrayCoords;
51 ArrayCoordsFromXY(x, xArrayCoords, y, yArrayCoords);
52 auto x1 = (G4int)std::floor(xArrayCoords);
53 auto y1 = (G4int)std::floor(yArrayCoords);
54 xFrac = xArrayCoords - x1;
55 yFrac = yArrayCoords - y1;
56
57 std::array<G4int, 2> indexArrX = {x1, x1+1};
58 std::array<G4int, 2> indexArrY = {y1, y1+1};
59 G4int indexOriginalX, indexOriginalY, indexTransformedX, indexTransformedY;
60 for (G4int i = 0; i < 2; i++)
61 {
62 for (G4int j = 0; j < 2; j++)
63 {
64 indexOriginalX = indexArrX[i];
65 indexTransformedX = indexOriginalX;
66 indexOriginalY = indexArrY[j];
67 indexTransformedY = indexOriginalY;
68 indexOperator->Apply(indexTransformedX, indexTransformedY);
69 BDSFieldValue v = GetConst(indexTransformedX, indexTransformedY);
70 localData[i][j] = valueOperator->Apply(v, indexOriginalX, indexOriginalY);
71 }
72 }
73}
74
76 G4double y,
77 BDSFieldValue (&localData)[4][4],
78 G4double& xFrac,
79 G4double& yFrac) const
80{
81 G4double xArrayCoords = ArrayCoordsFromX(x);
82 G4double yArrayCoords = ArrayCoordsFromY(y);
83 auto x1 = (G4int)std::floor(xArrayCoords);
84 auto y1 = (G4int)std::floor(yArrayCoords);
85 xFrac = xArrayCoords - x1;
86 yFrac = yArrayCoords - y1;
87
88 std::array<G4int, 4> indexArrX = {x1-1, x1, x1+1, x1+2};
89 std::array<G4int, 4> indexArrY = {y1-1, y1, y1+1, y1+2};
90 G4int indexOriginalX, indexOriginalY, indexTransformedX, indexTransformedY;
91 for (G4int i = 0; i < 4; i++)
92 {
93 for (G4int j = 0; j < 4; j++)
94 {
95 indexOriginalX = indexArrX[i];
96 indexTransformedX = indexOriginalX;
97 indexOriginalY = indexArrY[j];
98 indexTransformedY = indexOriginalY;
99 indexOperator->Apply(indexTransformedX, indexTransformedY);
100 BDSFieldValue v = GetConst(indexTransformedX, indexTransformedY);
101 localData[i][j] = valueOperator->Apply(v, indexOriginalX, indexOriginalY);
102 }
103 }
104}
105
107 G4double y,
108 G4double /*z*/,
109 G4double /*t*/) const
110{
111 G4int indexOriginalX = NearestX(x);
112 G4int indexOriginalY = NearestY(y);
113 G4int indexTransformedX = indexOriginalX;
114 G4int indexTransformedY = indexOriginalY;
115 indexOperator->Apply(indexTransformedX, indexTransformedY);
116 BDSFieldValue v = GetConst(indexTransformedX, indexTransformedY);
117 v = valueOperator->Apply(v, indexOriginalX, indexOriginalY);
118 return v;
119}
120
121std::ostream& BDSArray2DCoordsTransformed::Print(std::ostream& out) const
122{
123 out << "Spatial limits are the original ones" << G4endl;
124 out << "Array index operator: " << indexOperator->Name() << G4endl;
125 out << "Array value operator: " << valueOperator->Name() << G4endl;
126 return BDSArray2DCoords::Print(out);
127}
128
129std::ostream& operator<< (std::ostream& out, BDSArray2DCoordsTransformed const &a)
130{
131 return a.Print(out);
132}
Wrapped BDSArray2DCoords with possible transformation to extend field.
virtual std::ostream & Print(std::ostream &out) const
BDSArray2DCoordsTransformed()=delete
No default constructor.
virtual BDSFieldValue ExtractNearest(G4double x, G4double y=0, G4double z=0, G4double t=0) const
Extract nearest field value from array. z,t ignored but required for overload.
virtual void ExtractSection2x2(G4double x, G4double y, BDSFieldValue(&localData)[2][2], G4double &xFrac, G4double &yFrac) const
virtual void ExtractSection4x4(G4double x, G4double y, BDSFieldValue(&localData)[4][4], G4double &xFrac, G4double &yFrac) const
2D array with spatial mapping derived from BDSArray4DCoords.
void ArrayCoordsFromXY(G4double &x, G4double &xArr, G4double &y, G4double &yArr) const
Utility version to forward to individual function.
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 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 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.