Ejemplo n.º 1
0
def read(solution,
         frame,
         path='./',
         file_prefix='claw',
         read_aux=False,
         options={}):
    r"""
    Read in pickles and PETSc data files representing the solution
    
    :Input:
     - *solution* - (:class:`~pyclaw.solution.Solution`) Solution object to 
       read the data into.
     - *frame* - (int) Frame number to be read in
     - *path* - (string) Path to the current directory of the file
     - *file_prefix* - (string) Prefix of the files to be read in.  
       ``default = 'fort'``
     - *read_aux* (bool) Whether or not an auxiliary file will try to be read 
       in.  ``default = False``
     - *options* - (dict) Optional argument dictionary, see 
       `PETScIO Option Table`_
    
    .. _`PETScIO Option Table`:
    
    format   : one of 'ascii' or 'binary'
     
    """
    import os

    # Option parsing
    option_defaults = {'format': 'binary'}

    for k in option_defaults.iterkeys():
        if options.has_key(k):
            pass
        else:
            options[k] = option_defaults[k]

    pickle_filename = os.path.join(
        path, '%s.pkl' % file_prefix) + str(frame).zfill(4)
    viewer_filename = os.path.join(
        path, '%s.ptc' % file_prefix) + str(frame).zfill(4)
    aux_viewer_filename1 = os.path.join(
        path, '%s_aux.ptc' % file_prefix) + str(frame).zfill(4)
    aux_viewer_filename2 = os.path.join(
        path, '%s_aux.ptc' % file_prefix) + str(0).zfill(4)
    if os.path.exists(aux_viewer_filename1):
        aux_viewer_filename = aux_viewer_filename1
    else:
        aux_viewer_filename = aux_viewer_filename2

    if frame < 0:
        # Don't construct file names with negative frameno values.
        raise IOError("Frame " + str(frame) + " does not exist ***")

    try:
        pickle_file = open(pickle_filename, 'rb')
    except IOError:
        print "Error: file " + pickle_filename + " does not exist or is unreadable."
        raise

    # this dictionary is mostly holding debugging information, only nstates is needed
    # most of this information is explicitly saved in the individual patches
    value_dict = pickle.load(pickle_file)
    nstates = value_dict['nstates']
    num_dim = value_dict['num_dim']
    num_aux = value_dict['num_aux']
    num_eqn = value_dict['num_eqn']

    # now set up the PETSc viewer
    if options['format'] == 'ascii':
        viewer = PETSc.Viewer().createASCII(viewer_filename,
                                            PETSc.Viewer.Mode.READ)
        if read_aux:
            aux_viewer = PETSc.Viewer().createASCII(aux_viewer_filename,
                                                    PETSc.Viewer.Mode.READ)
    elif options['format'] == 'binary':
        if hasattr(PETSc.Viewer, 'createMPIIO'):
            viewer = PETSc.Viewer().createMPIIO(viewer_filename,
                                                PETSc.Viewer.Mode.READ)
        else:
            viewer = PETSc.Viewer().createBinary(viewer_filename,
                                                 PETSc.Viewer.Mode.READ)
        if read_aux:
            if os.path.exists(aux_viewer_filename):
                if hasattr(PETSc.Viewer, 'createMPIIO'):
                    aux_viewer = PETSc.Viewer().createMPIIO(
                        aux_viewer_filename, PETSc.Viewer.Mode.READ)
                else:
                    aux_viewer = PETSc.Viewer().createBinary(
                        aux_viewer_filename, PETSc.Viewer.Mode.READ)
            else:
                from warnings import warn
                aux_file_path = os.path.join(path, aux_viewer_filename)
                warn('read_aux=True but aux file %s does not exist' %
                     aux_file_path)
                read_aux = False
    else:
        raise IOError('format type %s not supported' % options['format'])

    patches = []
    for m in xrange(nstates):
        patch_dict = pickle.load(pickle_file)

        level = patch_dict['level']
        names = patch_dict['names']
        lower = patch_dict['lower']
        n = patch_dict['num_cells']
        d = patch_dict['delta']

        from clawpack import petclaw
        dimensions = []
        for i in xrange(num_dim):
            dimensions.append(
                #pyclaw.solution.Dimension(names[i],lower[i],lower[i] + n[i]*d[i],n[i]))
                petclaw.Dimension(names[i], lower[i], lower[i] + n[i] * d[i],
                                  n[i]))
        #patch = pyclaw.solution.Patch(dimensions)
        patch = petclaw.Patch(dimensions)
        patch.level = level
        #state = pyclaw.state.State(patch)
        state = petclaw.State(patch, num_eqn, num_aux)  ##
        state.t = value_dict['t']
        state.problem_data = value_dict['problem_data']

        #       DA View/Load is broken in Petsc-3.1.8, we can load/view the DA if needed in petsc-3.2
        #       state.q_da.load(viewer)
        state.gqVec.load(viewer)

        if read_aux:
            state.gauxVec.load(aux_viewer)

        solution.states.append(state)
        patches.append(state.patch)
    solution.domain = petclaw.geometry.Domain(patches)

    pickle_file.close()
    viewer.destroy()
    if read_aux:
        aux_viewer.destroy()
