Changeset eba9885 in sasview for sansmodels/src/sans/models/c_models
- Timestamp:
- Aug 5, 2009 5:39:03 PM (16 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:
- f88624d
- Parents:
- 8344c50
- Location:
- sansmodels/src/sans/models/c_models
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
sansmodels/src/sans/models/c_models/c_models.cpp
r812b901 reba9885 48 48 void addCGaussian(PyObject *module); 49 49 void addCLorentzian(PyObject *module); 50 void addCLogNormal(PyObject *module); 51 void addCSchulz(PyObject *module); 50 52 } 51 53 … … 69 71 70 72 /** 73 * Delete a lognormal dispersion model object 74 */ 75 void del_lognormal_dispersion(void *ptr){ 76 LogNormalDispersion * disp = static_cast<LogNormalDispersion *>(ptr); 77 delete disp; 78 return; 79 } 80 81 /** 82 * Create a lognormal dispersion model as a python object 83 */ 84 PyObject * new_lognormal_dispersion(PyObject *, PyObject *args) { 85 LogNormalDispersion *disp = new LogNormalDispersion(); 86 return PyCObject_FromVoidPtr(disp, del_lognormal_dispersion); 87 } 88 89 /** 71 90 * Delete a gaussian dispersion model object 72 91 */ … … 84 103 return PyCObject_FromVoidPtr(disp, del_gaussian_dispersion); 85 104 } 105 106 /** 107 * Delete a schulz dispersion model object 108 */ 109 void del_schulz_dispersion(void *ptr){ 110 SchulzDispersion * disp = static_cast<SchulzDispersion *>(ptr); 111 delete disp; 112 return; 113 } 114 /** 115 * Create a schulz dispersion model as a python object 116 */ 117 PyObject * new_schulz_dispersion(PyObject *, PyObject *args) { 118 SchulzDispersion *disp = new SchulzDispersion(); 119 return PyCObject_FromVoidPtr(disp, del_schulz_dispersion); 120 } 121 86 122 87 123 /** … … 147 183 {"new_gaussian_model", (PyCFunction)new_gaussian_dispersion, METH_VARARGS, 148 184 "Create a new GaussianDispersion object"}, 185 {"new_lognormal_model", (PyCFunction)new_lognormal_dispersion, METH_VARARGS, 186 "Create a new LogNormalDispersion object"}, 187 {"new_schulz_model", (PyCFunction)new_schulz_dispersion, METH_VARARGS, 188 "Create a new SchulzDispersion object"}, 149 189 {"new_array_model", (PyCFunction)new_array_dispersion , METH_VARARGS, 150 190 "Create a new ArrayDispersion object"}, … … 194 234 addDisperser(m); 195 235 addCGaussian(m); 236 addCSchulz(m); 237 addCLogNormal(m); 196 238 addCLorentzian(m); 197 239 addCVesicleModel(m); 198 240 199 200 } 241 } -
sansmodels/src/sans/models/c_models/dispersion_visitor.cpp
rfca6936 reba9885 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("i", disp->nsigmas)); 30 #endif 31 } 32 33 void DispersionVisitor:: lognormal_to_dict(void* dispersion, void* dictionary) { 34 #ifndef __MODELS_STANDALONE__ 35 LogNormalDispersion * disp = (LogNormalDispersion*)dispersion; 36 PyObject * dict = (PyObject*)dictionary; 37 38 PyDict_SetItemString(dict, "type", Py_BuildValue("s", "lognormal")); 39 PyDict_SetItemString(dict, "npts", Py_BuildValue("i", disp->npts)); 40 PyDict_SetItemString(dict, "width", Py_BuildValue("d", disp->width)); 41 PyDict_SetItemString(dict, "nsigmas", Py_BuildValue("i", disp->nsigmas)); 42 #endif 43 } 44 45 46 void DispersionVisitor:: schulz_to_dict(void* dispersion, void* dictionary) { 47 #ifndef __MODELS_STANDALONE__ 48 SchulzDispersion * disp = (SchulzDispersion*)dispersion; 49 PyObject * dict = (PyObject*)dictionary; 50 51 PyDict_SetItemString(dict, "type", Py_BuildValue("s", "schulz")); 27 52 PyDict_SetItemString(dict, "npts", Py_BuildValue("i", disp->npts)); 28 53 PyDict_SetItemString(dict, "width", Py_BuildValue("d", disp->width)); … … 61 86 } 62 87 88 void DispersionVisitor:: lognormal_from_dict(void* dispersion, void* dictionary) { 89 #ifndef __MODELS_STANDALONE__ 90 LogNormalDispersion * disp = (LogNormalDispersion*)dispersion; 91 PyObject * dict = (PyObject*)dictionary; 92 93 disp->npts = PyInt_AsLong( PyDict_GetItemString(dict, "npts") ); 94 disp->width = PyFloat_AsDouble( PyDict_GetItemString(dict, "width") ); 95 disp->nsigmas = PyFloat_AsDouble( PyDict_GetItemString(dict, "nsigmas") ); 96 #endif 97 } 98 void DispersionVisitor:: schulz_from_dict(void* dispersion, void* dictionary) { 99 #ifndef __MODELS_STANDALONE__ 100 SchulzDispersion * disp = (SchulzDispersion*)dispersion; 101 PyObject * dict = (PyObject*)dictionary; 102 103 disp->npts = PyInt_AsLong( PyDict_GetItemString(dict, "npts") ); 104 disp->width = PyFloat_AsDouble( PyDict_GetItemString(dict, "width") ); 105 disp->nsigmas = PyFloat_AsDouble( PyDict_GetItemString(dict, "nsigmas") ); 106 #endif 107 } 108 63 109 void DispersionVisitor:: array_from_dict(void* dispersion, void* dictionary) {} -
sansmodels/src/sans/models/c_models/dispersion_visitor.hh
rfca6936 reba9885 19 19 void dispersion_to_dict(void *, void *); 20 20 void gaussian_to_dict(void *, void *); 21 void lognormal_to_dict(void *, void *); 22 void schulz_to_dict(void*, void *); 21 23 void array_to_dict(void *, void *); 22 24 23 25 void dispersion_from_dict(void*, void *); 24 26 void gaussian_from_dict(void*, void *); 27 void lognormal_from_dict(void*, void *); 28 void schulz_from_dict(void*, void *); 25 29 void array_from_dict(void*, void *); 26 30 -
sansmodels/src/sans/models/c_models/parameters.cpp
r07da749 reba9885 136 136 } 137 137 } 138 139 140 /** 141 * LogNormal dispersion 142 */ 143 144 LogNormalDispersion :: LogNormalDispersion() { 145 npts = 1; 146 width = 0.0; 147 nsigmas = 2; 148 }; 149 150 void LogNormalDispersion :: accept_as_source(DispersionVisitor* visitor, void* from, void* to) { 151 visitor->lognormal_to_dict(from, to); 152 } 153 void LogNormalDispersion :: accept_as_destination(DispersionVisitor* visitor, void* from, void* to) { 154 visitor->lognormal_from_dict(from, to); 155 } 156 157 double lognormal_weight(double mean, double sigma, double x) { 158 159 double sigma2 = pow(sigma, 2); 160 return 1/(x*sigma2) * exp( -pow((log(x) -mean), 2) / (2*sigma2)); 161 162 } 163 164 /** 165 * Lognormal dispersion 166 * @param mean: mean value of the LogNormal 167 * @param sigma: standard deviation of the LogNormal 168 * @param x: value at which the LogNormal is evaluated 169 * @return: value of the LogNormal 170 */ 171 void LogNormalDispersion :: operator() (void *param, vector<WeightPoint> &weights){ 172 // Check against zero width 173 if (width<=0) { 174 width = 0.0; 175 npts = 1; 176 nsigmas = 3; 177 } 178 179 Parameter* par = (Parameter*)param; 180 double value = (*par)(); 181 182 if (npts<2) { 183 weights.insert(weights.end(), WeightPoint(value, 1.0)); 184 } else { 185 for(int i=0; i<npts; i++) { 186 // We cover n(nsigmas) times sigmas on each side of the mean 187 double val = value + width * (2.0*nsigmas*i/float(npts-1) - nsigmas); 188 189 if ( ((*par).has_min==false || val>(*par).min) 190 && ((*par).has_max==false || val<(*par).max) ) { 191 double _w = lognormal_weight(value, width, val); 192 weights.insert(weights.end(), WeightPoint(val, _w)); 193 } 194 } 195 } 196 } 197 198 199 200 /** 201 * Schulz dispersion 202 */ 203 204 SchulzDispersion :: SchulzDispersion() { 205 npts = 1; 206 width = 0.0; 207 nsigmas = 2; 208 }; 209 210 void SchulzDispersion :: accept_as_source(DispersionVisitor* visitor, void* from, void* to) { 211 visitor->schulz_to_dict(from, to); 212 } 213 void SchulzDispersion :: accept_as_destination(DispersionVisitor* visitor, void* from, void* to) { 214 visitor->schulz_from_dict(from, to); 215 } 216 217 double schulz_weight(double mean, double sigma, double x) { 218 double vary, expo_value; 219 double z = pow(mean/ sigma, 2)-1; 220 double R= x/mean; 221 double zz= z+1; 222 return pow(zz,zz) * pow(R,z) * exp(-1*R*zz)/((mean) * tgamma(zz)) ; 223 } 224 225 /** 226 * Schulz dispersion 227 * @param mean: mean value of the Schulz 228 * @param sigma: standard deviation of the Schulz 229 * @param x: value at which the Schulz is evaluated 230 * @return: value of the Schulz 231 */ 232 void SchulzDispersion :: operator() (void *param, vector<WeightPoint> &weights){ 233 // Check against zero width 234 if (width<=0) { 235 width = 0.0; 236 npts = 1; 237 nsigmas = 3; 238 } 239 240 Parameter* par = (Parameter*)param; 241 double value = (*par)(); 242 243 if (npts<2) { 244 weights.insert(weights.end(), WeightPoint(value, 1.0)); 245 } else { 246 for(int i=0; i<npts; i++) { 247 // We cover n(nsigmas) times sigmas on each side of the mean 248 double val = value + width * (2.0*nsigmas*i/float(npts-1) - nsigmas); 249 250 if ( ((*par).has_min==false || val>(*par).min) 251 && ((*par).has_max==false || val<(*par).max) ) { 252 double _w = schulz_weight(value, width, val); 253 weights.insert(weights.end(), WeightPoint(val, _w)); 254 } 255 } 256 } 257 } 258 259 260 138 261 139 262 /** -
sansmodels/src/sans/models/c_models/parameters.hh
rfca6936 reba9885 77 77 78 78 /** 79 * Schulz dispersion model 80 */ 81 class SchulzDispersion: public DispersionModel { 82 public: 83 /// Number of sigmas on each side of the mean 84 int nsigmas; 85 86 SchulzDispersion(); 87 void operator()(void *, vector<WeightPoint>&); 88 void accept_as_source(DispersionVisitor*, void*, void*); 89 void accept_as_destination(DispersionVisitor*, void*, void*); 90 }; 91 92 /** 93 * LogNormal dispersion model 94 */ 95 class LogNormalDispersion: public DispersionModel { 96 public: 97 /// Number of sigmas on each side of the mean 98 int nsigmas; 99 100 LogNormalDispersion(); 101 void operator()(void *, vector<WeightPoint>&); 102 void accept_as_source(DispersionVisitor*, void*, void*); 103 void accept_as_destination(DispersionVisitor*, void*, void*); 104 }; 105 106 107 /** 79 108 * Dispersion model based on arrays provided by the user 80 109 */
Note: See TracChangeset
for help on using the changeset viewer.