From d84721a44c70a93af81fae58a6681194a895a5f3 Mon Sep 17 00:00:00 2001
From: Amira Abdel-Rahman <amira-rahman@aucegypt.edu>
Date: Wed, 8 Apr 2020 11:13:57 -0400
Subject: [PATCH] integrated simulation

---
 01_Code/physical_computing_interface/app.js   | 242 +++++++++++++-----
 .../css/style.css                             |   4 +-
 .../indexSimulation.html                      |  49 ++--
 .../physical_computing_interface/metavoxels   |   2 +-
 4 files changed, 215 insertions(+), 82 deletions(-)

diff --git a/01_Code/physical_computing_interface/app.js b/01_Code/physical_computing_interface/app.js
index f40627e..69cacb3 100644
--- a/01_Code/physical_computing_interface/app.js
+++ b/01_Code/physical_computing_interface/app.js
@@ -19,69 +19,197 @@ initEditor();// todo enclose into class
 // assembler.run();
 
 
-/////////////simulation////////////////////
+/////////////assmebly////////////////////
 var assembler= new Assembler(three,GLOBALS,1,50,[new THREE.Vector3(0,0,0)],[new THREE.Vector3(GLOBALS.gridSize/2.0*GLOBALS.voxelSpacing,0,0)]);
 assembler.run();
 
 
-// old simulation stuff
-// var setup;
-// $.getJSON("./simulation/json/setup.json", function(json) {
-//     setup=json.setup;
-//     setup.viz.colorMaps=[coolwarm,YlGnBu, winter ,jet];
-    
-    
-// });
-
-// var setup={//empty
-// 	nodes: [
-// 		],
-// 	edges: [
-// 		],
-
-// 	//material properties - AISI 1095 Carbon Steel (Spring Steel)
-// 	ndofs   : 3*6,
-
-// 	animation :  {
-	
-// 		showDisplacement : true,
-// 		exageration : 2000,
-// 		speed:3.0
-		
-// 	},
-// 	viz :  {
-	
-		
-// 		minStress:-500,
-// 		maxStress: 500,
-// 		colorMaps:[coolwarm,YlGnBu, winter ,jet],
-// 		colorMap:0,
-		
-// 	},
-// 	solve: solveFea,
-// 	bar:false
-	
-// };
-// var node=true;
-// // init();
-// // initRhino3dm(1,10,createVoxelnotRhino,true,true);
-// // play ();
-
-// // drawStructure();
-// // colorEdges();
-// // animate();
-// var size=5;
+/////////////simulation////////////////////
+///new simulation stuff 
+var sim; //todo change location
+var setup=JSON.parse(JSON.stringify(setupEmpty));
+
+// var ex=gui.add(globalSetup, 'exageration', 0, 10e4).listen();
+// var sp=gui.add(globalSetup, 'speed', 0, 5).listen();
+// var sp=gui.add(globalSetup, 'updateStress').listen();
+
+var static=true;
+
+var latticeSize=10;
+var voxelSize=5;
+var globalSetup={
+    exageration:10e10,
+    speed:3.0,
+    updateStress:false
+
+};
+var material,supports,loads;
+
+
+setup.voxelSize=voxelSize;
+setup.gridSize=GLOBALS.gridSize
 
 document.addEventListener('runNode', function (e) { 
-    // var tgt=cy.$id(e.detail.id);
-    // runNode(tgt); //local run node //TODO CHANGE ADD CLOSURE
-    // console.log(GLOBALS.occupancy);
-    // console.log(GLOBALS.timeline[0]);
-    // simulate(size,GLOBALS.timeline,createVoxelnotRhino );
-    // init();
-    // drawStructure();
-    // colorEdges();
-    // animate();
+    solveParallel(10);
+
+}, false);
 
+document.addEventListener('addNode', function (e) { 
+    resetMetavoxels(e);
 }, false);
 
