-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfollow.py
More file actions
150 lines (136 loc) · 5.26 KB
/
follow.py
File metadata and controls
150 lines (136 loc) · 5.26 KB
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
# -*- coding: utf-8 -*-
"""follow.ipynb
Automatically generated by Colab.
Original file is located at
https://colab.research.google.com/drive/1JX7M6AHdEzqKoI22yTKCxA1h5SRtWqbJ
"""
import numpy
from numpy.linalg import norm
from google.colab import drive
drive.mount('/content/drive')
f = numpy.loadtxt("unitVecdata.dat", delimiter = " ", skiprows = 1)
xold = [1, 0, 0];
yold = [0, 1, 0];
zold = [0, 0, 1];
time = f[:,0]
x = f[:,1:4]
y = f[:,4:7]
z = f[:,7:10]
cg = f[:,10:13]
"""With regards to the computing R: Numpy matrix multiplication is giving incorrect dimensions. As a workaround I have hardcoded the required matrix. This only works because the old matrix is the identity matrix (xold, yold, zold). The original equation is:
R = xnorm'*xold + ynorm'*yold + znorm'*zold
"""
invR = numpy.zeros((len(time),3,3))
for i in range(0,len(time)):
newcg = cg[i];
xnew = x[i] - newcg
ynew = y[i] - newcg
znew = z[i] - newcg
xnorm = xnew/norm(xnew,2)
ynorm = ynew/norm(ynew,2)
znorm = znew/norm(znew,2)
R = [[xnorm[0], ynorm[0], znorm[0]], [xnorm[1], ynorm[1], znorm[1]], [xnorm[2], ynorm[2], znorm[2]]]
invR[i,:,:] = numpy.linalg.inv(R)
pvtu = open("/content/drive/My Drive/Colab Notebooks/c380/coarse_brain_c380_test_pt.0000.pvtu", "r")
count = 0;
for a in pvtu:
if(a[1:15]==" <Piece Source"):
count = count + 1;
node = []
refcoord = []
for j in range(count):
name = "/content/drive/My Drive/Colab Notebooks/c380/coarse_brain_c380_test_pt.vtu.0000.000" + str(j)
writename = "/content/drive/My Drive/Colab Notebooks/c380rel/coarse_brain_c380_test_pt_rel.vtu.0000.000" + str(j)
vtu = open(name, "r")
writevtu = open(writename, "w")
for b in vtu:
if(b==' <DataArray type="Float64" NumberOfComponents="3" format="ascii">\n'):
break
else:
writevtu.write("%s"%b)
writevtu.write("%s"%b)
for b in vtu:
if(b==' </DataArray>\n'):
break
else:
tempx, tempy, tempz = b.split()
node = [float(tempx), float(tempy), float(tempz)]
refcoord.append(node)
noderotate = numpy.matmul(invR[0,:,:], node)
reldisp = noderotate
writevtu.write("\t\t\t\t\t%f %f %f\n"%(reldisp[0], reldisp[1], reldisp[2]))
writevtu.write("%s"%b)
for b in vtu:
if(b==' <PointData>\n'):
break
else:
writevtu.write("%s"%b)
writevtu.write("\t\t</Piece>\n")
writevtu.write("\t</UnstructuredGrid>\n")
writevtu.write("</VTKFile>\n")
vtu.close()
writevtu.close()
node = []
for i in range(1, len(time)):
for j in range(count):
if i<10:
name = "/content/drive/My Drive/Colab Notebooks/c380/coarse_brain_c380_test_pt.vtu.000" + str(i) + ".000" + str(j)
writename = "/content/drive/My Drive/Colab Notebooks/c380rel/coarse_brain_c380_test_pt_rel.vtu.000" + str(i) + ".000" + str(j)
else:
name = "/content/drive/My Drive/Colab Notebooks/c380/coarse_brain_c380_test_pt.vtu.00" + str(i) + ".000" + str(j)
writename = "/content/drive/My Drive/Colab Notebooks/c380rel/coarse_brain_c380_test_pt_rel.vtu.00" + str(i) + ".000" + str(j)
vtu = open(name, "r")
writevtu = open(writename, "w")
for b in vtu:
if(b==' <DataArray type="Float64" NumberOfComponents="3" format="ascii">\n'):
break
else:
writevtu.write("%s"%b)
writevtu.write("%s"%b)
for b in vtu:
if(b==' </DataArray>\n'):
break
else:
tempx, tempy, tempz = b.split()
node = [float(tempx), float(tempy), float(tempz)]
noderotate = numpy.matmul(invR[i,:,:], node)
reldisp = noderotate
id = id + 1
writevtu.write("\t\t\t\t\t%f %f %f\n"%(reldisp[0], reldisp[1], reldisp[2]))
writevtu.write("%s"%b)
for b in vtu:
if(b==' <PointData>\n'):
break
else:
writevtu.write("%s"%b)
writevtu.write("\t\t</Piece>\n")
writevtu.write("\t</UnstructuredGrid>\n")
writevtu.write("</VTKFile>\n")
vtu.close()
writevtu.close()
for j in range(len(time)):
if j<10:
pvtuname = "/content/drive/My Drive/Colab Notebooks/c380pvtu/coarse_brain_c380_test_pt.000" + str(j) + ".pvtu"
else:
pvtuname = "/content/drive/My Drive/Colab Notebooks/c380pvtu/coarse_brain_c380_test_pt.00" + str(j) + ".pvtu"
pvtuwrite = open(pvtuname, "w")
pvtuwrite.write('<?xml version="1.0"?>\n')
pvtuwrite.write('<VTKFile type="PUnstructuredGrid" version="0.1" byte_order="LittleEndian">\n')
pvtuwrite.write('\t<PUnstructuredGrid GhostLevel="0">\n')
pvtuwrite.write('\t\t<PPoints>\n')
pvtuwrite.write('\t\t\t<PDataArray type="Float64" NumberOfComponents="3" format="ascii"/>\n')
pvtuwrite.write('\t\t</PPoints>\n')
pvtuwrite.write('\t\t<PCells>\n')
pvtuwrite.write('\t\t\t<PDataArray type="Int32" Name="connectivity" NumberOfComponents="1"/>\n')
pvtuwrite.write('\t\t\t<PDataArray type="Int32" Name="offsets" NumberOfComponents="1"/>\n')
pvtuwrite.write('\t\t\t<PDataArray type="Int32" Name="types" NumberOfComponents="1"/>\n')
pvtuwrite.write('\t\t</PCells>\n')
for i in range(count):
if j<10:
pvtuwrite.write('\t\t<Piece Source="c380/coarse_brain_c380_test_pt_rel.vtu.000%s.000%s"/>\n'%(str(j),i))
else:
pvtuwrite.write('\t\t<Piece Source="c380/coarse_brain_c380_test_pt_rel.vtu.00%s.000%s"/>\n'%(str(j),i))
pvtuwrite.write('\t</PUnstructuredGrid>\n')
pvtuwrite.write('</VTKFile>\n')
pvtuwrite.close()
invR