source: development/stochastic_study/run_model.py @ 2959

Last change on this file since 2959 was 2959, checked in by ole, 19 years ago

Fiddling with stochastic study. Realisations seemed identical?

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