Ignore:
Timestamp:
Oct 10, 2006, 3:02:21 PM (18 years ago)
Author:
ole
Message:

Added proper error messages to return NULL in c extensions.
Fixed up some indentation issues as well

Location:
anuga_core/source/anuga
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • anuga_core/source/anuga/abstract_2d_finite_volumes/quantity_ext.c

    r3719 r3730  
    230230
    231231        // Convert Python arguments to C
    232         if (!PyArg_ParseTuple(args, "Od", &quantity, &timestep))
    233                 return NULL;
     232        if (!PyArg_ParseTuple(args, "Od", &quantity, &timestep)) {
     233          PyErr_SetString(PyExc_RuntimeError,
     234                          "quantity_ext.c: update could not parse input");
     235          return NULL;
     236        }
    234237
    235238        centroid_values = get_consecutive_array(quantity, "centroid_values");
     
    240243
    241244        err = _update(N, timestep,
    242                         (double*) centroid_values -> data,
    243                         (double*) explicit_update -> data,
    244                         (double*) semi_implicit_update -> data);
     245                      (double*) centroid_values -> data,
     246                      (double*) explicit_update -> data,
     247                      (double*) semi_implicit_update -> data);
    245248
    246249
    247250        if (err != 0) {
    248                 PyErr_SetString(PyExc_RuntimeError,
    249                         "Zero division in semi implicit update - call Stephen :)");
    250                 return NULL;
     251          PyErr_SetString(PyExc_RuntimeError,
     252                          "Zero division in semi implicit update - call Stephen :)");
     253          return NULL;
    251254        }
    252255
     
    268271
    269272        // Convert Python arguments to C
    270         if (!PyArg_ParseTuple(args, "O", &quantity))
    271                 return NULL;
    272 
     273        if (!PyArg_ParseTuple(args, "O", &quantity)) {
     274          PyErr_SetString(PyExc_RuntimeError,
     275                          "quantity_ext.c: interpolate_from_vertices_to_edges could not parse input");
     276          return NULL;
     277        }
     278       
    273279        vertex_values = get_consecutive_array(quantity, "vertex_values");
    274280        edge_values = get_consecutive_array(quantity, "edge_values");
     
    281287
    282288        if (err != 0) {
    283                 PyErr_SetString(PyExc_RuntimeError, "Interpolate could not be computed");
    284                 return NULL;
     289          PyErr_SetString(PyExc_RuntimeError,
     290                          "Interpolate could not be computed");
     291          return NULL;
    285292        }
    286293
     
    306313
    307314        // Convert Python arguments to C
    308         if (!PyArg_ParseTuple(args, "O", &quantity))
    309                 return NULL;
     315        if (!PyArg_ParseTuple(args, "O", &quantity)) {
     316          PyErr_SetString(PyExc_RuntimeError,
     317                          "quantity_ext.c: compute_gradients could not parse input");   
     318          return NULL;
     319        }
    310320
    311321        domain = PyObject_GetAttrString(quantity, "domain");
    312         if (!domain)
    313                 return NULL;
     322        if (!domain) {
     323          PyErr_SetString(PyExc_RuntimeError,
     324                          "compute_gradients could not obtain domain object from quantity");
     325          return NULL;
     326        }
    314327
    315328        //Get pertinent variables
     
    341354
    342355        if (err != 0) {
    343                 PyErr_SetString(PyExc_RuntimeError, "Gradient could not be computed");
    344                 return NULL;
     356          PyErr_SetString(PyExc_RuntimeError, "Gradient could not be computed");
     357          return NULL;
    345358        }
    346359
     
    377390
    378391        // Convert Python arguments to C
    379         if (!PyArg_ParseTuple(args, "O", &quantity))
    380                 return NULL;
     392        if (!PyArg_ParseTuple(args, "O", &quantity)) {
     393          PyErr_SetString(PyExc_RuntimeError,
     394                          "extrapolate_second_order could not parse input");   
     395          return NULL;
     396        }
    381397
    382398        domain = PyObject_GetAttrString(quantity, "domain");
    383         if (!domain)
    384                 return NULL;
     399        if (!domain) {
     400          PyErr_SetString(PyExc_RuntimeError,
     401                          "extrapolate_second_order could not obtain domain object from quantity");     
     402          return NULL;
     403        }
    385404
    386405        //Get pertinent variables
     
    419438
    420439        if (err != 0) {
    421                 PyErr_SetString(PyExc_RuntimeError, "Gradient could not be computed");
    422                 return NULL;
     440          PyErr_SetString(PyExc_RuntimeError, "Gradient could not be computed");
     441          return NULL;
    423442        }
    424443
     
    433452
    434453        if (err != 0) {
    435                 PyErr_SetString(PyExc_RuntimeError,
    436                         "Internal function _extrapolate failed");
    437                 return NULL;
     454          PyErr_SetString(PyExc_RuntimeError,
     455                          "Internal function _extrapolate failed");
     456          return NULL;
    438457        }
    439458
     
    468487
    469488        // Convert Python arguments to C
    470         if (!PyArg_ParseTuple(args, "O", &quantity))
    471                 return NULL;
     489        if (!PyArg_ParseTuple(args, "O", &quantity)) {
     490          PyErr_SetString(PyExc_RuntimeError,
     491                          "quantity_ext.c: limit could not parse input");
     492          return NULL;
     493        }
    472494
    473495        domain = PyObject_GetAttrString(quantity, "domain");
    474         if (!domain)
    475                 return NULL;
     496        if (!domain) {
     497          PyErr_SetString(PyExc_RuntimeError,
     498                          "quantity_ext.c: limit could not obtain domain object from quantity");                       
     499         
     500          return NULL;
     501        }
    476502
    477503        //neighbours = (PyArrayObject*) PyObject_GetAttrString(domain, "neighbours");
     
    480506        //Get safety factor beta_w
    481507        Tmp = PyObject_GetAttrString(domain, "beta_w");
    482         if (!Tmp)
    483                 return NULL;
     508        if (!Tmp) {
     509          PyErr_SetString(PyExc_RuntimeError,
     510                          "quantity_ext.c: limit could not obtain beta_w object from domain");                 
     511         
     512          return NULL;
     513        }       
    484514
    485515        beta_w = PyFloat_AsDouble(Tmp);
  • anuga_core/source/anuga/shallow_water/shallow_water_ext.c

    r3719 r3730  
    616616  if (!PyArg_ParseTuple(args, "dOOOOO",
    617617                        &g, &h, &v, &x,
    618                         &xmom, &ymom))
    619     return NULL;
     618                        &xmom, &ymom)) {
     619    PyErr_SetString(PyExc_RuntimeError, "shallow_water_ext.c: gravity could not parse input arguments");
     620    return NULL;
     621  }
    620622
    621623  N = h -> dimensions[0];
     
    667669  if (!PyArg_ParseTuple(args, "ddOOOOOOO",
    668670                        &g, &eps, &w, &z, &uh, &vh, &eta,
    669                         &xmom, &ymom))
    670     return NULL;
     671                        &xmom, &ymom)) {
     672    PyErr_SetString(PyExc_RuntimeError, "shallow_water_ext.c: manning_friction could not parse input arguments");
     673    return NULL;
     674  }
     675
    671676
    672677  N = w -> dimensions[0];
     
    788793  //get the safety factor beta_w, set in the config.py file. This is used in the limiting process
    789794  Tmp = PyObject_GetAttrString(domain, "beta_w");
    790   if (!Tmp)
    791     return NULL;
     795  if (!Tmp) {
     796    PyErr_SetString(PyExc_RuntimeError, "shallow_water_ext.c: extrapolate_second_order_sw could not obtain object beta_w from domain");
     797    return NULL;
     798  } 
    792799  beta_w = PyFloat_AsDouble(Tmp);
    793800  Py_DECREF(Tmp);
    794801 
    795802  Tmp = PyObject_GetAttrString(domain, "beta_w_dry");
    796   if (!Tmp)
    797     return NULL;
     803  if (!Tmp) {
     804    PyErr_SetString(PyExc_RuntimeError, "shallow_water_ext.c: extrapolate_second_order_sw could not obtain object beta_w_dry from domain");
     805    return NULL;
     806  } 
    798807  beta_w_dry = PyFloat_AsDouble(Tmp);
    799808  Py_DECREF(Tmp);
    800809 
    801810  Tmp = PyObject_GetAttrString(domain, "beta_uh");
    802   if (!Tmp)
    803     return NULL;
     811  if (!Tmp) {
     812    PyErr_SetString(PyExc_RuntimeError, "shallow_water_ext.c: extrapolate_second_order_sw could not obtain object beta_uh from domain");
     813    return NULL;
     814  } 
    804815  beta_uh = PyFloat_AsDouble(Tmp);
    805816  Py_DECREF(Tmp);
    806817 
    807818  Tmp = PyObject_GetAttrString(domain, "beta_uh_dry");
    808   if (!Tmp)
    809     return NULL;
     819  if (!Tmp) {
     820    PyErr_SetString(PyExc_RuntimeError, "shallow_water_ext.c: extrapolate_second_order_sw could not obtain object beta_uh_dry from domain");
     821    return NULL;
     822  } 
    810823  beta_uh_dry = PyFloat_AsDouble(Tmp);
    811824  Py_DECREF(Tmp);
    812825
    813826  Tmp = PyObject_GetAttrString(domain, "beta_vh");
    814   if (!Tmp)
    815     return NULL;
     827  if (!Tmp) {
     828    PyErr_SetString(PyExc_RuntimeError, "shallow_water_ext.c: extrapolate_second_order_sw could not obtain object beta_vh from domain");
     829    return NULL;
     830  } 
    816831  beta_vh = PyFloat_AsDouble(Tmp);
    817832  Py_DECREF(Tmp);
    818833 
    819834  Tmp = PyObject_GetAttrString(domain, "beta_vh_dry");
    820   if (!Tmp)
    821     return NULL;
     835  if (!Tmp) {
     836    PyErr_SetString(PyExc_RuntimeError, "shallow_water_ext.c: extrapolate_second_order_sw could not obtain object beta_vh_dry from domain");
     837    return NULL;
     838  } 
    822839  beta_vh_dry = PyFloat_AsDouble(Tmp);
    823840  Py_DECREF(Tmp);
    824841 
    825842  Tmp = PyObject_GetAttrString(domain, "minimum_allowed_height");
    826   if (!Tmp)
    827     return NULL;
     843  if (!Tmp) {
     844    PyErr_SetString(PyExc_RuntimeError, "shallow_water_ext.c: extrapolate_second_order_sw could not obtain object minimum_allowed_heigt");
     845    return NULL;
     846  } 
    828847  minimum_allowed_height = PyFloat_AsDouble(Tmp);
    829848  Py_DECREF(Tmp); 
     
    881900      area2 = dy2*dx1 - dy1*dx2;//the triangle is guaranteed to be counter-clockwise
    882901      //If the mesh is 'weird' near the boundary, the trianlge might be flat or clockwise:
    883       if (area2<=0)
    884                 return NULL;
    885 
    886           //### Calculate heights of neighbouring cells
    887           hc = ((double *)stage_centroid_values->data)[k]  - ((double *)elevation_centroid_values->data)[k];
    888           h0 = ((double *)stage_centroid_values->data)[k0] - ((double *)elevation_centroid_values->data)[k0];
    889           h1 = ((double *)stage_centroid_values->data)[k1] - ((double *)elevation_centroid_values->data)[k1];
    890           h2 = ((double *)stage_centroid_values->data)[k2] - ((double *)elevation_centroid_values->data)[k2];
    891           hmin = min(hc,min(h0,min(h1,h2)));
     902      if (area2<=0) {
     903        PyErr_SetString(PyExc_RuntimeError, "shallow_water_ext.c: negative triangle area encountered");
     904        return NULL;
     905      } 
     906     
     907
     908      //### Calculate heights of neighbouring cells
     909      hc = ((double *)stage_centroid_values->data)[k]  - ((double *)elevation_centroid_values->data)[k];
     910      h0 = ((double *)stage_centroid_values->data)[k0] - ((double *)elevation_centroid_values->data)[k0];
     911      h1 = ((double *)stage_centroid_values->data)[k1] - ((double *)elevation_centroid_values->data)[k1];
     912      h2 = ((double *)stage_centroid_values->data)[k2] - ((double *)elevation_centroid_values->data)[k2];
     913      hmin = min(hc,min(h0,min(h1,h2)));
     914     
    892915      //### stage ###
    893916      //calculate the difference between vertex 0 of the auxiliary triangle and the FV triangle centroid
     
    908931      //and compute jumps from the centroid to the min and max
    909932      find_qmin_and_qmax(dq0,dq1,dq2,&qmin,&qmax);
    910           // Playing with dry wet interface
    911           hmin = qmin;
    912           beta_tmp = beta_w;
    913           if (hmin<minimum_allowed_height)
    914                 beta_tmp = beta_w_dry;
     933      // Playing with dry wet interface
     934      hmin = qmin;
     935      beta_tmp = beta_w;
     936      if (hmin<minimum_allowed_height)
     937        beta_tmp = beta_w_dry;
    915938      limit_gradient(dqv,qmin,qmax,beta_tmp);//the gradient will be limited
    916939      for (i=0;i<3;i++)
    917940        ((double *)stage_vertex_values->data)[k3+i]=((double *)stage_centroid_values->data)[k]+dqv[i];
    918 
     941     
    919942      //### xmom ###
    920943      //calculate the difference between vertex 0 of the auxiliary triangle and the FV triangle centroid
     
    935958      //and compute jumps from the centroid to the min and max
    936959      find_qmin_and_qmax(dq0,dq1,dq2,&qmin,&qmax);
    937           beta_tmp = beta_uh;
    938           if (hmin<minimum_allowed_height)
    939                 beta_tmp = beta_uh_dry;
     960      beta_tmp = beta_uh;
     961      if (hmin<minimum_allowed_height)
     962        beta_tmp = beta_uh_dry;
    940963      limit_gradient(dqv,qmin,qmax,beta_tmp);//the gradient will be limited
    941964      for (i=0;i<3;i++)
    942965        ((double *)xmom_vertex_values->data)[k3+i]=((double *)xmom_centroid_values->data)[k]+dqv[i];
    943 
     966     
    944967      //### ymom ###
    945968      //calculate the difference between vertex 0 of the auxiliary triangle and the FV triangle centroid
     
    960983      //and compute jumps from the centroid to the min and max
    961984      find_qmin_and_qmax(dq0,dq1,dq2,&qmin,&qmax);
    962           beta_tmp = beta_vh;
    963           if (hmin<minimum_allowed_height)
    964                 beta_tmp = beta_vh_dry;
     985      beta_tmp = beta_vh;
     986      if (hmin<minimum_allowed_height)
     987        beta_tmp = beta_vh_dry;
    965988      limit_gradient(dqv,qmin,qmax,beta_tmp);//the gradient will be limited
    966989      for (i=0;i<3;i++)
     
    974997          break;
    975998      }
    976       if ((k2==k3+3))//if we didn't find an internal neighbour
     999      if ((k2==k3+3)) {//if we didn't find an internal neighbour
     1000        PyErr_SetString(PyExc_RuntimeError, "shallow_water_ext.c: Internal neighbour not found");     
    9771001        return NULL;//error
     1002      }
     1003     
    9781004      k1=((long *)surrogate_neighbours->data)[k2];
    9791005      //the coordinates of the triangle are already (x,y). Get centroid of the neighbour (x1,y1)
     
    9901016      dy2=dx2*dy1;
    9911017      dx2*=dx1;
    992 
     1018     
    9931019      //## stage ###
    9941020      //compute differentials
     
    10101036        qmax=0.0;
    10111037      }
     1038     
     1039     
    10121040      limit_gradient(dqv,qmin,qmax,beta_w);//the gradient will be limited
    10131041      for (i=0;i<3;i++)
    10141042        ((double *)stage_vertex_values->data)[k3+i]=((double *)stage_centroid_values->data)[k]+dqv[i];
    1015 
     1043     
    10161044      //## xmom ###
    10171045      //compute differentials
     
    10361064      for (i=0;i<3;i++)
    10371065        ((double *)xmom_vertex_values->data)[k3+i]=((double *)xmom_centroid_values->data)[k]+dqv[i];
    1038 
     1066     
    10391067      //## ymom ###
    10401068      //compute differentials
     
    10641092}//extrapolate_second-order_sw
    10651093
     1094
    10661095PyObject *rotate(PyObject *self, PyObject *args, PyObject *kwargs) {
    10671096  //
     
    10811110  // Convert Python arguments to C
    10821111  if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO|i", argnames,
    1083                                    &Q, &Normal, &direction))
    1084     return NULL;
     1112                                   &Q, &Normal, &direction)) {
     1113    PyErr_SetString(PyExc_RuntimeError, "shallow_water_ext.c: rotate could not parse input arguments");
     1114    return NULL;
     1115  } 
    10851116
    10861117  //Input checks (convert sequences into numeric arrays)
     
    14981529                        &maximum_allowed_speed,
    14991530                        &epsilon,
    1500                         &wc, &zc, &xmomc, &ymomc))
    1501     return NULL;
     1531                        &wc, &zc, &xmomc, &ymomc)) {
     1532    PyErr_SetString(PyExc_RuntimeError, "shallow_water_ext.c: protect could not parse input arguments");
     1533    return NULL;
     1534  } 
    15021535
    15031536  N = wc -> dimensions[0];
     
    15421575                        &wc, &zc, &hc,
    15431576                        &wv, &zv, &hv, &hvbar,
    1544                         &xmomc, &ymomc, &xmomv, &ymomv))
    1545     return NULL;
     1577                        &xmomc, &ymomc, &xmomv, &ymomv)) {
     1578    PyErr_SetString(PyExc_RuntimeError, "shallow_water_ext.c: balance_deep_and_shallow could not parse input arguments");
     1579    return NULL;
     1580  } 
     1581         
    15461582
    15471583  N = wc -> dimensions[0];
     
    15801616
    15811617  // Convert Python arguments to C
    1582   if (!PyArg_ParseTuple(args, "OOO", &domain, &hc, &hv))
    1583     return NULL;
    1584 
     1618  if (!PyArg_ParseTuple(args, "OOO", &domain, &hc, &hv)) {
     1619    PyErr_SetString(PyExc_RuntimeError, "shallow_water_ext.c: h_limiter could not parse input arguments");
     1620    return NULL;
     1621  } 
     1622 
    15851623  neighbours = get_consecutive_array(domain, "neighbours");
    15861624
    15871625  //Get safety factor beta_h
    15881626  Tmp = PyObject_GetAttrString(domain, "beta_h");
    1589   if (!Tmp)
    1590     return NULL;
    1591 
     1627  if (!Tmp) {
     1628    PyErr_SetString(PyExc_RuntimeError, "shallow_water_ext.c: h_limiter could not obtain object beta_h from domain");
     1629    return NULL;
     1630  } 
    15921631  beta_h = PyFloat_AsDouble(Tmp);
    15931632
     
    16501689  double beta_h; //Safety factor (see config.py)
    16511690  double hmin, hmax, dh[3];
    1652   // Convert Python arguments to C
    1653   if (!PyArg_ParseTuple(args, "OOO", &domain, &hc, &hv))
    1654     return NULL;
     1691// Convert Python arguments to C
     1692  if (!PyArg_ParseTuple(args, "OOO", &domain, &hc, &hv)) {
     1693    PyErr_SetString(PyExc_RuntimeError, "shallow_water_ext.c: h_limiter_sw could not parse input arguments");
     1694    return NULL;
     1695  } 
    16551696  neighbours = get_consecutive_array(domain, "neighbours");
    16561697
    16571698  //Get safety factor beta_h
    16581699  Tmp = PyObject_GetAttrString(domain, "beta_h");
    1659   if (!Tmp)
    1660     return NULL;
     1700  if (!Tmp) {
     1701    PyErr_SetString(PyExc_RuntimeError, "shallow_water_ext.c: h_limiter_sw could not obtain object beta_h from domain");
     1702    return NULL;
     1703  } 
    16611704  beta_h = PyFloat_AsDouble(Tmp);
    16621705
     
    17251768                        &ymom_update,
    17261769                        &s_vec, &phi_vec,
    1727                         &cw))
    1728     return NULL;
     1770                        &cw)) {
     1771    PyErr_SetString(PyExc_RuntimeError, "shallow_water_ext.c: assign_windfield_values could not parse input arguments");
     1772    return NULL;
     1773  } 
     1774                       
    17291775
    17301776  N = xmom_update -> dimensions[0];
  • anuga_core/source/anuga/utilities/polygon_ext.c

    r3633 r3730  
    159159
    160160  // Convert Python arguments to C
    161   if (!PyArg_ParseTuple(args, "dddddd", &x, &y, &x0, &y0, &x1, &y1))
     161  if (!PyArg_ParseTuple(args, "dddddd", &x, &y, &x0, &y0, &x1, &y1)) {
     162    PyErr_SetString(PyExc_RuntimeError,
     163                    "point_on_line could not parse input");   
    162164    return NULL;
     165  }
    163166
    164167
     
    227230                        &indices,
    228231                        &closed,
    229                         &verbose))
     232                        &verbose)) {
     233   
     234
     235    PyErr_SetString(PyExc_RuntimeError,
     236                    "separate_points_by_polygon could not parse input");
    230237    return NULL;
     238  }
    231239
    232240  M = points -> dimensions[0];   //Number of points
  • anuga_core/source/anuga/utilities/sparse_ext.c

    r2503 r3730  
    8181 
    8282  // Convert Python arguments to C 
    83   if (!PyArg_ParseTuple(args, "OO", &csr_sparse, &xin))
    84     return NULL;
     83  if (!PyArg_ParseTuple(args, "OO", &csr_sparse, &xin)) {
     84    PyErr_SetString(PyExc_RuntimeError, "Csr_mv could not parse input"); 
     85    return NULL;
     86  }
    8587
    8688  x = (PyArrayObject*) PyArray_ContiguousFromObject(xin,PyArray_DOUBLE,1,2);
    87   if (!x)
    88     return NULL;
     89  if (!x) {
     90    PyErr_SetString(PyExc_RuntimeError,
     91                    "Input array could not be read in csr_mv");   
     92    return NULL;
     93  }
    8994
    9095/*   printf("x.nd = %i\n",x->nd); */
     
    101106
    102107
    103   data =  (PyArrayObject*)
     108  data = (PyArrayObject*)
    104109    PyObject_GetAttrString(csr_sparse, "data");     
    105   if (!data)
    106     return NULL; 
     110  if (!data) {
     111    PyErr_SetString(PyExc_RuntimeError,
     112                    "Data array could not be allocated in csr_mv");     
     113    return NULL;
     114  } 
    107115
    108116  colind = (PyArrayObject*)
    109117    PyObject_GetAttrString(csr_sparse, "colind");
    110   if (!colind) return NULL;   
     118  if (!colind) {
     119    PyErr_SetString(PyExc_RuntimeError,
     120                    "Column index array could not be allocated in csr_mv");     
     121    return NULL;
     122  } 
    111123
    112124  row_ptr = (PyArrayObject*)
    113125    PyObject_GetAttrString(csr_sparse, "row_ptr");   
    114   if (!row_ptr) return NULL;       
     126  if (!row_ptr) {
     127    PyErr_SetString(PyExc_RuntimeError,
     128                    "Row pointer array could not be allocated in csr_mv");
     129  }
    115130 
    116131  M = (row_ptr -> dimensions[0])-1;
     
    132147                           
    133148    if (err != 0) {
    134       PyErr_SetString(PyExc_RuntimeError, "matrix vector mult could not be calculated");
     149      PyErr_SetString(PyExc_RuntimeError, "Matrix vector mult could not be calculated");
    135150      return NULL;
    136151    }
  • anuga_core/source/anuga/utilities/util_ext.c

    r2738 r3730  
    3535  // Convert Python arguments to C
    3636  if (!PyArg_ParseTuple(args, "ddddddddd", &x0, &y0, &x1, &y1, &x2, &y2,
    37                         &q0, &q1, &q2))
     37                        &q0, &q1, &q2)) {
     38   
     39    PyErr_SetString(PyExc_RuntimeError,
     40                    "gradient could not parse input");   
    3841    return NULL;
     42  }
    3943
    4044
     
    5761
    5862  // Convert Python arguments to C
    59   if (!PyArg_ParseTuple(args, "dddddd", &x0, &y0, &x1, &y1, &q0, &q1))
     63  if (!PyArg_ParseTuple(args, "dddddd", &x0, &y0, &x1, &y1, &q0, &q1)) {
     64    PyErr_SetString(PyExc_RuntimeError,
     65                    "gradient2 could not parse input");     
    6066    return NULL;
     67  }
    6168
    6269
Note: See TracChangeset for help on using the changeset viewer.