Skip to content

Commit

Permalink
Fixed issues with scipy sparse storage in scipy 1.11 (#943)
Browse files Browse the repository at this point in the history
* updates for changes to scipy sparse

* efficiency improvement by avoiding a format change
  • Loading branch information
robfalck authored Jun 28, 2023
1 parent 90e95d3 commit 6ae47d1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
7 changes: 4 additions & 3 deletions dymos/transcriptions/common/control_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,12 @@ def _configure_controls(self):

size = np.prod(shape)
self.sizes[name] = size
sp_eye = sp.eye(size, format='csr')

# The partial of interpolated value wrt the control input values is linear
# and can be computed as the kronecker product of the interpolation matrix (L)
# and eye(size).
J_val = sp.kron(self.L, sp.eye(size), format='csr')
J_val = sp.kron(self.L, sp_eye, format='csr')
rs, cs, data = sp.find(J_val)
self.declare_partials(of=self._output_val_names[name],
wrt=self._input_names[name],
Expand All @@ -138,14 +139,14 @@ def _configure_controls(self):
# The partials of the rates and second derivatives are nonlinear but the sparsity
# pattern is obtained from the kronecker product of the 1st and 2nd differentiation
# matrices (D and D2) and eye(size).
self.rate_jacs[name] = sp.kron(sp.csr_matrix(self.D), sp.eye(size), format='csr')
self.rate_jacs[name] = sp.kron(self.D, sp_eye, format='csr')
rs, cs = self.rate_jacs[name].nonzero()

self.declare_partials(of=self._output_rate_names[name],
wrt=self._input_names[name],
rows=rs, cols=cs)

self.rate2_jacs[name] = sp.kron(sp.csr_matrix(self.D2), sp.eye(size), format='csr')
self.rate2_jacs[name] = sp.kron(self.D2, sp_eye, format='csr')
rs, cs = self.rate2_jacs[name].nonzero()

self.declare_partials(of=self._output_rate2_names[name],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,10 @@ def configure_io(self):

for key in self.jacs:
# Each jacobian matrix has a form that is defined by the Kronecker product
# of the interpolation matrix and np.eye(size). Make sure to specify csc format
# here to avoid spurious zeros.
# of the interpolation matrix eye(size).
self.jacs[key][name] = sp.kron(sp.csr_matrix(self.matrices[key]),
sp.eye(size),
format='csc')
sp.eye(size, format='csr'),
format='csr')

self.sizes[name] = size

Expand All @@ -157,15 +156,15 @@ def configure_io(self):
self.declare_partials(of=self.xc_str[name], wrt=self.xd_str[name],
rows=Ai_rows, cols=Ai_cols, val=data)

Bi_rows, Bi_cols, _ = sp.find(self.jacs['Bi'][name])
Bi_rows, Bi_cols = self.jacs['Bi'][name].nonzero()
self.declare_partials(of=self.xc_str[name], wrt=self.fd_str[name],
rows=Bi_rows, cols=Bi_cols)

Bd_rows, Bd_cols, data = sp.find(self.jacs['Bd'][name])
self.declare_partials(of=self.xdotc_str[name], wrt=self.fd_str[name],
rows=Bd_rows, cols=Bd_cols, val=data)

Ad_rows, Ad_cols, _ = sp.find(self.jacs['Ad'][name])
Ad_rows, Ad_cols = self.jacs['Ad'][name].nonzero()
self.declare_partials(of=self.xdotc_str[name], wrt=self.xd_str[name],
rows=Ad_rows, cols=Ad_cols)

Expand Down Expand Up @@ -242,7 +241,8 @@ def _compute_partials_radau(self, inputs, partials):

dstau_dt_x_size = np.repeat(dstau_dt, size)[:, np.newaxis]

partials[xdotc_name, xd_name] = self.jacs['Ad'][name].multiply(dstau_dt_x_size).data
dxdotc_dxd = self.jacs['Ad'][name].multiply(dstau_dt_x_size)
partials[xdotc_name, xd_name] = dxdotc_dxd.data

def _compute_partials_gauss_lobatto(self, inputs, partials):
ndn = self.options['grid_data'].subset_num_nodes['state_disc']
Expand Down

0 comments on commit 6ae47d1

Please sign in to comment.