Skip to content

Python Examples

Greg Sjaardema edited this page Dec 5, 2022 · 4 revisions

Copy input mesh to output mesh adding one or more element variables

#!/usr/bin/env python
import exodus

exofileIN = '8-block.g'
exofileOUT = 'out.exo'
 
with exodus.exodus(exofileIN,array_type='numpy') as exoIN:
    with exoIN.copy(exofileOUT) as exoOUT: # Copy mesh portion, not transient

        # Get number of element variables on the input mesh...
        evar_names = exoIN.get_element_variable_names()
        num_evars = len(evar_names)

        # Define number of output element variables. (1 more than input)
        exoOUT.set_element_variable_number(num_evars + 1)

        # Now copy the transient portion of the input mesh to the output...
        # Would be nice to have a way to do this without going down to low levels...
        exodus.EXODUS_LIB.ex_copy_transient(exoIN.fileId, exoOUT.fileId)

        # This needs to be after the `ex_copy_transient` since that call will
        # copy the IN names to the OUT names.  At this point, can rename IN names,
        # or add OUT name(s)
        exoOUT.put_element_variable_name("new_evar", num_evars + 1)

Yet another example of adding variables -- nodal in this case

import exodus

fromFileName = "orig.e"
toFileName = "orig_with_new_variables.e"

## Add new node variables
addNodeVariables = ["new_x_var", "new_y_var", "new_z_var"]

exo_copy = exodus.copyTransfer(fromFileName,toFileName,additionalNodalVariables=addNodeVariables)

## Fill in new variables
print("Exodus file has", exo_copy.num_times(), "time steps")
if exo_copy.num_times() > 0:
    TIMES = exo_copy.get_times()
    for step in range(len(TIMES)):
        print("\tstep ", step+1, ", time = ", TIMES[step])
        vals_x = exo_copy.get_node_variable_values("disp_x", step)
        vals_y = exo_copy.get_node_variable_values("disp_y", step)
        vals_z = exo_copy.get_node_variable_values("disp_z", step)
        for i in range(len(vals)):
            vals_x[i] = vals_x[i] + 10.0
            vals_y[i] = vals_x[i] + vals_y[i]
            vals_z[i] = vals_z[i] + vals_y[i]
        exo_copy.put_node_variable_values("new_x_var", step, vals_x)
        exo_copy.put_node_variable_values("new_y_var", step, vals_y)
        exo_copy.put_node_variable_values("new_z_var", step, vals_z)

exo_copy.close()

Yet another example of adding variables -- nodal in this case but using exomerge

# import the module
import exomerge

# load the results
model = exomerge.import_model('input.e')

model.calculate_node_field('disp_x_rom = 10 + disp_x * 1.111')
model.calculate_node_field('disp_y_rom = 20 + disp_x * 2.222')
model.calculate_node_field('disp_z_rom = 30 + disp_x * disp_y')
# output the new file
model.export_model('output_copy.e')

Add QA / Info records to a file (copy original to new, adding QA records)

#!/usr/bin/env python
"""
CopyTest routine for SEACAS exodus.py module
"""
import exodus

ORIG_PATH = "8-block.g"
COPY_PATH = ORIG_PATH[:-2] + '_copy.e'

with exodus.exodus(ORIG_PATH) as original:
    qa_recs = original.get_qa_records()

    with exodus.exodus(COPY_PATH, mode='w+') as exofile:
        QA = qa_recs + [("copy_script","0.1.2-3","20220801","12:34:56")]
        exofile.put_qa_records(QA)
        original.copy_file(exofile.fileId, include_transient=True)

Scale the time on a database (modified in place, no copy)

#!/usr/bin/env python
import exodus

DATABASE_PATH = "8-block.e"

with exodus.exodus(DATABASE_PATH, mode='a', array_type='ctype') as EXO:
    print("Exodus file has title:", EXO.title())
    print("Exodus file has", EXO.num_dimensions(), "dimensions")
    print("Exodus file has", EXO.num_nodes(), "nodes")
    print("Exodus file has", EXO.num_elems(), "elements")
    print("Exodus file has", EXO.num_blks(), "blocks")
    print("Exodus file has", EXO.num_node_sets(), "node sets")
    print("Exodus file has", EXO.num_side_sets(), "side sets")
    print("Exodus file has", EXO.num_times(), "time steps")

    if EXO.num_times() > 0:
        TIMES = EXO.get_times()
        scale = 2.0
        offset = 10.0

        step = 1
        for time in TIMES:
            new_time = time * scale + offset
            print ("Time = {}, New_Time = {}, Step = {}".format(time, new_time, step))
            EXO.put_time(step, new_time)
            step += 1