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