Exemple #1
0
    def save_nodal_pce(self, pc_coeffs, meshfilename, outputfilename):
        if os.path.isfile(outputfilename): os.remove(outputfilename)
        print("Save nodal PCE %s" % outputfilename)
        times = self.get_solution_times()

        varnames = []
        for coeff_idx in range(pc_coeffs.shape[1]):
            varnames.append('PCE_%d' % coeff_idx)

        e = exodus.copy_mesh(meshfilename, outputfilename)
        e.close()
        e = exodus.exodus(outputfilename, mode='a')
        exodus.add_variables(e, nodal_vars=varnames)

        numnodes = pc_coeffs.shape[0] / self.num_timesteps

        for timestep in range(self.num_timesteps):
            for coeff_idx in range(pc_coeffs.shape[1]):
                varname = varnames[coeff_idx]
                nodal_values = e.get_node_variable_values(varname, 1)
                for nidx in range(numnodes):
                    index = timestep * numnodes + nidx
                    nodal_values[nidx] = pc_coeffs[index, coeff_idx]
                e.put_node_variable_values(varname, timestep + 1, nodal_values)
                e.put_time(timestep + 1, times[timestep])

        e.close()
Exemple #2
0
def save_nodal_fields_from_structured(meshfilename, outputfilename, fieldnames,
                                      fielddata):
    if os.path.isfile(outputfilename): os.remove(outputfilename)

    with Suppressor():
        e = exodus.exodus(meshfilename, array_type='numpy', mode='r')
        x, y, z = e.get_coords()
        e.close()

    ux = np.unique(x.round(decimals=6))
    uy = np.unique(y.round(decimals=6))

    dx = ux[1] - ux[0]
    dy = uy[1] - uy[0]

    i = np.rint((x - min(x)) / dx)
    j = np.rint((y - min(y)) / dy)

    with Suppressor():
        e = exodus.copy_mesh(meshfilename, outputfilename)
        e.close()
        e = exodus.exodus(outputfilename, mode='a', array_type="numpy")
        exodus.add_variables(e, nodal_vars=fieldnames)
        for vidx, name in enumerate(fieldnames):
            for ts in range(fielddata.shape[0]):
                e.put_node_variable_values(
                    name, ts + 1, fielddata[ts, vidx,
                                            i.astype(int),
                                            j.astype(int)])
                e.put_time(ts + 1, ts)
        e.close()
Exemple #3
0
def save_nodal_fields(meshfilename, outputfilename, fieldnames, fielddata):
    if os.path.isfile(outputfilename): os.remove(outputfilename)
    with Suppressor():
        e = exodus.copy_mesh(meshfilename, outputfilename)
        e.close()
        e = exodus.exodus(outputfilename, mode='a', array_type="numpy")
        exodus.add_variables(e, nodal_vars=fieldnames)

        for i, name in enumerate(fieldnames):
            e.put_node_variable_values(name, 1, fielddata[i])

        e.close()
Exemple #4
0
def save_nodal_fields_transient(meshfilename, outputfilename, fieldnames,
                                fielddata):
    # assert len(fieldnames) == fielddata.shape[1]
    # assert get_num_nodes(meshfilename) == fielddata.shape[2]
    if os.path.isfile(outputfilename): os.remove(outputfilename)
    with Suppressor():
        e = exodus.copy_mesh(meshfilename, outputfilename)
        e.close()
        e = exodus.exodus(outputfilename, mode='a', array_type="numpy")
        exodus.add_variables(e, nodal_vars=fieldnames)
        for i, name in enumerate(fieldnames):
            for ts in range(fielddata.shape[0]):
                e.put_node_variable_values(name, ts + 1, fielddata[ts, i, :])
                e.put_time(ts + 1, ts)
        e.close()
