|
| 1 | +""" |
| 2 | +.. _ref_dpf_basic_example: |
| 3 | +
|
| 4 | +Basic DPF-Core Usage with PyMAPDL |
| 5 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 6 | +
|
| 7 | +This example is adapted from |
| 8 | +`Basic DPF-Core Usage Example <https://dpf.docs.pyansys.com/examples/00-basic/00-basic_example.html>`_ |
| 9 | +and it shows how to open a result file in `DPF <https://dpf.docs.pyansys.com/>`_ and do some |
| 10 | +basic postprocessing. |
| 11 | +
|
| 12 | +If you have Ansys 2021 R1 installed, starting DPF is quite easy |
| 13 | +as DPF-Core takes care of launching all the services that |
| 14 | +are required for postprocessing Ansys files. |
| 15 | +
|
| 16 | +First, import the DPF-Core module as ``dpf_core`` and import the |
| 17 | +included examples file. |
| 18 | +
|
| 19 | +
|
| 20 | +""" |
| 21 | +import tempfile |
| 22 | + |
| 23 | +from ansys.dpf import core as dpf |
| 24 | + |
| 25 | +from ansys.mapdl.core import launch_mapdl |
| 26 | +from ansys.mapdl.core.examples import vmfiles |
| 27 | + |
| 28 | +############################################################################### |
| 29 | +# ## Model creation |
| 30 | +# Running a verification manual example in MAPDL |
| 31 | +# |
| 32 | +mapdl = launch_mapdl() |
| 33 | + |
| 34 | +vm5 = vmfiles["vm5"] |
| 35 | +output = mapdl.input(vm5) |
| 36 | + |
| 37 | +print(output) |
| 38 | + |
| 39 | +# If you are working locally, you don't need to perform the following steps |
| 40 | +temp_directory = tempfile.gettempdir() |
| 41 | +# Downloading RST file to the current folder |
| 42 | +rst_path = mapdl.download_result(temp_directory) |
| 43 | + |
| 44 | +############################################################################### |
| 45 | +# Next, open the generated RST file and print out the |
| 46 | +# :class:`Model <ansys.dpf.core.model.Model>` object. |
| 47 | +# The :class:`Model <ansys.dpf.core.model.Model>` class helps to |
| 48 | +# organize access methods for the result by |
| 49 | +# keeping track of the operators and data sources used by the result |
| 50 | +# file. |
| 51 | +# |
| 52 | +# Printing the model displays: |
| 53 | +# |
| 54 | +# - Analysis type |
| 55 | +# - Available results |
| 56 | +# - Size of the mesh |
| 57 | +# - Number of results |
| 58 | +# |
| 59 | +# Also, note that the first time you create a DPF object, Python |
| 60 | +# automatically attempts to start the server in the background. If you |
| 61 | +# want to connect to an existing server (either local or remote), use |
| 62 | +# :func:`dpf.connect_to_server <ansys.dpf.core.server.connect_to_server>`. |
| 63 | +# In this case, DPF will attempt to connect to a local server at the port 50052 |
| 64 | +# unless another port is specified. |
| 65 | + |
| 66 | +dpf.connect_to_server() |
| 67 | + |
| 68 | + |
| 69 | +############################################################################### |
| 70 | +# If you are working with a remote server, you might need to upload the ``RST`` |
| 71 | +# file before working with it. |
| 72 | +server_file_path = dpf.upload_file_in_tmp_folder(rst_path) |
| 73 | + |
| 74 | + |
| 75 | +############################################################################### |
| 76 | +# Then you can create the :class:`DPF Model <ansys.dpf.core.model.Model>`. |
| 77 | +# |
| 78 | + |
| 79 | +model = dpf.Model(server_file_path) |
| 80 | +print(model) |
| 81 | + |
| 82 | +############################################################################### |
| 83 | +# Model Metadata |
| 84 | +# ~~~~~~~~~~~~~~ |
| 85 | +# Specific metadata can be extracted from the model by referencing the |
| 86 | +# model's :attr:`metadata <ansys.dpf.core.model.Model.metadata>` |
| 87 | +# property. For example, to print only the |
| 88 | +# :attr:`result_info <ansys.dpf.core.model.Metadata.result_info>`: |
| 89 | + |
| 90 | +metadata = model.metadata |
| 91 | +print(metadata.result_info) |
| 92 | + |
| 93 | +############################################################################### |
| 94 | +# To print the :class:`mesh region<ansys.dpf.core.meshed_region.MeshedRegion>`: |
| 95 | + |
| 96 | +print(metadata.meshed_region) |
| 97 | + |
| 98 | +############################################################################### |
| 99 | +# To print the time or frequency of the results use |
| 100 | +# :class:`time_freq_support <ansys.dpf.core.time_freq_support.TimeFreqSupport>`: |
| 101 | + |
| 102 | +print(metadata.time_freq_support) |
| 103 | + |
| 104 | +############################################################################### |
| 105 | +# Extracting Displacement Results |
| 106 | +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 107 | +# All results of the model can be accessed through the :class:`Results <ansys.dpf.core.results.Results>` |
| 108 | +# property, which returns the :class:`ansys.dpf.core.results.Results` |
| 109 | +# class. This class contains the DPF result operators available to a |
| 110 | +# specific result file, which are listed when printing the object with |
| 111 | +# ``print(results)``. |
| 112 | +# |
| 113 | +# Here, the :class:`displacement <ansys.dpf.core.operators.result.displacement.displacement>` |
| 114 | +# operator is connected with |
| 115 | +# :class:`DataSources <ansys.dpf.core.data_sources.DataSources>`, which |
| 116 | +# takes place automatically when running |
| 117 | +# :class:`results.displacement() <ansys.dpf.core.operators.result.displacement.displacement>`. |
| 118 | +# By default, the :class:`displacement <ansys.dpf.core.operators.result.displacement.displacement>` |
| 119 | +# operator is connected to the first result set, |
| 120 | +# which for this static result is the only result. |
| 121 | + |
| 122 | +results = model.results |
| 123 | +displacements = results.displacement() |
| 124 | +fields = displacements.outputs.fields_container() |
| 125 | + |
| 126 | +# Finally, extract the data of the displacement field: |
| 127 | +disp = fields[0].data |
| 128 | +disp |
| 129 | + |
| 130 | +############################################################################### |
| 131 | +# Plot displacements |
| 132 | +# ~~~~~~~~~~~~~~~~~~ |
| 133 | +# |
| 134 | +# You can plot the previous displacement field using: |
| 135 | + |
| 136 | +model.metadata.meshed_region.plot(fields, cpos="xy") |
| 137 | + |
| 138 | +############################################################################### |
| 139 | +# Or using |
| 140 | +# |
| 141 | + |
| 142 | +fields[0].plot(cpos="xy") |
| 143 | + |
| 144 | +############################################################################### |
| 145 | +# This way is particularly useful if you have used :class:`ansys.dpf.core.scoping.Scoping` |
| 146 | +# on the mesh or results. |
| 147 | + |
| 148 | + |
| 149 | +############################################################################### |
| 150 | +# Clean up |
| 151 | +# ~~~~~~~~ |
| 152 | +# |
| 153 | +# Stop MAPDL session. |
| 154 | +# |
| 155 | +mapdl.exit() |
0 commit comments