/* * sfac.c * * Scattering factors * * (c) 2007-2009 Thomas White * * pattern_sim - Simulate diffraction patterns from small crystals * */ #include #include #include #include #include #include "utils.h" #include "sfac.h" #define N_MEMO 1024 /* Look up f1 and f2 for this atom at this energy (in J/photon) */ static double complex get_f1f2(const char *n, double en) { FILE *fh; char filename[64]; char line[1024]; char *rval; double last_E, last_f1, last_f2; static char *memo_n[N_MEMO]; static double memo_en[N_MEMO]; static double complex memo_res[N_MEMO]; static int n_memo = 0; int i; for ( i=0; in_species = 0; fh = fopen("molecule.pdb", "r"); if ( fh == NULL ) { fprintf(stderr, "Couldn't open file\n"); return NULL; } do { char el[4]; int j, r; int done = 0; float xf, yf, zf, occf, Bf; double x, y, z, occ, B; char *coords; rval = fgets(line, 1023, fh); /* Only interested in atoms */ if ( strncmp(line, "HETATM", 6) != 0 ) continue; /* The following crimes against programming style * were brought to you by Wizbit Enterprises, Inc. */ if ( line[76] == ' ' ) { el[0] = line[77]; el[1] = '\0'; } else { el[0] = line[76]; el[1] = line[77]; el[2] = '\0'; } coords = line + 29; r = sscanf(coords, "%f %f %f %f %f", &xf, &yf, &zf, &occf, &Bf); if ( r != 5 ) { fprintf(stderr, "WTF?\n"); abort(); } /* Promote to double precision */ x = xf; y = yf; z = zf; occ = occf; B = Bf; for ( j=0; jn_species; j++ ) { struct mol_species *spec; int n; spec = mol->species[j]; if ( strcmp(spec->species, el) != 0 ) continue; n = mol->species[j]->n_atoms; spec->x[n] = x*1.0e-10; /* Convert to nm */ spec->y[n] = y*1.0e-10; spec->z[n] = z*1.0e-10; spec->occ[n] = occ; spec->B[n] = B*1.0e-20; /* Convert to m^2 */ mol->species[j]->n_atoms++; done = 1; } if ( !done ) { /* Need to create record for this species */ struct mol_species *spec; spec = malloc(sizeof(struct mol_species)); memcpy(spec->species, el, 4); spec->x[0] = x*1.0e-10; /* Convert to nm */ spec->y[0] = y*1.0e-10; spec->z[0] = z*1.0e-10; spec->occ[0] = occ; spec->B[0] = B*1.0e-20; /* Convert to nm^2 */ spec->n_atoms = 1; mol->species[mol->n_species] = spec; mol->n_species++; } } while ( rval != NULL ); fclose(fh); printf("There are %i species\n", mol->n_species); for ( i=0; in_species; i++ ) { printf("%3s : %6i\n", mol->species[i]->species, mol->species[i]->n_atoms); } return mol; }