EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Mgrid.h
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file Mgrid.h
1 /* -------------------------------------------------------------------------- */
2 /* Mgrid.h */
3 /* */
4 /* A.Kisselev, PNPI, St.Petersburg, Russia. */
5 /* e-mail: kisselev@hermes.desy.de */
6 /* -------------------------------------------------------------------------- */
7 
8 #include <cstdio>
9 
10 #ifndef _MGRID_H
11 #define _MGRID_H
12 
13 #include <MgridHeader.h>
14 #include <MgridInterpolation.h>
15 #include <Mgrid.h>
16 #include <ThreeDeePolySpace.h>
17 
18 // Accomplish existing _X_,_Y_,_Z_ set;
19 #define _R_ 0
20 #define _F_ 1
21 
23  public:
24  // Just marks the necessity to apply shift
25  // and/or rotation (save CPU if not needed);
27 
28  // 'direct' is for input coordinates; 'reversed' for the output field;
29  TRotation direct, reversed;
30 } ;
31 
33  // _CARTESIAN_/_CYLINDRICAL_;
34  unsigned char system_type;
35  // 3D vector in this coordinate system;
36  TVector3 xx;
37 } ;
38 
39 #define _MODE_OFF_ 0
40 #define _MODE_HRC_ 1
41 #define _MODE_ADIM_ 2
42 
43 typedef struct {
44  int mode, adim[3];
46 
47 class Mgrid: public MgridHeader {
48  friend class MgridInterpolation;
49 
50  //
51  // NB: it is critically important that 1) MgridHeader size and format
52  // do not change (except for the reserved[] bytes contents); 2) MgridHeader
53  // is the very first data block in Mgrid class; otherwise at least I/O
54  // routines will become confused;
55  //
56  // Variables below may follow in any order, they are dynamically
57  // allocated & filled;
58  //
59 
60  public:
61  // Basically memory allocation and creation of header;
62  Mgrid(char *_name, MgridType _type);
63  // Direct import from binary file;
64  Mgrid(FILE *fin);
65 
66  // Interpolation setting routines;
68  int setInterpolationMode(unsigned mode, int adim[]);
71  int setFittingMode(unsigned mode, int adim[], int pdim[]);
72 
73  // Interpolation toggling routines;
74  int switchInterpolation(char field_component_name, MgridInterpolation *inter);
76 
77  // Binary mgrid file export routine;
78  int exportFieldMap(char *file_name, unsigned char contents);
79 
80  // Used in import.cc, sorry;
81  int attachToHeap(Mgrid *heap);
82 
83  // Used in my user code;
84  int initializeAsHeap();
85  int initializeAsSingleMgrid(int _ram_cell_size);
86 
87  unsigned getCellNum() { return cell_num;};
88 
89  void markCellAsSafe(unsigned id) {properties[id] = _SAFE_CELL_;};
90 
91  // A field value of a grid which has no further childred;
92  private:
93  int directFieldValue(TVector3 &xx, TVector3 &B);
94 
95  // Internal interpolation creation methods;
96  int setModeWrapper(unsigned mode, int adim[], int pdim[], char sequence[]);
98  MgridInterpolation *cookSequentialFastInterpolation(int _adim[], char _sequence[]);
99  MgridInterpolation *cookSequentialHighInterpolation(int _adim[], char _sequence[]);
100  MgridInterpolation *cookSequentialHighFitting(int _adim[], int _pdim[],
101  char _sequence[]);
103  MgridInterpolation *cookMultiDimFitting(int _adim[], int _pdim[]);
104  MgridInterpolation *cookInterpolation(unsigned mode, int _adim[] = 0,
105  int _pdim[] = 0, char _sequence[] = 0);
106 
107  // Address calculation methods;
108  int linearAddrToMulti(int linear, int id[3]);
109  MgridCell *multiAddrToCellPtr(int id[3]);
110  int multiAddrToLinear(int id[3]);
111  int multiAddrToCombiCell(int id[], MgridCombiCell *qcell);
112  public:
113  // Ok, these two are used in my codes;
114  int cellPtrToLinear(const MgridCell *cell) const;
115  MgridCell *coordToCellPtr(double xx[]);
116  MgridCell *linearAddrToCellPtr(int linear);
117  int coordToMultiAddr(TVector3 &xx, int id[]);
118  int coordToCombiCell(TVector3 &xx, MgridCombiCell *qcell);
119  MgridCell *coordToCellPtr(TVector3 &xx);
120  int linearAddrToCoord(int linear, TVector3 &xx);
121  int multiAddrToCoord(int id[], TVector3 &xx);
122  // And this call is used in import.cc;
123  int compressedCoordToLinearAddr(TVector3 &xx);
124  int cellPtrToCoord(MgridCell *cell, TVector3 &xx);
125 
126  private:
127  // Well, this method should become public once I need it in user code;
128  int changePosition(double shift[3], double theta[3], double phi[3]);
129 
130  int recalculatePosition();
131 
132  // Field value calculation routines;
133  public:
134  int getCylindricalFieldValue(TVector3 &xx, TVector3 &B);
135  int getCartesianFieldValue(TVector3 &xx, TVector3 &B);
136  private:
138 
139  private:
140  int findSelfCell(TVector3 &xx);
141 
143 
144  int exportCore(FILE *fout, unsigned char contents);
145 
146  int getFixedTypeFieldValue(unsigned char system_type, TVector3 &xx, TVector3 &B);
147 
149 
151  unsigned char wanted_field_coord_system_type);
152 
153  // 'Useful' cell size in bytes; depends on the number
154  // of field components and on whether the error info is included;
155  // 'ram_cell_size' (what is actually stored in 'cells')
156  // may be bigger since it may contain extra pointers and
157  // intermediate data;
161 
162  // It is convenient to keep this variable in the same structure;
163  // it defines scaling factor applied to the calculated field value;
165 
166  // Make import.cc happy; fix later;
167  public:
168  // --- This stuff is used if this object ---
169  // --- is a collection of daughter grids; ---
171 
172  // --- This stuff is used if this object is a single grid; ---
173 
174  private:
175  // Expansion (mapping to 3dim) arrays for coordinates and field;
176  int *cexp, *fexp;
177 
178  // Total dimension of the 'cells' array;
179  int cell_num;
180  // Actual size of cells stored in 'cells' array; may be bigger than
181  // mgrid->useful_cell_size; since grids may be expanded/squeezed
182  // dynamically (say during regriding, may want to store last actual
183  // 'ram_cell_size' as 'last_cell_size' in order to restore it later;
184  // of course this mechanism will work for an ordered change/restore
185  // sequence (and not for say change/change/restore;
187  // 1-dim array of grid cells; want a linear addressing
188  // because prefer to use a piece of memory directly (in
189  // the future share maps via HSM or a similar mechanism);
190  // t_mgrid_cell will be cutted down appropriately => makes
191  // no sense to define this as a t_mgrid_cell* array;
192  unsigned char *cells, *properties;
193 
194  // Coordinates of cell centers of cube edges; '[3]': 3 dimensions
195  // at most; ordering is in a natural XYZ (RFZ) sequence;
196  double *cell_center_coord[3];
197 
198  // Frame will be calculated out of mgrid->shift/theta/phi; if object
199  // needs to be positioned in a mother volume, this pointer will be
200  // non-zero --> need to convert input coordinates and output field;
202 
203  // Will default to the simple interpolation upon downloading;
205 
206  // 3dim index of cell containing xx[] point in grid array;
207  int self[3];
209 
210  // This is an additional return value; bitwise pattern
211  // describing interpolation result; reset to 0 and then
212  // calling subroutines will |= possible problems;
213  // lower 4 bits: self cell property; then 4 bits for the
214  // logical OR of the USED neighbour cells; then see the
215  // beginning of mvalue.c for other bits;
217 
218  // Make Runge-Kutta routines happy; fix later;
219  public:
220  // For MINUIT-like applications it is desirable to avoid any jumps
221  // in calculated values => it is better to keep cell structure used
222  // for fitting unchanged even if (by small xx[] shift inter->self cell
223  // actually changes; it is not completely fine (better to guarantee
224  // field differentiability by all conditions), but should work well;
226 } ;
227 
228 typedef double (*conversionfun)(double xx);
229 
231  // _CARTESIAN_/_CYLINDRICAL_ for now;
232  unsigned char system_type;
233 
234  // Number of coordinates;
236 
237  // 'X', 'Y', 'Z', 'F', 'R', should be present
238  // in coord_names[] for this 'system_type';
239  char coord_names[3];
240 
241  // Function converting to 'cm' or 'kGs';
243 } ;
244 
245 #ifdef __cplusplus
246 extern "C" {
247 #endif
248  // User code external functions to create/import mgrids;
249  Mgrid *import_field_map(char *file_name);
250 
252  CoordSystem *field, MgridDirection *dir[3],
253  unsigned cell_contents_bits);
254  Mgrid *create_mgrid_heap_header(char *name, int field_calculation_method);
255 
256  Mgrid *import_ascii_field_map(char *file_name, char *mgrid_name, t_ascii_coord *coord,
257  t_ascii_coord *field, int lines_to_skip);
258 
259  // Uncomment back if this routine is ever needed;
260  //void release_cell_list_ram(MgridCell *cells);
261 
262  // This is indeed a service routine;
263  int basic_types_match( void );
264 #ifdef __cplusplus
265 }
266 #endif
267 
268 #endif