1+ import os
12import numpy as np
23import warnings
34import logging
@@ -79,6 +80,10 @@ def __init__(self, filename):
7980
8081 """
8182
83+ # check if file exists
84+ if not os .path .isfile (filename ):
85+ raise Exception ('{:s} not found' .format (filename ))
86+
8287 self .filename = filename
8388 self .header = _parsefull .ReturnHeader (filename )
8489
@@ -91,39 +96,44 @@ def __init__(self, filename):
9196 raise Exception ("Unable to read an unsymmetric mass/stiffness matrix." )
9297
9398
94- def LoadFullKM (self ):
99+ def LoadKM (self , as_sparse = True , sort = True ):
95100 """
96- Load indices for constructing symmetric mass and stiffness
97- matricies
101+ Load and construct mass and stiffness matrices from an ANSYS full file.
98102
99- STORES TO SELF:
100-
101- nref (np.int32 array)
102- Ordered reference to original ANSYS node numbers
103+ Parameters
104+ ----------
105+ as_sparse : bool, optional
106+ Outputs the mass and stiffness matrices as scipy csc sparse arrays
107+ when True by default.
108+
109+ sort : bool, optional
110+ By default, this setting sorts the rows and columns such that the
111+ nodes are in order. ANSYS stores the mass and stiffness matrices
112+ such that the bandwidth of the arrays is minimized. Therefore, to
113+ minimize the bandwidth of the arrays, make this setting False.
114+
103115
104- dref (np.int32 array)
105- DOF reference where
116+ Returns
117+ -------
118+ dof_ref : (n x 2) np.int32 array
119+ This array contains the node and degree corresponding to each row
120+ and column in the mass and stiffness matrices. When the sort
121+ parameter is set to True this array will be sorted by node number
122+ first and then by the degree of freedom. In a 3 DOF analysis the
123+ intergers will correspond to:
106124 0 - x
107125 1 - y
108126 2 - z
109-
110- krows (np.int32 array)
111- Rows to construct the sparse stiffness matrix
112-
113- kcols (np.int32 array)
114- Columns to construct the sparse stiffness matrix
127+
128+ k : (n x n) np.float or scipy.csc array
129+ Stiffness array
130+
131+ m : (n x n) np.float or scipy.csc array
132+ Mass array
133+
134+ Notes
135+ -----
115136
116- kdata (np.int32 array)
117- Data for each entry in thesparse stiffness matrix
118-
119- mrows (np.int32 array)
120- Rows to construct the mass stiffness matrix
121-
122- mcols (np.int32 array)
123- Columns to construct the mass stiffness matrix
124-
125- mdata (np.int32 array)
126- Data to construct the mass stiffness matrix
127137
128138 """
129139 data = _parsefull .ReturnFull_KM (self .filename )
@@ -144,7 +154,54 @@ def LoadFullKM(self):
144154 self .mcols = data [6 ]
145155 self .mdata = data [7 ]
146156
147-
157+ # see if
158+ if as_sparse :
159+ try :
160+ from scipy .sparse import csc_matrix
161+ except :
162+ raise Exception ('Unable to load scipy, matricies will be full' )
163+ as_sparse = False
164+
165+ # number of dimentions and degree of freedom reference
166+ ndim = self .nref .size
167+ dof_ref = np .vstack ((self .nref , self .dref )).T
168+ idx , ridx = Unique_Rows (dof_ref )
169+
170+ # resort K and M matries to ordered sorted node order
171+ if sort :
172+ # determine unique nodes and sorted indices
173+ krow = ridx [self .krows ]
174+ kcol = ridx [self .kcols ]
175+
176+ # get number of degrees of freedom
177+ krow = ridx [self .krows ]
178+ kcol = ridx [self .kcols ]
179+ mrow = ridx [self .mrows ]
180+ mcol = ridx [self .mcols ]
181+
182+ # sorted references
183+ dof_ref = dof_ref [idx ]
184+
185+ else :
186+ krow = self .krows
187+ kcol = self .kcols
188+ mrow = self .mrows
189+ mcol = self .mcols
190+
191+ # output as a sparse matrix
192+ if as_sparse :
193+ k = csc_matrix ((self .kdata , (krow , kcol )), shape = (ndim ,)* 2 )
194+ m = csc_matrix ((self .mdata , (mrow , mcol )), shape = (ndim ,)* 2 )
195+
196+ else :
197+ k = np .zeros ((ndim ,)* 2 )
198+ k [krow , kcol ] = self .kdata
199+
200+ m = np .zeros ((ndim ,)* 2 )
201+ m [mrow , mcol ] = self .mdata
202+
203+ return dof_ref , k , m
204+
148205
149206class ResultReader (object ):
150207 """
@@ -153,19 +210,22 @@ class ResultReader(object):
153210
154211 def __init__ (self , filename , logger = False ):
155212 """
156- SUMMARY
157- Loads basic result information from result file.
158-
213+ Loads basic result information from result file and initializes result
214+ object.
159215
160- INPUTS
161- filename (str)
216+ Parameters
217+ ----------
218+ filename : string
162219 Filename of the ANSYS binary result file.
220+
221+ logger : bool
222+ Enables logging if True. Debug feature.
163223
164224
165- OUTPUTS
225+ Returns
226+ -------
166227 None
167228
168-
169229 """
170230
171231 # set logging level depending on settings
@@ -826,3 +886,12 @@ def GetResultInfo(filename):
826886 return resultheader
827887
828888
889+
890+ def Unique_Rows (a ):
891+ """ Returns unique rows of a and indices of those rows """
892+ b = np .ascontiguousarray (a ).view (np .dtype ((np .void , a .dtype .itemsize * a .shape [1 ])))
893+ _ , idx , ridx = np .unique (b , return_index = True , return_inverse = True )
894+
895+ return idx , ridx
896+
897+
0 commit comments