NAMD
Public Member Functions | Protected Member Functions | Protected Attributes | Static Protected Attributes | Friends | List of all members
GridforceFullMainGrid Class Reference

#include <GridForceGrid.h>

Inheritance diagram for GridforceFullMainGrid:
GridforceGrid GridforceFullBaseGrid

Public Member Functions

 GridforceFullMainGrid (int gridnum)
 
virtual ~GridforceFullMainGrid ()
 
void initialize (char *potfilename, SimParameters *simParams, MGridforceParams *mgridParams, int border)
 
void initialize (char *potfilename, SimParameters *simParams, MGridforceParams *mgridParams)
 
void reinitialize (SimParameters *simParams, MGridforceParams *mgridParams)
 
Position get_center (void) const
 
Position get_origin (void) const
 
Tensor get_e (void) const
 
Tensor get_inv (void) const
 
Vector get_scale (void) const
 
Bool get_checksize (void) const
 
int get_k0 (void) const
 
int get_k1 (void) const
 
int get_k2 (void) const
 
int get_border (void) const
 
int compute_VdV (Position pos, float &V, Vector &dV) const
 
int get_total_grids (void) const
 
void set_scale (Vector s)
 
- Public Member Functions inherited from GridforceGrid
virtual ~GridforceGrid ()
 
Position wrap_position (const Position &pos, const Lattice &lattice)
 
bool fits_lattice (const Lattice &lattice)
 
int compute_VdV (Position pos, float &V, Vector &dV) const
 
GridforceGridType get_grid_type (void)
 
- Public Member Functions inherited from GridforceFullBaseGrid
 GridforceFullBaseGrid (void)
 
virtual ~GridforceFullBaseGrid ()
 
Position get_center (void) const
 
Position get_origin (void) const
 
Tensor get_e (void) const
 
Tensor get_inv (void) const
 
Vector get_scale (void) const
 
Bool get_checksize (void) const
 
float get_grid (int i0, int i1, int i2) const
 
double get_grid_d (int i0, int i1, int i2) const
 
void set_grid (int i0, int i1, int i2, float V)
 
void set_scale (Vector s)
 
int compute_VdV (Position pos, float &V, Vector &dV) const
 
int get_k0 (void) const
 
int get_k1 (void) const
 
int get_k2 (void) const
 
void readHeader (SimParameters *simParams, MGridforceParams *mgridParams)
 
long int grid_index (int i0, int i1, int i2) const
 
int get_inds (Position pos, int *inds, Vector &dg, Vector &gapscale) const
 
void compute_a (float *a, float *b) const
 
float compute_V (float *a, float *x, float *y, float *z) const
 
Vector compute_dV (float *a, float *x, float *y, float *z) const
 
Vector compute_d2V (float *a, float *x, float *y, float *z) const
 
float compute_d3V (float *a, float *x, float *y, float *z) const
 
void readSubgridHierarchy (FILE *poten, int &totalGrids)
 

Protected Member Functions

void pack (MOStream *msg) const
 
void unpack (MIStream *msg)
 
long int get_all_gridvals (float **all_gridvals) const
 
void set_all_gridvals (float *all_gridvals, long int sz)
 
void compute_b (float *b, int *inds, Vector gapscale) const
 
void buildSubgridsFlat (void)
 
- Protected Member Functions inherited from GridforceGrid
Position get_corner (int idx)
 
 GridforceGrid ()
 

Protected Attributes

char filename [NAMD_FILENAME_BUFFER_SIZE]
 
int totalGrids
 
GridforceFullSubGrid ** subgrids_flat
 
int border
 
- Protected Attributes inherited from GridforceGrid
GridforceGridType type
 
int mygridnum
 

Static Protected Attributes

static const int default_border = 1
 

Friends

class GridforceFullBaseGrid
 
class GridforceFullSubGrid
 

Additional Inherited Members

- Public Types inherited from GridforceGrid
enum  GridforceGridType { GridforceGridTypeUndefined = 0, GridforceGridTypeFull, GridforceGridTypeLite }
 
- Static Public Member Functions inherited from GridforceGrid
static GridforceGridnew_grid (int gridnum, char *potfilename, SimParameters *simParams, MGridforceParams *mgridParams)
 
static void pack_grid (GridforceGrid *grid, MOStream *msg)
 
static GridforceGridunpack_grid (int gridnum, MIStream *msg)
 
- Public Attributes inherited from GridforceFullBaseGrid
FILE * poten_fp
 
float * grid
 
GridforceFullSubGrid ** subgrids
 
int numSubgrids
 
int generation
 
int k [3]
 
int k_nopad [3]
 
long int size
 
long int size_nopad
 
long int dk [3]
 
long int dk_nopad [3]
 
float factor
 
Position origin
 
Position center
 
Tensor e
 
Tensor inv
 
double p_sum [3]
 
double n_sum [3]
 
double pad_p [3]
 
double pad_n [3]
 
Bool cont [3]
 
float offset [3]
 
float gap [3]
 
float gapinv [3]
 
Vector scale
 
Bool checksize
 

Detailed Description

Definition at line 190 of file GridForceGrid.h.

Constructor & Destructor Documentation

◆ GridforceFullMainGrid()

GridforceFullMainGrid::GridforceFullMainGrid ( int  gridnum)
explicit

◆ ~GridforceFullMainGrid()

GridforceFullMainGrid::~GridforceFullMainGrid ( )
virtual

Definition at line 362 of file GridForceGrid.C.

References subgrids_flat.

363 {
364  delete[] subgrids_flat;
365 }
GridforceFullSubGrid ** subgrids_flat

