Skip to content

OpenMdaoSubProblem passes duplicated of=/wrt= lists to compute_totals #80

Description

@chrislupp

Problem

OpenMdaoSubProblem.compute_partials builds the of= and wrt= lists for compute_totals by appending once per entry in _partials_map, with no deduplication.

philote_mdo/openmdao/group.py:371-377:

func = []
var = []
for val in self._partials_map.values():
    func += [val[0]]
    var += [val[1]]

totals = self._prob.compute_totals(of=func, wrt=var)

_partials_map is keyed on the (output, input) pair, so an output with three inputs contributes its name to of three times, and an input feeding several outputs repeats in wrt.

Consequence

OpenMDAO tolerates the repeats, so results are correct. The cost is redundant work inside compute_totals, which grows with the number of declared partials -- exactly the disciplines where a gradient call is already the expensive part.

This is the mildest of the four issues found alongside #76 and is a cleanup rather than a defect.

Proposed fix

Deduplicate while preserving order:

func = list(dict.fromkeys(val[0] for val in self._partials_map.values()))
var = list(dict.fromkeys(val[1] for val in self._partials_map.values()))

The lookup below is unaffected, since it indexes totals by the (of, wrt) pair rather than by position:

for local, sub in self._partials_map.items():
    partials[local] = totals[sub]

Worth a test that declares an output against two inputs and asserts the lists handed to compute_totals contain no repeats.

Notes

Found while tracing OpenMdaoSubProblem for #76. Pre-existing on main.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions