Beispiel #1
0
def read_t(frame,path='./',file_prefix='fort'):
    r"""Read only the fort.t file and return the data
    
    :Input:
     - *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'``
     
    :Output:
     - (list) List of output variables
     - *t* - (int) Time of frame
     - *num_eqn* - (int) Number of equations in the frame
     - *nstates* - (int) Number of states
     - *num_aux* - (int) Auxillary value in the frame
     - *num_dim* - (int) Number of dimensions in q and aux
    
    """

    base_path = os.path.join(path,)
    path = os.path.join(base_path, '%s.t' % file_prefix) + str(frame).zfill(4)
    logger.debug("Opening %s file." % path)
    with open(path,'r') as f:
        t = read_data_line(f)
        num_eqn = read_data_line(f,data_type=int)
        nstates = read_data_line(f,data_type=int)
        num_aux = read_data_line(f,data_type=int)
        num_dim = read_data_line(f,data_type=int)
    
    return t, num_eqn, nstates, num_aux, num_dim
Beispiel #2
0
def read_t(frame, path='./', file_prefix='fort'):
    r"""Read only the fort.t file and return the data
    
    :Input:
     - *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'``
     
    :Output:
     - (list) List of output variables
     - *t* - (int) Time of frame
     - *num_eqn* - (int) Number of equations in the frame
     - *nstates* - (int) Number of states
     - *num_aux* - (int) Auxillary value in the frame
     - *num_dim* - (int) Number of dimensions in q and aux
    
    """

    base_path = os.path.join(path, )
    path = os.path.join(base_path, '%s.t' % file_prefix) + str(frame).zfill(4)
    logger.debug("Opening %s file." % path)
    with open(path, 'r') as f:
        t = read_data_line(f)
        num_eqn = read_data_line(f, data_type=int)
        nstates = read_data_line(f, data_type=int)
        num_aux = read_data_line(f, data_type=int)
        num_dim = read_data_line(f, data_type=int)

    return t, num_eqn, nstates, num_aux, num_dim
Beispiel #3
0
def setadjoint():
    #-------------------
    
    """
        Reverse order of adjoint images, for plotting
        adjacent to forward plots.
        """
    
    import os,sys,glob
    from clawpack.pyclaw import io
    from clawpack.pyclaw.util import read_data_line
    
    outdir = '../adjoint/_output'
    outdir2 = '../adjoint/_outputReversed'
    
    os.system('mkdir -p %s' % outdir2)
    
    files = glob.glob(outdir+'/fort.b*')
    files.sort()
    n = len(files)
    
    if (n >= 1):
        # Find the final time.
        fname = files[n-1]
        fname = fname.replace('b','t')
        f = open(fname,'r')
        tfinal,meqn,npatches,maux,num_dim = io.ascii.read_t(n-1,path=outdir)
        
        for k in range(n):
            # Creating new files
            fname = files[k]
            newname = outdir2 + '/fort.b%s' % str(n-k-1).zfill(4)
            cmd = 'cp %s %s' % (fname,newname)
            os.system(cmd)
            
            fname = fname.replace('b','q')
            newname = newname.replace('b','q')
            cmd = 'cp %s %s' % (fname,newname)
            os.system(cmd)
            
            fname = fname.replace('q','t')
            newname = newname.replace('q','t')
            cmd = 'cp %s %s' % (fname,newname)
            os.system(cmd)
            
            # Reversing time
            f = open(newname,'r+')
            frameno = n-k-1
            
            t = read_data_line(f)
            
            t = tfinal - t
            
            # Writting new time out to file
            f.seek(0)
            f.write('%18.8e     time\n' % t)
            f.close()
