-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript_single_layer.py
More file actions
279 lines (215 loc) · 8.2 KB
/
script_single_layer.py
File metadata and controls
279 lines (215 loc) · 8.2 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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
import sqlite3
from copy import deepcopy
from pathlib import Path
import numpy as np
from joblib import Parallel, delayed
from GKTH.Global_Parameter import GlobalParams
from GKTH.Layer import Layer
from GKTH.self_consistency_delta import (
GKTH_self_consistency_1S_find_root,
GKTH_self_consistency_1S_iterate,
GKTH_self_consistency_1S_residual,
)
DATA_DIR = Path("data")
DATA_DIR.mkdir(exist_ok=True)
def read_residual_delta_database(query: tuple):
# Connect to the database
conn = sqlite3.connect(DATA_DIR / "residual_delta.db")
c = conn.cursor()
# Read the data from the database
c.execute(*query)
rows = c.fetchall()
# Close the connection
conn.close()
# Process the data
plot_tuples = []
Deltas = []
h_list = []
for row in rows:
h, Delta, x_vals_str, residuals_str = row
h_list.append(h)
Deltas.append(Delta)
x_vals = eval(x_vals_str)
residuals = eval(residuals_str)
plot_tuples.append((x_vals, residuals))
h_list = np.array(h_list)
Deltas = np.array(Deltas)
return h_list, Deltas, plot_tuples
def read_residuals_delta_database_mev(query: tuple):
h_list, Deltas, plot_tuples = read_residual_delta_database(query)
h_list_mev = h_list * 1e3
Deltas_mev = Deltas * 1e3
plot_tuples_mev = [
(np.array(x_vals) * 1e3, np.array(residuals) * 1e3)
for x_vals, residuals in plot_tuples
]
return h_list_mev, Deltas_mev, plot_tuples_mev
def get_delta_vs_h(_lambda):
query = (
"SELECT h, Delta, x_vals, residuals FROM results WHERE _lambda = ?",
(_lambda,),
)
h_list, Deltas, _ = read_residual_delta_database(query)
return h_list, Deltas
def get_single_layer_list(_lambda: float):
layer = Layer(_lambda=_lambda, symmetry="s")
layer.Delta_0 = 0.01
layer.tNN = -0.1523
layer.tNNN = 0
layer.mu = 0.1025
return [layer]
def get_single_layer_parameters(h: float):
return GlobalParams(h=h, a=3.30e-10, nkpoints=300)
def run_residuals_phase(_lambda: float, h_end: float, max_Delta: float, N: int):
def func(Delta, h):
# Connect to database
conn = sqlite3.connect(DATA_DIR / "residuals.db")
c = conn.cursor()
# Create table if it doesn't exist
conn.execute(
"CREATE TABLE IF NOT EXISTS residuals (lambda REAL, Delta REAL, h REAL, residual REAL)"
)
# If data is in the database, skip it
c.execute(
f"SELECT * FROM residuals WHERE lambda={_lambda} AND Delta={Delta} AND h={h}"
)
query = c.fetchone()
if query is not None:
residual = query[3]
print(f"Delta: {Delta}, h: {h}, residual: {residual}")
return
# Get result
print(
f"Starting run_residuals_phase, lambda: {_lambda}, Delta: {Delta}, h: {h}"
)
p = get_single_layer_parameters(h)
layers = get_single_layer_list(_lambda)
residual = GKTH_self_consistency_1S_residual(
Delta_0_fit=Delta, p=p, layers=layers, layers_to_check=[0]
)
# Insert into database
c.execute(f"INSERT INTO residuals VALUES ({_lambda}, {Delta}, {h}, {residual})")
conn.commit()
conn.close()
print(
f"Finished run_residuals_phase, lambda: {_lambda}, Delta: {Delta}, h: {h}, residual: {residual}"
)
Delta_lin = np.round(np.linspace(0.00, max_Delta, N), 9)
h_lin = np.round(np.linspace(0.00, h_end, N), 9)
Delta_mesh, h_mesh = np.meshgrid(Delta_lin, h_lin)
Parallel(n_jobs=-1)(
delayed(func)(Delta, h)
for Delta, h in zip(Delta_mesh.flatten(), h_mesh.flatten())
)
def get_residuals(_lambda, Delta_list, h_list):
residual_list = []
conn = sqlite3.connect(DATA_DIR / "residuals.db")
c = conn.cursor()
# Use a single query to fetch all residuals at once
placeholders = ", ".join(["?"] * len(Delta_list))
query = f"SELECT Delta, h, residual FROM residuals WHERE lambda=? AND Delta IN ({placeholders}) AND h IN ({placeholders})"
params = [_lambda] + list(Delta_list) + list(h_list)
c.execute(query, params)
# Create a dictionary for quick lookup
residual_dict = {(row[0], row[1]): row[2] for row in c.fetchall()}
for Delta, h in zip(Delta_list, h_list):
residual = residual_dict.get((Delta, h), None)
residual_list.append(residual)
conn.close()
return residual_list
def get_residuals_phase(_lambda: float, max_Delta: float, h_end: float, N: int):
Delta_lin = np.round(np.linspace(0.00, max_Delta, N), 9)
h_lin = np.round(np.linspace(0.00, h_end, N), 9)
Delta_mesh, h_mesh = np.meshgrid(Delta_lin, h_lin)
# Query residuals from database
Delta_list = Delta_mesh.flatten()
h_list = h_mesh.flatten()
residual_list = get_residuals(_lambda, Delta_list, h_list)
residual_mesh = np.array(residual_list).reshape(Delta_mesh.shape)
Delta_mesh_mev = Delta_mesh * 1e3
h_mesh_mev = h_mesh * 1e3
residual_mesh_mev = residual_mesh * 1e3
return Delta_mesh_mev, h_mesh_mev, residual_mesh_mev
def run_for_lambda(_lambda, h_end=1e-3, delta_end=2e-3):
# Round to take care of floating point errors
h_list = np.round(np.linspace(0, h_end, 21), 9)
plot_tuples = []
Deltas = []
for h in h_list:
p = get_single_layer_parameters(h)
# Connect to database
conn = sqlite3.connect(DATA_DIR / "residual_delta.db")
c = conn.cursor()
# Create table if it doesn't exist
c.execute(
"""CREATE TABLE IF NOT EXISTS results
(_lambda REAL, h REAL, Delta REAL, layers STRING, x_vals STRING, residuals STRING)"""
)
# If already in database, skip
c.execute(
"SELECT Delta FROM results WHERE _lambda = ? AND h = ?",
(_lambda, h),
)
if c.fetchone():
continue
print(f"Calculating for lambda: {_lambda}, h: {h}")
layers = get_single_layer_list(_lambda)
# See how residuals vary with Delta to check find root
x_vals, residuals = GKTH_self_consistency_1S_iterate(
p, deepcopy(layers), max_Delta=delta_end
)
plot_tuples.append((x_vals, residuals))
# Find root where residuals are zero
Delta, layers = GKTH_self_consistency_1S_find_root(
p, deepcopy(layers), max_Delta=delta_end
)
Deltas.append(Delta)
# Insert the data
layers_data = list(map(lambda l: l.__dict__, layers))
c.execute(
"INSERT INTO results (_lambda, h, Delta, layers, x_vals, residuals) VALUES (?, ?, ?, ?, ?, ?)",
(
_lambda,
h,
Delta,
repr(layers_data),
repr(x_vals.tolist()),
repr(residuals),
),
),
# Commit the changes and close the connection
conn.commit()
conn.close()
def drop_lambda(_lambda):
conn = sqlite3.connect(DATA_DIR / "residual_delta.db")
c = conn.cursor()
c.execute("DELETE FROM results WHERE _lambda = ?", (_lambda,))
conn.commit()
conn.close()
if __name__ == "__main__":
lambda_list = np.round(np.linspace(0.0, 0.1, 6), 9)
h_end_list = np.round(np.repeat(1e-3, len(lambda_list)), 9)
max_Delta_list = np.round(np.repeat(2e-3, len(lambda_list)), 9)
lambda_list = np.append(lambda_list, [0.1, 0.15, 0.2])
h_end_list = np.append(h_end_list, [1e-3, 2e-2, 5e-2])
max_Delta_list = np.append(max_Delta_list, [2e-3, 2e-2, 50e-3])
def lambda_fn(i):
run_for_lambda(lambda_list[i], h_end=h_end_list[i], delta_end=max_Delta_list[i])
# result = Parallel(n_jobs=-1)(delayed(lambda_fn)(i) for i in range(len(lambda_list)))
# (_lambda, h_end, max_Delta)
tuple_list = [
(0.1, 1e-3, 2e-3),
(0.11, 5e-3, 5e-3),
(0.12, 5e-3, 5e-3),
(0.13, 1e-2, 1e-2),
(0.14, 2e-2, 2e-2),
(0.15, 2e-2, 2e-2),
(0.16, 3e-2, 3e-2),
(0.17, 3e-2, 3e-2),
(0.18, 4e-2, 4e-2),
(0.19, 4e-2, 4e-2),
(0.20, 5e-2, 5e-2),
]
N = 41
for _lambda, h_end, max_Delta in tuple_list:
run_residuals_phase(_lambda, h_end=h_end, max_Delta=max_Delta, N=N)