-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtx.py
168 lines (134 loc) · 5.46 KB
/
vtx.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# -*- coding: utf-8 -*-
"""
Class definition for vorticity data
for BlobFlow Explorer
Created on Fri Aug 2 20:46:35 2013
@author: Louis Rossi
"""
from scipy import *
from string import split,atof
#import matplotlib.pylab as plt
from PyQt4 import QtCore, QtGui
from multiprocessing import Process,Pipe
from multiprocessing.connection import Client
import pickle
# Read in from the file.
class Vorticity_Frame():
def __init__(self,host,port,n,AlertFcn, domainLL=r_[-2,-2], domainUR = r_[2.,2.]):
self.FrameNumber = n
self.alert = AlertFcn
self.x0 = domainLL[0]
self.y0 = domainLL[1]
self.x1 = domainUR[0]
self.y1 = domainUR[1]
self.vdata = []
# GridStatus:
# -1 = uploading
# 0 = data arrived, no grid
# 1 = working on grid
# 2 = grid created
self.GridStatus = -1
self.UploadWait = 0
self.parent_conn,self.child_conn = Pipe()
self.UploadProc = Process(target=self.TryToConnect,args=(self.child_conn,host,port,self.FrameNumber))
self.UploadProc.start()
self.uploadTimer = QtCore.QTimer()
QtCore.QObject.connect(self.uploadTimer, QtCore.SIGNAL("timeout()"), self.DataArrived)
self.uploadTimer.start(500)
self.meshTimer = QtCore.QTimer()
QtCore.QObject.connect(self.meshTimer, QtCore.SIGNAL("timeout()"), self.MeshReady)
def TryToConnect(self,child,host,port,n):
address = (host,port)
# address = ('jeremyfisher.math.udel.edu', 6000)
#address = ('nutkin', 6000)
conn = Client(address, authkey='secret password')
conn.send(pickle.dumps(n,pickle.HIGHEST_PROTOCOL))
vdata = pickle.loads(conn.recv())
conn.send('close')
conn.close()
child.send(vdata)
def DataArrived(self):
if (self.GridStatus == -1):
if (self.parent_conn.poll()):
try:
self.vdata = self.parent_conn.recv()
self.UploadProc.join()
# self.UploadProc.terminate()
self.num_vorts = len(self.vdata)
self.uploadTimer.stop()
self.GridStatus = 0
self.alert(self.FrameNumber)
except IOError:
print "Ouch!"
self.UploadProc.terminate()
self.parent_conn,self.child_conn = Pipe()
self.UploadProc = Process(target=self.TryToConnect,args=(self.child_conn,self.FrameNumber))
self.UploadProc.start()
else:
#print "UploadWaitCounter {0:d} {1:d}".format(self.FrameNumber,self.UploadWait)
self.UploadWait += 1
# Probably do not need this.
# if (self.UploadWait > 40000):
# # Transmission lost... Try again.
# print "Trying again {0:d} {1:d}".format(self.FrameNumber,self.UploadWait)
## self.UploadProc.join()
# self.UploadProc.terminate()
# self.UploadWait = 0
# self.parent_conn,self.child_conn = Pipe()
# self.UploadProc = Process(target=self.TryToConnect,args=(self.child_conn,self.FrameNumber))
# self.UploadProc.start()
def findFrame():
x0 = vdata[0,0]
x1 = vdata[0,0]
y0 = vdata[0,1]
y1 = vdata[0,1]
for k in range(1,len(self.vdata)):
if (vdata[k,0] < x0):
x0 = vdata[k,0]
if (vdata[k,0] > x1):
x1 = vdata[k,0]
if (vdata[k,1] < y0):
y0 = vdata[k,1]
if (vdata[k,1] > y1):
y1 = vdata[k,1]
# mesh it.
def meshthread(self,nmesh,conn,meshtol = 1.0e-4):
x = r_[self.x0:self.x1:nmesh*1j]
y = r_[self.y0:self.y1:nmesh*1j]
[xm,ym] = meshgrid(x,y)
xtmp = xm.reshape(size(xm),)
ytmp = ym.reshape(size(ym),)
w = zeros(size(xm))
for k in range(len(self.vdata)):
dx = xtmp-self.vdata[k,0]
dy = ytmp-self.vdata[k,1]
r2 = dx**2 + dy**2
idx = (r2/self.vdata[k,3]>meshtol)
c = cos(self.vdata[k,5])
s = sin(self.vdata[k,5])
tmp = -(dx[idx]**2/(c**2/self.vdata[k,4] + s**2*self.vdata[k,4])+ \
dy[idx]**2/(s**2/self.vdata[k,4] + c**2*self.vdata[k,4]) + \
2.*dx[idx]*dy[idx]*c*s*(1./self.vdata[k,4]+self.vdata[k,4]))/4./self.vdata[k,3]
w[idx] += self.vdata[k,2]*exp(tmp)/2./self.vdata[k,3]
wm = w.reshape((len(x),len(y)))
conn.send((xm,ym,wm))
def SpawnMesh(self,nmesh):
self.parent_conn,self.child_conn = Pipe()
self.proc = Process(target=self.meshthread,args=(nmesh,self.child_conn))
self.proc.start()
self.meshTimer.start(1000)
self.GridStatus = 1
self.alert(self.FrameNumber)
def GrabMesh(self):
if (self.parent_conn.poll()):
self.xm,self.ym,self.wm = self.parent_conn.recv()
self.proc.join()
self.proc.terminate()
self.GridStatus=2
self.alert(self.FrameNumber)
def MeshReady(self):
if (self.GridStatus == 1):
if (self.parent_conn.poll()):
self.GrabMesh()
self.meshTimer.stop()
return(self.GridStatus)