1 | #!/usr/bin/env python |
---|
2 | from PyQt5.QtCore import pyqtSignal, QThread |
---|
3 | from math import floor |
---|
4 | from palm_gf_files.palm_gf_conf import cfg_read |
---|
5 | from datetime import datetime |
---|
6 | import sqlite3 |
---|
7 | |
---|
8 | |
---|
9 | class MyThread(QThread): |
---|
10 | change_value = pyqtSignal(int) |
---|
11 | completed = pyqtSignal(int, int) |
---|
12 | busy = pyqtSignal() |
---|
13 | |
---|
14 | def run(self): |
---|
15 | # has to be defined in QThread class |
---|
16 | # starts calculation |
---|
17 | self.mainroutine() |
---|
18 | |
---|
19 | def mainroutine(self): |
---|
20 | # determines all possible solutions for given parameters |
---|
21 | start = datetime.now() |
---|
22 | conn = sqlite3.connect("palm_gf_data.db") |
---|
23 | c = conn.cursor() |
---|
24 | c.execute("DROP TABLE IF EXISTS grid_current") |
---|
25 | c.execute("DROP TABLE IF EXISTS grid_limits") |
---|
26 | c.execute("CREATE TABLE IF NOT EXISTS grid_current(nx INT, ny INT, nz INT, npex INT, npey INT, pxpy FLOAT," |
---|
27 | "np INT, ngpts FLOAT, nxnpex FLOAT, nynpey FLOAT)") |
---|
28 | c.execute("CREATE TABLE IF NOT EXISTS grid_limits(nx INT, ny INT, nz INT, npex INT, npey INT, pxpy FLOAT," |
---|
29 | "np INT, ngpts FLOAT, nxnpex FLOAT, nynpey FLOAT)") |
---|
30 | conn.commit() |
---|
31 | var = cfg_read() |
---|
32 | |
---|
33 | for j in range(12, 17): |
---|
34 | if var[j] == "True": |
---|
35 | var[j] = True |
---|
36 | else: |
---|
37 | var[j] = False |
---|
38 | |
---|
39 | nx_min = int(var[0]) |
---|
40 | nx_max = int(var[1]) |
---|
41 | ny_min = int(var[2]) |
---|
42 | ny_max = int(var[3]) |
---|
43 | nz_min = int(var[4]) |
---|
44 | nz_max = int(var[5]) |
---|
45 | procs_min = int(var[6]) |
---|
46 | procs_max = int(var[7]) |
---|
47 | tpn = int(var[8]) |
---|
48 | dnxny = float(var[9]) |
---|
49 | dpxpy = float(var[10]) |
---|
50 | dpxpy_dev = int(var[11]) |
---|
51 | poisfft = var[12] |
---|
52 | switch = var[13] |
---|
53 | temperton = var[14] |
---|
54 | mlt_grid = var[15] |
---|
55 | spectr = var[16] |
---|
56 | |
---|
57 | np_used = procs_min |
---|
58 | counter = 0 |
---|
59 | cnt = 0 |
---|
60 | nx = nx_min |
---|
61 | ny = ny_min |
---|
62 | nz = nz_min |
---|
63 | |
---|
64 | def factors(n): |
---|
65 | # prime factorization |
---|
66 | result = [] |
---|
67 | for i in range(2, n + 1): |
---|
68 | s = 0 |
---|
69 | while n / i == floor(n / float(i)): |
---|
70 | n = n / float(i) |
---|
71 | s += 1 |
---|
72 | if s > 0: |
---|
73 | for k in range(s): |
---|
74 | result.append(i) |
---|
75 | if n == 1: |
---|
76 | return result |
---|
77 | |
---|
78 | def temperton_check(nx_, ny_): |
---|
79 | # uses previously defined prime factorization and determines if ny+1/nx+1 is multiple of 2,3,5 |
---|
80 | nx_list = factors(nx_ + 1) |
---|
81 | ny_list = factors(ny_ + 1) |
---|
82 | return_value = 1 |
---|
83 | i = 0 |
---|
84 | if nx_list is not None: |
---|
85 | while i < len(nx_list): |
---|
86 | if not (nx_list[i] == 2 or nx_list[i] == 3 or nx_list[i] == 5): |
---|
87 | return_value = 0 |
---|
88 | i += 1 |
---|
89 | j = 0 |
---|
90 | if ny_list is not None: |
---|
91 | while j < len(ny_list): |
---|
92 | if not (ny_list[j] == 2 or ny_list[j] == 3 or ny_list[j] == 5): |
---|
93 | return_value = 0 |
---|
94 | j += 1 |
---|
95 | |
---|
96 | if nx_list is None: |
---|
97 | return_value = 0 |
---|
98 | if ny_list is None: |
---|
99 | return_value = 0 |
---|
100 | |
---|
101 | return return_value |
---|
102 | # * * * |
---|
103 | |
---|
104 | while np_used <= procs_max: |
---|
105 | a = 1 |
---|
106 | while a <= np_used: |
---|
107 | if cnt != ((np_used - procs_min) / (procs_max - procs_min)) * 100: |
---|
108 | cnt = ((np_used - procs_min) / (procs_max - procs_min)) * 100 |
---|
109 | self.change_value.emit(cnt) |
---|
110 | procs_var = np_used % a |
---|
111 | if procs_var != 0: |
---|
112 | a += 1 |
---|
113 | elif procs_var == 0: |
---|
114 | npex = a |
---|
115 | npey = int(np_used / npex) |
---|
116 | |
---|
117 | if tpn != 0: |
---|
118 | if np_used % tpn != 0: |
---|
119 | a += 1 |
---|
120 | continue |
---|
121 | |
---|
122 | if dpxpy != 0.: |
---|
123 | if (dpxpy - dpxpy_dev*dpxpy) > (float(npex)/float(npey)) or \ |
---|
124 | (float(npex)/float(npey)) > (dpxpy + dpxpy_dev*dpxpy): |
---|
125 | a += 1 |
---|
126 | continue |
---|
127 | |
---|
128 | # if dpxpy != 0. and float(npex) / float(npey) != dpxpy: |
---|
129 | # a += 1 |
---|
130 | # continue |
---|
131 | |
---|
132 | while nx <= nx_max: |
---|
133 | if (nx + 1) % npex != 0: |
---|
134 | nx += 1 |
---|
135 | continue |
---|
136 | |
---|
137 | if mlt_grid is True and nx % 2 != 0: |
---|
138 | nx += 1 |
---|
139 | continue |
---|
140 | |
---|
141 | if switch is True and (nx + 1) % npey != 0: |
---|
142 | nx += 1 |
---|
143 | continue |
---|
144 | |
---|
145 | if npex > nx: |
---|
146 | nx += 1 |
---|
147 | continue |
---|
148 | |
---|
149 | while ny <= ny_max: |
---|
150 | |
---|
151 | if dnxny != 0. and float(nx) / float(ny) != float(dnxny): |
---|
152 | ny += 1 |
---|
153 | continue |
---|
154 | |
---|
155 | if (ny + 1) % npey != 0: |
---|
156 | ny += 1 |
---|
157 | continue |
---|
158 | |
---|
159 | if mlt_grid is True and ny % 2 != 0: |
---|
160 | ny += 1 |
---|
161 | continue |
---|
162 | |
---|
163 | if switch is True and (ny + 1) % npex != 0: |
---|
164 | ny += 1 |
---|
165 | continue |
---|
166 | |
---|
167 | if npey > ny: |
---|
168 | ny += 1 |
---|
169 | continue |
---|
170 | |
---|
171 | while nz <= nz_max: |
---|
172 | |
---|
173 | if mlt_grid is True and nz % 2 != 0: |
---|
174 | nz += 1 |
---|
175 | continue |
---|
176 | |
---|
177 | if poisfft is True and nz % npex != 0: |
---|
178 | nz += 1 |
---|
179 | continue |
---|
180 | |
---|
181 | if spectr is True and nz % npey != 0: |
---|
182 | nz += 1 |
---|
183 | continue |
---|
184 | |
---|
185 | if temperton is True and nx > 1 and ny > 1 and temperton_check(nx, ny) == 0: |
---|
186 | nz += 1 |
---|
187 | continue |
---|
188 | |
---|
189 | npxnpy = format(float(npex) / float(npey), '.2f') |
---|
190 | nxnpex = float(nx + 1) / float(npex) |
---|
191 | nynpey = float(ny + 1) / float(npey) |
---|
192 | |
---|
193 | c.execute("""INSERT OR REPLACE INTO grid_current(nx, ny, nz, npex, npey, pxpy, |
---|
194 | np, ngpts, nxnpex, nynpey) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)""", |
---|
195 | (nx, ny, nz, npex, npey, npxnpy, (npex * npey), (nx * ny * nz), |
---|
196 | nxnpex, nynpey)) |
---|
197 | |
---|
198 | counter += 1 |
---|
199 | nz += 1 |
---|
200 | nz = nz_min |
---|
201 | ny += 1 |
---|
202 | ny = ny_min |
---|
203 | nx += 1 |
---|
204 | nx = nx_min |
---|
205 | a += 1 |
---|
206 | np_used += 1 |
---|
207 | conn.commit() |
---|
208 | |
---|
209 | conn.commit() |
---|
210 | self.busy.emit() |
---|
211 | # read out limits of data in .db |
---|
212 | try: |
---|
213 | c.execute("SELECT nx FROM grid_current ORDER BY nx DESC LIMIT 1") |
---|
214 | mx_nx = c.fetchone()[0] |
---|
215 | c.execute("SELECT nx FROM grid_current ORDER BY nx LIMIT 1") |
---|
216 | mn_nx = c.fetchone()[0] |
---|
217 | c.execute("SELECT ny FROM grid_current ORDER BY ny DESC LIMIT 1") |
---|
218 | mx_ny = c.fetchone()[0] |
---|
219 | c.execute("SELECT ny FROM grid_current ORDER BY ny LIMIT 1") |
---|
220 | mn_ny = c.fetchone()[0] |
---|
221 | c.execute("SELECT nz FROM grid_current ORDER BY nz DESC LIMIT 1") |
---|
222 | mx_nz = c.fetchone()[0] |
---|
223 | c.execute("SELECT nz FROM grid_current ORDER BY nz LIMIT 1") |
---|
224 | mn_nz = c.fetchone()[0] |
---|
225 | c.execute("SELECT npex FROM grid_current ORDER BY npex DESC LIMIT 1") |
---|
226 | mx_npex = c.fetchone()[0] |
---|
227 | c.execute("SELECT npex FROM grid_current ORDER BY npex LIMIT 1") |
---|
228 | mn_npex = c.fetchone()[0] |
---|
229 | c.execute("SELECT npey FROM grid_current ORDER BY npey DESC LIMIT 1") |
---|
230 | mx_npey = c.fetchone()[0] |
---|
231 | c.execute("SELECT npey FROM grid_current ORDER BY npey LIMIT 1") |
---|
232 | mn_npey = c.fetchone()[0] |
---|
233 | c.execute("SELECT pxpy FROM grid_current ORDER BY pxpy DESC LIMIT 1") |
---|
234 | mx_npxnpy = c.fetchone()[0] |
---|
235 | c.execute("SELECT pxpy FROM grid_current ORDER BY pxpy LIMIT 1") |
---|
236 | mn_npxnpy = c.fetchone()[0] |
---|
237 | c.execute("SELECT np FROM grid_current ORDER BY np DESC LIMIT 1") |
---|
238 | mx_np = c.fetchone()[0] |
---|
239 | c.execute("SELECT np FROM grid_current ORDER BY np LIMIT 1") |
---|
240 | mn_np = c.fetchone()[0] |
---|
241 | c.execute("SELECT ngpts FROM grid_current ORDER BY ngpts DESC LIMIT 1") |
---|
242 | mx_ngpts = c.fetchone()[0] |
---|
243 | c.execute("SELECT ngpts FROM grid_current ORDER BY ngpts LIMIT 1") |
---|
244 | mn_ngpts = c.fetchone()[0] |
---|
245 | c.execute("SELECT nxnpex FROM grid_current ORDER BY nxnpex DESC LIMIT 1") |
---|
246 | mx_nxpex = c.fetchone()[0] |
---|
247 | c.execute("SELECT nxnpex FROM grid_current ORDER BY nxnpex LIMIT 1") |
---|
248 | mn_nxpex = c.fetchone()[0] |
---|
249 | c.execute("SELECT nynpey FROM grid_current ORDER BY nynpey DESC LIMIT 1") |
---|
250 | mx_nypey = c.fetchone()[0] |
---|
251 | c.execute("SELECT nynpey FROM grid_current ORDER BY nynpey LIMIT 1") |
---|
252 | mn_nypey = c.fetchone()[0] |
---|
253 | |
---|
254 | conn.commit() |
---|
255 | c.execute( |
---|
256 | """INSERT OR REPLACE INTO grid_limits(nx, ny, nz, npex, npey, |
---|
257 | pxpy, np, ngpts, nxnpex, nynpey) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)""", |
---|
258 | (mn_nx, mn_ny, mn_nz, mn_npex, mn_npey, mn_npxnpy, mn_np, mn_ngpts, mn_nxpex, mn_nypey)) |
---|
259 | |
---|
260 | c.execute( |
---|
261 | """INSERT OR REPLACE INTO grid_limits(nx, ny, nz, npex, npey, |
---|
262 | pxpy, np, ngpts, nxnpex, nynpey) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)""", |
---|
263 | (mx_nx, mx_ny, mx_nz, mx_npex, mx_npey, mx_npxnpy, mx_np, mx_ngpts, mx_nxpex, mx_nypey)) |
---|
264 | conn.commit() |
---|
265 | |
---|
266 | c.close() |
---|
267 | conn.close() |
---|
268 | except TypeError: |
---|
269 | pass |
---|
270 | |
---|
271 | finish = datetime.now() |
---|
272 | timedelta = finish - start |
---|
273 | timer = (int(timedelta.seconds)) |
---|
274 | self.completed.emit(counter, timer) |
---|