-
Notifications
You must be signed in to change notification settings - Fork 2
/
transformations.py
180 lines (155 loc) · 5.68 KB
/
transformations.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
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the license found in the
# LICENSE file in the root directory of this source tree.
#
from typing import List, Tuple, Callable
import torch
import numpy as np
# ========================================================
# Generic Lie Trotter implementation
# ========================================================
def lie_trotter_exp_2(
state: Tuple,
functions: List[Callable],
strengths: List[float],
T: int = 1,
factor: float = 1.,
**f_args
):
for _ in range(T):
for h, t in zip(reversed(functions), reversed(strengths)):
term = factor * t / T / 2
state = h(term, *state, **f_args)
for h, t in zip(functions, strengths):
term = factor * t / T / 2
state = h(term, *state, **f_args)
return state
def lie_trotter_exp(
state: Tuple,
functions: List[Callable],
strengths: List[float],
order: int = 2,
T: int = 1,
factor: float = 1.,
**f_args
):
if T == 0:
return state
factor = factor / T
for _ in range(T):
if order == 2:
state = lie_trotter_exp_2(state, functions, strengths, T=1, factor=factor, **f_args)
elif order > 2:
u_k = 1 / (4 - 4**(1 / (2 * order - 1)))
state = lie_trotter_exp(state, functions, strengths, order=order - 2, T=1, factor=factor * u_k, **f_args)
state = lie_trotter_exp(state, functions, strengths, order=order - 2, T=1, factor=factor * u_k, **f_args)
state = lie_trotter_exp(state, functions, strengths, order=order - 2, T=1, factor=factor * (1 - 4 * u_k), **f_args)
state = lie_trotter_exp(state, functions, strengths, order=order - 2, T=1, factor=factor * u_k, **f_args)
state = lie_trotter_exp(state, functions, strengths, order=order - 2, T=1, factor=factor * u_k, **f_args)
elif order == 0:
pass
else:
raise NotImplementedError()
return state
# ========================================================
# Symmetry related functions for Navier-Stokes
# ========================================================
class NSTransforms:
# time translation
@staticmethod
def group_1(g, t, x, y, u, v, px=None, py=None):
if px is None:
return t + g, x, y, u, v
else:
return t + g, x, y, u, v, px, py
# x translation
@staticmethod
def group_2(g, t, x, y, u, v, px=None, py=None):
if px is None:
return t, x + g, y, u, v
else:
return t, x + g, y, u, v, px, py
# y translation
@staticmethod
def group_3(g, t, x, y, u, v, px=None, py=None):
if px is None:
return t, x, y + g, u, v
else:
return t, x, y + g, u, v, px, py
# scale change
@staticmethod
def group_4(g, t, x, y, u, v, px=None, py=None):
g = torch.exp(g)
if px is None:
return g * g * t, g * x, g * y, u / g, v / g
else:
return g * g * t, g * x, g * y, u / g, v / g, px / (g * g), py / (g * g)
# rotation
@staticmethod
def group_5(g, t, x, y, u, v, px=None, py=None):
if px is None:
return t, torch.cos(g) * x - torch.sin(g) * y, torch.sin(g) * x + np.cos(g) * y, torch.cos(g) * u - torch.sin(g) * v, torch.sin(g) * u + torch.cos(g) * v
else:
return t, torch.cos(g) * x - torch.sin(g) * y, torch.sin(g) * x + np.cos(g) * y, torch.cos(g) * u - torch.sin(g) * v, torch.sin(g) * u + torch.cos(g) * v, px, py
# Linear E(x)
@staticmethod
def group_6(g, t, x, y, u, v, px=None, py=None):
if px is None:
return t, x + g * t, y, u + g, v
else:
return t, x + g * t, y, u + g, v, px, py
# Linear E(y)
@staticmethod
def group_7(g, t, x, y, u, v, px=None, py=None):
if px is None:
return t, x, y + g * t, u, v + g
else:
return t, x, y + g * t, u, v + g, px, py
# Quadratic E(x)
@staticmethod
def group_8(g, t, x, y, u, v, px=None, py=None):
if px is None:
return t, x + g * t, y, u + g, v
else:
return t, x + g * t * t, y, u + 2 * g * t, v, px - g, py
# Quadratic E(y)
@staticmethod
def group_9(g, t, x, y, u, v, px=None, py=None):
if px is None:
return t, x, y + g * t * t, u, v + g
else:
return t, x, y + g * t, u, v + 2 * g * t, px, py - g
def apply(self, gs, t, x, y, u, v, px=None, py=None, order=4, steps=1, **f_args):
group_ops = [
NSTransforms.group_1,
NSTransforms.group_2,
NSTransforms.group_3,
NSTransforms.group_4,
NSTransforms.group_5,
NSTransforms.group_6,
NSTransforms.group_7,
NSTransforms.group_8,
NSTransforms.group_9,
]
if px is None:
state = lie_trotter_exp(
(t, x, y, u, v),
group_ops,
gs,
order=order,
T=steps,
**f_args
)
return state[0], state[1], state[2], state[3], state[4]
else:
state = lie_trotter_exp(
(t, x, y, u, v, px, py),
group_ops,
gs,
order=order,
T=steps,
**f_args
)
return state[0], state[1], state[2], state[3], state[4], state[5], state[6]