Ejemplo n.º 2
0
def read(solution,frame,path='./',file_prefix='claw',read_aux=True,
                options={}):
    r"""
    Read in a HDF5 file into a Solution

    :Input:
     - *solution* - (:class:`~pyclaw.solution.Solution`) Pyclaw object to be
       output
     - *frame* - (int) Frame number
     - *path* - (string) Root path
     - *file_prefix* - (string) Prefix for the file name.  ``default = 'claw'``
     - *write_aux* - (bool) Boolean controlling whether the associated
       auxiliary array should be written out.  ``default = False``
     - *options* - (dict) Optional argument dictionary, not used for reading.
    """
    filename = os.path.join(path,'%s%s.hdf' %
                                (file_prefix,str(frame).zfill(4)))
    patches = []

    with h5py.File(filename,'r',driver='mpio',comm=MPI.COMM_WORLD) as f:
        for patch in six.itervalues(f):
            # Construct each dimension
            dimensions = []
            dim_names = [ name.decode('ascii')
                          for name in patch.attrs['dimensions'] ]
            for dim_name in dim_names:
                dim = geometry.Dimension(
                    patch.attrs["%s.lower" % dim_name],
                    patch.attrs["%s.upper" % dim_name],
                    patch.attrs["%s.num_cells" % dim_name],
                    name = dim_name)
                # Optional attributes
                for attr in ['units']:
                    attr_name = "%s.%s" % (dim_name,attr)
                    if patch.attrs.get(attr_name, None):
                        setattr(dim,attr,patch.attrs["%s.%s" % (dim_name,attr)])
                dimensions.append(dim)

            pyclaw_patch = petclaw.Patch(dimensions)

            # Fetch general patch properties
            for attr in ['t','num_eqn','patch_index','level']:
                setattr(pyclaw_patch,attr,patch.attrs[attr])

            state = petclaw.state.State(pyclaw_patch, \
                                        patch.attrs['num_eqn'],patch.attrs['num_aux'])
            state.t = patch.attrs['t']

            globalSize = []
            globalSize.append(state.q.shape[0])
            globalSize.extend(pyclaw_patch.num_cells_global)
            r = pyclaw_patch._da.getRanges()

            dset = patch['q'][:].reshape(globalSize)
            state.q = from_hdf5_dataset(dset, len(pyclaw_patch.dimensions), r, state.q.shape)

            # Read in aux if applicable
            if read_aux and patch.get('aux',None) is not None:
                dset = patch['aux'][:]
                state.aux = from_hdf5_dataset(dset, len(pyclaw_patch.dimensions), r, state.aux.shape)

            solution.states.append(state)
            patches.append(pyclaw_patch)

        solution.domain = geometry.Domain(patches)
Ejemplo n.º 3
0
def read(solution,frame,path='./',file_prefix='claw',read_aux=False,options={}):
    r"""
    Read in pickles and PETSc data files representing the solution
    
    :Input:
     - *solution* - (:class:`~pyclaw.solution.Solution`) Solution object to 
       read the data into.
     - *frame* - (int) Frame number to be read in
     - *path* - (string) Path to the current directory of the file
     - *file_prefix* - (string) Prefix of the files to be read in.  
       ``default = 'fort'``
     - *read_aux* (bool) Whether or not an auxiliary file will try to be read 
       in.  ``default = False``
     - *options* - (dict) Optional argument dictionary, see 
       `PETScIO Option Table`_
    
    .. _`PETScIO Option Table`:
    
    format   : one of 'ascii' or 'binary'
     
    """
    # Default format is binary
    file_format = options.get('format', 'binary')

    filenames = set_filenames(frame,path,file_format,file_prefix,read_aux)

    if read_aux:
        if not os.path.exists(filenames['aux']):
            # If no aux file for this frame, assume it is time-independent
            filenames['aux'] = os.path.join(path, '%s_aux.ptc' % file_prefix) + str(0).zfill(4)

    try:
        metadata_file = open(filenames['metadata'],'rb')
    except IOError:
        print "Error: file " + filenames['metadata'] + " does not exist or is unreadable."
        raise

    # this dictionary is mostly holding debugging information, only nstates is needed
    # most of this information is explicitly saved in the individual patches
    value_dict = pickle.load(metadata_file)
    nstates    = value_dict['nstates']                    
    num_dim       = value_dict['num_dim']
    num_aux       = value_dict['num_aux']
    num_eqn       = value_dict['num_eqn']

    if read_aux and not os.path.exists(filenames['aux']):
        # Don't abort if aux file is missing
        from warnings import warn
        aux_file_path = os.path.join(path,filenames['aux'])
        warn('read_aux=True but aux file %s does not exist' % aux_file_path)
        read_aux = False

    q_viewer = set_up_viewers(filenames['q'],file_format.lower(),PETSc.Viewer.Mode.READ)
    if read_aux:
        aux_viewer = set_up_viewers(filenames['aux'],file_format.lower(),PETSc.Viewer.Mode.READ)

    patches = []
    for m in xrange(nstates):
        patch_dict = pickle.load(metadata_file)

        level   = patch_dict['level']
        names   = patch_dict['names']
        lower   = patch_dict['lower']
        n       = patch_dict['num_cells']
        d       = patch_dict['delta']

        from clawpack import petclaw
        dimensions = []
        for i in xrange(num_dim):
            dimensions.append(
                petclaw.Dimension(lower[i],lower[i] + n[i]*d[i],n[i],name=names[i]))
        patch = petclaw.Patch(dimensions)
        patch.level = level 
        state = petclaw.State(patch,num_eqn,num_aux)
        state.t = value_dict['t']
        state.problem_data = value_dict.get('problem_data',{})
        if value_dict.has_key('mapc2p'):
            # If no mapc2p is provided, leave the default identity map in grid
            state.grid.mapc2p = value_dict['mapc2p']

