Changes in doc/guide/plugin.rst [3048ec6:7e6bc45e] in sasmodels
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/guide/plugin.rst
r3048ec6 r7e6bc45e 292 292 **Note: The order of the parameters in the definition will be the order of the 293 293 parameters in the user interface and the order of the parameters in Iq(), 294 Iqxy() and form_volume(). And** *scale* **and** *background* **parameters are 295 implicit to all models, so they do not need to be included in the parameter table.** 294 Iqac(), Iqabc() and form_volume(). And** *scale* **and** *background* 295 **parameters are implicit to all models, so they do not need to be included 296 in the parameter table.** 296 297 297 298 - **"name"** is the name of the parameter shown on the FitPage. … … 362 363 scattered intensity. 363 364 364 - "volume" parameters are passed to Iq(), Iqxy(), and form_volume(), and 365 have polydispersity loops generated automatically. 366 367 - "orientation" parameters are only passed to Iqxy(), and have angular 368 dispersion. 365 - "volume" parameters are passed to Iq(), Iqac(), Iqabc() and form_volume(), 366 and have polydispersity loops generated automatically. 367 368 - "orientation" parameters are not passed, but instead are combined with 369 orientation dispersity to translate *qx* and *qy* to *qa*, *qb* and *qc*. 370 These parameters should appear at the end of the table with the specific 371 names *theta*, *phi* and for asymmetric shapes *psi*, in that order. 369 372 370 373 Some models will have integer parameters, such as number of pearls in the … … 419 422 That is, the individual models do not need to include polydispersity 420 423 calculations, but instead rely on numerical integration to compute the 421 appropriately smeared pattern. Angular dispersion values over polar angle 422 $\theta$ requires an additional $\cos \theta$ weighting due to decreased 423 arc length for the equatorial angle $\phi$ with increasing latitude. 424 appropriately smeared pattern. 424 425 425 426 Python Models … … 468 469 barbell). If I(q; pars) is NaN for any $q$, then those parameters will be 469 470 ignored, and not included in the calculation of the weighted polydispersity. 470 471 Similar to *Iq*, you can define *Iqxy(qx, qy, par1, par2, ...)* where the472 parameter list includes any orientation parameters. If *Iqxy* is not defined,473 then it will default to *Iqxy = Iq(sqrt(qx**2+qy**2), par1, par2, ...)*.474 471 475 472 Models should define *form_volume(par1, par2, ...)* where the parameter … … 497 494 } 498 495 499 *Iqxy* is similar to *Iq*, except it uses parameters *qx, qy* instead of *q*,500 and it includes orientation parameters.501 502 496 *form_volume* defines the volume of the shape. As in python models, it 503 497 includes only the volume parameters. 504 498 505 *Iqxy* will default to *Iq(sqrt(qx**2 + qy**2), par1, ...)* and506 *form_volume* will default to 1.0.507 508 499 **source=['fn.c', ...]** includes the listed C source files in the 509 program before *Iq* and *Iqxy* are defined. This allows you to extend the 510 library of C functions available to your model. 500 program before *Iq* and *form_volume* are defined. This allows you to 501 extend the library of C functions available to your model. 502 503 *c_code* includes arbitrary C code into your kernel, which can be 504 handy for defining helper functions for *Iq* and *form_volume*. Note that 505 you can put the full function definition for *Iq* and *form_volume* 506 (include function declaration) into *c_code* as well, or put them into an 507 external C file and add that file to the list of sources. 511 508 512 509 Models are defined using double precision declarations for the … … 533 530 #define INVALID(v) (v.bell_radius < v.radius) 534 531 532 The INVALID define can go into *Iq*, or *c_code*, or an external C file 533 listed in *source*. 534 535 Oriented Shapes 536 ............... 537 538 If the scattering is dependent on the orientation of the shape, then you 539 will need to include *orientation* parameters *theta*, *phi* and *psi* 540 at the end of the parameter table. As described in the section 541 :ref:`orientation`, the individual $(q_x, q_y)$ points on the detector will 542 be rotated into $(q_a, q_b, q_c)$ points relative to the sample in its 543 canonical orientation with $a$-$b$-$c$ aligned with $x$-$y$-$z$ in the 544 laboratory frame and beam travelling along $-z$. 545 546 The oriented C model is called using *Iqabc(qa, qb, qc, par1, par2, ...)* where 547 *par1*, etc. are the parameters to the model. If the shape is rotationally 548 symmetric about *c* then *psi* is not needed, and the model is called 549 as *Iqac(qab, qc, par1, par2, ...)*. In either case, the orientation 550 parameters are not included in the function call. 551 552 For 1D oriented shapes, an integral over all angles is usually needed for 553 the *Iq* function. Given symmetry and the substitution $u = \cos(\alpha)$, 554 $du = -\sin(\alpha)\,d\alpha$ this becomes 555 556 .. math:: 557 558 I(q) &= \frac{1}{4\pi} \int_{-\pi/2}^{pi/2} \int_{-pi}^{pi} 559 F(q_a, q_b, q_c)^2 \sin(\alpha)\,d\beta\,d\alpha \\ 560 &= \frac{8}{4\pi} \int_{0}^{pi/2} \int_{0}^{\pi/2} 561 F^2 \sin(\alpha)\,d\beta\,d\alpha \\ 562 &= \frac{8}{4\pi} \int_1^0 \int_{0}^{\pi/2} - F^2 \,d\beta\,du \\ 563 &= \frac{8}{4\pi} \int_0^1 \int_{0}^{\pi/2} F^2 \,d\beta\,du 564 565 for 566 567 .. math:: 568 569 q_a &= q \sin(\alpha)\sin(\beta) = q \sqrt{1-u^2} \sin(\beta) \\ 570 q_b &= q \sin(\alpha)\cos(\beta) = q \sqrt{1-u^2} \cos(\beta) \\ 571 q_c &= q \cos(\alpha) = q u 572 573 Using the $z, w$ values for Gauss-Legendre integration in "lib/gauss76.c", the 574 numerical integration is then:: 575 576 double outer_sum = 0.0; 577 for (int i = 0; i < GAUSS_N; i++) { 578 const double cos_alpha = 0.5*GAUSS_Z[i] + 0.5; 579 const double sin_alpha = sqrt(1.0 - cos_alpha*cos_alpha); 580 const double qc = cos_alpha * q; 581 double inner_sum = 0.0; 582 for (int j = 0; j < GAUSS_N; j++) { 583 const double beta = M_PI_4 * GAUSS_Z[j] + M_PI_4; 584 double sin_beta, cos_beta; 585 SINCOS(beta, sin_beta, cos_beta); 586 const double qa = sin_alpha * sin_beta * q; 587 const double qb = sin_alpha * cos_beta * q; 588 const double form = Fq(qa, qb, qc, ...); 589 inner_sum += GAUSS_W[j] * form * form; 590 } 591 outer_sum += GAUSS_W[i] * inner_sum; 592 } 593 outer_sum *= 0.25; // = 8/(4 pi) * outer_sum * (pi/2) / 4 594 595 The *z* values for the Gauss-Legendre integration extends from -1 to 1, so 596 the double sum of *w[i]w[j]* explains the factor of 4. Correcting for the 597 average *dz[i]dz[j]* gives $(1-0) \cdot (\pi/2-0) = \pi/2$. The $8/(4 \pi)$ 598 factor comes from the integral over the quadrant. With less symmetry (eg., 599 in the bcc and fcc paracrystal models), then an integral over the entire 600 sphere may be necessary. 601 602 For simpler models which are rotationally symmetric a single integral 603 suffices: 604 605 .. math:: 606 607 I(q) &= \frac{1}{\pi}\int_{-\pi/2}^{\pi/2} 608 F(q_{ab}, q_c)^2 \sin(\alpha)\,d\alpha/\pi \\ 609 &= \frac{2}{\pi} \int_0^1 F^2\,du 610 611 for 612 613 .. math:: 614 615 q_{ab} &= q \sin(\alpha) = q \sqrt{1 - u^2} \\ 616 q_c &= q \cos(\alpha) = q u 617 618 619 with integration loop:: 620 621 double sum = 0.0; 622 for (int i = 0; i < GAUSS_N; i++) { 623 const double cos_alpha = 0.5*GAUSS_Z[i] + 0.5; 624 const double sin_alpha = sqrt(1.0 - cos_alpha*cos_alpha); 625 const double qab = sin_alpha * q; 626 const double qc = cos_alpha * q; 627 const double form = Fq(qab, qc, ...); 628 sum += GAUSS_W[j] * form * form; 629 } 630 sum *= 0.5; // = 2/pi * sum * (pi/2) / 2 631 632 Magnetism 633 ......... 634 635 Magnetism is supported automatically for all shapes by modifying the 636 effective SLD of particle according to the Halpern-Johnson vector 637 describing the interaction between neutron spin and magnetic field. All 638 parameters marked as type *sld* in the parameter table are treated as 639 possibly magnetic particles with magnitude *M0* and direction 640 *mtheta* and *mphi*. Polarization parameters are also provided 641 automatically for magnetic models to set the spin state of the measurement. 642 643 For more complicated systems where magnetism is not uniform throughout 644 the individual particles, you will need to write your own models. 645 You should not mark the nuclear sld as type *sld*, but instead leave 646 them unmarked and provide your own magnetism and polarization parameters. 647 For 2D measurements you will need $(q_x, q_y)$ values for the measurement 648 to compute the proper magnetism and orientation, which you can implement 649 using *Iqxy(qx, qy, par1, par2, ...)*. 650 535 651 Special Functions 536 652 ................. … … 543 659 M_PI, M_PI_2, M_PI_4, M_SQRT1_2, M_E: 544 660 $\pi$, $\pi/2$, $\pi/4$, $1/\sqrt{2}$ and Euler's constant $e$ 545 exp, log, pow(x,y), expm1, sqrt:546 Power functions $e^x$, $\ln x$, $x^y$, $e^x - 1$, $\ sqrt{x}$.547 The function expm1(x) is accurate across all $x$, including $x$548 very close to zero.661 exp, log, pow(x,y), expm1, log1p, sqrt, cbrt: 662 Power functions $e^x$, $\ln x$, $x^y$, $e^x - 1$, $\ln 1 + x$, 663 $\sqrt{x}$, $\sqrt[3]{x}$. The functions expm1(x) and log1p(x) 664 are accurate across all $x$, including $x$ very close to zero. 549 665 sin, cos, tan, asin, acos, atan: 550 666 Trigonometry functions and inverses, operating on radians. … … 557 673 atan(y/x) would return a value in quadrant I. Similarly for 558 674 quadrants II and IV when $x$ and $y$ have opposite sign. 559 f min(x,y), fmax(x,y), trunc, rint:675 fabs(x), fmin(x,y), fmax(x,y), trunc, rint: 560 676 Floating point functions. rint(x) returns the nearest integer. 561 677 NAN: 562 678 NaN, Not a Number, $0/0$. Use isnan(x) to test for NaN. Note that 563 679 you cannot use :code:`x == NAN` to test for NaN values since that 564 will always return false. NAN does not equal NAN! 680 will always return false. NAN does not equal NAN! The alternative, 681 :code:`x != x` may fail if the compiler optimizes the test away. 565 682 INFINITY: 566 683 $\infty, 1/0$. Use isinf(x) to test for infinity, or isfinite(x) … … 734 851 Similar arrays are available in :code:`gauss20.c` for 20-point 735 852 quadrature and in :code:`gauss150.c` for 150-point quadrature. 853 The macros :code:`GAUSS_N`, :code:`GAUSS_Z` and :code:`GAUSS_W` are 854 defined so that you can change the order of the integration by 855 selecting an different source without touching the C code. 736 856 737 857 :code:`source = ["lib/gauss76.c", ...]` … … 791 911 can be a model that runs 1000x faster on a good card. Even your laptop may 792 912 show a 50x improvement or more over the equivalent pure python model. 793 794 External C Models795 .................796 797 External C models are very much like embedded C models, except that798 *Iq*, *Iqxy* and *form_volume* are defined in an external source file799 loaded using the *source=[...]* statement. You need to supply the function800 declarations for each of these that you need instead of building them801 automatically from the parameter table.802 913 803 914 … … 1002 1113 variable name *Rg* for example because $R_g$ is the right name for the model 1003 1114 parameter then ignore the lint errors. Also, ignore *missing-docstring* 1004 for standard model functions *Iq*, *Iq xy*, etc.1115 for standard model functions *Iq*, *Iqac*, etc. 1005 1116 1006 1117 We will have delinting sessions at the SasView Code Camps, where we can
Note: See TracChangeset
for help on using the changeset viewer.