[419] | 1 | """Example of shallow water wave equation. |
---|
| 2 | |
---|
| 3 | Specific methods pertaining to the 2D shallow water equation |
---|
| 4 | are imported from shallow_water |
---|
| 5 | for use with the generic finite volume framework |
---|
| 6 | |
---|
| 7 | A example of running this program is; |
---|
| 8 | python run_tsh.py visualise hill.tsh 0.05 1 |
---|
| 9 | """ |
---|
| 10 | |
---|
| 11 | ###################### |
---|
| 12 | # Module imports |
---|
| 13 | # |
---|
| 14 | |
---|
| 15 | import sys |
---|
| 16 | from os import sep, path |
---|
| 17 | sys.path.append('..'+sep+'pyvolution') |
---|
| 18 | |
---|
| 19 | from shallow_water import Domain, Reflective_boundary, Dirichlet_boundary,\ |
---|
[821] | 20 | Transmissive_boundary, Time_boundary, Add_value_to_region |
---|
[419] | 21 | from mesh_factory import rectangular |
---|
| 22 | from pmesh2domain import pmesh_to_domain, pmesh_to_domain_instance |
---|
| 23 | |
---|
| 24 | from Numeric import array |
---|
| 25 | |
---|
[885] | 26 | import time |
---|
[419] | 27 | |
---|
[924] | 28 | #from config import default_datadir |
---|
[866] | 29 | |
---|
[419] | 30 | ###################### |
---|
| 31 | # Domain |
---|
| 32 | |
---|
| 33 | import sys |
---|
| 34 | |
---|
[1152] | 35 | |
---|
| 36 | ######NEW |
---|
| 37 | def add_x_y(x, y): |
---|
| 38 | return x+y |
---|
| 39 | |
---|
| 40 | ######NEW |
---|
| 41 | |
---|
[419] | 42 | usage = "usage: %s ['visual'|'non-visual'] pmesh_file_name yieldstep finaltime" % path.basename(sys.argv[0]) |
---|
| 43 | |
---|
| 44 | if len(sys.argv) < 4: |
---|
| 45 | print usage |
---|
| 46 | else: |
---|
| 47 | if sys.argv[1][0] == "n" or sys.argv[1][0] == "N": |
---|
| 48 | visualise = False |
---|
| 49 | else: |
---|
| 50 | visualise = True |
---|
| 51 | filename = sys.argv[2] |
---|
| 52 | yieldstep = float(sys.argv[3]) |
---|
| 53 | finaltime = float(sys.argv[4]) |
---|
| 54 | |
---|
| 55 | print 'Creating domain from', filename |
---|
| 56 | domain = pmesh_to_domain_instance(filename, Domain) |
---|
| 57 | print "Number of triangles = ", len(domain) |
---|
| 58 | |
---|
| 59 | domain.checkpoint = False #True |
---|
[449] | 60 | domain.default_order = 1 |
---|
[419] | 61 | domain.visualise = visualise |
---|
[866] | 62 | domain.smooth = True |
---|
[925] | 63 | domain.set_datadir('.') |
---|
[419] | 64 | |
---|
| 65 | if (domain.visualise): |
---|
| 66 | domain.store = False #True #Store for visualisation purposes |
---|
| 67 | else: |
---|
| 68 | domain.store = True #True #Store for visualisation purposes |
---|
| 69 | domain.format = 'sww' #Native netcdf visualisation format |
---|
| 70 | |
---|
| 71 | file_path, filename = path.split(filename) |
---|
| 72 | filename, ext = path.splitext(filename) |
---|
| 73 | if domain.smooth is True: |
---|
| 74 | s = 'smooth' |
---|
| 75 | else: |
---|
| 76 | s = 'nonsmooth' |
---|
[421] | 77 | domain.filename = filename + '_' + s + '_ys'+ str(yieldstep) + \ |
---|
| 78 | '_ft' + str(finaltime) |
---|
[924] | 79 | print "Output being written to " + domain.get_datadir() + sep + \ |
---|
[419] | 80 | domain.filename + "." + domain.format |
---|
| 81 | |
---|
| 82 | |
---|
| 83 | #Set friction |
---|
[449] | 84 | manning = 0.07 |
---|
[809] | 85 | inflow_stage = 10.0 |
---|
[1152] | 86 | |
---|
| 87 | ######NEW |
---|
[419] | 88 | domain.set_quantity('friction', manning) |
---|
[1152] | 89 | domain.set_quantity('stage', add_x_y) |
---|
| 90 | domain.set_quantity('elevation', |
---|
| 91 | domain.quantities['stage'].vertex_values+ \ |
---|
| 92 | domain.quantities['elevation'].vertex_values) |
---|
| 93 | domain.set_quantity('stage', 0.0) |
---|
| 94 | ######NEW |
---|
[419] | 95 | |
---|
| 96 | ###################### |
---|
| 97 | # Boundary conditions |
---|
| 98 | # |
---|
| 99 | print 'Boundaries' |
---|
[718] | 100 | reflective = Reflective_boundary(domain) |
---|
[419] | 101 | Bt = Transmissive_boundary(domain) |
---|
| 102 | |
---|
| 103 | #Constant inflow |
---|
[809] | 104 | Bd = Dirichlet_boundary(array([10, 0.0, 0.0])) |
---|
[419] | 105 | |
---|
| 106 | #Time dependent inflow |
---|
| 107 | from math import sin, pi |
---|
| 108 | Bw = Time_boundary(domain=domain, |
---|
| 109 | f=lambda x: array([(1 + sin(x*pi/4))*\ |
---|
| 110 | (inflow_stage*(sin(2.5*x*pi)+0.7)),0,0])) |
---|
| 111 | |
---|
| 112 | |
---|
| 113 | print 'Available boundary tags are', domain.get_boundary_tags() |
---|
| 114 | |
---|
| 115 | #Set boundary conditions |
---|
| 116 | |
---|
| 117 | tags = {} |
---|
| 118 | tags['left'] = Bw |
---|
| 119 | tags['1'] = Time_boundary(domain=domain, |
---|
| 120 | f=lambda x: array([(1 + sin(x*pi/4))*\ |
---|
| 121 | (0.15*(sin(2.5*x*pi)+0.7)),0,0])) |
---|
| 122 | tags['wave'] = Bd |
---|
[866] | 123 | tags['wave'] = Time_boundary(domain=domain, |
---|
| 124 | f=lambda x: array([(1 + sin(x*pi/4))*\ |
---|
| 125 | (0.15*(sin(2.5*x*pi)+0.7)),0,0])) |
---|
[419] | 126 | tags['internal'] = None |
---|
| 127 | tags['levee'] = None |
---|
[718] | 128 | tags['0'] = reflective |
---|
| 129 | tags['wall'] = reflective |
---|
[809] | 130 | tags['external'] = reflective |
---|
| 131 | tags['exterior'] = reflective |
---|
[718] | 132 | tags['open'] = Bd |
---|
| 133 | tags['opening'] = None |
---|
[419] | 134 | |
---|
| 135 | domain.set_boundary(tags) |
---|
| 136 | |
---|
[718] | 137 | # region tags |
---|
| 138 | #domain.set_region(Set_region('mound', 'elevation', 100, location='unique vertices')) |
---|
[817] | 139 | domain.set_region(Add_value_to_region('floodplain', 'elevation', 100, location='unique vertices', initial_quantity='elevation')) |
---|
[718] | 140 | domain.set_region(Add_value_to_region('mound', 'elevation', 100, location='unique vertices', initial_quantity='elevation')) |
---|
[774] | 141 | domain.set_region(Add_value_to_region('reservoir', 'stage', 100.0,initial_quantity='elevation')) |
---|
[718] | 142 | |
---|
[419] | 143 | #print domain.quantities['elevation'].vertex_values |
---|
[774] | 144 | #print domain.quantities['stage'].vertex_values |
---|
[419] | 145 | |
---|
| 146 | domain.check_integrity() |
---|
| 147 | |
---|
| 148 | ###################### |
---|
| 149 | #Evolution |
---|
[885] | 150 | t0 = time.time() |
---|
[419] | 151 | for t in domain.evolve(yieldstep = yieldstep, finaltime = finaltime): |
---|
| 152 | domain.write_time() |
---|
| 153 | |
---|
[885] | 154 | print 'That took %.2f seconds' %(time.time()-t0) |
---|
[419] | 155 | |
---|
| 156 | |
---|