{ "cells": [ { "cell_type": "markdown", "id": "5488de5e", "metadata": {}, "source": [ "# Magnetomotive force along a line\n", "\n", "This example shows how to use PyAEDT to calculate\n", "the magnetomotive force along a line that changes position.\n", "It shows how to leverage the PyAEDT advanced fields calculator\n", "to insert a custom formula, which in this case is the integral\n", "of the H field along a line.\n", "The example shows two options to achieve the intent.\n", "The first one creates many lines as to simulate a contour that changes position.\n", "The integral of the H field is computed for each line.\n", "The second option creates one parametric polyline and then uses a parametric sweep to change its position.\n", "The integral of the H field is computed for each position." ] }, { "cell_type": "markdown", "id": "24ba17c2", "metadata": {}, "source": [ "Keywords: **Maxwell 2D**, **magnetomotive force**." ] }, { "cell_type": "markdown", "id": "6e78a219", "metadata": {}, "source": [ "## Perform imports and define constants\n", "\n", "Perform required imports." ] }, { "cell_type": "code", "execution_count": null, "id": "172f21e4", "metadata": {}, "outputs": [], "source": [ "import tempfile\n", "import time\n", "\n", "import ansys.aedt.core\n", "from ansys.aedt.core.examples.downloads import download_file" ] }, { "cell_type": "markdown", "id": "e9b7fe59", "metadata": {}, "source": [ "Define constants." ] }, { "cell_type": "code", "execution_count": null, "id": "175a2eaa", "metadata": {}, "outputs": [], "source": [ "AEDT_VERSION = \"2025.1\"\n", "NUM_CORES = 4\n", "NG_MODE = False # Open AEDT UI when it is launched." ] }, { "cell_type": "markdown", "id": "6d2ab1bc", "metadata": {}, "source": [ "## Create temporary directory\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": "ee0092ec", "metadata": {}, "outputs": [], "source": [ "temp_folder = tempfile.TemporaryDirectory(suffix=\".ansys\")" ] }, { "cell_type": "markdown", "id": "647b2ab7", "metadata": {}, "source": [ "## Import project\n", "\n", "Download the files required to run this example to the temporary working folder." ] }, { "cell_type": "code", "execution_count": null, "id": "c5e987c0", "metadata": {}, "outputs": [], "source": [ "project_path = download_file(\n", " source=\"maxwell_magnetic_force\",\n", " name=\"Maxwell_Magnetic_Force.aedt\",\n", " local_path=temp_folder.name,\n", ")" ] }, { "cell_type": "markdown", "id": "445ebecc", "metadata": {}, "source": [ "## Initialize and launch Maxwell 2D\n", "\n", "Initialize and launch Maxwell 2D, providing the version and the path of the project." ] }, { "cell_type": "code", "execution_count": null, "id": "b5b0e661", "metadata": {}, "outputs": [], "source": [ "m2d = ansys.aedt.core.Maxwell2d(\n", " version=AEDT_VERSION,\n", " non_graphical=NG_MODE,\n", " project=project_path,\n", " new_desktop=True,\n", " design=\"Maxwell2DDesign1\",\n", ")" ] }, { "cell_type": "markdown", "id": "74c82a91", "metadata": {}, "source": [ "# First option" ] }, { "cell_type": "markdown", "id": "d6043959", "metadata": {}, "source": [ "## Create a polyline\n", "\n", "Create a polyline, specifying its ends." ] }, { "cell_type": "code", "execution_count": null, "id": "0afbdd08", "metadata": {}, "outputs": [], "source": [ "poly = m2d.modeler.create_polyline(points=[[10, -10, 0], [10, 10, 0]], name=\"polyline\")" ] }, { "cell_type": "markdown", "id": "88c0559c", "metadata": {}, "source": [ "Duplicate the polyline along a vector." ] }, { "cell_type": "code", "execution_count": null, "id": "e62a8265", "metadata": {}, "outputs": [], "source": [ "polys = [poly.name]\n", "polys.extend(poly.duplicate_along_line(vector=[-0.5, 0, 0], clones=10))" ] }, { "cell_type": "markdown", "id": "0e178981", "metadata": {}, "source": [ "## Compute magnetomotive force along each line\n", "\n", "Create and add a new formula to add in the PyAEDT advanced fields calculator.\n", "Create the fields report object and get field data.\n", "Create a data table report for the H field along each line and export it to a .csv file." ] }, { "cell_type": "code", "execution_count": null, "id": "20379c91", "metadata": {}, "outputs": [], "source": [ "my_expression = {\n", " \"name\": None,\n", " \"description\": \"Magnetomotive force along a line\",\n", " \"design_type\": [\"Maxwell 2D\", \"Maxwell 3D\"],\n", " \"fields_type\": [\"Fields\"],\n", " \"primary_sweep\": \"distance\",\n", " \"assignment\": None,\n", " \"assignment_type\": [\"Line\"],\n", " \"operations\": [\n", " \"Fundamental_Quantity('H')\",\n", " \"Operation('Tangent')\",\n", " \"Operation('Dot')\",\n", " \"EnterLine('assignment')\",\n", " \"Operation('LineValue')\",\n", " \"Operation('Integrate')\",\n", " ],\n", " \"report\": [\"Data Table\"],\n", "}" ] }, { "cell_type": "code", "execution_count": null, "id": "94b6ddf0", "metadata": {}, "outputs": [], "source": [ "quantities = []\n", "for p in polys:\n", " quantity = \"H_field_{}\".format(p)\n", " quantities.append(quantity)\n", " my_expression[\"name\"] = quantity\n", " my_expression[\"assignment\"] = quantity\n", " m2d.post.fields_calculator.add_expression(my_expression, p)\n", " report = m2d.post.create_report(\n", " expressions=quantity,\n", " report_category=\"Fields\",\n", " plot_type=\"Data Table\",\n", " plot_name=quantity\n", " )" ] }, { "cell_type": "markdown", "id": "1a5682d0", "metadata": {}, "source": [ "# Second option" ] }, { "cell_type": "markdown", "id": "c6cc7fef", "metadata": {}, "source": [ "## Create a design variable\n", "\n", "Parametrize the polyline x position." ] }, { "cell_type": "code", "execution_count": null, "id": "5af234be", "metadata": {}, "outputs": [], "source": [ "m2d[\"xl\"] = \"10mm\"" ] }, { "cell_type": "markdown", "id": "7c65e866", "metadata": {}, "source": [ "## Create polyline\n", "\n", "Create a parametrized polyline, specifying its ends." ] }, { "cell_type": "code", "execution_count": null, "id": "c75a31d2", "metadata": {}, "outputs": [], "source": [ "poly = m2d.modeler.create_polyline(\n", " points=[[\"xl\", -10, 0], [\"xl\", 10, 0]], name=\"polyline_sweep\"\n", ")" ] }, { "cell_type": "markdown", "id": "55381a1d", "metadata": {}, "source": [ "## Add parametric sweep\n", "\n", "Add a parametric sweep where the parameter to sweep is ``xl``.\n", "Create a linear step sweep from ``10mm`` to ``15mm`` every ``1mm`` step." ] }, { "cell_type": "code", "execution_count": null, "id": "b1756814", "metadata": {}, "outputs": [], "source": [ "param_sweep = m2d.parametrics.add(\n", " variable=\"xl\",\n", " start_point=\"10mm\",\n", " end_point=\"15mm\",\n", " step=1,\n", " variation_type=\"LinearStep\",\n", ")" ] }, { "cell_type": "markdown", "id": "015bcd74", "metadata": {}, "source": [ "## Compute magnetomotive force along the line\n", "\n", "Create and add a new formula to add in the PyAEDT advanced fields calculator." ] }, { "cell_type": "code", "execution_count": null, "id": "fb16266c", "metadata": {}, "outputs": [], "source": [ "quantity_sweep = \"H_field_{}\".format(poly.name)\n", "my_expression[\"name\"] = quantity_sweep\n", "my_expression[\"assignment\"] = poly.name\n", "m2d.post.fields_calculator.add_expression(my_expression, poly.name)" ] }, { "cell_type": "markdown", "id": "8546ad82", "metadata": {}, "source": [ "## Add parametric sweep calculation specifying the quantity (H) and save fields." ] }, { "cell_type": "code", "execution_count": null, "id": "1a8adf84", "metadata": {}, "outputs": [], "source": [ "param_sweep.add_calculation(calculation=quantity_sweep, report_type=\"Fields\", ranges={})\n", "param_sweep.props[\"ProdOptiSetupDataV2\"][\"SaveFields\"] = True" ] }, { "cell_type": "markdown", "id": "f2920c57", "metadata": {}, "source": [ "## Create data table report\n", "\n", "Create a data table report to display H for each polyline position." ] }, { "cell_type": "code", "execution_count": null, "id": "c2bc3161", "metadata": {}, "outputs": [], "source": [ "report_sweep = m2d.post.create_report(\n", " expressions=quantity_sweep,\n", " report_category=\"Fields\",\n", " plot_type=\"Data Table\",\n", " plot_name=quantity_sweep,\n", " primary_sweep_variable=\"xl\",\n", " variations={\"xl\": \"All\"},\n", ")" ] }, { "cell_type": "markdown", "id": "a493c654", "metadata": {}, "source": [ "## Analyze parametric sweep" ] }, { "cell_type": "code", "execution_count": null, "id": "b4eccdd4", "metadata": {}, "outputs": [], "source": [ "param_sweep.analyze(cores=NUM_CORES)" ] }, { "cell_type": "markdown", "id": "e9419816", "metadata": {}, "source": [ "## Export results\n", "\n", "Export results in a .csv file for the parametric sweep analysis (second option)." ] }, { "cell_type": "code", "execution_count": null, "id": "4d7eea6a", "metadata": {}, "outputs": [], "source": [ "m2d.post.export_report_to_csv(\n", " project_dir=temp_folder.name,\n", " plot_name=quantity_sweep,\n", ")" ] }, { "cell_type": "markdown", "id": "419fe7f2", "metadata": {}, "source": [ "Export results in a .csv file for each polyline (first option)." ] }, { "cell_type": "code", "execution_count": null, "id": "abdba2d2", "metadata": {}, "outputs": [], "source": [ "[\n", " m2d.post.export_report_to_csv(\n", " project_dir=temp_folder.name,\n", " plot_name=q,\n", " )\n", " for q in quantities\n", "]" ] }, { "cell_type": "markdown", "id": "c8e4820e", "metadata": {}, "source": [ "## Release AEDT" ] }, { "cell_type": "code", "execution_count": null, "id": "c605cb71", "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": "e8e485fc", "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": "f6d68b98", "metadata": {}, "outputs": [], "source": [ "temp_folder.cleanup()" ] } ], "metadata": { "jupytext": { "cell_metadata_filter": "-all", "main_language": "python", "notebook_metadata_filter": "-all" } }, "nbformat": 4, "nbformat_minor": 5 }