Esempio n. 1
0
static void *open_vaspoutcar_read(const char *filename, const char *filetype, int *natoms)
{
  vasp_plugindata_t *data;
  char lineptr[LINESIZE];

  /* Verify that input is OK */
  if (!filename || !natoms) return NULL;

  /* Start with undefined value; set it after successful read */
  *natoms = MOLFILE_NUMATOMS_UNKNOWN;

  data = vasp_plugindata_malloc();
  if (!data) return NULL;

  data->file = fopen(filename, "rb");
  if (!data->file) {
    vasp_plugindata_free(data);
    return NULL;
  }
  
  data->filename = strdup(filename);

  /* Catch total number of atoms */
  data->numatoms = 0;
  while (fgets(lineptr, LINESIZE, data->file) && data->numatoms == 0) {
   if (strstr(lineptr, "NIONS =") != NULL) {
      sscanf(lineptr, " %*[ a-zA-Z] = %*d %*[ a-zA-Z] = %d", &data->numatoms);
      break;
    }
  }

  if (data->numatoms <= 0) {
    vasp_plugindata_free(data);
    fprintf(stderr, "\n\nVASP OUTCAR read) ERROR: file '%s' does not contain the number of atoms.\n", filename);
    return NULL;
  }

  *natoms = data->numatoms;

  /* Catch the lattice vectors */
  while (fgets(lineptr, LINESIZE, data->file)) {
     if (strstr(lineptr, "direct lattice vectors") != NULL) {
       int i;
       for (i = 0; i < 3; ++i) {
	 fgets(lineptr, LINESIZE, data->file);
	 if (3 != sscanf(lineptr, "%f %f %f", &data->cell[i][0], &data->cell[i][1], &data->cell[i][2])) {
           vasp_plugindata_free(data);
           fprintf(stderr, "\n\nVASP OUTCAR read) ERROR: file '%s' does not contain lattice vectors.\n", filename);
           return NULL;
	 }
       }
       break;
     }
  }
  vasp_buildrotmat(data);

  rewind(data->file);

  return data;
}
Esempio n. 2
0
static void *open_vaspparchg_read(const char *filename, const char *filetype, int *natoms)
{
    vasp_plugindata_t *data;
    char lineptr[LINESIZE];
    float lc;
    int i;

    /* Verify that input is OK */
    if (!filename || !natoms) return NULL;

    /* Start with undefined value; set it after successful read */
    *natoms = MOLFILE_NUMATOMS_UNKNOWN;

    data = vasp_plugindata_malloc();
    if (!data) return NULL;

    /* VASP4 is assumed in default */
    data->version = 4;
    data->file = fopen(filename, "rb");
    if (!data->file) {
        vasp_plugindata_free(data);
        return NULL;
    }

    data->filename = strdup(filename);

    /* Read system title */
    fgets(lineptr, LINESIZE, data->file);
    data->titleline = strdup(lineptr);

    /* Read lattice constant */
    fgets(lineptr, LINESIZE, data->file);
    lc = atof(strtok(lineptr, " "));

    /* Read unit cell lattice vectors and multiply by lattice constant */
    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;
    }

    /* Build rotation matrix */
    vasp_buildrotmat(data);

    /* Count number of atoms */
    fgets(lineptr, LINESIZE, data->file);
    data->numatoms = 0;
    for (i = 0; i < MAXATOMTYPES; ++i) {
        char const *tmplineptr = strdup(lineptr);
        char const *token = (i == 0 ? strtok(lineptr, " ") : strtok(NULL, " "));
        int const n = (token ? atoi(token) : -1);

        /* if fails to read number of atoms, then assume VASP5 */
        if (i == 0 && n <= 0) {
            data->version = 5;
            data->titleline =  strdup(tmplineptr);
            fgets(lineptr, LINESIZE, data->file);
            break;
        } else if (n <= 0) break;

        data->eachatom[i] = n;
        data->numatoms += n;
    }

    if (data->version == 5) {
        data->numatoms = 0;
        for (i = 0; i < MAXATOMTYPES; ++i) {
            char const *token = (i == 0 ? strtok(lineptr, " ") : strtok(NULL, " "));
            int const n = (token ? atoi(token) : -1);

            if (n <= 0) break;

            data->eachatom[i] = n;
            data->numatoms += n;
        }
    }

    if (data->numatoms == 0) {
        vasp_plugindata_free(data);
        fprintf(stderr, "\n\nVASP PARCHG read) ERROR: file '%s' does not contain list of atom numbers.\n", filename);
        return NULL;
    }

    /* Skip lines up to the grid numbers */
    for (i = 0; i < data->numatoms + 2; ++i) fgets(lineptr, LINESIZE, data->file);

    *natoms = data->numatoms;

    return data;
}
Esempio n. 3
0
static void *open_vaspxml_read(const char *filename, const char *filetype, int *natoms)
{
  vasp_plugindata_t *data;
  char lineptr[LINESIZE];
  int cellcoords, finished;

  /* Verify that input is OK */
  if (!filename || !natoms) return NULL;

  /* Start with undefined value; set it after successful read */
  *natoms = MOLFILE_NUMATOMS_UNKNOWN;

  data = vasp_plugindata_malloc();
  if (!data) return NULL;

  data->file = fopen(filename, "rb");
  if (!data->file) {
    vasp_plugindata_free(data);
    return NULL;
  }

  data->filename = strdup(filename);

  /* Scan xml file */
  data->numatoms = cellcoords = finished = 0;
  while (fgets(lineptr, LINESIZE, data->file) && !finished) {

    if (strstr(lineptr, "SYSTEM") != NULL && data->titleline == NULL) {
       /* Extract title line */
       char *begin = strstr(lineptr, ">") + 1;
       char *end = strstr(lineptr, "</i>");
       if (end) end = '\0';
       if (begin) data->titleline = strdup(begin);

    } else if (strstr(lineptr, "atominfo") != NULL && data->numatoms == 0) {
       /* Extract number of atoms */
       fgets(lineptr, LINESIZE, data->file);
       sscanf(lineptr, " <atoms> %d </atoms>", &data->numatoms);

    } else if (strstr(lineptr, "crystal") != NULL && cellcoords == 0) {
       /* Extract lattice vectors */
       int i;
       fgets(lineptr, LINESIZE, data->file);
       for (i = 0; i < 3 && fgets(lineptr, LINESIZE, data->file); ++i) cellcoords += sscanf(lineptr, " <v> %f %f %f </v>", &data->cell[i][0], &data->cell[i][1], &data->cell[i][2]);
    }

    finished = data->titleline != NULL && data->numatoms != 0 && cellcoords != 0;
  }

  if (data->numatoms <= 0) {
     vasp_plugindata_free(data);
     fprintf(stderr, "\n\nVASP xml read) ERROR: file '%s' does not contain the number of atoms.\n", filename);
     return NULL;
  }

  if (cellcoords != 9) {
     vasp_plugindata_free(data);
     fprintf(stderr, "\n\nVASP xml read) ERROR: file '%s' does not contain lattice vectors.\n", filename);
     return NULL;
  }

  vasp_buildrotmat(data);

  *natoms = data->numatoms;
  rewind(data->file);

  return data;
}
Esempio n. 4
0
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_vaspposcar_timestep(void *mydata, int natoms, molfile_timestep_t *ts)
{
  int i, direct;
  char lineptr[LINESIZE];
  float lc;
  
  vasp_plugindata_t *data = (vasp_plugindata_t *)mydata;

  /* Save coords only if we're given a timestep pointer,
   * otherwise assume that VMD wants us to skip past it.
   */
  if (!ts || !data) return MOLFILE_EOF;

  /* VMD keeps calling for a next timestep, until we reach End-Of-File here */
  if (fgets(lineptr, LINESIZE, data->file) == NULL) return MOLFILE_EOF;

  fgets(lineptr, LINESIZE, data->file);
  sscanf(lineptr, "%f", &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);

  /* Skip numbers of atom types */
  for (i = 0; i < data->version - 2; ++i) fgets(lineptr, LINESIZE, data->file);

  /* Skip selective tag-line, starting with 's' or 'S'. */
  if (tolower(lineptr[0]) == 's') fgets(lineptr, LINESIZE, data->file);

  /* Detect direct coordinates tag, starting with 'd' or 'D'. */
  direct = (tolower(lineptr[0]) == 'd' ? 1 : 0);

  for (i = 0; i < data->numatoms; ++i) {
    float x, y, z, rotx, roty, rotz;
    fgets(lineptr, LINESIZE, data->file);
    if (3 != sscanf(lineptr, "%f %f %f", &x, &y, &z)) {
      fprintf(stderr, "VASP POSCAR read) missing type or coordinate(s) in file '%s' for atom '%d'\n", data->filename, i+1);
      return MOLFILE_EOF;
    }

    if (direct) {
       rotx = x*data->cell[0][0]+y*data->cell[1][0]+z*data->cell[2][0];
       roty = x*data->cell[0][1]+y*data->cell[1][1]+z*data->cell[2][1];
       rotz = x*data->cell[0][2]+y*data->cell[1][2]+z*data->cell[2][2];
    } else {
       rotx = x*lc;
       roty = y*lc;
       rotz = z*lc;
    }
    ts->coords[3*i  ] = data->rotmat[0][0]*rotx+data->rotmat[0][1]*roty+data->rotmat[0][2]*rotz;
    ts->coords[3*i+1] = data->rotmat[1][0]*rotx+data->rotmat[1][1]*roty+data->rotmat[1][2]*rotz;
    ts->coords[3*i+2] = data->rotmat[2][0]*rotx+data->rotmat[2][1]*roty+data->rotmat[2][2]*rotz;
  }

  vasp_timestep_unitcell(ts, data);

  /* POSCAR type files have only one single timestep.
   * Therefore proceed till end of file after reading the coordinates.
   */
  fseek(data->file, 0, SEEK_END);

  return MOLFILE_SUCCESS;
}