Member Function Documentation

◆ buildSubgridsFlat()

void GridforceFullMainGrid::buildSubgridsFlat ( void  )
protected

Definition at line 406 of file GridForceGrid.C.

References GridforceFullSubGrid::addToSubgridsFlat(), DebugM, endi(), GridforceFullBaseGrid::numSubgrids, GridforceFullBaseGrid::subgrids, subgrids_flat, and totalGrids.

Referenced by initialize(), and unpack().

407 {
408  DebugM(4, "buildSubgridsFlat() called, totalGrids-1 = " << totalGrids-1 << "\n" << endi);
409  delete[] subgrids_flat;
411  for (int i = 0; i < numSubgrids; i++) {
412  DebugM(3, "adding to subgridsFlat\n" << endi);
414  DebugM(3, "success!\n" << endi);
415  }
416  for (int i = 0; i < totalGrids-1; i++) {
417  DebugM(4, "subgrids_flat[" << i << "]->numSubgrids = " << subgrids_flat[i]->numSubgrids << "\n" << endi);
418  }
419  for (int i = 0; i < numSubgrids; i++) {
420  DebugM(4, "subgrids[" << i << "]->numSubgrids = " << subgrids[i]->numSubgrids << "\n" << endi);
421  }
422 }
#define DebugM(x, y)
Definition: Debug.h:75
std::ostream & endi(std::ostream &s)
Definition: InfoStream.C:54
GridforceFullSubGrid ** subgrids
GridforceFullSubGrid ** subgrids_flat
void addToSubgridsFlat(void)

◆ compute_b()

void GridforceFullMainGrid::compute_b ( float *  b,
int *  inds,
Vector  gapscale 
) const
protectedvirtual

Implements GridforceFullBaseGrid.

Definition at line 781 of file GridForceGrid.C.

References GridforceFullBaseGrid::cont, DebugM, endi(), FALSE, GridforceFullBaseGrid::gap, GridforceFullBaseGrid::get_grid(), GridforceFullBaseGrid::get_grid_d(), GridforceFullBaseGrid::k, GridforceFullBaseGrid::offset, and TRUE.