+document.addEventListener('removeNode', function (e) { 
+    resetMetavoxels(e);
+}, false);
+
+function resetMetavoxels(e){
+    setup=sim.reset();
+    //get present voxels
+    var present=[];
+    console.log(GLOBALS.timeline)
+    GLOBALS.timeline.forEach(step => {
+        
+        
+        if(step[0].addRemove){
+            present.push(step[0])
+        } else {
+            
+            for (var i=present.length-1; i>=0; i--) {
+                var e= present[i];
+                if ( (e.addRemove && e.x==step[0].x&&e.z==step[0].z &&e.y==step[0].y)) {
+                    present.splice(i, 1);
+                    break;       //<-- Uncomment  if only the first term has to be removed
+                }
+            }
+
+        }
+
+    });
+
+    present.forEach(step => {
+        var latticeCoordinates=new THREE.Vector3(step.x,step.z,step.y);
+        var pos= latticeCoordinates.clone().multiplyScalar(voxelSize)
+        createVoxel(setup,voxelSize,pos,material.area,material.density,material.stiffness);
+        
+        
+    });
+
+    restrainFromBox(setup,supports);
+    loadFromBox(setup,loads);
+    sim.update(setup);
+
+}
+
+rhino3dm().then(async m => {
+    console.log('Loaded rhino3dm.');
+
+    _rhino3dm = m; // global
+
+
+    material={
+		area:1.0,
+		density:0.028,
+		stiffness:10000000
+    };
+    
+    var material2={
+		area:1.0,
+		density:0.028,
+		stiffness:1000000
+	};
+
+    setup.hierarical=false;
+    setup.voxelSize=voxelSize;
+
+    const position=new THREE.Vector3(0,0,0);
+    const position2=new THREE.Vector3(voxelSize*2,0,0);
+
+    ///
+    //try bounding box conditions
+    // var support=new _rhino3dm.BoundingBox([-voxelSize/2+position.x,-voxelSize/2+position.y,-voxelSize/2+position.z], [voxelSize/2+position.x,voxelSize/2+position.y,voxelSize/2+position.z]);
+    var support=new _rhino3dm.BoundingBox([-voxelSize/2,-voxelSize/2,-voxelSize/2], [voxelSize/2+voxelSize*GLOBALS.gridSize,-voxelSize/4,voxelSize/2+voxelSize*GLOBALS.gridSize]);
+
+    // var temp=voxelSize*latticeSize-voxelSize;
+    
+    // var load=new _rhino3dm.BoundingBox([-voxelSize/2+position.x,-voxelSize/2+position.y,0-voxelSize/2+position.z], [voxelSize/2+position.x,voxelSize/2+position.y,temp+voxelSize/2+position.z]);
+    var load=new _rhino3dm.BoundingBox([-voxelSize/2,-voxelSize/2,-voxelSize/2], [voxelSize/2+voxelSize*GLOBALS.gridSize,voxelSize/2+voxelSize*GLOBALS.gridSize,voxelSize/2+voxelSize*GLOBALS.gridSize]);
+
+
+
+    // var temp1=voxelSize*latticeSize-3*voxelSize;
+
+    // var matB=new _rhino3dm.BoundingBox([-voxelSize/2+position2.x,-voxelSize/2+position2.y,temp1-voxelSize/2+position2.z], [voxelSize/2+position2.x,voxelSize/2+position2.y,temp]);
+
+    var dof=[true,true,true,true,true,true];
+    supports=[[support,dof]];
+    loads=[[load,{x:0,y:-100,z:0}]];
+    // var diffMaterialBox=[[matB,material2]];
+
+    ///
+    
+
+    // lateralLoadSetup(setup,position,latticeSize,voxelSize,createVoxel,supports,loads,material);
+    // changeMaterialFromBox(setup,diffMaterialBox);
+
+    // var latticeCoordinates=new THREE.Vector3(0,0,0);
+    // var latticeCoordinates1=new THREE.Vector3(1,0,0);
+    // var pos= latticeCoordinates.clone().multiplyScalar(voxelSize).add(position);
+    // var pos1= latticeCoordinates1.clone().multiplyScalar(voxelSize).add(position);
+    // createVoxel(setup,voxelSize,pos,material.area,material.density,material.stiffness);
+    // createVoxel(setup,voxelSize,pos1,material.area,material.density,material.stiffness);
+    // restrainFromBox(setup,supports);
+
+    setup.viz.colorMaps=[YlGnBu,coolwarm, winter ,jet];
+    setup.viz.minStress=10e6;
+    setup.viz.maxStress=-10e6;
+
+    setup.viz.exageration=globalSetup.exageration;
+    setup.solve=solveParallel;
+    setup.numTimeSteps=100;
+    // setup.supports=supports;
+    // setup.loads=loads;
+
+   
+    sim= new threejs1(setup,"webgl1","graph",static);
+    sim.init();
+    // three.drawConstraintBoundingBoxes([support],[load]);
+   
+
+    
+    // gui.add(three.setup,"numTimeSteps", 0, 500).listen();
+    // gui.add(three.setup,"solve");
+    
+    
+    
+    // document.getElementById("footer2").innerHTML = "Press solve for structure simulation.";
+
+    // more stuff
+    
+    
+});
+
+function solveParallel(numTimeSteps){
+    // document.getElementById("footer2").innerHTML = "Running...";
+    
+    /////////////////////////////////////////////////////
+    
+
+    var dt=0.0251646; //?? todo change to recommended step
+    var dt=0.01; //?? todo change to recommended step
+    
+
+    setTimeout(function() { 
+        simulateParallel( setup,numTimeSteps,dt,static,2); 
+    }, 1);
+
+    // var dt=0.0251646; //?? todo change to recommended step
+    // simulateParallel(setup1,numTimeSteps,dt);
+
+    ///////////////////////////////////////
+    // updateColors();
+    // sim.animate();
+    // three1.animate();
+    
+    /////////////////////
+
+}
diff --git a/01_Code/physical_computing_interface/css/style.css b/01_Code/physical_computing_interface/css/style.css
index 9a35ca3..70ec477 100644
--- a/01_Code/physical_computing_interface/css/style.css
+++ b/01_Code/physical_computing_interface/css/style.css
@@ -96,11 +96,11 @@ body,html{width:100%;height:100%;padding:0;margin:0;}
 }
 
 #webgl1{
