forked from jungla/ICOM-fluidity-toolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyfun.py
149 lines (111 loc) · 2.88 KB
/
myfun.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
#!/share/apps/python/2.7.2/bin/python
import numpy as np
global np
## assign values to a vtk grid from another vtk grid
def replaceScalar(T,coord,coord_o):
Tn = []
for id in range(0,len(coord_o)-1):
id_x = np.asarray(np.where(np.trunc(coord[:,0]) == np.trunc(coord_o[id,0])))
id_y = np.asarray(np.where(np.trunc(coord[:,1]) == np.trunc(coord_o[id,1])))
id_z = np.asarray(np.where(np.trunc(coord[:,2]) == np.trunc(-coord_o[id,2])))
id_T = np.intersect1d(np.intersect1d(id_z,id_y),id_x)
Tval = T[id_T]
coord = np.delete(coord,id_T,0)
T = np.delete(T,id_T,0)
Tn.append(Tval[1])
return Tn
## digits format
def digit(day,n):
lday = str(day)
while (len(lday) < n):
lday = '0'+lday
return lday
# function to sort one matrix: used to write VTK files
def f1(X):
a,b,c = X.shape
return [(X[i,j,k]) for k in xrange(c) for j in xrange(b) for i in xrange(a)]
# function to sort three matrices: used to write VTK files
def f3(X,Y,Z):
a,b,c = X.shape
return [(X[i,j,k],Y[i,j,k],Z[i,j,k]) for k in xrange(c) for j in xrange(b) for i in xrange(a)]
# remove duplicates from list
def f5(seq, idfun=None):
# order preserving
if idfun is None:
def idfun(x): return x
seen = {}
result = []
r_id = []
id = 0
for item in seq:
marker = idfun(item)
id = id + 1
if marker in seen: continue
seen[marker] = 1
result.append(item)
r_id.append(id)
return result, r_id
# define triangles from a rectilinear mesh
def pts2trs(idm,jdm):
triangles = []
for j in range(jdm):
for i in range(idm-1):
a=i+j*idm
b=a+1
c=a+idm
d=c+1
triangles.append([a,b,c])
triangles.append([c,b,d])
# print j,i,d,[a,b,c],[c,b,d]
if d == idm*jdm-1: break
if d == idm*jdm-1: break
return triangles
def meshgrid2(*arrs):
arrs = tuple(reversed(arrs)) #edit
lens = map(len, arrs)
dim = len(arrs)
sz = 1
for s in lens:
sz*=s
ans = []
for i, arr in enumerate(arrs):
slc = [1]*dim
slc[i] = lens[i]
arr2 = np.asarray(arr).reshape(slc)
for j, sz in enumerate(lens):
if j!=i:
arr2 = arr2.repeat(sz, axis=j)
ans.append(arr2)
return tuple(ans[::-1])
def smooth2(R,times):
global np
ids,jds = R.shape
Rf = np.zeros([ids,jds]);
Rf[:,:] = np.nan
for t in xrange(times):
# smooth
for i in xrange(ids):
for j in xrange(jds):
if ~np.isnan(R[i,j]):
Rf[i,j] = R[i,j]
pn = 1
for k in range(4):
if k == 0:
ip = -1
jp = 0
elif k == 1:
ip = 0
jp = -1
elif k == 2:
ip = 1
jp = 0
else:
ip = 0
jp = 1
if i+ip>0 and j+jp>0 and i+ip<ids and j+jp<jds:
if ~np.isnan(R[i+ip,j+jp]):
Rf[i,j] = Rf[i,j] + R[i+ip,j+jp]
pn = pn + 1
Rf[i,j] = Rf[i,j]/pn
R = Rf
return R