Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementation for POEM_022 - Determining variable shape at runtime based on connections #1671

Merged
merged 35 commits into from
Sep 14, 2020
Merged
Show file tree
Hide file tree
Changes from 29 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
a0da723
tests passing. need more tests
naylor-b Aug 25, 2020
2f48290
tests passing
naylor-b Aug 25, 2020
454ab04
Merge branch 'master' of https://github.com/OpenMDAO/OpenMDAO into dy…
naylor-b Aug 28, 2020
c8daaf7
merged out pull_src_err branch. tests passing
naylor-b Aug 28, 2020
b0fc3f9
added cycle test
naylor-b Aug 28, 2020
cd4acda
added more tests and reworked the algorithm to take advantage of the …
naylor-b Aug 29, 2020
8dd9861
updated comment
naylor-b Aug 31, 2020
b616a48
Merge branch 'master' of https://github.com/OpenMDAO/OpenMDAO into dy…
naylor-b Aug 31, 2020
1de2956
fixed test and added docs
naylor-b Aug 31, 2020
e4a26ab
renamed dyn shape function to _setup_dynamic_shapes
naylor-b Aug 31, 2020
3a968e6
added resetup test
naylor-b Aug 31, 2020
f137b45
fix for resetup if vars created in __init__
naylor-b Aug 31, 2020
1bc86a3
more testing. 1 failure
naylor-b Sep 1, 2020
a2f4d4d
Merge branch 'master' of https://github.com/OpenMDAO/OpenMDAO into dy…
naylor-b Sep 3, 2020
4570275
commented out test
naylor-b Sep 3, 2020
1b80f08
interim
naylor-b Sep 3, 2020
da900e4
tests passing
naylor-b Sep 4, 2020
f9044b2
cleanup
naylor-b Sep 4, 2020
aeeb765
Merge branch 'master' of https://github.com/OpenMDAO/OpenMDAO into dy…
naylor-b Sep 4, 2020
52bd7c5
added mismatched dyn shape test and setup_partials hook
naylor-b Sep 4, 2020
c0bcf9e
tests passing. updated docs and moved declare_partials to setup_partials
naylor-b Sep 4, 2020
9e59d67
more setup_partials conversions. added error message for shape misma…
naylor-b Sep 4, 2020
605c1d6
added test using dynamic sizing and setup_partials
naylor-b Sep 4, 2020
c565c2a
added test for access to shape/size/value during configure for dynami…
naylor-b Sep 8, 2020
8a75139
Merge branch 'master' of https://github.com/OpenMDAO/OpenMDAO into dy…
naylor-b Sep 8, 2020
b749870
cleanup
naylor-b Sep 8, 2020
73cfd0d
cleanup
naylor-b Sep 8, 2020
fdf431c
no-mpi fix
naylor-b Sep 8, 2020
db6d6d2
more tests
naylor-b Sep 9, 2020
41a9d80
added a test
naylor-b Sep 9, 2020
4877290
added openmdao command view_dyn_shapes, added error check for src_ind…
naylor-b Sep 10, 2020
137d857
fixed expected test results
naylor-b Sep 10, 2020
e0c8172
added check for empty graph
naylor-b Sep 10, 2020
4308ab6
tweaked display of missing shapes in dyn shape plot
naylor-b Sep 14, 2020
cd543fa
review fixes
naylor-b Sep 14, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 11 additions & 3 deletions openmdao/components/exec_comp.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# Names of metadata entries allowed for ExecComp variables.
_allowed_meta = {'value', 'shape', 'units', 'res_units', 'desc',
'ref', 'ref0', 'res_ref', 'lower', 'upper', 'src_indices',
'flat_src_indices', 'tags'}
'flat_src_indices', 'tags', 'shape_by_conn', 'copy_shape'}

# Names that are not allowed for input or output variables (keywords for options)
_disallowed_names = {'has_diag_partials', 'units', 'shape'}
Expand Down Expand Up @@ -289,6 +289,12 @@ def setup(self):
init_vals[arg] = val['value']
del kwargs2[arg]['value']

if 'shape_by_conn' in val or 'copy_shape' in val:
if val.get('shape') is not None or val.get('value') is not None:
raise RuntimeError(f"{self.msginfo}: Can't set 'shape' or 'value' for "
f"variable '{arg}' along with 'copy_shape' or "
"'shape_by_conn'.")

if 'shape' in val:
if arg not in init_vals:
init_vals[arg] = np.ones(val['shape'])
Expand All @@ -302,14 +308,16 @@ def setup(self):
init_vals[arg] = val

for var in sorted(allvars):
meta = kwargs2.get(var, {'units': units, 'shape': shape})

# if user supplied an initial value, use it, otherwise set to 1.0
if var in init_vals:
val = init_vals[var]
elif 'shape_by_conn' or 'copy_shape' in meta:
init_vals[var] = val = None
else:
init_vals[var] = val = 1.0

meta = kwargs2.get(var, {'units': units, 'shape': shape})

