Skip to content

Commit

Permalink
Merge 4e7f251 into 54004c1
Browse files Browse the repository at this point in the history
  • Loading branch information
Kenneth-T-Moore committed Jun 20, 2019
2 parents 54004c1 + 4e7f251 commit 74f2425
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 52 deletions.
8 changes: 4 additions & 4 deletions openmdao/components/eq_constraint_comp.py
Expand Up @@ -90,10 +90,10 @@ def __init__(self, name=None, eq_units=None, lhs_name=None, rhs_name=None, rhs_v
Value of response variable that scales to 0.0 in the driver. This option is only
meaningful when add_constraint=True.
adder : float or ndarray, optional
Value to add to the model value to get the scaled value. Adder
Value to add to the model value to get the scaled value for the driver. adder
is first in precedence. This option is only meaningful when add_constraint=True.
scaler : float or ndarray, optional
value to multiply the model value to get the scaled value. Scaler
value to multiply the model value to get the scaled value for the driver. scaler
is second in precedence. This option is only meaningful when add_constraint=True.
**kwargs : dict
Additional arguments to be passed for the creation of the output variable.
Expand Down Expand Up @@ -290,10 +290,10 @@ def add_eq_output(self, name, eq_units=None, lhs_name=None, rhs_name=None, rhs_v
Value of response variable that scales to 0.0 in the driver. This option is only
meaningful when add_constraint=True.
adder : float or ndarray, optional
Value to add to the model value to get the scaled value. Adder
Value to add to the model value to get the scaled value for the driver. adder
is first in precedence. This option is only meaningful when add_constraint=True.
scaler : float or ndarray, optional
Value to multiply the model value to get the scaled value. Scaler
Value to multiply the model value to get the scaled value for the driver. scaler
is second in precedence. This option is only meaningful when add_constraint=True.
**kwargs : dict
Additional arguments to be passed for the creation of the output variable.
Expand Down
59 changes: 35 additions & 24 deletions openmdao/core/driver.py
Expand Up @@ -443,7 +443,7 @@ def _setup_recording(self):
for sub in self._problem.model.system_iter(recurse=True, include_self=True):
self._rec_mgr.record_metadata(sub)

def _get_voi_val(self, name, meta, remote_vois, unscaled=False, ignore_indices=False):
def _get_voi_val(self, name, meta, remote_vois, driver_scaling=True, ignore_indices=False):
"""
Get the value of a variable of interest (objective, constraint, or design var).
Expand All @@ -458,8 +458,10 @@ def _get_voi_val(self, name, meta, remote_vois, unscaled=False, ignore_indices=F
remote_vois : dict
Dict containing (owning_rank, size) for all remote vois of a particular
type (design var, constraint, or objective).
unscaled : bool
Set to True if unscaled (physical) design variables are desired.
driver_scaling : bool
When True, return values that are scaled according to either the adder and scaler or
the ref and ref0 values that were specified when add_design_var, add_objective, and
add_constraint were called on the model. Default is True.
ignore_indices : bool
Set to True if the full array is desired, not just those indicated by indices.
Expand Down Expand Up @@ -510,7 +512,7 @@ def _get_voi_val(self, name, meta, remote_vois, unscaled=False, ignore_indices=F
else:
val = vec[name][indices]

if self._has_scaling and not unscaled:
if self._has_scaling and driver_scaling:
# Scale design variable values
adder = meta['adder']
if adder is not None:
Expand All @@ -522,7 +524,7 @@ def _get_voi_val(self, name, meta, remote_vois, unscaled=False, ignore_indices=F

return val

def get_design_var_values(self, filter=None, unscaled=False, ignore_indices=False):
def get_design_var_values(self, filter=None, driver_scaling=True, ignore_indices=False):
"""
Return the design variable values.
Expand All @@ -532,8 +534,10 @@ def get_design_var_values(self, filter=None, unscaled=False, ignore_indices=Fals
----------
filter : list
List of desvar names used by recorders.
unscaled : bool
Set to True if unscaled (physical) design variables are desired.
driver_scaling : bool
When True, return values that are scaled according to either the adder and scaler or
the ref and ref0 values that were specified when add_design_var, add_objective, and
add_constraint were called on the model. Default is True.
ignore_indices : bool
Set to True if the full array is desired, not just those indicated by indices.
Expand All @@ -548,7 +552,8 @@ def get_design_var_values(self, filter=None, unscaled=False, ignore_indices=Fals
# use all the designvars
dvs = self._designvars

return {n: self._get_voi_val(n, self._designvars[n], self._remote_dvs, unscaled=unscaled,
return {n: self._get_voi_val(n, self._designvars[n], self._remote_dvs,
driver_scaling=driver_scaling,
ignore_indices=ignore_indices) for n in dvs}

def set_design_var(self, name, value):
Expand Down Expand Up @@ -580,8 +585,8 @@ def set_design_var(self, name, value):
desvar = problem.model._outputs._views_flat[name]
desvar[indices] = value

# Undo driver scaling when setting design var values into model.
if self._has_scaling:
# Scale design variable values
scaler = meta['scaler']
if scaler is not None:
desvar[indices] *= 1.0 / scaler
Expand Down Expand Up @@ -611,14 +616,16 @@ def get_response_values(self, filter=None):

return {n: self._get_voi_val(n, self._responses[n], self._remote_objs) for n in resps}

def get_objective_values(self, unscaled=False, filter=None, ignore_indices=False):
def get_objective_values(self, driver_scaling=True, filter=None, ignore_indices=False):
"""
Return objective values.
Parameters
----------
unscaled : bool
Set to True if unscaled (physical) design variables are desired.
driver_scaling : bool
When True, return values that are scaled according to either the adder and scaler or
the ref and ref0 values that were specified when add_design_var, add_objective, and
add_constraint were called on the model. Default is True.
filter : list
List of objective names used by recorders.
ignore_indices : bool
Expand All @@ -634,11 +641,12 @@ def get_objective_values(self, unscaled=False, filter=None, ignore_indices=False
else:
objs = self._objs

return {n: self._get_voi_val(n, self._objs[n], self._remote_objs, unscaled=unscaled,
return {n: self._get_voi_val(n, self._objs[n], self._remote_objs,
driver_scaling=driver_scaling,
ignore_indices=ignore_indices)
for n in objs}

def get_constraint_values(self, ctype='all', lintype='all', unscaled=False, filter=None,
def get_constraint_values(self, ctype='all', lintype='all', driver_scaling=True, filter=None,
ignore_indices=False):
"""
Return constraint values.
Expand All @@ -651,8 +659,10 @@ def get_constraint_values(self, ctype='all', lintype='all', unscaled=False, filt
lintype : string
Default is 'all'. Optionally return just the linear constraints
with 'linear' or the nonlinear constraints with 'nonlinear'.
unscaled : bool
Set to True if unscaled (physical) design variables are desired.
driver_scaling : bool
When True, return values that are scaled according to either the adder and scaler or
the ref and ref0 values that were specified when add_design_var, add_objective, and
add_constraint were called on the model. Default is True.
filter : list
List of constraint names used by recorders.
ignore_indices : bool
Expand Down Expand Up @@ -684,7 +694,8 @@ def get_constraint_values(self, ctype='all', lintype='all', unscaled=False, filt
if ctype == 'ineq' and meta['equals'] is not None:
continue

con_dict[name] = self._get_voi_val(name, meta, self._remote_cons, unscaled=unscaled,
con_dict[name] = self._get_voi_val(name, meta, self._remote_cons,
driver_scaling=driver_scaling,
ignore_indices=ignore_indices)

return con_dict
Expand Down Expand Up @@ -846,17 +857,17 @@ def record_iteration(self):
filt = self._filtered_vars_to_record

if opts['record_desvars']:
des_vars = self.get_design_var_values(unscaled=True, ignore_indices=True)
des_vars = self.get_design_var_values(driver_scaling=False, ignore_indices=True)
else:
des_vars = {}

if opts['record_objectives']:
obj_vars = self.get_objective_values(unscaled=True, ignore_indices=True)
obj_vars = self.get_objective_values(driver_scaling=False, ignore_indices=True)
else:
obj_vars = {}

if opts['record_constraints']:
con_vars = self.get_constraint_values(unscaled=True, ignore_indices=True)
con_vars = self.get_constraint_values(driver_scaling=False, ignore_indices=True)
else:
con_vars = {}

Expand Down Expand Up @@ -1130,7 +1141,7 @@ def _pre_run_model_debug_print(self):
print(len(header) * '-')

if 'desvars' in debug_opt:
desvar_vals = self.get_design_var_values(unscaled=True, ignore_indices=True)
desvar_vals = self.get_design_var_values(driver_scaling=False, ignore_indices=True)
if not MPI or MPI.COMM_WORLD.rank == 0:
print("Design Vars")
if desvar_vals:
Expand All @@ -1152,7 +1163,7 @@ def _post_run_model_debug_print(self):
Optionally print some debugging information after the model runs.
"""
if 'nl_cons' in self.options['debug_print']:
cons = self.get_constraint_values(lintype='nonlinear', unscaled=True)
cons = self.get_constraint_values(lintype='nonlinear', driver_scaling=False)
if not MPI or MPI.COMM_WORLD.rank == 0:
print("Nonlinear constraints")
if cons:
Expand All @@ -1162,7 +1173,7 @@ def _post_run_model_debug_print(self):
print()

if 'ln_cons' in self.options['debug_print']:
cons = self.get_constraint_values(lintype='linear', unscaled=True)
cons = self.get_constraint_values(lintype='linear', driver_scaling=False)
if not MPI or MPI.COMM_WORLD.rank == 0:
print("Linear constraints")
if cons:
Expand All @@ -1172,7 +1183,7 @@ def _post_run_model_debug_print(self):
print()

if 'objs' in self.options['debug_print']:
objs = self.get_objective_values(unscaled=True)
objs = self.get_objective_values(driver_scaling=False)
if not MPI or MPI.COMM_WORLD.rank == 0:
print("Objectives")
if objs:
Expand Down
12 changes: 7 additions & 5 deletions openmdao/core/problem.py
Expand Up @@ -1129,7 +1129,7 @@ def check_partials(self, out_stream=_DEFAULT_OUT_STREAM, includes=None, excludes
dinputs.set_const(0.0)
dstate.set_const(0.0)

# Dictionary access returns a scaler for 1d input, and we
# Dictionary access returns a scalar for 1d input, and we
# need a vector for clean code, so use _views_flat.
flat_view[idx] = 1.0

Expand Down Expand Up @@ -1346,8 +1346,9 @@ def check_totals(self, of=None, wrt=None, out_stream=_DEFAULT_OUT_STREAM, compac
compact_print : bool
Set to True to just print the essentials, one line per unknown-param pair.
driver_scaling : bool
Set to True to scale derivative values by the quantities specified when the desvars and
responses were added. Default if False, which is unscaled.
When True, return derivatives that are scaled according to either the adder and scaler
or the ref and ref0 values that were specified when add_design_var, add_objective, and
add_constraint were called on the model. Default is False, which is unscaled.
abs_err_tol : float
Threshold value for absolute error. Errors about this value will have a '*' displayed
next to them in output, making them easy to search for. Default is 1.0E-6.
Expand Down Expand Up @@ -1458,8 +1459,9 @@ def compute_totals(self, of=None, wrt=None, return_format='flat_dict', debug_pri
debug_print : bool
Set to True to print out some debug information during linear solve.
driver_scaling : bool
Set to True to scale derivative values by the quantities specified when the desvars and
responses were added. Default if False, which is unscaled.
When True, return derivatives that are scaled according to either the adder and scaler
or the ref and ref0 values that were specified when add_design_var, add_objective, and
add_constraint were called on the model. Default is False, which is unscaled.
Returns
-------
Expand Down
40 changes: 24 additions & 16 deletions openmdao/core/system.py
Expand Up @@ -2347,11 +2347,13 @@ def add_design_var(self, name, lower=None, upper=None, ref=None,
interest for this particular design variable. These may be
positive or negative integers.
adder : float or ndarray, optional
Value to add to the model value to get the scaled value. Adder
is first in precedence.
Value to add to the model value to get the scaled value for the driver. adder
is first in precedence. adder and scaler are an alterantive to using ref
and ref0.
scaler : float or ndarray, optional
value to multiply the model value to get the scaled value. Scaler
is second in precedence.
value to multiply the model value to get the scaled value for the driver. scaler
is second in precedence. adder and scaler are an alterantive to using ref
and ref0.
parallel_deriv_color : string
If specified, this design var will be grouped for parallel derivative
calculations with other variables sharing the same parallel_deriv_color.
Expand Down Expand Up @@ -2482,11 +2484,13 @@ def add_response(self, name, type_, lower=None, upper=None, equals=None,
If variable is an array, this indicates which entry is of
interest for this particular response.
adder : float or ndarray, optional
Value to add to the model value to get the scaled value. Adder
is first in precedence.
Value to add to the model value to get the scaled value for the driver. adder
is first in precedence. adder and scaler are an alterantive to using ref
and ref0.
scaler : float or ndarray, optional
value to multiply the model value to get the scaled value. Scaler
is second in precedence.
value to multiply the model value to get the scaled value for the driver. scaler
is second in precedence. adder and scaler are an alterantive to using ref
and ref0.
linear : bool
Set to True if constraint is linear. Default is False.
parallel_deriv_color : string
Expand Down Expand Up @@ -2643,11 +2647,13 @@ def add_constraint(self, name, lower=None, upper=None, equals=None,
ref0 : float or ndarray, optional
Value of response variable that scales to 0.0 in the driver.
adder : float or ndarray, optional
Value to add to the model value to get the scaled value. Adder
is first in precedence.
Value to add to the model value to get the scaled value for the driver. adder
is first in precedence. adder and scaler are an alterantive to using ref
and ref0.
scaler : float or ndarray, optional
value to multiply the model value to get the scaled value. Scaler
is second in precedence.
value to multiply the model value to get the scaled value for the driver. scaler
is second in precedence. adder and scaler are an alterantive to using ref
and ref0.
indices : sequence of int, optional
If variable is an array, these indicate which entries are of
interest for this particular response. These may be positive or
Expand Down Expand Up @@ -2696,11 +2702,13 @@ def add_objective(self, name, ref=None, ref0=None, index=None,
interest for this particular response. This may be a positive
or negative integer.
adder : float or ndarray, optional
Value to add to the model value to get the scaled value. Adder
is first in precedence.
Value to add to the model value to get the scaled value for the driver. adder
is first in precedence. adder and scaler are an alterantive to using ref
and ref0.
scaler : float or ndarray, optional
value to multiply the model value to get the scaled value. Scaler
is second in precedence.
value to multiply the model value to get the scaled value for the driver. scaler
is second in precedence. adder and scaler are an alterantive to using ref
and ref0.
parallel_deriv_color : string
If specified, this design var will be grouped for parallel derivative
calculations with other variables sharing the same parallel_deriv_color.
Expand Down
8 changes: 5 additions & 3 deletions openmdao/core/total_jac.py
Expand Up @@ -1255,8 +1255,9 @@ def compute_totals(self):

jac_setter(inds, mode)

# Driver scaling.
if self.has_scaling:
self._do_scaling(self.J_dict)
self._do_driver_scaling(self.J_dict)

if debug_print:
# Debug outputs scaled derivatives.
Expand Down Expand Up @@ -1360,8 +1361,9 @@ def compute_totals_approx(self, initialize=False):
msg = "Unsupported return format '%s." % return_format
raise NotImplementedError(msg)

# Driver scaling.
if self.has_scaling:
self._do_scaling(totals)
self._do_driver_scaling(totals)

if return_format == 'array':
totals = self.J # change back to array version
Expand Down Expand Up @@ -1412,7 +1414,7 @@ def _save_linear_solution(self, vec_names, key, mode):
doutputs = self.output_vec[mode][vec_name]
save_vec[:] = doutputs._data

def _do_scaling(self, J):
def _do_driver_scaling(self, J):
"""
Apply scalers to the jacobian if the driver defined any.
Expand Down

0 comments on commit 74f2425

Please sign in to comment.