Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
rate_units = get_rate_units(units, time_units, deriv=1)
rate2_units = get_rate_units(units, time_units, deriv=2)
self.add_output('controls:{0}'.format(control_name), shape=shape, units=units)
self.add_output('control_rates:{0}_rate'.format(control_name), shape=shape,
units=rate_units)
self.add_output('control_rates:{0}_rate2'.format(control_name), shape=shape,
units=rate2_units)
for control_name, options in self.options['polynomial_control_options'].items():
shape = options['shape']
units = options['units']
rate_units = get_rate_units(units, time_units, deriv=1)
rate2_units = get_rate_units(units, time_units, deriv=2)
self.add_output('polynomial_controls:{0}'.format(control_name), shape=shape,
units=units)
self.add_output('polynomial_control_rates:{0}_rate'.format(control_name), shape=shape,
units=rate_units)
self.add_output('polynomial_control_rates:{0}_rate2'.format(control_name), shape=shape,
units=rate2_units)
ode_iface.model.add_subsystem('indep_states', subsys=indep, promotes_outputs=['*'])
# The Controls comp
if self.control_options:
indep = IndepVarComp()
for name, options in iteritems(self.control_options):
ode_targets = self.ode_options._parameters[name]['targets']
indep.add_output('control_values:{0}'.format(name),
shape=(1, np.prod(options['shape'])),
units=options['units'])
indep.add_output('control_rates:{0}_rate'.format(name),
shape=(1, np.prod(options['shape'])),
units=get_rate_units(options['units'], time_options['units']))
indep.add_output('control_rates:{0}_rate2'.format(name),
shape=(1, np.prod(options['shape'])),
units=get_rate_units(options['units'], time_options['units'],
deriv=2))
if ode_targets:
ode_iface.model.connect('control_values:{0}'.format(name),
['ode.{0}'.format(tgt) for tgt in ode_targets])
if options['rate_param']:
ode_targets = self.ode_options._parameters[options['rate_param']]['targets']
ode_iface.model.connect('control_rates:{0}_rate'.format(name),
['ode.{0}'.format(tgt) for tgt in ode_targets])
if options['rate2_param']:
ode_targets = self.ode_options._parameters[options['rate2_param']]['targets']
ode_iface.model.connect('control_rates:{0}_rate2'.format(name),
['ode.{0}'.format(tgt) for tgt in ode_targets])
ode_iface.model.add_subsystem('controls_comp', subsys=indep, promotes_outputs=['*'])
# The Design parameters comp
state_units = self.state_options[var]['units']
options['shape'] = state_shape
options['units'] = state_units if con_units is None else con_units
options['linear'] = False
for iseg in range(gd.num_segments):
src = 'seg_{0}.step_states:{1}'.format(iseg, var)
tgt = 'path_constraints.seg_{0}_values:{1}'.format(iseg, con_name)
self.connect(src_name=src, tgt_name=tgt)
elif var_type in ('indep_control', 'input_control', 'control_rate', 'control_rate2'):
control_shape = self.control_options[var]['shape']
control_units = self.control_options[var]['units']
options['shape'] = control_shape
if var_type == 'control_rate':
options['units'] = get_rate_units(control_units, time_units) \
if con_units is None else con_units
elif var_type == 'control_rate2':
options['units'] = get_rate_units(control_units, time_units, deriv=2) \
if con_units is None else con_units
else:
options['units'] = control_units if con_units is None else con_units
options['linear'] = False
size = np.prod(options['shape'])
for iseg in range(gd.num_segments):
# Get all indices of the source
num_steps = self.grid_data.num_steps_per_segment[iseg]
src_total_size = num_steps * num_stages * size
src_indexer = np.reshape(np.arange(src_total_size, dtype=int),
newshape=(num_steps, num_stages) + options['shape'])
# Select only the indices that are step values
src_idxs = np.concatenate((src_indexer[:, 0, ...], src_indexer[-1:, -1, ...]),
def setup(self):
time_units = self.options['time_units']
self.add_input('time', val=1.0, units=time_units)
for control_name, options in self.options['control_options'].items():
shape = options['shape']
units = options['units']
rate_units = get_rate_units(units, time_units, deriv=1)
rate2_units = get_rate_units(units, time_units, deriv=2)
self.add_output('controls:{0}'.format(control_name), shape=shape, units=units)
self.add_output('control_rates:{0}_rate'.format(control_name), shape=shape,
units=rate_units)
self.add_output('control_rates:{0}_rate2'.format(control_name), shape=shape,
units=rate2_units)
for control_name, options in self.options['polynomial_control_options'].items():
shape = options['shape']
units = options['units']
rate_units = get_rate_units(units, time_units, deriv=1)
rate2_units = get_rate_units(units, time_units, deriv=2)
def _setup_controls(self):
control_options = self.options['control_options']
num_nodes = self.num_nodes
time_units = self.options['time_units']
for name, options in iteritems(control_options):
self._input_names[name] = 'controls:{0}'.format(name)
self._output_rate_names[name] = 'control_rates:{0}_rate'.format(name)
self._output_rate2_names[name] = 'control_rates:{0}_rate2'.format(name)
shape = options['shape']
units = options['units']
io_shape = (num_nodes,) + shape
rate_units = get_rate_units(units, time_units)
rate2_units = get_rate_units(units, time_units, deriv=2)
self._dynamic_names.append(name)
self.add_input(self._input_names[name], val=np.ones(io_shape), units=units)
self.add_output(self._output_rate_names[name], shape=io_shape, units=rate_units)
self.add_output(self._output_rate2_names[name], shape=io_shape,
units=rate2_units)
size = np.prod(shape)
self.jacs[name] = np.zeros((num_nodes, size, num_nodes, size))
self.jacs2[name] = np.zeros((num_nodes, size, num_nodes, size))
for i in range(size):
self.jacs[name][:, i, :, i] = self.D
self.jacs2[name][:, i, :, i] = self.D2
num_steps = self.options['num_steps']
method = self.options['method']
num_stages = rk_methods[method]['num_stages']
num_nodes = num_steps * num_stages
for name, options in iteritems(control_options):
self._input_names[name] = 'disc_controls:{0}'.format(name)
self._output_val_names[name] = 'stage_control_values:{0}'.format(name)
self._output_rate_names[name] = 'stage_control_rates:{0}_rate'.format(name)
self._output_rate2_names[name] = 'stage_control_rates:{0}_rate2'.format(name)
shape = options['shape']
input_shape = (num_control_disc_nodes,) + shape
output_shape = (num_steps, num_stages) + shape
units = options['units']
rate_units = get_rate_units(units, time_units)
rate2_units = get_rate_units(units, time_units, deriv=2)
self._dynamic_names.append(name)
self.add_input(self._input_names[name], val=np.ones(input_shape), units=units)
self.add_output(self._output_val_names[name], shape=output_shape, units=units)
self.add_output(self._output_rate_names[name], shape=output_shape, units=rate_units)
self.add_output(self._output_rate2_names[name], shape=output_shape,
units=rate2_units)
size = np.prod(shape)
self.val_jacs[name] = np.zeros((num_nodes, size, num_control_disc_nodes, size))
self.rate_jacs[name] = np.zeros((num_nodes, size, num_control_disc_nodes, size))
ode_iface.model.connect(rate_path,
'state_rate_collector.state_rates_in:{0}_rate'.format(name))
ode_iface.model.add_subsystem('indep_states', subsys=indep, promotes_outputs=['*'])
# The Controls comp
if self.control_options:
indep = IndepVarComp()
for name, options in iteritems(self.control_options):
ode_targets = self.ode_options._parameters[name]['targets']
indep.add_output('control_values:{0}'.format(name),
shape=(1, np.prod(options['shape'])),
units=options['units'])
indep.add_output('control_rates:{0}_rate'.format(name),
shape=(1, np.prod(options['shape'])),
units=get_rate_units(options['units'], time_options['units']))
indep.add_output('control_rates:{0}_rate2'.format(name),
shape=(1, np.prod(options['shape'])),
units=get_rate_units(options['units'], time_options['units'],
deriv=2))
if ode_targets:
ode_iface.model.connect('control_values:{0}'.format(name),
['ode.{0}'.format(tgt) for tgt in ode_targets])
if options['rate_param']:
ode_targets = self.ode_options._parameters[options['rate_param']]['targets']
ode_iface.model.connect('control_rates:{0}_rate'.format(name),
['ode.{0}'.format(tgt) for tgt in ode_targets])
if options['rate2_param']:
ode_targets = self.ode_options._parameters[options['rate2_param']]['targets']
ode_iface.model.connect('control_rates:{0}_rate2'.format(name),
['ode.{0}'.format(tgt) for tgt in ode_targets])
options['linear'] = False
elif var_type in ('control_rate', 'control_rate2'):
if var.endswith('_rate'):
control_name = var[:-5]
elif var.endswith('_rate2'):
control_name = var[:-6]
control_shape = phase.control_options[control_name]['shape']
control_units = phase.control_options[control_name]['units']
options['shape'] = control_shape
if var_type == 'control_rate':
options['units'] = get_rate_units(control_units, time_units) \
if con_units is None else con_units
elif var_type == 'control_rate2':
options['units'] = get_rate_units(control_units, time_units, deriv=2) \
if con_units is None else con_units
options['linear'] = False
elif var_type in ('polynomial_control_rate', 'polynomial_control_rate2'):
if var.endswith('_rate'):
control_name = var[:-5]
elif var.endswith('_rate2'):
control_name = var[:-6]
control_shape = phase.polynomial_control_options[control_name]['shape']
control_units = phase.polynomial_control_options[control_name]['units']
options['shape'] = control_shape
if var_type == 'polynomial_control_rate':
options['units'] = get_rate_units(control_units, time_units) \
if con_units is None else con_units
num_segments = self.options['grid_data'].num_segments
time_units = self.options['time_units']
if num_segments <= 1:
# Control value and rate continuity is enforced even with compressed transcription
return
self.add_input('t_duration', units=time_units, val=1.0,
desc='time duration of the phase')
for control_name, options in control_options.items():
shape = options['shape']
size = np.prod(shape)
units = options['units']
rate_units = get_rate_units(units, time_units, deriv=1)
rate2_units = get_rate_units(units, time_units, deriv=2)
# Define the sparsity pattern for rate and rate2 continuity
rs_size1 = np.repeat(np.arange(num_segments - 1, dtype=int), 2)
cs_size1 = np.arange(1, num_segend_nodes - 1, dtype=int)
template = np.zeros((num_segments - 1, num_segend_nodes))
template[rs_size1, cs_size1] = 1.0
template = np.kron(template, np.eye(size))
rs, cs = template.nonzero()
vals = np.zeros(len(rs), dtype=float)
vals[0::2] = -1.0
vals[1::2] = 1.0
self.rate_jac_templates[control_name] = vals
#
def setup(self):
self._var_names = {}
num_seg = self.options['num_segments']
rk_data = rk_methods[self.options['method']]
num_stages = rk_data['num_stages']
self.add_input('h', val=np.ones(num_seg), units=self.options['time_units'],
desc='step size for current Runge-Kutta segment.')
for name, options in self.options['state_options'].items():
shape = options['shape']
units = options['units']
rate_units = get_rate_units(units, self.options['time_units'])
self._var_names[name] = {}
self._var_names[name]['f'] = 'f:{0}'.format(name)
self._var_names[name]['k'] = 'k:{0}'.format(name)
self.add_input(self._var_names[name]['f'], shape=(num_seg, num_stages) + shape,
units=rate_units,
desc='The predicted values of the state at the ODE evaluation points.')
self.add_output(self._var_names[name]['k'], shape=(num_seg, num_stages) + shape,
units=units, desc='RK multiplier k for each stage in the segment.')
size = np.prod(shape)
ar = np.arange(size * num_stages * num_seg, dtype=int)
self.declare_partials(of=self._var_names[name]['k'],
wrt=self._var_names[name]['f'],