782 {
783  for (int i0 = 0; i0 < 8; i0++) {
784  int inds2[3];
785  int zero_derivs = FALSE;
786 
787  float voff = 0.0;
788  int bit = 1; // bit = 2^i1 in the below loop
789  for (int i1 = 0; i1 < 3; i1++) {
790  inds2[i1] = (inds[i1] + ((i0 & bit) ? 1 : 0)) % k[i1];
791 
792  // Deal with voltage offsets
793  if (cont[i1] && inds[i1] == (k[i1]-1) && inds2[i1] == 0) {
794  voff += offset[i1];
795  DebugM(3, "offset[" << i1 << "] = " << offset[i1] << "\n" << endi);
796  }
797 
798  bit <<= 1; // i.e. multiply by 2
799  }
800 
801  DebugM(1, "inds2 = " << inds2[0] << " " << inds2[1] << " " << inds2[2] << "\n" << endi);
802 
803  // NOTE: leaving everything in terms of unit cell coordinates for now,
804  // eventually will multiply by inv tensor when applying the force
805 
806  // First set variables 'dk_{hi,lo}' (glob notation). The 'hi'
807  // ('lo') variable in a given dimension is the number added (subtracted)
808  // to go up (down) one grid point in that dimension; both are normally
809  // just the corresponding 'dk[i]'. However, if we are sitting on a
810  // boundary and we are using a continuous grid, then we want to map the
811  // next point off the grid back around to the other side. e.g. point
812  // (k[0], i1, k) maps to point (0, i1, k), which would be
813  // accomplished by changing 'dk1_hi' to -(k[0]-1)*dk1.
814 
815  int d_hi[3] = {1, 1, 1};
816  int d_lo[3] = {1, 1, 1};
817  float voffs[3];
818  float dscales[3] = {0.5, 0.5, 0.5};
819  for (int i1 = 0; i1 < 3; i1++) {
820  if (inds2[i1] == 0) {
821  if (cont[i1]) {
822  d_lo[i1] = -(k[i1]-1);
823  voffs[i1] = offset[i1];
824  dscales[i1] = 1.0/(1.0 + gap[i1]) * 1.0/gapscale[i1];
825  }
826  else zero_derivs = TRUE;
827  }
828  else if (inds2[i1] == k[i1]-1) {
829  if (cont[i1]) {
830  d_hi[i1] = -(k[i1]-1);
831  voffs[i1] = offset[i1];
832  dscales[i1] = 1.0/(1.0 + gap[i1]) * 1.0/gapscale[i1];
833  }
834  else zero_derivs = TRUE;
835  }
836  else {
837  voffs[i1] = 0.0;
838  }
839  }
840 
841 // DebugM(2, "cont = " << cont[0] << " " << cont[1] << " " << cont[2] << "\n" << endi);
842 // DebugM(2, "zero_derivs = " << zero_derivs << "\n" << endi);
843 // DebugM(2, "d_hi = " << d_hi[0] << " " << d_hi[1] << " " << d_hi[2] << "\n" << endi);
844 // DebugM(2, "d_lo = " << d_lo[0] << " " << d_lo[1] << " " << d_lo[2] << "\n" << endi);
845  DebugM(1, "dscales = " << dscales[0] << " " << dscales[1] << " " << dscales[2] << "\n" << endi);
846  DebugM(1, "voffs = " << voffs[0] << " " << voffs[1] << " " << voffs[2] << "\n" << endi);
847 
848  // V
849  b[i0] = get_grid(inds2[0],inds2[1],inds2[2]) + voff;
850 
851  if (zero_derivs) {
852  DebugM(2, "zero_derivs\n" << endi);
853  b[8+i0] = 0.0;
854  b[16+i0] = 0.0;
855  b[24+i0] = 0.0;
856  b[32+i0] = 0.0;
857  b[40+i0] = 0.0;
858  b[48+i0] = 0.0;
859  b[56+i0] = 0.0;
860  } else {
861  b[8+i0] = dscales[0] * (get_grid_d(inds2[0]+d_hi[0],inds2[1],inds2[2]) - get_grid_d(inds2[0]-d_lo[0],inds2[1],inds2[2]) + voffs[0]); // dV/dx
862  b[16+i0] = dscales[1] * (get_grid_d(inds2[0],inds2[1]+d_hi[1],inds2[2]) - get_grid_d(inds2[0],inds2[1]-d_lo[1],inds2[2]) + voffs[1]); // dV/dy
863  b[24+i0] = dscales[2] * (get_grid_d(inds2[0],inds2[1],inds2[2]+d_hi[2]) - get_grid_d(inds2[0],inds2[1],inds2[2]-d_lo[2]) + voffs[2]); // dV/dz
864  b[32+i0] = dscales[0] * dscales[1]
865  * (get_grid_d(inds2[0]+d_hi[0],inds2[1]+d_hi[1],inds2[2]) - get_grid_d(inds2[0]-d_lo[0],inds2[1]+d_hi[1],inds2[2]) -
866  get_grid_d(inds2[0]+d_hi[0],inds2[1]-d_lo[1],inds2[2]) + get_grid_d(inds2[0]-d_lo[0],inds2[1]-d_lo[1],inds2[2])); // d2V/dxdy
867  b[40+i0] = dscales[0] * dscales[2]
868  * (get_grid_d(inds2[0]+d_hi[0],inds2[1],inds2[2]+d_hi[2]) - get_grid_d(inds2[0]-d_lo[0],inds2[1],inds2[2]+d_hi[2]) -
869  get_grid_d(inds2[0]+d_hi[0],inds2[1],inds2[2]-d_lo[2]) + get_grid_d(inds2[0]-d_lo[0],inds2[1],inds2[2]-d_lo[2])); // d2V/dxdz
870  b[48+i0] = dscales[1] * dscales[2]
871  * (get_grid_d(inds2[0],inds2[1]+d_hi[1],inds2[2]+d_hi[2]) - get_grid_d(inds2[0],inds2[1]-d_lo[1],inds2[2]+d_hi[2]) -
872  get_grid_d(inds2[0],inds2[1]+d_hi[1],inds2[2]-d_lo[2]) + get_grid_d(inds2[0],inds2[1]-d_lo[1],inds2[2]-d_lo[2])); // d2V/dydz
873 
874  b[56+i0] = dscales[0] * dscales[1] * dscales[2] // d3V/dxdydz
875  * (get_grid_d(inds2[0]+d_hi[0],inds2[1]+d_hi[1],inds2[2]+d_hi[2]) - get_grid_d(inds2[0]+d_hi[0],inds2[1]+d_hi[1],inds2[2]-d_lo[2]) -
876  get_grid_d(inds2[0]+d_hi[0],inds2[1]-d_lo[1],inds2[2]+d_hi[2]) - get_grid_d(inds2[0]-d_lo[0],inds2[1]+d_hi[1],inds2[2]+d_hi[2]) +
877  get_grid_d(inds2[0]+d_hi[0],inds2[1]-d_lo[1],inds2[2]-d_lo[2]) + get_grid_d(inds2[0]-d_lo[0],inds2[1]+d_hi[1],inds2[2]-d_lo[2]) +
878  get_grid_d(inds2[0]-d_lo[0],inds2[1]-d_lo[1],inds2[2]+d_hi[2]) - get_grid_d(inds2[0]-d_lo[0],inds2[1]-d_lo[1],inds2[2]-d_lo[2]));
879  }
880 
881  DebugM(1, "V = " << b[i0] << "\n");
882 
883  DebugM(1, "dV/dx = " << b[8+i0] << "\n");
884  DebugM(1, "dV/dy = " << b[16+i0] << "\n");
885  DebugM(1, "dV/dz = " << b[24+i0] << "\n");
886 
887  DebugM(1, "d2V/dxdy = " << b[32+i0] << "\n");
888  DebugM(1, "d2V/dxdz = " << b[40+i0] << "\n");
889  DebugM(1, "d2V/dydz = " << b[48+i0] << "\n");
890 
891  DebugM(1, "d3V/dxdydz = " << b[56+i0] << "\n" << endi);
892  }
893 }
#define DebugM(x, y)
Definition: Debug.h:75
std::ostream & endi(std::ostream &s)
Definition: InfoStream.C:54
#define FALSE
Definition: common.h:127
float get_grid(int i0, int i1, int i2) const
double get_grid_d(int i0, int i1, int i2) const
#define TRUE
Definition: common.h:128

◆ compute_VdV()

int GridforceFullMainGrid::compute_VdV ( Position  pos,
float &  V,
Vector dV 
) const
inline

Definition at line 215 of file GridForceGrid.h.

References GridforceFullBaseGrid::compute_VdV().

215 { return GridforceFullBaseGrid::compute_VdV(pos, V, dV); };
int compute_VdV(Position pos, float &V, Vector &dV) const

◆ get_all_gridvals()

