source: anuga_work/development/anuga_1d/comp_flux_ext.c @ 5565

Last change on this file since 5565 was 5564, checked in by sudi, 16 years ago
File size: 9.5 KB
Line 
1#include "Python.h"
2#include "Numeric/arrayobject.h"
3#include "math.h"
4#include <stdio.h>
5const double pi = 3.14159265358979;
6
7
8// Shared code snippets
9#include "util_ext.h"
10
11
12/* double max(double a, double b) { */
13/*      double z; */
14/*      z=(a>b)?a:b; */
15/*      return z;} */
16       
17/* double min(double a, double b) { */
18/*      double z; */
19/*      z=(a<b)?a:b; */
20/*      return z;} */
21       
22
23//Innermost flux function (using w=z+h)
24int _flux_function(double *q_left, double *q_right,
25        double z_left, double z_right,
26                double normals, double g, double epsilon,
27                double *edgeflux, double *max_speed) {
28               
29                int i;
30                double ql[2], qr[2], flux_left[2], flux_right[2];
31                double z, w_left, h_left, uh_left, soundspeed_left, u_left;
32                double w_right, h_right, uh_right, soundspeed_right, u_right;
33                double s_max, s_min, denom;
34               
35               
36                ql[0] = q_left[0];
37                ql[1] = q_left[1];
38                ql[1] = ql[1]*normals;
39       
40                qr[0] = q_right[0];
41                qr[1] = q_right[1];
42                qr[1] = qr[1]*normals;
43         
44                z = (z_left+z_right)/2.0;                 
45                                   
46                w_left = ql[0];
47                h_left = w_left-z;
48                uh_left = ql[1];
49               
50
51
52                // Compute speeds in x-direction
53                w_left = ql[0];         
54                h_left = w_left-z;
55                uh_left = ql[1];
56                if (h_left < epsilon) {
57                        u_left = 0.0; h_left = 0.0;
58                }
59                else {
60                        u_left = uh_left/h_left;
61                }
62       
63                w_right = qr[0];
64                h_right = w_right-z;
65                uh_right = qr[1];
66                if (h_right < epsilon) {
67                        u_right = 0.0; h_right = 0.0; 
68                }
69                else {
70                        u_right = uh_right/h_right;
71                }
72 
73                soundspeed_left = sqrt(g*h_left);
74                soundspeed_right = sqrt(g*h_right);
75               
76                s_max = max(u_left+soundspeed_left, u_right+soundspeed_right);
77                if (s_max < 0.0) s_max = 0.0;
78       
79                s_min = min(u_left-soundspeed_left, u_right-soundspeed_right);
80                if (s_min > 0.0) s_min = 0.0;
81               
82               
83                // Flux formulas
84                flux_left[0] = u_left*h_left;
85                flux_left[1] = u_left*uh_left + 0.5*g*h_left*h_left;
86
87                flux_right[0] = u_right*h_right;
88                flux_right[1] = u_right*uh_right + 0.5*g*h_right*h_right;
89
90                // Flux computation
91                denom = s_max-s_min;
92                if (denom < epsilon) {
93                        for (i=0; i<2; i++) edgeflux[i] = 0.0;
94                        *max_speed = 0.0;
95                } else {
96                        edgeflux[0] = s_max*flux_left[0] - s_min*flux_right[0];
97                        edgeflux[0] += s_max*s_min*(qr[0]-ql[0]);
98                        edgeflux[0] /= denom;
99                        edgeflux[1] = s_max*flux_left[1] - s_min*flux_right[1];
100                        edgeflux[1] += s_max*s_min*(qr[1]-ql[1]);
101                        edgeflux[1] /= denom;
102                        edgeflux[1] *= normals;
103   
104                // Maximal wavespeed
105        *max_speed = max(fabs(s_max), fabs(s_min));
106                } 
107    return 0;           
108        }
109               
110               
111       
112       
113// Computational function for flux computation
114double _compute_fluxes_ext(double timestep,
115                double epsilon,
116                double g,
117                long* neighbours,
118                long* neighbour_vertices,
119                double* normals,
120                double* areas,
121                double* stage_edge_values,
122                double* xmom_edge_values,
123                double* bed_edge_values,
124                double* stage_boundary_values,
125                double* xmom_boundary_values,
126                double* stage_explicit_update,
127                double* xmom_explicit_update,
128                int number_of_elements,
129                double* max_speed_array) {
130               
131                double flux[2], ql[2], qr[2], edgeflux[2];
132                double zl, zr, max_speed, normal;
133                int k, i, ki, n, m, nm=0;
134               
135                for (k=0; k<number_of_elements; k++) {
136                        flux[0] = 0.0;
137                        flux[1] = 0.0;
138                       
139                        for (i=0; i<2; i++) {
140                                ki = k*2+i;
141                               
142                                ql[0] = stage_edge_values[ki];
143                                ql[1] = xmom_edge_values[ki];
144                                zl = bed_edge_values[ki];
145                               
146                                n = neighbours[ki];
147                                if (n<0) {
148                                        m = -n-1;
149                                        qr[0] = stage_boundary_values[m];
150                                        qr[1] = xmom_boundary_values[m];
151                                        zr = zl;
152                                } else {
153                                        m = neighbour_vertices[ki];
154                                        nm = n*2+m;
155                                        qr[0] = stage_edge_values[nm];
156                                        qr[1] = xmom_edge_values[nm];
157                                        zr = bed_edge_values[nm];                               
158                                }
159                               
160                                normal = normals[ki];
161                                _flux_function(ql, qr, zl, zr, normal, g, epsilon, edgeflux, &max_speed);
162                                flux[0] -= edgeflux[0];
163                                flux[1] -= edgeflux[1];
164                               
165                                // Update timestep based on edge i and possibly neighbour n
166                                if (max_speed > epsilon) {
167                        // Original CFL calculation
168                                       
169                                        timestep = min(timestep, 0.5*areas[k]/max_speed); //Here, CFL=1.0 is assumed. ?????????????????????????????????????????????
170                                        if (n>=0) {
171                                                timestep = min(timestep, 0.5*areas[n]/max_speed); //Here, CFL=1.0 is assumed. ?????????????????????????????????????????????
172                                        }
173                                }
174            } // End edge i (and neighbour n)
175                        flux[0] /= areas[k];
176                        stage_explicit_update[k] = flux[0];
177                        flux[1] /= areas[k];
178                        xmom_explicit_update[k] = flux[1];
179                       
180                        //Keep track of maximal speeds
181                        max_speed_array[k]=max_speed;
182                }
183                return timestep;        }
184       
185       
186       
187       
188       
189
190
191
192//=========================================================================
193// Python Glue
194//=========================================================================
195PyObject *compute_fluxes_ext(PyObject *self, PyObject *args) {
196 
197    PyArrayObject
198        *neighbours, 
199        *neighbour_vertices,
200        *normals, 
201        *areas,
202        *stage_edge_values,
203        *xmom_edge_values,
204        *bed_edge_values,
205        *stage_boundary_values,
206        *xmom_boundary_values,
207        *stage_explicit_update,
208        *xmom_explicit_update,
209        *max_speed_array;
210   
211  double timestep, epsilon, g;
212  int number_of_elements;
213   
214  // Convert Python arguments to C
215  if (!PyArg_ParseTuple(args, "dddOOOOOOOOOOOiO",
216                        &timestep,
217                        &epsilon,
218                        &g,
219                        &neighbours,
220                        &neighbour_vertices,
221                        &normals,
222                        &areas,
223                        &stage_edge_values,
224                        &xmom_edge_values,
225                        &bed_edge_values,
226                        &stage_boundary_values,
227                        &xmom_boundary_values,
228                        &stage_explicit_update,
229                        &xmom_explicit_update,
230                        &number_of_elements,
231                        &max_speed_array)) {
232    PyErr_SetString(PyExc_RuntimeError, "comp_flux_ext.c: compute_fluxes_ext could not parse input");
233    return NULL;
234  }
235
236 
237  // Call underlying flux computation routine and update
238  // the explicit update arrays
239  timestep = _compute_fluxes_ext(timestep,
240                                 epsilon,
241                                 g,
242                                 (long*) neighbours -> data,
243                                 (long*) neighbour_vertices -> data,
244                                 (double*) normals -> data,
245                                 (double*) areas -> data,
246                                 (double*) stage_edge_values -> data,
247                                 (double*) xmom_edge_values -> data,
248                                 (double*) bed_edge_values -> data,
249                                 (double*) stage_boundary_values -> data,
250                                 (double*) xmom_boundary_values -> data,
251                                 (double*) stage_explicit_update -> data,
252                                 (double*) xmom_explicit_update -> data,
253                                 number_of_elements,
254                                 (double*) max_speed_array -> data);
255
256  // Return updated flux timestep
257  return Py_BuildValue("d", timestep);
258}
259
260
261PyObject *compute_fluxes_ext_short(PyObject *self, PyObject *args) {
262 
263   PyObject
264        *domain,
265        *stage, 
266        *xmom, 
267        *bed;
268
269    PyArrayObject
270        *neighbours, 
271        *neighbour_vertices,
272        *normals, 
273        *areas,
274        *stage_vertex_values,
275        *xmom_vertex_values,
276        *bed_vertex_values,
277        *stage_boundary_values,
278        *xmom_boundary_values,
279        *stage_explicit_update,
280        *xmom_explicit_update,
281        *max_speed_array;
282   
283  double timestep, epsilon, g;
284  int number_of_elements;
285
286   
287  // Convert Python arguments to C
288  if (!PyArg_ParseTuple(args, "dOOOO",
289                        &timestep,
290                        &domain,
291                        &stage,
292                        &xmom,
293                        &bed)) {
294      PyErr_SetString(PyExc_RuntimeError, "comp_flux_ext.c: compute_fluxes_ext_short could not parse input");
295      return NULL;
296  }
297
298
299    epsilon           = get_python_double(domain,"epsilon");
300    g                 = get_python_double(domain,"g");
301 
302   
303    neighbours        = get_consecutive_array(domain, "neighbours");
304    neighbour_vertices= get_consecutive_array(domain, "neighbour_vertices"); 
305    normals           = get_consecutive_array(domain, "normals");
306    areas             = get_consecutive_array(domain, "areas");   
307    max_speed_array   = get_consecutive_array(domain, "max_speed_array");
308   
309    stage_vertex_values = get_consecutive_array(stage, "vertex_values");   
310    xmom_vertex_values  = get_consecutive_array(xmom, "vertex_values");   
311    bed_vertex_values   = get_consecutive_array(bed, "vertex_values");   
312
313    stage_boundary_values = get_consecutive_array(stage, "boundary_values");   
314    xmom_boundary_values  = get_consecutive_array(xmom, "boundary_values");   
315
316
317    stage_explicit_update = get_consecutive_array(stage, "explicit_update");   
318    xmom_explicit_update  = get_consecutive_array(xmom, "explicit_update");   
319
320
321
322    number_of_elements = stage_vertex_values -> dimensions[0];
323
324
325 
326    // Call underlying flux computation routine and update
327    // the explicit update arrays
328    timestep = _compute_fluxes_ext(timestep,
329                                 epsilon,
330                                 g,
331                                 (long*) neighbours -> data,
332                                 (long*) neighbour_vertices -> data,
333                                 (double*) normals -> data,
334                                 (double*) areas -> data,
335                                 (double*) stage_vertex_values -> data,
336                                 (double*) xmom_vertex_values -> data,
337                                 (double*) bed_vertex_values -> data,
338                                 (double*) stage_boundary_values -> data,
339                                 (double*) xmom_boundary_values -> data,
340                                 (double*) stage_explicit_update -> data,
341                                 (double*) xmom_explicit_update -> data,
342                                 number_of_elements,
343                                 (double*) max_speed_array -> data);
344
345
346  Py_DECREF(neighbours);
347  Py_DECREF(neighbour_vertices);
348  Py_DECREF(normals);
349  Py_DECREF(areas);
350  Py_DECREF(stage_vertex_values);
351  Py_DECREF(xmom_vertex_values);
352  Py_DECREF(bed_vertex_values);
353  Py_DECREF(stage_boundary_values);
354  Py_DECREF(xmom_boundary_values);
355  Py_DECREF(stage_explicit_update);
356  Py_DECREF(xmom_explicit_update);
357  Py_DECREF(max_speed_array);
358
359
360
361
362  // Return updated flux timestep
363  return Py_BuildValue("d", timestep);
364}
365
366 
367
368
369//-------------------------------
370// Method table for python module
371//-------------------------------
372
373static struct PyMethodDef MethodTable[] = {
374  {"compute_fluxes_ext", compute_fluxes_ext, METH_VARARGS, "Print out"},
375  {"compute_fluxes_ext_short", compute_fluxes_ext_short, METH_VARARGS, "Print out"},
376  {NULL, NULL}
377};
378
379// Module initialisation
380void initcomp_flux_ext(void){
381  Py_InitModule("comp_flux_ext", MethodTable);
382  import_array();
383}
Note: See TracBrowser for help on using the repository browser.