def write_extxyz(fname, cell, PBC, species, coords, energy=None, forces=
    None, stress=None):
    """Write configuration info to a file in extended xyz format.

    Parameters
    ----------
    fname: str
        name of the written file

    cell: 2D array of shape(3,3)
        supercell lattice vectors

    PBC: list of 3 bool
        periodic boundary conditions

    species: list of N str, where N is the number of atoms
        species of atoms

    coords: 2D array of shape (N, 3)
        coordinates of atoms

    energy: float (optional)
        potential energy of the configuration

    forces: 2D array of shape (N, 3) (optional)
        forces on atoms

    stress: list of 6 float (optional)
        stress on the cell in Voigt notation
    """
    with open(fname, 'w') as fout:
        natoms = len(species)
        fout.write('{}\n'.format(natoms))
        fout.write('Lattice="')
        for i, line in enumerate(cell):
            for j, item in enumerate(line):
                if i == 2 and j == 2:
                    fout.write('{:.15g}" '.format(item))
                else:
                    fout.write('{:.15g} '.format(item))
        PBC = [int(i) for i in PBC]
        fout.write('PBC="{} {} {}" '.format(PBC[0], PBC[1], PBC[2]))
        if energy is not None:
            fout.write('Energy="{:.15g}" '.format(energy))
        if stress is not None:
            fout.write('Stress="')
            for i, s in enumerate(stress):
                if i == 5:
                    fout.write('{:.15g}" '.format(s))
                else:
                    fout.write('{:.15g} '.format(s))
        properties = 'Properties=species:S:1:pos:R:3'
        if forces is not None:
            properties += ':for:R:3\n'
        else:
            properties += '\n'
        fout.write(properties)
        for i in range(natoms):
            fout.write('{:2s} '.format(species[i]))
            fout.write('{:23.15e} {:23.15e} {:23.15e} '.format(coords[i][0],
                coords[i][1], coords[i][2]))
            if forces is not None:
                fout.write('{:23.15e} {:23.15e} {:23.15e}'.format(forces[i]
                    [0], forces[i][1], forces[i][2]))
            fout.write('\n')
