-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloopcompare.py
211 lines (160 loc) · 6.84 KB
/
loopcompare.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
# -*- coding: utf-8 -*-
"""
Created on Fri Oct 16 16:24:39 2020
@author: mi19356
"""
import numpy as np
import pandas as pd
import os
import matplotlib.pyplot as plt
import numpy.polynomial.polynomial as P
class load_info:
def __init__(self,cname):
loc=os.path.join(os.path.dirname(__file__),'data')
#import data
exp=pd.read_excel(os.path.join(loc, cname + '.xlsx'),header=None,sheet_name='Exp').to_numpy()
sim=pd.read_excel(os.path.join(loc, cname + '.xlsx'),header=None,sheet_name='Sim').to_numpy()
self.exp=exp
self.sim=sim
def fitcurves(self,**kwargs):
exp=self.exp
sim=self.sim
#number of x points to use
xint=int(kwargs['xpoints'])
#number of polynomials
poly=int(kwargs['poly'])
#determine if increasing values or decreasing ###need to have this for exp and sim
decreas=[]
decreassim=[]
ppp=0
pp=0
for i in range(0,len(exp[0,0::2])):
if (exp[0,pp]>exp[1,pp]):
decreas.append(True)
else:
decreas.append(False)
pp+=2
if (sim[0,ppp]>sim[1,ppp]):
decreassim.append(True)
else:
decreassim.append(False)
ppp+=2
#find indeces of loading and unloading interchange###need to have this for both exp and sim
pp=0
turnpointexp=[]
turnpointsim=[]
for i in range(0,len(exp[0,0::2])):
if (decreas[i]==False):
turnpointexp.append(np.argmax(exp[:,pp]))
if(decreassim[i]==False):
turnpointsim.append(np.argmax(sim[:,pp]))
if(decreas[i]==True):
turnpointexp.append(np.argmin(exp[:,pp]))
if(decreassim[i]==True):
turnpointsim.append(np.argmin(sim[:,pp]))
pp+=2
maxvalsexp=np.asarray([max(x) for x in zip(*exp[:,0::2])])
minvalsexp=np.asarray([min(x) for x in zip(*exp[:,0::2])])
#polyfit
zexp1=[]
zexp2=[]
zsim1=[]
zsim2=[]
n=0
t=1
p=2
for i in range(0,len(exp[0,:])):
if i % p == 0:
zexp1.append(P.polyfit(exp[0:turnpointexp[n],i],exp[0:turnpointexp[n],t],poly))
zexp2.append(P.polyfit(exp[turnpointexp[n]:,i],exp[turnpointexp[n]:,t],poly))
zsim1.append(P.polyfit(sim[0:turnpointsim[n],i],sim[0:turnpointsim[n],t],poly))
zsim2.append(P.polyfit(sim[turnpointsim[n]:,i],sim[turnpointsim[n]:,t],poly))
else:
t+=2
n+=1
#new values and extra x-values
xpoints1=np.linspace(minvalsexp,maxvalsexp,xint)
xpoints2=np.linspace(maxvalsexp,minvalsexp,xint)
ffitexp1=[]
ffitexp2=[]
ffitsim1=[]
ffitsim2=[]
for i in range(0,len(exp[0,0::2])):
ffitexp1.append(P.polyval(xpoints1[:,i], zexp1[i]))
ffitexp2.append(P.polyval(xpoints2[:,i], zexp2[i]))
ffitsim1.append(P.polyval(xpoints1[:,i], zsim1[i]))
ffitsim2.append(P.polyval(xpoints2[:,i], zsim2[i]))
self.ffitexp1=np.asarray(ffitexp1)
self.ffitsim1=np.asarray(ffitsim1)
self.ffitexp2=np.asarray(ffitexp2)
self.ffitsim2=np.asarray(ffitsim2)
self.xpoints1=xpoints1
self.xpoints2=xpoints2
self.decreas=decreas
self.decreassim=decreassim
self.stress=exp[:,1::2]
self.strain=exp[:,0::2]
self.stresssim=sim[:,1::2]
self.strainsim=sim[:,0::2]
def plotter(self,**kwargs):
#used to plot the supplied data with the fitted data
#which loop to compare
interestloop=int(kwargs['interestloop'])-1
ffitexp1=self.ffitexp1
ffitsim1=self.ffitsim1
ffitexp2=self.ffitexp2
ffitsim2=self.ffitsim2
xpoints1=self.xpoints1
xpoints2=self.xpoints2
stress=self.stress
strain=self.strain
stresssim=self.stresssim
strainsim=self.strainsim
ffitsim=[[]]*len(ffitexp1[:,0])
ffitexp=[[]]*len(ffitexp1[:,0])
xpoints=[[]]*len(ffitexp1[:,0])
for i in range(0,len(ffitexp1[:,0])):
ffitexp[i]=np.append(ffitexp1[i,:],ffitexp2[i,:])
ffitsim[i]=np.append(ffitsim1[i,:],ffitsim2[i,:])
xpoints[i]=np.append(xpoints1[:,i],xpoints2[:,i])
pltexpfit,=plt.plot(xpoints[interestloop],ffitexp[interestloop],'b',label='exp fit')
pltexp,=plt.plot(strain,stress,'b--',label='exp')
pltsimfit,=plt.plot(xpoints[interestloop],ffitsim[interestloop],'r',label='sim fit')
pltsim,=plt.plot(strainsim,stresssim,'r--',label='sim')
plt.legend(handles=[pltexpfit,pltexp,pltsimfit,pltsim])
plt.show()
def errorcal(self):
ffitexp1=self.ffitexp1
ffitsim1=self.ffitsim1
ffitexp2=self.ffitexp2
ffitsim2=self.ffitsim2
decreas=self.decreas
decreassim=self.decreassim
ffitsim=[[]]*len(ffitexp1[:,0])
ffitexp=[[]]*len(ffitexp1[:,0])
for i in range(0,len(ffitexp1[:,0])):
ffitexp[i]=np.append(ffitexp1[i,:],ffitexp2[i,:])
ffitsim[i]=np.append(ffitsim1[i,:],ffitsim2[i,:])
#need to check if false and true, must be the same
for i in range(0,len(decreas)):
if (decreas[i]!=decreassim[i]):
ffitsim[i]=ffitsim[i][::-1]
#combine exp and sim data
totalffitmax=[]
totalffitmin=[]
for i in range(0,len(ffitexp1[:,0])):
totalffitmax.append(np.maximum(ffitexp[i],ffitsim[i]))
totalffitmin.append(np.minimum(ffitexp[i],ffitsim[i]))
#divide predicted with experiment
ratio=[]
for i in range(0,len(ffitexp1[:,0])):
ratio.append(np.log(abs(np.asarray(ffitsim[i]))/abs(np.asarray(ffitexp[i]))))
#calculate the unsigned relative error
diff=(np.asarray(totalffitmax)-np.asarray(totalffitmin))/np.asarray(totalffitmin)
#find the median unsigned percantage error
MAPE=np.asarray([np.exp(np.median(np.abs(x)))-1 for x in zip(*np.transpose(ratio))])*100
return MAPE
def find_nearest(array, value):
array = np.asarray(array)
idx = (np.abs(array - value)).argmin()
return idx