Exemple #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;
}
static void *open_vasp5xdatcar_read(const char *filename, const char *filetype, int *natoms)
{
  vasp_plugindata_t *data;
  char lineptr[LINESIZE];
  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;

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

  data->filename = strdup(filename);

  /* Ignore rest of header up to the line with atom numbers */
  for (i = 0; i < 5; ++i) fgets(lineptr, LINESIZE, data->file);

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

  /* Read the number of atoms per atom type */
  data->numatoms = 0;
  fgets(lineptr, LINESIZE, data->file);
  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\nVASP5 XDATCAR read) ERROR: file '%s' does not have list of atom numbers.\n", filename);
    return NULL;
  }

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

  return data;
}
static void *open_vaspposcar_write(const char *filename, const char *filetype, int natoms)
{
  vasp_plugindata_t *data;

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

  data->file = fopen(filename, "w");
  if (!data->file) {
    vasp_plugindata_free(data);
    fprintf(stderr, "VASP POSCAR write) ERROR: Unable to open vaspposcar file '%s' for writing\n", filename);
    return NULL;
  }

  data->filename = strdup(filename);
  data->numatoms = natoms;

  return data;
}
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;
}
Exemple #5
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;
}