Esempio n. 1
0
def merge_ugrid3d_and_bdf_to_ugrid3d_filename(
        ugrid_filename: str,
        bdf_filename: str,
        ugrid_filename_out: str,
        pshell_pids_to_remove: List[int],
        update_equivalence: bool = True,
        tol: float = 0.01,
        log: Optional[SimpleLogger] = None):
    """
    assumes cid=0

        Parameters
    ----------
    ugrid_filename : str
        the AFLR3/UGrid3d filename
    bdf_filename : str
        the BDF filename
    ugrid_filename_out : str
        the output AFLR3/UGrid3d filename
    pshell_pids_to_remove : List[int, ...]
        ???
    tol : float; default=0.01
        the equivalence tolerance
    update_equivalence : bool; default=True
        calls ``equivalence_ugrid3d_and_bdf_to_bdf`` to equivalence nodes

    """
    #base, ext = os.path.splitext(ugrid_filename_out)
    #bdf_filename = base + '.bdf'
    log = get_logger2(log, debug=True)
    log.debug(
        f'merge_ugrid3d_and_bdf_to_ugrid3d_filename - bdf_filename = {bdf_filename}'
    )
    log.debug(
        f'merge_ugrid3d_and_bdf_to_ugrid3d_filename - ugrid_filename = {ugrid_filename}'
    )

    if update_equivalence:
        bdf_filename2 = equivalence_ugrid3d_and_bdf_to_bdf(
            ugrid_filename,
            bdf_filename,
            pshell_pids_to_remove,
            tol,
            renumber=True,
            log=log)
    else:
        base = os.path.splitext(bdf_filename)[0]
        #bdf_merged_filename = base + '_merged.bdf'
        #bdf_equivalence_filename = base + '_equivalence.bdf'
        bdf_renumber_filename = base + '_renumber.bdf'
        bdf_filename2 = bdf_renumber_filename

    log.debug(f'**** bdf_filename2 = {bdf_filename2}')
    model = read_bdf(bdf_filename2, xref=False, log=log)

    outi = determine_dytpe_nfloat_endian_from_ugrid_filename(ugrid_filename)
    ndarray_float, float_fmt, nfloat, endian, ugrid_filename = outi
    #ndarray_float, float_fmt, nfloat, endian, ugrid_filename

    # more for documentation than anything else
    assert ndarray_float in ['float32', 'float64'], ndarray_float
    assert float_fmt in ['f', 'd'], float_fmt
    assert nfloat in [4, 8], nfloat
    assert endian in ['<', '>'], endian

    cards_to_get = [
        'GRID', 'CTRIA3', 'CQUAD4', 'PSHELL', 'CTETRA', 'CPYRAM', 'CPENTA',
        'CHEXA'
    ]
    out = model.get_card_ids_by_card_types(cards_to_get)
    nids = out['GRID']
    nnodes = len(nids)

    ntris = len(out['CTRIA3'])
    nquads = len(out['CQUAD4'])

    ntets = len(out['CTETRA'])
    npyramids = len(out['CPYRAM'])
    npentas = len(out['CPENTA'])
    nhexas = len(out['CHEXA'])
    nshells = ntris + nquads
    nsolids = ntets + npyramids + npentas + nhexas
    if nshells == 0:
        raise RuntimeError(
            'the UGRID model does not have any boundary condition surfaces...')
    assert nsolids > 0, nsolids

    #nodes = zeros((nnodes, 3), dtype=ndarray_float)

    #tris = zeros((ntris, 3), dtype='int32')
    #quads = zeros((nquads, 4), dtype='int32')

    #tets = zeros((ntetras, 4), dtype='int32')
    #pyramids = zeros((npyramids, 5), dtype='int32')
    #pentas = zeros((npyramids, 6), dtype='int32')
    #hexas = zeros((nhexas, 6), dtype='int32')

    xyz = array([model.nodes[nid].xyz for nid in sorted(nids)],
                dtype=ndarray_float)

    # get the pshells
    #pshells = out['PSHELL']
    # TODO: need to think about property IDs
    tris = out['CTRIA3']
    quads = out['CQUAD4']
    eids = tris + quads
    pids = [model.elements[eid].pid for eid in eids]

    #tris_shrink = [eid for eid, pid in zip(eids[:ntris], pids[:ntris])
    #if pid in pshell_pids_to_save]
    #quads_shrink = [eid for eid, pid in zip(eids[ntris:], pids[ntris:])
    #if pid in pshell_pids_to_save]
    #ntris = len(tris_shrink)
    #nquads = len(quads_shrink)
    nshells = nquads + ntris
    npids = len(pids)
    if not nshells == npids:
        raise RuntimeError('nshells=%s npids=%s; must be the same' %
                           (nshells, npids))

    #pids_shrink = [pidi for pidi in pids
    #if pidi in pshell_pids_to_save]
    pids_shrink = pids

    with open(ugrid_filename_out, 'wb') as f_ugrid:
        #element_ids = hstack([
        #out['CTRIA3'], out['CQUAD4'],
        #out['CTETRA'] + out['CPYRAM'], out['CPENTA'], out['CHEXA']
        #])
        #fmt = '%ii' % (nsolids * nnodes)
        structi = Struct(endian + '7i')
        f_ugrid.write(
            structi.pack(nnodes, ntris, nquads, ntets, npyramids, npentas,
                         nhexas))

        # %3f or %3d
        fmt = endian + '%i%s' % (nnodes * 3, float_fmt)  # len(x,y,z) = 3
        structi = Struct(fmt)
        f_ugrid.write(structi.pack(*xyz.ravel()))

        for card_type in cards_to_get[1:]:  # drop the GRIDs & PSHELLs
            if card_type == 'PSHELL':
                assert len(pids) > 0, 'pids=%s' % pids
                #print('writing %s' % card_type)

                # %10i
                fmt = endian + '%ii' % (nshells)
                structi = Struct(fmt)
                pids = pids_shrink
                f_ugrid.write(structi.pack(*pids))
            elif card_type in ['CTRIA3', 'CQUAD4'] and 0:
                if card_type == 'CTRIA3':
                    eids = tris
                elif card_type == 'CQUAD4':
                    eids = quads

                # if there are cards
                if len(eids):
                    #print('writing %s' % card_type)
                    nelements = len(eids)
                    eid0 = eids[0]

                    # get the 0th card so we can size the formatter
                    element0 = model.elements[eid0]
                    nnodes_per_element = len(element0.nodes)

                    node_ids = array(
                        [model.elements[eid].node_ids for eid in sorted(eids)],
                        dtype='int32')

                    # '%8i'
                    fmt = endian + '%ii' % (nelements * nnodes_per_element)
                    structi = Struct(fmt)
                    f_ugrid.write(structi.pack(*node_ids.ravel()))
            else:
                eids = out[card_type]

                # if there are cards
                if len(eids):
                    #print('writing %s' % card_type)
                    nelements = len(eids)
                    eid0 = eids[0]

                    # get the 0th card so we can size the formatter
                    element0 = model.elements[eid0]
                    nnodes_per_element = len(element0.nodes)

                    node_ids = array(
                        [model.elements[eid].node_ids for eid in sorted(eids)],
                        dtype='int32')
                    # '%8i'
                    fmt = endian + '%ii' % (nelements * nnodes_per_element)
                    structi = Struct(fmt)
                    f_ugrid.write(structi.pack(*node_ids.ravel()))
