PSCF v1.5.0
field.py
1"""! Module for parsing field files. """
2
3
105class Field:
106
107
112 def __init__(self, filename):
113 self.data = []
114 self.header = {}
115 self.type = ''
116 with open(filename) as f:
117 self.read(f)
118
119
129 def read(self, openFile):
130 line = openFile.readline()
131 l = line.split()
132 if l[0] == 'format':
133 self.header['format'] = [getValue(l[1]), getValue(l[2])]
134 elif l[0] == 'nx':
135 self.type ='fd1d'
136 self.header['nx'] = getValue(l[1])
137 else:
138 raise Exception('Not valid field file.')
139
140 if self.type != 'fd1d':
141 lineCount = 1
142 while lineCount <= 15:
143 line = openFile.readline()
144 l = line.split()
145 lineCount += 1
146 if lineCount%2 == 0:
147 name = l[0]
148 if name == 'N_basis' or name == "N_star":
149 self.type = 'basis'
150 else:
151 if name == 'mesh' or name == 'ngrid' or name == 'cell_param':
152 d = []
153 for i in range(0, len(l)):
154 d.append(getValue(l[i]))
155 self.header[name] = d
156 else:
157 self.header[name] = getValue(l[0])
158 else:
159 line = openFile.readline()
160 l = line.split()
161 self.header[l[0]] = getValue(l[1])
162 line = openFile.readline()
163 l = line.split()
164
165 while line != '':
166 d = []
167 for i in range(0, len(l)):
168 d.append(getValue(l[i]))
169 self.data.append(d)
170 line = openFile.readline()
171 l = line.split()
172
173
174 if self.type != 'basis' and self.type != 'fd1d':
175 if type(self.data[0][0]) is int:
176 self.type = 'kgrid'
177 else:
178 self.type = 'rgrid'
179
180
186 def __str__(self):
187 out = ''
188 for x, y in self.header.items():
189 if self.type == 'fd1d':
190 o = f'{x:<7}' + str(y) + '\n'
191 else:
192 if x == 'format':
193 o = x + ' ' + str(y[0]) + ' ' + str(y[1]) + '\n'
194 else:
195 o = x + '\n'
196 if x == 'cell_param':
197 val = f'{y[0]:.10e}'
198 o += f'{val:>20}'
199 if len(y) == 1:
200 o += '\n'
201 for i in range(1, len(y)):
202 val = f'{y[i]:.10e}'
203 o += f'{val:>18}'
204 if i == len(y)-1:
205 o += '\n'
206 elif x == 'mesh' or x == 'ngrid':
207 o += f'{y[0]:>20}'
208 if len(y) == 1:
209 o += '\n'
210 for i in range(1, len(y)):
211 o += f'{y[i]:>8}'
212 if i == len(y)-1:
213 o += '\n'
214 else:
215 o += f'{str(y):>20s}' + '\n'
216
217 out += o
218
219 for i in range(0, len(self.data)):
220 row = ''
221 for j in range(0, len(self.data[0])):
222 if self.type == 'fd1d':
223 if j == 0:
224 row += f'{self.data[i][j]:>6}'
225 else:
226 val = f'{self.data[i][j]:.11e}'
227 row += f'{val:>20}'
228
229 if self.type == 'basis':
230 if j < self.header['N_monomer']:
231 val = f'{self.data[i][j]:.10e}'
232 row += f'{val:>20}'
233 else:
234 # Each integer column is preceded by a space, so that
235 # columns stay separated when a value fills its width
236 if j == self.header['N_monomer']:
237 row += ' '
238 row += ' ' + f'{self.data[i][j]:>4}'
239
240 if self.type == 'rgrid':
241 val = f'{self.data[i][j]:.15e}'
242 row += ' ' + f'{val:>22}'
243
244 if self.type == 'kgrid':
245 if j == 0:
246 row += f'{self.data[i][j]:>6}'
247 else:
248 val = f'{self.data[i][j]:.12e}'
249 if j%2 != 0:
250 row += ' ' + f'{val:>21}'
251 else:
252 row += ' ' + f'{val:>19}'
253
254 out += row + '\n'
255
256 return out
257
258
267 def write(self, filename):
268 with open(filename, 'w') as f:
269 f.write(self.__str__())
270
271
291 def addColumn(self, index, element):
292 if isinstance(element, list):
293 if not (len(element) == len(self.data)):
294 raise Exception('Incorrect dimension for parameter element')
295 for i in range(0, len(self.data)):
296 self.data[i].insert(index, element[i])
297 else:
298 for i in range(0, len(self.data)):
299 self.data[i].insert(index, element)
300
301
310 def deleteColumn(self, index):
311 for i in range(0, len(self.data)):
312 self.data[i].pop(index)
313
314
351 def reorder(self, order):
352 nc = len(order)
353 newData = []
354 for i in range(0, len(self.data)):
355 if not (nc == len(self.data[i])):
356 raise Exception('Incorrect length for permutation order')
357 d = []
358 for j in range(0,len(order)):
359 d.append(self.data[i][order[j]])
360 newData.append(d)
361 self.data = newData
362
363
376def getValue(v):
377 if (v.isdigit() == True) or (v[0] == '-' and v[1:].isdigit() == True):
378 val = int(v)
379 else:
380 try:
381 val = float(v)
382 except ValueError:
383 val = str(v)
384
385 return val
386
Container for data in a PSCF field file.
Definition field.py:105
addColumn(self, index, element)
Add a new column to the data list.
Definition field.py:291
read(self, openFile)
Read and parse the passed-in file.
Definition field.py:129
write(self, filename)
Write out field to a file.
Definition field.py:267
deleteColumn(self, index)
Delete an existing column from the data list.
Definition field.py:310
__str__(self)
Return string representation of this Field.
Definition field.py:186
reorder(self, order)
Reorder the data list.
Definition field.py:351
__init__(self, filename)
Constructor.
Definition field.py:112
getValue(v)
Distinguish the correct type of the value from a string.
Definition field.py:376