long int GridforceFullMainGrid::get_all_gridvals ( float **  all_gridvals) const
protectedvirtual

Implements GridforceGrid.

Definition at line 717 of file GridForceGrid.C.

References DebugM, endi(), GridforceFullBaseGrid::grid, GridforceFullBaseGrid::size, subgrids_flat, and totalGrids.

718 {
719  // Creates a flat array of all grid values, including subgrids,
720  // and puts it in the value pointed to by the 'grids'
721  // argument. Returns the resulting array size. Caller is
722  // responsible for destroying the array via 'delete[]'
723 
724  DebugM(4, "get_all_gridvals called\n" << endi);
725 
726  long int sz = 0;
727  sz += size;
728  for (int i = 0; i < totalGrids-1; i++) {
729  sz += subgrids_flat[i]->size;
730  }
731  DebugM(4, "size = " << sz << "\n" << endi);
732 
733  float *grid_vals = new float[sz];
734  long int idx = 0;
735  for (long int i = 0; i < size; i++) {
736  grid_vals[idx++] = grid[i];
737  }
738  for (int j = 0; j < totalGrids-1; j++) {
739  for (long int i = 0; i < subgrids_flat[j]->size; i++) {
740  grid_vals[idx++] = subgrids_flat[j]->grid[i];
741  }
742  }
743  CmiAssert(idx == sz);
744 
745  *all_gridvals = grid_vals;
746 
747  DebugM(4, "get_all_gridvals finished\n" << endi);
748 
749  return sz;
750 }
#define DebugM(x, y)
Definition: Debug.h:75
std::ostream & endi(std::ostream &s)
Definition: InfoStream.C:54
GridforceFullSubGrid ** subgrids_flat

◆ get_border()

int GridforceFullMainGrid::get_border ( void  ) const
inlinevirtual

Implements GridforceFullBaseGrid.

Definition at line 213 of file GridForceGrid.h.

References border.

213 { return border; }

◆ get_center()

Position GridforceFullMainGrid::get_center ( void  ) const
inlinevirtual

Implements GridforceGrid.

Definition at line 204 of file GridForceGrid.h.

References GridforceFullBaseGrid::get_center().

Referenced by GridforceLiteGrid::initialize().

Position get_center(void) const
Definition: GridForceGrid.h:92

◆ get_checksize()

Bool GridforceFullMainGrid::get_checksize ( void  ) const
inlinevirtual

Implements GridforceGrid.

Definition at line 209 of file GridForceGrid.h.

References GridforceFullBaseGrid::get_checksize().

Bool get_checksize(void) const
Definition: GridForceGrid.h:97

◆ get_e()

Tensor GridforceFullMainGrid::get_e ( void  ) const
inlinevirtual

Implements GridforceGrid.

Definition at line 206 of file GridForceGrid.h.

References GridforceFullBaseGrid::get_e().

Referenced by GridforceLiteGrid::initialize().

206 { return GridforceFullBaseGrid::get_e(); };
Tensor get_e(void) const
Definition: GridForceGrid.h:94

◆ get_inv()

Tensor GridforceFullMainGrid::get_inv ( void  ) const
inlinevirtual

Implements GridforceGrid.

Definition at line 207 of file GridForceGrid.h.

References GridforceFullBaseGrid::get_inv().

Referenced by GridforceLiteGrid::initialize().

207 { return GridforceFullBaseGrid::get_inv(); };
Tensor get_inv(void) const
Definition: GridForceGrid.h:95

◆ get_k0()

int GridforceFullMainGrid::get_k0 ( void  ) const
inlinevirtual

Implements GridforceGrid.

Definition at line 210 of file GridForceGrid.h.

References GridforceFullBaseGrid::get_k0().

Referenced by GridforceLiteGrid::initialize().

210 { return GridforceFullBaseGrid::get_k0(); };
int get_k0(void) const

◆ get_k1()

int GridforceFullMainGrid::get_k1 ( void  ) const
inlinevirtual

Implements GridforceGrid.

Definition at line 211 of file GridForceGrid.h.

References GridforceFullBaseGrid::get_k1().

Referenced by GridforceLiteGrid::initialize().

211 { return GridforceFullBaseGrid::get_k1(); };
int get_k1(void) const

◆ get_k2()

int GridforceFullMainGrid::get_k2 ( void  ) const
inlinevirtual

Implements GridforceGrid.

Definition at line 212 of file GridForceGrid.h.

References GridforceFullBaseGrid::get_k2().

Referenced by GridforceLiteGrid::initialize().

212 { return GridforceFullBaseGrid::get_k2(); };
int get_k2(void) const

◆ get_origin()

Position GridforceFullMainGrid::get_origin ( void  ) const
inlinevirtual

Implements GridforceGrid.

Definition at line 205 of file GridForceGrid.h.

References GridforceFullBaseGrid::get_origin().

Referenced by GridforceLiteGrid::initialize().

Position get_origin(void) const
Definition: GridForceGrid.h:93

◆ get_scale()

Vector GridforceFullMainGrid::get_scale ( void  ) const
inlinevirtual

Implements GridforceGrid.

Definition at line 208 of file GridForceGrid.h.

References GridforceFullBaseGrid::get_scale().

Referenced by GridforceLiteGrid::initialize().

Vector get_scale(void) const
Definition: GridForceGrid.h:96

◆ get_total_grids()

int GridforceFullMainGrid::get_total_grids ( void  ) const
inlinevirtual

Implements GridforceGrid.

Definition at line 217 of file GridForceGrid.h.

References totalGrids.

Referenced by GridforceLiteGrid::initialize().

