pyyeti.nastran.op4.load¶
- pyyeti.nastran.op4.load(filename=None, namelist=None, into='dct', justmatrix=False, sparse=False)[source]¶
Read all matching matrices from op4 file into dictionary or list; non-member version of
OP4.load().This is a the same as
read()except justmatrix default is False.- Parameters:
filename (string or None; optional) – Name of op4 file to read. Can also be the name of a directory or None; in these cases, a GUI is opened for file selection.
namelist (list, string, or None; optional) – List of variable names to read in, or string with name of the single variable to read in, or None. If None, all matrices are read in.
into (string; optional) – Either ‘dct’ or ‘list’. Use ‘list’ if multiple matrices share the same name. See below.
justmatrix (bool; optional) – If True, only the matrix is stored in the dictionary. If False, a tuple of
(matrix, form, mtype)is stored. This option is ignored ifinto == 'list'.sparse (bool or None or two-tuple_like; optional) – Specifies whether output matrices will be regular numpy arrays or sparse arrays. If not two-tuple_like:
sparse
Action
None
Auto setting: each matrix will be sparse if and only if it was written in a sparse format
True
Matrices will be returned in sparse format
False
Matrices will be returned in regular (dense) numpy arrays
If sparse is two-tuple_like, the first element is either None, True, or False (see table above) and the second element is a callable, as in:
X = callable(X). A common usage of the callable would be to convert from “COO” sparse form (seescipy.sparse.coo_matrix) to a more desirable form. For example, to ensure all matrices are returned in CSC form (seescipy.sparse.csc_matrix) use:sparse=(True, scipy.sparse.coo_matrix.tocsc)
The callable is ignored for non-sparse matrices.
- Returns:
dct (
collections.OrderedDict, ifinto == 'dct') – Keys are the lower-case matrix names and the values are either just the matrix or a tuple of:(matrix, form, mtype)depending on justmatrix.tup (tuple, if
into == 'list') – Tuple of 4 lists:(names, matrices, forms, mtypes)
Notes
The default form for sparse matrices is the “COO” sparse form (see
scipy.sparse.coo_matrix). To override, provide a callable in the sparse option (see above).Examples
This examples translates a sparse format binary op4 file to a simpler ascii format while preserving the matrix forms.
First, create a file in “bigmat” sparse format and set the form on the “m” matrix to be symmetric (form=6):
>>> import numpy as np >>> from pyyeti.nastran import op4 >>> m = np.array([[1, 2], [2.1, 3]]) >>> k = np.array([3, 5]) >>> b = np.array([4, 6]) >>> names = ['m', 'k', 'b'] >>> values = [eval(n) for n in names] >>> op4.write('mkb.op4', names, values, forms=[6, 2, 2], ... sparse='bigmat')
Now, translate it to simple ascii, preserving the forms:
>>> dct = op4.load('mkb.op4') >>> op4.write('mkb_ascii.op4', dct, binary=False)
Check that the order and forms are the same:
>>> _ = op4.dir('mkb.op4') m , 2 x 2 , form=6, mtype=2 k , 1 x 2 , form=2, mtype=2 b , 1 x 2 , form=2, mtype=2
>>> _ = op4.dir('mkb_ascii.op4') m , 2 x 2 , form=6, mtype=2 k , 1 x 2 , form=2, mtype=2 b , 1 x 2 , form=2, mtype=2
Clean up:
>>> import os >>> os.remove('mkb.op4') >>> os.remove('mkb_ascii.op4')