Skip to content

Commit

Permalink
Merge branch 'master' into work
Browse files Browse the repository at this point in the history
  • Loading branch information
swryan committed Jul 24, 2020
2 parents c4e75c5 + 336d08f commit 821d3a0
Show file tree
Hide file tree
Showing 52 changed files with 2,097 additions and 1,772 deletions.
6 changes: 3 additions & 3 deletions openmdao/approximation_schemes/approximation_scheme.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,10 +259,10 @@ def _init_approximations(self, system):
wrt = key[0]
directional = key[-1]
data = self._get_approx_data(system, key)
if wrt in inputs._views_flat:
if inputs._contains_abs(wrt):
arr = inputs
slices = in_slices
elif wrt in outputs._views_flat:
elif outputs._contains_abs(wrt):
arr = outputs
slices = out_slices
else: # wrt is remote
Expand Down Expand Up @@ -295,7 +295,7 @@ def _compute_approximations(self, system, jac, total, under_cs):
system._set_approx_mode(True)

# Clean vector for results
results_array = system._outputs._data.copy() if total else system._residuals._data.copy()
results_array = system._outputs.asarray(True) if total else system._residuals.asarray(True)

# To support driver src_indices, we need to override some checks in Jacobian, but do it
# selectively.
Expand Down
26 changes: 13 additions & 13 deletions openmdao/approximation_schemes/finite_difference.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,10 @@ def _get_approx_data(self, system, data):
fd_form = _generate_fd_coeff(form, order, system)

if step_calc == 'rel':
if wrt in system._outputs._views_flat:
step *= np.linalg.norm(system._outputs._views_flat[wrt])
elif wrt in system._inputs._views_flat:
step *= np.linalg.norm(system._inputs._views_flat[wrt])
if system._outputs._contains_abs(wrt):
step *= np.linalg.norm(system._outputs._abs_get_val(wrt))
elif system._inputs._contains_abs(wrt):
step *= np.linalg.norm(system._inputs._abs_get_val(wrt))

deltas = fd_form.deltas * step
coeffs = fd_form.coeffs / step
Expand Down Expand Up @@ -187,9 +187,9 @@ def compute_approximations(self, system, jac=None, total=False):
if jac is None:
jac = system._jacobian

self._starting_outs = system._outputs._data.copy()
self._starting_resids = system._residuals._data.copy()
self._starting_ins = system._inputs._data.copy()
self._starting_outs = system._outputs.asarray(True)
self._starting_resids = system._residuals.asarray(True)
self._starting_ins = system._inputs.asarray(True)
if total:
self._results_tmp = self._starting_outs.copy()
else:
Expand Down Expand Up @@ -296,20 +296,20 @@ def _run_sub_point(self, system, idx_info, delta, total):
"""
for vec, idxs in idx_info:
if vec is not None:
vec._data[idxs] += delta
vec.iadd(delta, idxs)

if total:
system.run_solve_nonlinear()
self._results_tmp[:] = system._outputs._data
self._results_tmp[:] = system._outputs.asarray()
else:
system.run_apply_nonlinear()
self._results_tmp[:] = system._residuals._data
self._results_tmp[:] = system._residuals.asarray()

system._residuals._data[:] = self._starting_resids
system._residuals.set_val(self._starting_resids)

# save results and restore starting inputs/outputs
system._inputs._data[:] = self._starting_ins
system._outputs._data[:] = self._starting_outs
system._inputs.set_val(self._starting_ins)
system._outputs.set_val(self._starting_outs)

return self._results_tmp

Expand Down
4 changes: 2 additions & 2 deletions openmdao/components/exec_comp.py
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ def compute_partials(self, inputs, partials):
uwrap = _TmpDict(self._outputs, return_complex=True)

# solve with complex param value
self._residuals.set_const(0.0)
self._residuals.set_val(0.0)
self.compute(pwrap, uwrap)

for u in out_names:
Expand All @@ -468,7 +468,7 @@ def compute_partials(self, inputs, partials):
uwrap = _TmpDict(self._outputs, return_complex=True)

# solve with complex param value
self._residuals.set_const(0.0)
self._residuals.set_val(0.0)
self.compute(pwrap, uwrap)

for u in out_names:
Expand Down
1 change: 0 additions & 1 deletion openmdao/core/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,6 @@ def add_input(self, name, val=1.0, shape=None, src_indices=None, flat_src_indice
else:
metadata['src_indices'] = np.asarray(src_indices, dtype=INT_DTYPE)

# We may not know the pathname yet, so we have to use name for now, instead of abs_name.
if self._static_mode:
var_rel2meta = self._static_var_rel2meta
var_rel_names = self._static_var_rel_names
Expand Down
22 changes: 11 additions & 11 deletions openmdao/core/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ def _setup_driver(self, problem):

# Now determine if later we'll need to allgather cons, objs, or desvars.
if model.comm.size > 1 and model._subsystems_allprocs:
local_out_vars = set(model._outputs._views)
local_out_vars = set(model._outputs._abs_iter())
remote_dvs = set(src_design_vars) - local_out_vars
remote_cons = set(src_cons) - local_out_vars
remote_objs = set(src_objs) - local_out_vars
Expand Down Expand Up @@ -413,17 +413,17 @@ def _get_vars_to_record(self, recording_options):
if recording_options['record_outputs']:
myoutputs = sorted([n for n, prom in abs2prom.items() if check_path(prom, incl, excl)])

views = model._outputs._views
model_outs = model._outputs

if model._var_discrete['output']:
# if we have discrete outputs then residual name set doesn't match output one
if recording_options['record_residuals']:
myresiduals = [n for n in myoutputs if n in views]
myresiduals = [n for n in myoutputs if model_outs._contains_abs(n)]
elif recording_options['record_residuals']:
myresiduals = myoutputs

elif recording_options['record_residuals']:
myresiduals = [n for n in model._residuals._views
myresiduals = [n for n in model._residuals._abs_iter()
if check_path(abs2prom[n], incl, excl)]

myoutputs = set(myoutputs)
Expand Down Expand Up @@ -486,7 +486,7 @@ def _get_voi_val(self, name, meta, remote_vois, driver_scaling=True, rank=None):
"""
model = self._problem().model
comm = model.comm
vec = model._outputs._views_flat
get = model._outputs._abs_get_val
distributed_vars = self._dist_driver_vars
indices = meta['indices']

