Changeset 1134


Ignore:
Timestamp:
Mar 23, 2005, 11:21:51 AM (20 years ago)
Author:
prow
Message:

testing sww2domain

File:
1 edited

Legend:

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

    r1132 r1134  
    728728
    729729        from coordinate_transforms.redfearn import redfearn
    730 
     730        import os
    731731        fid1 = NetCDFFile('test_ha.nc','w')
    732732        fid2 = NetCDFFile('test_ua.nc','w')
     
    836836                   origin = (56, 0, 0))
    837837
     838        os.remove('test_va.nc')
     839        os.remove('test_ua.nc')
     840        os.remove('test_ha.nc')
     841        os.remove('test_e.nc')
    838842
    839843        #Read output file 'test.sww'
     
    859863
    860864        #Cleanup
    861         import os
    862865        os.remove('test.sww')
    863         os.remove('test_va.nc')
    864         os.remove('test_ua.nc')
    865         os.remove('test_ha.nc')
    866866
    867867
     
    939939        os.remove('small.sww')
    940940
     941    def test_sww2domain(self):
     942        ################################################
     943        #Create a test domain, and evolve and save it.
     944        ################################################
     945        from mesh_factory import rectangular
     946        from shallow_water import Domain, Reflective_boundary, Dirichlet_boundary,\
     947             Constant_height, Time_boundary, Transmissive_boundary
     948        from Numeric import array
     949
     950        #Create basic mesh
     951        points, vertices, boundary = rectangular(2,2)
     952
     953        #Create shallow water domain
     954        domain = Domain(points, vertices, boundary)
     955        domain.smooth = False
     956        domain.visualise = False
     957        domain.store = True
     958        domain.filename = 'bedslope'
     959        domain.default_order=2
     960        #Bed-slope and friction
     961        domain.set_quantity('elevation', lambda x,y: -x/3)
     962        domain.set_quantity('friction', 0.1)
     963        # Boundary conditions
     964        from math import sin, pi
     965        Br = Reflective_boundary(domain)
     966        Bt = Transmissive_boundary(domain)
     967        Bd = Dirichlet_boundary([0.2,0.,0.])
     968        Bw = Time_boundary(domain=domain,
     969                           f=lambda t: [(0.1*sin(t*2*pi)), 0.0, 0.0])
     970
     971        domain.set_boundary({'left': Bd, 'right': Br, 'top': Br, 'bottom': Br})
     972        domain.quantities_to_be_stored.extend(['xmomentum','ymomentum'])
     973        #Initial condition
     974        h = 0.05
     975        elevation = domain.quantities['elevation'].vertex_values
     976        domain.set_quantity('stage', elevation + h)
     977        #elevation = domain.get_quantity('elevation',location='unique vertices')
     978        #domain.set_quantity('stage', elevation + h,location='unique vertices')
     979
     980        domain.check_integrity()
     981        dir(domain)
     982        #Evolution
     983        for t in domain.evolve(yieldstep = 1, finaltime = 2.0):
     984        #    domain.write_time()
     985            pass
     986
     987
     988        ##########################################
     989        #Import the example's file as a new domain
     990        ##########################################
     991        from data_manager import sww2domain
     992        from Numeric import allclose
     993
     994        filename = domain.datadir+'\\'+domain.filename+'.sww'
     995
     996        domain2 = sww2domain(filename,fail_if_NaN=False,verbose = False)
     997
     998        ###################
     999        ##NOW TEST IT!!!
     1000        ##################
     1001
     1002        bits = ['xllcorner','yllcorner','vertex_coordinates','time','starttime']
     1003
     1004        for quantity in ['elevation']+domain.quantities_to_be_stored:
     1005            bits.append('get_quantity("%s")'%quantity)
     1006
     1007        for bit in bits:
     1008        #    print 'testing that domain.'+bit+' has been restored'
     1009            assert allclose(eval('domain.'+bit),eval('domain2.'+bit))
     1010
     1011        #print 'passed'
     1012
     1013
     1014    def test_sww2domain2(self):
     1015        ##################################################################
     1016        #Same as previous test, but this checks how NaNs are handled.
     1017        ##################################################################
     1018
     1019
     1020        from mesh_factory import rectangular
     1021        from shallow_water import Domain, Reflective_boundary, Dirichlet_boundary,\
     1022             Constant_height, Time_boundary, Transmissive_boundary
     1023        from Numeric import array
     1024
     1025        #Create basic mesh
     1026        points, vertices, boundary = rectangular(2,2)
     1027
     1028        #Create shallow water domain
     1029        domain = Domain(points, vertices, boundary)
     1030        domain.smooth = False
     1031        domain.visualise = False
     1032        domain.store = True
     1033        domain.filename = 'bedslope'
     1034        domain.default_order=2
     1035        domain.quantities_to_be_stored=['stage']
     1036
     1037        domain.set_quantity('elevation', lambda x,y: -x/3)
     1038        domain.set_quantity('friction', 0.1)
     1039
     1040        from math import sin, pi
     1041        Br = Reflective_boundary(domain)
     1042        Bt = Transmissive_boundary(domain)
     1043        Bd = Dirichlet_boundary([0.2,0.,0.])
     1044        Bw = Time_boundary(domain=domain,
     1045                           f=lambda t: [(0.1*sin(t*2*pi)), 0.0, 0.0])
     1046
     1047        domain.set_boundary({'left': Bd, 'right': Br, 'top': Br, 'bottom': Br})
     1048
     1049        h = 0.05
     1050        elevation = domain.quantities['elevation'].vertex_values
     1051        domain.set_quantity('stage', elevation + h)
     1052        #elevation = domain.get_quantity('elevation',location='unique vertices')
     1053        #domain.set_quantity('stage', elevation + h,location='unique vertices')
     1054
     1055        domain.check_integrity()
     1056       
     1057        for t in domain.evolve(yieldstep = 1, finaltime = 2.0):
     1058            pass
     1059            #domain.write_time()
     1060
     1061        ##################################
     1062        #Import the file as a new domain
     1063        ##################################
     1064        from data_manager import sww2domain
     1065        from Numeric import allclose
     1066
     1067        filename = domain.datadir+'\\'+domain.filename+'.sww'
     1068
     1069        #Fail because NaNs are present
     1070        try:
     1071            domain2 = sww2domain(filename,fail_if_NaN=True,verbose=False)
     1072            assert True == False
     1073        except:
     1074            #Now import it, filling NaNs to be 0
     1075            filler = 0
     1076            domain2 = sww2domain(filename,fail_if_NaN=False,NaN_filler = filler,verbose=False)
     1077
     1078        bits = ['xllcorner','yllcorner','vertex_coordinates','time','starttime']
     1079
     1080        for quantity in ['elevation']+domain.quantities_to_be_stored:
     1081            bits.append('get_quantity("%s")'%quantity)
     1082
     1083        for bit in bits:
     1084        #    print 'testing that domain.'+bit+' has been restored'
     1085            assert allclose(eval('domain.'+bit),eval('domain2.'+bit))
     1086
     1087        #print max(max(domain2.get_quantity('xmomentum')))
     1088        #print min(min(domain2.get_quantity('xmomentum')))
     1089        #print max(max(domain2.get_quantity('ymomentum')))
     1090        #print min(min(domain2.get_quantity('ymomentum')))
     1091
     1092        assert max(max(domain2.get_quantity('xmomentum')))==filler
     1093        assert min(min(domain2.get_quantity('xmomentum')))==filler
     1094        assert max(max(domain2.get_quantity('ymomentum')))==filler
     1095        assert min(min(domain2.get_quantity('ymomentum')))==filler
     1096
     1097        #print 'passed'
     1098
     1099        #cleanup
     1100        #import os
     1101        #os.remove(domain.datadir+'/'+domain.filename+'.sww')
    9411102
    9421103#-------------------------------------------------------------
Note: See TracChangeset for help on using the changeset viewer.