Skip to content

Commit

Permalink
Merge pull request #3035 from swryan/deprecations
Browse files Browse the repository at this point in the history
Cleaned up some deprecations in the OpenMDAO code base
  • Loading branch information
swryan committed Sep 26, 2023
2 parents 5917d3a + 4e061ec commit eca4a09
Show file tree
Hide file tree
Showing 30 changed files with 126 additions and 127 deletions.
7 changes: 6 additions & 1 deletion openmdao/components/func_comp_common.py
Expand Up @@ -9,9 +9,14 @@

import numpy as np
try:
from jax import vmap, linear_util
from jax import vmap
import jax.numpy as jnp
from jax.config import config
# linear_util moved to jax.extend in jax 0.4.17, previous location is deprecated
try:
from jax.extend import linear_util
except ImportError:
from jax import linear_util
from jax.api_util import argnums_partial
from jax._src.api import _jvp, _vjp
config.update("jax_enable_x64", True) # jax by default uses 32 bit floats
Expand Down
7 changes: 4 additions & 3 deletions openmdao/components/interp_util/interp.py
Expand Up @@ -362,7 +362,7 @@ def _interpolate(self, xi):
# TODO: it might be possible to vectorize over n_nodes.
for j in range(n_nodes):
val, d_x, d_values, d_grid = table.evaluate(xi[j, ...])
result[j] = val
result[j] = val.item()
derivs_x[j, :] = d_x.ravel()
if d_values is not None:
if derivs_val is None:
Expand Down Expand Up @@ -442,7 +442,7 @@ def _evaluate_spline(self, values):
for k in range(nx):
x_pt = np.atleast_2d(xi[k])
val, _, d_values, _ = table.evaluate(x_pt)
result[j, k] = val
result[j, k] = val.item()
if d_values is not None:
if derivs_val is None:
dv_shape = [n_nodes, nx]
Expand Down Expand Up @@ -529,7 +529,8 @@ def training_gradients(self, pt):
values[j] = 1.0
table = interp([grid[i]], values, interp, **opts)
table._compute_d_dvalues = False
deriv_i[j], _, _, _ = table.evaluate(pt[i:i + 1])
deriv_i_j, _, _, _ = table.evaluate(pt[i:i + 1])
deriv_i[j] = deriv_i_j.item()
values[j] = 0.0

if i == 0:
Expand Down
22 changes: 11 additions & 11 deletions openmdao/components/interp_util/interp_akima.py
Expand Up @@ -1019,32 +1019,32 @@ def interpolate(self, x):
dm3_dv = np.zeros(n_this, dtype=dtype)
dm4_dv = np.zeros(n_this, dtype=dtype)
dm5_dv = np.zeros(n_this, dtype=dtype)
dm3_dv[idx_val4] = 1.0 / (grid[idx + 1] - grid[idx])
dm3_dv[idx_val3] = - dm3_dv[idx_val4]
dm3_dv[idx_val4] = (1.0 / (grid[idx + 1] - grid[idx])).item()
dm3_dv[idx_val3] = (- dm3_dv[idx_val4]).item()

if idx >= 2:
m1 = (val2 - val1) / (grid[idx - 1] - grid[idx - 2])
if compute_local_train:
dm1_dv[idx_val2] = 1.0 / (grid[idx - 1] - grid[idx - 2])
dm1_dv[idx_val1] = - dm1_dv[idx_val2]
dm1_dv[idx_val2] = (1.0 / (grid[idx - 1] - grid[idx - 2])).item()
dm1_dv[idx_val1] = (- dm1_dv[idx_val2]).item()

if idx >= 1:
m2 = (val3 - val2) / (grid[idx] - grid[idx - 1])
if compute_local_train:
dm2_dv[idx_val3] = 1.0 / (grid[idx] - grid[idx - 1])
dm2_dv[idx_val2] = - dm2_dv[idx_val3]
dm2_dv[idx_val3] = (1.0 / (grid[idx] - grid[idx - 1])).item()
dm2_dv[idx_val2] = (- dm2_dv[idx_val3]).item()

