From caaeb1fa96b9547fb94b9e0e3ca85ed29a0b41d8 Mon Sep 17 00:00:00 2001
From: Amanda Ghassaei <amandaghassaei@gmail.com>
Date: Sat, 24 Oct 2015 02:12:46 -0400
Subject: [PATCH] calculate structural connectivity

---
 js/cells/GIKCell.js                           | 10 ++++
 js/menus/ESetupMenuView.js                    | 10 +++-
 js/menus/templates/ESetupMenuView.html        | 20 +++++++-
 js/simulation/electronics/LatticeEsim.js      | 46 ++++++++++++++++++-
 js/simulation/electronics/cells/eSimCell.js   | 23 ++++++++++
 .../electronics/cells/eSimSuperCell.js        | 11 +++++
 js/simulation/electronics/eSim.js             |  7 +++
 7 files changed, 121 insertions(+), 6 deletions(-)

diff --git a/js/cells/GIKCell.js b/js/cells/GIKCell.js
index 1b0b7773..b1b54507 100644
--- a/js/cells/GIKCell.js
+++ b/js/cells/GIKCell.js
@@ -90,5 +90,15 @@ define(['underscore', 'three', 'threeModel', 'lattice', 'appState', 'cubeCell'],
         CubeCell.prototype.propagateConductorGroupNum.call(this, num);
     };
 
+    GIKCell.prototype.propagateStructuralGroupNum = function(num){
+        if (num === undefined) num = this._eSimStructuralGroup;
+        var self = this;
+        this.superCell._loopCells(function(cell){
+            if (cell == self) return;
+            if (cell) cell.setStructuralGroupNum(num);
+        });
+        CubeCell.prototype.propagateStructuralGroupNum.call(this, num);
+    };
+
     return GIKCell;
 });
