]> SALOME platform Git repositories - modules/hydrosolver.git/blob - src/HYDROTools/boundaryConditions.py
Salome HOME
Lot4: add boundary conditions reader/writer, presets reader and tests
[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 = None
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                 
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 presets.has_key(values[0]):
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         except IOError as err:
53             self._errors.append(err.strerror)
54                 
55         return presets    
56
57 """Boundary condition object"""
58 class BoundaryCondition():
59     def __init__(self, lihbor, liubor, livbor, litbor, group):
60         self.lihbor = lihbor
61         self.liubor = liubor
62         self.livbor = livbor
63         self.litbor = litbor
64         self.group = group
65         
66 """Boundary conditions file reader"""
67 class BoundaryConditionReader():
68     def __init__(self, file_name):
69         self.file_name = file_name
70         self._errors = []
71     
72     def __get_condition(self, str):
73         condition = None
74                 
75         try:
76             cstr = " ".join(str.split())
77             values = cstr.strip().split(BND_SEP)
78             
79             if len(values) == 5:
80                lihbor = int(values[0])
81                liubor = int(values[1])
82                livbor = int(values[2])
83                litbor = int(values[3])
84                group = values[4]
85                
86                condition = BoundaryCondition(lihbor, liubor, livbor, litbor, group)
87         except ValueError:
88             pass
89                 
90         return condition    
91     
92     @property
93     def errors(self):
94         return self._errors
95        
96     def read(self):
97         del self._errors[:]
98         
99         conditions = []
100         
101         try:
102             with open(self.file_name) as f:
103                 # Read the number of conditions
104                 nb_conditions = 0
105                 first_line = f.readline().strip()
106                 if not first_line.isdigit():
107                     self._errors.append("First line is not a number.")
108                 else:
109                     nb_conditions = int(first_line)
110                         
111                 # Read conditions line by line
112                 line_number = 2
113                 cnd_count = 0
114                 groups = []
115                 for line in f:
116                     cnd = self.__get_condition(line)
117                 
118                     if cnd:
119                         if groups.count(cnd.group) == 0:
120                             conditions.append(cnd)
121                             groups.append(cnd.group)
122                             cnd_count += 1
123                         else:
124                             self._errors.append("Line #%s: group name '%s' is already used." % (line_number, cnd.group))
125                     else:
126                         self._errors.append("Line #%s: wrong format." % line_number)
127                 
128                     line_number += 1
129                 
130                 if cnd_count != nb_conditions:
131                     self._errors.append("Number of conditions does not match (%s instead of %s)" %  (cnd_count, nb_conditions))
132         except IOError as err:
133             self._errors.append(err.strerror)
134                 
135         return conditions
136         
137 """Boundary conditions file writer"""
138 class BoundaryConditionWriter():
139     def __init__(self, file_name):
140         self.file_name = file_name
141         self._errors = []
142     
143     def __get_string(self, condition):
144         lihbor = str(condition.lihbor)
145         liubor = str(condition.liubor)
146         livbor = str(condition.livbor)
147         litbor = str(condition.litbor)
148         values = (lihbor, liubor, livbor, litbor, condition.group)
149         line = BND_SEP.join(values)
150                 
151         return line
152     
153     @property
154     def errors(self):
155         return self._errors
156        
157     def write(self, conditions):
158         del self._errors[:]
159     
160         try:
161             with open(self.file_name, 'w') as f:
162                 lines = []
163             
164                 # The number of conditions
165                 lines.append(str(len(conditions)) + '\n')
166                         
167                 # Conditions
168                 for cnd in conditions:
169                     lines.append(self.__get_string(cnd) + '\n')
170                     
171                 # Write
172                 if len(lines) > 1:
173                     f.writelines(lines)
174         except IOError as err:
175             self._errors.append(err.strerror)           
176