Salome HOME
647c2984d0fbfaf31c8a399f1e9e51d09052a3c0
[modules/hydrosolver.git] / src / HYDROTools / boundaryConditions.py
1 import os
2
3 PRS_SEP = ","
4 BND_SEP = " "
5
6 """Preset file reader"""
7 class PresetReader():
8     def __init__(self, file_name):
9         self.file_name = file_name
10         self._errors = []
11     
12     @property
13     def errors(self):
14         return self._errors
15     
16     def __get_value(self, str):
17         value = ''
18     
19         if str.isdigit():
20             value = int(str)
21             
22         return value
23     
24     def read(self):
25         del self._errors[:]
26         
27         presets = {}
28         
29         try:
30             with open(self.file_name) as f:
31                 # Read presets line by line
32                 line_number = 1
33                 groups = []
34                 for line in f:
35                     cstr = " ".join(line.split())
36                     values = cstr.strip().split(PRS_SEP)
37                     #print values
38                     if len(values) != 5:
39                         self._errors.append("Line #%s: wrong number of values in the preset." % line_number)
40                     elif values[0].strip() == "":
41                         self._errors.append("Line #%s: empty name of the preset." % line_number)
42                     elif values[0] in presets:
43                         self._errors.append("Line #%s: preset name %s is already used." % (line_number, values[0]))
44                     else:
45                         name = values[0]
46                         lihbor = self.__get_value(values[1])
47                         liubor = self.__get_value(values[2])
48                         livbor = self.__get_value(values[3])
49                         litbor = self.__get_value(values[4])
50                         
51                         presets[name] = (lihbor, liubor, livbor, litbor)
52                         #print name, presets[name]
53             #print presets.keys()
54         except IOError as err:
55             self._errors.append(err.strerror)
56         return presets    
57
58 """Boundary condition object"""
59 class BoundaryCondition():
60     def __init__(self, lihbor, liubor, livbor, litbor, group):
61         self.lihbor = lihbor
62         self.liubor = liubor
63         self.livbor = livbor
64         self.litbor = litbor
65         self.group = group
66         
67     def get_min(self):
68         return min( self.lihbor, self.liubor, self.livbor,  self.litbor )
69         
70     def get_max(self):
71         return max( self.lihbor, self.liubor, self.livbor,  self.litbor )
72         
73     def set_range(self, min_val, max_val):
74         self.lihbor = min(self.lihbor, max_val)
75         self.lihbor = max(self.lihbor, min_val)
76         self.liubor = min(self.liubor, max_val)
77         self.liubor = max(self.liubor, min_val)
78         self.livbor = min(self.livbor, max_val)
79         self.livbor = max(self.livbor, min_val)
80         self.litbor = min(self.litbor, max_val)
81         self.litbor = max(self.litbor, min_val)
82         
83 """Boundary conditions file reader"""
84 class BoundaryConditionReader():
85     def __init__(self, file_name):
86         self.file_name = file_name
87         self._errors = []
88     
89     def __get_condition(self, str):
90         condition = None
91                 
92         try:
93             cstr = " ".join(str.split())
94             values = cstr.strip().split(BND_SEP)
95             
96             if len(values) == 5:
97                lihbor = int(values[0])
98                liubor = int(values[1])
99                livbor = int(values[2])
100                litbor = int(values[3])
101                group = values[4]
102                
103                condition = BoundaryCondition(lihbor, liubor, livbor, litbor, group)
104         except ValueError:
105             pass
106                 
107         return condition    
108     
109     @property
110     def errors(self):
111         return self._errors
112        
113     def read(self):
114         del self._errors[:]
115         
116         conditions = []
117         
118         try:
119             with open(self.file_name) as f:
120                 # Read the number of conditions
121                 nb_conditions = 0
122                 first_line = f.readline().strip()
123                 if not first_line.isdigit():
124                     self._errors.append("First line is not a number.")
125                 else:
126                     nb_conditions = int(first_line)
127                         
128                 # Read conditions line by line
129                 line_number = 2
130                 cnd_count = 0
131                 groups = []
132                 for line in f:
133                     cnd = self.__get_condition(line)
134                 
135                     if cnd:
136                         if groups.count(cnd.group) == 0:
137                             if cnd.get_min() < 0 or cnd.get_max() > 6:
138                                 cnd.set_range(0, 6)
139                                 self._errors.append("Line #%s: values out of range [0, 6] have been adjusted." % line_number)
140                         
141                             conditions.append(cnd)
142                             groups.append(cnd.group)
143                             cnd_count += 1
144                         else:
145                             self._errors.append("Line #%s: group name '%s' is already used." % (line_number, cnd.group))
146                     else:
147                         self._errors.append("Line #%s: wrong format." % line_number)
148                 
149                     line_number += 1
150                 
151                 if cnd_count != nb_conditions:
152                     self._errors.append("Number of conditions does not match (%s instead of %s)" %  (cnd_count, nb_conditions))
153         except IOError as err:
154             self._errors.append(err.strerror)
155         return conditions
156         
157 """Boundary conditions file writer"""
158 class BoundaryConditionWriter():
159     def __init__(self, file_name):
160         self.file_name = file_name
161         self._errors = []
162     
163     def __get_string(self, condition):
164         lihbor = str(condition.lihbor)
165         liubor = str(condition.liubor)
166         livbor = str(condition.livbor)
167         litbor = str(condition.litbor)
168         values = (lihbor, liubor, livbor, litbor, condition.group)
169         line = BND_SEP.join(values)
170                 
171         return line
172     
173     @property
174     def errors(self):
175         return self._errors
176        
177     def write(self, conditions):
178         del self._errors[:]
179     
180         try:
181             with open(self.file_name, 'w') as f:
182                 lines = []
183             
184                 # The number of conditions
185                 lines.append(str(len(conditions)) + '\n')
186                         
187                 # Conditions
188                 for cnd in conditions:
189                     lines.append(self.__get_string(cnd) + '\n')
190                     
191                 # Write
192                 if len(lines) > 1:
193                     f.writelines(lines)
194         except IOError as err:
195             self._errors.append(err.strerror)           
196