if var in outs:
self.add_output(var, val, **meta)
else:
Expand Down
13 changes: 10 additions & 3 deletions openmdao/components/linear_system_comp.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ def setup(self):
vec_size = self.options['vec_size']
vec_size_A = self.vec_size_A = vec_size if self.options['vectorize_A'] else 1
size = self.options['size']
mat_size = size * size
full_size = size * vec_size

self._lup = []
shape = (vec_size, size) if vec_size > 1 else (size, )
Expand All @@ -64,7 +62,16 @@ def setup(self):
self.add_input("b", val=np.ones(shape))
self.add_output("x", shape=shape, val=.1)

# Set up the derivatives.
def setup_partials(self):
"""
Set up the derivatives.
"""
vec_size = self.options['vec_size']
vec_size_A = self.vec_size_A = vec_size if self.options['vectorize_A'] else 1
size = self.options['size']
mat_size = size * size
full_size = size * vec_size

row_col = np.arange(full_size, dtype="int")

self.declare_partials('x', 'b', val=np.full(full_size, -1.0), rows=row_col, cols=row_col)
Expand Down
2 changes: 1 addition & 1 deletion openmdao/components/meta_model_structured_comp.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ def _setup_partials(self):
'dependent': True,
}

for name in self._outputs:
for name in self._var_rel_names['output']:
self._declare_partials(of=name, wrt=pnames, dct=dct)
if self.options['training_data_gradients']:
self._declare_partials(of=name, wrt="%s_train" % name, dct={'dependent': True})
Expand Down
15 changes: 13 additions & 2 deletions openmdao/components/multifi_meta_model_unstructured_comp.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,8 @@ def add_input(self, name, val=1.0, shape=None, src_indices=None, flat_src_indice
self._input_sizes[fi] += input_size

def add_output(self, name, val=1.0, surrogate=None, shape=None, units=None, res_units=None,
desc='', lower=None, upper=None, ref=1.0, ref0=0.0, res_ref=1.0):
desc='', lower=None, upper=None, ref=1.0, ref0=0.0, res_ref=1.0, tags=None,
shape_by_conn=False, copy_shape=None):
"""
Add an output variable to the component.

Expand Down Expand Up @@ -230,13 +231,23 @@ def add_output(self, name, val=1.0, surrogate=None, shape=None, units=None, res_
res_ref : float
Scaling parameter. The value in the user-defined res_units of this output's residual
when the scaled value is 1. Default is 1.
tags : str or list of strs or set of strs
User defined tags that can be used to filter what gets listed when calling
list_inputs and list_outputs.
shape_by_conn : bool
If True, shape this output to match its connected input(s).
copy_shape : str or None
If a str, that str is the name of a variable. Shape this output to match that of
the named variable.
"""
super(MultiFiMetaModelUnStructuredComp, self).add_output(name, val, shape=shape,
units=units, res_units=res_units,
desc=desc, lower=lower,
upper=upper, ref=ref,
ref0=ref0, res_ref=res_ref,
surrogate=surrogate)
surrogate=surrogate, tags=tags,
shape_by_conn=shape_by_conn,
copy_shape=copy_shape)
self._training_output[name] = self._nfi * [np.empty(0)]

# Add train:<outvar>_fi<n>
Expand Down
6 changes: 5 additions & 1 deletion openmdao/components/tests/test_external_code_comp.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ def setup(self):
sys.executable, 'extcode_paraboloid.py', self.input_file, self.output_file
]

def setup_partials(self):
# this external code does not provide derivatives, use finite difference
self.declare_partials(of='*', wrt='*', method='fd')

Expand Down Expand Up @@ -357,6 +358,7 @@ def setup(self):
self.input_file, self.output_file, self.derivs_file
]

def setup_partials(self):
# this external code does provide derivatives
self.declare_partials(of='*', wrt='*')

Expand Down Expand Up @@ -549,7 +551,6 @@ def initialize(self):
def setup(self):
self.add_input('area_ratio', val=1.0, units=None)
self.add_output('mach', val=1., units=None)
self.declare_partials(of='mach', wrt='area_ratio', method='fd')

self.input_file = 'mach_input.dat'
self.output_file = 'mach_output.dat'
Expand All @@ -570,6 +571,9 @@ def setup(self):
# If you want to write your own string command, the code below will also work.
# self.options['command_apply'] = ('python extcode_mach.py {} {}').format(self.input_file, self.output_file)

def setup_partials(self):
self.declare_partials(of='mach', wrt='area_ratio', method='fd')

def apply_nonlinear(self, inputs, outputs, residuals):
with open(self.input_file, 'w') as input_file:
input_file.write('residuals\n')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -890,6 +890,8 @@ def setup(self):
surrogate = SinSurrogate()
self.add_input('x', 0.)
self.add_output('sin_x', 0., surrogate=surrogate)

def setup_partials(self):
self.declare_partials('sin_x', 'x', method='fd',
form='backward', step=1e-7, step_calc='rel')

Expand Down