Changeset 1007


Ignore:
Timestamp:
Mar 4, 2005, 4:58:03 PM (20 years ago)
Author:
ole
Message:

Added test for balance_deep_and_shallow:
That stage is conserved
Also checked that C-version produces the same as the Python version.
More tests of this would be nice

Location:
inundation/ga/storm_surge/pyvolution
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • inundation/ga/storm_surge/pyvolution/shallow_water.py

    r998 r1007  
    696696    wv = domain.quantities['stage'].vertex_values
    697697    zv = domain.quantities['elevation'].vertex_values
    698     hv = wv-zv
     698    hv = wv - zv
    699699
    700700    #Call C-extension
    701701    from shallow_water_ext import h_limiter
    702     h_limiter(domain, hc, hv)
    703 
    704     return hv
     702    hvbar = h_limiter(domain, hc, hv)
     703
     704    return hvbar
    705705
    706706                                           
  • inundation/ga/storm_surge/pyvolution/shallow_water_ext.c

    r1000 r1007  
    239239      alpha = 1.0;  //Flat bed
    240240
     241    //printf("dz = %.3f, alpha = %.8f\n", dz, alpha);
    241242
    242243    //  Let
     
    743744  PyArrayObject
    744745    *hv, *hc, //Depth at vertices and centroids       
    745     //*hvbar,   //Limited depth at vertices (return values) FIXME: Not used?
     746    *hvbar,   //Limited depth at vertices (return values) FIXME: Not used?
    746747    *neighbours;
    747748   
    748749  int k, i, n, N, k3;
    749   //int dimensions[2];
     750  int dimensions[2];
    750751  double beta_h; //Safety factor (see config.py)
    751752  double *hmin, *hmax, hn;
     
    769770 
    770771  //Create hvbar
    771   //dimensions[0] = N;
    772   //dimensions[1] = 3; 
    773   //hvbar = (PyArrayObject *) PyArray_FromDims(2, dimensions, PyArray_DOUBLE);
     772  dimensions[0] = N;
     773  dimensions[1] = 3; 
     774  hvbar = (PyArrayObject *) PyArray_FromDims(2, dimensions, PyArray_DOUBLE);
    774775 
    775776
     
    785786    for (i=0; i<3; i++) {
    786787      n = ((long*) neighbours -> data)[k3+i];
    787       //((double*) hvbar -> data)[k3+i] = ((double*) hc -> data)[k]; 
     788     
     789      //Initialise hvbar with valus from hv
     790      ((double*) hvbar -> data)[k3+i] = ((double*) hv -> data)[k3+i]; 
     791     
    788792      if (n >= 0) {
    789793        hn = ((double*) hc -> data)[n]; //Neighbour's centroid value
     
    796800 
    797801  // Call underlying routine
    798   _limit(N, beta_h, (double*) hc -> data, (double*) hv -> data, hmin, hmax);
     802  _limit(N, beta_h, (double*) hc -> data, (double*) hvbar -> data, hmin, hmax);
    799803         
    800804  // // //Py_DECREF(domain); //FIXME: NEcessary?         
     
    803807 
    804808  //return result using PyArray to avoid memory leak 
    805   //return PyArray_Return(hvbar);
    806   return Py_BuildValue("");   
     809  return PyArray_Return(hvbar);
     810  //return Py_BuildValue("");   
    807811}
    808812
  • inundation/ga/storm_surge/pyvolution/test_shallow_water.py

    r907 r1007  
    14451445        assert allclose(X[1,:], [0.0143507718962, 0.0142502147066, 0.00931268339717])
    14461446        assert allclose(Y[1,:], [-0.000117062180693, 7.94434448109e-005, -0.000151505429018])
    1447        
     1447
     1448       
     1449       
     1450    def test_balance_deep_and_shallow(self):
     1451        """Test that balanced limiters preserve conserved quantites.
     1452        """
     1453        import copy
     1454       
     1455        a = [0.0, 0.0]
     1456        b = [0.0, 2.0]
     1457        c = [2.0, 0.0]
     1458        d = [0.0, 4.0]
     1459        e = [2.0, 2.0]
     1460        f = [4.0, 0.0]
     1461
     1462        points = [a, b, c, d, e, f]
     1463
     1464        #bac, bce, ecf, dbe
     1465        elements = [ [1,0,2], [1,2,4], [4,2,5], [3,1,4] ]
     1466
     1467        mesh = Domain(points, elements)       
     1468        mesh.check_integrity()
     1469
     1470        #Create a deliberate overshoot
     1471        mesh.set_quantity('stage', [[3,0,3], [2,2,6], [5,3,8], [8,3,5]])
     1472        mesh.set_quantity('elevation', 0) #Flat bed
     1473        stage = mesh.quantities['stage']       
     1474       
     1475        ref_centroid_values = copy.copy(stage.centroid_values[:]) #Copy
     1476   
     1477        #Limit
     1478        mesh.distribute_to_vertices_and_edges()
     1479
     1480        #Assert that quantities are conserved
     1481        from Numeric import sum
     1482        for k in range(mesh.number_of_elements):
     1483            assert allclose (ref_centroid_values[k],
     1484                             sum(stage.vertex_values[k,:])/3)
     1485       
     1486
     1487        #Now try with a non-flat bed - closely hugging initial stage in places
     1488        #This will create alphas in the range [0, 0.478260, 1]
     1489        mesh.set_quantity('stage', [[3,0,3], [2,2,6], [5,3,8], [8,3,5]])
     1490        mesh.set_quantity('elevation', [[0,0,0],
     1491                                        [1.8,1.9,5.9],
     1492                                        [4.6,0,0],
     1493                                        [0,2,4]])
     1494        stage = mesh.quantities['stage']       
     1495       
     1496        ref_centroid_values = copy.copy(stage.centroid_values[:]) #Copy
     1497        ref_vertex_values = copy.copy(stage.vertex_values[:]) #Copy     
     1498   
     1499        #Limit
     1500        mesh.distribute_to_vertices_and_edges()
     1501
     1502       
     1503        #Assert that all vertex quantities have changed
     1504        for k in range(mesh.number_of_elements):
     1505            #print ref_vertex_values[k,:], stage.vertex_values[k,:]     
     1506            assert not allclose (ref_vertex_values[k,:], stage.vertex_values[k,:])             
     1507        #and assert that quantities are still conserved
     1508        from Numeric import sum
     1509        for k in range(mesh.number_of_elements):
     1510            assert allclose (ref_centroid_values[k],
     1511                             sum(stage.vertex_values[k,:])/3)
     1512       
     1513   
     1514        #Also check that Python and C version produce the same
     1515        assert allclose (stage.vertex_values,
     1516                         [[2,2,2],
     1517                          [1.93333333, 2.03333333, 6.03333333],
     1518                          [6.93333333, 4.53333333, 4.53333333],
     1519                          [5.33333333, 5.33333333, 5.33333333]])
     1520                         
     1521                         
     1522
     1523       
    14481524       
    14491525    def test_second_order_flat_bed_onestep(self):
     
    26932769if __name__ == "__main__":
    26942770    suite = unittest.makeSuite(TestCase,'test')
    2695     #suite = unittest.makeSuite(TestCase,'test_spatio_temporal_boundary_2')
     2771    #suite = unittest.makeSuite(TestCase,'test_balance_deep_and_shallow')
    26962772    runner = unittest.TextTestRunner()
    26972773    runner.run(suite)
Note: See TracChangeset for help on using the changeset viewer.