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,\ |
---|
20 | Transmissive_boundary, Time_boundary |
---|
21 | from region import Add_value_to_region, Set_region |
---|
22 | from mesh_factory import rectangular |
---|
23 | from pmesh2domain import pmesh_to_domain_instance |
---|
24 | |
---|
25 | from Numeric import array |
---|
26 | |
---|
27 | import time |
---|
28 | |
---|
29 | #from config import default_datadir |
---|
30 | |
---|
31 | ###################### |
---|
32 | # Domain |
---|
33 | |
---|
34 | import sys |
---|
35 | |
---|
36 | |
---|
37 | ######NEW |
---|
38 | def add_x_y(x, y): |
---|
39 | return x+y |
---|
40 | |
---|
41 | ######NEW |
---|
42 | |
---|
43 | usage = "usage: %s ['visual'|'non-visual'] pmesh_file_name yieldstep finaltime" % path.basename(sys.argv[0]) |
---|
44 | |
---|
45 | if len(sys.argv) < 4: |
---|
46 | print usage |
---|
47 | else: |
---|
48 | if sys.argv[1][0] == "n" or sys.argv[1][0] == "N": |
---|
49 | visualise = False |
---|
50 | else: |
---|
51 | visualise = True |
---|
52 | filename = sys.argv[2] |
---|
53 | yieldstep = float(sys.argv[3]) |
---|
54 | finaltime = float(sys.argv[4]) |
---|
55 | |
---|
56 | print 'Creating domain from', filename |
---|
57 | domain = pmesh_to_domain_instance(filename, Domain) |
---|
58 | print "Number of triangles = ", len(domain) |
---|
59 | print "domain.geo_reference",domain.geo_reference |
---|
60 | domain.checkpoint = False #True |
---|
61 | domain.default_order = 1 |
---|
62 | domain.visualise = visualise |
---|
63 | domain.smooth = True |
---|
64 | domain.set_datadir('.') |
---|
65 | |
---|
66 | if (domain.visualise): |
---|
67 | domain.store = False #True #Store for visualisation purposes |
---|
68 | else: |
---|
69 | domain.store = True #True #Store for visualisation purposes |
---|
70 | domain.format = 'sww' #Native netcdf visualisation format |
---|
71 | |
---|
72 | file_path, filename = path.split(filename) |
---|
73 | filename, ext = path.splitext(filename) |
---|
74 | if domain.smooth is True: |
---|
75 | s = 'smooth' |
---|
76 | else: |
---|
77 | s = 'nonsmooth' |
---|
78 | domain.filename = filename + '_' + s + '_ys'+ str(yieldstep) + \ |
---|
79 | '_ft' + str(finaltime) |
---|
80 | print "Output being written to " + domain.get_datadir() + sep + \ |
---|
81 | domain.filename + "." + domain.format |
---|
82 | |
---|
83 | |
---|
84 | #Set friction |
---|
85 | manning = 0.07 |
---|
86 | inflow_stage = 10.0 |
---|
87 | |
---|
88 | ######NEW |
---|
89 | domain.set_quantity('friction', manning) |
---|
90 | |
---|
91 | #domain.set_quantity('stage', add_x_y) |
---|
92 | #domain.set_quantity('elevation', |
---|
93 | # domain.quantities['stage'].vertex_values+ \ |
---|
94 | # domain.quantities['elevation'].vertex_values) |
---|
95 | #domain.set_quantity('stage', 0.0) |
---|
96 | ######NEW |
---|
97 | |
---|
98 | ###################### |
---|
99 | # Boundary conditions |
---|
100 | # |
---|
101 | print 'Boundaries' |
---|
102 | reflective = Reflective_boundary(domain) |
---|
103 | Bt = Transmissive_boundary(domain) |
---|
104 | |
---|
105 | #Constant inflow |
---|
106 | Bd = Dirichlet_boundary(array([3, 0.0, 0.0])) |
---|
107 | |
---|
108 | #Time dependent inflow |
---|
109 | from math import sin, pi |
---|
110 | Bw = Time_boundary(domain=domain, |
---|
111 | f=lambda x: array([(1 + sin(x*pi/4))*\ |
---|
112 | (inflow_stage*(sin(2.5*x*pi)+0.7)),0,0])) |
---|
113 | |
---|
114 | |
---|
115 | print 'Available boundary tags are', domain.get_boundary_tags() |
---|
116 | |
---|
117 | #Set boundary conditions |
---|
118 | |
---|
119 | tags = {} |
---|
120 | tags['left'] = Bw |
---|
121 | tags['1'] = Bd |
---|
122 | |
---|
123 | tags['wave'] = Bd |
---|
124 | tags['wave'] = Time_boundary(domain=domain, |
---|
125 | f=lambda x: array([(1 + sin(x*pi/4))*\ |
---|
126 | (0.15*(sin(2.5*x*pi)+0.7)),0,0])) |
---|
127 | tags['internal'] = None |
---|
128 | tags['levee'] = None |
---|
129 | tags['0'] = reflective |
---|
130 | tags['wall'] = reflective |
---|
131 | tags['external'] = reflective |
---|
132 | tags['exterior'] = reflective |
---|
133 | tags['open'] = Bd |
---|
134 | tags['opening'] = None |
---|
135 | |
---|
136 | domain.set_boundary(tags) |
---|
137 | |
---|
138 | # region tags |
---|
139 | |
---|
140 | domain.set_region(Set_region('slow', 'friction', 20, location='unique vertices')) |
---|
141 | domain.set_region(Set_region('silo', 'elevation', 20, location='unique vertices')) |
---|
142 | domain.set_region(Set_region('wet', 'elevation', 0, location='unique vertices')) |
---|
143 | domain.set_region(Set_region('dry', 'elevation', 2, location='unique vertices')) |
---|
144 | domain.set_region(Add_value_to_region('wet', 'stage', 1.5, location='unique vertices', initial_quantity='elevation')) |
---|
145 | domain.set_region(Add_value_to_region('dry', 'stage', 0, location='unique vertices', initial_quantity='elevation')) |
---|
146 | |
---|
147 | #print domain.quantities['elevation'].vertex_values |
---|
148 | #print domain.quantities['stage'].vertex_values |
---|
149 | |
---|
150 | domain.check_integrity() |
---|
151 | |
---|
152 | ###################### |
---|
153 | #Evolution |
---|
154 | t0 = time.time() |
---|
155 | for t in domain.evolve(yieldstep = yieldstep, finaltime = finaltime): |
---|
156 | domain.write_time() |
---|
157 | |
---|
158 | print 'That took %.2f seconds' %(time.time()-t0) |
---|
159 | |
---|
160 | |
---|