Changeset 4303


Ignore:
Timestamp:
Mar 14, 2007, 11:22:54 AM (18 years ago)
Author:
duncan
Message:

finishing a momentum check and adding another way of reading txt boundary files.

Location:
anuga_core/source/anuga/shallow_water
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • anuga_core/source/anuga/shallow_water/data_manager.py

    r4302 r4303  
    30303030
    30313031
    3032 def timefile2netcdf(filename, quantity_names = None):
     3032def timefile2netcdf(filename, quantity_names=None, time_as_seconds=False):
    30333033    """Template for converting typical text files with time series to
    30343034    NetCDF tms file.
     
    30433043      31/08/04 00:00:00, 1.328223 0 0
    30443044      31/08/04 00:15:00, 1.292912 0 0
     3045
     3046    or time (seconds), value0 value1 value2 ...
     3047   
     3048      0.0, 1.328223 0 0
     3049      0.1, 1.292912 0 0
    30453050
    30463051    will provide a time dependent function f(t) with three attributes
     
    30643069    assert len(fields) == 2, msg
    30653070
    3066     try:
    3067         starttime = calendar.timegm(time.strptime(fields[0], time_format))
    3068     except ValueError:
    3069         msg = 'First field in file %s must be' %filename
    3070         msg += ' date-time with format %s.\n' %time_format
    3071         msg += 'I got %s instead.' %fields[0]
    3072         raise DataTimeError, msg
     3071    if not time_as_seconds:
     3072        try:
     3073            starttime = calendar.timegm(time.strptime(fields[0], time_format))
     3074        except ValueError:
     3075            msg = 'First field in file %s must be' %filename
     3076            msg += ' date-time with format %s.\n' %time_format
     3077            msg += 'I got %s instead.' %fields[0]
     3078            raise DataTimeError, msg
     3079    else:
     3080        try:
     3081            starttime = float(fields[0])
     3082        except Error:
     3083            msg = "Bad time format"
     3084            raise DataTimeError, msg
    30733085
    30743086
     
    31023114    for i, line in enumerate(lines):
    31033115        fields = line.split(',')
    3104         realtime = calendar.timegm(time.strptime(fields[0], time_format))
     3116        if not time_as_seconds:
     3117            realtime = calendar.timegm(time.strptime(fields[0], time_format))
     3118        else:
     3119             realtime = float(fields[0])
    31053120        T[i] = realtime - starttime
    31063121
     
    31263141    #FIXME: Use Georef
    31273142    fid.starttime = starttime
    3128 
    31293143
    31303144    # dimension definitions
     
    48664880    ymomentum[slice_index] = va*h
    48674881
    4868 def URS2txt(basename_in):
     4882def urs2txt(basename_in, location_index=None):
    48694883    """
    48704884    Not finished or tested
     
    48754889    quantities = ['HA','UA','VA']
    48764890
     4891    d = ","
     4892   
    48774893    # instanciate urs_points of the three mux files.
    48784894    mux = {}
     
    48864902   
    48874903    # Convert to utm
    4888     lat = a_mux.lonlatdep[:,1]
    4889     long = a_mux.lonlatdep[:,0]
     4904    latitudes = a_mux.lonlatdep[:,1]
     4905    longitudes = a_mux.lonlatdep[:,0]
    48904906    points_utm, zone = convert_from_latlon_to_utm( \
    4891         latitudes=lat, longitudes=long)
     4907        latitudes=latitudes, longitudes=longitudes)
    48924908    #print "points_utm", points_utm
    48934909    #print "zone", zone
    4894     elevations = a_mux.lonlatdep[:,2] * -1 #
     4910    depths = a_mux.lonlatdep[:,2] #
    48954911   
    48964912    fid = open(basename_in + '.txt', 'w')
    48974913
    4898     fid.write("zone" + str(zone) + "\n")
    4899 
    4900     for elevation, point_utm in map(None, elevations, points_utm):
    4901         pass
     4914    fid.write("zone: " + str(zone) + "\n")
     4915
     4916    if location_index is not None:
     4917        #Title
     4918        li = location_index
     4919        fid.write('location_index'+d+'lat'+d+ 'long' +d+ 'Easting' +d+ \
     4920                  'Northing' + "\n")
     4921        fid.write(str(li) +d+ str(latitudes[li])+d+ \
     4922              str(longitudes[li]) +d+ str(points_utm[li][0]) +d+ \
     4923              str(points_utm[li][01]) + "\n")
     4924
     4925    # the non-time dependent stuff
     4926    #Title
     4927    fid.write('location_index'+d+'lat'+d+ 'long' +d+ 'Easting' +d+ \
     4928                  'Northing' +d+ 'depth m' + "\n")
     4929    i = 0
     4930    for depth, point_utm, lat, long in map(None, depths,
     4931                                               points_utm, latitudes,
     4932                                               longitudes):
     4933       
     4934        fid.write(str(i) +d+ str(lat)+d+ str(long) +d+ str(point_utm[0]) +d+ \
     4935                  str(point_utm[01]) +d+ str(depth) + "\n")
     4936        i +=1
     4937    #Time dependent
     4938    if location_index is not None:
     4939        time_step = a_mux.time_step
     4940        i = 0
     4941        #Title
     4942        fid.write('time' +d+ 'HA depth m'+d+ \
     4943                      'UA momentum East x m/sec' +d+ 'VA momentum North y m/sec' \
     4944                      + "\n")
     4945        for HA, UA, VA in map(None, mux['HA'], mux['UA'], mux['VA']):
     4946            fid.write(str(i*time_step) +d+ str(HA[location_index])+d+ \
     4947                      str(UA[location_index]) +d+ str(VA[location_index]) \
     4948                      + "\n")
     4949           
     4950            i +=1
    49024951   
    49034952class Urs_points:
  • anuga_core/source/anuga/shallow_water/test_data_manager.py

    r4302 r4303  
    49394939        ha=2
    49404940        ua=5
    4941         va=5
     4941        va=10
    49424942        base_name, files = self.write_mux(lat_long_points,
    49434943                                          time_step_count, time_step,
     
    49694969        elevation = fid.variables['elevation'][:]
    49704970        #assert allclose(stage[0,0], e + tide)  #Meters
    4971 
     4971        #print "xmomentum", xmomentum
     4972        #print "ymomentum", ymomentum
    49724973        #Check the momentums - ua
    4973         #momentum = velocity*(stage-elevation)
    4974         #momentum = velocity*(stage+elevation)
    4975         # -(-elevation) since elevation is inverted in mux files
    4976         # = n*(e+tide+n) based on how I'm writing these files
    4977         #answer = n*(e+tide+n)
     4974        #momentum = velocity*water height
     4975        #water height = mux_depth + mux_height +tide
     4976        #water height = mux_depth + mux_height +tide
     4977        #momentum = velocity*(mux_depth + mux_height +tide)
     4978        #
     4979       
     4980        answer = 115
    49784981        actual = xmomentum[0,0]
    4979         #assert allclose(answer, actual)  #Meters
     4982        assert allclose(answer, actual)  #Meters^2/ sec
     4983        answer = 230
     4984        actual = ymomentum[0,0]
     4985        assert allclose(answer, actual)  #Meters^2/ sec
    49804986
    49814987        # check the stage values, first time step.
     
    57335739        # extend this so it interpolates onto the boundary.
    57345740        # have it fail if there is NaN
     5741
     5742    def not_really_test_urs2txt(self):
     5743        # not really a test, since it doesn't check the output data
     5744       
     5745        #This will write 3 non-gridded mux files, for testing.
     5746        #If no quantities are passed in,
     5747        #na and va quantities will be the Easting values.
     5748        #Depth and ua will be the Northing value.
     5749        # this was manually checked to be correct
     5750       
     5751        tide = 1
     5752        time_step_count = 3
     5753        time_step = 2
     5754
     5755        #Zone:   50   
     5756        #Easting:  240992.578  Northing: 7620442.472
     5757        #Latitude:   -21  30 ' 0.00000 ''  Longitude: 114  30 ' 0.00000 ''
     5758
     5759        # This is gridded
     5760        lat_long_points =[(-21.5,114.5),(-21,114.5),(-21.5,115), (-21.,115.)]
     5761        base_name, files = self.write_mux(lat_long_points,
     5762                                          time_step_count, time_step)
     5763        urs2txt(base_name, 0)
     5764        print "base_name", base_name
     5765       
     5766        self.delete_mux(files)
     5767        #os.remove(sww_file)
     5768        # delete the txt file if this becomes automatic
     5769       
     5770    def daves_urs2txt(self):
     5771        # not really a test, since it doesn't check the output data
     5772       
     5773        #This will write 3 non-gridded mux files, for testing.
     5774        #If no quantities are passed in,
     5775        #na and va quantities will be the Easting values.
     5776        #Depth and ua will be the Northing value.
     5777        # this was manually checked to be correct
     5778       
     5779        tide = 1
     5780        time_step_count = 3
     5781        time_step = 2
     5782
     5783        #Zone:   50   
     5784        #Easting:  240992.578  Northing: 7620442.472
     5785        #Latitude:   -21  30 ' 0.00000 ''  Longitude: 114  30 ' 0.00000 ''
     5786
     5787        # This is gridded
     5788        lat_long_points =[(-21.5,114.5),(-21,114.5),(-21.5,115), (-21.,115.)]
     5789        base_name, files = self.write_mux(lat_long_points,
     5790                                          time_step_count, time_step)
     5791        urs2txt(base_name, 0)
     5792        print "base_name", base_name
     5793       
     5794        self.delete_mux(files)
     5795        #os.remove(sww_file)
     5796        # delete the txt file if this becomes automatic
    57355797#-------------------------------------------------------------
    57365798if __name__ == "__main__":
    5737     #suite = unittest.makeSuite(Test_Data_Manager,'davids_test_points_urs_ungridded2sww')
     5799    #suite = unittest.makeSuite(Test_Data_Manager,'test_urs2txt')
    57385800    #suite = unittest.makeSuite(Test_Data_Manager,'cache_test_URS_points_needed_and_urs_ungridded2sww')
    5739     #suite = unittest.makeSuite(Test_Data_Manager,'visual_test_URS_points_needed_and_urs_ungridded2sww')
     5801    #suite = unittest.makeSuite(Test_Data_Manager,'test_urs2sww_momentum')
    57405802    suite = unittest.makeSuite(Test_Data_Manager,'test')
    57415803    runner = unittest.TextTestRunner()
  • anuga_core/source/anuga/shallow_water/test_shallow_water_domain.py

    r4259 r4303  
    13971397        from data_manager import timefile2netcdf       
    13981398        timefile2netcdf(filename)
     1399        os.remove(filename + '.txt')
     1400
     1401       
     1402        #Setup wind stress
     1403        F = file_function(filename + '.tms', quantities = ['Attribute0',
     1404                                                           'Attribute1'])
     1405        os.remove(filename + '.tms')
     1406       
     1407
     1408        #print 'F(5)', F(5)
     1409
     1410        #print 'F(5,x,y)', F(5,x=zeros(3),y=zeros(3))
     1411       
     1412        #print dir(F)
     1413        #print F.T
     1414        #print F.precomputed_values
     1415        #
     1416        #F = file_function(filename + '.txt')       
     1417        #
     1418        #print dir(F)
     1419        #print F.T       
     1420        #print F.Q
     1421       
     1422        W = Wind_stress(F)
     1423
     1424        domain.forcing_terms = []
     1425        domain.forcing_terms.append(W)
     1426
     1427        domain.compute_forcing_terms()
     1428
     1429        #Compute reference solution
     1430        const = eta_w*rho_a/rho_w
     1431
     1432        N = len(domain) # number_of_triangles
     1433
     1434        t = domain.time
     1435
     1436        s = speed(t,[1],[0])[0]
     1437        phi = angle(t,[1],[0])[0]
     1438
     1439        #Convert to radians
     1440        phi = phi*pi/180
     1441
     1442
     1443        #Compute velocity vector (u, v)
     1444        u = s*cos(phi)
     1445        v = s*sin(phi)
     1446
     1447        #Compute wind stress
     1448        S = const * sqrt(u**2 + v**2)
     1449
     1450        for k in range(N):
     1451            assert allclose(domain.quantities['stage'].explicit_update[k], 0)
     1452            assert allclose(domain.quantities['xmomentum'].explicit_update[k], S*u)
     1453            assert allclose(domain.quantities['ymomentum'].explicit_update[k], S*v)
     1454
     1455
     1456    def test_windfield_from_file_seconds(self):
     1457        from anuga.config import rho_a, rho_w, eta_w
     1458        from math import pi, cos, sin, sqrt
     1459        from anuga.config import time_format
     1460        from anuga.abstract_2d_finite_volumes.util import file_function
     1461        import time
     1462
     1463
     1464        a = [0.0, 0.0]
     1465        b = [0.0, 2.0]
     1466        c = [2.0, 0.0]
     1467        d = [0.0, 4.0]
     1468        e = [2.0, 2.0]
     1469        f = [4.0, 0.0]
     1470
     1471        points = [a, b, c, d, e, f]
     1472        #bac, bce, ecf, dbe
     1473        vertices = [ [1,0,2], [1,2,4], [4,2,5], [3,1,4]]
     1474
     1475        domain = Domain(points, vertices)
     1476
     1477        #Flat surface with 1m of water
     1478        domain.set_quantity('elevation', 0)
     1479        domain.set_quantity('stage', 1.0)
     1480        domain.set_quantity('friction', 0)
     1481
     1482        Br = Reflective_boundary(domain)
     1483        domain.set_boundary({'exterior': Br})
     1484
     1485
     1486        domain.time = 7 #Take a time that is represented in file (not zero)
     1487
     1488        #Write wind stress file (ensure that domain.time is covered)
     1489        #Take x=1 and y=0
     1490        filename = 'test_windstress_from_file'
     1491        start = time.mktime(time.strptime('2000', '%Y'))
     1492        fid = open(filename + '.txt', 'w')
     1493        dt = 0.5 #1  #One second interval
     1494        t = 0.0
     1495        while t <= 10.0:
     1496            fid.write('%s, %f %f\n' %(str(t),
     1497                                      speed(t,[1],[0])[0],
     1498                                      angle(t,[1],[0])[0]))
     1499            t += dt
     1500
     1501        fid.close()
     1502
     1503
     1504        #Convert ASCII file to NetCDF (Which is what we really like!)
     1505        from data_manager import timefile2netcdf       
     1506        timefile2netcdf(filename, time_as_seconds=True)
    13991507        os.remove(filename + '.txt')
    14001508
Note: See TracChangeset for help on using the changeset viewer.