Changeset 812b901 in sasview for sansmodels/src/sans/models/c_models/CCylinderModel.cpp
- Timestamp:
- Jul 27, 2009 3:56:14 PM (15 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:
- 4176435
- Parents:
- 6f2c919
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sansmodels/src/sans/models/c_models/CCylinderModel.cpp
r0f5bc9f r812b901 23 23 */ 24 24 25 #define NO_IMPORT_ARRAY 26 #define PY_ARRAY_UNIQUE_SYMBOL PyArray_API_sans 27 25 28 extern "C" { 26 29 #include <Python.h> 30 #include <arrayobject.h> 27 31 #include "structmember.h" 28 32 #include <stdio.h> … … 142 146 } 143 147 } 144 145 148 /** 149 * Function to call to evaluate model 150 * @param args: input numpy array q[] 151 * @return: numpy array object 152 */ 153 154 static PyObject *evaluateOneDim(CylinderModel* model, PyArrayObject *q){ 155 PyArrayObject *result; 156 157 // Check validity of array q , q must be of dimension 1, an array of double 158 if (q->nd != 1 || q->descr->type_num != PyArray_DOUBLE) 159 { 160 //const char * message= "Invalid array: q->nd=%d,type_num=%d\n",q->nd,q->descr->type_num; 161 //PyErr_SetString(PyExc_ValueError , message); 162 return NULL; 163 } 164 result = (PyArrayObject *)PyArray_FromDims(q->nd, (int *)(q->dimensions), 165 PyArray_DOUBLE); 166 if (result == NULL) { 167 const char * message= "Could not create result "; 168 PyErr_SetString(PyExc_RuntimeError , message); 169 return NULL; 170 } 171 for (int i = 0; i < q->dimensions[0]; i++){ 172 double q_value = *(double *)(q->data + i*q->strides[0]); 173 double *result_value = (double *)(result->data + i*result->strides[0]); 174 *result_value =(*model)(q_value); 175 } 176 return PyArray_Return(result); 177 } 178 /** 179 * Function to call to evaluate model 180 * @param args: input numpy array [q[],phi[]] 181 * @return: numpy array object 182 */ 183 static PyObject * evaluateTwoDim( CylinderModel* model, 184 PyArrayObject *q, PyArrayObject *phi) 185 { 186 PyArrayObject *result; 187 //check validity of input vectors 188 if (q->nd != 1 || q->descr->type_num != PyArray_DOUBLE 189 || phi->nd != 1 || phi->descr->type_num != PyArray_DOUBLE 190 || phi->dimensions[0] != q->dimensions[0]){ 191 192 //const char * message= "Invalid array: q->nd=%d,type_num=%d\n",q->nd,q->descr->type_num; 193 //PyErr_SetString(PyExc_ValueError , message); 194 return NULL; 195 } 196 result= (PyArrayObject *)PyArray_FromDims(q->nd,(int*)(q->dimensions), PyArray_DOUBLE); 197 198 if (result == NULL){ 199 const char * message= "Could not create result "; 200 PyErr_SetString(PyExc_RuntimeError , message); 201 return NULL; 202 } 203 204 for (int i = 0; i < q->dimensions[0]; i++) { 205 double q_value = *(double *)(q->data + i*q->strides[0]); 206 double phi_value = *(double *)(phi->data + i*phi->strides[0]); 207 double *result_value = (double *)(result->data + i*result->strides[0]); 208 if (q_value == 0) 209 *result_value = 0.0; 210 else 211 *result_value = model->evaluate_rphi(q_value, phi_value); 212 } 213 return PyArray_Return(result); 214 } 215 216 /** 217 * Function to call to evaluate model 218 * @param args: input numpy array [x[],y[]] 219 * @return: numpy array object 220 */ 221 static PyObject * evaluateTwoDimXY( CylinderModel* model, 222 PyArrayObject *x, PyArrayObject *y) 223 { 224 PyArrayObject *result; 225 //check validity of input vectors 226 if (x->nd != 1 || x->descr->type_num != PyArray_DOUBLE 227 || y->nd != 1 || y->descr->type_num != PyArray_DOUBLE 228 || y->dimensions[0] != x->dimensions[0]){ 229 230 //const char * message= "Invalid array: x->nd=%d,type_num=%d\n",x->nd,x->descr->type_num; 231 //PyErr_SetString(PyExc_ValueError , message); 232 return NULL; 233 } 234 result= (PyArrayObject *)PyArray_FromDims(x->nd,(int*)(x->dimensions), PyArray_DOUBLE); 235 236 if (result == NULL){ 237 const char * message= "Could not create result "; 238 PyErr_SetString(PyExc_RuntimeError , message); 239 return NULL; 240 } 241 242 for (int i = 0; i < x->dimensions[0]; i++) { 243 double x_value = *(double *)(x->data + i*x->strides[0]); 244 double y_value = *(double *)(y->data + i*y->strides[0]); 245 double *result_value = (double *)(result->data + i*result->strides[0]); 246 *result_value = (*model)(x_value, y_value); 247 } 248 return PyArray_Return(result); 249 } 250 146 251 /** 147 252 * Function to call to evaluate model … … 194 299 return NULL; 195 300 } 301 196 302 // We have a vector q, get the q and phi values at which 197 303 // to evaluate I(q,phi) 198 q_value = CCylinderModel_readDouble(PyList_GET_ITEM(pars,0)); 199 phi_value = CCylinderModel_readDouble(PyList_GET_ITEM(pars,1)); 200 // Skip zero 201 if (q_value==0) { 202 return Py_BuildValue("d",0.0); 203 } 204 return Py_BuildValue("d",(*(self->model)).evaluate_rphi(q_value,phi_value)); 205 304 PyObject *q, *phi; 305 q = PyList_GET_ITEM(pars,0); 306 phi = PyList_GET_ITEM(pars,1); 307 308 if (PyArray_Check(q) && PyArray_Check(phi)) { 309 return evaluateTwoDim(self->model, (PyArrayObject*)q, (PyArrayObject*)phi); 310 } else if (PyArray_Check(q) || PyArray_Check(phi)) { 311 return NULL; 312 } else { 313 q_value = CCylinderModel_readDouble(PyList_GET_ITEM(pars,0)); 314 phi_value = CCylinderModel_readDouble(PyList_GET_ITEM(pars,1)); 315 // Skip zero 316 if (q_value==0) { 317 return Py_BuildValue("d",0.0); 318 } 319 return Py_BuildValue("d",(*(self->model)).evaluate_rphi(q_value,phi_value)); 320 } 206 321 } else { 207 208 // We have a scalar q, we will evaluate I(q) 322 if (PyArray_Check(pars)) { 323 return evaluateOneDim(self->model, (PyArrayObject*)pars); 324 } else { 325 // We have a scalar q, we will evaluate I(q) 209 326 q_value = CCylinderModel_readDouble(pars); 210 211 return Py_BuildValue("d",(*(self->model))(q_value));212 } 327 return Py_BuildValue("d",(*(self->model))(q_value)); 328 } 329 } 213 330 } 214 331 … … 265 382 // We have a vector q, get the qx and qy values at which 266 383 // to evaluate I(qx,qy) 267 qx_value = CCylinderModel_readDouble(PyList_GET_ITEM(pars,0)); 268 qy_value = CCylinderModel_readDouble(PyList_GET_ITEM(pars,1)); 269 return Py_BuildValue("d",(*(self->model))(qx_value,qy_value)); 384 PyObject *x, *y; 385 x = PyList_GET_ITEM(pars,0); 386 y = PyList_GET_ITEM(pars,1); 387 388 if (PyArray_Check(x) && PyArray_Check(y)) { 389 390 return evaluateTwoDimXY(self->model, (PyArrayObject*)x, (PyArrayObject*)y); 391 } else if (PyArray_Check(x) || PyArray_Check(y)) { 392 return NULL; 393 } else { 394 qx_value = CCylinderModel_readDouble(PyList_GET_ITEM(pars,0)); 395 qy_value = CCylinderModel_readDouble(PyList_GET_ITEM(pars,1)); 396 return Py_BuildValue("d",(*(self->model))(qx_value,qy_value)); 397 } 270 398 271 399 } else { 272 273 // We have a scalar q, we will evaluate I(q) 274 qx_value = CCylinderModel_readDouble(pars); 400 if (PyArray_Check(pars)) { 401 return evaluateOneDim(self->model, (PyArrayObject*)pars); 402 } else { 403 // We have a scalar q, we will evaluate I(q) 404 qx_value = CCylinderModel_readDouble(pars); 275 405 276 return Py_BuildValue("d",(*(self->model))(qx_value)); 406 return Py_BuildValue("d",(*(self->model))(qx_value)); 407 } 277 408 } 278 409 } … … 376 507 377 508 378 static PyMethodDef module_methods[] = {379 {NULL}380 };381 382 509 /** 383 510 * Function used to add the model class to a module
Note: See TracChangeset
for help on using the changeset viewer.