Beispiel #1
0
gint core_match(const gchar *label, struct core_pak *core)
{
gint code;

#if DEBUG_ATOM_MATCH
printf("[%s] : [%s]\n", label, core->atom_label);
#endif

code = elem_symbol_test(label);

/* if input label doesnt match the element symbol length - it means the */
/* user has put in something like H1 - compare this with the atom label */
if (code)
  {
  if (g_ascii_strcasecmp(label, elements[core->atom_code].symbol) != 0)
    {
    if (g_ascii_strcasecmp(core->atom_label, label) == 0)
      return(1);
    }
  else
    return(1);
  }
else
  return(1);

#if DEBUG_ATOM_MATCH
printf("rejected.\n");
#endif

return(0);
}
Beispiel #2
0
/* TEMP - parse diffax sfc data */
void parse_sfc(FILE *fp)
{
gint i, n;
gchar *line, *type, **buff;
gdouble *sfc;
GSList *list;

while ((line = file_read_line(fp)))
  {
  type = g_strstrip(g_strndup(line, 6));

  if (elem_symbol_test(type))
    {
/* tokenize everything after the atom type */
    buff = tokenize(&line[6], &n);

printf("[%s] ", type);
    list = NULL;
    for (i=0 ; i<n ; i++)
      {
      sfc = g_malloc(sizeof(gdouble));
      *sfc = str_to_float(*(buff+i));

printf("[%f] ", *sfc);
      list = g_slist_prepend(list, sfc);
      }
    list = g_slist_reverse(list);
printf("\n");

    g_hash_table_insert(sysenv.sfc_table, type, list);

    g_strfreev(buff);
    }
  g_free(line);
  }
}
Beispiel #3
0
gint read_aims(gchar *filename, struct model_pak *model)
{
gint i, num_tokens;
gchar **buff;
gpointer scan;
struct core_pak *core;

g_assert(model != NULL);

scan = scan_new(filename);
if (!scan) return(1);

while (!scan_complete(scan)) {

  buff = scan_get_tokens(scan, &num_tokens);

/* 
   for debugging purposes 
   produces a compiler warning about an
	implicit declaration of function 'g_printf'
   though this is a valid glib function since 2.2
	http://library.gnome.org/devel/glib/2.18/glib-String-Utility-Functions.html#g-printf
*/
/*
  for (i=0; i<num_tokens; i++) {
	g_printf(" %s ", buff[i]);
  }
  printf("\n");
*/

  if (!buff) break;

  /* read cell vectors */
  if ( g_strrstr(*buff, "lattice_vector") != NULL ) {
     for (i=0 ; i<3 ; i++) {
         if (num_tokens >= 3) {
            model->latmat[i] = str_to_float(*(buff+1));
            model->latmat[i+3] = str_to_float(*(buff+2));
            model->latmat[i+6] = str_to_float(*(buff+3));
         }
         else { 
            gui_text_show(ERROR, "error reading AIMS lattice vectors \n"); 
            return(2);
         }
         g_strfreev(buff);
         buff = scan_get_tokens(scan, &num_tokens);
     }
     model->periodic = 3;
     model->construct_pbc = TRUE;
  }

  /* read coordinates */
  if ( g_strrstr(*buff, "atom") != NULL ) {
     if ( ( num_tokens >= 4 ) && ( elem_symbol_test(*(buff+4)) ) ) {
        core = new_core(*(buff+4), model);
        core->x[0] = str_to_float(*(buff+1));
        core->x[1] = str_to_float(*(buff+2));
        core->x[2] = str_to_float(*(buff+3));
        model->cores = g_slist_prepend(model->cores, core);
     }
     else {
        gui_text_show(ERROR, "error reading AIMS lattice vectors \n"); 
        return(2);
     }
  }

  g_strfreev(buff);
}

/* done reading */
scan_free(scan);

/* model setup */
g_free(model->filename);
model->filename = g_strdup(filename);

g_free(model->basename);
model->basename = g_path_get_basename(filename);

model->fractional = FALSE;
model_prep(model);

return(0);
}
Beispiel #4
0
gint read_cel(gchar *filename, struct model_pak *model)
{
gchar *line;
FILE *fp;
int i;
gint num_tokens, natom=0;
gchar **buff;

struct core_pak *core;

/* checks */
g_return_val_if_fail(model != NULL, 1);
g_return_val_if_fail(filename != NULL, 2);

fp = fopen(filename, "rt");
if (!fp)
  return 3;

/* 1st line  - cell parameters */
line = file_read_line(fp);
if (!line || strlen(line) < 5 || g_ascii_strncasecmp("cell", line, 4) != 0)
  {
  printf("The first line should start with the keyword CELL.\n");
  return 4;
  }
buff = tokenize(line+4, &num_tokens);
g_free(line);
if (num_tokens < 6)
  {
  g_strfreev(buff);
  printf("Keyword CELL should be followed by six numbers.\n");
  return 5;
  }
for (i=0; i<3; ++i)
  model->pbc[i] = str_to_float(buff[i]);
for (i=3; i<6; ++i)
  model->pbc[i] = str_to_float(buff[i]) * D2R;
g_strfreev(buff);

/* next lines - atomic positions */
for (;;)
  {
  line = file_read_line(fp);
  if (!line) /*the end of file*/
    {
    printf("No 'rgnr' symmetry line found.\n");
    return 6;
    }
  else if (g_ascii_strncasecmp("rgnr", line, 4) == 0) /*no more atomic pos.*/
    {
    break;
    }
  else if (g_ascii_strncasecmp("natom", line, 5) == 0)/*number of atoms */
    /*some old .cel files have second line with number of atoms eg. "natom 6"*/
    {
    buff = tokenize(line, &num_tokens);
    if (num_tokens > 1)
      natom = str_to_float(buff[1]);
    else
      printf("Warning: ignoring `natom' line:\n%s\n", line);
    g_free(line);
    g_strfreev(buff);
    }
  else if (strncmp("    ", line, 4) != 0) /* atomic position */
    {
    buff = tokenize(line, &num_tokens);
    g_free(line);
    if (num_tokens < 5)
      {
      g_strfreev(buff);
      continue;
      }
    core = new_core(*buff, model);
    core->atom_label = g_strdup(buff[0]);

    /* in second column there is either atomic number 
     * or something like "Mg2+" or "K+". The second form is for
     * " the use of different bonding states of one and the same 
     *   element (e.g. Fe2+ and Fe3+ in Fe3O4)"
     * FIXME how these bonding states can be interpreted in GDIS */
    if (g_ascii_isdigit(buff[1][0]))
      core->atom_code = str_to_float(buff[1]);
    else 
      {
      core->atom_code = elem_symbol_test(buff[1]);
      }
    for (i=0; i<3; ++i)
      core->x[i] = str_to_float(buff[2+i]);
    /* TODO interpret 2 next optional numbers:
     *   so-called multiplied substitution and replacement factor (SOF)
     *   and isotropic Debye-Waller factor
     * FIXME can they be interpreted by GDIS? -MW */
    model->cores = g_slist_prepend(model->cores, core);
    g_strfreev(buff);
    }
  else /* replacement atom */
    {
    /*FIXME replacement atoms are now silently ignored 
     *  how can I use this information in GDIS? - MW*/ 
    g_free(line);
    }
  }

/* last line - symmetry */
buff = tokenize(line, &num_tokens);
model->sginfo.spacenum = str_to_float(buff[1]);
/* FIXME/TODO how to interpret the second (optional) number?
 * From fileformat docs: 
 * "sometimes there exists more than one setting of a space-group type. 
 * Thus, a further number must be given if the structure hasn't been described 
 * using a conventional setting (standard setting)."
 * http://users.omskreg.ru/~kolosov/bam/a_v/v_1/powder/details/strucdat.htm
 * http://users.omskreg.ru/~kolosov/bam/a_v/v_1/powder/details/setting.htm
 * Unfortunatelly I'm ignorant about space-groups - MW
 */
g_free(line);
g_strfreev(buff);

if (natom>0 && natom != g_slist_length(model->cores))
  printf("Warning: expected %i atoms, have %i.", natom, 
		                                 g_slist_length(model->cores));

/* model setup */
model->fractional = TRUE;
model->periodic = 3;
strcpy(model->filename, filename);
g_free(model->basename);
model->basename = parse_strip(filename);

model_prep(model);

return 0;
}
Beispiel #5
0
gint read_dmol_frame(FILE *fp, struct model_pak *model)
{
gint i, num_tokens;
gchar *line, **buff;
struct core_pak *core;

g_assert(fp != NULL);

line = file_read_line(fp);

while (line)
  {
/* read cell vectors */
  if (g_ascii_strncasecmp(line, "$cell", 5) == 0 && model)
    {
    for (i=0 ; i<3 ; i++)
      {
      g_free(line);
      line = file_read_line(fp);
      buff = tokenize(line, &num_tokens);
      if (num_tokens > 2)
        {
        model->latmat[i] = AU2ANG*str_to_float(*(buff));
        model->latmat[i+3] = AU2ANG*str_to_float(*(buff+1));
        model->latmat[i+6] = AU2ANG*str_to_float(*(buff+2));
        }
      g_strfreev(buff);
      }
    model->periodic = 3;
    model->construct_pbc = TRUE;
    }

/* read coordinates */
  if (g_ascii_strncasecmp(line, "$coord", 5) == 0 && model)
    {
    g_free(line);
    line = file_read_line(fp);
    buff = tokenize(line, &num_tokens);
    while (num_tokens > 3)
      {
      if (elem_symbol_test(*buff))
        {
        core = new_core(*buff, model);
        model->cores = g_slist_prepend(model->cores, core);
        core->x[0] = AU2ANG*str_to_float(*(buff+1));
        core->x[1] = AU2ANG*str_to_float(*(buff+2));
        core->x[2] = AU2ANG*str_to_float(*(buff+3));
        }
      g_free(line);
      line = file_read_line(fp);
      g_strfreev(buff);
      buff = tokenize(line, &num_tokens);
      }
    g_strfreev(buff);
    model->fractional = FALSE;
    }

/* terminate frame read */
  if (g_ascii_strncasecmp(line, "$end", 4) == 0)
    return(0);

  g_free(line);
  line = file_read_line(fp);
  }

return(1);
}
Beispiel #6
0
gint read_diffax(gchar *filename, struct model_pak *model)
{
gint num_tokens, num_layer, tot_layer;
gdouble offset;
gchar **buff;
GSList *list1, *list2;
struct core_pak *core;
struct layer_pak *layer;
FILE *fp;

/* checks */
g_return_val_if_fail(model != NULL, 1);
g_return_val_if_fail(filename != NULL, 2);
fp = fopen(filename, "rt");
if (!fp)
  return(3);

/* setup */
model->id = DIFFAX_INP;
model->fractional = TRUE;
model->periodic = 3;
model->colour_scheme = REGION;

strcpy(model->filename, filename);
g_free(model->basename);
model->basename = parse_strip(filename);

/* scan the file */
while ((buff = get_tokenized_line(fp, &num_tokens)))
  {
  diffax_keyword_search:;

/* restricted unit cell */
  if (g_ascii_strncasecmp("structural", *buff, 10) == 0)
    {
    g_strfreev(buff);
    buff = get_tokenized_line(fp, &num_tokens);
    if (num_tokens > 3)
      {
      model->pbc[0] = str_to_float(*(buff+0));
      model->pbc[1] = str_to_float(*(buff+1));
      model->pbc[2] = str_to_float(*(buff+2));
      model->pbc[3] = PI/2.0;
      model->pbc[4] = PI/2.0;
      model->pbc[5] = D2R*str_to_float(*(buff+3));
      }
    }

/* layer testing */
  if (g_ascii_strncasecmp("layer", *buff, 5) == 0)
    {
    layer = g_malloc(sizeof(struct model_pak));
    layer->width = 1.0;
    VEC3SET(layer->centroid, 0.5, 0.5, 0.5);
    layer->cores = NULL;
    model->layer_list = g_slist_prepend(model->layer_list, layer);

    g_strfreev(buff);
    buff = get_tokenized_line(fp, &num_tokens);
    if (buff)
      {
/* TODO - if centrosymmetric : add a -1 operation */
      }

/* get layer data */
    g_strfreev(buff);
    buff = get_tokenized_line(fp, &num_tokens);
    while (buff)
      {
      if (elem_symbol_test(*buff))
        {
        if (num_tokens > 6)
          {
/*
printf("[%s] [%s  %s  %s]\n", *buff, *(buff+2), *(buff+3), *(buff+4));
*/
          core = new_core(*buff, model);
          model->cores = g_slist_prepend(model->cores, core);
          layer->cores = g_slist_prepend(layer->cores, core);

          core->x[0] = str_to_float(*(buff+2));
          core->x[1] = str_to_float(*(buff+3));
          core->x[2] = str_to_float(*(buff+4));

          core->sof = str_to_float(*(buff+5));
          }
        }
      else
        goto diffax_keyword_search;

/* get next line of tokens */
      g_strfreev(buff);
      buff = get_tokenized_line(fp, &num_tokens);
      }
    }

  g_strfreev(buff);
  }

/* TODO - enumerate layers and scale, so they are stacked 1..n in a single cell */
/* also label the layers as different region types */
model->layer_list = g_slist_reverse(model->layer_list);
num_layer = 0;
tot_layer = g_slist_length(model->layer_list);

model->pbc[2] *= tot_layer;

#if DEBUG_READ_DIFFAX
printf("Read in %d layers.\n", tot_layer);
#endif

for (list1=model->layer_list ; list1 ; list1=g_slist_next(list1))
  {
  layer = (struct layer_pak *) list1->data;
  layer->width = 1.0 / (gdouble) tot_layer;

  offset = (gdouble) num_layer * layer->width;

  VEC3SET(layer->centroid, 0.0, 0.0, offset);

  for (list2=layer->cores ; list2 ; list2=g_slist_next(list2))
    {
    core = (struct core_pak *) list2->data;

/* scale to within the big cell (encloses all DIFFAX layers) */
    core->x[2] *= layer->width;

/* offset each particular layer */
    core->x[2] += offset;

    core->region = num_layer;
    }
  num_layer++;
  }

/* end of read */
fclose(fp);

/* post read setup */
model->cores = g_slist_reverse(model->cores);
model_prep(model);

return(0);
}
Beispiel #7
0
gint read_rietica(gchar *filename, struct model_pak *model)
{
gint i, phases=0, skip, num_tokens;
gchar **buff, *line;
float x, y, z;
gpointer scan;
GSList *list;
struct core_pak *core;

/* checks */
g_assert(model != NULL);
scan = scan_new(filename);
if (!scan)
  return(1);

/* FIXME - stop the previous file routines setting this */
model->id = -1;

while (!scan_complete(scan))
  {
  buff = scan_get_tokens(scan, &num_tokens);
  
/* search for structure start */
  if (num_tokens)
    {
    if (g_ascii_strncasecmp(*buff, "***", 3) == 0)
      {
      if (phases)
        model = model_new();
      phases++;

/* structure name - omit the 1st and last tokens (ie "***") */
      if (num_tokens > 1)
        {
        g_free(*(buff+num_tokens-1));
        *(buff+num_tokens-1) = NULL;
        g_free(model->basename);
        model->basename = g_strjoinv(" ", buff+1);
        }

/* parse spacegroup */
      line = scan_get_line(scan);
      line = scan_get_line(scan);
      model->sginfo.spacename = g_strstrip(g_strdup(line));
      model->sginfo.spacenum = -1;

/* parse a structure */
      skip = 0;
      while (!scan_complete(scan))
        {
        g_strfreev(buff);
        buff = scan_get_tokens(scan, &num_tokens);

        if (num_tokens)
          {
          if (elem_symbol_test(*buff))
            {
/* new core */
/*
            if (num_tokens > 6)
*/
              {
              core = new_core(*buff, model);
              model->cores = g_slist_prepend(model->cores, core);

/* formatted output can result in -ve signs "joining" tokens */
              line = scan_cur_line(scan);
/* no doubt some fortran programmer thought this was a clever format */
              sscanf(line, "%*16c%8f%8f%8f", &x, &y, &z);
              VEC3SET(core->x, x, y, z);

/*
printf("> %s", line);
P3VEC(" - ", core->x);
              core->x[0] = str_to_float(*(buff+2));
              core->x[1] = str_to_float(*(buff+3));
              core->x[2] = str_to_float(*(buff+4));
              core->sof = str_to_float(*(buff+6));
*/

              skip = 0;
              }
            }
          else
            skip++;
          }

/* 4 lines after the last core - parse cell info and terminate structure */
        if (skip == 4)
          {
          if (num_tokens > 5)
            {
            for (i=6 ; i-- ; )
              model->pbc[i] = str_to_float(*(buff+i));
            model->pbc[3] *= D2R;
            model->pbc[4] *= D2R;
            model->pbc[5] *= D2R;
            }
          break;
          }
        }
      }
    }
  g_strfreev(buff);
  }

/* setup all new structures */
for (list=sysenv.mal ; list ; list=g_slist_next(list))
  {
  model = list->data;

  if (model->id == -1)
    {
    model->id = RIETICA;
    model->periodic = 3;
    model->fractional = TRUE;
    strcpy(model->filename, filename);
    model->cores = g_slist_reverse(model->cores);
    model_prep(model);
    }
  }

scan_free(scan);

return(0);
}