217 { return totalGrids; }

◆ initialize() [1/2]

void GridforceFullMainGrid::initialize ( char *  potfilename,
SimParameters simParams,
MGridforceParams mgridParams,
int  border 
)

Definition at line 425 of file GridForceGrid.C.

References border, buildSubgridsFlat(), GridforceFullBaseGrid::checksize, GridforceFullBaseGrid::cont, DebugM, default_border, GridforceFullBaseGrid::dk, GridforceFullBaseGrid::dk_nopad, GridforceFullBaseGrid::e, endi(), GridforceFullBaseGrid::factor, FALSE, filename, GridforceGrid::fits_lattice(), Fopen(), GridforceFullBaseGrid::gap, GridforceFullBaseGrid::gapinv, GridforceFullBaseGrid::get_grid(), GridforceFullBaseGrid::grid, MGridforceParams::gridforceCheckSize, MGridforceParams::gridforceCont, MGridforceParams::gridforceScale, MGridforceParams::gridforceVOffset, MGridforceParams::gridforceVolts, GridforceFullSubGrid::initialize(), GridforceFullBaseGrid::inv, iout, iWARN(), GridforceFullBaseGrid::k, GridforceFullBaseGrid::k_nopad, GridforceGrid::mygridnum, GridforceFullBaseGrid::n_sum, NAMD_die(), GridforceFullBaseGrid::numSubgrids, GridforceFullBaseGrid::offset, GridforceFullBaseGrid::origin, GridforceFullBaseGrid::p_sum, GridforceFullBaseGrid::pad_n, GridforceFullBaseGrid::pad_p, GridforceFullBaseGrid::poten_fp, GridforceFullBaseGrid::readHeader(), GridforceFullBaseGrid::readSubgridHierarchy(), GridforceFullBaseGrid::scale, GridforceFullBaseGrid::set_grid(), simParams, GridforceFullBaseGrid::size, GridforceFullBaseGrid::size_nopad, GridforceFullBaseGrid::subgrids, totalGrids, and TRUE.

Referenced by initialize(), GridforceLiteGrid::initialize(), and reinitialize().

