Changeset 5bb05a4 in sasview for src/sas/sascalc


Ignore:
Timestamp:
Dec 6, 2017 11:04:17 AM (6 years ago)
Author:
GitHub <noreply@…>
Branches:
master, magnetic_scatt, release-4.2.2, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1243, ticket-1249, ticket885, unittest-saveload
Children:
a64f8f4, b6b81a3, 0a88623, 66285f5
Parents:
f53d684 (diff), bc4f5bb (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
git-author:
Paul Kienzle <pkienzle@…> (12/06/17 11:04:17)
git-committer:
GitHub <noreply@…> (12/06/17 11:04:17)
Message:

Merge pull request 127 from SasView?/ticket-1032-sldi. Closes #1032.

Location:
src/sas/sascalc
Files:
1 added
1 deleted
13 edited
2 moved

Legend:

Unmodified
Added
Removed
  • src/sas/sascalc/calculator/c_extensions/libfunc.c

    rb6c8abe r144e032a  
    9595// spintheta: angle (anti-clock-wise) between neutron spin(up) and x axis 
    9696// Note: all angles are in degrees. 
    97 polar_sld cal_msld(int isangle, double qx, double qy, double bn, 
     97void cal_msld(polar_sld *p_sld, int isangle, double qx, double qy, double bn, 
    9898                                double m01, double mtheta1, double mphi1, 
    9999                                double spinfraci, double spinfracf, double spintheta) 
     
    124124        double my = 0.0; 
    125125        double mz = 0.0; 
    126         polar_sld p_sld; 
    127         p_sld.uu = sld; 
    128         p_sld.dd = sld; 
    129         p_sld.re_ud = 0.0; 
    130         p_sld.im_ud = 0.0; 
    131         p_sld.re_du = 0.0; 
    132         p_sld.im_du = 0.0; 
     126        double uu = sld; 
     127        double dd = sld; 
     128        double re_ud = 0.0; 
     129        double im_ud = 0.0; 
     130        double re_du = 0.0; 
     131        double im_du = 0.0; 
    133132 
    134133        //No mag means no further calculation 
    135         if (isangle>0){ 
     134        if (isangle>0) { 
    136135                if (m_max < 1.0e-32){ 
    137                         p_sld.uu = sqrt(sqrt(in_spin * out_spin)) * p_sld.uu; 
    138                         p_sld.dd = sqrt(sqrt((1.0 - in_spin) * (1.0 - out_spin))) * p_sld.dd; 
    139                         return p_sld; 
    140                 } 
    141         } 
    142         else{ 
    143                 if (fabs(m_max)< 1.0e-32 && fabs(m_phi)< 1.0e-32 && fabs(m_theta)< 1.0e-32){ 
    144                         p_sld.uu = sqrt(sqrt(in_spin * out_spin)) * p_sld.uu; 
    145                         p_sld.dd = sqrt(sqrt((1.0 - in_spin) * (1.0 - out_spin))) * p_sld.dd; 
    146                         return p_sld; 
    147                 } 
    148         } 
    149  
    150         //These are needed because of the precision of inputs 
    151         if (in_spin < 0.0) in_spin = 0.0; 
    152         if (in_spin > 1.0) in_spin = 1.0; 
    153         if (out_spin < 0.0) out_spin = 0.0; 
    154         if (out_spin > 1.0) out_spin = 1.0; 
    155  
    156         if (q_x == 0.0) q_angle = pi / 2.0; 
    157         else q_angle = atan(q_y/q_x); 
    158         if (q_y < 0.0 && q_x < 0.0) q_angle -= pi; 
    159         else if (q_y > 0.0 && q_x < 0.0) q_angle += pi; 
    160  
    161         q_angle = pi/2.0 - q_angle; 
    162         if (q_angle > pi) q_angle -= 2.0 * pi; 
    163         else if (q_angle < -pi) q_angle += 2.0 * pi; 
    164  
    165         if (fabs(q_x) < 1.0e-16 && fabs(q_y) < 1.0e-16){ 
    166                 m_perp = 0.0; 
    167                 } 
    168         else { 
    169                 m_perp = m_max; 
    170                 } 
    171         if (is_angle > 0){ 
    172                 m_phi *= pi/180.0; 
    173                 m_theta *= pi/180.0; 
    174                 mx = m_perp * cos(m_theta) * cos(m_phi); 
    175                 my = m_perp * sin(m_theta); 
    176                 mz = -(m_perp * cos(m_theta) * sin(m_phi)); 
    177         } 
    178         else{ 
    179                 mx = m_perp; 
    180                 my = m_phi; 
    181                 mz = m_theta; 
    182         } 
    183         //ToDo: simplify these steps 
    184         // m_perp1 -m_perp2 
    185         m_perp_x = (mx) *  cos(q_angle); 
    186         m_perp_x -= (my) * sin(q_angle); 
    187         m_perp_y = m_perp_x; 
    188         m_perp_x *= cos(-q_angle); 
    189         m_perp_y *= sin(-q_angle); 
    190         m_perp_z = mz; 
    191  
    192         m_sigma_x = (m_perp_x * cos(-s_theta) - m_perp_y * sin(-s_theta)); 
    193         m_sigma_y = (m_perp_x * sin(-s_theta) + m_perp_y * cos(-s_theta)); 
    194         m_sigma_z = (m_perp_z); 
    195  
    196         //Find b 
    197         p_sld.uu -= m_sigma_x; 
    198         p_sld.dd += m_sigma_x; 
    199         p_sld.re_ud = m_sigma_y; 
    200         p_sld.re_du = m_sigma_y; 
    201         p_sld.im_ud = m_sigma_z; 
    202         p_sld.im_du = -m_sigma_z; 
    203  
    204         p_sld.uu = sqrt(sqrt(in_spin * out_spin)) * p_sld.uu; 
    205         p_sld.dd = sqrt(sqrt((1.0 - in_spin) * (1.0 - out_spin))) * p_sld.dd; 
    206  
    207         p_sld.re_ud = sqrt(sqrt(in_spin * (1.0 - out_spin))) * p_sld.re_ud; 
    208         p_sld.im_ud = sqrt(sqrt(in_spin * (1.0 - out_spin))) * p_sld.im_ud; 
    209         p_sld.re_du = sqrt(sqrt((1.0 - in_spin) * out_spin)) * p_sld.re_du; 
    210         p_sld.im_du = sqrt(sqrt((1.0 - in_spin) * out_spin)) * p_sld.im_du; 
    211  
    212         return p_sld; 
     136                        uu = sqrt(sqrt(in_spin * out_spin)) * uu; 
     137                        dd = sqrt(sqrt((1.0 - in_spin) * (1.0 - out_spin))) * dd; 
     138                } 
     139        } 
     140        else if (fabs(m_max)< 1.0e-32 && fabs(m_phi)< 1.0e-32 && fabs(m_theta)< 1.0e-32){ 
     141                        uu = sqrt(sqrt(in_spin * out_spin)) * uu; 
     142                        dd = sqrt(sqrt((1.0 - in_spin) * (1.0 - out_spin))) * dd; 
     143        } else { 
     144 
     145                //These are needed because of the precision of inputs 
     146                if (in_spin < 0.0) in_spin = 0.0; 
     147                if (in_spin > 1.0) in_spin = 1.0; 
     148                if (out_spin < 0.0) out_spin = 0.0; 
     149                if (out_spin > 1.0) out_spin = 1.0; 
     150 
     151                if (q_x == 0.0) q_angle = pi / 2.0; 
     152                else q_angle = atan(q_y/q_x); 
     153                if (q_y < 0.0 && q_x < 0.0) q_angle -= pi; 
     154                else if (q_y > 0.0 && q_x < 0.0) q_angle += pi; 
     155 
     156                q_angle = pi/2.0 - q_angle; 
     157                if (q_angle > pi) q_angle -= 2.0 * pi; 
     158                else if (q_angle < -pi) q_angle += 2.0 * pi; 
     159 
     160                if (fabs(q_x) < 1.0e-16 && fabs(q_y) < 1.0e-16){ 
     161                        m_perp = 0.0; 
     162                        } 
     163                else { 
     164                        m_perp = m_max; 
     165                        } 
     166                if (is_angle > 0){ 
     167                        m_phi *= pi/180.0; 
     168                        m_theta *= pi/180.0; 
     169                        mx = m_perp * cos(m_theta) * cos(m_phi); 
     170                        my = m_perp * sin(m_theta); 
     171                        mz = -(m_perp * cos(m_theta) * sin(m_phi)); 
     172                } 
     173                else{ 
     174                        mx = m_perp; 
     175                        my = m_phi; 
     176                        mz = m_theta; 
     177                } 
     178                //ToDo: simplify these steps 
     179                // m_perp1 -m_perp2 
     180                m_perp_x = (mx) *  cos(q_angle); 
     181                m_perp_x -= (my) * sin(q_angle); 
     182                m_perp_y = m_perp_x; 
     183                m_perp_x *= cos(-q_angle); 
     184                m_perp_y *= sin(-q_angle); 
     185                m_perp_z = mz; 
     186 
     187                m_sigma_x = (m_perp_x * cos(-s_theta) - m_perp_y * sin(-s_theta)); 
     188                m_sigma_y = (m_perp_x * sin(-s_theta) + m_perp_y * cos(-s_theta)); 
     189                m_sigma_z = (m_perp_z); 
     190 
     191                //Find b 
     192                uu -= m_sigma_x; 
     193                dd += m_sigma_x; 
     194                re_ud = m_sigma_y; 
     195                re_du = m_sigma_y; 
     196                im_ud = m_sigma_z; 
     197                im_du = -m_sigma_z; 
     198 
     199                uu = sqrt(sqrt(in_spin * out_spin)) * uu; 
     200                dd = sqrt(sqrt((1.0 - in_spin) * (1.0 - out_spin))) * dd; 
     201 
     202                re_ud = sqrt(sqrt(in_spin * (1.0 - out_spin))) * re_ud; 
     203                im_ud = sqrt(sqrt(in_spin * (1.0 - out_spin))) * im_ud; 
     204                re_du = sqrt(sqrt((1.0 - in_spin) * out_spin)) * re_du; 
     205                im_du = sqrt(sqrt((1.0 - in_spin) * out_spin)) * im_du; 
     206        } 
     207        p_sld->uu = uu; 
     208        p_sld->dd = dd; 
     209        p_sld->re_ud = re_ud; 
     210        p_sld->im_ud = im_ud; 
     211        p_sld->re_du = re_du; 
     212        p_sld->im_du = im_du; 
    213213} 
    214214 
  • src/sas/sascalc/calculator/c_extensions/libfunc.h

    rb6c8abe r144e032a  
    1818//double gamln(double x); 
    1919 
    20 polar_sld cal_msld(int isangle, double qx, double qy, double bn, double m01, double mtheta1,  
     20void cal_msld(polar_sld*, int isangle, double qx, double qy, double bn, double m01, double mtheta1,  
    2121                        double mphi1, double spinfraci, double spinfracf, double spintheta); 
    2222 
  • src/sas/sascalc/calculator/c_extensions/librefl.c

    r4c29e4d r144e032a  
    77#include <stdio.h> 
    88#include <stdlib.h> 
    9 #if defined(_MSC_VER) 
     9#if defined _MSC_VER  || defined __TINYCC__ 
    1010#define NEED_ERF 
    1111#endif 
     
    2121 
    2222 
    23 #ifdef _WIN32 
     23#ifdef __TINYCC__ 
     24# ifdef isnan 
     25#   undef isnan 
     26# endif 
     27# ifdef isfinite 
     28#   undef isfinite 
     29# endif 
     30# define isnan(x) (x != x) 
     31# define isfinite(x) (x != INFINITY && x != -INFINITY) 
     32#elif defined _WIN32 
    2433# include <float.h> 
    2534# if !defined __MINGW32__ || defined __NO_ISOCEXT 
     
    3039#   define isinf(x) (!_finite(x) && !_isnan(x)) 
    3140#  endif 
    32 #  ifndef finite 
    33 #   define finite(x) _finite(x) 
     41#  ifndef isfinite 
     42#   define isfinite(x) _finite(x) 
    3443#  endif 
    3544# endif 
     
    8493double erf(double x) 
    8594{ 
    86     if (!finite(x)) { 
     95    if (!isfinite(x)) { 
    8796        if (isnan(x)) return x;      /* erf(NaN)   = NaN   */ 
    8897        return (x>0 ? 1.0 : -1.0);   /* erf(+-inf) = +-1.0 */ 
     
    94103double erfc(double x) 
    95104{ 
    96     if (!finite(x)) { 
     105    if (!isfinite(x)) { 
    97106        if (isnan(x)) return x;      /* erfc(NaN)   = NaN      */ 
    98107        return (x>0 ? 0.0 : 2.0);    /* erfc(+-inf) = 0.0, 2.0 */ 
     
    103112#endif // NEED_ERF 
    104113 
    105 complex cassign(real, imag) 
    106         double real, imag; 
    107 { 
    108         complex x; 
    109         x.re = real; 
    110         x.im = imag; 
    111         return x; 
    112 } 
    113  
    114  
    115 complex cplx_add(x,y) 
    116         complex x,y; 
    117 { 
    118         complex z; 
    119         z.re = x.re + y.re; 
    120         z.im = x.im + y.im; 
    121         return z; 
    122 } 
    123  
    124 complex rcmult(x,y) 
    125         double x; 
    126     complex y; 
    127 { 
    128         complex z; 
    129         z.re = x*y.re; 
    130         z.im = x*y.im; 
    131         return z; 
    132 } 
    133  
    134 complex cplx_sub(x,y) 
    135         complex x,y; 
    136 { 
    137         complex z; 
    138         z.re = x.re - y.re; 
    139         z.im = x.im - y.im; 
    140         return z; 
    141 } 
    142  
    143  
    144 complex cplx_mult(x,y) 
    145         complex x,y; 
    146 { 
    147         complex z; 
    148         z.re = x.re*y.re - x.im*y.im; 
    149         z.im = x.re*y.im + x.im*y.re; 
    150         return z; 
    151 } 
    152  
    153 complex cplx_div(x,y) 
    154         complex x,y; 
    155 { 
    156         complex z; 
    157         z.re = (x.re*y.re + x.im*y.im)/(y.re*y.re + y.im*y.im); 
    158         z.im = (x.im*y.re - x.re*y.im)/(y.re*y.re + y.im*y.im); 
    159         return z; 
    160 } 
    161  
    162 complex cplx_exp(b) 
    163         complex b; 
    164 { 
    165         complex z; 
     114void cassign(Cplx *x, double real, double imag) 
     115{ 
     116        x->re = real; 
     117        x->im = imag; 
     118} 
     119 
     120 
     121void cplx_add(Cplx *z, Cplx x, Cplx y) 
     122{ 
     123        z->re = x.re + y.re; 
     124        z->im = x.im + y.im; 
     125} 
     126 
     127void rcmult(Cplx *z, double x, Cplx y) 
     128{ 
     129        z->re = x*y.re; 
     130        z->im = x*y.im; 
     131} 
     132 
     133void cplx_sub(Cplx *z, Cplx x, Cplx y) 
     134{ 
     135        z->re = x.re - y.re; 
     136        z->im = x.im - y.im; 
     137} 
     138 
     139 
     140void cplx_mult(Cplx *z, Cplx x, Cplx y) 
     141{ 
     142        z->re = x.re*y.re - x.im*y.im; 
     143        z->im = x.re*y.im + x.im*y.re; 
     144} 
     145 
     146void cplx_div(Cplx *z, Cplx x, Cplx y) 
     147{ 
     148        z->re = (x.re*y.re + x.im*y.im)/(y.re*y.re + y.im*y.im); 
     149        z->im = (x.im*y.re - x.re*y.im)/(y.re*y.re + y.im*y.im); 
     150} 
     151 
     152void cplx_exp(Cplx *z, Cplx b) 
     153{ 
    166154        double br,bi; 
    167155        br=b.re; 
    168156        bi=b.im; 
    169         z.re = exp(br)*cos(bi); 
    170         z.im = exp(br)*sin(bi); 
    171         return z; 
    172 } 
    173  
    174  
    175 complex cplx_sqrt(z)    //see Schaum`s Math Handbook p. 22, 6.6 and 6.10 
    176         complex z; 
    177 { 
    178         complex c; 
     157        z->re = exp(br)*cos(bi); 
     158        z->im = exp(br)*sin(bi); 
     159} 
     160 
     161 
     162void cplx_sqrt(Cplx *c, Cplx z)    //see Schaum`s Math Handbook p. 22, 6.6 and 6.10 
     163{ 
    179164        double zr,zi,x,y,r,w; 
    180165 
     
    184169        if (zr==0.0 && zi==0.0) 
    185170        { 
    186     c.re=0.0; 
    187         c.im=0.0; 
    188     return c; 
    189         } 
    190         else 
    191         { 
     171                c->re=0.0; 
     172                c->im=0.0; 
     173        } else { 
    192174                x=fabs(zr); 
    193175                y=fabs(zi); 
     
    196178                        r=y/x; 
    197179                        w=sqrt(x)*sqrt(0.5*(1.0+sqrt(1.0+r*r))); 
    198                 } 
    199                 else 
    200                 { 
     180                } else { 
    201181                        r=x/y; 
    202182                        w=sqrt(y)*sqrt(0.5*(r+sqrt(1.0+r*r))); 
     
    204184                if (zr >=0.0) 
    205185                { 
    206                         c.re=w; 
    207                         c.im=zi/(2.0*w); 
     186                        c->re=w; 
     187                        c->im=zi/(2.0*w); 
     188                } else { 
     189                        c->im=(zi >= 0) ? w : -w; 
     190                        c->re=zi/(2.0*c->im); 
    208191                } 
    209                 else 
    210                 { 
    211                         c.im=(zi >= 0) ? w : -w; 
    212                         c.re=zi/(2.0*c.im); 
    213                 } 
    214                 return c; 
    215         } 
    216 } 
    217  
    218 complex cplx_cos(b) 
    219         complex b; 
    220 { 
    221         complex zero,two,z,i,bi,negbi; 
    222         zero = cassign(0.0,0.0); 
    223         two = cassign(2.0,0.0); 
    224         i = cassign(0.0,1.0); 
    225         bi = cplx_mult(b,i); 
    226         negbi = cplx_sub(zero,bi); 
    227         z = cplx_div(cplx_add(cplx_exp(bi),cplx_exp(negbi)),two); 
    228         return z; 
     192        } 
     193} 
     194 
     195void cplx_cos(Cplx *z, Cplx b) 
     196{ 
     197        // cos(b) = (e^bi + e^-bi)/2 
     198        //        = (e^b.im e^-i bi.re) + e^-b.im e^i b.re)/2 
     199        //        = (e^b.im cos(-b.re) + e^b.im sin(-b.re) i)/2 + (e^-b.im cos(b.re) + e^-b.im sin(b.re) i)/2 
     200        //        = e^b.im cos(b.re)/2 - e^b.im sin(b.re)/2 i + 1/e^b.im cos(b.re)/2 + 1/e^b.im sin(b.re)/2 i 
     201        //        = (e^b.im + 1/e^b.im)/2 cos(b.re) + (-e^b.im + 1/e^b.im)/2 sin(b.re) i 
     202        //        = cosh(b.im) cos(b.re) - sinh(b.im) sin(b.re) i 
     203        double exp_b_im = exp(b.im); 
     204        z->re = 0.5*(+exp_b_im + 1.0/exp_b_im) * cos(b.re); 
     205        z->im = -0.5*(exp_b_im - 1.0/exp_b_im) * sin(b.re); 
    229206} 
    230207 
  • src/sas/sascalc/calculator/c_extensions/librefl.h

    r9e531f2 r144e032a  
    55        double re; 
    66        double im; 
    7 } complex; 
     7} Cplx; 
    88 
    99typedef struct { 
    10         complex a; 
    11         complex b; 
    12         complex c; 
    13         complex d; 
     10        Cplx a; 
     11        Cplx b; 
     12        Cplx c; 
     13        Cplx d; 
    1414} matrix; 
    1515 
    16 complex cassign(double real, double imag); 
     16void cassign(Cplx*, double real, double imag); 
    1717 
    18 complex cplx_add(complex x,complex y); 
     18void cplx_add(Cplx*, Cplx x,Cplx y); 
    1919 
    20 complex rcmult(double x,complex y); 
     20void rcmult(Cplx*, double x,Cplx y); 
    2121 
    22 complex cplx_sub(complex x,complex y); 
     22void cplx_sub(Cplx*, Cplx x,Cplx y); 
    2323 
    24 complex cplx_mult(complex x,complex y); 
     24void cplx_mult(Cplx*, Cplx x,Cplx y); 
    2525 
    26 complex cplx_div(complex x,complex y); 
     26void cplx_div(Cplx*, Cplx x,Cplx y); 
    2727 
    28 complex cplx_exp(complex b); 
     28void cplx_exp(Cplx*, Cplx b); 
    2929 
    30 complex cplx_sqrt(complex z); 
     30void cplx_sqrt(Cplx*, Cplx z); 
    3131 
    32 complex cplx_cos(complex b); 
     32void cplx_cos(Cplx*, Cplx b); 
    3333 
    3434double intersldfunc(int fun_type, double n_sub, double i, double nu, double sld_l, double sld_r); 
  • src/sas/sascalc/calculator/c_extensions/sld2i.c

    rb523c0e r144e032a  
    22Computes the (magnetic) scattering form sld (n and m) profile 
    33 */ 
    4 #include "sld2i.hh" 
    54#include <stdio.h> 
    65#include <math.h> 
    7 using namespace std; 
    8 extern "C" { 
    9         #include "libfunc.h" 
    10         #include "librefl.h" 
    11 } 
     6#include "sld2i.h" 
     7#include "libfunc.h" 
     8#include "librefl.h" 
    129/** 
    1310 * Constructor for GenI 
     
    2825 * @param s_theta: angle (from x-axis) of the up spin in degree 
    2926 */ 
    30 GenI :: GenI(int npix, double* x, double* y, double* z, double* sldn, 
     27void initGenI(GenI* this, int is_avg, int npix, double* x, double* y, double* z, double* sldn, 
    3128                        double* mx, double* my, double* mz, double* voli, 
    3229                        double in_spin, double out_spin, 
    3330                        double s_theta) { 
    34         //this->qx_val = qx; 
    35         //this->qy_val = qy; 
    36         //this->qz_val = qz; 
     31        this->is_avg = is_avg; 
    3732        this->n_pix = npix; 
    3833        this->x_val = x; 
     
    4742        this->outspin = out_spin; 
    4843        this->stheta = s_theta; 
    49 }; 
     44} 
    5045 
    5146/** 
    5247 * Compute 2D anisotropic 
    5348 */ 
    54 void GenI :: genicomXY(int npoints, double *qx, double *qy, double *I_out){ 
    55         //npoints is given negative for angular averaging  
     49void genicomXY(GenI* this, int npoints, double *qx, double *qy, double *I_out){ 
     50        //npoints is given negative for angular averaging 
    5651        // Assumes that q doesn't have qz component and sld_n is all real 
    5752        //double q = 0.0; 
     
    5954        polar_sld b_sld; 
    6055        double qr = 0.0; 
    61         complex iqr = cassign(0.0, 0.0); 
    62         complex ephase = cassign(0.0, 0.0); 
    63         complex comp_sld = cassign(0.0, 0.0); 
    64  
    65         complex sumj_uu; 
    66         complex sumj_ud; 
    67         complex sumj_du; 
    68         complex sumj_dd; 
    69         complex temp_fi; 
     56        Cplx iqr; 
     57        Cplx ephase; 
     58        Cplx comp_sld; 
     59 
     60        Cplx sumj_uu; 
     61        Cplx sumj_ud; 
     62        Cplx sumj_du; 
     63        Cplx sumj_dd; 
     64        Cplx temp_fi; 
    7065 
    7166        double count = 0.0; 
    72         //check if this computation is for averaging 
     67        int i, j; 
     68 
     69        cassign(&iqr, 0.0, 0.0); 
     70        cassign(&ephase, 0.0, 0.0); 
     71        cassign(&comp_sld, 0.0, 0.0); 
    7372 
    7473        //Assume that pixel volumes are given in vol_pix in A^3 unit 
     
    7675        //int y_size = 0; //in Ang 
    7776        //int z_size = 0; //in Ang 
    78          
     77 
    7978        // Loop over q-values and multiply apply matrix 
    80          
    81         for(int i=0; i<npoints; i++){ 
     79 
     80        //printf("npoints: %d, npix: %d\n", npoints, this->n_pix); 
     81        for(i=0; i<npoints; i++){ 
    8282                //I_out[i] = 0.0; 
    83                 sumj_uu = cassign(0.0, 0.0); 
    84                 sumj_ud = cassign(0.0, 0.0); 
    85                 sumj_du = cassign(0.0, 0.0); 
    86                 sumj_dd = cassign(0.0, 0.0);             
    87                 //printf ("%d ", i); 
     83                cassign(&sumj_uu, 0.0, 0.0); 
     84                cassign(&sumj_ud, 0.0, 0.0); 
     85                cassign(&sumj_du, 0.0, 0.0); 
     86                cassign(&sumj_dd, 0.0, 0.0); 
     87                //printf("i: %d\n", i); 
    8888                //q = sqrt(qx[i]*qx[i] + qy[i]*qy[i]); // + qz[i]*qz[i]); 
    8989 
    90                 for(int j=0; j<n_pix; j++){ 
    91                         if (sldn_val[j]!=0.0||mx_val[j]!=0.0||my_val[j]!=0.0||mz_val[j]!=0.0) 
    92                         {        
     90                for(j=0; j<this->n_pix; j++){ 
     91                        if (this->sldn_val[j]!=0.0 
     92                                ||this->mx_val[j]!=0.0 
     93                                ||this->my_val[j]!=0.0 
     94                                ||this->mz_val[j]!=0.0) 
     95                        { 
     96                            // printf("i,j: %d,%d\n", i,j); 
    9397                                //anisotropic 
    94                                 temp_fi = cassign(0.0, 0.0); 
    95                                 b_sld = cal_msld(0, qx[i], qy[i], sldn_val[j], 
    96                                                          mx_val[j], my_val[j], mz_val[j], 
    97                                                          inspin, outspin, stheta); 
    98                                 qr = (qx[i]*x_val[j] + qy[i]*y_val[j]); 
    99                                 iqr = cassign(0.0, qr); 
    100                                 ephase = cplx_exp(iqr); 
    101                                  
     98                                cassign(&temp_fi, 0.0, 0.0); 
     99                                cal_msld(&b_sld, 0, qx[i], qy[i], this->sldn_val[j], 
     100                                                         this->mx_val[j], this->my_val[j], this->mz_val[j], 
     101                                                         this->inspin, this->outspin, this->stheta); 
     102                                qr = (qx[i]*this->x_val[j] + qy[i]*this->y_val[j]); 
     103                                cassign(&iqr, 0.0, qr); 
     104                                cplx_exp(&ephase, iqr); 
     105 
    102106                                //Let's multiply pixel(atomic) volume here 
    103                                 ephase = rcmult(vol_pix[j], ephase); 
     107                                rcmult(&ephase, this->vol_pix[j], ephase); 
    104108                                //up_up 
    105                                 if (inspin > 0.0 && outspin > 0.0){ 
    106                                         comp_sld = cassign(b_sld.uu, 0.0); 
    107                                         temp_fi = cplx_mult(comp_sld, ephase); 
    108                                         sumj_uu = cplx_add(sumj_uu, temp_fi); 
     109                                if (this->inspin > 0.0 && this->outspin > 0.0){ 
     110                                        cassign(&comp_sld, b_sld.uu, 0.0); 
     111                                        cplx_mult(&temp_fi, comp_sld, ephase); 
     112                                        cplx_add(&sumj_uu, sumj_uu, temp_fi); 
    109113                                } 
    110114                                //down_down 
    111                                 if (inspin < 1.0 && outspin < 1.0){ 
    112                                         comp_sld = cassign(b_sld.dd, 0.0); 
    113                                         temp_fi = cplx_mult(comp_sld, ephase); 
    114                                         sumj_dd = cplx_add(sumj_dd, temp_fi); 
     115                                if (this->inspin < 1.0 && this->outspin < 1.0){ 
     116                                        cassign(&comp_sld, b_sld.dd, 0.0); 
     117                                        cplx_mult(&temp_fi, comp_sld, ephase); 
     118                                        cplx_add(&sumj_dd, sumj_dd, temp_fi); 
    115119                                } 
    116120                                //up_down 
    117                                 if (inspin > 0.0 && outspin < 1.0){ 
    118                                         comp_sld = cassign(b_sld.re_ud, b_sld.im_ud); 
    119                                         temp_fi = cplx_mult(comp_sld, ephase); 
    120                                         sumj_ud = cplx_add(sumj_ud, temp_fi); 
     121                                if (this->inspin > 0.0 && this->outspin < 1.0){ 
     122                                        cassign(&comp_sld, b_sld.re_ud, b_sld.im_ud); 
     123                                        cplx_mult(&temp_fi, comp_sld, ephase); 
     124                                        cplx_add(&sumj_ud, sumj_ud, temp_fi); 
    121125                                } 
    122126                                //down_up 
    123                                 if (inspin < 1.0 && outspin > 0.0){ 
    124                                         comp_sld = cassign(b_sld.re_du, b_sld.im_du); 
    125                                         temp_fi = cplx_mult(comp_sld, ephase); 
    126                                         sumj_du = cplx_add(sumj_du, temp_fi); 
    127                                 } 
    128  
     127                                if (this->inspin < 1.0 && this->outspin > 0.0){ 
     128                                        cassign(&comp_sld, b_sld.re_du, b_sld.im_du); 
     129                                        cplx_mult(&temp_fi, comp_sld, ephase); 
     130                                        cplx_add(&sumj_du, sumj_du, temp_fi); 
     131                                } 
    129132 
    130133                                if (i == 0){ 
    131                                         count += vol_pix[j]; 
     134                                        count += this->vol_pix[j]; 
    132135                                } 
    133136                        } 
     
    142145                I_out[i] *= (1.0E+8 / count); //in cm (unit) / number; //to be multiplied by vol_pix 
    143146        } 
    144         //printf ("count = %d %g %g %g %g\n", count, sldn_val[0],mx_val[0], my_val[0], mz_val[0]); 
     147        //printf("count = %d %g %g %g %g\n", count, this->sldn_val[0],this->mx_val[0], this->my_val[0], this->mz_val[0]); 
    145148} 
    146149/** 
     
    149152 * Also assumes there is no polarization: No dependency on spin 
    150153 */ 
    151 void GenI :: genicom(int npoints, double *q, double *I_out){ 
    152         //npoints is given negative for angular averaging  
     154void genicom(GenI* this, int npoints, double *q, double *I_out){ 
     155        //npoints is given negative for angular averaging 
    153156        // Assumes that q doesn't have qz component and sld_n is all real 
    154157        //double Pi = 4.0*atan(1.0); 
    155         int is_sym = 0; 
    156158        double qr = 0.0; 
    157159        double sumj; 
    158160        double sld_j = 0.0; 
    159161        double count = 0.0; 
    160         if (n_pix < 0 ){ 
    161                 is_sym = 1; 
    162                 n_pix = n_pix * -1; 
    163         } 
     162        int i, j, k; 
     163 
    164164        //Assume that pixel volumes are given in vol_pix in A^3 unit 
    165165        // Loop over q-values and multiply apply matrix 
    166         for(int i=0; i<npoints; i++){ 
    167                 sumj =0.0;               
    168                 for(int j=0; j<n_pix; j++){ 
     166        for(i=0; i<npoints; i++){ 
     167                sumj =0.0; 
     168                for(j=0; j<this->n_pix; j++){ 
    169169                        //Isotropic: Assumes all slds are real (no magnetic) 
    170170                        //Also assumes there is no polarization: No dependency on spin 
    171                         if (is_sym == 1){ 
     171                        if (this->is_avg == 1){ 
    172172                                // approximation for a spherical symmetric particle 
    173                                 qr = sqrt(x_val[j]*x_val[j]+y_val[j]*y_val[j]+z_val[j]*z_val[j])*q[i]; 
     173                                qr = sqrt(this->x_val[j]*this->x_val[j]+this->y_val[j]*this->y_val[j]+this->z_val[j]*this->z_val[j])*q[i]; 
    174174                                if (qr > 0.0){ 
    175175                                        qr = sin(qr) / qr; 
    176                                         sumj += sldn_val[j] * vol_pix[j] * qr; 
     176                                        sumj += this->sldn_val[j] * this->vol_pix[j] * qr; 
    177177                                } 
    178178                                else{ 
    179                                         sumj += sldn_val[j] * vol_pix[j]; 
     179                                        sumj += this->sldn_val[j] * this->vol_pix[j]; 
    180180                                } 
    181181                        } 
     
    183183                                //full calculation 
    184184                                //pragma omp parallel for 
    185                                 for(int k=0; k<n_pix; k++){ 
    186                                         sld_j =  sldn_val[j] * sldn_val[k] * vol_pix[j] * vol_pix[k]; 
    187                                         qr = (x_val[j]-x_val[k])*(x_val[j]-x_val[k])+ 
    188                                                       (y_val[j]-y_val[k])*(y_val[j]-y_val[k])+  
    189                                                       (z_val[j]-z_val[k])*(z_val[j]-z_val[k]); 
     185                                for(k=0; k<this->n_pix; k++){ 
     186                                        sld_j =  this->sldn_val[j] * this->sldn_val[k] * this->vol_pix[j] * this->vol_pix[k]; 
     187                                        qr = (this->x_val[j]-this->x_val[k])*(this->x_val[j]-this->x_val[k])+ 
     188                                                      (this->y_val[j]-this->y_val[k])*(this->y_val[j]-this->y_val[k])+ 
     189                                                      (this->z_val[j]-this->z_val[k])*(this->z_val[j]-this->z_val[k]); 
    190190                                        qr = sqrt(qr) * q[i]; 
    191191                                        if (qr > 0.0){ 
     
    198198                        } 
    199199                        if (i == 0){ 
    200                                 count += vol_pix[j]; 
     200                                count += this->vol_pix[j]; 
    201201                        } 
    202202                } 
    203203                I_out[i] = sumj; 
    204                 if (is_sym == 1){ 
     204                if (this->is_avg == 1) { 
    205205                        I_out[i] *= sumj; 
    206206                } 
    207207                I_out[i] *= (1.0E+8 / count); //in cm (unit) / number; //to be multiplied by vol_pix 
    208208        } 
    209         //printf ("count = %d %g %g %g %g\n", count, sldn_val[0],mx_val[0], my_val[0], mz_val[0]); 
     209        //printf("count = %d %g %g %g %g\n", count, sldn_val[0],mx_val[0], my_val[0], mz_val[0]); 
    210210} 
  • src/sas/sascalc/calculator/c_extensions/sld2i_module.c

    r1d014cb ra1daf86  
    44#include <Python.h> 
    55#include <stdio.h> 
    6 #include <sld2i.hh> 
     6#include "sld2i.h" 
    77 
    88#if PY_MAJOR_VERSION < 3 
     
    3535void 
    3636del_sld2i(PyObject *obj){ 
    37         GenI* sld2i = static_cast<GenI *>(PyCapsule_GetPointer(obj, "GenI")); 
    38         delete sld2i; 
    39         return; 
     37#if PY_MAJOR_VERSION < 3 
     38        GenI* sld2i = (GenI *)obj; 
     39#else 
     40        GenI* sld2i = (GenI *)(PyCapsule_GetPointer(obj, "GenI")); 
     41#endif 
     42        PyMem_Free((void *)sld2i); 
    4043} 
    4144 
     
    4346 * Create a GenI as a python object by supplying arrays 
    4447 */ 
    45 PyObject * new_GenI(PyObject *, PyObject *args) { 
     48PyObject * new_GenI(PyObject *self, PyObject *args) { 
    4649        PyObject *x_val_obj; 
    4750        PyObject *y_val_obj; 
     
    5255        PyObject *mz_val_obj; 
    5356        PyObject *vol_pix_obj; 
    54         Py_ssize_t n_x; 
    55         //PyObject rlimit_obj; 
    56         //PyObject npoints_obj; 
    57         //PyObject nrbins_obj; 
    58         //PyObject nphibins_obj; 
    59         int n_pix; 
     57        Py_ssize_t n_x, n_y, n_z, n_sld, n_mx, n_my, n_mz, n_vol_pix; 
     58        int is_avg; 
    6059        double* x_val; 
    6160        double* y_val; 
     
    6968        double outspin; 
    7069        double stheta; 
    71  
    72         if (!PyArg_ParseTuple(args, "iOOOOOOOOddd", &n_pix, &x_val_obj, &y_val_obj, &z_val_obj, &sldn_val_obj, &mx_val_obj, &my_val_obj, &mz_val_obj, &vol_pix_obj, &inspin, &outspin, &stheta)) return NULL; 
    73         OUTVECTOR(x_val_obj, x_val, n_x); 
    74         OUTVECTOR(y_val_obj, y_val, n_x); 
    75         OUTVECTOR(z_val_obj, z_val, n_x); 
    76         OUTVECTOR(sldn_val_obj, sldn_val, n_x); 
    77         OUTVECTOR(mx_val_obj, mx_val, n_x); 
    78         OUTVECTOR(my_val_obj, my_val, n_x); 
    79         OUTVECTOR(mz_val_obj, mz_val, n_x); 
    80         OUTVECTOR(vol_pix_obj, vol_pix, n_x); 
    81         GenI* sld2i = new GenI(n_pix,x_val,y_val,z_val,sldn_val,mx_val,my_val,mz_val,vol_pix,inspin,outspin,stheta); 
    82         return PyCapsule_New(sld2i, "GenI", del_sld2i); 
     70        PyObject *obj; 
     71        GenI* sld2i; 
     72 
     73        //printf("new GenI\n"); 
     74        if (!PyArg_ParseTuple(args, "iOOOOOOOOddd", &is_avg, &x_val_obj, &y_val_obj, &z_val_obj, &sldn_val_obj, &mx_val_obj, &my_val_obj, &mz_val_obj, &vol_pix_obj, &inspin, &outspin, &stheta)) return NULL; 
     75        INVECTOR(x_val_obj, x_val, n_x); 
     76        INVECTOR(y_val_obj, y_val, n_y); 
     77        INVECTOR(z_val_obj, z_val, n_z); 
     78        INVECTOR(sldn_val_obj, sldn_val, n_sld); 
     79        INVECTOR(mx_val_obj, mx_val, n_mx); 
     80        INVECTOR(my_val_obj, my_val, n_my); 
     81        INVECTOR(mz_val_obj, mz_val, n_mz); 
     82        INVECTOR(vol_pix_obj, vol_pix, n_vol_pix); 
     83        sld2i = PyMem_Malloc(sizeof(GenI)); 
     84        //printf("sldi:%p\n", sld2i); 
     85        if (sld2i != NULL) { 
     86                initGenI(sld2i,is_avg,(int)n_x,x_val,y_val,z_val,sldn_val,mx_val,my_val,mz_val,vol_pix,inspin,outspin,stheta); 
     87        } 
     88        obj = PyCapsule_New(sld2i, "GenI", del_sld2i); 
     89        //printf("constructed %p\n", obj); 
     90        return obj; 
    8391} 
    8492 
     
    8694 * GenI the given input (2D) according to a given object 
    8795 */ 
    88 PyObject * genicom_inputXY(PyObject *, PyObject *args) { 
    89         int npoints; 
     96PyObject * genicom_inputXY(PyObject *self, PyObject *args) { 
     97        PyObject *gen_obj; 
    9098        PyObject *qx_obj; 
     99        PyObject *qy_obj; 
     100        PyObject *I_out_obj; 
     101        Py_ssize_t n_qx, n_qy, n_out; 
    91102        double *qx; 
    92         PyObject *qy_obj; 
    93103        double *qy; 
    94         PyObject *I_out_obj; 
    95         Py_ssize_t n_out; 
    96104        double *I_out; 
    97         PyObject *gen_obj; 
    98  
    99         if (!PyArg_ParseTuple(args, "OiOOO",  &gen_obj, &npoints, &qx_obj, &qy_obj, &I_out_obj)) return NULL; 
    100         OUTVECTOR(qx_obj, qx, n_out); 
    101         OUTVECTOR(qy_obj, qy, n_out); 
     105        GenI* sld2i; 
     106 
     107        //printf("in genicom_inputXY\n"); 
     108        if (!PyArg_ParseTuple(args, "OOOO",  &gen_obj, &qx_obj, &qy_obj, &I_out_obj)) return NULL; 
     109        sld2i = (GenI *)PyCapsule_GetPointer(gen_obj, "GenI"); 
     110        INVECTOR(qx_obj, qx, n_qx); 
     111        INVECTOR(qy_obj, qy, n_qy); 
    102112        OUTVECTOR(I_out_obj, I_out, n_out); 
     113        //printf("qx, qy, I_out: %d %d %d, %d %d %d\n", qx, qy, I_out, n_qx, n_qy, n_out); 
    103114 
    104115        // Sanity check 
    105         //if(n_in!=n_out) return Py_BuildValue("i",-1); 
    106  
    107         // Set the array pointers 
    108         void *temp = PyCapsule_GetPointer(gen_obj, "GenI"); 
    109         GenI* s = static_cast<GenI *>(temp); 
    110  
    111         s->genicomXY(npoints, qx, qy, I_out); 
     116        //if(n_q!=n_out) return Py_BuildValue("i",-1); 
     117 
     118        genicomXY(sld2i, (int)n_qx, qx, qy, I_out); 
     119        //printf("done calc\n"); 
    112120        //return PyCObject_FromVoidPtr(s, del_genicom); 
    113121        return Py_BuildValue("i",1); 
     
    117125 * GenI the given 1D input according to a given object 
    118126 */ 
    119 PyObject * genicom_input(PyObject *, PyObject *args) { 
    120         int npoints; 
     127PyObject * genicom_input(PyObject *self, PyObject *args) { 
     128        PyObject *gen_obj; 
    121129        PyObject *q_obj; 
     130        PyObject *I_out_obj; 
     131        Py_ssize_t n_q, n_out; 
    122132        double *q; 
    123         PyObject *I_out_obj; 
    124         Py_ssize_t n_out; 
    125133        double *I_out; 
    126         PyObject *gen_obj; 
    127  
    128         if (!PyArg_ParseTuple(args, "OiOO",  &gen_obj, &npoints, &q_obj, &I_out_obj)) return NULL; 
    129         OUTVECTOR(q_obj, q, n_out); 
     134        GenI *sld2i; 
     135 
     136        if (!PyArg_ParseTuple(args, "OOO",  &gen_obj, &q_obj, &I_out_obj)) return NULL; 
     137        sld2i = (GenI *)PyCapsule_GetPointer(gen_obj, "GenI"); 
     138        INVECTOR(q_obj, q, n_q); 
    130139        OUTVECTOR(I_out_obj, I_out, n_out); 
    131140 
    132141        // Sanity check 
    133         //if(n_in!=n_out) return Py_BuildValue("i",-1); 
    134  
    135         // Set the array pointers 
    136         void *temp = PyCapsule_GetPointer(gen_obj, "GenI"); 
    137         GenI* s = static_cast<GenI *>(temp); 
    138  
    139         s->genicom(npoints, q, I_out); 
    140         //return PyCObject_FromVoidPtr(s, del_genicom); 
     142        //if (n_q!=n_out) return Py_BuildValue("i",-1); 
     143 
     144        genicom(sld2i, (int)n_q, q, I_out); 
    141145        return Py_BuildValue("i",1); 
    142146} 
  • src/sas/sascalc/calculator/sas_gen.py

    rb58265c3 r144e032a  
    118118        self.is_avg = is_avg 
    119119 
    120     def _gen(self, x, y, i): 
     120    def _gen(self, qx, qy): 
    121121        """ 
    122122        Evaluate the function 
     
    129129        pos_y = self.data_y 
    130130        pos_z = self.data_z 
    131         len_x = len(pos_x) 
    132131        if self.is_avg is None: 
    133             len_x *= -1 
    134132            pos_x, pos_y, pos_z = transform_center(pos_x, pos_y, pos_z) 
    135         len_q = len(x) 
    136133        sldn = copy.deepcopy(self.data_sldn) 
    137134        sldn -= self.params['solvent_SLD'] 
    138         model = mod.new_GenI(len_x, pos_x, pos_y, pos_z, 
    139                              sldn, self.data_mx, self.data_my, 
    140                              self.data_mz, self.data_vol, 
    141                              self.params['Up_frac_in'], 
    142                              self.params['Up_frac_out'], 
    143                              self.params['Up_theta']) 
    144         if y == []: 
    145             mod.genicom(model, len_q, x, i) 
    146         else: 
    147             mod.genicomXY(model, len_q, x, y, i) 
     135        # **** WARNING **** new_GenI holds pointers to numpy vectors 
     136        # be sure that they are contiguous double precision arrays and make  
     137        # sure the GC doesn't eat them before genicom is called. 
     138        # TODO: rewrite so that the parameters are passed directly to genicom 
     139        args = ( 
     140            (1 if self.is_avg else 0), 
     141            pos_x, pos_y, pos_z, 
     142            sldn, self.data_mx, self.data_my, 
     143            self.data_mz, self.data_vol, 
     144            self.params['Up_frac_in'], 
     145            self.params['Up_frac_out'], 
     146            self.params['Up_theta']) 
     147        model = mod.new_GenI(*args) 
     148        if len(qy): 
     149            qx, qy = _vec(qx), _vec(qy) 
     150            I_out = np.empty_like(qx) 
     151            #print("npoints", qx.shape, "npixels", pos_x.shape) 
     152            mod.genicomXY(model, qx, qy, I_out) 
     153            #print("I_out after", I_out) 
     154        else: 
     155            qx = _vec(qx) 
     156            I_out = np.empty_like(qx) 
     157            mod.genicom(model, qx, I_out) 
    148158        vol_correction = self.data_total_volume / self.params['total_volume'] 
    149         return  self.params['scale'] * vol_correction * i + \ 
    150                         self.params['background'] 
     159        result = (self.params['scale'] * vol_correction * I_out 
     160                  + self.params['background']) 
     161        return result 
    151162 
    152163    def set_sld_data(self, sld_data=None): 
     
    156167        self.sld_data = sld_data 
    157168        self.data_pos_unit = sld_data.pos_unit 
    158         self.data_x = sld_data.pos_x 
    159         self.data_y = sld_data.pos_y 
    160         self.data_z = sld_data.pos_z 
    161         self.data_sldn = sld_data.sld_n 
    162         self.data_mx = sld_data.sld_mx 
    163         self.data_my = sld_data.sld_my 
    164         self.data_mz = sld_data.sld_mz 
    165         self.data_vol = sld_data.vol_pix 
     169        self.data_x = _vec(sld_data.pos_x) 
     170        self.data_y = _vec(sld_data.pos_y) 
     171        self.data_z = _vec(sld_data.pos_z) 
     172        self.data_sldn = _vec(sld_data.sld_n) 
     173        self.data_mx = _vec(sld_data.sld_mx) 
     174        self.data_my = _vec(sld_data.sld_my) 
     175        self.data_mz = _vec(sld_data.sld_mz) 
     176        self.data_vol = _vec(sld_data.vol_pix) 
    166177        self.data_total_volume = sum(sld_data.vol_pix) 
    167178        self.params['total_volume'] = sum(sld_data.vol_pix) 
     
    180191        :return: (I value) 
    181192        """ 
    182         if x.__class__.__name__ == 'list': 
     193        if isinstance(x, list): 
    183194            if len(x[1]) > 0: 
    184195                msg = "Not a 1D." 
    185196                raise ValueError(msg) 
    186             i_out = np.zeros_like(x[0]) 
    187197            # 1D I is found at y =0 in the 2D pattern 
    188             out = self._gen(x[0], [], i_out) 
     198            out = self._gen(x[0], []) 
    189199            return out 
    190200        else: 
     
    199209        :Use this runXY() for the computation 
    200210        """ 
    201         if x.__class__.__name__ == 'list': 
    202             i_out = np.zeros_like(x[0]) 
    203             out = self._gen(x[0], x[1], i_out) 
    204             return out 
     211        if isinstance(x, list): 
     212            return self._gen(x[0], x[1]) 
    205213        else: 
    206214            msg = "Q must be given as list of qx's and qy's" 
     
    214222                      where qx,qy are 1D ndarrays (for 2D). 
    215223        """ 
    216         if qdist.__class__.__name__ == 'list': 
    217             if len(qdist[1]) < 1: 
    218                 out = self.run(qdist) 
    219             else: 
    220                 out = self.runXY(qdist) 
    221             return out 
     224        if isinstance(qdist, list): 
     225            return self.run(qdist) if len(qdist[1]) < 1 else self.runXY(qdist) 
    222226        else: 
    223227            mesg = "evalDistribution is expecting an ndarray of " 
    224228            mesg += "a list [qx,qy] where qx,qy are arrays." 
    225229            raise RuntimeError(mesg) 
     230 
     231def _vec(v): 
     232    return np.ascontiguousarray(v, 'd') 
    226233 
    227234class OMF2SLD(object): 
     
    10411048        self.line_z = line_z 
    10421049 
     1050def _get_data_path(*path_parts): 
     1051    from os.path import realpath, join as joinpath, dirname, abspath 
     1052    # in sas/sascalc/calculator;  want sas/sasview/test 
     1053    return joinpath(dirname(realpath(__file__)), 
     1054                    '..', '..', 'sasview', 'test', *path_parts) 
     1055 
    10431056def test_load(): 
    10441057    """ 
     
    10461059    """ 
    10471060    from mpl_toolkits.mplot3d import Axes3D 
    1048     current_dir = os.path.abspath(os.path.curdir) 
    1049     print(current_dir) 
    1050     for i in range(6): 
    1051         current_dir, _ = os.path.split(current_dir) 
    1052         tfile = os.path.join(current_dir, "test", "CoreXY_ShellZ.txt") 
    1053         ofile = os.path.join(current_dir, "test", "A_Raw_Example-1.omf") 
    1054         if os.path.isfile(tfile): 
    1055             tfpath = tfile 
    1056             ofpath = ofile 
    1057             break 
     1061    tfpath = _get_data_path("1d_data", "CoreXY_ShellZ.txt") 
     1062    ofpath = _get_data_path("coordinate_data", "A_Raw_Example-1.omf") 
     1063    if not os.path.isfile(tfpath) or not os.path.isfile(ofpath): 
     1064        raise ValueError("file(s) not found: %r, %r"%(tfpath, ofpath)) 
    10581065    reader = SLDReader() 
    10591066    oreader = OMFReader() 
    1060     output = decode(reader.read(tfpath)) 
    1061     ooutput = decode(oreader.read(ofpath)) 
     1067    output = reader.read(tfpath) 
     1068    ooutput = oreader.read(ofpath) 
    10621069    foutput = OMF2SLD() 
    10631070    foutput.set_data(ooutput) 
     
    10881095    plt.show() 
    10891096 
     1097def test_save(): 
     1098    ofpath = _get_data_path("coordinate_data", "A_Raw_Example-1.omf") 
     1099    if not os.path.isfile(ofpath): 
     1100        raise ValueError("file(s) not found: %r"%(ofpath,)) 
     1101    oreader = OMFReader() 
     1102    omfdata = oreader.read(ofpath) 
     1103    omf2sld = OMF2SLD() 
     1104    omf2sld.set_data(omfdata) 
     1105    writer = SLDReader() 
     1106    writer.write("out.txt", omf2sld.output) 
     1107 
    10901108def test(): 
    10911109    """ 
    10921110        Test code 
    10931111    """ 
    1094     current_dir = os.path.abspath(os.path.curdir) 
    1095     for i in range(3): 
    1096         current_dir, _ = os.path.split(current_dir) 
    1097         ofile = os.path.join(current_dir, "test", "A_Raw_Example-1.omf") 
    1098         if os.path.isfile(ofile): 
    1099             ofpath = ofile 
    1100             break 
     1112    ofpath = _get_data_path("coordinate_data", "A_Raw_Example-1.omf") 
     1113    if not os.path.isfile(ofpath): 
     1114        raise ValueError("file(s) not found: %r"%(ofpath,)) 
    11011115    oreader = OMFReader() 
    1102     ooutput = decode(oreader.read(ofpath)) 
    1103     foutput = OMF2SLD() 
    1104     foutput.set_data(ooutput) 
    1105     writer = SLDReader() 
    1106     writer.write(os.path.join(os.path.dirname(ofpath), "out.txt"), 
    1107                  foutput.output) 
     1116    omfdata = oreader.read(ofpath) 
     1117    omf2sld = OMF2SLD() 
     1118    omf2sld.set_data(omfdata) 
    11081119    model = GenSAS() 
    1109     model.set_sld_data(foutput.output) 
    1110     x = np.arange(1000)/10000. + 1e-5 
    1111     y = np.arange(1000)/10000. + 1e-5 
    1112     i = np.zeros(1000) 
    1113     model.runXY([x, y, i]) 
     1120    model.set_sld_data(omf2sld.output) 
     1121    x = np.linspace(0, 0.1, 11)[1:] 
     1122    return model.runXY([x, x]) 
    11141123 
    11151124if __name__ == "__main__": 
     1125    #test_load() 
     1126    #test_save() 
     1127    #print(test()) 
    11161128    test() 
    1117     test_load() 
  • src/sas/sascalc/dataloader/file_reader_base_class.py

    r20fa5fe r3053a4a  
    2727 
    2828class FileReader(object): 
    29     # List of Data1D and Data2D objects to be sent back to data_loader 
    30     output = [] 
    31     # Current plottable_(1D/2D) object being loaded in 
    32     current_dataset = None 
    33     # Current DataInfo object being loaded in 
    34     current_datainfo = None 
    3529    # String to describe the type of data this reader can load 
    3630    type_name = "ASCII" 
     
    4337    # Able to import the unit converter 
    4438    has_converter = True 
    45     # Open file handle 
    46     f_open = None 
    4739    # Default value of zero 
    4840    _ZERO = 1e-16 
    4941 
     42    def __init__(self): 
     43        # List of Data1D and Data2D objects to be sent back to data_loader 
     44        self.output = [] 
     45        # Current plottable_(1D/2D) object being loaded in 
     46        self.current_dataset = None 
     47        # Current DataInfo object being loaded in 
     48        self.current_datainfo = None 
     49        # File path sent to reader 
     50        self.filepath = None 
     51        # Open file handle 
     52        self.f_open = None 
     53 
    5054    def read(self, filepath): 
    5155        """ 
     
    5458        :param filepath: The full or relative path to a file to be loaded 
    5559        """ 
     60        self.filepath = filepath 
    5661        if os.path.isfile(filepath): 
    5762            basename, extension = os.path.splitext(os.path.basename(filepath)) 
     
    96101        self.current_datainfo = None 
    97102        self.current_dataset = None 
     103        self.filepath = None 
    98104        self.output = [] 
    99105 
  • src/sas/sascalc/dataloader/readers/cansas_reader.py

    r61f329f0 r2469df7  
    8282 
    8383    def read(self, xml_file, schema_path="", invalid=True): 
    84         if schema_path != "" or invalid != True: 
     84        if schema_path != "" or not invalid: 
    8585            # read has been called from self.get_file_contents because xml file doens't conform to schema 
    8686            _, self.extension = os.path.splitext(os.path.basename(xml_file)) 
     
    942942            pos, "z", datainfo.sample.position.z, 
    943943            {"unit": datainfo.sample.position_unit}) 
    944         if written == True: 
     944        if written: 
    945945            self.append(pos, sample) 
    946946 
     
    955955            ori, "yaw", datainfo.sample.orientation.z, 
    956956            {"unit": datainfo.sample.orientation_unit}) 
    957         if written == True: 
     957        if written: 
    958958            self.append(ori, sample) 
    959959 
     
    10021002            size, "z", datainfo.source.beam_size.z, 
    10031003            {"unit": datainfo.source.beam_size_unit}) 
    1004         if written == True: 
     1004        if written: 
    10051005            self.append(size, source) 
    10061006 
     
    10581058                    size, "z", aperture.size.z, 
    10591059                    {"unit": aperture.size_unit}) 
    1060                 if written == True: 
     1060                if written: 
    10611061                    self.append(size, apert) 
    10621062 
     
    10811081            written = written | self.write_node(det, "SDD", item.distance, 
    10821082                                                {"unit": item.distance_unit}) 
    1083             if written == True: 
     1083            if written: 
    10841084                self.append(det, instr) 
    10851085 
     
    10911091            written = written | self.write_node(off, "z", item.offset.z, 
    10921092                                                {"unit": item.offset_unit}) 
    1093             if written == True: 
     1093            if written: 
    10941094                self.append(off, det) 
    10951095 
     
    11031103                                                item.orientation.z, 
    11041104                                                {"unit": item.orientation_unit}) 
    1105             if written == True: 
     1105            if written: 
    11061106                self.append(ori, det) 
    11071107 
     
    11151115                                                item.beam_center.z, 
    11161116                                                {"unit": item.beam_center_unit}) 
    1117             if written == True: 
     1117            if written: 
    11181118                self.append(center, det) 
    11191119 
     
    11251125            written = written | self.write_node(pix, "z", item.pixel_size.z, 
    11261126                                                {"unit": item.pixel_size_unit}) 
    1127             if written == True: 
     1127            if written: 
    11281128                self.append(pix, det) 
    11291129            self.write_node(det, "slit_length", item.slit_length, 
  • src/sas/sascalc/dataloader/readers/danse_reader.py

    raf3e9f5 r2469df7  
    157157        # Store all data 
    158158        # Store wavelength 
    159         if has_converter == True and self.current_datainfo.source.wavelength_unit != 'A': 
     159        if has_converter and self.current_datainfo.source.wavelength_unit != 'A': 
    160160            conv = Converter('A') 
    161161            wavelength = conv(wavelength, 
     
    164164 
    165165        # Store distance 
    166         if has_converter == True and detector.distance_unit != 'm': 
     166        if has_converter and detector.distance_unit != 'm': 
    167167            conv = Converter('m') 
    168168            distance = conv(distance, units=detector.distance_unit) 
     
    170170 
    171171        # Store pixel size 
    172         if has_converter == True and detector.pixel_size_unit != 'mm': 
     172        if has_converter and detector.pixel_size_unit != 'mm': 
    173173            conv = Converter('mm') 
    174174            pixel = conv(pixel, units=detector.pixel_size_unit) 
  • src/sas/sascalc/dataloader/readers/sesans_reader.py

    r849094a r3053a4a  
    1212from ..file_reader_base_class import FileReader 
    1313from ..data_info import plottable_1D, DataInfo 
    14 from ..loader_exceptions import FileContentsException, DataReaderException 
     14from ..loader_exceptions import FileContentsException 
    1515 
    1616# Check whether we have a converter available 
     
    1818try: 
    1919    from sas.sascalc.data_util.nxsunit import Converter 
    20 except: 
     20except ImportError: 
    2121    has_converter = False 
    2222_ZERO = 1e-16 
     
    4646        line = self.nextline() 
    4747        params = {} 
    48         while not line.startswith("BEGIN_DATA"): 
     48        while line and not line.startswith("BEGIN_DATA"): 
    4949            terms = line.split() 
    5050            if len(terms) >= 2: 
     
    6363            raise FileContentsException("Wavelength has no units") 
    6464        if params["SpinEchoLength_unit"] != params["Wavelength_unit"]: 
    65             raise FileContentsException("The spin echo data has rudely used " 
    66                                "different units for the spin echo length " 
    67                                "and the wavelength.  While sasview could " 
    68                                "handle this instance, it is a violation " 
    69                                "of the file format and will not be " 
    70                                "handled by other software.") 
     65            raise FileContentsException( 
     66                "The spin echo data has rudely used " 
     67                "different units for the spin echo length " 
     68                "and the wavelength.  While sasview could " 
     69                "handle this instance, it is a violation " 
     70                "of the file format and will not be " 
     71                "handled by other software.") 
    7172 
    7273        headers = self.nextline().split() 
     
    8687 
    8788        if not data.size: 
    88             raise FileContentsException("{} is empty".format(path)) 
     89            raise FileContentsException("{} is empty".format(self.filepath)) 
    8990        x = data[:, headers.index("SpinEchoLength")] 
    9091        if "SpinEchoLength_error" in headers: 
  • src/sas/sascalc/fit/MultiplicationModel.py

    r574adc7 r0a9cbc3  
    6868        try: 
    6969            multiplicity = p_model.multiplicity 
    70         except: 
     70        except AttributeError: 
    7171            multiplicity = 1 
    7272        ## functional multiplicity of the model 
     
    7676        self.non_fittable = p_model.non_fittable 
    7777        self.multiplicity_info = [] 
    78         self.fun_list = {} 
     78        self.fun_list = [] 
    7979        if self.non_fittable > 1: 
    8080            try: 
     
    8282                self.fun_list = p_model.fun_list 
    8383                self.is_multiplicity_model = True 
    84             except: 
     84            except AttributeError: 
    8585                pass 
    8686        else: 
  • src/sas/sascalc/fit/qsmearing.py

    r50fcb09 r2469df7  
    9090            #print "data1D.dx[0]",data1D.dx[0],data1D.dxl[0] 
    9191    # If we found resolution smearing data, return a QSmearer 
    92     if _found_resolution == True: 
     92    if _found_resolution: 
    9393         return pinhole_smear(data, model) 
    9494 
     
    113113                break 
    114114    # If we found slit smearing data, return a slit smearer 
    115     if _found_slit == True: 
     115    if _found_slit: 
    116116        return slit_smear(data, model) 
    117117    return None 
  • src/sas/sascalc/pr/invertor.py

    rd04ac05 r2469df7  
    222222        elif name == 'est_bck': 
    223223            value = self.get_est_bck() 
    224             if value == 1: 
    225                 return True 
    226             else: 
    227                 return False 
     224            return value == 1 
    228225        elif name in self.__dict__: 
    229226            return self.__dict__[name] 
     
    460457 
    461458        # If we need to fit the background, add a term 
    462         if self.est_bck == True: 
     459        if self.est_bck: 
    463460            nfunc_0 = nfunc 
    464461            nfunc += 1 
     
    506503 
    507504        # Keep a copy of the last output 
    508         if self.est_bck == False: 
     505        if not self.est_bck: 
    509506            self.out = c 
    510507            self.cov = err 
     
    658655        file.write("#slit_width=%g\n" % self.slit_width) 
    659656        file.write("#background=%g\n" % self.background) 
    660         if self.est_bck == True: 
     657        if self.est_bck: 
    661658            file.write("#has_bck=1\n") 
    662659        else: 
     
    738735                    elif line.startswith('#has_bck='): 
    739736                        toks = line.split('=') 
    740                         if int(toks[1]) == 1: 
    741                             self.est_bck = True 
    742                         else: 
    743                             self.est_bck = False 
     737                        self.est_bck = int(toks[1]) == 1 
    744738 
    745739                    # Now read in the parameters 
  • src/sas/sascalc/pr/num_term.py

    ra1b8fee r2469df7  
    5555        medi = 0 
    5656        for i in range(dv): 
    57             if odd == True: 
     57            if odd: 
    5858                medi = osc[int(med)] 
    5959            else: 
     
    9898                new_osc3.append(self.osc_list[i]) 
    9999 
    100         if flag9 == True: 
     100        if flag9: 
    101101            self.dataset = new_osc1 
    102         elif flag8 == True: 
     102        elif flag8: 
    103103            self.dataset = new_osc2 
    104104        else: 
     
    141141            div = len(nts) 
    142142            tem = float(div) / 2.0 
    143             odd = self.is_odd(div) 
    144             if odd == True: 
     143            if self.is_odd(div): 
    145144                nt = nts[int(tem)] 
    146145            else: 
     
    148147            return nt, self.alpha_list[nt - 10], self.mess_list[nt - 10] 
    149148        except: 
    150             #TODO: check the logic above and make sure it doesn't  
     149            #TODO: check the logic above and make sure it doesn't 
    151150            # rely on the try-except. 
    152151            return self.nterm_min, self.invertor.alpha, '' 
Note: See TracChangeset for help on using the changeset viewer.