### Start MAPDL and Enter Pre-processing Source: https://github.com/pbxie/pymapdl-example/blob/master/examples/01-geometry/04-primitives.txt Launches an instance of MAPDL and enters the pre-processing routine (prep7). This is a common setup step before defining geometry. ```Python from ansys.mapdl.core import launch_mapdl # start MAPDL and enter the pre-processing routine mapdl = launch_mapdl() mapdl.clear() mapdl.prep7() print(mapdl) ``` -------------------------------- ### Download and Run MAPDL Input Script Source: https://github.com/pbxie/pymapdl-example/blob/master/examples/00-mapdl-examples/spotweld.txt Downloads a specified MAPDL input file ('spotweld.inp') from example data and executes it within the current MAPDL session. This loads the simulation setup. ```python from ansys.mapdl.core.examples.downloads import download_example_data spotweld_data = download_example_data( filename="spotweld.inp", directory="pymapdl/spotweld" ) mapdl.input(spotweld_data) ``` -------------------------------- ### Launch MAPDL and Enter Preprocessing Source: https://github.com/pbxie/pymapdl-example/blob/master/examples/01-geometry/00-keypoints.txt Starts an instance of MAPDL and enters the preprocessing routine (PREP7). This is a foundational step for most geometry creation tasks. ```Python from ansys.mapdl.core import launch_mapdl # start MAPDL and enter the pre-processing routine mapdl = launch_mapdl() mapdl.clear() mapdl.prep7() print(mapdl) ``` -------------------------------- ### Launch MAPDL Service Source: https://github.com/pbxie/pymapdl-example/blob/master/examples/00-mapdl-examples/3d_plate_thermal.txt Starts MAPDL as a service and disables all but error messages. This is the initial setup step for using PyMAPDL. ```Python from ansys.mapdl.core import launch_mapdl mapdl = launch_mapdl() ``` -------------------------------- ### Launch MAPDL Service Source: https://github.com/pbxie/pymapdl-example/blob/master/examples/00-mapdl-examples/cyclic_analysis.txt Starts MAPDL as a service to enable interaction with the solver. This is a prerequisite for most PyMAPDL operations. ```Python from ansys.mapdl.core import launch_mapdl mapdl = launch_mapdl() ``` -------------------------------- ### Launch MAPDL Service Source: https://github.com/pbxie/pymapdl-example/blob/master/examples/02-tips-n-tricks/01-smoothing-element-size-transitions.txt Starts MAPDL as a service using the `launch_mapdl` function from the `ansys.mapdl.core` library. This is the initial step to interact with MAPDL. ```Python from ansys.mapdl.core import launch_mapdl mapdl = launch_mapdl() print(mapdl) ``` -------------------------------- ### Setup Transient Analysis and Loads Source: https://github.com/pbxie/pymapdl-example/blob/master/examples/00-mapdl-examples/transient_thermal.txt Configures the transient analysis by setting time points for results, controlling output, selecting a solver, and applying initial conditions and convective loads. ```Python mapdl.parameters["my_tsres"] = [120, 130, 500, 700, 710, 900, end_time] mapdl.tsres("%my_tsres%") mapdl.outres("ERASE") mapdl.outres("ALL", "ALL") mapdl.eqslv("SPARSE") # use sparse solver mapdl.tunif(75) # force uniform starting temperature (otherwise zero) # apply the convective load (convection coefficient plus bulk temperature) # use "%" around table array names mapdl.sfa(6, 1, "CONV", "%my_conv%", " %my_bulk%") # solve mapdl.solve() ``` -------------------------------- ### Setup Solution in PyMAPDL Source: https://github.com/pbxie/pymapdl-example/blob/master/examples/02-tips-n-tricks/03-using_inline_functions_and_Query.txt Configures the solution phase of the analysis. It sets the analysis type to static, selects all nodes, solves the model, and then finishes the solution process. ```Python mapdl.run("/SOLU") mapdl.antype("STATIC") mapdl.allsel() mapdl.solve() mapdl.finish(mute=True) ``` -------------------------------- ### Launch MAPDL Instance Source: https://github.com/pbxie/pymapdl-example/blob/master/examples/00-mapdl-examples/2d_pressure_vessel.txt Starts an instance of MAPDL using the `launch_mapdl` function from the `ansys.mapdl.core` library. This is the initial step to interact with MAPDL. ```Python from ansys.mapdl.core import launch_mapdl # start mapdl mapdl = launch_mapdl() ``` -------------------------------- ### Create Cube Volume using Keypoints (PyMAPDL) Source: https://github.com/pbxie/pymapdl-example/blob/master/examples/01-geometry/03-volumes.txt This snippet shows how to define a cube volume by creating eight keypoints and then using the `v` command to generate the volume from these keypoints. It utilizes the `launch_mapdl` function to start a MAPDL session and the `prep7` routine for preprocessing. ```Python from ansys.mapdl.core import launch_mapdl # start MAPDL and enter the pre-processing routine mapdl = launch_mapdl() mapdl.clear() mapdl.prep7() # Create a simple cube volume. k0 = mapdl.k("", 0, 0, 0) k1 = mapdl.k("", 1, 0, 0) k2 = mapdl.k("", 1, 1, 0) k3 = mapdl.k("", 0, 1, 0) k4 = mapdl.k("", 0, 0, 1) k5 = mapdl.k("", 1, 0, 1) k6 = mapdl.k("", 1, 1, 1) k7 = mapdl.k("", 0, 1, 1) v0 = mapdl.v(k0, k1, k2, k3, k4, k5, k6, k7) mapdl.vplot(show_lines=True) ``` -------------------------------- ### Basic PyMAPDL Operations Source: https://github.com/pbxie/pymapdl-example/blob/master/examples/02-tips-n-tricks/00-example-template.txt Illustrates fundamental PyMAPDL operations including clearing the session, entering the preparation phase (prep7), and printing the MAPDL object's representation. This is a common starting point for PyMAPDL scripts. ```python mapdl.clear() mapdl.prep7() print(mapdl) ``` -------------------------------- ### Setup Mesh in PyMAPDL Source: https://github.com/pbxie/pymapdl-example/blob/master/examples/02-tips-n-tricks/03-using_inline_functions_and_Query.txt Configures the simulation mesh by assigning a SOLID5 element type, creating a block geometry, setting the element size, meshing the block, and plotting the resulting elements. ```Python mapdl.et(1, "SOLID5") mapdl.block(0, 10, 0, 20, 0, 30) mapdl.esize(2) mapdl.vmesh("ALL") mapdl.eplot() ``` -------------------------------- ### Generate Volumes using VSYMM (Symmetry) Source: https://github.com/pbxie/pymapdl-example/blob/master/examples/01-geometry/03-volumes.txt This example shows how to create volumes using the VSYMM command to reflect a block across the YZ and XZ planes. It starts by creating a block and then applies symmetry operations, followed by a plot. ```Python mapdl.clear() mapdl.prep7() vnum = mapdl.blc4(1, 1, 1, 1, depth=1) mapdl.vsymm("X", vnum) mapdl.vsymm("Y", "ALL") mapdl.vplot(show_lines=True, show_bounds=True) ``` -------------------------------- ### Setup Transient Analysis Source: https://github.com/pbxie/pymapdl-example/blob/master/examples/00-mapdl-examples/transient_thermal.txt Configures the solver for a transient analysis. It sets the analysis type to transient, specifies a full transient solution, and enables automatic time stepping with defined substep parameters. ```Python mapdl.run("/SOLU") mapdl.antype(4) # transient analysis mapdl.trnopt("FULL") # full transient analysis mapdl.kbc(0) # ramp loads up and down # Time stepping end_time = 1500 mapdl.time(end_time) # end time for load step mapdl.autots("ON") # use automatic time stepping # setup where the subset time is 10 seconds, time mapdl.deltim(10, 2, 25) # substep size (seconds) ``` -------------------------------- ### Launch MAPDL and Set Units Source: https://github.com/pbxie/pymapdl-example/blob/master/examples/00-mapdl-examples/pressure_vessel.txt Starts an instance of MAPDL and sets the analysis units to US Customary (inches). This is a foundational step for any simulation. ```Python from ansys.mapdl.core import launch_mapdl # start mapdl, enter the preprocessor, and set the units mapdl = launch_mapdl() mapdl.clear() mapdl.prep7() # US Customary system using inches (in, lbf*s2/in, s, °F). mapdl.units("BIN") ``` -------------------------------- ### PyMAPDL Geometry and Plotting Source: https://github.com/pbxie/pymapdl-example/blob/master/examples/02-tips-n-tricks/00-example-template.txt Shows how to create a simple block geometry using PyMAPDL's 'block' command and then visualize it using the 'vplot' command. This is essential for previewing model geometry during analysis setup. ```python mapdl.block(0, 1, 0, 1, 0, 1) mapdl.vplot() ``` -------------------------------- ### PyMAPDL Example Header Block Source: https://github.com/pbxie/pymapdl-example/blob/master/examples/02-tips-n-tricks/00-example-template.txt This snippet shows the required header block for a new PyMAPDL gallery example. It includes a reference tag (e.g., `.. _ref_my_example:`) and a title for the example. ```Python """ .. _ref_my_example: ============================ My new example title ============================ This new example... """ ``` -------------------------------- ### Setup and Run Cyclic Modal Analysis (20 Sectors) Source: https://github.com/pbxie/pymapdl-example/blob/master/examples/00-mapdl-examples/cyclic_analysis.txt Clears MAPDL, prepares the geometry for a 20-sector model, applies cyclic conditions, meshes the model, assigns material properties, and runs a modal analysis. ```Python mapdl.clear() mapdl.prep7() # Generate a single sector of a 20 sector model gen_sector(mapdl, 20) # make it cyclic mapdl.cyclic() # Mesh it esize = 0.001 mapdl.et(1, 186) mapdl.esize(esize) mapdl.vsweep("all") # apply materials mapdl.mp("EX", 1, 210e9) # Elastic moduli in Pa (kg/(m*s**2)) mapdl.mp("DENS", 1, 7800) # Density in kg/m3 mapdl.mp("NUXY", 1, 0.3) # Poisson's Ratio mapdl.emodif("ALL", "MAT", 1) # Run the modal analysis output = mapdl.modal_analysis(nmode=6, freqb=1) # grab the result object from MAPDL result = mapdl.result print(result) ``` -------------------------------- ### Create Triangular Prism Volume using Keypoints (PyMAPDL) Source: https://github.com/pbxie/pymapdl-example/blob/master/examples/01-geometry/03-volumes.txt This example demonstrates creating a triangular prism volume by defining keypoints and using the `v` command. It clears previous geometry, enters the preprocessing routine, defines the necessary keypoints, creates the volume, and plots it. ```Python mapdl.clear() mapdl.prep7() k0 = mapdl.k("", 0, 0, 0) k1 = mapdl.k("", 1, 0, 0) k2 = mapdl.k("", 1, 1, 0) k3 = mapdl.k("", 0, 1, 0) k4 = mapdl.k("", 0, 0, 1) k5 = mapdl.k("", 1, 0, 1) k6 = mapdl.k("", 1, 1, 1) k7 = mapdl.k("", 0, 1, 1) v1 = mapdl.v(k0, k1, k2, k2, k4, k5, k6, k6) mapdl.vplot(show_lines=True) ``` -------------------------------- ### Launch MAPDL and Load Mesh Source: https://github.com/pbxie/pymapdl-example/blob/master/examples/00-mapdl-examples/cyclic_static.txt Launches an instance of MAPDL and loads an example rotor sector mesh from the PyMAPDL examples. It then plots the loaded mesh. ```Python from ansys.mapdl.reader import examples from ansys.mapdl.core import launch_mapdl # launch mapdl mapdl = launch_mapdl() ############################################################################### # Load in the mesh # ~~~~~~~~~~~~~~~~ # Load in the example sector and plot it. mapdl.cdread("db", examples.sector_archive_file) mapdl.eplot() ``` -------------------------------- ### Setup Boundary Conditions in PyMAPDL Source: https://github.com/pbxie/pymapdl-example/blob/master/examples/02-tips-n-tricks/03-using_inline_functions_and_Query.txt Applies material properties (elastic modulus and Poisson's ratio) and boundary conditions. It fixes nodes at one end of the block by constraining all degrees of freedom and applies a force to nodes at the other end, then finishes preprocessing. ```Python mapdl.mp("EX", 1, 210e9) mapdl.mp("PRXY", 1, 0.3) mapdl.nsel("S", "LOC", "Z", 30) mapdl.d("ALL", "UX") mapdl.d("ALL", "UY") mapdl.d("ALL", "UZ") mapdl.nsel("S", "LOC", "Z", 0) mapdl.f("ALL", "FX", 10000) mapdl.finish() ``` -------------------------------- ### RST Ordered List Example Source: https://github.com/pbxie/pymapdl-example/blob/master/examples/02-tips-n-tricks/00-example-template.txt Shows the reStructuredText syntax for creating an ordered list. ```RST #. Item 1 #. Item 2 ``` -------------------------------- ### Load and Print Geometry Source: https://github.com/pbxie/pymapdl-example/blob/master/examples/00-mapdl-examples/geometry.txt Downloads an example bracket IGES file, loads it into the AUX15 process in MAPDL, and prints the geometry information. The `download_bracket` function provides the file path. ```Python # note that this method just returns a file path bracket_file = examples.download_bracket() # load the bracket and then print out the geometry mapdl.aux15() mapdl.igesin(bracket_file) print(mapdl.geometry) ``` -------------------------------- ### RST Unordered List Example Source: https://github.com/pbxie/pymapdl-example/blob/master/examples/02-tips-n-tricks/00-example-template.txt Shows the reStructuredText syntax for creating an unordered list. ```RST - Item 1 - Item 2 ``` -------------------------------- ### RST Equation Example Source: https://github.com/pbxie/pymapdl-example/blob/master/examples/02-tips-n-tricks/00-example-template.txt Demonstrates how to include a mathematical equation in reStructuredText using the `.. math::` directive. ```RST .. math:: f(x) = x^2 + 2x + 1 ``` -------------------------------- ### Create Cylinder by Extruding Area (PyMAPDL) Source: https://github.com/pbxie/pymapdl-example/blob/master/examples/01-geometry/03-volumes.txt This example demonstrates creating a cylinder by extruding a circular area along the Z-axis using the `vext` command. It involves creating a circle, defining an area from it, and then extruding this area by a specified distance. ```Python mapdl.clear() mapdl.prep7() # first, create an area from a circle k0 = mapdl.k("", 0, 0, 0) k1 = mapdl.k("", 0, 0, 1) k2 = mapdl.k("", 0, 0, 0.5) carc0 = mapdl.circle(k0, 1, k1) a0 = mapdl.al(*carc0) # next, extrude it and plot it mapdl.vext(a0, dz=4) mapdl.vplot(show_lines=True, quality=5) ``` -------------------------------- ### Create Tetrahedral Volume from Areas (PyMAPDL) Source: https://github.com/pbxie/pymapdl-example/blob/master/examples/01-geometry/03-volumes.txt This example demonstrates creating a tetrahedral volume bounded by four areas using the `va` command. It involves defining keypoints, creating areas from these keypoints, and then generating the volume from the specified areas. ```Python mapdl.clear() mapdl.prep7() k0 = mapdl.k("", -1, 0, 0) k1 = mapdl.k("", 1, 0, 0) k2 = mapdl.k("", 1, 1, 0) k3 = mapdl.k("", 1, 0.5, 1) # create faces a0 = mapdl.a(k0, k1, k2) a1 = mapdl.a(k0, k1, k3) a2 = mapdl.a(k1, k2, k3) a3 = mapdl.a(k0, k2, k3) # generate and plot the volume vnum = mapdl.va(a0, a1, a2, a3) mapdl.aplot(show_lines=True, show_bounds=True) ``` -------------------------------- ### APDL Command: V (Define Volume) Source: https://github.com/pbxie/pymapdl-example/blob/master/examples/01-geometry/03-volumes.txt The APDL `V` command is used to define a volume through keypoints. This example shows the APDL syntax for creating a cube by specifying its eight corner keypoints. ```APDL V,KP1,KP2,KP3,KP4,KP5,KP6,KP7,KP8 ``` -------------------------------- ### Launch PyMAPDL and Initialize Source: https://github.com/pbxie/pymapdl-example/blob/master/examples/00-mapdl-examples/acoustic_analysis.txt Launches an instance of PyMAPDL, clears existing data, sets the analysis to preprocessing mode, and configures units to SI. ```Python from ansys.mapdl.core import launch_mapdl mapdl = launch_mapdl() mapdl.clear() mapdl.prep7() mapdl.units("SI") ``` -------------------------------- ### Launch MAPDL and Access XPL - Python Source: https://github.com/pbxie/pymapdl-example/blob/master/examples/02-tips-n-tricks/05-explore_mapdl_files.txt Launches MAPDL as a service and accesses the XPL interface for exploring binary files. This is the initial setup for interacting with MAPDL's file exploration capabilities. ```Python from ansys.mapdl.core import launch_mapdl from ansys.mapdl.core.examples import vmfiles mapdl = launch_mapdl() xpl = mapdl.xpl help(xpl) ``` -------------------------------- ### Get Keypoint Coordinates Source: https://github.com/pbxie/pymapdl-example/blob/master/examples/01-geometry/00-keypoints.txt Retrieves the coordinates of all keypoints as a NumPy array using the `mapdl.geometry.get_keypoints()` method. ```Python # Keypoint Coordinates # Return an array of the keypoint locations kloc = mapdl.geometry.get_keypoints() kloc ``` -------------------------------- ### Get Keypoints Geometry Source: https://github.com/pbxie/pymapdl-example/blob/master/examples/01-geometry/00-keypoints.txt Retrieves the keypoints geometry as a VTK MultiBlock object using `mapdl.geometry.keypoints`. This object can be used for visualization or further processing. ```Python # Keypoints geometry # Get the VTK ``MultiBlock`` containing keypoints. This VTK mesh can be # saved or plotted. For more information, visit # `PyVista documentation `_. keypoints = mapdl.geometry.keypoints keypoints ``` -------------------------------- ### Launch MAPDL and Set Compression Source: https://github.com/pbxie/pymapdl-example/blob/master/examples/00-mapdl-examples/mapdl_beam.txt Launches an interactive MAPDL session and sets the file compression level. This is the initial step to interact with MAPDL. ```Python from ansys.mapdl.core import launch_mapdl mapdl = launch_mapdl() mapdl.fcomp("rst", 0) # specify compression level ``` -------------------------------- ### Get and Print Results Object Source: https://github.com/pbxie/pymapdl-example/blob/master/examples/00-mapdl-examples/cyclic_analysis.txt Retrieves the result object from MAPDL after a modal analysis and prints it. This object is used for further post-processing and visualization. ```Python result = mapdl.result print(result) ``` -------------------------------- ### Launch PyMAPDL and Initialize Source: https://github.com/pbxie/pymapdl-example/blob/master/examples/00-mapdl-examples/transient_thermal.txt Launches an instance of PyMAPDL and initializes the analysis environment by clearing previous data and entering the preprocessing section. ```Python from ansys.mapdl.core import launch_mapdl mapdl = launch_mapdl(loglevel="ERROR") mapdl.clear() mapdl.prep7() ``` -------------------------------- ### Launch MAPDL Instance with PyMAPDL Source: https://github.com/pbxie/pymapdl-example/blob/master/examples/02-tips-n-tricks/00-example-template.txt This snippet demonstrates how to launch an instance of MAPDL using the `launch_mapdl` function from the `ansys.mapdl.core` library. It initializes the MAPDL environment and prints the MAPDL object. ```Python from ansys.mapdl.core import launch_mapdl # start MAPDL mapdl = launch_mapdl() print(mapdl) ``` -------------------------------- ### Get Line IDs Source: https://github.com/pbxie/pymapdl-example/blob/master/examples/01-geometry/01-lines.txt Retrieves an array containing the IDs of all lines currently present in the geometry. This is accessed via the `lnum` attribute of `mapdl.geometry`. ```Python lnum = mapdl.geometry.lnum print(lnum) ``` -------------------------------- ### Launch MAPDL and Enter Pre-processor Source: https://github.com/pbxie/pymapdl-example/blob/master/examples/00-mapdl-examples/contact_elements.txt Launches an instance of MAPDL and enters the pre-processing environment. This is the initial step for any MAPDL operation. ```python from ansys.mapdl import core as pymapdl mapdl = pymapdl.launch_mapdl() ``` -------------------------------- ### Get Line Geometry Source: https://github.com/pbxie/pymapdl-example/blob/master/examples/01-geometry/01-lines.txt Retrieves the VTK 'MultiBlock' object containing the line geometry. This mesh can be used for plotting or saving, referencing the PyVista library for further details. ```Python lines = mapdl.geometry.lines print(lines) ``` -------------------------------- ### Launch MAPDL and Run Input Source: https://github.com/pbxie/pymapdl-example/blob/master/examples/00-mapdl-examples/basic_dpf_example.txt Launches an instance of MAPDL and runs a specified input file. It then downloads the result file to a temporary directory for further processing. ```Python from ansys.dpf.core import core from ansys.mapdl.core import launch_mapdl from ansys.mapdl.core.examples import vmfiles import tempfile mapdl = launch_mapdl() vm5 = vmfiles["vm5"] output = mapdl.input(vm5) print(output) # If you are working locally, you don't need to perform the following steps temp_directory = tempfile.gettempdir() # Downloading RST file to the current folder rst_path = mapdl.download_result(temp_directory) ``` -------------------------------- ### Initialize PyMAPDL Source: https://github.com/pbxie/pymapdl-example/blob/master/examples/00-mapdl-examples/torsional_load.txt Starts an Ansys PyMAPDL session in the current working directory with a default jobname. This is the initial step for any PyMAPDL script. ```Python import os from ansys.mapdl.core import launch_mapdl # start Ansys in the current working directory with default jobname "file" mapdl = launch_mapdl(run_location=os.getcwd(), version=23.1) ``` -------------------------------- ### Create Volume from Keypoints (PyMAPDL) Source: https://github.com/pbxie/pymapdl-example/blob/master/examples/01-geometry/03-volumes.txt This snippet illustrates creating a volume using the `v` command with a specific set of keypoints. It includes clearing the MAPDL environment, entering the preprocessing mode, defining keypoints, creating the volume, and plotting it. ```Python mapdl.clear() mapdl.prep7() k0 = mapdl.k("", 0, 0, 0) k1 = mapdl.k("", 1, 0, 0) k2 = mapdl.k("", 1, 1, 0) k3 = mapdl.k("", 0, 0, 1) v0 = mapdl.v(k0, k1, k2, k2, k3, k3, k3, k3) mapdl.vplot(show_lines=True) ``` -------------------------------- ### Post-Processing: Temperature at a Single Point Source: https://github.com/pbxie/pymapdl-example/blob/master/examples/00-mapdl-examples/transient_thermal.txt Retrieves the temperature at a specific node (e.g., Node 12) using the `get_value` command, which is analogous to APDL's `*GET` command. ```Python # for example, the temperature of Node 12 is can be retrieved simply with: mapdl.get_value("node", 12, "TEMP") ``` -------------------------------- ### Get Area Geometry as VTK Multiblock Source: https://github.com/pbxie/pymapdl-example/blob/master/examples/01-geometry/02-areas.txt Obtains the geometric data of areas as a VTK 'Multiblock' object, which can be used for further processing, saving, or plotting. This is accessed via `mapdl.geometry.areas`. ```Python areas = mapdl.geometry.areas print(areas) ``` -------------------------------- ### Launch MAPDL Service - PyMAPDL Source: https://github.com/pbxie/pymapdl-example/blob/master/examples/00-mapdl-examples/2d_magnetostatic_solenoid-BodyFlux_Averaging.txt Launches an instance of the MAPDL service using the PyMAPDL library. It then clears any existing data and enters the preprocessing section. ```Python from ansys.mapdl.core import launch_mapdl mapdl = launch_mapdl() mapdl.clear() mapdl.prep7() mapdl.title("2-D Solenoid Actuator Static Analysis") ``` -------------------------------- ### Get Nodal Principal Stress Source: https://github.com/pbxie/pymapdl-example/blob/master/examples/00-mapdl-examples/lathe_cutter.txt Retrieves all nodal principal stresses for the first principal component. This function is used to obtain the stress data for further analysis or plotting. ```Python mapdl.post_processing.nodal_principal_stress("1") ``` -------------------------------- ### Launch MAPDL and Load Archive Source: https://github.com/pbxie/pymapdl-example/blob/master/examples/00-mapdl-examples/mapdl_3d_beam.txt Launches MAPDL as a service and reads a beam archive file. It then selects elements and nodes for further processing and defines material properties for a dummy steel material. ```python from ansys.mapdl.reader import examples from ansys.mapdl.core import launch_mapdl mapdl = launch_mapdl() # load a beam stored as an example archive file and mesh it mapdl.cdread("db", examples.hexarchivefile) mapdl.esel("s", "ELEM", vmin=5, vmax=20) mapdl.cm("ELEM_COMP", "ELEM") mapdl.nsel("s", "NODE", vmin=5, vmax=20) mapdl.cm("NODE_COMP", "NODE") # boundary conditions mapdl.allsel() # dummy steel properties mapdl.prep7() mapdl.mp("EX", 1, 200e9) # Elastic moduli in Pa (kg/(m*s**2)) mapdl.mp("DENS", 1, 7800) # Density in kg/m3 mapdl.mp("NUXY", 1, 0.3) # Poissons Ratio mapdl.emodif("ALL", "MAT", 1) # fix one end of the beam mapdl.nsel("S", "LOC", "Z") mapdl.d("all", "all") mapdl.allsel() # plot the boundary conditions mapdl.nplot(plot_bc=True) ``` -------------------------------- ### Initialize PyMAPDL Session Source: https://github.com/pbxie/pymapdl-example/blob/master/examples/00-mapdl-examples/spotweld.txt Launches an MAPDL session using the PyMAPDL library. This is the first step to interact with MAPDL programmatically. ```python from ansys.mapdl.core import launch_mapdl mapdl = launch_mapdl() ``` -------------------------------- ### Select Line and Get Line Number Source: https://github.com/pbxie/pymapdl-example/blob/master/examples/00-mapdl-examples/bracket_static.txt Selects a line based on its Y-location and retrieves its maximum number using mapdl.get(). This is useful for identifying and referencing specific lines in the model. ```Python line1 = mapdl.lsel("S", "LOC", "Y", box1[2]) l1 = mapdl.get("line1", "LINE", 0, "NUM", "MAX") ``` -------------------------------- ### Launch MAPDL Service and Import Packages Source: https://github.com/pbxie/pymapdl-example/blob/master/examples/00-mapdl-examples/composite_dcb.txt This snippet imports necessary libraries such as os, tempfile, ansys.dpf.core, matplotlib.pyplot, numpy, pyvista, and ansys.mapdl.core. It then launches an instance of MAPDL as a service and prints its status. ```Python import os import tempfile from ansys.dpf import core as dpf import matplotlib.pyplot as plt import numpy as np import pyvista as pv from ansys.mapdl.core import launch_mapdl # Start MAPDL as a service mapdl = launch_mapdl() print(mapdl) ``` -------------------------------- ### Select Second Line and Get Line Number Source: https://github.com/pbxie/pymapdl-example/blob/master/examples/00-mapdl-examples/bracket_static.txt Selects a second line based on its X-location and retrieves its maximum number using mapdl.get(). This is a prerequisite for creating a fillet between two lines. ```Python line2 = mapdl.lsel("S", "LOC", "X", box2[0]) l2 = mapdl.get("line2", "LINE", 0, "NUM", "MAX") ``` -------------------------------- ### Get Area Geometry as PyVista PolyData Source: https://github.com/pbxie/pymapdl-example/blob/master/examples/01-geometry/02-areas.txt Retrieves area geometry as 'pyvista.PolyData' objects, allowing for selection of mesh quality and output format (merged or individual). This method is called using `mapdl.geometry.get_areas()`. ```Python area = mapdl.geometry.get_areas(quality=3) print(area) # optionally save the area, or plot it # area.save('mesh.vtk') # area.plot() ``` -------------------------------- ### Initialize and Configure PyMAPDL Source: https://github.com/pbxie/pymapdl-example/blob/master/examples/00-mapdl-examples/lathe_cutter.txt Sets up PyMAPDL, defines parameters, and configures units and title for the analysis. ```python pressure_length = mapdl.parameters["PRESS_LENGTH"] print(mapdl.parameters) mapdl.units("Bin") mapdl.title("Lathe Cutter") ``` -------------------------------- ### Select Nodes and Get Principal Stresses Source: https://github.com/pbxie/pymapdl-example/blob/master/examples/00-mapdl-examples/lathe_cutter.txt Selects nodes within a specified range of minimum and maximum values, then retrieves their principal nodal stresses. This is useful for analyzing stresses in a specific region of the model. ```Python mapdl.nsel("S", vmin=1200, vmax=1210) mapdl.esln() mapdl.nsle() print("The node numbers are:") print(mapdl.mesh.nnum) # get node numbers print("The principal nodal stresses are:") mapdl.post_processing.nodal_principal_stress("1") ``` -------------------------------- ### Launch MAPDL and Set Up Beam Model Source: https://github.com/pbxie/pymapdl-example/blob/master/examples/00-mapdl-examples/path_operations.txt Launches MAPDL as a service and configures a simple 3D beam model. It sets material properties, defines the beam geometry using `blc4`, applies mesh density, and constrains one end while applying a non-uniform load to the other. The model is then solved for a static analysis. ```python import matplotlib.pyplot as plt import numpy as np import pyvista as pv from ansys.mapdl.core import launch_mapdl mapdl = launch_mapdl(loglevel="ERROR") # beam dimensions width_ = 0.5 height_ = 2 length_ = 10 # simple 3D beam mapdl.clear() mapdl.prep7() mapdl.mp("EX", 1, 70000) mapdl.mp("NUXY", 1, 0.3) mapdl.csys(0) mapdl.blc4(0, 0, 0.5, 2, length_) mapdl.et(1, "SOLID186") mapdl.type(1) mapdl.keyopt(1, 2, 1) mapdl.desize("", 100) mapdl.vmesh("ALL") # mapdl.eplot() # fixed constraint mapdl.nsel("s", "loc", "z", 0) mapdl.d("all", "ux", 0) mapdl.d("all", "uy", 0) mapdl.d("all", "uz", 0) # arbitrary non-uniform load mapdl.nsel("s", "loc", "z", length_) mapdl.f("all", "fz", 1) mapdl.f("all", "fy", 10) mapdl.nsel("r", "loc", "y", 0) mapdl.f("all", "fx", 10) mapdl.allsel() mapdl.run("/solu") sol_output = mapdl.solve() # plot the normalized global displacement mapdl.post_processing.plot_nodal_displacement(lighting=False, show_edges=True) ``` -------------------------------- ### Launch MAPDL Instance Source: https://github.com/pbxie/pymapdl-example/blob/master/examples/00-mapdl-examples/geometry.txt Launches an instance of MAPDL using the PyMAPDL library. This is the initial step to interact with MAPDL functionalities. ```Python import numpy as np from ansys.mapdl import core as pymapdl from ansys.mapdl.core import examples from ansys.mapdl.core.plotting import MapdlTheme mapdl = pymapdl.launch_mapdl() ``` -------------------------------- ### Create Half Hollow Sphere with PyMAPDL Source: https://github.com/pbxie/pymapdl-example/blob/master/examples/01-geometry/04-primitives.txt Creates a half hollow sphere using the `sphere` command, specifying inner and outer radii, and start and end angles. The `vplot` command is used to display the generated spherical volume. ```Python mapdl.clear() mapdl.prep7() vnum = mapdl.sphere(rad1=0.95, rad2=1.0, theta1=90, theta2=270) mapdl.vplot(show_lines=False, quality=4, show_bounds=True) ``` -------------------------------- ### Launch MAPDL Instance Source: https://github.com/pbxie/pymapdl-example/blob/master/examples/00-mapdl-examples/lathe_cutter.txt Launches an instance of MAPDL using the PyMAPDL library. It demonstrates how to use the `launch_mapdl` function, including specifying additional switches like '-smp' for minimal solver files and managing the run location. ```Python import os import numpy as np from ansys.mapdl.core import launch_mapdl from ansys.mapdl.core.examples.downloads import download_example_data from ansys.mapdl.core.plotting import GraphicsBackend # cwd = current working directory path = os.getcwd() PI = np.PI EXX = 1.0e7 NU = 0.27 mapdl = launch_mapdl(additional_switches="-smp") ``` -------------------------------- ### Obtain Von Mises Stresses with PyMAPDL Source: https://github.com/pbxie/pymapdl-example/blob/master/examples/00-mapdl-examples/bracket_static.txt Retrieves nodal stress data and extracts the von Mises stress values. The `principal_nodal_stress()` command is used to get the stress results, and the last column is identified as von Mises stress. ```Python nnum, stress = result.principal_nodal_stress(0) # Von Mises stress is the last column in the stress results von_mises = stress[:, -1] ``` -------------------------------- ### Launch MAPDL Instance Source: https://github.com/pbxie/pymapdl-example/blob/master/examples/00-mapdl-examples/modal_beam.txt Launches an instance of MAPDL with interactive plotting enabled. This is the initial step to interact with the MAPDL solver. ```Python from ansys.mapdl.core import launch_mapdl nmodes = 10 # start MAPDL mapdl = launch_mapdl() print(mapdl) ``` -------------------------------- ### Create Cylindrical Volume Centered at Origin with PyMAPDL Source: https://github.com/pbxie/pymapdl-example/blob/master/examples/01-geometry/04-primitives.txt Generates a hollow cylindrical volume centered about the working plane origin using the `cylind` command. This function takes inner and outer radii, and Z-coordinates for the start and end of the cylinder's height. ```Python mapdl.clear() mapdl.prep7() vnum = mapdl.cylind(0.9, 1, z1=0, z2=5) mapdl.vplot(show_lines=False, quality=4, show_bounds=True) ``` -------------------------------- ### Open and Explore a MAPDL File - Python Source: https://github.com/pbxie/pymapdl-example/blob/master/examples/02-tips-n-tricks/05-explore_mapdl_files.txt Opens a MAPDL result file (e.g., .RST) and demonstrates listing available records within the file. It first runs a verification manual input file to create the result file. ```Python mapdl.input(vmfiles["vm1"]) print(xpl.open("file.rst")) print(xpl.list()) ``` -------------------------------- ### Get Principal Stresses as List, Array, and DataFrame Source: https://github.com/pbxie/pymapdl-example/blob/master/examples/00-mapdl-examples/lathe_cutter.txt Demonstrates how to retrieve nodal principal stresses using `mapdl.prnsol` and convert them into different data structures: a Python list, a NumPy array, and a Pandas DataFrame. This facilitates various data manipulation and analysis tasks. ```Python print(mapdl.prnsol("S", "PRIN")) mapdl_s_1_list = mapdl.prnsol("S", "PRIN").to_list() print(mapdl_s_1_list) mapdl_s_1_array = mapdl.prnsol("S", "PRIN").to_array() print(mapdl_s_1_array) mapdl_s_1_df = mapdl.prnsol("S", "PRIN").to_dataframe() mapdl_s_1_df.head() ``` -------------------------------- ### Get Principal Nodal Stress with PyMAPDL Source: https://github.com/pbxie/pymapdl-example/blob/master/examples/00-mapdl-examples/pressure_vessel.txt Retrieves principal nodal stress values from MAPDL using the `principal_nodal_stress` method. It then extracts the von-Mises stress, which is typically the last column, and calculates the minimum and maximum values. This section also includes a check for equivalence with pre-defined MAPDL results. ```Python nnum, stress = result.principal_nodal_stress(0) von_mises = stress[:, -1] # von-Mises stress is the right most column min_von_mises, max_von_mises = np.min(von_mises), np.max(von_mises) print("All close:", np.allclose(von_mises, von_mises_mapdl)) ``` -------------------------------- ### Run Static Analysis and Solve Source: https://github.com/pbxie/pymapdl-example/blob/master/examples/00-mapdl-examples/mapdl_beam.txt Configures and executes a static structural analysis. It initiates the solution process after all pre-processing steps are completed. ```Python mapdl.run("/solu") mapdl.antype("static") print(mapdl.solve()) ``` -------------------------------- ### Mesh Setup for Shell Element Source: https://github.com/pbxie/pymapdl-example/blob/master/examples/02-tips-n-tricks/04-rotational-displacement.txt Configures the mesh for a shell element simulation. It defines the element type as SHELL181, sets material properties (elastic modulus and Poisson's ratio), defines the section properties (shell type and thickness), sets the element size, meshes the geometry, and plots the resulting mesh. ```Python mapdl.et(1, "SHELL181") mapdl.mp("EX", 1, 2e5) mapdl.mp("PRXY", 1, 0.3) mapdl.rectng(0, 1, 0, 1) mapdl.sectype(1, "SHELL") mapdl.secdata(0.1) mapdl.esize(0.2) mapdl.amesh("all") mapdl.eplot() ``` -------------------------------- ### Navigate MAPDL File Structure - Python Source: https://github.com/pbxie/pymapdl-example/blob/master/examples/02-tips-n-tricks/05-explore_mapdl_files.txt Demonstrates navigating the hierarchical structure of a MAPDL binary file using 'step' to go down into branches and 'up' to return to parent levels. The 'where' command shows the current location in the file's tree. ```Python xpl.step("GEO") print(xpl.list()) print(xpl.where()) xpl.up() print(xpl.list()) ``` -------------------------------- ### Configure and Solve Acoustic Analysis Source: https://github.com/pbxie/pymapdl-example/blob/master/examples/00-mapdl-examples/acoustic_analysis.txt Sets up the solver for a harmonic analysis, defines frequency range, time stepping, and output requests to minimize file size while retaining necessary results for post-processing. ```Python mapdl.allsel() mapdl.run("/SOLU") mapdl.antype(3) mapdl.harfrq(freqb=200, freqe=1000) mapdl.autots("off") mapdl.nsubst(40) mapdl.kbc(0) mapdl.outres("erase") mapdl.outres("all", "none") mapdl.outres("nsol", "all") mapdl.outres("fgrad", "all") mapdl.outres("misc", "all") mapdl.solve() ``` -------------------------------- ### Compare Stress with MAPDL PRNSOL Command Source: https://github.com/pbxie/pymapdl-example/blob/master/examples/00-mapdl-examples/pressure_vessel.txt This snippet demonstrates how to disable MAPDL headers and then use the `prnsol` command to get stress results. The output is parsed to extract the equivalent stress values, which are then compared to the von-Mises stress obtained earlier using PyMAPDL. The comparison uses `np.allclose` with a relative tolerance to account for potential rounding differences. ```Python mapdl.header("OFF", "OFF", "OFF", "OFF", "OFF", "OFF") table = mapdl.prnsol("S", "PRIN").splitlines()[1:] prnsol_eqv = np.genfromtxt(table)[:, -1] # eqv is the last column # show these are equivalent (RTOL due to rounding within PRNSOL) print("All close:", np.allclose(von_mises, prnsol_eqv, rtol=1e-4)) print(f"LEGACY Reader and MAPDL VGET Min: {min_von_mises}") print(f"PRNSOL MAPDL Min: {prnsol_eqv.min()}") print() print(f"LEGACY Reader and MAPDL VGET Min: {max_von_mises}") print(f"PRNSOL MAPDL Min: {prnsol_eqv.max()}") ``` -------------------------------- ### Create 2D Pressure Vessel Model Source: https://github.com/pbxie/pymapdl-example/blob/master/examples/00-mapdl-examples/2d_pressure_vessel.txt Defines a Python function `pipe_plane_strain` to create and analyze a 2D pressure vessel model. It handles MAPDL setup, geometry creation (using `pcirc`), material properties, meshing, boundary conditions (fixed nodes and pressure load), solving, and basic post-processing to retrieve maximum equivalent stress. ```Python import matplotlib.pyplot as plt import numpy as np def pipe_plane_strain(e, nu, inn_radius, out_radius, press, aesize): """Create 2D cross section modeling a pipe.""" # reset mapdl mapdl.clear() mapdl.prep7() # Define element attributes # Quad 4 node 182 with keyoption 3 = 2 (plain strain formulation) mapdl.et(1, "PLANE182", kop3=2) # Create geometry # create a quadrant of the pressure vessel # We perform plane strain analysis on one quadrant (0deg - 90deg) of the # pressure vessel mapdl.pcirc(inn_radius, out_radius, theta1=0, theta2=90) mapdl.components["PIPE_PROFILE"] = "AREA" # Define material properties mapdl.mp("EX", 1, e) # Youngs modulus mapdl.mp("PRXY", 1, nu) # Poissons ratio # Define mesh controls mapdl.aesize("ALL", aesize) mapdl.mshape(0, "2D") # mesh the area with 2D Quad elements mapdl.mshkey(1) # free mesh mapdl.cmsel("S", "PIPE_PROFILE") # Select the area component to be meshed mapdl.amesh("ALL") # Create components for defining loads and constraints mapdl.nsel("S", "LOC", "X", 0) # Select nodes on top left edge mapdl.components["X_FIXED"] = "NODES" # Create nodal component mapdl.nsel("S", "LOC", "Y", 0) # Select nodes on bottom right edge mapdl.components["Y_FIXED"] = "NODES" # Create nodal component mapdl.allsel() mapdl.lsel("S", "RADIUS", vmin=rad1) # Select the line along inner radius mapdl.components["PRESSURE_EDGE"] = "LINE" # Create a line component mapdl.allsel() # Define solution controls mapdl.slashsolu() # Enter solution mapdl.antype("STATIC", "NEW") # Specify a new static analysis (Optional) mapdl.d("X_FIXED", "UX", 0) # Fix the selected nodes in X direction mapdl.d("Y_FIXED", "UY", 0) # Fix the selected nodes in Y direction # Change the active Cartesian Coordinate system to Cylindrical Coordinate system mapdl.csys(1) # Apply uniform pressure load to the selected edge mapdl.sfl("PRESSURE_EDGE", "PRES", press) # Solve the model mapdl.allsel() mapdl.solve() mapdl.finish() # Enter post-processor mapdl.post1() mapdl.set(1, 1) # Select the first load step max_eqv_stress = np.max(mapdl.post_processing.nodal_eqv_stress()) all_dof = mapdl.mesh.nnum_all num_dof = all_dof.size return num_dof, max_eqv_stress ```