示例#1
0
文件: bfng93.c 项目: Mrktn/bfng93
static int process(void)
{
    int val1, val2;

    if(context.stringmode)
    {
        if(matrix[context.pos.y][context.pos.x] == '"')
            context.stringmode = 0;
        else
            push(matrix[context.pos.y][context.pos.x]);

        return 0;
    }

    if(matrix[context.pos.y][context.pos.x] == ' ')
        return 0;

    switch(matrix[context.pos.y][context.pos.x])
    {
    /* @ (end) ends program */
    case '@':
        return EOP;

    case '>': case '<': case '^': case 'v':
        context.direction = car2dir(matrix[context.pos.y][context.pos.x]);
        break;

    /* _ (horizontal if) <boolean value> PC->left if <value>, else PC->right */
    case '_':
        context.direction = (pop() ? LEFT : RIGHT);
        break;

    /* | (vertical if) <boolean value> PC->up if <value>, else PC->down */
    case '|':
        context.direction = (pop() ? UP : DOWN);
        break;

    /* : (dup)  <value>   <value> <value> */
    case ':':
        push(val1 = pop());
        push(val1);
        break;

    /* ? (random) PC -> right? left? up? down? ??? */
    case '?':
        context.direction = rand() % DIRMAX;
        break;

    case '+': case '-': case '*': case '/': case '%':
        val1 = pop(), val2 = pop();
        push(binaryfuncs[binaryopcar2index(matrix[context.pos.y][context.pos.x])](val2, val1));
        break;

    case '!':
        push(!pop());
        break;

    /* ` (greater)  <value1> <value2>   <1 if value1 > value2, 0 otherwise> */
    case '`':
        push(pop() < pop());
        break;

    /* \ (swap) <value1> <value2>  <value2> <value1> */
    case '\\':
        val1 = pop(), val2 = pop();
        push(val1);
        push(val2);
        break;

    /* $ (pop) <value>   pops <value> but does nothing */
    case '$':
        (void) pop();
        break;

    /* . (pop) <value>   outputs <value> as integer */
    case '.':
        printf("%d", pop());
        break;

    /* , (pop)         <value>                 outputs <value> as ASCII */
    case ',':
        putchar(pop());
        break;

    /* # (bridge)  'jumps' PC one farther; skips over next command */
    case '#':
        nextcell();
        break;

    /* ~ (input character)   <character user entered> */
    case '~':
        push(xgetchar());
        break;

    case '"':
        context.stringmode = !context.stringmode;
        break;

    /* & (input value)   <value user entered> */
    case '&':
        push(getint());
        break;

    case 'p':
        val1 = pop(), val2 = pop();
        matrix[val1][val2] = pop();
        break;

    case 'g':
        val1 = pop(), val2 = pop();
        push(matrix[val1][val2]);
        break;

    default:
        if(isdigit(matrix[context.pos.y][context.pos.x]))
            push(matrix[context.pos.y][context.pos.x] - '0');
    }

    return 0;
}
示例#2
0
// to intialize the class
DynMat::DynMat(int narg, char **arg)
{
  attyp = NULL;
  memory = NULL;
  M_inv_sqrt = NULL;
  interpolate = NULL;
  DM_q = DM_all = NULL;
  binfile = funit = dmfile = NULL;

  attyp = NULL;
  basis = NULL;
  flag_reset_gamma = flag_skip = 0;

  // analyze the command line options
  int iarg = 1;
  while (narg > iarg){
    if (strcmp(arg[iarg], "-s") == 0){
      flag_reset_gamma = flag_skip = 1;

    } else if (strcmp(arg[iarg], "-r") == 0){
      flag_reset_gamma = 1;

    } else if (strcmp(arg[iarg], "-h") == 0){
      help();

    } else {
      if (binfile) delete []binfile;
      int n = strlen(arg[iarg]) + 1;
      binfile = new char[n];
      strcpy(binfile, arg[iarg]); 
    }

    iarg++;
  }

  ShowVersion();
  // get the binary file name from user input if not found in command line
  char str[MAXLINE];
  if (binfile == NULL) {
    char *ptr = NULL;
    printf("\n");
    do {
      printf("Please input the binary file name from fix_phonon: ");
      fgets(str,MAXLINE,stdin);
      ptr = strtok(str, " \n\t\r\f");
    } while (ptr == NULL);

    int n = strlen(ptr) + 1;
    binfile = new char[n];
    strcpy(binfile, ptr);
  }

  // open the binary file
  FILE *fp = fopen(binfile, "rb");
  if (fp == NULL) {
    printf("\nFile %s not found! Programe terminated.\n", binfile);
    help();
  }

  // read header info from the binary file
  if ( fread(&sysdim, sizeof(int),    1, fp) != 1) {printf("\nError while reading sysdim from file: %s\n", binfile); fclose(fp); exit(2);}
  if ( fread(&nx,     sizeof(int),    1, fp) != 1) {printf("\nError while reading nx from file: %s\n", binfile); fclose(fp); exit(2);}
  if ( fread(&ny,     sizeof(int),    1, fp) != 1) {printf("\nError while reading ny from file: %s\n", binfile); fclose(fp); exit(2);}
  if ( fread(&nz,     sizeof(int),    1, fp) != 1) {printf("\nError while reading nz from file: %s\n", binfile); fclose(fp); exit(2);}
  if ( fread(&nucell, sizeof(int),    1, fp) != 1) {printf("\nError while reading nucell from file: %s\n", binfile); fclose(fp); exit(2);}
  if ( fread(&boltz,  sizeof(double), 1, fp) != 1) {printf("\nError while reading boltz from file: %s\n", binfile); fclose(fp); exit(2);}

  fftdim = sysdim*nucell; fftdim2 = fftdim*fftdim;
  npt = nx*ny*nz;

  // display info related to the read file
  printf("\n"); for (int i = 0; i < 80; ++i) printf("="); printf("\n");
  printf("Dynamical matrix is read from file: %s\n", binfile);
  printf("The system size in three dimension: %d x %d x %d\n", nx, ny, nz);
  printf("Number of atoms per unit cell     : %d\n", nucell);
  printf("System dimension                  : %d\n", sysdim);
  printf("Boltzmann constant in used units  : %g\n", boltz);
  for (int i = 0; i < 80; ++i) printf("="); printf("\n");
  if (sysdim < 1||sysdim > 3||nx < 1||ny < 1||nz < 1||nucell < 1){
    printf("Wrong values read from header of file: %s, please check the binary file!\n", binfile);
    fclose(fp); exit(3);
  }

  funit = new char[4];
  strcpy(funit, "THz");
  if (boltz == 1.){eml2f = 1.; delete funit; funit = new char[27]; strcpy(funit,"sqrt(epsilon/(m.sigma^2))");}
  else if (boltz == 0.0019872067)  eml2f = 3.256576161;
  else if (boltz == 8.617343e-5)   eml2f = 15.63312493;
  else if (boltz == 1.3806504e-23) eml2f = 1.;
  else if (boltz == 1.3806504e-16) eml2f = 1.591549431e-14;
  else {
    printf("WARNING: Because of float precision, I cannot get the factor to convert sqrt(E/ML^2)\n");
    printf("into THz, instead, I set it to be 1; you should check the unit used by LAMMPS.\n");
    eml2f = 1.;
  }

  // now to allocate memory for DM
  memory = new Memory();
  memory->create(DM_all, npt, fftdim2, "DynMat:DM_all");
  memory->create(DM_q, fftdim,fftdim,"DynMat:DM_q");

  // read all dynamical matrix info into DM_all
  if ( fread(DM_all[0], sizeof(doublecomplex), npt*fftdim2, fp) != size_t(npt*fftdim2)){
    printf("\nError while reading the DM from file: %s\n", binfile);
    fclose(fp);
    exit(1);
  }

  // now try to read unit cell info from the binary file
  memory->create(basis, nucell, sysdim, "DynMat:basis");
  memory->create(attyp, nucell,         "DynMat:attyp");
  memory->create(M_inv_sqrt, nucell,    "DynMat:M_inv_sqrt");
  
  if ( fread(&Tmeasure,      sizeof(double), 1,      fp) != 1     ){printf("\nError while reading temperature from file: %s\n",   binfile); fclose(fp); exit(3);}
  if ( fread(&basevec[0],    sizeof(double), 9,      fp) != 9     ){printf("\nError while reading lattice info from file: %s\n",  binfile); fclose(fp); exit(3);}
  if ( fread(basis[0],       sizeof(double), fftdim, fp) != fftdim){printf("\nError while reading basis info from file: %s\n",    binfile); fclose(fp); exit(3);}
  if ( fread(&attyp[0],      sizeof(int),    nucell, fp) != nucell){printf("\nError while reading atom types from file: %s\n",    binfile); fclose(fp); exit(3);}
  if ( fread(&M_inv_sqrt[0], sizeof(double), nucell, fp) != nucell){printf("\nError while reading atomic masses from file: %s\n", binfile); fclose(fp); exit(3);}
  fclose(fp);

  car2dir();
  real2rec();

  // initialize interpolation
  interpolate = new Interpolate(nx,ny,nz,fftdim2,DM_all);
  if (flag_reset_gamma) interpolate->reset_gamma();

  // Enforcing Austic Sum Rule
  EnforceASR();

  // get the dynamical matrix from force constant matrix: D = 1/M x Phi
  for (int idq = 0; idq < npt; ++idq){
    int ndim =0;
    for (int idim = 0; idim < fftdim; ++idim)
    for (int jdim = 0; jdim < fftdim; ++jdim){
      double inv_mass = M_inv_sqrt[idim/sysdim]*M_inv_sqrt[jdim/sysdim];
      DM_all[idq][ndim].r *= inv_mass;
      DM_all[idq][ndim].i *= inv_mass;
      ndim++;
    }
  }

  // ask for the interpolation method
  interpolate->set_method();

  return;
}