In [1]:
import numpy as np
import Triangulation as tr
import HierarchicalSmooth as hs
import Base as base
from scipy.sparse import csc_matrix
import time

In [31]:
import matplotlib.pyplot as plt
# %matplotlib inline

from matplotlib import rc
rc( 'text', usetex=True )

In [2]:
import plotly as py
from plotly.tools import FigureFactory as FF
from plotly.graph_objs import graph_objs

py.offline.init_notebook_mode()

from scipy.spatial import Delaunay

import Base as base



In [3]:
filePrefix = '/home/siddharth/CMU/intro_to_research/intro/DREAM3D/DREAM3D-6.1.3-Linux-x86_64/Data/Mesh'

P = np.loadtxt( filePrefix + '/SharedVertexList.txt' ).T
tri = np.loadtxt( filePrefix + '/SharedTriList.txt', dtype=int )
nFaceLabels = np.loadtxt( filePrefix + '/FaceLabels.txt', dtype=int )
nType = np.loadtxt( filePrefix + '/NodeType.txt', dtype=int )

In [4]:
BoundaryDict = {}  # hashing surface elements takes a few minutes; better now than later!
for i in range( len ( nFaceLabels ) ):
    if nFaceLabels[i,0] > nFaceLabels[i,1]:
        nFaceLabels[i,0], nFaceLabels[i,1] = nFaceLabels[i,1], nFaceLabels[i,0]
        tri[i,0], tri[i,1] = tri[i,1], tri[i,0]
    try:
        BoundaryDict[ str( nFaceLabels[i] ) ].append( i )
    except KeyError:
        BoundaryDict[ str( nFaceLabels[i] ) ] = [ i ] # start a new list

nUniqFaces = np.vstack( { tuple( row ) for row in nFaceLabels} )

In [5]:
print 'Number of grain boundaries = %d' % len( BoundaryDict )
print 'Number of unique grains = %d' % np.unique( nUniqFaces ).size


Number of grain boundaries = 4728
Number of unique grains = 795

In [ ]:
nGB = 1

# triGB = tri[ np.where( base.ismember( nFaceLabels, nUniqFaces[nGB].reshape( 1, -1 ), 'rows' )[0] )[0], : ]
triGB = tri[ BoundaryDict[ str( nUniqFaces[nGB] ) ], : ]
pointGB = np.unique( triGB )
triGB = np.array( base.ismember( triGB, pointGB )[1] ).reshape( -1, 3 )
xGB = P[ :, pointGB ]
typeGB = nType[ pointGB ]

t0 = time.time()
FB, FBsec = hs.DifferentiateFaces( tr.Triangulation( triGB, xGB ) )
print 'Differentiating faces %s seconds' % str( time.time() - t0 )

In [14]:
%autoreload
nGrain = np.array( [ 182, 146, 23 ] )
nGrainPatches = np.any( base.ismember( nFaceLabels, nGrain )[0], axis=1 )
nGrainFaces = nFaceLabels[ nGrainPatches ]
triGrain = tri[ nGrainPatches, : ]
pointGrain = np.unique( triGrain )
triGrain = np.array( base.ismember( triGrain, pointGrain )[1] ).reshape( -1, 3 )
PGrain = P[ :, pointGrain ]
nGrainType = nType[ pointGrain ]

print 'Patches = %d' % len( triGrain )
print 'nodes = %d' % PGrain.shape[1]
# nFaceLabels[ np.any( nFaceLabels==nGrain, axis=1 ) ]


Patches = 3760
nodes = 1884

In [ ]:
fig1 = FF.create_trisurf(x=PGrain[0], y=PGrain[1], z=PGrain[2],
                         simplices=triGrain,
                         title="System of %d grains" % nGrain.size, 
                         aspectratio=dict( 
                            x = np.ptp( PGrain[0] ), 
                            y = np.ptp( PGrain[1] ), 
                            z = np.ptp( PGrain[2] )
                        )
                        )
