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) 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 "BDSArray2DCoordsTransformed.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 BDSArray2DCoords(*arrayIn),
34 indexOperator(indexOperatorIn),
35 valueOperator(valueOperatorIn),
36 returnValue(BDSFieldValue())
37{;}
38
39BDSArray2DCoordsTransformed::~BDSArray2DCoordsTransformed()
40{
41 delete indexOperator;
42 delete valueOperator;
43}
44
46 G4double y,
47 BDSFieldValue (&localData)[2][2],
48 G4double& xFrac,
49 G4double& yFrac) const
50{
51 G4double xArrayCoords, yArrayCoords;
52 ArrayCoordsFromXY(x, xArrayCoords, y, yArrayCoords);
53 auto x1 = (G4int)std::floor(xArrayCoords);
54 auto y1 = (G4int)std::floor(yArrayCoords);
55 xFrac = xArrayCoords - x1;
56 yFrac = yArrayCoords - y1;
57
58 std::array<G4int, 2> indexArrX = {x1, x1+1};
59 std::array<G4int, 2> indexArrY = {y1, y1+1};
60 G4int indexOriginalX, indexOriginalY, indexTransformedX, indexTransformedY;
61 for (G4int i = 0; i < 2; i++)
62 {
63 for (G4int j = 0; j < 2; j++)
64 {
65 indexOriginalX = indexArrX[i];
66 indexTransformedX = indexOriginalX;
67 indexOriginalY = indexArrY[j];
68 indexTransformedY = indexOriginalY;
69 indexOperator->Apply(indexTransformedX, indexTransformedY);
70 BDSFieldValue v = GetConst(indexTransformedX, indexTransformedY);
71 localData[i][j] = valueOperator->Apply(v, indexOriginalX, indexOriginalY);
72 }
73 }
74}
75
77 G4double y,
78 BDSFieldValue (&localData)[4][4],
79 G4double& xFrac,
80 G4double& yFrac) const
81{
82 G4double xArrayCoords = ArrayCoordsFromX(x);
83 G4double yArrayCoords = ArrayCoordsFromY(y);
84 auto x1 = (G4int)std::floor(xArrayCoords);
85 auto y1 = (G4int)std::floor(yArrayCoords);
86 xFrac = xArrayCoords - x1;
87 yFrac = yArrayCoords - y1;
88
89 std::array<G4int, 4> indexArrX = {x1-1, x1, x1+1, x1+2};
90 std::array<G4int, 4> indexArrY = {y1-1, y1, y1+1, y1+2};
91 G4int indexOriginalX, indexOriginalY, indexTransformedX, indexTransformedY;
92 for (G4int i = 0; i < 4; i++)
93 {
94 for (G4int j = 0; j < 4; j++)
95 {
96 indexOriginalX = indexArrX[i];
97 indexTransformedX = indexOriginalX;
98 indexOriginalY = indexArrY[j];
99 indexTransformedY = indexOriginalY;
100 indexOperator->Apply(indexTransformedX, indexTransformedY);
101 BDSFieldValue v = GetConst(indexTransformedX, indexTransformedY);
102 localData[i][j] = valueOperator->Apply(v, indexOriginalX, indexOriginalY);
103 }
104 }
105}
106
108 G4double y,
109 G4double /*z*/,
110 G4double /*t*/) const
111{
112 G4int indexOriginalX = NearestX(x);
113 G4int indexOriginalY = NearestY(y);
114 G4int indexTransformedX = indexOriginalX;
115 G4int indexTransformedY = indexOriginalY;
116 indexOperator->Apply(indexTransformedX, indexTransformedY);
117 BDSFieldValue v = GetConst(indexTransformedX, indexTransformedY);
118 v = valueOperator->Apply(v, indexOriginalX, indexOriginalY);
119 return v;
120}
121
122std::ostream& BDSArray2DCoordsTransformed::Print(std::ostream& out) const
123{
124 out << "Spatial limits are the original ones" << G4endl;
125 out << "Array index operator: " << indexOperator->Name() << G4endl;
126 out << "Array value operator: " << valueOperator->Name() << G4endl;
127 return BDSArray2DCoords::Print(out);
128}
129
130std::ostream& operator<< (std::ostream& out, BDSArray2DCoordsTransformed const &a)
131{
132 return a.Print(out);
133}
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: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.