Ejemplo n.º 1
0
def compute_average(source_zones,
                    variables_to_average,
                    constant_variables=None,
                    chunk_size=50):
    dataset = source_zones[0].dataset
    avg_zone = dataset.copy_zones(source_zones[0])[0]
    strand = avg_zone.strand
    avg_zone.name = "Time Average - " + str(strand)
    avg_zone.strand = 0
    avg_zone.solution_time = 0
    avg_zone.aux_data['source_strand'] = str(strand)
    avg_zone.aux_data['zone_type'] = "Average"

    for v in dataset.variables():
        # Skip over the constants
        if constant_variables and v in constant_variables:
            continue

        with tputils.ForceEditableVariable(v):
            if v in variables_to_average:
                print("Computing average for strand: {}, var: {}".format(
                    strand, v.name))
                compute_sum(v, source_zones, avg_zone, chunk_size=chunk_size)
                equation = "{%s} = {%s}/%d" % (v.name, v.name,
                                               len(source_zones))
                tp.data.operate.execute_equation(equation, zones=[avg_zone])
            else:
                # If the variable is not to be averaged, set it to zero so we don't
                # mislead the user with results that were copied from a source zone.
                # Making the variable passive would be better, but there's currently
                # no way to convert a variable to passive.
                tp.data.operate.execute_equation("{%s} = 0" % (v.name),
                                                 zones=[avg_zone])
    return avg_zone
Ejemplo n.º 2
0
def compute_phaseAverage(source_zones, zones_per_timeperiod, variables_to_average, constant_variables=None, chunk_size=50):
    # currently implemented for constant delta t solution files
    dataset = source_zones[0].dataset
    phAvg_zones = dataset.copy_zones(source_zones[slice(zones_per_timeperiod)])
    # rename zones
    for zone, i in zip(phAvg_zones, range(zones_per_timeperiod)):
        zone.name = "%s %.2f pi - %s" % ('phase', 2*i/zones_per_timeperiod, zone.name)
        zone.strand = dataset.num_zones
    # all_zNames = dataset.zone_names
    # phAvg_zNames = ["%s %.2f pi - %s" % ('phase', 2*i/zones_per_timeperiod, all_zNames[i]) for i in range(zones_per_timeperiod)]
    # dataset.zone_names[-zones_per_timeperiod:] = phAvg_zNames[:]
    for i in range(zones_per_timeperiod):
        #list zones with same phase
        phase_i_zones = source_zones[slice(i, len(source_zones), zones_per_timeperiod)]
        
        for v in dataset.variables():
            # Skip over the constants
            if constant_variables and v in constant_variables:
                continue
            
            with tputils.ForceEditableVariable(v):
                if v in variables_to_average:
                    print("Computing average for phase: {} pi, var: {}".format(i*2/zones_per_timeperiod, v.name))
                    compute_sum(v, phase_i_zones, phAvg_zones[i], chunk_size=chunk_size)
                    equation = "{%s} = {%s}/%d"%(v.name, v.name, len(phase_i_zones))
                    tp.data.operate.execute_equation(equation, zones=[phAvg_zones[i]])
                else:
                    # If the variable is not to be averaged, set it to zero so we don't
                    # mislead the user with results that were copied from a source zone.
                    # Making the variable passive would be better, but there's currently
                    # no way to convert a variable to passive.
                    tp.data.operate.execute_equation("{%s} = 0"%(v.name), zones=[phAvg_zones[i]])
    return phAvg_zones
Ejemplo n.º 3
0
def compute_minmax(variable, source_zones, dest_zone, operator='<'):
    with tputils.ForceEditableVariable(variable):
        # Initialize with values from the first source zone
        print(variable.name)
        equation = "{{{var}}} = {{{var}}}[{zone}]".format(
            var=variable.name, zone=source_zones[0].index + 1)
        tp.data.operate.execute_equation(equation, zones=[dest_zone])

        for zone in source_zones:
            equation = "{{{v}}} = IF({{{v}}} {operator} {{{v}}}[{z}], {{{v}}}, {{{v}}}[{z}])".format(
                v=variable.name, operator=operator, z=zone.index + 1)
            print(equation)
            tp.data.operate.execute_equation(equation, zones=[dest_zone])
Ejemplo n.º 4
0
def compute_sum(variable, source_zones, dest_zone, chunk_size=50):
    # Tecplot equation syntax has a length limit, so we chunk the summation to ensure
    # that we don't overflow that length limit.  In testing the limit is ~300 zones, but
    # it depends on the length of the variable name as well.
    #
    # Larger chunk_size will typically result in faster run times, but will consume
    # more RAM since more zones need to be loaded simultaneously.
    with tputils.ForceEditableVariable(variable):
        tp.data.operate.execute_equation("{%s} = 0" % (variable.name), zones=[dest_zone])
        for zones in tputils.chunks(source_zones, chunk_size):
            equation = "{%s} = {%s}" % (variable.name, variable.name)
            for z in zones:
                zone_num = z.index + 1  # Adding 1 because execute equation uses 1-based zone indices
                equation += " + {%s}[%d]" % (variable.name, zone_num)
            tp.data.operate.execute_equation(equation, zones=[dest_zone])
Ejemplo n.º 5
0
def compute_minmax(variable, source_zones, dest_zone, operator='<', chunk_size=10):
    with tputils.ForceEditableVariable(variable):
        # Initialize with values from the first source zone
        print(variable.name)
        equation = "{{{var}}} = {{{var}}}[{zone}]".format(var=variable.name, zone=source_zones[0].index+1)
        tp.data.operate.execute_equation(equation, zones=[dest_zone])

        print("Computing in chunks of ", chunk_size)
        for zones in tputils.chunks(source_zones, chunk_size):
            equation = "{{{v}}} = IF({{{v}}} {operator} {{{v}}}[{z}], {{{v}}}, ".format(v=variable.name, operator=operator, z=zones[0].index+1)
            for i,zone in enumerate(zones):
                if i+1 == len(zones):
                    equation += "{{{v}}}[{cur_index}]".format(v=variable.name, cur_index=zone.index+1)
                else:
                    cur_index = zone.index+1
                    next_index = zones[i+1].index+1
                    equation += "IF({{{v}}}[{cur_index}] {operator} {{{v}}}[{next_index}], {{{v}}}[{cur_index}], ".format(v=variable.name, operator=operator, cur_index=cur_index, next_index=next_index)
            for i in zones:
                equation += ")"
            tp.data.operate.execute_equation(equation, zones=[dest_zone])