.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples\03-Maxwell\Maxwell2D_DCConduction.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_examples_03-Maxwell_Maxwell2D_DCConduction.py: Maxwell 2D: resistance calculation ---------------------------------- This example uses PyAEDT to set up a resistance calculation and solve it using the Maxwell 2D DCConduction solver. Keywords: DXF import, material sweep, expression cache .. GENERATED FROM PYTHON SOURCE LINES 8-14 .. code-block:: Python import os.path import pyaedt from pyaedt.generic.pdf import AnsysReport .. GENERATED FROM PYTHON SOURCE LINES 15-18 Set AEDT version ~~~~~~~~~~~~~~~~ Set AEDT version. .. GENERATED FROM PYTHON SOURCE LINES 18-21 .. code-block:: Python aedt_version = "2024.1" .. GENERATED FROM PYTHON SOURCE LINES 22-27 Launch AEDT and Maxwell 2D ~~~~~~~~~~~~~~~~~~~~~~~~~~ Launch AEDT and Maxwell 2D after first setting up the project and design names, the solver, and the version. The following code also creates an instance of the ``Maxwell2d`` class named ``m2d``. .. GENERATED FROM PYTHON SOURCE LINES 27-37 .. code-block:: Python m2d = pyaedt.Maxwell2d( specified_version=aedt_version, new_desktop_session=True, close_on_exit=True, solution_type="DCConduction", projectname="M2D_DC_Conduction", designname="Ansys_resistor" ) .. rst-class:: sphx-glr-script-out .. code-block:: none C:\actions-runner\_work\_tool\Python\3.10.9\x64\lib\subprocess.py:1072: ResourceWarning: subprocess 6796 is still running _warn("subprocess %s is still running" % self.pid, C:\actions-runner\_work\pyaedt\pyaedt\.venv\lib\site-packages\pyaedt\generic\settings.py:383: ResourceWarning: unclosed file <_io.TextIOWrapper name='D:\\Temp\\pyaedt_ansys.log' mode='a' encoding='cp1252'> self._logger = val .. GENERATED FROM PYTHON SOURCE LINES 38-41 Create results folder ~~~~~~~~~~~~~~~~~~~~ Create results folder. .. GENERATED FROM PYTHON SOURCE LINES 41-46 .. code-block:: Python results_folder = os.path.join(m2d.working_directory, "M2D_DC_Conduction") if not os.path.exists(results_folder): os.mkdir(results_folder) .. GENERATED FROM PYTHON SOURCE LINES 47-52 Import geometry as a DXF file ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ You can test importing a DXF or a Parasolid file by commenting/uncommenting the following lines. Importing DXF files only works in graphical mode. .. GENERATED FROM PYTHON SOURCE LINES 52-60 .. code-block:: Python # DXFPath = pyaedt.downloads.download_file("dxf", "Ansys_logo_2D.dxf") # dxf_layers = m2d.get_dxf_layers(DXFPath) # m2d.import_dxf(DXFPath, dxf_layers, scale=1E-05) parasolid_path = pyaedt.downloads.download_file("x_t", "Ansys_logo_2D.x_t") m2d.modeler.import_3d_cad(parasolid_path) .. rst-class:: sphx-glr-script-out .. code-block:: none True .. GENERATED FROM PYTHON SOURCE LINES 61-65 Define variables ~~~~~~~~~~~~~~~~ Define conductor thickness in z-direction, material array with 4 materials, and MaterialIndex referring to the material array .. GENERATED FROM PYTHON SOURCE LINES 65-73 .. code-block:: Python m2d["MaterialThickness"] = "5mm" m2d["ConductorMaterial"] = "[\"Copper\", \"Aluminum\", \"silver\", \"gold\"]" MaterialIndex = 0 m2d["MaterialIndex"] = str(MaterialIndex) no_materials = 4 .. GENERATED FROM PYTHON SOURCE LINES 74-78 Assign materials ~~~~~~~~~~~~~~~~ A voltage port is defined as a perfect electric conductor (pec). A conductor gets the material defined by the 0th entry of the material array. .. GENERATED FROM PYTHON SOURCE LINES 78-82 .. code-block:: Python m2d.assign_material(["ANSYS_LOGO_2D_1", "ANSYS_LOGO_2D_2"], "gold") m2d.modeler["ANSYS_LOGO_2D_3"].material_name = "ConductorMaterial[MaterialIndex]" .. GENERATED FROM PYTHON SOURCE LINES 83-86 Assign voltages ~~~~~~~~~~~~~~~ 1V and 0V .. GENERATED FROM PYTHON SOURCE LINES 86-90 .. code-block:: Python m2d.assign_voltage(["ANSYS_LOGO_2D_1"], amplitude=1, name="1V") m2d.assign_voltage(["ANSYS_LOGO_2D_2"], amplitude=0, name="0V") .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 91-94 Setup conductance calculation ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 1V is the source, 0V ground .. GENERATED FROM PYTHON SOURCE LINES 94-97 .. code-block:: Python m2d.assign_matrix(assignment=['1V'], matrix_name="Matrix1", group_sources=['0V']) .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 98-101 Assign mesh operation ~~~~~~~~~~~~~~~~~~~~~ 3mm on the conductor .. GENERATED FROM PYTHON SOURCE LINES 101-104 .. code-block:: Python m2d.mesh.assign_length_mesh(["ANSYS_LOGO_2D_3"], maximum_length=3, maximum_elements=None, name="conductor") .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 105-109 Create simulation setup and enable expression cache ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Create simulation setup with minimum 4 adaptive passes to ensure convergence. Enable expression cache to observe the convergence. .. GENERATED FROM PYTHON SOURCE LINES 109-119 .. code-block:: Python setup1 = m2d.create_setup(name="Setup1", MinimumPasses=4) setup1.enable_expression_cache( # doesn't work? report_type="DCConduction", expressions="1/Matrix1.G(1V,1V)/MaterialThickness", isconvergence=True, conv_criteria=1, use_cache_for_freq=False) setup1.analyze() .. GENERATED FROM PYTHON SOURCE LINES 120-124 Create parametric sweep ~~~~~~~~~~~~~~~~~~~~~~~ Create parametric sweep to sweep all the entries in the material array. Save fields and mesh and use the mesh for all the materials. .. GENERATED FROM PYTHON SOURCE LINES 124-133 .. code-block:: Python param_sweep = m2d.parametrics.add( "MaterialIndex", 0, no_materials-1, 1, "LinearStep", parametricname="MaterialSweep") param_sweep["SaveFields"] = True param_sweep["CopyMesh"] = True param_sweep["SolveWithCopiedMeshOnly"] = True param_sweep.analyze() .. rst-class:: sphx-glr-script-out .. code-block:: none True .. GENERATED FROM PYTHON SOURCE LINES 134-137 Create resistance report ~~~~~~~~~~~~~~~~~~~~~~~~ Create R. vs. material report .. GENERATED FROM PYTHON SOURCE LINES 137-143 .. code-block:: Python variations = {"MaterialIndex": ["All"], "MaterialThickness": ["Nominal"]} report = m2d.post.create_report(expressions="1/Matrix1.G(1V,1V)/MaterialThickness", variations=variations, primary_sweep_variable="MaterialIndex", report_category="DCConduction", plot_type="Data Table", plot_name="Resistance vs. Material") .. GENERATED FROM PYTHON SOURCE LINES 144-148 Get solution data ~~~~~~~~~~~~~~~~~ Get solution data using the object ``report``` to get resistance values and plot data outside AEDT. .. GENERATED FROM PYTHON SOURCE LINES 148-155 .. code-block:: Python d = report.get_solution_data() resistence = d.data_magnitude() material_index = d.primary_sweep_values d.primary_sweep = "MaterialIndex" d.plot(snapshot_path=os.path.join(results_folder, "M2D_DCConduction.jpg")) .. image-sg:: /examples/03-Maxwell/images/sphx_glr_Maxwell2D_DCConduction_001.png :alt: Simulation Results Plot :srcset: /examples/03-Maxwell/images/sphx_glr_Maxwell2D_DCConduction_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none No artists with labels found to put in legend. Note that artists whose label start with an underscore are ignored when legend() is called with no argument.
.. GENERATED FROM PYTHON SOURCE LINES 156-160 Create material index vs resistance table ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Create material index vs resistance table to use in PDF report generator. Create ``colors`` table to customize each row of the material index vs resistance table. .. GENERATED FROM PYTHON SOURCE LINES 160-167 .. code-block:: Python material_index_vs_resistance = [["Material", "Resistance"]] colors = [[(255, 255, 255), (0, 255, 0)]] for i in range(len(d.primary_sweep_values)): material_index_vs_resistance.append([str(d.primary_sweep_values[i]), str(resistence[i])]) colors.append([None, None]) .. GENERATED FROM PYTHON SOURCE LINES 168-171 Field overlay ~~~~~~~~~~~~~ Plot electric field and current density on the conductor surface .. GENERATED FROM PYTHON SOURCE LINES 171-176 .. code-block:: Python conductor_surface = m2d.modeler["ANSYS_LOGO_2D_3"].faces plot1 = m2d.post.create_fieldplot_surface(conductor_surface, "Mag_E", plot_name="Electric Field") plot2 = m2d.post.create_fieldplot_surface(conductor_surface, "Mag_J", plot_name="Current Density") .. GENERATED FROM PYTHON SOURCE LINES 177-180 Field overlay ~~~~~~~~~~~~~ Plot electric field using pyvista and saving to an image .. GENERATED FROM PYTHON SOURCE LINES 180-190 .. code-block:: Python py_vista_plot = m2d.post.plot_field("Mag_E", conductor_surface, show=False, plot_cad_objs=False) py_vista_plot.isometric_view = False py_vista_plot.camera_position = [0, 0, 7] py_vista_plot.focal_point = [0, 0, 0] py_vista_plot.roll_angle = 0 py_vista_plot.elevation_angle = 0 py_vista_plot.azimuth_angle = 0 py_vista_plot.plot(os.path.join(results_folder, "mag_E.jpg")) .. image-sg:: /examples/03-Maxwell/images/sphx_glr_Maxwell2D_DCConduction_002.png :alt: Maxwell2D DCConduction :srcset: /examples/03-Maxwell/images/sphx_glr_Maxwell2D_DCConduction_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none True .. GENERATED FROM PYTHON SOURCE LINES 191-194 Field animation ~~~~~~~~~~~~~~~ Plot current density vs the Material index. .. GENERATED FROM PYTHON SOURCE LINES 194-207 .. code-block:: Python animated = m2d.post.plot_animated_field(quantity="Mag_J", assignment=conductor_surface, variation_variable="MaterialIndex", variations=[0, 1, 2, 3], show=False, log_scale=True, export_gif=False, export_path=results_folder) animated.isometric_view = False animated.camera_position = [0, 0, 7] animated.focal_point = [0, 0, 0] animated.roll_angle = 0 animated.elevation_angle = 0 animated.azimuth_angle = 0 animated.animate() .. image-sg:: /examples/03-Maxwell/images/sphx_glr_Maxwell2D_DCConduction_003.png :alt: Maxwell2D DCConduction :srcset: /examples/03-Maxwell/images/sphx_glr_Maxwell2D_DCConduction_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none C:\actions-runner\_work\pyaedt\pyaedt\.venv\lib\site-packages\pyvista\plotting\plotter.py:4644: PyVistaDeprecationWarning: This method is deprecated and will be removed in a future version of PyVista. Directly modify the scalars of a mesh in-place instead. warnings.warn( True .. GENERATED FROM PYTHON SOURCE LINES 208-211 Export model picture ~~~~~~~~~~~~~~~~~~~~ Export model picture. .. GENERATED FROM PYTHON SOURCE LINES 211-214 .. code-block:: Python model_picture = m2d.post.export_model_picture() .. GENERATED FROM PYTHON SOURCE LINES 215-218 Generate PDF report ~~~~~~~~~~~~~~~~~~~ Generate a PDF report with output of simulation. .. GENERATED FROM PYTHON SOURCE LINES 218-268 .. code-block:: Python pdf_report = AnsysReport(version=aedt_version, design_name=m2d.design_name, project_name=m2d.project_name) # Customize text font. pdf_report.report_specs.font = "times" pdf_report.report_specs.text_font_size = 10 # Create report pdf_report.create() # Add project's design info to report. pdf_report.add_project_info(m2d) # Add model picture in a new chapter and add text. pdf_report.add_chapter("Model Picture") pdf_report.add_text("This section contains the model picture") pdf_report.add_image(model_picture, "Model Picture", width=80, height=60) # Add in a new chapter field overlay plots. pdf_report.add_chapter("Field overlay") pdf_report.add_sub_chapter("Plots") pdf_report.add_text("This section contains the fields overlay.") pdf_report.add_image(os.path.join(results_folder, "mag_E.jpg"), "Mag E", width=120, height=80) pdf_report.add_page_break() # Add a new section to display results. pdf_report.add_section() pdf_report.add_chapter("Results") pdf_report.add_sub_chapter("Resistance vs. Material") pdf_report.add_text("This section contains resistance vs material data.") # Aspect ratio is automatically calculated if only width is provided pdf_report.add_image(os.path.join(results_folder, "M2D_DCConduction.jpg"), width=130) # Add a new subchapter to display resistance data from previously created table. pdf_report.add_sub_chapter("Resistance data table") pdf_report.add_text("This section contains Resistance data.") pdf_report.add_table("Resistance Data", content=material_index_vs_resistance, formatting=colors, col_widths=[75, 100]) # Add table of content and save PDF. pdf_report.add_toc() pdf_report.save_pdf(results_folder, "AEDT_Results.pdf") .. image-sg:: /examples/03-Maxwell/images/sphx_glr_Maxwell2D_DCConduction_004.png :alt: Maxwell2D DCConduction :srcset: /examples/03-Maxwell/images/sphx_glr_Maxwell2D_DCConduction_004.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none 'C:/Users/ansys/Documents/Ansoft/M2D_DC_Conduction.pyaedt\\Ansys_resistor\\M2D_DC_Conduction\\AEDT_Results.pdf' .. GENERATED FROM PYTHON SOURCE LINES 269-271 Release desktop ~~~~~~~~~~~~~~~ .. GENERATED FROM PYTHON SOURCE LINES 271-273 .. code-block:: Python m2d.release_desktop() .. rst-class:: sphx-glr-script-out .. code-block:: none True .. rst-class:: sphx-glr-timing **Total running time of the script:** (1 minutes 57.803 seconds) .. _sphx_glr_download_examples_03-Maxwell_Maxwell2D_DCConduction.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: Maxwell2D_DCConduction.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: Maxwell2D_DCConduction.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_