Changeset 8dc02d8b in sasview for sansmodels/src/sans/models


Ignore:
Timestamp:
Jan 20, 2011 4:10:08 PM (14 years ago)
Author:
Jae Cho <jhjcho@…>
Branches:
master, ESS_GUI, ESS_GUI_Docs, ESS_GUI_batch_fitting, ESS_GUI_bumps_abstraction, ESS_GUI_iss1116, ESS_GUI_iss879, ESS_GUI_iss959, ESS_GUI_opencl, ESS_GUI_ordering, ESS_GUI_sync_sascalc, costrafo411, magnetic_scatt, release-4.1.1, release-4.1.2, release-4.2.2, release_4.0.1, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1243, ticket-1249, ticket885, unittest-saveload
Children:
75df58b
Parents:
943cacb
Message:

added rectangular function for poly-dispersion

Location:
sansmodels/src/sans/models
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • sansmodels/src/sans/models/c_models/c_models.cpp

    r96656e3 r8dc02d8b  
    124124} 
    125125 
     126 
     127/** 
     128 * Delete a rectangle dispersion model object 
     129 */ 
     130void del_rectangle_dispersion(void *ptr){ 
     131        RectangleDispersion * disp = static_cast<RectangleDispersion *>(ptr); 
     132        delete disp; 
     133        return; 
     134} 
     135 
     136/** 
     137 * Create a rectangle dispersion model as a python object 
     138 */ 
     139PyObject * new_rectangle_dispersion(PyObject *, PyObject *args) { 
     140        RectangleDispersion *disp = new RectangleDispersion(); 
     141        return PyCObject_FromVoidPtr(disp, del_rectangle_dispersion); 
     142} 
     143 
     144 
    126145/** 
    127146 * Delete a schulz dispersion model object 
     
    203222        {"new_gaussian_model",   (PyCFunction)new_gaussian_dispersion, METH_VARARGS, 
    204223                  "Create a new GaussianDispersion object"}, 
     224        {"new_rectangle_model",   (PyCFunction)new_rectangle_dispersion, METH_VARARGS, 
     225                  "Create a new RectangleDispersion object"}, 
    205226    {"new_lognormal_model",   (PyCFunction)new_lognormal_dispersion, METH_VARARGS, 
    206227                  "Create a new LogNormalDispersion object"}, 
  • sansmodels/src/sans/models/c_models/dispersion_visitor.cpp

    r1d78e4b r8dc02d8b  
    2525 
    2626        PyDict_SetItemString(dict, "type",  Py_BuildValue("s", "gaussian")); 
     27    PyDict_SetItemString(dict, "npts",  Py_BuildValue("i", disp->npts)); 
     28    PyDict_SetItemString(dict, "width", Py_BuildValue("d", disp->width)); 
     29    PyDict_SetItemString(dict, "nsigmas", Py_BuildValue("d", disp->nsigmas)); 
     30#endif 
     31} 
     32 
     33void DispersionVisitor:: rectangle_to_dict(void* dispersion, void* dictionary) { 
     34#ifndef __MODELS_STANDALONE__ 
     35        RectangleDispersion * disp = (RectangleDispersion*)dispersion; 
     36        PyObject * dict = (PyObject*)dictionary; 
     37 
     38        PyDict_SetItemString(dict, "type",  Py_BuildValue("s", "rectangle")); 
    2739    PyDict_SetItemString(dict, "npts",  Py_BuildValue("i", disp->npts)); 
    2840    PyDict_SetItemString(dict, "width", Py_BuildValue("d", disp->width)); 
     
    8698} 
    8799 
     100void DispersionVisitor:: rectangle_from_dict(void* dispersion, void* dictionary) { 
     101#ifndef __MODELS_STANDALONE__ 
     102        RectangleDispersion * disp = (RectangleDispersion*)dispersion; 
     103        PyObject * dict = (PyObject*)dictionary; 
     104 
     105        disp->npts    = PyInt_AsLong( PyDict_GetItemString(dict, "npts") ); 
     106        disp->width   = PyFloat_AsDouble( PyDict_GetItemString(dict, "width") ); 
     107        disp->nsigmas = PyFloat_AsDouble( PyDict_GetItemString(dict, "nsigmas") ); 
     108#endif 
     109} 
     110 
    88111void DispersionVisitor:: lognormal_from_dict(void* dispersion, void* dictionary) { 
    89112#ifndef __MODELS_STANDALONE__ 
  • sansmodels/src/sans/models/c_models/dispersion_visitor.hh

    reba9885 r8dc02d8b  
    1919        void dispersion_to_dict(void *, void *); 
    2020        void gaussian_to_dict(void *, void *); 
     21        void rectangle_to_dict(void *, void *); 
    2122        void lognormal_to_dict(void *, void *); 
    2223        void schulz_to_dict(void*, void *); 
     
    2526        void dispersion_from_dict(void*, void *); 
    2627        void gaussian_from_dict(void*, void *); 
     28        void rectangle_from_dict(void*, void *); 
    2729        void lognormal_from_dict(void*, void *); 
    2830        void schulz_from_dict(void*, void *); 
  • sansmodels/src/sans/models/c_models/parameters.cpp

    r1d78e4b r8dc02d8b  
    9898        double vary, expo_value; 
    9999    vary = x-mean; 
    100     expo_value = -vary*vary/(2*sigma*sigma); 
     100    expo_value = -vary*vary/(2.0*sigma*sigma); 
    101101    //return 1.0; 
    102102    return exp(expo_value); 
     
    130130                          && ((*par).has_max==false || val<(*par).max)  ) { 
    131131                                double _w = gaussian_weight(value, width, val); 
     132                                weights.insert(weights.end(), WeightPoint(val, _w)); 
     133                        } 
     134                } 
     135        } 
     136} 
     137 
     138 
     139/** 
     140 * Flat dispersion 
     141 */ 
     142 
     143RectangleDispersion :: RectangleDispersion() { 
     144        npts  = 21; 
     145        width = 0.0; 
     146        nsigmas = 1.0; 
     147}; 
     148 
     149void RectangleDispersion :: accept_as_source(DispersionVisitor* visitor, void* from, void* to) { 
     150        visitor->rectangle_to_dict(from, to); 
     151} 
     152void RectangleDispersion :: accept_as_destination(DispersionVisitor* visitor, void* from, void* to) { 
     153        visitor->rectangle_from_dict(from, to); 
     154} 
     155 
     156double rectangle_weight(double mean, double sigma, double x) { 
     157        double vary, expo_value; 
     158    double sig = fabs(sigma); 
     159    if (x>= (mean-sig) && x<(mean+sig)){ 
     160        return 1.0; 
     161    } 
     162    else{ 
     163        return 0.0; 
     164    } 
     165} 
     166 
     167/** 
     168 * Flat dispersion 
     169 * @param mean: mean value 
     170 * @param sigma: half width of top hat function 
     171 * @param x: value at which the Flat is evaluated 
     172 * @return: value of the Flat 
     173 */ 
     174void RectangleDispersion :: operator() (void *param, vector<WeightPoint> &weights){ 
     175        // Check against zero width 
     176        if (width<=0) { 
     177                width = 0.0; 
     178                npts  = 1; 
     179                nsigmas = 1.0; 
     180        } 
     181 
     182        Parameter* par = (Parameter*)param; 
     183        double value = (*par)(); 
     184 
     185        if (npts<2) { 
     186                weights.insert(weights.end(), WeightPoint(value, 1.0)); 
     187        } else { 
     188                for(int i=0; i<npts; i++) { 
     189                        // We cover n(nsigmas) times sigmas on each side of the mean 
     190                        double val = value + width * (2.0*nsigmas*double(i)/double(npts-1) - nsigmas); 
     191                        if ( ((*par).has_min==false || val>(*par).min) 
     192                          && ((*par).has_max==false || val<(*par).max)  ) { 
     193                                double _w = rectangle_weight(value, width, val); 
    132194                                weights.insert(weights.end(), WeightPoint(val, _w)); 
    133195                        } 
  • sansmodels/src/sans/models/c_models/parameters.hh

    r1d78e4b r8dc02d8b  
    7171 
    7272        GaussianDispersion(); 
     73        void operator()(void *, vector<WeightPoint>&); 
     74        void accept_as_source(DispersionVisitor*, void*, void*); 
     75        void accept_as_destination(DispersionVisitor*, void*, void*); 
     76}; 
     77 
     78/** 
     79 * Flat dispersion model 
     80 */ 
     81class RectangleDispersion: public DispersionModel { 
     82public: 
     83        /// Number of sigmas on each side of the mean 
     84        double nsigmas; 
     85 
     86        RectangleDispersion(); 
    7387        void operator()(void *, vector<WeightPoint>&); 
    7488        void accept_as_source(DispersionVisitor*, void*, void*); 
  • sansmodels/src/sans/models/dispersion_models.py

    r79ac6f8 r8dc02d8b  
    7878        message += "  Solution: Use an ArrayDispersion object" 
    7979        raise "RuntimeError", message 
     80         
     81class RectangleDispersion(DispersionModel): 
     82    """ 
     83    Python bridge class for a dispersion model based  
     84    on a Gaussian distribution. 
     85    """ 
     86    def __init__(self): 
     87        self.cdisp = c_models.new_rectangle_model() 
     88         
     89    def set_weights(self, values, weights): 
     90        """ 
     91            Set the weights of an array dispersion 
     92        """ 
     93        message = "set_weights is not available for GaussianDispersion.\n" 
     94        message += "  Solution: Use an ArrayDispersion object" 
     95        raise "RuntimeError", message  
    8096     
    8197class SchulzDispersion(DispersionModel): 
     
    133149        """ 
    134150        if len(values) != len(weights): 
    135             raise ValueError, "ArrayDispersion.set_weights: given arrays are of different lengths" 
     151            raise ValueError, "ArrayDispersion.set_weights: \ 
     152            given arrays are of different lengths" 
    136153         
    137154        c_models.set_dispersion_weights(self.cdisp, values, weights) 
    138   
    139 models = {"gaussian":GaussianDispersion, "array":ArrayDispersion, 
    140           "schulz":SchulzDispersion, "lognormal":LogNormalDispersion}        
    141155         
     156models = {"gaussian":GaussianDispersion,  "rectangula":RectangleDispersion, 
     157          "array":ArrayDispersion, "schulz":SchulzDispersion,  
     158          "lognormal":LogNormalDispersion}        
     159         
Note: See TracChangeset for help on using the changeset viewer.