py.offline.iplot(fig1 )

In [12]:
%autoreload
xS, bPS = hs.HierarchicalSmooth( PGrain, triGrain, nGrainFaces, nGrainType )


Hashing interface elements (this may take some time)... 1.3663380146 seconds. 
Interface [146 257]: 1 of 39...
HierarchicalSmooth.Smooth warning: Max. number of iterations reached.
HierarchicalSmooth.Smooth warning: Max. number of iterations reached.
 44.6793279648 seconds. 
Interface [ 23 748]: 2 of 39... 1.12443709373 seconds. 
Interface [122 182]: 3 of 39... 0.377675056458 seconds. 
Interface [135 182]: 4 of 39... 1.14991497993 seconds. 
Interface [182 422]: 5 of 39... 0.940422058105 seconds. 
Interface [169 182]: 6 of 39...
HierarchicalSmooth.Smooth warning: Max. number of iterations reached.
 22.5697309971 seconds. 
Interface [146 398]: 7 of 39... 0.725904941559 seconds. 
Interface [146 317]: 8 of 39... 0.959426879883 seconds. 
Interface [146 657]: 9 of 39... 1.22398996353 seconds. 
Interface [ 23 301]: 10 of 39... 0.966820001602 seconds. 
Interface [ 23 231]: 11 of 39... 0.458081960678 seconds. 
Interface [182 639]: 12 of 39... 1.22573685646 seconds. 
Interface [ -1 146]: 13 of 39...
HierarchicalSmooth.Smooth warning: Max. number of iterations reached.
 24.0119428635 seconds. 
Interface [146 579]: 14 of 39... 0.814415931702 seconds. 
Interface [ 11 182]: 15 of 39... 0.527235031128 seconds. 
Interface [146 259]: 16 of 39... 0.237420797348 seconds. 
Interface [146 715]: 17 of 39... 1.14065003395 seconds. 
Interface [146 697]: 18 of 39... 1.12144899368 seconds. 
Interface [182 311]: 19 of 39... 0.917966127396 seconds. 
Interface [182 368]: 20 of 39... 0.650207996368 seconds. 
Interface [146 352]: 21 of 39... 0.952342987061 seconds. 
Interface [182 541]: 22 of 39... 0.859292984009 seconds. 
Interface [  1 182]: 23 of 39... 0.357870101929 seconds. 
Interface [ 23 353]: 24 of 39... 0.898932933807 seconds. 
Interface [142 146]: 25 of 39... 0.116030931473 seconds. 
Interface [ 79 182]: 26 of 39... 1.28814411163 seconds. 
Interface [182 565]: 27 of 39... 1.10059690475 seconds. 
Interface [146 536]: 28 of 39... 0.689841985703 seconds. 
Interface [146 391]: 29 of 39... 0.537807941437 seconds. 
Interface [146 486]: 30 of 39... 0.801457881927 seconds. 
Interface [ 23 759]: 31 of 39... 0.0119819641113 seconds. 
Interface [182 339]: 32 of 39... 0.957401990891 seconds. 
Interface [146 184]: 33 of 39... 0.0147759914398 seconds. 
Interface [182 667]: 34 of 39... 2.30075097084 seconds. 
Interface [ 71 182]: 35 of 39... 0.576992034912 seconds. 
Interface [-1 23]: 36 of 39... 0.59910607338 seconds. 
Interface [146 614]: 37 of 39... 0.72262096405 seconds. 
Interface [146 574]: 38 of 39... 0.490240097046 seconds. 
Interface [ -1 182]: 39 of 39... 0.623727083206 seconds. 

In [13]:
np.savetxt( filePrefix + '/SmoothVertexList.txt', xS.T, fmt='%f' )
np.savetxt( filePrefix + '/SmoothTriList.txt', triGrain, fmt='%d')
np.savetxt( filePrefix + '/BoolSmoothed.txt', bPS, fmt='%d' )
np.savetxt( filePrefix + '/TypeSmoothed.txt', nGrainType, fmt='%d' )

