Beispiel #1
0
def check_output(fn_h5, grp_name, overwrite):
    '''Check if the output is already present in print a warning if --overwrite is not used

       **Arguments:**

       fn_h5
            The output HDF5 file

       grp_name
            The HDF5 group in which the output is stored.

       overwrite
            Whether the user wants to overwrite contents that were already
            present.
    '''
    if os.path.isfile(fn_h5):
        with LockedH5File(fn_h5, 'r') as f:
            if grp_name in f and len(f[grp_name]) > 0:
                if overwrite:
                    if log.do_warning:
                        log.warn('Overwriting the contents of "%s" in the file "%s".' % (grp_name, fn_h5))
                    return False
                else:
                    if log.do_warning:
                        log.warn('Skipping because the group "%s" is already present in the file "%s" and it is not empty.' % (grp_name, fn_h5))
                    return True
    return False
Beispiel #2
0
def check_output(fn_h5, grp_name, overwrite):
    '''Check if the output is already present in print a warning if --overwrite is not used

       **Arguments:**

       fn_h5
            The output HDF5 file

       grp_name
            The HDF5 group in which the output is stored.

       overwrite
            Whether the user wants to overwrite contents that were already
            present.
    '''
    if os.path.isfile(fn_h5):
        with LockedH5File(fn_h5, 'r') as f:
            if grp_name in f and len(f[grp_name]) > 0:
                if overwrite:
                    if log.do_warning:
                        log.warn('Overwriting the contents of "%s" in the file "%s".' % (grp_name, fn_h5))
                    return False
                else:
                    if log.do_warning:
                        log.warn('Skipping because the group "%s" is already present in the file "%s" and it is not empty.' % (grp_name, fn_h5))
                    return True
    return False
Beispiel #3
0
    def write_input(self, number, charge, mult, template, do_overwrite):
        # Directory stuff
        nel = number - charge
        dn_mult = '%03i_%s_%03i_q%+03i/mult%02i' % (
            number, periodic[number].symbol.lower().rjust(
                2, '_'), nel, charge, mult)

        # Figure out if we want to write
        fn_inp = '%s/atom.in' % dn_mult
        exists = os.path.isfile(fn_inp)
        do_write = not exists or do_overwrite

        if do_write:
            try:
                subs = template.get_subs(number, nel, mult)
            except KeyError:
                if log.do_warning:
                    log.warn(
                        'Could not find all subs for %03i.%03i.%03i. Skipping.'
                        % (number, nel, mult))
                return dn_mult, False
            if not os.path.isdir(dn_mult):
                os.makedirs(dn_mult)
            with open(fn_inp, 'w') as f:
                f.write(
                    template.substitute(
                        subs,
                        charge=str(charge),
                        mult=str(mult),
                        number=str(number),
                        element=periodic[number].symbol,
                    ))
            if log.do_medium:
                if exists:
                    log('Overwritten:      ', fn_inp)
                else:
                    log('Written new:      ', fn_inp)
        elif log.do_medium:
            log('Not overwriting:  ', fn_inp)

        return dn_mult, do_write
Beispiel #4
0
    def write_input(self, number, charge, mult, template, do_overwrite):
        # Directory stuff
        nel = number - charge
        dn_mult = "%03i_%s_%03i_q%+03i/mult%02i" % (
            number,
            periodic[number].symbol.lower().rjust(2, "_"),
            nel,
            charge,
            mult,
        )

        # Figure out if we want to write
        fn_inp = "%s/atom.in" % dn_mult
        exists = os.path.isfile(fn_inp)
        do_write = not exists or do_overwrite

        if do_write:
            try:
                subs = template.get_subs(number, nel, mult)
            except KeyError:
                if log.do_warning:
                    log.warn("Could not find all subs for %03i.%03i.%03i. Skipping." % (number, nel, mult))
                return dn_mult, False
            if not os.path.isdir(dn_mult):
                os.makedirs(dn_mult)
            with open(fn_inp, "w") as f:
                f.write(
                    template.substitute(
                        subs, charge=str(charge), mult=str(mult), number=str(number), element=periodic[number].symbol
                    )
                )
            if log.do_medium:
                if exists:
                    log("Overwritten:      ", fn_inp)
                else:
                    log("Written new:      ", fn_inp)
        elif log.do_medium:
            log("Not overwriting:  ", fn_inp)

        return dn_mult, do_write
