{ "cells": [ { "cell_type": "markdown", "id": "a36b029c", "metadata": {}, "source": [ "# Static ROM\n", "\n", "This example shows how to create a static reduced order model (ROM)\n", "in Twin Builder and run a transient simulation.\n", "\n", "> **Note:** This example uses functionality only available in Twin Builder 2024 R2 and later.\n", "\n", "Keywords: **Twin Builder**, **Static ROM**." ] }, { "cell_type": "markdown", "id": "68f55255", "metadata": {}, "source": [ "## Perform imports and define constants\n", "\n", "Perform required imports." ] }, { "cell_type": "code", "execution_count": null, "id": "d27903d7", "metadata": {}, "outputs": [], "source": [ "import os\n", "import shutil\n", "import tempfile\n", "import time" ] }, { "cell_type": "code", "execution_count": null, "id": "0546ae2a", "metadata": {}, "outputs": [], "source": [ "from ansys.aedt.core import TwinBuilder, downloads" ] }, { "cell_type": "markdown", "id": "0065a09a", "metadata": {}, "source": [ "Define constants." ] }, { "cell_type": "code", "execution_count": null, "id": "6c58f3e5", "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": "7cf5804f", "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": "8b386670", "metadata": {}, "outputs": [], "source": [ "temp_folder = tempfile.TemporaryDirectory(suffix=\".ansys\")" ] }, { "cell_type": "markdown", "id": "1613dad5", "metadata": {}, "source": [ "## Set up input data\n", "\n", "The following files are downloaded along with the\n", "other project data used to run this example." ] }, { "cell_type": "code", "execution_count": null, "id": "dd0370b5", "metadata": {}, "outputs": [], "source": [ "source_snapshot_data_zipfilename = \"Ex1_Fluent_StaticRom.zip\"\n", "source_build_conf_file = \"SROMbuild.conf\"\n", "source_props_conf_file = \"SROM_props.conf\"" ] }, { "cell_type": "markdown", "id": "58035669", "metadata": {}, "source": [ "## Download example data\n", "\n", "The following cell downloads the required files needed to run this example and\n", "extracts them in a local folder named ``\"Ex04\"``." ] }, { "cell_type": "code", "execution_count": null, "id": "4f010fbe", "metadata": {}, "outputs": [], "source": [ "_ = downloads.download_twin_builder_data(\n", " file_name=source_snapshot_data_zipfilename,\n", " force_download=True,\n", " destination=temp_folder.name,\n", ")\n", "\n", "_ = downloads.download_twin_builder_data(source_build_conf_file, True, temp_folder.name)\n", "source_data_folder = downloads.download_twin_builder_data(\n", " source_props_conf_file, True, temp_folder.name\n", ")\n", "\n", "# Target folder to extract project files.\n", "data_folder = os.path.join(source_data_folder, \"Ex04\")\n", "\n", "# Unzip training data and config file\n", "downloads.unzip(\n", " os.path.join(source_data_folder, source_snapshot_data_zipfilename), data_folder\n", ")\n", "shutil.copyfile(\n", " os.path.join(source_data_folder, source_build_conf_file),\n", " os.path.join(data_folder, source_build_conf_file),\n", ")\n", "shutil.copyfile(\n", " os.path.join(source_data_folder, source_props_conf_file),\n", " os.path.join(data_folder, source_props_conf_file),\n", ")" ] }, { "cell_type": "markdown", "id": "0530b910", "metadata": {}, "source": [ "## Launch Twin Builder and build ROM component\n", "\n", "Launch Twin Builder using an implicit declaration and add a new design with\n", "the default setup for building the static ROM component." ] }, { "cell_type": "code", "execution_count": null, "id": "b6080f18", "metadata": {}, "outputs": [], "source": [ "project_name = os.path.join(temp_folder.name, \"static_rom.aedt\")\n", "tb = TwinBuilder(\n", " project=project_name,\n", " version=AEDT_VERSION,\n", " non_graphical=NG_MODE,\n", " new_desktop=True,\n", ")" ] }, { "cell_type": "markdown", "id": "4da7e5fa", "metadata": {}, "source": [ "## Configure AEDT\n", "\n", "> **Note:** Only run the following cell if AEDT is not configured to run Twin Builder.\n", ">\n", "> The following cell configures AEDT and the schematic editor\n", "> to use the ``Twin Builder`` configuration.\n", "> The Static ROM feature is only available with a Twin Builder license.\n", "> A cell at the end of this example restores the AEDT configuration. If your\n", "> environment is set up to use the ``Twin Builder`` configuration, you do not\n", "> need to run these sections." ] }, { "cell_type": "code", "execution_count": null, "id": "f4b402ba", "metadata": {}, "outputs": [], "source": [ "current_desktop_config = tb._odesktop.GetDesktopConfiguration()\n", "current_schematic_environment = tb._odesktop.GetSchematicEnvironment()\n", "tb._odesktop.SetDesktopConfiguration(\"Twin Builder\")\n", "tb._odesktop.SetSchematicEnvironment(1)" ] }, { "cell_type": "code", "execution_count": null, "id": "ac15b9c1", "metadata": {}, "outputs": [], "source": [ "# Get the static ROM builder object.\n", "rom_manager = tb._odesign.GetROMManager()\n", "static_rom_builder = rom_manager.GetStaticROMBuilder()\n", "\n", "# Build the static ROM with the specified configuration file\n", "confpath = os.path.join(data_folder, source_build_conf_file)\n", "static_rom_builder.Build(confpath.replace(\"\\\\\", \"/\"))\n", "\n", "# Test if the ROM was created successfully.\n", "static_rom_path = os.path.join(data_folder, \"StaticRom.rom\")\n", "if os.path.exists(static_rom_path):\n", " tb.logger.info(\"Built intermediate rom file successfully at: %s\", static_rom_path)\n", "else:\n", " tb.logger.error(\"Intermediate rom file not found at: %s\", static_rom_path)\n", "\n", "# Create the ROM component definition in Twin Builder.\n", "rom_manager.CreateROMComponent(static_rom_path.replace(\"\\\\\", \"/\"), \"staticrom\")" ] }, { "cell_type": "markdown", "id": "ec149b01", "metadata": {}, "source": [ "## Create schematic\n", "\n", "Place components to create the schematic." ] }, { "cell_type": "code", "execution_count": null, "id": "a73f08e4", "metadata": {}, "outputs": [], "source": [ "# Define the grid distance for ease in calculations.\n", "G = 0.00254\n", "\n", "# Place a dynamic ROM component.\n", "rom1 = tb.modeler.schematic.create_component(\"ROM1\", \"\", \"staticrom\", [40 * G, 25 * G])\n", "\n", "# Place two excitation sources.\n", "source1 = tb.modeler.schematic.create_periodic_waveform_source(\n", " None, \"SINE\", 2.5, 0.01, 0, 7.5, 0, [20 * G, 29 * G]\n", ")\n", "source2 = tb.modeler.schematic.create_periodic_waveform_source(\n", " None, \"SINE\", 50, 0.02, 0, 450, 0, [20 * G, 25 * G]\n", ")" ] }, { "cell_type": "markdown", "id": "00d72288", "metadata": {}, "source": [ "Connect components with wires." ] }, { "cell_type": "code", "execution_count": null, "id": "6b2498cd", "metadata": {}, "outputs": [], "source": [ "tb.modeler.schematic.create_wire([[22 * G, 29 * G], [33 * G, 29 * G]])\n", "tb.modeler.schematic.create_wire(\n", " [[22 * G, 25 * G], [30 * G, 25 * G], [30 * G, 28 * G], [33 * G, 28 * G]]\n", ")" ] }, { "cell_type": "markdown", "id": "4b35aeae", "metadata": {}, "source": [ "Enable storage of views." ] }, { "cell_type": "code", "execution_count": null, "id": "a1d27966", "metadata": {}, "outputs": [], "source": [ "rom1.set_property(\"store_snapshots\", 1)\n", "rom1.set_property(\"view1_storage_period\", \"10s\")\n", "rom1.set_property(\"view2_storage_period\", \"10s\")" ] }, { "cell_type": "code", "execution_count": null, "id": "64521b7d", "metadata": {}, "outputs": [], "source": [ "# Zoom to fit the schematic\n", "tb.modeler.zoom_to_fit()" ] }, { "cell_type": "markdown", "id": "270b5607", "metadata": {}, "source": [ "## Parametrize transient setup\n", "\n", "Parametrize the default transient setup by setting the end time." ] }, { "cell_type": "code", "execution_count": null, "id": "762ff50e", "metadata": {}, "outputs": [], "source": [ "tb.set_end_time(\"300s\")\n", "tb.set_hmin(\"1s\")\n", "tb.set_hmax(\"1s\")" ] }, { "cell_type": "markdown", "id": "b0c47111", "metadata": {}, "source": [ "## Solve transient setup\n", "\n", "Solve the transient setup. Skipping this step in case the documentation is being built." ] }, { "cell_type": "markdown", "id": "b5e44975", "metadata": {}, "source": [ "> **Note:** The following code can be uncommented.\n", "\n", "tb.analyze_setup(\"TR\")" ] }, { "cell_type": "markdown", "id": "33214153", "metadata": {}, "source": [ "## Get report data and plot using Matplotlib\n", "\n", "Get report data and plot it using Matplotlib. The following code gets and plots\n", "the values for the voltage on the pulse voltage source and the values for the\n", "output of the dynamic ROM." ] }, { "cell_type": "markdown", "id": "539adb3c", "metadata": { "lines_to_next_cell": 2 }, "source": [ "> **Note:** The following code can be uncommented, but it depends on the previous commented code.\n", "\n", "e_value = \"ROM1.outfield_mode_1\"\n", "x = tb.post.get_solution_data(e_value, \"TR\", \"Time\")\n", "x.plot()\n", "e_value = \"ROM1.outfield_mode_2\"\n", "x = tb.post.get_solution_data(e_value, \"TR\", \"Time\")\n", "x.plot()\n", "e_value = \"SINE1.VAL\"\n", "x = tb.post.get_solution_data(e_value, \"TR\", \"Time\")\n", "x.plot()\n", "e_value = \"SINE2.VAL\"\n", "x = tb.post.get_solution_data(e_value, \"TR\", \"Time\")\n", "x.plot()" ] }, { "cell_type": "markdown", "id": "5a4b0e8c", "metadata": {}, "source": [ "## Close Twin Builder\n", "\n", "After the simulation is completed, either close Twin Builder or release it.\n", "All methods provide for saving the project before closing." ] }, { "cell_type": "code", "execution_count": null, "id": "798ed477", "metadata": {}, "outputs": [], "source": [ "# Clean up the downloaded data.\n", "shutil.rmtree(source_data_folder)" ] }, { "cell_type": "code", "execution_count": null, "id": "8583921a", "metadata": {}, "outputs": [], "source": [ "# Restore earlier desktop configuration and schematic environment.\n", "tb._odesktop.SetDesktopConfiguration(current_desktop_config)\n", "tb._odesktop.SetSchematicEnvironment(current_schematic_environment)" ] }, { "cell_type": "markdown", "id": "f7b29063", "metadata": {}, "source": [ "## Release AEDT\n", "\n", "Release AEDT and close the example." ] }, { "cell_type": "code", "execution_count": null, "id": "59c73beb", "metadata": {}, "outputs": [], "source": [ "tb.save_project()\n", "tb.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": "01d5d27a", "metadata": {}, "source": [ "## Clean up\n", "\n", "All project files are saved in the folder ``temp_folder.name``. If you've run this example as a Jupyter notebook, you\n", "can retrieve those project files. The following cell removes all temporary files, including the project folder." ] }, { "cell_type": "code", "execution_count": null, "id": "b8664dc5", "metadata": {}, "outputs": [], "source": [ "temp_folder.cleanup()" ] } ], "metadata": { "jupytext": { "cell_metadata_filter": "-all", "main_language": "python", "notebook_metadata_filter": "-all" } }, "nbformat": 4, "nbformat_minor": 5 }