diff --git a/README.md b/README.md index 3f59998..146c10d 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,10 @@ The motivation is to improve the performance of SynRMs without using permanent m Run "run.py" for random design evaluation and "run_optimization.py" for Bayesian optimization. Ansys Electronics Desktop and PyAEDT are required. +## Configuration + +Machine-specific settings (Ansys version, number of cores, workers, project directory) are read with 'load_config()' from 'machine_design.config', which merges 'machine_design/config.default.json' with an optional local override at '~/.machine_design/config.json'. Put your own values in the local file instead of editing the defaults or a script directly, so they never end up in a commit. + ## Citation If you use this code, please cite our paper: diff --git a/machine_design/config.default.json b/machine_design/config.default.json new file mode 100644 index 0000000..438c68c --- /dev/null +++ b/machine_design/config.default.json @@ -0,0 +1,6 @@ +{ + "aedt_version": "2024.1", + "num_cores": 4, + "n_workers": 22, + "project_dir": "data" +} diff --git a/machine_design/config.py b/machine_design/config.py new file mode 100644 index 0000000..70f46d6 --- /dev/null +++ b/machine_design/config.py @@ -0,0 +1,17 @@ +import json +import os + +default_config_path = os.path.join(os.path.dirname(__file__), "config.default.json") +local_config_path = os.path.join(os.path.expanduser("~"), ".machine_design", "config.json") + + +def load_config(local_path=local_config_path): + + with open(default_config_path) as f: + config = json.load(f) + + if os.path.exists(local_path): + with open(local_path) as f: + config.update(json.load(f)) + + return config diff --git a/machine_design/parallel_calculation/runner_parallel.py b/machine_design/parallel_calculation/runner_parallel.py index 3273db3..5f7ef22 100644 --- a/machine_design/parallel_calculation/runner_parallel.py +++ b/machine_design/parallel_calculation/runner_parallel.py @@ -8,18 +8,20 @@ import numpy as np from ansys.aedt.core import Desktop, Maxwell2d +from machine_design.config import load_config + # ========================================================= # CONFIGURATION # ========================================================= -AEDT_VERSION = os.getenv("AEDT_VERSION", "2026.1") +config = load_config() +AEDT_VERSION = config["aedt_version"] -PATH = "d:\\DATA\\Test" -PROJECT_DIR = os.getenv("ANSYS_PROJECT_DIR", PATH) +PROJECT_DIR = os.path.join(os.getcwd(), config["project_dir"]) BASE_PROJECT = "SynRM_orig" DESIGN_NAME = "Design02_def_Idq" SETUP_NAME = "Setup1" -N_WORKERS = 22 # less then number of logical processors +N_WORKERS = config["n_workers"] # less then number of logical processors # ========================================================= diff --git a/notebooks/run.py b/notebooks/run.py index 34728fe..ecba004 100644 --- a/notebooks/run.py +++ b/notebooks/run.py @@ -5,6 +5,7 @@ import numpy as np import pandas as pd +from machine_design.config import load_config from machine_design.designs import load_design from machine_design.optimization import ( HacklGenerator_3BrokenLines, @@ -16,11 +17,12 @@ ) from motors.motor1 import Computation, Geometry -aedt_version = "2024.1" +config = load_config() +aedt_version = config["aedt_version"] +num_cores = config["num_cores"] n_designs = 50 r_stator_end = 0.7 offset = 0.7 / 2 -num_cores = 4 plot_design = True project_name = "SynRM_test" diff --git a/notebooks/run_optimization.py b/notebooks/run_optimization.py index b32a971..2df015e 100644 --- a/notebooks/run_optimization.py +++ b/notebooks/run_optimization.py @@ -13,6 +13,7 @@ from botorch.utils.transforms import normalize, unnormalize from gpytorch.mlls import ExactMarginalLogLikelihood +from machine_design.config import load_config from machine_design.designs import load_design from machine_design.optimization import ( HacklGenerator_3BrokenLines, @@ -27,11 +28,12 @@ torch.set_default_dtype(torch.float64) -aedt_version = "2024.1" +config = load_config() +aedt_version = config["aedt_version"] +num_cores = config["num_cores"] n_evals = 250 r_stator_end = 0.7 offset = 0.7 / 2 -num_cores = 4 batch_size = 4 max_candidate_tries = 10 objective_fallback = {"torque": 1.0, "ripple": 40.0} diff --git a/pyproject.toml b/pyproject.toml index d6e4e24..606c121 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -47,6 +47,9 @@ packages = [ "motors", ] +[tool.setuptools.package-data] +machine_design = ["config.default.json"] + [tool.pytest.ini_options] testpaths = ["tests"] markers = [ diff --git a/tests/test_ansys_regression.py b/tests/test_ansys_regression.py index 0e87a54..9fdbb0d 100644 --- a/tests/test_ansys_regression.py +++ b/tests/test_ansys_regression.py @@ -17,13 +17,14 @@ import numpy as np import pytest +from machine_design.config import load_config from machine_design.designs.design import Design as LiveDesign from machine_design.optimization.generators import HacklGenerator_OneLambda from motors.motor1 import Computation, Geometry pytestmark = pytest.mark.ansys -AEDT_VERSION = "2024.1" +AEDT_VERSION = load_config()["aedt_version"] R_STATOR_END = 0.7 OFFSET = 0.35 SEEDS = (42, 43) diff --git a/tests/test_config.py b/tests/test_config.py new file mode 100644 index 0000000..b8ff2ed --- /dev/null +++ b/tests/test_config.py @@ -0,0 +1,23 @@ +import json + +from machine_design.config import load_config + + +def test_load_config_defaults(tmp_path): + config = load_config(local_path=tmp_path / "missing.json") + assert config == { + "aedt_version": "2024.1", + "num_cores": 4, + "n_workers": 22, + "project_dir": "data", + } + + +def test_load_config_merges_local_override(tmp_path): + local_path = tmp_path / "config.json" + local_path.write_text(json.dumps({"aedt_version": "2025.1"})) + + config = load_config(local_path=local_path) + + assert config["aedt_version"] == "2025.1" + assert config["num_cores"] == 4