426 {
427  if (brd >= 0) {
428  border = brd;
429  } else {
431  }
432 
433  // FROM init1
434  //FILE *poten = Fopen(potfilename, "r");
435  poten_fp = Fopen(potfilename, "r");
436  if (!poten_fp) {
437  NAMD_die("Problem reading grid force potential file");
438  }
439 
440  // save file name so that grid can be re-read via Tcl
441  strcpy(filename, potfilename);
442 
443  // Read special comment fields and create subgrid objects
444  totalGrids = 1;
445  char line[256];
446  Bool flag = FALSE;
447  numSubgrids = 0;
448  float version;
449  long int poten_offset;
450  do {
451  poten_offset = ftell(poten_fp);
452  fgets(line, 256, poten_fp); // Read comment lines
453  //flag = sscanf(line, "# maingrid subgrids count %d\n", &numSubgrids);
454  flag = sscanf(line, "# namdnugrid version %f\n", &version);
455  } while (line[0] == '#' && !flag);
456 
457  if (flag) {
458  if (version != 1.0) {
459  NAMD_die("Unsupported version of non-uniform grid file format!");
460  }
461  fscanf(poten_fp, "# namdnugrid maingrid subgrids count %d\n", &numSubgrids);
464  } else {
465  fseek(poten_fp, poten_offset, SEEK_SET);
466  }
467 
468  // Read header
469  readHeader(simParams, mgridParams);
470 
471  factor = 1.0;
472  if (mgridParams->gridforceVolts)
473  {
474  factor /= 0.0434; // convert V -> kcal/mol*e
475  }
476  scale = mgridParams->gridforceScale;
477  checksize = mgridParams->gridforceCheckSize;
478 
479  // Allocate storage for potential and read it
480  float *grid_nopad = new float[size_nopad];
481 
482  float tmp2;
483  for (long int count = 0; count < size_nopad; count++) {
484  int err = fscanf(poten_fp, "%f", &tmp2);
485  if (err == EOF || err == 0) {
486  NAMD_die("Grid force potential file incorrectly formatted");
487  }
488  grid_nopad[count] = tmp2 * factor; // temporary, so just store flat
489  }
490  fscanf(poten_fp, "\n");
491 
492  // Shortcuts for accessing 1-D array with four indices
493  dk_nopad[0] = k_nopad[1] * k_nopad[2];
494  dk_nopad[1] = k_nopad[2];
495  dk_nopad[2] = 1;
496 
497  Vector Kvec[3];
498  Kvec[0] = e * Position(k_nopad[0]-1, 0, 0);
499  Kvec[1] = e * Position(0, k_nopad[1]-1, 0);
500  Kvec[2] = e * Position(0, 0, k_nopad[2]-1);
501  Vector Avec[3];
502  Avec[0] = simParams->lattice.a();
503  Avec[1] = simParams->lattice.b();
504  Avec[2] = simParams->lattice.c();
505 
506  // Decide whether we're wrapping
507  for (int i0 = 0; i0 < 3; i0++) {
508  if (mgridParams->gridforceCont[i0])
509  {
510  Bool found = FALSE;
511  for (int i1 = 0; i1 < 3; i1++) {
512  if (cross(Avec[i0].unit(), Kvec[i1].unit()).length() < 1e-4) {
513  found = TRUE;
514  cont[i1] = TRUE;
515  offset[i1] = mgridParams->gridforceVOffset[i0] * factor;
516  // want in grid-point units (normal = 1)
517  gap[i1] = (inv * (Avec[i0] - Kvec[i1])).length();
518  gapinv[i1] = 1.0/gap[i1];
519 
520  if (gap[i1] < 0) {
521  NAMD_die("Gridforce Grid overlap!");
522  }
523 
524  DebugM(4, "cont[" << i1 << "] = " << cont[i1] << "\n");
525  DebugM(4, "gap[" << i1 << "] = " << gap[i1] << "\n");
526  DebugM(4, "gapinv[" << i1 << "] = " << gapinv[i1] << "\n" << endi);
527  }
528  }
529 
530  if (!found) {
531  NAMD_die("No Gridforce unit vector found parallel to requested continuous grid direction!");
532  }
533  } else {
534  // check for grid overlap in non-wrapping dimensions
535  // occurs below
536  }
537  }
538 
539  // Figure out size of true grid (padded on non-periodic sides)
540  Vector delta = 0;
541  for (int i = 0; i < 3; i++) {
542  if (cont[i]) {
543  k[i] = k_nopad[i];
544  } else {
545  k[i] = k_nopad[i] + 2*border;
546  delta[i] -= border;
547  }
548  }
549  DebugM(4, "delta = " << e * delta << " (" << delta << ")\n" << endi);
550  origin += e * delta;
551 
552  // Check for grid overlap
553  if (!fits_lattice(simParams->lattice)) {
554  char errmsg[512];
555  if (checksize) {
556  sprintf(errmsg, "Warning: Periodic cell basis too small for Gridforce grid %d. Set gridforcechecksize off in configuration file to ignore.\n", mygridnum);
557  NAMD_die(errmsg);
558  }
559  }
560 
561  size = k[0] * k[1] * k[2];
562  dk[0] = k[1] * k[2];
563  dk[1] = k[2];
564  dk[2] = 1;
565 
566  DebugM(3, "size = " << size << ", size_nopad = " << size_nopad << "\n" << endi);
567 
568  delete[] grid;
569  grid = new float[size];
570 
571  n_sum[0] = n_sum[1] = n_sum[2] = 0;
572  p_sum[0] = p_sum[1] = p_sum[2] = 0;
573  for (int i0 = 0; i0 < k_nopad[0]; i0++) {
574  for (int i1 = 0; i1 < k_nopad[1]; i1++) {
575  for (int i2 = 0; i2 < k_nopad[2]; i2++) {
576  // Edges are special cases -- take force there to be
577  // zero for smooth transition across potential
578  // boundary
579 
580  long int ind_nopad = i0*dk_nopad[0] + i1*dk_nopad[1] + i2*dk_nopad[2];
581  int j0 = (cont[0]) ? i0 : i0 + border;
582  int j1 = (cont[1]) ? i1 : i1 + border;
583  int j2 = (cont[2]) ? i2 : i2 + border;
584  long int ind = j0*dk[0] + j1*dk[1] + j2*dk[2];
585 
586  if (i0 == 0) n_sum[0] += grid_nopad[ind_nopad];
587  else if (i0 == k_nopad[0]-1) p_sum[0] += grid_nopad[ind_nopad];
588  if (i1 == 0) n_sum[1] += grid_nopad[ind_nopad];
589  else if (i1 == k_nopad[1]-1) p_sum[1] += grid_nopad[ind_nopad];
590  if (i2 == 0) n_sum[2] += grid_nopad[ind_nopad];
591  else if (i2 == k_nopad[2]-1) p_sum[2] += grid_nopad[ind_nopad];
592 
593  //grid[ind] = grid_nopad[ind_nopad];
594  set_grid(j0, j1, j2, grid_nopad[ind_nopad]);
595  }
596  }
597  }
598 
599  const BigReal modThresh = 1.0;
600 
601  BigReal n_avg[3], p_avg[3];
602  int i0;
603  for (int i0 = 0; i0 < 3; i0++) {
604  int i1 = (i0 + 1) % 3;
605  int i2 = (i0 + 2) % 3;
606  n_avg[i0] = n_sum[i0] / (k_nopad[i1] * k_nopad[i2]);
607  p_avg[i0] = p_sum[i0] / (k_nopad[i1] * k_nopad[i2]);
608 
609  if (cont[i0] && fabs(offset[i0] - (p_avg[i0]-n_avg[i0])) > modThresh)
610  {
611  iout << iWARN << "GRID FORCE POTENTIAL DIFFERENCE IN K" << i0
612  << " DIRECTION IS "
613  << offset[i0] - (p_avg[i0]-n_avg[i0])
614  << " KCAL/MOL*E\n" << endi;
615  }
616  }
617 
618  Bool twoPadVals = (cont[0] + cont[1] + cont[2] == 2);
619  double padVal = 0.0;
620  long int weight = 0;
621  if (!twoPadVals) {
622  // Determine pad value (must average)
623  if (!cont[0]) {
624  padVal += p_sum[0] + n_sum[0];
625  weight += 2 * k_nopad[1] * k_nopad[2];
626  }
627  if (!cont[1]) {
628  padVal += p_sum[1] + n_sum[1];
629  weight += 2 * k_nopad[0] * k_nopad[2];
630  }
631  if (!cont[2]) {
632  padVal += p_sum[2] + n_sum[2];
633  weight += 2 * k_nopad[0] * k_nopad[1];
634  }
635  padVal /= weight;
636  }
637 
638  for (int i = 0; i < 3; i++) {
639  pad_n[i] = (cont[i]) ? 0.0 : (twoPadVals) ? n_avg[i] : padVal;
640  pad_p[i] = (cont[i]) ? 0.0 : (twoPadVals) ? p_avg[i] : padVal;
641  DebugM(4, "pad_n[" << i << "] = " << pad_n[i] << "\n");
642  DebugM(4, "pad_p[" << i << "] = " << pad_p[i] << "\n" << endi);
643  }
644 
645  if (cont[0] && cont[1] && cont[2]) {
646  // Nothing to do
647  return;
648  }
649 
650  // Now fill in rest of new grid
651  for (int i0 = 0; i0 < k[0]; i0++) {
652  for (int i1 = 0; i1 < k[1]; i1++) {
653  for (int i2 = 0; i2 < k[2]; i2++) {
654  if ( (cont[0] || (i0 >= border && i0 < k[0]-border))
655  && (cont[1] || (i1 >= border && i1 < k[1]-border))
656  && (cont[2] || i2 == border) )
657  {
658  i2 += k_nopad[2]-1;
659  continue;
660  }
661 
662  long int ind = i0*dk[0] + i1*dk[1] + i2*dk[2];
663 
664  Position pos = e * Position(i0, i1, i2);
665  int var[3] = {i0, i1, i2};
666 
667  for (int dir = 0; dir < 3; dir++) {
668  if (cont[dir])
669  continue;
670 
671  if (var[dir] < border) {
672  //grid[ind] = pad_n[dir];
673  set_grid(i0, i1, i2, pad_n[dir]);
674  } else if (var[dir] >= k[dir]-border) {
675  //grid[ind] = pad_p[dir];
676  set_grid(i0, i1, i2, pad_p[dir]);
677  }
678  }
679 
680 // DebugM(2, "grid[" << ind << "; " << i0 << ", " << i1
681 // << ", " << i2 << "] = " << get_grid(ind)
682 // << "\n" << endi);
683  }
684  }
685  }
686 
687  for (int i0 = 0; i0 < k[0]; i0++) {
688  for (int i1 = 0; i1 < k[1]; i1++) {
689  for (int i2 = 0; i2 < k[2]; i2++) {
690  DebugM(1, "grid[" << i0 << ", " << i1 << ", " << i2 << "] = " << get_grid(i0,i1,i2) << "\n" << endi);
691  }
692  }
693  }
694 
695  // Clean up
696  DebugM(3, "clean up\n" << endi);
697  delete[] grid_nopad;
698 
699  // Call initialize for each subgrid
700  for (int i = 0; i < numSubgrids; i++) {
701  subgrids[i]->poten_fp = poten_fp;
702  subgrids[i]->initialize(simParams, mgridParams);
703  }
704 
705  // close file pointer
706  fclose(poten_fp);
707 }
Definition: Vector.h:72
#define DebugM(x, y)
Definition: Debug.h:75
std::ostream & endi(std::ostream &s)
Definition: InfoStream.C:54
#define FALSE
Definition: common.h:127
std::ostream & iWARN(std::ostream &s)
Definition: InfoStream.C:82
#define iout
Definition: InfoStream.h:51
char filename[NAMD_FILENAME_BUFFER_SIZE]
void readHeader(SimParameters *simParams, MGridforceParams *mgridParams)
GridforceFullSubGrid ** subgrids
bool fits_lattice(const Lattice &lattice)
Definition: GridForceGrid.C:84
void set_grid(int i0, int i1, int i2, float V)
int Bool
Definition: common.h:142
FILE * Fopen(const char *filename, const char *mode)
Definition: common.C:341
float get_grid(int i0, int i1, int i2) const
Vector Position
Definition: NamdTypes.h:24
void readSubgridHierarchy(FILE *poten, int &totalGrids)
void NAMD_die(const char *err_msg)
Definition: common.C:147
#define simParams
Definition: Output.C:129
void buildSubgridsFlat(void)
static const int default_border
void initialize(SimParameters *simParams, MGridforceParams *mgridParams)
#define TRUE
Definition: common.h:128
double BigReal
Definition: common.h:123

