def write_pvd_file(unstructured_grid, program_data):
    if is_rank_zero():
        print("Writing pvd file")
    time_steps = program_data["time_steps"]
    output_prefix = program_data["paraview_output_file"]
    fh = open(output_prefix + ".pvd", "w")
    fh.write('<?xml version="1.0"?>\n')
    fh.write('<VTKFile type="Collection" version="0.1"\n')
    fh.write('               byte_order="LittleEndian"\n')
    fh.write('               compressor="vtkZLibDataCompressor">\n')
    fh.write('   <Collection>    \n')

    array_names = get_array_names_on_unstructured_grid(unstructured_grid)

    if time_steps:
        for time in sorted(time_steps.keys()):
            write_pvd_time_step(fh, time, array_names, program_data)
    else:
        time = 0
        write_pvd_time_step(fh, time, array_names, program_data)

    fh.write('   </Collection>    \n')
    fh.write('</VTKFile>    \n')
    fh.close()
    if is_rank_zero():
        print("Finished writing pvd file")
def create_grid_from_grid_files(program_data):
    grid_file = program_data["grid_desc"]["read_grid"]
    time_steps = program_data["time_steps"]
    sgf = SpartaGridFile(grid_file)
    merge_points = create_merge_points(program_data)
    unstructured_grid = vtk.vtkUnstructuredGrid()
    unstructured_grid.SetPoints(merge_points.GetPoints())
    init_flow_file_arrays(unstructured_grid, program_data)
    global_ids = {}
    count = 1
    if is_rank_zero():
        print("Started sorted grid file read")
        print("Creating grid")
    with open(get_bucket_file_name(get_rank(), program_data), 'r') as f:
        for cell_id in f:
            cell_id = cell_id.strip()
            cell = create_grid_cell(cell_id, sgf, program_data, merge_points)
            unstructured_grid.InsertNextCell(cell.GetCellType(),
                                             cell.GetPointIds())
            if time_steps:
                lcid = sgf.get_local_cell_id_from_dashed_cell_id(cell_id)
                global_ids[lcid] = count - 1
            insert_value_in_flow_file_arrays(0.0, unstructured_grid)
            if is_rank_zero() and count % 10000 == 0:
                print("Read " + str(count) + " cells")
            count += 1
    if is_rank_zero():
        print("Finished sorted grid file read")
        print("Finished creating grid")
    return (unstructured_grid, global_ids)
示例#3
0
def sort_grid_file_to_files(grid_file_path):
    cells = get_grid_file_cells(grid_file_path)
    prefix = get_grid_file_prefix(grid_file_path)
    if is_rank_zero():
        print("Started parallel sort")
    parallel_sort_to_file_buckets(cells,
        SpartaGridFile.compare_dashed_ids, prefix)
    if is_rank_zero():
        print("Finished parallel sort")
def main():
    args = parse_command_line()
    check_command_line(args)
    time_steps = get_time_steps(args)
    check_time_step_files(args, time_steps)
    grid_desc = create_grid_description(args)
    program_data = distribute_program_data(args, grid_desc, time_steps)
    (unstructured_grid, global_ids) = create_grid(program_data)
    write_grid(unstructured_grid, global_ids, program_data)
    if is_rank_zero():
        write_pvd_file(unstructured_grid, program_data)
    barrier()
    if is_rank_zero():
        print("grid2paraview_cells finished")
def get_time_steps(args):
    error_flag = False
    time_steps_dict = None
    if is_rank_zero():
        result_file_list = get_time_steps_file_list(args)
        if result_file_list is not None:
            time_steps_dict = {}
            for f in result_file_list:
                try:
                    fh = open(f, "r")
                except IOError:
                    print("Unable to open SPARTA result file: ", f)
                    error_flag = True
                    break

                for line in fh:
                    s = clean_line(line)
                    if s.lower().replace(" ", "") == "item:timestep":
                        for line in fh:
                            time = int(line)
                            if time in time_steps_dict.keys():
                                time_steps_dict[time].append(f)
                            else:
                                time_steps_dict[time] = [f]
                            break
                    break
                fh.close()
        else:
            error_flag = True

    if error_found_on_rank_zero(error_flag):
        sys.exit(1)

    return time_steps_dict