if idx < ngrid - 2:
m4 = (val5 - val4) / (grid[idx + 2] - grid[idx + 1])
if compute_local_train:
dm4_dv[idx_val5] = 1.0 / (grid[idx + 2] - grid[idx + 1])
dm4_dv[idx_val4] = - dm4_dv[idx_val5]
dm4_dv[idx_val5] = (1.0 / (grid[idx + 2] - grid[idx + 1])).item()
dm4_dv[idx_val4] = (- dm4_dv[idx_val5]).item()

if idx < ngrid - 3:
m5 = (val6 - val5) / (grid[idx + 3] - grid[idx + 2])
if compute_local_train:
dm5_dv[idx_val6] = 1.0 / (grid[idx + 3] - grid[idx + 2])
dm5_dv[idx_val5] = - dm5_dv[idx_val6]
dm5_dv[idx_val6] = (1.0 / (grid[idx + 3] - grid[idx + 2])).item()
dm5_dv[idx_val5] = (- dm5_dv[idx_val6]).item()

if idx == 0:
m2 = 2 * m3 - m4
Expand Down Expand Up @@ -1148,7 +1148,7 @@ def interpolate(self, x):
da_dv[idx_val3] = 1.0
dc_dv = dd_dv = 0

deriv_dx[0] = b + dx * (2.0 * c + 3.0 * d * dx)
deriv_dx[0] = (b + dx * (2.0 * c + 3.0 * d * dx)).item()
if compute_local_train:
deriv_dv = da_dv + dx * (db_dv + dx * (dc_dv + dx * dd_dv))

Expand Down
12 changes: 6 additions & 6 deletions openmdao/components/interp_util/interp_lagrange2.py
Expand Up @@ -233,13 +233,13 @@ def interpolate(self, x):
# All other cases, we are in an extrapolation sub-region.
extrap = True

xx1 = x[0] - grid[idx]
xx2 = x[0] - grid[idx + 1]
xx3 = x[0] - grid[idx + 2]
xx1 = (x[0] - grid[idx]).item()
xx2 = (x[0] - grid[idx + 1]).item()
xx3 = (x[0] - grid[idx + 2]).item()

c12 = grid[idx] - grid[idx + 1]
c13 = grid[idx] - grid[idx + 2]
c23 = grid[idx + 1] - grid[idx + 2]
c12 = (grid[idx] - grid[idx + 1]).item()
c13 = (grid[idx] - grid[idx + 2]).item()
c23 = (grid[idx + 1] - grid[idx + 2]).item()

if subtables is not None:

Expand Down
22 changes: 10 additions & 12 deletions openmdao/components/interp_util/interp_lagrange3.py
Expand Up @@ -341,23 +341,21 @@ def interpolate(self, x):
d_value = None
if self._compute_d_dvalues:
d_value = np.empty(4, dtype=dtype)
d_value[0] = xx2 * xx3 * xx4 * fact1
d_value[1] = -xx1 * xx3 * xx4 * fact2
d_value[2] = xx1 * xx2 * xx4 * fact3
d_value[3] = -xx1 * xx2 * xx3 * fact4
d_value[0] = (xx2 * xx3 * xx4 * fact1).item()
d_value[1] = (-xx1 * xx3 * xx4 * fact2).item()
d_value[2] = (xx1 * xx2 * xx4 * fact3).item()
d_value[3] = (-xx1 * xx2 * xx3 * fact4).item()

d_value = (d_value,
[self._idx[idx - 1], self._idx[idx],
self._idx[idx + 1], self._idx[idx + 2]])