Exemple #5
0
def append_exodus(filenamelist,
                  outputfilename="joined-output.e",
                  skip_first=0,
                  skip_last=0):

    if os.path.isfile(outputfilename): os.remove(outputfilename)

    with Suppressor():
        # Copy Mesh
        e = exodus.copy_mesh(filenamelist[0], outputfilename)
        e.close()
        e = exodus.exodus(outputfilename, mode='a', array_type="numpy")

        # Add Variable Names
        var_names = []
        gvar_names = []
        for f in filenamelist:
            exo = exodus.exodus(f, mode='r', array_type="numpy")
            var_names.extend(exo.get_node_variable_names())
            gvar_names.extend(exo.get_global_variable_names())
            exo.close()
        var_names = list(set(var_names))
        gvar_names = list(set(gvar_names))
        exodus.add_variables(e, nodal_vars=var_names)
        e.set_global_variable_number(len(gvar_names))
        for i, gvar in enumerate(gvar_names):
            e.put_global_variable_name(gvar, i + 1)

    # Add Variable Data
    ts = 1
    for f in filenamelist:
        exo = exodus.exodus(f, mode='r', array_type="numpy")
        for step in range(skip_first, exo.num_times() - skip_last):
            for var in exo.get_node_variable_names():
                e.put_node_variable_values(
                    var, ts, exo.get_node_variable_values(var, step + 1))
            if len(gvar_names) > 0:
                gvar_vals = []
                for gvar in exo.get_global_variable_names():
                    gvar_vals.append(
                        exo.get_global_variable_values(gvar)[step])
                e.put_all_global_variable_values(ts, gvar_vals)
            e.put_time(ts, ts - 1)
            ts += 1
        exo.close()
    e.close()
Exemple #6
0
def normalize_data(filename, outputfilename="normalized-output.e"):
    if os.path.isfile(outputfilename): os.remove(outputfilename)

    # Copy Mesh
    e = exodus.copy_mesh(filename, outputfilename)
    e.close()
    exo_out = exodus.exodus(outputfilename, mode='a', array_type="numpy")
    exo_in = exodus.exodus(filename, mode='r', array_type="numpy")

    # Add Variable Names
    var_names = exo_in.get_node_variable_names()
    gvar_names = exo_in.get_global_variable_names()
    exodus.add_variables(exo_out, nodal_vars=var_names)
    exo_out.set_global_variable_number(len(gvar_names))
    for i, gvar in enumerate(gvar_names):
        exo_out.put_global_variable_name(gvar, i + 1)

    # Compute Var Min/Max
    minmax = []
    for var in var_names:
        print(var)
        vmin = float("inf")
        vmax = -vmin
        for step in range(exo_in.num_times()):
            data = exo_in.get_node_variable_values(var, step + 1)
            vmin = min(vmin, min(data))
            vmax = max(vmax, max(data))

        print((vmin, vmax))
        minmax.append((vmin, vmax))

    # Add Data
    for step in range(exo_in.num_times()):
        for i, var in enumerate(var_names):
            data = exo_in.get_node_variable_values(var, step + 1)
            vmin, vmax = minmax[i]
            exo_out.put_node_variable_values(var, step + 1,
                                             (data - vmin) / (vmax - vmin))
        exo_out.put_time(step + 1, step)

    # Add Global Data

    exo_in.close()
    exo_out.close()
