def _read_grid(self, data, n): # 21.8 sec, 18.9 """(4501,45,1) - the marker for Record 17""" s = Struct(b(self._endian + 'ii3f3i')) ntotal = 32 nentries = (len(data) - n) // ntotal self._increase_card_count('GRID', nentries) for i in range(nentries): edata = data[n:n + 32] out = s.unpack(edata) (nid, cp, x1, x2, x3, cd, ps, seid) = out if self.is_debug_file: self.binary_debug.write(' GRID=%s\n' % str(out)) if nid < 10000000: # cd can be < 0 if ps == 0: ps = '' node = GRID(nid, cp, np.array([x1, x2, x3]), cd, ps, seid) self.nodes[nid] = node #if nid in self.nodes: #self.reject_lines.append(str(node)) #else: #self.nodes[nid] = node #self.add_node(node) else: self.log.debug( '*nid=%s cp=%s x1=%-5.2f x2=%-5.2f x3=%-5.2f cd=%-2s ps=%s ' 'seid=%s' % (nid, cp, x1, x2, x3, cd, ps, seid)) node = GRID(nid, cp, np.array([x1, x2, x3]), cd, ps, seid) self.rejects.append(str(node)) n += ntotal return n
def _read_grid(self, data: bytes, n: int) -> int: # 21.8 sec, 18.9 """(4501,45,1) - the marker for Record 17""" structi = Struct(self._endian + b'ii3f3i') ntotal = 32 nentries = (len(data) - n) // ntotal nfailed = 0 for unused_i in range(nentries): edata = data[n:n + 32] out = structi.unpack(edata) (nid, cp, x1, x2, x3, cd, ps, seid) = out if self.is_debug_file: self.binary_debug.write(' GRID=%s\n' % str(out)) if nid < 10000000: # cd can be < 0 if ps == 0: ps = '' node = GRID(nid, np.array([x1, x2, x3]), cp, cd, ps, seid) self._type_to_id_map['GRID'].append(nid) self.nodes[nid] = node #if nid in self.nodes: #self.reject_lines.append(str(node)) #else: #self.nodes[nid] = node #self.add_node(node) else: #self.log.warning('*nid=%s cp=%s x1=%-5.2f x2=%-5.2f x3=%-5.2f cd=%-2s ps=%s ' #'seid=%s' % (nid, cp, x1, x2, x3, cd, ps, seid)) #node = GRID(nid, np.array([x1, x2, x3]), cp, cd, ps, seid) #self.rejects.append(str(node)) nfailed += 1 n += ntotal self.increase_card_count('GRID', nentries - nfailed) return n
def test_time_type_check(self): """this tests what the best way to do type checking is""" g = GRID(4, [0., 0., 0.]) s = SPOINT(4) import time import six time0 = time.time() for i in six.moves.range(5000000): if g.type == 'GRID': pass if s.type == 'GRID': pass dt_type = time.time() - time0 time1 = time.time() for i in six.moves.range(5000000): if isinstance(g, GRID): pass if isinstance(s, GRID): pass dt_instance = time.time() - time1 #print('dt_type=%.4f dt_instance=%.4f' % (dt_type, dt_instance)) if dt_instance < dt_type: msg = ("flip the way you do type checking; card.type == 'GRID' " "is faster than isinstance(card, GRID); dt_instance=%s dt_type=%s" % (dt_instance, dt_type)) raise ValueError(msg)
def test_time_type_check(self): """this tests what the best way to do type checking is""" g = GRID(4, [0., 0., 0.]) s = SPOINT(4) import time time0 = time.time() for unused_i in range(5000000): if g.type == 'GRID': pass if s.type == 'GRID': pass dt_type = time.time() - time0 time1 = time.time() for unused_i in range(5000000): if isinstance(g, GRID): pass if isinstance(s, GRID): pass dt_instance = time.time() - time1 #print('dt_type=%.4f dt_instance=%.4f' % (dt_type, dt_instance)) log = SimpleLogger(level='debug', encoding='utf-8', log_func=None) msg = ( "flip the way you do type checking; card.type == 'GRID' " "is faster than isinstance(card, GRID); dt_instance=%s dt_type=%s" % (dt_instance, dt_type)) if dt_instance < dt_type: log.warning(msg)
def _read_grid_maybe(self, data: bytes, n: int) -> int: # pragma: no cover """ (4501, 45, 1120001) - the marker for Record 17 this is a GRID card with double vales for xyz """ ntotal = 44 * self.factor structi = Struct(mapfmt(self._endian + b'2i 3d 3i', self.size)) nentries = (len(data) - n) // ntotal leftover = (len(data) - n) % ntotal assert leftover == 0, f'ndata={len(data)-n} leftover={leftover}' for unused_i in range(nentries): edata = data[n:n + ntotal] out = structi.unpack(edata) (nid, cp, x1, x2, x3, cd, ps, seid) = out if self.is_debug_file: self.binary_debug.write(' GRID=%s\n' % str(out)) # cd can be < 0 if ps == 0: ps = '' node = GRID(nid, np.array([x1, x2, x3]), cp, cd, ps, seid) self._type_to_id_map['GRID'].append(nid) self.nodes[nid] = node #self.log.debug(f' nid={nid} cp={cp} x=[{x1:g}, {x2:g}, {x3:g}] cd={cd} ps={ps} seid={seid}') n += ntotal self.increase_card_count('GRID', nentries) return n
def test_grid_01(self): """tests GRID""" nid = 1 cp = 2 cd = 0 ps = '' seid = 0 datai = [nid, cp, 0., 0., 0., cd, ps, seid] n1 = GRID.add_op2_data(datai) #print(n1) msg = n1.write_card(size=8) #print(msg) msg = n1.write_card(size=16) #print(msg) msg = n1.write_card(size=16, is_double=True) #print(msg) msg = n1.write_card(size=8) #print('%r' % msg) if 0: # pragma: no cover # small field self.assertEqual( msg, 'GRID 1 2 0. 0. 0. \n' ) msg = n1.write_card(size=16) # large field card = ( 'GRID* 1 2 .-0 .-0\n' '* .-0 \n' ) #print('%r' % msg) ref = 'ERROR\n' if card != msg: # pragma: no cover scard = card.split('\n') smsg = msg.split('\n') i = 0 print(scard) print(smsg) for sc, sm in zip(scard, smsg): if sc != sm: ref += 'i=%s\ncard=%r\nmsg =%r\n' % (i, sc, sm) i += 1 print(ref) self.assertEqual(msg, card), ref
def _read_grid_11(self, data: bytes, n: int) -> Tuple[int, Dict[int, GRID]]: # 21.8 sec, 18.9 """(4501,45,1) - the marker for Record 17""" ntotal = 44 structi = Struct(self._endian + b'ii 3d 3i') ndatai = len(data) - n nentries = ndatai // ntotal #print(f'len(data)={len(data)} ndatai={ndatai} ntotal={ntotal} nentries={nentries}') assert ndatai % ntotal == 0, f'len(data)={len(data)} ndatai={ndatai} ntotal={ntotal} nentries={nentries}' assert nentries > 0, f'len(data)={len(data)} ndatai={ndatai} ntotal={ntotal} nentries={nentries}' nfailed = 0 grids = {} for unused_i in range(nentries): edata = data[n:n + ntotal] out = structi.unpack(edata) #cp, x1, x2, x3, cd, ps, seid (nid, cp, x1, x2, x3, cd, ps, seid) = out if self.is_debug_file: self.binary_debug.write(' GRID=%s\n' % str(out)) #print(f'nid={nid}, cp={cp} x=({x1}, {x2}, {x3}), cd={cd} ps={ps}, seid={seid}') assert cd >= 0, f'nid={nid}, cp={cp} x=({x1}, {x2}, {x3}), cd={cd} ps={ps}, seid={seid}' assert seid == 0, f'nid={nid}, cp={cp} x=({x1}, {x2}, {x3}), cd={cd} ps={ps}, seid={seid}' if nid < 10000000: # cd can be < 0 if ps == 0: ps = '' node = GRID(nid, np.array([x1, x2, x3]), cp, cd, ps, seid) #print(node) #self._type_to_id_map['GRID'].append(nid) #self.nodes[nid] = node grids[nid] = node #if nid in self.nodes: #self.reject_lines.append(str(node)) #else: #self.nodes[nid] = node #self.add_node(node) else: #self.log.warning('*nid=%s cp=%s x1=%-5.2f x2=%-5.2f x3=%-5.2f cd=%-2s ps=%s ' #'seid=%s' % (nid, cp, x1, x2, x3, cd, ps, seid)) #node = GRID(nid, np.array([x1, x2, x3]), cp, cd, ps, seid) #self.rejects.append(str(node)) nfailed += 1 n += ntotal return n, grids
def _readGrid(self, data, n): # 21.8 sec, 18.9 """(4501,45,1) - the marker for Record 17""" s = Struct(b'ii3f3i') ntotal = 32 nentries = (len(data) - n) // ntotal for i in xrange(nentries): edata = data[n:n + 32] out = s.unpack(edata) (nID, cp, x1, x2, x3, cd, ps, seid) = out if cd >= 0 and nID < 10000000: node = GRID(None, out) self.add_node(node) else: self.log.debug( "*nID=%s cp=%s x1=%-5.2f x2=%-5.2f x3=%-5.2f cd=%-2s ps=%s seid=%s" % (nID, cp, x1, x2, x3, cd, ps, seid)) n += ntotal self.card_count['GRID'] = nentries return n
def readGrid(self, data): # 21.8 sec, 18.9 """(4501,45,1) - the marker for Record 17""" #print "reading GRID" n = 0 nEntries = len(data) // 32 for i in range(nEntries): eData = data[n:n + 32] out = unpack('iifffiii', eData) (nID, cp, x1, x2, x3, cd, ps, seid) = out if cd >= 0 and nID < 10000000: node = GRID(None, out) self.add_node(node) else: self.log.debug( "*nID=%s cp=%s x1=%-5.2f x2=%-5.2f x3=%-5.2f cd=%-2s ps=%s seid=%s" % (nID, cp, x1, x2, x3, cd, ps, seid)) #print str(grid)[:-1] n += 32 data = data[n:]
def test_grid_02(self): """tests GRID""" nid = 1 cp = 2 cd = 0 ps = '1' seid = 4 datai = ['GRID', nid, cp, 0., 0., 0., cd, ps, seid] card = BDFCard(datai) n1 = GRID.add_card(card) x = 0.1 y = 0.2 z = 0.3 self.assertEqual(n1.get_field(3), 0., msg='%s' % n1.get_field(3)) self.assertEqual(n1.get_field(4), 0., msg='%s' % n1.get_field(4)) self.assertEqual(n1.get_field(5), 0., msg='%s' % n1.get_field(5)) self.assertEqual(n1.get_field(6), cd, msg='%s' % n1.get_field(6)) self.assertEqual(n1.get_field(7), ps, msg='%s' % n1.get_field(7)) self.assertEqual(n1.get_field(8), seid, msg='%s' % n1.get_field(8)) n1.update_field(3, x) n1.update_field(4, y) n1.update_field(5, z) #print('ps = %r' % n1.ps) self.assertEqual(n1.xyz[0], x) self.assertEqual(n1.xyz[1], y) self.assertEqual(n1.xyz[2], z) self.assertEqual(n1.ps, ps) self.assertEqual(n1.cd, cd) self.assertEqual(n1.seid, seid) self.assertEqual(n1.get_field(3), x, msg='%s' % n1.get_field(3)) self.assertEqual(n1.get_field(4), y, msg='%s' % n1.get_field(4)) self.assertEqual(n1.get_field(5), z, msg='%s' % n1.get_field(5)) self.assertEqual(n1.get_field(6), cd, msg='%s' % n1.get_field(6)) self.assertEqual(n1.get_field(7), ps, msg='%s' % n1.get_field(7)) self.assertEqual(n1.get_field(8), seid, msg='%s' % n1.get_field(8))
def _read_grid_8(self, data: bytes, n: int) -> Tuple[int, Dict[int, GRID]]: # 21.8 sec, 18.9 """(4501,45,1) - the marker for Record 17""" structi = Struct(mapfmt(self._endian + b'ii 3f 3i', self.size)) ntotal = 32 * self.factor ndatai = len(data) - n nentries = ndatai // ntotal assert nentries > 0, nentries assert ndatai % ntotal == 0, f'ndatai={ndatai} ntotal={ntotal} leftover={ndatai % ntotal}' grids = {} for unused_i in range(nentries): edata = data[n:n + ntotal] out = structi.unpack(edata) (nid, cp, x1, x2, x3, cd, ps, seid) = out if self.is_debug_file: self.binary_debug.write(' GRID=%s\n' % str(out)) #if nid < 10000000: # cd can be < 0 if ps == 0: ps = '' node = GRID(nid, np.array([x1, x2, x3]), cp, cd, ps, seid) #self._type_to_id_map['GRID'].append(nid) #self.nodes[nid] = node grids[nid] = node #if nid in self.nodes: #self.reject_lines.append(str(node)) #else: #self.nodes[nid] = node #self.add_node(node) #else: #self.log.warning('*nid=%s cp=%s x1=%-5.2f x2=%-5.2f x3=%-5.2f cd=%-2s ps=%s ' #'seid=%s' % (nid, cp, x1, x2, x3, cd, ps, seid)) #node = GRID(nid, np.array([x1, x2, x3]), cp, cd, ps, seid) #self.rejects.append(str(node)) #nfailed += 1 n += ntotal #self.increase_card_count('GRID', nentries - nfailed) return n, grids
def create_spar_cap(model, eids, nids, width, nelements=1, symmetric=True, xyz_cid0=None, vector1=None, vector2=None, idir=None, eid_start=1): """ Builds elements along a line of nodes that are normal to the element face. .. code-block:: console 1 2 3 4 5 D 4 E 6 +----+-----+------+ *-----+-----* | A | B | C | | +----+-----+------+ | (side view) (along the axis) Nodes at the upper corner of Element A, B, C. Parameters ---------- xyz_cid0 : (n, 3) ndarray nodes in cid=0 width : float the width of the spar cap nelements : int; default=1 the number of nodes to create that are normal to the plane vector1 : (float, float, float) overwrite the normal of the normals (not supported) vector2 : (float, float, float) overwrite the normal of the normals (not supported) idir : int; default=1 A primary direction to which the normals should be consistent (not supported). 0 : x 1 : y 2 : z For an aircraft rib, y would be the preferred direction for the normal. For an aircraft spar, x/z would work. Examples -------- >>> eids = [A, B, C] >>> nids = [1, 2, 3, 4] >>> width = 3.0 >>> nodes, elements = create_spar_cap(model, eids, nids, width) >>> nodes [5, 6] >>> elements [D, E] """ assert vector1 is None, vector1 assert vector2 is None, vector2 assert idir is None, idir assert nelements == 1, idir if xyz_cid0 is None: xyz_cid0 = model.get_xyz_in_coord(cid=0) #print(xyz_cid0) nid_cd = np.array([[nid, node.Cd()] for nid, node in sorted(model.nodes.items())]) all_nids = nid_cd[:, 0] width_array = np.linspace(0., width, num=nelements, endpoint=True)[1:] nodes = [] elements = [] nid_start = all_nids.max() + 1 eids = np.asarray(eids, dtype='int32') nids = np.asarray(nids, dtype='int32') # Create the CQUAD4s map_nid = {} map_nid2 = {} neids = eids.size all_common_nids = set() normals = np.zeros((neids, 3), dtype='float64') for eid in eids: elem = model.elements[eid] pid = elem.Pid() enids = elem.node_ids common_nids = np.where(np.in1d(enids, nids)) # A in B all_common_nids.update(common_nids) i = searchsorted(all_nids, enids) xyz = xyz_cid0[i, :] etype = elem.type if etype == 'CQUAD4': # 3---2 # | | # 0---1 normal = np.cross( xyz[2, :] - xyz[0, :], xyz[3, :] - xyz[1, :], ) elif etype == 'CTRIA3': normal = np.cross( xyz[1, :] - xyz[0, :], xyz[2, :] - xyz[0, :], ) else: raise NotImplementedError(etype) normi = np.linalg.norm(normal) assert np.allclose(normi, 1.0), normi normal /= normi # add some new nodes nid0, nid1 = common_nids if nid0 not in map_nid: map_nid[nid0] = np.arange(nid_start, nid_start + nelements) nid_start += nelements if nid1 not in map_nid: map_nid[nid1] = np.arange(nid_start, nid_start + nelements) nid_start += nelements nid3 = map_nid[nid1][0] nid4 = map_nid[nid0][0] nid0i = nid0 nid1i = nid1 for ielement in range(nelements): nid3 = map_nid[nid1][ielement] nid4 = map_nid[nid0][ielement] new_nids = [nid0i, nid1i, nid3, nid4] cquad4 = CQUAD4(eid_start, pid, new_nids) elements.append(cquad4) eid_start += 1 nid0i = nid3 nid1i = nid4 if symmetric: if nid0 not in map_nid2: map_nid2[nid0] = np.arange(nid_start, nid_start + nelements) nid_start += nelements if nid1 not in map_nid2: map_nid2[nid1] = np.arange(nid_start, nid_start + nelements) nid_start += nelements nid3 = map_nid2[nid1][0] nid4 = map_nid2[nid0][0] nid0i = nid0 nid1i = nid1 for ielement in range(nelements): nid3 = map_nid2[nid1][ielement] nid4 = map_nid2[nid0][ielement] new_nids = [nid0i, nid1i, nid3, nid4] cquad4 = CQUAD4(eid_start, pid, new_nids) elements.append(cquad4) eid_start += 1 nid0i = nid3 nid1i = nid4 # Create the GRIDs inids = searchsorted(all_nids, nids) xyz = xyz_cid0[inids, :] for nid in all_common_nids: mapped_nids = map_nid[nid] avg_normal_at_node = np.zeros(3, dtype='float64') unused_node = model.nodes[nid] node_elems = nid.elements nelems = len(node_elems) for elem in node_elems: eid = elem.eid j = searchsorted(eids, eid) avg_normal_at_node += normals[j, :] avg_normal_at_node /= nelems for mapped_nid in mapped_nids: xyzi = xyz[i, :] + avg_normal_at_node * width_array[i] node1 = GRID(mapped_nid, xyz=xyzi) nodes.append(node1) if symmetric: mapped_nids2 = map_nid2[nid] for unused_imap, mapped_nid2 in enumerate(mapped_nids2): xyzi = xyz[i, :] + avg_normal_at_node * width_array[i] node2 = GRID(mapped_nid2, xyz=xyzi) nodes.append(node2) return nodes, elements
def _read_grid_maybe(self, data: bytes, n: int) -> int: # pragma: no cover """(4501, 45, 1120001) - the marker for Record 17""" return len(data) nfields = (len(data) - n) // 4 # nfields = 3 * 11 * 17 * 71 # it's not 11, 17... #self.show_data(data[12:], types='if') # 2i: correct # id, 0 # i: ??? # i/f: ??? # f: correct # i: ??? # f: correct # 5i: ??? # ? ? ? structi = Struct(self._endian + b'2i i f i f 5i') # 11...decent #structi = Struct(self._endian + b'17i') # 17...not a chance # i: id # i/f: ??? # 3f #structi = Struct(self._endian + b'i i 3f 28i') # 33...better?...still not right #structi = Struct(self._endian + b'51i') # 17*3..nope #structi = Struct(self._endian + b'71i') # 71...nope #structi = Struct(self._endian + b'187i') # 11*17... ntotal = 4 * 11 nentries = (len(data) - n) // ntotal leftover = (len(data) - n) % ntotal assert leftover == 0, f'ndata={len(data)-n} leftover={leftover}' nfailed = 0 for unused_i in range(nentries): edata = data[n:n + ntotal] out = structi.unpack(edata) self.log.debug(out) n += ntotal continue (nid, cp, x1, x2, x3, cd, ps, seid) = out if self.is_debug_file: self.binary_debug.write(' GRID=%s\n' % str(out)) if nid < 10000000: # cd can be < 0 if ps == 0: ps = '' node = GRID(nid, np.array([x1, x2, x3]), cp, cd, ps, seid) self._type_to_id_map['GRID'].append(nid) self.nodes[nid] = node #if nid in self.nodes: #self.reject_lines.append(str(node)) #else: #self.nodes[nid] = node #self.add_node(node) else: #self.log.warning('*nid=%s cp=%s x1=%-5.2f x2=%-5.2f x3=%-5.2f cd=%-2s ps=%s ' #'seid=%s' % (nid, cp, x1, x2, x3, cd, ps, seid)) #node = GRID(nid, np.array([x1, x2, x3]), cp, cd, ps, seid) #self.rejects.append(str(node)) nfailed += 1 n += ntotal self.increase_card_count('GRID', nentries - nfailed) return n