Skip to content

Commit 0ea8b92

Browse files
committed
Added full file reader
1 parent dcf25ef commit 0ea8b92

File tree

10 files changed

+281
-26
lines changed

10 files changed

+281
-26
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
The MIT License (MIT)
22
Copyright (c) 2016 Alex Kaszynski
33

4-
"Software" refers to python module ANSYScdb and all python and cython source code within.
4+
"Software" refers to python module pyansys and all python and cython source code within.
55

66
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
77

MANIFEST.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
include pyansys/cython/*.h
2+

README.rst

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ pyansys
22
========
33

44
Python module to extract data from ANSYS binary files and to display
5-
them if vtk is installed. Currently only supports (*.rst) files.
5+
them if vtk is installed. Currently supports (*.rst) and (*.full) files.
66

77
Installation
88
------------
99

10-
From source directory
10+
From PyPi directory
1111

12-
``pip install .``
12+
``pip install pyansys``
1313

1414
or
1515

@@ -24,7 +24,7 @@ provided in the file named ``LICENSE``.
2424
Dependencies
2525
------------
2626

27-
Required: ``numpy``, ``ANSYScdb``. Optional: ``vtk``
27+
Required: ``numpy``, ``cython``, ``ANSYScdb``. Optional: ``vtk``
2828

2929
Minimum requirements are numpy to extract results from a results file. To
3030
convert the raw data to a VTK unstructured grid, vtk 5.0 or greater must
@@ -33,7 +33,7 @@ be installed with Python bindings.
3333
Tests
3434
-----
3535

36-
Test installation with the following from Python
36+
Test installation with the following
3737

3838
.. code:: python
3939
@@ -45,12 +45,15 @@ Test installation with the following from Python
4545
# Display first bending mode of that beam
4646
Tests.Reader.Display()
4747
48+
# Load mass and stiffness matrices from the beam
49+
Tests.Reader.LoadKM()
4850
49-
Example Code
50-
------------
5151
52-
Assumes you have the example files . Otherwise, replace
53-
‘Beam.cdb’ with your own blocked \*.cdb file.
52+
Example: Reading a Result File
53+
------------------------------
54+
This example reads in binary results from a modal analysis from ANSYS.
55+
56+
Example files can be found within the Tests folder in installation folder.
5457

5558
.. code:: python
5659
@@ -75,3 +78,35 @@ Assumes you have the example files . Otherwise, replace
7578
# Plot the displacement of Mode 41 in the x direction
7679
fobj.PlotDisplacement(40, 'x')
7780
81+
82+
Example: Reading a full file
83+
----------------------------
84+
This example reads in mass and stiffness matrices associated with `Beam.cdb`
85+
86+
Example files can be found within the Tests folder in installation folder.
87+
88+
.. code:: python
89+
90+
# Load the reader from pyansys
91+
from pyansys import Reader
92+
93+
# Create result reader object
94+
fobj = Reader.FullReader('file.full')
95+
96+
# Read in full file
97+
fobj.LoadFullKM()
98+
99+
# Data from the full file can now be accessed from the object
100+
# Can be used construct a sparse matrix and solve it
101+
102+
# from scipy.sparse import csc_matrix, linalg
103+
#ndim = fobj.nref.size
104+
#k = csc_matrix(fobj.kdata, (fobj.krows, fobj.kcols), shape=(ndim, ndim))
105+
#m = csc_matrix(fobj.kdata, (fobj.krows, fobj.kcols), shape=(ndim, ndim))
106+
# Solve
107+
#w, v = linalg.eigsh(k, k=20, M=m, sigma=10000)
108+
# System natural frequencies
109+
#f = np.sqrt(real(w))/(2*np.pi)
110+
111+
112+

pyansys/Reader.py

Lines changed: 94 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
import numpy as np
2-
from ANSYScdb import CDB_Reader
32
import warnings
43

4+
from pyansys import _parsefull
5+
6+
# Try to load optional items
7+
try:
8+
from ANSYScdb import CDB_Reader
9+
except:
10+
warnings.warn('CDB_Reader uninstalled')
11+
512
try:
613
import vtk
714
from vtk.util import numpy_support as VN
@@ -10,6 +17,92 @@
1017
except:
1118
warnings.warn('Cannot load vtk\nWill be unable to display results or load CDB.')
1219
vtkloaded = False
20+
21+
22+
class FullReader(object):
23+
"""
24+
Object to store the results of an ANSYS full file
25+
26+
NOTES:
27+
Currently only supports symmetric and real stiffness matrices as well as
28+
non-lumped mass matrices.
29+
30+
"""
31+
32+
def __init__(self, filename):
33+
"""
34+
Loads full header on initialization
35+
36+
See ANSYS programmer's reference manual full header section for
37+
definitions of each header.
38+
39+
"""
40+
41+
self.filename = filename
42+
self.header = _parsefull.ReturnHeader(filename)
43+
44+
#// Check if lumped (item 11)
45+
if self.header[11]:
46+
raise Exception("Unable to read a lumped mass matrix. Terminating.")
47+
48+
# Check if arrays are unsymmetric (item 14)
49+
if self.header[14]:
50+
raise Exception ("Unable to read an unsymmetric mass/stiffness matrix.")
51+
52+
53+
def LoadFullKM(self):
54+
"""
55+
Load indices for constructing symmetric mass and stiffness
56+
matricies
57+
58+
STORES TO SELF:
59+
60+
nref (np.int32 array)
61+
Ordered reference to original ANSYS node numbers
62+
63+
dref (np.int32 array)
64+
DOF reference where
65+
0 - x
66+
1 - y
67+
2 - z
68+
69+
krows (np.int32 array)
70+
Rows to construct the sparse stiffness matrix
71+
72+
kcols (np.int32 array)
73+
Columns to construct the sparse stiffness matrix
74+
75+
kdata (np.int32 array)
76+
Data for each entry in thesparse stiffness matrix
77+
78+
mrows (np.int32 array)
79+
Rows to construct the mass stiffness matrix
80+
81+
mcols (np.int32 array)
82+
Columns to construct the mass stiffness matrix
83+
84+
mdata (np.int32 array)
85+
Data to construct the mass stiffness matrix
86+
87+
"""
88+
data = _parsefull.ReturnFull_KM(self.filename)
89+
90+
# nodal reference
91+
self.nref = data[0]
92+
93+
# DOF reference
94+
self.dref = data[1]
95+
96+
# stiffness rows, columns, and data
97+
self.krows = data[2]
98+
self.kcols = data[3]
99+
self.kdata = data[4]
100+
101+
# stiffness rows, columns, and data
102+
self.mrows = data[5]
103+
self.mcols = data[6]
104+
self.mdata = data[7]
105+
13106

14107

15108
class ResultReader(object):

pyansys/Tests/Reader.py

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@
77

88
from pyansys import Reader
99

10-
def Load():
10+
pth = dirname(realpath(__file__))
11+
12+
def LoadResult():
1113
""" Load and plot hexahedral beam """
1214

1315
# get location of this file
14-
pth = dirname(realpath(__file__))
1516
filename = join(pth, 'file.rst')
1617

1718
fobj = Reader.ResultReader(filename)
@@ -31,16 +32,30 @@ def Load():
3132
z = disp[i, 2]
3233
print('{:2d} {:10.6f} {:10.6f} {:10.6f}'.format(node, x, y, z))
3334

34-
# return disp
3535

36-
def Display():
36+
def DisplayResult():
3737
""" Load and plot hexahedral beam """
3838

3939
# get location of this file
40-
pth = dirname(realpath(__file__))
41-
4240
fobj = Reader.ResultReader(join(pth, 'file.rst'))
4341
fobj.LoadCDB(join(pth, 'HexBeam.cdb'))
4442

4543
print('Displaying ANSYS Mode 1')
4644
fobj.PlotDisplacement(0)
45+
46+
47+
def LoadKM():
48+
""" Loads m and k matrices from a full file """
49+
50+
# Create file reader object
51+
fobj = Reader.FullReader(join(pth, 'file.full'))
52+
fobj.LoadFullKM()
53+
54+
ndim = fobj.nref.size
55+
56+
# print results
57+
print('Loaded {:d} x {:d} mass and stiffness matrices'.format(ndim, ndim))
58+
print('\t k has {:d} entries'.format(fobj.krows.size))
59+
print('\t m has {:d} entries'.format(fobj.mrows.size))
60+
61+

pyansys/Tests/file.full

832 KB
Binary file not shown.

pyansys/cython/_parsefull.pyx

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import numpy as np
2+
cimport numpy as np
3+
4+
# Definitions from c header
5+
cdef extern from "parsefull.h":
6+
int return_fheader(char*, int*)
7+
8+
void read_full(int*, int*, int*, int*, int*, double*, int*, int*, double*,
9+
int*, char*);
10+
11+
12+
def ReturnHeader(filename):
13+
""" Just reads in the header """
14+
# Convert python string to char array
15+
cdef bytes py_bytes = filename.encode()
16+
cdef char* c_filename = py_bytes
17+
18+
# Read header
19+
cdef int [::1] fheader = np.empty(101, np.int32)
20+
return_fheader(c_filename, &fheader[0])
21+
22+
return np.asarray(fheader)
23+
24+
25+
def ReturnFull_KM(filename):
26+
"""
27+
Reads an ANSYS full file and returns indices to construct symmetric, real,
28+
and sparse mass and stiffness matrices
29+
"""
30+
31+
# Convert python string to char array
32+
cdef bytes py_bytes = filename.encode()
33+
cdef char* c_filename = py_bytes
34+
35+
# Read header
36+
cdef int [::1] fheader = np.empty(101, np.int32)
37+
cdef int rst = return_fheader(c_filename, &fheader[0])
38+
39+
cdef int neqn = fheader[2]; # Number of equations
40+
cdef int ntermK = fheader[9]; # number of terms in stiffness matrix
41+
cdef int nNodes = fheader[33]; # Number of nodes considered by assembly
42+
cdef int ntermM = fheader[34]; # number of terms in mass matrix
43+
44+
#### Sanity check ####
45+
#// Check if lumped (item 11)
46+
if fheader[11]:
47+
raise Exception("Unable to read a lumped mass matrix. Terminating.")
48+
49+
# Check if arrays are unsymmetric (item 14)
50+
if fheader[14]:
51+
raise Exception ("Unable to read an unsymmetric mass/stiffness matrix.")
52+
53+
# Create numpy memory views so they can be garbage collected later
54+
cdef int[::1] numdat = np.empty(3, np.int32)
55+
# tracks array sizes (nfree, kentry, mentry)
56+
57+
# node and dof reference arrays
58+
cdef int[::1] nref = np.empty(nNodes*3, np.int32)
59+
cdef int[::1] dref = np.empty(nNodes*3, np.int32)
60+
61+
# size mass and stiffness arrays
62+
cdef int karrsz = 2*ntermK - neqn
63+
cdef int [::1] krows = np.empty(karrsz, np.int32)
64+
cdef int [::1] kcols = np.empty(karrsz, np.int32)
65+
cdef double [::1] kdata = np.empty(karrsz, np.double)
66+
67+
cdef int marrsz = 2*ntermM - neqn
68+
cdef int [::1] mrows = np.empty(marrsz, np.int32)
69+
cdef int [::1] mcols = np.empty(marrsz, np.int32)
70+
cdef double [::1] mdata = np.empty(marrsz, np.double)
71+
72+
# Populate these arrays
73+
read_full(&numdat[0], &nref[0], &dref[0], &krows[0], &kcols[0], &kdata[0],
74+
&mrows[0], &mcols[0], &mdata[0], &fheader[0], c_filename)
75+
76+
# Grab array sizes
77+
nfree = numdat[0]
78+
kentry = numdat[1]
79+
mentry = numdat[2]
80+
81+
# Return these to python
82+
return (np.asarray(nref)[:nfree],
83+
np.asarray(dref)[:nfree],
84+
np.asarray(krows)[:kentry],
85+
np.asarray(kcols)[:kentry],
86+
np.asarray(kdata)[:kentry],
87+
np.asarray(mrows)[:mentry],
88+
np.asarray(mcols)[:mentry],
89+
np.asarray(mdata)[:mentry])
90+
91+
92+
93+
94+
95+
96+
97+
98+

pyansys/cython/parsefull.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
int return_fheader(char*, int*);
2+
3+
void read_full(int*, int*, int*, int*, int*, double*, int*, int*, double*, int*, char*);

pyansys/cython/test

17.2 KB
Binary file not shown.

0 commit comments

Comments
 (0)