Skip to content

Commit

Permalink
Merge pull request #1698 from tadkollar/aivc1684
Browse files Browse the repository at this point in the history
Make auto-ivc component visible again in N2
  • Loading branch information
swryan committed Sep 30, 2020
2 parents 9a5cfee + b401505 commit da4783b
Show file tree
Hide file tree
Showing 13 changed files with 1,224 additions and 1,130 deletions.
4 changes: 2 additions & 2 deletions openmdao/recorders/tests/test_sqlite_recorder.py
Expand Up @@ -555,7 +555,7 @@ def test_driver_records_metadata(self):
assertMetadataRecorded(self, prom2abs, abs2prom)
expected_problem_metadata = {
'connections_list_length': 11,
'tree_children_length': 5,
'tree_children_length': 6,
'abs2prom': abs2prom,
}
assertViewerDataRecorded(self, expected_problem_metadata)
Expand Down Expand Up @@ -2514,7 +2514,7 @@ def test_feature_problem_metadata(self):

self.assertEqual(cr.problem_metadata['tree']['name'], 'root')
self.assertEqual(sorted([child["name"] for child in cr.problem_metadata['tree']["children"]]),
['con_cmp1', 'con_cmp2', 'd1', 'd2', 'obj_cmp'])
['_auto_ivc', 'con_cmp1', 'con_cmp2', 'd1', 'd2', 'obj_cmp'])

def test_feature_problem_metadata_with_driver_information(self):
import openmdao.api as om
Expand Down
6 changes: 3 additions & 3 deletions openmdao/visualization/n2_viewer/index.html
Expand Up @@ -190,11 +190,11 @@
<div id="solvers-legend"></div>
</div>
<div class="draggable">
<div id='draggable-legend-dots'>
<div class='draggable-legend-dots'>
<p class="legend-dots"></p>
</div>
<div class="icon-key"></div>
<div id='draggable-legend-dots'>
<div class='draggable-legend-dots'>
<p class="legend-dots"></p>
</div>
</div>
Expand Down Expand Up @@ -246,4 +246,4 @@
{utils_lib} {symboltype_lib} {n2treenode_lib} {modeldata_lib} {n2style_lib}
{n2layout_lib} {n2matrixcell_lib} {n2legend_lib} {n2matrix_lib} {n2arrow_lib}
{n2search_lib} {n2toolbar_lib} {n2diagram_lib}
{n2userinterface_lib} {defaults_lib} {ptn2_lib}
{nodeinfo_lib} {n2userinterface_lib} {defaults_lib} {ptn2_lib}
8 changes: 5 additions & 3 deletions openmdao/visualization/n2_viewer/n2_viewer.py
Expand Up @@ -55,6 +55,8 @@ def _convert_nans_in_nested_list(val_as_list):
else:
if np.isnan(val):
val_as_list[i] = "nan"
elif np.isinf(val):
val_as_list[i] = "infinity"
else:
val_as_list[i] = val

