source: sasview/sansmodels/src/sans/models/c_extensions/elliptical_cylinder.c @ 72a90bd

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalccostrafo411magnetic_scattrelease-4.1.1release-4.1.2release-4.2.2release_4.0.1ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since 72a90bd was 3fe701a, checked in by Jae Cho <jhjcho@…>, 15 years ago

think 2d bug fixed

  • Property mode set to 100644
File size: 4.2 KB
RevLine 
[ae3ce4e]1/**
2 * Scattering model for a cylinder with elliptical cross-section
3 */
4
5#include "elliptical_cylinder.h"
6#include <math.h>
7#include "libCylinder.h"
8#include <stdio.h>
9#include <stdlib.h>
10
11
12/**
13 * Function to evaluate 1D scattering function
14 * @param pars: parameters of the cylinder
15 * @param q: q-value
16 * @return: function value
17 */
18double elliptical_cylinder_analytical_1D(EllipticalCylinderParameters *pars, double q) {
19        double dp[6];
[3fe701a]20
[ae3ce4e]21        // Fill paramater array
22        dp[0] = pars->scale;
23        dp[1] = pars->r_minor;
24        dp[2] = pars->r_ratio;
25        dp[3] = pars->length;
26        dp[4] = pars->contrast;
27        dp[5] = pars->background;
[3fe701a]28
[ae3ce4e]29        // Call library function to evaluate model
[3fe701a]30        return EllipCyl20(dp, q);
[ae3ce4e]31}
32
[3fe701a]33double elliptical_cylinder_kernel(EllipticalCylinderParameters *pars, double q, double alpha, double psi, double nu) {
[ae3ce4e]34        double qr;
35        double qL;
36        double r_major;
37        double kernel;
[3fe701a]38
[ae3ce4e]39        r_major = pars->r_ratio * pars->r_minor;
40
[3fe701a]41        qr = q*sin(alpha)*sqrt( r_major*r_major*sin(nu)*sin(nu) + pars->r_minor*pars->r_minor*cos(nu)*cos(nu) );
[ae3ce4e]42        qL = q*pars->length*cos(alpha)/2.0;
[3fe701a]43
[ae3ce4e]44        kernel = 2.0*NR_BessJ1(qr)/qr * sin(qL)/qL;
45        return kernel*kernel;
46}
47
48/**
49 * Function to evaluate 2D scattering function
50 * @param pars: parameters of the cylinder
51 * @param q: q-value
52 * @return: function value
53 */
54double elliptical_cylinder_analytical_2DXY(EllipticalCylinderParameters *pars, double qx, double qy) {
55        double q;
56        q = sqrt(qx*qx+qy*qy);
57    return elliptical_cylinder_analytical_2D_scaled(pars, q, qx/q, qy/q);
[3fe701a]58}
[ae3ce4e]59
60/**
61 * Function to evaluate 2D scattering function
62 * @param pars: parameters of the cylinder
63 * @param q: q-value
[3fe701a]64 * @param theta: angle theta = angle wrt z axis
65 * @param phi: angle phi = angle around y axis (starting from the x+-direction as phi = 0)
[ae3ce4e]66 * @return: function value
67 */
68double elliptical_cylinder_analytical_2D(EllipticalCylinderParameters *pars, double q, double phi) {
69    return elliptical_cylinder_analytical_2D_scaled(pars, q, cos(phi), sin(phi));
[3fe701a]70}
[ae3ce4e]71
72/**
73 * Function to evaluate 2D scattering function
74 * @param pars: parameters of the cylinder
75 * @param q: q-value
76 * @param q_x: q_x / q
77 * @param q_y: q_y / q
78 * @return: function value
79 */
80double elliptical_cylinder_analytical_2D_scaled(EllipticalCylinderParameters *pars, double q, double q_x, double q_y) {
81        double cyl_x, cyl_y, cyl_z;
[3fe701a]82        double ell_x, ell_y;
[ae3ce4e]83        double q_z;
84        double alpha, vol, cos_val;
[3fe701a]85        double nu, cos_nu;
[ae3ce4e]86        double answer;
[3fe701a]87
88    //Cylinder orientation
[ae3ce4e]89    cyl_x = sin(pars->cyl_theta) * cos(pars->cyl_phi);
90    cyl_y = sin(pars->cyl_theta) * sin(pars->cyl_phi);
91    cyl_z = cos(pars->cyl_theta);
[3fe701a]92
[ae3ce4e]93    // q vector
94    q_z = 0;
[3fe701a]95
[ae3ce4e]96    // Compute the angle btw vector q and the
97    // axis of the cylinder
98    cos_val = cyl_x*q_x + cyl_y*q_y + cyl_z*q_z;
[3fe701a]99
[ae3ce4e]100    // The following test should always pass
101    if (fabs(cos_val)>1.0) {
102        printf("cyl_ana_2D: Unexpected error: cos(alpha)>1\n");
103        return 0;
104    }
[3fe701a]105
[ae3ce4e]106    // Note: cos(alpha) = 0 and 1 will get an
107    // undefined value from CylKernel
108        alpha = acos( cos_val );
[3fe701a]109
110    //ellipse orientation:
111        // the elliptical corss section was transformed and projected
112        // into the detector plane already through sin(alpha)and furthermore psi remains as same
113        // on the detector plane.
114        // So, all we need is to calculate the angle (nu) of the minor axis of the ellipse wrt
115        // the wave vector q.
116
117        //x- y- component on the detector plane.
118    ell_x =  cos(pars->cyl_psi);
119    ell_y =  sin(pars->cyl_psi);
120
121    // calculate the axis of the ellipse wrt q-coord.
122    cos_nu = ell_x*q_x + ell_y*q_y;
123    nu = acos(cos_nu);
124
125    // The following test should always pass
126    if (fabs(cos_nu)>1.0) {
127        printf("cyl_ana_2D: Unexpected error: cos(nu)>1\n");
128        return 0;
129    }
130
131        answer = elliptical_cylinder_kernel(pars, q, alpha, pars->cyl_psi,nu);
132
[ae3ce4e]133        // Multiply by contrast^2
134        answer *= pars->contrast*pars->contrast;
[3fe701a]135
[ae3ce4e]136        //normalize by cylinder volume
137        //NOTE that for this (Fournet) definition of the integral, one must MULTIPLY by Vcyl
138    vol = acos(-1.0) * pars->r_minor * pars->r_minor * pars->r_ratio * pars->length;
139        answer *= vol;
[3fe701a]140
[ae3ce4e]141        //convert to [cm-1]
142        answer *= 1.0e8;
[3fe701a]143
[ae3ce4e]144        //Scale
145        answer *= pars->scale;
[3fe701a]146
[ae3ce4e]147        // add in the background
148        answer += pars->background;
[3fe701a]149
[ae3ce4e]150        return answer;
151}
[3fe701a]152
Note: See TracBrowser for help on using the repository browser.