Skip to content

Commit 4f3ee7b

Browse files
gmalinveanur7Samuelopez-ansys
authored
Add 2 options to calculate H field along a line (#201)
* add 2 options to calculato H field along a line * fix description * project_path corrected * project_path corrected --------- Co-authored-by: anur7 <nurabdun07@gmail.com> Co-authored-by: Samuel Lopez <85613111+Samuelopez-ansys@users.noreply.github.com>
1 parent 3b09576 commit 4f3ee7b

File tree

2 files changed

+262
-0
lines changed

2 files changed

+262
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
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 several lines.
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+
9+
# ## Perform required imports
10+
#
11+
# Perform required imports.
12+
13+
import os
14+
import tempfile
15+
import time
16+
17+
import pyaedt
18+
19+
# ## Define constants
20+
21+
AEDT_VERSION = "2024.2"
22+
NG_MODE = False
23+
24+
# ## Create temporary directory and download files
25+
#
26+
# Create a temporary directory where we store downloaded data or
27+
# dumped data.
28+
# If you'd like to retrieve the project data for subsequent use,
29+
# the temporary folder name is given by ``temp_folder.name``.
30+
31+
temp_folder = tempfile.TemporaryDirectory(suffix=".ansys")
32+
33+
# ## Import project
34+
#
35+
# The files required to run this example will be downloaded into the temporary working folder.
36+
37+
project_path = pyaedt.downloads.download_file(
38+
source="maxwell_magnetic_force",
39+
name="Maxwell_Magnetic_Force.aedt",
40+
destination=temp_folder.name,
41+
)
42+
43+
# ## Initialize and launch Maxwell 2D
44+
#
45+
# Initialize and launch Maxwell 2D, providing the version and the path of the project.
46+
47+
m2d = pyaedt.Maxwell2d(
48+
version=AEDT_VERSION,
49+
non_graphical=NG_MODE,
50+
project=project_path,
51+
design="Maxwell2DDesign1",
52+
)
53+
54+
# ## Create a polyline
55+
#
56+
# Create a polyline specifying its points.
57+
58+
poly = m2d.modeler.create_polyline(points=[[10, -10, 0], [10, 10, 0]], name="polyline")
59+
60+
# Duplicate polyline along a vector
61+
62+
polys = [poly.name]
63+
polys.extend(poly.duplicate_along_line(vector=[-0.5, 0, 0], clones=10))
64+
65+
# ## Analyze setup
66+
#
67+
# Analyze setup specifying setup name
68+
69+
m2d.analyze_setup(name=m2d.setups[0].name)
70+
71+
# ## Compute magnetomotive force along each line
72+
#
73+
# Create and add a new formula to add in PyAEDT advanced fields calculator.
74+
# Create fields report object and get field data.
75+
# Create a Data Table report for H field along each line and export it in a .csv file.
76+
77+
for p in polys:
78+
quantity = "H_field_{}".format(p)
79+
my_expression = {
80+
"name": quantity,
81+
"description": "Magnetomotive force along a line",
82+
"design_type": ["Maxwell 2D", "Maxwell 3D"],
83+
"fields_type": ["Fields"],
84+
"primary_sweep": "distance",
85+
"assignment": p,
86+
"assignment_type": ["Line"],
87+
"operations": [
88+
"Fundamental_Quantity('H')",
89+
"Operation('Tangent')",
90+
"Operation('Dot')",
91+
"EnterLine('assignment')",
92+
"Operation('LineValue')",
93+
"Operation('Integrate')",
94+
],
95+
"report": ["Data Table"],
96+
}
97+
m2d.post.fields_calculator.add_expression(my_expression, p)
98+
report = m2d.post.reports_by_category.fields(
99+
expressions=quantity, setup=m2d.nominal_sweep, polyline=p
100+
)
101+
data = report.get_solution_data()
102+
h = data.data_magnitude()
103+
report = m2d.post.create_report(
104+
expressions=quantity,
105+
context=p,
106+
polyline_points=1,
107+
report_category="Fields",
108+
plot_type="Data Table",
109+
plot_name=quantity,
110+
)
111+
m2d.post.export_report_to_csv(
112+
project_dir=os.path.join(temp_folder.name, "{}.csv".format(quantity)),
113+
plot_name=quantity,
114+
)
115+
116+
# ## Release AEDT and clean up temporary directory
117+
#
118+
# Release AEDT and remove both the project and temporary directory.
119+
120+
m2d.release_desktop()
121+
122+
time.sleep(3)
123+
temp_folder.cleanup()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
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

Comments
 (0)