Expand Down Expand Up @@ -177,9 +179,8 @@ def _get_tree_dict(system, component_execution_orders, component_execution_index

children = []
for s in system._subsystems_myproc:
if (s.name != '_auto_ivc'):
children.append(_get_tree_dict(s, component_execution_orders,
component_execution_index, is_parallel))
children.append(_get_tree_dict(s, component_execution_orders,
component_execution_index, is_parallel))

if system.comm.size > 1:
if system._subsystems_myproc:
Expand Down Expand Up @@ -501,6 +502,7 @@ def n2(data_source, outfile='n2.html', show_browser=True, embeddable=False,
'N2Search', \
'N2Toolbar', \
'N2Diagram', \
'NodeInfo', \
'N2UserInterface', \
'defaults', \
'ptN2'
Expand Down
48 changes: 45 additions & 3 deletions openmdao/visualization/n2_viewer/src/ModelData.js
Expand Up @@ -37,13 +37,18 @@ class ModelData {
this._computeConnections();
stopTimer('ModelData._computeConnections');

this._updateAutoIvcNames();

debugInfo("New model: ", this);
// this.errorCheck();
}

static uncompressModel(b64str) {
const compressedData = atob(b64str);
const jsonStr = window.pako.inflate(compressedData, { to: 'string' });
/* for ( let pos = 0; pos < jsonStr.length; pos += 100) {
console.log(pos, jsonStr.substring(pos, pos+99));
} */
return JSON.parse(jsonStr);
}

Expand Down Expand Up @@ -113,6 +118,13 @@ class ModelData {
node.absPathName += node.name;

this.nodePaths[node.absPathName] = node;

if (this.abs2prom.input[node.absPathName] !== undefined) {
node.promotedName = this.abs2prom.input[node.absPathName];
}
else if (this.abs2prom.output[node.absPathName] !== undefined) {
node.promotedName = this.abs2prom.output[node.absPathName];
}
}

this.identifyUnconnectedInput(node);
Expand Down Expand Up @@ -221,6 +233,24 @@ class ModelData {
return false;
}

/**
* Find the target of an Auto-IVC variable.
* @param {String} elementPath The full path of the element to check. Must start with _auto_ivc.
* @return {String} The absolute path of the target element, or undefined if not found.
*/
getAutoIvcTgt(elementPath) {
if (!elementPath.match(/^_auto_ivc.*$/)) return undefined;

for (let conn of this.conns) {
if (conn.src == elementPath) {
return conn.tgt;
}
}

console.warn(`No target connection found for ${elementPath}.`)
return undefined;
}

/**
* Create an array in each node containing references to its
* children that are subsystems. Runs recursively over the node's
Expand Down Expand Up @@ -286,9 +316,6 @@ class ModelData {
let throwLbl = 'ModelData._computeConnections: ';

for (let conn of this.conns) {
// Ignore connections from _auto_ivc, which is intentionally not included.
if (conn.src.match(/^_auto_ivc.*$/)) continue;

// Process sources
let srcObj = this.nodePaths[conn.src];

Expand Down Expand Up @@ -397,6 +424,21 @@ class ModelData {
}
}

/**
*
*/
_updateAutoIvcNames() {
const aivc = this.nodePaths['_auto_ivc'];
if (aivc !== undefined && aivc.hasChildren()) {
for (const ivc of aivc.children) {
const tgtPath = this.getAutoIvcTgt(ivc.absPathName);
if (tgtPath !== undefined) {
ivc.promotedName = this.nodePaths[tgtPath].promotedName;
}
}
}
}

/**
* If an element has no connection naming it as a source or target,
* relabel it as unconnected.
Expand Down
61 changes: 24 additions & 37 deletions openmdao/visualization/n2_viewer/src/N2Layout.js
Expand Up @@ -24,7 +24,6 @@ class N2Layout {
this.zoomedElement = newZoomedElement;
this.showLinearSolverNames = showLinearSolverNames;

this.outputNamingType = "Absolute";
this.zoomedNodes = [];
this.visibleNodes = [];

Expand All @@ -46,9 +45,6 @@ class N2Layout {
this._updateTextWidths();
stopTimer('N2Layout._updateTextWidths');

startTimer('N2Layout._updateSolverTextWidths');
this._updateSolverTextWidths();
stopTimer('N2Layout._updateSolverTextWidths');
delete (this.textRenderer);

startTimer('N2Layout._computeColumnWidths');
Expand Down Expand Up @@ -165,11 +161,11 @@ class N2Layout {

let retVal = node.name;

if (this.outputNamingType == "Promoted" &&
node.isInputOrOutput() &&
this.zoomedElement.propExists('promotions') &&
this.zoomedElement.promotions.propExists(node.absPathName)) {
retVal = this.zoomedElement.promotions[node.absPathName];
if (node.name == '_auto_ivc') {
retVal = 'Auto-IVC';
}
else if (node.absPathName.match(/^_auto_ivc.*/) && node.promotedName !== undefined) {
retVal = node.promotedName;
}

return retVal;
Expand Down Expand Up @@ -203,41 +199,31 @@ class N2Layout {
node.nameWidthPx = this._getTextWidth(this.getText(node)) + 2 *
this.size.rightTextMargin;

if (node.hasChildren() && !node.isMinimized) {
for (let child of node.children) {
this._updateTextWidths(child);
}
}
}

