|
| 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 | + |
0 commit comments