Expand All @@ -511,9 +511,9 @@ def _get_voi_val(self, name, meta, remote_vois, driver_scaling=True, rank=None):
else:
if owner == comm.rank:
if indices is None:
val = vec[src_name].copy()
val = get(name).copy()
else:
val = vec[src_name][indices]
val = get(name)[indices]
else:
if indices is not None:
size = len(indices)
Expand Down Expand Up @@ -547,9 +547,9 @@ def _get_voi_val(self, name, meta, remote_vois, driver_scaling=True, rank=None):
raise ValueError(msg)

elif indices is None:
val = vec[src_name].copy()
val = get(src_name).copy()
else:
val = vec[src_name][indices]
val = get(src_name)[indices]

if self._has_scaling and driver_scaling:
# Scale design variable values
Expand Down Expand Up @@ -616,8 +616,8 @@ def set_design_var(self, name, value):

problem.model._discrete_outputs[src_name] = value

elif src_name in problem.model._outputs._views_flat:
desvar = problem.model._outputs._views_flat[src_name]
elif problem.model._outputs._contains_abs(src_name):
desvar = problem.model._outputs._abs_get_val(src_name)
if src_name in self._dist_driver_vars:
loc_idxs, _, dist_idxs = self._dist_driver_vars[src_name]
else:
Expand Down
6 changes: 3 additions & 3 deletions openmdao/core/explicitcomponent.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,8 @@ def _approx_subjac_keys_iter(self):
for abs_key, meta in self._subjacs_info.items():
if 'method' in meta:
method = meta['method']
if (method is not None and method in self._approx_schemes and abs_key[1]
not in self._outputs._views_flat):
if (method is not None and method in self._approx_schemes and
not self._outputs._contains_abs(abs_key[1])):
yield abs_key

def _apply_nonlinear(self):
Expand Down Expand Up @@ -250,7 +250,7 @@ def _solve_nonlinear(self):
"""
with Recording(self.pathname + '._solve_nonlinear', self.iter_count, self):
with self._unscaled_context(outputs=[self._outputs], residuals=[self._residuals]):
self._residuals.set_const(0.0)
self._residuals.set_val(0.0)
self._inputs.read_only = True
try:
if self._discrete_inputs or self._discrete_outputs:
Expand Down
6 changes: 3 additions & 3 deletions openmdao/core/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -1935,7 +1935,7 @@ def _guess_nonlinear(self):
self._residuals._under_complex_step = True

self._outputs.set_complex_step_mode(True)
self._outputs._data[:] += imag_cache * 1j
self._outputs.iadd(imag_cache * 1j)

def guess_nonlinear(self, inputs, outputs, residuals,
discrete_inputs=None, discrete_outputs=None):
Expand Down Expand Up @@ -2003,7 +2003,7 @@ def _apply_linear(self, jac, vec_names, rel_systems, mode, scope_out=None, scope
if rel_systems is not None:
for s in irrelevant_subs:
# zero out dvecs of irrelevant subsystems
s._vectors['residual']['linear'].set_const(0.0)
s._vectors['residual']['linear'].set_val(0.0)

for subsys in self._subsystems_myproc:
if rel_systems is None or subsys.pathname in rel_systems:
Expand All @@ -2016,7 +2016,7 @@ def _apply_linear(self, jac, vec_names, rel_systems, mode, scope_out=None, scope
if rel_systems is not None:
for s in irrelevant_subs:
# zero out dvecs of irrelevant subsystems
s._vectors['output']['linear'].set_const(0.0)
s._vectors['output']['linear'].set_val(0.0)

def _solve_linear(self, vec_names, mode, rel_systems):
"""
Expand Down
36 changes: 18 additions & 18 deletions openmdao/core/problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -507,11 +507,11 @@ def set_val(self, name, value, units=None, indices=None):
if indices is None:
indices = _full_slice

