|
| 1 | +import os |
| 2 | +import shutil |
| 3 | +import time |
| 4 | +from multiprocessing import Pool |
| 5 | + |
| 6 | +import calculate_combination |
| 7 | +import h5py |
| 8 | +import numpy as np |
| 9 | +from ansys.aedt.core import Desktop, Maxwell2d |
| 10 | + |
| 11 | +from machine_design.config import load_config |
| 12 | + |
| 13 | +# ========================================================= |
| 14 | +# CONFIGURATION |
| 15 | +# ========================================================= |
| 16 | +config = load_config() |
| 17 | +AEDT_VERSION = config["aedt_version"] |
| 18 | + |
| 19 | +PROJECT_DIR = os.path.join(os.getcwd(), config["project_dir"]) |
| 20 | +BASE_PROJECT = "SynRM_orig" |
| 21 | +DESIGN_NAME = "Design02_def_Idq" |
| 22 | +SETUP_NAME = "Setup1" |
| 23 | + |
| 24 | +N_WORKERS = config["n_workers"] # less then number of logical processors |
| 25 | + |
| 26 | + |
| 27 | +# ========================================================= |
| 28 | +# PROJECT COPY |
| 29 | +# ========================================================= |
| 30 | +def copy_project(worker_id): |
| 31 | + src = os.path.join(PROJECT_DIR, BASE_PROJECT + ".aedt") |
| 32 | + dst_name = f"SynRM_worker_{worker_id}.aedt" |
| 33 | + dst = os.path.join(PROJECT_DIR, dst_name) |
| 34 | + |
| 35 | + shutil.copyfile(src, dst) |
| 36 | + |
| 37 | + return dst_name |
| 38 | + |
| 39 | + |
| 40 | +# ========================================================= |
| 41 | +# WORKER |
| 42 | +# ========================================================= |
| 43 | +def worker(args): |
| 44 | + |
| 45 | + worker_id, task_chunk = args |
| 46 | + |
| 47 | + project_name = copy_project(worker_id) |
| 48 | + |
| 49 | + desktop = Desktop(specified_version=AEDT_VERSION, non_graphical=True, new_desktop=True, close_on_exit=False) |
| 50 | + |
| 51 | + app = Maxwell2d(project=project_name, design=DESIGN_NAME) |
| 52 | + |
| 53 | + results = calculate_combination.main(app, task_chunk, SETUP_NAME) |
| 54 | + |
| 55 | + desktop.release_desktop(close_projects=True, close_on_exit=False) |
| 56 | + |
| 57 | + return results |
| 58 | + |
| 59 | + |
| 60 | +# ========================================================= |
| 61 | +# MAIN |
| 62 | +# ========================================================= |
| 63 | +def run(): |
| 64 | + |
| 65 | + Id_vec = np.arange(0.0, 3.2, 0.2) # 16 values |
| 66 | + Iq_vec = np.arange(0.0, 3.2, 0.2) # 16 values |
| 67 | + |
| 68 | + tasks = [] |
| 69 | + for i, Id in enumerate(Id_vec): |
| 70 | + for j, Iq in enumerate(Iq_vec): |
| 71 | + tasks.append((Id, Iq, i, j)) |
| 72 | + |
| 73 | + chunk_size = int(np.ceil(len(tasks) / N_WORKERS)) |
| 74 | + chunks = [tasks[i : i + chunk_size] for i in range(0, len(tasks), chunk_size)] |
| 75 | + |
| 76 | + args = [(i, chunk) for i, chunk in enumerate(chunks)] |
| 77 | + |
| 78 | + results = np.zeros((len(Id_vec), len(Iq_vec), 11)) |
| 79 | + |
| 80 | + with Pool(N_WORKERS) as pool: |
| 81 | + outputs = pool.map(worker, args) |
| 82 | + |
| 83 | + rows = [] |
| 84 | + for chunk in outputs: |
| 85 | + for i, j, res in chunk: |
| 86 | + results[i, j, :] = res |
| 87 | + rows.append(list(results[i, j, :])) |
| 88 | + data_out = np.array(rows) |
| 89 | + |
| 90 | + header = "Id,Iq,Flux_d,Flux_q,Flux_e_d,Flux_e_q,L_d,L_q,L_dq,L_qd,Torque" |
| 91 | + np.savetxt("results_grid.csv", data_out, delimiter=",", header=header, comments="") |
| 92 | + |
| 93 | + with h5py.File("results_grid.h5", "w") as f: |
| 94 | + f.create_dataset("Id", data=Id_vec) |
| 95 | + f.create_dataset("Iq", data=Iq_vec) |
| 96 | + f.create_dataset("results", data=results) |
| 97 | + |
| 98 | + print("[INFO] DONE") |
| 99 | + |
| 100 | + return results |
| 101 | + |
| 102 | + |
| 103 | +# ========================================================= |
| 104 | +# ENTRY POINT |
| 105 | +# ========================================================= |
| 106 | +if __name__ == "__main__": |
| 107 | + start = time.perf_counter() |
| 108 | + |
| 109 | + run() |
| 110 | + |
| 111 | + end = time.perf_counter() |
| 112 | + |
| 113 | + print(f"Time of calculation: {end - start:.6f} s") |
0 commit comments