def test_wtop4(): matfile = "tests/nastran_op4_data/r_c_rc.mat" o4 = op4.OP4() m = matlab.loadmat(matfile) names = ["rmat", "cmat", "rcmat"] mats = [] wtdct = {} for nm in names: mats.append(m[nm]) wtdct[nm] = m[nm] # write(filename, names, matrices=None, # binary=True, digits=16, endian='') filenames = [ ["tests/nastran_op4_data/temp_ascii.op4", False, ""], ["tests/nastran_op4_data/temp_le.op4", True, "<"], ["tests/nastran_op4_data/temp_be.op4", True, ">"], ] for item in filenames: filename = item[0] binary = item[1] endian = item[2] o4.write(filename, names, mats, binary=binary, endian=endian) names2, sizes, forms, mtypes = o4.dir(filename, verbose=False) assert names2 == names dct = o4.dctload(filename) for nm in dct: assert np.allclose(m[nm], dct[nm][0]) o4.write(filename, wtdct, binary=binary, endian=endian) dct = o4.dctload(filename) for nm in dct: assert np.allclose(m[nm], dct[nm][0]) # clean up: for item in filenames: os.remove(item[0])
def test_wtop4_single(): matfile = "tests/nastran_op4_data/r_c_rc.mat" o4 = op4.OP4() m = matlab.loadmat(matfile) name = "rmat" mat = m[name] # write(filename, names, matrices=None, # binary=True, digits=16, endian='') filenames = [["tests/nastran_op4_data/temp_ascii.op4", False, ""]] for item in filenames: filename = item[0] binary = item[1] endian = item[2] o4.write( filename, name, mat, binary=binary, endian=endian, forms=45 ) # 45 is not actually a valid setting dct = o4.dctload(filename) for nm in dct: assert nm == name assert np.allclose(m[nm], dct[nm][0]) assert dct[nm][1] == 45 # clean up: for item in filenames: os.remove(item[0])
def test_rdop4(): # unittest bug avoidance: import sys for v in list(sys.modules.values()): if getattr(v, "__warningregistry__", None): v.__warningregistry__ = {} o4 = op4.OP4() _rdop4_tst(o4)
def test_forced_bigmat(): mat = np.zeros((1000000, 1)) o4 = op4.OP4() o4.save("temp.op4", dict(mat=mat), sparse="nonbigmat") m = o4.load("temp.op4", into="dct") assert np.all(mat == m["mat"][0]) o4.save("temp.op4", dict(mat=mat), sparse="nonbigmat", binary=False) m = o4.load("temp.op4", into="dct") assert np.all(mat == m["mat"][0]) os.remove("temp.op4")
def test_rdop4_partb(): filenames = glob("tests/nastran_op4_data/*other") file1 = filenames[0] filenames = filenames[1:] o4 = op4.OP4() dct = o4.dctload(file1) for filename in filenames: dct2 = o4.dctload(filename) assert set(dct2.keys()) == set(dct.keys()) for nm in dct: for j in range(3): assert np.allclose(dct2[nm][j], dct[nm][j])
def test_non_float64(): i8 = np.array([1, 2, 0, 4], np.int8) i16 = i8.astype(np.int16) i32 = i8.astype(np.int32) i64 = i8.astype(np.int64) f32 = i8.astype(np.float32) c32 = (f32 + 1j * f32).astype(np.complex64) o4 = op4.OP4() for mat in [i8, i16, i32, i64, f32, c32]: o4.write("temp.op4", dict(mat=mat)) mat2 = o4.dctload("temp.op4", "mat")["mat"][0] assert np.all(mat2 == mat) os.remove("temp.op4")
def test_wtop4_bigmat_ascii_1(): filenames = glob("tests/nastran_op4_data/*.op4") + glob( "tests/nastran_op4_data/*.op4.other") o4 = op4.OP4() for name in filenames[:1]: if name.find("badname") != -1: continue data = o4.load(name, into="list") o4.write("temp.op4", data[0], data[1], sparse="bigmat", binary=False) data2 = o4.load("temp.op4", into="list") assert data[0] == data2[0] for d1, d2 in zip(data[1], data2[1]): assert np.all(d1 == d2) os.remove("temp.op4")
def test_wtop4_nonbigmat_binary(): filenames = glob("tests/nastran_op4_data/*.op4") + glob( "tests/nastran_op4_data/*.op4.other") o4 = op4.OP4() for name in filenames: if name.find("badname") != -1: continue data = o4.listload(name) o4.write("temp.op4", data[0], data[1], sparse="nonbigmat") data2 = o4.listload("temp.op4") assert data[0] == data2[0] for d1, d2 in zip(data[1], data2[1]): assert np.all(d1 == d2) os.remove("temp.op4")
def test_wtop4_single_save_1(): matfile = "tests/nastran_op4_data/r_c_rc.mat" o4 = op4.OP4() m = matlab.loadmat(matfile) name = "rmat" mat = m[name] # write(filename, names, matrices=None, # binary=True, digits=16, endian='') filenames = [["tests/nastran_op4_data/temp_ascii.op4", False, ""]] for item in filenames: filename = item[0] binary = item[1] endian = item[2] o4.save(filename, name, mat, binary=binary, endian=endian) dct = o4.load(filename, into="dct") for nm in dct: assert np.allclose(m[nm], dct[nm][0]) # clean up: for item in filenames: os.remove(item[0])
def test_wtop4_single_4(): matfile = "tests/nastran_op4_data/r_c_rc.mat" o4 = op4.OP4() assert_raises(ValueError, o4.load, matfile, into="badstring")
def test_rdop4_zero_rowscutoff(): o4 = op4.OP4() o4._rowsCutoff = 0 _rdop4_tst(o4)
def test_drm12_reader(): import numpy as np o4 = op4.OP4() drm12 = "tests/nastran_drm12/drm12" mats = o4.dctload(drm12 + ".op4") dsorted = op2.procdrm12(drm12, dosort=True) # just to exercise more code: with op2.OP2("tests/nastran_drm12/drm12.op2") as o2: dkeys = o2.rddrm2op2(1) # check desc: assert np.all(["T1", "T2", "T3"] * 3 == dsorted["DTM_desc"]) assert np.all(["T1", "T2", "T3"] * 3 == dsorted["ATM_desc"]) spcf_desc = ["Fx", "Fy", "Fz", "Mx", "My", "Mz"] assert np.all(spcf_desc * 4 == dsorted["SPCF_desc"]) stress = [ "CBAR Bending Stress 1 - End A", # 2 "CBAR Bending Stress 2 - End A", # 3 "CBAR Bending Stress 3 - End A", # 4 "CBAR Bending Stress 4 - End A", # 5 "CBAR Axial Stress", # 6 "CBAR Max. Bend. Stress -End A", # 7 "CBAR Min. Bend. Stress -End A", # 8 "CBAR M.S. Tension", # 9 "CBAR Bending Stress 1 - End B", # 10 "CBAR Bending Stress 2 - End B", # 11 "CBAR Bending Stress 3 - End B", # 12 "CBAR Bending Stress 4 - End B", # 13 "CBAR Max. Bend. Stress -End B", # 14 "CBAR Min. Bend. Stress -End B", # 15 "CBAR M.S. Compression", ] # 16 assert np.all(stress * 2 == dsorted["STM_desc"]) force = [ "CBAR Bending Moment 1 - End A", # 2 "CBAR Bending Moment 2 - End A", # 3 "CBAR Bending Moment 1 - End B", # 4 "CBAR Bending Moment 2 - End B", # 5 "CBAR Shear 1", # 6 "CBAR Shear 2", # 7 "CBAR Axial Force", # 8 "CBAR Torque", ] # 9 assert np.all(force * 2 + force[-2:] == dsorted["LTM_desc"]) # check id_dof: ids = np.array([[12] * 3, [14] * 3, [32] * 3]).reshape((1, -1)).T dof = np.array([[1, 2, 3] * 3]).T iddof = np.hstack((ids, dof)) assert np.all(iddof == dsorted["DTM_id_dof"]) assert np.all(iddof == dsorted["ATM_id_dof"]) ids = np.array([[3] * 6, [11] * 6, [19] * 6, [27] * 6]).reshape((1, -1)).T dof = np.array([[1, 2, 3, 4, 5, 6] * 4]).T iddof = np.hstack((ids, dof)) assert np.all(iddof == dsorted["SPCF_id_dof"]) ids = np.array([[11] * 15, [89] * 15]).reshape((1, -1)).T dof = np.array([[i for i in range(2, 17)] * 2]).T iddof = np.hstack((ids, dof)) assert np.all(iddof == dsorted["STM_id_dof"]) ids = np.array([[11] * 8 + [23] * 8 + [28] * 2]).reshape((1, -1)).T dof = np.array([[i for i in range(2, 10)] * 2 + [8, 9]]).T iddof = np.hstack((ids, dof)) assert np.all(iddof == dsorted["LTM_id_dof"]) # check drms: # manually getting rows from TOUGV1, etc in .out file: rows = np.array([13, 14, 15, 19, 20, 21, 49, 50, 51]) - 1 assert np.all(mats["mougs1"][0][rows] == dsorted["DTMD"]) assert np.all(mats["mougd1"][0][rows] == dsorted["DTMA"]) assert np.all(mats["mougv1"][0][rows] == dsorted["ATM"]) assert np.all(mats["moqgs1"][0] == dsorted["SPCFD"]) assert np.all(mats["moqgd1"][0] == dsorted["SPCFA"]) rows = np.array([i for i in range(1, 17)] + [23, 24]) - 1 assert np.all(mats["moefs1"][0][rows] == dsorted["LTMD"]) assert np.all(mats["moefd1"][0][rows] == dsorted["LTMA"]) assert np.all(mats["moess1"][0] == dsorted["STMD"]) assert np.all(mats["moesd1"][0] == dsorted["STMA"]) draw = op2.procdrm12(drm12, dosort=False) # check desc: assert np.all(["T1", "T2", "T3"] * 3 == draw["DTM_desc"]) assert np.all(["T3", "T1", "T2"] + ["T1", "T2", "T3"] * 2 == draw["ATM_desc"]) spcf_desc = ["Fx", "Fy", "Fz", "Mx", "My", "Mz"] assert np.all(spcf_desc * 4 == draw["SPCF_desc"]) stress = [ "CBAR Bending Stress 1 - End A", # 2 "CBAR Bending Stress 2 - End A", # 3 "CBAR Bending Stress 3 - End A", # 4 "CBAR Bending Stress 4 - End A", # 5 "CBAR Axial Stress", # 6 "CBAR Max. Bend. Stress -End A", # 7 "CBAR Min. Bend. Stress -End A", # 8 "CBAR M.S. Tension", # 9 "CBAR Bending Stress 1 - End B", # 10 "CBAR Bending Stress 2 - End B", # 11 "CBAR Bending Stress 3 - End B", # 12 "CBAR Bending Stress 4 - End B", # 13 "CBAR Max. Bend. Stress -End B", # 14 "CBAR Min. Bend. Stress -End B", # 15 "CBAR M.S. Compression", ] # 16 assert np.all(stress * 2 == draw["STM_desc"]) force = [ "CBAR Bending Moment 1 - End A", # 2 "CBAR Bending Moment 2 - End A", # 3 "CBAR Bending Moment 1 - End B", # 4 "CBAR Bending Moment 2 - End B", # 5 "CBAR Shear 1", # 6 "CBAR Shear 2", # 7 "CBAR Axial Force", # 8 "CBAR Torque", ] # 9 assert np.all(force + force[-1:] + force[-2:-1] + force == draw["LTM_desc"]) # check id_dof: ids = np.array([[14] * 3, [12] * 3, [32] * 3]).reshape((1, -1)).T dof = np.array([[1, 2, 3] * 3]).T iddof = np.hstack((ids, dof)) assert np.all(iddof == draw["DTM_id_dof"]) dof = np.array([[3, 1, 2] + [1, 2, 3] * 2]).T iddof = np.hstack((ids, dof)) assert np.all(iddof == draw["ATM_id_dof"]) ids = np.array([[3] * 6, [11] * 6, [19] * 6, [27] * 6]).reshape((1, -1)).T dof = np.array([[1, 2, 3, 4, 5, 6] * 4]).T iddof = np.hstack((ids, dof)) assert np.all(iddof == draw["SPCF_id_dof"]) ids = np.array([[89] * 15, [11] * 15]).reshape((1, -1)).T dof = np.array([[i for i in range(2, 17)] * 2]).T iddof = np.hstack((ids, dof)) assert np.all(iddof == draw["STM_id_dof"]) ids = np.array([[23] * 8 + [28] * 2 + [11] * 8]).reshape((1, -1)).T dof = np.array([[i for i in range(2, 10)] + [9, 8] + [i for i in range(2, 10)]]).T iddof = np.hstack((ids, dof)) assert np.all(iddof == draw["LTM_id_dof"]) # check drms: # manually getting rows from TOUGV1, etc in .out file: rows = np.array([19, 20, 21, 13, 14, 15, 49, 50, 51]) - 1 assert np.all(mats["mougs1"][0][rows] == draw["DTMD"]) assert np.all(mats["mougd1"][0][rows] == draw["DTMA"]) rows = np.array([21, 19, 20, 13, 14, 15, 49, 50, 51]) - 1 assert np.all(mats["mougv1"][0][rows] == draw["ATM"]) assert np.all(mats["moqgs1"][0] == draw["SPCFD"]) assert np.all(mats["moqgd1"][0] == draw["SPCFA"]) rows = np.array( [9, 10, 11, 12, 13, 14, 15, 16, 24, 23, 1, 2, 3, 4, 5, 6, 7, 8]) - 1 assert np.all(mats["moefs1"][0][rows] == draw["LTMD"]) assert np.all(mats["moefd1"][0][rows] == draw["LTMA"]) rows = np.array([i for i in range(16, 31)] + [i for i in range(1, 16)]) - 1 assert np.all(mats["moess1"][0][rows] == draw["STMD"]) assert np.all(mats["moesd1"][0][rows] == draw["STMA"])