|
| 1 | +# # Maxwell 2D: Magnetomotive force calculation along a contour |
| 2 | +# |
| 3 | +# This example shows how to use PyAEDT to calculate |
| 4 | +# the magnetomotive force along a line that changes position. |
| 5 | +# It shows how to leverage PyAEDT advanced fields calculator |
| 6 | +# to insert a custom formula, in this case the integral |
| 7 | +# of the H field along a line |
| 8 | +# and compute the field for each position with a parametric sweep. |
| 9 | + |
| 10 | +# ## Perform required imports |
| 11 | +# |
| 12 | +# Perform required imports. |
| 13 | + |
| 14 | +import os |
| 15 | +import tempfile |
| 16 | +import time |
| 17 | + |
| 18 | +import pyaedt |
| 19 | + |
| 20 | +# ## Define constants |
| 21 | + |
| 22 | +AEDT_VERSION = "2024.2" |
| 23 | +NG_MODE = False |
| 24 | + |
| 25 | +# ## Create temporary directory and download files |
| 26 | +# |
| 27 | +# Create a temporary directory where we store downloaded data or |
| 28 | +# dumped data. |
| 29 | +# If you'd like to retrieve the project data for subsequent use, |
| 30 | +# the temporary folder name is given by ``temp_folder.name``. |
| 31 | + |
| 32 | +temp_folder = tempfile.TemporaryDirectory(suffix=".ansys") |
| 33 | + |
| 34 | +# ## Import project |
| 35 | +# |
| 36 | +# The files required to run this example will be downloaded into the temporary working folder. |
| 37 | + |
| 38 | +project_path = pyaedt.downloads.download_file( |
| 39 | + source="maxwell_magnetic_force", |
| 40 | + name="Maxwell_Magnetic_Force.aedt", |
| 41 | + destination=temp_folder.name, |
| 42 | +) |
| 43 | + |
| 44 | +# ## Initialize and launch Maxwell 2D |
| 45 | +# |
| 46 | +# Initialize and launch Maxwell 2D, providing the version and the path of the project. |
| 47 | + |
| 48 | +m2d = pyaedt.Maxwell2d( |
| 49 | + version=AEDT_VERSION, |
| 50 | + non_graphical=NG_MODE, |
| 51 | + project=project_path, |
| 52 | + design="Maxwell2DDesign1", |
| 53 | +) |
| 54 | + |
| 55 | +# ## Create a new design variable |
| 56 | +# |
| 57 | +# Parametrize polyline x position. |
| 58 | + |
| 59 | +m2d["xl"] = "10mm" |
| 60 | + |
| 61 | +# ## Create a polyline |
| 62 | +# |
| 63 | +# Create a polyline specifying its points. |
| 64 | + |
| 65 | +poly = m2d.modeler.create_polyline( |
| 66 | + points=[["xl", -10, 0], ["xl", 10, 0]], name="polyline" |
| 67 | +) |
| 68 | + |
| 69 | +# ## Add a parametric sweep |
| 70 | +# |
| 71 | +# Add a parametric sweep where the parameter to sweep is ``xl``. |
| 72 | +# Create a linear step sweep from ``10mm`` to ``15mm`` every ``1mm`` step. |
| 73 | + |
| 74 | +param_sweep = m2d.parametrics.add( |
| 75 | + variable="xl", |
| 76 | + start_point="10mm", |
| 77 | + end_point="15mm", |
| 78 | + step=1, |
| 79 | + variation_type="LinearStep", |
| 80 | +) |
| 81 | + |
| 82 | +# ## Compute magnetomotive force along the line |
| 83 | +# |
| 84 | +# Create and add a new formula to add in PyAEDT advanced fields calculator. |
| 85 | + |
| 86 | +quantity = "H_field_{}".format(poly.name) |
| 87 | +my_expression = { |
| 88 | + "name": quantity, |
| 89 | + "description": "Magnetomotive force along a line", |
| 90 | + "design_type": ["Maxwell 2D", "Maxwell 3D"], |
| 91 | + "fields_type": ["Fields"], |
| 92 | + "primary_sweep": "distance", |
| 93 | + "assignment": poly.name, |
| 94 | + "assignment_type": ["Line"], |
| 95 | + "operations": [ |
| 96 | + "Fundamental_Quantity('H')", |
| 97 | + "Operation('Tangent')", |
| 98 | + "Operation('Dot')", |
| 99 | + "EnterLine('assignment')", |
| 100 | + "Operation('LineValue')", |
| 101 | + "Operation('Integrate')", |
| 102 | + ], |
| 103 | + "report": ["Data Table"], |
| 104 | +} |
| 105 | +m2d.post.fields_calculator.add_expression(my_expression, poly.name) |
| 106 | + |
| 107 | +# ## Add parametric sweep calculation specifying the quantity (H). |
| 108 | + |
| 109 | +param_sweep.add_calculation(calculation=quantity, report_type="Fields", ranges={}) |
| 110 | + |
| 111 | +# ## Analyze parametric sweep |
| 112 | + |
| 113 | +param_sweep.analyze() |
| 114 | + |
| 115 | +# ## Create a data table report |
| 116 | +# |
| 117 | +# Create a data table report to display H for each polyline position. |
| 118 | +# Afterward export results in a .csv file. |
| 119 | + |
| 120 | +report = m2d.post.create_report( |
| 121 | + expressions=quantity, |
| 122 | + report_category="Fields", |
| 123 | + plot_type="Data Table", |
| 124 | + plot_name=quantity, |
| 125 | + primary_sweep_variable="xl", |
| 126 | +) |
| 127 | +m2d.post.export_report_to_csv( |
| 128 | + project_dir=os.path.join(temp_folder.name, "{}.csv".format(quantity)), |
| 129 | + plot_name=quantity, |
| 130 | +) |
| 131 | + |
| 132 | +# ## Release AEDT and clean up temporary directory |
| 133 | +# |
| 134 | +# Release AEDT and remove both the project and temporary directory. |
| 135 | + |
| 136 | +m2d.release_desktop() |
| 137 | + |
| 138 | +time.sleep(3) |
| 139 | +temp_folder.cleanup() |
0 commit comments