\ No newline at end of file
diff --git a/js/menus/ESetupMenuView.js b/js/menus/ESetupMenuView.js
index 0105bdf9..8bc8a982 100644
--- a/js/menus/ESetupMenuView.js
+++ b/js/menus/ESetupMenuView.js
@@ -8,7 +8,8 @@ define(['jquery', 'underscore', 'menuParent', 'plist', 'materialsPlist', 'text!e
     return MenuParentView.extend({
 
         events: {
-            "click #calcConnectivity":                              "_calcConnectivity",
+            "click #calcElectricalConnectivity":                    "_calcElectricalConnectivity",
+            "click #calcStructuralConnectivity":                    "_calcStructuralConnectivity",
             "click #showOverlappingCells":                          "_showOverlappingCells"
         },
 
@@ -27,11 +28,16 @@ define(['jquery', 'underscore', 'menuParent', 'plist', 'materialsPlist', 'text!e
             this.model.set("currentNav", materialClass + "NavSim");
         },
 
-        _calcConnectivity: function(e){
+        _calcElectricalConnectivity: function(e){
             e.preventDefault();
             lattice.calculateConductorConnectivity();
         },
 
+        _calcStructuralConnectivity: function(e){
+            e.preventDefault();
+            lattice.calculateStructuralConnectivity();
+        },
+
         _showOverlappingCells: function(e){
             e.preventDefault();
             lattice.highlightOverlappingCells();
diff --git a/js/menus/templates/ESetupMenuView.html b/js/menus/templates/ESetupMenuView.html
index c102974e..8dc6bdb7 100644
--- a/js/menus/templates/ESetupMenuView.html
+++ b/js/menus/templates/ESetupMenuView.html
@@ -12,7 +12,7 @@ Simulation Type: &nbsp;&nbsp;
             <% }); %>
         </ul>
     </div><br/><br/>
-<a href="#" id="calcConnectivity" class="btn btn-block btn-lg btn-default"><% if(conductorGroups){ %>Re-<% } %>Calculate Connectivity</a><br/>
+<a href="#" id="calcElectricalConnectivity" class="btn btn-block btn-lg btn-default"><% if(conductorGroups){ %>Re-<% } %>Calculate Electrical Connectivity</a><br/>
 <% if(conductorGroups){ %>
     Num Connected Conductors: &nbsp;&nbsp<%= _.keys(conductorGroups).length %><br/>
     <% if(_.keys(conductorGroups).length > 0){ %>
@@ -31,4 +31,20 @@ Simulation Type: &nbsp;&nbsp;
             </label>
         <% }); %>
     <% } %>
-<% } %>
\ No newline at end of file
+<% } %>
+<a href="#" id="calcStructuralConnectivity" class="btn btn-block btn-lg btn-default"><% if(structuralGroups){ %>Re-<% } %>Calculate Structural Connectivity</a><br/>
+<% if(structuralGroups){ %>
+    Num Separate Structures: &nbsp;&nbsp<%= _.keys(structuralGroups).length %><br/>
+    <% if(_.keys(structuralGroups).length > 0){ %>
+        <label class="radio">
+            <input type="radio" <% if (visibleStructuralGroup == -1){ %>checked<% } %> name="visibleStructuralGroup" value="-1" data-toggle="radio" class="custom-radio eSim"><span class="icons"><span class="icon-unchecked"></span><span class="icon-checked"></span></span>
+            Show Everything
+        </label>
+        <% var index = 0;  _.each(structuralGroups, function(groupData, id){ %>
+            <label class="radio">
+                <input type="radio" <% if (id == visibleStructuralGroup){ %>checked<% } %> name="visibleStructuralGroup" value="<%= id %>" data-toggle="radio" class="custom-radio eSim"><span class="icons"><span class="icon-unchecked"></span><span class="icon-checked"></span></span>
+                Group <%= ++index %>
+            </label>
+        <% }); %>
+    <% } %>
+<% } %>
diff --git a/js/simulation/electronics/LatticeEsim.js b/js/simulation/electronics/LatticeEsim.js
index 90c555bf..12ba6085 100644
--- a/js/simulation/electronics/LatticeEsim.js
+++ b/js/simulation/electronics/LatticeEsim.js
@@ -31,6 +31,47 @@ define(['lattice', 'appState', 'three', 'threeModel', 'numeric', 'eSim', 'eSimFi
             three.render();
         },
 
+        _showStructure: function(){
+            var groupNum = eSim.get("visibleStructuralGroup");
+            if (!eSim.get("structuralGroups") || _.keys(eSim.get("structuralGroups")).length == 0 || groupNum == -1){
+                this.setOpaque();
+                three.render();
+                return;
+            }
+            var allVisible = groupNum == -1;
+            this._loopCells(this.sparseCells, function(cell){
+                if (cell) cell.setTransparentByEval(function(evalCell){
+                    return !(allVisible || evalCell.structuralGroupVisible(groupNum));
+                });
+            });
+            three.render();
+        },
+
+        calculateStructuralConnectivity: function(){
+            var num = 1;
+            this._loopCells(this.cells, function(cell){
+                if (cell) cell.setStructuralGroupNum(num++, true);
+            });
+            this._loopCells(this.cells, function(cell){
+                if (cell) cell.propagateStructuralGroupNum();
+            });
+            this._calcNumberStructurallyConnectedComponents();
+            this._showConductors();
+        },
+
+        _calcNumberStructurallyConnectedComponents: function(){
+            var groups = {};
+            this._loopCells(this.cells, function(cell){
+                if (!cell) return;
+                if (_.filter(groups, function(group){
+                    return group.id == cell.getStructuralGroupNum();
+                }).length == 0) {
+                    groups[cell.getStructuralGroupNum()] = {};
+                }
+            });
+            eSim.set("structuralGroups", groups);
+        },
+
         calculateConductorConnectivity: function(){
             var num = 1;
             this._loopCells(this.cells, function(cell){
@@ -39,11 +80,11 @@ define(['lattice', 'appState', 'three', 'threeModel', 'numeric', 'eSim', 'eSimFi
             this._loopCells(this.cells, function(cell){
                 if (cell) cell.propagateConductorGroupNum();
             });
-            this._calcNumberConnectedComponents();
+            this._calcNumberDCConnectedComponents();
             this._showConductors();
         },
 
-        _calcNumberConnectedComponents: function(){
+        _calcNumberDCConnectedComponents: function(){
             var groups = {};
             this._loopCells(this.cells, function(cell){
                 if (!cell) return;
@@ -207,6 +248,7 @@ define(['lattice', 'appState', 'three', 'threeModel', 'numeric', 'eSim', 'eSimFi
     _.extend(lattice, eSimMethods);
     lattice.listenTo(appState, "change:currentTab", lattice._eSimTabChanged);
     lattice.listenTo(eSim, "change:visibleConductorGroup", lattice._showConductors);
+    lattice.listenTo(eSim, "change:visibleStructuralGroup", lattice._showStructure);
     lattice._showConductors();
 
 
diff --git a/js/simulation/electronics/cells/eSimCell.js b/js/simulation/electronics/cells/eSimCell.js
index 1f0eef5d..b93eb8c1 100644
--- a/js/simulation/electronics/cells/eSimCell.js
+++ b/js/simulation/electronics/cells/eSimCell.js
@@ -33,5 +33,28 @@ define(['cell', 'lattice'], function(DMACell, lattice){
         return this.isConductive() && (allVisible || groupNum == this._eSimConductorGroup);
     };
 
+    DMACell.prototype.setStructuralGroupNum = function(num, force){
+        if (force) this._eSimStructuralGroup = num;
+        else if (this._eSimStructuralGroup>num){
+            this._eSimStructuralGroup = num;
+            this.propagateStructuralGroupNum(num);
+        }
+    };
+
+    DMACell.prototype.propagateStructuralGroupNum = function(num){
+        if (num === undefined) num = this._eSimStructuralGroup;
+        lattice.propagateToNeighbors(this.getAbsoluteIndex(), function(neighbor){
+            if (neighbor) neighbor.setStructuralGroupNum(num);
+        });
+    };
+
+    DMACell.prototype.getStructuralGroupNum = function(){
+        return this._eSimStructuralGroup;
+    };
+
+    DMACell.prototype.structuralGroupVisible = function(groupNum){
+        return groupNum == this._eSimStructuralGroup;
+    };
+
 
 });
\ No newline at end of file
diff --git a/js/simulation/electronics/cells/eSimSuperCell.js b/js/simulation/electronics/cells/eSimSuperCell.js
index 83b4e66a..9b2f7894 100644
--- a/js/simulation/electronics/cells/eSimSuperCell.js
+++ b/js/simulation/electronics/cells/eSimSuperCell.js
@@ -17,5 +17,16 @@ define(['underscore', 'superCell'], function(_, DMASuperCell){
         return false;
     };
 
+    DMASuperCell.prototype.structuralGroupVisible = function(groupNum){
+        for (var i=0;i<this.cells.length;i++){
+            for (var j=0;j<this.cells[0].length;j++){
+                for (var k=0;k<this.cells[0][0].length;k++){
+                    if (this.cells[i][j][k] && this.cells[i][j][k].structuralGroupVisible(groupNum)) return true;
+                }
+            }
+        }
+        return false;
+    };
+
     return DMASuperCell;
 });
\ No newline at end of file
diff --git a/js/simulation/electronics/eSim.js b/js/simulation/electronics/eSim.js
index 56508a8b..fc3ba55d 100644
--- a/js/simulation/electronics/eSim.js
+++ b/js/simulation/electronics/eSim.js
@@ -8,8 +8,15 @@ define(['underscore', 'backbone', 'threeModel', 'appState'], function(_, Backbon
     var eSim = Backbone.Model.extend({
 
         defaults:{
+
+            //electrical connections
             conductorGroups: null,//{{current:xx, voltage:xx}, ...}
             visibleConductorGroup: -1,//-2 = show everything, -1 = show all conductors
+
+            //structural connections
+            structuralGroups: null,
+            visibleStructuralGroup: -1,//-1 = show everything
+
             globalInductance: null,
             voltageUnits: "1",
             currentUnits: "0.001",
-- 
GitLab