In [6]:
import HierarchicalSmooth2 as hs2

In [7]:
%autoreload
triBad = tri[ BoundaryDict[ '[126 716]' ], : ]
pBad = np.unique( triBad )
triBad = np.array( base.ismember( triBad, pBad )[1] ).reshape( -1, 3 )
pointBad = P[ :, pBad ]
typeBad = nType[ pBad ]

T = tr.Triangulation( triBad, pointBad )
FB, FBsec = hs.DifferentiateFaces( T )
thisFB = FBsec[0]
thisRange = FB[ thisFB[0]:thisFB[1]+1, 0 ]

# pointPerimeter = pointBad[ :, thisRange ]
# fixPerimeter = np.where( ( typeBad[ thisRange ] )%10==4 )[0]

# xS = hs.Smooth( pointPerimeter.T, 1.e-4, 2000, 
#                nFixed=fixPerimeter
#               )[0]
# np.savetxt( filePrefix + '/Perimeter.txt', pointPerimeter, fmt='%f' )
# np.savetxt( filePrefix + '/Fixed.txt', fixPerimeter, fmt='%d' )
# np.savetxt( filePrefix + '/PSmooth.txt', xS.T, fmt='%f' )

xS, PS = hs.HierarchicalSmooth( 
    xPoints=pointBad, 
    tri=triBad, 
    nFaceLabels=np.array( [ 182, 667 ] ).reshape( 1, -1 ).repeat( len( triBad ), axis=0 ),
    nNodeType=typeBad, 
    fThreshold=1.e-5  
)

np.savetxt( filePrefix + '/BadVertex.txt', pointBad.T, fmt='%f' )
np.savetxt( filePrefix + '/BadTri.txt', triBad, fmt='%d' )
np.savetxt( filePrefix + '/BadType.txt', typeBad, fmt='%d' )
np.savetxt( filePrefix + '/BadSmooth.txt', xS.T, fmt='%f' )
np.savetxt( filePrefix + '/BadBool.txt', PS, fmt='%d' )

# print triBad.shape
# print pBad.shape
# # print nType[ pBad ]

# T = tr.Triangulation( triBad, pointBad )
# print hs.DifferentiateFaces( T )


Hashing interface elements (this may take some time)... 0.323470115662 seconds. 
Interface [182 667]: 1 of 1... 5.85118603706 seconds. 

In [16]:
fig1 = FF.create_trisurf(x=pointBad[0], y=pointBad[1], z=pointBad[2],
                         simplices=triBad,
                         title="Exotic topology (disjoint 'amphitheater' structure)", 
                         aspectratio=dict( 
                            x = np.ptp( pointBad[0] ), 
                            y = np.ptp( pointBad[1] ), 
                            z = np.ptp( pointBad[2] )
                        )
                        )
py.offline.iplot(fig1 )



In [87]:
tr = reload( tr )
T = tr.Triangulation( triBad, pointBad )
print hs.DifferentiateFaces( T )