derivs[0] = q1 * (x[0] * (3.0 * x[0] - 2.0 * (p4 + p3 + p2)) +
p4 * (p2 + p3) + p2 * p3) - \
q2 * (x[0] * (3.0 * x[0] - 2.0 * (p4 + p3 + p1)) +
p4 * (p1 + p3) + p1 * p3) + \
q3 * (x[0] * (3.0 * x[0] - 2.0 * (p4 + p2 + p1)) +
p4 * (p2 + p1) + p2 * p1) - \
q4 * (x[0] * (3.0 * x[0] - 2.0 * (p3 + p2 + p1)) +
p1 * (p2 + p3) + p2 * p3)
derivs[0] = (
q1 * (x[0] * (3.0 * x[0] - 2.0 * (p4 + p3 + p2)) + p4 * (p2 + p3) + p2 * p3) -
q2 * (x[0] * (3.0 * x[0] - 2.0 * (p4 + p3 + p1)) + p4 * (p1 + p3) + p1 * p3) +
q3 * (x[0] * (3.0 * x[0] - 2.0 * (p4 + p2 + p1)) + p4 * (p2 + p1) + p2 * p1) -
q4 * (x[0] * (3.0 * x[0] - 2.0 * (p3 + p2 + p1)) + p1 * (p2 + p3) + p2 * p3)
).item()

return xx4 * (xx3 * (q1 * xx2 - q2 * xx1) + q3 * xx1 * xx2) - q4 * xx1 * xx2 * xx3, \
derivs, d_value, extrap
Expand Down
2 changes: 1 addition & 1 deletion openmdao/components/interp_util/interp_scipy.py
Expand Up @@ -237,7 +237,7 @@ def _evaluate_splines(self, data_values, xi, ki, compute_gradients=True):
gradient[i] = self._evaluate_splines(local_derivs,
x[: i],
ki,
compute_gradients=False)
compute_gradients=False).item()

# All values have been folded down to a single dimensional array
# compute the final interpolated results, and gradient w.r.t. the
Expand Down
2 changes: 1 addition & 1 deletion openmdao/components/interp_util/interp_semi.py
Expand Up @@ -180,7 +180,7 @@ def _interpolate(self, xi):
# Loop over n_nodes because there isn't a way to vectorize.
for j in range(n_nodes):
val, d_x, d_values_tuple, extrapolate = table.interpolate(xi[j, :])
result[j] = val
result[j] = val.item()
derivs_x[j, :] = d_x.ravel()
if self._compute_d_dvalues:
d_values, idx = d_values_tuple
Expand Down
6 changes: 3 additions & 3 deletions openmdao/components/interp_util/interp_slinear.py
Expand Up @@ -183,7 +183,7 @@ def interpolate(self, x):
# (Not much we can do for linear.)
extrap = extrap or flag1 or flag0

slope = (val1 - val0) * h
slope = ((val1 - val0) * h).item()

derivs = np.empty(len(dx0) + 1, dtype=x.dtype)
derivs[0] = slope
Expand Down Expand Up @@ -212,8 +212,8 @@ def interpolate(self, x):
d_value = None
if self._compute_d_dvalues:
d_value = np.empty(2, dtype=x.dtype)
d_value[1] = h * (x - grid[idx])
d_value[0] = 1.0 - d_value[1]
d_value[1] = (h * (x - grid[idx])).item()
d_value[0] = (1.0 - d_value[1]).item()

d_value = (d_value, [self._idx[idx], self._idx[idx + 1]])

Expand Down
2 changes: 1 addition & 1 deletion openmdao/components/mux_comp.py
Expand Up @@ -111,7 +111,7 @@ def add_var(self, name, val=1.0, shape=None, units=None, desc='', axis=0):
in_templates[i].flat[j] = 1
temp_out = np.stack(in_templates, axis=ax)
cs.append(j)
rs.append(int(np.nonzero(temp_out.ravel())[0]))
rs.append(int(np.nonzero(temp_out.ravel())[0].item()))

self.declare_partials(of=name, wrt=in_name, rows=rs, cols=cs, val=1.0)

Expand Down
12 changes: 6 additions & 6 deletions openmdao/components/tests/test_external_code_comp.py
Expand Up @@ -281,8 +281,8 @@ def setup(self):
self.options['command'] = ('python extcode_paraboloid.py {} {}').format(self.input_file, self.output_file)

