-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
119 lines (86 loc) · 3.31 KB
/
util.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
import numpy as np
import scipy as spy
import scipy.interpolate as interp
import scipy.integrate as integ
import scipy.optimize as opt
#------------------ Exceptions ------------------
#----------------------------------------------------------
#------------------ Integrations ------------------
#----------------------------------------------------------
def scalar_prod_samples_func_1D(x_samples, y_samples, product_function, bounds, integration_product = None):
inter_func = interp.interp1d(x_samples, y_samples, kind='cubic')
if integration_product == None:
product = lambda x : inter_func * product_function
a, b = bounds
result, error_est = integ.quad(product, a, b)
else:
result = integration_product(inter_func, product_function)
return result
def phase_computation(func_1, func_2, scalar_product):
phase = 0
def func_to_minimize(phase):
delta_func_2 = lambda x : func_2(x + delta_func_2)
return - scalar_product(func_1, func_2)
final_phase = opt.minimize(func_to_minimize, phase)
return final_phase
#------------------ MAPDL - XPL ------------------
#----------------------------------------------------------
def nb_modes(xpl):
dic = xpl.json()
list_informations = dic['children']
dic = list_informations[3]
list_of_modes = dic['children']
return len(list_of_modes)
#------------------ Matrixs treatment ------------------
#----------------------------------------------------------
def from_unsym_to_sym(M):
diag = spy.sparse.diags(M.diagonal())
return M + M.transpose() - diag
def compute_modal_coeff(matrix, eigen_vector):
coeff = eigen_vector.T.dot(matrix.dot(eigen_vector)) \
/ eigen_vector.T.dot(eigen_vector)
return coeff
#------------------Eigen value following ------------------
#----------------------------------------------------------
def diff_cost(freqs_1, freqs_2):
n_1, n_2 = np.size(freqs_1), np.size(freqs_2)
cost_matrix = np.zeros((n_1, n_2))
for i, freq_1 in enumerate(freqs_1):
for j, freq_2 in enumerate(freqs_2):
cost_matrix[i,j] = abs(freq_1 - freq_2)
return cost_matrix
#------------------Added mass computation------------------
#----------------------------------------------------------
def freq_to_ev(freq):
return (2 * np.pi * freq) ** 2
#---------------------Polar coordinates---------------------
#-----------------------------------------------------------
def cart2pol(x, y, positive_only=True):
"""Converts 2D cartesian coordinates to polar coordinates
Parameters
----------
x (float): x coordinate of point
y (float): y coordinate of point
positive_only (boolean): to have angles from 0 to 2*pi rather than -pi to pi
Returns
-------
radius (float): calculated radius of point
angle (float): calculated angle of point
"""
radius = np.linalg.norm([x,y])
angle = np.arctan2(y, x)
if positive_only:
if angle < 0:
angle = 2*np.pi + angle
return (radius, angle)
def cart2pol_array(U, positive_only = True):
"""
Applies cart2pol to an array.
"""
X = U[:,0]
Y = U[:,1]
(n_ligns,) = np.shape(X)
U_pol = np.zeros((n_ligns, 2))
for i in range(n_ligns):
U_pol[i,0], U_pol[i,1] = cart2pol(X[i], Y[i], positive_only)
return U_pol