Пример #1
0
def get_fc2(supercell,
            primitive,
            displacements,
            forces,
            atom_list=None,
            options=None,
            log_level=0):
    if options is None:
        option_dict = {}
    else:
        option_dict = decode_options(options)

    structures = []
    for d, f in zip(displacements, forces):
        structure = Atoms(cell=supercell.cell,
                          scaled_positions=supercell.scaled_positions,
                          numbers=supercell.numbers,
                          pbc=True)
        structure.new_array('displacements', d)
        structure.new_array('forces', f)
        structure.calc = None
        structures.append(structure)

    cutoffs = [
        option_dict['cutoff'],
    ]
    cs = ClusterSpace(structures[0], cutoffs)
    print(cs)
    cs.print_orbits()

    sc = StructureContainer(cs)
    for structure in structures:
        sc.add_structure(structure)
    print(sc)

    opt = Optimizer(sc.get_fit_data())
    opt.train()
    print(opt)

    fcp = ForceConstantPotential(cs, opt.parameters)
    print(fcp)
Пример #2
0
def run_hiphive(supercell, primitive, displacements, forces, options, symprec,
                log_level):
    """Run hiphive

    supercell : Supercell
        Perfect supercell.
    primitive : Primitive
        Primitive cell.
    displacements : ndarray
        Displacements of atoms in supercell.
        shape=(supercells, natom, 3)
    forces : ndarray
        Forces on atoms in supercell.
        shape=(supercells, natom, 3)
    options : str
        Force constants calculation options.
    log_level : int
        Log control. 0: quiet, 1: normal, 2: verbose 3: debug

    """

    ase_supercell = phonopy_atoms_to_ase(supercell)
    ase_prim = phonopy_atoms_to_ase(primitive)

    # setup training structures
    structures = []
    for d, f in zip(displacements, forces):
        structure = ase_supercell.copy()
        structure.new_array('displacements', d)
        structure.new_array('forces', f)
        structures.append(structure)

    # parse options
    if options is None:
        options_dict = {}
    else:
        options_dict = _decode_options(options)

    # select cutoff
    max_cutoff = estimate_maximum_cutoff(ase_supercell) - 1e-5
    if 'cutoff' in options_dict:
        cutoff = options_dict['cutoff']
        if cutoff > max_cutoff:
            raise ValueError(
                'Cutoff {:.4f} is larger than maximum allowed '
                'cutoff, {:.4f}, for the given supercell.'
                '\nDecrease cutoff or provide larger supercells.'.format(
                    cutoff, max_cutoff))
    else:
        cutoff = max_cutoff

    # setup ClusterSpace
    cutoffs = [cutoff]
    cs = ClusterSpace(ase_prim, cutoffs, symprec=symprec)
    cs.print_orbits()

    sc = StructureContainer(cs)
    for structure in structures:
        sc.add_structure(structure)
    n_rows, n_cols = sc.data_shape
    if n_rows < n_cols:
        raise ValueError('Fitting problem is under-determined.'
                         '\nProvide more structures or decrease cutoff.')

    # Estimate error
    opt = Optimizer(sc.get_fit_data(), train_size=0.75)
    opt.train()
    print(opt)
    print('RMSE train : {:.4f}'.format(opt.rmse_train))
    print('RMSE test  : {:.4f}'.format(opt.rmse_test))

    # Final train
    opt = Optimizer(sc.get_fit_data(), train_size=1.0)
    opt.train()

    # get force constants
    fcp = ForceConstantPotential(cs, opt.parameters)
    fcs = fcp.get_force_constants(ase_supercell)
    fc2 = fcs.get_fc_array(order=2)

    return fc2
Пример #3
0
def run_hiphive(supercell, primitive, displacements, forces, options, symprec,
                log_level):
    """Run hiphive.

    supercell : Supercell
        Perfect supercell.
    primitive : Primitive
        Primitive cell.
    displacements : ndarray
        Displacements of atoms in supercell.
        shape=(supercells, natom, 3)
    forces : ndarray
        Forces on atoms in supercell.
        shape=(supercells, natom, 3)
    options : str
        Force constants calculation options.
    log_level : int
        Log control. 0: quiet, 1: normal, 2: verbose 3: debug

    """
    try:
        from hiphive import ClusterSpace, ForceConstantPotential, StructureContainer
        from hiphive.cutoffs import estimate_maximum_cutoff
        from hiphive.fitting import Optimizer
        from hiphive.input_output.logging_tools import set_config
    except ImportError:
        raise ImportError("hiPhive python module was not found.")

    set_config(level=30)

    ase_supercell = phonopy_atoms_to_ase(supercell)
    ase_prim = phonopy_atoms_to_ase(primitive)

    # setup training structures
    structures = []
    for d, f in zip(displacements, forces):
        structure = ase_supercell.copy()
        structure.new_array("displacements", d)
        structure.new_array("forces", f)
        structures.append(structure)

    # parse options
    if options is None:
        options_dict = {}
    else:
        options_dict = _decode_options(options)

    # select cutoff
    max_cutoff = estimate_maximum_cutoff(ase_supercell) - 1e-5
    if "cutoff" in options_dict:
        cutoff = options_dict["cutoff"]
        if cutoff > max_cutoff:
            raise ValueError(
                "Cutoff {:.4f} is larger than maximum allowed "
                "cutoff, {:.4f}, for the given supercell."
                "\nDecrease cutoff or provide larger supercells.".format(
                    cutoff, max_cutoff))
    else:
        cutoff = max_cutoff

    # setup ClusterSpace
    cutoffs = [cutoff]
    cs = ClusterSpace(ase_prim, cutoffs, symprec=symprec)
    cs.print_orbits()

    sc = StructureContainer(cs)
    for structure in structures:
        sc.add_structure(structure)
    n_rows, n_cols = sc.data_shape
    if n_rows < n_cols:
        raise ValueError("Fitting problem is under-determined."
                         "\nProvide more structures or decrease cutoff.")

    # Estimate error
    opt = Optimizer(sc.get_fit_data(), train_size=0.75)
    opt.train()
    print(opt)
    print("RMSE train : {:.4f}".format(opt.rmse_train))
    print("RMSE test  : {:.4f}".format(opt.rmse_test))

    # Final train
    opt = Optimizer(sc.get_fit_data(), train_size=1.0)
    opt.train()

    # get force constants
    fcp = ForceConstantPotential(cs, opt.parameters)
    fcs = fcp.get_force_constants(ase_supercell)
    fc2 = fcs.get_fc_array(order=2)

    return fc2