Skip to content

Commit d678f46

Browse files
Fix: fix examples for release (#444)
Co-authored-by: Samuel Lopez <[email protected]>
1 parent a8a17e0 commit d678f46

File tree

7 files changed

+99
-70
lines changed

7 files changed

+99
-70
lines changed

examples/high_frequency/layout/signal_integrity/ami.py

Lines changed: 56 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -143,26 +143,23 @@
143143
tstop_ns = scale_time * tstop
144144
tstart_ns = scale_time * tstart
145145

146-
for time_value in original_data_value[plot_name].index:
147-
if tstart_ns <= time_value[0]:
148-
start_index_original_data = time_value[0]
149-
break
150-
for time_value in original_data_value[plot_name][start_index_original_data:].index:
151-
if time_value[0] >= tstop_ns:
152-
stop_index_original_data = time_value[0]
153-
break
154-
for time_value in sample_waveform[0].index:
155-
if tstart <= time_value:
156-
sample_index = sample_waveform[0].index == time_value
157-
start_index_waveform = sample_index.tolist().index(True)
158-
break
159-
for time_value in sample_waveform[0].index:
160-
if time_value >= tstop:
161-
sample_index = sample_waveform[0].index == time_value
162-
stop_index_waveform = sample_index.tolist().index(True)
163-
break
164-
165-
original_data_zoom = original_data_value[
146+
orig_times = np.array([row[0] for row in original_data_value[plot_name]])
147+
start_index_original_data = int(np.searchsorted(orig_times, tstart_ns, side="left"))
148+
if start_index_original_data >= orig_times.size:
149+
start_index_original_data = orig_times.size - 1
150+
stop_index_original_data = int(np.searchsorted(orig_times, tstop_ns, side="left"))
151+
if stop_index_original_data >= orig_times.size:
152+
stop_index_original_data = orig_times.size - 1
153+
154+
sample_index_array = np.array(sample_waveform[0].index)
155+
start_index_waveform = int(np.searchsorted(sample_index_array, tstart, side="left"))
156+
if start_index_waveform >= sample_index_array.size:
157+
start_index_waveform = sample_index_array.size - 1
158+
stop_index_waveform = int(np.searchsorted(sample_index_array, tstop, side="left"))
159+
if stop_index_waveform >= sample_index_array.size:
160+
stop_index_waveform = sample_index_array.size - 1
161+
162+
original_data_zoom = original_data_value[plot_name][
166163
start_index_original_data:stop_index_original_data
167164
]
168165
sampled_data_zoom = (
@@ -175,8 +172,8 @@
175172
fig, ax = plt.subplots()
176173
ax.plot(sampled_time_zoom, sampled_data_zoom, "r*")
177174
ax.plot(
178-
np.array(list(original_data_zoom.index.values)),
179-
original_data_zoom.values,
175+
np.array(list(original_data_zoom[:,0])),
176+
original_data_zoom[:,1],
180177
color="blue",
181178
)
182179
ax.set_title("WaveAfterProbe")
@@ -190,7 +187,7 @@
190187
# Create the plot from a start time to stop time in seconds.
191188

192189
fig, ax2 = plt.subplots()
193-
ax2.plot(sample_waveform[0].index, sample_waveform[0].values, "r*")
190+
ax2.plot(sample_waveform[0].index, np.concatenate(sample_waveform[0].values), "r*")
194191
ax2.set_title("Slicer Scatter: WaveAfterProbe")
195192
ax2.set_xlabel("s")
196193
ax2.set_ylabel("V")
@@ -202,7 +199,7 @@
202199

203200
fig, ax4 = plt.subplots()
204201
ax4.set_title("Slicer Histogram: WaveAfterProbe")
205-
ax4.hist(sample_waveform[0].values, orientation="horizontal")
202+
ax4.hist(np.concatenate(sample_waveform[0].values), orientation="horizontal")
206203
ax4.set_ylabel("V")
207204
ax4.grid()
208205
plt.show()
@@ -226,9 +223,10 @@
226223

227224
# +
228225
original_data.enable_pandas_output = False
229-
original_data_value = original_data.data_real()
226+
original_data_value = original_data.get_expression_data(formula="real")[1]
230227
original_data_sweep = original_data.primary_sweep_values
231228
waveform_unit = original_data.units_data[plot_name]
229+
232230
waveform_sweep_unit = original_data.units_sweeps["Time"]
233231
tics = np.arange(20e-9, 100e-9, 1e-10, dtype=float)
234232

@@ -260,14 +258,14 @@
260258
tstop_ns = scale_time * tstop
261259
tstart_ns = scale_time * tstart
262260

263-
for time_value in original_data_sweep:
264-
if tstart_ns <= time_value:
265-
start_index_original_data = original_data_sweep.index(time_value.value)
266-
break
267-
for time_value in original_data_sweep[start_index_original_data:]:
268-
if time_value >= tstop_ns:
269-
stop_index_original_data = original_data_sweep.index(time_value.value)
270-
break
261+
start_index_original_data = int(np.searchsorted(original_data_sweep, tstart_ns, side="left"))
262+
if start_index_original_data >= original_data_sweep.size:
263+
start_index_original_data = original_data_sweep.size - 1
264+
265+
stop_index_original_data = int(np.searchsorted(original_data_sweep, tstop_ns, side="left"))
266+
if stop_index_original_data >= original_data_sweep.size:
267+
stop_index_original_data = original_data_sweep.size - 1
268+
271269
cont = 0
272270
for frame in sample_waveform:
273271
if tstart <= frame[0]:
@@ -290,11 +288,23 @@
290288
list(map(list, zip(original_sweep_zoom, original_data_zoom)))
291289
)
292290
original_data_zoom_array[:, 0] *= 1
293-
sampled_data_zoom_array = np.array(
294-
sample_waveform[start_index_waveform:stop_index_waveform]
295-
)
296-
sampled_data_zoom_array[:, 0] *= scale_time
297-
sampled_data_zoom_array[:, 1] *= scale_data
291+
sampled_slice = sample_waveform[start_index_waveform:stop_index_waveform]
292+
# Build a homogeneous Nx2 array [time, value] from the sliced frames, with a guard for empty slices.
293+
if len(sampled_slice):
294+
sampled_data_zoom_array = np.array(
295+
[
296+
[
297+
float(np.asarray(frame[0]).ravel()[0]),
298+
float(np.asarray(frame[1]).ravel()[0]),
299+
]
300+
for frame in sampled_slice
301+
],
302+
dtype=float,
303+
)
304+
sampled_data_zoom_array[:, 0] *= scale_time
305+
sampled_data_zoom_array[:, 1] *= scale_data
306+
else:
307+
sampled_data_zoom_array = np.empty((0, 2))
298308

299309
fig, ax = plt.subplots()
300310
ax.plot(sampled_data_zoom_array[:, 0], sampled_data_zoom_array[:, 1], "r*")
@@ -309,7 +319,14 @@
309319
#
310320
# Create the plot from a start time to stop time in seconds.
311321

312-
sample_waveform_array = np.array(sample_waveform)
322+
sample_waveform_array = np.array(
323+
[
324+
[float(frame[0]), float(np.asarray(frame[1]).ravel()[0])]
325+
for frame in sample_waveform
326+
],
327+
dtype=float,
328+
)
329+
313330
fig, ax2 = plt.subplots()
314331
ax2.plot(sample_waveform_array[:, 0], sample_waveform_array[:, 1], "r*")
315332
ax2.set_title("Slicer Scatter: " + plot_name)

examples/low_frequency/general/field_export.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import ansys.aedt.core
1919
from ansys.aedt.core.examples.downloads import download_file
20+
from ansys.aedt.core.generic.constants import unit_converter
2021
# -
2122

2223
# Define constants.
@@ -141,17 +142,17 @@
141142
# The J field is plotted on the surface of each coil for every time-step.
142143
# Fields data is exported to the temporary folder as an AEDTPLT file.
143144

144-
for time_step in time_steps:
145-
time_step = time_step.to("ms")
146-
145+
unit = data.units_sweeps["Time"]
146+
converted = unit_converter(time_steps, "Time", unit, "ms")
147+
for time_step in converted:
147148
m3d.post.create_fieldplot_surface(
148149
assignment=m3d.modeler.objects_by_name["Coil_A2"],
149150
quantity=quantity[0],
150-
plot_name="J_{}_ms".format(time_step.value),
151-
intrinsics={"Time": time_step.expression},
151+
plot_name="J_{}_ms".format(time_step),
152+
intrinsics={"Time": "ms"},
152153
)
153154
mean_j_field_export = m3d.post.export_field_plot(
154-
plot_name="J_{}_ms".format(time_step.value),
155+
plot_name="J_{}_ms".format(time_step),
155156
output_dir=temp_folder.name,
156157
file_format="aedtplt",
157158
)
@@ -160,11 +161,11 @@
160161
o for o in m3d.modeler.solid_objects if o.material_name == "copper"
161162
],
162163
quantity="Mag_J",
163-
plot_name="Mag_J_Coils_{}_ms".format(time_step.value),
164-
intrinsics={"Time": time_step.expression},
164+
plot_name="Mag_J_Coils_{}_ms".format(time_step),
165+
intrinsics={"Time": "ms"},
165166
)
166167
mag_j_field_export = m3d.post.export_field_plot(
167-
plot_name="Mag_J_Coils_{}_ms".format(time_step.value),
168+
plot_name="Mag_J_Coils_{}_ms".format(time_step),
168169
output_dir=temp_folder.name,
169170
file_format="aedtplt",
170171
)