def parse_command_line():
    args = None
    if is_rank_zero():
        parser = argparse.ArgumentParser()
        parser.add_argument("sparta_grid_description_file",
                            help="SPARTA grid description input file name")
        parser.add_argument("paraview_output_file",
                            help="ParaView output file name")
        parser.add_argument(
            '-f',
            '--float',
            action='store_true',
            help="Use float precision for flow file data (default is double)")
        parser.add_argument('-v', '--variables', nargs='+', type=str,
            help="Flow file variable names (f_1[2] f_1[3] f_1[4] etc.) to " +\
                 "create (default creates all variables found in flow files)")
        group = parser.add_mutually_exclusive_group()
        group.add_argument('-r',
                           '--result',
                           help="Optional list of SPARTA dump result files",
                           nargs='+')
        group.add_argument(
            '-rf',
            '--resultfile',
            help=
            "Optional filename containing path names of SPARTA dump result files"
        )
        args = parser.parse_args()
    return args
示例#7
0
def get_grid_file_cells(grid_file_path):
    sgf = SpartaGridFile(grid_file_path)
    sgf.set_iteration_start(get_rank())
    sgf.set_iteration_skip(get_size())
    if is_rank_zero():
        print("Reading Sparta grid file " + grid_file_path)
    cells = []
    count = 0
    for cell in sgf:
        cells.append(cell)
        count += 1
        if is_rank_zero() and count % 10000 == 0:
            print("Read " + str(count) + " cell(s) from grid file")
    if is_rank_zero():
        print("Finished grid file read")
    return cells
def exist_grid_file_files(program_data):
    files_exist = True
    if is_rank_zero():
        for rank in range(get_size()):
            files_exist = files_exist and \
                os.path.isfile(get_bucket_file_name(rank, program_data))
    return get_comm_world().bcast(files_exist, root=0)
def create_grid_description(args):
    error_flag = False
    grid_desc = None
    if is_rank_zero():
        grid_desc = {}
        gdf = open(args.sparta_grid_description_file, "r")
        read_grid_description_file(gdf, grid_desc)
        gdf.close()

        if "dimension" not in grid_desc:
            print(
                "Error: grid description file does not have a dimension statement: ",
                args.sparta_grid_description_file)
            error_flag = True

        if "create_box" not in grid_desc:
            print(
                "Error: grid description file does not have a create_box statement: ",
                args.sparta_grid_description_file)
            error_flag = True

        if "read_grid" not in grid_desc:
            print(
                "Error: grid description file does not have a read_grid statement: ",
                args.sparta_grid_description_file)
            error_flag = True

        if os.path.isfile(args.paraview_output_file + '.pvd'):
            print("ParaView output file exists: ",
                  args.paraview_output_file + '.pvd')
            error_flag = True

        try:
            sgf = SpartaGridFile(grid_desc["read_grid"])
        except:
            error_flag = True

    if error_found_on_rank_zero(error_flag):
        sys.exit(1)

    if is_rank_zero():
        print("Processing " + str(sgf.number_of_cells) +\
            " cell(s) on " + str(get_size()) + " MPI rank(s)")
        os.mkdir(args.paraview_output_file)

    return grid_desc
示例#10
0
def parse_command_line():
    if is_rank_zero():
        parser = argparse.ArgumentParser()
        parser.add_argument("sparta_grid_file_path", help="Sparta grid file path")
        args = parser.parse_args()
        return args
    else:
        return None
示例#11
0
def get_grid_file_path(args):
    path = None
    if is_rank_zero():
        path = args.sparta_grid_file_path
        path = get_comm_world().bcast(path, root = 0)
    else:
        path = get_comm_world().bcast(path, root = 0)
    return path
示例#12
0
def check_grid_file(args):
    error_flag = False
    if is_rank_zero():
        try:
            sgf = SpartaGridFile(args.sparta_grid_file_path)
        except:
            error_flag = True

    if error_found_on_rank_zero(error_flag):
        sys.exit(1)
