| 1 | // Form factor computation: test program |
| 2 | // JWu 2016 |
| 3 | |
| 4 | // Reference: |
| 5 | // Joachim Wuttke |
| 6 | // "Form factor (Fourier shape transform) of polygon and polyhedron." |
| 7 | |
| 8 | // To use these tests, FormFactorPolyhedron.cpp must be compiled |
| 9 | // with flag -D POLYHEDRAL_DIAGNOSTIC=ON |
| 10 | |
| 11 | #include <cassert> |
| 12 | #include <iostream> |
| 13 | #include <iomanip> |
| 14 | #include <complex> |
| 15 | #include <vector> |
| 16 | |
| 17 | #include "ParticleShapes.h" |
| 18 | #include "FormFactorTriangle.h" |
| 19 | |
| 20 | using std::cout; |
| 21 | using std::cerr; |
| 22 | using std::vector; |
| 23 | |
| 24 | static complex_t I(0.,1.); |
| 25 | static double eps(2e-16); |
| 26 | |
| 27 | Diagnosis diagnosis; |
| 28 | |
| 29 | int nshape = 13; |
| 30 | |
| 31 | extern int n_limit; |
| 32 | extern double q_limit_series; |
| 33 | extern double qpa_limit_series; |
| 34 | |
| 35 | //! Returns a pointer to a particle, according to given code |
| 36 | |
| 37 | IFormFactorBorn* make_particle( int ishape ) |
| 38 | { |
| 39 | if ( ishape==1 ) { |
| 40 | return new FormFactorDodecahedron(3.); |
| 41 | } else if( ishape==2 ) { |
| 42 | return new FormFactorIcosahedron(15.); |
| 43 | } else if( ishape==3 ) { // true tetrahedron |
| 44 | double alpha = 72 * Units::degree; |
| 45 | return new FormFactorTetrahedron(1., tan(alpha)/2/sqrt(3), alpha); |
| 46 | } else if( ishape==4 ) { // tetrahedral frustum |
| 47 | double alpha = 72 * Units::degree; |
| 48 | return new FormFactorTetrahedron(1., 0.5*tan(alpha)/2/sqrt(3), alpha); |
| 49 | } else if( ishape==5 ) { // tetrahedral frustum, flat one |
| 50 | double alpha = 80 * Units::degree; |
| 51 | return new FormFactorTetrahedron(1., 0.1*tan(alpha)/2/sqrt(3), alpha); |
| 52 | } else if( ishape==6 ) { // tetrahedral frustum as in BasicTest |
| 53 | return new FormFactorTetrahedron(16., 4., .8); |
| 54 | } else if( ishape==7 ) { |
| 55 | double alpha = 72 * Units::degree; |
| 56 | return new FormFactorCone6(10., 10., alpha); |
| 57 | } else if( ishape==8 ) { |
| 58 | return new FormFactorPyramid(1.5, .24, 1.); |
| 59 | } else if( ishape==9 ) { |
| 60 | return new FormFactorAnisoPyramid(2, .4, .24, 1.); |
| 61 | } else if( ishape==10) { |
| 62 | return new FormFactorPrism3(1.2, 1.); |
| 63 | } else if( ishape==11) { |
| 64 | return new FormFactorPrism6(1., 1.); |
| 65 | } else if( ishape==12) { |
| 66 | return new FormFactorTruncatedCube(4., 1.); |
| 67 | } else if( ishape==13 ) { |
| 68 | double alpha = 73 * Units::degree; |
| 69 | return new FormFactorCuboctahedron(1., 1., .8, alpha); |
| 70 | } else if( ishape==90 ) { |
| 71 | return new FormFactorTriangle(1.); |
| 72 | } else |
| 73 | throw "Shape not implemented"; |
| 74 | } |
| 75 | |
| 76 | //! Print q in a form that can be easily pasted to the command line for further investigation |
| 77 | |
| 78 | std::string nice_q( cvector_t q ) |
| 79 | { |
| 80 | std::ostringstream ret; |
| 81 | double qmax = 0; |
| 82 | ret << std::setprecision(16); |
| 83 | for( int i=0; i<3; ++i ) |
| 84 | qmax = std::max( qmax, q[i].real() ); |
| 85 | for( int i=0; i<3; ++i ) |
| 86 | ret << q[i].real()/qmax << " " << q[i].imag()/qmax << " "; |
| 87 | ret << qmax; |
| 88 | return ret.str(); |
| 89 | } |
| 90 | |
| 91 | //! Bisect between two q's to find possible discontinuities |
| 92 | |
| 93 | void bisect( |
| 94 | const IFormFactorBorn* polyh, int ishape, double q0mag, |
| 95 | cvector_t qi, complex_t ri, const Diagnosis di, |
| 96 | cvector_t qf, complex_t rf, const Diagnosis df, |
| 97 | double& maxrelstep ) |
| 98 | { |
| 99 | assert( di!=df ); |
| 100 | if( (qi-qf).mag()<4*eps*q0mag ) { |
| 101 | // narrowed down to minimal step, now check for continuity |
| 102 | double aval = (std::abs(ri) + std::abs(rf))/2; |
| 103 | double step = std::abs(ri-rf); |
| 104 | double relstep = step/aval; |
| 105 | if( relstep>maxrelstep){ |
| 106 | cout<<"ishape="<<ishape<<": relstep "<<std::setprecision(8)<<relstep<<"="<<step<<"/"<<std::setprecision(16)<<aval<<" for "<<di<<"->"<<df<<" at q between "<<nice_q(qi)<<" and "<<nice_q(qf)<<"\n"; |
| 107 | maxrelstep = relstep; |
| 108 | } |
| 109 | return; |
| 110 | } |
| 111 | cvector_t q2 = (qi + qf)/2.; |
| 112 | complex_t r2 = polyh->evaluate_for_q(q2); |
| 113 | Diagnosis d2 = diagnosis; |
| 114 | if( d2!=di ) |
| 115 | bisect( polyh, ishape, q0mag, qi, ri, di, q2, r2, d2, maxrelstep ); |
| 116 | if( d2!=df ) |
| 117 | bisect( polyh, ishape, q0mag, q2, r2, d2, qf, rf, df, maxrelstep ); |
| 118 | } |
| 119 | |
| 120 | //! Computes form factor, and prints result according to outfilter. |
| 121 | |
| 122 | void run( const IFormFactorBorn* polyh, int ishape, cvector_t q, int outfilter ) |
| 123 | { |
| 124 | complex_t ret = polyh->evaluate_for_q(q); |
| 125 | cout<<std::scientific<<std::setprecision(16)<<std::setfill('0'); |
| 126 | if ( outfilter==0 ) |
| 127 | cout<<q.mag()<<" "<<std::abs(ret)<<" "<<ret.real()<<" "<<ret.imag()<<" "<< |
| 128 | diagnosis.nExpandedFaces<<std::noshowpos<<" "<<diagnosis.maxOrder; |
| 129 | else if( outfilter==1 ) |
| 130 | cout<<ret.real(); |
| 131 | else if( outfilter==2 ) |
| 132 | cout<<ret.imag(); |
| 133 | cout<<"\n"; |
| 134 | } |
| 135 | |
| 136 | //! Compute a form factor with modified control parameter settings |
| 137 | |
| 138 | complex_t ff_modified( cvector_t q, const IFormFactorBorn* polyh, bool expand_qpa, bool expand_q ) |
| 139 | { |
| 140 | PolyhedralFace::setLimits( expand_qpa ? 1e99 : 1e-99, 80 ); |
| 141 | FormFactorPolyhedron::setLimits( expand_q ? 1e99 : 1e-99, 80 ); |
| 142 | return polyh->evaluate_for_q( q ); |
| 143 | } |
| 144 | |
| 145 | void test_matching( int ishape, const vector<vector<cvector_t>>& scans ) |
| 146 | { |
| 147 | cout<<ishape<<"\n"; |
| 148 | cerr<<"shape "<<ishape<<" ...\n"; |
| 149 | IFormFactorBorn* polyh( make_particle( ishape ) ); |
| 150 | int n_mag = 25; |
| 151 | double mag_i = 1e-3; |
| 152 | double mag_f = 1e1; |
| 153 | for( int i=1; i<n_mag; ++i ) { |
| 154 | double mag = mag_i*pow(mag_f/mag_i,i/(n_mag-1.)); |
| 155 | double res = 0; |
| 156 | for( const vector<cvector_t>& q_scan: scans ) { |
| 157 | assert( q_scan.size()== 1 ); |
| 158 | cvector_t uq = q_scan[0]; |
| 159 | const cvector_t q = mag * uq; |
| 160 | complex_t ff[2]; |
| 161 | |
| 162 | ff[0] = ff_modified( q, polyh, false, false ); |
| 163 | ff[1] = ff_modified( q, polyh, true, false ); |
| 164 | |
| 165 | double dev = std::abs(ff[0]-ff[1])*2/(std::abs(ff[0])+std::abs(ff[1])); |
| 166 | res = std::max(res, dev ); |
| 167 | if( 0 && dev>.1 ) |
| 168 | cerr<<ishape<<" "<<mag<<" "<<std::setprecision(16)<< |
| 169 | dev<<" "<<ff[0]<<" "<<ff[1]<<" @ "<<q<<"\n"; |
| 170 | } |
| 171 | cout<<" "<<mag*polyh->getRadius()<<" "<<std::setprecision(8)<<res<<"\n"; |
| 172 | } |
| 173 | cout<<"\n"; |
| 174 | } |
| 175 | |
| 176 | double test_continuity( int ishape, const vector<vector<cvector_t>>& scans ) |
| 177 | { |
| 178 | IFormFactorBorn* polyh( make_particle( ishape ) ); |
| 179 | double maxrelstep = 0; |
| 180 | for( size_t j=0; j<scans.size(); ++j ) { |
| 181 | if( !(j%100) ) |
| 182 | fprintf( stderr, "%2i: %8li/%li\r", ishape, j, scans.size() ); |
| 183 | const vector<cvector_t>& q_scan = scans[j]; |
| 184 | for( size_t i=1; i<q_scan.size(); ++i ) { |
| 185 | complex_t last_ret = polyh->evaluate_for_q(q_scan[i-1]); |
| 186 | Diagnosis last_diag = diagnosis; |
| 187 | complex_t ret = polyh->evaluate_for_q(q_scan[i]); |
| 188 | Diagnosis diag = diagnosis; |
| 189 | if( diag!=last_diag ) |
| 190 | bisect( polyh, ishape, q_scan[i].mag(), |
| 191 | q_scan[i-1], last_ret, last_diag, |
| 192 | q_scan[i], ret, diag, |
| 193 | maxrelstep ); |
| 194 | } |
| 195 | } |
| 196 | fprintf( stderr, "\n" ); |
| 197 | cout<<"shape "<<ishape<<" => max rel step = "<<maxrelstep<<"\n"; |
| 198 | return maxrelstep; |
| 199 | } |
| 200 | |
| 201 | void loop_one_shape( int ishape, const vector<vector<cvector_t>>& scans ) |
| 202 | { |
| 203 | IFormFactorBorn* polyh( make_particle( ishape ) ); |
| 204 | for( size_t j=0; j<scans.size(); ++j ) { |
| 205 | if( !(j%100) ) |
| 206 | fprintf( stderr, "%2i: %8li/%li\r", ishape, j, scans.size() ); |
| 207 | const vector<cvector_t>& q_scan = scans[j]; |
| 208 | for( const cvector_t q: q_scan ) { |
| 209 | complex_t ret = polyh->evaluate_for_q(q); |
| 210 | cout << |
| 211 | ishape<<" " << |
| 212 | std::setprecision(16) << |
| 213 | q[0].real()<<" "<<q[0].imag()<<" " << |
| 214 | q[1].real()<<" "<<q[1].imag()<<" " << |
| 215 | q[2].real()<<" "<<q[2].imag()<<" " << |
| 216 | std::setprecision(17) << |
| 217 | q.mag()<<" " << |
| 218 | ret.real()<<" "<<ret.imag()<<" "<<std::abs(ret)<<" " << |
| 219 | diagnosis.nExpandedFaces<<" "<<diagnosis.maxOrder<<"\n"; |
| 220 | } |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | vector<vector<cvector_t>> create_scans( int mode ) |
| 225 | { |
| 226 | static int n_dir = 7; |
| 227 | cvector_t dir[n_dir] = { |
| 228 | { 1., 0., 0. }, |
| 229 | { 0., 1., 0. }, |
| 230 | { 0., 0., 1. }, |
| 231 | |
| 232 | { 0., 1., 1. }, |
| 233 | { 1., 0., 1. }, |
| 234 | { 1., 1., 0. }, |
| 235 | |
| 236 | { 1., 1., 1. } |
| 237 | }; |
| 238 | |
| 239 | static int n_absdir = 8; // absorption component |
| 240 | cvector_t absdir[n_absdir] = { |
| 241 | { 0., 0., 0. }, |
| 242 | { 1e-15*I, 0., 0. }, |
| 243 | { 0., 1e-10*I, 0. }, |
| 244 | { 0., 0., 1e-5*I }, |
| 245 | { .1*I, 0., 0. }, |
| 246 | { 0., .1*I, 0. }, |
| 247 | { 0., 0., .1*I }, |
| 248 | { .1*I, .1*I, .1*I } |
| 249 | }; |
| 250 | |
| 251 | vector<double> mag; |
| 252 | if ( mode==0 ) { |
| 253 | mag.resize(1); |
| 254 | mag[0] = 1; |
| 255 | } else if ( mode==1 ) { |
| 256 | mag = vector<double>( { 0, 1e-12, 1e-10, 1e-8, 1e-7, 1e-6, 1e-5, 1e-4, 1e-3, |
| 257 | .01, .06, .2, .5, 1, 2, 5, 10, 20, 50, 100 } ); |
| 258 | } else if (mode==2 ) { |
| 259 | mag.resize(1001); |
| 260 | mag[0] = 0.; |
| 261 | for( size_t i=1; i<mag.size(); ++i ) |
| 262 | mag[i] = 1e-10*pow(1e13,(i-1.)/(mag.size()-2)); |
| 263 | } |
| 264 | |
| 265 | static const int n_difmag = 17; |
| 266 | static double difmag[n_difmag] = |
| 267 | { 0., 3e-16, 1e-15, 3e-15, 1e-14, 1e-13, 1e-12, 1e-11, 1e-9, 1e-7, |
| 268 | 1e-5, 1e-3, .01, .03, .1, .2, .3 }; |
| 269 | |
| 270 | vector<vector<cvector_t>> scans; |
| 271 | for( int i=0; i<n_dir; ++i ) { |
| 272 | for( int j=0; j<n_dir; ++j ) { |
| 273 | if( i==j ) |
| 274 | continue; |
| 275 | for( int k=0; k<n_difmag; ++k ) { |
| 276 | for( int l=0; l<n_absdir; ++l ) { |
| 277 | cvector_t uq = ( dir[i].unit() + difmag[k]*dir[j] + absdir[l] ).unit(); |
| 278 | vector<cvector_t> scan; |
| 279 | for( size_t m=0; m<mag.size(); ++m ) |
| 280 | scan.push_back( mag[m] * uq ); |
| 281 | scans.push_back( scan ); |
| 282 | } |
| 283 | } |
| 284 | } |
| 285 | } |
| 286 | return scans; |
| 287 | } |
| 288 | |
| 289 | void help_and_exit() |
| 290 | { |
| 291 | cerr<<"fftest limits inmode debmsg outfilter shape qxr qxi qyr qyi qzr qzi [q]\n"; |
| 292 | cerr<<" limits: \"def\" | qlim qpalim nlim\n"; |
| 293 | cerr<<" inmode: get q from 0:stdin | 1:cmdline | 2:loop\n"; |
| 294 | cerr<<" debmsg: 0..6 (only if inmode=1)\n"; |
| 295 | cerr<<" outfilter: return 0:all | 1:real | 2:imag\n"; |
| 296 | cerr<<"fftest limits loop shape\n"; |
| 297 | cerr<<" limits: \"def\" | qlim qpalim nlim\n"; |
| 298 | cerr<<" loop: 3[cont_test] | 4[matching_test] | 5[for_plot]\n"; |
| 299 | cerr<<" shape: 0[all] | 1..[specific]\n"; |
| 300 | exit(0); |
| 301 | } |
| 302 | |
| 303 | #define NEXTARG if( (++arg)-argv >= argc ) help_and_exit() |
| 304 | #define MULTIARG(n) if( (arg+=n)-argv >= argc ) help_and_exit() |
| 305 | |
| 306 | int main (int argc, const char *argv[]) |
| 307 | { |
| 308 | try { |
| 309 | diagnosis.debmsg = 0; |
| 310 | diagnosis.request_convergence = false; |
| 311 | const char** arg = argv; |
| 312 | NEXTARG; |
| 313 | if ( !strncmp( *arg, "def", 3 ) ) { |
| 314 | // do nothing |
| 315 | } else { |
| 316 | double q_limit = atof( *arg ); |
| 317 | NEXTARG; |
| 318 | double qpa_limit = atof( *arg ); |
| 319 | NEXTARG; |
| 320 | int n_limit = atoi( *arg ); |
| 321 | FormFactorPolyhedron::setLimits( q_limit, n_limit ); |
| 322 | PolyhedralFace::setLimits( qpa_limit, n_limit ); |
| 323 | } |
| 324 | NEXTARG; |
| 325 | int inmode = atoi( *arg ); |
| 326 | if( inmode==1 ) { |
| 327 | NEXTARG; |
| 328 | diagnosis.debmsg = atoi( *arg ); |
| 329 | } |
| 330 | if( inmode<=2 ) { |
| 331 | NEXTARG; |
| 332 | int outfilter = atoi( *arg ); |
| 333 | NEXTARG; |
| 334 | int ishape = atoi( *arg ); |
| 335 | IFormFactorBorn* P( make_particle( ishape ) ); |
| 336 | MULTIARG(6); |
| 337 | cvector_t uq( |
| 338 | complex_t( atof(*(arg-5)), atof(*(arg-4)) ), |
| 339 | complex_t( atof(*(arg-3)), atof(*(arg-2)) ), |
| 340 | complex_t( atof(*(arg-1)), atof(*(arg-0)) ) ); |
| 341 | double mag; |
| 342 | if( inmode==0 ) { |
| 343 | // for use through Frida |
| 344 | int nop; |
| 345 | std::cin >> nop; |
| 346 | while( std::cin >> mag ) |
| 347 | run( P, ishape, mag*uq, outfilter ); |
| 348 | } else if( inmode==1 ) { |
| 349 | NEXTARG; |
| 350 | mag = atof( *arg ); |
| 351 | run( P, ishape, mag*uq, outfilter ); |
| 352 | } else if( inmode==2 ) { |
| 353 | int n_mag = 2001; |
| 354 | double mag_i = 1e-20; |
| 355 | double mag_f = 1e4; |
| 356 | for( int i=1; i<n_mag; ++i ) { |
| 357 | //mag = 180.*i/(n_mag-1); |
| 358 | mag = mag_i*pow(mag_f/mag_i,i/(n_mag-1.)); |
| 359 | run( P, ishape, mag*uq, outfilter ); |
| 360 | } |
| 361 | } |
| 362 | exit(0); |
| 363 | } |
| 364 | // it's some loop |
| 365 | NEXTARG; |
| 366 | int ishapepar = atoi( *arg ); |
| 367 | |
| 368 | if( inmode==3 ) { // continuity test |
| 369 | diagnosis.request_convergence = true; |
| 370 | vector<vector<cvector_t>> scans = create_scans( 1 ); |
| 371 | double totmaxrelstep = 0; |
| 372 | if( ishapepar==0 ) { |
| 373 | for( int ishape=1; ishape<=nshape; ++ishape ) |
| 374 | totmaxrelstep = std::max( totmaxrelstep, test_continuity( ishape, scans ) ); |
| 375 | cout<<"grand total max rel step = "<<totmaxrelstep<<"\n"; |
| 376 | } else { |
| 377 | test_continuity( ishapepar, scans ); |
| 378 | } |
| 379 | exit (0); |
| 380 | } |
| 381 | if( inmode==4 ) { // compare computation methods |
| 382 | vector<vector<cvector_t>> scans = create_scans( 0 ); |
| 383 | if( ishapepar==0 ) { |
| 384 | for( int ishape=1; ishape<=nshape; ++ishape ) |
| 385 | test_matching( ishape, scans ); |
| 386 | } else |
| 387 | test_matching( ishapepar, scans ); |
| 388 | exit (0); |
| 389 | } |
| 390 | // it's a straight loop over q |
| 391 | vector<vector<cvector_t>> scans = create_scans( 2 ); |
| 392 | if( ishapepar==0 ) { |
| 393 | for( int ishape=1; ishape<=nshape; ++ishape ) |
| 394 | loop_one_shape( ishape, scans ); |
| 395 | } else |
| 396 | loop_one_shape( ishapepar, scans ); |
| 397 | exit(0); |
| 398 | |
| 399 | } catch( const char* ex ) { |
| 400 | cerr<<"F(q) failed: "<<ex<<"\n"; |
| 401 | exit(0); |
| 402 | } |
| 403 | } |