examples/low_frequency/general/resistance.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@
178178
# and plot data outside AEDT.
179179

180180
data = report.get_solution_data()
181-
resistance = data.data_magnitude()
181+
resistance = data.get_expression_data(formula="magnitude")[1]
182182
material_index = data.primary_sweep_values
183183
data.primary_sweep = "MaterialIndex"
184184
data.plot(snapshot_path=os.path.join(temp_folder.name, "M2D_DCConduction.jpg"))

examples/low_frequency/magnetic/magnet_collector.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -111,15 +111,15 @@
111111

112112
# +
113113
collector = m3d.modeler.create_cylinder(
114-
orientation=ansys.aedt.core.constants.AXIS.Z,
114+
orientation=ansys.aedt.core.constants.Axis.Z,
115115
origin=[12, 13, 25],
116116
height="3mm",
117117
radius="9.5mm",
118118
axisdir="Z",
119119
name="collector",
120120
)
121121
cylinder2 = m3d.modeler.create_cylinder(
122-
orientation=ansys.aedt.core.constants.AXIS.Z,
122+
orientation=ansys.aedt.core.constants.Axis.Z,
123123
origin=[12, 13, 25],
124124
height="3mm",
125125
radius="8mm",
@@ -132,15 +132,15 @@
132132
)
133133

