Ejemplo n.º 1
0
def _write_spc1(card_type: str, cards, unused_ncards: int, op2_file, op2_ascii,
                endian: bytes) -> int:
    key = (5481, 58, 12)
    #sid, components = out[:2]
    #thru_flag = out[2]
    #if thru_flag == 0:  # repeat 4 to end
        #nids = out[3:].tolist()
        #thru_check = False
    #elif thru_flag == 1:
        #n1 = out[3]
        #n2 = out[4]
        #nids = list(range(n1, n2+1))
        #thru_check = True
    #else:
        #raise NotImplementedError('SPC1; thru_flag=%s' % thru_flag)

    max_spc_id = max([spc.conid for spc in cards])
    try:
        nids = [max(spc.node_ids) for spc in cards]
    except ValueError:
        for spc in cards:
            try:
                max(spc.node_ids)
            except ValueError:
                print(spc)
                raise
    max_nid = max(nids)

    if max_spc_id > 99999999:
        raise SixtyFourBitError(f'64-bit OP2 writing is not supported; max spc_id={max_spc_id}')
    if max_nid > 99999999:
        raise SixtyFourBitError(f'64-bit OP2 writing is not supported; max SPC nid={max_nid}')

    nfields = 0
    fields = []  # type: List[int]
    data = defaultdict(list)  # type: Dict[Tuple[int, int], List[int]]
    for spc in cards:
        data_key = (spc.conid, int(spc.components))
        ids = spc.node_ids
        data[data_key] += ids

    for data_key, nodes in data.items():
        conid, components = data_key
        singles, doubles = collapse_thru_packs(nodes)
        nsingles = len(singles)
        ndoubles = len(doubles)
        if nsingles:
            nfields += 4 + nsingles # conid, components, thru_flag, singles, -1
            fields += [conid, components, 0, ] + singles
            fields.append(-1)
        if ndoubles:
            for double in doubles:
                fields += [conid, components, 1, double[0], double[2], -1] # + doubles
            nfields += 6 * ndoubles
        assert len(fields) == nfields

    nbytes = write_header_nvalues(card_type, nfields, key, op2_file, op2_ascii)
    op2_file.write(pack(endian + b'%ii' % nfields, *fields))
    return nbytes
