Skip to content

t_eval is not respected for system with 0 states #742

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

Closed
hyumo opened this issue Jun 6, 2022 · 1 comment · Fixed by #743
Closed

t_eval is not respected for system with 0 states #742

hyumo opened this issue Jun 6, 2022 · 1 comment · Fixed by #743
Labels

Comments

@hyumo
Copy link

hyumo commented Jun 6, 2022

It seems that t_eval is ignored when simulating a system with nstates=0.

The bug seems to be located at:

if nstates == 0:
# No states => map input to output
u = U[0] if len(U.shape) == 1 else U[:, 0]
y = np.zeros((np.shape(sys._out(T[0], X0, u))[0], len(T)))
for i in range(len(T)):
u = U[i] if len(U.shape) == 1 else U[:, i]
y[:, i] = sys._out(T[i], [], u)
return TimeResponseData(
T, y, None, U, issiso=sys.issiso(),
output_labels=sys.output_index, input_labels=sys.input_index,
transpose=transpose, return_x=return_x, squeeze=squeeze)

Instead, the following lines are expected to be used, I think:

def ufun(t):
# Find the value of the index using linear interpolation
# Use clip to allow for extrapolation if t is out of range
idx = np.clip(np.searchsorted(T, t, side='left'), 1, len(T)-1)
dt = (t - T[idx-1]) / (T[idx] - T[idx-1])
return U[..., idx-1] * (1. - dt) + U[..., idx] * dt
# Check to make sure this is not a static function
if nstates == 0: # No states => map input to output
# Make sure the user gave a time vector for evaluation (or 'T')
if t_eval is None:
# User overrode t_eval with None, but didn't give us the times...
warn("t_eval set to None, but no dynamics; using T instead")
t_eval = T
# Allocate space for the inputs and outputs
u = np.zeros((ninputs, len(t_eval)))
y = np.zeros((noutputs, len(t_eval)))
# Compute the input and output at each point in time
for i, t in enumerate(t_eval):
u[:, i] = ufun(t)
y[:, i] = sys._out(t, [], u[:, i])
return TimeResponseData(
t_eval, y, None, u, issiso=sys.issiso(),
output_labels=sys.output_index, input_labels=sys.input_index,
transpose=transpose, return_x=return_x, squeeze=squeeze)

@murrayrm
Copy link
Member

murrayrm commented Jun 7, 2022

Good catch @hyumo. The first check for nstates == 0 was redundant, so deleting it fixed the problem. I also added some unit tests to catch this error.

This issue will close when #743 is merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants