Skip to content

Commit a06d60f

Browse files
authored
Patch *.dat Reader (ansys#318)
* add DAT example file and update reader * added string parameter reader * version bump to 0.44.19 * ignore additional NBLOCKs * use smaller example *.dat file * add missing test file and add file closing within binary reader
1 parent 26f8597 commit a06d60f

File tree

8 files changed

+2342
-37
lines changed

8 files changed

+2342
-37
lines changed

pyansys/_version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# major, minor, patch
2-
version_info = 0, 44, 18
2+
version_info = 0, 44, 19
33

44
# Nice string for the version
55
__version__ = '.'.join(map(str, version_info))

pyansys/cython/_binary_reader.pyx

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,9 @@ def load_elements(filename, int64_t loc, int nelem, int64_t [::1] e_disp_table):
305305
# add final position here for parser to know the size of the last element
306306
elem_off[nelem] = c
307307

308+
# this isn't collected automatically, must close manually
309+
del binfile
310+
308311
return np.array(elem[:c]), np.array(elem_off)
309312

310313

@@ -341,6 +344,9 @@ def read_element_stress(filename, int64_t [::1] ele_ind_table,
341344
&ele_data_arr[c, 0], as_global)
342345
c += nnode_elem
343346

347+
# this isn't collected automatically, must close manually
348+
del binfile
349+
344350

345351
def populate_surface_element_result(filename,
346352
int64_t [::1] ele_ind_table,
@@ -473,6 +479,9 @@ def populate_surface_element_result(filename,
473479
data[cj] = 0
474480
cj += 1
475481

482+
# this isn't collected automatically, must close manually
483+
del binfile
484+
476485
return np.array(data)
477486

478487

@@ -913,11 +922,9 @@ def read_nodal_values_dist(filename,
913922

914923
c += 1 # global solution cell index
915924

916-
# must delete to manually collect
925+
# this isn't collected automatically, must close manually
917926
del binfile
918927

919-
# have to increment again
920-
# return c + 1
921928
return c
922929

923930
# indices of a wedge must be reordered (see _parser.store_weg)

pyansys/cython/_reader.pyx

Lines changed: 44 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -187,9 +187,21 @@ def read(filename, read_parameters=False, debug=False):
187187

188188
# element number and element type
189189
et_val = line.decode().split(',')
190-
elem_type.append([int(et_val[1]), int(et_val[2])])
190+
try:
191+
int(et_val[1])
192+
elem_type.append([int(et_val[1]), int(et_val[2])])
193+
except:
194+
if debug:
195+
print('Invalid "ET" command %s' % line.decode())
196+
continue
191197

192198
elif b'EBLOCK,' == line[:7] or b'eblock,' == line[:7]:
199+
if eblock_read:
200+
# Sometimes, DAT files contain two EBLOCKs. Read
201+
# only the first block.
202+
if debug:
203+
print('EBLOCK already read, skipping...')
204+
continue
193205
if debug:
194206
print('reading EBLOCK')
195207

@@ -283,6 +295,10 @@ def read(filename, read_parameters=False, debug=False):
283295
elif 'N' == line[0] or 'n' == line[0]:
284296
# if line contains the start of the node block
285297
if line[:6] == b'NBLOCK' or line[:6] == b'nblock':
298+
if nodes_read:
299+
if debug:
300+
print('Skipping additional NBLOCK')
301+
continue
286302
start_pos = n
287303
if debug:
288304
print('reading NBLOCK')
@@ -385,38 +401,36 @@ def read(filename, read_parameters=False, debug=False):
385401
elif b'ELEM' in line_comp_type:
386402
elem_comps[comname] = component_interperter(component)
387403

388-
elif '*' == line[0] and read_parameters: # dim
404+
elif '*' == line[0] and read_parameters: # maybe *DIM
389405
if b'DIM' in line:
390-
_, name, _, dim0, dim1, dim2, _ = line.decode().split(',')
391-
392-
# dim = []
393-
# for d in [dim0, dim1, dim2]:
394-
# if d.strip():
395-
# dim.append(int(d))
396-
397-
# while dim[-1] == 1:
398-
# if len(dim) == 1:
399-
# break
400-
# del dim[-1]
401-
402-
# init_arr = np.zeros(np.prod(dim))
406+
items = line.decode().split(',')
407+
if len(items) < 3:
408+
continue
403409

404-
myfgets(line, raw, &n, fsize)
405-
if b'PREAD' in line:
406-
if debug:
407-
print('reading PREAD')
410+
name = items[1]
411+
if items[2].lower() == 'string':
412+
myfgets(line, raw, &n, fsize)
413+
string_items = line.decode().split('=')
414+
if len(string_items) > 1:
415+
parameters[name] = string_items[1].replace("'", '').strip()
416+
else:
417+
parameters[name] = line.decode()
418+
elif items[2].lower() == 'array':
419+
myfgets(line, raw, &n, fsize)
420+
if b'PREAD' in line:
421+
if debug:
422+
print('reading PREAD')
408423

409-
_, name, arr_size = line.decode().split(',')
410-
name = name.strip()
411-
st = n
412-
en = raw.find(b'END PREAD', n)
413-
if debug:
414-
print(st, en)
415-
if st != -1 and en != -1:
416-
lines = raw[st:en].split()
417-
arr = np.genfromtxt(raw[st:en].split())
418-
# init_arr[arr.size] = arr
419-
parameters[name] = arr
424+
_, name, arr_size = line.decode().split(',')
425+
name = name.strip()
426+
st = n
427+
en = raw.find(b'END PREAD', n)
428+
if debug:
429+
print(st, en)
430+
if st != -1 and en != -1:
431+
lines = raw[st:en].split()
432+
arr = np.genfromtxt(raw[st:en].split())
433+
parameters[name] = arr
420434

421435
# if the node block was not read for some reason
422436
if not nodes_read:

pyansys/mapdl.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""Module to control interaction with MAPDL through Python"""
2+
import time
23
import glob
34
import re
45
import os
@@ -1244,7 +1245,8 @@ def _flush_stored(self):
12441245

12451246
self._store_commands = False
12461247
self._stored_commands = []
1247-
out = self.input(tmp_inp, write_to_log=False)
1248+
_ = self.input(tmp_inp, write_to_log=False)
1249+
time.sleep(0.1) # allow MAPDL to close the file
12481250
if os.path.isfile(tmp_out):
12491251
self._response = '\n' + open(tmp_out).read()
12501252

pyansys/parameters.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ def _write_numpy_array(self, filename, arr):
437437
self._mapdl.upload(filename, progress_bar=False)
438438

439439
def interp_star_status(status):
440-
"""Interperts \*STATUS command output from MAPDL
440+
"""Interprets \*STATUS command output from MAPDL
441441
442442
Parameters
443443
----------

tests/archive/test_archive.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
test_path = os.path.dirname(os.path.abspath(__file__))
2525
testfiles_path = os.path.join(test_path, 'test_data')
26+
DAT_FILE = os.path.join(testfiles_path, 'Panel_Transient.dat')
2627

2728

2829
@pytest.fixture(scope='module')
@@ -40,6 +41,14 @@ def all_solid_cells_archive_linear():
4041
return pyansys.Archive(os.path.join(testfiles_path, 'all_solid_cells.cdb'),
4142
force_linear=True)
4243

44+
45+
def test_load_dat():
46+
arch = pyansys.Archive(DAT_FILE, read_parameters=True)
47+
assert arch.n_node == 1263 # through inspection of the dat file
48+
assert arch.n_elem == 160 # through inspection of the dat file
49+
assert 'Panelflattern' in arch.parameters['_wb_userfiles_dir']
50+
51+
4352
def test_repr(hex_archive):
4453
assert '%s' % hex_archive.n_node in str(hex_archive)
4554
assert '%s' % hex_archive.n_elem in str(hex_archive)

0 commit comments

Comments
 (0)