134134
cylinder3 = m3d.modeler.create_cylinder(
135-
orientation=ansys.aedt.core.constants.AXIS.Z,
135+
orientation=ansys.aedt.core.constants.Axis.Z,
136136
origin=[12, 13, 28],
137137
height="3mm",
138138
radius="8.7mm",
139139
axisdir="Z",
140140
name="cyl3",
141141
)
142142
cylinder4 = m3d.modeler.create_cylinder(
143-
orientation=ansys.aedt.core.constants.AXIS.Z,
143+
orientation=ansys.aedt.core.constants.Axis.Z,
144144
origin=[12, 13, 28],
145145
height="3mm",
146146
radius="2mm",
@@ -190,8 +190,8 @@
190190
)
191191
section = m3d.modeler.create_rectangle(
192192
orientation="XZ",
193-
position=[-25, 0, -35],
194-
dimension_list=["50mm", "50mm"],
193+
origin=[-25, 0, -35],
194+
sizes=["50mm", "50mm"],
195195
name="section",
196196
)
197197
m3d.modeler.set_working_coordinate_system("Global")

examples/low_frequency/motor/aedt_motor/ipm_optimization.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -294,9 +294,9 @@
294294
core_loss_data.active_variation = var
295295
solid_loss_data.active_variation = var
296296

297-
torque_values = torque_data.data_magnitude()
298-
core_loss_values = core_loss_data.data_magnitude()
299-
solid_loss_values = solid_loss_data.data_magnitude()
297+
torque_values = torque_data.get_expression_data(formula="magnitude")[1]
298+
core_loss_values = core_loss_data.get_expression_data(formula="magnitude")[1]
299+
solid_loss_values = solid_loss_data.get_expression_data(formula="magnitude")[1]
300300

301301
torque_data_average = sum(torque_values) / len(torque_values)
302302
core_loss_average = sum(core_loss_values) / len(core_loss_values)

examples/low_frequency/motor/aedt_motor/pm_synchronous.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import ansys.aedt.core
2020
import matplotlib.pyplot as plt
21+
import numpy as np
2122
from ansys.aedt.core.examples.downloads import download_leaf
2223
from ansys.aedt.core.generic.constants import unit_converter
2324
from ansys.aedt.core.generic.numbers_utils import Quantity
@@ -896,7 +897,7 @@ def create_cs_magnets(pm_id, cs_name, point_direction):
896897
#
897898
# List of shaft torque points and compute average.
898899

899-
mag = solutions.data_magnitude()
900+
mag = solutions.get_expression_data(formula="magnitude")[1]
900901
avg = sum(mag) / len(mag)
901902

902903
# ## Export a report to a file
@@ -930,12 +931,22 @@ def create_cs_magnets(pm_id, cs_name, point_direction):
930931