#       DA View/Load is broken in Petsc-3.1.8, we can load/view the DA if needed in petsc-3.2
#       state.q_da.load(q_viewer)
        state.gqVec.load(q_viewer)
        
        if read_aux:
            state.gauxVec.load(aux_viewer)
        
        solution.states.append(state)
        patches.append(state.patch)
    solution.domain = petclaw.geometry.Domain(patches)

    metadata_file.close()
    q_viewer.destroy() # Destroys aux_viewer also
Ejemplo n.º 4
0
    def read_petsc(self):

        if hasattr(self, 'frame'): frame = self.frame
        if hasattr(self, 'file_prefix'): file_prefix = self.file_prefix
        if hasattr(self, 'path'): path = self.path
        if hasattr(self, 'write_aux'): write_aux = self.write_aux
        if hasattr(self, 'write_aux'): read_aux = self.read_aux
        if hasattr(self, 'write_p'): write_p = self.write_p

        pickle_filename = os.path.join(
            path, '%s.pkl' % file_prefix) + str(frame).zfill(4)
        viewer_filename = os.path.join(
            path, '%s.ptc' % file_prefix) + str(frame).zfill(4)
        aux_viewer_filename1 = os.path.join(
            path, '%s_aux.ptc' % file_prefix) + str(frame).zfill(4)
        aux_viewer_filename2 = os.path.join(
            path, '%s_aux.ptc' % file_prefix) + str(0).zfill(4)
        if os.path.exists(aux_viewer_filename1):
            aux_viewer_filename = aux_viewer_filename1
        else:
            aux_viewer_filename = aux_viewer_filename2

        pickle_file = open(pickle_filename, 'rb')

        # this dictionary is mostly holding debugging information, only nstates is needed
        # most of this information is explicitly saved in the individual patches
        value_dict = pickle.load(pickle_file)
        nstates = value_dict['nstates']
        num_dim = value_dict['num_dim']
        num_aux = value_dict['num_aux']
        num_eqn = value_dict['num_eqn']

        self.__setattr__('num_dim', num_dim)
        self.__setattr__('num_aux', num_aux)
        self.__setattr__('num_eqn', num_eqn)
        # now set up the PETSc viewer (assuming binary)
        viewer = PETSc.Viewer().createBinary(viewer_filename,
                                             PETSc.Viewer.Mode.READ)
        if read_aux:
            aux_viewer = PETSc.Viewer().createBinary(aux_viewer_filename,
                                                     PETSc.Viewer.Mode.READ)

        patches = []
        for m in xrange(nstates):
            patch_dict = pickle.load(pickle_file)

            level = patch_dict['level']
            names = patch_dict['names']
            lower = patch_dict['lower']
            n = patch_dict['num_cells']
            d = patch_dict['delta']
            from clawpack import petclaw
            dimensions = []
            for i in xrange(num_dim):
                dimensions.append(
                    petclaw.Dimension(names[i], lower[i],
                                      lower[i] + n[i] * d[i], n[i]))
            patch = petclaw.Patch(dimensions)
            self.__setattr__('_patch', patch)

            if num_dim == 1:
                self.__setattr__('x', patch.x)
            elif num_dim == 2:
                self.__setattr__('x', patch.x)
                self.__setattr__('y', patch.y)
            elif num_dim == 3:
                self.__setattr__('y', patch.y)
                self.__setattr__('z', path.z)

            self.__setattr__('num_cells', patch.num_cells_global)
            claw = petclaw.State(patch, num_eqn, num_aux)  ##
            self.__setattr__('_claw', claw)
            self.t = value_dict['t']
            self.problem_data = value_dict['problem_data']
            self.nstates = value_dict['nstates']
            self._claw.gqVec.load(viewer)
            if read_aux:
                self._claw.gauxVec.load(aux_viewer)
                self.__setattr__('aux', self._claw.aux)

            self.__setattr__('q', self._claw.q)
            self.__setattr__('frame', frame)
            self.__setattr__('file_prefix', file_prefix)
            self.__setattr__('read_aux', read_aux)
            self.__setattr__('write_aux', write_aux)
            self.__setattr__('write_p', write_p)
            self.__setattr__('path', path)

        return self