Beispiel #5
0
def main():
    args = parse_args()

    fn_h5, grp_name = parse_h5(args.h5, 'h5')
    with LockedH5File(fn_h5, 'r') as fin, open(args.csv, 'w') as fout:
        w = csv.writer(fout)
        w.writerow(['Converted data from %s' % args.h5])
        w.writerow([])
        for name, dset in iter_datasets(fin[grp_name]):
            if len(dset.shape) > 3:
                if log.do_warning:
                    log.warn(
                        'Skipping %s because it has more than three axes.' %
                        name)
            else:
                log('Converting %s' % name)

            w.writerow(['Dataset', name])
            w.writerow(['Shape'] + list(dset.shape))
            if len(dset.shape) == 0:
                w.writerow([dset[()]])
            elif len(dset.shape) == 1:
                for value in dset:
                    w.writerow([value])
            elif len(dset.shape) == 2:
                for row in dset:
                    w.writerow([value for value in row])
            elif len(dset.shape) == 3:
                for array in dset:
                    l = []
                    for col in array.T:
                        for value in col:
                            l.append(value)
                        l.append('')
                    del l[-1]
                    w.writerow(l)
            else:
                w.writerow(['Skipped because ndim=%i>3' % len(dset.shape)])
            w.writerow([])
Beispiel #6
0
def main():
    args = parse_args()

    fn_h5, grp_name = parse_h5(args.h5, 'h5')
    with LockedH5File(fn_h5, 'r') as fin, open(args.csv, 'w') as fout:
        w = csv.writer(fout)
        w.writerow(['Converted data from %s' % args.h5])
        w.writerow([])
        for name, dset in iter_datasets(fin[grp_name]):
            if len(dset.shape) > 3:
                if log.do_warning:
                    log.warn('Skipping %s because it has more than three axes.' % name)
            else:
                log('Converting %s' % name)

            w.writerow(['Dataset', name])
            w.writerow(['Shape'] + list(dset.shape))
            if len(dset.shape) == 0:
                w.writerow([dset[()]])
            elif len(dset.shape) == 1:
                for value in dset:
                    w.writerow([value])
            elif len(dset.shape) == 2:
                for row in dset:
                    w.writerow([value for value in row])
            elif len(dset.shape) == 3:
                for array in dset:
                    l = []
                    for col in array.T:
                        for value in col:
                            l.append(value)
                        l.append('')
                    del l[-1]
                    w.writerow(l)
            else:
                w.writerow(['Skipped because ndim=%i>3' % len(dset.shape)])
            w.writerow([])