-    top:100;
+    top:var(--top);
     bottom: var(--bottom);
     overflow-y: hidden;
     font-family: var(--font);
-    color: var(--color2);
+    color: var(--color1);
 }
 
 #graph{
diff --git a/01_Code/physical_computing_interface/indexSimulation.html b/01_Code/physical_computing_interface/indexSimulation.html
index 5bb684b..e01e67d 100644
--- a/01_Code/physical_computing_interface/indexSimulation.html
+++ b/01_Code/physical_computing_interface/indexSimulation.html
@@ -34,7 +34,7 @@
 
     <div id="simulation">
         <div id="threejs1">
-            <div class="header1">
+            <div class="header2">
                     <i> Simulation </i>
             </div>
             <div class="dragbar2"></div>
@@ -131,36 +131,41 @@
 
 <!-- simulation stuff -->
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
-<!-- <script type="text/javascript" src="https://files.mcneel.com/rhino3dm/js/latest/rhino3dm.js"></script> -->
-<script src="./rhino3dm.js"></script>
-<!-- <script async type="text/javascript" src="https://files.mcneel.com/rhino3dm/js/latest/rhino3dm.js"></script>
-<script async type="text/javascript" src="https://files.mcneel.com/rhino3dm/js/latest/compute.rhino3d.js"></script> -->
+
+<script type="text/javascript" src="https://files.mcneel.com/rhino3dm/js/latest/rhino3dm.js"></script>
+<script type="text/javascript" src="./metavoxels/01_Code/191115_NodeJsJulia/demos/rhino3dm.js"></script>
+
 
 <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@1.0.0/dist/tf.min.js"></script>
 
 <!-- simulation -->
