-
Notifications
You must be signed in to change notification settings - Fork 4
/
ex_vector_field_flat_surface.py
78 lines (62 loc) · 2.17 KB
/
ex_vector_field_flat_surface.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
"""This example illustrates MARBLE for a vector field on a flat surface."""
import numpy as np
import sys
from MARBLE import plotting, preprocessing, dynamics, net, postprocessing
import matplotlib.pyplot as plt
def f0(x):
return x * 0 + np.array([-1, -1])
def f1(x):
return x * 0 + np.array([1, 1])
def f2(x):
eps = 1e-1
norm = np.sqrt((x[:, [0]] + 1) ** 2 + x[:, [1]] ** 2 + eps)
u = x[:, [1]] / norm
v = -(x[:, [0]] + 1) / norm
return np.hstack([u, v])
def f3(x):
eps = 1e-1
norm = np.sqrt((x[:, [0]] - 1) ** 2 + x[:, [1]] ** 2 + eps)
u = x[:, [1]] / norm
v = -(x[:, [0]] - 1) / norm
return np.hstack([u, v])
# def f2(x):
# eps = 1e-1
# norm = np.sqrt((x[:, [0]]) ** 2 + x[:, [1]] ** 2 + eps)
# u = -(x[:, [0]] ) / norm
# v = -(x[:, [1]] ) / norm
# return np.hstack([u, v])
# def f3(x):
# eps = 1e-1
# norm = np.sqrt((x[:, [0]]) ** 2 + x[:, [1]] ** 2 + eps)
# u = (x[:, [0]] ) / norm
# v = (x[:, [1]] ) / norm
# return np.hstack([u, v])
def main():
# generate simple vector fields
# f0: linear, f1: point source, f2: point vortex, f3: saddle
n = 512
x = [dynamics.sample_2d(n, [[-1, -1], [1, 1]], "random", seed=i) for i in range(4)]
y = [f0(x[0]), f1(x[1]), f2(x[2]), f3(x[3])] # evaluated functions
# construct data object
data = preprocessing.construct_dataset(x, y)
# train model
model = net(data, params={'inner_product_features': False,
'diffusion': False})
model.fit(data)
# evaluate model on data
data = model.transform(data)
data = postprocessing.cluster(data)
data = postprocessing.embed_in_2D(data)
# plot results
titles = ["Linear left", "Linear right", "Vortex right", "Vortex left"]
plotting.fields(data, titles=titles, col=2, width=0.01)
plt.savefig('fields.svg')
plotting.embedding(data, data.y.numpy(), titles=titles, clusters_visible=True)
plt.savefig('embedding.svg')
plotting.histograms(data, titles=titles)
plt.savefig('histogram.svg')
plotting.neighbourhoods(data)
plt.savefig('neighbourhoods.svg')
plt.show()
if __name__ == "__main__":
sys.exit(main())