def compute(self, inputs, outputs):
x = inputs['x']
y = inputs['y']
x = inputs['x'].item()
y = inputs['y'].item()

# generate the input file for the paraboloid external code
with open(self.input_file, 'w') as input_file:
Expand Down Expand Up @@ -322,8 +322,8 @@ def setup_partials(self):
self.declare_partials(of='*', wrt='*', method='fd')

def compute(self, inputs, outputs):
x = inputs['x']
y = inputs['y']
x = inputs['x'].item()
y = inputs['y'].item()

# generate the input file for the paraboloid external code
with open(self.input_file, 'w') as input_file:
Expand Down Expand Up @@ -365,8 +365,8 @@ def setup_partials(self):
self.declare_partials(of='*', wrt='*')

def compute(self, inputs, outputs):
x = inputs['x']
y = inputs['y']
x = inputs['x'].item()
y = inputs['y'].item()

# generate the input file for the paraboloid external code
with open(self.input_file, 'w') as input_file:
Expand Down
Expand Up @@ -1347,7 +1347,7 @@ def test_shape(self):
f = np.sqrt(P1) + P2 * P3

# verify the shape matches the order and size of the input params
print(f.shape)
self.assertEqual(f.shape, (25, 5, 10))

# Create regular grid interpolator instance
interp = om.MetaModelStructuredComp(method='scipy_cubic')
Expand Down
10 changes: 8 additions & 2 deletions openmdao/components/tests/test_meta_model_unstructured_comp.py
Expand Up @@ -768,7 +768,10 @@ def train(self, x, y):
pass

def predict(self, x):
return sin(x)
if x.size == 1:
return sin(x.item())
else:
return sin(x)

class SinTwoInputsSurrogate(om.SurrogateModel):
def train(self, x, y):
Expand Down Expand Up @@ -936,7 +939,10 @@ def train(self, x, y):
pass

def predict(self, x):
return sin(x)
if x.size == 1:
return sin(x.item())
else:
return sin(x)

class TrigWithFdInSetup(om.MetaModelUnStructuredComp):
def setup(self):
Expand Down
2 changes: 1 addition & 1 deletion openmdao/core/driver.py
Expand Up @@ -753,7 +753,7 @@ def set_design_var(self, name, value, set_remote=True):
elif isinstance(value, np.ndarray):
if isinstance(problem.model._discrete_outputs[src_name], int):
# Setting an integer value with a 1D array - don't want to convert to array.
value = int(value)
value = int(value.item())
else:
value = value.astype(int)

Expand Down
2 changes: 2 additions & 0 deletions openmdao/core/group.py
Expand Up @@ -2870,6 +2870,8 @@ def meta2node_data(meta):
else:
meta['shape'] = shape
meta['size'] = size
# Passing None into shape arguments as an alias for () is deprecated (Numpy 1.20)
shape = shape if shape is not None else ()
meta['val'] = np.full(shape, meta['val'], dtype=float)

# save graph info for possible later plotting
Expand Down
4 changes: 2 additions & 2 deletions openmdao/core/tests/test_coloring.py
Expand Up @@ -214,8 +214,8 @@ def compute(self, inputs, outputs):
con[:10] = inputs['r_con_g']
con[10:15] = inputs['theta_con_g']
con[15:20] = inputs['delta_theta_con_g']
con[20] = inputs['l_conx_g']
con[21] = inputs['y']
con[20] = inputs['l_conx_g'].item()
con[21] = inputs['y'].item()

p.model.add_subsystem('mux', MuxComp())
p.model.connect('r_con.g', 'mux.r_con_g')
Expand Down
4 changes: 2 additions & 2 deletions openmdao/core/tests/test_compute_jacvec_prod.py
Expand Up @@ -78,9 +78,9 @@ def _compute_partials_fwd(self, inputs, partials):
seed['comp.inp'][:] = jvp['comp.out']

