source: development/stochastic_study/run_model.py @ 3290

Last change on this file since 3290 was 3035, checked in by ole, 18 years ago

Working with Suresh at ACFR

File size: 6.1 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# Read in processor information
41#-----------------------------------------------------------------------------
42
43try:
44    import pypar
45except:
46    print 'Could not import pypar'
47    myid = 0
48    numprocs = 1
49    processor_name = 'local host'
50else:   
51    myid = pypar.rank()
52    numprocs = pypar.size()
53    processor_name = pypar.Get_processor_name()
54
55print 'I am process %d of %d running on %s' %(myid, numprocs, processor_name)
56
57
58#-----------------------------------------------------------------------------
59# Setup computational domain
60#-----------------------------------------------------------------------------
61#print 'Creating domain from', project.mesh_filename
62
63domain = Domain(project.working_dir + project.mesh_filename,
64                use_cache=False,
65                verbose=False)               
66               
67
68#print 'Number of triangles = ', len(domain)
69#print domain.statistics()
70
71
72domain.set_datadir(project.working_dir)
73domain.set_quantities_to_be_stored(['stage', 'xmomentum', 'ymomentum'])
74
75
76#------------------------------------------------------------------------------
77# Setup boundary conditions
78#------------------------------------------------------------------------------
79
80function = file_function(project.boundary_filename, domain, verbose = False)
81Bts = Transmissive_Momentum_Set_Stage_boundary(domain, function) #Input wave
82Br = Reflective_boundary(domain) #Wall
83
84# Bind boundary objects to tags
85domain.set_boundary({'wave': Bts, 'wall': Br})
86
87
88#------------------------------------------------------------------------------
89# Setup initial conditions
90#------------------------------------------------------------------------------
91domain.set_quantity('friction', 0.0)
92domain.set_quantity('stage', 0.0)
93
94# Get prefitted realisations
95
96finaltime = 22.5
97timestep = 0.05
98
99
100realisation = 0
101for filename in os.listdir(project.working_dir):
102    if filename.startswith(project.basename) and filename.endswith('.pck'):
103        print 'P%d: Reading %s' %(myid, filename)
104        fid = open(project.working_dir + filename)
105        V = cPickle.load(fid)
106        fid.close()
107
108        #if myid == 0:
109        #    print 'V', V[6:7,:]
110           
111        # For each column (each realisation)
112        for i in range(V.shape[1]):
113
114            # Distribute work in round-robin fashion
115            if i%numprocs == myid:
116               
117                name = project.basename + '_P%d' %myid   
118                domain.set_name(name)                     #Output name
119                print 'V', V.shape
120                domain.set_quantity('elevation', V[:,i])  #Assign bathymetry
121
122                print 'P%d: Setting quantity %d: %s' %(myid, i, str(V[:4,i]))
123               
124                domain.set_time(0.0)                      #Reset time
125
126                #---------------------------------------------------
127                # Evolve system through time
128                #---------------------------------------------------
129                print 'P%d: Running realisation %d of %d in block %s'\
130                      %(myid, realisation, V.shape[1], filename)
131
132                t0 = time.time()
133                for t in domain.evolve(yieldstep = timestep,
134                                       finaltime = finaltime):
135                    pass
136                    domain.write_time()
137                   
138       
139                print 'P%d: Simulation of realisation %d took %.2f seconds'\
140                      %(myid, realisation, time.time()-t0)
141
142
143
144
145                #---------------------------------------------------
146                # Now extract the 3 timeseries (Ch 5-7-9) and store them
147                # in three files for this realisation
148
149                print 'P%d: Extracting time series for realisation %d from file %s'\
150                      %(myid, realisation, project.working_dir + domain.filename + '.sww')
151                f = file_function(project.working_dir + domain.filename + '.sww',
152                                  quantities='stage',
153                                  interpolation_points=project.gauges,
154                                  verbose=False)
155
156
157                simulation_name = project.working_dir + \
158                                  project.basename + '_realisation_%d' %realisation
159
160                print 'P%d: Writing to file %s'\
161                      %(myid, simulation_name + '_' + name + '.txt')
162
163                for k, name in enumerate(project.gauge_names):
164                    fid = open(simulation_name + '_' + name + '.txt', 'w')
165                    for t in f.get_time():
166                        #For all precomputed timesteps
167                        val = f(t, point_id = k)[0]
168                        fid.write('%f %f\n' %(t, val))
169
170                    fid.close()
171
172                   
173
174            realisation += 1           
175
176
177pypar.finalize()
Note: See TracBrowser for help on using the repository browser.