|
| 1 | +""" |
| 2 | +Derived module from filehandler.py to handle LS-DYNA keyword (.k) files. |
| 3 | +""" |
| 4 | +import numpy as np |
| 5 | +import pygem.filehandler as fh |
| 6 | + |
| 7 | + |
| 8 | +class KHandler(fh.FileHandler): |
| 9 | + """ |
| 10 | + LS-Dyna keyword file handler class |
| 11 | +
|
| 12 | + :cvar string infile: name of the input file to be processed. |
| 13 | + :cvar string outfile: name of the output file where to write in. |
| 14 | + :cvar list extensions: extensions of the input/output files. It is equal |
| 15 | + to '.k'. |
| 16 | + """ |
| 17 | + def __init__(self): |
| 18 | + super(KHandler, self).__init__() |
| 19 | + self.extensions = ['.k'] |
| 20 | + |
| 21 | + def parse(self, filename): |
| 22 | + """ |
| 23 | + Method to parse the file `filename`. It returns a matrix with all the |
| 24 | + coordinates. It reads only the section *NODE of the k files. |
| 25 | +
|
| 26 | + :param string filename: name of the input file. |
| 27 | +
|
| 28 | + :return: mesh_points: it is a `n_points`-by-3 matrix containing the |
| 29 | + coordinates of the points of the mesh. |
| 30 | + :rtype: numpy.ndarray |
| 31 | + """ |
| 32 | + self._check_filename_type(filename) |
| 33 | + self._check_extension(filename) |
| 34 | + self.infile = filename |
| 35 | + index = -9 |
| 36 | + mesh_points = [] |
| 37 | + with open(self.infile, 'r') as input_file: |
| 38 | + for num, line in enumerate(input_file): |
| 39 | + if line.startswith('*NODE'): |
| 40 | + index = num |
| 41 | + if num == index + 1: |
| 42 | + if line.startswith('$'): |
| 43 | + index = num |
| 44 | + elif line.startswith('*'): |
| 45 | + index = -9 |
| 46 | + else: |
| 47 | + l = [] |
| 48 | + l.append(float(line[8:24])) |
| 49 | + l.append(float(line[24:40])) |
| 50 | + l.append(float(line[40:56])) |
| 51 | + mesh_points.append(l) |
| 52 | + index = num |
| 53 | + mesh_points = np.array(mesh_points) |
| 54 | + return mesh_points |
| 55 | + |
| 56 | + def write(self, mesh_points, filename): |
| 57 | + """ |
| 58 | + Writes a .k file, called filename, copying all the lines from |
| 59 | + self.filename but the coordinates. mesh_points is a matrix that |
| 60 | + contains the new coordinates to write in the .k file. |
| 61 | +
|
| 62 | + :param numpy.ndarray mesh_points: it is a `n_points`-by-3 matrix |
| 63 | + containing the coordinates of the points of the mesh |
| 64 | + :param string filename: name of the output file. |
| 65 | + """ |
| 66 | + self._check_filename_type(filename) |
| 67 | + self._check_extension(filename) |
| 68 | + self._check_infile_instantiation() |
| 69 | + self.outfile = filename |
| 70 | + index = -9 |
| 71 | + i = 0 |
| 72 | + with open(self.outfile, 'w') as output_file: |
| 73 | + with open(self.infile, 'r') as input_file: |
| 74 | + for num, line in enumerate(input_file): |
| 75 | + if line.startswith('*NODE'): |
| 76 | + index = num |
| 77 | + if num == index + 1: |
| 78 | + if line.startswith('$'): |
| 79 | + index = num |
| 80 | + elif line.startswith('*'): |
| 81 | + index = -9 |
| 82 | + else: |
| 83 | + for j in range(0, 3): |
| 84 | + line = line[:8+16*(j)] + '{:16.10f}'.format(mesh_points[i][j]) + line[8+16*(j+1):] |
| 85 | + i += 1 |
| 86 | + index = num |
| 87 | + output_file.write(line) |
0 commit comments