if rhsname == 'comp.x':
partials[self.pathname + '.out', self.pathname +'.x'][0, rhs_i] = jvp[self.pathname + '.out']
partials[self.pathname + '.out', self.pathname +'.x'][0, rhs_i] = jvp[self.pathname + '.out'].item()
else:
partials[self.pathname + '.out', self.pathname + '.inp'][0, 0] = jvp[self.pathname + '.out']
partials[self.pathname + '.out', self.pathname + '.inp'][0, 0] = jvp[self.pathname + '.out'].item()

def _compute_partials_rev(self, inputs, partials):
p = self.prob
Expand Down
4 changes: 2 additions & 2 deletions openmdao/core/tests/test_des_vars_responses.py
Expand Up @@ -765,8 +765,8 @@ def setup_partials(self):

def compute(self, inputs, outputs):
# Retrieve Inputs
x = inputs["x"]
y = inputs["y"]
x = inputs["x"].item()
y = inputs["y"].item()

# Compute Values
f_xy = (x - 3.0) ** 2 + x * y + (y + 4.0) ** 2 - 3.0
Expand Down
12 changes: 6 additions & 6 deletions openmdao/core/tests/test_feature_cache_linear_solution.py
Expand Up @@ -32,19 +32,19 @@ def setup_partials(self):
self.declare_partials(of='*', wrt='*')

def apply_nonlinear(self, inputs, outputs, residuals):
a = inputs['a']
b = inputs['b']
c = inputs['c']
a = inputs['a'].item()
b = inputs['b'].item()
c = inputs['c'].item()
x = outputs['states'][0]
y = outputs['states'][1]

residuals['states'][0] = a * x ** 2 + b * x + c
residuals['states'][1] = a * y + b

def solve_nonlinear(self, inputs, outputs):
a = inputs['a']
b = inputs['b']
c = inputs['c']
a = inputs['a'].item()
b = inputs['b'].item()
c = inputs['c'].item()
outputs['states'][0] = (-b + (b ** 2 - 4 * a * c) ** 0.5) / (2 * a)
outputs['states'][1] = -b/a

Expand Down
8 changes: 5 additions & 3 deletions openmdao/core/tests/test_renamed_resids.py
Expand Up @@ -22,15 +22,17 @@ def setup_partials(self):
self.declare_partials('res', ['*'], method='fd')

def apply_nonlinear(self, inputs, outputs, residuals):
mm = inputs['mm'][0]
mm = inputs['mm'].item()
Re = outputs['Re'].item()
temp = outputs['temp'][0][0].item()

T = 389.97
cf = 0.01
temp = outputs['temp'][0][0]
RE = 1.479301E9 * .0260239151 * (T / 1.8 + 110.4) / (T / 1.8) ** 2
comb = 4.593153E-6 * 0.8 * (T + 198.72) / (RE * mm * T ** 1.5)
temp_ratio = 1.0 + 0.035 * mm * mm + 0.45 * (temp / T - 1.0)
CFL = cf / (1.0 + 3.59 * np.sqrt(cf) * temp_ratio)
residuals['res'][0] = outputs['Re'] - RE * mm
residuals['res'][0] = Re - RE * mm
residuals['res'][1] = (1.0 / (1.0 + comb * temp ** 3 / CFL) + temp) * 0.5 - temp


Expand Down
2 changes: 1 addition & 1 deletion openmdao/core/total_jac.py
Expand Up @@ -1724,7 +1724,7 @@ def _get_zero_inds(self, name, tup, jac_arr):
vslice = jac_arr[tup[0]]

if inds is None:
zero_idxs = np.nonzero(vslice.reshape(shape))
zero_idxs = np.atleast_1d(vslice.reshape(shape)).nonzero()
else:
zero_idxs = np.nonzero(vslice)
if zero_idxs[0].size == 0:
Expand Down

0 comments on commit eca4a09

Please sign in to comment.