(array([[  0,   9],
       [  9,  13],
       [ 13,  19],
       [ 19,  28],
       [ 28,  57],
       [ 57,  56],
       [ 56,  80],
       [ 80,  79],
       [ 79,  82],
       [ 82,  81],
       [ 81, 124],
       [124, 125],
       [125, 126],
       [126, 127],
       [127, 128],
       [128,  95],
       [ 95,  94],
       [ 94,  96],
       [ 96,  97],
       [ 97,  93],
       [ 93,  89],
       [ 89,  85],
       [ 85,  62],
       [ 62,  63],
       [ 63,  59],
       [ 59,  30],
       [ 30,  21],
       [ 21,  22],
       [ 22,  23],
       [ 23,  24],
       [ 24,  26],
       [ 26,  27],
       [ 27,  25],
       [ 25,  24],
       [ 24,  18],
       [ 18,   8],
       [  8,   7],
       [  7,   6],
       [  6,   3],
       [  3,   2],
       [  2,   1],
       [  1,   0],
       [ 31,  36],
       [ 36,  43],
       [ 43,  50],
       [ 50,  68],
       [ 68,  75],
       [ 75, 104],
       [104, 103],
       [103, 112],
       [112, 118],
       [118, 138],
       [138, 139],
       [139, 159],
       [159, 169],
       [169, 168],
       [168, 167],
       [167, 166],
       [166, 213],
       [213, 212],
       [212, 211],
       [211, 210],
       [210, 204],
       [204, 203],
       [203, 202],
       [202, 268],
       [268, 321],
       [321, 329],
       [329, 376],
       [376, 377],
       [377, 381],
       [381, 426],
       [426, 450],
       [450, 451],
       [451, 452],
       [452, 453],
       [453, 454],
       [454, 455],
       [455, 456],
       [456, 464],
       [464, 465],
       [465, 466],
       [466, 467],
       [467, 468],
       [468, 461],
       [461, 463],
       [463, 460],
       [460, 437],
       [437, 438],
       [438, 439],
       [439, 440],
       [440, 443],
       [443, 444],
       [444, 447],
       [447, 448],
       [448, 449],
       [449, 446],
       [446, 422],
       [422, 424],
       [424, 425],
       [425, 416],
       [416, 417],
       [417, 418],
       [418, 419],
       [419, 402],
       [402, 403],
       [403, 404],
       [404, 367],
       [367, 366],
       [366, 352],
       [352, 338],
       [338, 339],
       [339, 340],
       [340, 328],
       [328, 320],
       [320, 319],
       [319, 316],
       [316, 315],
       [315, 314],
       [314, 256],
       [256, 255],
       [255, 252],
       [252, 251],
       [251, 249],
       [249, 248],
       [248, 183],
       [183, 182],
       [182, 181],
       [181, 148],
       [148, 150],
       [150, 149],
       [149, 129],
       [129, 130],
       [130, 102],
       [102, 101],
       [101,  74],
       [ 74,  66],
       [ 66,  49],
       [ 49,  42],
       [ 42,  41],
       [ 41,  40],
       [ 40,  35],
       [ 35,  34],
       [ 34,  33],
       [ 33,  32],
       [ 32,  31]]), [[0, 41], [42, 145]])

In [80]:
d1 = {}
d2 = {}
d3 = {}

d1[ '2.' ] = 1
d2[ 1 ] = '2.'
d3[ 1 ] = [ 1, 2, 3 ]
d3[ 2 ] = [ 28, 29, 23 ]

print d1
print d2

print d1.has_key( '2.' )
print not( d1.has_key( '2.' ) )


{'2.': 1}
{1: '2.'}
True
False

In [75]:
print d3[1]
d3[1].pop()
print d3[1]


[]
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-75-85ce1492f359> in <module>()
      1 print d3[1]
----> 2 d3[1].pop()
      3 print d3[1]

IndexError: pop from empty list

In [46]:
lst = [ 2, 3, 4, 5 ]
print lst
print lst.pop()
print lst


[2, 3, 4, 5]
5
[2, 3, 4]

In [47]:
np.array( 2, 3 )


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-47-fdf42fad01bf> in <module>()
----> 1 np.array( 2, 3 )

TypeError: data type not understood

In [48]:
len( [] )


Out[48]:
0

In [36]:
x = np.linspace( 0., 1.e-2, 500 )
y = np.exp( x )
y2 = np.exp( x / 0.1 )
y3 = np.exp( x / 0.01 )

plt.semilogy( x, y,  '-', label='a=1' )
plt.semilogy( x, y2, '-', label='a=2' )
plt.semilogy( x, y3, '-', label='a=3' )
plt.legend( loc='best' )


Out[36]:
<matplotlib.legend.Legend at 0x7f74c0808a50>