示例#13
0
def write_grid(unstructured_grid, global_ids, program_data):
    writer = vtk.vtkXMLUnstructuredGridWriter()
    writer.SetInputData(unstructured_grid)
    time_steps = program_data["time_steps"]
    output_prefix = program_data["paraview_output_file"]

    if is_rank_zero():
        print("Writing grid over " + str(len(time_steps.keys())) +
              " time step(s)")

    if time_steps:
        for time in sorted(time_steps.keys()):
            read_time_step_data(time_steps[time][0], unstructured_grid,
                                global_ids, program_data)
            write_grid_to_file(writer, output_prefix, time)
    else:
        write_grid_to_file(writer, output_prefix, 0)

    if is_rank_zero():
        print("Finished writing grid")
 def checkResult(self, data, sorted_data, compare=None):
     barrier()
     all_data = get_comm_world().gather(data, root=0)
     all_sorted_data = get_comm_world().gather(sorted_data, root=0)
     if is_rank_zero():
         all_data = flatten_list(all_data)
         sort_list(all_data, compare)
         all_sorted_data = flatten_list(all_sorted_data)
         areEqual = all_data == all_sorted_data
     else:
         areEqual = None
     areEqual = get_comm_world().bcast(areEqual, 0)
     self.assertTrue(areEqual)
示例#15
0
def read_time_step_data(file_name, unstructured_grid, global_ids,
                        program_data):
    if is_rank_zero():
        print("Reading Sparta flow file " + file_name)

    variables = program_data["variables"]
    fh = open(file_name, "r")
    array_names = get_array_names(fh)

    if variables is None:
        variables = array_names

    id_index = array_names.index('id')

    count = 1
    for line in fh:
        s = clean_line(line)
        sl = s.split()
        if len(sl) == len(array_names):
            cell_id = int(sl[id_index])
            if cell_id not in global_ids:
                continue
            for v in variables:
                array = unstructured_grid.GetCellData().GetArray(v)
                array.SetValue(global_ids[cell_id],
                               float(sl[array_names.index(v)]))
        else:
            print("Error reading SPARTA result file: ", f)
            print("Flow data line cannot be processed:  ", line)
            return
        if is_rank_zero() and count % 100000 == 0:
            print("Read " + str(count) + " lines from flow file")
        count += 1

    fh.close()
    if is_rank_zero():
        print("Finished reading Sparta flow file " + file_name)
示例#16
0
def check_command_line(args):
    error_flag = False
    if is_rank_zero():
        if not os.path.isfile(args.sparta_grid_description_file):
            print("Unable to open SPARTA grid description file: ",
                  args.sparta_grid_description_file)
            error_flag = True

        if os.path.isdir(args.paraview_output_file):
            print("ParaView output directory exists: ",
                  args.paraview_output_file)
            error_flag = True

    if error_found_on_rank_zero(error_flag):
        sys.exit(1)
示例#17
0
def check_time_step_files(args, time_steps):
    error_flag = False
    if is_rank_zero():
        if time_steps and args.variables:
            for time in sorted(time_steps.keys()):
                fh = open(time_steps[time][0], "r")
                array_names = get_array_names(fh)
                for v in args.variables:
                    if not v in array_names:
                        print("Error: requested flow variable " +\
                            v + " not in flow file " + time_steps[time][0])
                        error_flag = True
                if 'id' not in array_names:
                    print("Error: id column not in flow file " +\
                        time_steps[time][0])
                    error_flag = True
                fh.close()
    if error_found_on_rank_zero(error_flag):
        sys.exit(1)
示例#18
0
def distribute_program_data(args, grid_desc, time_steps):
    pd = {}
    if is_rank_zero():
        pd["paraview_output_file"] = args.paraview_output_file
        pd["float"] = args.float
        pd["variables"] = args.variables
        pd["grid_desc"] = grid_desc
        pd["time_steps"] = time_steps
    else:
        pd["paraview_output_file"] = None
        pd["float"] = None
        pd["variables"] = None
        pd["grid_desc"] = None
        pd["time_steps"] = None

    pd["paraview_output_file"] = get_comm_world().bcast(
        pd["paraview_output_file"], root=0)
    pd["float"] = get_comm_world().bcast(pd["float"], root=0)
    pd["variables"] = get_comm_world().bcast(pd["variables"], root=0)
    pd["grid_desc"] = get_comm_world().bcast(pd["grid_desc"], root=0)
    pd["time_steps"] = get_comm_world().bcast(pd["time_steps"], root=0)
    return pd