source: sasview/sansmodels/src/sans/models/c_extensions/elliptical_cylinder.c @ 2e38ebb

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 2e38ebb was 975ec8e, checked in by Jae Cho <jhjcho@…>, 15 years ago

working on 2D models. Still need smore corrections and unit tests.

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