1 | """Stochastic study of the ANUGA implementation of the |
---|
2 | shallow water wave equation. |
---|
3 | |
---|
4 | This script runs the model for one realisation of bathymetry as |
---|
5 | given in the file bathymetry.txt and outputs a full simulation is \ |
---|
6 | sww NetCDF format. |
---|
7 | |
---|
8 | The left boundary condition is a timeseries defined in |
---|
9 | NetCDF file: input_wave.tms |
---|
10 | |
---|
11 | Note: This scripts needs create_mesh.py to have been run |
---|
12 | |
---|
13 | Suresh Kumar and Ole Nielsen 2006 |
---|
14 | """ |
---|
15 | |
---|
16 | |
---|
17 | #------------------------------------------------------------------------------ |
---|
18 | # Import necessary modules |
---|
19 | #------------------------------------------------------------------------------ |
---|
20 | |
---|
21 | # Standard modules |
---|
22 | import os |
---|
23 | import time |
---|
24 | import cPickle |
---|
25 | |
---|
26 | # Related major packages |
---|
27 | from pyvolution.shallow_water import Domain |
---|
28 | from pyvolution.shallow_water import Reflective_boundary |
---|
29 | from pyvolution.shallow_water import Transmissive_Momentum_Set_Stage_boundary |
---|
30 | from pyvolution.pmesh2domain import pmesh_to_domain_instance |
---|
31 | from pyvolution.data_manager import xya2pts |
---|
32 | from pyvolution.util import file_function |
---|
33 | from caching.caching import cache |
---|
34 | |
---|
35 | # Application specific imports |
---|
36 | import project # Definition of file names and polygons |
---|
37 | |
---|
38 | |
---|
39 | |
---|
40 | |
---|
41 | #----------------------------------------------------------------------------- |
---|
42 | # Setup computational domain |
---|
43 | #----------------------------------------------------------------------------- |
---|
44 | print 'Creating domain from', project.mesh_filename |
---|
45 | |
---|
46 | #domain = pmesh_to_domain_instance(project.mesh_filename, Domain, |
---|
47 | # use_cache=True, |
---|
48 | # verbose=True) |
---|
49 | |
---|
50 | domain = Domain(project.mesh_filename, |
---|
51 | use_cache=True, |
---|
52 | verbose=True) |
---|
53 | |
---|
54 | |
---|
55 | print 'Number of triangles = ', len(domain) |
---|
56 | print 'The extent is ', domain.get_extent() |
---|
57 | print domain.statistics() |
---|
58 | |
---|
59 | import sys; sys.exit() |
---|
60 | |
---|
61 | |
---|
62 | domain.set_datadir('.') |
---|
63 | domain.set_quantities_to_be_stored(['stage', 'xmomentum', 'ymomentum']) |
---|
64 | |
---|
65 | #domain.check_integrity() |
---|
66 | |
---|
67 | |
---|
68 | #------------------------------------------------------------------------------ |
---|
69 | # Setup boundary conditions |
---|
70 | #------------------------------------------------------------------------------ |
---|
71 | |
---|
72 | function = file_function(project.boundary_filename, domain, verbose = True) |
---|
73 | Bts = Transmissive_Momentum_Set_Stage_boundary(domain, function) #Input wave |
---|
74 | Br = Reflective_boundary(domain) #Wall |
---|
75 | |
---|
76 | # Bind boundary objects to tags |
---|
77 | domain.set_boundary({'wave': Bts, 'wall': Br}) |
---|
78 | |
---|
79 | |
---|
80 | #------------------------------------------------------------------------------ |
---|
81 | # Setup initial conditions |
---|
82 | #------------------------------------------------------------------------------ |
---|
83 | domain.set_quantity('friction', 0.0) |
---|
84 | domain.set_quantity('stage', 0.0) |
---|
85 | |
---|
86 | # Get prefitted realisations |
---|
87 | |
---|
88 | finaltime = 22.5 |
---|
89 | timestep = 0.05 |
---|
90 | |
---|
91 | |
---|
92 | |
---|
93 | realisation = 0 |
---|
94 | for filename in os.listdir('.'): |
---|
95 | if filename.startswith(project.basename) and filename.endswith('.pck'): |
---|
96 | print 'Reading %s' %filename |
---|
97 | fid = open(filename) |
---|
98 | V = cPickle.load(fid) |
---|
99 | fid.close() |
---|
100 | |
---|
101 | # For each column (each realisation) |
---|
102 | for i in range(V.shape[1]): |
---|
103 | domain.set_name(project.basename) #Output name |
---|
104 | domain.set_quantity('elevation', V[:,i]) #Assign bathymetry |
---|
105 | domain.starttime = 0.0 #Reset time |
---|
106 | |
---|
107 | #--------------------------------------------------- |
---|
108 | # Evolve system through time |
---|
109 | #--------------------------------------------------- |
---|
110 | print 'Running realisation %d of %d in block %s'\ |
---|
111 | %(i, V.shape[1], filename) |
---|
112 | t0 = time.time() |
---|
113 | for t in domain.evolve(yieldstep = timestep, finaltime = finaltime): |
---|
114 | domain.write_time() |
---|
115 | |
---|
116 | |
---|
117 | print 'Realisation %d took %.2f seconds'\ |
---|
118 | %(realisation, time.time()-t0) |
---|
119 | |
---|
120 | |
---|
121 | |
---|
122 | |
---|
123 | #--------------------------------------------------- |
---|
124 | # Now extract the 3 timeseries (Ch 5-7-9) and store them |
---|
125 | # in three files for this realisation |
---|
126 | gauges = [[4.521, 1.196], [4.521, 1.696], [4.521, 2.196]] |
---|
127 | gauge_names = ['ch5', 'ch7', 'ch9'] |
---|
128 | |
---|
129 | |
---|
130 | f = file_function(domain.filename + '.sww', |
---|
131 | quantities='stage', |
---|
132 | interpolation_points=gauges, |
---|
133 | verbose = True) |
---|
134 | |
---|
135 | |
---|
136 | simulation_name = domain.filename + '_realisation_%d' %realisation |
---|
137 | |
---|
138 | for k, name in enumerate(gauge_names): |
---|
139 | fid = open(simulation_name + '_' + name + '.txt', 'w') |
---|
140 | for t in f.T: |
---|
141 | #For all precomputed timesteps |
---|
142 | val = f(t, point_id = k)[0] |
---|
143 | fid.write('%f %f\n' %(t, val)) |
---|
144 | |
---|
145 | fid.close() |
---|
146 | |
---|
147 | |
---|
148 | |
---|
149 | realisation += 1 |
---|