def read(self, filename, mode='', ws='all', idiotproof=True):
        if idiotproof and not filename.endswith('.ind'):
            raise IOError('Filename must end with `.ind`.')

        reads = self._parse_readwritemode(mode)

        # Open reader (handles masters)
        tar = Reader(filename)

        # Actual read
        self.nw = tar.dimension('nw')
        if ws == 'all':
            ws = range(self.nw)
        self.nw = len(ws)
        self._read(tar, reads, ws)

        # Close
        tar.close()
        self.world.barrier()
Example #2
0
    def read(self, filename, idiotproof=True):
        if idiotproof and not filename.endswith('.ftd'):
            raise IOError('Filename must end with `.ftd`.')

        tar = Reader(filename)

        # Test data type
        dtype = {'Float':float, 'Complex':complex}[tar['DataType']]
        if dtype != self.dtype:
            raise IOError('Data is an incompatible type.')

        # Test time
        time = tar['Time']
        if idiotproof and abs(time-self.time) >= 1e-9:
            raise IOError('Timestamp is incompatible with calculator.')

        # Test timestep (non-critical)
        timestep = tar['TimeStep']
        if abs(timestep - self.timestep) > 1e-12:
            print 'Warning: Time-step has been altered. (%lf -> %lf)' \
                % (self.timestep, timestep)
        self.timestep = timestep

        # Test dimensions
        nw = tar.dimension('nw')
        nspins = tar.dimension('nspins')
        ng = (tar.dimension('ngptsx'), tar.dimension('ngptsy'), \
              tar.dimension('ngptsz'),)

        if (nw != self.nw or nspins != self.nspins or
            (ng != self.gd.get_size_of_global_array()).any()):
            raise IOError('Data has incompatible shapes.')

        # Test width (non-critical)
        sigma = tar['Width']
        if ((sigma is None)!=(self.sigma is None) or # float <-> None
            (sigma is not None and self.sigma is not None and \
             abs(sigma - self.sigma) > 1e-12)): # float -> float
            print 'Warning: Width has been altered. (%s -> %s)' \
                % (self.sigma, sigma)
        self.sigma = sigma

        # Read frequencies
        self.omega_w[:] = tar.get('Frequency')

        # Read cumulative phase factors
        self.gamma_w[:] = tar.get('PhaseFactor')

        # Read average densities on master and distribute
        for s in range(self.nspins):
            all_Ant_G = tar.get('Average', s)
            self.gd.distribute(all_Ant_G, self.Ant_sG[s])

        # Read fourier transforms on master and distribute
        for w in range(self.nw):
            for s in range(self.nspins):
                all_Fnt_G = tar.get('FourierTransform', w, s)
                self.gd.distribute(all_Fnt_G, self.Fnt_wsG[w,s])

        # Close for good measure
        tar.close()
Example #3
0
    def read(self, filename, idiotproof=True):
        if idiotproof and not filename.endswith('.ftd'):
            raise IOError('Filename must end with `.ftd`.')

        tar = Reader(filename)

        # Test data type
        dtype = {'Float': float, 'Complex': complex}[tar['DataType']]
        if dtype != self.dtype:
            raise IOError('Data is an incompatible type.')

        # Test time
        time = tar['Time']
        if idiotproof and abs(time - self.time) >= 1e-9:
            raise IOError('Timestamp is incompatible with calculator.')

        # Test timestep (non-critical)
        timestep = tar['TimeStep']
        if abs(timestep - self.timestep) > 1e-12:
            print('Warning: Time-step has been altered. (%lf -> %lf)' \
                % (self.timestep, timestep))
        self.timestep = timestep

        # Test dimensions
        nw = tar.dimension('nw')
        nspins = tar.dimension('nspins')
        ng = (tar.dimension('ngptsx'), tar.dimension('ngptsy'), \
              tar.dimension('ngptsz'),)

        if (nw != self.nw or nspins != self.nspins
                or (ng != self.gd.get_size_of_global_array()).any()):
            raise IOError('Data has incompatible shapes.')

        # Test width (non-critical)
        sigma = tar['Width']
        if ((sigma is None)!=(self.sigma is None) or # float <-> None
            (sigma is not None and self.sigma is not None and \
             abs(sigma - self.sigma) > 1e-12)): # float -> float
            print('Warning: Width has been altered. (%s -> %s)' \
                % (self.sigma, sigma))
        self.sigma = sigma

        # Read frequencies
        self.omega_w[:] = tar.get('Frequency')

        # Read cumulative phase factors
        self.gamma_w[:] = tar.get('PhaseFactor')

        # Read average densities on master and distribute
        for s in range(self.nspins):
            all_Ant_G = tar.get('Average', s)
            self.gd.distribute(all_Ant_G, self.Ant_sG[s])

        # Read fourier transforms on master and distribute
        for w in range(self.nw):
            for s in range(self.nspins):
                all_Fnt_G = tar.get('FourierTransform', w, s)
                self.gd.distribute(all_Fnt_G, self.Fnt_wsG[w, s])

        # Close for good measure
        tar.close()