Exemple #7
0
def interpolate_to_timeline(input_filename, output_filename, output_times):
    with Suppressor():
        e = exodus.copy_mesh(input_filename, output_filename)
        e.close()

        exo_in = exodus.exodus(input_filename, mode='r', array_type="numpy")
        exo_out = exodus.exodus(output_filename, mode='a', array_type="numpy")

    input_times = exo_in.get_times()
    #if list(input_times) == output_times:
    #    print("skip")
    #    return

    #print("interpolating %d input times to %d output times..." % (len(input_times), len(output_times)))

    node_varnames = exo_in.get_node_variable_names()
    global_varnames = exo_in.get_global_variable_names()

    exodus.add_variables(exo_out,
                         nodal_vars=node_varnames,
                         global_vars=global_varnames)

    for step, time in enumerate(output_times):
        idx = np.searchsorted(input_times, time)
        if idx == 0:
            left_idx = idx
        else:
            left_idx = idx - 1

        if idx == len(input_times):
            right_idx = idx - 1
        else:
            right_idx = idx

        for varname in node_varnames:
            left_data = exo_in.get_node_variable_values(varname, left_idx + 1)
            if left_idx == right_idx:
                exo_out.put_node_variable_values(varname, step + 1, left_data)
            else:
                right_data = exo_in.get_node_variable_values(
                    varname, right_idx + 1)
                alpha = (time - input_times[left_idx]) / (
                    input_times[right_idx] - input_times[left_idx])
                interp_data = left_data + alpha * (right_data - left_data)
                exo_out.put_node_variable_values(varname, step + 1,
                                                 interp_data)

        for varname in global_varnames:
            left_data = exo_in.get_global_variable_value(varname, left_idx + 1)
            if left_idx == right_idx:
                exo_out.put_global_variable_value(varname, step + 1, left_data)
            else:
                right_data = exo_in.get_global_variable_value(
                    varname, right_idx + 1)
                alpha = (time - input_times[left_idx]) / (
                    input_times[right_idx] - input_times[left_idx])
                interp_data = left_data + alpha * (right_data - left_data)
                exo_out.put_global_variable_value(varname, step + 1,
                                                  interp_data)

        exo_out.put_time(step + 1, time)

    with Suppressor():
        exo_in.close()
        exo_out.close()
                volume = elem_var_values[block_id][1][-1]
                radius = math.pow((3.0 * volume) / (4.0 * math.pi), 1.0 / 3.0)
                elem_var_values[block_id][-1].append(radius)

    new_database = old_database.copy(output_file_name)
    old_database.close()

    elem_names_and_blocks = {}
    for block_id in elem_var_names.keys():
        for name in elem_var_names[block_id]:
            if name not in elem_names_and_blocks:
                elem_names_and_blocks[name] = []
            elem_names_and_blocks[name].append(block_id)

    g_var_names = []
    n_var_names = []
    e_var_names = []
    for var_name in elem_names_and_blocks.keys():
        e_var_names.append((var_name, elem_names_and_blocks[var_name]))

    new_database.put_time(1, 0.0)
    exodus.add_variables(new_database, g_var_names, n_var_names, e_var_names)

    for block_id in block_ids:
        for i_var in range(len(elem_var_names[block_id])):
            new_database.put_element_variable_values(
                block_id, elem_var_names[block_id][i_var], 1,
                elem_var_values[block_id][i_var])

    new_database.close()
Exemple #9
0
def append_exodus_ss(filenamelist,
                     outputfilename="joined-output.e",
                     labels=None):
    if os.path.isfile(outputfilename): os.remove(outputfilename)

    with Suppressor():
        # Copy Mesh
        e = exodus.copy_mesh(filenamelist[0], outputfilename)
        e.close()
        e = exodus.exodus(outputfilename, mode='a', array_type="numpy")

        # Add Variable Names
        nvar_names = []
        gvar_names = []
        evar_names = []
        for f in filenamelist:
            exo = exodus.exodus(f, mode='r', array_type="numpy")
            nvar_names.extend(exo.get_node_variable_names())
            gvar_names.extend(exo.get_global_variable_names())
            evar_names.extend(exo.get_element_variable_names())
            exo.close()
        if labels:
            gvar_names.extend(labels.keys())
        nvar_names = list(set(nvar_names))
        gvar_names = list(set(gvar_names))
        evar_names = list(set(evar_names))

        exodus.add_variables(e, nodal_vars=nvar_names, element_vars=evar_names)
        e.set_global_variable_number(len(gvar_names))
        for i, gvar in enumerate(gvar_names):
            e.put_global_variable_name(gvar, i + 1)

        gvar_vals = {}
        for gvar in gvar_names:
            gvar_vals[gvar] = []

        ts = 1
        for f in filenamelist:
            exo = exodus.exodus(f, mode='r', array_type="numpy")
            step = exo.num_times()
            e.put_time(ts, ts)
            for var in exo.get_node_variable_names():
                e.put_node_variable_values(
                    var, ts, exo.get_node_variable_values(var, step))
            for evar in exo.get_element_variable_names():
                # TODO: only works for 1 block
                e.put_element_variable_values(
                    1, evar, ts,
                    exo.get_element_variable_values(1, evar, step))
            for gvar in exo.get_global_variable_names():
                val = exo.get_global_variable_value(gvar, step)
                gvar_vals[gvar].append(val)
            if labels:
                for key in labels:
                    val = labels[key][ts - 1]
                    gvar_vals[key].append(val)
            ts += 1
            exo.close()
        for ts in range(1, e.num_times() + 1):
            vals = []
            for gvar in e.get_global_variable_names():
                vals.append(gvar_vals[gvar][ts - 1])
            e.put_all_global_variable_values(ts, vals)
        e.close()
        sys.exit(1)

    input_file_name = sys.argv[1]
    output_file_name = sys.argv[2]

    print "\n-- imprint_dirichlet_field_on_mesh.py --\n"
    print "Genesis input file:", input_file_name
    print "Genesis output file:", output_file_name

    if os.path.exists(output_file_name):
        os.remove(output_file_name)

    old_database = exodus.exodus(input_file_name, 'r')
    x, y, z = old_database.get_coords()
    new_database = old_database.copy(output_file_name)
    old_database.close()

    g_var_names = []
    n_var_names = ["dirichlet_field"]
    e_var_names = []
    exodus.add_variables(new_database, g_var_names, n_var_names, e_var_names)

    dirichlet_field_values = []
    for i in range(len(x)):
        val = DirichletField(x[i], y[i], z[i])
        dirichlet_field_values.append(val)

    new_database.put_node_variable_values("dirichlet_field", 1, dirichlet_field_values)

    new_database.close()
