source: sasview/sansmodels/src/sans/models/c_extensions/capcyl.c @ 8bac371

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 8bac371 was 890ac7f1, checked in by Mathieu Doucet <doucetm@…>, 13 years ago

Re #5 fixing samsmodels compilation on MSVC

  • Property mode set to 100644
File size: 4.2 KB
Line 
1/*
2 * Scattering model for a Capped Cylinder
3 */
4#include "capcyl.h"
5#include <math.h>
6#include "GaussWeights.h"
7#include "libCylinder.h"
8#include <stdio.h>
9
10/**
11 * Function to evaluate 1D scattering function
12 * @param pars: parameters of the CappedCylinder
13 * @param q: q-value
14 * @return: function value
15 */
16double capcyl_analytical_1D(CapCylParameters *pars, double q) {
17        double dp[7];
18        double result;
19
20        dp[0] = pars->scale;
21        dp[1] = pars->rad_cyl;
22        dp[2] = pars->len_cyl;
23        dp[3] = pars->rad_cap;
24        dp[4] = pars->sld_capcyl;
25        dp[5] = pars->sld_solv;
26        dp[6] = pars->background;
27
28        result = CappedCylinder(dp, q);
29        // Make Sure it never goes to inf/nan.
30        if ( result == INFINITY || result == NAN){
31                result = pars->background;
32        }
33        return result;
34}
35
36
37double capcyl2d_kernel(double dp[], double q, double alpha) {
38        int i,j;
39        double Pi;
40        double scale,contr,bkg,sldc,slds;
41        double len,rad,hDist,endRad;
42        int nordj=76;
43        double zi=alpha,yyy,answer;                     //running tally of integration
44        double summj,vaj,vbj,zij;                       //for the inner integration
45        double arg1,arg2,inner,be;
46
47
48        scale = dp[0];
49        rad = dp[1];
50        len = dp[2];
51        endRad = dp[3];
52        sldc = dp[4];
53        slds = dp[5];
54        bkg = dp[6];
55
56        hDist = -1.0*sqrt(fabs(endRad*endRad-rad*rad));         //by definition for this model
57
58        contr = sldc-slds;
59
60        Pi = 4.0*atan(1.0);
61        vaj = -1.0*hDist/endRad;
62        vbj = 1.0;              //endpoints of inner integral
63
64        summj=0.0;
65
66        for(j=0;j<nordj;j++) {
67                //20 gauss points for the inner integral
68                zij = ( Gauss76Z[j]*(vbj-vaj) + vaj + vbj )/2.0;                //the "t" dummy
69                yyy = Gauss76Wt[j] * ConvLens_kernel(dp,q,zij,zi);              //uses the same Kernel as the Dumbbell, here L>0
70                summj += yyy;
71        }
72        //now calculate the value of the inner integral
73        inner = (vbj-vaj)/2.0*summj;
74        inner *= 4.0*Pi*endRad*endRad*endRad;
75
76        //now calculate outer integrand
77        arg1 = q*len/2.0*cos(zi);
78        arg2 = q*rad*sin(zi);
79        yyy = inner;
80
81        if(arg2 == 0) {
82                be = 0.5;
83        } else {
84                be = NR_BessJ1(arg2)/arg2;
85        }
86
87        if(arg1 == 0.0) {               //limiting value of sinc(0) is 1; sinc is not defined in math.h
88                yyy += Pi*rad*rad*len*2.0*be;
89        } else {
90                yyy += Pi*rad*rad*len*sin(arg1)/arg1*2.0*be;
91        }
92        yyy *= yyy;   //sin(zi);
93        answer = yyy;
94
95
96        answer /= Pi*rad*rad*len + 2.0*Pi*(2.0*endRad*endRad*endRad/3.0+endRad*endRad*hDist-hDist*hDist*hDist/3.0);             //divide by volume
97        answer *= 1.0e8;                //convert to cm^-1
98        answer *= contr*contr;
99        answer *= scale;
100        answer += bkg;
101
102        return answer;
103}
104
105
106/**
107 * Function to evaluate 2D scattering function
108 * @param pars: parameters of the BarBell
109 * @param q: q-value
110 * @return: function value
111 */
112double capcyl_analytical_2DXY(CapCylParameters *pars, double qx, double qy){
113        double q;
114        q = sqrt(qx*qx+qy*qy);
115        return capcyl_analytical_2D_scaled(pars, q, qx/q, qy/q);
116}
117
118double capcyl_analytical_2D(CapCylParameters *pars, double q, double phi) {
119        return capcyl_analytical_2D_scaled(pars, q, cos(phi), sin(phi));
120}
121
122/**
123 * Function to evaluate 2D scattering function
124 * @param pars: parameters of the BarBell
125 * @param q: q-value
126 * @param q_x: q_x / q
127 * @param q_y: q_y / q
128 * @return: function value
129 */
130double capcyl_analytical_2D_scaled(CapCylParameters *pars, double q, double q_x, double q_y) {
131        double cyl_x, cyl_y, cyl_z;
132        double q_z;
133        double alpha, vol, cos_val;
134        double answer;
135        double dp[7];
136  //convert angle degree to radian
137  double pi = 4.0*atan(1.0);
138  double theta = pars->theta * pi/180.0;
139  double phi = pars->phi * pi/180.0;
140
141        dp[0] = pars->scale;
142        dp[1] = pars->rad_cyl;
143        dp[2] = pars->len_cyl;
144        dp[3] = pars->rad_cap;
145        dp[4] = pars->sld_capcyl;
146        dp[5] = pars->sld_solv;
147        dp[6] = pars->background;
148
149
150        //double Pi = 4.0*atan(1.0);
151    // Cylinder orientation
152    cyl_x = sin(theta) * cos(phi);
153    cyl_y = sin(theta) * sin(phi);
154    cyl_z = cos(theta);
155
156    // q vector
157    q_z = 0;
158
159    // Compute the angle btw vector q and the
160    // axis of the cylinder
161    cos_val = cyl_x*q_x + cyl_y*q_y + cyl_z*q_z;
162
163    // The following test should always pass
164    if (fabs(cos_val)>1.0) {
165        printf("cyl_ana_2D: Unexpected error: cos(alpha)>1\n");
166        return 0;
167    }
168
169    // Note: cos(alpha) = 0 and 1 will get an
170    // undefined value from CylKernel
171        alpha = acos( cos_val );
172
173        // Call the IGOR library function to get the kernel
174        answer = capcyl2d_kernel(dp, q, alpha)/sin(alpha);
175
176
177        return answer;
178
179}
Note: See TracBrowser for help on using the repository browser.