[4869] | 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 |
---|
[2116] | 7 | |
---|
| 8 | |
---|
[4869] | 9 | class MyThread(QThread): |
---|
| 10 | change_value = pyqtSignal(int) |
---|
| 11 | completed = pyqtSignal(int, int) |
---|
| 12 | busy = pyqtSignal() |
---|
[2116] | 13 | |
---|
[4869] | 14 | def run(self): |
---|
| 15 | # has to be defined in QThread class |
---|
| 16 | # starts calculation |
---|
| 17 | self.mainroutine() |
---|
[2116] | 18 | |
---|
[4869] | 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() |
---|
[2116] | 32 | |
---|
[4869] | 33 | for j in range(12, 17): |
---|
| 34 | if var[j] == "True": |
---|
| 35 | var[j] = True |
---|
| 36 | else: |
---|
| 37 | var[j] = False |
---|
[2116] | 38 | |
---|
[4869] | 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] |
---|
[2116] | 56 | |
---|
[4869] | 57 | np_used = procs_min |
---|
| 58 | counter = 0 |
---|
| 59 | cnt = 0 |
---|
| 60 | nx = nx_min |
---|
| 61 | ny = ny_min |
---|
| 62 | nz = nz_min |
---|
[2116] | 63 | |
---|
[4869] | 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 |
---|
[2116] | 77 | |
---|
[4869] | 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 |
---|
[2116] | 95 | |
---|
[4869] | 96 | if nx_list is None: |
---|
| 97 | return_value = 0 |
---|
| 98 | if ny_list is None: |
---|
| 99 | return_value = 0 |
---|
[2116] | 100 | |
---|
[4869] | 101 | return return_value |
---|
| 102 | # * * * |
---|
[2116] | 103 | |
---|
[4869] | 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: |
---|
[2116] | 112 | a += 1 |
---|
[4869] | 113 | elif procs_var == 0: |
---|
| 114 | npex = a |
---|
| 115 | npey = int(np_used / npex) |
---|
[2116] | 116 | |
---|
[4869] | 117 | if tpn != 0: |
---|
| 118 | if np_used % tpn != 0: |
---|
| 119 | a += 1 |
---|
| 120 | continue |
---|
[2116] | 121 | |
---|
[4869] | 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 |
---|
[2116] | 127 | |
---|
[4869] | 128 | # if dpxpy != 0. and float(npex) / float(npey) != dpxpy: |
---|
| 129 | # a += 1 |
---|
| 130 | # continue |
---|
[2116] | 131 | |
---|
[4869] | 132 | while nx <= nx_max: |
---|
| 133 | if (nx + 1) % npex != 0: |
---|
| 134 | nx += 1 |
---|
| 135 | continue |
---|
[2116] | 136 | |
---|
[4869] | 137 | if mlt_grid is True and nx % 2 != 0: |
---|
| 138 | nx += 1 |
---|
[2116] | 139 | continue |
---|
| 140 | |
---|
[4869] | 141 | if switch is True and (nx + 1) % npey != 0: |
---|
| 142 | nx += 1 |
---|
[2116] | 143 | continue |
---|
| 144 | |
---|
[4869] | 145 | if npex > nx: |
---|
| 146 | nx += 1 |
---|
[2116] | 147 | continue |
---|
| 148 | |
---|
[4869] | 149 | while ny <= ny_max: |
---|
[2116] | 150 | |
---|
[4869] | 151 | if dnxny != 0. and float(nx) / float(ny) != float(dnxny): |
---|
| 152 | ny += 1 |
---|
[2116] | 153 | continue |
---|
| 154 | |
---|
[4869] | 155 | if (ny + 1) % npey != 0: |
---|
| 156 | ny += 1 |
---|
[2116] | 157 | continue |
---|
| 158 | |
---|
[4869] | 159 | if mlt_grid is True and ny % 2 != 0: |
---|
| 160 | ny += 1 |
---|
[2116] | 161 | continue |
---|
| 162 | |
---|
[4869] | 163 | if switch is True and (ny + 1) % npex != 0: |
---|
| 164 | ny += 1 |
---|
| 165 | continue |
---|
[2116] | 166 | |
---|
[4869] | 167 | if npey > ny: |
---|
| 168 | ny += 1 |
---|
| 169 | continue |
---|
[2116] | 170 | |
---|
[4869] | 171 | while nz <= nz_max: |
---|
[2116] | 172 | |
---|
[4869] | 173 | if mlt_grid is True and nz % 2 != 0: |
---|
| 174 | nz += 1 |
---|
| 175 | continue |
---|
[2116] | 176 | |
---|
[4869] | 177 | if poisfft is True and nz % npex != 0: |
---|
| 178 | nz += 1 |
---|
| 179 | continue |
---|
[2116] | 180 | |
---|
[4869] | 181 | if spectr is True and nz % npey != 0: |
---|
| 182 | nz += 1 |
---|
| 183 | continue |
---|
[2116] | 184 | |
---|
[4869] | 185 | if temperton is True and nx > 1 and ny > 1 and temperton_check(nx, ny) == 0: |
---|
| 186 | nz += 1 |
---|
| 187 | continue |
---|
[2116] | 188 | |
---|
[4869] | 189 | npxnpy = format(float(npex) / float(npey), '.2f') |
---|
| 190 | nxnpex = float(nx + 1) / float(npex) |
---|
| 191 | nynpey = float(ny + 1) / float(npey) |
---|
[2116] | 192 | |
---|
[4869] | 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)) |
---|
[2116] | 197 | |
---|
[4869] | 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() |
---|
[2116] | 208 | |
---|
| 209 | conn.commit() |
---|
[4869] | 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] |
---|
[2116] | 253 | |
---|
[4869] | 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)) |
---|
[2116] | 259 | |
---|
[4869] | 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() |
---|
[2116] | 265 | |
---|
[4869] | 266 | c.close() |
---|
| 267 | conn.close() |
---|
| 268 | except TypeError: |
---|
| 269 | pass |
---|
[2116] | 270 | |
---|
[4869] | 271 | finish = datetime.now() |
---|
| 272 | timedelta = finish - start |
---|
| 273 | timer = (int(timedelta.seconds)) |
---|
| 274 | self.completed.emit(counter, timer) |
---|