Changeset 752


Ignore:
Timestamp:
Jan 20, 2005, 1:03:56 PM (20 years ago)
Author:
ole
Message:

Established common idion for obtaining Numeric arrays from Python objects
within c-extensions. This would help migration to 64 bit.

Location:
inundation/ga/storm_surge/pyvolution
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • inundation/ga/storm_surge/pyvolution/quantity_ext.c

    r587 r752  
    2323                        double* centroids,
    2424                        double* centroid_values,
    25                         int* number_of_boundaries,
    26                         int* surrogate_neighbours,
     25                        long* number_of_boundaries,
     26                        long* surrogate_neighbours,
    2727                        double* a,
    2828                        double* b) {
     
    3131  double x0, x1, x2, y0, y1, y2, q0, q1, q2, det;
    3232 
    33  
     33
    3434  for (k=0; k<N; k++) {
    3535    index3 = 3*k;
     
    4444      k1 = surrogate_neighbours[index3 + 1];
    4545      k2 = surrogate_neighbours[index3 + 2];           
     46
     47
    4648      if (k0 == k1 || k1 == k2) return -1;     
    4749
     
    217219    return NULL;
    218220
    219   centroid_values = (PyArrayObject*)
    220     PyObject_GetAttrString(quantity, "centroid_values");           
    221   if (!centroid_values) return NULL;               
    222 
    223   explicit_update = (PyArrayObject*)
    224     PyObject_GetAttrString(quantity, "explicit_update");   
    225   if (!explicit_update) return NULL;         
    226  
    227   semi_implicit_update = (PyArrayObject*)
    228     PyObject_GetAttrString(quantity, "semi_implicit_update");   
    229   if (!semi_implicit_update) return NULL;           
     221  centroid_values = get_consecutive_array(quantity, "centroid_values");
     222  explicit_update = get_consecutive_array(quantity, "explicit_update");   
     223  semi_implicit_update = get_consecutive_array(quantity, "semi_implicit_update");     
     224 
     225 
     226  //centroid_values = (PyArrayObject*)
     227  //  PyObject_GetAttrString(quantity, "centroid_values");           
     228  //if (!centroid_values) return NULL;               
     229
     230  //explicit_update = (PyArrayObject*)
     231  //  PyObject_GetAttrString(quantity, "explicit_update");   
     232  //if (!explicit_update) return NULL;         
     233 
     234  //semi_implicit_update = (PyArrayObject*)
     235  //  PyObject_GetAttrString(quantity, "semi_implicit_update");   
     236  //if (!semi_implicit_update) return NULL;           
    230237
    231238  N = centroid_values -> dimensions[0]; 
     
    264271    return NULL;
    265272
     273  vertex_values = get_consecutive_array(quantity, "vertex_values");
     274  edge_values = get_consecutive_array(quantity, "edge_values"); 
     275 
     276  /* 
    266277  vertex_values = (PyArrayObject*)
    267278    PyObject_GetAttrString(quantity, "vertex_values");           
     
    271282    PyObject_GetAttrString(quantity, "edge_values");   
    272283  if (!edge_values) return NULL;         
     284  */
    273285
    274286  N = vertex_values -> dimensions[0]; 
     
    312324
    313325  //Get pertinent variables
    314   centroids = (PyArrayObject*)
    315     PyObject_GetAttrString(domain, "centroid_coordinates");
    316   if (!centroids) return NULL;   
    317  
     326
     327  centroids = get_consecutive_array(domain, "centroid_coordinates");
     328  centroid_values = get_consecutive_array(quantity, "centroid_values");
     329  surrogate_neighbours = get_consecutive_array(domain, "surrogate_neighbours");
     330  number_of_boundaries = get_consecutive_array(domain, "number_of_boundaries");
     331
     332  //centroids = (PyArrayObject*)
     333  //  PyArray_ContiguousFromObject(
     334  //                             PyObject_GetAttrString(domain, "centroid_coordinates"),
     335  //                             PyArray_DOUBLE, 0, 0);
     336    //  if (!centroids) return NULL;   
     337 
     338  /*
    318339  centroid_values = (PyArrayObject*)
    319     PyObject_GetAttrString(quantity, "centroid_values");   
    320   if (!centroid_values) return NULL;       
    321  
     340    PyArray_ContiguousFromObject(
     341                                 PyObject_GetAttrString(quantity,
     342                                                        "centroid_values"),
     343                                 PyArray_DOUBLE, 0, 0);
     344  if (!centroid_values) return NULL;       
     345  */
     346 
     347  /*
    322348  surrogate_neighbours = (PyArrayObject*)
    323     PyObject_GetAttrString(domain, "surrogate_neighbours");
     349    PyArray_ContiguousFromObject(
     350                                 PyObject_GetAttrString(domain, "surrogate_neighbours"),
     351                                 PyArray_LONG, 0, 0);                           
    324352  if (!surrogate_neighbours) return NULL;       
    325353 
    326354  number_of_boundaries = (PyArrayObject*)
    327     PyObject_GetAttrString(domain, "number_of_boundaries");           
     355    PyArray_ContiguousFromObject(
     356                                 PyObject_GetAttrString(domain, "number_of_boundaries"),
     357                                 PyArray_LONG, 0, 0);                           
    328358  if (!number_of_boundaries) return NULL;           
     359
     360  */
    329361 
    330362  N = centroid_values -> dimensions[0];
     
    343375                           (double*) centroids -> data,
    344376                           (double*) centroid_values -> data,
    345                            (int*) number_of_boundaries -> data,
    346                            (int*) surrogate_neighbours -> data,
     377                           (long*) number_of_boundaries -> data,
     378                           (long*) surrogate_neighbours -> data,
    347379                           (double*) a -> data,
    348380                           (double*) b -> data);     
     
    393425
    394426  //Get pertinent variables
     427  centroids = get_consecutive_array(domain, "centroid_coordinates"); 
     428  centroid_values = get_consecutive_array(quantity, "centroid_values");
     429  surrogate_neighbours = get_consecutive_array(domain, "surrogate_neighbours");
     430  number_of_boundaries = get_consecutive_array(domain, "number_of_boundaries");
     431  vertex_coordinates = get_consecutive_array(domain, "vertex_coordinates");   
     432  vertex_values = get_consecutive_array(quantity, "vertex_values");     
     433 
     434 
     435  /*
    395436  centroids = (PyArrayObject*)
    396     PyObject_GetAttrString(domain, "centroid_coordinates");
     437    PyArray_ContiguousFromObject(
     438                                 PyObject_GetAttrString(domain, "centroid_coordinates"),
     439                                 PyArray_DOUBLE, 0, 0);
    397440  if (!centroids) return NULL;   
    398441 
    399   centroid_values = (PyArrayObject*)
    400     PyObject_GetAttrString(quantity, "centroid_values");   
     442 
     443  centroid_values = (PyArrayObject*)
     444    PyArray_ContiguousFromObject(
     445                                 PyObject_GetAttrString(quantity, "centroid_values"),
     446                                 PyArray_DOUBLE, 0, 0);
    401447  if (!centroid_values) return NULL;       
    402448 
    403449  surrogate_neighbours = (PyArrayObject*)
    404     PyObject_GetAttrString(domain, "surrogate_neighbours");
     450    PyArray_ContiguousFromObject(
     451                                 PyObject_GetAttrString(domain, "surrogate_neighbours"),
     452                                 PyArray_LONG, 0, 0);
    405453  if (!surrogate_neighbours) return NULL;       
    406454 
    407455  number_of_boundaries = (PyArrayObject*)
    408     PyObject_GetAttrString(domain, "number_of_boundaries");           
     456    PyArray_ContiguousFromObject(
     457                                 PyObject_GetAttrString(domain, "number_of_boundaries"),
     458                                 PyArray_LONG, 0, 0);
    409459  if (!number_of_boundaries) return NULL;           
    410460 
    411461  vertex_coordinates = (PyArrayObject*)
    412     PyObject_GetAttrString(domain, "vertex_coordinates");           
     462    PyArray_ContiguousFromObject(
     463                                 PyObject_GetAttrString(domain, "vertex_coordinates"),
     464                                 PyArray_DOUBLE, 0, 0);
    413465  if (!vertex_coordinates) return NULL;             
    414466 
    415   vertex_values = (PyArrayObject*)
    416     PyObject_GetAttrString(quantity, "vertex_values");           
     467  vertex_values = (PyArrayObject*)
     468    PyArray_ContiguousFromObject( 
     469                                 PyObject_GetAttrString(quantity, "vertex_values"),
     470                                 PyArray_DOUBLE, 0, 0);
    417471  if (!vertex_values) return NULL;               
    418 
    419  
    420   /*
    421   printf("In extrapolate C routine\n"); 
    422   printf("d0=%d, d1=%d\n",
    423          vertex_values -> dimensions[0],
    424          vertex_values -> dimensions[1]);
    425472  */
    426473 
    427   vertex_values = (PyArrayObject*)
    428     PyObject_GetAttrString(quantity, "vertex_values");           
    429   if (!vertex_values) return NULL;               
     474
     475 
    430476 
    431477  N = centroid_values -> dimensions[0];
     
    450496                           (double*) centroids -> data,
    451497                           (double*) centroid_values -> data,
    452                            (int*) number_of_boundaries -> data,
    453                            (int*) surrogate_neighbours -> data,
     498                           (long*) number_of_boundaries -> data,
     499                           (long*) surrogate_neighbours -> data,
    454500                           (double*) a -> data,
    455501                           (double*) b -> data);
     
    515561    return NULL; 
    516562   
    517   neighbours = (PyArrayObject*) PyObject_GetAttrString(domain, "neighbours");
     563  //neighbours = (PyArrayObject*) PyObject_GetAttrString(domain, "neighbours");
     564  neighbours = get_consecutive_array(domain, "neighbours");
    518565
    519566  //Get safety factor beta
     
    527574  Py_DECREF(domain);     
    528575
    529   qc = (PyArrayObject*) PyObject_GetAttrString(quantity, "centroid_values");
    530   qv = (PyArrayObject*) PyObject_GetAttrString(quantity, "vertex_values");   
     576  //qc = (PyArrayObject*) PyObject_GetAttrString(quantity, "centroid_values");
     577  //qv = (PyArrayObject*) PyObject_GetAttrString(quantity, "vertex_values");   
     578  qc = get_consecutive_array(quantity, "centroid_values");
     579  qv = get_consecutive_array(quantity, "vertex_values");   
     580   
     581 
    531582  N = qc -> dimensions[0];
    532583   
  • inundation/ga/storm_surge/pyvolution/util_ext.h

    r265 r752  
    6969  printf("\n"); 
    7070}
     71
     72PyArrayObject *get_consecutive_array(PyObject *O, char *name) {
     73  PyArrayObject *A, *B;
     74 
     75
     76  //Get array object from attribute
     77 
     78  /*
     79  //FIXME: THE TEST DOESN't WORK
     80  printf("Err = %d\n", PyObject_HasAttrString(O, name));
     81  if (PyObject_HasAttrString(O, name) == 1) {
     82    B = (PyArrayObject*) PyObject_GetAttrString(O, name);
     83    if (!B) return NULL;
     84  } else {
     85    return NULL;
     86    }
     87  */
     88   
     89  B = (PyArrayObject*) PyObject_GetAttrString(O, name);
     90  if (!B) return NULL;     
     91 
     92  //Convert to consecutive array
     93  A = (PyArrayObject*) PyArray_ContiguousFromObject((PyObject*) B, B -> descr -> type, 0, 0);
     94 
     95  //Py_DECREF(B); //FIXME: Is this really needed??
     96 
     97  if (!A) return NULL;
     98  return A;
     99}
     100
Note: See TracChangeset for help on using the changeset viewer.