{ "cells": [ { "cell_type": "markdown", "id": "60155a54", "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": "38eb11b1", "metadata": {}, "source": [ "Keywords: **Maxwell 2D**, **magnetomotive force**." ] }, { "cell_type": "markdown", "id": "9942a426", "metadata": {}, "source": [ "## Perform imports and define constants\n", "\n", "Perform required imports." ] }, { "cell_type": "code", "execution_count": null, "id": "4fb89e8f", "metadata": {}, "outputs": [], "source": [ "import tempfile\n", "import time" ] }, { "cell_type": "code", "execution_count": null, "id": "1ab476e4", "metadata": {}, "outputs": [], "source": [ "import ansys.aedt.core" ] }, { "cell_type": "markdown", "id": "3912d4ab", "metadata": {}, "source": [ "Define constants." ] }, { "cell_type": "code", "execution_count": null, "id": "71607702", "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": "7e7d3fc7", "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": "27f2036f", "metadata": {}, "outputs": [], "source": [ "temp_folder = tempfile.TemporaryDirectory(suffix=\".ansys\")" ] }, { "cell_type": "markdown", "id": "3d789ca8", "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": "a839b02b", "metadata": {}, "outputs": [], "source": [ "project_path = ansys.aedt.core.downloads.download_file(\n", " source=\"maxwell_magnetic_force\",\n", " name=\"Maxwell_Magnetic_Force.aedt\",\n", " destination=temp_folder.name,\n", ")" ] }, { "cell_type": "markdown", "id": "d2110cd5", "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": "801cfcd6", "metadata": {}, "outputs": [], "source": [ "m2d = ansys.aedt.core.Maxwell2d(\n", " version=AEDT_VERSION,\n", " non_graphical=NG_MODE,\n", " project=project_path,\n", " design=\"Maxwell2DDesign1\",\n", ")" ] }, { "cell_type": "markdown", "id": "54a1377b", "metadata": {}, "source": [ "# First option" ] }, { "cell_type": "markdown", "id": "b3d6fc89", "metadata": {}, "source": [ "## Create a polyline\n", "\n", "Create a polyline, specifying its ends." ] }, { "cell_type": "code", "execution_count": null, "id": "ff0c4f20", "metadata": {}, "outputs": [], "source": [ "poly = m2d.modeler.create_polyline(points=[[10, -10, 0], [10, 10, 0]], name=\"polyline\")" ] }, { "cell_type": "markdown", "id": "622b3794", "metadata": {}, "source": [ "Duplicate the polyline along a vector." ] }, { "cell_type": "code", "execution_count": null, "id": "7c9d9e92", "metadata": {}, "outputs": [], "source": [ "polys = [poly.name]\n", "polys.extend(poly.duplicate_along_line(vector=[-0.5, 0, 0], clones=10))" ] }, { "cell_type": "markdown", "id": "26352a42", "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": "429c8583", "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": "ed644da1", "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", " context=p,\n", " polyline_points=1,\n", " report_category=\"Fields\",\n", " plot_type=\"Data Table\",\n", " plot_name=quantity,\n", " )" ] }, { "cell_type": "markdown", "id": "d245c403", "metadata": {}, "source": [ "# Second option" ] }, { "cell_type": "markdown", "id": "e2dc6da4", "metadata": {}, "source": [ "## Create a design variable\n", "\n", "Parametrize the polyline x position." ] }, { "cell_type": "code", "execution_count": null, "id": "7e3bfecc", "metadata": {}, "outputs": [], "source": [ "m2d[\"xl\"] = \"10mm\"" ] }, { "cell_type": "markdown", "id": "989bd26f", "metadata": {}, "source": [ "## Create polyline\n", "\n", "Create a parametrized polyline, specifying its ends." ] }, { "cell_type": "code", "execution_count": null, "id": "02337783", "metadata": {}, "outputs": [], "source": [ "poly = m2d.modeler.create_polyline(\n", " points=[[\"xl\", -10, 0], [\"xl\", 10, 0]], name=\"polyline_sweep\"\n", ")" ] }, { "cell_type": "markdown", "id": "e51f5110", "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": "f76c5d3c", "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": "b323fa31", "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": "2a7bcfa9", "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": "62d57e23", "metadata": {}, "source": [ "## Add parametric sweep calculation specifying the quantity (H) and save fields." ] }, { "cell_type": "code", "execution_count": null, "id": "f1fc8f1b", "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": "fb97fe75", "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": "ef1bf2f5", "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": "6f5d0770", "metadata": {}, "source": [ "## Analyze parametric sweep" ] }, { "cell_type": "code", "execution_count": null, "id": "6d4e6c91", "metadata": {}, "outputs": [], "source": [ "param_sweep.analyze(cores=NUM_CORES)" ] }, { "cell_type": "markdown", "id": "7fb86940", "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": "6a6737b5", "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": "3cfc5547", "metadata": {}, "source": [ "Export results in a .csv file for each polyline (first option)." ] }, { "cell_type": "code", "execution_count": null, "id": "ee1cb417", "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": "9cd410b6", "metadata": {}, "source": [ "## Release AEDT" ] }, { "cell_type": "code", "execution_count": null, "id": "c94c6a96", "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": "11e9eb69", "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": "225a5ed5", "metadata": {}, "outputs": [], "source": [ "temp_folder.cleanup()" ] } ], "metadata": { "jupytext": { "cell_metadata_filter": "-all", "main_language": "python", "notebook_metadata_filter": "-all" } }, "nbformat": 4, "nbformat_minor": 5 }