def read_data(filename, keys=None, ws='all'):
    """
    Read data arrays for post processing.
    
    Not parallel safe. No GridDescriptor, only numpy arrays.
    
    Parameters
    ----------
    filename: string
        File to be read.
    keys: list of strings
        Keys to be read.
    ws: list of ints
        Indices of frequencies to be read.
    """

    key_to_tarname = {
        'n0t_sG': 'n0t_sG',
        'Fnt_wsG': 'Fnt_wsG',
        'Frho_wg': 'Frho_wg',
        'Fphi_wg': 'Fphi_wg',
        'Fef_wvg': 'Fef_wvg',
        'Ffe_wg': 'Ffe_wg',
        'eps0_G': 'eps0_G'
    }

    print('Reading %s' % (filename))

    if keys is None:
        keys = key_to_tarname.keys()  # all keys

    tar = Reader(filename)

    omega_w = tar.get('omega_w')

    if ws == 'all':
        ws = range(len(omega_w))
    else:
        omega_w = omega_w[ws]

    freq_w = omega_w * aufrequency_to_eV

    try:
        nspins = tar.dimension('nspins')
    except KeyError:
        nspins = None

    na = tar['na']
    try:
        atomnum_a = tar.get('atomnum_a')
        atompos_av = tar.get('atompos_a')
        atomcell_cv = tar.get('atomcell_cv')
        Fbgef_v = tar.get('Fbgef_v')

        atom_a = []
        for a in range(na):
            atom_a.append({'atom': atomnum_a[a], 'pos': atompos_av[a]})
    except KeyError:
        atom_a = None
        atomcell_cv = None
        Fbgef_v = None
        print('no atoms')

    data = dict()
    data['freq_w'] = freq_w
    data['nspins'] = nspins
    data['na'] = na
    data['atom_a'] = atom_a
    data['cell_cv'] = atomcell_cv
    data['Fbgef_v'] = Fbgef_v

    try:
        data['corner1_v'] = tar.get('corner1_v')
        data['corner2_v'] = tar.get('corner2_v')
    except:
        print('no corners')

    try:
        FD_awsp = {}
        D0_asp = {}
        for a in range(na):
            FD_awsp[a] = tar.get('FD_%dwsp' % a)
            D0_asp[a] = tar.get('D0_%dsp' % a)
        data['FD_awsp'] = FD_awsp
        data['D0_asp'] = D0_asp
    except KeyError:
        print('no FD_awsp')

    for key in keys:
        try:
            if '_w' in key:
                tmp = zero_pad(tar.get(key_to_tarname[key], ws[0]))
                data[key] = np.empty((len(ws), ) + tmp.shape, tmp.dtype)
                data[key][0] = tmp
                for w, wread in enumerate(ws[1:], 1):
                    data[key][w] = zero_pad(tar.get(key_to_tarname[key],
                                                    wread))
            else:
                data[key] = zero_pad(tar.get(key_to_tarname[key]))
        except KeyError:
            print('no %s' % key)
            pass

    tar.close()

    return data