Exemple #11
0
def main():
    if len(sys.argv) < 4:
        print_usage()
        sys.exit(1)

    with time_operation("Attaining lists of input files..."):
        (input_base_name, output_base_name, num_ranks) = read_io_info(sys.argv)

        print_io_info(input_base_name, output_base_name, num_ranks)

        input_files = genesis_files_from_base(input_base_name, num_ranks)
        output_files = genesis_files_from_base(output_base_name, num_ranks)

    with time_operation("Reading global node ids..."):
        global_node_ids = map(get_node_id_map_from_file, input_files)

    with time_operation("Finding the set of all global node ids..."):
        all_global_node_ids = set.union(*map(set, global_node_ids))

    with time_operation("Mapping node ids to cliques..."):
        clique_id_lookup = map_clique_ids_to_nodes(all_global_node_ids,
                                                   global_node_ids)

    with time_operation("Transfering node ids to cliques..."):
        node_clique_ids = [[
            clique_id_lookup[node_id] for node_id in rank_id_list
        ] for rank_id_list in global_node_ids]

    with time_operation("Calculating clique colors..."):
        node_clique_colors = assign_clique_colors(node_clique_ids)

    with time_operation("Cleaning existing genesis files..."):
        for file in output_files:
            if os.path.exists(file):
                os.remove(file)

    with time_operation("Writing modified genesis files..."):
        # write the modified genesis files
        for i_rank in range(len(input_files)):

            name = input_files[i_rank]
            new_name = output_files[i_rank]

            global_vars = []
            nodal_vars = ['clique', 'clique_color']
            element_vars = []

            #with run_silently():
            new_database = copy_exodus_file(name, new_name)
            print "DEBUGGING", name, i_rank, new_database.num_nodes(
            ), new_database.num_times()
            #with run_silently():
            exodus.add_variables(new_database, global_vars, nodal_vars,
                                 element_vars)
            new_database.put_node_variable_values('clique', 1,
                                                  node_clique_ids[i_rank])
            new_database.put_node_variable_values('clique_color', 1,
                                                  node_clique_colors[i_rank])
            new_database.close()

    print "Finished"
Exemple #12
0
                node_on_boundary[node_id] = 0

    node_var_name = 'clique'
    node_var_values = []
    for i_rank in range(len(global_node_ids)):
        values = []
        for node_id in global_node_ids[i_rank]:
            values.append(node_on_boundary[node_id])
        node_var_values.append(values)

    # write the modified genesis files
    for i_rank in range(len(input_file_names)):

        name = input_file_names[i_rank]
        pos = string.find(name, '.')
        new_name = output_base_name + name[pos:]

        if os.path.exists(new_name):
            os.remove(new_name)

        global_vars = []
        nodal_vars = [node_var_name]
        element_vars = []

        new_database = exodus.exodus(name, 'r').copy(new_name)
        exodus.add_variables(new_database, global_vars, nodal_vars,
                             element_vars)
        new_database.put_node_variable_values(node_var_name, 1,
                                              node_var_values[i_rank])
        new_database.close()