static int read_xyz_structure(void *mydata, int *optflags, molfile_atom_t *atoms) { int i, j; char *k; float coord; molfile_atom_t *atom; xyzdata *data = (xyzdata *)mydata; char buffer[1024], fbuffer[1024]; /* skip over the first two lines */ if (NULL == fgets(fbuffer, 1024, data->file)) return MOLFILE_ERROR; if (NULL == fgets(fbuffer, 1024, data->file)) return MOLFILE_ERROR; /* we set atom mass and VDW radius from the PTE. */ *optflags = MOLFILE_ATOMICNUMBER | MOLFILE_MASS | MOLFILE_RADIUS; for(i=0; i<data->numatoms; i++) { k = fgets(fbuffer, 1024, data->file); atom = atoms + i; j=sscanf(fbuffer, "%s %f %f %f", buffer, &coord, &coord, &coord); if (k == NULL) { fprintf(stderr, "xyz structure) missing atom(s) in file '%s'\n", data->file_name); fprintf(stderr, "xyz structure) expecting '%d' atoms, found only '%d'\n", data->numatoms, i); return MOLFILE_ERROR; } else if (j < 4) { fprintf(stderr, "xyz structure) missing type or coordinate(s) in file '%s' for atom '%d'\n", data->file_name, i+1); return MOLFILE_ERROR; } /* handle the case if the first item is an ordinal number * from the PTE */ if (isdigit(buffer[0])) { int idx; idx = atoi(buffer); strncpy(atom->name, get_pte_label(idx), sizeof(atom->name)); atom->atomicnumber = idx; atom->mass = get_pte_mass(idx); atom->radius = get_pte_vdw_radius(idx); } else { int idx; strncpy(atom->name, buffer, sizeof(atom->name)); idx = get_pte_idx(buffer); atom->atomicnumber = idx; atom->mass = get_pte_mass(idx); atom->radius = get_pte_vdw_radius(idx); } strncpy(atom->type, atom->name, sizeof(atom->type)); atom->resname[0] = '\0'; atom->resid = 1; atom->chain[0] = '\0'; atom->segid[0] = '\0'; /* skip to the end of line */ } rewind(data->file); return MOLFILE_SUCCESS; }
PsfType::PsfType(char* myelem) { atommass = get_pte_mass(get_pte_idx(myelem)); strcpy(element,get_pte_label(atoi(eatwhite(myelem)))); element[2]='\0'; strcpy(atomtype,"XX"); // atomtype[2]='\0'; strcat(atomtype,element); next=NULL; //fprintf(stdout,"New type created: %s %s \n",atomtype,element); }
int main() { int i; printf("Periodic table check/dump\n"); printf(" Table contains data for %d elements\n", nr_pte_entries); printf(" Mass table size check: %d\n", sizeof(pte_mass) / sizeof(float)); printf(" VDW table size check: %d\n", sizeof(pte_vdw_radius) / sizeof(float)); printf("\n"); printf("Symbol Num Mass rVDW\n"); for (i=0; i<nr_pte_entries; i++) { printf(" %-2s %3d %6.2f %4.2f\n", get_pte_label(i), i, get_pte_mass(i), get_pte_vdw_radius(i)); } return 0; }
static int read_vaspoutcar_structure(void *mydata, int *optflags, molfile_atom_t *atoms) { vasp_plugindata_t *data = (vasp_plugindata_t *)mydata; FILE *potcar; char lineptr[LINESIZE], potcarfile[1000]; float atommass[MAXATOMTYPES]; int atomcount, typecount, i; if (!data || !optflags || !atoms) return MOLFILE_ERROR; *optflags = MOLFILE_MASS; /* we set atom mass from the PTE. */ *optflags |= MOLFILE_ATOMICNUMBER | MOLFILE_RADIUS; typecount = 0; atomcount = 0; while (fgets(lineptr, LINESIZE, data->file) && atomcount < data->numatoms) { if (strstr(lineptr, "POMASS") != NULL) sscanf(lineptr, " POMASS = %f;", &atommass[typecount++]); if (strstr(lineptr, "ions per type =") != NULL) { char const *token = strtok(lineptr, "="); for (i = 0; i < typecount; ++i) { token = strtok(NULL, " "); atomcount += data->eachatom[i] = atoi(token); } } } if (atomcount != data->numatoms) { fprintf(stderr, "\n\nVASP OUTCAR read) ERROR: file '%s' does not have number of each atom.\n", data->filename); return MOLFILE_ERROR; } /* Read POTCAR file to determine atom types. * Each atom type section in POTCAR starts with a line * that contains the name of the element (H, He, C etc.). * Otherwise try to find similar mass in periodic table. */ if (strstr(potcarfile, "OUTCAR")) { strcpy(potcarfile, data->filename); strcpy(strstr(potcarfile, "OUTCAR"), "POTCAR"); potcar = fopen(potcarfile, "r"); } else { potcar = NULL; } for (atomcount = i = 0; atomcount < data->numatoms; ++i) { char const *label; float mass, radius; int k, idx = 0; if (potcar) { /* Obtain atom types from POTCAR file */ char atomtype[5] = "X"; if (fgets(lineptr, LINESIZE, potcar)) sscanf(lineptr, "%*s %4[^_. 0-9]", atomtype); idx = get_pte_idx(atomtype); /* Skip lines in potcar file until next element */ while (fgets(lineptr, LINESIZE, potcar)) if (strstr(lineptr, "End of Dataset")) break; } if (idx == 0) { /* Try to find atom type by browsing through periodic table */ idx = sizeof(pte_mass)/sizeof(pte_mass[0]); do idx--; while (idx > 0 && fabs(get_pte_mass(idx) - atommass[i]) > 0.01); } label = get_pte_label(idx); mass = (idx ? get_pte_mass(idx) : atommass[i]); radius = get_pte_vdw_radius(idx); for (k = 0; k < data->eachatom[i]; ++k, ++atomcount) { molfile_atom_t *atom = &(atoms[atomcount]); /* Required settings */ strncpy(atom->name, label, sizeof(atom->name)); strncpy(atom->type, atom->name, sizeof(atom->type)); atom->resname[0] = '\0'; atom->resid = 1; atom->segid[0]='\0'; atom->chain[0]='\0'; /* Optional flags (as defined in *optflags) */ atom->mass = mass; atom->radius = radius; atom->atomicnumber = idx; } } if (potcar) fclose(potcar); if (atomcount != data->numatoms) { fprintf(stderr, "\n\nVASP OUTCAR read) ERROR: file '%s' does contain list of atom names.\n", data->filename); return MOLFILE_ERROR; } atomcount = 0; while (fgets(lineptr, LINESIZE, data->file) && atomcount == 0) { if (strstr(lineptr, "position of ions in cartesian coordinates") != NULL) { for (i = 0; i < data->numatoms; ++i, ++atomcount) { float coord; fgets(lineptr, LINESIZE, data->file); if (3 != sscanf(lineptr, "%f %f %f", &coord, &coord, &coord)) { fprintf(stderr, "\n\nVASP OUTCAR read) missing type or coordinate(s) in file '%s' for atom '%d'\n", data->filename, i+1); return MOLFILE_ERROR; } } } } if (atomcount != data->numatoms) { fprintf(stderr, "\n\nVASP OUTCAR read) ERROR: file '%s' does contain list of atom names.\n", data->filename); return MOLFILE_ERROR; } rewind(data->file); return MOLFILE_SUCCESS; }
static int read_vasp5xdatcar_structure(void *mydata, int *optflags, molfile_atom_t *atoms) { vasp_plugindata_t *data = (vasp_plugindata_t *)mydata; FILE *potcar = NULL; int atomcount, i; char lineptr[LINESIZE], potcarfile[1000], *cp; float lc; if (!data || !optflags || !atoms) return MOLFILE_ERROR; *optflags = MOLFILE_MASS; /* we set atom mass from the PTE. */ *optflags |= MOLFILE_ATOMICNUMBER | MOLFILE_RADIUS; strcpy(potcarfile, data->filename); cp = strstr(potcarfile, "XDATCAR"); if (cp) { strcpy(cp, "POTCAR"); potcar = fopen(potcarfile, "r"); } /* Read POTCAR file to determine atom types. * Each atom type section in POTCAR starts with a line * that contains the name of the element (H, He, C etc.). * Otherwise try the title line instead. */ for (atomcount = i = 0; atomcount < data->numatoms; ++i) { int idx, j; char const *label; float mass, radius; if (potcar) { char atomtype[5] = "X"; /* Obtain atom types from POTCAR file */ if (fgets(lineptr, LINESIZE, potcar)) sscanf(lineptr, "%*s %4[^_. 0-9]", atomtype); idx = get_pte_idx(atomtype); /* Skip lines in potcar file until next element */ while (fgets(lineptr, LINESIZE, potcar)) if (strstr(lineptr, "End of Dataset")) break; } else { /* Try to obtain atom types from title line */ char const *token = (i == 0 ? strtok(data->titleline, " ") : strtok(NULL, " ")); idx = get_pte_idx(token); } label = get_pte_label(idx); mass = get_pte_mass(idx); radius = get_pte_vdw_radius(idx); for (j = 0; j < data->eachatom[i]; ++j, ++atomcount) { molfile_atom_t *const atom = &(atoms[atomcount]); /* Required settings */ strncpy(atom->name, label, sizeof(atom->name)); strncpy(atom->type, atom->name, sizeof(atom->type)); atom->resname[0] = '\0'; atom->resid = 1; atom->segid[0]='\0'; atom->chain[0]='\0'; /* Optional flags (as defined in *optflags) */ atom->mass = mass; atom->radius = radius; atom->atomicnumber = idx; } } if (potcar) fclose(potcar); if (atomcount != data->numatoms) { fprintf(stderr, "\n\nVASP5 XDATCAR read) ERROR: file '%s' doesn't seem to have list of atoms.\n", data->filename); return MOLFILE_ERROR; } for (i = 0; i < 2; ++i) fgets(lineptr, LINESIZE, data->file); sscanf(lineptr, "%f", &lc); fprintf(stderr, "%f\n", lc); for (i = 0; i < 3; ++i) { float x, y, z; fgets(lineptr, LINESIZE, data->file); sscanf(lineptr, "%f %f %f", &x, &y, &z); data->cell[i][0] = x*lc; data->cell[i][1] = y*lc; data->cell[i][2] = z*lc; } vasp_buildrotmat(data); /* Ignore header until X,Y,Z-coordinates */ for (i = 0; i < 3; ++i) fgets(lineptr, LINESIZE, data->file); /* Check whether all coordinates are present in the file */ for (i = 0; i < data->numatoms; ++i) { float coord; fgets(lineptr, LINESIZE, data->file); if (3 != sscanf(lineptr, "%f %f %f", &coord, &coord, &coord)) { fprintf(stderr, "\n\nVASP5 XDATCAR read) ERROR: structure is missing type or coordinate(s) in file '%s' for atom '%d'\n", data->filename, i+1); return MOLFILE_ERROR; } } rewind(data->file); /* Ignore header until X,Y,Z-coordinates */ for (i = 0; i < 8; ++i) fgets(lineptr, LINESIZE, data->file); return MOLFILE_SUCCESS; }
static int read_vaspxml_structure(void *mydata, int *optflags, molfile_atom_t *atoms) { vasp_plugindata_t *data = (vasp_plugindata_t *)mydata; int atomcount, coordscount, finished; char lineptr[LINESIZE]; /* Verify that input is OK */ if (!data || !optflags || !atoms) return MOLFILE_ERROR; *optflags = MOLFILE_MASS; /* we set atom mass from the PTE. */ *optflags |= MOLFILE_ATOMICNUMBER | MOLFILE_RADIUS; /* Scan xml file */ atomcount = coordscount = finished = 0; while (fgets(lineptr, LINESIZE, data->file) && !finished) { /* Extract atom types */ if (strstr(lineptr, "atomtype") != NULL && atomcount == 0) { int i; fgets(lineptr, LINESIZE, data->file); for (i = 0; i < data->numatoms; ++i, ++atomcount) { molfile_atom_t *atom = &(atoms[i]); char atomtype[5]; int idx; fgets(lineptr, LINESIZE, data->file); if (1 != sscanf(lineptr, " <rc><c> %4s </c>", atomtype)) break; idx = get_pte_idx(atomtype); /* Required settings */ strncpy(atom->name, get_pte_label(idx), sizeof(atom->name)); strncpy(atom->type, atom->name, sizeof(atom->type)); atom->resname[0] = '\0'; atom->resid = 1; atom->segid[0] ='\0'; atom->chain[0] = '\0'; /* Optional flags (as defined in *optflags) */ atom->mass = get_pte_mass(idx); atom->radius = get_pte_vdw_radius(idx); atom->atomicnumber = idx; } /* Verify presence of coordinates for all atoms */ } else if (strstr(lineptr, "positions") != NULL && coordscount == 0) { int i; for (i = 0; i < data->numatoms && fgets(lineptr, LINESIZE, data->file); ++i) { float x, y, z; if (3 != sscanf(lineptr, " <v> %f %f %f <\v>", &x, &y, &z)) break; } coordscount = 3 * i; } finished = atomcount != 0 && coordscount != 0; } if (atomcount != data->numatoms) { fprintf(stderr, "\n\nVASP xml read) ERROR: file '%s' does not have list of atom names.\n", data->filename); return MOLFILE_ERROR; } if (coordscount != 3 * data->numatoms) { fprintf(stderr, "\n\nVASP xml read) file '%s' does not contain coordinates of all atoms.\n", data->filename); return MOLFILE_ERROR; } rewind(data->file); return MOLFILE_SUCCESS; }
static int read_pdb_structure(void *mydata, int *optflags, molfile_atom_t *atoms) { pdbdata *pdb = (pdbdata *)mydata; molfile_atom_t *atom; char pdbrec[PDB_BUFFER_LENGTH]; int i, rectype, atomserial, pteidx; char ridstr[8]; char elementsymbol[3]; int badptecount = 0; long fpos = ftell(pdb->fd); *optflags = MOLFILE_INSERTION | MOLFILE_OCCUPANCY | MOLFILE_BFACTOR | MOLFILE_ALTLOC | MOLFILE_ATOMICNUMBER | MOLFILE_BONDSSPECIAL; i = 0; do { rectype = read_pdb_record(pdb->fd, pdbrec); switch (rectype) { case PDB_ATOM: atom = atoms+i; get_pdb_fields(pdbrec, strlen(pdbrec), &atomserial, atom->name, atom->resname, atom->chain, atom->segid, ridstr, atom->insertion, atom->altloc, elementsymbol, NULL, NULL, NULL, &atom->occupancy, &atom->bfactor); if (pdb->idxmap != NULL && atomserial < 100000) { pdb->idxmap[atomserial] = i; /* record new serial number translation */ } atom->resid = atoi(ridstr); /* determine atomic number from the element symbol */ pteidx = get_pte_idx_from_string(elementsymbol); atom->atomicnumber = pteidx; if (pteidx != 0) { atom->mass = get_pte_mass(pteidx); atom->radius = get_pte_vdw_radius(pteidx); } else { badptecount++; /* unrecognized element */ } strcpy(atom->type, atom->name); i++; break; case PDB_CONECT: /* only read CONECT records for structures where we know they can */ /* be valid for all of the atoms in the structure */ if (pdb->idxmap != NULL) { get_pdb_conect(pdbrec, pdb->natoms, pdb->idxmap, &pdb->maxbnum, &pdb->nbonds, &pdb->from, &pdb->to); } break; default: /* other record types are ignored in the structure callback */ /* and are dealt with in the timestep callback or elsewhere */ break; } } while (rectype != PDB_END && rectype != PDB_EOF); fseek(pdb->fd, fpos, SEEK_SET); /* if all atoms are recognized, set the mass and radius flags too, */ /* otherwise let VMD guess these for itself using it's own methods */ if (badptecount == 0) { *optflags |= MOLFILE_MASS | MOLFILE_RADIUS; } return MOLFILE_SUCCESS; }