@@ -75,20 +75,35 @@ void MagneticField::ProcessField() {
7575int MagneticField::FindRadialIndex (const double r) const {
7676 int radialIndex = 0 ;
7777 // given r find the owning radial index
78- const std::vector<double >::const_iterator it_r = std::lower_bound (r_point.begin (),
78+ const std::vector<double >::const_iterator it_r = std::upper_bound (r_point.begin (),
7979 r_point.end (), r);
80- radialIndex = it_r - r_point.begin ();
80+
81+ // if we are pointing outside return the last element
82+ if (it_r - r_point.begin () > r_point.size () - 1 )
83+ radialIndex = r_point.size () - 1 ;
84+ else if (it_r - r_point.begin () <= 0 )
85+ radialIndex = 0 ;
86+ else
87+ radialIndex = it_r - r_point.begin ();
8188
8289 return radialIndex;
8390}
8491
8592// given the vertical position return its index
8693int MagneticField::FindVerticalIndex (const double z) const {
8794 int verticalIndex = 0 ;
95+
8896 // given z find the owning vertical index
8997 std::vector<double >::const_iterator it_z = std::lower_bound (z_point.begin (),
9098 z_point.end (), z);
91- verticalIndex = it_z - z_point.begin ();
99+
100+ // if we are pointing outside return the last element
101+ if (it_z - z_point.begin () > z_point.size () - 1 )
102+ verticalIndex = z_point.size () - 1 ;
103+ else if (it_z - z_point.begin () <= 0 )
104+ verticalIndex = 0 ;
105+ else
106+ verticalIndex = it_z - z_point.begin ();
92107
93108 return verticalIndex;
94109}
@@ -178,7 +193,7 @@ std::array<std::array<double,4>, 3> MagneticField::GetFourFieldValuesByIndex(con
178193
179194 // order of values needs to be: p[0,0], p[0,1], p[1,0], p[1,1];
180195 for ( int dir = 0 ; dir < 3 ; dir++ ) {
181- int index = ( r_index- 1 ) *n_z + z_index- 1 ; // q11
196+ int index = r_index*n_z + z_index; // q11
182197 values[dir][0 ] = b_field[index][dir];
183198 index = (r_index-1 )*n_z + z_index; // q12
184199 values[dir][1 ] = b_field[index][dir];
0 commit comments