-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMaxwell2D_DCConduction.py
320 lines (255 loc) · 9.02 KB
/
Maxwell2D_DCConduction.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# # Resistance calculation
# This example uses PyAEDT to set up a resistance calculation
# and solve it using the Maxwell 2D DCConduction solver.
#
# Keywords: **Maxwell 2D**, **DXF import**, **material sweep**, **expression cache**.
# ## Perform required imports
#
# Perform required imports.
import os.path
import tempfile
import time
import ansys.aedt.core
from ansys.aedt.core.generic.pdf import AnsysReport
# ## Define constants
AEDT_VERSION = "2024.2"
NG_MODE = False
NUM_CORES = 4
# ## Create temporary directory
#
# The temporary directory is used to run the example and save simulation data.
# If you'd like to retrieve the project data for subsequent use,
# the temporary folder name is given by ``temp_folder.name``.
temp_folder = tempfile.TemporaryDirectory(suffix=".ansys")
# ## Launch AEDT and Maxwell 2D
#
# Launch AEDT and Maxwell 2D after first setting up the project and design names,
# the solver, and the version. The following code also creates an instance of the
# ``Maxwell2d`` class named ``m2d``.
project_name = os.path.join(temp_folder.name, "M2D_DC_Conduction.aedt")
m2d = ansys.aedt.core.Maxwell2d(
version=AEDT_VERSION,
new_desktop=True,
close_on_exit=True,
solution_type="DCConduction",
project=project_name,
design="Ansys_resistor",
non_graphical=NG_MODE,
)
# ## Import geometry as a DXF file
#
# You can test importing a DXF or a Parasolid file by commenting/uncommenting
# the following lines.
# Importing DXF files only works in graphical mode.
# +
# DXFPath = ansys.aedt.core.downloads.download_file("dxf", "Ansys_logo_2D.dxf")
# dxf_layers = m2d.get_dxf_layers(DXFPath)
# m2d.import_dxf(DXFPath, dxf_layers, scale=1E-05)
parasolid_path = ansys.aedt.core.downloads.download_file(
directory="x_t", filename="Ansys_logo_2D.x_t", destination=temp_folder.name
)
m2d.modeler.import_3d_cad(parasolid_path)
# -
# ## Define variables
#
# Define conductor thickness in z-direction, material array with 4 materials,
# and MaterialIndex referring to the material array.
m2d["MaterialThickness"] = "5mm"
m2d["ConductorMaterial"] = '["Copper", "Aluminum", "silver", "gold"]'
material_index = 0
m2d["MaterialIndex"] = str(material_index)
no_materials = 4
# ## Assign materials
#
# Voltage ports will be defined as gold, conductor
# gets the material defined by the 0th entry of the material array.
m2d.assign_material(obj=["ANSYS_LOGO_2D_1", "ANSYS_LOGO_2D_2"], mat="gold")
m2d.modeler["ANSYS_LOGO_2D_3"].material_name = "ConductorMaterial[MaterialIndex]"
# ## Assign voltages
#
# 1V and 0V.
m2d.assign_voltage(assignment=["ANSYS_LOGO_2D_1"], amplitude=1, name="1V")
m2d.assign_voltage(assignment=["ANSYS_LOGO_2D_2"], amplitude=0, name="0V")
# ## Setup conductance calculation
#
# 1V is the source, 0V ground.
m2d.assign_matrix(assignment=["1V"], group_sources=["0V"], matrix_name="Matrix1")
# ## Assign mesh operation
#
# 3mm on the .
m2d.mesh.assign_length_mesh(
assignment=["ANSYS_LOGO_2D_3"],
name="conductor",
maximum_length=3,
maximum_elements=None,
)
# ## Create simulation setup and enable expression cache
#
# Create simulation setup with minimum 4 adaptive passes to ensure convergence.
# Enable expression cache to observe the convergence.
setup = m2d.create_setup(setupname="Setup1", MinimumPasses=4)
setup.enable_expression_cache(
report_type="DCConduction",
expressions="1/Matrix1.G(1V,1V)/MaterialThickness",
isconvergence=True,
conv_criteria=1,
use_cache_for_freq=False,
)
# ## Analyze setup
#
# Run the analysis.
m2d.save_project()
m2d.analyze(setup=setup.name, cores=NUM_CORES, use_auto_settings=False)
# ## Create parametric sweep
#
# Create parametric sweep to sweep all the entries in the material array.
# Save fields and mesh and use the mesh for all the materials.
sweep = m2d.parametrics.add(
sweep_var="MaterialIndex",
start_point=0,
end_point=no_materials - 1,
step=1,
variation_type="LinearStep",
parametricname="MaterialSweep",
)
sweep["SaveFields"] = True
sweep["CopyMesh"] = True
sweep["SolveWithCopiedMeshOnly"] = True
sweep.analyze(cores=NUM_CORES)
# ## Create resistance report
#
# Create R. vs. material report.
# +
variations = {"MaterialIndex": ["All"], "MaterialThickness": ["Nominal"]}
report = m2d.post.create_report(
expressions="1/Matrix1.G(1V,1V)/MaterialThickness",
primary_sweep_variable="MaterialIndex",
report_category="DCConduction",
plot_type="Data Table",
variations=variations,
plot_name="Resistance vs. Material",
)
# ## Get solution data
#
# Get solution data using the object ``report``` to get resistance values
# and plot data outside AEDT.
data = report.get_solution_data()
resistance = data.data_magnitude()
material_index = data.primary_sweep_values
data.primary_sweep = "MaterialIndex"
data.plot(snapshot_path=os.path.join(temp_folder.name, "M2D_DCConduction.jpg"))
# ## Create material index vs resistance table
#
# Create material index vs resistance table to use in PDF report generator.
# Create ``colors`` table to customize each row of the material index vs resistance table.
material_index_vs_resistance = [["Material", "Resistance"]]
colors = [[(255, 255, 255), (0, 255, 0)]]
for i in range(len(data.primary_sweep_values)):
material_index_vs_resistance.append(
[str(data.primary_sweep_values[i]), str(resistance[i])]
)
colors.append([None, None])
# -
# ## Field overlay
#
# Plot electric field and current density on the conductor surface.
conductor_surface = m2d.modeler["ANSYS_LOGO_2D_3"].faces
plot1 = m2d.post.create_fieldplot_surface(
assignment=conductor_surface, quantity="Mag_E", plot_name="Electric Field"
)
plot2 = m2d.post.create_fieldplot_surface(
assignment=conductor_surface, quantity="Mag_J", plot_name="Current Density"
)
# ## Field overlay
#
# Plot electric field using pyvista and saving to an image.
py_vista_plot = m2d.post.plot_field(
quantity="Mag_E", assignment=conductor_surface, plot_cad_objs=False, show=False
)
py_vista_plot.isometric_view = False
py_vista_plot.camera_position = [0, 0, 7]
py_vista_plot.focal_point = [0, 0, 0]
py_vista_plot.roll_angle = 0
py_vista_plot.elevation_angle = 0
py_vista_plot.azimuth_angle = 0
py_vista_plot.plot(os.path.join(temp_folder.name, "mag_E.jpg"))
# ## Field animation
#
# Plot current density vs the Material index.
animated_plot = m2d.post.plot_animated_field(
quantity="Mag_J",
assignment=conductor_surface,
export_path=temp_folder.name,
variation_variable="MaterialIndex",
variations=[0, 1, 2, 3],
show=False,
export_gif=False,
log_scale=True,
)
animated_plot.isometric_view = False
animated_plot.camera_position = [0, 0, 7]
animated_plot.focal_point = [0, 0, 0]
animated_plot.roll_angle = 0
animated_plot.elevation_angle = 0
animated_plot.azimuth_angle = 0
animated_plot.animate()
# ## Export model picture
#
# Export model picture.
model_picture = m2d.post.export_model_picture()
# ## Generate PDF report
#
# Generate a PDF report with output of simulation.
pdf_report = AnsysReport(
project_name=m2d.project_name, design_name=m2d.design_name, version=AEDT_VERSION
)
# Customize text font.
pdf_report.report_specs.font = "times"
pdf_report.report_specs.text_font_size = 10
# Create report
pdf_report.create()
# Add project's design info to report.
pdf_report.add_project_info(m2d)
# Add model picture in a new chapter and add text.
pdf_report.add_chapter("Model Picture")
pdf_report.add_text("This section contains the model picture")
pdf_report.add_image(path=model_picture, caption="Model Picture", width=80, height=60)
# Add in a new chapter field overlay plots.
pdf_report.add_chapter("Field overlay")
pdf_report.add_sub_chapter("Plots")
pdf_report.add_text("This section contains the fields overlay.")
pdf_report.add_image(
os.path.join(temp_folder.name, "mag_E.jpg"), caption="Mag E", width=120, height=80
)
pdf_report.add_page_break()
# Add a new section to display results.
pdf_report.add_section()
pdf_report.add_chapter("Results")
pdf_report.add_sub_chapter("Resistance vs. Material")
pdf_report.add_text("This section contains resistance vs material data.")
# Aspect ratio is automatically calculated if only width is provided
pdf_report.add_image(os.path.join(temp_folder.name, "M2D_DCConduction.jpg"), width=130)
# Add a new subchapter to display resistance data from previously created table.
pdf_report.add_sub_chapter("Resistance data table")
pdf_report.add_text("This section contains Resistance data.")
pdf_report.add_table(
title="Resistance Data",
content=material_index_vs_resistance,
formatting=colors,
col_widths=[75, 100],
)
# Add table of content and save PDF.
pdf_report.add_toc()
pdf_report.save_pdf(temp_folder.name, "AEDT_Results.pdf")
# ## Release AEDT
m2d.save_project()
m2d.release_desktop()
# Wait 3 seconds to allow Electronics Desktop to shut down before cleaning the temporary directory.
time.sleep(3)
# ## Cleanup
#
# All project files are saved in the folder ``temp_dir.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.
temp_folder.cleanup()