-<!-- <script src="./simulation/lib/stats.js"></script>
-<script src="./simulation/lib/SVGRenderer.js"></script>
-<script src="./simulation/lib/js-colormaps.js"></script>
-<script src="./simulation/lib/LineSegments2.js"></script>
-<script src="./simulation/lib/LineSegmentsGeometry.js"></script>
-<script src="./simulation/lib/Line2.js"></script>
-<script src="./simulation/lib/LineMaterial.js"></script>
-<script src="./simulation/lib/LineGeometry.js"></script>
-<script src="./simulation/lib/GeometryUtils.js"></script>
-
-<script src="./simulation/fea/beamFea.js"></script> 
-<script src="./simulation/visualization/geometry.js"></script> 
-
-<script src="./simulation/visualization/utils.js"></script> 
-<script src="./simulation/visualization/main.js"></script>  -->
+<script src="./metavoxels/01_Code/191115_NodeJsJulia/lib/stats.js"></script>
+<script src="./metavoxels/01_Code/191115_NodeJsJulia/lib/SVGRenderer.js"></script>
+<script src="./metavoxels/01_Code/191115_NodeJsJulia/lib/js-colormaps.js"></script>
+<script src="./metavoxels/01_Code/191115_NodeJsJulia/lib/LineSegments2.js"></script>
+<script src="./metavoxels/01_Code/191115_NodeJsJulia/lib/LineSegmentsGeometry.js"></script>
+<script src="./metavoxels/01_Code/191115_NodeJsJulia/lib/Line2.js"></script>
+<script src="./metavoxels/01_Code/191115_NodeJsJulia/lib/LineMaterial.js"></script>
+<script src="./metavoxels/01_Code/191115_NodeJsJulia/lib/LineGeometry.js"></script>
+<script src="./metavoxels/01_Code/191115_NodeJsJulia/lib/GeometryUtils.js"></script>
+
+
+<!-- <script src="./metavoxels/01_Code/191115_NodeJsJulia/fea/beamFea.js"></script>  -->
+<!-- <script src="./simulation/visualization/geometry.js"></script>  -->
+
+<script src="./metavoxels/01_Code/191115_NodeJsJulia/visualization/utils.js"></script> 
+<script src="./metavoxels/01_Code/191115_NodeJsJulia/visualization/geometry.js"></script> 
+
+<script src="./metavoxels/01_Code/191115_NodeJsJulia/visualization/main.js"></script> 
+
+<script src="./metavoxels/01_Code/191115_NodeJsJulia/fea/parallelFea.js"></script> 
 
 <!-- assembler control -->
-<script src="./assembly/assemblerControl/utils.js" type="text/javascript"></script>
+<!-- <script src="./assembly/assemblerControl/utils.js" type="text/javascript"></script>
 <script src="./assembly/assemblerControl/setup.js" type="text/javascript"></script>
 <script src="./assembly/assemblerControl/processing.js" type="text/javascript"></script>
 <script src="./assembly/assemblerControl/serialMonitor.js" type="text/javascript"></script>
-<script type="text/javascript" src="./assembly/assemblerControl/bootstrap-multiselect.js"></script>
+<script type="text/javascript" src="./assembly/assemblerControl/bootstrap-multiselect.js"></script> -->
 
 
 <!-- code -->
diff --git a/01_Code/physical_computing_interface/metavoxels b/01_Code/physical_computing_interface/metavoxels
index 5d349cc..b203337 160000
--- a/01_Code/physical_computing_interface/metavoxels
+++ b/01_Code/physical_computing_interface/metavoxels
@@ -1 +1 @@
-Subproject commit 5d349cccfa3df367a5f84228dd07044d4eece9b0
+Subproject commit b2033373c07ec89f2d77ba88702f244c2ca4966b
-- 
GitLab