Changeset 8709


Ignore:
Timestamp:
Feb 21, 2013, 1:41:43 PM (12 years ago)
Author:
wilsonp
Message:

Changes to fitsmooth.c to use PyCObject when Python version is detected as lower than 2.7.3. Also, small fixes to caching of quad tree.

Location:
trunk/anuga_core/source/anuga
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/anuga_core/source/anuga/caching/caching.py

    r8697 r8709  
    959959
    960960  if isinstance(T, FitInterpolate):
    961 
    962961    if hasattr(T,"D"):
    963962        T.D=sparse_matrix_ext.deserialise_dok(T.D)
     
    965964        T.AtA=sparse_matrix_ext.deserialise_dok(T.AtA)
    966965    if hasattr(T,"root"):
    967         if hasattr(T.root,"root"):
    968             mesh = T.mesh
    969             extents = AABB(*mesh.get_extent(absolute=True))
    970             extents.grow(1.001)  # To avoid round off error
    971             extents = [extents.xmin, extents.xmax, extents.ymin, extents.ymax]
    972             T.root.root=quad_tree_ext.deserialise(T.root.root,\
    973                               mesh.triangles, mesh.vertex_coordinates, num.array(extents))
    974 
     966        T.build_quad_tree(verbose=verbose)
    975967  #---------------------------------------------------------------------------
    976968
     
    10851077
    10861078  (argsfile, compressed) = myopen(CD+FN+'_'+file_types[1], 'wb', compression)
    1087 
    10881079  if argsfile is None:
    10891080    msg = 'ERROR (caching): Could not open argsfile for writing: %s' %FN
     
    11471138        T.AtA=sparse_matrix_ext.serialise_dok(T.AtA)
    11481139    if hasattr(T,"root"):
    1149         if hasattr(T.root,"root"):
    1150             T.root.root=quad_tree_ext.serialise(T.root.root)
     1140        T.root.root=None
    11511141
    11521142
     
    11891179  #else:
    11901180  #  pass  # FIXME: Take care of access rights under Windows
    1191 
    1192   # PADARN NOTE 17/12/12: See above - deserialise in case will be used.
    1193   #---------------------------------------------------------------------------
    1194   from anuga.fit_interpolate.general_fit_interpolate import FitInterpolate
    1195 
    1196   if isinstance(T, FitInterpolate):
    1197     if hasattr(T,"D"):
    1198         T.D=sparse_matrix_ext.deserialise_dok(T.D)
    1199     if hasattr(T,"AtA"):
    1200         T.AtA=sparse_matrix_ext.deserialise_dok(T.AtA)
    1201     if hasattr(T,"root"):
    1202         if hasattr(T.root,"root"):
    1203             mesh = T.mesh
    1204             extents = AABB(*mesh.get_extent(absolute=True))
    1205             extents.grow(1.001)  # To avoid round off error
    1206             extents = [extents.xmin, extents.xmax, extents.ymin, extents.ymax]
    1207             T.root.root=quad_tree_ext.deserialise(T.root.root,\
    1208                               mesh.triangles, mesh.vertex_coordinates, num.array(extents))
    1209 
    1210 
    1211   #---------------------------------------------------------------------------
    12121181
    12131182  return(savetime)
  • trunk/anuga_core/source/anuga/fit_interpolate/fitsmooth.c

    r8692 r8709  
    1717#define ERRCODE 2
    1818#define ERR(e) {printf("Error: %s\n", nc_strerror(e)); exit(ERRCODE);}
     19
     20#include "patchlevel.h"
     21
     22// PYVERSION273 used to check python version for use of PyCapsule
     23#if PY_MAJOR_VERSION>=2 && PY_MINOR_VERSION>=7 && PY_MICRO_VERSION>=3
     24    #define PYVERSION273
     25#endif
    1926
    2027//------------------------ NET CDF READING ----------------------------
     
    462469// ----------------------------------------------------------------------------
    463470
    464 // --------------------- Capsule destructor methods ---------------------------
    465 
    466 // PADARN NOTE 11/12/12: The following two 'destructor' functions will free the
    467 // memory allocated to a quad_tree or sparse_dok. An error should be thrown
    468 // if the pointer is already Null, as this shouldn't happen.
     471// If using python 2.7.3 or later, build with PyCapsules
    469472
    470473// Delete capsule containing a quad tree - name of capsule must be exactly
    471 // "quad tree".
     474// "quad tree".
     475
     476#ifdef PYVERSION273
     477
     478
    472479void delete_quad_tree_cap(PyObject * cap){
    473480    quad_tree * kill = (quad_tree*) PyCapsule_GetPointer(cap,"quad tree");
     
    488495
    489496}
     497
     498#else
     499
     500// If using python earlier version, build with PyCObject
     501
     502// Delete cobj containing a quad tree
     503void delete_quad_tree_cobj(void * cobj){
     504    quad_tree * kill = (quad_tree*) cobj;
     505    if(kill!=NULL){
     506        delete_quad_tree(kill);
     507    }
     508}
     509
     510// Delete cobj containing a sparse_dok
     511void delete_dok_cobj(void *cobj){
     512
     513    sparse_dok * kill = (sparse_dok*) cobj;
     514    if(kill!=NULL){
     515        delete_dok_matrix(kill);
     516    }
     517
     518}
     519
     520#endif
    490521
    491522//----------------------- PYTHON WRAPPER FUNCTION -----------------------------
     
    520551    int n = triangles->dimensions[0];
    521552
     553    #ifdef PYVERSION273
     554   
    522555    return  PyCapsule_New((void*) _build_quad_tree(n,
     556                          (long*) triangles -> data,
     557                          (double*) vertex_coordinates -> data,
     558                          (double*) extents -> data),
     559                          "quad tree",
     560                          &delete_quad_tree_cap);
     561
     562    #else
     563
     564    return  PyCObject_FromVoidPtr((void*) _build_quad_tree(n,
    523565                      (long*) triangles -> data,
    524566                      (double*) vertex_coordinates -> data,
    525567                      (double*) extents -> data),
    526                       "quad tree",
    527                       &delete_quad_tree_cap);
    528 
     568                      &delete_quad_tree_cobj);
     569    #endif
    529570}
    530571
     
    575616    }
    576617
     618    #ifdef PYVERSION273
     619
    577620    return  PyCapsule_New((void*) smoothing_mat,
    578621                  "sparse dok",
    579622                  &delete_dok_cap);
    580    
     623
     624    #else
     625
     626    return  PyCObject_FromVoidPtr((void*) smoothing_mat,
     627                  &delete_dok_cobj);
     628   
     629    #endif
    581630
    582631}
     
    612661    CHECK_C_CONTIG(triangles);
    613662
     663    #ifdef PYVERSION273
    614664    quad_tree * quadtree = (quad_tree*) PyCapsule_GetPointer(tree,"quad tree");
    615 
     665    #else
     666    quad_tree * quadtree = (quad_tree*) PyCObject_AsVoidPtr(tree);
     667    #endif
    616668
    617669    sparse_dok * dok_AtA; // Should be an input argument?
     
    633685    }
    634686
     687    #ifdef PYVERSION273
    635688    PyObject * AtA_cap =  PyCapsule_New((void*) dok_AtA,
    636689                  "sparse dok",
    637690                  &delete_dok_cap);
     691    #else
     692    PyObject * AtA_cap =  PyCObject_FromVoidPtr((void*) dok_AtA,
     693                  &delete_dok_cobj);
     694    #endif
     695
    638696    PyObject *Atz_ret = c_double_array_to_list(Atz,N);
    639697
     
    682740    CHECK_C_CONTIG(z);
    683741
     742    #ifdef PYVERSION273
    684743    quad_tree * quadtree = (quad_tree*) PyCapsule_GetPointer(tree,"quad tree");
    685 
     744    #else
     745    quad_tree * quadtree = (quad_tree*) PyCObject_AsVoidPtr(tree);
     746    #endif
    686747
    687748    sparse_dok * dok_AtA; // Should be an input argument?
     
    709770    }
    710771
     772    #ifdef PYVERSION273
    711773    PyObject * AtA_cap =  PyCapsule_New((void*) dok_AtA,
    712774                  "sparse dok",
    713                   &delete_dok_cap);
     775                  &delete_dok_cap);
     776    #else
     777    PyObject * AtA_cap =  PyCObject_FromVoidPtr((void*) dok_AtA,
     778                  &delete_dok_cobj);
     779    #endif
    714780    PyObject * Atz_ret = PyList_New(zdims);
    715781    for(i=0;i<zdims;i++){
     
    746812    CHECK_C_CONTIG(point);
    747813
     814    #ifdef PYVERSION273
    748815    quad_tree * quadtree = (quad_tree*) PyCapsule_GetPointer(tree,"quad tree");
     816    #else
     817    quad_tree * quadtree = (quad_tree*) PyCObject_AsVoidPtr(tree);
     818    #endif
     819
    749820    double xp,yp;
    750821    if(PyArray_TYPE(point)==7){
     
    804875
    805876    // Extract quad_tree pointer from capsule
     877    #ifdef PYVERSION273
    806878    quad_tree * quadtree = (quad_tree*) PyCapsule_GetPointer(tree,"quad tree");
     879    #else
     880    quad_tree * quadtree = (quad_tree*) PyCObject_AsVoidPtr(tree);
     881    #endif;
    807882   
    808883    // Return the number of elements in the tree (stored in struct)
     
    827902
    828903    // Extract quad_tree pointer from capsule
     904    #ifdef PYVERSION273
    829905    quad_tree * quadtree = (quad_tree*) PyCapsule_GetPointer(tree,"quad tree");
     906    #else
     907    quad_tree * quadtree = (quad_tree*) PyCObject_AsVoidPtr(tree);
     908    #endif
    830909   
    831910    // Return the number of elements in the tree (stored in struct)
     
    861940
    862941    // Get pointers to sparse_dok objects from capsules
     942    #ifdef PYVERSION273
    863943    sparse_dok * dok_AtA1 = (sparse_dok*) PyCapsule_GetPointer(AtA_cap1,"sparse dok");
    864944    sparse_dok * dok_AtA2 = (sparse_dok*) PyCapsule_GetPointer(AtA_cap2,"sparse dok");
     945    #else
     946    sparse_dok * dok_AtA1 = (sparse_dok*) PyCObject_AsVoidPtr(AtA_cap1);
     947    sparse_dok * dok_AtA2 = (sparse_dok*) PyCObject_AsVoidPtr(AtA_cap2);
     948    #endif
    865949
    866950    // Combine the partial AtA and Atz
     
    896980
    897981    // Get pointer to spars_dok struct
     982    #ifdef PYVERSION273
    898983    sparse_dok * D_mat = (sparse_dok*) PyCapsule_GetPointer(D_cap,"sparse dok");
    899    
     984    #else
     985    sparse_dok * D_mat = (sparse_dok*) PyCObject_AsVoidPtr(D_cap);
     986    #endif
     987
    900988    // Check to make sure that the specified size of the matrix is at least as big
    901989    // as the entries stored in the sparse_dok.
     
    9551043
    9561044    // Extract pointers to c structs from capsules
     1045    #ifdef PYVERSION273
    9571046    sparse_dok * smoothing_mat = (sparse_dok*) PyCapsule_GetPointer(smoothing_mat_cap,"sparse dok");
    9581047    sparse_dok * dok_AtA = (sparse_dok*) PyCapsule_GetPointer(AtA_cap,"sparse dok");
     1048    #else
     1049    sparse_dok * smoothing_mat = (sparse_dok*) PyCObject_AsVoidPtr(smoothing_mat_cap);
     1050    sparse_dok * dok_AtA = (sparse_dok*) PyCObject_AsVoidPtr(AtA_cap);
     1051    #endif
    9591052
    9601053    // Add two sparse_dok matrices
  • trunk/anuga_core/source/anuga/fit_interpolate/general_fit_interpolate.py

    r8690 r8709  
    118118            #self.root.set_last_triangle()
    119119
     120    def build_quad_tree(self,verbose=False):
     121        self.root = MeshQuadtree(self.mesh, verbose=verbose)
     122
    120123    def __repr__(self):
    121124        return 'Interpolation object based on: ' + repr(self.mesh)
  • trunk/anuga_core/source/anuga/fit_interpolate/test_interpolate.py

    r8690 r8709  
    259259        # FIXME: This concat should roll into get_vertex_values
    260260
    261 
    262261        # Get interpolated values at centroids
    263262        interpolation_points = domain.get_centroid_coordinates()
     
    268267                             verbose=False)
    269268        assert num.allclose(result, answer)               
    270        
     269
    271270        # Second call using the cache
    272271        result = interpolate(vertex_coordinates, triangles,
  • trunk/anuga_core/source/anuga/operators/test_kinematic_viscosity_operator.py

    r8690 r8709  
    770770        kv.parabolic_solve(v, v, h, u_out=v, update_matrix=False, iprint=1, use_dt_tol=False)
    771771
    772 
    773         #print 'u'
    774         #print u.centroid_values
    775         #print u.boundary_values
    776 
    777         #print num.where(h.centroid_values > 0.0, 1.0, 0.0)
    778772        assert num.allclose(u.centroid_values, num.where(h.centroid_values > 0.0, 1.0, 0.0), rtol=1.0e-1)
    779773        assert num.allclose(u.boundary_values, num.ones_like(u.boundary_values))
  • trunk/anuga_core/source/anuga/utilities/quad_tree_ext.c

    r8691 r8709  
    1313
    1414
    15 
     15/*
    1616static int _serialise(quad_tree * quadtree, PyObject * serial_quadtree)
    1717{
     
    163163}
    164164
    165 
     165*/
    166166
    167167static void delete_quad_tree_cap(PyObject * cap){
     
    175175//----------------------- PYTHON WRAPPER FUNCTION -----------------------------
    176176
    177 
     177/*
    178178static PyObject *serialise(PyObject *self, PyObject *args) {
    179179
     
    236236
    237237}
    238 
     238*/
    239239
    240240//------------------------------------------------------------------------------
     
    249249// Method table for python module
    250250static struct PyMethodDef MethodTable[] = {
    251     {"serialise",serialise, METH_VARARGS, "Print out"},
    252     {"deserialise",deserialise, METH_VARARGS, "Print out"},
     251  //  {"serialise",serialise, METH_VARARGS, "Print out"},
     252  //  {"deserialise",deserialise, METH_VARARGS, "Print out"},
    253253        {NULL, NULL, 0, NULL}   // sentinel
    254254};
Note: See TracChangeset for help on using the changeset viewer.