source: sasview/pr_inversion/c_extensions/invertor.c @ 4241e48

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 4241e48 was abad620, checked in by Mathieu Doucet <doucetm@…>, 16 years ago

Added alpha estimate

  • Property mode set to 100644
File size: 3.4 KB
Line 
1#include <math.h>
2#include "invertor.h"
3#include <memory.h>
4#include <stdio.h>
5#include <stdlib.h>
6
7double pi = 3.1416;
8
9/**
10 * Deallocate memory
11 */
12void invertor_dealloc(Invertor_params *pars) {
13        free(pars->x);
14        free(pars->y);
15        free(pars->err);
16}
17
18void invertor_init(Invertor_params *pars) {
19        pars->d_max = 180;
20}
21
22
23/**
24 * P(r) of a sphere, for test purposes
25 *
26 * @param R: radius of the sphere
27 * @param r: distance, in the same units as the radius
28 * @return: P(r)
29 */
30double pr_sphere(double R, double r) {
31    if (r <= 2.0*R) {
32        return 12.0* pow(0.5*r/R, 2.0) * pow(1.0-0.5*r/R, 2.0) * ( 2.0 + 0.5*r/R );
33    } else {
34        return 0.0;
35    }
36}
37
38/**
39 * Orthogonal functions:
40 * B(r) = 2r sin(pi*nr/d)
41 *
42 */
43double ortho(double d_max, int n, double r) {
44        return 2.0*r*sin(pi*n*r/d_max);
45}
46
47/**
48 * Fourier transform of the nth orthogonal function
49 *
50 */
51double ortho_transformed(double d_max, int n, double q) {
52        return 8.0*pow(pi, 2.0)/q * d_max * n * pow(-1.0, n+1)
53                *sin(q*d_max) / ( pow(pi*n, 2.0) - pow(q*d_max, 2.0) );
54}
55
56/**
57 * First derivative in of the orthogonal function dB(r)/dr
58 *
59 */
60double ortho_derived(double d_max, int n, double r) {
61    return 2.0*sin(pi*n*r/d_max) + 2.0*r*cos(pi*n*r/d_max);
62}
63
64/**
65 * Scattering intensity calculated from the expansion.
66 */
67double iq(double *pars, double d_max, int n_c, double q) {
68    double sum = 0.0;
69        int i;
70    for (i=0; i<n_c; i++) {
71        sum += pars[i] * ortho_transformed(d_max, i+1, q);
72    }
73    return sum;
74}
75
76/**
77 * P(r) calculated from the expansion.
78 */
79double pr(double *pars, double d_max, int n_c, double r) {
80    double sum = 0.0; 
81        int i;
82    for (i=0; i<n_c; i++) {
83        sum += pars[i] * ortho(d_max, i+1, r);
84    }
85    return sum;
86}
87
88/**
89 * P(r) calculated from the expansion, with errors
90 */
91void pr_err(double *pars, double *err, double d_max, int n_c, 
92                double r, double *pr_value, double *pr_value_err) {
93    double sum = 0.0; 
94    double sum_err = 0.0;
95    double func_value;
96        int i;
97    for (i=0; i<n_c; i++) {
98        func_value = ortho(d_max, i+1, r);
99        sum += pars[i] * func_value;
100        sum_err += err[i]*err[i]*func_value*func_value;
101    }
102    *pr_value = sum;
103    if (sum_err>0) {
104        *pr_value_err = sqrt(sum_err);
105    } else {
106        *pr_value_err = sum;
107    }
108} 
109
110/**
111 * dP(r)/dr calculated from the expansion.
112 */
113double dprdr(double *pars, double d_max, int n_c, double r) {
114    double sum = 0.0; 
115        int i;
116    for (i=0; i<n_c; i++) {
117        sum += pars[i] * 2.0*(sin(pi*(i+1)*r/d_max) + pi*(i+1)*r/d_max * cos(pi*(i+1)*r/d_max));
118    }
119    return sum;
120}
121
122/**
123 * regularization term calculated from the expansion.
124 */
125double reg_term(double *pars, double d_max, int n_c, int nslice) {
126    double sum = 0.0; 
127    double r;
128    double deriv;
129        int i;
130    for (i=0; i<nslice; i++) {
131        r = d_max/(1.0*nslice)*i;
132        deriv = dprdr(pars, d_max, n_c, r);
133        sum += deriv*deriv;
134    }
135    return sum/(1.0*nslice)*d_max;
136}
137
138/**
139 * regularization term calculated from the expansion.
140 */
141double int_p2(double *pars, double d_max, int n_c, int nslice) {
142    double sum = 0.0; 
143    double r; 
144    double value;
145        int i;
146    for (i=0; i<nslice; i++) {
147        r = d_max/(1.0*nslice)*i;
148        value = pr(pars, d_max, n_c, r);
149        sum += value*value;
150    }
151    return sum/(1.0*nslice)*d_max;
152}
153
Note: See TracBrowser for help on using the repository browser.