19 double radius,
double angle,
double resolution);
21 double modulus,
double thick);
27 "Usage: bafm [options] input.map height.img",
28 "------------------------------------------",
29 "Simulates AFM imaging.",
30 "An optional force map is produced (where every 2D slice is a force profile ",
31 " at a cantilever height).",
32 "A set of height images is written.",
35 "-invert Invert inside & outside (default not).",
38 "-verbose 7 Verbosity of output.",
39 "-sampling 1.5 Sampling (default 1 A/pixel).",
42 "-tipradius 5.3 Generate tip: radius (angstrom).",
43 "-tipsize 10,12,5 Tip size (x,y,z pixels).",
44 "-tipangle 55.7 Tip side angle (degrees).",
46 "Simulation parameters:",
47 "-spring 0.1 Spring constant (default 0.05 N/m).",
48 "-modulus 2e8 Bulk modulus (default 1e9 N/m2).",
49 "-thickness 55 Thickness (default 50 A).",
50 "-forcestep 15 Force intervals for height maps (default 10 pN).",
53 "-Tip tip.map Read tip from file.",
56 "-Postscript file.ps Postscript force curve.",
57 "-Force force.map Force map file name.",
62 int main(
int argc,
char **argv)
68 double resmin(1 + sqrt(5.0));
82 for ( curropt = option; curropt; curropt = curropt->
next ) {
83 if ( curropt->
tag ==
"sampling" )
84 sam = curropt->
scale();
85 if ( curropt->
tag ==
"tipradius" )
86 if ( ( tipradius = curropt->
value.
real() ) < 1 )
87 cerr <<
"-tipradius: A radius must be specified!" << endl;
88 if ( curropt->
tag ==
"tipsize" )
89 tipsize = curropt->
size();
90 if ( curropt->
tag ==
"tipangle" )
91 if ( ( tipangle = curropt->
value.
real() ) < 1 )
92 cerr <<
"-tipangle: An angle must be specified!" << endl;
93 if ( curropt->
tag ==
"invert" )
95 if ( curropt->
tag ==
"spring" )
96 if ( ( spring = curropt->
value.
real() ) < 0.00000001 )
97 cerr <<
"-spring: The spring constant must be specified!" << endl;
98 if ( curropt->
tag ==
"modulus" )
99 if ( ( modulus = curropt->
value.
real() ) < 0.001 )
100 cerr <<
"-modulus: The bulk modulus must be specified!" << endl;
101 if ( curropt->
tag ==
"thickness" )
102 if ( ( thick = curropt->
value.
real() ) < 1 )
103 cerr <<
"-thickness: The thickness must be specified!" << endl;
104 if ( curropt->
tag ==
"forcestep" )
105 if ( ( forcestep = curropt->
value.
real() ) < 0.001 )
106 cerr <<
"-forcestep: The force step must be specified!" << endl;
107 if ( curropt->
tag ==
"Tip" )
109 if ( curropt->
tag ==
"Postscript" )
111 if ( curropt->
tag ==
"Force" )
123 cerr <<
"Error: No input file given!" << endl;
135 if ( tipradius > 0 ) {
136 tipangle *=
M_PI/180.0;
148 if ( setinvert ) p->
invert();
173 if ( argc > optind && strspn(argv[optind],
"-") != 1 )
201 double radius,
double angle,
double resolution)
204 double dx, dy, dz, dis, dis2;
206 if ( size[0] < 4*radius/sampling[0] ) size[0] = (long) (4*radius/sampling[0]);
207 if ( size[1] < 4*radius/sampling[1] ) size[1] = (long) (4*radius/sampling[1]);
208 if ( size[2] < 4*radius/sampling[2] ) size[2] = (long) (4*radius/sampling[2]);
211 cout <<
"Generating a 2D tip image" << endl;
212 cout <<
"Tip dimensions: " << size << endl;
213 cout <<
"Tip radius: " << radius <<
" A" << endl << endl;
215 cout <<
"Generating a 2D tip image" << endl;
222 radius /= sampling[0];
223 double radcut(radius/sqrt(2.0));
225 double rad2(radius*radius);
230 for ( i=yy=0; yy<p->
sizeY(); yy++ ) {
232 for ( xx=0; xx<p->
sizeX(); xx++, i++ ) {
234 dis2 = dx*dx + dy*dy;
237 dz = max - (2*radcut - sqrt(rad2 - dis2));
266 float* force,
double spring,
double modulus,
double thick)
268 long i, ti, di, xx, yy, zz, tx, ty, tz, hiz;
269 long dx, dy, dz, zshift;
271 double fraction, overlap_volume(1);
275 double proportion(scale*modulus/(spring*thick));
277 double* shift =
new double[p->
sizeZ()];
280 xx = ii - yy*p->
sizeX();
281 for ( zz=0; zz<p->
sizeZ(); zz++ ) {
283 if ( overlap_volume > 1e-30 ) {
287 if ( hiz > p->
sizeZ() - zz ) hiz = p->
sizeZ() - zz;
288 for ( ti=ty=0; ty<ptip->
sizeY(); ty++ ) {
290 while ( dy < 0 ) dy += p->
sizeY();
292 for ( tx=0; tx<ptip->
sizeX(); tx++, ti++ ) {
294 while ( dx < 0 ) dx += p->
sizeX();
296 di = dy*p->
sizeX()+dx;
297 for ( tz=0; tz<hiz; tz++ ) {
299 overlap_volume += (*p)[dz*ss+di]*(*ptip)[tz*tss+ti];
304 shift[zz] = overlap_volume*proportion;
308 for ( zz=0; zz<p->
sizeZ(); zz++ ) {
310 while ( zshift*p->
sampling(0)[2] < shift[zz+zshift] &&
311 zshift+zz < p->
sizeZ() ) zshift++;
312 if ( zshift > 0 ) zshift--;
313 fraction = (zshift*p->
sampling(0)[2] - shift[zz+zshift])/
314 (zshift*p->
sampling(0)[2] - shift[zz+zshift] -
315 (zshift+1)*p->
sampling(0)[2] + shift[zz+zshift+1]);
317 cout <<
"Problem:\t" << xx <<
tab << yy <<
tab << zz <<
tab <<
319 shift[zz+zshift] <<
tab << shift[zz+zshift+1] << endl;
321 force[i] = spring*(zshift + fraction)*p->
sampling(0)[2];
365 cout <<
"Simulating an AFM experiment" << endl;
366 cout <<
"Spring constant: " << spring <<
" N/m" << endl;
367 cout <<
"Bulk modulus: " << modulus <<
" N/m2" << endl;
368 cout <<
"Specimen thickness: " << thick <<
" A" << endl;
369 cout <<
"Density scale: " << p->
sampling(0).volume()/(p->
maximum()*ptip->
maximum()) <<
" A/pixel" << endl << endl;
371 cout <<
"Simulating an AFM experiment" << endl << endl;
380 dispatch_apply(ss, dispatch_get_global_queue(0, 0), ^(
size_t i){
384 #pragma omp parallel for
385 for (
long i=0; i<ss; i++ )
390 for ( i=0; i<ss*p->
sizeZ(); i++ )
393 Bstring title(
"AFM simulation");
394 int ncol(3), nv(p->
sizeZ());
412 for ( zz=0; zz<nv; zz++ ) {
413 for ( i=0, ii=zz*ss, avg_force=0; i<ss; i++, ii++ )
414 avg_force += force[ii];
417 (*plot)[zz+nv] = avg_force/spring;
418 (*plot)[zz+2*nv] = avg_force;
422 txt =
"Cantilever spring constant: " +
Bstring(spring/100,
"%lg N/m");
424 txt =
"Bulk modulus: " +
Bstring(modulus*1e8,
"%lg N/m2");
426 txt =
"Sample thickness: " +
Bstring(thick,
"%lg A");
430 cout <<
"Distance(A)\tDeflection(A)\tForce(pN)" << endl;
431 for ( zz=0; zz<nv; zz++ )
432 cout << setw(12) << (*plot)[zz] <<
tab << setw(12)
433 << (*plot)[zz+nv] <<
tab << (*plot)[zz+2*nv] << endl;
450 long i, j, xx, yy, zz, nn;
452 double force, denom, diff, scale, min, max;
454 nn = (long) (pf->
maximum()/force_step);
461 cout <<
"Converting an AFM force map into " << ph->
images() <<
" height images" << endl;
462 cout <<
"Force(pN)\tHeight_min(A)\tHeight_range(A)" << endl;
464 cout <<
"Converting an AFM force map into " << ph->
images() <<
" height images" << endl << endl;
466 for ( nn=0; nn<ph->
images(); nn++ ) {
467 force = force_step*(nn+1);
470 for ( i=nn*ss, yy=0; yy<ph->
sizeY(); yy++ ) {
471 for ( xx=0; xx<ph->
sizeX(); xx++, i++ ) {
473 j = yy*pf->
sizeX() + xx;
474 while ( zz < pf->sizeZ() && (*pf)[zz*ss + j] > force )
477 denom = (*pf)[j-ss] - (*pf)[j];
478 diff = force - (*pf)[j];
485 if ( fabs(diff) < 1 )
488 if ( min > (*ph)[i] ) min = (*ph)[i];
489 if ( max < (*ph)[i] ) max = (*ph)[i];
494 cout << setw(12) << force <<
tab << setw(12) << min <<
tab << scale << endl;
495 for ( i=0, j=nn*ss; i<ss; i++, j++ )
496 ph->
set(j, (*ph)[j] - min);
503 cout <<
"DEBUG afm_force_to_height: Done!" << endl << endl;