{ "cells": [ { "cell_type": "markdown", "id": "0ca69fec", "metadata": {}, "source": [ "# Electro DC analysis" ] }, { "cell_type": "markdown", "id": "7030b8ea", "metadata": {}, "source": [ "This example shows how to use PyAEDT to create a Maxwell DC analysis,\n", "compute mass center, and move coordinate systems.\n", "\n", "Keywords: **Maxwell 3D**, **DC**." ] }, { "cell_type": "markdown", "id": "030dd99d", "metadata": {}, "source": [ "## Perform imports and define constants\n", "\n", "Perform required imports." ] }, { "cell_type": "code", "execution_count": null, "id": "119e7c8e", "metadata": {}, "outputs": [], "source": [ "import os\n", "import tempfile\n", "import time\n", "\n", "from ansys.aedt.core import Maxwell3d\n", "from ansys.aedt.core.generic.constants import SolutionsMaxwell3D\n" ] }, { "cell_type": "markdown", "id": "51dba2ad", "metadata": {}, "source": [ "Define constants." ] }, { "cell_type": "code", "execution_count": null, "id": "07da56d5", "metadata": {}, "outputs": [], "source": [ "AEDT_VERSION = \"2025.2\"\n", "NUM_CORES = 4\n", "NG_MODE = False # Open AEDT UI when it is launched." ] }, { "cell_type": "markdown", "id": "e8fd378c", "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": "b2362389", "metadata": {}, "outputs": [], "source": [ "temp_folder = tempfile.TemporaryDirectory(suffix=\".ansys\")" ] }, { "cell_type": "markdown", "id": "eeeacd46", "metadata": {}, "source": [ "## Launch AEDT\n", "\n", "Create an instance of the ``Maxwell3d`` class named ``m3d`` by providing\n", "the project name, the version, and the graphical mode." ] }, { "cell_type": "code", "execution_count": null, "id": "b4437060", "metadata": {}, "outputs": [], "source": [ "project_name = os.path.join(temp_folder.name, \"conductor_example.aedt\")\n", "m3d = Maxwell3d(\n", " project=project_name,\n", " version=AEDT_VERSION,\n", " new_desktop=True,\n", " non_graphical=NG_MODE,\n", ")" ] }, { "cell_type": "markdown", "id": "091b3432", "metadata": {}, "source": [ "## Set up Maxwell solution\n", "\n", "Set up the Maxwell solution to DC." ] }, { "cell_type": "code", "execution_count": null, "id": "96456918", "metadata": {}, "outputs": [], "source": [ "solutions = SolutionsMaxwell3D.versioned(AEDT_VERSION)\n", "m3d.solution_type = solutions.ElectroDCConduction" ] }, { "cell_type": "markdown", "id": "d7b0d033", "metadata": {}, "source": [ "## Create conductor\n", "\n", "Create a conductor using copper, a predefined material in the Maxwell material library." ] }, { "cell_type": "code", "execution_count": null, "id": "5cc7dd4d", "metadata": {}, "outputs": [], "source": [ "conductor = m3d.modeler.create_box(\n", " origin=[7, 4, 22], sizes=[10, 5, 30], name=\"Conductor\", material=\"copper\"\n", ")" ] }, { "cell_type": "markdown", "id": "34405c07", "metadata": {}, "source": [ "## Create setup and assign voltage" ] }, { "cell_type": "code", "execution_count": null, "id": "75637d3c", "metadata": {}, "outputs": [], "source": [ "m3d.assign_voltage(assignment=conductor.faces, amplitude=0)\n", "m3d.create_setup()" ] }, { "cell_type": "markdown", "id": "f83fb579", "metadata": {}, "source": [ "## Solve setup" ] }, { "cell_type": "code", "execution_count": null, "id": "3d14faaf", "metadata": {}, "outputs": [], "source": [ "m3d.analyze(cores=NUM_CORES)" ] }, { "cell_type": "markdown", "id": "46217820", "metadata": {}, "source": [ "## Compute mass center\n", "\n", "Compute mass center using PyAEDT advanced fields calculator." ] }, { "cell_type": "code", "execution_count": null, "id": "35cf1fda", "metadata": {}, "outputs": [], "source": [ "scalar_function = \"\"\n", "mass_center = {\n", " \"name\": \"CM_X\",\n", " \"description\": \"Mass center computation\",\n", " \"design_type\": [\"Maxwell 3D\"],\n", " \"fields_type\": [\"Fields\"],\n", " \"primary_sweep\": \"distance\",\n", " \"assignment\": \"\",\n", " \"assignment_type\": [\"Solid\"],\n", " \"operations\": [\n", " scalar_function,\n", " \"EnterVolume('assignment')\",\n", " \"Operation('VolumeValue')\",\n", " \"Operation('Mean')\",\n", " ],\n", " \"report\": [\"Data Table\"],\n", "}\n", "scalar_function = \"Scalar_Function(FuncValue='X')\"\n", "mass_center[\"operations\"][0] = scalar_function\n", "m3d.post.fields_calculator.add_expression(mass_center, conductor.name)\n", "mass_center[\"name\"] = \"CM_Y\"\n", "scalar_function = \"Scalar_Function(FuncValue='Y')\"\n", "mass_center[\"operations\"][0] = scalar_function\n", "m3d.post.fields_calculator.add_expression(mass_center, conductor.name)\n", "mass_center[\"name\"] = \"CM_Z\"\n", "scalar_function = \"Scalar_Function(FuncValue='Z')\"\n", "mass_center[\"operations\"][0] = scalar_function\n", "m3d.post.fields_calculator.add_expression(mass_center, conductor.name)" ] }, { "cell_type": "markdown", "id": "556b8762", "metadata": {}, "source": [ "## Get mass center\n", "\n", "Get mass center using the fields calculator." ] }, { "cell_type": "code", "execution_count": null, "id": "af535ee4", "metadata": {}, "outputs": [], "source": [ "xval = m3d.post.get_scalar_field_value(quantity=\"CM_X\")\n", "yval = m3d.post.get_scalar_field_value(quantity=\"CM_Y\")\n", "zval = m3d.post.get_scalar_field_value(quantity=\"CM_Z\")" ] }, { "cell_type": "markdown", "id": "a47e667c", "metadata": {}, "source": [ "## Create variables\n", "\n", "Create variables with mass center values." ] }, { "cell_type": "code", "execution_count": null, "id": "71bda90e", "metadata": {}, "outputs": [], "source": [ "m3d[conductor.name + \"x\"] = str(xval * 1e3) + \"mm\"\n", "m3d[conductor.name + \"y\"] = str(yval * 1e3) + \"mm\"\n", "m3d[conductor.name + \"z\"] = str(zval * 1e3) + \"mm\"" ] }, { "cell_type": "markdown", "id": "5cff763a", "metadata": {}, "source": [ "## Create coordinate system\n", "\n", "Create a parametric coordinate system." ] }, { "cell_type": "code", "execution_count": null, "id": "4d7395fc", "metadata": {}, "outputs": [], "source": [ "cs1 = m3d.modeler.create_coordinate_system(\n", " origin=[conductor.name + \"x\", conductor.name + \"y\", conductor.name + \"z\"],\n", " reference_cs=\"Global\",\n", " name=conductor.name + \"CS\",\n", ")" ] }, { "cell_type": "markdown", "id": "a6dd611f", "metadata": {}, "source": [ "## Release AEDT" ] }, { "cell_type": "code", "execution_count": null, "id": "e13581ac", "metadata": {}, "outputs": [], "source": [ "m3d.save_project()\n", "m3d.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": "f180f6a2", "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": "74c9f003", "metadata": {}, "outputs": [], "source": [ "temp_folder.cleanup()" ] } ], "metadata": { "jupytext": { "cell_metadata_filter": "-all", "main_language": "python", "notebook_metadata_filter": "-all" } }, "nbformat": 4, "nbformat_minor": 5 }