/**
* Determine text width for this and all decendents if node is a solver.
* @param {N2TreeNode} [node = this.zoomedElement] Item to begin looking from.
*/
_updateSolverTextWidths(node = this.zoomedElement) {
if (node.isInput() || node.varIsHidden) {
return;
if (!node.isInputOrOutput()) {
node.nameSolverWidthPx = this._getTextWidth(this.getSolverText(node)) + 2 *
this.size.rightTextMargin;
}

node.nameSolverWidthPx = this._getTextWidth(this.getSolverText(node)) + 2 *
this.size.rightTextMargin;

if (node.hasChildren() && !node.isMinimized) {
for (let child of node.children) {
this._updateSolverTextWidths(child);
this._updateTextWidths(child);
}
}
}

/** Recurse through the tree and add up the number of leaves that each
/**
* Recurse through the tree and add up the number of leaves that each
* node has, based on their array of visible children.
* @param {N2TreeNode} [node = this.model.root] The starting node.
*/
_computeLeaves(node = this.model.root) {
node.numLeaves = 0;

if (! node.varIsHidden) {
if (this.model.nodeIds.length > Precollapse.minimumNodes) {
if (!node.varIsHidden) {
if (node.name == '_auto_ivc' && !node.manuallyExpanded) {
node.minimize();
}
else if (this.model.nodeIds.length > Precollapse.minimumNodes) {
node.minimizeIfLarge(this.model.depthCount[node.depth]);
}

Expand All @@ -253,7 +239,8 @@ class N2Layout {
}
}

/** For visible nodes with children, choose a column width
/**
* For visible nodes with children, choose a column width
* large enough to accomodate the widest label in their column.
* @param {N2TreeNode} node The item to operate on.
* @param {string} childrenProp Either 'children' or 'subsystem_children'.
Expand Down Expand Up @@ -284,7 +271,8 @@ class N2Layout {
}
}

/** Compute column widths across the model, then adjust ends as needed.
/**
* Compute column widths across the model, then adjust ends as needed.
* @param {N2TreeNode} [node = this.zoomedElement] Item to operate on.
*/
_computeColumnWidths(node = this.zoomedElement) {
Expand Down Expand Up @@ -313,7 +301,8 @@ class N2Layout {
this.cols[this.greatestDepth].width = lastColumnWidth;
}

/** Compute solver column widths across the model, then adjust ends as needed.
/**
* Compute solver column widths across the model, then adjust ends as needed.
* @param {N2TreeNode} [node = this.zoomedElement] Item to operate on.
*/
_computeSolverColumnWidths(node = this.zoomedElement) {
Expand Down Expand Up @@ -342,9 +331,7 @@ class N2Layout {
this.solverCols[this.greatestDepth].width = lastColumnWidth;
}

/** Set the location of the columns based on the width of the columns
* to the left.
*/
/** Set the location of the columns based on the width of the columns to the left. */
_setColumnLocations() {
this.size.partitionTree.width = 0;
this.size.solverTree.width = 0;
Expand Down Expand Up @@ -523,7 +510,7 @@ class N2Layout {
.duration(N2TransitionDefaults.duration)
.delay(transitionStartDelay)
// Hide the transition waiting animation when it ends:
.on('end', function() { dom.waiter.attr('class', 'no-show'); });
.on('end', function () { dom.waiter.attr('class', 'no-show'); });

this.transitionStartDelay = N2TransitionDefaults.startDelay;

Expand Down
6 changes: 5 additions & 1 deletion openmdao/visualization/n2_viewer/src/N2Style.js
Expand Up @@ -92,6 +92,9 @@ class N2Style {
'fill-opacity': '.8',
'fill': N2Style.color.collapsed,
},
'#tree > g.minimized > text': {
'fill': N2Style.color.collapsedText,
},
'#tree > g.autoivc_input > rect': {
'cursor': 'pointer',
'fill-opacity': '.8',
Expand Down Expand Up @@ -231,11 +234,12 @@ N2Style.color = {
'group': '#6092B5',
'component': '#02BFFF',
'collapsed': '#555555',
'collapsedText': 'white',
'unconnectedInput': '#F42F0D',
'inputArrow': 'salmon',
'outputArrow': 'seagreen',
'declaredPartial': 'black',
'autoivcInput': '#F42F0D'
'autoivcInput': '#ff7000'
};

Object.freeze(N2Style.color); // Make it the equivalent of a constant
Expand Down

0 comments on commit da4783b

Please sign in to comment.