source: anuga_work/development/pipeflow/old_sww_comp_flux_ext.c @ 7823

Last change on this file since 7823 was 7823, checked in by steve, 14 years ago

Adding old files as well as workinng files from anuga_1d

File size: 8.7 KB
Line 
1#include "Python.h"
2#include "numpy/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//Innermost flux function (using w=z+h)
13int _flux_function(double *q_left, double *q_right,
14                   double normals, double g, double epsilon, double h0,
15                   double *edgeflux, double *max_speed) {
16               
17                int i;
18                double flux_left[2], flux_right[2];
19                double w_left, h_left, uh_left, z_left, u_left, soundspeed_left;
20                double w_right, h_right, uh_right, z_right, u_right, soundspeed_right;
21                double z, s_max, s_min, denom;
22               
23                //printf("h0 = %f \n",h0);
24                w_left  = q_left[0];
25                uh_left = q_left[1]*normals;
26                h_left  = q_left[2];
27                z_left  = q_left[3];
28                u_left  = q_left[4]*normals;
29       
30                w_right  = q_right[0];
31                uh_right = q_right[1]*normals;
32                h_right  = q_right[2];
33                z_right  = q_right[3];
34                u_right  = q_right[4]*normals;         
35         
36                z = (z_left+z_right)/2.0;                 
37                                   
38                soundspeed_left = sqrt(g*h_left);
39                soundspeed_right = sqrt(g*h_right);
40               
41                s_max = max(u_left+soundspeed_left, u_right+soundspeed_right);
42                if (s_max < 0.0) s_max = 0.0;
43       
44                s_min = min(u_left-soundspeed_left, u_right-soundspeed_right);
45                if (s_min > 0.0) s_min = 0.0;
46               
47               
48                // Flux formulas
49                flux_left[0] = u_left*h_left;
50                flux_left[1] = u_left*u_left*h_left + 0.5*g*h_left*h_left;
51
52                flux_right[0] = u_right*h_right;
53                flux_right[1] = u_right*u_right*h_right + 0.5*g*h_right*h_right;
54
55                // Flux computation
56                denom = s_max-s_min;
57                if (denom < epsilon) {
58                        for (i=0; i<2; i++) edgeflux[i] = 0.0;
59                        *max_speed = 0.0;
60                } else {
61                        edgeflux[0] = s_max*flux_left[0] - s_min*flux_right[0];
62                        edgeflux[0] += s_max*s_min*(q_right[0]-q_left[0]);
63                        edgeflux[0] /= denom;
64                        edgeflux[1] = s_max*flux_left[1] - s_min*flux_right[1];
65                        edgeflux[1] += s_max*s_min*(q_right[1]-q_left[1]);
66                        edgeflux[1] /= denom;
67                        edgeflux[1] *= normals;
68   
69                        // Maximal wavespeed
70                        *max_speed = max(fabs(s_max), fabs(s_min));
71                } 
72                return 0;               
73}
74               
75               
76       
77       
78// Computational function for flux computation
79double _compute_fluxes_ext(
80                           double cfl,
81                           double timestep,
82                           double epsilon,
83                           double g,
84                           double h0,
85                           long* neighbours,
86                           long* neighbour_vertices,
87                           double* normals,
88                           double* areas,
89                           double* stage_edge_values,
90                           double* xmom_edge_values,
91                           double* bed_edge_values,
92                           double* height_edge_values,
93                           double* velocity_edge_values,
94                           double* stage_boundary_values,
95                           double* xmom_boundary_values,
96                           double* bed_boundary_values,
97                           double* height_boundary_values,
98                           double* velocity_boundary_values,
99                           double* stage_explicit_update,
100                           double* xmom_explicit_update,
101                           int number_of_elements,
102                           double* max_speed_array) {
103               
104                double flux[2], ql[5], qr[5], edgeflux[2];
105                double max_speed, normal;
106                int k, i, ki, n, m, nm=0;
107               
108               
109                for (k=0; k<number_of_elements; k++) {
110                        flux[0] = 0.0;
111                        flux[1] = 0.0;
112                       
113                        for (i=0; i<2; i++) {
114                                ki = k*2+i;
115                               
116                                ql[0] = stage_edge_values[ki];
117                                ql[1] = xmom_edge_values[ki];
118                                ql[2] = bed_edge_values[ki];
119                                ql[3] = height_edge_values[ki];
120                                ql[4] = velocity_edge_values[ki];
121                               
122                                n = neighbours[ki];
123                                if (n<0) {
124                                        m = -n-1;
125                                        qr[0] = stage_boundary_values[m];
126                                        qr[1] = xmom_boundary_values[m];
127                                        qr[2] = bed_edge_values[m];
128                                        qr[3] = height_edge_values[m];
129                                        qr[4] = velocity_edge_values[m];
130                                } else {
131                                        m = neighbour_vertices[ki];
132                                        nm = n*2+m;
133                                        qr[0] = stage_edge_values[nm];
134                                        qr[1] = xmom_edge_values[nm];
135                                        qr[2] = bed_edge_values[nm];
136                                        qr[3] = height_edge_values[nm];
137                                        qr[4] = velocity_edge_values[nm];
138                                }
139                               
140                                normal = normals[ki];
141                                _flux_function(ql, qr, normal, g, epsilon, h0, edgeflux, &max_speed);
142                                flux[0] -= edgeflux[0];
143                                flux[1] -= edgeflux[1];
144                               
145                                // Update timestep based on edge i and possibly neighbour n
146                                if (max_speed > epsilon) {
147                                    // Original CFL calculation
148                                   
149                                    timestep = min(timestep, 0.5*cfl*areas[k]/max_speed); 
150                                    if (n>=0) {
151                                        timestep = min(timestep, 0.5*cfl*areas[n]/max_speed); 
152                                    }
153                                }
154                        } // End edge i (and neighbour n)
155                        flux[0] /= areas[k];
156                        stage_explicit_update[k] = flux[0];
157                        flux[1] /= areas[k];
158                        xmom_explicit_update[k] = flux[1];
159                       
160                        //Keep track of maximal speeds
161                        max_speed_array[k]=max_speed;
162                }
163                return timestep;       
164        }
165
166//=========================================================================
167// Python Glue
168//=========================================================================
169PyObject *compute_fluxes_ext(PyObject *self, PyObject *args) {
170
171    PyObject
172        *domain,
173        *stage,
174        *xmom,
175        *bed,
176        *height,
177        *velocity;
178
179    PyArrayObject
180        *neighbours,
181        *neighbour_vertices,
182        *normals,
183        *areas,
184        *stage_vertex_values,
185        *xmom_vertex_values,
186        *bed_vertex_values,
187        *height_vertex_values,
188        *velocity_vertex_values,
189        *stage_boundary_values,
190        *xmom_boundary_values,
191        *bed_boundary_values,
192        *height_boundary_values,
193        *velocity_boundary_values,
194        *stage_explicit_update,
195        *xmom_explicit_update,
196        *max_speed_array;
197
198  double timestep, epsilon, g, h0, cfl;
199  int number_of_elements;
200
201
202  // Convert Python arguments to C
203  if (!PyArg_ParseTuple(args, "dOOOOOO",
204                        &timestep,
205                        &domain,
206                        &stage,
207                        &xmom,
208                        &bed,
209                        &height,
210                        &velocity)) {
211      PyErr_SetString(PyExc_RuntimeError, "comp_flux_ext.c: compute_fluxes_ext_short could not parse input");
212      return NULL;
213  }
214
215
216    epsilon           = get_python_double(domain,"epsilon");
217    g                 = get_python_double(domain,"g");
218    h0                = get_python_double(domain,"h0");
219    cfl               = get_python_double(domain,"cfl");
220
221
222    neighbours        = get_consecutive_array(domain, "neighbours");
223    neighbour_vertices= get_consecutive_array(domain, "neighbour_vertices");
224    normals           = get_consecutive_array(domain, "normals");
225    areas             = get_consecutive_array(domain, "areas");
226    max_speed_array   = get_consecutive_array(domain, "max_speed_array");
227
228    stage_vertex_values      = get_consecutive_array(stage,    "vertex_values");
229    xmom_vertex_values       = get_consecutive_array(xmom,     "vertex_values");
230    bed_vertex_values        = get_consecutive_array(bed,      "vertex_values");
231    height_vertex_values     = get_consecutive_array(height,   "vertex_values");
232    velocity_vertex_values   = get_consecutive_array(velocity, "vertex_values");
233
234    stage_boundary_values     = get_consecutive_array(stage,     "boundary_values");
235    xmom_boundary_values      = get_consecutive_array(xmom,      "boundary_values");
236    bed_boundary_values       = get_consecutive_array(bed,       "boundary_values");
237    height_boundary_values    = get_consecutive_array(height,    "boundary_values");
238    velocity_boundary_values  = get_consecutive_array(velocity,  "boundary_values");
239
240
241    stage_explicit_update = get_consecutive_array(stage, "explicit_update");
242    xmom_explicit_update  = get_consecutive_array(xmom,  "explicit_update");
243
244    number_of_elements = stage_vertex_values -> dimensions[0];
245
246    // Call underlying flux computation routine and update
247    // the explicit update arrays
248    timestep = _compute_fluxes_ext(
249        cfl,
250        timestep,
251        epsilon,
252        g,
253        h0,
254        (long*) neighbours -> data,
255        (long*) neighbour_vertices -> data,
256        (double*) normals -> data,
257        (double*) areas -> data,
258        (double*) stage_vertex_values -> data,
259        (double*) xmom_vertex_values -> data,
260        (double*) bed_vertex_values -> data,
261        (double*) height_vertex_values -> data,
262        (double*) velocity_vertex_values -> data,
263        (double*) stage_boundary_values -> data,
264        (double*) xmom_boundary_values -> data,
265        (double*) bed_boundary_values -> data,
266        (double*) height_boundary_values -> data,
267        (double*) velocity_boundary_values -> data,
268        (double*) stage_explicit_update -> data,
269        (double*) xmom_explicit_update -> data,
270        number_of_elements,
271        (double*) max_speed_array -> data);
272
273
274  Py_DECREF(neighbours);
275  Py_DECREF(neighbour_vertices);
276  Py_DECREF(normals);
277  Py_DECREF(areas);
278  Py_DECREF(stage_vertex_values);
279  Py_DECREF(xmom_vertex_values);
280  Py_DECREF(bed_vertex_values);
281  Py_DECREF(height_vertex_values);
282  Py_DECREF(velocity_vertex_values);
283  Py_DECREF(stage_boundary_values);
284  Py_DECREF(xmom_boundary_values);
285  Py_DECREF(bed_boundary_values);
286  Py_DECREF(height_boundary_values);
287  Py_DECREF(velocity_boundary_values);
288  Py_DECREF(stage_explicit_update);
289  Py_DECREF(xmom_explicit_update);
290  Py_DECREF(max_speed_array);
291
292
293  // Return updated flux timestep
294  return Py_BuildValue("d", timestep);
295}
296
297
298
299//-------------------------------
300// Method table for python module
301//-------------------------------
302
303static struct PyMethodDef MethodTable[] = {
304  {"compute_fluxes_ext", compute_fluxes_ext, METH_VARARGS, "Print out"},
305  {NULL, NULL}
306};
307
308// Module initialisation
309void initsww_comp_flux_ext(void){
310  Py_InitModule("sww_comp_flux_ext", MethodTable);
311  import_array();
312}
Note: See TracBrowser for help on using the repository browser.