◆ initialize() [2/2]

void GridforceFullMainGrid::initialize ( char *  potfilename,
SimParameters simParams,
MGridforceParams mgridParams 
)
inlinevirtual

Implements GridforceGrid.

Definition at line 199 of file GridForceGrid.h.

References default_border, initialize(), and simParams.

199  {
200  initialize(potfilename, simParams, mgridParams, default_border);
201  }
void initialize(char *potfilename, SimParameters *simParams, MGridforceParams *mgridParams, int border)
#define simParams
Definition: Output.C:129
static const int default_border

◆ pack()

void GridforceFullMainGrid::pack ( MOStream msg) const
protectedvirtual

Implements GridforceGrid.

Definition at line 368 of file GridForceGrid.C.

References DebugM, endi(), filename, GridforceGrid::mygridnum, GridforceFullBaseGrid::pack(), MOStream::put(), and totalGrids.

369 {
370  DebugM(4, "Packing maingrid\n" << endi);
371 
372 // msg->put(3*sizeof(float), (char*)pad_p);
373 // msg->put(3*sizeof(float), (char*)pad_n);
374  msg->put(totalGrids);
375  msg->put(mygridnum);
376  msg->put(129*sizeof(char), (char*)filename);
377 
378  DebugM(3, "calling GridforceFullBaseGrid::pack\n" << endi);
379 
381 }
#define DebugM(x, y)
Definition: Debug.h:75
std::ostream & endi(std::ostream &s)
Definition: InfoStream.C:54
char filename[NAMD_FILENAME_BUFFER_SIZE]
virtual void pack(MOStream *msg) const
MOStream * put(char data)
Definition: MStream.h:112