Ejemplo n.º 2
0
def _write_spc(card_type: str, cards, ncards: int, op2_file, op2_ascii,
               endian: bytes, nastran_format: str='nx') -> int:
    """writes an SPC"""
    key = (5501, 55, 16)
    #nastran_format = 'msc'
    max_spc_id = max([spc.conid for spc in cards])
    max_nid = max([max(spc.node_ids) for spc in cards])
    if max_spc_id > 99999999:
        raise SixtyFourBitError(f'64-bit OP2 writing is not supported; max spc_id={max_spc_id}')
    if max_nid > 99999999:
        raise SixtyFourBitError(f'64-bit OP2 writing is not supported; max SPC nid={max_nid}')

    data = []  # type: List[Union[int, float]]
    if nastran_format == 'msc':
        # MSC
        # SPC(5501,55,16) - Record 44
        #
        # 1 SID   I    Set identification number
        # 2 ID    I    Grid or scalar point identification number
        # 3 C     I    Component numbers
        # 4 UNDEF none Not used
        # 5 D     RX   Enforced displacement
        nfields = 5
        nbytes = write_header(card_type, nfields, ncards, key, op2_file, op2_ascii)
        for spc in cards:
            node_ids = spc.node_ids
            for nid, comp, enforcedi in zip(node_ids, spc.components, spc.enforced):
                datai = [spc.conid, nid, int(comp), 0, enforcedi]
            op2_ascii.write('  SPC data=%s\n' % str(datai))
            data += datai
        nfields = len(data)
        nbytes = write_header_nvalues(card_type, nfields, key, op2_file, op2_ascii)
        fmt = endian + b'4if' * (nfields // 5)
        op2_file.write(pack(fmt, *data))

    elif nastran_format == 'nx':
        # NX
        # SPC(5501,55,16) - Record 44
        #
        # 1 SID I  Set identification number
        # 2 ID  I  Grid or scalar point identification number
        # 3 C   I  Component numbers
        # 4 D   RS Enforced displacement
        for spc in cards:
            node_ids = spc.node_ids
            #assert len(node_ids) == 1, spc.get_stats()
            datai = []
            for nid, comp, enforcedi in zip(node_ids, spc.components, spc.enforced):
                datai = [spc.conid, nid, int(comp), enforcedi]
                op2_ascii.write('  SPC data=%s\n' % str(datai))

            data += datai
        nfields = len(data)
        nbytes = write_header_nvalues(card_type, nfields, key, op2_file, op2_ascii)
        fmt = endian + b'3if' * (nfields // 4)
        op2_file.write(pack(fmt, *data))
    else:  # pragma: no cover
        raise RuntimeError(f'nastran_format={nastran_format} not msc, nx')
    return nbytes
Ejemplo n.º 3
0
def write_geom2(op2, op2_ascii, obj, endian=b'<'):
    if not hasattr(obj, 'elements'):
        return
    #if not hasattr(obj, 'nodes'):
        #return
    nspoints = len(obj.spoints)
    nplotels = len(obj.plotels)
    nelements = len(obj.elements)
    if nelements == 0 and nplotels == 0 and nspoints == 0:
        return
    write_geom_header(b'GEOM2', op2, op2_ascii)
    itable = -3

    #etypes = [
        #'CROD', 'CONROD',
        #'CELAS1', 'CELAS2', 'CELAS3', 'CELAS4',
        #'CDAMP1', 'CDAMP2', 'CDAMP3', 'CDAMP4',
        #'CTRIA3', 'CQUAD4',
        #'CTETRA', 'CHEXA', 'CPENTA',
    #]
    etypes_to_skip = [
        'CHBDYE', 'CBEND',
        #'CHBDYP',
    ]
    out = defaultdict(list)
    for eid, element in obj.elements.items():
        out[element.type].append(eid)
    if nspoints:
        out['SPOINT'] = list(obj.spoints.keys())
    if nplotels:
        out['PLOTEL'] = list(obj.plotels.keys())

    # elements with fixed lengths
    mapper = {
        # key, spack, nfields
        'CHBDYP' : ((10908, 109, 407), b'12i 3f', 15),
        'CHBDYG' : ((10808, 108, 406), b'16i', 16),
        'PLOTEL' : ((5201, 52, 11), b'3i', 3),
        'CTUBE' : ((3701, 37, 49), b'4i', 4),
        'CSHEAR' : ((3101, 31, 61), b'6i', 6),
        'CQUAD4' : ((2958, 51, 177), b'6iffii4f', 14),
        'CTRIA3' : ((5959, 59, 282), b'5iff3i3f', 13),
        'CQUADR' : ((8009, 80, 367), b'6iffii4f', 14),  # same as CQUAD4
        'CTRIAR' : ((9200, 92, 385), b'5iff3i3f', 13),  # same as CTRIA3
        'CQUAD8' : ((4701, 47, 326), b'10i 6f i', 17),  # current; not 2001
        'CTRIA6' : ((4801, 48, 327), b'8i 5f i', 14),  # current; not 2001
        'CTRIAX' : ((10108, 101, 512), b'9i', 9),
        'CTRIAX6' : ((6108, 61, 107), b'8i f ii', 11),
        'CQUAD' : ((9108, 91, 507), b'11i', 11),
        'CQUADX' : ((9008, 90, 508), b'11i', 11),  # same as CQUAD
        'CROD' : ((3001, 30, 48), b'4i', 4),
        'CONROD' : ((1601, 16, 47), b'4i4f', 8),

        'CDAMP1' : ((201, 2, 69), b'6i', 6),
        'CDAMP2' : ((301, 3, 70), b'if4i', 6),
        'CDAMP3' : ((401, 4, 71), b'4i', 4),
        'CDAMP4' : ((501, 5, 72), b'ifii', 4),
        'CDAMP5' : ((10608, 106, 404), b'ifii', 4),

        'CELAS1' : ((601, 6, 73), b'6i', 6),
        'CELAS2' : ((701, 7, 74), b'if4iff', 8),
        'CELAS3' : ((801, 8, 75), b'4i', 4),
        'CELAS4' : ((901, 9, 76), b'ifii', 4),

        'CVISC' : ((3901, 39, 50), b'4i', 4),
        'CTRAX3' : ((6111, 61, 996), b'5if', 6),
        'CQUADX4' : ((6112, 61, 997), b'6if', 7),
        'CQUADX8' : ((6114, 61, 999), b'10if', 11),
        'CTRAX6' : ((6113, 61, 998), b'8if', 9),
    }
    for name, eids in sorted(out.items()):
        nelements = len(eids)
        if name in etypes_to_skip:
            obj.log.warning('skipping GEOM2-%s' % name)
            continue

        max_eid_id = max(eids)
        if max_eid_id > 99999999:
            raise SixtyFourBitError(f'64-bit OP2 writing is not supported; {name}: max eid={max_eid_id}')
        #if max_nid > 99999999:
            #raise SixtyFourBitError(f'64-bit OP2 writing is not supported; max SPC nid={max_nid}')

        #if nelements == 0:
            #continue
        #if name not in etypes:
            #obj.log.warning('skipping GEOM2-%s' % name)
            #continue

        if name in ['CTETRA', 'CHEXA', 'CPENTA', 'CPYRAM']:
            itable = _write_solid(obj, name, eids, nelements, itable, op2, op2_ascii, endian)
            continue

        if name in mapper:
            key, spacki, nfields = mapper[name]
            spack = Struct(endian + spacki)
            #print(name, spacki)
        elif name == 'CBAR':
            itable = _write_cbar(obj, name, eids, nelements, itable, op2, op2_ascii, endian)
            continue
        elif name == 'CBEAM':
            itable = _write_cbeam(obj, name, eids, nelements, itable, op2, op2_ascii, endian)
            continue
        elif name == 'CBUSH':
            key = (2608, 26, 60)
            spack = None
            nfields = 14
        elif name == 'CBUSH1D':
            key = (5608, 56, 218)
            spack = Struct(endian + b'8i')
            nfields = 8
        elif name == 'CGAP':
            key = (1908, 19, 104)
            spack = None
            nfields = 9
        elif name == 'SPOINT':
            key = (5551, 49, 105)
            spack = None
            nfields = 1
        else:
            obj.log.warning('skipping %s' % name)
            continue
        #else:  # pragma: no cover
            #raise NotImplementedError(name)

        #if self.is_debug_file:
            #self.binary_debug.write('ndata=%s\n' % (nelements * 44))

        nbytes = _write_intermediate_block(name, key, nfields, nelements, op2, op2_ascii)

        try:
            write_card(name, eids, spack, obj, op2, op2_ascii, endian)
        except:
            obj.log.error('failed GEOM2-%s' % name)
            raise
        itable = _write_end_block(nbytes, itable, op2, op2_ascii)

    #-------------------------------------
    #print('itable', itable)
    close_geom_table(op2, op2_ascii, itable)
Ejemplo n.º 4
0
def write_card(name, eids, spack, obj, op2, op2_ascii, endian):
    """writes the GEOM2 elements"""
    op2_ascii.write('GEOM2-%s\n' % name)

    eid_max = max(eids)
    if eid_max > 99999999:
        raise SixtyFourBitError(f'64-bit OP2 writing is not supported; {name} max(eid)={eid_max}')

    if name == 'CHBDYP':
        _write_chbdyp(eids, spack, obj, op2, op2_ascii)
    elif name == 'CHBDYG':
        _write_chbdyg(eids, spack, obj, op2, op2_ascii)

    elif name == 'PLOTEL':
        for eid in sorted(eids):
            elem = obj.plotels[eid]
            nids = elem.node_ids
            #(eid, n1, n2) = out
            data = [eid] + nids
            op2_ascii.write('  eid=%s nids=%s\n' % (eid, str(nids)))
            op2.write(spack.pack(*data))
    elif name == 'CBUSH':
        _write_cbush(eids, spack, obj, op2, op2_ascii, endian)

    elif name == 'CBUSH1D':
        _write_cbush1d(eids, spack, obj, op2, op2_ascii, endian)
    elif name == 'CGAP':
        _write_cgap(eids, spack, obj, op2, op2_ascii, endian)

    elif name in ['CQUAD4', 'CQUADR']:
        for eid in sorted(eids):
            elem = obj.elements[eid]
            nids = elem.node_ids
            pid = elem.pid
            #(eid, pid, n1, n2, n3, n4, theta, zoffs, blank, tflag,
             #t1, t2, t3, t4) = out
            theta = get_theta_from_theta_mcid(elem.theta_mcid)
            tflag = elem.tflag
            #if tflag is None:
                #tflag =
            t1 = elem.T1 if elem.T1 is not None else -1.
            t2 = elem.T2 if elem.T2 is not None else -1.
            t3 = elem.T3 if elem.T3 is not None else -1.
            t4 = elem.T4 if elem.T4 is not None else -1.
            data = [eid, pid] + nids + [theta, elem.zoffset, 0,
                                        tflag, t1, t2, t3, t4]
            assert tflag in [0, 1], elem.get_stats()
            #print('  CQUAD4 eid=%s pid=%s nids=%s data=%s\n' % (eid, pid, str(nids), data[6:]))
            op2_ascii.write('  eid=%s pid=%s nids=%s\n' % (eid, pid, str(nids)))
            assert None not in data, '  %s eid=%s pid=%s nids=%s\n%s' % (name, eid, pid, str(nids), data)
            op2.write(spack.pack(*data))
    elif name == 'CQUAD8':  # current; not 2001
        for eid in sorted(eids):
            elem = obj.elements[eid]
            nids = [nid if nid is not None else 0
                    for nid in elem.node_ids]
            pid = elem.pid
             #(eid, pid, n1, n2, n3, n4, n5, n6, n7, n8, t1, t2,
              #t3, t4, theta, zoffs, tflag) = out # current
            #(eid, pid, n1, n2, n3, n4, n5, n6, n7, n8,
            #t1, t2, t3, t4, theta, zoffs) = out  # cquad8; 2001
            theta = get_theta_from_theta_mcid(elem.theta_mcid)
            tflag = elem.tflag if elem.tflag is not None else 0
            t1 = elem.T1 if elem.T1 is not None else -1.
            t2 = elem.T2 if elem.T2 is not None else -1.
            t3 = elem.T3 if elem.T3 is not None else -1.
            t4 = elem.T4 if elem.T4 is not None else -1.
            data = [eid, pid] + nids + [t1, t2, t3, t4,
                                        theta, elem.zoffset, tflag]
            assert None not in data, '%s data=%s' % (name, data)
            assert isinstance(elem.tflag, int), elem.get_stats()
            assert elem.tflag in [-1, 0, 1], elem.get_stats()
            #print('  CQUAD8 eid=%s pid=%s nids=%s data=%s\n' % (eid, pid, str(nids), data[6:]))
            op2_ascii.write('  eid=%s pid=%s nids=%s\n' % (eid, pid, str(nids)))
            op2.write(spack.pack(*data))
    elif name == 'CTRIA6':  # current; not 2001
        for eid in sorted(eids):
            elem = obj.elements[eid]
            nids = [nid if nid is not None else 0
                    for nid in elem.node_ids]
            pid = elem.pid
            #(eid, pid, n1, n2, n3, n4, n5, n6, theta, zoffs, t1, t2, t3, tflag) = out
            theta = get_theta_from_theta_mcid(elem.theta_mcid)
            t1 = elem.T1 if elem.T1 is not None else -1.
            t2 = elem.T2 if elem.T2 is not None else -1.
            t3 = elem.T3 if elem.T3 is not None else -1.
            data = [eid, pid] + nids + [t1, t2, t3,
                                        theta, elem.zoffset, elem.tflag]
            assert None not in data, '%s data=%s' % (name, data)
            assert elem.tflag in [-1, 0, 1], elem.get_stats()
            #print('  CQUAD4 eid=%s pid=%s nids=%s data=%s\n' % (eid, pid, str(nids), data[6:]))
            op2_ascii.write('  eid=%s pid=%s nids=%s\n' % (eid, pid, str(nids)))
            op2.write(spack.pack(*data))
    elif name == 'CTRIAX':
        for eid in sorted(eids):
            elem = obj.elements[eid]
            nids = [nid if nid is not None else 0
                    for nid in elem.node_ids]
            pid = elem.pid
            #eid, pid, n1, n2, n3, n4, n5, n6, unused_undef1 = data
            data = [eid, pid] + nids + [0]
            assert None not in data, '%s data=%s' % (name, data)
            #print('  CTRIAX eid=%s mid=%s nids=%s data=%s\n' % (eid, pid, str(nids), data[6:]))
            op2_ascii.write('  eid=%s pid=%s nids=%s\n' % (eid, pid, str(nids)))
            op2.write(spack.pack(*data))

    elif name == 'CTRIAX6':
        for eid in sorted(eids):
            elem = obj.elements[eid]
            nids = [nid if nid is not None else 0
                    for nid in elem.node_ids]
            mid = elem.mid
            #eid, mid, n1, n2, n3, n4, n5, n6, theta, unused_undef1, unused_undef2 = data
            data = [eid, mid] + nids + [elem.theta, 0, 0]
            assert None not in data, '%s data=%s' % (name, data)
            #print('  CTRIAX6 eid=%s mid=%s nids=%s data=%s\n' % (eid, mid, str(nids), data[6:]))
            op2_ascii.write('  eid=%s mid=%s nids=%s\n' % (eid, mid, str(nids)))
            op2.write(spack.pack(*data))

    elif name in ['CQUAD', 'CQUADX']:
        for eid in sorted(eids):
            elem = obj.elements[eid]
            nids = [nid if nid is not None else 0
                    for nid in elem.node_ids]
            pid = elem.pid
            #(eid, pid, n1, n2, n3, n4, n5, n6, n7, n8, n9) = out
            data = [eid, pid] + nids
            assert None not in data, '%s data=%s' % (name, data)
            op2_ascii.write('  eid=%s pid=%s nids=%s\n' % (eid, pid, str(nids)))
            op2.write(spack.pack(*data))

    elif name in ['CTRIA3', 'CTRIAR']:
        for eid in sorted(eids):
            elem = obj.elements[eid]
            nids = elem.node_ids
            pid = elem.pid
            theta = get_theta_from_theta_mcid(elem.theta_mcid)
            t1 = elem.T1 if elem.T1 is not None else -1.
            t2 = elem.T2 if elem.T2 is not None else -1.
            t3 = elem.T3 if elem.T3 is not None else -1.
            #eid, pid, n1, n2, n3, theta_mcid, zoffs, blank1, blank2, tflag, t1, t2, t3
            data = [eid, pid] + nids + [theta, elem.zoffset, 0, 0,
                                        elem.tflag, t1, t2, t3]
            assert elem.tflag in [0, 1], elem.get_stats()
            op2_ascii.write('  eid=%s pid=%s nids=%s\n' % (eid, pid, str(nids)))
            op2.write(spack.pack(*data))
    elif name in ['CTRAX3', 'CTRAX6', 'CQUADX4', 'CQUADX8']:
        for eid in sorted(eids):
            elem = obj.elements[eid]
            nids = elem.node_ids
            pid = elem.pid
            data = [eid, pid] + nids + [elem.theta]
            assert None not in data, '  eid=%s pid=%s nids=%s theta=%r\n' % (eid, pid, str(nids), elem.theta)
            #print('  eid=%s pid=%s nids=%s theta=%r\n' % (eid, pid, str(nids), elem.theta))
            op2_ascii.write('  eid=%s pid=%s nids=%s theta=%r\n' % (eid, pid, str(nids), elem.theta))
            op2.write(spack.pack(*data))


    elif name in ['CROD', 'CTUBE', 'CVISC', 'CSHEAR']:
        for eid in sorted(eids):
            elem = obj.elements[eid]
            nids = elem.node_ids
            pid = elem.pid
            data = [eid, pid] + nids
            #print(data)
            op2_ascii.write('  eid=%s pid=%s nids=%s\n' % (eid, pid, str(nids)))
            op2.write(spack.pack(*data))
    elif name == 'CONROD':
        for eid in sorted(eids):
            elem = obj.elements[eid]
            nids = elem.node_ids
            #(eid, n1, n2, mid, a, j, c, nsm) = out
            data = [eid] + nids + [elem.mid, elem.A, elem.j, elem.c, elem.nsm]
            op2_ascii.write('  eid=%s nids=%s\n' % (eid, str(nids)))
            op2.write(spack.pack(*data))
    elif name in ['CELAS1', 'CDAMP1']:
        for eid in sorted(eids):
            elem = obj.elements[eid]
            n1, n2 = [nid if nid else 0 for nid in elem.node_ids]
            pid = elem.pid
            #(eid, pid, g1, g2, c1, c2)
            data = [eid, pid, n1, n2, elem.c1, elem.c2]
            #print(name, data)
            op2_ascii.write('  eid=%s pid=%s nids=[%s, %s]\n' % (eid, pid, n1, n2))
            op2.write(spack.pack(*data))
    elif name == 'CELAS2':
        for eid in sorted(eids):
            elem = obj.elements[eid]
            n1, n2 = [nid if nid else 0 for nid in elem.node_ids]
            #(eid, k, g1, g2, c1, c2, ge, s) = out
            c2 = elem.c2 if elem.c2 is not None else 0
            data = [eid, elem.k, n1, n2, elem.c1, c2, elem.ge, elem.s]
            #print('CELAS2', data)
            op2_ascii.write('  eid=%s nids=[%s, %s]\n' % (eid, n1, n2))
            op2.write(spack.pack(*data))
    elif name in ['CELAS3', 'CDAMP3', 'CDAMP5']:
        for eid in sorted(eids):
            elem = obj.elements[eid]
            n1, n2 = [nid if nid else 0 for nid in elem.node_ids]
            pid = elem.pid
            #(eid, pid, s1, s2) = out
            data = [eid, pid, n1, n2]
            #print(name, data)
            op2_ascii.write('  eid=%s pid=%s nids=[%s, %s]\n' % (eid, pid, n1, n2))
            op2.write(spack.pack(*data))
    elif name == 'CELAS4':
        for eid in sorted(eids):
            elem = obj.elements[eid]
            n1, n2 = [nid if nid else 0 for nid in elem.node_ids]
            #(eid, k, s1, s2) = out
            data = [eid, elem.k, n1, n2]
            #print(data)
            op2_ascii.write('  eid=%s nids=[%s, %s]\n' % (eid, n1, n2))
            op2.write(spack.pack(*data))
    elif name == 'CDAMP2':
        for eid in sorted(eids):
            elem = obj.elements[eid]
            n1, n2 = [nid if nid else 0 for nid in elem.node_ids]
            #(eid, bdamp, g1, g2, c1, c2) = out
            c1 = elem.c1 if elem.c1 is not None else 0
            c2 = elem.c2 if elem.c2 is not None else 0
            data = [eid, elem.b, n1, n2, c1, c2]
            #print(name, data)
            op2_ascii.write('  eid=%s nids=[%s, %s]\n' % (eid, n1, n2))
            op2.write(spack.pack(*data))
    elif name == 'CDAMP4':
        for eid in sorted(eids):
            elem = obj.elements[eid]
            n1, n2 = [nid if nid else 0 for nid in elem.node_ids]
            #(eid, b, s1, s2) = out
            data = [eid, elem.b, n1, n2]
            #print(name, data)
            op2_ascii.write('  eid=%s nids=[%s, %s]\n' % (eid, n1, n2))
            op2.write(spack.pack(*data))
    elif name == 'SPOINT':
        nids = eids
        nids.sort()
        spack = Struct('%ii' % len(nids))
        op2_ascii.write('  spoints%s\n' % str(nids))
        op2.write(spack.pack(*nids))
    else:  # pragma: no cover
        raise NotImplementedError(name)
    def write_op2(self,
                  op2_file,
                  fascii,
                  itable,
                  new_result,
                  date,
                  is_mag_phase=False,
                  endian='>'):
        """writes an OP2"""
        import inspect
        allowed_tables = [
            'OUGV1',
            'BOUGV1',
            'BOPHIG',
            'BOPG1',
            'OUPV1',
            'OUXY1',  # solution set
            'OQP1',
            'OQMG1',
            'OQG1',
            'OQGV1',
            'OPNL1',
            'OPG1',
            'OPGV1',
            'OUGCRM1',
            'OUGNO1',
            'OUGPSD1',
            'OUGRMS1',  # disp/vel/acc/eigenvector
            'OAGATO1',
            'OAGCRM1',
            'OAGNO1',
            'OAGPSD1',
            'OAGRMS1',  # acceleration
            'OPGNO1',
            'OPGRMS1',  # load vector
            'OQGPSD1',
            'OCRPG',
            'OCRUG',
            'OUG1',
            'OUGV1PAT',
            'RADCONS',
            'RADEATC',
            'RADEFFM',
        ]
        assert self.table_name in allowed_tables, self.table_name

        frame = inspect.currentframe()
        call_frame = inspect.getouterframes(frame, 2)
        fascii.write('%s.write_op2: %s\n' %
                     (self.__class__.__name__, call_frame[1][3]))

        if itable == -1:
            self._write_table_header(op2_file, fascii, date)
            itable = -3

        #print('nonlinear_factor =', self.nonlinear_factor)
        if self.is_sort1:
            op2_format = endian + b'2i6f'
        else:
            raise NotImplementedError('SORT2')
        s = Struct(op2_format)

        node = self.node_gridtype[:, 0]
        gridtype = self.node_gridtype[:, 1]
        max_id = node.max()
        if max_id > 99999999:
            raise SixtyFourBitError(
                f'64-bit OP2 writing is not supported; max id={max_id}')

        #format_table4_1 = Struct(self._endian + b'15i')
        #format_table4_2 = Struct(self._endian + b'3i')

        # table 4 info
        #ntimes = self.data.shape[0]
        nnodes = self.data.shape[1]
        nnodes_device = self.node_gridtype[:, 0] * 10 + self.device_code

        #(2+6) => (node_id, gridtypei, t1i, t2i, t3i, r1i, r2i, r3i)
        ntotal = nnodes * (2 + 6)

        #print('shape = %s' % str(self.data.shape))
        #assert nnodes > 1, nnodes
        assert ntotal > 1, ntotal

        unused_device_code = self.device_code
        fascii.write('  ntimes = %s\n' % self.ntimes)

        #fmt = '%2i %6f'
        #print('ntotal=%s' % (ntotal))
        for itime in range(self.ntimes):
            self._write_table_3(op2_file, fascii, new_result, itable, itime)

            # record 4
            itable -= 1
            header = [4, itable, 4, 4, 1, 4, 4, 0, 4, 4, ntotal, 4, 4 * ntotal]
            op2_file.write(pack(b'%ii' % len(header), *header))
            fascii.write('r4 [4, 0, 4]\n')
            fascii.write('r4 [4, %s, 4]\n' % (itable))
            fascii.write('r4 [4, %i, 4]\n' % (4 * ntotal))

            t1 = self.data[itime, :, 0]
            t2 = self.data[itime, :, 1]
            t3 = self.data[itime, :, 2]
            r1 = self.data[itime, :, 3]
            r2 = self.data[itime, :, 4]
            r3 = self.data[itime, :, 5]

            for node_id, gridtypei, t1i, t2i, t3i, r1i, r2i, r3i in zip(
                    nnodes_device, gridtype, t1, t2, t3, r1, r2, r3):
                data = [node_id, gridtypei, t1i, t2i, t3i, r1i, r2i, r3i]
                fascii.write(
                    '  nid, grid_type, dx, dy, dz, rx, ry, rz = %s\n' % data)
                op2_file.write(s.pack(*data))

            itable -= 1
            header = [
                4 * ntotal,
            ]
            op2_file.write(pack(b'i', *header))
            fascii.write('footer = %s\n' % header)
            new_result = False
        return itable
Ejemplo n.º 6
0
    def write_op2(self,
                  op2_file,
                  op2_ascii,
                  itable,
                  new_result,
                  date,
                  is_mag_phase=False,
                  endian='>'):
        """writes an OP2"""
        import inspect
        from struct import Struct, pack
        frame = inspect.currentframe()
        call_frame = inspect.getouterframes(frame, 2)
        op2_ascii.write(
            f'{self.__class__.__name__}.write_op2: {call_frame[1][3]}\n')

        if itable == -1:
            self._write_table_header(op2_file, op2_ascii, date)
            itable = -3

        nnodes, is_bilinear = self.get_nnodes_bilinear()
        if is_bilinear:
            nnodes_all = nnodes + 1
            ntotal = 2 + 17 * nnodes_all
        else:
            nnodes_all = nnodes
        #print("nnodes_all =", nnodes_all)
        #cen_word_ascii = f'CEN/{nnodes:d}'
        cen_word_bytes = b'CEN/'
        idtype = self.element_node.dtype
        fdtype = self.data.dtype
        if self.size == 4:
            pass
        else:
            print(f'downcasting {self.class_name}...')
            #cen_word_bytes = b'CEN/    '
            idtype = np.int32(1)
            fdtype = np.float32(1.0)

        #msg.append(f'  element_node.shape = {self.element_node.shape}\n')
        #msg.append(f'  data.shape={self.data.shape}\n')

        eids = self.element_node[:, 0]
        nids = self.element_node[:, 1]
        max_id = self.element_node.max()
        if max_id > 99999999:
            raise SixtyFourBitError(
                f'64-bit OP2 writing is not supported; max id={max_id}')

        eids_device = eids * 10 + self.device_code

        nelements = len(np.unique(eids))
        nlayers = len(eids)

        #print('nelements =', nelements)
        #print('nlayers =', nlayers)
        nnodes_per_element = nlayers // nelements // 2
        # 21 = 1 node, 3 principal, 6 components, 9 vectors, 2 p/ovm
        #ntotal = ((nnodes * 21) + 1) + (nelements * 4)

        ntotali = self.num_wide
        ntotal = ntotali * nelements
        assert nnodes > 1, nnodes

        op2_ascii.write(f'  ntimes = {self.ntimes}\n')

        #[fiber_dist, oxx, oyy, txy, angle, majorP, minorP, ovm]
        op2_ascii.write(
            '  #elementi = [eid_device, fd1, sx1, sy1, txy1, angle1, major1, minor1, vm1,\n'
        )
        op2_ascii.write(
            '  #                        fd2, sx2, sy2, txy2, angle2, major2, minor2, vm2,]\n'
        )  # 1+16

        op2_ascii.write(
            '  #elementi = [eid_device, node1, fd1, sx1, sy1, txy1, angle1, major1, minor1, vm1,\n'
        )
        op2_ascii.write(
            '  #                               fd2, sx2, sy2, txy2, angle2, major2, minor2, vm2,]\n'
        )  # 1 + 17*5
        op2_ascii.write(
            '  #elementi = [            node2, fd1, sx1, sy1, txy1, angle1, major1, minor1, vm1,\n'
        )
        op2_ascii.write(
            '  #                               fd2, sx2, sy2, txy2, angle2, major2, minor2, vm2,]\n'
        )  # 17
        op2_ascii.write(
            '  #elementi = [            node3, fd1, sx1, sy1, txy1, angle1, major1, minor1, vm1,\n'
        )
        op2_ascii.write(
            '  #                               fd2, sx2, sy2, txy2, angle2, major2, minor2, vm2,]\n'
        )  # 17
        op2_ascii.write(
            '  #elementi = [            node4, fd1, sx1, sy1, txy1, angle1, major1, minor1, vm1,\n'
        )
        op2_ascii.write(
            '  #                               fd2, sx2, sy2, txy2, angle2, major2, minor2, vm2,]\n'
        )  # 17
        op2_ascii.write(
            '  #elementi = [            node5, fd1, sx1, sy1, txy1, angle1, major1, minor1, vm1,\n'
        )
        op2_ascii.write(
            '  #                               fd2, sx2, sy2, txy2, angle2, major2, minor2, vm2,]\n'
        )  # 17

        if not self.is_sort1:
            raise NotImplementedError('SORT2')

        #struct_isi8f = Struct('i 4s i 8f')
        #struct_i8f = Struct(endian + b'i8f')
        #struct_8f = Struct(endian + b'8f')

        nelements_nnodes = len(nids) // 2
        is_centroid = self.element_type in [33, 74, 227, 228]
        is_nodes = self.element_type in [64, 70, 75, 82, 144]
        if is_centroid:
            eids_device2 = to_column_bytes([eids_device[::2]],
                                           idtype).view(fdtype)
            assert len(eids_device2) == nelements
        elif is_nodes:
            cen_word_array_temp = np.full((nelements, 1), cen_word_bytes)
            cen_word_array = cen_word_array_temp.view(fdtype)
            eids_device2 = view_dtype(
                eids_device[::2 * nnodes_per_element].reshape(nelements, 1),
                fdtype)
            nids2 = view_dtype(nids[::2].reshape(nelements_nnodes, 1), fdtype)

        #nheader = 15
        struct_i = Struct('i')
        struct_13i = Struct('13i')
        op2_ascii.write(f'nelements={nelements:d}\n')
        for itime in range(self.ntimes):
            self._write_table_3(op2_file, op2_ascii, new_result, itable, itime)

            # record 4
            #print('stress itable = %s' % itable)
            itable -= 1
            header = [4, itable, 4, 4, 1, 4, 4, 0, 4, 4, ntotal, 4, 4 * ntotal]
            op2_file.write(struct_13i.pack(*header))
            op2_ascii.write('r4 [4, 0, 4]\n')
            op2_ascii.write(f'r4 [4, {itable:d}, 4]\n')
            op2_ascii.write(f'r4 [4, {4 * ntotal:d}, 4]\n')

            if is_centroid:
                # [eid_device, fdi, oxxi, oyyi, txyi, anglei, major, minor, ovmi]
                # [            fdi, oxxi, oyyi, txyi, anglei, major, minor, ovmi]
                datai = view_dtype(
                    self.data[itime, :, :].reshape(nelements, 16), fdtype)
                data_out = np.hstack([eids_device2, datai])
            elif is_nodes:
                # CQUAD8, CTRIAR, CTRIA6, CQUADR, CQUAD4
                # bilinear
                datai = view_dtype(
                    self.data[itime, :, :].reshape(
                        nelements * nnodes_per_element, 16), fdtype)
                nids_data = np.hstack([nids2,
                                       datai]).reshape(nelements,
                                                       nnodes_per_element * 17)
                data_out = np.hstack([eids_device2, cen_word_array, nids_data])
            else:  # pragma: no cover
                msg = f'element_name={self.element_name} element_type={self.element_type}'
                raise NotImplementedError(msg)
            assert data_out.size == ntotal, f'data_out.shape={data_out.shape} size={data_out.size}; ntotal={ntotal}'
            op2_file.write(data_out)

            itable -= 1
            header = [
                4 * ntotal,
            ]

            op2_file.write(struct_i.pack(*header))
            op2_ascii.write('footer = %s\n' % header)
            new_result = False
        return itable
Ejemplo n.º 7
0
def write_geom1(op2, op2_ascii, obj, endian=b'<'):
    #if not hasattr(obj, 'nodes'):
    #return
    if not hasattr(obj, 'nodes'):
        return
    nnodes = len(obj.nodes)
    ncoords = len(obj.coords)
    ngeom1 = nnodes or ncoords
    if not ngeom1:
        return
    write_geom_header(b'GEOM1', op2, op2_ascii)
    itable = -3

    if nnodes:
        #nvalues = nnodes * 8
        #nbytes = nvalues * 4
        #assert nnodes == 72, nnodes
        nfields = 8  # nid, cp, x, y, z, cd, ps, seid
        nvalues = nfields * nnodes + 3  # 3 comes from the keys
        #assert nbytes == 2316, nbytes
        #op2.write(pack('6i', *[4, 0, 4, 4, 1, 4]))

        key = (4501, 45, 1)
        nbytes = write_block(op2, op2_ascii, nvalues, key)

        spack = Struct('ii 3f 3i')
        for unused_nid, node in sorted(obj.nodes.items()):
            xyz = node.xyz
            ps = node.ps
            if ps == '':
                psi = 0
            else:
                psi = int(ps)

            seid = node.seid
            if seid == '':
                seidi = 0
            else:
                seidi = int(seid)
            data = [
                node.nid,
                node.Cp(), xyz[0], xyz[1], xyz[2],
                node.Cd(), psi, seidi
            ]
            op2.write(spack.pack(*data))
            op2_ascii.write(
                '  nid=%s cp=%s xyz=(%s, %s, %s) cd=%s ps=%s seid=%s\n' %
                tuple(data))
        op2.write(pack('i', nbytes))
        itable -= 1
        data = [4, itable, 4, 4, 1, 4, 4, 0, 4]
        op2.write(pack('9i', *data))
        op2_ascii.write(str(data) + '\n')
        #-------------------------------------

    if ncoords:
        out = defaultdict(list)
        for cid, coord in obj.coords.items():
            if coord.type == 'GMCORD':
                obj.log.warning(f'skipping {coord.type}')
                continue
            out[coord.type].append(cid)

        coord_type_key_map = {
            'CORD1C': (1701, 17, 6),
            'CORD1R': (1801, 18, 5),
            'CORD1S': (1901, 19, 7),
            'CORD2C': (2001, 20, 9),
            'CORD2R': (2101, 21, 8),
            'CORD2S': (2201, 22, 10),
            'CORD3G': (14301, 143, 651),
        }
        for coord_type, cids in sorted(out.items()):
            max_cid = max(cids)
            if max_cid > 99999999:  #  is the max 2147483647?  2^31-1
                raise SixtyFourBitError(
                    f'64-bit OP2 writing is not supported; max {coord_type}={max_cid}'
                )

            key = coord_type_key_map[coord_type]
            ncards = len(cids)
            if '2' in coord_type:
                coord_int = 2
            elif '1' in coord_type:
                coord_int = 1
            else:  # pragma: no cover
                raise NotImplementedError(coord_type)

            if coord_type[-1] == 'R':
                coord_rcs_int = 1
            elif coord_type[-1] == 'C':
                coord_rcs_int = 2
            elif coord_type[-1] == 'S':
                coord_rcs_int = 3
            else:  # pragma: no cover
                raise NotImplementedError(coord_type)

            if coord_type in ['CORD2R', 'CORD2C', 'CORD2S']:
                nvalues = 13 * ncards + 3
                spack = Struct(b'4i 9f')
                nbytes = write_block(op2, op2_ascii, nvalues, key)

                for cid in sorted(cids):
                    coord = obj.coords[cid]
                    data = ([
                        cid,
                        coord_rcs_int,
                        coord_int,
                        coord.Rid(),
                    ] + list(coord.e1) + list(coord.e2) + list(coord.e3))
                    op2.write(spack.pack(*data))
                    op2_ascii.write(' cid=%s data=%s' % (cid, str(data[1:])))
            elif coord_type in ['CORD1R', 'CORD1C', 'CORD1S']:
                nvalues = 6 * ncards + 3
                spack = Struct(b'6i')
                nbytes = write_block(op2, op2_ascii, nvalues, key)
                nids = []
                for cid in cids:
                    coord = obj.coords[cid]
                    nids.extend([coord.G1(), coord.G2(), coord.G3()])
                max_nid = max(nids)
                if max_nid > 99999999:
                    raise SixtyFourBitError(
                        f'64-bit OP2 writing is not supported; {coord_type}: max nid={max_nid}'
                    )
                del nids

                for cid in sorted(cids):
                    coord = obj.coords[cid]
                    data = [
                        cid, coord_rcs_int, coord_int,
                        coord.G1(),
                        coord.G2(),
                        coord.G3()
                    ]
                    op2.write(spack.pack(*data))
                    op2_ascii.write(' cid=%s data=%s' % (cid, str(data[1:])))
            else:
                raise NotImplementedError(coord_type)
            op2.write(pack('i', nbytes))
            itable -= 1
            data = [4, itable, 4, 4, 1, 4, 4, 0, 4]
            op2.write(pack('9i', *data))
            op2_ascii.write(str(data) + '\n')

    #_write_markers(op2, op2_ascii, [2, 4])
    #-------------------------------------
    close_geom_table(op2, op2_ascii, itable)