pyyeti.nastran.op4.write

pyyeti.nastran.op4.write(filename, names, matrices=None, binary=True, digits=16, endian='=', sparse='auto', forms=None)[source]

Write op4 file; non-member version of OP4.write().

Parameters:
  • filename (string or None) – Name of file. Can also be the name of a directory or None; in these cases, a GUI is opened for file selection.

  • names (dictionary/OrderedDict or list or string) – Dictionary indexed by the matrix names with the values being either the matrices or a tuple/list where the first two items are (matrix, form). Alternatively, names can also be a list of matrix names (strings) or a single name (string) if just one matrix is to be saved. If using the list inputs, matrices is required to specify the matrices and, if forms needs to specified, that input would also be required.

  • matrices (array or list; optional) – 2d ndarray or list of 2d ndarrays. Ignored if names is a dictionary. Same length as names.

  • binary (bool; optional) – If true, a double precision binary file is written; otherwise an ascii file is created.

  • digits (integer; optional) – Number of significant digits after the decimal to include in the ascii output. Ignored for binary files.

  • endian (string; optional) – Endian setting for binary output: ‘=’ for native, ‘>’ for big-endian and ‘<’ for little-endian.

  • sparse (string; optional) – Specifies the output format:

    sparse

    Action

    ‘auto’

    Each 2d ndarray will be written in “dense” format and each sparse matrix will be written in “bigmat” sparse format

    ‘bigmat’

    Each matrix (whether sparse or not) is written in “bigmat” sparse format

    ‘dense’

    Each matrix will be written in “dense” format

    ‘nonbigmat’

    Each matrix is written in “nonbigmat” format. Note that if the number of rows is > 65535, then the “bigmat” format is used.

  • forms (integer or list or None; optional) – The matrix form(s). If None, the forms will be determined automatically to be 1, 2, or 6. Ignored if names is a dictionary (which would contain this information). If not None, forms must be the same length as names, but you can use None as the form for one or more matrices (see example below). From Nastran documentation:

    form

    Matrix format

    1

    Square

    2

    Rectangular

    3

    Diagonal

    4

    Lower triangular factor

    5

    Upper triangular factor

    6

    Symmetric

    8

    Identity

    9

    Pseudo identity

    10

    Cholesky factor

    11

    Trapezoidal factor

    13

    Sparse lower triangular factor

    15

    Sparse upper triangular factor

    Warning

    The validity of the values in forms is not checked in any way.

Returns:

None.

Notes

To write multiple matrices that have the same name, names must be a list, not a dictionary. If a list, the order is maintained. If a dictionary, the matrices are written in the order they are retrieved from the dictionary; use a collections.OrderedDict to specify a certain order.

save is an alias for write.

Examples

To write m, k, b, in that order to a binary file, you could use lists or an OrderedDict:

>>> import numpy as np
>>> from pyyeti.nastran import op4
>>> m = np.array([[1, 2], [2, 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)
>>> _ = 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

Or, order is also maintained when using an OrderedDict:

>>> from collections import OrderedDict
>>> odct = OrderedDict()
>>> for n in names:
...     odct[n] = eval(n)
>>> op4.write('mkb.op4', odct)
>>> _ = 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

On the other hand, if you don’t care about the order, you could use a regular dictionary. (In more recent versions of Python, this may behave like an OrderedDict.):

>>> op4.write('mkb.op4', dict(m=m, k=k, b=b))
>>> _ = 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

To specify the forms, include the forms option in either the list approach or the dictionary approach. Here, we’ll just say that “m” is square (not symmetric) for demonstration. The forms for “k” and “b” will be automatically detected. First, the list approach:

>>> op4.write('mkb.op4', ['m', 'k', 'b'], [m, k, b],
...           forms=[1, None, None])
>>> _ = op4.dir('mkb.op4')
m       ,      2 x 2     , form=1, mtype=2
k       ,      1 x 2     , form=2, mtype=2
b       ,      1 x 2     , form=2, mtype=2

Next, the dictionary approach. Since the form is attached to each matrix, it is only required if not None:

>>> odct = OrderedDict()
>>> odct['m'] = (m, 1)
>>> odct['k'] = k
>>> odct['b'] = b
>>> op4.write('mkb.op4', odct)
>>> _ = op4.dir('mkb.op4')
m       ,      2 x 2     , form=1, 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')

See also

load(), dir()