-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolver.py
82 lines (78 loc) · 3.02 KB
/
solver.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
import numpy as np
class solver:
def __init__(self, smoother, tol=None, verbose=False):
self.smoother = smoother
self.tol = tol
self.verbose = verbose
def sweep(self, equation, current, rhs, **kwargs):
current = self.smoother(equation, current, rhs, **kwargs)
return current
# current, rhs,
def detailed_solve(self, equation, current, rhs, **kwargs):
if self.tol == None:
J = int(np.log2(len(current[0])-1))
tol = 2**(-2*J)
else:
tol = self.tol
d = equation.rhs_defects(current, rhs)
i = 0
N = equation.n_equations
if self.verbose:
print('Tolerance is {:1.2}'.format(tol))
print('Iteration', i)
if N == 2:
print(' Defect 1 = {:1.2}, Defect 2 = {:1.2}'.format(d[0], d[1]))
if N == 1:
print(' Defect = {:1.2}'.format(d[0]))
if N == 1:
while d[0]>tol:
current = self.sweep(equation, current, rhs, **kwargs)
i+=1
d = equation.rhs_defects(current, rhs)
if self.verbose:
print('Iteration', i)
print(' Defect = {:1.2}'.format(d[0]))
if N == 2:
while d[0]>tol or d[1]>tol:
current = self.sweep(equation, current, rhs, **kwargs)
i+=1
d = equation.rhs_defects(current, rhs)
if self.verbose:
print('Iteration', i)
print(' Defect 1 = {:1.2}, Defect 2 = {:1.2}'.format(d[0], d[1]))
return current
def solve(self, equation, J, **kwargs):
current = equation.produce_initial_guess(J)
rhs = equation.rhs(current)
if self.tol == None:
J = int(np.log2(len(current[0])-1))
tol = 2**(-2*J)
else:
tol = self.tol
d = equation.defects(current)
i = 0
N = equation.n_equations
if self.verbose:
print('Tolerance is {:1.2}'.format(tol))
print('Iteration', i)
if N == 2:
print(' Defect 1 = {:1.2}, Defect 2 = {:1.2}'.format(d[0], d[1]))
if N == 1:
print(' Defect = {:1.2}'.format(d[0]))
if N == 1:
while d[0]>tol:
current = self.sweep(equation, current, rhs, **kwargs)
i+=1
d = equation.defects(current)
if self.verbose:
print('Iteration', i)
print(' Defect = {:1.2}'.format(d[0]))
if N == 2:
while d[0]>tol or d[1]>tol:
current = self.sweep(equation, current, rhs, **kwargs)
i+=1
d = equation.defects(current)
if self.verbose:
print('Iteration', i)
print(' Defect 1 = {:1.2}, Defect 2 = {:1.2}'.format(d[0], d[1]))
return current