1 | """Example of shallow water wave equation. |
---|
2 | |
---|
3 | This is called Netherlands because it shows a dam with a gap in it and |
---|
4 | stylised housed behind it and below the water surface. |
---|
5 | |
---|
6 | """ |
---|
7 | |
---|
8 | ###################### |
---|
9 | # Module imports |
---|
10 | # |
---|
11 | from shallow_water import Domain, Reflective_boundary, Dirichlet_boundary,\ |
---|
12 | Transmissive_boundary, Time_boundary, Constant_height |
---|
13 | |
---|
14 | from mesh_factory import from_polyfile, rectangular |
---|
15 | from Numeric import array |
---|
16 | from math import sqrt |
---|
17 | from least_squares import Interpolation |
---|
18 | |
---|
19 | |
---|
20 | print 'Creating domain' |
---|
21 | #data_points, _, data_values = from_polyfile('cornell_room_medres') |
---|
22 | #points, triangles, values = from_polyfile('hires2') |
---|
23 | data_points, _, data_values = from_polyfile('hires2') |
---|
24 | |
---|
25 | |
---|
26 | #Regrid onto numerically stable mesh |
---|
27 | # |
---|
28 | #Compute regular mesh based on resolution and extent of data |
---|
29 | data_points = array(data_points) |
---|
30 | pmax = max(data_points) |
---|
31 | pmin = min(data_points) |
---|
32 | |
---|
33 | M = len(data_points) |
---|
34 | |
---|
35 | N = int(0.8*sqrt(M)) |
---|
36 | |
---|
37 | #print N |
---|
38 | |
---|
39 | mesh_points, vertices, boundary = rectangular(N, N, |
---|
40 | len1=pmax[0]-pmin[0], |
---|
41 | len2=pmax[1]-pmin[1], |
---|
42 | origin = pmin) |
---|
43 | |
---|
44 | |
---|
45 | #Compute smooth surface on new mesh based on values from old (regrid) |
---|
46 | print 'Interp' |
---|
47 | interp = Interpolation(mesh_points, vertices, data_points, alpha=10) |
---|
48 | mesh_values = interp.fit(data_values) |
---|
49 | print 'Len mesh values', len(mesh_values) |
---|
50 | print 'Len mesh points', len(mesh_points) |
---|
51 | |
---|
52 | |
---|
53 | #Create shallow water domain |
---|
54 | print 'Creating domain' |
---|
55 | domain = Domain(mesh_points, vertices) #, boundary) |
---|
56 | |
---|
57 | domain.check_integrity() |
---|
58 | domain.default_order = 2 |
---|
59 | domain.smooth = True |
---|
60 | domain.reduction = min #Looks a lot better on top of steep slopes |
---|
61 | |
---|
62 | print "Number of triangles = ", len(domain) |
---|
63 | |
---|
64 | domain.visualise = False |
---|
65 | domain.checkpoint = False |
---|
66 | domain.store = True #Store for visualisation purposes |
---|
67 | domain.format = 'sww' #Native netcdf visualisation format |
---|
68 | import sys, os |
---|
69 | root, ext = os.path.splitext(sys.argv[0]) |
---|
70 | if domain.smooth is True: |
---|
71 | s = 'smooth' |
---|
72 | else: |
---|
73 | s = 'nonsmooth' |
---|
74 | domain.filename = root + '_' + s |
---|
75 | |
---|
76 | #Set bed-slope and friction |
---|
77 | manning = 0.0 |
---|
78 | |
---|
79 | print 'Field values' |
---|
80 | domain.set_quantity('elevation', mesh_values) |
---|
81 | domain.set_quantity('friction', manning) |
---|
82 | |
---|
83 | |
---|
84 | ###################### |
---|
85 | # Boundary conditions |
---|
86 | # |
---|
87 | print 'Boundaries' |
---|
88 | Br = Reflective_boundary(domain) |
---|
89 | domain.set_boundary({'exterior': Br}) |
---|
90 | |
---|
91 | |
---|
92 | |
---|
93 | ###################### |
---|
94 | #Initial condition |
---|
95 | # |
---|
96 | print 'Initial condition' |
---|
97 | |
---|
98 | #Define water height as a lump in one corner |
---|
99 | def height(x, y): |
---|
100 | from Numeric import zeros, Float |
---|
101 | |
---|
102 | N = len(x) |
---|
103 | assert N == len(y) |
---|
104 | |
---|
105 | xmin = min(x); xmax = max(x) |
---|
106 | ymin = min(y); ymax = max(y) |
---|
107 | |
---|
108 | xrange = xmax - xmin |
---|
109 | yrange = ymax - ymin |
---|
110 | |
---|
111 | z = zeros(N, Float) |
---|
112 | for i in range(N): |
---|
113 | if x[i] <= xmin + 0.25*xrange and y[i] <= ymin + 0.25*yrange: |
---|
114 | z[i] = 300 |
---|
115 | |
---|
116 | return z |
---|
117 | |
---|
118 | domain.set_quantity('stage', height) |
---|
119 | |
---|
120 | E = domain.quantities['elevation'].vertex_values |
---|
121 | L = domain.quantities['stage'].vertex_values |
---|
122 | domain.set_quantity('stage', E+L) |
---|
123 | |
---|
124 | #Evolve |
---|
125 | for t in domain.evolve(yieldstep = 0.05, finaltime = 5.0): |
---|
126 | domain.write_time() |
---|
127 | |
---|
128 | |
---|
129 | |
---|