-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConfKAM.py
147 lines (134 loc) · 6.2 KB
/
ConfKAM.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
#
# BSD 2-Clause License
#
# Copyright (c) 2021, Cristel Chandre
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import numpy as xp
from numpy import linalg as LA
from numpy.fft import fftn, ifftn, fftshift, ifftshift
from scipy.optimize import root
import matplotlib.pyplot as plt
from ConfKAM_modules import compute_line_norm, compute_region
from ConfKAM_dict import dict
#import gc
import warnings
warnings.filterwarnings("ignore")
def main():
case = ConfKAM(dict)
eval(case.Method + '(case)')
plt.show()
class ConfKAM:
def __repr__(self):
return '{self.__class__.__name__}({self.DictParams})'.format(self=self)
def __str__(self):
return 'KAM in configuration space ({self.__class__.__name__}) with omega0 = {self.omega0} and Omega = {self.Omega}'.format(self=self)
def __init__(self, dict):
for key in dict:
setattr(self, key, dict[key])
self.DictParams = dict
self.dim = len(self.omega0)
self.zero_ = self.dim * (0,)
self.id = xp.reshape(xp.identity(self.dim), 2 * (self.dim,) + self.dim * (1,))
def set_var(self, L):
ind_nu = self.dim * (xp.hstack((xp.arange(0, L // 2), xp.arange(- L // 2, 0))),)
ind_phi = self.dim * (xp.linspace(0.0, 2.0 * xp.pi, L, endpoint=False, dtype=self.Precision),)
nu = xp.meshgrid(*ind_nu, indexing='ij')
self.phi = xp.meshgrid(*ind_phi, indexing='ij')
self.norm_nu = LA.norm(nu, axis=0)
self.omega0_nu = xp.einsum('i,i...->...', self.omega0, nu)
self.Omega_nu = xp.einsum('i,i...->...', self.Omega, nu)
self.sml_div = - 1j * xp.divide(1.0, self.omega0_nu, where=self.omega0_nu!=0.0)
self.sml_div[self.zero_] = 0.0
self.lk = - self.omega0_nu ** 2
self.ilk = xp.divide(1.0, self.lk, where=self.lk!=0.0)
self.ilk[self.zero_] = 0.0
self.rescale_fft = self.Precision(L ** self.dim)
self.tail_indx = self.dim * xp.index_exp[L//4:3*L//4+1]
self.pad = self.dim * ((L//4, L//4),)
def initial_h(self, eps, L, method='one_step'):
self.set_var(L)
if method == 'zero':
return [xp.zeros_like(self.lk), 0.0]
elif method == 'one_step':
return [- ifftn(self.fft_h(self.Dv(self.phi, eps, self.Omega)) * self.ilk).real, 0.0]
else:
h = - ifftn(self.fft_h(self.Dv(self.phi, eps, self.Omega)) * self.ilk).real
sol = root(self.conjug_eq, h.flatten(), args=(eps, L), method=method, options={'fatol': 1e-9})
if sol.success:
return [sol.x.reshape((L, L)), 0.0]
else:
return [h, 0.0]
def conjug_eq(self, h, eps, L):
arg_v = (self.phi + xp.tensordot(self.Omega, h.reshape(self.dim * (L,)), axes=0)) % (2.0 * xp.pi)
return (ifftn(self.lk * self.fft_h(h.reshape(self.dim * (L,)))).real + self.Dv(arg_v, eps, self.Omega)).flatten()
def refine_h(self, h, lam, eps):
self.set_var(h.shape[0])
fft_h = self.fft_h(h)
arg_v = (self.phi + xp.tensordot(self.Omega, h, axes=0)) % (2.0 * xp.pi)
fft_l = 1j * self.Omega_nu * fft_h
fft_l[self.zero_] = self.rescale_fft
l = ifftn(fft_l).real
epsilon = ifftn(self.lk * fft_h).real + self.Dv(arg_v, eps, self.Omega) + lam
fft_leps = self.fft_h(l * epsilon, setzero=False)
delta = - fft_leps[self.zero_].real / self.rescale_fft
w = ifftn((delta * fft_l + fft_leps) * self.sml_div).real
#del fft_l, fft_leps, epsilon, arg_v, fft_h
#gc.collect()
fft_wll = self.fft_h(w / (l ** 2), setzero=False)
fft_ill = self.fft_h(1.0 / (l ** 2), setzero=False)
w0 = - fft_wll[self.zero_].real / fft_ill[self.zero_].real
beta = ifftn((fft_wll + w0 * fft_ill) * self.sml_div.conj()).real
fft_h = self.fft_h(h + beta * l - xp.mean(beta * l) * l)
lam_ = lam + delta
#del beta, l, fft_wll, fft_ill, w
#gc.collect()
tail_norm = xp.abs(fft_h[self.tail_indx]).max() / self.rescale_fft
if self.AdaptSize and (tail_norm >= self.Threshold) and (h.shape[0] < self.Lmax):
self.set_var(2 * h.shape[0])
fft_h = ifftshift(xp.pad(fftshift(self.fft_h(h)), self.pad)) * (2 ** self.dim)
lam_ = lam
h_ = ifftn(fft_h).real
arg_v = (self.phi + xp.tensordot(self.Omega, h_, axes=0)) % (2.0 * xp.pi)
err = xp.abs(ifftn(self.lk * fft_h).real + self.Dv(arg_v, eps, self.Omega) + lam_).max()
if self.AdaptSize and (tail_norm >= self.Threshold) and (h.shape[0] >= self.Lmax):
err = self.TolMax ** 2
if self.MonitorGrad:
dh_ = self.id + xp.tensordot(self.Omega, xp.gradient(h_, 2.0 * xp.pi / self.Precision(h_.shape[0])), axes=0)
det_h_ = xp.abs(LA.det(xp.moveaxis(dh_, [0, 1], [-2, -1]))).min()
if det_h_ <= self.TolMin:
print('\033[31m Warning: non-invertibility...\033[00m')
return h_, lam_, err
def fft_h(self, h, setzero=True):
fft_h = fftn(h)
if setzero:
fft_h[self.zero_] = 0.0
fft_h[xp.abs(fft_h) <= self.Threshold * xp.abs(fft_h).max()] = 0.0
return fft_h
def pad_h(self, h):
return ifftn(ifftshift(xp.pad(fftshift(self.fft_h(h)), self.pad))).real * (2 ** self.dim)
def norms(self, h, r=0):
fft_h = self.fft_h(h)
return xp.sqrt((self.norm_nu ** (2 * r) * (xp.abs(fft_h) / self.rescale_fft) ** 2).sum()), xp.sqrt((xp.abs(ifftn(self.omega0_nu ** r * fft_h)) ** 2).sum()), xp.sqrt((xp.abs(ifftn(self.Omega_nu ** r * fft_h)) ** 2).sum())
if __name__ == "__main__":
main()