{ "cells": [ { "cell_type": "markdown", "id": "758673bc", "metadata": {}, "source": [ "# Fields export in transient analysis\n", "\n", "This example shows how to leverage PyAEDT to set up a Maxwell 3D transient analysis and then\n", "compute the average value of the current density field over a specific coil surface\n", "and the magnitude of the current density field over all coil surfaces at each time step\n", "of the transient analysis.\n", "\n", "Keywords: **Maxwell 3D**, **transient**, **fields calculator**, **field export**." ] }, { "cell_type": "markdown", "id": "7237ec8d", "metadata": {}, "source": [ "## Perform imports and define constant\n", "\n", "Perform required imports." ] }, { "cell_type": "code", "execution_count": null, "id": "6d10ab61", "metadata": {}, "outputs": [], "source": [ "import tempfile\n", "import time" ] }, { "cell_type": "code", "execution_count": null, "id": "58450e5f", "metadata": {}, "outputs": [], "source": [ "import ansys.aedt.core" ] }, { "cell_type": "markdown", "id": "5080ea3f", "metadata": {}, "source": [ "Define constants." ] }, { "cell_type": "code", "execution_count": null, "id": "f73cab7e", "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": "7dba10b0", "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": "d85b8f18", "metadata": {}, "outputs": [], "source": [ "temp_folder = tempfile.TemporaryDirectory(suffix=\".ansys\")" ] }, { "cell_type": "markdown", "id": "1ebc428b", "metadata": {}, "source": [ "## Import project\n", "\n", "The files required to run this example are downloaded into the temporary working folder." ] }, { "cell_type": "code", "execution_count": null, "id": "aaf9e60f", "metadata": {}, "outputs": [], "source": [ "project_path = ansys.aedt.core.downloads.download_file(\n", " source=\"maxwell_transient_fields\",\n", " name=\"M3D_Transient_StrandedWindings.aedt\",\n", " destination=temp_folder.name,\n", ")" ] }, { "cell_type": "markdown", "id": "1af2a77e", "metadata": {}, "source": [ "## Initialize and launch Maxwell 3D\n", "\n", "Initialize and launch Maxwell 3D, providing the version and the path of the project." ] }, { "cell_type": "code", "execution_count": null, "id": "d4be955d", "metadata": {}, "outputs": [], "source": [ "m3d = ansys.aedt.core.Maxwell3d(\n", " project=project_path, version=AEDT_VERSION, non_graphical=NG_MODE\n", ")" ] }, { "cell_type": "markdown", "id": "1a580692", "metadata": {}, "source": [ "## Create setup and validate\n", "\n", "Create the setup specifying general settings such as ``StopTime``, ``TimeStep``,\n", "and ``SaveFieldsType``." ] }, { "cell_type": "code", "execution_count": null, "id": "224d10b1", "metadata": {}, "outputs": [], "source": [ "setup = m3d.create_setup(name=\"Setup1\")\n", "setup.props[\"StopTime\"] = \"0.02s\"\n", "setup.props[\"TimeStep\"] = \"0.002s\"\n", "setup.props[\"SaveFieldsType\"] = \"Every N Steps\"\n", "setup.props[\"N Steps\"] = \"2\"\n", "setup.props[\"Steps From\"] = \"0s\"\n", "setup.props[\"Steps To\"] = \"0.02s\"\n", "setup.update()" ] }, { "cell_type": "markdown", "id": "22205343", "metadata": {}, "source": [ "## Create field expressions\n", "\n", "Create a field expression to evaluate the J field normal to a surface using the advanced fields calculator.\n", "The expression is created as a dictionary and then provided as an argument to the ``add_expression()`` method." ] }, { "cell_type": "code", "execution_count": null, "id": "a4f4f639", "metadata": {}, "outputs": [], "source": [ "j_normal = {\n", " \"name\": \"Jn\",\n", " \"description\": \"J field normal to a surface\",\n", " \"design_type\": [\"Maxwell 3D\"],\n", " \"fields_type\": [\"Fields\"],\n", " \"primary_sweep\": \"Time\",\n", " \"assignment\": \"\",\n", " \"assignment_type\": [\"\"],\n", " \"operations\": [\n", " \"NameOfExpression('')\",\n", " \"Operation('Normal')\",\n", " \"Operation('Dot')\",\n", " ],\n", " \"report\": [\"Field_3D\"],\n", "}\n", "m3d.post.fields_calculator.add_expression(j_normal, None)" ] }, { "cell_type": "markdown", "id": "5553520c", "metadata": {}, "source": [ "Calculate the average value of the J field normal using the advanced fields calculator." ] }, { "cell_type": "code", "execution_count": null, "id": "178811a3", "metadata": {}, "outputs": [], "source": [ "j_avg = {\n", " \"name\": \"Jn_avg\",\n", " \"description\": \"Average J field normal to a surface\",\n", " \"design_type\": [\"Maxwell 3D\"],\n", " \"fields_type\": [\"Fields\"],\n", " \"primary_sweep\": \"Time\",\n", " \"assignment\": \"Coil_A2_ObjectFromFace1\",\n", " \"assignment_type\": [\"\"],\n", " \"operations\": [\n", " \"NameOfExpression('Jn')\",\n", " \"EnterSurface('assignment')\",\n", " \"Operation('SurfaceValue')\",\n", " \"Operation('Mean')\",\n", " ],\n", " \"report\": [\"Field_3D\"],\n", "}\n", "m3d.post.fields_calculator.add_expression(j_avg, None)" ] }, { "cell_type": "markdown", "id": "0e3ea1ad", "metadata": {}, "source": [ "## Analyze setup specifying setup name" ] }, { "cell_type": "code", "execution_count": null, "id": "a5b34dcc", "metadata": {}, "outputs": [], "source": [ "m3d.analyze_setup(name=setup.name, cores=NUM_CORES)" ] }, { "cell_type": "markdown", "id": "31fb92db", "metadata": {}, "source": [ "## Get available report quantities\n", "\n", "Get the available report quantities given a specific report category.\n", "In this case, ``Calculator Expressions`` is the category." ] }, { "cell_type": "code", "execution_count": null, "id": "9c64abf3", "metadata": {}, "outputs": [], "source": [ "report_types = m3d.post.available_report_types\n", "quantity = m3d.post.available_report_quantities(\n", " report_category=\"Fields\", quantities_category=\"Calculator Expressions\"\n", ")" ] }, { "cell_type": "markdown", "id": "65a35857", "metadata": {}, "source": [ "## Compute time steps of the analysis\n", "\n", "Create a fields report object given the nominal sweep.\n", "Get the report solution data to compute the time steps of the transient analysis." ] }, { "cell_type": "code", "execution_count": null, "id": "f0159d56", "metadata": {}, "outputs": [], "source": [ "sol = m3d.post.reports_by_category.fields(setup=m3d.nominal_sweep)\n", "data = sol.get_solution_data()\n", "time_steps = data.intrinsics[\"Time\"]" ] }, { "cell_type": "markdown", "id": "d8dc02d8", "metadata": {}, "source": [ "## Create field plots over the coil surfaces and export field data\n", "\n", "Convert each time step into millimeters.\n", "Create field plots over the surface of each coil by specifying the coil object,\n", "the quantity to plot, and the time step.\n", "\n", "The average value of the J field normal is plotted on the ``Coil_A2`` surface for every time step.\n", "The J field is plotted on the surface of each coil for every time-step.\n", "Fields data is exported to the temporary folder as an AEDTPLT file." ] }, { "cell_type": "code", "execution_count": null, "id": "fc5c61c5", "metadata": { "lines_to_next_cell": 2 }, "outputs": [], "source": [ "for time_step in time_steps:\n", " t = ansys.aedt.core.generic.constants.unit_converter(\n", " time_step,\n", " unit_system=\"Time\",\n", " input_units=data.units_sweeps[\"Time\"],\n", " output_units=\"ms\",\n", " )\n", " m3d.post.create_fieldplot_surface(\n", " assignment=m3d.modeler.objects_by_name[\"Coil_A2\"],\n", " quantity=quantity[0],\n", " plot_name=\"J_{}_ms\".format(t),\n", " intrinsics={\"Time\": \"{}ms\".format(t)},\n", " )\n", " mean_j_field_export = m3d.post.export_field_plot(\n", " plot_name=\"J_{}_ms\".format(t),\n", " output_dir=temp_folder.name,\n", " file_format=\"aedtplt\",\n", " )\n", " m3d.post.create_fieldplot_surface(\n", " assignment=[\n", " o for o in m3d.modeler.solid_objects if o.material_name == \"copper\"\n", " ],\n", " quantity=\"Mag_J\",\n", " plot_name=\"Mag_J_Coils_{}_ms\".format(t),\n", " intrinsics={\"Time\": \"{}ms\".format(t)},\n", " )\n", " mag_j_field_export = m3d.post.export_field_plot(\n", " plot_name=\"Mag_J_Coils_{}_ms\".format(t),\n", " output_dir=temp_folder.name,\n", " file_format=\"aedtplt\",\n", " )" ] }, { "cell_type": "markdown", "id": "4647f81d", "metadata": {}, "source": [ "## Release AEDT" ] }, { "cell_type": "code", "execution_count": null, "id": "03582c44", "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": "09d34349", "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": "285e5df2", "metadata": {}, "outputs": [], "source": [ "temp_folder.cleanup()" ] } ], "metadata": { "jupytext": { "cell_metadata_filter": "-all", "main_language": "python", "notebook_metadata_filter": "-all" } }, "nbformat": 4, "nbformat_minor": 5 }