-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
87 lines (69 loc) · 2.7 KB
/
utils.py
File metadata and controls
87 lines (69 loc) · 2.7 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
import numpy as np
class der:
def __init__(self, f):
self.f = f
def euler(self, h):
df = (self.f[1:] - self.f[:-1]) / h
return df
def euler_medio(self, h):
df = (self.f[2:] - self.f[:-2]) / (2 * h)
return df
def euler_medio2d(
self,
h: float = None,
dim: str = 'i',
delta_x: float = None,
delta_y: float = None,
) -> np.ndarray:
"""
Calcula la derivada euleriana en 2D
:param h: Espaciado en X/Y
:param dim: Dimensión sobre la que se deriva (i==X, j==Y). Si se selecciona both se calculan las 2
:param delta_x: Espaciado en X (solo para dim=both).
:param delta_y: Espaciado en Y (solo para dim=both)
"""
if dim == 'i':
df = (self.f[1:-1, 2:] - self.f[1:-1, :-2]) / (2 * h)
elif dim == 'j':
df = (self.f[2:, 1:-1] - self.f[:-2, 1:-1]) / (2 * h)
elif dim == 'both':
df = np.array(
[
(self.f[1:-1, 2:] - self.f[1:-1, :-2]) / (2 * delta_x),
(self.f[2:, 1:-1] - self.f[:-2, 1:-1]) / (2 * delta_y),
]
)
return df
def L1(self, df_true, df):
N = len(df)
return 1 / N * np.sum(np.abs(df_true - df))
def trac(N: int, r: float) -> np.ndarray:
"""
Calcula la posición de trazadores (partículas sobre el fluido) situados equiespaciadamente a un radio
:param N: Número de partículas
:param r: Radio donde situar los trazadores
"""
theta = np.linspace(0, 2 * np.pi, N, endpoint=False)
return np.column_stack((r * np.cos(theta), r * np.sin(theta)))
def semi_interp(
X: np.ndarray, Y: np.ndarray, x_p: np.ndarray, y_p: np.ndarray, trazador: np.ndarray
) -> tuple:
"""
Asigna el valor de x_p y y_p más cercano a la posición del trazador
:param X: Valores de X en el grid
:param Y: Valores de Y en el grid
:param x_p: Mapa del que se seleccionará el valor más cercano al trazador
:param y_p: Mapa del que se seleccionará el valor más cercano al trazador
:param trazador: Posiciones de los trazadores
"""
index_x = np.empty(len(trazador[:, 0]))
index_y = np.empty_like(index_x)
for i, tra in enumerate(trazador):
index_x[i] = np.argmin(np.abs(X[0, :] - tra[0]))
index_y[i] = np.argmin(np.abs(Y[:, 0] - tra[1]))
return (
x_p[np.array(index_y, dtype=int), np.array(index_x, dtype=int)],
y_p[np.array(index_y, dtype=int), np.array(index_x, dtype=int)],
)
def extraer_num(nombre):
return int(nombre.split('N')[-1].split('_')[0])