source: inundation/ga/storm_surge/pyvolution/shallow_water_ext.c @ 1486

Last change on this file since 1486 was 1387, checked in by steve, 20 years ago

Need to look at uint test for inscribed circle

File size: 22.9 KB
Line 
1// Python - C extension module for shallow_water.py
2//
3// To compile (Python2.3):
4//  gcc -c domain_ext.c -I/usr/include/python2.3 -o domain_ext.o -Wall -O
5//  gcc -shared domain_ext.o  -o domain_ext.so
6//
7// or use python compile.py
8//
9// See the module shallow_water.py
10//
11//
12// Ole Nielsen, GA 2004
13
14
15#include "Python.h"
16#include "Numeric/arrayobject.h"
17#include "math.h"
18#include <stdio.h>
19
20//Shared code snippets
21#include "util_ext.h"
22
23const double pi = 3.14159265358979;
24
25// Computational function for rotation
26int _rotate(double *q, double n1, double n2) {
27  /*Rotate the momentum component q (q[1], q[2])
28    from x,y coordinates to coordinates based on normal vector (n1, n2).
29
30    Result is returned in array 3x1 r
31    To rotate in opposite direction, call rotate with (q, n1, -n2)
32
33    Contents of q are changed by this function */
34
35
36  double q1, q2;
37
38  //Shorthands
39  q1 = q[1];  //uh momentum
40  q2 = q[2];  //vh momentum
41
42  //Rotate
43  q[1] =  n1*q1 + n2*q2;
44  q[2] = -n2*q1 + n1*q2;
45
46  return 0;
47}
48
49
50
51// Computational function for flux computation (using stage w=z+h)
52int flux_function(double *q_left, double *q_right,
53                  double z_left, double z_right,
54                  double n1, double n2,
55                  double epsilon, double g,
56                  double *edgeflux, double *max_speed) {
57
58  /*Compute fluxes between volumes for the shallow water wave equation
59    cast in terms of the 'stage', w = h+z using
60    the 'central scheme' as described in
61
62    Kurganov, Noelle, Petrova. 'Semidiscrete Central-Upwind Schemes For
63    Hyperbolic Conservation Laws and Hamilton-Jacobi Equations'.
64    Siam J. Sci. Comput. Vol. 23, No. 3, pp. 707-740.
65
66    The implemented formula is given in equation (3.15) on page 714
67  */
68
69  int i;
70
71  double w_left, h_left, uh_left, vh_left, u_left;
72  double w_right, h_right, uh_right, vh_right, u_right;
73  double s_min, s_max, soundspeed_left, soundspeed_right;
74  double denom, z;
75  double q_left_copy[3], q_right_copy[3];
76  double flux_right[3], flux_left[3];
77
78  //Copy conserved quantities to protect from modification
79  for (i=0; i<3; i++) {
80    q_left_copy[i] = q_left[i];
81    q_right_copy[i] = q_right[i];
82  }
83
84  //Align x- and y-momentum with x-axis
85  _rotate(q_left_copy, n1, n2);
86  _rotate(q_right_copy, n1, n2);
87
88  z = (z_left+z_right)/2; //Take average of field values
89
90  //Compute speeds in x-direction
91  w_left = q_left_copy[0];              // h+z
92  h_left = w_left-z;
93  uh_left = q_left_copy[1];
94
95  if (h_left < epsilon) {
96    h_left = 0.0;  //Could have been negative
97    u_left = 0.0;
98  } else {
99    u_left = uh_left/h_left;
100  }
101
102  w_right = q_right_copy[0];
103  h_right = w_right-z;
104  uh_right = q_right_copy[1];
105
106  if (h_right < epsilon) {
107    h_right = 0.0; //Could have been negative
108    u_right = 0.0;
109  } else {
110    u_right = uh_right/h_right;
111  }
112
113  //Momentum in y-direction
114  vh_left  = q_left_copy[2];
115  vh_right = q_right_copy[2];
116
117
118  //Maximal and minimal wave speeds
119  soundspeed_left  = sqrt(g*h_left);
120  soundspeed_right = sqrt(g*h_right);
121
122  s_max = max(u_left+soundspeed_left, u_right+soundspeed_right);
123  if (s_max < 0.0) s_max = 0.0;
124
125  s_min = min(u_left-soundspeed_left, u_right-soundspeed_right);
126  if (s_min > 0.0) s_min = 0.0;
127
128  //Flux formulas
129  flux_left[0] = u_left*h_left;
130  flux_left[1] = u_left*uh_left + 0.5*g*h_left*h_left;
131  flux_left[2] = u_left*vh_left;
132
133  flux_right[0] = u_right*h_right;
134  flux_right[1] = u_right*uh_right + 0.5*g*h_right*h_right;
135  flux_right[2] = u_right*vh_right;
136
137
138  //Flux computation
139  denom = s_max-s_min;
140  if (denom == 0.0) {
141    for (i=0; i<3; i++) edgeflux[i] = 0.0;
142    *max_speed = 0.0;
143  } else {
144    for (i=0; i<3; i++) {
145      edgeflux[i] = s_max*flux_left[i] - s_min*flux_right[i];
146      edgeflux[i] += s_max*s_min*(q_right_copy[i]-q_left_copy[i]);
147      edgeflux[i] /= denom;
148    }
149
150    //Maximal wavespeed
151    *max_speed = max(fabs(s_max), fabs(s_min));
152
153    //Rotate back
154    _rotate(edgeflux, n1, -n2);
155  }
156  return 0;
157}
158
159void _manning_friction(double g, double eps, int N,
160                       double* w, double* z,
161                       double* uh, double* vh,
162                       double* eta, double* xmom, double* ymom) {
163
164  int k;
165  double S, h;
166
167  for (k=0; k<N; k++) {
168    if (eta[k] > eps) {
169      h = w[k]-z[k];
170      if (h >= eps) {
171        S = -g * eta[k]*eta[k] * sqrt((uh[k]*uh[k] + vh[k]*vh[k]));
172        S /= pow(h, 7.0/3);      //Expensive (on Ole's home computer)
173        //S /= h*h*(1 + h/3.0 - h*h/9.0); //FIXME: Could use a Taylor expansion
174
175
176        //Update momentum
177        xmom[k] += S*uh[k];
178        ymom[k] += S*vh[k];
179      }
180    }
181  }
182}
183
184
185
186int _balance_deep_and_shallow(int N,
187                              double* wc,
188                              double* zc,
189                              double* hc,
190                              double* wv,
191                              double* zv,
192                              double* hv,
193                              double* hvbar,
194                              double* xmomc,
195                              double* ymomc,
196                              double* xmomv,
197                              double* ymomv) {
198
199  int k, k3, i;
200  double dz, hmin, alpha;
201
202  //Compute linear combination between w-limited stages and
203  //h-limited stages close to the bed elevation.
204
205  for (k=0; k<N; k++) {
206    // Compute maximal variation in bed elevation
207    // This quantitiy is
208    //     dz = max_i abs(z_i - z_c)
209    // and it is independent of dimension
210    // In the 1d case zc = (z0+z1)/2
211    // In the 2d case zc = (z0+z1+z2)/3
212
213    k3 = 3*k;
214
215    //FIXME: Try with this one precomputed
216    dz = 0.0;
217    hmin = hv[k3];
218    for (i=0; i<3; i++) {
219      dz = max(dz, fabs(zv[k3+i]-zc[k]));
220      hmin = min(hmin, hv[k3+i]);
221    }
222
223
224    //Create alpha in [0,1], where alpha==0 means using the h-limited
225    //stage and alpha==1 means using the w-limited stage as
226    //computed by the gradient limiter (both 1st or 2nd order)
227    //
228    //If hmin > dz/2 then alpha = 1 and the bed will have no effect
229    //If hmin < 0 then alpha = 0 reverting to constant height above bed.
230
231
232    if (dz > 0.0)
233      //if (hmin<0.0)
234      //        alpha = 0.0;
235      //else
236      //  alpha = max( min( hc[k]/dz, 1.0), 0.0 );
237      alpha = max( min( 2.0*hmin/dz, 1.0), 0.0 );
238    else
239      alpha = 1.0;  //Flat bed
240
241    //alpha = 1.0;
242
243    //printf("dz = %.3f, alpha = %.8f\n", dz, alpha);
244
245    //  Let
246    //
247    //    wvi be the w-limited stage (wvi = zvi + hvi)
248    //    wvi- be the h-limited state (wvi- = zvi + hvi-)
249    //
250    //
251    //  where i=0,1,2 denotes the vertex ids
252    //
253    //  Weighted balance between w-limited and h-limited stage is
254    //
255    //    wvi := (1-alpha)*(zvi+hvi-) + alpha*(zvi+hvi)
256    //
257    //  It follows that the updated wvi is
258    //    wvi := zvi + (1-alpha)*hvi- + alpha*hvi
259    //
260    //   Momentum is balanced between constant and limited
261
262    if (alpha < 1) {
263      for (i=0; i<3; i++) {
264        wv[k3+i] = zv[k3+i] + (1-alpha)*hvbar[k3+i] + alpha*hv[k3+i];
265
266        //Update momentum as a linear combination of
267        //xmomc and ymomc (shallow) and momentum
268        //from extrapolator xmomv and ymomv (deep).
269        xmomv[k3+i] = (1-alpha)*xmomc[k] + alpha*xmomv[k3+i];
270        ymomv[k3+i] = (1-alpha)*ymomc[k] + alpha*ymomv[k3+i];
271      }
272    }
273  }
274  return 0;
275}
276
277
278
279int _protect(int N,
280             double minimum_allowed_height,
281             double* wc,
282             double* zc,
283             double* xmomc,
284             double* ymomc) {
285
286  int k;
287  double hc;
288
289  //Protect against initesimal and negative heights
290
291  for (k=0; k<N; k++) {
292    hc = wc[k] - zc[k];
293
294    if (hc < minimum_allowed_height) {
295      wc[k] = zc[k];
296      xmomc[k] = 0.0;
297      ymomc[k] = 0.0;
298    }
299
300  }
301  return 0;
302}
303
304
305
306
307
308
309
310
311int _assign_wind_field_values(int N,
312                              double* xmom_update,
313                              double* ymom_update,
314                              double* s_vec,
315                              double* phi_vec,
316                              double cw) {
317
318  //Assign windfield values to momentum updates
319
320  int k;
321  double S, s, phi, u, v;
322
323  for (k=0; k<N; k++) {
324
325    s = s_vec[k];
326    phi = phi_vec[k];
327
328    //Convert to radians
329    phi = phi*pi/180;
330
331    //Compute velocity vector (u, v)
332    u = s*cos(phi);
333    v = s*sin(phi);
334
335    //Compute wind stress
336    S = cw * sqrt(u*u + v*v);
337    xmom_update[k] += S*u;
338    ymom_update[k] += S*v;
339  }
340  return 0;
341}
342
343
344
345///////////////////////////////////////////////////////////////////
346// Gateways to Python
347
348PyObject *gravity(PyObject *self, PyObject *args) {
349  //
350  //  gravity(g, h, v, x, xmom, ymom)
351  //
352
353
354  PyArrayObject *h, *v, *x, *xmom, *ymom;
355  int k, i, N, k3, k6;
356  double g, avg_h, zx, zy;
357  double x0, y0, x1, y1, x2, y2, z0, z1, z2;
358
359  if (!PyArg_ParseTuple(args, "dOOOOO",
360                        &g, &h, &v, &x,
361                        &xmom, &ymom))
362    return NULL;
363
364  N = h -> dimensions[0];
365  for (k=0; k<N; k++) {
366    k3 = 3*k;  // base index
367    k6 = 6*k;  // base index
368
369    avg_h = 0.0;
370    for (i=0; i<3; i++) {
371      avg_h += ((double *) h -> data)[k3+i];
372    }
373    avg_h /= 3;
374
375
376    //Compute bed slope
377    x0 = ((double*) x -> data)[k6 + 0];
378    y0 = ((double*) x -> data)[k6 + 1];
379    x1 = ((double*) x -> data)[k6 + 2];
380    y1 = ((double*) x -> data)[k6 + 3];
381    x2 = ((double*) x -> data)[k6 + 4];
382    y2 = ((double*) x -> data)[k6 + 5];
383
384
385    z0 = ((double*) v -> data)[k3 + 0];
386    z1 = ((double*) v -> data)[k3 + 1];
387    z2 = ((double*) v -> data)[k3 + 2];
388
389    _gradient(x0, y0, x1, y1, x2, y2, z0, z1, z2, &zx, &zy);
390
391    //Update momentum
392    ((double*) xmom -> data)[k] += -g*zx*avg_h;
393    ((double*) ymom -> data)[k] += -g*zy*avg_h;
394  }
395
396  return Py_BuildValue("");
397}
398
399
400PyObject *manning_friction(PyObject *self, PyObject *args) {
401  //
402  // manning_friction(g, eps, h, uh, vh, eta, xmom_update, ymom_update)
403  //
404
405
406  PyArrayObject *w, *z, *uh, *vh, *eta, *xmom, *ymom;
407  int N;
408  double g, eps;
409
410  if (!PyArg_ParseTuple(args, "ddOOOOOOO",
411                        &g, &eps, &w, &z, &uh, &vh, &eta,
412                        &xmom, &ymom))
413    return NULL;
414
415  N = w -> dimensions[0];
416  _manning_friction(g, eps, N,
417                    (double*) w -> data,
418                    (double*) z -> data,
419                    (double*) uh -> data,
420                    (double*) vh -> data,
421                    (double*) eta -> data,
422                    (double*) xmom -> data,
423                    (double*) ymom -> data);
424
425  return Py_BuildValue("");
426}
427
428PyObject *rotate(PyObject *self, PyObject *args, PyObject *kwargs) {
429  //
430  // r = rotate(q, normal, direction=1)
431  //
432  // Where q is assumed to be a Float numeric array of length 3 and
433  // normal a Float numeric array of length 2.
434
435
436  PyObject *Q, *Normal;
437  PyArrayObject *q, *r, *normal;
438
439  static char *argnames[] = {"q", "normal", "direction", NULL};
440  int dimensions[1], i, direction=1;
441  double n1, n2;
442
443  // Convert Python arguments to C
444  if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO|i", argnames,
445                                   &Q, &Normal, &direction))
446    return NULL;
447
448  //Input checks (convert sequences into numeric arrays)
449  q = (PyArrayObject *)
450    PyArray_ContiguousFromObject(Q, PyArray_DOUBLE, 0, 0);
451  normal = (PyArrayObject *)
452    PyArray_ContiguousFromObject(Normal, PyArray_DOUBLE, 0, 0);
453
454
455  if (normal -> dimensions[0] != 2) {
456    PyErr_SetString(PyExc_RuntimeError, "Normal vector must have 2 components");
457    return NULL;
458  }
459
460  //Allocate space for return vector r (don't DECREF)
461  dimensions[0] = 3;
462  r = (PyArrayObject *) PyArray_FromDims(1, dimensions, PyArray_DOUBLE);
463
464  //Copy
465  for (i=0; i<3; i++) {
466    ((double *) (r -> data))[i] = ((double *) (q -> data))[i];
467  }
468
469  //Get normal and direction
470  n1 = ((double *) normal -> data)[0];
471  n2 = ((double *) normal -> data)[1];
472  if (direction == -1) n2 = -n2;
473
474  //Rotate
475  _rotate((double *) r -> data, n1, n2);
476
477  //Release numeric arrays
478  Py_DECREF(q);
479  Py_DECREF(normal);
480
481  //return result using PyArray to avoid memory leak
482  return PyArray_Return(r);
483}
484
485
486PyObject *compute_fluxes(PyObject *self, PyObject *args) {
487  /*Compute all fluxes and the timestep suitable for all volumes
488    in domain.
489
490    Compute total flux for each conserved quantity using "flux_function"
491
492    Fluxes across each edge are scaled by edgelengths and summed up
493    Resulting flux is then scaled by area and stored in
494    explicit_update for each of the three conserved quantities
495    stage, xmomentum and ymomentum
496
497    The maximal allowable speed computed by the flux_function for each volume
498    is converted to a timestep that must not be exceeded. The minimum of
499    those is computed as the next overall timestep.
500
501    Python call:
502    domain.timestep = compute_fluxes(timestep,
503                                     domain.epsilon,
504                                     domain.g,
505                                     domain.neighbours,
506                                     domain.neighbour_edges,
507                                     domain.normals,
508                                     domain.edgelengths,
509                                     domain.radii,
510                                     domain.areas,
511                                     Stage.edge_values,
512                                     Xmom.edge_values,
513                                     Ymom.edge_values,
514                                     Bed.edge_values,
515                                     Stage.boundary_values,
516                                     Xmom.boundary_values,
517                                     Ymom.boundary_values,
518                                     Stage.explicit_update,
519                                     Xmom.explicit_update,
520                                     Ymom.explicit_update)
521
522
523    Post conditions:
524      domain.explicit_update is reset to computed flux values
525      domain.timestep is set to the largest step satisfying all volumes.
526
527
528  */
529
530
531  PyArrayObject *neighbours, *neighbour_edges,
532    *normals, *edgelengths, *radii, *areas,
533    *stage_edge_values,
534    *xmom_edge_values,
535    *ymom_edge_values,
536    *bed_edge_values,
537    *stage_boundary_values,
538    *xmom_boundary_values,
539    *ymom_boundary_values,
540    *stage_explicit_update,
541    *xmom_explicit_update,
542    *ymom_explicit_update;
543
544
545  //Local variables
546  double timestep, max_speed, epsilon, g;
547  double normal[2], ql[3], qr[3], zl, zr;
548  double flux[3], edgeflux[3]; //Work arrays for summing up fluxes
549
550  int number_of_elements, k, i, j, m, n;
551  int ki, nm, ki2; //Index shorthands
552
553
554  // Convert Python arguments to C
555  if (!PyArg_ParseTuple(args, "dddOOOOOOOOOOOOOOOO",
556                        &timestep,
557                        &epsilon,
558                        &g,
559                        &neighbours,
560                        &neighbour_edges,
561                        &normals,
562                        &edgelengths, &radii, &areas,
563                        &stage_edge_values,
564                        &xmom_edge_values,
565                        &ymom_edge_values,
566                        &bed_edge_values,
567                        &stage_boundary_values,
568                        &xmom_boundary_values,
569                        &ymom_boundary_values,
570                        &stage_explicit_update,
571                        &xmom_explicit_update,
572                        &ymom_explicit_update)) {
573    PyErr_SetString(PyExc_RuntimeError, "Input arguments failed");
574    return NULL;
575  }
576
577  number_of_elements = stage_edge_values -> dimensions[0];
578
579
580  for (k=0; k<number_of_elements; k++) {
581
582    //Reset work array
583    for (j=0; j<3; j++) flux[j] = 0.0;
584
585    //Loop through neighbours and compute edge flux for each
586    for (i=0; i<3; i++) {
587      ki = k*3+i;
588      ql[0] = ((double *) stage_edge_values -> data)[ki];
589      ql[1] = ((double *) xmom_edge_values -> data)[ki];
590      ql[2] = ((double *) ymom_edge_values -> data)[ki];
591      zl =    ((double *) bed_edge_values -> data)[ki];
592
593      //Quantities at neighbour on nearest face
594      n = ((long *) neighbours -> data)[ki];
595      if (n < 0) {
596        m = -n-1; //Convert negative flag to index
597        qr[0] = ((double *) stage_boundary_values -> data)[m];
598        qr[1] = ((double *) xmom_boundary_values -> data)[m];
599        qr[2] = ((double *) ymom_boundary_values -> data)[m];
600        zr = zl; //Extend bed elevation to boundary
601      } else {
602        m = ((long *) neighbour_edges -> data)[ki];
603
604        nm = n*3+m;
605        qr[0] = ((double *) stage_edge_values -> data)[nm];
606        qr[1] = ((double *) xmom_edge_values -> data)[nm];
607        qr[2] = ((double *) ymom_edge_values -> data)[nm];
608        zr =    ((double *) bed_edge_values -> data)[nm];
609      }
610
611      //printf("%d %d [%d] (%d, %d): %.2f %.2f %.2f\n", k, i, ki, n, m,
612      //             ql[0], ql[1], ql[2]);
613
614
615      // Outward pointing normal vector
616      // normal = domain.normals[k, 2*i:2*i+2]
617      ki2 = 2*ki; //k*6 + i*2
618      normal[0] = ((double *) normals -> data)[ki2];
619      normal[1] = ((double *) normals -> data)[ki2+1];
620
621      //Edge flux computation
622      flux_function(ql, qr, zl, zr,
623                    normal[0], normal[1],
624                    epsilon, g,
625                    edgeflux, &max_speed);
626
627
628      //flux -= edgeflux * edgelengths[k,i]
629      for (j=0; j<3; j++) {
630        flux[j] -= edgeflux[j]*((double *) edgelengths -> data)[ki];
631      }
632
633      //Update timestep
634      //timestep = min(timestep, domain.radii[k]/max_speed)
635      //FIXME: SR Add parameter for CFL condition
636      if (max_speed > epsilon) {
637        timestep = min(timestep, ((double *) radii -> data)[k]/max_speed);
638      }
639    } // end for i
640
641    //Normalise by area and store for when all conserved
642    //quantities get updated
643    // flux /= areas[k]
644    for (j=0; j<3; j++) {
645      flux[j] /= ((double *) areas -> data)[k];
646    }
647
648    ((double *) stage_explicit_update -> data)[k] = flux[0];
649    ((double *) xmom_explicit_update -> data)[k] = flux[1];
650    ((double *) ymom_explicit_update -> data)[k] = flux[2];
651
652  } //end for k
653
654  return Py_BuildValue("d", timestep);
655}
656
657
658
659PyObject *protect(PyObject *self, PyObject *args) {
660  //
661  //    protect(minimum_allowed_height, wc, zc, xmomc, ymomc)
662
663
664  PyArrayObject
665  *wc,            //Stage at centroids
666  *zc,            //Elevation at centroids
667  *xmomc,         //Momentums at centroids
668  *ymomc;
669
670
671  int N;
672  double minimum_allowed_height;
673
674  // Convert Python arguments to C
675  if (!PyArg_ParseTuple(args, "dOOOO",
676                        &minimum_allowed_height,
677                        &wc, &zc, &xmomc, &ymomc))
678    return NULL;
679
680  N = wc -> dimensions[0];
681
682  _protect(N,
683           minimum_allowed_height,
684           (double*) wc -> data,
685           (double*) zc -> data,
686           (double*) xmomc -> data,
687           (double*) ymomc -> data);
688
689  return Py_BuildValue("");
690}
691
692
693
694PyObject *balance_deep_and_shallow(PyObject *self, PyObject *args) {
695  //
696  //    balance_deep_and_shallow(wc, zc, hc, wv, zv, hv,
697  //                             xmomc, ymomc, xmomv, ymomv)
698
699
700  PyArrayObject
701    *wc,            //Stage at centroids
702    *zc,            //Elevation at centroids
703    *hc,            //Height at centroids
704    *wv,            //Stage at vertices
705    *zv,            //Elevation at vertices
706    *hv,            //Depths at vertices
707    *hvbar,         //h-Limited depths at vertices
708    *xmomc,         //Momentums at centroids and vertices
709    *ymomc,
710    *xmomv,
711    *ymomv;
712
713  int N; //, err;
714
715  // Convert Python arguments to C
716  if (!PyArg_ParseTuple(args, "OOOOOOOOOOO",
717                        &wc, &zc, &hc,
718                        &wv, &zv, &hv, &hvbar,
719                        &xmomc, &ymomc, &xmomv, &ymomv))
720    return NULL;
721
722  N = wc -> dimensions[0];
723
724  _balance_deep_and_shallow(N,
725                            (double*) wc -> data,
726                            (double*) zc -> data,
727                            (double*) hc -> data,
728                            (double*) wv -> data,
729                            (double*) zv -> data,
730                            (double*) hv -> data,
731                            (double*) hvbar -> data,
732                            (double*) xmomc -> data,
733                            (double*) ymomc -> data,
734                            (double*) xmomv -> data,
735                            (double*) ymomv -> data);
736
737
738  return Py_BuildValue("");
739}
740
741
742
743PyObject *h_limiter(PyObject *self, PyObject *args) {
744
745  PyObject *domain, *Tmp;
746  PyArrayObject
747    *hv, *hc, //Depth at vertices and centroids
748    *hvbar,   //Limited depth at vertices (return values)
749    *neighbours;
750
751  int k, i, n, N, k3;
752  int dimensions[2];
753  double beta_h; //Safety factor (see config.py)
754  double *hmin, *hmax, hn;
755
756  // Convert Python arguments to C
757  if (!PyArg_ParseTuple(args, "OOO", &domain, &hc, &hv))
758    return NULL;
759
760  neighbours = get_consecutive_array(domain, "neighbours");
761
762  //Get safety factor beta_h
763  Tmp = PyObject_GetAttrString(domain, "beta_h");
764  if (!Tmp)
765    return NULL;
766
767  beta_h = PyFloat_AsDouble(Tmp);
768
769  Py_DECREF(Tmp);
770
771  N = hc -> dimensions[0];
772
773  //Create hvbar
774  dimensions[0] = N;
775  dimensions[1] = 3;
776  hvbar = (PyArrayObject *) PyArray_FromDims(2, dimensions, PyArray_DOUBLE);
777
778
779  //Find min and max of this and neighbour's centroid values
780  hmin = malloc(N * sizeof(double));
781  hmax = malloc(N * sizeof(double));
782  for (k=0; k<N; k++) {
783    k3=k*3;
784
785    hmin[k] = ((double*) hc -> data)[k];
786    hmax[k] = hmin[k];
787
788    for (i=0; i<3; i++) {
789      n = ((long*) neighbours -> data)[k3+i];
790
791      //Initialise hvbar with values from hv
792      ((double*) hvbar -> data)[k3+i] = ((double*) hv -> data)[k3+i];
793
794      if (n >= 0) {
795        hn = ((double*) hc -> data)[n]; //Neighbour's centroid value
796
797        hmin[k] = min(hmin[k], hn);
798        hmax[k] = max(hmax[k], hn);
799      }
800    }
801  }
802
803  // Call underlying standard routine
804  _limit(N, beta_h, (double*) hc -> data, (double*) hvbar -> data, hmin, hmax);
805
806  // // //Py_DECREF(domain); //FIXME: NEcessary?
807  free(hmin);
808  free(hmax);
809
810  //return result using PyArray to avoid memory leak
811  return PyArray_Return(hvbar);
812  //return Py_BuildValue("");
813}
814
815
816
817
818PyObject *assign_windfield_values(PyObject *self, PyObject *args) {
819  //
820  //      assign_windfield_values(xmom_update, ymom_update,
821  //                              s_vec, phi_vec, self.const)
822
823
824
825  PyArrayObject   //(one element per triangle)
826  *s_vec,         //Speeds
827  *phi_vec,       //Bearings
828  *xmom_update,   //Momentum updates
829  *ymom_update;
830
831
832  int N;
833  double cw;
834
835  // Convert Python arguments to C
836  if (!PyArg_ParseTuple(args, "OOOOd",
837                        &xmom_update,
838                        &ymom_update,
839                        &s_vec, &phi_vec,
840                        &cw))
841    return NULL;
842
843  N = xmom_update -> dimensions[0];
844
845  _assign_wind_field_values(N,
846           (double*) xmom_update -> data,
847           (double*) ymom_update -> data,
848           (double*) s_vec -> data,
849           (double*) phi_vec -> data,
850           cw);
851
852  return Py_BuildValue("");
853}
854
855
856
857
858//////////////////////////////////////////
859// Method table for python module
860static struct PyMethodDef MethodTable[] = {
861  /* The cast of the function is necessary since PyCFunction values
862   * only take two PyObject* parameters, and rotate() takes
863   * three.
864   */
865
866  {"rotate", (PyCFunction)rotate, METH_VARARGS | METH_KEYWORDS, "Print out"},
867  {"compute_fluxes", compute_fluxes, METH_VARARGS, "Print out"},
868  {"gravity", gravity, METH_VARARGS, "Print out"},
869  {"manning_friction", manning_friction, METH_VARARGS, "Print out"},
870  {"balance_deep_and_shallow", balance_deep_and_shallow,
871   METH_VARARGS, "Print out"},
872  {"h_limiter", h_limiter,
873   METH_VARARGS, "Print out"},
874  {"protect", protect, METH_VARARGS | METH_KEYWORDS, "Print out"},
875  {"assign_windfield_values", assign_windfield_values,
876   METH_VARARGS | METH_KEYWORDS, "Print out"},
877  //{"distribute_to_vertices_and_edges",
878  // distribute_to_vertices_and_edges, METH_VARARGS},
879  //{"update_conserved_quantities",
880  // update_conserved_quantities, METH_VARARGS},
881  //{"set_initialcondition",
882  // set_initialcondition, METH_VARARGS},
883  {NULL, NULL}
884};
885
886// Module initialisation
887void initshallow_water_ext(void){
888  Py_InitModule("shallow_water_ext", MethodTable);
889
890  import_array();     //Necessary for handling of NumPY structures
891}
Note: See TracBrowser for help on using the repository browser.