def testSquare(): ptlist = { "vertices": np.array( ((0.0, 0.0), (0.5, 0.0), (1.0, 0.0), (0.0, 0.5), (0.5, 0.5), (1.0, 0.5), (0.0, 1.0), (0.5, 1.0), (1.0, 1.0)) ) } t = triangle.triangulate(ptlist) t1 = triangle.triangulate(ptlist, "qa0.001") triangle.plot.compare(plt, t, t1) # plt.show() L, M = FE.assembleMatrices(t) # print L # print '\n\n' # print M np.savetxt("textL", L) np.savetxt("textM", M) eig = FE.eigenvalues(L, M) elist = eig[0] efunc = eig[1] print elist[0] print elist[1] print elist[2] # vertices = np.asarray(t['vertices']) # faces = np.asarray(t['triangles']) # x = vertices[:,0] # y = vertices[:,1] # z = efunc[1] # plt.figure() # plt.tricontourf(x,y,faces,z,cmap='afmhot') # plt.show() print "****************************" L, M = FE.assembleMatrices(t1) eig = FE.eigenvalues(L, M) elist = eig[0] efunc = eig[1] for j in range(10): print elist[j] vertices = np.asarray(t1["vertices"]) faces = np.asarray(t1["triangles"]) x = vertices[:, 0] y = vertices[:, 1] z = efunc[:, 5] plt.figure() plt.tricontourf(x, y, z, 100, cmap="afmhot") plt.show() print "***************************\n\n\n\n\n"
def eigenvalueSurvey(n): fn_list = [] ds = 0.0005 domain_list = [] for x in np.arange(0.45,0.55,ds): max_height = min( 1.0 - x**2, 1.0 - (x-1.0)**2 ) for y in np.arange(np.sqrt(3.)/2. - 0.05, np.sqrt(3.)/2. + 0.05, ds): domain_list.append( [x,y] ) for p in tqdm(domain_list): x = p[0] y = p[1] area = y/2.0 # define triangle T = triangleMesh(x,y,0.001) # get eigenvalues L,M = FE.assembleMatrices(T) eigvals = FE.sparseEigs(L,M,n)[0] for j in range(len(eigvals)): eigvals[j] = eigvals[j]*area current_tuple = [x,y]+list(eigvals) fn_list.append(current_tuple) f = open('eig.dat', 'w+') f.truncate() for j in range(len(fn_list)): s = '' for k in range(n+2): s = s + str(fn_list[j][k]) + ' ' s = s + '\n' f.write( s ) f.close()
def testFE(): ptlist = {"vertices": np.array(((0, 0), (1, 0), (1, 1), (0, 1)))} triang = triangle.triangulate(ptlist) print triang print "\n\n" print triang["vertices"] print triang["triangles"] triangle.plot.compare(plt, ptlist, triang) plt.show() print "\n\nNow testing the FE assembly ..." L, M = FE.assembleMatrices(triang) elist = FE.eigenvalues(L, M)[0] elist.sort() print "eigenvalues:" for j in elist: print j triangle.plot.compare(plt, ptlist, triang) plt.show() triang = triangle.triangulate(triang, "rqa0.1") L, M = FE.assembleMatrices(triang) elist = FE.eigenvalues(L, M)[0] elist.sort() print "\n\neigenvalues:" for j in elist: print j triangle.plot.compare(plt, ptlist, triang) plt.show() triang = triangle.triangulate(triang, "rqa0.01") L, M = FE.assembleMatrices(triang) elist = FE.eigenvalues(L, M)[0] elist.sort() print "\n\neigenvalues:" for j in elist: print j triangle.plot.compare(plt, ptlist, triang) plt.show()
def testSubdivide(): g = Mesh() g.addVertex(0, 0) g.addVertex(1, 0) g.addVertex(0, 1) g.addFace(0, 1, 2) L, M = FE.assembleMatrices(g) # print L # print M elist = FE.eigenvalues(L, M) print "\n\n\n" # print elist g.plot() g.subdivide(0) L, M = FE.assembleMatrices(g) # print '\n\n' # print L # print M # print '\n\n' elist = FE.eigenvalues(L, M) # print '\n\n\n' # print elist # g.plot() # g.subdivide(0) # g.plot() # g.subdivide(0) # g.plot() # for k in range(10): # j = np.random.choice( range(len(g.face_list)) ) # g.subdivide(j) g.subdivide_all() g.plot() g.subdivide_all() g.subdivide_all() g.plot() L, M = FE.assembleMatrices(g) elist = FE.eigenvalues(L, M)[0] elist.sort() print elist
import FE import triangle import numpy as np import matplotlib.pyplot as plt from pylab import * tri = {'vertices':np.array([[0.,0.],[1.,0.],[0.7,0.05]]),'triangles':np.array([[0,1,2]])} tri_mesh = triangle.triangulate(tri,'qa0.000005') print str(len(tri_mesh['vertices'])) + ' nodes' L,M = FE.assembleMatrices(tri_mesh) eig = FE.eigenvalues(L,M) elist = eig[0] efunc = eig[1] for j in range(10): print elist[j] fig = plt.figure() vertices = np.asarray(tri_mesh['vertices']) faces = np.asarray(tri_mesh['triangles']) x = vertices[:,0] y = vertices[:,1] for j in range(5): z = efunc[:,j] plt.tricontourf(x,y,z,100,cmap='afmhot') axes().set_aspect('equal') plt.show()
def findTriangleEigs(x,y,h,n): mesh = assembleTriangle(x,y,h) evals,evecs = FE.findEigs(mesh,n=2*n) return evals[n], evecs[:,n], mesh
def findTriangleEig(x,y,h): mesh = assembleTriangle(x,y,h) evals,evecs = FE.findEigs(mesh,n=2) return evals[1], evecs[:,1], mesh
def __init__(self,root,canvas_width,canvas_height): self.root=root self.canvas_width = canvas_width self.canvas_height = canvas_height self.nodes = False self.eiglevel = 1 self.current_refine = 1 # set up the domain self.dom = {'vertices':np.array([[0.,0.],[1.,0.7],[0.5,0.5]])} self.mesh = triangle.triangulate(self.dom,'qa0.001') x = self.mesh['vertices'][:,0] y = self.mesh['vertices'][:,1] self.eigvals,self.eigvecs = FE.findEigs(self.mesh,10) z = self.eigvecs[:,self.eiglevel] # set up the matplotlib nonsense self.fig, self.ax = mpl.pyplot.subplots() self.ax.clear() self.ax.autoscale(enable=False) self.ax.axis('off') triang = tri.Triangulation(x,y) self.ax.tricontourf(triang,z,100,cmap=plt.get_cmap('copper')) self.canvas = FigureCanvasTkAgg(self.fig,master=root) self.canvas.show() self.canvas.get_tk_widget().pack() #self.canvas.mpl_connect("button_press_event", self.setVertex) self.canvas.mpl_connect("key_press_event", self.keyEvent) print 'connected key press' self.ax.set_xlim([0,1]) self.ax.set_ylim([0,1]) self.ax.autoscale(enable=False) self.fig.tight_layout() self.redraw() # build and pack other controls self.control_frame = Frame(master=root,width=canvas_width/4,height=canvas_height) self.control_frame.pack(side=TOP) self.b = Button(self.control_frame, text="QUIT", command = self.quit) self.b.pack(side=LEFT) self.c = Button(self.control_frame, text="Toggle Nodal", command=self.toggle) self.c.pack(side=LEFT) self.s = Button(self.control_frame, text="Energy Up", command=self.energy_up) self.s.pack(side=LEFT) self.t = Button(self.control_frame, text="Energy Down", command=self.energy_down) self.t.pack(side=LEFT)
import triangle.plot as plot dom = {'vertices':np.array([[0.,0.],[1.,0.],[1.,.49],[.9,.49],[.9,.1],[.1,.1],[.1,.9],[.9,.9],[.9,.51],[1.,.51],[1.,1.],[0.,1.]]),'triangles':np.array([[0,1,5],[0,5,6],[1,4,5],[1,4,2],[2,3,4],[0,6,11],[6,10,11],[6,7,10],[7,8,9],[7,9,10]])}#,'segments':np.array([[0,1],[1,2],[2,3],[3,4],[4,5],[5,6],[6,7],[7,8],[8,9],[9,10],[10,11]])} print len(dom['vertices']) print dom['triangles'] mesh = triangle.triangulate(dom,'pqa0.05') print mesh ax1 = plt.subplot(111,aspect='equal') plot.plot(ax1, **mesh) plt.show() L,M = FE.assembleMatrices(mesh) evals,efunc = FE.sparseEigs(L,M) fig = plt.figure() vertices = np.asarray(mesh['vertices']) faces = np.asarray(mesh['triangles']) x = vertices[:,0] y = vertices[:,1] #for j in range(1,5): # z = efunc[:,j] # plt.tricontourf(x,y,z,0,cmap='afmhot') # axes().set_aspect('equal') # plt.show() #z = efunc[:,11] #plt.tricontourf(x,y,z,0,cmap='afmhot')
mesh1 = triangle.triangulate(domain1, "pqa0.02r") mesh2 = triangle.triangulate(domain2, "pqa0.02r") ax1 = plt.subplot(121, aspect="equal") ax2 = plt.subplot(122, aspect="equal") plot.plot(ax1, **mesh1) plot.plot(ax2, **mesh2) plt.show() X1 = mesh1["vertices"][:, 0] Y1 = mesh1["vertices"][:, 1] X2 = mesh2["vertices"][:, 0] Y2 = mesh2["vertices"][:, 1] evals1, evecs1 = FE.findEigs(mesh1, 25) evals2, evecs2 = FE.findEigs(mesh2, 25) for j in range(25): x1 = evals1[j] x2 = evals2[j] print x1, x2, np.abs(x2 - x1), np.abs(x2 - x1) / min(x2, x1) k = 17 v1 = evecs1[:, k] v2 = evecs2[:, k] # ax1 = plt.subplot(121,aspect='equal') # ax1.tricontourf(X1,Y1,v1,0,cmap='copper') # ax2 = plt.subplot(122,aspect='equal') # ax2.tricontourf(X2,Y2,v2,0,cmap='copper')
import FE import triangle import matplotlib.pyplot as plt import matplotlib.tri as tri from pylab import * import triangle.plot as plot dom = {'vertices':np.array([[0.,0.],[1.,0.],[0.7,0.1]]),'triangles':np.array([[0,1,2]]),'segments':np.array([[0,1],[1,2],[2,0]])} mesh = triangle.triangulate(dom,'pcqa0.0005') ax1 = plt.subplot(111,aspect='equal') plot.plot(ax1, **mesh) plt.show() L,M = FE.assembleDirichlet(mesh) evals,efunc = FE.sparseEigs(L,M) fig = plt.figure() vertices = np.asarray(mesh['vertices']) faces = np.asarray(mesh['triangles']) x = vertices[:,0] y = vertices[:,1] for j in range(5): z = efunc[:,j] plt.tricontourf(x,y,z,100,cmap='afmhot') axes().set_aspect('equal') plt.show()
date = Tick0.keys() #日期List cnt = 0 #日期统计 day_cnt = [] #每个日期的数据 Tick0200 = pd.DataFrame() #特征数据 ############################################################################### # Step 1 :特征构建 for i in date[-ndays - test_day - shift:-shift]: '''取出每天的数据,并且提取特征''' cnt += 1 print("Now At %s day !" % (cnt)) tempTick = Tick0[i] tempTran = Tran0[i] #1.1 特征工程 -- 进入FE.py Feature_Generate = FE.FeatureEngeerning(tempTick, tempTran) tempTick = Feature_Generate.add_features() #1.2 预测的y值 -- dy/y dy y/y def y(x): global tempTick tempTick['Past_diff' + str(x)] = tempTick[PredictLabel].diff(x) y(2) y(3) y(4) y(5) # tempTick['Future_diff1'] = tempTick['AskP0'].diff(1).shift(-1).apply(Tools_kjy._compa0) tempTick['median_5'] = (tempTick[PredictLabel].rolling( window=5).median()).shift(-2) tempTick['median_5'], _ = Tools_kjy.smooth(tempTick['median_5'])
[6.4,-11], [30,-11], [30,0]]), 'segments':np.array([[0,1],[1,2],[2,3],[3,4],[4,5],[5,6],[6,7],[7,0]])}#, #'segment_markers': np.array([[1],[1],[1],[1],[1],[1],[1],[1]]), #'vertex_markers':np.array([[1],[1],[1],[1],[1],[1],[1],[1]])} mesh = triangle.triangulate(point_list,'pqa0.02') ax1 = plt.subplot(111, aspect='equal') plot.plot(ax1, **mesh) plt.show() print len(mesh['vertices']) evals, evecs = FE.findEigs(mesh,50) X = mesh['vertices'][:,0] Y = mesh['vertices'][:,1] v = evecs[:,45] fig = plt.figure() ax = fig.add_subplot(111,aspect='equal') ax.tricontourf(X,Y,v,0,cmap='copper') #ax.triplot(tri.Triangulation(t['vertices'][:,0],d['vertices'][:,1])) #ax.tricontourf(X,Y,v,levels=[-1e9,0,1e9],cmap='gray') plt.show()