{ "cells": [ { "cell_type": "markdown", "id": "f4130d4d", "metadata": {}, "source": [ "# Electro DC analysis" ] }, { "cell_type": "markdown", "id": "7b4620c4", "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": "f54f42c6", "metadata": {}, "source": [ "## Perform imports and define constants\n", "\n", "Perform required imports." ] }, { "cell_type": "code", "execution_count": null, "id": "c9d71d03", "metadata": {}, "outputs": [], "source": [ "import os\n", "import tempfile\n", "import time" ] }, { "cell_type": "code", "execution_count": null, "id": "ee0eda5d", "metadata": {}, "outputs": [], "source": [ "from ansys.aedt.core import Maxwell3d" ] }, { "cell_type": "markdown", "id": "9fc14488", "metadata": {}, "source": [ "Define constants." ] }, { "cell_type": "code", "execution_count": null, "id": "24f8a6b0", "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": "1c1d2edf", "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": "3ee65e9a", "metadata": {}, "outputs": [], "source": [ "temp_folder = tempfile.TemporaryDirectory(suffix=\".ansys\")" ] }, { "cell_type": "markdown", "id": "1f807938", "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": "7b66c7e7", "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": "65144ba1", "metadata": {}, "source": [ "## Set up Maxwell solution\n", "\n", "Set up the Maxwell solution to DC." ] }, { "cell_type": "code", "execution_count": null, "id": "9dfb42d6", "metadata": {}, "outputs": [], "source": [ "m3d.solution_type = m3d.SOLUTIONS.Maxwell3d.ElectroDCConduction" ] }, { "cell_type": "markdown", "id": "1ca924f4", "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": "cb62f5f9", "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": "6a0e986a", "metadata": {}, "source": [ "## Create setup and assign voltage" ] }, { "cell_type": "code", "execution_count": null, "id": "86ba426d", "metadata": {}, "outputs": [], "source": [ "m3d.assign_voltage(assignment=conductor.faces, amplitude=0)\n", "m3d.create_setup()" ] }, { "cell_type": "markdown", "id": "9aba2355", "metadata": {}, "source": [ "## Solve setup" ] }, { "cell_type": "code", "execution_count": null, "id": "98de911b", "metadata": {}, "outputs": [], "source": [ "m3d.analyze(cores=NUM_CORES)" ] }, { "cell_type": "markdown", "id": "c8f3b9fd", "metadata": {}, "source": [ "## Compute mass center\n", "\n", "Compute mass center using PyAEDT advanced fields calculator." ] }, { "cell_type": "code", "execution_count": null, "id": "0cd13ead", "metadata": {}, "outputs": [], "source": [ "scalar_function = \"\"\n", "mass_center = {\n", " \"name\": \"\",\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", "mass_center[\"name\"] = \"CM_X\"\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": "c8ac28a6", "metadata": {}, "source": [ "## Get mass center\n", "\n", "Get mass center using the fields calculator." ] }, { "cell_type": "code", "execution_count": null, "id": "07b70832", "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": "2798846f", "metadata": {}, "source": [ "## Create variables\n", "\n", "Create variables with mass center values." ] }, { "cell_type": "code", "execution_count": null, "id": "f8f89257", "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": "502b434a", "metadata": {}, "source": [ "## Create coordinate system\n", "\n", "Create a parametric coordinate system." ] }, { "cell_type": "code", "execution_count": null, "id": "3f15d2e0", "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": "52ba5d21", "metadata": {}, "source": [ "## Release AEDT" ] }, { "cell_type": "code", "execution_count": null, "id": "032f42e4", "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": "5f7add3e", "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": "389af3a0", "metadata": {}, "outputs": [], "source": [ "temp_folder.cleanup()" ] } ], "metadata": { "jupytext": { "cell_metadata_filter": "-all", "main_language": "python", "notebook_metadata_filter": "-all" } }, "nbformat": 4, "nbformat_minor": 5 }