Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace unnecessary try-excepts with hasattr #173

Merged
merged 1 commit into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/drive/flux_vector/plot_flux_vector_pmsyrm_5kw.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,4 +196,4 @@ def i_s(psi_s):
#
# .. [#Lel2024] Lelli, Hinkkanen, Giulii Capponi, "A saturation model based on
# a simplified equivalent magnetic circuit for permanent magnet machines,"
# TechRxiv., 2024, https://doi.org/10.36227/techrxiv.171332345.53790692/v1
# Proc. ICEM, 2024, https://doi.org/10.1109/ICEM60801.2024.10700403
30 changes: 14 additions & 16 deletions motulator/drive/control/sm/_torque.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ def current_limit(self, max_i_s, gamma1=np.pi, gamma2=0, N=20):

gamma = np.linspace(gamma1, gamma2, N)

# MTPA locus expressed with different quantities
# Current limit expressed with different quantities
i_s = max_i_s*np.exp(1j*gamma)
psi_s = self.flux(i_s)
tau_M = self.torque(psi_s)
Expand Down Expand Up @@ -475,13 +475,12 @@ def plot_flux_loci(self, max_i_s, base, N=20):
_, ax = plt.subplots()
ax.plot(
mtpa.psi_s.real/base.psi, mtpa.psi_s.imag/base.psi, label="MTPA")
try:

