Changeset 8726
- Timestamp:
- Mar 5, 2013, 2:40:28 PM (12 years ago)
- Location:
- trunk
- Files:
-
- 4 edited
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
trunk/anuga_core/source/anuga/file/sww.py
r8662 r8726 300 300 # 301 301 # In this branch it is assumed that elevation 302 # is also available as a quantity 302 # is also available as a quantity 303 304 305 # Smoothing for the get_vertex_values will be obtained 306 # from the smooth setting in domain 303 307 304 308 Q = domain.quantities['stage'] … … 543 547 # dimension definitions 544 548 outfile.createDimension('number_of_volumes', number_of_volumes) 549 outfile.createDimension('number_of_triangle_vertices', number_of_points) 545 550 outfile.createDimension('number_of_vertices', 3) 546 551 outfile.createDimension('numbers_in_range', 2) … … 708 713 # variable definitions 709 714 outfile.createVariable('tri_l2g', netcdf_int, ('number_of_volumes',)) 710 outfile.createVariable('node_l2g', netcdf_int, ('number_of_ points',))715 outfile.createVariable('node_l2g', netcdf_int, ('number_of_triangle_vertices',)) 711 716 outfile.createVariable('tri_full_flag', netcdf_int, ('number_of_volumes',)) 712 717 … … 717 722 outfile.variables['tri_l2g'][:] = tri_l2g.astype(num.int32) 718 723 719 #print node_l2g.shape724 print node_l2g.shape 720 725 #print node_l2g 721 #print outfile.variables['node_l2g'].shape726 print outfile.variables['node_l2g'].shape 722 727 723 728 outfile.variables['node_l2g'][:] = node_l2g.astype(num.int32) -
trunk/anuga_core/source/anuga/operators/kinematic_viscosity_operator.py
r8473 r8726 54 54 self.diffusivity = self.domain.get_quantity(diffusivity) 55 55 56 56 57 self.smooth = 0.001 58 57 59 assert isinstance(self.diffusivity, Quantity) 58 60 … … 114 116 """ Parabolic update of x and y velocity 115 117 116 (I + dt (div hgrad) ) U^{n+1} = U^{n}118 (I + dt (div d grad) ) U^{n+1} = U^{n} 117 119 118 120 """ -
trunk/anuga_core/source/anuga/operators/kinematic_viscosity_operator_ext.c
r8479 r8726 9 9 // taken from http://cprogramminglanguage.net/quicksort-algorithm-c-source-code.aspx 10 10 11 void swap(int *x,int *y) 12 { 13 int temp; 14 temp = *x; 15 *x = *y; 16 *y = temp; 17 } 18 19 int choose_pivot(int i,int j ) 20 { 21 return((i+j) /2); 22 } 23 24 void quicksort(int list[],int m,int n) 25 { 26 int key,i,j,k; 27 if( m < n) 28 { 29 k = choose_pivot(m,n); 30 swap(&list[m],&list[k]); 31 key = list[m]; 32 i = m+1; 33 j = n; 34 while(i <= j) 35 { 36 while((i <= n) && (list[i] <= key)) 11 void swap(int *x, int *y) { 12 int temp; 13 temp = *x; 14 *x = *y; 15 *y = temp; 16 } 17 18 int choose_pivot(int i, int j) { 19 return ((i + j) / 2); 20 } 21 22 void quicksort(int list[], int m, int n) { 23 int key, i, j, k; 24 if (m < n) { 25 k = choose_pivot(m, n); 26 swap(&list[m], &list[k]); 27 key = list[m]; 28 i = m + 1; 29 j = n; 30 while (i <= j) { 31 while ((i <= n) && (list[i] <= key)) 37 32 i++; 38 while((j >= m) && (list[j] > key))33 while ((j >= m) && (list[j] > key)) 39 34 j--; 40 if(i < j)41 swap(&list[i], &list[j]);42 }43 44 swap(&list[m],&list[j]);45 46 quicksort(list,m,j-1);47 quicksort(list,j+1,n);48 }49 } 50 51 int build_geo_structure(int n, 52 int tot_len,53 double *centroids,54 long *neighbours,55 double *edgelengths,56 double *edge_midpoints,57 long *geo_indices,58 35 if (i < j) 36 swap(&list[i], &list[j]); 37 } 38 // swap two elements 39 swap(&list[m], &list[j]); 40 // recursively sort the lesser list 41 quicksort(list, m, j - 1); 42 quicksort(list, j + 1, n); 43 } 44 } 45 46 int build_geo_structure(int n, 47 int tot_len, 48 double *centroids, 49 long *neighbours, 50 double *edgelengths, 51 double *edge_midpoints, 52 long *geo_indices, 53 double *geo_values) { 59 54 int i, edge, edge_counted, j, m; 60 61 62 for (i=0; i<n; i++) {63 64 this_x = centroids[2*i];65 this_y = centroids[2*i+1];66 for (edge=0; edge<3; edge++) {67 68 j = neighbours[3*i+edge];69 70 71 72 73 if (j < 0) {55 double dist, this_x, this_y, other_x, other_y, edge_length; 56 edge_counted = 0; 57 for (i = 0; i < n; i++) { 58 //The centroid coordinates of triangle i 59 this_x = centroids[2 * i]; 60 this_y = centroids[2 * i + 1]; 61 for (edge = 0; edge < 3; edge++) { 62 63 j = neighbours[3 * i + edge]; 64 65 //Get the index and the coordinates of the interacting point 66 67 // Edge 68 if (j < 0) { 74 69 m = -j - 1; 75 geo_indices[3*i+edge] = n + m; 76 edge_counted++; 77 78 other_x = edge_midpoints[2*(3*i+edge)]; 79 other_y = edge_midpoints[2*(3*i+edge)+1]; 80 } else { 81 geo_indices[3*i+edge] = j; 82 83 other_x = centroids[2*j]; 84 other_y = centroids[2*j+1]; 85 } 86 87 //Compute the interaction 88 edge_length = edgelengths[3*i+edge]; 89 dist = sqrt((this_x-other_x)*(this_x-other_x) + (this_y-other_y)*(this_y-other_y)); 90 geo_values[3*i+edge] = - edge_length / dist; 91 } 92 } 93 return 0; 94 } 95 70 geo_indices[3 * i + edge] = n + m; 71 edge_counted++; 72 73 other_x = edge_midpoints[2 * (3 * i + edge)]; 74 other_y = edge_midpoints[2 * (3 * i + edge) + 1]; 75 } else { 76 geo_indices[3 * i + edge] = j; 77 78 other_x = centroids[2 * j]; 79 other_y = centroids[2 * j + 1]; 80 } 81 82 //Compute the interaction 83 edge_length = edgelengths[3 * i + edge]; 84 dist = sqrt((this_x - other_x)*(this_x - other_x) + (this_y - other_y)*(this_y - other_y)); 85 geo_values[3 * i + edge] = -edge_length / dist; 86 } 87 } 88 return 0; 89 } 96 90 97 91 int build_elliptic_matrix_not_symmetric(int n, 98 int tot_len,99 long *geo_indices,100 double *geo_values,101 double *cell_data,102 double *bdry_data,103 double *data,104 long *colind) {105 int i, k, edge, j[4], sorted_j[4], this_index;106 double h_j, v[3], v_i; //v[k] = value of the interaction of edge k in a given triangle, v_i = (i,i) entry107 for (i=0; i<n; i++) {108 v_i = 0.0;109 j[3] = i;110 //Get the values of each interaction, and the column index at which they occur111 for (edge=0; edge<3; edge++) {112 j[edge] = geo_indices[3*i+edge];113 //if (j[edge]<n) { //interior114 // h_j = cell_data[j[edge]];115 //} else { //boundary116 // h_j = bdry_data[j[edge]-n];117 //}118 v[edge] = -cell_data[i]*geo_values[3*i+edge]; //the negative of the individual interaction119 v_i += cell_data[i]*geo_values[3*i+edge]; //sum the three interactions120 }121 if (cell_data[i]<=0.0) {122 v_i = 0.0;123 v[0] = 0.0;124 v[1] = 0.0;125 v[2] = 0.0;126 }127 //Organise the set of 4 values/indices into the data and colind arrays128 for (k=0; k<4; k++) sorted_j[k] = j[k];129 quicksort(sorted_j,0,3);130 for (k=0; k<4; k++) { //loop through the nonzero indices131 this_index = sorted_j[k];132 if (this_index == i) {133 data[4*i+k] = v_i;134 colind[4*i+k] = i;135 } else if (this_index == j[0]) {136 data[4*i+k] = v[0];137 colind[4*i+k] = j[0];138 } else if (this_index == j[1]) {139 data[4*i+k] = v[1];140 colind[4*i+k] = j[1];141 } else { //this_index == j[2]142 data[4*i+k] = v[2];143 colind[4*i+k] = j[2];144 }145 }146 }147 return 0;148 }149 150 int build_elliptic_matrix(int n,151 int tot_len,152 long *geo_indices,153 double *geo_values,154 double *cell_data,155 double *bdry_data,156 double *data,157 long *colind) {158 int i, k, edge, j[4], sorted_j[4], this_index;159 double h_j, v[3], v_i; //v[k] = value of the interaction of edge k in a given triangle, v_i = (i,i) entry160 for (i=0; i<n; i++) {161 v_i = 0.0;162 j[3] = i;163 //Get the values of each interaction, and the column index at which they occur164 for (edge=0; edge<3; edge++) {165 j[edge] = geo_indices[3*i+edge];166 if (j[edge]<n) { //interior167 h_j = cell_data[j[edge]];168 } else { //boundary169 h_j = bdry_data[j[edge]-n];170 }171 v[edge] = -0.5*(cell_data[i] + h_j)*geo_values[3*i+edge]; //the negative of the individual interaction172 v_i += 0.5*(cell_data[i] + h_j)*geo_values[3*i+edge]; //sum the three interactions173 }174 if (cell_data[i]<=0.0) {175 v_i = 0.0;176 v[0] = 0.0;177 v[1] = 0.0;178 v[2] = 0.0;179 }180 //Organise the set of 4 values/indices into the data and colind arrays181 for (k=0; k<4; k++) sorted_j[k] = j[k];182 quicksort(sorted_j,0,3);183 for (k=0; k<4; k++) { //loop through the nonzero indices184 this_index = sorted_j[k];185 if (this_index == i) {186 data[4*i+k] = v_i;187 colind[4*i+k] = i;188 } else if (this_index == j[0]) {189 data[4*i+k] = v[0];190 colind[4*i+k] = j[0];191 } else if (this_index == j[1]) {192 data[4*i+k] = v[1];193 colind[4*i+k] = j[1];194 } else { //this_index == j[2]195 data[4*i+k] = v[2];196 colind[4*i+k] = j[2];197 }198 }199 }200 return 0;201 }202 203 204 int update_elliptic_matrix_not_symmetric(int n,205 92 int tot_len, 206 93 long *geo_indices, … … 215 102 v_i = 0.0; 216 103 j[3] = i; 217 218 104 //Get the values of each interaction, and the column index at which they occur 219 105 for (edge = 0; edge < 3; edge++) { 220 106 j[edge] = geo_indices[3 * i + edge]; 221 if (j[edge] <n) { //interior222 h_j = cell_data[j[edge]];223 } else { //boundary224 h_j = bdry_data[j[edge] -n];225 }226 v[edge] = - 227 v_i += 107 //if (j[edge]<n) { //interior 108 // h_j = cell_data[j[edge]]; 109 //} else { //boundary 110 // h_j = bdry_data[j[edge]-n]; 111 //} 112 v[edge] = -cell_data[i] * geo_values[3 * i + edge]; //the negative of the individual interaction 113 v_i += cell_data[i] * geo_values[3 * i + edge]; //sum the three interactions 228 114 } 229 115 if (cell_data[i] <= 0.0) { … … 256 142 } 257 143 258 int update_elliptic_matrix(int n,144 int build_elliptic_matrix(int n, 259 145 int tot_len, 260 146 long *geo_indices, … … 269 155 v_i = 0.0; 270 156 j[3] = i; 271 272 157 //Get the values of each interaction, and the column index at which they occur 273 158 for (edge = 0; edge < 3; edge++) { … … 310 195 } 311 196 312 197 int update_elliptic_matrix_not_symmetric(int n, 198 int tot_len, 199 long *geo_indices, 200 double *geo_values, 201 double *cell_data, 202 double *bdry_data, 203 double *data, 204 long *colind) { 205 int i, k, edge, j[4], sorted_j[4], this_index; 206 double h_j, v[3], v_i; //v[k] = value of the interaction of edge k in a given triangle, v_i = (i,i) entry 207 for (i = 0; i < n; i++) { 208 v_i = 0.0; 209 j[3] = i; 210 211 //Get the values of each interaction, and the column index at which they occur 212 for (edge = 0; edge < 3; edge++) { 213 j[edge] = geo_indices[3 * i + edge]; 214 if (j[edge] < n) { //interior 215 h_j = cell_data[j[edge]]; 216 } else { //boundary 217 h_j = bdry_data[j[edge] - n]; 218 } 219 v[edge] = -cell_data[i] * geo_values[3 * i + edge]; //the negative of the individual interaction 220 v_i += cell_data[i] * geo_values[3 * i + edge]; //sum the three interactions 221 } 222 if (cell_data[i] <= 0.0) { 223 v_i = 0.0; 224 v[0] = 0.0; 225 v[1] = 0.0; 226 v[2] = 0.0; 227 } 228 //Organise the set of 4 values/indices into the data and colind arrays 229 for (k = 0; k < 4; k++) sorted_j[k] = j[k]; 230 quicksort(sorted_j, 0, 3); 231 for (k = 0; k < 4; k++) { //loop through the nonzero indices 232 this_index = sorted_j[k]; 233 if (this_index == i) { 234 data[4 * i + k] = v_i; 235 colind[4 * i + k] = i; 236 } else if (this_index == j[0]) { 237 data[4 * i + k] = v[0]; 238 colind[4 * i + k] = j[0]; 239 } else if (this_index == j[1]) { 240 data[4 * i + k] = v[1]; 241 colind[4 * i + k] = j[1]; 242 } else { //this_index == j[2] 243 data[4 * i + k] = v[2]; 244 colind[4 * i + k] = j[2]; 245 } 246 } 247 } 248 return 0; 249 } 250 251 int update_elliptic_matrix(int n, 252 int tot_len, 253 long *geo_indices, 254 double *geo_values, 255 double *cell_data, 256 double *bdry_data, 257 double *data, 258 long *colind) { 259 int i, k, edge, j[4], sorted_j[4], this_index; 260 double h_j, v[3], v_i; //v[k] = value of the interaction of edge k in a given triangle, v_i = (i,i) entry 261 for (i = 0; i < n; i++) { 262 v_i = 0.0; 263 j[3] = i; 264 265 //Get the values of each interaction, and the column index at which they occur 266 for (edge = 0; edge < 3; edge++) { 267 j[edge] = geo_indices[3 * i + edge]; 268 if (j[edge] < n) { //interior 269 h_j = cell_data[j[edge]]; 270 } else { //boundary 271 h_j = bdry_data[j[edge] - n]; 272 } 273 v[edge] = -0.5 * (cell_data[i] + h_j) * geo_values[3 * i + edge]; //the negative of the individual interaction 274 v_i += 0.5 * (cell_data[i] + h_j) * geo_values[3 * i + edge]; //sum the three interactions 275 } 276 if (cell_data[i] <= 0.0) { 277 v_i = 0.0; 278 v[0] = 0.0; 279 v[1] = 0.0; 280 v[2] = 0.0; 281 } 282 //Organise the set of 4 values/indices into the data and colind arrays 283 for (k = 0; k < 4; k++) sorted_j[k] = j[k]; 284 quicksort(sorted_j, 0, 3); 285 for (k = 0; k < 4; k++) { //loop through the nonzero indices 286 this_index = sorted_j[k]; 287 if (this_index == i) { 288 data[4 * i + k] = v_i; 289 colind[4 * i + k] = i; 290 } else if (this_index == j[0]) { 291 data[4 * i + k] = v[0]; 292 colind[4 * i + k] = j[0]; 293 } else if (this_index == j[1]) { 294 data[4 * i + k] = v[1]; 295 colind[4 * i + k] = j[1]; 296 } else { //this_index == j[2] 297 data[4 * i + k] = v[2]; 298 colind[4 * i + k] = j[2]; 299 } 300 } 301 } 302 return 0; 303 } 313 304 314 305 /** 315 * Python wrapper methods316 */306 * Python wrapper methods 307 */ 317 308 318 309 static PyObject *py_build_geo_structure(PyObject *self, PyObject *args) { … … 324 315 *edgelengths, 325 316 *edge_midpoint_coordinates, 326 327 328 317 *geo_indices, 318 *geo_values; 319 329 320 //Convert Python arguments to C 330 321 if (!PyArg_ParseTuple(args, "O", &kv_operator)) { … … 339 330 340 331 //Extract parameters 341 n = get_python_integer(kv_operator,"n");342 tot_len = get_python_integer(kv_operator, "tot_len");343 344 centroid_coordinates = get_consecutive_array(mesh, "centroid_coordinates");345 neighbours = get_consecutive_array(mesh, "neighbours");346 edgelengths = get_consecutive_array(mesh, "edgelengths");347 edge_midpoint_coordinates = get_consecutive_array(mesh, "edge_midpoint_coordinates");348 geo_indices = get_consecutive_array(kv_operator, "geo_structure_indices");349 geo_values = get_consecutive_array(kv_operator, "geo_structure_values");350 332 n = get_python_integer(kv_operator, "n"); 333 tot_len = get_python_integer(kv_operator, "tot_len"); 334 335 centroid_coordinates = get_consecutive_array(mesh, "centroid_coordinates"); 336 neighbours = get_consecutive_array(mesh, "neighbours"); 337 edgelengths = get_consecutive_array(mesh, "edgelengths"); 338 edge_midpoint_coordinates = get_consecutive_array(mesh, "edge_midpoint_coordinates"); 339 geo_indices = get_consecutive_array(kv_operator, "geo_structure_indices"); 340 geo_values = get_consecutive_array(kv_operator, "geo_structure_values"); 341 351 342 //Release 352 343 Py_DECREF(mesh); 353 354 err = build_geo_structure(n, tot_len,355 (double *)centroid_coordinates -> data,356 (long *)neighbours -> data,357 (double *)edgelengths->data,358 (double *)edge_midpoint_coordinates -> data,359 (long *)geo_indices -> data,360 (double *)geo_values -> data);344 345 err = build_geo_structure(n, tot_len, 346 (double *) centroid_coordinates -> data, 347 (long *) neighbours -> data, 348 (double *) edgelengths->data, 349 (double *) edge_midpoint_coordinates -> data, 350 (long *) geo_indices -> data, 351 (double *) geo_values -> data); 361 352 if (err != 0) { 362 353 PyErr_SetString(PyExc_RuntimeError, "Could not build geo structure"); 363 354 return NULL; 364 355 } 365 356 366 357 //Release the arrays 367 358 Py_DECREF(centroid_coordinates); … … 371 362 Py_DECREF(geo_indices); 372 363 Py_DECREF(geo_values); 373 364 374 365 return Py_BuildValue(""); 375 366 } 376 367 377 368 static PyObject *py_build_elliptic_matrix(PyObject *self, PyObject *args) { 378 379 380 381 382 383 384 385 386 387 388 369 PyObject *kv_operator; 370 int n, tot_len, err; 371 PyArrayObject 372 *cell_data, 373 *bdry_data, 374 *geo_indices, 375 *geo_values, 376 *_data, 377 *colind; 378 379 //Convert Python arguments to C 389 380 if (!PyArg_ParseTuple(args, "OOO", &kv_operator, &cell_data, &bdry_data)) { 390 381 PyErr_SetString(PyExc_RuntimeError, "build_elliptic_matrix could not parse input"); … … 393 384 394 385 395 n = get_python_integer(kv_operator,"n");396 tot_len = get_python_integer(kv_operator, "tot_len");397 398 399 geo_indices = get_consecutive_array(kv_operator,"geo_structure_indices");400 geo_values = get_consecutive_array(kv_operator,"geo_structure_values");401 _data = get_consecutive_array(kv_operator,"operator_data");402 colind = get_consecutive_array(kv_operator,"operator_colind");403 404 err = build_elliptic_matrix(n,tot_len,405 (long *)geo_indices -> data, 406 (double *)geo_values -> data, 407 (double *)cell_data -> data,408 (double *)bdry_data -> data,409 (double *)_data -> data, 410 (long *)colind -> data);386 n = get_python_integer(kv_operator, "n"); 387 tot_len = get_python_integer(kv_operator, "tot_len"); 388 389 390 geo_indices = get_consecutive_array(kv_operator, "geo_structure_indices"); 391 geo_values = get_consecutive_array(kv_operator, "geo_structure_values"); 392 _data = get_consecutive_array(kv_operator, "operator_data"); 393 colind = get_consecutive_array(kv_operator, "operator_colind"); 394 395 err = build_elliptic_matrix(n, tot_len, 396 (long *) geo_indices -> data, 397 (double *) geo_values -> data, 398 (double *) cell_data -> data, 399 (double *) bdry_data -> data, 400 (double *) _data -> data, 401 (long *) colind -> data); 411 402 if (err != 0) { 412 403 PyErr_SetString(PyExc_RuntimeError, "Could not get stage height interactions"); 413 404 return NULL; 414 405 } 415 416 417 418 419 420 406 407 Py_DECREF(geo_indices); 408 Py_DECREF(geo_values); 409 Py_DECREF(_data); 410 Py_DECREF(colind); 411 421 412 return Py_BuildValue(""); 422 413 } 423 414 424 425 415 static PyObject *py_update_elliptic_matrix(PyObject *self, PyObject *args) { 426 427 428 429 430 431 432 433 434 435 436 416 PyObject *kv_operator; 417 int n, tot_len, err; 418 PyArrayObject 419 *cell_data, 420 *bdry_data, 421 *geo_indices, 422 *geo_values, 423 *_data, 424 *colind; 425 426 //Convert Python arguments to C 437 427 if (!PyArg_ParseTuple(args, "OOO", &kv_operator, &cell_data, &bdry_data)) { 438 428 PyErr_SetString(PyExc_RuntimeError, "update_elliptic_matrix could not parse input"); … … 441 431 442 432 443 n = get_python_integer(kv_operator,"n");444 tot_len = get_python_integer(kv_operator, "tot_len");445 446 447 geo_indices = get_consecutive_array(kv_operator,"geo_structure_indices");448 geo_values = get_consecutive_array(kv_operator,"geo_structure_values");449 _data = get_consecutive_array(kv_operator,"operator_data");450 colind = get_consecutive_array(kv_operator,"operator_colind");451 452 err = update_elliptic_matrix(n,tot_len,453 (long *)geo_indices -> data,454 (double *)geo_values -> data,455 (double *)cell_data -> data,456 (double *)bdry_data -> data,457 (double *)_data -> data,458 (long *)colind -> data);433 n = get_python_integer(kv_operator, "n"); 434 tot_len = get_python_integer(kv_operator, "tot_len"); 435 436 437 geo_indices = get_consecutive_array(kv_operator, "geo_structure_indices"); 438 geo_values = get_consecutive_array(kv_operator, "geo_structure_values"); 439 _data = get_consecutive_array(kv_operator, "operator_data"); 440 colind = get_consecutive_array(kv_operator, "operator_colind"); 441 442 err = update_elliptic_matrix(n, tot_len, 443 (long *) geo_indices -> data, 444 (double *) geo_values -> data, 445 (double *) cell_data -> data, 446 (double *) bdry_data -> data, 447 (double *) _data -> data, 448 (long *) colind -> data); 459 449 if (err != 0) { 460 450 PyErr_SetString(PyExc_RuntimeError, "Could not get stage height interactions"); … … 462 452 } 463 453 464 465 466 467 454 Py_DECREF(geo_indices); 455 Py_DECREF(geo_values); 456 Py_DECREF(_data); 457 Py_DECREF(colind); 468 458 469 459 return Py_BuildValue(""); … … 472 462 473 463 static struct PyMethodDef MethodTable[] = { 474 {"build_geo_structure",py_build_geo_structure,METH_VARARGS,"Print out"},475 {"build_elliptic_matrix",py_build_elliptic_matrix,METH_VARARGS,"Print out"},476 {"update_elliptic_matrix",py_update_elliptic_matrix,METH_VARARGS,"Print out"},477 {NULL,NULL,0,NULL} // sentinel464 {"build_geo_structure", py_build_geo_structure, METH_VARARGS, "Print out"}, 465 {"build_elliptic_matrix", py_build_elliptic_matrix, METH_VARARGS, "Print out"}, 466 {"update_elliptic_matrix", py_update_elliptic_matrix, METH_VARARGS, "Print out"}, 467 {NULL, NULL, 0, NULL} // sentinel 478 468 }; 479 void initkinematic_viscosity_operator_ext(){ 469 470 void initkinematic_viscosity_operator_ext() { 480 471 (void) Py_InitModule("kinematic_viscosity_operator_ext", MethodTable); 481 472 import_array(); -
trunk/anuga_core/source/anuga/utilities/sww_merge.py
r8664 r8726 24 24 swwfiles = [ domain_global_name+"_P"+str(np)+"_"+str(v)+".sww" for v in range(np)] 25 25 26 _sww_merge_parallel (swwfiles, output, verbose, delete_old)26 _sww_merge_parallel_non_smooth(swwfiles, output, verbose, delete_old) 27 27 28 28 … … 176 176 177 177 178 def _sww_merge_parallel (swwfiles, output, verbose=False, delete_old=False):178 def _sww_merge_parallel_smooth(swwfiles, output, verbose=False, delete_old=False): 179 179 """ 180 180 Merge a list of sww files into a single file. … … 183 183 184 184 The sww files to be merged must have exactly the same timesteps. 185 186 It is assumed that the separate sww files have been stored in non_smooth 187 format. 185 188 186 189 Note that some advanced information and custom quantities may not be … … 429 432 os.remove(filename) 430 433 434 def _sww_merge_parallel_non_smooth(swwfiles, output, verbose=False, delete_old=False): 435 """ 436 Merge a list of sww files into a single file. 437 438 Used to merge files created by parallel runs. 439 440 The sww files to be merged must have exactly the same timesteps. 441 442 It is assumed that the separate sww files have been stored in non_smooth 443 format. 444 445 Note that some advanced information and custom quantities may not be 446 exported. 447 448 swwfiles is a list of .sww files to merge. 449 output is the output filename, including .sww extension. 450 verbose True to log output information 451 """ 452 453 if verbose: 454 print "MERGING SWW Files" 455 456 457 first_file = True 458 tri_offset = 0 459 for filename in swwfiles: 460 if verbose: 461 print 'Reading file ', filename, ':' 462 463 fid = NetCDFFile(filename, netcdf_mode_r) 464 465 if first_file: 466 467 times = fid.variables['time'][:] 468 n_steps = len(times) 469 number_of_timesteps = fid.dimensions['number_of_timesteps'] 470 #print n_steps, number_of_timesteps 471 starttime = int(fid.starttime) 472 473 out_s_quantities = {} 474 out_d_quantities = {} 475 476 477 xllcorner = fid.xllcorner 478 yllcorner = fid.yllcorner 479 480 number_of_global_triangles = int(fid.number_of_global_triangles) 481 number_of_global_nodes = int(fid.number_of_global_nodes) 482 number_of_global_triangle_vertices = 3*number_of_global_triangles 483 484 order = fid.order 485 xllcorner = fid.xllcorner; 486 yllcorner = fid.yllcorner ; 487 zone = fid.zone; 488 false_easting = fid.false_easting; 489 false_northing = fid.false_northing; 490 datum = fid.datum; 491 projection = fid.projection; 492 493 g_volumes = num.zeros((number_of_global_triangles,3),num.int) 494 g_x = num.zeros((number_of_global_nodes,),num.float32) 495 g_y = num.zeros((number_of_global_nodes,),num.float32) 496 497 g_points = num.zeros((number_of_global_nodes,2),num.float32) 498 499 quantities = ['elevation', 'stage', 'xmomentum', 'ymomentum'] 500 static_quantities = [] 501 dynamic_quantities = [] 502 503 for quantity in quantities: 504 # Test if elevation is static 505 if n_steps == fid.variables[quantity].shape[0]: 506 dynamic_quantities.append(quantity) 507 else: 508 static_quantities.append(quantity) 509 510 for quantity in static_quantities: 511 out_s_quantities[quantity] = num.zeros((3*number_of_global_triangles,),num.float32) 512 513 # Quantities are stored as a 2D array of timesteps x data. 514 for quantity in dynamic_quantities: 515 out_d_quantities[quantity] = \ 516 num.zeros((n_steps,3*number_of_global_triangles),num.float32) 517 518 description = 'merged:' + getattr(fid, 'description') 519 first_file = False 520 521 522 # Read in from files and add to global arrays 523 524 tri_l2g = fid.variables['tri_l2g'][:] 525 node_l2g = fid.variables['node_l2g'][:] 526 tri_full_flag = fid.variables['tri_full_flag'][:] 527 528 volumes = num.array(fid.variables['volumes'][:],dtype=num.int) 529 530 531 532 #l_volumes = num.zeros_like(volumes) 533 #l_old_volumes = num.zeros_like(volumes) 534 535 536 # Change the local node ids to global id in the 537 # volume array 538 539 540 print volumes 541 print volumes.shape 542 543 544 print fid.variables['x'][0:6] 545 print fid.variables['y'][0:6] 546 547 print fid.variables['x'].shape 548 549 550 g_n0 = node_l2g[volumes[:,0]].reshape(-1,1) 551 g_n1 = node_l2g[volumes[:,1]].reshape(-1,1) 552 g_n2 = node_l2g[volumes[:,2]].reshape(-1,1) 553 554 #print g_n0.shape 555 l_volumes = num.hstack((g_n0,g_n1,g_n2)) 556 557 #assert num.allclose(l_volumes, l_old_volumes) 558 559 # Just pick out the full triangles 560 #ftri_l2g = num.compress(tri_full_flag, tri_l2g) 561 562 #print l_volumes 563 #print tri_full_flag 564 print l_volumes.shape 565 print g_volumes.shape 566 print tri_l2g.shape 567 568 #fg_volumes = num.compress(tri_full_flag,l_volumes,axis=0) 569 g_volumes[tri_l2g] = l_volumes 570 571 572 573 574 #g_x[node_l2g] = fid.variables['x'] 575 #g_y[node_l2g] = fid.variables['y'] 576 577 g_points[tri_l2g,0] = fid.variables['x'] 578 g_points[tri_l2g,1] = fid.variables['y'] 579 580 581 #print number_of_timesteps 582 583 584 # FIXME SR: It seems that some of the "ghost" node quantity values 585 # are being storded. We should only store those nodes which are associated with 586 # full triangles. So we need an index array of "full" nodes, ie those in 587 # full triangles 588 589 #use numpy.compress and numpy.unique to get "full nodes 590 591 #f_volumes = num.compress(tri_full_flag,volumes,axis=0) 592 #fl_nodes = num.unique(f_volumes) 593 #f_node_l2g = node_l2g[fl_nodes] 594 595 #print len(node_l2g) 596 #print len(fl_nodes) 597 598 # Read in static quantities 599 for quantity in static_quantities: 600 #out_s_quantities[quantity][node_l2g] = \ 601 # num.array(fid.variables[quantity],dtype=num.float32) 602 q = fid.variables[quantity] 603 #print quantity, q.shape 604 out_s_quantities[quantity][f_node_l2g] = \ 605 num.array(q,dtype=num.float32)[fl_nodes] 606 607 608 #Collate all dynamic quantities according to their timestep 609 for quantity in dynamic_quantities: 610 q = fid.variables[quantity] 611 #print q.shape 612 for i in range(n_steps): 613 #out_d_quantities[quantity][i][node_l2g] = \ 614 # num.array(q[i],dtype=num.float32) 615 out_d_quantities[quantity][i][f_node_l2g] = \ 616 num.array(q[i],dtype=num.float32)[fl_nodes] 617 618 619 620 621 fid.close() 622 623 624 #--------------------------- 625 # Write out the SWW file 626 #--------------------------- 627 #print g_points.shape 628 629 #print number_of_global_triangles 630 #print number_of_global_nodes 631 632 633 if verbose: 634 print 'Writing file ', output, ':' 635 fido = NetCDFFile(output, netcdf_mode_w) 636 sww = Write_sww(static_quantities, dynamic_quantities) 637 sww.store_header(fido, starttime, 638 number_of_global_triangles, 639 number_of_global_nodes, 640 description=description, 641 sww_precision=netcdf_float32) 642 643 644 645 from anuga.coordinate_transforms.geo_reference import Geo_reference 646 geo_reference = Geo_reference() 647 648 sww.store_triangulation(fido, g_points, g_volumes, points_georeference=geo_reference) 649 650 fido.order = order 651 fido.xllcorner = xllcorner; 652 fido.yllcorner = yllcorner ; 653 fido.zone = zone; 654 fido.false_easting = false_easting; 655 fido.false_northing = false_northing; 656 fido.datum = datum; 657 fido.projection = projection; 658 659 sww.store_static_quantities(fido, verbose=verbose, **out_s_quantities) 660 661 # Write out all the dynamic quantities for each timestep 662 663 for i in range(n_steps): 664 fido.variables['time'][i] = times[i] 665 666 for q in dynamic_quantities: 667 q_values = out_d_quantities[q] 668 for i in range(n_steps): 669 fido.variables[q][i] = q_values[i] 670 671 # This updates the _range values 672 q_range = fido.variables[q + Write_sww.RANGE][:] 673 q_values_min = num.min(q_values) 674 if q_values_min < q_range[0]: 675 fido.variables[q + Write_sww.RANGE][0] = q_values_min 676 q_values_max = num.max(q_values) 677 if q_values_max > q_range[1]: 678 fido.variables[q + Write_sww.RANGE][1] = q_values_max 679 680 681 #print out_s_quantities 682 #print out_d_quantities 683 684 #print g_x 685 #print g_y 686 687 #print g_volumes 688 689 fido.close() 690 691 if delete_old: 692 import os 693 for filename in swwfiles: 694 695 if verbose: 696 print 'Deleting file ', filename, ':' 697 os.remove(filename) 698 699 700 701 702 703 431 704 if __name__ == "__main__": 432 705
Note: See TracChangeset
for help on using the changeset viewer.