PSCF v1.1
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':
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 if j == self.header['N_monomer']:
235 row += ' '
236 row += f'{self.data[i][j]:>5}'
237
238 if self.type == 'rgrid':
239 val = f'{self.data[i][j]:.15e}'
240 row += f'{val:>23}'
241
242 if self.type == 'kgrid':
243 if j == 0:
244 row += f'{self.data[i][j]:>6}'
245 else:
246 val = f'{self.data[i][j]:.12e}'
247 if j%2 != 0:
248 row += f'{val:>22}'
249 else:
250 row += f'{val:>20}'
251
252 out += row + '\n'
253
254 return out
255
256
265 def write(self, filename):
266 with open(filename, 'w') as f:
267 f.write(self.__str__())
268
269
289 def addColumn(self, index, element):
290 if isinstance(element, list):
291 if not (len(element) == len(self.data)):
292 raise Exception('Incorrect dimension for parameter element')
293 for i in range(0, len(self.data)):
294 self.data[i].insert(index, element[i])
295 else:
296 for i in range(0, len(self.data)):
297 self.data[i].insert(index, element)
298
299
308 def deleteColumn(self, index):
309 for i in range(0, len(self.data)):
310 self.data[i].pop(index)
311
312
349 def reorder(self, order):
350 nc = len(order)
351 newData = []
352 for i in range(0, len(self.data)):
353 if not (nc == len(self.data[i])):
354 raise Exception('Incorrect length for permutation order')
355 d = []
356 for j in range(0,len(order)):
357 d.append(self.data[i][order[j]])
358 newData.append(d)
359 self.data = newData
360
361
374def getValue(v):
375 if (v.isdigit() == True) or (v[0] == '-' and v[1:].isdigit() == True):
376 val = int(v)
377 else:
378 try:
379 val = float(v)
380 except ValueError:
381 val = str(v)
382
383 return val
384
Container for data in a PSCF field file.
Definition: field.py:105
def addColumn(self, index, element)
Add a new column to the data list.
Definition: field.py:289
def read(self, openFile)
Read and parse the passed-in file.
Definition: field.py:129
def __str__(self)
Return string representation of this Field.
Definition: field.py:186
def reorder(self, order)
Reorder the data list.
Definition: field.py:349
def __init__(self, filename)
Constructor.
Definition: field.py:112
def write(self, filename)
Write out field to a file.
Definition: field.py:265
def deleteColumn(self, index)
Delete an existing column from the data list.
Definition: field.py:308
def getValue(v)
Distinguish the correct type of the value from a string.
Definition: field.py:374