NAMD
GridForceGrid.h
Go to the documentation of this file.
1 
7 #ifndef GRIDFORCEGRID_H
8 #define GRIDFORCEGRID_H
9 
10 #include <set>
11 
12 #include "Vector.h"
13 #include "Tensor.h"
14 #include "SimParameters.h"
15 #include "NamdTypes.h"
16 #include "MStream.h"
17 //#include "ComputeGridForce.h"
18 
19 #include "MGridforceParams.h"
20 
21 
24 
25 
26 // GridforceGrid is now an abstract class to act as an interface to both GridforceFullMainGrid's and GridforceLiteGrid's
28 public:
29  static GridforceGrid * new_grid(int gridnum, char *potfilename, SimParameters *simParams, MGridforceParams *mgridParams);
30  virtual ~GridforceGrid();
31 
32  virtual void initialize(char *potfilename, SimParameters *simParams, MGridforceParams *mgridParams) = 0;
33  virtual void reinitialize(SimParameters *simParams, MGridforceParams *mgridParams) = 0;
34 
35  virtual Position get_center(void) const = 0;
36  virtual Position get_origin(void) const = 0;
37  virtual Tensor get_e (void) const = 0;
38  virtual Tensor get_inv(void) const = 0;
39  virtual Vector get_scale(void) const = 0;
40  virtual Bool get_checksize(void) const = 0;
41  virtual int get_k0(void) const = 0;
42  virtual int get_k1(void) const = 0;
43  virtual int get_k2(void) const = 0;
44  virtual int get_total_grids(void) const = 0;
45 
46  virtual long int get_all_gridvals(float** all_gridvals) const = 0;
47  virtual void set_all_gridvals(float* all_gridvals, long int sz) = 0;
48  virtual void set_scale(Vector s) = 0;
49 
50  Position wrap_position(const Position &pos, const Lattice &lattice);
51  bool fits_lattice(const Lattice &lattice);
52 
53  inline int compute_VdV(Position pos, float &V, Vector &dV) const { return -1; }
54 
55  static void pack_grid(GridforceGrid *grid, MOStream *msg);
56  static GridforceGrid * unpack_grid(int gridnum, MIStream *msg);
57 
58  typedef enum {
63 
64  inline GridforceGridType get_grid_type(void) { return type; }
65 
66 protected:
67  virtual void pack(MOStream *msg) const = 0;
68  virtual void unpack(MIStream *msg) = 0;
69 
70  Position get_corner(int idx);
71 
74  int mygridnum;
75 
76 private:
77  Vector corners[8];
78 };
79 
80 
82  friend class GridforceFullMainGrid;
83  friend class GridforceFullSubGrid;
84 
85 public:
87  virtual ~GridforceFullBaseGrid();
88 
89 // int request_box(Vector pos);
90 // int get_box(Box *box, Vector pos) const;
91 
92  inline Position get_center(void) const { return center; }
93  inline Position get_origin(void) const { return origin; }
94  inline Tensor get_e (void) const { return e; }
95  inline Tensor get_inv(void) const { return inv; }
96  inline Vector get_scale(void) const { return scale; }
97  inline Bool get_checksize(void) const { return checksize; }
98  virtual int get_border(void) const = 0;
99 
100  inline float get_grid(int i0, int i1, int i2) const {
101  return grid[grid_index(i0, i1, i2)];
102  }
103  inline double get_grid_d(int i0, int i1, int i2) const {
104  return double(get_grid(i0, i1, i2));
105  }
106  inline void set_grid(int i0, int i1, int i2, float V) {
107  grid[grid_index(i0, i1, i2)] = V;
108  }
109 
110  inline void set_scale(Vector s) { scale = s; }
111 
112  int compute_VdV(Position pos, float &V, Vector &dV) const;
113 
114  inline int get_k0(void) const { return k[0]; }
115  inline int get_k1(void) const { return k[1]; }
116  inline int get_k2(void) const { return k[2]; }
117 
118 
119  virtual void pack(MOStream *msg) const;
120  virtual void unpack(MIStream *msg);
121 
122  struct GridIndices {
123  int inds2;
124  int dk_hi;
125  int dk_lo;
127  };
128 
129  // Utility functions
130  void readHeader(SimParameters *simParams, MGridforceParams *mgridParams);
131 
132  inline long int grid_index(int i0, int i1, int i2) const {
133  register int inds[3] = {i0, i1, i2};
134 #ifdef DEBUGM
135  if (i0 < 0 || i0 >= k[0] || i1 < 0 || i1 >= k[1] || i2 < 0 || i2 >= k[2]) {
136  char buffer[256];
137  sprintf(buffer, "Bad grid index! (%d %d %d)", i0, i1, i2);
138  NAMD_bug(buffer);
139  }
140 #endif
141  return inds[0]*dk[0] + inds[1]*dk[1] + inds[2]*dk[2];
142  }
143 
144  //virtual int get_inds(Position pos, int *inds, Vector &dg, Vector &gapscale) const = 0;
145  int get_inds(Position pos, int *inds, Vector &dg, Vector &gapscale) const;
146  void compute_a(float *a, float *b) const;
147  virtual void compute_b(float *b, int *inds, Vector gapscale) const = 0;
148  float compute_V(float *a, float *x, float *y, float *z) const;
149  Vector compute_dV(float *a, float *x, float *y, float *z) const;
150  Vector compute_d2V(float *a, float *x, float *y, float *z) const;
151  float compute_d3V(float *a, float *x, float *y, float *z) const;
152 
153  void readSubgridHierarchy(FILE *poten, int &totalGrids);
154 
155  FILE *poten_fp;
156  float *grid; // Actual grid
157 
160  int generation; // Subgrid level (0 = main grid)
161 
162  // should move 'nopad' versions to maingrid only ... or not have them as ivars at all, why are they here?
163  int k[3]; // Grid dimensions
164  int k_nopad[3]; // Grid dimensions
165  long int size;
166  long int size_nopad;
167  long int dk[3];
168  long int dk_nopad[3];
169  float factor;
170 
171  Position origin; // Grid origin
172  Position center; // Center of grid (for wrapping)
173  Tensor e; // Grid unit vectors
174  Tensor inv; // Inverse of unit vectors
175 
176  double p_sum[3]; // Accumulators for sums
177  double n_sum[3];
178  double pad_p[3]; // Pad values (p = positive side, n = negative side) for each dimension
179  double pad_n[3];
180  Bool cont[3]; // Whether grid is continuous in each dimension
181  float offset[3]; // Potential offset in each dimension
182  float gap[3]; // Gap between images of grid in grid units for each dimension
183  float gapinv[3]; // 1.0/gap
184 
187 };
188 
189 
191  friend class GridforceFullBaseGrid;
192  friend class GridforceFullSubGrid;
193 
194 public:
195  explicit GridforceFullMainGrid(int gridnum);
196  virtual ~GridforceFullMainGrid();
197 
198  void initialize(char *potfilename, SimParameters *simParams, MGridforceParams *mgridParams, int border);
199  inline void initialize(char *potfilename, SimParameters *simParams, MGridforceParams *mgridParams) {
200  initialize(potfilename, simParams, mgridParams, default_border);
201  }
203 
204  inline Position get_center(void) const { return GridforceFullBaseGrid::get_center(); };
205  inline Position get_origin(void) const { return GridforceFullBaseGrid::get_origin(); };
206  inline Tensor get_e (void) const { return GridforceFullBaseGrid::get_e(); };
207  inline Tensor get_inv(void) const { return GridforceFullBaseGrid::get_inv(); };
208  inline Vector get_scale(void) const { return GridforceFullBaseGrid::get_scale(); };
209  inline Bool get_checksize(void) const { return GridforceFullBaseGrid::get_checksize(); };
210  inline int get_k0(void) const { return GridforceFullBaseGrid::get_k0(); };
211  inline int get_k1(void) const { return GridforceFullBaseGrid::get_k1(); };
212  inline int get_k2(void) const { return GridforceFullBaseGrid::get_k2(); };
213  inline int get_border(void) const { return border; }
214 
215  inline int compute_VdV(Position pos, float &V, Vector &dV) const { return GridforceFullBaseGrid::compute_VdV(pos, V, dV); };
216 
217  inline int get_total_grids(void) const { return totalGrids; }
218  inline void set_scale(Vector s) { scale = s; }
219 
220 protected:
221  void pack(MOStream *msg) const;
222  void unpack(MIStream *msg);
223 
224  long int get_all_gridvals(float **all_gridvals) const;
225  void set_all_gridvals(float *all_gridvals, long int sz);
226 
227  //int get_inds(Position pos, int *inds, Vector &dg, Vector &gapscale) const;
228  void compute_b(float *b, int *inds, Vector gapscale) const;
229  void buildSubgridsFlat(void);
230 
234  //int mygridnum;
235 
236  static const int default_border = 1;
237  int border;
238 };
239 
240 
242  friend class GridforceFullBaseGrid;
243  friend class GridforceFullMainGrid;
244 
245 public:
247 
248  void initialize(SimParameters *simParams, MGridforceParams *mgridParams);
249 
250  inline int get_border(void) const { return 0; }
251 
252  inline Tensor tensorMult (const Tensor &t1, const Tensor &t2) {
253  Tensor tmp;
254  tmp.xx = t1.xx * t2.xx + t1.xy * t2.yx + t1.xz * t2.zx;
255  tmp.xy = t1.xx * t2.xy + t1.xy * t2.yy + t1.xz * t2.zy;
256  tmp.xz = t1.xx * t2.xz + t1.xy * t2.yz + t1.xz * t2.zz;
257  tmp.yx = t1.yx * t2.xx + t1.yy * t2.yx + t1.yz * t2.zx;
258  tmp.yy = t1.yx * t2.xy + t1.yy * t2.yy + t1.yz * t2.zy;
259  tmp.yz = t1.yx * t2.xz + t1.yy * t2.yz + t1.yz * t2.zz;
260  tmp.zx = t1.zx * t2.xx + t1.zy * t2.yx + t1.zz * t2.zx;
261  tmp.zy = t1.zx * t2.xy + t1.zy * t2.yy + t1.zz * t2.zy;
262  tmp.zz = t1.zx * t2.xz + t1.zy * t2.yz + t1.zz * t2.zz;
263  return tmp;
264  }
265 
266 protected:
267  void pack(MOStream *msg) const;
268  void unpack(MIStream *msg);
269 
270  //int get_inds(Position pos, int *inds, Vector &dg, Vector &gapscale) const;
271  void compute_b(float *b, int *inds, Vector gapscale) const;
272  void addToSubgridsFlat(void);
273 
274  // Utility numbers
277  float scale_d3V;
278 
280  int pmin[3], pmax[3];
283 };
284 
285 
287 public:
288  explicit GridforceLiteGrid(int gridnum);
289  virtual ~GridforceLiteGrid();
290 
291  void initialize(char *potfilename, SimParameters *simParams, MGridforceParams *mgridParams);
293 
294  inline Position get_center(void) const { return center; }
295  inline Position get_origin(void) const { return origin; }
296  inline Tensor get_e (void) const { return e; }
297  inline Tensor get_inv(void) const { return inv; }
298  inline Vector get_scale(void) const { return scale; }
299  inline Bool get_checksize(void) const { return checksize; }
300  inline int get_k0(void) const { return k[0]; }
301  inline int get_k1(void) const { return k[1]; }
302  inline int get_k2(void) const { return k[2]; }
303  inline int get_total_grids(void) const { return 1; }
304  inline void set_scale(Vector s) { scale = s; }
305 
306  inline float get_grid(int i0, int i1, int i2, int i3) const {
307  return grid[grid_index(i0, i1, i2, i3)];
308  }
309  inline double get_grid_d(int i0, int i1, int i2, int i3) const {
310  return double(grid[grid_index(i0, i1, i2, i3)]);
311  }
312  inline void set_grid(int i0, int i1, int i2, int i3, float V) {
313  grid[grid_index(i0, i1, i2, i3)] = V;
314  }
315 
316  long int get_all_gridvals(float** all_gridvals) const;
317  void set_all_gridvals(float* all_gridvals, long int sz);
318 
319  int compute_VdV(Position pos, float &V, Vector &dV) const;
320 
321 protected:
322  void compute_derivative_grids(void);
323  void compute_wts(float *wts, const Vector &dg) const;
324  int get_inds(Position pos, int *inds, Vector &dg) const;
325  float linear_interpolate(int i0, int i1, int i2, int i3, const float *wts) const;
326 
327  void pack(MOStream *msg) const;
328  void unpack(MIStream *msg);
329 
330  inline long int grid_index(int i0, int i1, int i2, int i3) const {
331  // 'i3' is an index for the grid itself (0=V, 1=dV/dx, 2=dV/dy, 3=dV/dz)
332  register int inds[4] = {i0, i1, i2, i3};
333  return inds[0]*dk[0] + inds[1]*dk[1] + inds[2]*dk[2] + inds[3]*dk[3];
334  }
335 
336  float *grid;
337 
338  int k[4]; // Grid dimensions ... 4th is always 4, for the different grid types
339  long int size;
340  long int dk[4];
341 
342  Position origin; // Grid origin
343  Position center; // Center of grid (for wrapping)
344  Tensor e; // Grid unit vectors
345  Tensor inv; // Inverse of unit vectors
346 
349 
351 };
352 
353 #endif
void reinitialize(SimParameters *simParams, MGridforceParams *mgridParams)
Vector get_scale(void) const
Definition: GridForceGrid.h:96
int get_k0(void) const
BigReal zy
Definition: Tensor.h:19
void pack(MOStream *msg) const
Tensor get_e(void) const
int get_total_grids(void) const
long int grid_index(int i0, int i1, int i2) const
BigReal xz
Definition: Tensor.h:17
int get_inds(Position pos, int *inds, Vector &dg, Vector &gapscale) const
virtual ~GridforceGrid()
Definition: GridForceGrid.C:48
int get_k2(void) const
Position get_center(void) const
Definition: GridForceGrid.h:92
int compute_VdV(Position pos, float &V, Vector &dV) const
float compute_V(float *a, float *x, float *y, float *z) const
static GridforceGrid * new_grid(int gridnum, char *potfilename, SimParameters *simParams, MGridforceParams *mgridParams)
Definition: GridForceGrid.C:34
int get_inds(Position pos, int *inds, Vector &dg) const
Definition: Vector.h:72
Position get_origin(void) const
Definition: GridForceGrid.h:93
virtual void compute_b(float *b, int *inds, Vector gapscale) const =0
Position get_center(void) const
Position get_origin(void) const
Vector get_scale(void) const
void initialize(char *potfilename, SimParameters *simParams, MGridforceParams *mgridParams, int border)
long int get_all_gridvals(float **all_gridvals) const
int get_k0(void) const
static void pack_grid(GridforceGrid *grid, MOStream *msg)
Definition: GridForceGrid.C:50
Vector compute_d2V(float *a, float *x, float *y, float *z) const
virtual Position get_origin(void) const =0
int get_k0(void) const
BigReal yz
Definition: Tensor.h:18
double get_grid_d(int i0, int i1, int i2, int i3) const
int get_k2(void) const
float compute_d3V(float *a, float *x, float *y, float *z) const
virtual ~GridforceLiteGrid()
int get_border(void) const
#define NAMD_FILENAME_BUFFER_SIZE
Definition: common.h:45
virtual void set_all_gridvals(float *all_gridvals, long int sz)=0
virtual int get_k2(void) const =0
char filename[NAMD_FILENAME_BUFFER_SIZE]
Bool get_checksize(void) const
Definition: GridForceGrid.h:97
Bool get_checksize(void) const
Tensor get_inv(void) const
Definition: GridForceGrid.h:95
int get_k1(void) const
GridforceFullMainGrid * maingrid
void readHeader(SimParameters *simParams, MGridforceParams *mgridParams)
virtual void reinitialize(SimParameters *simParams, MGridforceParams *mgridParams)=0
void initialize(char *potfilename, SimParameters *simParams, MGridforceParams *mgridParams)
int get_total_grids(void) const
void unpack(MIStream *msg)
void compute_b(float *b, int *inds, Vector gapscale) const
void compute_derivative_grids(void)
void set_grid(int i0, int i1, int i2, int i3, float V)
virtual long int get_all_gridvals(float **all_gridvals) const =0
GridforceFullSubGrid ** subgrids
long int grid_index(int i0, int i1, int i2, int i3) const
GridforceFullBaseGrid * parent
bool fits_lattice(const Lattice &lattice)
Definition: GridForceGrid.C:84
virtual void pack(MOStream *msg) const
Tensor get_e(void) const
void set_grid(int i0, int i1, int i2, float V)
virtual void pack(MOStream *msg) const =0
void NAMD_bug(const char *err_msg)
Definition: common.C:195
BigReal yx
Definition: Tensor.h:18
void compute_b(float *b, int *inds, Vector gapscale) const
Position get_center(void) const
GridforceFullSubGrid ** subgrids_flat
Tensor get_inv(void) const
GridforceFullMainGrid(int gridnum)
virtual Position get_center(void) const =0
void reinitialize(SimParameters *simParams, MGridforceParams *mgridParams)
virtual ~GridforceFullBaseGrid()
int Bool
Definition: common.h:142
void set_all_gridvals(float *all_gridvals, long int sz)
long int get_all_gridvals(float **all_gridvals) const
float get_grid(int i0, int i1, int i2) const
void set_scale(Vector s)
GridforceFullSubGrid(GridforceFullBaseGrid *parent_in)
virtual void initialize(char *potfilename, SimParameters *simParams, MGridforceParams *mgridParams)=0
void readSubgridHierarchy(FILE *poten, int &totalGrids)
GridforceLiteGrid(int gridnum)
void addToSubgridsFlat(void)
virtual int get_border(void) const =0
virtual Bool get_checksize(void) const =0
virtual void unpack(MIStream *msg)
int compute_VdV(Position pos, float &V, Vector &dV) const
void set_all_gridvals(float *all_gridvals, long int sz)
Vector get_scale(void) const
BigReal xx
Definition: Tensor.h:17
virtual Vector get_scale(void) const =0
float linear_interpolate(int i0, int i1, int i2, int i3, const float *wts) const
BigReal zz
Definition: Tensor.h:19
GridforceGridType type
Definition: GridForceGrid.h:73
void set_scale(Vector s)
char filename[NAMD_FILENAME_BUFFER_SIZE]
Tensor get_inv(void) const
#define simParams
Definition: Output.C:129
void initialize(char *potfilename, SimParameters *simParams, MGridforceParams *mgridParams)
Tensor get_e(void) const
Definition: GridForceGrid.h:94
static GridforceGrid * unpack_grid(int gridnum, MIStream *msg)
Definition: GridForceGrid.C:60
void buildSubgridsFlat(void)
int get_k1(void) const
Definition: Tensor.h:15
BigReal xy
Definition: Tensor.h:17
Tensor tensorMult(const Tensor &t1, const Tensor &t2)
virtual int get_k0(void) const =0
Position get_corner(int idx)
virtual int get_k1(void) const =0
Position wrap_position(const Position &pos, const Lattice &lattice)
virtual void unpack(MIStream *msg)=0
virtual Tensor get_e(void) const =0
Vector compute_dV(float *a, float *x, float *y, float *z) const
BigReal yy
Definition: Tensor.h:18
void pack(MOStream *msg) const
virtual ~GridforceFullMainGrid()
int compute_VdV(Position pos, float &V, Vector &dV) const
Definition: GridForceGrid.h:53
Bool get_checksize(void) const
int compute_VdV(Position pos, float &V, Vector &dV) const
GridforceGridType get_grid_type(void)
Definition: GridForceGrid.h:64
void compute_wts(float *wts, const Vector &dg) const
static const int default_border
float get_grid(int i0, int i1, int i2, int i3) const
void pack(MOStream *msg) const
Position get_origin(void) const
void set_scale(Vector s)
double get_grid_d(int i0, int i1, int i2) const
void initialize(SimParameters *simParams, MGridforceParams *mgridParams)
int get_border(void) const
int get_k1(void) const
BigReal zx
Definition: Tensor.h:19
void unpack(MIStream *msg)
virtual void set_scale(Vector s)=0
virtual int get_total_grids(void) const =0
int get_k2(void) const
void unpack(MIStream *msg)
void compute_a(float *a, float *b) const
virtual Tensor get_inv(void) const =0