Esempio n. 2
0
def merge_ugrid3d_and_bdf_to_ugrid3d_filename(ugrid_filename,
                                              bdf_filename,
                                              ugrid_filename_out,
                                              pshell_pids_to_remove,
                                              update_equivalence=True,
                                              tol=0.01):
    """
    assumes cid=0
    """
    #base, ext = os.path.splitext(ugrid_filename_out)
    #bdf_filename = base + '.bdf'
    print('merge_ugrid3d_and_bdf_to_ugrid3d_filename - bdf_filename = ',
          bdf_filename)
    print('merge_ugrid3d_and_bdf_to_ugrid3d_filename - ugrid_filename = ',
          ugrid_filename)

    if update_equivalence:
        bdf_filename2 = equivalence_ugrid3d_and_bdf_to_bdf(
            ugrid_filename,
            bdf_filename,
            pshell_pids_to_remove,
            tol,
            renumber=True)
    else:
        base = os.path.splitext(bdf_filename)[0]
        #bdf_merged_filename = base + '_merged.bdf'
        #bdf_equivalence_filename = base + '_equivalence.bdf'
        bdf_renumber_filename = base + '_renumber.bdf'
        bdf_filename2 = bdf_renumber_filename

    print('**** bdf_filename2 = ', bdf_filename2)
    model = BDF()
    model.read_bdf(bdf_filename2, xref=False)

    outi = determine_dytpe_nfloat_endian_from_ugrid_filename(ugrid_filename)
    ndarray_float, float_fmt, nfloat, endian = outi

    # more for documentation than anything else
    assert ndarray_float in ['float32', 'float64'], ndarray_float
    assert float_fmt in ['f', 'd'], float_fmt
    assert nfloat in [4, 8], nfloat
    assert endian in ['<', '>'], endian

    cards_to_get = [
        'GRID', 'CTRIA3', 'CQUAD4', 'PSHELL', 'CTETRA', 'CPYRAM', 'CPENTA',
        'CHEXA'
    ]
    out = model.get_card_ids_by_card_types(cards_to_get)
    nids = out['GRID']
    nnodes = len(nids)

    ntris = len(out['CTRIA3'])
    nquads = len(out['CQUAD4'])

    ntets = len(out['CTETRA'])
    npyramids = len(out['CPYRAM'])
    npentas = len(out['CPENTA'])
    nhexas = len(out['CHEXA'])
    nshells = ntris + nquads
    nsolids = ntets + npyramids + npentas + nhexas
    assert nshells > 0, nshells
    assert nsolids > 0, nsolids

    #nodes = zeros((nnodes, 3), dtype=ndarray_float)

    #tris = zeros((ntris, 3), dtype='int32')
    #quads = zeros((nquads, 4), dtype='int32')

    #tets = zeros((ntetras, 4), dtype='int32')
    #pyramids = zeros((npyramids, 5), dtype='int32')
    #pentas = zeros((npyramids, 6), dtype='int32')
    #hexas = zeros((nhexas, 6), dtype='int32')

    with open(ugrid_filename_out, 'wb') as f_ugrid:
        #element_ids = hstack([
        #out['CTRIA3'], out['CQUAD4'],
        #out['CTETRA'] + out['CPYRAM'], out['CPENTA'], out['CHEXA']
        #])
        #fmt = '%ii' % (nsolids * nnodes)
        structi = Struct(endian + '7i')
        f_ugrid.write(
            structi.pack(nnodes, ntris, nquads, ntets, npyramids, npentas,
                         nhexas))

        xyz = array([model.nodes[nid].xyz for nid in sorted(nids)],
                    dtype=ndarray_float)

        # %3f or %3d
        fmt = endian + '%i%s' % (nnodes * 3, float_fmt)  # len(x,y,z) = 3
        structi = Struct(fmt)
        print('fmt = %r' % fmt)
        f_ugrid.write(structi.pack(*xyz.ravel()))

        # get the pshells
        #pshells = out['PSHELL']
        # TODO: need to think about property IDs
        tris = out['CTRIA3']
        quads = out['CQUAD4']
        eids = tris + quads
        pids = [model.elements[eid].pid for eid in eids]

        #tris_shrink = [eid for eid, pid in zip(eids[:ntris], pids[:ntris])
        #if pid in pshell_pids_to_save]
        #quads_shrink = [eid for eid, pid in zip(eids[ntris:], pids[ntris:])
        #if pid in pshell_pids_to_save]
        #ntris = len(tris_shrink)
        #nquads = len(quads_shrink)
        nshells = nquads + ntris

        #pids_shrink = [pidi for pidi in pids
        #if pidi in pshell_pids_to_save]
        pids_shrink = pids

        for card_type in cards_to_get[1:]:  # drop the GRIDs & PSHELLs
            if card_type == 'PSHELL':
                print('writing %s' % card_type)

                # %10i
                fmt = endian + '%ii' % (nshells)
                structi = Struct(fmt)
                print('fmt = %r' % fmt)
                pids = pids_shrink
                f_ugrid.write(structi.pack(*pids))
            elif card_type in ['CTRIA3', 'CQUAD4'] and 0:
                if card_type == 'CTRIA3':
                    eids = tris
                elif card_type == 'CQUAD4':
                    eids = quads

                # if there are cards
                if len(eids):
                    print('writing %s' % card_type)
                    nelements = len(eids)
                    eid0 = eids[0]

                    # get the 0th card so we can size the formatter
                    element0 = model.elements[eid0]
                    nnodes_per_element = len(element0.nodes)

                    node_ids = array(
                        [model.elements[eid].node_ids for eid in sorted(eids)],
                        dtype='int32')

                    # '%8i'
                    fmt = endian + '%ii' % (nelements * nnodes_per_element)
                    structi = Struct(fmt)
                    f_ugrid.write(structi.pack(*node_ids.ravel()))
            else:
                eids = out[card_type]

                # if there are cards
                if len(eids):
                    print('writing %s' % card_type)
                    nelements = len(eids)
                    eid0 = eids[0]

                    # get the 0th card so we can size the formatter
                    element0 = model.elements[eid0]
                    nnodes_per_element = len(element0.nodes)

                    node_ids = array(
                        [model.elements[eid].node_ids for eid in sorted(eids)],
                        dtype='int32')
                    # '%8i'
                    fmt = endian + '%ii' % (nelements * nnodes_per_element)
                    print('fmt = %r' % fmt)
                    structi = Struct(fmt)
                    f_ugrid.write(structi.pack(*node_ids.ravel()))