◆ reinitialize()

void GridforceFullMainGrid::reinitialize ( SimParameters simParams,
MGridforceParams mgridParams 
)
virtual

Implements GridforceGrid.

Definition at line 710 of file GridForceGrid.C.

References DebugM, endi(), filename, initialize(), and simParams.

711 {
712  DebugM(4, "reinitializing grid\n" << endi);
713  initialize(filename, simParams, mgridParams);
714 }
void initialize(char *potfilename, SimParameters *simParams, MGridforceParams *mgridParams, int border)
#define DebugM(x, y)
Definition: Debug.h:75
std::ostream & endi(std::ostream &s)
Definition: InfoStream.C:54
char filename[NAMD_FILENAME_BUFFER_SIZE]
#define simParams
Definition: Output.C:129

◆ set_all_gridvals()

void GridforceFullMainGrid::set_all_gridvals ( float *  all_gridvals,
long int  sz 
)
protectedvirtual

Implements GridforceGrid.

Definition at line 753 of file GridForceGrid.C.

References DebugM, endi(), GridforceFullBaseGrid::grid, GridforceFullBaseGrid::size, subgrids_flat, and totalGrids.

754 {
755  DebugM(4, "set_all_gridvals called\n" << endi);
756 
757  long int sz_calc = 0;
758  sz_calc += size;
759  for (int i = 0; i < totalGrids-1; i++) {
760  sz_calc += subgrids_flat[i]->size;
761  }
762  CmiAssert(sz == sz_calc);
763 
764  long int idx = 0;
765  for (long int i = 0; i < size; i++) {
766  DebugM(1, "all_gridvals[" << idx << "] = " << all_gridvals[idx] << "\n" << endi);
767  grid[i] = all_gridvals[idx++];
768  }
769  for (int j = 0; j < totalGrids-1; j++) {
770  for (long int i = 0; i < subgrids_flat[j]->size; i++) {
771  DebugM(1, "all_gridvals[" << idx << "] = " << all_gridvals[idx] << "\n" << endi);
772  subgrids_flat[j]->grid[i] = all_gridvals[idx++];
773  }
774  }
775  CmiAssert(idx == sz);
776 
777  DebugM(4, "set_all_gridvals finished\n" << endi);
778 }
#define DebugM(x, y)
Definition: Debug.h:75
std::ostream & endi(std::ostream &s)
Definition: InfoStream.C:54
GridforceFullSubGrid ** subgrids_flat

◆ set_scale()

void GridforceFullMainGrid::set_scale ( Vector  s)
inlinevirtual

Implements GridforceGrid.

Definition at line 218 of file GridForceGrid.h.

References GridforceFullBaseGrid::scale.

218 { scale = s; }

◆ unpack()

void GridforceFullMainGrid::unpack ( MIStream msg)
protectedvirtual

Implements GridforceGrid.

Definition at line 384 of file GridForceGrid.C.

References buildSubgridsFlat(), DebugM, endi(), filename, GridforceFullBaseGrid::gapinv, GridforceFullBaseGrid::generation, MIStream::get(), GridforceGrid::mygridnum, GridforceFullBaseGrid::numSubgrids, GridforceFullBaseGrid::size, totalGrids, and GridforceFullBaseGrid::unpack().

385 {
386  DebugM(4, "Unpacking maingrid\n" << endi);
387 
388 // msg->get(3*sizeof(float), (char*)pad_p);
389 // msg->get(3*sizeof(float), (char*)pad_n);
390  msg->get(totalGrids);
391  msg->get(mygridnum);
392  msg->get(129*sizeof(char), (char*)filename);
393 
395 
396  DebugM(4, "size = " << size << "\n");
397  DebugM(4, "numSubgrids = " << numSubgrids << "\n");
398  DebugM(4, "gapinv = " << gapinv[0] << " " << gapinv[2] << " " << gapinv[2] << " " << "\n");
399  DebugM(4, "generation = " << generation << "\n" << endi);
400  DebugM(4, "filename = " << filename << "\n" << endi);
401 
403 }
#define DebugM(x, y)
Definition: Debug.h:75
std::ostream & endi(std::ostream &s)
Definition: InfoStream.C:54
MIStream * get(char &data)
Definition: MStream.h:29
char filename[NAMD_FILENAME_BUFFER_SIZE]
virtual void unpack(MIStream *msg)
void buildSubgridsFlat(void)

Friends And Related Function Documentation

◆ GridforceFullBaseGrid

friend class GridforceFullBaseGrid
friend

Definition at line 191 of file GridForceGrid.h.

◆ GridforceFullSubGrid

friend class GridforceFullSubGrid
friend

Definition at line 192 of file GridForceGrid.h.

Member Data Documentation

◆ border

int GridforceFullMainGrid::border
protected

Definition at line 237 of file GridForceGrid.h.

Referenced by get_border(), and initialize().

◆ default_border

const int GridforceFullMainGrid::default_border = 1
staticprotected

Definition at line 236 of file GridForceGrid.h.

Referenced by initialize().

◆ filename

char GridforceFullMainGrid::filename[NAMD_FILENAME_BUFFER_SIZE]
protected

Definition at line 231 of file GridForceGrid.h.

Referenced by initialize(), pack(), reinitialize(), and unpack().

◆ subgrids_flat

GridforceFullSubGrid** GridforceFullMainGrid::subgrids_flat
protected

◆ totalGrids

int GridforceFullMainGrid::totalGrids
protected

The documentation for this class was generated from the following files: