source: sasview/sansmodels/src/sans/models/c_extensions/spheresld.c @ 01de557

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 01de557 was 839f7e28, checked in by Mathieu Doucet <doucetm@…>, 13 years ago

Re #4 A few more warnings

  • Property mode set to 100644
File size: 6.5 KB
Line 
1/**
2 * spheresld model
3 */
4#include <math.h>
5#include "spheresld.h"
6#include "libmultifunc/librefl.h"
7#include <stdio.h>
8#include <stdlib.h>
9
10#define lamda 4.62
11
12
13double sphere_sld_kernel(double dp[], double q) {
14        int n = dp[0];
15        int i,j,k;
16
17        double scale = dp[1];
18        double thick_inter_core = dp[2];
19        double sld_core = dp[4];
20        double sld_solv = dp[5];
21        double background = dp[6];
22        double npts = dp[57]; //number of sub_layers in each interface
23  double nsl=npts;//21.0; //nsl = Num_sub_layer:  MUST ODD number in double //no other number works now
24  int n_s;
25
26  double sld_i,sld_f,dz,bes,fun,f,vol,vol_pre,vol_sub,qr,r,contr,f2;
27  double sign,slope=0.0;
28  double pi;
29
30  int* fun_type;
31  double* sld;
32  double* thick_inter;
33  double* thick;
34  double* fun_coef;
35
36        double total_thick=0.0;
37
38        fun_type = (int*)malloc((n+2)*sizeof(int));
39  sld = (double*)malloc((n+2)*sizeof(double));
40  thick_inter = (double*)malloc((n+2)*sizeof(double));
41  thick = (double*)malloc((n+2)*sizeof(double));
42  fun_coef = (double*)malloc((n+2)*sizeof(double));
43
44        fun_type[0] = dp[3];
45        fun_coef[0] = fabs(dp[58]);
46        for (i =1; i<=n; i++){
47                sld[i] = dp[i+6];
48                thick_inter[i]= dp[i+16];
49                thick[i] = dp[i+26];
50                fun_type[i] = dp[i+36];
51                fun_coef[i] = fabs(dp[i+46]);
52                total_thick += thick[i] + thick_inter[i];
53        }
54        sld[0] = sld_core;
55        sld[n+1] = sld_solv;
56        thick[0] = dp[59];
57        thick[n+1] = total_thick/5.0;
58        thick_inter[0] = thick_inter_core;
59        thick_inter[n+1] = 0.0;
60        fun_coef[n+1] = 0.0;
61
62    pi = 4.0*atan(1.0);
63    f = 0.0;
64    r = 0.0;
65    vol = 0.0;
66    vol_pre = 0.0;
67    vol_sub = 0.0;
68    sld_f = sld_core;
69
70        //floor_nsl = floor(nsl/2.0);
71
72    dz = 0.0;
73    // iteration for # of shells + core + solvent
74    for (i=0;i<=n+1; i++){
75                //iteration for N sub-layers
76        //if (fabs(thick[i]) <= 1e-24){
77        //   continue;
78        //}
79        // iteration for flat and interface
80                for (j=0;j<2;j++){
81                        // iteration for sub_shells in the interface
82                        // starts from #1 sub-layer
83                        for (n_s=1;n_s<=nsl; n_s++){
84                                // for solvent, it doesn't have an interface
85                                if (i==n+1 && j==1)
86                                        break;
87                                // for flat layers
88                                if (j==0){
89                                        dz = thick[i];
90                                        sld_i = sld[i];
91                                        slope = 0.0;
92                                }
93                                // for interfacial sub_shells
94                                else{
95                                        dz = thick_inter[i]/nsl;
96                                        // find sld_i at the outer boundary of sub-layer #n_s
97                                        sld_i = intersldfunc(fun_type[i],nsl, n_s, fun_coef[i], sld[i], sld[i+1]);
98                                        // calculate slope
99                                        slope= (sld_i -sld_f)/dz;
100                                }
101                                contr = sld_f-slope*r;
102                                // iteration for the left and right boundary of the shells(or sub_shells)
103                                for (k=0; k<2; k++){
104                                        // At r=0, the contribution to I is zero, so skip it.
105                                        if ( i == 0 && j == 0 && k == 0){
106                                                continue;
107                                                }
108                                        // On the top of slovent there is no interface; skip it.
109                                        if (i == n+1 && k == 1){
110                                                continue;
111                                                }
112                                        // At the right side (outer) boundary
113                                        if ( k == 1){
114                                                sign = 1.0;
115                                                r += dz;
116                                                }
117                                        // At the left side (inner) boundary
118                                        else{
119                                                sign = -1.0;
120                                                }
121                                        qr = q * r;
122                                        fun = 0.0;
123                                        if(qr == 0.0){
124                                                // sigular point
125                                                bes = sign * 1.0;
126                                                }
127                                        else{
128                                                // for flat sub-layer
129                                                bes = sign *  3.0 * (sin(qr) - qr * cos(qr)) / (qr * qr * qr);
130                                                // with linear slope
131                                                if (fabs(slope) > 0.0 ){
132                                                        fun = sign * 3.0 * r * (2.0*qr*sin(qr)-((qr*qr)-2.0)*cos(qr))/(qr * qr * qr * qr);
133                                                        }
134                                                }
135                                        // update total volume
136                                        vol = 4.0 * pi / 3.0 * r * r * r;
137                                        // we won't do the following volume correction for now.
138                                        // substrate empty area of volume
139                                        //if (k == 1 && fabs(sld_in[i]-sld_solv) < 1e-04*fabs(sld_solv) && fun_type[i]==0){
140                                        //      vol_sub += (vol_pre - vol);
141                                        //}
142                                        f += vol * (bes * contr + fun * slope);
143                                }
144                                // remember this sld as sld_f
145                                sld_f =sld_i;
146                                // no sub-layer iteration (n_s loop) for the flat layer
147                                if (j==0)
148                                        break;
149                        }
150                }
151    }
152    //vol += vol_sub;
153    f2 = f * f / vol * 1.0e8;
154        f2 *= scale;
155        f2 += background;
156
157  free(fun_type);
158  free(sld);
159  free(thick_inter);
160  free(thick);
161  free(fun_coef);
162
163    return (f2);
164}
165
166/**
167 * Function to evaluate spheresld function
168 * @param pars: parameters of spheresld
169 * @param q: q-value
170 * @return: function value
171 */
172
173double sphere_sld_analytical_1D(SphereSLDParameters *pars, double q) {
174        double dp[60];
175
176        dp[0] = pars->n_shells;
177        dp[1] = pars->scale;
178        dp[2] = pars->thick_inter0;
179        dp[3] = pars->func_inter0;
180        dp[4] = pars->sld_core0;
181        dp[5] = pars->sld_solv;
182        dp[6] = pars->background;
183
184        dp[7] = pars->sld_flat1;
185        dp[8] = pars->sld_flat2;
186        dp[9] = pars->sld_flat3;
187        dp[10] = pars->sld_flat4;
188        dp[11] = pars->sld_flat5;
189        dp[12] = pars->sld_flat6;
190        dp[13] = pars->sld_flat7;
191        dp[14] = pars->sld_flat8;
192        dp[15] = pars->sld_flat9;
193        dp[16] = pars->sld_flat10;
194
195        dp[17] = pars->thick_inter1;
196        dp[18] = pars->thick_inter2;
197        dp[19] = pars->thick_inter3;
198        dp[20] = pars->thick_inter4;
199        dp[21] = pars->thick_inter5;
200        dp[22] = pars->thick_inter6;
201        dp[23] = pars->thick_inter7;
202        dp[24] = pars->thick_inter8;
203        dp[25] = pars->thick_inter9;
204        dp[26] = pars->thick_inter10;
205
206        dp[27] = pars->thick_flat1;
207        dp[28] = pars->thick_flat2;
208        dp[29] = pars->thick_flat3;
209        dp[30] = pars->thick_flat4;
210        dp[31] = pars->thick_flat5;
211        dp[32] = pars->thick_flat6;
212        dp[33] = pars->thick_flat7;
213        dp[34] = pars->thick_flat8;
214        dp[35] = pars->thick_flat9;
215        dp[36] = pars->thick_flat10;
216
217        dp[37] = pars->func_inter1;
218        dp[38] = pars->func_inter2;
219        dp[39] = pars->func_inter3;
220        dp[40] = pars->func_inter4;
221        dp[41] = pars->func_inter5;
222        dp[42] = pars->func_inter6;
223        dp[43] = pars->func_inter7;
224        dp[44] = pars->func_inter8;
225        dp[45] = pars->func_inter9;
226        dp[46] = pars->func_inter10;
227
228        dp[47] = pars->nu_inter1;
229        dp[48] = pars->nu_inter2;
230        dp[49] = pars->nu_inter3;
231        dp[50] = pars->nu_inter4;
232        dp[51] = pars->nu_inter5;
233        dp[52] = pars->nu_inter6;
234        dp[53] = pars->nu_inter7;
235        dp[54] = pars->nu_inter8;
236        dp[55] = pars->nu_inter9;
237        dp[56] = pars->nu_inter10;
238
239        dp[57] = pars->npts_inter;
240        dp[58] = pars->nu_inter0;
241        dp[59] = pars->rad_core0;
242
243        return sphere_sld_kernel(dp, q);
244}
245
246/**
247 * Function to evaluate spheresld function
248 * @param pars: parameters of spheresld
249 * @param q: q-value
250 * @return: function value
251 */
252double sphere_sld_analytical_2D(SphereSLDParameters *pars, double q, double phi) {
253        return sphere_sld_analytical_1D(pars,q);
254}
255
256double sphere_sld_analytical_2DXY(SphereSLDParameters *pars, double qx, double qy){
257        return sphere_sld_analytical_1D(pars,sqrt(qx*qx+qy*qy));
258}
Note: See TracBrowser for help on using the repository browser.