Changeset 265
- Timestamp:
- Sep 1, 2004, 2:54:04 PM (20 years ago)
- Location:
- inundation/ga/storm_surge/pyvolution
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
inundation/ga/storm_surge/pyvolution/quantity.py
r262 r265 31 31 self.vertex_values = zeros((N, 3), Float) 32 32 else: 33 self.vertex_values = array(vertex_values) 33 self.vertex_values = array(vertex_values).astype(Float) 34 34 35 35 N, V = self.vertex_values.shape … … 72 72 73 73 def interpolate_from_vertices_to_edges(self): 74 for k in range(self.vertex_values.shape[0]): 75 q0 = self.vertex_values[k, 0] 76 q1 = self.vertex_values[k, 1] 77 q2 = self.vertex_values[k, 2] 78 79 self.edge_values[k, 0] = 0.5*(q1+q2) 80 self.edge_values[k, 1] = 0.5*(q0+q2) 81 self.edge_values[k, 2] = 0.5*(q0+q1) 82 74 #Call correct module function 75 #(either from this module or C-extension) 76 interpolate_from_vertices_to_edges(self) 83 77 84 78 … … 285 279 286 280 287 def extrapolate_second_order(self): 281 282 def interpolate_from_vertices_to_edges(quantity): 283 """Compute edge values from vertex values using linear interpolation 284 """ 285 286 for k in range(quantity.vertex_values.shape[0]): 287 q0 = quantity.vertex_values[k, 0] 288 q1 = quantity.vertex_values[k, 1] 289 q2 = quantity.vertex_values[k, 2] 290 291 quantity.edge_values[k, 0] = 0.5*(q1+q2) 292 quantity.edge_values[k, 1] = 0.5*(q0+q2) 293 quantity.edge_values[k, 2] = 0.5*(q0+q1) 294 295 296 297 def extrapolate_second_order(quantity): 288 298 """Extrapolate conserved quantities from centroid to 289 299 vertices for each volume using … … 291 301 """ 292 302 293 a, b = self.compute_gradients()294 295 X = self.domain.get_vertex_coordinates()296 qc = self.centroid_values297 qv = self.vertex_values303 a, b = quantity.compute_gradients() 304 305 X = quantity.domain.get_vertex_coordinates() 306 qc = quantity.centroid_values 307 qv = quantity.vertex_values 298 308 299 309 #Check each triangle 300 for k in range( self.domain.number_of_elements):310 for k in range(quantity.domain.number_of_elements): 301 311 #Centroid coordinates 302 x, y = self.domain.centroids[k]312 x, y = quantity.domain.centroids[k] 303 313 304 314 #vertex coordinates … … 452 462 453 463 from quantity_ext import limit, compute_gradients,\ 454 extrapolate_second_order 464 extrapolate_second_order, interpolate_from_vertices_to_edges 465 -
inundation/ga/storm_surge/pyvolution/quantity_ext.c
r262 r265 127 127 vertex_values[k3+2] = centroid_values[k] + a[k]*(x2-x) + b[k]*(y2-y); 128 128 129 /*130 printf("V %f, %f, %f\n", vertex_values[k3+0],131 vertex_values[k3+1],132 vertex_values[k3+2]);133 */134 135 //printf("k=%d, a=%f, b=%f\n", k, a[k], b[k]);136 137 138 //printf("C %f, %f, %f\n", centroid_values[k],139 // centroid_values[k],140 // centroid_values[k]);141 142 //printf("X %f, %f, %f, %f, %f, %f\n", x0, y0, x1, y1, x2, y2);143 129 } 144 130 return 0; … … 146 132 147 133 134 135 136 int _interpolate(int N, 137 double* vertex_values, 138 double* edge_values) { 139 140 int k, k3; 141 double q0, q1, q2; 142 143 144 for (k=0; k<N; k++) { 145 k3 = 3*k; 146 147 q0 = vertex_values[k3 + 0]; 148 q1 = vertex_values[k3 + 1]; 149 q2 = vertex_values[k3 + 2]; 150 151 //printf("%f, %f, %f\n", q0, q1, q2); 152 edge_values[k3 + 0] = 0.5*(q1+q2); 153 edge_values[k3 + 1] = 0.5*(q0+q2); 154 edge_values[k3 + 2] = 0.5*(q0+q1); 155 } 156 return 0; 157 } 158 148 159 149 160 ///////////////////////////////////////////////// 150 161 // Gateways to Python 162 163 164 PyObject *interpolate_from_vertices_to_edges(PyObject *self, PyObject *args) { 165 166 PyObject *quantity; 167 PyArrayObject *vertex_values, *edge_values; 168 169 int N, err; 170 171 // Convert Python arguments to C 172 if (!PyArg_ParseTuple(args, "O", &quantity)) 173 return NULL; 174 175 vertex_values = (PyArrayObject*) 176 PyObject_GetAttrString(quantity, "vertex_values"); 177 if (!vertex_values) return NULL; 178 179 edge_values = (PyArrayObject*) 180 PyObject_GetAttrString(quantity, "edge_values"); 181 if (!edge_values) return NULL; 182 183 N = vertex_values -> dimensions[0]; 184 185 err = _interpolate(N, 186 (double*) vertex_values -> data, 187 (double*) edge_values -> data); 188 189 if (err != 0) { 190 PyErr_SetString(PyExc_RuntimeError, "Interpolate could not be computed"); 191 return NULL; 192 } 193 194 //Release and return 195 Py_DECREF(vertex_values); 196 Py_DECREF(edge_values); 197 198 return Py_BuildValue(""); 199 } 200 201 151 202 PyObject *compute_gradients(PyObject *self, PyObject *args) { 152 203 … … 272 323 PyObject_GetAttrString(quantity, "vertex_values"); 273 324 if (!vertex_values) return NULL; 325 326 327 /* 328 printf("In extrapolate C routine\n"); 329 printf("d0=%d, d1=%d\n", 330 vertex_values -> dimensions[0], 331 vertex_values -> dimensions[1]); 332 */ 333 334 vertex_values = (PyArrayObject*) 335 PyObject_GetAttrString(quantity, "vertex_values"); 336 if (!vertex_values) return NULL; 274 337 275 338 N = centroid_values -> dimensions[0]; … … 411 474 {"extrapolate_second_order", extrapolate_second_order, 412 475 METH_VARARGS, "Print out"}, 476 {"interpolate_from_vertices_to_edges", 477 interpolate_from_vertices_to_edges, 478 METH_VARARGS, "Print out"}, 413 479 {NULL, NULL, 0, NULL} /* sentinel */ 414 480 }; -
inundation/ga/storm_surge/pyvolution/show_balanced_limiters.py
r239 r265 80 80 81 81 #Evolve 82 for t in domain.evolve(yieldstep = 0.1, finaltime = 20):82 for t in domain.evolve(yieldstep = 0.1, finaltime = 50): 83 83 domain.write_time() 84 84 #print domain.quantities['level'].centroid_values[:6] -
inundation/ga/storm_surge/pyvolution/test_quantity.py
r262 r265 77 77 78 78 def test_interpolation2(self): 79 quantity = Quantity(self.mesh4,79 quantity = Conserved_quantity(self.mesh4, 80 80 [[1,2,3], [5,5,5], [0,0,9], [-6, 3, 3]]) 81 81 assert allclose(quantity.centroid_values, [2., 5., 3., 0.]) #Centroid 82 82 83 84 quantity.extrapolate_second_order() 85 86 assert allclose(quantity.vertex_values, [[2., 2., 2.], 87 [3.+2./3, 6.+2./3, 4.+2./3], 88 [7.5, 0.5, 1.], 89 [-5, -2.5, 7.5]]) 90 91 #print quantity.edge_values 83 92 assert allclose(quantity.edge_values, [[2.5, 2.0, 1.5], 84 93 [5., 5., 5.], … … 236 245 assert allclose(b[i], 1.0) 237 246 238 239 247 quantity.extrapolate_second_order() 240 248 249 #print quantity.vertex_values 241 250 assert allclose(quantity.vertex_values[1,0], 2.0) 242 251 assert allclose(quantity.vertex_values[1,1], 6.0) -
inundation/ga/storm_surge/pyvolution/util_ext.h
r258 r265 10 10 // Ole Nielsen, GA 2004 11 11 12 12 #include "Python.h" 13 #include "Numeric/arrayobject.h" 13 14 #include "math.h" 14 15 … … 49 50 } 50 51 52 53 void print_numeric_array(PyArrayObject *x) { 54 int i, j; 55 for (i=0; i<x->dimensions[0]; i++) { 56 for (j=0; j<x->dimensions[1]; j++) { 57 printf("%f ", *(double*) (x->data + i*x->strides[0] + j*x->strides[1])); 58 } 59 printf("\n"); 60 } 61 printf("\n"); 62 } 63 64 void print_numeric_vector(PyArrayObject *x) { 65 int i; 66 for (i=0; i<x->dimensions[0]; i++) { 67 printf("%f ", *(double*) (x->data + i*x->strides[0])); 68 } 69 printf("\n"); 70 }
Note: See TracChangeset
for help on using the changeset viewer.