forked from araujolma/SOAR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
executable file
·118 lines (94 loc) · 2.81 KB
/
utils.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
# -*- coding: utf-8 -*-
"""
Created on Fri Dec 16 18:53:01 2016
@author: levi
"""
import numpy
import datetime
#%% Interpolation methods
def interpV(t,tVec,xVec):
Nsize = xVec.shape[1]
ans = numpy.empty(Nsize)
for k in range(Nsize):
ans[k] = numpy.interp(t,tVec,xVec[:,k])
return ans
def interpM(t,tVec,xVec):
ni = xVec.shape[1]
nj = xVec.shape[2]
ans = numpy.empty((ni,nj))
for i in range(ni):
for j in range(nj):
ans[i,j] = numpy.interp(t,tVec,xVec[:,i,j])
return ans
#%% Calculus and related stuff
def ddt(vec,N):
dt = 1.0/(N-1)
dvec = numpy.empty_like(vec)
dvec[0] = (vec[1]-vec[0])/dt
dvec[N-1] = (vec[N-1]-vec[N-2])/dt
for k in range(1,N-1):
dvec[k] = .5*(vec[k+1]-vec[k-1])/dt
# for k in range(N-1):
# dvec[k] = vec[k+1]-vec[k]
# dvec[N-1] = dvec[N-2]
# dvec *= (1.0/dt)
return dvec
def simp(vec, N, onlyCoef=False):
""" Simple integration of array according to Simpson's method.
It can also only yield the coefficients if one wants to do the integration
by oneself (maybe in an optimized loop)."""
# coefList = numpy.ones(N)
# coefList[0] = 17.0/48.0; coefList[N-1] = coefList[0]
# coefList[1] = 59.0/48.0; coefList[N-2] = coefList[1]
# coefList[2] = 43.0/48.0; coefList[N-3] = coefList[2]
# coefList[3] = 49.0/48.0; coefList[N-4] = coefList[3]
# coefList *= 1.0/(N-1)
coefList = numpy.ones(N)
for k in range(1,N-1):
if k % 2 == 0:
coefList[k] = 2.0
else:
coefList[k] = 4.0
#
coefList /= (3.0 * (N-1))
if onlyCoef:
return coefList
else:
return coefList.dot(vec)
def testAlgn(x,y):
"""Test the alignment of three points on a plane. Returns the determinant
of the three points, which acutally proportional to the area of the trian-
gle determined by the points.
x: array with the x coordinates
y: array with the y coordinates"""
A = numpy.ones((3,3))
A[:,1] = x
A[:,2] = y
return numpy.linalg.det(A)
#%% Date, time, etc
def getNowStr():
"""Returns a string with the current time, all non numeric characters
switched to _'s. """
thisStr = str(datetime.datetime.now())
thisStr = thisStr.replace(' ','_')
thisStr = thisStr.replace('-','_')
thisStr = thisStr.replace(':','_')
thisStr = thisStr.replace('.','_')
return thisStr
if __name__ == "__main__":
print("In utils.py!")
print("Testing testAlgn:")
x = numpy.array([1.0,2.0,3.0])
y = 5.0*x
print(testAlgn(x,y))
x[0]=0.0
print(testAlgn(x,y))
N = 501
x = numpy.linspace(0,1,num=N) ** 3
Isimp = simp(x,N)
print("Isimp = "+str(Isimp))
Itrap = 0.0
Itrap = .5 * (x[0]+x[-1])
Itrap += x[1:(N-1)].sum()
Itrap *= 1.0/(N-1)
print("Itrap = "+str(Itrap))