00001 #include "MagneticField/Interpolation/src/SpecialCylindricalMFGrid.h"
00002 #include "MagneticField/Interpolation/src/binary_ifstream.h"
00003 #include "MagneticField/Interpolation/src/LinearGridInterpolator3D.h"
00004
00005
00006 #include <iostream>
00007
00008 using namespace std;
00009
00010 SpecialCylindricalMFGrid::SpecialCylindricalMFGrid( binary_ifstream& inFile,
00011 const GloballyPositioned<float>& vol, int gridType)
00012 : MFGrid3D(vol)
00013 {
00014 if (gridType == 5 ) {
00015 sector1 = false;
00016 } else if (gridType == 6 ) {
00017 sector1 = true;
00018 } else {
00019 cout << "ERROR wrong SpecialCylindricalMFGrid type " << gridType << endl;
00020 sector1 = false;
00021 }
00022
00023 int n1, n2, n3;
00024 inFile >> n1 >> n2 >> n3;
00025 #ifdef DEBUG_GRID
00026 cout << "n1 " << n1 << " n2 " << n2 << " n3 " << n3 << endl;
00027 #endif
00028 double xref, yref, zref;
00029 inFile >> xref >> yref >> zref;
00030 double stepx, stepy, stepz;
00031 inFile >> stepx >> stepy >> stepz;
00032
00033 double RParAsFunOfPhi[4];
00034 inFile >> RParAsFunOfPhi[0] >> RParAsFunOfPhi[1] >> RParAsFunOfPhi[2] >> RParAsFunOfPhi[3];
00035
00036 vector<BVector> fieldValues;
00037 float Bx, By, Bz;
00038 int nLines = n1*n2*n3;
00039 for (int iLine=0; iLine<nLines; ++iLine){
00040 inFile >> Bx >> By >> Bz;
00041
00042
00043
00044
00045
00046
00047 Vector3DBase<double, LocalTag> lB = frame().toLocal(Vector3DBase<double, GlobalTag>(Bx,By,Bz));
00048 fieldValues.push_back(BVector(lB.x(), lB.y(), lB.z()));
00049 }
00050
00051 string lastEntry;
00052 inFile >> lastEntry;
00053 if (lastEntry != "complete"){
00054 cout << "ERROR during file reading: file is not complete" << endl;
00055 }
00056
00057 GlobalPoint grefp( GlobalPoint::Cylindrical( xref, yref, zref));
00058
00059 #ifdef DEBUG_GRID
00060 LocalPoint lrefp = frame().toLocal( grefp);
00061 cout << "Grid reference point in grid system: " << xref << "," << yref << "," << zref << endl;
00062 cout << "Grid reference point in global x,y,z: " << grefp << endl;
00063 cout << "Grid reference point in local x,y,z: " << lrefp << endl;
00064 cout << "steps " << stepx << "," << stepy << "," << stepz << endl;
00065 cout << "RParAsFunOfPhi[0...4] = ";
00066 for (int i=0; i<4; ++i) cout << RParAsFunOfPhi[i] << " "; cout << endl;
00067 #endif
00068
00069 Grid1D<double> gridX( 0, n1-1, n1);
00070 Grid1D<double> gridY( yref, yref + stepy*(n2-1), n2);
00071 Grid1D<double> gridZ( grefp.z(), grefp.z() + stepz*(n3-1), n3);
00072
00073 grid_ = GridType( gridX, gridY, gridZ, fieldValues);
00074
00075 stepConstTerm_ = (RParAsFunOfPhi[0] - RParAsFunOfPhi[2]) / (n1-1);
00076 stepPhiTerm_ = (RParAsFunOfPhi[1] - RParAsFunOfPhi[3]) / (n1-1);
00077 startConstTerm_ = RParAsFunOfPhi[2];
00078 startPhiTerm_ = RParAsFunOfPhi[3];
00079
00080
00081
00082
00083 }
00084
00085
00086 MFGrid::LocalVector SpecialCylindricalMFGrid::uncheckedValueInTesla( const LocalPoint& p) const
00087 {
00088
00089
00090
00091 LinearGridInterpolator3D<GridType::ValueType, GridType::Scalar> interpol( grid_);
00092 double a, b, c;
00093 toGridFrame( p, a, b, c);
00094
00095
00096
00097 return LocalVector(interpol.interpolate( a, b, c));
00098 }
00099
00100 void SpecialCylindricalMFGrid::dump() const {}
00101
00102
00103 MFGrid::LocalPoint SpecialCylindricalMFGrid::fromGridFrame( double a, double b, double c) const
00104 {
00105 double sinPhi;
00106 if (sector1) {
00107 sinPhi = cos(b);
00108 } else {
00109 sinPhi = sin(b);
00110 }
00111
00112 double R = a*stepSize(sinPhi) + startingPoint(sinPhi);
00113
00114
00115 GlobalPoint gp( GlobalPoint::Cylindrical(R, b, c));
00116 return frame().toLocal(gp);
00117 }
00118
00119 void SpecialCylindricalMFGrid::toGridFrame( const LocalPoint& p,
00120 double& a, double& b, double& c) const
00121 {
00122 GlobalPoint gp = frame().toGlobal(p);
00123 double sinPhi;
00124 if (sector1) {
00125 sinPhi = cos(gp.phi());
00126 } else {
00127 sinPhi = sin(gp.phi());
00128 }
00129 a = (gp.perp()-startingPoint(sinPhi))/stepSize(sinPhi);
00130
00131
00132 b = gp.phi();
00133 c = gp.z();
00134
00135 #ifdef DEBUG_GRID
00136 if (sector1) {
00137 cout << "toGridFrame: sinPhi " ;
00138 } else {
00139 cout << "toGridFrame: cosPhi " ;
00140 }
00141 cout << sinPhi << " LocalPoint " << p
00142 << " GlobalPoint " << gp << endl
00143 << " a " << a << " b " << b << " c " << c << endl;
00144
00145 #endif
00146 }
00147