-
Notifications
You must be signed in to change notification settings - Fork 0
/
ksp_monitor.py
131 lines (117 loc) · 3.9 KB
/
ksp_monitor.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
import numpy as np
import time
class KSPMonitor(object):
'''KSP Monitor writing to stdout.
:arg label: Name of solver
:arg verbose: verbosity. 0=print nothing, 1=only print summary of results, 2=print everything in detail
'''
def __init__(self,label='',verbose=2,comm=None):
self.label = label
self.verbose = verbose
self.initial_residual = 1.0
self.iterations = []
self.resnorm = []
self.t_start = 0.0
self.t_start_iter = 0.0
self.t_finish = 0.0
self.its = 0
self.niter= []
self._comm = comm
self.list_of_norms = []
'''Call logger.
This method is called by the KSP class and should write the output.
:arg ksp: The calling ksp instance
:arg its: The current iterations
:arg rnorm: The current residual norm
'''
def __call__(self,ksp,its,rnorm):
if (its==0):
self.rnorm0 = rnorm
self.its-=1
if (self.verbose>=2):
print()
if (self.verbose>=2):
s = ' KSP '+('%20s' % self.label)
s += (' %6d' % its)+' : '
s += (' %10.6e' % rnorm)
s += (' %10.6e' % (rnorm/self.rnorm0))
if (its > 0):
s += (' %8.4f' % (rnorm/self.rnorm_old))
else:
s += ' ----'
self._print(s)
self.its += 1
if (self.its == 1):
self.t_start_iter = time.process_time()
self.rnorm = rnorm
self.iterations.append(its)
self.resnorm.append(rnorm)
self.rnorm_old = rnorm
def __enter__(self):
'''Print information at beginning of iteration.
'''
self.iterations = []
self.resnorm = []
self.its = 0
if (self.verbose >= 1):
self._print('')
if (self.verbose == 2):
s = ' KSP '+('%20s' % self.label)
s += ' iter rnrm rnrm/rnrm_0 rho'
self._print(s)
self.t_start = time.process_time()
self.rnorm = 1.0
self.rnorm0 = 1.0
self.rnorm_old = 1.0
return self
def __exit__(self,*exc):
'''Print information at end of iteration.
'''
self.t_finish = time.process_time()
niter = self.its
if (niter == 0):
niter = 1
if (self.verbose == 1):
s = ' KSP '+('%20s' % self.label)
s += ' iter rnrm rnrm/rnrm_0 rho_avg'
self._print(s)
s = ' KSP '+('%20s' % self.label)
s += (' %6d' % niter)+' : '
s += (' %10.6e' % self.rnorm)
# Hack to avoid dividing by 0
if self.rnorm0 == 0:
self.rnorm0 = 1E-16
s += (' %10.6e' % (self.rnorm/self.rnorm0))
s += (' %8.4f' % (self.rnorm/self.rnorm0)**(1./float(niter)))
self._print(s)
if (self.verbose >= 1):
t_elapsed = self.t_finish - self.t_start
t_elapsed_iter = self.t_finish - self.t_start_iter
s = ' KSP '+('%20s' % self.label)
s += (' n_iter = %4d' % niter)
s += (' t_solve = %8.4f s' % t_elapsed)
s += (' t_iter = %8.4f s' % (t_elapsed_iter/niter))
s += (' [%8.4f s' % (self.t_start_iter-self.t_start))+']'
self._print(s)
self._print('')
self.niter.append(niter)
def _print(self,*s):
'''Print only on master processor
:arg s: stuff to print
'''
if (self._comm is None):
print (*s)
else:
if (self._comm.rank == 0):
print (*s)
def add_reconstructor(self, foo):
pass
class KSPMonitorDummy(object):
def __init__(self):
pass
def __call__(self,ksp,its,rnorm):
pass
def __enter__(self):
pass
def __exit__(self,*exc):
pass