-
Notifications
You must be signed in to change notification settings - Fork 89
/
base_operator.py
126 lines (73 loc) · 3.05 KB
/
base_operator.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
from __future__ import print_function
from anuga.utilities.system_tools import log_to_file
class Operator:
"""Operator - generic structure for a fractional operator
This is the base class for all fractional step operators
"""
counter = 0
def __init__(self,
domain,
description = None,
label = None,
logging = False,
verbose = False):
self.domain = domain
self.domain.set_fractional_step_operator(self)
# useful aliases
self.stage_c = self.domain.quantities['stage'].centroid_values
self.xmom_c = self.domain.quantities['xmomentum'].centroid_values
self.ymom_c = self.domain.quantities['ymomentum'].centroid_values
self.elev_c = self.domain.quantities['elevation'].centroid_values
self.coord_c = self.domain.centroid_coordinates
self.areas = self.domain.areas
if domain.numproc > 1:
msg = 'Not implemented to run in parallel'
assert self.parallel_safe(), msg
if description is None:
self.description = ' '
else:
self.description = description
self.set_label(label)
self.verbose = verbose
# Keep count of inlet operator
Operator.counter += 1
self.set_logging(logging)
def __call__(self):
#timestep = self.domain.get_timestep()
raise Exception('Need to implement __call__ for your operator')
def get_timestep(self):
return self.domain.get_timestep()
def get_time(self):
return self.domain.get_time()
def parallel_safe(self):
"""By default an operator is not parallel safe
"""
return False
def statistics(self):
message = 'You need to implement operator statistics for your operator'
return message
def timestepping_statistics(self):
message = 'You need to implement timestepping statistics for your operator'
return message
def print_statistics(self):
print(self.statistics())
def print_timestepping_statistics(self):
print(self.timestepping_statistics())
def log_timestepping_statistics(self):
from anuga.utilities.system_tools import log_to_file
if self.logging:
log_to_file(self.log_filename, self.timestepping_statistics())
def set_label(self, label=None):
if label is None:
self.label = "operator_%g" % Operator.counter
else:
self.label = label + '_%g' % Operator.counter
def set_logging(self, flag=True):
self.logging = flag
def activate_logging(self):
# If flag is true open file with mode = "w" to form a clean file for logging
if self.logging:
self.log_filename = self.label + '.log'
log_to_file(self.log_filename, self.statistics(), mode='w')
#log_to_file(self.log_filename, 'time,Q')
#log_to_file(self.log_filename, self.culvert_type)