Changeset 8dc02d8b in sasview for sansmodels/src/sans/models
- Timestamp:
- Jan 20, 2011 4:10:08 PM (14 years ago)
- 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
- Location:
- sansmodels/src/sans/models
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
sansmodels/src/sans/models/c_models/c_models.cpp
r96656e3 r8dc02d8b 124 124 } 125 125 126 127 /** 128 * Delete a rectangle dispersion model object 129 */ 130 void 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 */ 139 PyObject * new_rectangle_dispersion(PyObject *, PyObject *args) { 140 RectangleDispersion *disp = new RectangleDispersion(); 141 return PyCObject_FromVoidPtr(disp, del_rectangle_dispersion); 142 } 143 144 126 145 /** 127 146 * Delete a schulz dispersion model object … … 203 222 {"new_gaussian_model", (PyCFunction)new_gaussian_dispersion, METH_VARARGS, 204 223 "Create a new GaussianDispersion object"}, 224 {"new_rectangle_model", (PyCFunction)new_rectangle_dispersion, METH_VARARGS, 225 "Create a new RectangleDispersion object"}, 205 226 {"new_lognormal_model", (PyCFunction)new_lognormal_dispersion, METH_VARARGS, 206 227 "Create a new LogNormalDispersion object"}, -
sansmodels/src/sans/models/c_models/dispersion_visitor.cpp
r1d78e4b r8dc02d8b 25 25 26 26 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 33 void 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")); 27 39 PyDict_SetItemString(dict, "npts", Py_BuildValue("i", disp->npts)); 28 40 PyDict_SetItemString(dict, "width", Py_BuildValue("d", disp->width)); … … 86 98 } 87 99 100 void 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 88 111 void DispersionVisitor:: lognormal_from_dict(void* dispersion, void* dictionary) { 89 112 #ifndef __MODELS_STANDALONE__ -
sansmodels/src/sans/models/c_models/dispersion_visitor.hh
reba9885 r8dc02d8b 19 19 void dispersion_to_dict(void *, void *); 20 20 void gaussian_to_dict(void *, void *); 21 void rectangle_to_dict(void *, void *); 21 22 void lognormal_to_dict(void *, void *); 22 23 void schulz_to_dict(void*, void *); … … 25 26 void dispersion_from_dict(void*, void *); 26 27 void gaussian_from_dict(void*, void *); 28 void rectangle_from_dict(void*, void *); 27 29 void lognormal_from_dict(void*, void *); 28 30 void schulz_from_dict(void*, void *); -
sansmodels/src/sans/models/c_models/parameters.cpp
r1d78e4b r8dc02d8b 98 98 double vary, expo_value; 99 99 vary = x-mean; 100 expo_value = -vary*vary/(2 *sigma*sigma);100 expo_value = -vary*vary/(2.0*sigma*sigma); 101 101 //return 1.0; 102 102 return exp(expo_value); … … 130 130 && ((*par).has_max==false || val<(*par).max) ) { 131 131 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 143 RectangleDispersion :: RectangleDispersion() { 144 npts = 21; 145 width = 0.0; 146 nsigmas = 1.0; 147 }; 148 149 void RectangleDispersion :: accept_as_source(DispersionVisitor* visitor, void* from, void* to) { 150 visitor->rectangle_to_dict(from, to); 151 } 152 void RectangleDispersion :: accept_as_destination(DispersionVisitor* visitor, void* from, void* to) { 153 visitor->rectangle_from_dict(from, to); 154 } 155 156 double 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 */ 174 void 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); 132 194 weights.insert(weights.end(), WeightPoint(val, _w)); 133 195 } -
sansmodels/src/sans/models/c_models/parameters.hh
r1d78e4b r8dc02d8b 71 71 72 72 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 */ 81 class RectangleDispersion: public DispersionModel { 82 public: 83 /// Number of sigmas on each side of the mean 84 double nsigmas; 85 86 RectangleDispersion(); 73 87 void operator()(void *, vector<WeightPoint>&); 74 88 void accept_as_source(DispersionVisitor*, void*, void*); -
sansmodels/src/sans/models/dispersion_models.py
r79ac6f8 r8dc02d8b 78 78 message += " Solution: Use an ArrayDispersion object" 79 79 raise "RuntimeError", message 80 81 class 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 80 96 81 97 class SchulzDispersion(DispersionModel): … … 133 149 """ 134 150 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" 136 153 137 154 c_models.set_dispersion_weights(self.cdisp, values, weights) 138 139 models = {"gaussian":GaussianDispersion, "array":ArrayDispersion,140 "schulz":SchulzDispersion, "lognormal":LogNormalDispersion}141 155 156 models = {"gaussian":GaussianDispersion, "rectangula":RectangleDispersion, 157 "array":ArrayDispersion, "schulz":SchulzDispersion, 158 "lognormal":LogNormalDispersion} 159
Note: See TracChangeset
for help on using the changeset viewer.