Efficiency, flexibility, versatility, ease-of use; these are hallmarks of Python.
PyMAPDL enriches the Pythonic version of APDL commands with categories
to bring these hallmarks to all PyMAPDL workflows for all users.
In this lesson, we will introduce five of the most often-used PyMAPDL categories by
reviewing some of the category functions and highlight how
they add efficiency and ease of use to the PyMAPDL
interactive workflow.
As appropriate, we will discuss other features of the category.
To do so, we will work our way through an example analysis
of a lathe cutter.
Ok, let s get started.
What we are calling categories in PyMAPDL are various public classes, functions, and
attributes.
There are multiple reasons for these categories,
and as discussed, two are ease and efficiency of use.
For our users with a Python background, APDL can be
an unfamiliar language.
Some of the categories offer APDL functionality in a Python style format
that is more natural for Python users.
For our users with more of a MAPDL background, in a manner similar to APDL macros, some of
the categories package the functionality of multiple
APDL and Python commands into one function to
complete a particular task.
Like some APDL commands, these categories can often be used at multiple stages of a
finite element analysis workflow.
To begin, let s review the workshop model up to launching MAPDL.
The model is a lathe cutter made of typical steel.
Symmetry boundary conditions will be defined on
these areas, with a sinusoidal pressure load that is a function of the applied area length
defined here.
We start by importing OS and Numpy.
Numpy will be used for working with arrays, some functions and
a constant.
The OS module provides operating system dependent functions.
Now let s define the path to be the current working directory, the constant Pi, and two
variables for typical values of Young s Modulus and Poisson
s ratio of steel.
Now, we are ready to launch MAPDL which has options.
This example will result in MAPDL generated files, so to make them easy to find we use
the Jupyter Lab working directory as the MAPDL working
directory as well.
The Parameters category is a complement to the APDL commands for working with MAPDL parameters.
Let s first do a quick recap of parameters in MAPDL.
In MAPDL, single-value variables are called parameters, while multi-valued are arrays.
Parameters are used to hold both input and result values.
Arrays come in two styles; table arrays are used mainly to define inputs and we will cover
this later in the lathe model.
Arrays are often used to gather data for manipulation and export.
Some APDL commands result in data automatically collected
to a parameter or an array.
The parameter category is also an ease-of-use tool that can be used to define a MAPDL parameter
with a value or an expression.
Enclosing the value in single quotes results in a character
parameter.
The command without a parameter name will return the list of MAPDL parameters and
their values.
The parameter values can be returned to Python variables.
The same holds true for array parameters.
Moving back to the lathe model, we next read the supplied lathe cutter CAD file.
Most MAPDL input file types can include parameters, so mapdl.parameters
is used to inspect whether there are any defined in
the file and we see that there are.
The length of the area with the applied pressure will be needed
later, so this is captured to a Python variable.
And the unit system will be set to match that of the input
file.
The Python variables holding the material property values will be used in defining the
material model.
The rest of the PyMAPDL commands mesh the volume and define the symmetry boundary
conditions.
Looking ahead, the next PyMAPDL category used will be a volume plot so let s discuss
plotting a bit.
The Plotting category is another addition that makes PyMAPDL versatile and easy to use.
The instance of MAPDL that PyMAPDL starts runs as a batch
process for outputs this means that MAPDL plots would
be static images.
The Plotting category brings interactive plotting to PyMAPDL which is extremely useful
when running PyMAPDL in an interactive environment like Jupyter Lab.
The MAPDL plot controls are not currently used by this category, though the category
does respect other settings such as MAPDL selection.
The various CAD plot commands include their own set up
arguments which includes the **kwargs (key word arguments) from the general plotter.
Let s review a few examples with the lathe model.
In step three, a local coordinate system is defined at the corner of the area where the
pressure will be applied.
The local X direction is along the long edge of the area.
Normally, we would plot something, like the volume, with the local coordinate
system symbol turned on for verification.
Let s try that: set the areas to be shown in different colors,
and the edges in a bolded black.
Other useful options are cpoc that sets the camera position.
As we can see, the local coordinate system symbol is not included.
We can save a static image by setting VTK equal to False; this results in MAPDL saving
a screen shot to a png file which shows up in the list of files.
This file can be opened as well.
We can see that the local coordinate system is defined.
Looking ahead we see that the mapdl.load_table will be used soon, so as before let s discuss
the MAPDL category first.
In addition to the Pythonic versions of APDL commands, the MAPDL category extends some
APDL commands to make them more Python-friendly.
Two examples are the APDL *GET and *VGET commands which retrieve scalar and vector
data from the MAPDL database into MAPDL parameters or
arrays, respectively.
This category includes mapdl.get_value and mapdl.get_arrary which do the
same but return the data directly to a Python variable or array.
This makes the commands more efficient as they wrap two functions, gathering
the data in MAPDL and transfering it to Python, into one
function.
If you have viewed the Ansys Innovation Courses APDL class More on Parameters, you will have
learned that MAPDL has a type of array, a Table, that is especially useful when defining
APDL command inputs as functions.
An example is applied pressure as a function of time.
The Python module numpy does not have a table type of array, so this
category includes mapdl.load_table that can take a Python
array and load it into MAPDL as a table.
For users developing an PyMAPDL script interactively, like in Jupyter Lab, the category includes
mapdl.open_gui which can be a valuable development tool.
It saves the MAPDL database; closes the MAPDL instance opened by PyMAPDL; launches
MAPDL interactively; and resumes the saved MAPDL
database.
The PyMAPDL script developer can then work with the model in MAPDL.
This can be helpful in several ways from checking your work to that point,
to practicing an APDL command you may not be familiar
with and would like immediate feedback.
Any work done in MAPDL interactive does not affect the
PyMAPDL session once MAPDL interactive is exited.
Once exited, the MAPDL instance in PyMAPDL is
started back up and we can continue to write the script.
Ok, let s get back to the lathe model and define
the load.
To define the pressure load, we will first define a vector of X positions along the pressure
area; ten should be sufficient.
Then, a pressure vector that is a half sine shape is defined.
The two vectors are then stacked into the correct array shape
for a MAPDL table array.
The table is then loaded into MAPDL using mapdl.load_table with the primary variable
as X location and using the local coordinate system
with ID 11.
This table is then used to define the pressure load on the area nodes being sure to enclose
the table name in percent signs.
As a sanity check, we could list the pressure array table from MAPDL
and compare it to the pressure array defined in Python.
Or we could use the mapdl.open_gui discussed earlier to inspect the finite element
model.
We continue with the model by entering the solution processor, turning on the pressure
applied load symbols as a contour plot, and issuing a mapdl.eplot
command to produce a MAPDL style image where we can see that the pressure does have a sine
shape to it.
To complete the solution, we turn on large deflection and solve.
mapdl.solution is another category that has Python style *get functions to retrieve
information about the solution.
Looking ahead, a vertical application where the user interface is a web browser
form which returns the convergence status would be a quick
and straightforward way of informing the user that the solution has completed as intended.
The Post Processing category brings a Pythonic structure to MAPDL post processing; it addresses
the need to post processing with interactive plots,
like Plotting, when running PyMAPDL in an interactive
environment; and it brings the Parameter category type versatility by making it easy to return
arrays of MAPDL results to Python.
Like Plotting, it respects the result of PyMAPDL selection commands.
Back to the lathe cutter model, let us go ahead and try plotting the nodal first principal
stress.
As with plotting, entity selections are respected, so let s do this by selecting a set of nodes
on the model; then select all the elements that have at
least one of those nodes; and finish by selecting all the nodes of
the selected elements.
Then plot the same result with the element edges shown in white.
For the last plot, let s look at the contour legend, or what is known as the scalar bar
in Python.
Using a Python data structure dictionary we can change
the text color, position, number of contour labels,
among others.
Issue the result plot one more time.
Click on the ... to see an alternate method of defining a dictionary with some additional
legend controls that you can practice with.
Now, if we issue this same command without the leading plot an array of result values
is returned.
The category results are all single results, but let s say we would like to capture all
three principal stresses in an array.
First, we will select nodes that meet an upper first principal stress value just to
make the list size reasonable and reissue the post processing listing command.
We can use the mesh category to check the number of selected nodes;
we will revisit the mesh category later.
MAPDL can list nodal results with PRNSOL command.
Now if we wrap the command in a print, we get back a listing.
However, we would prefer to have these in an array.
For ease-of-use PyMAPDL extends the Pythonic version of
some APDL listing commands with additional methods.
That is, we can choose to return the values to a list,
a array, or a data frame.
A data frame is a spreadsheet type of view.
The Data Frame is a Pandas feature Pandas is a Python module for working with structured
data and times series, much like a spreadsheet.
And one function of Pandas is the ability to write the data frame
to many different formats.
CSV is a traditional format employed by MAPDL users.
As with the converged status, the JSON or HTML format
would be appropriate for users developing a browser front
end to some analysis.
Lastly the PyMAPDL category Mesh really brings versatility and efficient Python like functions
to our workflow.
The category contains functions for retrieving mesh information such as the selected node
numbers list that we did earlier.
The mapdl.mesh.grid returns an VTK unstructured grid data of the finite element mesh.
This really opens PyMAPDL to work with various other Python
modules and makes for a versatile workflow.
Here we can capture the first principal stress
values to a list, then associate them with the unstructured grid.
A grid slice with normal in the global Z direction is defined, then the slice plotted.
Other predefined slice functions are available like this orthogonal, which slices by three
orthogonal planes aligned with the global coordinate
system.
Select the ... to see an example of plotting multiple aligned slices.
With that we have completed the learning objective of this lesson.
In this video we have learned of various PyMAPDL categories that extend PyMAPDL
from the Pythonic version of APDL commands that
enhance its use.
Several of the often-used categories were reviewed via their use in a structural
analysis.
This concludes the video, and we will continue our learning in the next video.