How to export coordinates and electric field values of each mesh element in a volume using PyAEDT?

I would like to write the electric field values and coordinates of each mesh element into a file. Using AEDT API, I can do this by:

# project_name, design_name, volume_name, and file_path are variables
oProject = oDesktop.SetActiveProject(project_name)
oDesign = oProject.SetActiveDesign(design_name)
oModule = oDesign.GetModule("FieldsReporter")
oModule.EnterQty("E")
oModule.EnterVol(volume_name)
oModule.CalcOp("Value")
oModule.CalculatorWrite(file_path, 
    [
        "Solution:="        , "MySetupAuto : LastAdaptive"
    ], [])

How is this implemented in PyAEDT? Right now, I combine PyAEDT with AEDT API, but I would prefer to do everything with PyAEDT.

Initially, I tried to define a new calculator expression, thinking I can use aedtapp.post.fields_calculator.write() afterwards.

my_expression = {
    "name": "efield_volume",
    "description": "Electric field in a volume",
    "design_type": ["Maxwell 3D"],
    "fields_type": ["Fields"],
    "solution_type": "",
    "primary_sweep": "Freq",
    "assignment": "",
    "assignment_type": ["Solid"],
    "operations": [
        "NameOfExpression('<Ex,Ey,Ez>')",
        "EnterVol('assignment')",
        "Operation('Value')"
    ],
    "report" : ["Data Table"]
}

new_expression = aedtapp.post.fields_calculator.add_expression(my_expression, assignment=volume_name)

However, I get a PyAEDT error.

PyAEDT ERROR: **************************************************************
PyAEDT ERROR:   File "file_name.py", line 48, in <module>
PyAEDT ERROR:     new_expression = aedtapp.post.fields_calculator.add_expression(my_expression, assignment=volume_name)
PyAEDT ERROR:   File "~\AppData\Roaming\.pyaedt_env\3_10\lib\site-packages\ansys\aedt\core\visualization\post\fields_calculator.py", line 249, in add_expression
PyAEDT ERROR:     self.ofieldsreporter.LoadNamedExpressions(
PyAEDT ERROR: AEDT API Error on add_expression
PyAEDT ERROR: Last Electronics Desktop Message - [error] script macro error: abnormal script termination. (01:53:26 pm  jan 09, 2025)

PyAEDT ERROR: Method arguments:
PyAEDT ERROR:     calculation = {'name': 'efield_voltage_write', 'description': 'Electric field in a volume', 'design_type': ['Maxwell 3D'], 'fields_type': ['Fields'], 'solution_type': '', 'primary_sweep': 'Freq', 'assignment': 'Region', 'assignment_type': ['Solid
PyAEDT ERROR: '], 'operations': ["NameOfExpression('<Ex,Ey,Ez>')", "EnterVol('Region')", "Operation('Value')"], 'report': ['Data Table'], 'dependent_expressions': []}
PyAEDT ERROR:     assignment = Region
PyAEDT ERROR: **************************************************************

Best Answer

  • Samuel Lopez
    Samuel Lopez Member, Employee Posts: 23
    Second Anniversary 5 Answers 10 Comments Ansys Employee
    ✭✭✭✭
    Answer ✓

    Hi @HFrancisco ,

    This method is used to create a new expression, but your expression can not be created because in the fields calculator you can only create expression that results in a numeric value.

    What you need is different, you want to export the mesh and fields, and it is quite easy in PyAEDT, you can two options. I have this dummy project solved:

    With the following line, the method will export the Mag E field in the object called "shoulder", in a format called .case, this file is used by other Ansys tools like Fluent, once this file is exported, it is automatically loaded in PyVista, so you can view the field directly:

    plot = app.post.plot_field("Mag_E",
    "shoulder",
    plot_type="Surface",
    mesh_on_fields=True, show=True)

    If you change the show parameter to False, it will return the PyVista object, and you can find and manipulate the data:

    If you want something simpler, you could use this method that is exporting the data in a .fld file:

    file= app.post.export_field_file("Mag_E", assignment="shoulder", objects_type="Surf")

    Thank you for using PyAEDT.

    Samuel

Answers