Download this example
Download this example as a Jupyter Notebook or as a Python script.
Magnetomotive force along a line#
This example shows how to use PyAEDT to calculate the magnetomotive force along a line that changes position. It shows how to leverage the PyAEDT advanced fields calculator to insert a custom formula, which in this case is the integral of the H field along a line. The example shows two options to achieve the intent. The first one creates many lines as to simulate a contour that changes position. The integral of the H field is computed for each line. The second option creates one parametric polyline and then uses a parametric sweep to change its position. The integral of the H field is computed for each position.
Keywords: Maxwell 2D, magnetomotive force.
Perform imports and define constants#
Perform required imports.
[1]:
import tempfile
import time
[2]:
import ansys.aedt.core
Define constants.
[3]:
AEDT_VERSION = "2024.2"
NUM_CORES = 4
NG_MODE = False # Open AEDT UI when it is launched.
Create temporary directory#
Create a temporary directory where downloaded data or dumped data can be stored. If you’d like to retrieve the project data for subsequent use, the temporary folder name is given by temp_folder.name
.
[4]:
temp_folder = tempfile.TemporaryDirectory(suffix=".ansys")
Import project#
Download the files required to run this example to the temporary working folder.
[5]:
project_path = ansys.aedt.core.downloads.download_file(
source="maxwell_magnetic_force",
name="Maxwell_Magnetic_Force.aedt",
destination=temp_folder.name,
)
Initialize and launch Maxwell 2D#
Initialize and launch Maxwell 2D, providing the version and the path of the project.
[6]:
m2d = ansys.aedt.core.Maxwell2d(
version=AEDT_VERSION,
non_graphical=NG_MODE,
project=project_path,
design="Maxwell2DDesign1",
)
PyAEDT INFO: Parsing C:\Users\ansys\AppData\Local\Temp\tmpc5dmj3u5.ansys\maxwell_magnetic_force\Maxwell_Magnetic_Force.aedt.
PyAEDT INFO: Python version 3.10.11 (tags/v3.10.11:7d4cc5a, Apr 5 2023, 00:38:17) [MSC v.1929 64 bit (AMD64)]
PyAEDT INFO: PyAEDT version 0.12.dev0.
PyAEDT INFO: Initializing new Desktop session.
PyAEDT INFO: Log on console is enabled.
PyAEDT INFO: Log on file C:\Users\ansys\AppData\Local\Temp\pyaedt_ansys_f390606b-b280-4deb-9558-4d397236551c.log is enabled.
PyAEDT INFO: Log on AEDT is enabled.
PyAEDT INFO: Debug logger is disabled. PyAEDT methods will not be logged.
PyAEDT INFO: Launching PyAEDT with gRPC plugin.
PyAEDT INFO: New AEDT session is starting on gRPC port 53430
PyAEDT INFO: File C:\Users\ansys\AppData\Local\Temp\tmpc5dmj3u5.ansys\maxwell_magnetic_force\Maxwell_Magnetic_Force.aedt correctly loaded. Elapsed time: 0m 0sec
PyAEDT INFO: AEDT installation Path C:\Program Files\AnsysEM\v242\Win64
PyAEDT INFO: Ansoft.ElectronicsDesktop.2024.2 version started with process ID 4988.
PyAEDT INFO: Project Maxwell_Magnetic_Force has been opened.
PyAEDT INFO: Aedt Objects correctly read
First option#
Create a polyline#
Create a polyline, specifying its ends.
[7]:
poly = m2d.modeler.create_polyline(points=[[10, -10, 0], [10, 10, 0]], name="polyline")
PyAEDT INFO: Modeler2D class has been initialized!
PyAEDT INFO: Modeler class has been initialized! Elapsed time: 0m 1sec
PyAEDT INFO: Materials class has been initialized! Elapsed time: 0m 0sec
Duplicate the polyline along a vector.
[8]:
polys = [poly.name]
polys.extend(poly.duplicate_along_line(vector=[-0.5, 0, 0], clones=10))
Compute magnetomotive force along each line#
Create and add a new formula to add in the PyAEDT advanced fields calculator. Create the fields report object and get field data. Create a data table report for the H field along each line and export it to a .csv file.
[9]:
my_expression = {
"name": None,
"description": "Magnetomotive force along a line",
"design_type": ["Maxwell 2D", "Maxwell 3D"],
"fields_type": ["Fields"],
"primary_sweep": "distance",
"assignment": None,
"assignment_type": ["Line"],
"operations": [
"Fundamental_Quantity('H')",
"Operation('Tangent')",
"Operation('Dot')",
"EnterLine('assignment')",
"Operation('LineValue')",
"Operation('Integrate')",
],
"report": ["Data Table"],
}
[10]:
quantities = []
for p in polys:
quantity = "H_field_{}".format(p)
quantities.append(quantity)
my_expression["name"] = quantity
my_expression["assignment"] = quantity
m2d.post.fields_calculator.add_expression(my_expression, p)
report = m2d.post.create_report(
expressions=quantity,
context=p,
polyline_points=1,
report_category="Fields",
plot_type="Data Table",
plot_name=quantity,
)
PyAEDT INFO: PostProcessor class has been initialized! Elapsed time: 0m 0sec
PyAEDT INFO: Post class has been initialized! Elapsed time: 0m 0sec
Second option#
Create a design variable#
Parametrize the polyline x position.
[11]:
m2d["xl"] = "10mm"
Create polyline#
Create a parametrized polyline, specifying its ends.
[12]:
poly = m2d.modeler.create_polyline(
points=[["xl", -10, 0], ["xl", 10, 0]], name="polyline_sweep"
)
Add parametric sweep#
Add a parametric sweep where the parameter to sweep is xl
. Create a linear step sweep from 10mm
to 15mm
every 1mm
step.
[13]:
param_sweep = m2d.parametrics.add(
variable="xl",
start_point="10mm",
end_point="15mm",
step=1,
variation_type="LinearStep",
)
Compute magnetomotive force along the line#
Create and add a new formula to add in the PyAEDT advanced fields calculator.
[14]:
quantity_sweep = "H_field_{}".format(poly.name)
my_expression["name"] = quantity_sweep
my_expression["assignment"] = poly.name
m2d.post.fields_calculator.add_expression(my_expression, poly.name)
[14]:
'H_field_polyline_sweep'
Add parametric sweep calculation specifying the quantity (H) and save fields.#
[15]:
param_sweep.add_calculation(calculation=quantity_sweep, report_type="Fields", ranges={})
param_sweep.props["ProdOptiSetupDataV2"]["SaveFields"] = True
Create data table report#
Create a data table report to display H for each polyline position.
[16]:
report_sweep = m2d.post.create_report(
expressions=quantity_sweep,
report_category="Fields",
plot_type="Data Table",
plot_name=quantity_sweep,
primary_sweep_variable="xl",
variations={"xl": "All"},
)
Analyze parametric sweep#
[17]:
param_sweep.analyze(cores=NUM_CORES)
PyAEDT INFO: Key Desktop/ActiveDSOConfigurations/Maxwell 2D correctly changed.
PyAEDT INFO: Solving Optimetrics
PyAEDT INFO: Key Desktop/ActiveDSOConfigurations/Maxwell 2D correctly changed.
PyAEDT INFO: Design setup Parametric_EVSEHH solved correctly in 0.0h 0.0m 27.0s
[17]:
True
Export results#
Export results in a .csv file for the parametric sweep analysis (second option).
[18]:
m2d.post.export_report_to_csv(
project_dir=temp_folder.name,
plot_name=quantity_sweep,
)
[18]:
'C:\\Users\\ansys\\AppData\\Local\\Temp\\tmpc5dmj3u5.ansys\\H_field_polyline_sweep.csv'
Export results in a .csv file for each polyline (first option).
[19]:
[
m2d.post.export_report_to_csv(
project_dir=temp_folder.name,
plot_name=q,
)
for q in quantities
]
[19]:
['C:\\Users\\ansys\\AppData\\Local\\Temp\\tmpc5dmj3u5.ansys\\H_field_polyline.csv',
'C:\\Users\\ansys\\AppData\\Local\\Temp\\tmpc5dmj3u5.ansys\\H_field_polyline_1.csv',
'C:\\Users\\ansys\\AppData\\Local\\Temp\\tmpc5dmj3u5.ansys\\H_field_polyline_2.csv',
'C:\\Users\\ansys\\AppData\\Local\\Temp\\tmpc5dmj3u5.ansys\\H_field_polyline_3.csv',
'C:\\Users\\ansys\\AppData\\Local\\Temp\\tmpc5dmj3u5.ansys\\H_field_polyline_4.csv',
'C:\\Users\\ansys\\AppData\\Local\\Temp\\tmpc5dmj3u5.ansys\\H_field_polyline_5.csv',
'C:\\Users\\ansys\\AppData\\Local\\Temp\\tmpc5dmj3u5.ansys\\H_field_polyline_6.csv',
'C:\\Users\\ansys\\AppData\\Local\\Temp\\tmpc5dmj3u5.ansys\\H_field_polyline_7.csv',
'C:\\Users\\ansys\\AppData\\Local\\Temp\\tmpc5dmj3u5.ansys\\H_field_polyline_8.csv',
'C:\\Users\\ansys\\AppData\\Local\\Temp\\tmpc5dmj3u5.ansys\\H_field_polyline_9.csv']
Release AEDT#
[20]:
m2d.save_project()
m2d.release_desktop()
# Wait 3 seconds to allow AEDT to shut down before cleaning the temporary directory.
time.sleep(3)
PyAEDT INFO: Project Maxwell_Magnetic_Force Saved correctly
PyAEDT INFO: Desktop has been released and closed.
Clean up#
All project files are saved in the folder temp_folder.name
. If you’ve run this example as a Jupyter notebook, you can retrieve those project files. The following cell removes all temporary files, including the project folder.
[21]:
temp_folder.cleanup()
Download this example
Download this example as a Jupyter Notebook or as a Python script.