931932
# Find the indices corresponding to the start and stop times
932933

933-
index_start_time = time_interval.index(start_time)
934-
index_stop_time = time_interval.index(stop_time)
934+
# Convert Quantity objects to numeric values (time_intrinsics are in ns)
935+
numeric_start = start_time.value
936+
numeric_stop = stop_time.value
937+
938+
# Use numpy.searchsorted to find the indices in the numpy array
939+
index_start_time = int(np.searchsorted(time_interval, numeric_start, side="left"))
940+
index_stop_time = int(np.searchsorted(time_interval, numeric_stop, side="right"))
941+
942+
# Clamp indices to valid range
943+
index_start_time = max(0, min(index_start_time, len(time_interval) - 1))
944+
index_stop_time = max(0, min(index_stop_time, len(time_interval)))
935945

936946
# ## Extract the torque values within the specified time range
937947

938-
torque_values = solutions.data_real()
948+
# Ensure torque values are a numpy array for slicing
949+
torque_values = solutions.get_expression_data(formula="Real")[1]
939950
time_electric_period = time_interval[index_start_time:index_stop_time]
940951
torque_electric_period = torque_values[index_start_time:index_stop_time]
941952

examples/low_frequency/multiphysics/maxwell_icepak.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import time
1919

2020
import ansys.aedt.core # Interface to Ansys Electronics Desktop
21-
from ansys.aedt.core.generic.constants import AXIS
21+
from ansys.aedt.core.generic.constants import Axis
2222
# -
2323

2424
# ### Define constants
@@ -81,15 +81,15 @@
8181
coil = m3d.modeler.create_rectangle(
8282
orientation="XZ", origin=coil_origin, sizes=coil_xsection, name="Coil"
8383
)
84-
coil.sweep_around_axis(axis=AXIS.Z)
84+
coil.sweep_around_axis(axis=Axis.Z)
8585
coil_terminal = m3d.modeler.create_rectangle(
8686
orientation="XZ", origin=coil_origin, sizes=coil_xsection, name="Coil_terminal"
8787
)
8888

8989
core = m3d.modeler.create_rectangle(
9090
orientation="XZ", origin=core_origin, sizes=core_xsection, name="Core"
9191
)
92-
core.sweep_around_axis(axis=AXIS.Z)
92+
core.sweep_around_axis(axis=Axis.Z)
9393

9494
# The air region should be sufficiently large to avoid interaction with the
9595
# coil magnetic field.
@@ -216,11 +216,11 @@
216216
# +
217217
report = m3d.post.create_report(expressions="Matrix1.R(Winding1,Winding1)")
218218
solution = report.get_solution_data()
219-
resistance = solution.data_magnitude()[0] # Resistance is the first matrix element.
219+
resistance = solution.get_expression_data(formula="magnitude")[0][0] # Resistance is the first matrix element.
220220

221221
report_loss = m3d.post.create_report(expressions="StrandedLossAC")
222222
solution_loss = report_loss.get_solution_data()
223-
em_loss = solution_loss.data_magnitude()[0]
223+
em_loss = solution_loss.get_expression_data(formula="magnitude")[0][0]
224224
# -
225225

226226
# ### Analyitic calculation of DC resistance
@@ -349,7 +349,7 @@
349349
expressions="PointMonitor1.Temperature", primary_sweep_variable="X"
350350
)
351351
solution_temp = report_temp.get_solution_data()
352-
temp = solution_temp.data_magnitude()[0]
352+
temp = solution_temp.get_expression_data(formula="magnitude")[1][0]
353353
m3d.logger.info("*******Coil temperature = {:.2f}deg C".format(temp))
354354
# -
355355

@@ -360,12 +360,12 @@
360360
# +
361361
report_new = m3d.post.create_report(expressions="Matrix1.R(Winding1,Winding1)")
362362
solution_new = report_new.get_solution_data()
363-
resistance_new = solution_new.data_magnitude()[0]
363+
resistance_new = solution_new.get_expression_data(formula="magnitude")[1][0]
364364
resistance_increase = (resistance_new - resistance) / resistance * 100
365365

366366
report_loss_new = m3d.post.create_report(expressions="StrandedLossAC")
367367
solution_loss_new = report_loss_new.get_solution_data()
368-
em_loss_new = solution_loss_new.data_magnitude()[0]
368+
em_loss_new = solution_loss_new.get_expression_data(formula="magnitude")[1][0]
369369

370370
m3d.logger.info(
371371
"*******Coil resistance at 150kHz AFTER temperature feedback = {:.2f}Ohm".format(

0 commit comments

Comments
 (0)