# end of function setadjoint
# ----------------------
Beispiel #4
0
def read_patch_header(f, num_dim):
    r"""Read header describing the next patch
    
    :Input:
     - *f* - (file) Handle to open file
     - *num_dim* - (int) Number of dimensions
     
    :Output:
     - *patch* - (clawpack.pyclaw.geometry.Patch) Initialized patch represented
       by the header data.
    
    """

    n = np.zeros((num_dim), dtype=int)
    d = np.zeros((num_dim))
    lower = np.zeros((num_dim))
    patch_index = read_data_line(f, data_type=int)
    level = read_data_line(f, data_type=int)
    for i in range(num_dim):
        n[i] = read_data_line(f, data_type=int)
    for i in range(num_dim):
        lower[i] = read_data_line(f)
    for i in range(num_dim):
        d[i] = read_data_line(f)

    blank = f.readline()

    # Construct the patch
    # Since we do not have names here, we will construct each patch with
    # dimension names x,y,z
    names = ['x', 'y', 'z']
    dimensions = [
        pyclaw.Dimension(lower[i], lower[i] + n[i] * d[i], n[i], name=names[i])
        for i in range(num_dim)
    ]
    patch = pyclaw.geometry.Patch(dimensions)

    # Add AMR attributes:
    patch.patch_index = patch_index
    patch.level = level

    return patch
Beispiel #5
0
def read_patch_header(f, num_dim):
    r"""Read header describing the next patch
    
    :Input:
     - *f* - (file) Handle to open file
     - *num_dim* - (int) Number of dimensions
     
    :Output:
     - *patch* - (clawpack.pyclaw.geometry.Patch) Initialized patch represented
       by the header data.
    
    """

    n = np.zeros((num_dim), dtype=int)
    d = np.zeros((num_dim))
    lower = np.zeros((num_dim))
    patch_index = read_data_line(f, data_type=int)
    level       = read_data_line(f, data_type=int)
    for i in range(num_dim):
        n[i] = read_data_line(f, data_type=int)
    for i in range(num_dim):
        lower[i] = read_data_line(f)
    for i in range(num_dim):
        d[i] = read_data_line(f)

    blank = f.readline()

    # Construct the patch
    # Since we do not have names here, we will construct each patch with
    # dimension names x,y,z
    names = ['x', 'y', 'z']
    dimensions = [pyclaw.Dimension(lower[i], lower[i] + n[i] * d[i],
                                  n[i], name=names[i]) for i in range(num_dim)]
    patch = pyclaw.geometry.Patch(dimensions)

    # Add AMR attributes:
    patch.patch_index = patch_index
    patch.level = level

    return patch
Beispiel #6
0
def read_t(frame, path='./', file_prefix='fort'):
    r"""Read only the fort.t file and return the data.

    Note this file is always ascii and now contains a line that tells
    the file_format, so we can read this file before importing the 
    appropriate read function for the solution data.

    For backward compatibility, if file_format line is missing then
    return None and handle this where it is called.

    This version also reads in num_ghost so that if the data is binary,
    we can extract only the data that's relevant (since ghost cells are
    included).
    
    :Input:
     - *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'``
     
    :Output:
     - (list) List of output variables
     - *t* - (int) Time of frame
     - *num_eqn* - (int) Number of equations in the frame
     - *nstates* - (int) Number of states
     - *num_aux* - (int) Auxiliary value in the frame
     - *num_dim* - (int) Number of dimensions in q and aux
     - *num_ghost* - (int) Number of ghost cells on each side
     - *file_format* - (str) 'ascii', 'binary32', 'binary64'
    
    """

    from clawpack.pyclaw.util import read_data_line
    import logging
    logger = logging.getLogger('pyclaw.fileio')

    base_path = os.path.join(path, )
    path = os.path.join(base_path, '%s.t' % file_prefix) + str(frame).zfill(4)
    logger.debug("Opening %s file." % path)
    with open(path, 'r') as f:
        t = read_data_line(f)
        num_eqn = read_data_line(f, data_type=int)
        nstates = read_data_line(f, data_type=int)
        num_aux = read_data_line(f, data_type=int)
        num_dim = read_data_line(f, data_type=int)
        num_ghost = read_data_line(f, data_type=int)
        try:
            file_format = read_data_line(f, data_type=str)
        except:
            file_format = None

    return t, num_eqn, nstates, num_aux, num_dim, num_ghost, file_format