if abs_name in model._outputs._views:
if model._outputs._contains_abs(abs_name):
model._outputs.set_var(abs_name, value, indices)
elif abs_name in conns: # input name given. Set value into output
if src in model._outputs._views: # src is local
if (model._outputs._views_flat[src].size == 0 and
if model._outputs._contains_abs(src): # src is local
if (model._outputs._abs_get_val(src).size == 0 and
src.rsplit('.', 1)[0] == '_auto_ivc' and all_meta[src]['distributed']):
pass # special case, auto_ivc dist var with 0 local size
elif tmeta['has_src_indices'] and n_proms < 2:
Expand Down Expand Up @@ -542,7 +542,7 @@ def set_val(self, name, value, units=None, indices=None):
# also set the input
# TODO: maybe remove this if inputs are removed from case recording
if n_proms < 2:
if abs_name in model._inputs._views:
if model._inputs._contains_abs(abs_name):
model._inputs.set_var(abs_name, ivalue, indices)
elif abs_name in model._discrete_inputs:
model._discrete_inputs[abs_name] = value
Expand All @@ -554,7 +554,7 @@ def set_val(self, name, value, units=None, indices=None):
"Local assignment ignored.")
elif abs_name in model._discrete_outputs:
model._discrete_outputs[abs_name] = value
elif abs_name in model._inputs._views: # could happen if model is a component
elif model._inputs._contains_abs(abs_name): # could happen if model is a component
model._inputs.set_var(abs_name, value, indices)
elif abs_name in model._discrete_inputs: # could happen if model is a component
model._discrete_inputs[abs_name] = value
Expand Down Expand Up @@ -674,7 +674,7 @@ def compute_jacvec_product(self, of, wrt, mode, seed):
rvec = self.model._vectors[rkind]['linear']
lvec = self.model._vectors[lkind]['linear']

rvec._data[:] = 0.
rvec.set_val(0.)

conns = self.model._conn_global_abs_in2out

Expand Down Expand Up @@ -1048,8 +1048,8 @@ def check_partials(self, out_stream=_DEFAULT_OUT_STREAM, includes=None, excludes
partials_data = defaultdict(lambda: defaultdict(dict))

# Caching current point to restore after setups.
input_cache = model._inputs._data.copy()
output_cache = model._outputs._data.copy()
input_cache = model._inputs.asarray(copy=True)
output_cache = model._outputs.asarray(copy=True)

# Keep track of derivative keys that are declared dependent so that we don't print them
# unless they are in error.
Expand All @@ -1061,8 +1061,8 @@ def check_partials(self, out_stream=_DEFAULT_OUT_STREAM, includes=None, excludes
# Analytic Jacobians
print_reverse = False
for mode in ('fwd', 'rev'):
model._inputs.set_const(input_cache)
model._outputs.set_const(output_cache)
model._inputs.set_val(input_cache)
model._outputs.set_val(output_cache)
# Make sure we're in a valid state
model.run_apply_nonlinear()

Expand Down Expand Up @@ -1112,10 +1112,10 @@ def check_partials(self, out_stream=_DEFAULT_OUT_STREAM, includes=None, excludes
directional = c_name in mfree_directions

try:
flat_view = dinputs._views_flat[inp_abs]
flat_view = dinputs._abs_get_val(inp_abs)
except KeyError:
# Implicit state
flat_view = dstate._views_flat[inp_abs]
flat_view = dstate._abs_get_val(inp_abs)

if directional:
n_in = 1
Expand All @@ -1129,8 +1129,8 @@ def check_partials(self, out_stream=_DEFAULT_OUT_STREAM, includes=None, excludes

for idx in range(n_in):

dinputs.set_const(0.0)
dstate.set_const(0.0)
dinputs.set_val(0.0)
dstate.set_val(0.0)

# Dictionary access returns a scalar for 1d input, and we
# need a vector for clean code, so use _views_flat.
Expand All @@ -1146,10 +1146,10 @@ def check_partials(self, out_stream=_DEFAULT_OUT_STREAM, includes=None, excludes
out_abs = rel_name2abs_name(comp, out)

try:
derivs = doutputs._views_flat[out_abs]
derivs = doutputs._abs_get_val(out_abs)
except KeyError:
# Implicit state
derivs = dstate._views_flat[out_abs]
derivs = dstate._abs_get_val(out_abs)

if mode == 'fwd':
key = out, inp
Expand Down Expand Up @@ -1239,8 +1239,8 @@ def check_partials(self, out_stream=_DEFAULT_OUT_STREAM, includes=None, excludes

partials_data[c_name][rel_key][jac_key] = deriv_value.copy()

model._inputs.set_const(input_cache)
model._outputs.set_const(output_cache)
model._inputs.set_val(input_cache)
model._outputs.set_val(output_cache)
model.run_apply_nonlinear()

# Finite Difference to calculate Jacobian
Expand Down

0 comments on commit 821d3a0

Please sign in to comment.