{ "cells": [ { "cell_type": "markdown", "id": "0217ffc4", "metadata": {}, "source": [ "# IPM geometry optimization\n", "\n", "This example shows how to use PyAEDT to find the best machine 2D geometry\n", "to achieve high torque and low losses.\n", "The example shows how to setup an optimetrics analysis to sweep geometries\n", "for a single value of stator current angle.\n", "The torque and losses results are then exported in a .csv file.\n", "\n", "Keywords: **Maxwell 2D**, **transient**, **motor**, **optimization**." ] }, { "cell_type": "markdown", "id": "b0e45e96", "metadata": {}, "source": [ "## Perform imports and define constants\n", "\n", "Perform required imports." ] }, { "cell_type": "code", "execution_count": null, "id": "9754da99", "metadata": {}, "outputs": [], "source": [ "import csv\n", "import os\n", "import tempfile\n", "import time" ] }, { "cell_type": "code", "execution_count": null, "id": "781cb66a", "metadata": {}, "outputs": [], "source": [ "import ansys.aedt.core" ] }, { "cell_type": "markdown", "id": "37f5a9c2", "metadata": {}, "source": [ "Define constants." ] }, { "cell_type": "code", "execution_count": null, "id": "9f709db5", "metadata": {}, "outputs": [], "source": [ "AEDT_VERSION = \"2024.2\"\n", "NUM_CORES = 4\n", "NG_MODE = False # Open AEDT UI when it is launched." ] }, { "cell_type": "markdown", "id": "c92230fc", "metadata": {}, "source": [ "## Create temporary directory and download files\n", "\n", "Create a temporary directory where downloaded data or\n", "dumped data can be stored.\n", "If you'd like to retrieve the project data for subsequent use,\n", "the temporary folder name is given by ``temp_folder.name``." ] }, { "cell_type": "code", "execution_count": null, "id": "14742e7d", "metadata": {}, "outputs": [], "source": [ "temp_folder = tempfile.TemporaryDirectory(suffix=\".ansys\")" ] }, { "cell_type": "markdown", "id": "7c1e0016", "metadata": {}, "source": [ "## Download AEDT file example\n", "\n", "Set the local temporary folder to export the AEDT file to." ] }, { "cell_type": "code", "execution_count": null, "id": "1dc52f3d", "metadata": {}, "outputs": [], "source": [ "aedt_file = ansys.aedt.core.downloads.download_file(\n", " source=\"maxwell_motor_optimization\",\n", " name=\"IPM_optimization.aedt\",\n", " destination=temp_folder.name,\n", ")" ] }, { "cell_type": "markdown", "id": "667e4a0b", "metadata": {}, "source": [ "## Launch Maxwell 2D\n", "\n", "Launch AEDT and Maxwell 2D after first setting up the project, the version and the graphical mode." ] }, { "cell_type": "code", "execution_count": null, "id": "1e3e5016", "metadata": {}, "outputs": [], "source": [ "m2d = ansys.aedt.core.Maxwell2d(\n", " project=aedt_file,\n", " version=AEDT_VERSION,\n", " new_desktop=True,\n", " non_graphical=NG_MODE,\n", ")" ] }, { "cell_type": "markdown", "id": "68d4eff6", "metadata": {}, "source": [ "## Add parametric setup\n", "\n", "Add a parametric setup made up of geometry variable sweep definitions and single value for the stator current angle.\n", "Note: Step variations have been minimized to reduce the analysis time. If needed they can be increased by changing\n", "the ``step`` argument." ] }, { "cell_type": "code", "execution_count": null, "id": "31507894", "metadata": {}, "outputs": [], "source": [ "param_sweep = m2d.parametrics.add(\n", " variable=\"bridge\",\n", " start_point=\"0.5mm\",\n", " end_point=\"1mm\",\n", " step=\"0.5mm\",\n", " variation_type=\"LinearStep\",\n", ")\n", "param_sweep.add_variation(\n", " sweep_variable=\"din\",\n", " start_point=70,\n", " end_point=80,\n", " step=10,\n", " units=\"mm\",\n", " variation_type=\"LinearStep\",\n", ")\n", "param_sweep.add_variation(\n", " sweep_variable=\"phase_advance\",\n", " start_point=0,\n", " end_point=45,\n", " step=45,\n", " units=\"deg\",\n", " variation_type=\"LinearStep\",\n", ")\n", "param_sweep.add_variation(\n", " sweep_variable=\"Ipeak\", start_point=200, units=\"A\", variation_type=\"SingleValue\"\n", ")" ] }, { "cell_type": "markdown", "id": "c0fab760", "metadata": {}, "source": [ "## Analyze parametric sweep" ] }, { "cell_type": "code", "execution_count": null, "id": "0da8054e", "metadata": {}, "outputs": [], "source": [ "param_sweep.analyze(cores=NUM_CORES)" ] }, { "cell_type": "markdown", "id": "275311fd", "metadata": {}, "source": [ "## Post-processing\n", "\n", "Create reports to get torque and loss results for all variations." ] }, { "cell_type": "code", "execution_count": null, "id": "670f251f", "metadata": {}, "outputs": [], "source": [ "report_torque = m2d.post.create_report(\n", " expressions=\"Moving1.Torque\",\n", " domain=\"Sweep\",\n", " variations={\"bridge\": \"All\", \"din\": \"All\", \"Ipeak\": \"All\"},\n", " primary_sweep_variable=\"Time\",\n", " plot_type=\"Rectangular Plot\",\n", " plot_name=\"TorqueAllVariations\",\n", ")" ] }, { "cell_type": "code", "execution_count": null, "id": "5ee0d73f", "metadata": {}, "outputs": [], "source": [ "report_solid_loss = m2d.post.create_report(\n", " expressions=\"SolidLoss\",\n", " domain=\"Sweep\",\n", " variations={\"bridge\": \"All\", \"din\": \"All\", \"Ipeak\": \"All\"},\n", " primary_sweep_variable=\"Time\",\n", " plot_type=\"Rectangular Plot\",\n", " plot_name=\"SolidLossAllVariations\",\n", ")" ] }, { "cell_type": "code", "execution_count": null, "id": "f819b3aa", "metadata": {}, "outputs": [], "source": [ "report_core_loss = m2d.post.create_report(\n", " expressions=\"CoreLoss\",\n", " domain=\"Sweep\",\n", " variations={\"bridge\": \"All\", \"din\": \"All\", \"Ipeak\": \"All\"},\n", " primary_sweep_variable=\"Time\",\n", " plot_type=\"Rectangular Plot\",\n", " plot_name=\"CoreLossAllVariations\",\n", ")" ] }, { "cell_type": "markdown", "id": "79cb20a1", "metadata": {}, "source": [ "Get torque and loss solution data for all available variations." ] }, { "cell_type": "code", "execution_count": null, "id": "bece65c1", "metadata": {}, "outputs": [], "source": [ "torque_data = m2d.post.get_solution_data(\n", " expressions=[\"Moving1.Torque\"],\n", " setup_sweep_name=m2d.nominal_sweep,\n", " domain=\"Sweep\",\n", " variations={\"bridge\": \"All\", \"din\": \"All\", \"Ipeak\": \"All\"},\n", " primary_sweep_variable=\"Time\",\n", " report_category=\"Standard\",\n", ")" ] }, { "cell_type": "code", "execution_count": null, "id": "5ea709c4", "metadata": {}, "outputs": [], "source": [ "solid_loss_data = m2d.post.get_solution_data(\n", " expressions=[\"CoreLoss\"],\n", " setup_sweep_name=m2d.nominal_sweep,\n", " domain=\"Sweep\",\n", " variations={\"bridge\": \"All\", \"din\": \"All\", \"Ipeak\": \"All\"},\n", " primary_sweep_variable=\"Time\",\n", " report_category=\"Standard\",\n", ")" ] }, { "cell_type": "code", "execution_count": null, "id": "dd9f1eb2", "metadata": {}, "outputs": [], "source": [ "core_loss_data = m2d.post.get_solution_data(\n", " expressions=[\"SolidLoss\"],\n", " setup_sweep_name=m2d.nominal_sweep,\n", " domain=\"Sweep\",\n", " variations={\"bridge\": \"All\", \"din\": \"All\", \"Ipeak\": \"All\"},\n", " primary_sweep_variable=\"Time\",\n", " report_category=\"Standard\",\n", ")" ] }, { "cell_type": "markdown", "id": "3b0aa91f", "metadata": {}, "source": [ "Calculate torque and loss average values for each variation and write data in a .csv file." ] }, { "cell_type": "code", "execution_count": null, "id": "633af016", "metadata": {}, "outputs": [], "source": [ "csv_data = []\n", "for var in core_loss_data.variations:\n", " torque_data.active_variation = var\n", " core_loss_data.active_variation = var\n", " solid_loss_data.active_variation = var\n", "\n", " torque_values = torque_data.data_magnitude()\n", " core_loss_values = core_loss_data.data_magnitude()\n", " solid_loss_values = solid_loss_data.data_magnitude()\n", "\n", " torque_data_average = sum(torque_values) / len(torque_values)\n", " core_loss_average = sum(core_loss_values) / len(core_loss_values)\n", " solid_loss_average = sum(solid_loss_values) / len(solid_loss_values)\n", "\n", " csv_data.append(\n", " {\n", " \"active_variation\": str(torque_data.active_variation),\n", " \"average_torque\": str(torque_data_average),\n", " \"average_core_loss\": str(core_loss_average),\n", " \"average_solid_loss\": str(solid_loss_average),\n", " }\n", " )\n", "\n", " with open(\n", " os.path.join(temp_folder.name, \"motor_optimization.csv\"), \"w\", newline=\"\"\n", " ) as csvfile:\n", " fields = [\n", " \"active_variation\",\n", " \"average_torque\",\n", " \"average_core_loss\",\n", " \"average_solid_loss\",\n", " ]\n", " writer = csv.DictWriter(csvfile, fieldnames=fields)\n", " writer.writeheader()\n", " writer.writerows(csv_data)" ] }, { "cell_type": "markdown", "id": "667970d8", "metadata": {}, "source": [ "## Release AEDT" ] }, { "cell_type": "code", "execution_count": null, "id": "87eed072", "metadata": {}, "outputs": [], "source": [ "m2d.save_project()\n", "m2d.release_desktop()\n", "# Wait 3 seconds to allow AEDT to shut down before cleaning the temporary directory.\n", "time.sleep(3)" ] }, { "cell_type": "markdown", "id": "55a51790", "metadata": {}, "source": [ "## Clean up\n", "\n", "All project files are saved in the folder ``temp_folder.name``.\n", "If you've run this example as a Jupyter notebook, you\n", "can retrieve those project files. The following cell\n", "removes all temporary files, including the project folder." ] }, { "cell_type": "code", "execution_count": null, "id": "fcfe3c39", "metadata": {}, "outputs": [], "source": [ "temp_folder.cleanup()" ] } ], "metadata": { "jupytext": { "cell_metadata_filter": "-all", "main_language": "python", "notebook_metadata_filter": "-all" } }, "nbformat": 4, "nbformat_minor": 5 }