Skip to content

pyxems.run

check_config()

Check if the OpenEMS executable is available.

Returns:

Name Type Description
bool bool

True if the OpenEMS executable is found, False otherwise.

Source code in src/pyxems/run.py
47
48
49
50
51
52
53
54
55
def check_config() -> bool:
    """
    Check if the OpenEMS executable is available.

    Returns:
        bool: True if the OpenEMS executable is found, False otherwise.
    """
    openems_executable = find_openems_executable()
    return openems_executable is not None

find_openems_executable()

Find the OpenEMS executable by checking the system PATH and the OPENEMS_PATH environment variable. Returns: Optional[Path]: The path to the OpenEMS executable if found, otherwise None.

Source code in src/pyxems/run.py
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
def find_openems_executable() -> Optional[Path]:
    """
    Find the OpenEMS executable by checking the system PATH and the OPENEMS_PATH environment variable.
    Returns:
        Optional[Path]: The path to the OpenEMS executable if found, otherwise None.
    """
    if which("openEMS") is not None:
        logging.info(f"Found OpenEMS executable in PATH: {which('openEMS')}")
        return Path(which("openEMS"))  # type: ignore
    load_dotenv()
    if "OPENEMS_PATH" not in os.environ:
        logging.info(
            "OPENEMS_PATH environment variable not set. Please set it to the directory containing the OpenEMS executable."
        )
        return None
    openems_path = Path(os.environ["OPENEMS_PATH"]) / (
        "openEMS" if os.name != "nt" else "openEMS.exe"
    )
    if not openems_path.is_file():
        logging.info(
            f"OpenEMS executable not found at {openems_path}. Please ensure OPENEMS_PATH is set correctly."
        )
        return None
    return openems_path

simulate(config_path, run_dir)

Run an OpenEMS simulation using the specified configuration file and optional run directory.

Source code in src/pyxems/run.py
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
@app.command()
def simulate(config_path: Path, run_dir: Optional[Path]) -> CompletedProcess:
    """
    Run an OpenEMS simulation using the specified configuration file and optional run directory.
    """
    openems_path = find_openems_executable()
    if openems_path is None:
        raise FileNotFoundError(
            "OpenEMS executable not found. Please ensure it is installed and in your PATH, or set the OPENEMS_PATH environment variable."
        )
    config_path = config_path.resolve()
    if run_dir is None:
        run_dir = config_path.parent
    os.chdir(run_dir)
    cmd = [
        str(openems_path),
        config_path,
    ]
    proc = run(cmd, capture_output=True, text=True)
    return proc