static void density_in_time (const char *fn, atom_id **index, int gnx[], real bw, real bwz, int nsttblock, real *****Densdevel, int *xslices, int *yslices, int *zslices, int *tblock, t_topology *top, int ePBC, int axis, gmx_bool bCenter, gmx_bool bps1d, const gmx_output_env_t *oenv) { /* * *****Densdevel pointer to array of density values in slices and frame-blocks Densdevel[*nsttblock][*xslices][*yslices][*zslices] * Densslice[x][y][z] * nsttblock - nr of frames in each time-block * bw widths of normal slices * * axis - axis direction (normal to slices) * nndx - number ot atoms in **index * grpn - group number in index */ t_trxstatus *status; gmx_rmpbc_t gpbc = NULL; matrix box; /* Box - 3x3 -each step*/ rvec *x0; /* List of Coord without PBC*/ int i, j, /* loop indices, checks etc*/ ax1 = 0, ax2 = 0, /* tangent directions */ framenr = 0, /* frame number in trajectory*/ slicex, slicey, slicez; /*slice # of x y z position */ real ***Densslice = NULL; /* Density-slice in one frame*/ real dscale; /*physical scaling factor*/ real t, x, y, z; /* time and coordinates*/ rvec bbww; *tblock = 0; /* blocknr in block average - initialise to 0*/ /* Axis: X=0, Y=1,Z=2 */ switch (axis) { case 0: ax1 = YY; ax2 = ZZ; /*Surface: YZ*/ break; case 1: ax1 = ZZ; ax2 = XX; /* Surface : XZ*/ break; case 2: ax1 = XX; ax2 = YY; /* Surface XY*/ break; default: gmx_fatal(FARGS, "Invalid axes. Terminating\n"); } if (read_first_x(oenv, &status, fn, &t, &x0, box) == 0) { gmx_fatal(FARGS, "Could not read coordinates from file"); /* Open trajectory for read*/ } *zslices = 1+static_cast<int>(std::floor(box[axis][axis]/bwz)); *yslices = 1+static_cast<int>(std::floor(box[ax2][ax2]/bw)); *xslices = 1+static_cast<int>(std::floor(box[ax1][ax1]/bw)); if (bps1d) { if (*xslices < *yslices) { *xslices = 1; } else { *yslices = 1; } } fprintf(stderr, "\nDividing the box in %5d x %5d x %5d slices with binw %f along axis %d\n", *xslices, *yslices, *zslices, bw, axis ); /****Start trajectory processing***/ /*Initialize Densdevel and PBC-remove*/ gpbc = gmx_rmpbc_init(&top->idef, ePBC, top->atoms.nr); *Densdevel = NULL; do { bbww[XX] = box[ax1][ax1]/ *xslices; bbww[YY] = box[ax2][ax2]/ *yslices; bbww[ZZ] = box[axis][axis]/ *zslices; gmx_rmpbc(gpbc, top->atoms.nr, box, x0); /*Reset Densslice every nsttblock steps*/ /* The first conditional is for clang to understand that this branch is * always taken the first time. */ if (Densslice == NULL || framenr % nsttblock == 0) { snew(Densslice, *xslices); for (i = 0; i < *xslices; i++) { snew(Densslice[i], *yslices); for (j = 0; j < *yslices; j++) { snew(Densslice[i][j], *zslices); } } /* Allocate Memory to extra frame in Densdevel - rather stupid approach: * A single frame each time, although only every nsttblock steps. */ srenew(*Densdevel, *tblock+1); (*Densdevel)[*tblock] = Densslice; } dscale = (*xslices)*(*yslices)*(*zslices)*AMU/ (box[ax1][ax1]*box[ax2][ax2]*box[axis][axis]*nsttblock*(NANO*NANO*NANO)); if (bCenter) { center_coords(&top->atoms, box, x0, axis); } for (j = 0; j < gnx[0]; j++) { /*Loop over all atoms in selected index*/ x = x0[index[0][j]][ax1]; y = x0[index[0][j]][ax2]; z = x0[index[0][j]][axis]; while (x < 0) { x += box[ax1][ax1]; } while (x > box[ax1][ax1]) { x -= box[ax1][ax1]; } while (y < 0) { y += box[ax2][ax2]; } while (y > box[ax2][ax2]) { y -= box[ax2][ax2]; } while (z < 0) { z += box[axis][axis]; } while (z > box[axis][axis]) { z -= box[axis][axis]; } slicex = static_cast<int>(x/bbww[XX]) % *xslices; slicey = static_cast<int>(y/bbww[YY]) % *yslices; slicez = static_cast<int>(z/bbww[ZZ]) % *zslices; Densslice[slicex][slicey][slicez] += (top->atoms.atom[index[0][j]].m*dscale); } framenr++; if (framenr % nsttblock == 0) { /*Implicit incrementation of Densdevel via renewal of Densslice*/ /*only every nsttblock steps*/ (*tblock)++; } } while (read_next_x(oenv, status, &t, x0, box)); /*Free memory we no longer need and exit.*/ gmx_rmpbc_done(gpbc); close_trj(status); if (0) { FILE *fp; fp = fopen("koko.xvg", "w"); for (j = 0; (j < *zslices); j++) { fprintf(fp, "%5d", j); for (i = 0; (i < *tblock); i++) { fprintf(fp, " %10g", (*Densdevel)[i][9][1][j]); } fprintf(fp, "\n"); } fclose(fp); } }
void calc_density(const char *fn, atom_id **index, int gnx[], double ***slDensity, int *nslices, t_topology *top, int ePBC, int axis, int nr_grps, real *slWidth, gmx_bool bCenter, atom_id *index_center, int ncenter, gmx_bool bRelative, const output_env_t oenv) { rvec *x0; /* coordinates without pbc */ matrix box; /* box (3x3) */ double invvol; int natoms; /* nr. atoms in trj */ t_trxstatus *status; int **slCount, /* nr. of atoms in one slice for a group */ i, j, n, /* loop indices */ ax1 = 0, ax2 = 0, nr_frames = 0, /* number of frames */ slice; /* current slice */ real t, z; real boxSz, aveBox; char *buf; /* for tmp. keeping atomname */ gmx_rmpbc_t gpbc = NULL; if (axis < 0 || axis >= DIM) { gmx_fatal(FARGS, "Invalid axes. Terminating\n"); } if ((natoms = read_first_x(oenv, &status, fn, &t, &x0, box)) == 0) { gmx_fatal(FARGS, "Could not read coordinates from statusfile\n"); } aveBox = 0; if (!*nslices) { *nslices = (int)(box[axis][axis] * 10); /* default value */ fprintf(stderr, "\nDividing the box in %d slices\n", *nslices); } snew(*slDensity, nr_grps); for (i = 0; i < nr_grps; i++) { snew((*slDensity)[i], *nslices); } gpbc = gmx_rmpbc_init(&top->idef, ePBC, top->atoms.nr); /*********** Start processing trajectory ***********/ do { gmx_rmpbc(gpbc, natoms, box, x0); /* Translate atoms so the com of the center-group is in the * box geometrical center. */ if (bCenter) { center_coords(&top->atoms, index_center, ncenter, box, x0); } invvol = *nslices/(box[XX][XX]*box[YY][YY]*box[ZZ][ZZ]); if (bRelative) { *slWidth = 1.0/(*nslices); boxSz = 1.0; } else { *slWidth = box[axis][axis]/(*nslices); boxSz = box[axis][axis]; } aveBox += box[axis][axis]; for (n = 0; n < nr_grps; n++) { for (i = 0; i < gnx[n]; i++) /* loop over all atoms in index file */ { z = x0[index[n][i]][axis]; while (z < 0) { z += box[axis][axis]; } while (z > box[axis][axis]) { z -= box[axis][axis]; } if (bRelative) { z = z/box[axis][axis]; } /* determine which slice atom is in */ if (bCenter) { slice = floor( (z-(boxSz/2.0)) / (*slWidth) ) + *nslices/2; } else { slice = floor(z / (*slWidth)); } /* Slice should already be 0<=slice<nslices, but we just make * sure we are not hit by IEEE rounding errors since we do * math operations after applying PBC above. */ if (slice < 0) { slice += *nslices; } else if (slice >= *nslices) { slice -= *nslices; } (*slDensity)[n][slice] += top->atoms.atom[index[n][i]].m*invvol; } } nr_frames++; } while (read_next_x(oenv, status, &t, x0, box)); gmx_rmpbc_done(gpbc); /*********** done with status file **********/ close_trj(status); /* slDensity now contains the total mass per slice, summed over all frames. Now divide by nr_frames and volume of slice */ fprintf(stderr, "\nRead %d frames from trajectory. Calculating density\n", nr_frames); if (bRelative) { aveBox /= nr_frames; *slWidth = aveBox/(*nslices); } for (n = 0; n < nr_grps; n++) { for (i = 0; i < *nslices; i++) { (*slDensity)[n][i] /= nr_frames; } } sfree(x0); /* free memory used by coordinate array */ }
void read_str_file() { int len = 1024; int readed = -1; STR buff; int file_line = 0; FILE* str_file; if( str_filename == NULL ) UNERR("str_filename not defined"); buff = (STR) malloc( sizeof(char) * len ); CHMEM(buff); str_file = fopen( str_filename, "r" ); if( str_file == NULL ) UNERR("Can't open str-file"); while( fgets( buff, len, str_file ) ) { if( readed ) /*Non empty line*/ { char* pos = strchr( buff, COMMENT_SIGN ); char* keyword; file_line++; if( pos ) *pos = '\0'; /* 'remove' comment */ if( !strlen(buff) ) continue; keyword = strtok( buff, " \t\n" ); if( !keyword ) continue; /* ok we have only spaces and tabs */ if( strcmp( keyword , SUB_TAG ) == 0 ) { parse_sub(); } else if( strcmp( keyword, BOND_TAG ) == 0 ) { parse_bond(); } else if( strcmp( keyword, ANGLE_TAG ) == 0 ) { parse_angle(); } else if( strcmp( keyword, DIHE_TAG ) == 0 ) { parse_dihe(); } else { warning( keyword, __FILE__, __LINE__ ); UNERR( "Unknow tag in str file" ); } } } end_sub(); end_angle(); end_angle_cos(); end_dihe(); end_dihe_angle(); end_bond(); free( buff ); fclose( str_file ); center_coords();/* Center coordinates */ }
void calc_electron_density(const char *fn, atom_id **index, int gnx[], double ***slDensity, int *nslices, t_topology *top, int ePBC, int axis, int nr_grps, real *slWidth, t_electron eltab[], int nr, gmx_bool bCenter, atom_id *index_center, int ncenter, gmx_bool bRelative, const output_env_t oenv) { rvec *x0; /* coordinates without pbc */ matrix box; /* box (3x3) */ double invvol; int natoms; /* nr. atoms in trj */ t_trxstatus *status; int i, n, /* loop indices */ nr_frames = 0, /* number of frames */ slice; /* current slice */ t_electron *found; /* found by bsearch */ t_electron sought; /* thingie thought by bsearch */ real boxSz, aveBox; gmx_rmpbc_t gpbc = NULL; real t, z; if (axis < 0 || axis >= DIM) { gmx_fatal(FARGS, "Invalid axes. Terminating\n"); } if ((natoms = read_first_x(oenv, &status, fn, &t, &x0, box)) == 0) { gmx_fatal(FARGS, "Could not read coordinates from statusfile\n"); } aveBox = 0; if (!*nslices) { *nslices = (int)(box[axis][axis] * 10); /* default value */ fprintf(stderr, "\nDividing the box in %d slices\n", *nslices); } snew(*slDensity, nr_grps); for (i = 0; i < nr_grps; i++) { snew((*slDensity)[i], *nslices); } gpbc = gmx_rmpbc_init(&top->idef, ePBC, top->atoms.nr); /*********** Start processing trajectory ***********/ do { gmx_rmpbc(gpbc, natoms, box, x0); /* Translate atoms so the com of the center-group is in the * box geometrical center. */ if (bCenter) { center_coords(&top->atoms, index_center, ncenter, box, x0); } invvol = *nslices/(box[XX][XX]*box[YY][YY]*box[ZZ][ZZ]); if (bRelative) { *slWidth = 1.0/(*nslices); boxSz = 1.0; } else { *slWidth = box[axis][axis]/(*nslices); boxSz = box[axis][axis]; } aveBox += box[axis][axis]; for (n = 0; n < nr_grps; n++) { for (i = 0; i < gnx[n]; i++) /* loop over all atoms in index file */ { z = x0[index[n][i]][axis]; while (z < 0) { z += box[axis][axis]; } while (z > box[axis][axis]) { z -= box[axis][axis]; } if (bRelative) { z = z/box[axis][axis]; } /* determine which slice atom is in */ if (bCenter) { slice = floor( (z-(boxSz/2.0)) / (*slWidth) ) + *nslices/2; } else { slice = (z / (*slWidth)); } sought.nr_el = 0; sought.atomname = gmx_strdup(*(top->atoms.atomname[index[n][i]])); /* now find the number of electrons. This is not efficient. */ found = (t_electron *) bsearch((const void *)&sought, (const void *)eltab, nr, sizeof(t_electron), (int(*)(const void*, const void*))compare); if (found == NULL) { fprintf(stderr, "Couldn't find %s. Add it to the .dat file\n", *(top->atoms.atomname[index[n][i]])); } else { (*slDensity)[n][slice] += (found->nr_el - top->atoms.atom[index[n][i]].q)*invvol; } free(sought.atomname); } } nr_frames++; } while (read_next_x(oenv, status, &t, x0, box)); gmx_rmpbc_done(gpbc); /*********** done with status file **********/ close_trj(status); /* slDensity now contains the total number of electrons per slice, summed over all frames. Now divide by nr_frames and volume of slice */ fprintf(stderr, "\nRead %d frames from trajectory. Counting electrons\n", nr_frames); if (bRelative) { aveBox /= nr_frames; *slWidth = aveBox/(*nslices); } for (n = 0; n < nr_grps; n++) { for (i = 0; i < *nslices; i++) { (*slDensity)[n][i] /= nr_frames; } } sfree(x0); /* free memory used by coordinate array */ }