if hasattr(mtpv, 'psi_s') and np.any(mtpv.psi_s):
ax.plot(
mtpv.psi_s.real/base.psi,
mtpv.psi_s.imag/base.psi,
label="MTPV")
except AttributeError:
pass
ax.plot(
current_lim.psi_s.real/base.psi,
current_lim.psi_s.imag/base.psi,
Expand Down Expand Up @@ -519,10 +518,10 @@ def plot_current_loci(self, max_i_s, base, N=20):
# Plot the i_sd--i_sq current plane
_, ax = plt.subplots()
ax.plot(mtpa.i_s.real/base.i, mtpa.i_s.imag/base.i, label="MTPA")
try:

if hasattr(mtpv, 'i_s') and np.any(mtpv.i_s):
ax.plot(mtpv.i_s.real/base.i, mtpv.i_s.imag/base.i, label="MTPV")
except AttributeError:
pass

ax.plot(
current_lim.i_s.real/base.i,
current_lim.i_s.imag/base.i,
Expand Down Expand Up @@ -565,10 +564,10 @@ def plot_torque_current(self, max_i_s, base, N=20):
# Plot i_sd vs. tau_M
_, (ax1, ax2) = plt.subplots(2, 1)
ax1.plot(mtpa.tau_M/base.tau, mtpa.i_s.real/base.i)
try:

if hasattr(mtpv, 'tau_M') and hasattr(mtpv.i_s, 'real'):
ax1.plot(mtpv.tau_M/base.tau, mtpv.i_s.real/base.i)
except AttributeError:
pass

ax1.plot(current_lim.tau_M/base.tau, current_lim.i_s.real/base.i)

ax1.set_xlim(0, max_i_s/base.i)
Expand All @@ -581,10 +580,9 @@ def plot_torque_current(self, max_i_s, base, N=20):

# Plot i_sq vs. tau_M
ax2.plot(mtpa.tau_M/base.tau, mtpa.i_s.imag/base.i)
try:
if hasattr(mtpv, 'tau_M') and hasattr(mtpv.i_s, 'imag'):
ax2.plot(mtpv.tau_M/base.tau, mtpv.i_s.imag/base.i)
except TypeError:
pass

ax2.plot(current_lim.tau_M/base.tau, current_lim.i_s.imag/base.i)

ax2.set_xlim(0, max_i_s/base.i)
Expand Down Expand Up @@ -619,10 +617,10 @@ def plot_torque_flux(self, max_i_s, base, N=20):
# Plot
_, ax = plt.subplots(1, 1)
ax.plot(np.abs(mtpa.psi_s)/base.psi, mtpa.tau_M/base.tau)
try:

if hasattr(mtpv, 'psi_s') and hasattr(mtpv, 'tau_M'):
ax.plot(np.abs(mtpv.psi_s)/base.psi, mtpv.tau_M/base.tau)
except AttributeError:
pass

ax.plot(np.abs(current_lim.psi_s)/base.psi, current_lim.tau_M/base.tau)

ax.legend(["MTPA", "MTPV", "Constant current"])
Expand Down
72 changes: 22 additions & 50 deletions motulator/drive/utils/_plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
plt.rcParams.update({"text.usetex": False})


def _get_machine_type(mdl):
return "im" if hasattr(mdl.machine.data, 'psi_Rs') else "sm"


# %%
# pylint: disable=too-many-branches
def plot(sim, base=None, t_span=None):
Expand All @@ -35,7 +39,6 @@ def plot(sim, base=None, t_span=None):

"""
# pylint: disable=too-many-statements
#mdl = sim.mdl.data # Continuous-time data
mdl = sim.mdl # Continuous-time data
ctrl = sim.ctrl.data # Discrete-time data
ctrl.t = ctrl.ref.t # Discrete time
Expand All @@ -51,63 +54,48 @@ def plot(sim, base=None, t_span=None):
else:
pu_vals = True

# Recognize the motor type by checking if the rotor flux data exist
try:
if mdl.psi_Rs is not None:
motor_type = "im"
except AttributeError:
motor_type = "sm"

fig, (ax1, ax2, ax3, ax4, ax5) = plt.subplots(5, 1, figsize=(8, 10))

# Subplot 1: angular speeds
try:
if hasattr(ctrl.ref, 'w_m') and np.any(ctrl.ref.w_m):
ax1.plot(
ctrl.t,
ctrl.ref.w_m/base.w,
"--",
ds="steps-post",
label=r"$\omega_\mathrm{m,ref}$")
except (AttributeError, TypeError):
pass
ax1.plot(
mdl.machine.data.t,
mdl.machine.data.w_m/base.w,
label=r"$\omega_\mathrm{m}$")
try:
if hasattr(ctrl.fbk, 'w_m'):
ax1.plot(
ctrl.t,
ctrl.fbk.w_m/base.w,
label=r"$\hat \omega_\mathrm{m}$",
ds="steps-post")
except AttributeError:
pass
ax1.legend()
ax1.set_xlim(t_span)
ax1.set_xticklabels([])

# Subplot 2: torques
try:
if hasattr(mdl.mechanics.data, 'tau_L_tot'):
ax2.plot(
mdl.mechanics.data.t,
mdl.mechanics.data.tau_L_tot/base.tau,
":",
label=r"$\tau_\mathrm{L,tot}$")
except AttributeError:
pass
ax2.plot(
mdl.machine.data.t,
mdl.machine.data.tau_M/base.tau,
label=r"$\tau_\mathrm{M}$")
try:
if hasattr(ctrl.ref, 'tau_M'):
ax2.plot(
ctrl.t,
ctrl.ref.tau_M/base.tau,
"--",
label=r"$\tau_\mathrm{M,ref}$",
ds="steps-post")
except AttributeError:
pass
ax2.legend()
ax2.set_xlim(t_span)
ax2.set_xticklabels([])
Expand All @@ -123,7 +111,7 @@ def plot(sim, base=None, t_span=None):
ctrl.fbk.i_s.imag/base.i,
label=r"$i_\mathrm{sq}$",
ds="steps-post")
try:
if hasattr(ctrl.ref, 'i_s'):
ax3.plot(
ctrl.t,
ctrl.ref.i_s.real/base.i,
Expand All @@ -136,8 +124,6 @@ def plot(sim, base=None, t_span=None):
"--",
label=r"$i_\mathrm{sq,ref}$",
ds="steps-post")
except (AttributeError, TypeError):
pass
ax3.legend()
ax3.set_xlim(t_span)
ax3.set_xticklabels([])
Expand All @@ -159,44 +145,39 @@ def plot(sim, base=None, t_span=None):
ax4.set_xticklabels([])

# Subplot 5: flux linkages
if motor_type == "sm":
if _get_machine_type(mdl) == "sm":
ax5.plot(
mdl.machine.data.t,
np.abs(mdl.machine.data.psi_ss)/base.psi,
label=r"$\psi_\mathrm{s}$")
try:
if hasattr(ctrl.fbk, 'psi_s'):
ax5.plot(
ctrl.t,
np.abs(ctrl.fbk.psi_s)/base.psi,
label=r"$\hat\psi_\mathrm{s}$",
ds="steps-post")
except (AttributeError, TypeError):
pass
try:
if hasattr(ctrl.ref, 'psi_s'):
ax5.plot(
ctrl.t,
np.abs(ctrl.ref.psi_s)/base.psi,
"--",
label=r"$\psi_\mathrm{s,ref}$",
ds="steps-post")
except (AttributeError, TypeError):
pass

else:
ax5.plot(
mdl.machine.data.t,
np.abs(mdl.machine.data.psi_ss)/base.psi,
label=r"$\psi_\mathrm{s}$")
ax5.plot(
mdl.t, np.abs(mdl.psi_Rs)/base.psi, label=r"$\psi_\mathrm{R}$")
try:
mdl.machine.data.t,
np.abs(mdl.machine.data.psi_Rs)/base.psi,
label=r"$\psi_\mathrm{R}$")
if hasattr(ctrl.fbk, 'psi_s'):
ax5.plot(
ctrl.t,
np.abs(ctrl.fbk.psi_s)/base.psi,
label=r"$\hat\psi_\mathrm{s}$",
ds="steps-post")
except AttributeError:
pass
ax5.legend()
ax5.set_xlim(t_span)

Expand Down Expand Up @@ -252,10 +233,10 @@ def plot_extra(sim, base=None, t_span=None):
base = SimpleNamespace(w=1, u=1, i=1, psi=1, tau=1)

# Angle of synchronous coordinates
try:
theta = ctrl.fbk.theta_s # Induction machine
except AttributeError:
theta = ctrl.fbk.theta_m # Synchronous machine
if _get_machine_type(mdl) == "sm":
theta = ctrl.fbk.theta_m
else:
theta = ctrl.fbk.theta_s

# Quantities in stator coordinates
ctrl.fbk.u_ss = np.exp(1j*theta)*ctrl.fbk.u_s
Expand Down Expand Up @@ -301,12 +282,7 @@ def plot_extra(sim, base=None, t_span=None):
fig1.align_ylabels()

# Plots the DC bus and grid-side variables (if exist)
try:
mdl.converter.data.i_L
except AttributeError:
mdl.converter.data.i_L = None

if mdl.converter.data.i_L is not None:
if hasattr(mdl.converter.data, 'i_L'):
fig2, (ax1, ax2) = plt.subplots(2, 1)

# Subplot 1: voltages
Expand Down Expand Up @@ -349,12 +325,8 @@ def plot_extra(sim, base=None, t_span=None):
else:
ax1.set_ylabel("Voltage (V)")
ax2.set_ylabel("Current (A)")
ax2.set_xlabel("Time (s)")

try:
fig2.align_ylabels()
except UnboundLocalError:
pass
fig2.align_ylabels()

plt.tight_layout()
plt.show()
Expand Down
Loading