20 #define MAXSID 1024 // Maximum grid sidelength
21 #define MAXSCAT 100 // Maximum number of atomic scattering data points
22 #define MAXRAD 100 // Maximum radial points at 0.1 A/point for calculating atomic potential curves
26 double resolution,
double Bfactor,
int wrap,
int gextype);
28 Batomtype* atompar,
double resolution,
int wrap,
double Bfactor);
55 int wrap,
int gextype,
int spacegroup,
UnitCell unit_cell)
61 if ( sampling.
volume() < 1e-6 )
62 sampling[0] = sampling[1] = sampling[2] = 1;
64 sampling = sampling.
max(0.01);
66 if ( resolution < sampling[0] ) resolution = sampling[0];
67 if ( resolution < 0.01 ) resolution = 2*sampling[0];
69 if ( size[0] < 1 ) size[0] = (long) ((molgroup->
max[0] + 3*resolution)/sampling[0] + origin[0]);
70 if ( size[1] < 1 ) size[1] = (long) ((molgroup->
max[1] + 3*resolution)/sampling[1] + origin[1]);
71 if ( size[2] < 1 ) size[2] = (long) ((molgroup->
max[2] + 3*resolution)/sampling[2] + origin[2]);
76 cout <<
"DEBUG img_sf_from_molecule: size = " << size << endl;
84 int side = (long) floor((3.0/2.0)*resolution/p->
sampling(0)[0]+1);
88 if ( gextype ) cout <<
"Calculating atomic potential:" << endl;
89 else cout <<
"Calculating density by simple gaussian expansion:" << endl;
90 cout <<
"Dimensions: " << p->
size() <<
" voxels" << endl;
92 cout <<
"Sampling: " << p->
sampling(0) <<
" A/voxel" << endl;
94 cout <<
"Resolution: " << resolution <<
" A" << endl;
95 cout <<
"Global B-factor: " << Bfactor <<
" A2" << endl;
96 cout <<
"Wrap: " << wrap << endl;
98 cout <<
"Calculating density" << endl << endl;
100 mol_to_image(molgroup, p, atompar, resolution, Bfactor, wrap, gextype);
130 double resolution,
double Bfactor,
int wrap,
int gextype)
132 long h, i, j, irad, nsig, t;
133 long x, y, z, ix, iy, iz;
136 double sigma, sigma2, avgsig, dx, dy, dz, dist2;
137 double sigma2_global = resolution*resolution/4 + Bfactor/(8*
M_PI*
M_PI);
139 if ( interval > 0.1 ) interval = 0.1;
141 double voxel_size = p->
sampling(0).volume();
142 double thisamp, thisdens, thisatom, summass(0), totmass(0);
143 long side = (long) floor((3.0/2.0)*resolution/p->
sampling(0)[0]+1);
150 double radius2 = side*side*sam2;
153 cout <<
"Cube edge length: " << 2*side+1 <<
" voxels" << endl;
164 for ( h=0, mol = molgroup->
mol; mol; mol = mol->
next )
if ( mol->
sel ) {
166 for( res = mol->
res; res; res = res->
next ) {
167 for ( atom = res->
atom; atom; atom = atom->
next ) {
171 lo[0] = (long) (cgrid[0] + 0.5 - side);
172 hi[0] = (long) (cgrid[0] + 0.5 + side);
173 lo[1] = (long) (cgrid[1] + 0.5 - side);
174 hi[1] = (long) (cgrid[1] + 0.5 + side);
175 lo[2] = (long) (cgrid[2] + 0.5 - side);
176 hi[2] = (long) (cgrid[2] + 0.5 + side);
178 if ( lo[0] < 0 ) lo[0] = 0;
179 if ( lo[1] < 0 ) lo[1] = 0;
180 if ( lo[2] < 0 ) lo[2] = 0;
181 if ( hi[0] > (
long)p->
sizeX() - 1 ) hi[0] = p->
sizeX() - 1;
182 if ( hi[1] > (
long)p->
sizeY() - 1 ) hi[1] = p->
sizeY() - 1;
183 if ( hi[2] > (
long)p->
sizeZ() - 1 ) hi[2] = p->
sizeZ() - 1;
185 if ( Bfactor > 0 ) sigma2 = sigma2_global;
186 else sigma2 = sigma2_global + atom->
b/(8*
M_PI*
M_PI);
187 if ( sigma2 < 0.01 ) sigma2 = 0.01;
188 sigma = sqrt(sigma2);
192 cout << atom->
coord <<
" " << atom->
type <<
" " << atom->
mass << endl;
194 thisamp = atom->
mass*voxel_size/(sqrt(
TWOPI*sigma2)*
TWOPI*sigma2);
195 sigma2 = -1.0/(2.0*sigma2);
196 ix = (long) (cgrid[0] + 0.5);
197 iy = (long) (cgrid[1] + 0.5);
198 iz = (long) (cgrid[2] + 0.5);
199 i = p->
index(0, ix, iy, iz, 0);
200 for ( z=lo[2]; z<=hi[2]; z++ ) {
201 dz = (cgrid[2] - z)*(cgrid[2] - z)*sam2;
202 for ( y=lo[1]; y<=hi[1]; y++ ) {
203 dy = (cgrid[1] - y)*(cgrid[1] - y)*sam2;
204 for ( x=lo[0]; x<=hi[0]; x++ ) {
205 dx = (cgrid[0] - x)*(cgrid[0] - x)*sam2;
206 dist2 = dx + dy + dz;
207 if ( dist2 <= radius2 ) {
212 while ( ix < 0 ) ix += p->
sizeX();
213 while ( iy < 0 ) iy += p->
sizeY();
214 while ( iz < 0 ) iz += p->
sizeZ();
219 j = p->
index(ix, iy, iz);
224 thisdens = apot[t*
MAXRAD];
226 irad = (long) (sqrt(dist2)/interval + 0.5);
230 thisdens = thisamp*exp(dist2*sigma2);
233 thisatom += thisdens;
239 summass += atom->
mass;
243 cout <<
"Molecules done: " << h+1 <<
"\r" << flush;
257 cout <<
"Average sigma: " << avgsig/nsig <<
" A" << endl;
258 cout <<
"Total mass (sum): " << summass <<
" Da" << endl;
259 cout <<
"Total mass (density): " << totmass <<
" Da" << endl;
260 cout <<
" Ratio: " << totmass/summass << endl;
261 cout <<
"Total volume: " << totmass/
RHO <<
" A3" << endl << endl;
264 if ( apot )
delete[] apot;
285 double min = 1e30, max = -1e30, Qavg(0), Qsd(0);
288 for ( mol = molgroup->
mol; mol; mol = mol->
next ) {
289 for( res = mol->
res; res; res = res->
next ) {
290 for ( atom = res->
atom; atom; atom = atom->
next ) {
294 while ( x < 0 ) x += pcalc->
sizeX();
295 while ( x >= pcalc->
sizeX() ) x -= pcalc->
sizeX();
296 while ( y < 0 ) y += pcalc->
sizeY();
297 while ( y >= pcalc->
sizeY() ) y -= pcalc->
sizeY();
298 while ( z < 0 ) z += pcalc->
sizeZ();
299 while ( z >= pcalc->
sizeZ() ) z -= pcalc->
sizeZ();
300 j = pcalc->
index(x, y, z);
301 if ( (*pcalc)[j] ) atom->
q = scale*(*p)[j]/(*pcalc)[j];
302 if ( atom->
q > 1 ) atom->
q = 1;
303 if ( min > atom->
q ) min = atom->
q;
304 if ( max < atom->q ) max = atom->
q;
306 Qsd += atom->
q*atom->
q;
313 Qsd = sqrt(Qsd/natom - Qavg*Qavg);
316 cout <<
"Comparison of molecule " << mol->
id <<
" with map " << p->
file_name() << endl;
317 cout <<
"Origin (original): " << p->
image->
origin() << endl;
318 cout <<
"Origin (calculated): " << pcalc->
image->
origin() << endl;
319 cout <<
"Scale: " << scale << endl;
320 cout <<
"Min & max: " << min <<
" " << max << endl;
321 cout <<
"Average and standard deviation: " << Qavg <<
" " << Qsd << endl << endl;
323 cout <<
"Comparison of molecule " << mol->
id <<
" with map " << p->
file_name() << endl;
324 cout <<
"Average and standard deviation: " << Qavg <<
" " << Qsd << endl << endl;
359 int spacegroup,
UnitCell unit_cell,
int wrap,
double Bfactor,
Bstring& paramfile)
363 if ( sampling.
volume() < 1e-6 )
364 sampling[0] = sampling[1] = sampling[2] = 1;
366 sampling = sampling.
max(0.01);
368 if ( resolution < sampling[0] ) resolution = sampling[0];
369 if ( resolution < 0.01 ) resolution = 2*sampling[0];
371 if ( size[0] < 1 ) size[0] = (long) ((molgroup->
max[0] - molgroup->
min[0])/sampling[0]);
372 if ( size[1] < 1 ) size[1] = (long) ((molgroup->
max[1] - molgroup->
min[1])/sampling[1]);
373 if ( size[2] < 1 ) size[2] = (long) ((molgroup->
max[2] - molgroup->
min[2])/sampling[2]);
378 cout <<
"DEBUG img_sf_from_molecule: size = " << size << endl;
388 if ( resolution > 0.01 ) smax = 1.0/resolution;
391 cout <<
"Calculating structure factors from atomic coordinates" << endl;
394 cout <<
"Size: " << p->
size() <<
" voxels" << endl;
396 cout <<
"Sampling: " << p->
sampling(0) <<
" A/voxel" << endl;
397 cout <<
"Resolution: " << resolution <<
" A" << endl;
398 cout <<
"Reciprocal resolution: " << smax <<
" /A" << endl;
412 if ( s <= 0 )
return 0;
416 double fraction, fraction1, f, phi;
423 if ( x < (p->
sizeX() + 1)/2 ) h = x;
else h = (long)x - p->
sizeX();
424 if ( y < (p->
sizeY() + 1)/2 ) k = y;
else k = (long)y - p->
sizeY();
425 if ( z < (p->
sizeZ() + 1)/2 ) l = z;
else l = (long)z - p->
sizeZ();
429 fraction1 = 1 - fraction;
431 for ( mol = molgroup->
mol; mol; mol = mol->
next ) {
432 for( res = mol->
res; res; res = res->
next ) {
433 for ( atom = res->
atom; atom; atom = atom->
next )
436 f = atom->
q*(fraction1*scat[t] + fraction*scat[t+1]);
438 k*atom->
vel[1] + l*atom->
vel[2]);
448 cout <<
"DEBUG mol_to_structure_factors: " << x <<
" " << y <<
" " << z <<
" " <<
476 Batomtype* atompar,
double resolution,
int wrap,
double Bfactor)
479 long i, j, h, k, l, x, y, z, yi, zi, n, t, nmax;
480 long nmol(0), natom(0);
481 double s, smax, sx, sy, sz;
489 if ( resolution ) smax = 1.0/resolution;
490 resolution = 1.0/smax;
492 double recip_interval = 1.1*smax/
MAXSCAT;
493 double inv_recip_int = 1/recip_interval;
507 for ( nmol = 0, mol = molgroup->
mol; mol; mol = mol->
next )
if ( mol->
sel ) {
509 for( res = mol->
res; res; res = res->
next ) {
510 for ( atom = res->
atom; atom; atom = atom->
next, natom++ ) {
512 for ( t=0, at = atompar; at && atom->
tnum < 0; at = at->
next, t++ )
513 if ( strncmp(atom->
el, at->
el, 2) == 0 )
515 if ( atom->
tnum < 0 ) atom->
sel = 0;
517 cout <<
"DEBUG mol_to_structure_factors: Atom = " << atom->
el << endl;
518 fcoor = atom->
coord + ori;
519 atom->
vel = frac_mat * fcoor;
521 while ( atom->
vel[0] < 0 ) atom->
vel[0] += 1;
522 while ( atom->
vel[1] < 0 ) atom->
vel[1] += 1;
523 while ( atom->
vel[2] < 0 ) atom->
vel[2] += 1;
524 while ( atom->
vel[0] > 1 ) atom->
vel[0] -= 1;
525 while ( atom->
vel[1] > 1 ) atom->
vel[1] -= 1;
526 while ( atom->
vel[2] > 1 ) atom->
vel[2] -= 1;
527 }
else if ( wrap > 1 ) {
528 if ( atom->
vel[0] < 0 ) atom->
sel = 0;
529 if ( atom->
vel[1] < 0 ) atom->
sel = 0;
530 if ( atom->
vel[0] > 1 ) atom->
sel = 0;
531 if ( atom->
vel[1] > 1 ) atom->
sel = 0;
532 while ( atom->
vel[2] < 0 ) atom->
vel[2] += 1;
533 while ( atom->
vel[2] > 1 ) atom->
vel[2] -= 1;
535 if ( atom->
vel[0] < 0 ) atom->
sel = 0;
536 if ( atom->
vel[1] < 0 ) atom->
sel = 0;
537 if ( atom->
vel[2] < 0 ) atom->
sel = 0;
538 if ( atom->
vel[0] > 1 ) atom->
sel = 0;
539 if ( atom->
vel[1] > 1 ) atom->
sel = 0;
540 if ( atom->
vel[2] > 1 ) atom->
sel = 0;
543 cout <<
"DEBUG mol_to_structure_factors: "
544 << atom->
el <<
" " << atom->
vel << endl;
550 cout <<
"Molecules and atoms: " << nmol <<
" " << natom << endl;
551 cout <<
"Unit cell: " <<
552 unit_cell.
a() <<
" " << unit_cell.
b() <<
" " << unit_cell.
c() <<
" " <<
554 cout <<
"Fractionalization matrix: " << endl << frac_mat << endl;
561 nmax = (long) ((4.0*
M_PI/3.0)*smax*smax*smax/(frac_mat[0][0]*frac_mat[1][1]*frac_mat[2][2]));
562 if ( p->
sizeZ() < 2 )
563 nmax = (
long) (
M_PI*smax*smax/(frac_mat[0][0]*frac_mat[1][1]));
570 cout <<
"Reflections estimated: " << nmax << endl;
579 cout <<
"DEBUG mol_to_structure_factors: scale = " << scale << endl;
583 long hx1 = (p->
sizeX()+1)/2, hy1 = (p->
sizeY()+1)/2, hz1 = (p->
sizeZ()+1)/2;
590 for ( l=-hz; l<hz1; l++ ) {
591 z = ( l < 0 ) ? l + p->
sizeZ() : l;
592 sz = l*frac_mat[2][2];
594 for ( k=-hy; k<hy1; k++ ) {
595 y = ( k < 0 ) ? k + p->
sizeY() : k;
596 sy = k*frac_mat[1][1];
598 for ( h=-hx; h<=0; h++ ) {
599 x = ( h < 0 ) ? h + p->
sizeX() : h;
600 sx = h*frac_mat[0][0];
602 s = sqrt(sx + sy + sz);
604 i = p->
index(x, y, z);
616 dispatch_apply(imgsize, dispatch_get_global_queue(0, 0), ^(
size_t i){
617 if ( fom[i] )
one_sf(molgroup, p, i, fom[i], scale, scat);
620 #pragma omp parallel for
621 for ( i=0; i<imgsize; i++ )
if ( fom[i] )
622 one_sf(molgroup, p, i, fom[i], scale, scat);
628 for ( l=-hz; l<hz1; l++ ) {
629 z = ( l < 0 ) ? l + p->
sizeZ() : l;
630 zi = ( z == 0 ) ? 0 : p->
sizeZ() - z;
631 sz = l*frac_mat[2][2];
633 for ( k=-hy; k<hy1; k++ ) {
634 y = ( k < 0 ) ? k + p->
sizeY() : k;
635 yi = ( y == 0 ) ? 0 : p->
sizeY() - y;
636 sy = k*frac_mat[1][1];
638 for ( h=1; h<hx1; h++ ) {
639 sx = h*frac_mat[0][0];
641 s = sqrt(sx + sy + sz);
644 j = p->
index(h, y, z);
653 cout <<
"Reflections calculated: " << n << endl << endl;
663 double interval2 = -4*
M_PI*
M_PI*interval*interval, prefac, expfac;
667 for ( n=0, at = atompar; at; at = at->
next, n++ ) ;
671 cout <<
"DEBUG get_potential_curves: n=" << n <<
" interval=" << interval << endl;
673 double* apot =
new double[
MAXRAD*n];
674 for ( i=0; i<
MAXRAD*n; i++ ) apot[i] = 0;
676 for ( t=0, at = atompar; at; at = at->
next, t++ ) {
678 for ( j=0; j<5; j++ ) {
680 expfac = interval2/at->
sfb[j];
681 for ( i=0; i<
MAXRAD; i++ )
682 apot[t*
MAXRAD+i] += prefac*exp(expfac*i*i);
687 cout <<
"Scattering coefficients:" << endl;
688 for ( at = atompar; at; at = at->
next )
689 cout <<
tab << at->
el <<
"(" << at->
z <<
")";
691 for ( j=0; j<5; j++ ) {
693 for ( at = atompar; at; at = at->
next )
694 cout <<
tab << at->
sfa[j];
697 for ( j=0; j<5; j++ ) {
699 for ( at = atompar; at; at = at->
next )
700 cout <<
tab << at->
sfb[j];
704 for ( at = atompar; at; at = at->
next )
706 cout << endl <<
"Atomic radial potential curves:" << endl <<
"s";
707 for ( at = atompar; at; at = at->
next )
708 cout <<
tab << at->
el <<
"(" << at->
z <<
")";
710 for ( i=0; i<
MAXRAD; i++ ) {
712 for ( t=0, at = atompar; at; at = at->
next, t++ )
723 double recip_interval,
long& nscat)
726 double s2, fact = -Bfactor/4;
729 if ( recip_interval < 1e-10 ) {
730 cerr <<
"Error: Reciprocal space interval not defined!" << endl;
734 double recip_int2 = recip_interval*recip_interval/4;
737 for ( n=0, at = atompar; at; at = at->
next, n++ ) ;
741 cout <<
"DEBUG get_scattering_curves: nscat = " << n << endl;
743 double* scat =
new double[
MAXSCAT*n];
744 for ( i=0; i<
MAXSCAT*n; i++ ) scat[i] = 0;
748 for ( t=0, at = atompar; at; at = at->
next, t++ ) {
750 for ( j=0; j<5; j++ )
752 scat[t*
MAXSCAT+i] *= exp(fact*s2);
757 cout <<
"Scattering coefficients:" << endl;
758 for ( at = atompar; at; at = at->
next )
759 cout <<
tab << at->
el <<
"(" << at->
z <<
")";
761 for ( j=0; j<5; j++ ) {
763 for ( at = atompar; at; at = at->
next )
764 cout <<
tab << at->
sfa[j];
767 for ( j=0; j<5; j++ ) {
769 for ( at = atompar; at; at = at->
next )
770 cout <<
tab << at->
sfb[j];
774 for ( at = atompar; at; at = at->
next )
776 cout << endl <<
"Scattering curves:" << endl <<
"s";
777 for ( at = atompar; at; at = at->
next )
778 cout <<
tab << at->
el <<
"(" << at->
z <<
")";
783 for ( t=0, at = atompar; at; at = at->
next, t++ )
810 double recip_interval = 1.0/
MAXSCAT;
811 double recip_int2 = recip_interval*recip_interval/4;
818 char elsel[1024], *aptr;
819 if ( selection.
length() ) strcpy(elsel, selection.
c_str());
820 else for ( at = atompar, aptr = elsel; at; aptr += strlen(at->
el), at = at->
next )
821 strcpy(aptr, at->
el);
823 ofstream fd(outfile.
c_str());
824 if ( fd.fail() )
return -1;
827 cout <<
"Writing scattering curves to: " << outfile << endl;
828 cout <<
"Elemental selection: " << elsel << endl << endl;
831 fd <<
"Scattering coefficients:" << endl;
832 for ( at = atompar; at; at = at->
next )
833 if ( strstr(elsel, at->
el) )
834 fd <<
tab << at->
el <<
"(" << at->
z <<
")";
836 for ( j=0; j<5; j++ ) {
838 for ( at = atompar; at; at = at->
next )
839 if ( strstr(elsel, at->
el) )
843 for ( j=0; j<5; j++ ) {
845 for ( at = atompar; at; at = at->
next )
846 if ( strstr(elsel, at->
el) )
851 for ( at = atompar; at; at = at->
next )
852 if ( strstr(elsel, at->
el) )
856 fd <<
"Scattering curves:" << endl <<
"s";
857 for ( at = atompar; at; at = at->
next )
858 if ( strstr(elsel, at->
el) )
859 fd <<
tab << at->
el <<
"(" << at->
z <<
")";
864 for ( j=0, at = atompar; at; at = at->
next, j++ )
865 if ( strstr(elsel, at->
el) )