Beispiel #7
0
def plot_atoms(proatomdb):
    try:
        import matplotlib.pyplot as pt
    except ImportError:
        if log.do_warning:
            log.warn('Skipping plots because matplotlib was not found.')
        return

    lss = {True: '-', False: ':'}
    for number in proatomdb.get_numbers():
        r = proatomdb.get_rgrid(number).radii
        symbol = periodic[number].symbol
        charges = proatomdb.get_charges(number)
        suffix = '%03i_%s' % (number, symbol.lower().rjust(2, '_'))

        # The density (rho)
        pt.clf()
        for i, charge in enumerate(charges):
            record = proatomdb.get_record(number, charge)
            y = record.rho
            ls = lss[record.safe]
            color = get_color(i)
            label = 'q=%+i' % charge
            pt.semilogy(r / angstrom, y, lw=2, ls=ls, label=label, color=color)
        pt.xlim(0, 3)
        pt.ylim(ymin=1e-5)
        pt.xlabel('Distance from the nucleus [A]')
        pt.ylabel('Spherically averaged density [Bohr**-3]')
        pt.title('Proatoms for element %s (%i)' % (symbol, number))
        pt.legend(loc=0)
        fn_png = 'dens_%s.png' % suffix
        pt.savefig(fn_png)
        if log.do_medium:
            log('Written', fn_png)

        # 4*pi*r**2*rho
        pt.clf()
        for i, charge in enumerate(charges):
            record = proatomdb.get_record(number, charge)
            y = record.rho
            ls = lss[record.safe]
            color = get_color(i)
            label = 'q=%+i' % charge
            pt.plot(r / angstrom,
                    4 * np.pi * r**2 * y,
                    lw=2,
                    ls=ls,
                    label=label,
                    color=color)
        pt.xlim(0, 3)
        pt.ylim(ymin=0.0)
        pt.xlabel('Distance from the nucleus [A]')
        pt.ylabel('4*pi*r**2*density [Bohr**-1]')
        pt.title('Proatoms for element %s (%i)' % (symbol, number))
        pt.legend(loc=0)
        fn_png = 'rdens_%s.png' % suffix
        pt.savefig(fn_png)
        if log.do_medium:
            log('Written', fn_png)

        fukui_data = []
        if number - charges[0] == 1:
            record0 = proatomdb.get_record(number, charges[0])
            fukui_data.append((record0.rho, record0.safe, '%+i' % charges[0]))
        for i, charge in enumerate(charges[1:]):
            record0 = proatomdb.get_record(number, charge)
            record1 = proatomdb.get_record(number, charges[i])
            fukui_data.append(
                (record0.rho - record1.rho, record0.safe
                 and record1.safe, '%+i-%+i' % (charge, charges[i])))

        # The Fukui functions
        pt.clf()
        for i, (f, safe, label) in enumerate(fukui_data):
            ls = lss[safe]
            color = get_color(i)
            pt.semilogy(r / angstrom,
                        f,
                        lw=2,
                        ls=ls,
                        label=label,
                        color=color,
                        alpha=1.0)
            pt.semilogy(r / angstrom, -f, lw=2, ls=ls, color=color, alpha=0.2)
        pt.xlim(0, 3)
        pt.ylim(ymin=1e-5)
        pt.xlabel('Distance from the nucleus [A]')
        pt.ylabel('Fukui function [Bohr**-3]')
        pt.title('Proatoms for element %s (%i)' % (symbol, number))
        pt.legend(loc=0)
        fn_png = 'fukui_%s.png' % suffix
        pt.savefig(fn_png)
        if log.do_medium:
            log('Written', fn_png)

        # 4*pi*r**2*Fukui
        pt.clf()
        for i, (f, safe, label) in enumerate(fukui_data):
            ls = lss[safe]
            color = get_color(i)
            pt.plot(r / angstrom,
                    4 * np.pi * r**2 * f,
                    lw=2,
                    ls=ls,
                    label=label,
                    color=color)
        pt.xlim(0, 3)
        pt.xlabel('Distance from the nucleus [A]')
        pt.ylabel('4*pi*r**2*Fukui [Bohr**-1]')
        pt.title('Proatoms for element %s (%i)' % (symbol, number))
        pt.legend(loc=0)
        fn_png = 'rfukui_%s.png' % suffix
        pt.savefig(fn_png)
        if log.do_medium:
            log('Written', fn_png)
