Exporting to GlennHT Connectivity FilesΒΆ

This is an example of how I read a mesh, convert it to ascii, save it, find connectivity, find periodicty and export to glennht format. In this example we will use the file PahtCascade-ASCII

 1from itertools import combinations
 2import os, sys
 3sys.path.insert(0,'../../')
 4from plot3d import write_plot3D, read_plot3D, find_periodicity
 5from plot3d import find_matching_blocks, get_outer_faces, connectivity
 6from glennht_con import export_to_glennht_conn
 7import pickle
 8
 9# Convert to binary because of size
10blocks = read_plot3D('PahtCascade-ASCII.xyz', binary = False)
11write_plot3D('PahtCascade.xyz',blocks,binary=True)
12
13blocks = read_plot3D('PahtCascade.xyz', binary = True, big_endian=True)
14
15if not os.path.exists('connectivity.pickle'):
16    blocks = read_plot3D('PahtCascade.xyz', binary = True, big_endian=True)
17    # Block 1 is the blade O-Mesh k=0
18    # outer_faces, _ = get_outer_faces(blocks[0]) # lets check
19    face_matches, outer_faces_formatted = connectivity(blocks)
20    with open('connectivity.pickle','wb') as f:
21        pickle.dump({"face_matches":face_matches, "outer_faces":outer_faces_formatted},f)
22
23with open('connectivity.pickle','rb') as f:
24    data = pickle.load(f)
25    face_matches = data['face_matches']
26    outer_faces = data['outer_faces']
27
28blocks = read_plot3D('PahtCascade.xyz', binary = True, big_endian=True)
29periodic_surfaces, outer_faces_to_keep = find_periodicity(blocks,outer_faces,periodic_direction='k')
30# Append periodic surfaces to face_matches
31face_matches.extend(periodic_surfaces)
32
33export_to_glennht_conn(face_matches,outer_faces_to_keep,'finalmesh')
  1from typing import List
  2def export_to_glennht_conn(nblocks:int,matches:List,block_surfaces:dict,filename:str,other_settings:dict=None):
  3    """Exports the connectivity to GlennHT format
  4
  5    Args:
  6        matches (dict): Any matching faces between blocks
  7        block_surfaces (dict): Non matching faces of all blocks or surfaces to consider
  8        filename (str): filename to write to
  9        other_settings (dict, optional): contains general interfaces, zones, blocks that are in the zone. Defaults to None.
 10    """
 11
 12    blocks = ['block1','block2'] # Block 1 and Block 2 are arbitrary names. Their index matters
 13    with open(filename + '.ght_conn','w') as fp:
 14        # Print matches
 15        nMatches = len(matches)
 16        fp.write(f'{nMatches}\n') # Print number of matches
 17        for match in matches:
 18            for block in blocks:
 19                block_indx = match[block]['index']+1 # block1 and block2 are arbitrary names, the key is the block index
 20                block_IMIN = match[block]['IMIN']+1
 21                block_JMIN = match[block]['JMIN']+1
 22                block_KMIN = match[block]['KMIN']+1
 23
 24                block_IMAX = match[block]['IMAX']+1
 25                block_JMAX = match[block]['JMAX']+1
 26                block_KMAX = match[block]['KMAX']+1
 27
 28                fp.write(f"{block_indx:3d}\t{block_IMIN:5d} {block_JMIN:5d} {block_KMIN:5d}\t{block_IMAX:5d} {block_JMAX:5d} {block_KMAX:5d}\n")
 29        # Print Surfaces
 30        # Get total number of surfaces
 31        id = 1
 32        lines = list()
 33        for block in block_surfaces:
 34            block_indx = block['index']+1
 35            for surface in block['surfaces']:
 36                IMIN = surface['IMIN']+1
 37                JMIN = surface['JMIN']+1
 38                KMIN = surface['KMIN']+1
 39
 40                IMAX = surface['IMAX']+1
 41                JMAX = surface['JMAX']+1
 42                KMAX = surface['KMAX']+1
 43                lines.append(f"{block_indx:3d}\t{IMIN:5d} {JMIN:5d} {KMIN:5d}\t{IMAX:5d} {JMAX:5d} {KMAX:5d}\t{id:4d}\n")
 44                id+=1
 45
 46        fp.write(f'{len(lines)}\n')
 47        [fp.write(line) for line in lines]
 48
 49        # Write general interfaces
 50        n_gif = len(other_settings['general_interfaces'])
 51        fp.write(f'{n_gif:d}\n')
 52        for gif in other_settings['general_interfaces']:
 53            surf1 = gif['surface_pairs'][0]
 54            surf2 = gif['surface_pairs'][1]
 55            if gif['gif_type'].lower() == 'mixing_plane':
 56                gif_kind = 2
 57            else:
 58                gif_kind = 1 # for conjugate
 59            if gif['is_polar']:
 60                gif_kind*=-1
 61            fp.write(f'{surf1} {surf2} {gif_kind} 1 ') # Assume no new lines and just write on a single line.
 62        if len(other_settings['general_interfaces'])>1:
 63            fp.write('\n')
 64
 65        def zonetype_to_glennht(type:str):
 66            if type == 'fluid':
 67                return '1'
 68            else:                   # Second zone is solid
 69                return '2'
 70
 71        def lookup_zone_index(block_indx:int) -> int:
 72            """Searches through all the zones for the block_index
 73
 74            Args:
 75                block_indx (int): index of block
 76
 77            Returns:
 78                int: the zone index
 79            """
 80            zone_indx = 1
 81            for zone in other_settings['zones']:
 82                if block_indx in zone['blocks']:
 83                    return zone_indx
 84                zone_index += 1
 85
 86        # Write the Zones
 87        n_zones = len(other_settings['zones'])
 88        fp.write(f'{n_zones:d}\n')
 89        for zone in other_settings['zones']:    # Write which zone is fluid and which is solid
 90            fp.write(zonetype_to_glennht(zone['type']) + ' ')
 91        if len(other_settings['zones'])>0:
 92            fp.write('\n')
 93
 94        # Write out which block belongs to what zone index
 95        for block_indx in range(nblocks):
 96            zone_index = lookup_zone_index(block_indx)
 97            if (block_indx+1) % 6 == 0:
 98                fp.write(f'{zone_index:d}')
 99                fp.write('\n')
100            else:
101                fp.write(f'{zone_index:d}' + ' ')