Changeset a8d6888 in sasview for sansmodels/src/sans/models/c_models
- Timestamp:
- Aug 24, 2009 10:08:37 AM (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:
- df4702f
- Parents:
- 2339ff1e
- Location:
- sansmodels/src/sans/models/c_models
- Files:
-
- 32 edited
Legend:
- Unmodified
- Added
- Removed
-
sansmodels/src/sans/models/c_models/CBinaryHSModel.cpp
r975ec8e ra8d6888 18 18 * 19 19 * WARNING: THIS FILE WAS GENERATED BY WRAPPERGENERATOR.PY 20 * DO NOT MODIFY THIS FILE, MODIFY binaryH S.h20 * DO NOT MODIFY THIS FILE, MODIFY binaryHs.h 21 21 * AND RE-RUN THE GENERATOR SCRIPT 22 22 * … … 33 33 #include <math.h> 34 34 #include <time.h> 35 #include "binaryH S.h"35 #include "binaryHs.h" 36 36 } 37 37 … … 191 191 192 192 if (PyArray_Check(x) && PyArray_Check(y)) { 193 x_len = dims[0]= x->dimensions[0]; 194 y_len = dims[1]= y->dimensions[1]; 193 194 x_len = dims[1]= x->dimensions[1]; 195 y_len = dims[0]= y->dimensions[0]; 195 196 196 197 // Make a new double matrix of same dims … … 205 206 for ( i=0; i< x_len; i++) { 206 207 for ( j=0; j< y_len; j++) { 207 double x_value = *(double *)(x->data + i*x->strides[ 0]);208 double y_value = *(double *)(y->data + j*y->strides[ 1]);208 double x_value = *(double *)(x->data + i*x->strides[1]); 209 double y_value = *(double *)(y->data + j*y->strides[0]); 209 210 double *result_value = (double *)(result->data + 210 i*result->strides[ 0] + j*result->strides[1]);211 i*result->strides[1] + j*result->strides[0]); 211 212 *result_value = (*model)(x_value, y_value); 212 213 } -
sansmodels/src/sans/models/c_models/CBinaryHSPSF11Model.cpp
r9bd69098 ra8d6888 170 170 return PyArray_Return(result); 171 171 } 172 /** 173 * Function to call to evaluate model 174 * @param args: input numpy array [q[],phi[]] 175 * @return: numpy array object 176 */ 177 static PyObject * evaluateTwoDim( BinaryHSPSF11Model* model, 178 PyArrayObject *q, PyArrayObject *phi) 179 { 180 PyArrayObject *result; 181 //check validity of input vectors 182 if (q->nd != 1 || q->descr->type_num != PyArray_DOUBLE 183 || phi->nd != 1 || phi->descr->type_num != PyArray_DOUBLE 184 || phi->dimensions[0] != q->dimensions[0]){ 185 186 //const char * message= "Invalid array: q->nd=%d,type_num=%d\n",q->nd,q->descr->type_num; 187 PyErr_SetString(PyExc_ValueError ,"wrong input"); 188 return NULL; 189 } 190 result= (PyArrayObject *)PyArray_FromDims(q->nd,(int*)(q->dimensions), PyArray_DOUBLE); 191 192 if (result == NULL){ 193 const char * message= "Could not create result "; 194 PyErr_SetString(PyExc_RuntimeError , message); 195 return NULL; 196 } 197 198 for (int i = 0; i < q->dimensions[0]; i++) { 199 double q_value = *(double *)(q->data + i*q->strides[0]); 200 double phi_value = *(double *)(phi->data + i*phi->strides[0]); 201 double *result_value = (double *)(result->data + i*result->strides[0]); 202 if (q_value == 0) 203 *result_value = 0.0; 204 else 205 *result_value = model->evaluate_rphi(q_value, phi_value); 206 } 207 return PyArray_Return(result); 208 } 172 209 173 /** 210 174 * Function to call to evaluate model … … 227 191 228 192 if (PyArray_Check(x) && PyArray_Check(y)) { 229 x_len = dims[0]= x->dimensions[0]; 230 y_len = dims[1]= y->dimensions[1]; 193 194 x_len = dims[1]= x->dimensions[1]; 195 y_len = dims[0]= y->dimensions[0]; 231 196 232 197 // Make a new double matrix of same dims … … 241 206 for ( i=0; i< x_len; i++) { 242 207 for ( j=0; j< y_len; j++) { 243 double x_value = *(double *)(x->data + i*x->strides[ 0]);244 double y_value = *(double *)(y->data + j*y->strides[ 1]);208 double x_value = *(double *)(x->data + i*x->strides[1]); 209 double y_value = *(double *)(y->data + j*y->strides[0]); 245 210 double *result_value = (double *)(result->data + 246 i*result->strides[ 0] + j*result->strides[1]);211 i*result->strides[1] + j*result->strides[0]); 247 212 *result_value = (*model)(x_value, y_value); 248 213 } -
sansmodels/src/sans/models/c_models/CCoreShellCylinderModel.cpp
r9bd69098 ra8d6888 181 181 return PyArray_Return(result); 182 182 } 183 /** 184 * Function to call to evaluate model 185 * @param args: input numpy array [q[],phi[]] 186 * @return: numpy array object 187 */ 188 static PyObject * evaluateTwoDim( CoreShellCylinderModel* model, 189 PyArrayObject *q, PyArrayObject *phi) 190 { 191 PyArrayObject *result; 192 //check validity of input vectors 193 if (q->nd != 1 || q->descr->type_num != PyArray_DOUBLE 194 || phi->nd != 1 || phi->descr->type_num != PyArray_DOUBLE 195 || phi->dimensions[0] != q->dimensions[0]){ 196 197 //const char * message= "Invalid array: q->nd=%d,type_num=%d\n",q->nd,q->descr->type_num; 198 PyErr_SetString(PyExc_ValueError ,"wrong input"); 199 return NULL; 200 } 201 result= (PyArrayObject *)PyArray_FromDims(q->nd,(int*)(q->dimensions), PyArray_DOUBLE); 202 203 if (result == NULL){ 204 const char * message= "Could not create result "; 205 PyErr_SetString(PyExc_RuntimeError , message); 206 return NULL; 207 } 208 209 for (int i = 0; i < q->dimensions[0]; i++) { 210 double q_value = *(double *)(q->data + i*q->strides[0]); 211 double phi_value = *(double *)(phi->data + i*phi->strides[0]); 212 double *result_value = (double *)(result->data + i*result->strides[0]); 213 if (q_value == 0) 214 *result_value = 0.0; 215 else 216 *result_value = model->evaluate_rphi(q_value, phi_value); 217 } 218 return PyArray_Return(result); 219 } 183 220 184 /** 221 185 * Function to call to evaluate model … … 238 202 239 203 if (PyArray_Check(x) && PyArray_Check(y)) { 240 x_len = dims[0]= x->dimensions[0]; 241 y_len = dims[1]= y->dimensions[1]; 204 205 x_len = dims[1]= x->dimensions[1]; 206 y_len = dims[0]= y->dimensions[0]; 242 207 243 208 // Make a new double matrix of same dims … … 252 217 for ( i=0; i< x_len; i++) { 253 218 for ( j=0; j< y_len; j++) { 254 double x_value = *(double *)(x->data + i*x->strides[ 0]);255 double y_value = *(double *)(y->data + j*y->strides[ 1]);219 double x_value = *(double *)(x->data + i*x->strides[1]); 220 double y_value = *(double *)(y->data + j*y->strides[0]); 256 221 double *result_value = (double *)(result->data + 257 i*result->strides[ 0] + j*result->strides[1]);222 i*result->strides[1] + j*result->strides[0]); 258 223 *result_value = (*model)(x_value, y_value); 259 224 } -
sansmodels/src/sans/models/c_models/CCoreShellEllipsoidModel.cpp
reddff027 ra8d6888 205 205 206 206 if (PyArray_Check(x) && PyArray_Check(y)) { 207 x_len = dims[0]= x->dimensions[0]; 208 y_len = dims[1]= y->dimensions[1]; 207 208 x_len = dims[1]= x->dimensions[1]; 209 y_len = dims[0]= y->dimensions[0]; 209 210 210 211 // Make a new double matrix of same dims … … 219 220 for ( i=0; i< x_len; i++) { 220 221 for ( j=0; j< y_len; j++) { 221 double x_value = *(double *)(x->data + i*x->strides[ 0]);222 double y_value = *(double *)(y->data + j*y->strides[ 1]);222 double x_value = *(double *)(x->data + i*x->strides[1]); 223 double y_value = *(double *)(y->data + j*y->strides[0]); 223 224 double *result_value = (double *)(result->data + 224 i*result->strides[ 0] + j*result->strides[1]);225 i*result->strides[1] + j*result->strides[0]); 225 226 *result_value = (*model)(x_value, y_value); 226 227 } -
sansmodels/src/sans/models/c_models/CCoreShellModel.cpp
r9bd69098 ra8d6888 169 169 return PyArray_Return(result); 170 170 } 171 /** 172 * Function to call to evaluate model 173 * @param args: input numpy array [q[],phi[]] 174 * @return: numpy array object 175 */ 176 static PyObject * evaluateTwoDim( CoreShellModel* model, 177 PyArrayObject *q, PyArrayObject *phi) 178 { 179 PyArrayObject *result; 180 //check validity of input vectors 181 if (q->nd != 1 || q->descr->type_num != PyArray_DOUBLE 182 || phi->nd != 1 || phi->descr->type_num != PyArray_DOUBLE 183 || phi->dimensions[0] != q->dimensions[0]){ 184 185 //const char * message= "Invalid array: q->nd=%d,type_num=%d\n",q->nd,q->descr->type_num; 186 PyErr_SetString(PyExc_ValueError ,"wrong input"); 187 return NULL; 188 } 189 result= (PyArrayObject *)PyArray_FromDims(q->nd,(int*)(q->dimensions), PyArray_DOUBLE); 190 191 if (result == NULL){ 192 const char * message= "Could not create result "; 193 PyErr_SetString(PyExc_RuntimeError , message); 194 return NULL; 195 } 196 197 for (int i = 0; i < q->dimensions[0]; i++) { 198 double q_value = *(double *)(q->data + i*q->strides[0]); 199 double phi_value = *(double *)(phi->data + i*phi->strides[0]); 200 double *result_value = (double *)(result->data + i*result->strides[0]); 201 if (q_value == 0) 202 *result_value = 0.0; 203 else 204 *result_value = model->evaluate_rphi(q_value, phi_value); 205 } 206 return PyArray_Return(result); 207 } 171 208 172 /** 209 173 * Function to call to evaluate model … … 226 190 227 191 if (PyArray_Check(x) && PyArray_Check(y)) { 228 x_len = dims[0]= x->dimensions[0]; 229 y_len = dims[1]= y->dimensions[1]; 192 193 x_len = dims[1]= x->dimensions[1]; 194 y_len = dims[0]= y->dimensions[0]; 230 195 231 196 // Make a new double matrix of same dims … … 240 205 for ( i=0; i< x_len; i++) { 241 206 for ( j=0; j< y_len; j++) { 242 double x_value = *(double *)(x->data + i*x->strides[ 0]);243 double y_value = *(double *)(y->data + j*y->strides[ 1]);207 double x_value = *(double *)(x->data + i*x->strides[1]); 208 double y_value = *(double *)(y->data + j*y->strides[0]); 244 209 double *result_value = (double *)(result->data + 245 i*result->strides[ 0] + j*result->strides[1]);210 i*result->strides[1] + j*result->strides[0]); 246 211 *result_value = (*model)(x_value, y_value); 247 212 } -
sansmodels/src/sans/models/c_models/CCylinderModel.cpp
r975ec8e ra8d6888 196 196 197 197 if (PyArray_Check(x) && PyArray_Check(y)) { 198 x_len = dims[0]= x->dimensions[0]; 199 y_len = dims[1]= y->dimensions[1]; 198 199 x_len = dims[1]= x->dimensions[1]; 200 y_len = dims[0]= y->dimensions[0]; 200 201 201 202 // Make a new double matrix of same dims … … 210 211 for ( i=0; i< x_len; i++) { 211 212 for ( j=0; j< y_len; j++) { 212 double x_value = *(double *)(x->data + i*x->strides[ 0]);213 double y_value = *(double *)(y->data + j*y->strides[ 1]);213 double x_value = *(double *)(x->data + i*x->strides[1]); 214 double y_value = *(double *)(y->data + j*y->strides[0]); 214 215 double *result_value = (double *)(result->data + 215 i*result->strides[ 0] + j*result->strides[1]);216 i*result->strides[1] + j*result->strides[0]); 216 217 *result_value = (*model)(x_value, y_value); 217 218 } -
sansmodels/src/sans/models/c_models/CDiamCylFunc.cpp
r9bd69098 ra8d6888 164 164 return PyArray_Return(result); 165 165 } 166 /** 167 * Function to call to evaluate model 168 * @param args: input numpy array [q[],phi[]] 169 * @return: numpy array object 170 */ 171 static PyObject * evaluateTwoDim( DiamCylFunc* model, 172 PyArrayObject *q, PyArrayObject *phi) 173 { 174 PyArrayObject *result; 175 //check validity of input vectors 176 if (q->nd != 1 || q->descr->type_num != PyArray_DOUBLE 177 || phi->nd != 1 || phi->descr->type_num != PyArray_DOUBLE 178 || phi->dimensions[0] != q->dimensions[0]){ 179 180 //const char * message= "Invalid array: q->nd=%d,type_num=%d\n",q->nd,q->descr->type_num; 181 PyErr_SetString(PyExc_ValueError ,"wrong input"); 182 return NULL; 183 } 184 result= (PyArrayObject *)PyArray_FromDims(q->nd,(int*)(q->dimensions), PyArray_DOUBLE); 185 186 if (result == NULL){ 187 const char * message= "Could not create result "; 188 PyErr_SetString(PyExc_RuntimeError , message); 189 return NULL; 190 } 191 192 for (int i = 0; i < q->dimensions[0]; i++) { 193 double q_value = *(double *)(q->data + i*q->strides[0]); 194 double phi_value = *(double *)(phi->data + i*phi->strides[0]); 195 double *result_value = (double *)(result->data + i*result->strides[0]); 196 if (q_value == 0) 197 *result_value = 0.0; 198 else 199 *result_value = model->evaluate_rphi(q_value, phi_value); 200 } 201 return PyArray_Return(result); 202 } 166 203 167 /** 204 168 * Function to call to evaluate model … … 221 185 222 186 if (PyArray_Check(x) && PyArray_Check(y)) { 223 x_len = dims[0]= x->dimensions[0]; 224 y_len = dims[1]= y->dimensions[1]; 187 188 x_len = dims[1]= x->dimensions[1]; 189 y_len = dims[0]= y->dimensions[0]; 225 190 226 191 // Make a new double matrix of same dims … … 235 200 for ( i=0; i< x_len; i++) { 236 201 for ( j=0; j< y_len; j++) { 237 double x_value = *(double *)(x->data + i*x->strides[ 0]);238 double y_value = *(double *)(y->data + j*y->strides[ 1]);202 double x_value = *(double *)(x->data + i*x->strides[1]); 203 double y_value = *(double *)(y->data + j*y->strides[0]); 239 204 double *result_value = (double *)(result->data + 240 i*result->strides[ 0] + j*result->strides[1]);205 i*result->strides[1] + j*result->strides[0]); 241 206 *result_value = (*model)(x_value, y_value); 242 207 } -
sansmodels/src/sans/models/c_models/CDiamEllipFunc.cpp
r63c2095 ra8d6888 22 22 * 23 23 */ 24 #define NO_IMPORT_ARRAY 25 #define PY_ARRAY_UNIQUE_SYMBOL PyArray_API_sans 24 26 25 27 extern "C" { 26 28 #include <Python.h> 29 #include <arrayobject.h> 27 30 #include "structmember.h" 28 31 #include <stdio.h> … … 131 134 } 132 135 } 133 136 /** 137 * Function to call to evaluate model 138 * @param args: input numpy array q[] 139 * @return: numpy array object 140 */ 141 142 static PyObject *evaluateOneDim(DiamEllipFunc* model, PyArrayObject *q){ 143 PyArrayObject *result; 144 145 // Check validity of array q , q must be of dimension 1, an array of double 146 if (q->nd != 1 || q->descr->type_num != PyArray_DOUBLE) 147 { 148 //const char * message= "Invalid array: q->nd=%d,type_num=%d\n",q->nd,q->descr->type_num; 149 //PyErr_SetString(PyExc_ValueError , message); 150 return NULL; 151 } 152 result = (PyArrayObject *)PyArray_FromDims(q->nd, (int *)(q->dimensions), 153 PyArray_DOUBLE); 154 if (result == NULL) { 155 const char * message= "Could not create result "; 156 PyErr_SetString(PyExc_RuntimeError , message); 157 return NULL; 158 } 159 for (int i = 0; i < q->dimensions[0]; i++){ 160 double q_value = *(double *)(q->data + i*q->strides[0]); 161 double *result_value = (double *)(result->data + i*result->strides[0]); 162 *result_value =(*model)(q_value); 163 } 164 return PyArray_Return(result); 165 } 166 167 /** 168 * Function to call to evaluate model 169 * @param args: input numpy array [x[],y[]] 170 * @return: numpy array object 171 */ 172 static PyObject * evaluateTwoDimXY( DiamEllipFunc* model, 173 PyArrayObject *x, PyArrayObject *y) 174 { 175 PyArrayObject *result; 176 int i,j, x_len, y_len, dims[2]; 177 //check validity of input vectors 178 if (x->nd != 2 || x->descr->type_num != PyArray_DOUBLE 179 || y->nd != 2 || y->descr->type_num != PyArray_DOUBLE 180 || y->dimensions[1] != x->dimensions[0]){ 181 const char * message= "evaluateTwoDimXY expect 2 numpy arrays"; 182 PyErr_SetString(PyExc_ValueError , message); 183 return NULL; 184 } 185 186 if (PyArray_Check(x) && PyArray_Check(y)) { 187 188 x_len = dims[1]= x->dimensions[1]; 189 y_len = dims[0]= y->dimensions[0]; 190 191 // Make a new double matrix of same dims 192 result=(PyArrayObject *) PyArray_FromDims(2,dims,NPY_DOUBLE); 193 if (result == NULL){ 194 const char * message= "Could not create result "; 195 PyErr_SetString(PyExc_RuntimeError , message); 196 return NULL; 197 } 198 199 /* Do the calculation. */ 200 for ( i=0; i< x_len; i++) { 201 for ( j=0; j< y_len; j++) { 202 double x_value = *(double *)(x->data + i*x->strides[1]); 203 double y_value = *(double *)(y->data + j*y->strides[0]); 204 double *result_value = (double *)(result->data + 205 i*result->strides[1] + j*result->strides[0]); 206 *result_value = (*model)(x_value, y_value); 207 } 208 } 209 return PyArray_Return(result); 210 211 }else{ 212 PyErr_SetString(CDiamEllipFuncError, 213 "CDiamEllipFunc.evaluateTwoDimXY couldn't run."); 214 return NULL; 215 } 216 } 217 /** 218 * evalDistribution function evaluate a model function with input vector 219 * @param args: input q as vector or [qx, qy] where qx, qy are vectors 220 * 221 */ 222 static PyObject * evalDistribution(CDiamEllipFunc *self, PyObject *args){ 223 PyObject *qx, *qy; 224 PyArrayObject * pars; 225 int npars ,mpars; 226 227 // Get parameters 228 229 // Reader parameter dictionary 230 self->model->radius_b = PyFloat_AsDouble( PyDict_GetItemString(self->params, "radius_b") ); 231 self->model->radius_a = PyFloat_AsDouble( PyDict_GetItemString(self->params, "radius_a") ); 232 // Read in dispersion parameters 233 PyObject* disp_dict; 234 DispersionVisitor* visitor = new DispersionVisitor(); 235 disp_dict = PyDict_GetItemString(self->dispersion, "radius_a"); 236 self->model->radius_a.dispersion->accept_as_destination(visitor, self->model->radius_a.dispersion, disp_dict); 237 disp_dict = PyDict_GetItemString(self->dispersion, "radius_b"); 238 self->model->radius_b.dispersion->accept_as_destination(visitor, self->model->radius_b.dispersion, disp_dict); 239 240 241 // Get input and determine whether we have to supply a 1D or 2D return value. 242 if ( !PyArg_ParseTuple(args,"O",&pars) ) { 243 PyErr_SetString(CDiamEllipFuncError, 244 "CDiamEllipFunc.evalDistribution expects a q value."); 245 return NULL; 246 } 247 // Check params 248 249 if(PyArray_Check(pars)==1) { 250 251 // Length of list should 1 or 2 252 npars = pars->nd; 253 if(npars==1) { 254 // input is a numpy array 255 if (PyArray_Check(pars)) { 256 return evaluateOneDim(self->model, (PyArrayObject*)pars); 257 } 258 }else{ 259 PyErr_SetString(CDiamEllipFuncError, 260 "CDiamEllipFunc.evalDistribution expect numpy array of one dimension."); 261 return NULL; 262 } 263 }else if( PyList_Check(pars)==1) { 264 // Length of list should be 2 for I(qx,qy) 265 mpars = PyList_GET_SIZE(pars); 266 if(mpars!=2) { 267 PyErr_SetString(CDiamEllipFuncError, 268 "CDiamEllipFunc.evalDistribution expects a list of dimension 2."); 269 return NULL; 270 } 271 qx = PyList_GET_ITEM(pars,0); 272 qy = PyList_GET_ITEM(pars,1); 273 if (PyArray_Check(qx) && PyArray_Check(qy)) { 274 return evaluateTwoDimXY(self->model, (PyArrayObject*)qx, 275 (PyArrayObject*)qy); 276 }else{ 277 PyErr_SetString(CDiamEllipFuncError, 278 "CDiamEllipFunc.evalDistribution expect 2 numpy arrays in list."); 279 return NULL; 280 } 281 }else{ 282 PyErr_SetString(CDiamEllipFuncError, 283 "CDiamEllipFunc.evalDistribution couln't be run."); 284 return NULL; 285 } 286 } 134 287 135 288 /** … … 293 446 {"runXY", (PyCFunction)runXY , METH_VARARGS, 294 447 "Evaluate the model at a given Q or Qx, Qy"}, 448 449 {"evalDistribution", (PyCFunction)evalDistribution , METH_VARARGS, 450 "Evaluate the model at a given Q or Qx, Qy vector "}, 295 451 {"reset", (PyCFunction)reset , METH_VARARGS, 296 452 "Reset pair correlation"}, … … 343 499 344 500 345 static PyMethodDef module_methods[] = {346 {NULL}347 };501 //static PyMethodDef module_methods[] = { 502 // {NULL} 503 //}; 348 504 349 505 /** -
sansmodels/src/sans/models/c_models/CEllipsoidModel.cpp
r975ec8e ra8d6888 196 196 197 197 if (PyArray_Check(x) && PyArray_Check(y)) { 198 x_len = dims[0]= x->dimensions[0]; 199 y_len = dims[1]= y->dimensions[1]; 198 199 x_len = dims[1]= x->dimensions[1]; 200 y_len = dims[0]= y->dimensions[0]; 200 201 201 202 // Make a new double matrix of same dims … … 210 211 for ( i=0; i< x_len; i++) { 211 212 for ( j=0; j< y_len; j++) { 212 double x_value = *(double *)(x->data + i*x->strides[ 0]);213 double y_value = *(double *)(y->data + j*y->strides[ 1]);213 double x_value = *(double *)(x->data + i*x->strides[1]); 214 double y_value = *(double *)(y->data + j*y->strides[0]); 214 215 double *result_value = (double *)(result->data + 215 i*result->strides[ 0] + j*result->strides[1]);216 i*result->strides[1] + j*result->strides[0]); 216 217 *result_value = (*model)(x_value, y_value); 217 218 } -
sansmodels/src/sans/models/c_models/CEllipticalCylinderModel.cpp
r975ec8e ra8d6888 204 204 205 205 if (PyArray_Check(x) && PyArray_Check(y)) { 206 x_len = dims[0]= x->dimensions[0]; 207 y_len = dims[1]= y->dimensions[1]; 206 207 x_len = dims[1]= x->dimensions[1]; 208 y_len = dims[0]= y->dimensions[0]; 208 209 209 210 // Make a new double matrix of same dims … … 218 219 for ( i=0; i< x_len; i++) { 219 220 for ( j=0; j< y_len; j++) { 220 double x_value = *(double *)(x->data + i*x->strides[ 0]);221 double y_value = *(double *)(y->data + j*y->strides[ 1]);221 double x_value = *(double *)(x->data + i*x->strides[1]); 222 double y_value = *(double *)(y->data + j*y->strides[0]); 222 223 double *result_value = (double *)(result->data + 223 i*result->strides[ 0] + j*result->strides[1]);224 i*result->strides[1] + j*result->strides[0]); 224 225 *result_value = (*model)(x_value, y_value); 225 226 } -
sansmodels/src/sans/models/c_models/CFlexibleCylinderModel.cpp
r9bd69098 ra8d6888 171 171 return PyArray_Return(result); 172 172 } 173 /** 174 * Function to call to evaluate model 175 * @param args: input numpy array [q[],phi[]] 176 * @return: numpy array object 177 */ 178 static PyObject * evaluateTwoDim( FlexibleCylinderModel* model, 179 PyArrayObject *q, PyArrayObject *phi) 180 { 181 PyArrayObject *result; 182 //check validity of input vectors 183 if (q->nd != 1 || q->descr->type_num != PyArray_DOUBLE 184 || phi->nd != 1 || phi->descr->type_num != PyArray_DOUBLE 185 || phi->dimensions[0] != q->dimensions[0]){ 186 187 //const char * message= "Invalid array: q->nd=%d,type_num=%d\n",q->nd,q->descr->type_num; 188 PyErr_SetString(PyExc_ValueError ,"wrong input"); 189 return NULL; 190 } 191 result= (PyArrayObject *)PyArray_FromDims(q->nd,(int*)(q->dimensions), PyArray_DOUBLE); 192 193 if (result == NULL){ 194 const char * message= "Could not create result "; 195 PyErr_SetString(PyExc_RuntimeError , message); 196 return NULL; 197 } 198 199 for (int i = 0; i < q->dimensions[0]; i++) { 200 double q_value = *(double *)(q->data + i*q->strides[0]); 201 double phi_value = *(double *)(phi->data + i*phi->strides[0]); 202 double *result_value = (double *)(result->data + i*result->strides[0]); 203 if (q_value == 0) 204 *result_value = 0.0; 205 else 206 *result_value = model->evaluate_rphi(q_value, phi_value); 207 } 208 return PyArray_Return(result); 209 } 173 210 174 /** 211 175 * Function to call to evaluate model … … 228 192 229 193 if (PyArray_Check(x) && PyArray_Check(y)) { 230 x_len = dims[0]= x->dimensions[0]; 231 y_len = dims[1]= y->dimensions[1]; 194 195 x_len = dims[1]= x->dimensions[1]; 196 y_len = dims[0]= y->dimensions[0]; 232 197 233 198 // Make a new double matrix of same dims … … 242 207 for ( i=0; i< x_len; i++) { 243 208 for ( j=0; j< y_len; j++) { 244 double x_value = *(double *)(x->data + i*x->strides[ 0]);245 double y_value = *(double *)(y->data + j*y->strides[ 1]);209 double x_value = *(double *)(x->data + i*x->strides[1]); 210 double y_value = *(double *)(y->data + j*y->strides[0]); 246 211 double *result_value = (double *)(result->data + 247 i*result->strides[ 0] + j*result->strides[1]);212 i*result->strides[1] + j*result->strides[0]); 248 213 *result_value = (*model)(x_value, y_value); 249 214 } -
sansmodels/src/sans/models/c_models/CGaussian.cpp
r9bd69098 ra8d6888 159 159 return PyArray_Return(result); 160 160 } 161 /** 162 * Function to call to evaluate model 163 * @param args: input numpy array [q[],phi[]] 164 * @return: numpy array object 165 */ 166 static PyObject * evaluateTwoDim( Gaussian* model, 167 PyArrayObject *q, PyArrayObject *phi) 168 { 169 PyArrayObject *result; 170 //check validity of input vectors 171 if (q->nd != 1 || q->descr->type_num != PyArray_DOUBLE 172 || phi->nd != 1 || phi->descr->type_num != PyArray_DOUBLE 173 || phi->dimensions[0] != q->dimensions[0]){ 174 175 //const char * message= "Invalid array: q->nd=%d,type_num=%d\n",q->nd,q->descr->type_num; 176 PyErr_SetString(PyExc_ValueError ,"wrong input"); 177 return NULL; 178 } 179 result= (PyArrayObject *)PyArray_FromDims(q->nd,(int*)(q->dimensions), PyArray_DOUBLE); 180 181 if (result == NULL){ 182 const char * message= "Could not create result "; 183 PyErr_SetString(PyExc_RuntimeError , message); 184 return NULL; 185 } 186 187 for (int i = 0; i < q->dimensions[0]; i++) { 188 double q_value = *(double *)(q->data + i*q->strides[0]); 189 double phi_value = *(double *)(phi->data + i*phi->strides[0]); 190 double *result_value = (double *)(result->data + i*result->strides[0]); 191 if (q_value == 0) 192 *result_value = 0.0; 193 else 194 *result_value = model->evaluate_rphi(q_value, phi_value); 195 } 196 return PyArray_Return(result); 197 } 161 198 162 /** 199 163 * Function to call to evaluate model … … 216 180 217 181 if (PyArray_Check(x) && PyArray_Check(y)) { 218 x_len = dims[0]= x->dimensions[0]; 219 y_len = dims[1]= y->dimensions[1]; 182 183 x_len = dims[1]= x->dimensions[1]; 184 y_len = dims[0]= y->dimensions[0]; 220 185 221 186 // Make a new double matrix of same dims … … 230 195 for ( i=0; i< x_len; i++) { 231 196 for ( j=0; j< y_len; j++) { 232 double x_value = *(double *)(x->data + i*x->strides[ 0]);233 double y_value = *(double *)(y->data + j*y->strides[ 1]);197 double x_value = *(double *)(x->data + i*x->strides[1]); 198 double y_value = *(double *)(y->data + j*y->strides[0]); 234 199 double *result_value = (double *)(result->data + 235 i*result->strides[ 0] + j*result->strides[1]);200 i*result->strides[1] + j*result->strides[0]); 236 201 *result_value = (*model)(x_value, y_value); 237 202 } -
sansmodels/src/sans/models/c_models/CHardsphereStructure.cpp
r9bd69098 ra8d6888 161 161 return PyArray_Return(result); 162 162 } 163 /** 164 * Function to call to evaluate model 165 * @param args: input numpy array [q[],phi[]] 166 * @return: numpy array object 167 */ 168 static PyObject * evaluateTwoDim( HardsphereStructure* model, 169 PyArrayObject *q, PyArrayObject *phi) 170 { 171 PyArrayObject *result; 172 //check validity of input vectors 173 if (q->nd != 1 || q->descr->type_num != PyArray_DOUBLE 174 || phi->nd != 1 || phi->descr->type_num != PyArray_DOUBLE 175 || phi->dimensions[0] != q->dimensions[0]){ 176 177 //const char * message= "Invalid array: q->nd=%d,type_num=%d\n",q->nd,q->descr->type_num; 178 PyErr_SetString(PyExc_ValueError ,"wrong input"); 179 return NULL; 180 } 181 result= (PyArrayObject *)PyArray_FromDims(q->nd,(int*)(q->dimensions), PyArray_DOUBLE); 182 183 if (result == NULL){ 184 const char * message= "Could not create result "; 185 PyErr_SetString(PyExc_RuntimeError , message); 186 return NULL; 187 } 188 189 for (int i = 0; i < q->dimensions[0]; i++) { 190 double q_value = *(double *)(q->data + i*q->strides[0]); 191 double phi_value = *(double *)(phi->data + i*phi->strides[0]); 192 double *result_value = (double *)(result->data + i*result->strides[0]); 193 if (q_value == 0) 194 *result_value = 0.0; 195 else 196 *result_value = model->evaluate_rphi(q_value, phi_value); 197 } 198 return PyArray_Return(result); 199 } 163 200 164 /** 201 165 * Function to call to evaluate model … … 218 182 219 183 if (PyArray_Check(x) && PyArray_Check(y)) { 220 x_len = dims[0]= x->dimensions[0]; 221 y_len = dims[1]= y->dimensions[1]; 184 185 x_len = dims[1]= x->dimensions[1]; 186 y_len = dims[0]= y->dimensions[0]; 222 187 223 188 // Make a new double matrix of same dims … … 232 197 for ( i=0; i< x_len; i++) { 233 198 for ( j=0; j< y_len; j++) { 234 double x_value = *(double *)(x->data + i*x->strides[ 0]);235 double y_value = *(double *)(y->data + j*y->strides[ 1]);199 double x_value = *(double *)(x->data + i*x->strides[1]); 200 double y_value = *(double *)(y->data + j*y->strides[0]); 236 201 double *result_value = (double *)(result->data + 237 i*result->strides[ 0] + j*result->strides[1]);202 i*result->strides[1] + j*result->strides[0]); 238 203 *result_value = (*model)(x_value, y_value); 239 204 } -
sansmodels/src/sans/models/c_models/CHayterMSAStructure.cpp
r9bd69098 ra8d6888 165 165 return PyArray_Return(result); 166 166 } 167 /** 168 * Function to call to evaluate model 169 * @param args: input numpy array [q[],phi[]] 170 * @return: numpy array object 171 */ 172 static PyObject * evaluateTwoDim( HayterMSAStructure* model, 173 PyArrayObject *q, PyArrayObject *phi) 174 { 175 PyArrayObject *result; 176 //check validity of input vectors 177 if (q->nd != 1 || q->descr->type_num != PyArray_DOUBLE 178 || phi->nd != 1 || phi->descr->type_num != PyArray_DOUBLE 179 || phi->dimensions[0] != q->dimensions[0]){ 180 181 //const char * message= "Invalid array: q->nd=%d,type_num=%d\n",q->nd,q->descr->type_num; 182 PyErr_SetString(PyExc_ValueError ,"wrong input"); 183 return NULL; 184 } 185 result= (PyArrayObject *)PyArray_FromDims(q->nd,(int*)(q->dimensions), PyArray_DOUBLE); 186 187 if (result == NULL){ 188 const char * message= "Could not create result "; 189 PyErr_SetString(PyExc_RuntimeError , message); 190 return NULL; 191 } 192 193 for (int i = 0; i < q->dimensions[0]; i++) { 194 double q_value = *(double *)(q->data + i*q->strides[0]); 195 double phi_value = *(double *)(phi->data + i*phi->strides[0]); 196 double *result_value = (double *)(result->data + i*result->strides[0]); 197 if (q_value == 0) 198 *result_value = 0.0; 199 else 200 *result_value = model->evaluate_rphi(q_value, phi_value); 201 } 202 return PyArray_Return(result); 203 } 167 204 168 /** 205 169 * Function to call to evaluate model … … 222 186 223 187 if (PyArray_Check(x) && PyArray_Check(y)) { 224 x_len = dims[0]= x->dimensions[0]; 225 y_len = dims[1]= y->dimensions[1]; 188 189 x_len = dims[1]= x->dimensions[1]; 190 y_len = dims[0]= y->dimensions[0]; 226 191 227 192 // Make a new double matrix of same dims … … 236 201 for ( i=0; i< x_len; i++) { 237 202 for ( j=0; j< y_len; j++) { 238 double x_value = *(double *)(x->data + i*x->strides[ 0]);239 double y_value = *(double *)(y->data + j*y->strides[ 1]);203 double x_value = *(double *)(x->data + i*x->strides[1]); 204 double y_value = *(double *)(y->data + j*y->strides[0]); 240 205 double *result_value = (double *)(result->data + 241 i*result->strides[ 0] + j*result->strides[1]);206 i*result->strides[1] + j*result->strides[0]); 242 207 *result_value = (*model)(x_value, y_value); 243 208 } -
sansmodels/src/sans/models/c_models/CHollowCylinderModel.cpp
r9bd69098 ra8d6888 179 179 return PyArray_Return(result); 180 180 } 181 /** 182 * Function to call to evaluate model 183 * @param args: input numpy array [q[],phi[]] 184 * @return: numpy array object 185 */ 186 static PyObject * evaluateTwoDim( HollowCylinderModel* model, 187 PyArrayObject *q, PyArrayObject *phi) 188 { 189 PyArrayObject *result; 190 //check validity of input vectors 191 if (q->nd != 1 || q->descr->type_num != PyArray_DOUBLE 192 || phi->nd != 1 || phi->descr->type_num != PyArray_DOUBLE 193 || phi->dimensions[0] != q->dimensions[0]){ 194 195 //const char * message= "Invalid array: q->nd=%d,type_num=%d\n",q->nd,q->descr->type_num; 196 PyErr_SetString(PyExc_ValueError ,"wrong input"); 197 return NULL; 198 } 199 result= (PyArrayObject *)PyArray_FromDims(q->nd,(int*)(q->dimensions), PyArray_DOUBLE); 200 201 if (result == NULL){ 202 const char * message= "Could not create result "; 203 PyErr_SetString(PyExc_RuntimeError , message); 204 return NULL; 205 } 206 207 for (int i = 0; i < q->dimensions[0]; i++) { 208 double q_value = *(double *)(q->data + i*q->strides[0]); 209 double phi_value = *(double *)(phi->data + i*phi->strides[0]); 210 double *result_value = (double *)(result->data + i*result->strides[0]); 211 if (q_value == 0) 212 *result_value = 0.0; 213 else 214 *result_value = model->evaluate_rphi(q_value, phi_value); 215 } 216 return PyArray_Return(result); 217 } 181 218 182 /** 219 183 * Function to call to evaluate model … … 236 200 237 201 if (PyArray_Check(x) && PyArray_Check(y)) { 238 x_len = dims[0]= x->dimensions[0]; 239 y_len = dims[1]= y->dimensions[1]; 202 203 x_len = dims[1]= x->dimensions[1]; 204 y_len = dims[0]= y->dimensions[0]; 240 205 241 206 // Make a new double matrix of same dims … … 250 215 for ( i=0; i< x_len; i++) { 251 216 for ( j=0; j< y_len; j++) { 252 double x_value = *(double *)(x->data + i*x->strides[ 0]);253 double y_value = *(double *)(y->data + j*y->strides[ 1]);217 double x_value = *(double *)(x->data + i*x->strides[1]); 218 double y_value = *(double *)(y->data + j*y->strides[0]); 254 219 double *result_value = (double *)(result->data + 255 i*result->strides[ 0] + j*result->strides[1]);220 i*result->strides[1] + j*result->strides[0]); 256 221 *result_value = (*model)(x_value, y_value); 257 222 } -
sansmodels/src/sans/models/c_models/CLamellarFFHGModel.cpp
r9bd69098 ra8d6888 169 169 return PyArray_Return(result); 170 170 } 171 /** 172 * Function to call to evaluate model 173 * @param args: input numpy array [q[],phi[]] 174 * @return: numpy array object 175 */ 176 static PyObject * evaluateTwoDim( LamellarFFHGModel* model, 177 PyArrayObject *q, PyArrayObject *phi) 178 { 179 PyArrayObject *result; 180 //check validity of input vectors 181 if (q->nd != 1 || q->descr->type_num != PyArray_DOUBLE 182 || phi->nd != 1 || phi->descr->type_num != PyArray_DOUBLE 183 || phi->dimensions[0] != q->dimensions[0]){ 184 185 //const char * message= "Invalid array: q->nd=%d,type_num=%d\n",q->nd,q->descr->type_num; 186 PyErr_SetString(PyExc_ValueError ,"wrong input"); 187 return NULL; 188 } 189 result= (PyArrayObject *)PyArray_FromDims(q->nd,(int*)(q->dimensions), PyArray_DOUBLE); 190 191 if (result == NULL){ 192 const char * message= "Could not create result "; 193 PyErr_SetString(PyExc_RuntimeError , message); 194 return NULL; 195 } 196 197 for (int i = 0; i < q->dimensions[0]; i++) { 198 double q_value = *(double *)(q->data + i*q->strides[0]); 199 double phi_value = *(double *)(phi->data + i*phi->strides[0]); 200 double *result_value = (double *)(result->data + i*result->strides[0]); 201 if (q_value == 0) 202 *result_value = 0.0; 203 else 204 *result_value = model->evaluate_rphi(q_value, phi_value); 205 } 206 return PyArray_Return(result); 207 } 171 208 172 /** 209 173 * Function to call to evaluate model … … 226 190 227 191 if (PyArray_Check(x) && PyArray_Check(y)) { 228 x_len = dims[0]= x->dimensions[0]; 229 y_len = dims[1]= y->dimensions[1]; 192 193 x_len = dims[1]= x->dimensions[1]; 194 y_len = dims[0]= y->dimensions[0]; 230 195 231 196 // Make a new double matrix of same dims … … 240 205 for ( i=0; i< x_len; i++) { 241 206 for ( j=0; j< y_len; j++) { 242 double x_value = *(double *)(x->data + i*x->strides[ 0]);243 double y_value = *(double *)(y->data + j*y->strides[ 1]);207 double x_value = *(double *)(x->data + i*x->strides[1]); 208 double y_value = *(double *)(y->data + j*y->strides[0]); 244 209 double *result_value = (double *)(result->data + 245 i*result->strides[ 0] + j*result->strides[1]);210 i*result->strides[1] + j*result->strides[0]); 246 211 *result_value = (*model)(x_value, y_value); 247 212 } -
sansmodels/src/sans/models/c_models/CLamellarModel.cpp
r975ec8e ra8d6888 185 185 186 186 if (PyArray_Check(x) && PyArray_Check(y)) { 187 x_len = dims[0]= x->dimensions[0]; 188 y_len = dims[1]= y->dimensions[1]; 187 188 x_len = dims[1]= x->dimensions[1]; 189 y_len = dims[0]= y->dimensions[0]; 189 190 190 191 // Make a new double matrix of same dims … … 199 200 for ( i=0; i< x_len; i++) { 200 201 for ( j=0; j< y_len; j++) { 201 double x_value = *(double *)(x->data + i*x->strides[ 0]);202 double y_value = *(double *)(y->data + j*y->strides[ 1]);202 double x_value = *(double *)(x->data + i*x->strides[1]); 203 double y_value = *(double *)(y->data + j*y->strides[0]); 203 204 double *result_value = (double *)(result->data + 204 i*result->strides[ 0] + j*result->strides[1]);205 i*result->strides[1] + j*result->strides[0]); 205 206 *result_value = (*model)(x_value, y_value); 206 207 } -
sansmodels/src/sans/models/c_models/CLamellarPSHGModel.cpp
r96b59384 ra8d6888 22 22 * 23 23 */ 24 #define NO_IMPORT_ARRAY 25 #define PY_ARRAY_UNIQUE_SYMBOL PyArray_API_sans 24 26 25 27 extern "C" { 26 28 #include <Python.h> 29 #include <arrayobject.h> 27 30 #include "structmember.h" 28 31 #include <stdio.h> … … 142 145 } 143 146 } 144 145 146 147 /** 147 148 * Function to call to evaluate model 148 * @param args: input q or [q,phi]149 * @return: function value149 * @param args: input numpy array q[] 150 * @return: numpy array object 150 151 */ 151 static PyObject * run(CLamellarPSHGModel *self, PyObject *args) { 152 double q_value, phi_value; 153 PyObject* pars; 154 int npars; 152 153 static PyObject *evaluateOneDim(LamellarPSHGModel* model, PyArrayObject *q){ 154 PyArrayObject *result; 155 156 // Check validity of array q , q must be of dimension 1, an array of double 157 if (q->nd != 1 || q->descr->type_num != PyArray_DOUBLE) 158 { 159 //const char * message= "Invalid array: q->nd=%d,type_num=%d\n",q->nd,q->descr->type_num; 160 //PyErr_SetString(PyExc_ValueError , message); 161 return NULL; 162 } 163 result = (PyArrayObject *)PyArray_FromDims(q->nd, (int *)(q->dimensions), 164 PyArray_DOUBLE); 165 if (result == NULL) { 166 const char * message= "Could not create result "; 167 PyErr_SetString(PyExc_RuntimeError , message); 168 return NULL; 169 } 170 for (int i = 0; i < q->dimensions[0]; i++){ 171 double q_value = *(double *)(q->data + i*q->strides[0]); 172 double *result_value = (double *)(result->data + i*result->strides[0]); 173 *result_value =(*model)(q_value); 174 } 175 return PyArray_Return(result); 176 } 177 178 /** 179 * Function to call to evaluate model 180 * @param args: input numpy array [x[],y[]] 181 * @return: numpy array object 182 */ 183 static PyObject * evaluateTwoDimXY( LamellarPSHGModel* model, 184 PyArrayObject *x, PyArrayObject *y) 185 { 186 PyArrayObject *result; 187 int i,j, x_len, y_len, dims[2]; 188 //check validity of input vectors 189 if (x->nd != 2 || x->descr->type_num != PyArray_DOUBLE 190 || y->nd != 2 || y->descr->type_num != PyArray_DOUBLE 191 || y->dimensions[1] != x->dimensions[0]){ 192 const char * message= "evaluateTwoDimXY expect 2 numpy arrays"; 193 PyErr_SetString(PyExc_ValueError , message); 194 return NULL; 195 } 196 197 if (PyArray_Check(x) && PyArray_Check(y)) { 198 199 x_len = dims[1]= x->dimensions[1]; 200 y_len = dims[0]= y->dimensions[0]; 201 202 // Make a new double matrix of same dims 203 result=(PyArrayObject *) PyArray_FromDims(2,dims,NPY_DOUBLE); 204 if (result == NULL){ 205 const char * message= "Could not create result "; 206 PyErr_SetString(PyExc_RuntimeError , message); 207 return NULL; 208 } 209 210 /* Do the calculation. */ 211 for ( i=0; i< x_len; i++) { 212 for ( j=0; j< y_len; j++) { 213 double x_value = *(double *)(x->data + i*x->strides[1]); 214 double y_value = *(double *)(y->data + j*y->strides[0]); 215 double *result_value = (double *)(result->data + 216 i*result->strides[1] + j*result->strides[0]); 217 *result_value = (*model)(x_value, y_value); 218 } 219 } 220 return PyArray_Return(result); 221 222 }else{ 223 PyErr_SetString(CLamellarPSHGModelError, 224 "CLamellarPSHGModel.evaluateTwoDimXY couldn't run."); 225 return NULL; 226 } 227 } 228 /** 229 * evalDistribution function evaluate a model function with input vector 230 * @param args: input q as vector or [qx, qy] where qx, qy are vectors 231 * 232 */ 233 static PyObject * evalDistribution(CLamellarPSHGModel *self, PyObject *args){ 234 PyObject *qx, *qy; 235 PyArrayObject * pars; 236 int npars ,mpars; 155 237 156 238 // Get parameters … … 181 263 if ( !PyArg_ParseTuple(args,"O",&pars) ) { 182 264 PyErr_SetString(CLamellarPSHGModelError, 265 "CLamellarPSHGModel.evalDistribution expects a q value."); 266 return NULL; 267 } 268 // Check params 269 270 if(PyArray_Check(pars)==1) { 271 272 // Length of list should 1 or 2 273 npars = pars->nd; 274 if(npars==1) { 275 // input is a numpy array 276 if (PyArray_Check(pars)) { 277 return evaluateOneDim(self->model, (PyArrayObject*)pars); 278 } 279 }else{ 280 PyErr_SetString(CLamellarPSHGModelError, 281 "CLamellarPSHGModel.evalDistribution expect numpy array of one dimension."); 282 return NULL; 283 } 284 }else if( PyList_Check(pars)==1) { 285 // Length of list should be 2 for I(qx,qy) 286 mpars = PyList_GET_SIZE(pars); 287 if(mpars!=2) { 288 PyErr_SetString(CLamellarPSHGModelError, 289 "CLamellarPSHGModel.evalDistribution expects a list of dimension 2."); 290 return NULL; 291 } 292 qx = PyList_GET_ITEM(pars,0); 293 qy = PyList_GET_ITEM(pars,1); 294 if (PyArray_Check(qx) && PyArray_Check(qy)) { 295 return evaluateTwoDimXY(self->model, (PyArrayObject*)qx, 296 (PyArrayObject*)qy); 297 }else{ 298 PyErr_SetString(CLamellarPSHGModelError, 299 "CLamellarPSHGModel.evalDistribution expect 2 numpy arrays in list."); 300 return NULL; 301 } 302 }else{ 303 PyErr_SetString(CLamellarPSHGModelError, 304 "CLamellarPSHGModel.evalDistribution couln't be run."); 305 return NULL; 306 } 307 } 308 309 /** 310 * Function to call to evaluate model 311 * @param args: input q or [q,phi] 312 * @return: function value 313 */ 314 static PyObject * run(CLamellarPSHGModel *self, PyObject *args) { 315 double q_value, phi_value; 316 PyObject* pars; 317 int npars; 318 319 // Get parameters 320 321 // Reader parameter dictionary 322 self->model->n_plates = PyFloat_AsDouble( PyDict_GetItemString(self->params, "n_plates") ); 323 self->model->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") ); 324 self->model->deltaT = PyFloat_AsDouble( PyDict_GetItemString(self->params, "deltaT") ); 325 self->model->spacing = PyFloat_AsDouble( PyDict_GetItemString(self->params, "spacing") ); 326 self->model->sld_tail = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_tail") ); 327 self->model->sld_solvent = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_solvent") ); 328 self->model->caille = PyFloat_AsDouble( PyDict_GetItemString(self->params, "caille") ); 329 self->model->sld_head = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_head") ); 330 self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") ); 331 self->model->deltaH = PyFloat_AsDouble( PyDict_GetItemString(self->params, "deltaH") ); 332 // Read in dispersion parameters 333 PyObject* disp_dict; 334 DispersionVisitor* visitor = new DispersionVisitor(); 335 disp_dict = PyDict_GetItemString(self->dispersion, "deltaT"); 336 self->model->deltaT.dispersion->accept_as_destination(visitor, self->model->deltaT.dispersion, disp_dict); 337 disp_dict = PyDict_GetItemString(self->dispersion, "deltaH"); 338 self->model->deltaH.dispersion->accept_as_destination(visitor, self->model->deltaH.dispersion, disp_dict); 339 disp_dict = PyDict_GetItemString(self->dispersion, "spacing"); 340 self->model->spacing.dispersion->accept_as_destination(visitor, self->model->spacing.dispersion, disp_dict); 341 342 343 // Get input and determine whether we have to supply a 1D or 2D return value. 344 if ( !PyArg_ParseTuple(args,"O",&pars) ) { 345 PyErr_SetString(CLamellarPSHGModelError, 183 346 "CLamellarPSHGModel.run expects a q value."); 184 347 return NULL; … … 326 489 {"runXY", (PyCFunction)runXY , METH_VARARGS, 327 490 "Evaluate the model at a given Q or Qx, Qy"}, 491 492 {"evalDistribution", (PyCFunction)evalDistribution , METH_VARARGS, 493 "Evaluate the model at a given Q or Qx, Qy vector "}, 328 494 {"reset", (PyCFunction)reset , METH_VARARGS, 329 495 "Reset pair correlation"}, … … 376 542 377 543 378 static PyMethodDef module_methods[] = {379 {NULL}380 };544 //static PyMethodDef module_methods[] = { 545 // {NULL} 546 //}; 381 547 382 548 /** -
sansmodels/src/sans/models/c_models/CLamellarPSModel.cpp
rc1c29b6 ra8d6888 190 190 191 191 if (PyArray_Check(x) && PyArray_Check(y)) { 192 x_len = dims[0]= x->dimensions[0]; 193 y_len = dims[1]= y->dimensions[1]; 192 193 x_len = dims[1]= x->dimensions[1]; 194 y_len = dims[0]= y->dimensions[0]; 194 195 195 196 // Make a new double matrix of same dims … … 204 205 for ( i=0; i< x_len; i++) { 205 206 for ( j=0; j< y_len; j++) { 206 double x_value = *(double *)(x->data + i*x->strides[ 0]);207 double y_value = *(double *)(y->data + j*y->strides[ 1]);207 double x_value = *(double *)(x->data + i*x->strides[1]); 208 double y_value = *(double *)(y->data + j*y->strides[0]); 208 209 double *result_value = (double *)(result->data + 209 i*result->strides[ 0] + j*result->strides[1]);210 i*result->strides[1] + j*result->strides[0]); 210 211 *result_value = (*model)(x_value, y_value); 211 212 } -
sansmodels/src/sans/models/c_models/CLogNormal.cpp
r5b56b7a ra8d6888 159 159 return PyArray_Return(result); 160 160 } 161 /** 162 * Function to call to evaluate model 163 * @param args: input numpy array [q[],phi[]] 164 * @return: numpy array object 165 */ 166 static PyObject * evaluateTwoDim( LogNormal* model, 167 PyArrayObject *q, PyArrayObject *phi) 168 { 169 PyArrayObject *result; 170 //check validity of input vectors 171 if (q->nd != 1 || q->descr->type_num != PyArray_DOUBLE 172 || phi->nd != 1 || phi->descr->type_num != PyArray_DOUBLE 173 || phi->dimensions[0] != q->dimensions[0]){ 174 175 //const char * message= "Invalid array: q->nd=%d,type_num=%d\n",q->nd,q->descr->type_num; 176 PyErr_SetString(PyExc_ValueError ,"wrong input"); 177 return NULL; 178 } 179 result= (PyArrayObject *)PyArray_FromDims(q->nd,(int*)(q->dimensions), PyArray_DOUBLE); 180 181 if (result == NULL){ 182 const char * message= "Could not create result "; 183 PyErr_SetString(PyExc_RuntimeError , message); 184 return NULL; 185 } 186 187 for (int i = 0; i < q->dimensions[0]; i++) { 188 double q_value = *(double *)(q->data + i*q->strides[0]); 189 double phi_value = *(double *)(phi->data + i*phi->strides[0]); 190 double *result_value = (double *)(result->data + i*result->strides[0]); 191 if (q_value == 0) 192 *result_value = 0.0; 193 else 194 *result_value = model->evaluate_rphi(q_value, phi_value); 195 } 196 return PyArray_Return(result); 197 } 161 198 162 /** 199 163 * Function to call to evaluate model … … 216 180 217 181 if (PyArray_Check(x) && PyArray_Check(y)) { 218 x_len = dims[0]= x->dimensions[0]; 219 y_len = dims[1]= y->dimensions[1]; 182 183 x_len = dims[1]= x->dimensions[1]; 184 y_len = dims[0]= y->dimensions[0]; 220 185 221 186 // Make a new double matrix of same dims … … 230 195 for ( i=0; i< x_len; i++) { 231 196 for ( j=0; j< y_len; j++) { 232 double x_value = *(double *)(x->data + i*x->strides[ 0]);233 double y_value = *(double *)(y->data + j*y->strides[ 1]);197 double x_value = *(double *)(x->data + i*x->strides[1]); 198 double y_value = *(double *)(y->data + j*y->strides[0]); 234 199 double *result_value = (double *)(result->data + 235 i*result->strides[ 0] + j*result->strides[1]);200 i*result->strides[1] + j*result->strides[0]); 236 201 *result_value = (*model)(x_value, y_value); 237 202 } -
sansmodels/src/sans/models/c_models/CLorentzian.cpp
r9bd69098 ra8d6888 159 159 return PyArray_Return(result); 160 160 } 161 /** 162 * Function to call to evaluate model 163 * @param args: input numpy array [q[],phi[]] 164 * @return: numpy array object 165 */ 166 static PyObject * evaluateTwoDim( Lorentzian* model, 167 PyArrayObject *q, PyArrayObject *phi) 168 { 169 PyArrayObject *result; 170 //check validity of input vectors 171 if (q->nd != 1 || q->descr->type_num != PyArray_DOUBLE 172 || phi->nd != 1 || phi->descr->type_num != PyArray_DOUBLE 173 || phi->dimensions[0] != q->dimensions[0]){ 174 175 //const char * message= "Invalid array: q->nd=%d,type_num=%d\n",q->nd,q->descr->type_num; 176 PyErr_SetString(PyExc_ValueError ,"wrong input"); 177 return NULL; 178 } 179 result= (PyArrayObject *)PyArray_FromDims(q->nd,(int*)(q->dimensions), PyArray_DOUBLE); 180 181 if (result == NULL){ 182 const char * message= "Could not create result "; 183 PyErr_SetString(PyExc_RuntimeError , message); 184 return NULL; 185 } 186 187 for (int i = 0; i < q->dimensions[0]; i++) { 188 double q_value = *(double *)(q->data + i*q->strides[0]); 189 double phi_value = *(double *)(phi->data + i*phi->strides[0]); 190 double *result_value = (double *)(result->data + i*result->strides[0]); 191 if (q_value == 0) 192 *result_value = 0.0; 193 else 194 *result_value = model->evaluate_rphi(q_value, phi_value); 195 } 196 return PyArray_Return(result); 197 } 161 198 162 /** 199 163 * Function to call to evaluate model … … 216 180 217 181 if (PyArray_Check(x) && PyArray_Check(y)) { 218 x_len = dims[0]= x->dimensions[0]; 219 y_len = dims[1]= y->dimensions[1]; 182 183 x_len = dims[1]= x->dimensions[1]; 184 y_len = dims[0]= y->dimensions[0]; 220 185 221 186 // Make a new double matrix of same dims … … 230 195 for ( i=0; i< x_len; i++) { 231 196 for ( j=0; j< y_len; j++) { 232 double x_value = *(double *)(x->data + i*x->strides[ 0]);233 double y_value = *(double *)(y->data + j*y->strides[ 1]);197 double x_value = *(double *)(x->data + i*x->strides[1]); 198 double y_value = *(double *)(y->data + j*y->strides[0]); 234 199 double *result_value = (double *)(result->data + 235 i*result->strides[ 0] + j*result->strides[1]);200 i*result->strides[1] + j*result->strides[0]); 236 201 *result_value = (*model)(x_value, y_value); 237 202 } -
sansmodels/src/sans/models/c_models/CMultiShellModel.cpp
r9bd69098 ra8d6888 173 173 return PyArray_Return(result); 174 174 } 175 /** 176 * Function to call to evaluate model 177 * @param args: input numpy array [q[],phi[]] 178 * @return: numpy array object 179 */ 180 static PyObject * evaluateTwoDim( MultiShellModel* model, 181 PyArrayObject *q, PyArrayObject *phi) 182 { 183 PyArrayObject *result; 184 //check validity of input vectors 185 if (q->nd != 1 || q->descr->type_num != PyArray_DOUBLE 186 || phi->nd != 1 || phi->descr->type_num != PyArray_DOUBLE 187 || phi->dimensions[0] != q->dimensions[0]){ 188 189 //const char * message= "Invalid array: q->nd=%d,type_num=%d\n",q->nd,q->descr->type_num; 190 PyErr_SetString(PyExc_ValueError ,"wrong input"); 191 return NULL; 192 } 193 result= (PyArrayObject *)PyArray_FromDims(q->nd,(int*)(q->dimensions), PyArray_DOUBLE); 194 195 if (result == NULL){ 196 const char * message= "Could not create result "; 197 PyErr_SetString(PyExc_RuntimeError , message); 198 return NULL; 199 } 200 201 for (int i = 0; i < q->dimensions[0]; i++) { 202 double q_value = *(double *)(q->data + i*q->strides[0]); 203 double phi_value = *(double *)(phi->data + i*phi->strides[0]); 204 double *result_value = (double *)(result->data + i*result->strides[0]); 205 if (q_value == 0) 206 *result_value = 0.0; 207 else 208 *result_value = model->evaluate_rphi(q_value, phi_value); 209 } 210 return PyArray_Return(result); 211 } 175 212 176 /** 213 177 * Function to call to evaluate model … … 230 194 231 195 if (PyArray_Check(x) && PyArray_Check(y)) { 232 x_len = dims[0]= x->dimensions[0]; 233 y_len = dims[1]= y->dimensions[1]; 196 197 x_len = dims[1]= x->dimensions[1]; 198 y_len = dims[0]= y->dimensions[0]; 234 199 235 200 // Make a new double matrix of same dims … … 244 209 for ( i=0; i< x_len; i++) { 245 210 for ( j=0; j< y_len; j++) { 246 double x_value = *(double *)(x->data + i*x->strides[ 0]);247 double y_value = *(double *)(y->data + j*y->strides[ 1]);211 double x_value = *(double *)(x->data + i*x->strides[1]); 212 double y_value = *(double *)(y->data + j*y->strides[0]); 248 213 double *result_value = (double *)(result->data + 249 i*result->strides[ 0] + j*result->strides[1]);214 i*result->strides[1] + j*result->strides[0]); 250 215 *result_value = (*model)(x_value, y_value); 251 216 } -
sansmodels/src/sans/models/c_models/COblateModel.cpp
r975ec8e ra8d6888 199 199 200 200 if (PyArray_Check(x) && PyArray_Check(y)) { 201 x_len = dims[0]= x->dimensions[0]; 202 y_len = dims[1]= y->dimensions[1]; 201 202 x_len = dims[1]= x->dimensions[1]; 203 y_len = dims[0]= y->dimensions[0]; 203 204 204 205 // Make a new double matrix of same dims … … 213 214 for ( i=0; i< x_len; i++) { 214 215 for ( j=0; j< y_len; j++) { 215 double x_value = *(double *)(x->data + i*x->strides[ 0]);216 double y_value = *(double *)(y->data + j*y->strides[ 1]);216 double x_value = *(double *)(x->data + i*x->strides[1]); 217 double y_value = *(double *)(y->data + j*y->strides[0]); 217 218 double *result_value = (double *)(result->data + 218 i*result->strides[ 0] + j*result->strides[1]);219 i*result->strides[1] + j*result->strides[0]); 219 220 *result_value = (*model)(x_value, y_value); 220 221 } -
sansmodels/src/sans/models/c_models/CParallelepipedModel.cpp
r8e36cdd ra8d6888 204 204 205 205 if (PyArray_Check(x) && PyArray_Check(y)) { 206 x_len = dims[0]= x->dimensions[0]; 207 y_len = dims[1]= y->dimensions[1]; 206 207 x_len = dims[1]= x->dimensions[1]; 208 y_len = dims[0]= y->dimensions[0]; 208 209 209 210 // Make a new double matrix of same dims … … 218 219 for ( i=0; i< x_len; i++) { 219 220 for ( j=0; j< y_len; j++) { 220 double x_value = *(double *)(x->data + i*x->strides[ 0]);221 double y_value = *(double *)(y->data + j*y->strides[ 1]);221 double x_value = *(double *)(x->data + i*x->strides[1]); 222 double y_value = *(double *)(y->data + j*y->strides[0]); 222 223 double *result_value = (double *)(result->data + 223 i*result->strides[ 0] + j*result->strides[1]);224 i*result->strides[1] + j*result->strides[0]); 224 225 *result_value = (*model)(x_value, y_value); 225 226 } -
sansmodels/src/sans/models/c_models/CProlateModel.cpp
r9bd69098 ra8d6888 176 176 return PyArray_Return(result); 177 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( ProlateModel* 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 ,"wrong input"); 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 } 178 215 179 /** 216 180 * Function to call to evaluate model … … 233 197 234 198 if (PyArray_Check(x) && PyArray_Check(y)) { 235 x_len = dims[0]= x->dimensions[0]; 236 y_len = dims[1]= y->dimensions[1]; 199 200 x_len = dims[1]= x->dimensions[1]; 201 y_len = dims[0]= y->dimensions[0]; 237 202 238 203 // Make a new double matrix of same dims … … 247 212 for ( i=0; i< x_len; i++) { 248 213 for ( j=0; j< y_len; j++) { 249 double x_value = *(double *)(x->data + i*x->strides[ 0]);250 double y_value = *(double *)(y->data + j*y->strides[ 1]);214 double x_value = *(double *)(x->data + i*x->strides[1]); 215 double y_value = *(double *)(y->data + j*y->strides[0]); 251 216 double *result_value = (double *)(result->data + 252 i*result->strides[ 0] + j*result->strides[1]);217 i*result->strides[1] + j*result->strides[0]); 253 218 *result_value = (*model)(x_value, y_value); 254 219 } -
sansmodels/src/sans/models/c_models/CSphereModel.cpp
r975ec8e ra8d6888 184 184 185 185 if (PyArray_Check(x) && PyArray_Check(y)) { 186 x_len = dims[0]= x->dimensions[0]; 187 y_len = dims[1]= y->dimensions[1]; 186 187 x_len = dims[1]= x->dimensions[1]; 188 y_len = dims[0]= y->dimensions[0]; 188 189 189 190 // Make a new double matrix of same dims … … 198 199 for ( i=0; i< x_len; i++) { 199 200 for ( j=0; j< y_len; j++) { 200 double x_value = *(double *)(x->data + i*x->strides[ 0]);201 double y_value = *(double *)(y->data + j*y->strides[ 1]);201 double x_value = *(double *)(x->data + i*x->strides[1]); 202 double y_value = *(double *)(y->data + j*y->strides[0]); 202 203 double *result_value = (double *)(result->data + 203 i*result->strides[ 0] + j*result->strides[1]);204 i*result->strides[1] + j*result->strides[0]); 204 205 *result_value = (*model)(x_value, y_value); 205 206 } -
sansmodels/src/sans/models/c_models/CSquareWellStructure.cpp
r9bd69098 ra8d6888 163 163 return PyArray_Return(result); 164 164 } 165 /** 166 * Function to call to evaluate model 167 * @param args: input numpy array [q[],phi[]] 168 * @return: numpy array object 169 */ 170 static PyObject * evaluateTwoDim( SquareWellStructure* model, 171 PyArrayObject *q, PyArrayObject *phi) 172 { 173 PyArrayObject *result; 174 //check validity of input vectors 175 if (q->nd != 1 || q->descr->type_num != PyArray_DOUBLE 176 || phi->nd != 1 || phi->descr->type_num != PyArray_DOUBLE 177 || phi->dimensions[0] != q->dimensions[0]){ 178 179 //const char * message= "Invalid array: q->nd=%d,type_num=%d\n",q->nd,q->descr->type_num; 180 PyErr_SetString(PyExc_ValueError ,"wrong input"); 181 return NULL; 182 } 183 result= (PyArrayObject *)PyArray_FromDims(q->nd,(int*)(q->dimensions), PyArray_DOUBLE); 184 185 if (result == NULL){ 186 const char * message= "Could not create result "; 187 PyErr_SetString(PyExc_RuntimeError , message); 188 return NULL; 189 } 190 191 for (int i = 0; i < q->dimensions[0]; i++) { 192 double q_value = *(double *)(q->data + i*q->strides[0]); 193 double phi_value = *(double *)(phi->data + i*phi->strides[0]); 194 double *result_value = (double *)(result->data + i*result->strides[0]); 195 if (q_value == 0) 196 *result_value = 0.0; 197 else 198 *result_value = model->evaluate_rphi(q_value, phi_value); 199 } 200 return PyArray_Return(result); 201 } 165 202 166 /** 203 167 * Function to call to evaluate model … … 220 184 221 185 if (PyArray_Check(x) && PyArray_Check(y)) { 222 x_len = dims[0]= x->dimensions[0]; 223 y_len = dims[1]= y->dimensions[1]; 186 187 x_len = dims[1]= x->dimensions[1]; 188 y_len = dims[0]= y->dimensions[0]; 224 189 225 190 // Make a new double matrix of same dims … … 234 199 for ( i=0; i< x_len; i++) { 235 200 for ( j=0; j< y_len; j++) { 236 double x_value = *(double *)(x->data + i*x->strides[ 0]);237 double y_value = *(double *)(y->data + j*y->strides[ 1]);201 double x_value = *(double *)(x->data + i*x->strides[1]); 202 double y_value = *(double *)(y->data + j*y->strides[0]); 238 203 double *result_value = (double *)(result->data + 239 i*result->strides[ 0] + j*result->strides[1]);204 i*result->strides[1] + j*result->strides[0]); 240 205 *result_value = (*model)(x_value, y_value); 241 206 } -
sansmodels/src/sans/models/c_models/CStackedDisksModel.cpp
r975ec8e ra8d6888 204 204 205 205 if (PyArray_Check(x) && PyArray_Check(y)) { 206 x_len = dims[0]= x->dimensions[0]; 207 y_len = dims[1]= y->dimensions[1]; 206 207 x_len = dims[1]= x->dimensions[1]; 208 y_len = dims[0]= y->dimensions[0]; 208 209 209 210 // Make a new double matrix of same dims … … 218 219 for ( i=0; i< x_len; i++) { 219 220 for ( j=0; j< y_len; j++) { 220 double x_value = *(double *)(x->data + i*x->strides[ 0]);221 double y_value = *(double *)(y->data + j*y->strides[ 1]);221 double x_value = *(double *)(x->data + i*x->strides[1]); 222 double y_value = *(double *)(y->data + j*y->strides[0]); 222 223 double *result_value = (double *)(result->data + 223 i*result->strides[ 0] + j*result->strides[1]);224 i*result->strides[1] + j*result->strides[0]); 224 225 *result_value = (*model)(x_value, y_value); 225 226 } -
sansmodels/src/sans/models/c_models/CStickyHSStructure.cpp
r9bd69098 ra8d6888 163 163 return PyArray_Return(result); 164 164 } 165 /** 166 * Function to call to evaluate model 167 * @param args: input numpy array [q[],phi[]] 168 * @return: numpy array object 169 */ 170 static PyObject * evaluateTwoDim( StickyHSStructure* model, 171 PyArrayObject *q, PyArrayObject *phi) 172 { 173 PyArrayObject *result; 174 //check validity of input vectors 175 if (q->nd != 1 || q->descr->type_num != PyArray_DOUBLE 176 || phi->nd != 1 || phi->descr->type_num != PyArray_DOUBLE 177 || phi->dimensions[0] != q->dimensions[0]){ 178 179 //const char * message= "Invalid array: q->nd=%d,type_num=%d\n",q->nd,q->descr->type_num; 180 PyErr_SetString(PyExc_ValueError ,"wrong input"); 181 return NULL; 182 } 183 result= (PyArrayObject *)PyArray_FromDims(q->nd,(int*)(q->dimensions), PyArray_DOUBLE); 184 185 if (result == NULL){ 186 const char * message= "Could not create result "; 187 PyErr_SetString(PyExc_RuntimeError , message); 188 return NULL; 189 } 190 191 for (int i = 0; i < q->dimensions[0]; i++) { 192 double q_value = *(double *)(q->data + i*q->strides[0]); 193 double phi_value = *(double *)(phi->data + i*phi->strides[0]); 194 double *result_value = (double *)(result->data + i*result->strides[0]); 195 if (q_value == 0) 196 *result_value = 0.0; 197 else 198 *result_value = model->evaluate_rphi(q_value, phi_value); 199 } 200 return PyArray_Return(result); 201 } 165 202 166 /** 203 167 * Function to call to evaluate model … … 220 184 221 185 if (PyArray_Check(x) && PyArray_Check(y)) { 222 x_len = dims[0]= x->dimensions[0]; 223 y_len = dims[1]= y->dimensions[1]; 186 187 x_len = dims[1]= x->dimensions[1]; 188 y_len = dims[0]= y->dimensions[0]; 224 189 225 190 // Make a new double matrix of same dims … … 234 199 for ( i=0; i< x_len; i++) { 235 200 for ( j=0; j< y_len; j++) { 236 double x_value = *(double *)(x->data + i*x->strides[ 0]);237 double y_value = *(double *)(y->data + j*y->strides[ 1]);201 double x_value = *(double *)(x->data + i*x->strides[1]); 202 double y_value = *(double *)(y->data + j*y->strides[0]); 238 203 double *result_value = (double *)(result->data + 239 i*result->strides[ 0] + j*result->strides[1]);204 i*result->strides[1] + j*result->strides[0]); 240 205 *result_value = (*model)(x_value, y_value); 241 206 } -
sansmodels/src/sans/models/c_models/CTriaxialEllipsoidModel.cpp
r3c102d4 ra8d6888 195 195 196 196 if (PyArray_Check(x) && PyArray_Check(y)) { 197 x_len = dims[0]= x->dimensions[0]; 198 y_len = dims[1]= y->dimensions[1]; 197 198 x_len = dims[1]= x->dimensions[1]; 199 y_len = dims[0]= y->dimensions[0]; 199 200 200 201 // Make a new double matrix of same dims … … 209 210 for ( i=0; i< x_len; i++) { 210 211 for ( j=0; j< y_len; j++) { 211 double x_value = *(double *)(x->data + i*x->strides[ 0]);212 double y_value = *(double *)(y->data + j*y->strides[ 1]);212 double x_value = *(double *)(x->data + i*x->strides[1]); 213 double y_value = *(double *)(y->data + j*y->strides[0]); 213 214 double *result_value = (double *)(result->data + 214 i*result->strides[ 0] + j*result->strides[1]);215 i*result->strides[1] + j*result->strides[0]); 215 216 *result_value = (*model)(x_value, y_value); 216 217 } -
sansmodels/src/sans/models/c_models/CVesicleModel.cpp
r42f193a ra8d6888 22 22 * 23 23 */ 24 #define NO_IMPORT_ARRAY 25 #define PY_ARRAY_UNIQUE_SYMBOL PyArray_API_sans 24 26 25 27 extern "C" { 26 28 #include <Python.h> 29 #include <arrayobject.h> 27 30 #include "structmember.h" 28 31 #include <stdio.h> … … 135 138 } 136 139 } 137 140 /** 141 * Function to call to evaluate model 142 * @param args: input numpy array q[] 143 * @return: numpy array object 144 */ 145 146 static PyObject *evaluateOneDim(VesicleModel* model, PyArrayObject *q){ 147 PyArrayObject *result; 148 149 // Check validity of array q , q must be of dimension 1, an array of double 150 if (q->nd != 1 || q->descr->type_num != PyArray_DOUBLE) 151 { 152 //const char * message= "Invalid array: q->nd=%d,type_num=%d\n",q->nd,q->descr->type_num; 153 //PyErr_SetString(PyExc_ValueError , message); 154 return NULL; 155 } 156 result = (PyArrayObject *)PyArray_FromDims(q->nd, (int *)(q->dimensions), 157 PyArray_DOUBLE); 158 if (result == NULL) { 159 const char * message= "Could not create result "; 160 PyErr_SetString(PyExc_RuntimeError , message); 161 return NULL; 162 } 163 for (int i = 0; i < q->dimensions[0]; i++){ 164 double q_value = *(double *)(q->data + i*q->strides[0]); 165 double *result_value = (double *)(result->data + i*result->strides[0]); 166 *result_value =(*model)(q_value); 167 } 168 return PyArray_Return(result); 169 } 170 171 /** 172 * Function to call to evaluate model 173 * @param args: input numpy array [x[],y[]] 174 * @return: numpy array object 175 */ 176 static PyObject * evaluateTwoDimXY( VesicleModel* model, 177 PyArrayObject *x, PyArrayObject *y) 178 { 179 PyArrayObject *result; 180 int i,j, x_len, y_len, dims[2]; 181 //check validity of input vectors 182 if (x->nd != 2 || x->descr->type_num != PyArray_DOUBLE 183 || y->nd != 2 || y->descr->type_num != PyArray_DOUBLE 184 || y->dimensions[1] != x->dimensions[0]){ 185 const char * message= "evaluateTwoDimXY expect 2 numpy arrays"; 186 PyErr_SetString(PyExc_ValueError , message); 187 return NULL; 188 } 189 190 if (PyArray_Check(x) && PyArray_Check(y)) { 191 192 x_len = dims[1]= x->dimensions[1]; 193 y_len = dims[0]= y->dimensions[0]; 194 195 // Make a new double matrix of same dims 196 result=(PyArrayObject *) PyArray_FromDims(2,dims,NPY_DOUBLE); 197 if (result == NULL){ 198 const char * message= "Could not create result "; 199 PyErr_SetString(PyExc_RuntimeError , message); 200 return NULL; 201 } 202 203 /* Do the calculation. */ 204 for ( i=0; i< x_len; i++) { 205 for ( j=0; j< y_len; j++) { 206 double x_value = *(double *)(x->data + i*x->strides[1]); 207 double y_value = *(double *)(y->data + j*y->strides[0]); 208 double *result_value = (double *)(result->data + 209 i*result->strides[1] + j*result->strides[0]); 210 *result_value = (*model)(x_value, y_value); 211 } 212 } 213 return PyArray_Return(result); 214 215 }else{ 216 PyErr_SetString(CVesicleModelError, 217 "CVesicleModel.evaluateTwoDimXY couldn't run."); 218 return NULL; 219 } 220 } 221 /** 222 * evalDistribution function evaluate a model function with input vector 223 * @param args: input q as vector or [qx, qy] where qx, qy are vectors 224 * 225 */ 226 static PyObject * evalDistribution(CVesicleModel *self, PyObject *args){ 227 PyObject *qx, *qy; 228 PyArrayObject * pars; 229 int npars ,mpars; 230 231 // Get parameters 232 233 // Reader parameter dictionary 234 self->model->core_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "core_sld") ); 235 self->model->core_radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "core_radius") ); 236 self->model->thickness = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thickness") ); 237 self->model->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") ); 238 self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") ); 239 self->model->shell_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "shell_sld") ); 240 // Read in dispersion parameters 241 PyObject* disp_dict; 242 DispersionVisitor* visitor = new DispersionVisitor(); 243 disp_dict = PyDict_GetItemString(self->dispersion, "core_radius"); 244 self->model->core_radius.dispersion->accept_as_destination(visitor, self->model->core_radius.dispersion, disp_dict); 245 disp_dict = PyDict_GetItemString(self->dispersion, "thickness"); 246 self->model->thickness.dispersion->accept_as_destination(visitor, self->model->thickness.dispersion, disp_dict); 247 248 249 // Get input and determine whether we have to supply a 1D or 2D return value. 250 if ( !PyArg_ParseTuple(args,"O",&pars) ) { 251 PyErr_SetString(CVesicleModelError, 252 "CVesicleModel.evalDistribution expects a q value."); 253 return NULL; 254 } 255 // Check params 256 257 if(PyArray_Check(pars)==1) { 258 259 // Length of list should 1 or 2 260 npars = pars->nd; 261 if(npars==1) { 262 // input is a numpy array 263 if (PyArray_Check(pars)) { 264 return evaluateOneDim(self->model, (PyArrayObject*)pars); 265 } 266 }else{ 267 PyErr_SetString(CVesicleModelError, 268 "CVesicleModel.evalDistribution expect numpy array of one dimension."); 269 return NULL; 270 } 271 }else if( PyList_Check(pars)==1) { 272 // Length of list should be 2 for I(qx,qy) 273 mpars = PyList_GET_SIZE(pars); 274 if(mpars!=2) { 275 PyErr_SetString(CVesicleModelError, 276 "CVesicleModel.evalDistribution expects a list of dimension 2."); 277 return NULL; 278 } 279 qx = PyList_GET_ITEM(pars,0); 280 qy = PyList_GET_ITEM(pars,1); 281 if (PyArray_Check(qx) && PyArray_Check(qy)) { 282 return evaluateTwoDimXY(self->model, (PyArrayObject*)qx, 283 (PyArrayObject*)qy); 284 }else{ 285 PyErr_SetString(CVesicleModelError, 286 "CVesicleModel.evalDistribution expect 2 numpy arrays in list."); 287 return NULL; 288 } 289 }else{ 290 PyErr_SetString(CVesicleModelError, 291 "CVesicleModel.evalDistribution couln't be run."); 292 return NULL; 293 } 294 } 138 295 139 296 /** … … 305 462 {"runXY", (PyCFunction)runXY , METH_VARARGS, 306 463 "Evaluate the model at a given Q or Qx, Qy"}, 464 465 {"evalDistribution", (PyCFunction)evalDistribution , METH_VARARGS, 466 "Evaluate the model at a given Q or Qx, Qy vector "}, 307 467 {"reset", (PyCFunction)reset , METH_VARARGS, 308 468 "Reset pair correlation"}, … … 355 515 356 516 357 static PyMethodDef module_methods[] = {358 {NULL}359 };517 //static PyMethodDef module_methods[] = { 518 // {NULL} 519 //}; 360 520 361 521 /** -
sansmodels/src/sans/models/c_models/classTemplate.txt
r26e4a24 ra8d6888 170 170 171 171 if (PyArray_Check(x) && PyArray_Check(y)) { 172 x_len = dims[0]= x->dimensions[0]; 173 y_len = dims[1]= y->dimensions[1]; 172 173 x_len = dims[1]= x->dimensions[1]; 174 y_len = dims[0]= y->dimensions[0]; 174 175 175 176 // Make a new double matrix of same dims … … 184 185 for ( i=0; i< x_len; i++) { 185 186 for ( j=0; j< y_len; j++) { 186 double x_value = *(double *)(x->data + i*x->strides[ 0]);187 double y_value = *(double *)(y->data + j*y->strides[ 1]);187 double x_value = *(double *)(x->data + i*x->strides[1]); 188 double y_value = *(double *)(y->data + j*y->strides[0]); 188 189 double *result_value = (double *)(result->data + 189 i*result->strides[ 0] + j*result->strides[1]);190 i*result->strides[1] + j*result->strides[0]); 190 191 *result_value = (*model)(x_value, y_value); 191 192 }
Note: See TracChangeset
for help on using the changeset viewer.