Beispiel #8
0
def plot_atoms(proatomdb):
    try:
        import matplotlib.pyplot as pt
    except ImportError:
        if log.do_warning:
            log.warn('Skipping plots because matplotlib was not found.')
        return

    lss = {True: '-', False: ':'}
    for number in proatomdb.get_numbers():
        r = proatomdb.get_rgrid(number).radii
        symbol = periodic[number].symbol
        charges = proatomdb.get_charges(number)
        suffix = '%03i_%s' % (number, symbol.lower().rjust(2, '_'))

        # The density (rho)
        pt.clf()
        for i, charge in enumerate(charges):
            record = proatomdb.get_record(number, charge)
            y = record.rho
            ls = lss[record.safe]
            color = get_color(i)
            label = 'q=%+i' % charge
            pt.semilogy(r/angstrom, y, lw=2, ls=ls, label=label, color=color)
        pt.xlim(0, 3)
        pt.ylim(ymin=1e-5)
        pt.xlabel('Distance from the nucleus [A]')
        pt.ylabel('Spherically averaged density [Bohr**-3]')
        pt.title('Proatoms for element %s (%i)' % (symbol, number))
        pt.legend(loc=0)
        fn_png  = 'dens_%s.png' % suffix
        pt.savefig(fn_png)
        if log.do_medium:
            log('Written', fn_png)

        # 4*pi*r**2*rho
        pt.clf()
        for i, charge in enumerate(charges):
            record = proatomdb.get_record(number, charge)
            y = record.rho
            ls = lss[record.safe]
            color = get_color(i)
            label = 'q=%+i' % charge
            pt.plot(r/angstrom, 4*np.pi*r**2*y, lw=2, ls=ls, label=label, color=color)
        pt.xlim(0, 3)
        pt.ylim(ymin=0.0)
        pt.xlabel('Distance from the nucleus [A]')
        pt.ylabel('4*pi*r**2*density [Bohr**-1]')
        pt.title('Proatoms for element %s (%i)' % (symbol, number))
        pt.legend(loc=0)
        fn_png  = 'rdens_%s.png' % suffix
        pt.savefig(fn_png)
        if log.do_medium:
            log('Written', fn_png)

        fukui_data = []
        if number - charges[0] == 1:
            record0 = proatomdb.get_record(number, charges[0])
            fukui_data.append((record0.rho, record0.safe, '%+i' % charges[0]))
        for i, charge in enumerate(charges[1:]):
            record0 = proatomdb.get_record(number, charge)
            record1 = proatomdb.get_record(number, charges[i])
            fukui_data.append((
                record0.rho - record1.rho,
                record0.safe and record1.safe,
                '%+i-%+i' % (charge, charges[i])
            ))

        # The Fukui functions
        pt.clf()
        for i, (f, safe, label) in enumerate(fukui_data):
            ls = lss[safe]
            color = get_color(i)
            pt.semilogy(r/angstrom, f, lw=2, ls=ls, label=label, color=color, alpha=1.0)
            pt.semilogy(r/angstrom, -f, lw=2, ls=ls, color=color, alpha=0.2)
        pt.xlim(0, 3)
        pt.ylim(ymin=1e-5)
        pt.xlabel('Distance from the nucleus [A]')
        pt.ylabel('Fukui function [Bohr**-3]')
        pt.title('Proatoms for element %s (%i)' % (symbol, number))
        pt.legend(loc=0)
        fn_png  = 'fukui_%s.png' % suffix
        pt.savefig(fn_png)
        if log.do_medium:
            log('Written', fn_png)

        # 4*pi*r**2*Fukui
        pt.clf()
        for i, (f, safe, label) in enumerate(fukui_data):
            ls = lss[safe]
            color = get_color(i)
            pt.plot(r/angstrom, 4*np.pi*r**2*f, lw=2, ls=ls, label=label, color=color)
        pt.xlim(0, 3)
        pt.xlabel('Distance from the nucleus [A]')
        pt.ylabel('4*pi*r**2*Fukui [Bohr**-1]')
        pt.title('Proatoms for element %s (%i)' % (symbol, number))
        pt.legend(loc=0)
        fn_png  = 'rfukui_%s.png' % suffix
        pt.savefig(fn_png)
        if log.do_medium:
            log('Written', fn_png)