-
Notifications
You must be signed in to change notification settings - Fork 0
/
propagation_clicker.py
319 lines (221 loc) · 7.87 KB
/
propagation_clicker.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
## Imports
# Science
import matplotlib.pyplot as plt
import numpy as np
import scipy.io as scio
from scipy import interpolate
#
# def rosette_to_tensor(ch_1,ch_2,ch_3):
# """
# converts a 45 degres rosette signal into a full tensor.
# input : the three channels of the rosette
# output : $\epsilon_{xx},\epsilon_{yy},\epsilon_{xy}
# https://www.efunda.com/formulae/solid_mechanics/mat_mechanics/strain_gage_rosette.cfm
# """
# eps_xx=ch_1+ch_3-ch_2
# eps_yy=ch_2
# eps_xy=(ch_1-ch_3)/2
# return(eps_xx,eps_yy,eps_xy)
try :
from Python_DAQ import *
except :
from DAQ_Python.Python_DAQ import *
### load data
loc_folder="D:/Users/Manips/Documents/DATA/FRICS/2023/2023-06-27-granular-light-matlab-export/manip_4/"
loc_file="event-005.npy"
loc_file_zero = "event-001.npy"
loc_params="parameters.txt"
# just in case there's nothing in the saved params
x=np.array([0,2.5,4.5,6.5,8.5])
exec(load_params(loc_folder+loc_params))
loc=loc_folder+loc_file
loc_zero=loc_folder+loc_file_zero
sampling_freq_in = clock/10
# Setup
speedup=1
speedup_smooth=1
roll_smooth=51
start=0
navg=1
data=np.load(loc,allow_pickle=True)
data_zero=np.load(loc_zero,allow_pickle=True)
interest = np.arange(0,15)
ylabels=[r"$\varepsilon_{{xx}}^{{{}}}$",r"$\varepsilon_{{yy}}^{{{}}}$",r"$\varepsilon_{{xy}}^{{{}}}$"]*5
for i in range(15):
ylabels[i]=ylabels[i].format((i+3)//3)
forces=data[(16,17),:]
mu=data[17,:100].mean()/data[16,:100].mean()
data=data[interest]
data_zero=data_zero[interest]
### Convert voltage to strains
def voltage_to_strains(ch1,ch2,ch3):
a=lambda x: V_to_strain(x,amp=495,G=1.79,i_0=0.0017,R=350)
b=lambda x: -V_to_strain(x,amp=495,G=1.86,i_0=0.0017,R=350)
ch1=a(ch1)
ch2=b(ch2)
ch3=a(ch3)
return(ch1,ch2,ch3)
for i in range(5):
ch_1=data[3*i]
ch_2=data[3*i+1]
ch_3=data[3*i+2]
ch_1,ch_2,ch_3=voltage_to_strains(ch_1,ch_2,ch_3)
a,b,c=rosette_to_tensor(ch_1,ch_2,ch_3)
data[3*i]=a
data[3*i+1]=b
data[3*i+2]=c
for i in range(5):
ch_1=data_zero[3*i]
ch_2=data_zero[3*i+1]
ch_3=data_zero[3*i+2]
ch_1,ch_2,ch_3=voltage_to_strains(ch_1,ch_2,ch_3)
a,b,c=rosette_to_tensor(ch_1,ch_2,ch_3)
data_zero[3*i]=a
data_zero[3*i+1]=b
data_zero[3*i+2]=c
### smooth data
data_smooth=smooth(data,roll_smooth)
data_smooth=np.array([avg_bin(data_smooth[i],speedup_smooth) for i in range(len(data_smooth))])
forces_smooth=smooth(forces,roll_smooth)
forces_smooth=np.array([avg_bin(forces_smooth[i],speedup_smooth) for i in range(len(forces_smooth))])
#data=np.transpose(np.transpose(data)-data[:,start])
data_smooth=np.transpose(np.transpose(data_smooth)-np.mean(data_zero,axis=1))
# !!! WEIRD CHOICE, DON'T RUN THIS SECTION ALONE
data=data_smooth
forces=forces_smooth
### Plot macroscopic checks
sm=np.load(loc_folder+"Full_Daq.npy",allow_pickle=True).all()
time_sm=np.load(loc_folder+"slowmon_time.npy")
eps_yy=-sm["data"][[1,4,7,10,13]]
fn=-sm["data"][15]
fs=-sm["data"][16]
sm=np.load(loc_folder+"slowmon.npy",allow_pickle=True)
trigger=100*sm[18]
plt.plot(time_sm,fn)
plt.plot(time_sm,fs)
plt.plot(time_sm,trigger)
plt.xlabel("time (s)")
plt.title("Fn, Fs and trigger")
plt.grid()
plt.savefig(loc_folder+"slowmon_forces.png")
plt.show()
plt.close()
colors=["r","orange","gold","green","blue"]
for i in range(5):
plt.plot(smooth(time_sm,100),smooth(eps_yy[i]-eps_yy[i][0:100].mean(),100),label="x={} m".format(x[i]), alpha=.7,color=colors[i])
plt.grid(which="both")
plt.xlabel("time (s)")
plt.ylabel(r"$\varepsilon_{yy}(t)$")
plt.legend()
plt.tight_layout()
plt.savefig(loc_folder+"slowmon_eps_yy.png")
plt.show()
plt.close()
plt.plot(x,np.mean(data[1::3,:1000],axis=1))
plt.xlabel("position (m)")
plt.ylabel(r"$\varepsilon_{yy}$ (m/m)")
plt.ylim([0,1.1*np.max(np.mean(data[1::3,:1000],axis=1))])
plt.grid(which="both")
plt.savefig(loc_folder+"{}_load_profile.png".format(loc_file[:-4]))
plt.show()
plt.close()
plt.plot(x,np.mean(-data[2::3,:1000],axis=1)/np.mean(-data[1::3,:1000],axis=1))
plt.xlabel("position (m)")
plt.ylabel(r"$\varepsilon_{xy}/\varepsilon_{yy}$")
plt.grid(which="both")
plt.savefig(loc_folder+"{}_load_profile_mu.png".format(loc_file[:-4]))
plt.show()
plt.close()
### Pick Events
sampling_freq_in=sampling_freq_in/speedup_smooth
time=np.arange(len(data[0]))/sampling_freq_in*navg
plt.plot(time,-500*forces[1,:]/3)
plt.xlabel("time (s)")
plt.ylabel(r"$F_s$ (kg)")
plt.grid(which="both")
plt.show()
indexes = np.zeros((3,5))
def onpick(event):
x_coord = event.mouseevent.xdata
y_coord = event.mouseevent.ydata
ax=event.mouseevent.inaxes
row=(ax.get_subplotspec().rowspan.start)
col=(ax.get_subplotspec().colspan.start)
print(f'Picked point: ({x_coord:.2f}, {y_coord:.2f})')
indexes[row,col]=x_coord
fig, axs = plt.subplots(3,5,sharex=True,sharey=False)
for i in range(len(data)):
axs[i%3][i//3].plot(time[start::speedup],data[i][start::speedup], label=ylabels[i], picker=True, pickradius=6)
axs[i%3][i//3].ticklabel_format(axis="y", style="sci", scilimits=(0,0))
axs[i%3][i//3].grid("both")
axs[i%3][i//3].legend()
#axs[i%3][i//3].axvline(time[find_event(newdata[i])])
axs[-1][0].set_xlabel('time (s)')
fig.set_size_inches(14,8)
fig.canvas.mpl_connect('pick_event', onpick)
plt.show()
plt.close()
### Show propagation as picked in \varepsilon_{xy}
E=2.78e9
fig, axs = plt.subplots(5,1,sharex=True,sharey=False)
for i in range(5):
axs[i].plot(1000*time[start::speedup]-1000*time.mean(),(data[3*(i+1)-1][start::speedup]-data[3*(i+1)-1][0:10000].mean())*E*1e-6, label="$\Delta\sigma_{{xy}}^{{{}}}$, $x={{{}}}$".format(i+1,x[i]))
axs[i].ticklabel_format(axis="y", style="sci", scilimits=(0,0))
axs[i].grid("both")
#plt.ticklabel_format(style='plain', axis='y')
#plt.yticks([0,-0.25])
#plt.ylim([-0.35,0.15])
axs[i].legend(loc='upper right')
axs[i].axvline(1000*indexes[2][i]-1000*time.mean(),color='k',linestyle="dashed")
axs[i].axhline(0,color='grey',linestyle="solid",alpha=.5,linewidth=2)
axs[-1].set_xlim([-0.15,0.05])
axs[-1].set_xlabel('time (ms)')
axs[2].set_ylabel('$\sigma_{xy}$ (MPa)')
plt.savefig(loc_folder+loc_file+"propagation_verticale.png")
plt.show()
plt.close()
### Print times and speeds (xy)
print(r"µ={:.3f}".format(mu))
times=indexes[-1,:]
print(np.diff(x)/np.diff(times))
### save them
np.save(loc+"_times_hand_picked.npy",indexes)
### pick them up
indexes=np.load(loc+"_times_hand_picked.npy")
### Fit propagation
from scipy.odr import Model, RealData, ODR
def linear_func(p, x):
return p[0]*x + p[1]
linear_model = Model(linear_func)
x_err = np.ones(5)*1e-3
times=(indexes.transpose()-np.mean(indexes,axis=1)).transpose()
y=[times[2,i] for i in range(5)]
# y=[np.mean(times[:,i]) for i in range(5)]
#y_err=[np.std(times[:,i]) for i in range(5)]
y_err=[1e-5 for i in range(5)]
# determine speed
datar = RealData(y, x, sx=y_err, sy=x_err)
odr = ODR(datar, linear_model, beta0=[1, 0])
out = odr.run()
popt = out.beta
perr = out.sd_beta
v=popt[0]
sigmav=perr[0]
# plot
#plt.scatter(x,times[0],label=r"$\varepsilon_{xx}$")
#plt.scatter(x,times[1],label=r"$\varepsilon_{yy}$")
plt.scatter(x,times[2],label=r"$\varepsilon_{xy}$")
plt.errorbar(x,y,yerr=y_err,xerr=x_err)#,label=r"All $\varepsilon$")
datar = RealData(x, y, sx=x_err, sy=y_err)
odr = ODR(datar, linear_model, beta0=[1, 0])
out = odr.run()
popt = out.beta
perr = out.sd_beta
plt.plot(x, linear_func(popt, x), 'r-', label='Linear fit, $v={:.0f}\pm{:.0f}$ m/s'.format(v,sigmav))
plt.xlabel("Position (m)")
plt.ylabel("Temps de passage (s)")
plt.grid(which="both")
plt.legend()
plt.savefig(loc+"_propagation.png",dpi=600)
plt.show()
plt.close()