Changeset 288


Ignore:
Timestamp:
Sep 10, 2004, 5:56:19 PM (18 years ago)
Author:
ole
Message:

Started playing with checkpointing

Location:
inundation/ga/storm_surge/pyvolution
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • inundation/ga/storm_surge/pyvolution/data_manager.py

    r287 r288  
    284284        self.domain = domain
    285285   
    286 
    287     # def store_connectivity(self):
    288 #         """Writes x,y,z coordinates of triangles constituting
    289 #         the bed elevation.
    290 #         """
    291 
    292 #         msg = 'Method should be overridden in concrete class'
    293 #         raise msg
    294 
    295 #     def store_timestep(self, t, V0, V1, V2):
    296 #         """Store time, water heights (and momentums) to file
    297 #         """
    298 
    299 #         msg = 'Method should be overridden in concrete class'
    300 #         raise msg       
    301286   
    302287    #FIXME: Should we have a general set_precision function?
    303288   
    304289
    305 #Function for storing output to e.g. visualisation
     290
     291#Class for storing output to e.g. visualisation
    306292class Data_format_sww(Data_format):
    307293    """Interface to native NetCDF format (.sww)
     
    325311            #Create new file
    326312            fid.institution = 'Geoscience Australia'
     313            fid.description = 'Output from pyvolution suitable for plotting'
     314           
    327315            if domain.smooth:
    328316                fid.smoothing = 'Yes'
     
    454442
    455443
     444#Class for handling checkpoints data
     445class Data_format_cpt(Data_format):
     446    """Interface to native NetCDF format (.cpt)
     447    """
     448
     449       
     450    def __init__(self, domain, mode = 'w'):
     451        from Scientific.IO.NetCDF import NetCDFFile
     452        from Numeric import Int, Float, Float
     453
     454        self.precision = Float #Use full precision
     455       
     456        Data_format.__init__(self, domain, 'sww', mode)
     457
     458
     459        # NetCDF file definition
     460        fid = NetCDFFile(self.filename, mode)
     461       
     462        if mode == 'w':
     463            #Create new file
     464            fid.institution = 'Geoscience Australia'
     465            fid.description = 'Checkpoint data'
     466            #fid.smooth = domain.smooth               
     467            fid.order = domain.default_order   
     468
     469            # dimension definitions
     470            fid.createDimension('number_of_volumes', self.number_of_volumes) 
     471            fid.createDimension('number_of_vertices', 3)
     472
     473            #Store info at all vertices (no smoothing)
     474            fid.createDimension('number_of_points', 3*self.number_of_volumes)
     475            fid.createDimension('number_of_timesteps', None) #extensible
     476
     477            # variable definitions
     478
     479            #Mesh
     480            fid.createVariable('x', self.precision, ('number_of_points',))
     481            fid.createVariable('y', self.precision, ('number_of_points',))
     482            #fid.createVariable('z', self.precision, ('number_of_points',))
     483       
     484            fid.createVariable('volumes', Int, ('number_of_volumes',
     485                                                'number_of_vertices'))
     486
     487            fid.createVariable('time', self.precision,
     488                               ('number_of_timesteps',))
     489
     490            #Allocate space for all quantities
     491            for name in domain.quantities.keys():
     492                fid.createVariable(name, self.precision,
     493                                   ('number_of_timesteps',
     494                                    'number_of_points'))
     495
     496        #Close
     497        fid.close()               
     498
     499
     500    def store_checkpoint(self):
     501        """
     502        Write x,y coordinates of triangles.
     503        Write connectivity (
     504        constituting
     505        the bed elevation.
     506        """
     507
     508        from Scientific.IO.NetCDF import NetCDFFile
     509
     510        from Numeric import concatenate
     511
     512        domain = self.domain
     513
     514        #Get NetCDF
     515        fid = NetCDFFile(self.filename, 'a')  #Open existing file for append
     516       
     517        # Get the variables
     518        x = fid.variables['x']
     519        y = fid.variables['y']
     520       
     521        volumes = fid.variables['volumes']
     522
     523        # Get X, Y and bed elevation Z
     524        Q = domain.quantities['elevation']
     525        X,Y,Z,V = Q.get_vertex_values(xy=True,
     526                                      precision = self.precision)
     527                                     
     528
     529
     530        x[:] = X.astype(self.precision)
     531        y[:] = Y.astype(self.precision)
     532        z[:] = Z.astype(self.precision)
     533
     534        volumes[:] = V
     535       
     536        #Close
     537        fid.close()
     538
     539
     540    def store_timestep(self, name):
     541        """Store time and named quantity to file
     542        """
     543        from Scientific.IO.NetCDF import NetCDFFile
     544        from time import sleep
     545               
     546        #Get NetCDF
     547        retries = 0
     548        file_open = False
     549        while not file_open and retries < 10:
     550            try:       
     551                fid = NetCDFFile(self.filename, 'a') #Open existing file
     552            except IOError:
     553                #This could happen if someone was reading the file.
     554                #In that case, wait a while and try again
     555                msg = 'Warning (store_timestep): File %s could not be opened'\
     556                %self.filename
     557                msg += ' - trying again'
     558                print msg
     559                retries += 1
     560                sleep(1)
     561            else:
     562               file_open = True
     563               
     564        if not file_open:
     565            msg = 'File %s could not be opened for append' %self.filename
     566            raise msg
     567               
     568           
     569        domain = self.domain
     570       
     571        # Get the variables
     572        time = fid.variables['time']
     573        stage = fid.variables['stage']       
     574        i = len(time)
     575
     576        #Store stage
     577        time[i] = self.domain.time
     578
     579        # Get quantity
     580        Q = domain.quantities[name]
     581        A,V = Q.get_vertex_values(xy=False,
     582                                  precision = self.precision)
     583       
     584        stage[i,:] = A.astype(self.precision)
     585
     586        #Flush and close
     587        fid.sync()
     588        fid.close()
     589
     590
     591
     592
     593
    456594
    457595
  • inundation/ga/storm_surge/pyvolution/mesh.py

    r275 r288  
    541541        return vertex_coordinates
    542542 
    543 
    544 
    545 
     543    def get_vertices(unique = False):
     544        """Get connectivity
     545        If unique is True give them only once as stored internally.
     546        If unique is False return
     547        """
     548
     549        if unique is True:
     550            V = self.domain.vertices
     551        else:
     552           
     553
     554
     555#FIXME: May get rid of
    546556def rectangular_mesh(m, n, len1=1.0, len2=1.0, origin = (0.0, 0.0)):
    547557    from mesh_factory import rectangular
  • inundation/ga/storm_surge/pyvolution/netherlands.py

    r287 r288  
    9292
    9393N = 600 #Size = 720000
    94 N = 100
     94N = 220
    9595
    9696
     
    171171t0 = time.time()
    172172
    173 for t in domain.evolve(yieldstep = 0.1, finaltime = 1.5):
     173for t in domain.evolve(yieldstep = 0.01, finaltime = 1.0):
    174174    domain.write_time()
    175175   
  • inundation/ga/storm_surge/pyvolution/quantity.py

    r283 r288  
    234234   
    235235    #Method for outputting model results
     236    #FIXME: Split up into geometric and numeric stuff.
     237    #FIXME: Geometric (X,Y,V) should live in mesh.py
    236238    def get_vertex_values(self,
    237239                          xy=True,
     
    282284            N = len(self.domain.vertexlist)
    283285            A = zeros(N, precision)
    284             V = self.domain.vertices
     286            V = self.domain.get_vertices(smooth=True)
    285287           
    286288            #Smoothing loop
Note: See TracChangeset for help on using the changeset viewer.