使用 PyMAPDL 的 DPF-Core 基本用法#

本例改编自 DPF-Core 基本用法示例 , 展示了如何在 DPF 中打开结果文件,并进行一些基本的后处理。

如果安装了 Ansys 2021 R1,启动 DPF 就非常简单了,因为 DPF-Core 会负责启动后处理 Ansys 文件所需的所有服务。

首先,以 dpf_core 的形式导入 DPF-Core 模块,并导入随附的示例文件。

import tempfile

from ansys.dpf import core as dpf

from ansys.mapdl.core import launch_mapdl
from ansys.mapdl.core.examples import vmfiles

Create model#

运行 MAPDL 验证手册中的示例

mapdl = launch_mapdl()

vm5 = vmfiles["vm5"]
output = mapdl.input(vm5)

print(output)

# 如果在本地工作,则无需执行以下步骤
temp_directory = tempfile.gettempdir()
print(f'默认临时文件地址: {temp_directory}')

# 将 RST 文件下载到当前文件夹
rst_path = mapdl.download_result(temp_directory)
C:\Users\ff\AppData\Local\Programs\Python\Python310\lib\site-packages\ansys\tools\path\path.py:818: DeprecationWarning: This method is going to be deprecated in future versions. Please use 'get_mapdl_path'.
  warnings.warn(

 /INPUT FILE= C:\Users\ff\AppData\Local\Programs\Python\Python310\lib\site-packages\ansys\mapdl\core\examples\verif\vm5.dat  LINE=       0
 ANSYS MEDIA REL. 2023R1 (11/04/2022) REF. VERIF. MANUAL: REL. 2023R1

 *** VERIFICATION RUN - CASE VM5                              ***  OPTION=  4


 /SHOW SWITCH PLOTS TO  JPEG        - RASTER MODE.
   *****MAPDL VERIFICATION RUN ONLY*****
     DO NOT USE RESULTS FOR PRODUCTION

          ***** MAPDL ANALYSIS DEFINITION (PREP7) *****

 TITLE=
  VM5, LATERALLY LOADED TAPERED SUPPORT STRUCTURE (QUAD. ELEMENTS)


 C***     MECHANICS OF SOLIDS, CRANDALL AND DAHL, 1959, PAGE 342, PROB. 7.18


 C***          USING PLANE42 ELEMENTS



 PERFORM A STATIC ANALYSIS
  THIS WILL BE A NEW ANALYSIS

 ELEMENT TYPE          1 IS PLANE182     2-D 4-NODE PLANE STRS SOLID
  KEYOPT( 1- 6)=        2      0      3        0      0      0
  KEYOPT( 7-12)=        0      0      0        0      0      0
  KEYOPT(13-18)=        0      0      0        0      0      0

 CURRENT NODAL DOF SET IS  UX    UY
  TWO-DIMENSIONAL MODEL

 REAL CONSTANT SET          1  ITEMS   1 TO   6
    2.0000       0.0000       0.0000       0.0000       0.0000       0.0000

 MATERIAL          1     EX   =  0.3000000E+08

 MATERIAL          1     NUXY =   0.000000

 NODE          1  KCS=      0  X,Y,Z=  25.000       0.0000       0.0000

 NODE          7  KCS=      0  X,Y,Z=  75.000       0.0000       0.0000

 FILL       5 POINTS BETWEEN NODE       1 AND NODE       7
  START WITH NODE       2 AND INCREMENT BY       1

 NODE          8  KCS=      0  X,Y,Z=  25.000      -3.0000       0.0000

 NODE         14  KCS=      0  X,Y,Z=  75.000      -9.0000       0.0000

 FILL       5 POINTS BETWEEN NODE       8 AND NODE      14
  START WITH NODE       9 AND INCREMENT BY       1

 ELEMENT      1       2      1      8      9

 GENERATE       6 TOTAL SETS OF ELEMENTS WITH NODE INCREMENT OF         1
   SET IS SELECTED ELEMENTS IN RANGE         1 TO         1 IN STEPS OF       1

 MAXIMUM ELEMENT NUMBER=         6

 SELECT       FOR ITEM=LOC  COMPONENT=X     BETWEEN  75.000     AND   75.000
   KABS=  0.  TOLERANCE= 0.375000

          2  NODES (OF         14  DEFINED) SELECTED BY  NSEL  COMMAND.

 SPECIFIED CONSTRAINT UX   FOR SELECTED NODES            1 TO          14 BY           1
 REAL=  0.00000000       IMAG=  0.00000000
 ADDITIONAL DOFS=  UY

 ALL SELECT   FOR ITEM=NODE COMPONENT=
  IN RANGE         1 TO         14 STEP          1

         14  NODES (OF         14  DEFINED) SELECTED BY NSEL  COMMAND.

 SPECIFIED NODAL LOAD FY   FOR SELECTED NODES         1 TO        1 BY        1
  REAL= -4000.00000       IMAG=  0.00000000


 ***** ROUTINE COMPLETED *****  CP =         0.000



 *****  MAPDL SOLUTION ROUTINE  *****

 PRINT BASI ITEMS WITH A FREQUENCY OF      1
   FOR ALL APPLICABLE ENTITIES

 /OUTPUT FILE=

默认临时文件地址: C:\Users\ff\AppData\Local\Temp

接下来,打开生成的 RST 文件并打印出 Model 对象。 Model 类通过跟踪结果文件使用的运算符和数据源,有助于组织结果的访问方法。

打印模型显示:

  • Analysis type 分析类型

  • Available results 可用结果

  • Size of the mesh 网格尺寸

  • Number of results 结果数量

如果要使用远程服务器,可能需要先上传 RST 文件,然后再使用它。 然后你就可以创建 DPF Model

dpf.core.make_tmp_dir_server(dpf.SERVER)

if dpf.SERVER.local_server:
    model = dpf.Model(rst_path)
else:
    server_file_path = dpf.upload_file_in_tmp_folder(rst_path)
    model = dpf.Model(server_file_path)

print(model)
DPF Model
------------------------------
Static analysis
Unit system: SI: m, kg, s, V, A, K
Physics Type: Mechanical
Available results:
     -  displacement: Nodal Displacement
     -  reaction_force: Nodal Force
     -  element_nodal_forces: ElementalNodal Element nodal Forces
     -  stress: ElementalNodal Stress
     -  elemental_volume: Elemental Volume
     -  stiffness_matrix_energy: Elemental Energy-stiffness matrix
     -  artificial_hourglass_energy: Elemental Hourglass Energy
     -  thermal_dissipation_energy: Elemental thermal dissipation energy
     -  kinetic_energy: Elemental Kinetic Energy
     -  co_energy: Elemental co-energy
     -  incremental_energy: Elemental incremental energy
     -  elastic_strain: ElementalNodal Strain
     -  thermal_strain: ElementalNodal Thermal Strains
     -  thermal_strains_eqv: ElementalNodal Thermal Strains eqv
     -  swelling_strains: ElementalNodal Swelling Strains
     -  structural_temperature: ElementalNodal Temperature
------------------------------
DPF  Meshed Region:
  33 nodes
  6 elements
  Unit:
  With shell (2D) elements, shell (3D) elements
------------------------------
DPF  Time/Freq Support:
  Number of sets: 1
Cumulative     Time (s)       LoadStep       Substep
1              1.000000       1              1

Model Metadata#

可以通过引用模型的 metadata 属性从模型中提取特定元数据。 例如,只打印 result_info 属性:

metadata = model.metadata
print(metadata.result_info)
Static analysis
Unit system: SI: m, kg, s, V, A, K
Physics Type: Mechanical
Available results:
     -  displacement: Nodal Displacement
     -  reaction_force: Nodal Force
     -  element_nodal_forces: ElementalNodal Element nodal Forces
     -  stress: ElementalNodal Stress
     -  elemental_volume: Elemental Volume
     -  stiffness_matrix_energy: Elemental Energy-stiffness matrix
     -  artificial_hourglass_energy: Elemental Hourglass Energy
     -  thermal_dissipation_energy: Elemental thermal dissipation energy
     -  kinetic_energy: Elemental Kinetic Energy
     -  co_energy: Elemental co-energy
     -  incremental_energy: Elemental incremental energy
     -  elastic_strain: ElementalNodal Strain
     -  thermal_strain: ElementalNodal Thermal Strains
     -  thermal_strains_eqv: ElementalNodal Thermal Strains eqv
     -  swelling_strains: ElementalNodal Swelling Strains
     -  structural_temperature: ElementalNodal Temperature

打印 mesh region(网格区域)

print(metadata.meshed_region)
DPF  Meshed Region:
  33 nodes
  6 elements
  Unit:
  With shell (2D) elements, shell (3D) elements

要打印结果的时间或频率,请使用 time_freq_support

print(metadata.time_freq_support)
DPF  Time/Freq Support:
  Number of sets: 1
Cumulative     Time (s)       LoadStep       Substep
1              1.000000       1              1

Extracting Displacement Results#

可以通过 Results 属性访问模型的所有结果, 该属性返回 ansys.dpf.core.results.Results 类。该类包含特定结果文件可用的 DPF 结果运算符,使用 print(results) 打印对象时会列出这些运算符。

这里,displacement 运算符与 DataSources(数据源) 相联, 这会在运行 results.displacement() 时自动进行。 默认情况下,displacement 运算符连接到第一个结果集,对于此静态结果而言,它是唯一的结果。

results = model.results
displacements = results.displacement()
fields = displacements.outputs.fields_container()

# 最后,提取位移场数据:
disp = fields[0].data
disp
DPFArray([[-4.38851905e-03, -8.90023185e-03,  0.00000000e+00],
          [-6.69683824e-03, -2.11175625e-02,  0.00000000e+00],
          [ 4.17859820e-03, -2.11057663e-02,  0.00000000e+00],
          [ 3.30502628e-03, -8.89049154e-03,  0.00000000e+00],
          [-5.53644478e-03, -1.42612890e-02,  0.00000000e+00],
          [-1.20792265e-03, -2.11145211e-02,  0.00000000e+00],
          [ 3.82436228e-03, -1.42559816e-02,  0.00000000e+00],
          [-4.90502176e-04, -8.89780417e-03,  0.00000000e+00],
          [-2.17801557e-03, -2.15151386e-03,  0.00000000e+00],
          [ 1.82830253e-03, -2.15124124e-03,  0.00000000e+00],
          [-3.27160527e-03, -4.90756375e-03,  0.00000000e+00],
          [ 2.63019818e-03, -4.89004246e-03,  0.00000000e+00],
          [-8.95838911e-05, -2.15503538e-03,  0.00000000e+00],
          [ 0.00000000e+00,  0.00000000e+00,  0.00000000e+00],
          [ 0.00000000e+00,  0.00000000e+00,  0.00000000e+00],
          [-1.07316284e-03, -5.41584305e-04,  0.00000000e+00],
          [ 9.59021573e-04, -5.64429271e-04,  0.00000000e+00],
          [ 0.00000000e+00,  0.00000000e+00,  0.00000000e+00],
          [-1.10106159e-02, -6.60224200e-02,  0.00000000e+00],
          [-1.20838957e-02, -9.90994350e-02,  0.00000000e+00],
          [ 3.61860276e-04, -9.88721453e-02,  0.00000000e+00],
          [ 3.23207196e-03, -6.59380057e-02,  0.00000000e+00],
          [-1.17563515e-02, -8.18061655e-02,  0.00000000e+00],
          [-5.82220244e-03, -9.89754713e-02,  0.00000000e+00],
          [ 2.08036965e-03, -8.18682795e-02,  0.00000000e+00],
          [-3.87518431e-03, -6.59854725e-02,  0.00000000e+00],
          [-8.99066833e-03, -3.98506343e-02,  0.00000000e+00],
          [ 4.27062988e-03, -3.98210573e-02,  0.00000000e+00],
          [-1.00608366e-02, -5.19510806e-02,  0.00000000e+00],
          [ 3.92601832e-03, -5.19563394e-02,  0.00000000e+00],
          [-2.31550786e-03, -3.98370089e-02,  0.00000000e+00],
          [-7.85485340e-03, -2.95981071e-02,  0.00000000e+00],
          [ 4.34038437e-03, -2.95942670e-02,  0.00000000e+00]])

Plot displacements#

您可以使用以下方法打印上面的位移场:

model.metadata.meshed_region.plot(fields, cpos="xy")
basic dpf example

或使用

fields[0].plot(cpos="xy")
basic dpf example

如果您在网格或结果上使用了 ansys.dpf.core.scoping.Scoping 方法,这种方法会特别有用。

Close session#

Stop MAPDL session.

mapdl.exit()

Total running time of the script: (0 minutes 9.507 seconds)