def get_pseudos_qe(structure, family_name): """ Set the pseudo to use for all atomic kinds, picking pseudos from the family with name family_name. :note: The structure must already be set. :param family_name: the name of the group containing the pseudos """ from collections import defaultdict from aiida.orm.data.upf import get_pseudos_from_structure # A dict {kind_name: pseudo_object} kind_pseudo_dict = get_pseudos_from_structure(structure, family_name) # We have to group the species by pseudo, I use the pseudo PK # pseudo_dict will just map PK->pseudo_object pseudo_dict = {} # Will contain a list of all species of the pseudo with given PK pseudo_species = defaultdict(list) for kindname, pseudo in kind_pseudo_dict.iteritems(): pseudo_dict[pseudo.pk] = pseudo pseudo_species[pseudo.pk].append(kindname) pseudos = {} for pseudo_pk in pseudo_dict: pseudo = pseudo_dict[pseudo_pk] kinds = pseudo_species[pseudo_pk] for kind in kinds: pseudos[kind] = pseudo return pseudos
def get_pseudos(structure, family_name): from collections import defaultdict from aiida.orm.data.upf import get_pseudos_from_structure # A dict {kind_name: pseudo_object} kind_pseudo_dict = get_pseudos_from_structure(structure, family_name) # We have to group the species by pseudo, I use the pseudo PK # pseudo_dict will just map PK->pseudo_object pseudo_dict = {} # Will contain a list of all species of the pseudo with given PK pseudo_species = defaultdict(list) for kindname, pseudo in kind_pseudo_dict.iteritems(): pseudo_dict[pseudo.pk] = pseudo pseudo_species[pseudo.pk].append(kindname) pseudos = {} for pseudo_pk in pseudo_dict: pseudo = pseudo_dict[pseudo_pk] kinds = pseudo_species[pseudo_pk] for kind in kinds: pseudos[kind] = pseudo return pseudos
def get_pseudos(structure, family_name): """ Set the pseudo to use for all atomic kinds, picking pseudos from the family with name family_name. :note: The structure must already be set. :param family_name: the name of the group containing the pseudos """ from collections import defaultdict from aiida.orm.data.upf import get_pseudos_from_structure # A dict {kind_name: pseudo_object} kind_pseudo_dict = get_pseudos_from_structure(structure, family_name) # We have to group the species by pseudo, I use the pseudo PK # pseudo_dict will just map PK->pseudo_object pseudo_dict = {} # Will contain a list of all species of the pseudo with given PK pseudo_species = defaultdict(list) for kindname, pseudo in kind_pseudo_dict.iteritems(): pseudo_dict[pseudo.pk] = pseudo pseudo_species[pseudo.pk].append(kindname) pseudos = {} for pseudo_pk in pseudo_dict: pseudo = pseudo_dict[pseudo_pk] kinds = pseudo_species[pseudo_pk] for kind in kinds: pseudos[kind] = pseudo return pseudos
def _get_pseudos(self, structure, family_name): kind_pseudo_dict = get_pseudos_from_structure(structure, family_name) pseudos = {} for p in kind_pseudo_dict.values(): ps = [k for k, v in kind_pseudo_dict.items() if v == p] kinds = "_".join(ps) pseudos[kinds] = p return pseudos
def validate_and_prepare_pseudos_inputs(structure, pseudos=None, pseudo_family=None): """ Use the explicitly passed pseudos dictionary or use the pseudo_family in combination with the structure to obtain that dictionary. The pseudos dictionary should now be a dictionary of UPF nodes with the kind as linkname As such, if there are multiple kinds with the same element, there will be duplicate UPF nodes but multiple links for the same input node are not allowed. Moreover, to couple the UPF nodes to the Calculation instance, we have to go through the use_pseudo method, which takes the kind name as an additional parameter. When creating a Calculation through a Process instance, one cannot call the use methods directly but rather should pass them as keyword arguments. However, we can pass the additional parameters by using them as the keys of a dictionary :param structure: StructureData node :param pseudos: a dictionary where keys are the kind names and value are UpfData nodes :param pseudo_family: string name of the pseudopotential family to use :raises: ValueError if neither pseudos or pseudo_family is specified or if no UpfData is found for every element in the structure :returns: a dictionary of UpfData nodes where the key is a tuple with the kind name """ from aiida.orm.data.base import Str result_pseudos = {} unique_pseudos = {} if pseudos and pseudo_family: raise ValueError( 'You cannot specify both "pseudos" and "pseudo_family"') elif pseudos is None and pseudo_family is None: raise ValueError( 'Neither an explicit pseudos dictionary nor a pseudo_family was specified' ) elif pseudo_family: # This will already raise some exceptions, potentially, like the ones below pseudos = get_pseudos_from_structure(structure, str(pseudo_family)) if isinstance(pseudos, (str, unicode, Str)): raise TypeError( 'You passed "pseudos" as a string - maybe you wanted to pass it as "pseudo_family" instead?' ) for kind in structure.get_kind_names(): if kind not in pseudos: raise ValueError('no pseudo available for element {}'.format(kind)) elif not isinstance(pseudos[kind], UpfData): raise ValueError( 'pseudo for element {} is not of type UpfData'.format(kind)) for kind, pseudo in pseudos.iteritems(): unique_pseudos.setdefault(pseudo, []).append(kind) for pseudo, kinds in unique_pseudos.iteritems(): result_pseudos[tuple(kinds)] = pseudo return result_pseudos
def launch(code, structure, pseudo_family, kpoints, max_num_machines, max_wallclock_seconds, daemon, mode): """ Run a PwCalculation for a given input structure """ from aiida.orm import load_node from aiida.orm.data.parameter import ParameterData from aiida.orm.data.upf import get_pseudos_from_structure from aiida.orm.utils import CalculationFactory from aiida.work.run import run, submit from aiida_quantumespresso.utils.resources import get_default_options PwCalculation = CalculationFactory('quantumespresso.pw') parameters = { 'CONTROL': { 'calculation': mode, }, 'SYSTEM': { 'ecutwfc': 30., 'ecutrho': 240., }, } inputs = { 'code': code, 'structure': structure, 'pseudo': get_pseudos_from_structure(structure, pseudo_family), 'kpoints': kpoints, 'parameters': ParameterData(dict=parameters), 'settings': ParameterData(dict={}), '_options': get_default_options(max_num_machines, max_wallclock_seconds), } process = PwCalculation.process() if daemon: calculation = submit(process, **inputs) click.echo('Submitted {}<{}> to the daemon'.format( PwCalculation.__name__, calculation.pid)) else: click.echo('Running a PwCalculation in the {} mode... '.format(mode)) results, pk = run(process, _return_pid=True, **inputs) calculation = load_node(pk) click.echo('PwCalculation<{}> terminated with state: {}'.format( pk, calculation.get_state())) click.echo('\n{link:25s} {node}'.format(link='Output link', node='Node pk and type')) click.echo('{s}'.format(s='-' * 60)) for link, node in sorted(calculation.get_outputs(also_labels=True)): click.echo('{:25s} <{}> {}'.format(link, node.pk, node.__class__.__name__))
def get_pseudo(structure, pseudo_family): kind_pseudo_dict = get_pseudos_from_structure(structure, pseudo_family) pseudo_dict = {} pseudo_species = defaultdict(list) for kindname, pseudo in kind_pseudo_dict.iteritems(): pseudo_dict[pseudo.pk] = pseudo pseudo_species[pseudo.pk].append(kindname) pseudos = {} for pseudo_pk in pseudo_dict: pseudo = pseudo_dict[pseudo_pk] kinds = pseudo_species[pseudo_pk] for kind in kinds: pseudos[kind] = pseudo return pseudos
def use_pseudos_from_family(self, family_name): """ Set the pseudo to use for all atomic kinds, picking pseudos from the family with name family_name. :note: The structure must already be set. :param family_name: the name of the group containing the pseudos """ from collections import defaultdict try: structure = self._get_reference_structure() except AttributeError: raise ValueError( "Structure is not set yet! Therefore, the method " "use_pseudos_from_family cannot automatically set " "the pseudos") # A dict {kind_name: pseudo_object} kind_pseudo_dict = get_pseudos_from_structure(structure, family_name) # We have to group the species by pseudo, I use the pseudo PK # pseudo_dict will just map PK->pseudo_object pseudo_dict = {} # Will contain a list of all species of the pseudo with given PK pseudo_species = defaultdict(list) for kindname, pseudo in kind_pseudo_dict.iteritems(): pseudo_dict[pseudo.pk] = pseudo pseudo_species[pseudo.pk].append(kindname) for pseudo_pk in pseudo_dict: pseudo = pseudo_dict[pseudo_pk] kinds = pseudo_species[pseudo_pk] # I set the pseudo for all species, sorting alphabetically self.use_pseudo(pseudo, sorted(kinds))
inputs['settings'] = ParameterData(dict={'cmdline': ['-nk', str(num_pools)]}) if __name__ == "__main__": import argparse parser = argparse.ArgumentParser(description='NSCF calculation.') parser.add_argument('--code', type=str, dest='codename', required=True, help='The pw codename to use') parser.add_argument('--pseudo', type=str, dest='pseudo', required=True, help='The pseudo family to use') parser.add_argument('--parent', type=int, dest='parent', required=True, help='The parent to use') args = parser.parse_args() code = Code.get_from_string(args.codename) structure = load_node(args.parent).inp.structure inputs['structure'] = structure inputs['pseudo'] = get_pseudos_from_structure(structure, args.pseudo) inputs['code'] = code inputs['parent_folder'] = load_node(args.parent).out.remote_folder process = PwCalculation.process() running = submit(process, **inputs) print "Created calculation; with pid={}".format(running.pid)
def launch(code, structure, pseudo_family, kpoints, max_num_machines, max_wallclock_seconds, daemon, ecutwfc, ecutrho, hubbard_u, hubbard_v, hubbard_file_pk, starting_magnetization, smearing, mode): """ Run a PwCalculation for a given input structure """ from aiida.orm.data.parameter import ParameterData from aiida.orm.data.upf import get_pseudos_from_structure from aiida.orm.utils import CalculationFactory from aiida.work.launch import run_get_node, submit from aiida_quantumespresso.utils.resources import get_default_options PwCalculation = CalculationFactory('quantumespresso.pw') parameters = { 'CONTROL': { 'calculation': mode, }, 'SYSTEM': { 'ecutwfc': ecutwfc, 'ecutrho': ecutrho, }, } try: hubbard_file = validate.validate_hubbard_parameters( structure, parameters, hubbard_u, hubbard_v, hubbard_file_pk) except ValueError as exception: raise click.BadParameter(exception.message) try: validate.validate_starting_magnetization(structure, parameters, starting_magnetization) except ValueError as exception: raise click.BadParameter(exception.message) try: validate.validate_smearing(parameters, smearing) except ValueError as exception: raise click.BadParameter(exception.message) inputs = { 'code': code, 'structure': structure, 'pseudo': get_pseudos_from_structure(structure, pseudo_family), 'kpoints': kpoints, 'parameters': ParameterData(dict=parameters), 'settings': ParameterData(dict={}), 'options': get_default_options(max_num_machines, max_wallclock_seconds), } if hubbard_file: inputs['hubbard_file'] = hubbard_file if daemon: calculation = submit(PwCalculation, **inputs) click.echo('Submitted {}<{}> to the daemon'.format( PwCalculation.__name__, calculation.pk)) else: click.echo( 'Running a pw.x calculation in the {} mode... '.format(mode)) results, calculation = run_get_node(PwCalculation, **inputs) click.echo('PwCalculation<{}> terminated with state: {}'.format( calculation.pk, calculation.get_state())) click.echo('\n{link:25s} {node}'.format(link='Output link', node='Node pk and type')) click.echo('{s}'.format(s='-' * 60)) for link, node in sorted(calculation.get_outputs(also_labels=True)): click.echo('{:25s} <{}> {}'.format(link, node.pk, node.__class__.__name__))