def __init__(self, qparams=None, setup=None, modules=None, shell_env=None, omp_env=None, pre_run=None, post_run=None, mpi_runner=None): """ Args: setup: String or list of commands to execute during the initial setup. modules: String or list of modules to load before running the application. shell_env: Dictionary with the environment variables to export before running the application. omp_env: Dictionary with the OpenMP variables. pre_run: String or list of commands to execute before launching the calculation. post_run: String or list of commands to execute once the calculation is completed. mpi_runner: Path to the MPI runner or `MpiRunner` instance. None if not used """ # Make defensive copies so that we can change the values at runtime. self.qparams = qparams.copy() if qparams is not None else {} if is_string(setup): setup = [setup] self.setup = setup[:] if setup is not None else [] self.omp_env = omp_env.copy() if omp_env is not None else {} if is_string(modules): modules = [modules] self.modules = modules[:] if modules is not None else [] self.shell_env = shell_env.copy() if shell_env is not None else {} self.mpi_runner = mpi_runner if not isinstance(mpi_runner, MpiRunner): self.mpi_runner = MpiRunner(mpi_runner) if is_string(pre_run): pre_run = [pre_run] self.pre_run = pre_run[:] if pre_run is not None else [] if is_string(post_run): post_run = [post_run] self.post_run = post_run[:] if post_run is not None else [] # Parse the template so that we know the list of supported options. cls = self.__class__ if hasattr(cls, "QTEMPLATE"): # Consistency check. err_msg = "" for param in self.qparams: if param not in self.supported_qparams: err_msg += "Unsupported QUEUE parameter name %s\n" % param if err_msg: raise ValueError(err_msg)
def __init__(self, pseudos): """ Args: pseudos: List of pseudopotentials or filepaths """ # Store pseudos in a default dictionary with z as key. # Note that we can have more than one pseudo for given z. # hence the values are lists of pseudos. if not isinstance(pseudos, collections.Iterable): pseudos = [pseudos] if is_string(pseudos[0]): pseudos = list_strings(pseudos) self._pseudos_with_z = collections.defaultdict(list) for pseudo in pseudos: p = pseudo if not isinstance(pseudo, Pseudo): p = Pseudo.from_file(pseudo) self._pseudos_with_z[p.Z].append(p) for z in self.zlist: pseudo_list = self._pseudos_with_z[z] symbols = [p.symbol for p in pseudo_list] symbol = symbols[0] if any(symb != symbol for symb in symbols): raise ValueError( "All symbols must be equal while they are: %s" % str(symbols)) setattr(self, symbol, pseudo_list)
def __init__(self, pseudos): """ Args: pseudos: List of pseudopotentials or filepaths """ # Store pseudos in a default dictionary with z as key. # Note that we can have more than one pseudo for given z. # hence the values are lists of pseudos. if not isinstance(pseudos, collections.Iterable): pseudos = [pseudos] if is_string(pseudos[0]): pseudos = list_strings(pseudos) self._pseudos_with_z = collections.defaultdict(list) for pseudo in pseudos: p = pseudo if not isinstance(pseudo, Pseudo): p = Pseudo.from_file(pseudo) self._pseudos_with_z[p.Z].append(p) for z in self.zlist: pseudo_list = self._pseudos_with_z[z] symbols = [p.symbol for p in pseudo_list] symbol = symbols[0] if any(symb != symbol for symb in symbols): raise ValueError("All symbols must be equal while they are: %s" % str(symbols)) setattr(self, symbol, pseudo_list)
def asabistructure(obj): """ Convert obj into an AbiStructure object. Accepts: - AbiStructure instance - Subinstances of pymatgen. - File paths """ if isinstance(obj, AbiStructure): return obj if isinstance(obj, Structure): # Promote return AbiStructure(obj) if is_string(obj): # Handle file paths. if os.path.isfile(obj): if obj.endswith(".nc"): structure = structure_from_etsf_file(obj) #print(structure._sites) else: structure = read_structure(obj) # Promote return AbiStructure(structure) raise ValueError("Don't know how to convert object %s to an AbiStructure structure" % str(obj))
def get_values(self, keys): """Return a list of values associated to a particular list of keys""" if is_string(keys): return [s.__dict__[keys] for s in self.sections] else: values = [] for k in keys: values.append([s.__dict__[k] for s in self.sections]) return values
def _dict_from_lines(lines, key_nums, sep=None): """ Helper function to parse formatted text structured like: value1 value2 ... sep key1, key2 ... key_nums is a list giving the number of keys for each line. 0 if line should be skipped. sep is a string denoting the character that separates the keys from the value (None if no separator is present). Returns: dict{key1 : value1, key2 : value2, ...} Raises: ValueError if parsing fails. """ if is_string(lines): lines = [lines] if not isinstance(key_nums, collections.Iterable): key_nums = list(key_nums) if len(lines) != len(key_nums): err_msg = "lines = %s\n key_num = %s" % (str(lines), str(key_nums)) raise ValueError(err_msg) kwargs = FrozenDict() for (i, nk) in enumerate(key_nums): if nk == 0: continue line = lines[i] tokens = [t.strip() for t in line.split()] values, keys = tokens[:nk], "".join(tokens[nk:]) # Sanitize keys: In some case we might string in for foo[,bar] keys.replace("[", "").replace("]", "") keys = keys.split(",") if sep is not None: check = keys[0][0] if check != sep: raise ValueError("Expecting separator %s, got %s" % (sep, check)) keys[0] = keys[0][1:] if len(values) != len(keys): msg = "line: %s\n len(keys) != len(value)\nkeys: %s\n values: %s" % ( line, keys, values) warnings.warn(msg) #raise ValueError(msg) kwargs.update(zip(keys, values)) return kwargs
def any2sch(obj): """Convert string or int to Schoenflies symbol. Returns None if invalid input""" if is_string(obj): if obj in sch_symbols: return obj else: # Try Hermann-Mauguin return herm2sch(obj) else: # Spacegroup ID? return spgid2sch(obj)
def _dict_from_lines(lines, key_nums, sep=None): """ Helper function to parse formatted text structured like: value1 value2 ... sep key1, key2 ... key_nums is a list giving the number of keys for each line. 0 if line should be skipped. sep is a string denoting the character that separates the keys from the value (None if no separator is present). Returns: dict{key1 : value1, key2 : value2, ...} Raises: ValueError if parsing fails. """ if is_string(lines): lines = [lines] if not isinstance(key_nums, collections.Iterable): key_nums = list(key_nums) if len(lines) != len(key_nums): err_msg = "lines = %s\n key_num = %s" % (str(lines), str(key_nums)) raise ValueError(err_msg) kwargs = FrozenDict() for (i, nk) in enumerate(key_nums): if nk == 0: continue line = lines[i] tokens = [t.strip() for t in line.split()] values, keys = tokens[:nk], "".join(tokens[nk:]) # Sanitize keys: In some case we might string in for foo[,bar] keys.replace("[", "").replace("]", "") keys = keys.split(",") if sep is not None: check = keys[0][0] if check != sep: raise ValueError("Expecting separator %s, got %s" % (sep, check)) keys[0] = keys[0][1:] if len(values) != len(keys): msg = "line: %s\n len(keys) != len(value)\nkeys: %s\n values: %s" % (line, keys, values) warnings.warn(msg) #raise ValueError(msg) kwargs.update(zip(keys, values)) return kwargs
def to_csv(self, fileobj=sys.stdout): """Write data on file fileobj using CSV format.""" openclose = is_string(fileobj) if openclose: fileobj = open(fileobj, "w") for (idx, section) in enumerate(self.sections): fileobj.write(section.to_csvline(with_header=(idx == 0))) fileobj.flush() if openclose: fileobj.close()
def sendmail(subject, text, mailto, sender=None): """ Sends an e-mail either with unix sendmail. Args: subject: String with the subject of the mail. text: String with the body of the mail. mailto: String or list of string with the recipients. sender: string with the sender address. If sender is None, username@hostname is used. Returns: exit status """ def user_at_host(): from socket import gethostname return os.getlogin() + "@" + gethostname() # Body of the message. sender = user_at_host() if sender is None else sender if is_string(mailto): mailto = [mailto] from email.mime.text import MIMEText mail = MIMEText(text) mail["Subject"] = subject mail["From"] = sender mail["To"] = ", ".join(mailto) msg = mail.as_string() # sendmail works much better than the python interface. # Note that sendmail is available only on Unix-like OS. from subprocess import Popen, PIPE sendmail = which("sendmail") if sendmail is None: return -1 p = Popen([sendmail, "-t"], stdin=PIPE, stderr=PIPE) outdata, errdata = p.communicate(msg) return len(errdata)
def _format_kv(key, value): """Formatter""" if value is None: return [] # Use ABINIT default. if isinstance(value, collections.Iterable) and not is_string(value): arr = np.array(value) if len(arr.shape) in [0,1]: # scalar or vector. token = [key, " ".join(str(i) for i in arr)] else: # array --> matrix matrix = np.reshape(arr, (-1, arr.shape[-1])) lines = [] for (idx, row) in enumerate(matrix): lines.append(" ".join(str(i) for i in row)) token = [key +"\n", "\n".join(lines)] else: token = [key, str(value)] return token
def _iflat_tasks_wti(self, status=None, op="=", with_wti=True): """ Generators that produces a flat sequence of task. if status is not None, only the tasks with the specified status are selected. Returns: (task, work_index, task_index) if with_wti is True else task """ if status is None: for wi, work in enumerate(self): for ti, task in enumerate(work): if with_wti: yield task, wi, ti else: yield task else: # Get the operator from the string. import operator op = { "=": operator.eq, "!=": operator.ne, ">": operator.gt, ">=": operator.ge, "<": operator.lt, "<=": operator.le, }[op] # Accept Task.S_FLAG or string. if is_string(status): status = getattr(Task, status) for wi, work in enumerate(self): for ti, task in enumerate(work): if op(task.status, status): if with_wti: yield task, wi, ti else: yield task
def _iflat_tasks_wti(self, status=None, op="==", with_wti=True): """ Generators that produces a flat sequence of task. if status is not None, only the tasks with the specified status are selected. Returns: (task, work_index, task_index) if with_wti is True else task """ if status is None: for wi, work in enumerate(self): for ti, task in enumerate(work): if with_wti: yield task, wi, ti else: yield task else: # Get the operator from the string. import operator op = { "==": operator.eq, "!=": operator.ne, ">": operator.gt, ">=": operator.ge, "<": operator.lt, "<=": operator.le, }[op] # Accept Task.S_FLAG or string. if is_string(status): status = getattr(Task, status) for wi, work in enumerate(self): for ti, task in enumerate(work): if op(task.status, status): if with_wti: yield task, wi, ti else: yield task
def _format_kv(key, value): """Formatter""" if value is None: return [] # Use ABINIT default. if isinstance(value, collections.Iterable) and not is_string(value): arr = np.array(value) if len(arr.shape) in [0, 1]: # scalar or vector. token = [key, " ".join(str(i) for i in arr)] else: # array --> matrix matrix = np.reshape(arr, (-1, arr.shape[-1])) lines = [] for (idx, row) in enumerate(matrix): lines.append(" ".join(str(i) for i in row)) token = [key + "\n", "\n".join(lines)] else: token = [key, str(value)] return token
def __init__(self, command): if is_string(command): import shlex command = shlex.split(command) self.command = command
def _add(self, text, pre=""): if is_string(text): self._lines.append(pre + text) else: self._lines.extend([pre + t for t in text])
def _add(self, text, pre=""): if is_string(text): self._lines.append(pre+text) else: self._lines.extend([pre + t for t in text])