Source code for ase2sprkkr_cli.commands.run

#!/usr/bin/env python
"""
This is a script to run calculation from commandline. Run it to see the doc.
"""

import argparse

from ase2sprkkr_cli.errors import CLIUsageError




help = "Run SPRKKR calculation."
description = "Run a calculation with optional NAME=VALUE input parameters."
unknowns = "options"  # Allow positional overrides after CLI flags across Python versions.


[docs] def parser(parser): bool_arg = getattr(argparse, "BooleanOptionalAction", "store_true") parser.add_argument("pot", help="SPR-KKR potential file or input file. The type is determined by its content.") grp = parser.add_mutually_exclusive_group() grp.add_argument( "-t", "--task", dest="task", type=str, help="Task to compute (see 'ase2sprkkr info --task').", ) grp.add_argument( "--input-file", "-i", help="Run ase2sprkkr -i inputfile potfile, if you want to run task determined by the give input file with a given potential.", type=str, ) parser.add_argument("--print-output", "-O", help="Print the output of SPRKKR.", default="None", action=bool_arg) parser.add_argument("--output-file", "-o", help="Output file", type=str) parser.add_argument( "--empty-spheres", "-e", help="Try to add empty spheres (by default, they are added if the potential is not converged and no vacuum atom is present.", action=bool_arg, ) parser.add_argument( "--profile", metavar="NAME", help="Use the named SPR-KKR execution profile (default: configured default).", ) parser.add_argument( "--project-id", metavar="UUID", help="Create a persistent run associated with this existing project ID.", ) parser.add_argument( "--submit-only", action="store_true", help="Submit a project run without waiting; requires a managed backend.", ) parser.add_argument( "--directory", metavar="PATH", help="Calculation working directory (default: project directory or current directory).", ) parser.add_argument( "options", nargs="*", help="Input parameters in the form <name>=<value>.", type=str )
[docs] def _project_manager(identifier): """Resolve an existing project UUID through ASE2SPRKKR's storage policy.""" from uuid import UUID from ase2sprkkr.sprkkr.projects import project_execution_manager try: project_id = UUID(identifier).hex except (ValueError, TypeError, AttributeError): raise CLIUsageError(f"Invalid project ID {identifier!r}; expected a UUID") from None try: manager = project_execution_manager(project=project_id) project = manager.projects.project(manager.project_id) except (KeyError, FileNotFoundError, ValueError): raise CLIUsageError(f"Project ID {identifier!r} does not exist") from None return manager, project
[docs] def run(args, global_args): if args.submit_only and not args.project_id: raise CLIUsageError("--submit-only requires --project-id") from ase2sprkkr import InputParameters from ase2sprkkr.common.module_registry import RegistryEntryNotFound from ase2sprkkr.common.tools import parse_named_option import pyparsing if args.task is not None: try: args.task = InputParameters.task_registry[args.task].name.lower() except RegistryEntryNotFound: raise CLIUsageError(f"Unknown task {args.task!r}") from None try: options = dict(parse_named_option(option) for option in args.options) except (ValueError, pyparsing.ParseBaseException) as exc: raise CLIUsageError(f"Invalid input parameter: {exc}") from None from ase2sprkkr import SPRKKR, Potential # NOQA import sys calc = SPRKKR(profile=args.profile) if args.profile is not None else SPRKKR() ip = None try: potential = Potential.from_file(args.pot) except pyparsing.ParseException: try: calc.input_parameters = ip = InputParameters.from_file(args.pot) potential = ip.CONTROL.POTFIL() except pyparsing.ParseException: print(f"File {args.pot} is neither potential nor input_parameters file") sys.exit(-1) if args.input_file: calc.input_parameters = args.input_file elif args.task: calc.input_parameters = args.task kwargs = {} for i in "empty_spheres", "print_output", "output_file": o = getattr(args, i) if o is not None: kwargs[i] = o manager = None if args.project_id is not None: manager, project = _project_manager(args.project_id) kwargs["project"] = manager else: project = None if args.directory is not None: kwargs["directory"] = args.directory elif project is not None and project.directory is not None: kwargs["directory"] = project.directory if manager is None: return calc.calculate( potential=potential, input_parameters=ip, options=options, **kwargs ) runner = calc.create_run( potential=potential, input_parameters=ip, options=options, **kwargs ) if args.submit_only and runner.client.backend.execution_mode != "managed": runner.close() raise CLIUsageError( "--submit-only requires a managed backend (kkrunner or psij); " "select one using --profile" ) runner.submit() print(f"Run ID: {runner.id}", flush=True) if args.submit_only: return result = runner.wait() from ase.calculators.calculator import CalculationFailed from kkrunner import RunState if runner.state != RunState.FINISHED: raise CalculationFailed( f"SPR-KKR calculation {runner.state.value} " f"(return code {runner.return_code}): {runner.failure_message}" ) return result