|
| 1 | +#!/usr/bin/env python |
| 2 | +"""Unified DeePKS command-line interface.""" |
| 3 | + |
| 4 | +import os |
| 5 | +import sys |
| 6 | + |
| 7 | + |
| 8 | +def _get_model_backend(): |
| 9 | + """Get model backend instance.""" |
| 10 | + from deepks.ml.backend import CorrNetModelBackend |
| 11 | + return CorrNetModelBackend() |
| 12 | + |
| 13 | + |
| 14 | +def _get_physics_backend(scf_soft='pyscf'): |
| 15 | + """Get physics backend based on scf_soft parameter. |
| 16 | +
|
| 17 | + Args: |
| 18 | + scf_soft: SCF software name ('pyscf' or 'abacus') |
| 19 | +
|
| 20 | + Returns: |
| 21 | + PhysicsBackend instance |
| 22 | + """ |
| 23 | + from deepks.physics.backends import get_scf_backend |
| 24 | + return get_scf_backend(scf_soft) |
| 25 | + |
| 26 | + |
| 27 | +def main(): |
| 28 | + """Main entry point for DeePKS CLI.""" |
| 29 | + import argparse |
| 30 | + # Force line-buffered stdout/stderr so output appears in log.iter in real time |
| 31 | + # even when the process is launched with >> redirection (non-TTY). |
| 32 | + sys.stdout = open(sys.stdout.fileno(), mode='w', buffering=1, closefd=False) |
| 33 | + sys.stderr = open(sys.stderr.fileno(), mode='w', buffering=1, closefd=False) |
| 34 | + |
| 35 | + parser = argparse.ArgumentParser( |
| 36 | + prog="deepks", |
| 37 | + description="DeePKS: Deep Kohn-Sham DFT with machine learning" |
| 38 | + ) |
| 39 | + parser.add_argument( |
| 40 | + "config", |
| 41 | + nargs="?", |
| 42 | + default="input.yaml", |
| 43 | + help="Configuration file (default: input.yaml)" |
| 44 | + ) |
| 45 | + parser.add_argument( |
| 46 | + "-v", "--version", |
| 47 | + action="version", |
| 48 | + version="DeePKS 1.0" |
| 49 | + ) |
| 50 | + |
| 51 | + args = parser.parse_args() |
| 52 | + |
| 53 | + # Check if config file exists |
| 54 | + if not os.path.exists(args.config): |
| 55 | + print(f"Error: Configuration file '{args.config}' not found", file=sys.stderr) |
| 56 | + sys.exit(1) |
| 57 | + |
| 58 | + # Load and process configuration |
| 59 | + from deepks.io.input import load_config, get_default_config |
| 60 | + from deepks.io.input.merger import merge_configs, apply_parameter_inheritance |
| 61 | + from deepks.io.input.dispatcher import dispatch_command |
| 62 | + |
| 63 | + try: |
| 64 | + # Load configuration file |
| 65 | + config = load_config(args.config) |
| 66 | + |
| 67 | + # Determine type from config |
| 68 | + if 'type' not in config: |
| 69 | + print("Error: 'type' field is required in configuration file", file=sys.stderr) |
| 70 | + print("Valid types: train, test, scf, stats, iterate", file=sys.stderr) |
| 71 | + sys.exit(1) |
| 72 | + |
| 73 | + task_type = config['type'] |
| 74 | + |
| 75 | + # Get defaults based on type and backend |
| 76 | + scf_soft = config.get('scf_soft', 'pyscf') |
| 77 | + defaults = get_default_config(task_type, scf_soft) |
| 78 | + |
| 79 | + # Merge defaults with config |
| 80 | + config = merge_configs(defaults, config) |
| 81 | + |
| 82 | + # Apply parameter inheritance for iterate type |
| 83 | + if task_type == 'iterate': |
| 84 | + config = apply_parameter_inheritance(config) |
| 85 | + |
| 86 | + # Dispatch to appropriate handler |
| 87 | + dispatch_command(config) |
| 88 | + |
| 89 | + except Exception as e: |
| 90 | + print(f"Error: {e}", file=sys.stderr) |
| 91 | + import traceback |
| 92 | + traceback.print_exc() |
| 93 | + sys.exit(1) |
| 94 | + |
| 95 | + |
| 96 | +if __name__ == "__main__": |
| 97 | + main() |
0 commit comments