def bader_analysis_from_objects(chgcar, potcar=None, aeccar0=None, aeccar2=None):
    """
    Convenience method to run Bader analysis from a set
    of pymatgen Chgcar and Potcar objects.

    This method will:

    1. If aeccar objects are present, constructs a temporary reference
    file as AECCAR0 + AECCAR2
    2. Runs Bader analysis twice: once for charge, and a second time
    for the charge difference (magnetization density).

    :param chgcar: Chgcar object
    :param potcar: (optional) Potcar object
    :param aeccar0: (optional) Chgcar object from aeccar0 file
    :param aeccar2: (optional) Chgcar object from aeccar2 file
    :return: summary dict
    """

    with ScratchDir(".") as temp_dir:

        if aeccar0 and aeccar2:
            # construct reference file
            chgref = aeccar0.linear_add(aeccar2)
            chgref_path = os.path.join(temp_dir, 'CHGCAR_ref')
            chgref.write_file(chgref_path)
        else:
            chgref_path = None

        chgcar.write_file('CHGCAR')
        chgcar_path = os.path.join(temp_dir, 'CHGCAR')

        if potcar:
            potcar.write_file('POTCAR')
            potcar_path = os.path.join(temp_dir, 'POTCAR')
        else:
            potcar_path = None

        ba = BaderAnalysis(chgcar_path, potcar_filename=potcar_path, chgref_filename=chgref_path)

        summary = {
            "min_dist": [d['min_dist'] for d in ba.data],
            "charge": [d['charge'] for d in ba.data],
            "atomic_volume": [d['atomic_vol'] for d in ba.data],
            "vacuum_charge": ba.vacuum_charge,
            "vacuum_volume": ba.vacuum_volume,
            "reference_used": True if chgref_path else False,
            "bader_version": ba.version,
        }

        if potcar:
            charge_transfer = [ba.get_charge_transfer(i) for i in range(len(ba.data))]
            summary['charge_transfer'] = charge_transfer

        if chgcar.is_spin_polarized:
            # write a CHGCAR containing magnetization density only
            chgcar.data['total'] = chgcar.data['diff']
            chgcar.is_spin_polarized = False
            chgcar.write_file('CHGCAR_mag')

            chgcar_mag_path = os.path.join(temp_dir, 'CHGCAR_mag')
            ba = BaderAnalysis(chgcar_mag_path, potcar_filename=potcar_path, chgref_filename=chgref_path)
            summary["magmom"] = [d['charge'] for d in ba.data]

        return summary
def bader_analysis_from_objects ( chgcar , potcar = None , aeccar0 = None , aeccar2 = None ) : with ScratchDir ( "." ) as temp_dir : if aeccar0 and aeccar2 : chgref = aeccar0 . linear_add ( aeccar2 ) chgref_path = os . path . join ( temp_dir , 'CHGCAR_ref' ) chgref . write_file ( chgref_path ) else : chgref_path = None chgcar . write_file ( 'CHGCAR' ) chgcar_path = os . path . join ( temp_dir , 'CHGCAR' ) if potcar : potcar . write_file ( 'POTCAR' ) potcar_path = os . path . join ( temp_dir , 'POTCAR' ) else : potcar_path = None ba = BaderAnalysis ( chgcar_path , potcar_filename = potcar_path , chgref_filename = chgref_path ) summary = { "min_dist" : [ d [ 'min_dist' ] for d in ba . data ] , "charge" : [ d [ 'charge' ] for d in ba . data ] , "atomic_volume" : [ d [ 'atomic_vol' ] for d in ba . data ] , "vacuum_charge" : ba . vacuum_charge , "vacuum_volume" : ba . vacuum_volume , "reference_used" : True if chgref_path else False , "bader_version" : ba . version , } if potcar : charge_transfer = [ ba . get_charge_transfer ( i ) for i in range ( len ( ba . data ) ) ] summary [ 'charge_transfer' ] = charge_transfer if chgcar . is_spin_polarized : chgcar . data [ 'total' ] = chgcar . data [ 'diff' ] chgcar . is_spin_polarized = False chgcar . write_file ( 'CHGCAR_mag' ) chgcar_mag_path = os . path . join ( temp_dir , 'CHGCAR_mag' ) ba = BaderAnalysis ( chgcar_mag_path , potcar_filename = potcar_path , chgref_filename = chgref_path ) summary [ "magmom" ] = [ d [ 'charge' ] for d in ba . data ] return summary
Convenience method to run Bader analysis from a set
    of pymatgen Chgcar and Potcar objects.

    This method will:

    1. If aeccar objects are present, constructs a temporary reference
    file as AECCAR0 + AECCAR2
    2. Runs Bader analysis twice: once for charge, and a second time
    for the charge difference (magnetization density).

    :param chgcar: Chgcar object
    :param potcar: (optional) Potcar object
    :param aeccar0: (optional) Chgcar object from aeccar0 file
    :param aeccar2: (optional) Chgcar object from aeccar2 file
    :return: summary dict