Skip to content
Snippets Groups Projects
Select Git revision
  • 846a2305ce23531cb6a5b93e33bef8ca9d5c4555
  • master default protected
2 results

precision_diff.py

Blame
  • precision_diff.py 3.63 KiB
    #!/usr/bin/env python
    
    # $Id$
    
    # script to find the difference between two Finesse .out files if they fall
    # outside a given precision
    
    # need the Numeric module so that can guarantee decent numerical precision
    import math
    from Numeric import *
    import sys
    import getopt  # argument parsing
    
    def get_mantissa_data(fname):
        # read the string data in
        fp = open(fname, 'r')
        lines = fp.readlines()
        fp.close()
    
        # sometimes there is only a return character on a given line, if so,
        # remove it.
        line_index = 0
        for line in lines:
            if (line == "\n"):
                lines.pop(line_index)
            line_index += 1
            
        # find out the number of rows and columns
        num_rows = len(lines)
        if (num_rows == 0):
            raise ValueError, \
                    "Zero rows returned when reading input file: %s" % fname
        
        num_cols = len(lines[0].split())
        if (num_cols == 0):
            raise ValueError, \
                    "Zero columns returned when reading input file: %s" % fname
        
        # read in the data
        data = zeros( (num_rows, num_cols), typecode=Float)
        row_index = 0
        for line in lines:
            line_array = line.split()
            for col_index in range(num_cols):
                (mantissa, exponent) = math.frexp(float(line_array[col_index]))
                data[row_index][col_index] = mantissa
            row_index += 1
    
        return (data, num_rows, num_cols)
    
    # print usage information
    def usage():
        print """Usage:
        precision_diff.py [options]
        
        Finds the difference between two Finesse .out files
    
        options:
        [-h/--help]       Print usage information and exit
        [-r/--ref-file]   Reference file to compare to
        [-c/--check-file] The file to check against the reference
        """
    # parse the input options
    try:
        opts, args = getopt.getopt(sys.argv[1:], "hr:c:", 
    	    ["help", "ref-file=", "check-file="])
    except getopt.GetoptError:
        # print help information and exit:
        usage()
        sys.exit(2)
    
    ref_fname = None
    check_fname = None
    for option, argument in opts:
        if option in ("-h", "--help"):
            usage()
            sys.exit()
        if option in ("-r", "--ref-file"):
            ref_fname = argument
        if option in ("-c", "--check-file"):
            check_fname = argument
    
    # do some checking on the arguments
    if (ref_fname is None):
        raise ValueError, "You need to enter a reference .out file name"
    
    if (check_fname is None):
        raise ValueError, "You need to enter a .out file name to check"
    
    (ref_data, ref_num_rows, ref_num_cols) = get_mantissa_data(ref_fname)
    (check_data, check_num_rows, check_num_cols) = get_mantissa_data(check_fname)
    
    # check the structure of both files
    if (ref_num_rows != check_num_rows):
        raise ValueError, \
                "Reference and check files do not have the same number of rows!"
    
    if (ref_num_cols != check_num_cols):
        raise ValueError, \
                "Reference and check files do not have the same number of columns!"
    
    zero_data = zeros( (ref_num_rows, ref_num_cols), typecode=Float)
    diff_data = abs(ref_data - check_data)
    
    if ( diff_data != zero_data ):
        print "Reference file %s and check file %s differ" % \
                (ref_fname, check_fname)
        print "Maximum absolute difference = %g" % max(max(diff_data))
        diff_fname = check_fname + ".pdiff"
        print "Writing diff file: %s" % diff_fname
        fp_diff = open(diff_fname, "w")
        (num_rows, num_cols) = shape(diff_data)
        for row_index in range(num_rows):
            for col_index in range(num_cols-1):
                fp_diff.write(str(diff_data[row_index][col_index]))
                fp_diff.write(" ")
            fp_diff.write(str(diff_data[num_rows-1][num_cols-1]))
            fp_diff.write("\n")
        fp_diff.close()
    
    # vim: expandtab shiftwidth=4: