def read_q(grid_file, q_file, multiblock=True, blanking=False, logger=None):
    """
    Read grid and solution files.
    Returns a :class:`DomainObj` initialized from `grid_file` and `q_file`.

    grid_file: string
        Grid filename.

    q_file: string
        Q data filename.
    """
    logger = logger or NullLogger()

    domain = read_plot3d_grid(grid_file, multiblock, dim=3, blanking=blanking,
                              planes=False, binary=True, big_endian=False,
                              single_precision=False, unformatted=True,
                              logger=logger)

    with open(q_file, 'rb') as inp:
        logger.info("reading Q file '%s'", q_file)
        stream = Stream(inp, binary=True, big_endian=False,
                        single_precision=False, integer_8=False,
                        unformatted=True, recordmark_8=False)
        if multiblock:
            # Read number of zones.
            nblocks = stream.read_int(full_record=True)
        else:
            nblocks = 1
        if nblocks != len(domain.zones):
            raise RuntimeError('Q zones %d != Grid zones %d' \
                               % (nblocks, len(domain.zones)))

        # Read zone dimensions, nq, nqc.
        reclen = stream.read_recordmark()
        expected = stream.reclen_ints(3*nblocks + 2)
        if reclen != expected:
            logger.warning('unexpected dimensions recordlength'
                           ' %d vs. %d', reclen, expected)

        for zone in domain.zones:
            name = domain.zone_name(zone)
            imax, jmax, kmax = stream.read_ints(3)
            if imax < 1 or jmax < 1 or kmax < 1:
                raise ValueError("invalid dimensions: %dx%dx%d" \
                                 % (imax, jmax, kmax))
            logger.debug('    %s: %dx%dx%d', name, imax, jmax, kmax)
            zone_i, zone_j, zone_k = zone.shape
            if imax != zone_i or jmax != zone_j or kmax != zone_k:
                raise RuntimeError('%s: Q %dx%dx%d != Grid %dx%dx%d' \
                                   % (name, imax, jmax, kmax,
                                      zone_i, zone_j, zone_k))

        nq, nqc = stream.read_ints(2)
        logger.debug('    nq %d, nqc %d', nq, nqc)

        reclen2 = stream.read_recordmark()
        if reclen2 != reclen:
            logger.warning('mismatched dimensions recordlength'
                           ' %d vs. %d', reclen2, reclen)

        # Read zone scalars and variables.
        for zone in domain.zones:
            name = domain.zone_name(zone)
            logger.debug('reading data for %s', name)
            _read_scalars(zone, nqc, stream, logger)
            _read_vars(zone, nq, nqc, stream, logger)

    return domain
Beispiel #2
0
def read_q(grid_file, q_file, multiblock=True, blanking=False, logger=None):
    """
    Read grid and solution files.
    Returns a :class:`DomainObj` initialized from `grid_file` and `q_file`.

    grid_file: string
        Grid filename.

    q_file: string
        Q data filename.
    """
    logger = logger or NullLogger()

    domain = read_plot3d_grid(grid_file,
                              multiblock,
                              dim=3,
                              blanking=blanking,
                              planes=False,
                              binary=True,
                              big_endian=False,
                              single_precision=False,
                              unformatted=True,
                              logger=logger)

    with open(q_file, 'rb') as inp:
        logger.info("reading Q file '%s'", q_file)
        stream = Stream(inp,
                        binary=True,
                        big_endian=False,
                        single_precision=False,
                        integer_8=False,
                        unformatted=True,
                        recordmark_8=False)
        if multiblock:
            # Read number of zones.
            nblocks = stream.read_int(full_record=True)
        else:
            nblocks = 1
        if nblocks != len(domain.zones):
            raise RuntimeError('Q zones %d != Grid zones %d' \
                               % (nblocks, len(domain.zones)))

        # Read zone dimensions, nq, nqc.
        reclen = stream.read_recordmark()
        expected = stream.reclen_ints(3 * nblocks + 2)
        if reclen != expected:
            logger.warning('unexpected dimensions recordlength'
                           ' %d vs. %d', reclen, expected)

        for zone in domain.zones:
            name = domain.zone_name(zone)
            imax, jmax, kmax = stream.read_ints(3)
            if imax < 1 or jmax < 1 or kmax < 1:
                raise ValueError("invalid dimensions: %dx%dx%d" \
                                 % (imax, jmax, kmax))
            logger.debug('    %s: %dx%dx%d', name, imax, jmax, kmax)
            zone_i, zone_j, zone_k = zone.shape
            if imax != zone_i or jmax != zone_j or kmax != zone_k:
                raise RuntimeError('%s: Q %dx%dx%d != Grid %dx%dx%d' \
                                   % (name, imax, jmax, kmax,
                                      zone_i, zone_j, zone_k))

        nq, nqc = stream.read_ints(2)
        logger.debug('    nq %d, nqc %d', nq, nqc)

        reclen2 = stream.read_recordmark()
        if reclen2 != reclen:
            logger.warning('mismatched dimensions recordlength'
                           ' %d vs. %d', reclen2, reclen)

        # Read zone scalars and variables.
        for zone in domain.zones:
            name = domain.zone_name(zone)
            logger.debug('reading data for %s', name)
            _read_scalars(zone, nqc, stream, logger)
            _read_vars(zone, nq, nqc, stream, logger)

    return domain
Beispiel #3
0
def read_plot3d_f(grid_file, f_file, varnames=None, multiblock=True, dim=3,
                  blanking=False, planes=False, binary=True, big_endian=False,
                  single_precision=True, unformatted=True, logger=None):
    """
    Returns a :class:`DomainObj` initialized from Plot3D `grid_file` and
    `f_file`.  Variables are assigned to names of the form `f_N`.

    grid_file: string
        Grid filename.

    f_file: string
        Function data filename.
    """
    logger = logger or NullLogger()

    domain = read_plot3d_grid(grid_file, multiblock, dim, blanking, planes,
                              binary, big_endian, single_precision,
                              unformatted, logger)

    mode = 'rb' if binary else 'r'
    with open(f_file, mode) as inp:
        logger.info('reading F file %r', f_file)
        stream = Stream(inp, binary, big_endian, single_precision, False,
                        unformatted, False)
        if multiblock:
            # Read number of zones.
            nblocks = stream.read_int(full_record=True)
        else:
            nblocks = 1
        if nblocks != len(domain.zones):
            raise RuntimeError('F zones %d != Grid zones %d'
                               % (nblocks, len(domain.zones)))

        # Read zone dimensions.
        if unformatted:
            reclen = stream.read_recordmark()
            expected = stream.reclen_ints((dim+1) * nblocks)
            if reclen != expected:
                logger.warning('unexpected dimensions recordlength'
                               ' %d vs. %d', reclen, expected)
        for zone in domain.zones:
            name = domain.zone_name(zone)
            imax, jmax, kmax, nvars = _read_plot3d_dims(stream, dim, True)
            if dim > 2:
                logger.debug('    %s: %dx%dx%d %d',
                             name, imax, jmax, kmax, nvars)
                zone_i, zone_j, zone_k = zone.shape
                if imax != zone_i or jmax != zone_j or kmax != zone_k:
                    raise RuntimeError('%s: F %dx%dx%d != Grid %dx%dx%d'
                                       % (name, imax, jmax, kmax,
                                          zone_i, zone_j, zone_k))
            else:
                logger.debug('    %s: %dx%d %d', name, imax, jmax, nvars)
                zone_i, zone_j = zone.shape
                if imax != zone_i or jmax != zone_j:
                    raise RuntimeError('%s: F %dx%d != Grid %dx%d'
                                       % (name, imax, jmax, zone_i, zone_j))
        if unformatted:
            reclen2 = stream.read_recordmark()
            if reclen2 != reclen:
                logger.warning('mismatched dimensions recordlength'
                               ' %d vs. %d', reclen2, reclen)

        # Read zone variables.
        for zone in domain.zones:
            name = domain.zone_name(zone)
            logger.debug('reading data for %s', name)
            _read_plot3d_fvars(zone, stream, dim, nvars, varnames, planes,
                               logger)
    return domain
Beispiel #4
0
def read_plot3d_q(grid_file, q_file, multiblock=True, dim=3, blanking=False,
                  planes=False, binary=True, big_endian=False,
                  single_precision=True, unformatted=True, logger=None):
    """
    Returns a :class:`DomainObj` initialized from Plot3D `grid_file` and
    `q_file`.  Q variables are assigned to 'density', 'momentum', and
    'energy_stagnation_density'.  Scalars are assigned to 'mach', 'alpha',
    'reynolds', and 'time'.

    grid_file: string
        Grid filename.

    q_file: string
        Q data filename.
    """
    logger = logger or NullLogger()

    domain = read_plot3d_grid(grid_file, multiblock, dim, blanking, planes,
                              binary, big_endian, single_precision,
                              unformatted, logger)

    mode = 'rb' if binary else 'r'
    with open(q_file, mode) as inp:
        logger.info('reading Q file %r', q_file)
        stream = Stream(inp, binary, big_endian, single_precision, False,
                        unformatted, False)
        if multiblock:
            # Read number of zones.
            nblocks = stream.read_int(full_record=True)
        else:
            nblocks = 1
        if nblocks != len(domain.zones):
            raise RuntimeError('Q zones %d != Grid zones %d'
                               % (nblocks, len(domain.zones)))

        # Read zone dimensions.
        if unformatted:
            reclen = stream.read_recordmark()
            expected = stream.reclen_ints(dim * nblocks)
            if reclen != expected:
                logger.warning('unexpected dimensions recordlength'
                               ' %d vs. %d', reclen, expected)
        for zone in domain.zones:
            name = domain.zone_name(zone)
            imax, jmax, kmax = _read_plot3d_dims(stream, dim)
            if dim > 2:
                logger.debug('    %s: %dx%dx%d', name, imax, jmax, kmax)
                zone_i, zone_j, zone_k = zone.shape
                if imax != zone_i or jmax != zone_j or kmax != zone_k:
                    raise RuntimeError('%s: Q %dx%dx%d != Grid %dx%dx%d'
                                       % (name, imax, jmax, kmax,
                                          zone_i, zone_j, zone_k))
            else:
                logger.debug('    %s: %dx%d', name, imax, jmax)
                zone_i, zone_j = zone.shape
                if imax != zone_i or jmax != zone_j:
                    raise RuntimeError('%s: Q %dx%d != Grid %dx%d'
                                       % (name, imax, jmax, zone_i, zone_j))
        if unformatted:
            reclen2 = stream.read_recordmark()
            if reclen2 != reclen:
                logger.warning('mismatched dimensions recordlength'
                               ' %d vs. %d', reclen2, reclen)

        # Read zone scalars and variables.
        for zone in domain.zones:
            name = domain.zone_name(zone)
            logger.debug('reading data for %s', name)
            _read_plot3d_qscalars(zone, stream, logger)
            _read_plot3d_qvars(zone, stream, planes, logger)

    return domain
    def test_int32(self):
        logging.debug('')
        logging.debug('test_int32')

        # 'Normal' integers.
        data = numpy.arange(0, 10, dtype=numpy.int32)
        with open(self.filename, 'wb') as out:
            stream = Stream(out, binary=True)
            stream.write_ints(data)
        self.assertEqual(os.path.getsize(self.filename), 40)
        with open(self.filename, 'rb') as inp:
            stream = Stream(inp, binary=True)
            new_data = stream.read_ints(data.size)
        numpy.testing.assert_array_equal(new_data, data)

        # Text scalar.
        with open(self.filename, 'w') as out:
            stream = Stream(out)
            stream.write_int(4, sep=' ')
            stream.write_int(2, full_record=True)
        size = 5 if sys.platform == 'win32' else 4  # CR LF
        self.assertEqual(os.path.getsize(self.filename), size)
        with open(self.filename, 'r') as inp:
            new_data = inp.read()
            self.assertEqual(new_data, '4 2\n')

        # Unformatted scalar.
        with open(self.filename, 'wb') as out:
            stream = Stream(out, binary=True, unformatted=True)
            stream.write_int(1, full_record=True)
        self.assertEqual(os.path.getsize(self.filename), 12)
        with open(self.filename, 'rb') as inp:
            new_data = inp.read()
            self.assertEqual(new_data, UNF_I4)
        with open(self.filename, 'rb') as inp:
            stream = Stream(inp, binary=True)
            new_data = stream.read_int()
        try:
            self.assertEqual(new_data, 1)
        except AssertionError:
            pass
        else:
            self.fail('Expected AssertionError')
        with open(self.filename, 'rb') as inp:
            stream = Stream(inp, binary=True, unformatted=True)
            new_data = stream.read_int(full_record=True)
        self.assertEqual(new_data, 1)

        # Unformatted array.
        data = numpy.arange(1, 9, dtype=numpy.int32)
        with open(self.filename, 'wb') as out:
            stream = Stream(out, binary=True, unformatted=True)
            stream.write_ints(data, full_record=True)
        self.assertEqual(os.path.getsize(self.filename), 40)
        with open(self.filename, 'rb') as inp:
            new_data = inp.read()
            self.assertEqual(new_data, UNF_I4A)
        with open(self.filename, 'rb') as inp:
            stream = Stream(inp, binary=True)
            new_data = stream.read_ints(data.size)
        try:
            numpy.testing.assert_array_equal(new_data, data)
        except AssertionError:
            pass
        else:
            self.fail('Expected AssertionError')
        with open(self.filename, 'rb') as inp:
            stream = Stream(inp, binary=True, unformatted=True)
            new_data = stream.read_ints(data.size, full_record=True)
        numpy.testing.assert_array_equal(new_data, data)

        # Byteswapped.
        swap_endian = sys.byteorder == 'little'
        wrong_endian = not swap_endian
        with open(self.filename, 'wb') as out:
            stream = Stream(out, binary=True, big_endian=swap_endian)
            stream.write_ints(data)
        self.assertEqual(os.path.getsize(self.filename), 32)
        with open(self.filename, 'rb') as inp:
            stream = Stream(inp, binary=True, big_endian=wrong_endian)
            new_data = stream.read_ints(data.size)
        try:
            numpy.testing.assert_array_equal(new_data, data)
        except AssertionError:
            pass
        else:
            self.fail('Expected AssertionError')
        with open(self.filename, 'rb') as inp:
            stream = Stream(inp, binary=True, big_endian=swap_endian)
            new_data = stream.read_ints(data.size)
        numpy.testing.assert_array_equal(new_data, data)

        # Write as 8-byte integers.
        with open(self.filename, 'wb') as out:
            stream = Stream(out, binary=True, integer_8=True)
            stream.write_ints(data)
        self.assertEqual(os.path.getsize(self.filename), 64)
        with open(self.filename, 'rb') as inp:
            stream = Stream(inp, binary=True, integer_8=True)
            new_data = stream.read_ints(data.size)
        numpy.testing.assert_array_equal(new_data, data)

        # Write from list.
        data = list(data)
        with open(self.filename, 'wb') as out:
            stream = Stream(out, binary=True)
            stream.write_ints(data)
        self.assertEqual(os.path.getsize(self.filename), 32)
        with open(self.filename, 'rb') as inp:
            stream = Stream(inp, binary=True)
            new_data = stream.read_ints(len(data))
        numpy.testing.assert_array_equal(new_data, data)
def read(casename, logger, suffix='.restart.new'):
    """ Return domain read from ADPAC .input, .mesh, and .restart files. """
    # Read input.
    input = Input()
    input.read(casename)

    # Read mesh.
    domain = read_plot3d_grid(casename+'.mesh', big_endian=True,
                              unformatted=False, logger=logger)

    # Set global reference state.
    domain.reference_state = {
        'ideal_gas_constant': PhysicalQuantity(input.rgas, 'ft*lbf/(slug*degR)'),
        'length_reference': PhysicalQuantity(input.diam, 'ft'),
        'pressure_reference': PhysicalQuantity(input.pref, 'lbf/ft**2'),
        'specific_heat_ratio': PhysicalQuantity(input.gamma, 'unitless'),
        'temperature_reference': PhysicalQuantity(input.tref, 'degR'),
    }

    # Set zone handedness and symmetry.  Also make cylindrical if necessary.
    for i, zone in enumerate(domain.zones):
        zone.right_handed = False
        try:
            nbld = input.nbld[i]
        except IndexError:
            nbld = 1  # Default.
        if nbld > 1:
            zone.symmetry = 'rotational'
            zone.symmetry_axis = 'x'
            zone.symmetry_instances = input.nbld[i]
        try:
            fcarb = input.fcarb[i]
        except IndexError:
            fcarb = input.fcart  # Default
        else:
            if fcarb == -1:
                fcarb = input.fcart
        if not fcarb:
            zone.make_cylindrical(axis='x')

    # Read restart.
    restart = casename+suffix
    with open(restart, 'rb') as inp:
        logger.info('reading restart file %r', restart)
        stream = Stream(inp, binary=True, big_endian=True,
                        single_precision=True, integer_8=False,
                        unformatted=False, recordmark_8=False)

        # Read number of zones.
        nblocks = stream.read_int()
        if nblocks != len(domain.zones):
            raise RuntimeError('nblocks (%d) in %r != #Mesh zones (%d)'
                               % (nblocks, restart, len(domain.zones)))

        # Read zone dimensions.
        for zone in domain.zones:
            name = domain.zone_name(zone)
            imax, jmax, kmax = stream.read_ints(3)
            logger.debug('    %s: %dx%dx%d', name, imax, jmax, kmax)
            zone_i, zone_j, zone_k = zone.shape
            if imax != zone_i+1 or jmax != zone_j+1 or kmax != zone_k+1:
                raise RuntimeError('%s: Restart %dx%dx%d != Mesh %dx%dx%d' \
                                   % (name, imax, jmax, kmax,
                                      zone_i, zone_j, zone_k))
        # Read zone variables.
        for i, zone in enumerate(domain.zones):
            name = domain.zone_name(zone)
            zone_i, zone_j, zone_k = zone.shape
            shape = (zone_i+1, zone_j+1, zone_k+1)
            logger.debug('reading data for %s', name)

            zone.flow_solution.grid_location = 'CellCenter'
            zone.flow_solution.ghosts = [1, 1, 1, 1, 1, 1]

            name = 'density'
            arr = stream.read_floats(shape, order='Fortran')
            logger.debug('    %s min %g, max %g', name, arr.min(), arr.max())
            zone.flow_solution.add_array(name, arr)

            vec = Vector()
            if zone.coordinate_system == 'Cartesian':
                vec.x = stream.read_floats(shape, order='Fortran')
                logger.debug('    momentum.x min %g, max %g',
                             vec.x.min(), vec.x.max())
                vec.y = stream.read_floats(shape, order='Fortran')
                logger.debug('    momentum.y min %g, max %g',
                             vec.y.min(), vec.y.max())
                vec.z = stream.read_floats(shape, order='Fortran')
                logger.debug('    momentum.z min %g, max %g',
                             vec.z.min(), vec.z.max())
            else:
                vec.z = stream.read_floats(shape, order='Fortran')
                logger.debug('    momentum.z min %g, max %g',
                             vec.z.min(), vec.z.max())
                vec.r = stream.read_floats(shape, order='Fortran')
                logger.debug('    momentum.r min %g, max %g',
                             vec.r.min(), vec.r.max())
                vec.t = stream.read_floats(shape, order='Fortran')
                logger.debug('    momentum.t min %g, max %g',
                             vec.t.min(), vec.t.max())
            zone.flow_solution.add_vector('momentum', vec)

            name = 'energy_stagnation_density'
            arr = stream.read_floats(shape, order='Fortran')
            logger.debug('    %s min %g, max %g', name, arr.min(), arr.max())
            zone.flow_solution.add_array(name, arr)

            name = 'pressure'
            arr = stream.read_floats(shape, order='Fortran')
            logger.debug('    %s min %g, max %g', name, arr.min(), arr.max())
            zone.flow_solution.add_array(name, arr)

        # Read zone scalars.
        ncyc = stream.read_ints(len(domain.zones))
        dtheta = stream.read_floats(len(domain.zones))
        omegal = stream.read_floats(len(domain.zones))
        logger.debug('    ncyc %s', str(ncyc))
        logger.debug('    dtheta %s', str(dtheta))
        logger.debug('    omegal %s', str(omegal))
        for i, zone in enumerate(domain.zones):
            zone.flow_solution.ncyc = ncyc[i]
            zone.flow_solution.dtheta = dtheta[i]
            zone.flow_solution.omegal = omegal[i]

        # Implicit calculation data not supported.

    return domain
    def test_int64(self):
        logging.debug('')
        logging.debug('test_int64')

        # Big integers, which are default on some machines.
        data = numpy.arange(1, 9, dtype=numpy.int64)
        with open(self.filename, 'wb') as out:
            stream = Stream(out, binary=True, integer_8=True)
            stream.write_ints(data)
        self.assertEqual(os.path.getsize(self.filename), 64)
        with open(self.filename, 'rb') as inp:
            stream = Stream(inp, binary=True, integer_8=True)
            new_data = stream.read_ints(data.size)
        numpy.testing.assert_array_equal(new_data, data)

        # Unformatted scalar.
        with open(self.filename, 'wb') as out:
            stream = Stream(out, binary=True, integer_8=True, unformatted=True)
            stream.write_int(1, full_record=True)
        self.assertEqual(os.path.getsize(self.filename), 16)
        with open(self.filename, 'rb') as inp:
            new_data = inp.read()
            self.assertEqual(new_data, UNF_I8)
        with open(self.filename, 'rb') as inp:
            stream = Stream(inp, binary=True, integer_8=True)
            new_data = stream.read_int()
        try:
            self.assertEqual(new_data, 1)
        except AssertionError:
            pass
        else:
            self.fail('Expected AssertionError')
        with open(self.filename, 'rb') as inp:
            stream = Stream(inp, binary=True, integer_8=True, unformatted=True)
            new_data = stream.read_int(full_record=True)
        self.assertEqual(new_data, 1)

        # Unformatted array.
        with open(self.filename, 'wb') as out:
            stream = Stream(out, binary=True, integer_8=True, unformatted=True)
            stream.write_ints(data, full_record=True)
        self.assertEqual(os.path.getsize(self.filename), 72)
        with open(self.filename, 'rb') as inp:
            new_data = inp.read()
            self.assertEqual(new_data, UNF_I8A)
        with open(self.filename, 'rb') as inp:
            stream = Stream(inp, binary=True)
            new_data = stream.read_ints(data.size)
        try:
            numpy.testing.assert_array_equal(new_data, data)
        except AssertionError:
            pass
        else:
            self.fail('Expected AssertionError')
        with open(self.filename, 'rb') as inp:
            stream = Stream(inp, binary=True, integer_8=True, unformatted=True)
            new_data = stream.read_ints(data.size, full_record=True)
        numpy.testing.assert_array_equal(new_data, data)

        # Write as 4-byte integers.
        with open(self.filename, 'wb') as out:
            stream = Stream(out, binary=True)
            stream.write_ints(data)
        self.assertEqual(os.path.getsize(self.filename), 32)
        with open(self.filename, 'rb') as inp:
            stream = Stream(inp, binary=True)
            new_data = stream.read_ints(data.size)
        numpy.testing.assert_array_equal(new_data, data)

        # Row-major.
        data = numpy.arange(0, 10, dtype=numpy.int64)
        arr2d = data.reshape((5, 2))
        with open(self.filename, 'wb') as out:
            stream = Stream(out, binary=True, integer_8=True)
            stream.write_ints(arr2d)
        self.assertEqual(os.path.getsize(self.filename), 80)
        with open(self.filename, 'rb') as inp:
            stream = Stream(inp, binary=True, integer_8=True)
            new_data = stream.read_ints((5, 2))
        numpy.testing.assert_array_equal(new_data, arr2d)

        # Row-major text.
        with open(self.filename, 'w') as out:
            stream = Stream(out)
            stream.write_ints(arr2d, linecount=4)
        with open(self.filename, 'r') as inp:
            stream = Stream(inp)
            new_data = stream.read_ints((5, 2), order='Fortran')
        try:
            numpy.testing.assert_array_equal(new_data, arr2d)
        except AssertionError:
            pass
        else:
            self.fail('Expected AssertionError')
        with open(self.filename, 'r') as inp:
            stream = Stream(inp)
            new_data = stream.read_ints((5, 2), order='C')
        numpy.testing.assert_array_equal(new_data, arr2d)

        # Column-major.
        with open(self.filename, 'wb') as out:
            stream = Stream(out, binary=True, integer_8=True)
            stream.write_ints(arr2d, order='Fortran')
        self.assertEqual(os.path.getsize(self.filename), 80)
        with open(self.filename, 'rb') as inp:
            stream = Stream(inp, binary=True, integer_8=True)
            new_data = stream.read_ints((5, 2))
        try:
            numpy.testing.assert_array_equal(new_data, arr2d)
        except AssertionError:
            pass
        else:
            self.fail('Expected AssertionError')
        with open(self.filename, 'rb') as inp:
            stream = Stream(inp, binary=True, integer_8=True)
            new_data = stream.read_ints((5, 2), order='Fortran')
        numpy.testing.assert_array_equal(new_data, arr2d)

        # Column-major text.
        with open(self.filename, 'w') as out:
            stream = Stream(out)
            stream.write_ints(arr2d, order='Fortran', linecount=4)
        with open(self.filename, 'r') as inp:
            stream = Stream(inp)
            new_data = stream.read_ints((5, 2))
        try:
            numpy.testing.assert_array_equal(new_data, arr2d)
        except AssertionError:
            pass
        else:
            self.fail('Expected AssertionError')
        with open(self.filename, 'r') as inp:
            stream = Stream(inp)
            new_data = stream.read_ints((5, 2), order='Fortran')
        numpy.testing.assert_array_equal(new_data, arr2d)

        # Illegal-order text.
        with open(self.filename, 'w') as out:
            stream = Stream(out)
            assert_raises(self, "stream.write_ints(arr2d, order='Unknown')",
                          globals(), locals(), ValueError,
                          "order must be 'C' or 'Fortran'")
Beispiel #8
0
def read(casename, logger):
    """ Return domain read from ADPAC .input, .mesh, and .restart files. """
    rgas  = 1716.3507
    diam  = 0.083333
    pref  = 759.0528
    gamma = 1.4
    tref  = 444.3192
    nbld  = 64

    try:
        PhysicalQuantity(0., 'slug')
    except ValueError:
        add_unit('slug', '14.5939*kg', 'Slug')

    # Read mesh.
    domain = read_plot3d_grid(casename+'.mesh', big_endian=True,
                              unformatted=False, logger=logger)

    # Set global reference state.
    domain.reference_state = {
        'ideal_gas_constant': PhysicalQuantity(rgas, 'ft*lbf/(slug*degR)'),
        'length_reference': PhysicalQuantity(diam, 'ft'),
        'pressure_reference': PhysicalQuantity(pref, 'lbf/ft**2'),
        'specific_heat_ratio': PhysicalQuantity(gamma, 'unitless'),
        'temperature_reference': PhysicalQuantity(tref, 'degR'),
    }

    # Set zone handedness and symmetry.  Also make cylindrical if necessary.
    for i, zone in enumerate(domain.zones):
        zone.right_handed = False
        zone.symmetry = 'rotational'
        zone.symmetry_axis = 'x'
        zone.symmetry_instances = nbld
        zone.make_cylindrical(axis='x')

    # Read restart.
    restart = casename+'.restart.new'
    with open(restart, 'rb') as inp:
        logger.info("reading restart file '%s'", restart)
        stream = Stream(inp, binary=True, big_endian=True,
                        single_precision=True, integer_8=False,
                        unformatted=False, recordmark_8=False)

        # Read number of zones.
        nblocks = stream.read_int()
        if nblocks != len(domain.zones):
            raise RuntimeError("nblocks (%d) in '%s' != #Mesh zones (%d)"
                               % (nblocks, restart, len(domain.zones)))

        # Read zone dimensions.
        for zone in domain.zones:
            name = domain.zone_name(zone)
            imax, jmax, kmax = stream.read_ints(3)
            logger.debug('    %s: %dx%dx%d', name, imax, jmax, kmax)
            zone_i, zone_j, zone_k = zone.shape
            if imax != zone_i+1 or jmax != zone_j+1 or kmax != zone_k+1:
                raise RuntimeError('%s: Restart %dx%dx%d != Mesh %dx%dx%d' \
                                   % (name, imax, jmax, kmax,
                                      zone_i, zone_j, zone_k))
        # Read zone variables.
        for zone in domain.zones:
            name = domain.zone_name(zone)
            zone_i, zone_j, zone_k = zone.shape
            shape = (zone_i+1, zone_j+1, zone_k+1)
            logger.debug('reading data for %s', name)

            zone.flow_solution.grid_location = 'CellCenter'
            zone.flow_solution.ghosts = [1, 1, 1, 1, 1, 1]

            name = 'density'
            arr = stream.read_floats(shape, order='Fortran')
            logger.debug('    %s min %g, max %g', name, arr.min(), arr.max())
            zone.flow_solution.add_array(name, arr)

            vec = Vector()
            if zone.coordinate_system == 'Cartesian':
                vec.x = stream.read_floats(shape, order='Fortran')
                logger.debug('    momentum.x min %g, max %g',
                             vec.x.min(), vec.x.max())
                vec.y = stream.read_floats(shape, order='Fortran')
                logger.debug('    momentum.y min %g, max %g',
                             vec.y.min(), vec.y.max())
                vec.z = stream.read_floats(shape, order='Fortran')
                logger.debug('    momentum.z min %g, max %g',
                             vec.z.min(), vec.z.max())
            else:
                vec.z = stream.read_floats(shape, order='Fortran')
                logger.debug('    momentum.z min %g, max %g',
                             vec.z.min(), vec.z.max())
                vec.r = stream.read_floats(shape, order='Fortran')
                logger.debug('    momentum.r min %g, max %g',
                             vec.r.min(), vec.r.max())
                vec.t = stream.read_floats(shape, order='Fortran')
                logger.debug('    momentum.t min %g, max %g',
                             vec.t.min(), vec.t.max())
            zone.flow_solution.add_vector('momentum', vec)

            name = 'energy_stagnation_density'
            arr = stream.read_floats(shape, order='Fortran')
            logger.debug('    %s min %g, max %g', name, arr.min(), arr.max())
            zone.flow_solution.add_array(name, arr)

            name = 'pressure'
            arr = stream.read_floats(shape, order='Fortran')
            logger.debug('    %s min %g, max %g', name, arr.min(), arr.max())
            zone.flow_solution.add_array(name, arr)

    return domain
    def test_int32(self):
        logging.debug('')
        logging.debug('test_int32')

        # 'Normal' integers.
        data = numpy.arange(0, 10, dtype=numpy.int32)
        with open(self.filename, 'wb') as out:
            stream = Stream(out, binary=True)
            stream.write_ints(data)
        self.assertEqual(os.path.getsize(self.filename), 40)
        with open(self.filename, 'rb') as inp:
            stream = Stream(inp, binary=True)
            new_data = stream.read_ints(data.size)
        numpy.testing.assert_array_equal(new_data, data)

        # Text scalar.
        with open(self.filename, 'w') as out:
            stream = Stream(out)
            stream.write_int(4, sep=' ')
            stream.write_int(2, full_record=True)
        size = 5 if sys.platform == 'win32' else 4  # CR LF
        self.assertEqual(os.path.getsize(self.filename), size)
        with open(self.filename, 'r') as inp:
            new_data = inp.read()
            self.assertEqual(new_data, '4 2\n')

        # Unformatted scalar.
        with open(self.filename, 'wb') as out:
            stream = Stream(out, binary=True, unformatted=True)
            stream.write_int(1, full_record=True)
        self.assertEqual(os.path.getsize(self.filename), 12)
        with open(self.filename, 'rb') as inp:
            new_data = inp.read()
            self.assertEqual(new_data, UNF_I4)
        with open(self.filename, 'rb') as inp:
            stream = Stream(inp, binary=True)
            new_data = stream.read_int()
        try:
            self.assertEqual(new_data, 1)
        except AssertionError:
            pass
        else:
            self.fail('Expected AssertionError')
        with open(self.filename, 'rb') as inp:
            stream = Stream(inp, binary=True, unformatted=True)
            new_data = stream.read_int(full_record=True)
        self.assertEqual(new_data, 1)

        # Unformatted array.
        data = numpy.arange(1, 9, dtype=numpy.int32)
        with open(self.filename, 'wb') as out:
            stream = Stream(out, binary=True, unformatted=True)
            stream.write_ints(data, full_record=True)
        self.assertEqual(os.path.getsize(self.filename), 40)
        with open(self.filename, 'rb') as inp:
            new_data = inp.read()
            self.assertEqual(new_data, UNF_I4A)
        with open(self.filename, 'rb') as inp:
            stream = Stream(inp, binary=True)
            new_data = stream.read_ints(data.size)
        try:
            numpy.testing.assert_array_equal(new_data, data)
        except AssertionError:
            pass
        else:
            self.fail('Expected AssertionError')
        with open(self.filename, 'rb') as inp:
            stream = Stream(inp, binary=True, unformatted=True)
            new_data = stream.read_ints(data.size, full_record=True)
        numpy.testing.assert_array_equal(new_data, data)

        # Byteswapped.
        swap_endian = sys.byteorder == 'little'
        wrong_endian = not swap_endian
        with open(self.filename, 'wb') as out:
            stream = Stream(out, binary=True, big_endian=swap_endian)
            stream.write_ints(data)
        self.assertEqual(os.path.getsize(self.filename), 32)
        with open(self.filename, 'rb') as inp:
            stream = Stream(inp, binary=True, big_endian=wrong_endian)
            new_data = stream.read_ints(data.size)
        try:
            numpy.testing.assert_array_equal(new_data, data)
        except AssertionError:
            pass
        else:
            self.fail('Expected AssertionError')
        with open(self.filename, 'rb') as inp:
            stream = Stream(inp, binary=True, big_endian=swap_endian)
            new_data = stream.read_ints(data.size)
        numpy.testing.assert_array_equal(new_data, data)

        # Write as 8-byte integers.
        with open(self.filename, 'wb') as out:
            stream = Stream(out, binary=True, integer_8=True)
            stream.write_ints(data)
        self.assertEqual(os.path.getsize(self.filename), 64)
        with open(self.filename, 'rb') as inp:
            stream = Stream(inp, binary=True, integer_8=True)
            new_data = stream.read_ints(data.size)
        numpy.testing.assert_array_equal(new_data, data)

        # Write from list.
        data = list(data)
        with open(self.filename, 'wb') as out:
            stream = Stream(out, binary=True)
            stream.write_ints(data)
        self.assertEqual(os.path.getsize(self.filename), 32)
        with open(self.filename, 'rb') as inp:
            stream = Stream(inp, binary=True)
            new_data = stream.read_ints(len(data))
        numpy.testing.assert_array_equal(new_data, data)
    def test_int64(self):
        logging.debug('')
        logging.debug('test_int64')

        # Big integers, which are default on some machines.
        data = numpy.arange(1, 9, dtype=numpy.int64)
        with open(self.filename, 'wb') as out:
            stream = Stream(out, binary=True, integer_8=True)
            stream.write_ints(data)
        self.assertEqual(os.path.getsize(self.filename), 64)
        with open(self.filename, 'rb') as inp:
            stream = Stream(inp, binary=True, integer_8=True)
            new_data = stream.read_ints(data.size)
        numpy.testing.assert_array_equal(new_data, data)

        # Unformatted scalar.
        with open(self.filename, 'wb') as out:
            stream = Stream(out, binary=True, integer_8=True, unformatted=True)
            stream.write_int(1, full_record=True)
        self.assertEqual(os.path.getsize(self.filename), 16)
        with open(self.filename, 'rb') as inp:
            new_data = inp.read()
            self.assertEqual(new_data, UNF_I8)
        with open(self.filename, 'rb') as inp:
            stream = Stream(inp, binary=True, integer_8=True)
            new_data = stream.read_int()
        try:
            self.assertEqual(new_data, 1)
        except AssertionError:
            pass
        else:
            self.fail('Expected AssertionError')
        with open(self.filename, 'rb') as inp:
            stream = Stream(inp, binary=True, integer_8=True, unformatted=True)
            new_data = stream.read_int(full_record=True)
        self.assertEqual(new_data, 1)

        # Unformatted array.
        with open(self.filename, 'wb') as out:
            stream = Stream(out, binary=True, integer_8=True, unformatted=True)
            stream.write_ints(data, full_record=True)
        self.assertEqual(os.path.getsize(self.filename), 72)
        with open(self.filename, 'rb') as inp:
            new_data = inp.read()
            self.assertEqual(new_data, UNF_I8A)
        with open(self.filename, 'rb') as inp:
            stream = Stream(inp, binary=True)
            new_data = stream.read_ints(data.size)
        try:
            numpy.testing.assert_array_equal(new_data, data)
        except AssertionError:
            pass
        else:
            self.fail('Expected AssertionError')
        with open(self.filename, 'rb') as inp:
            stream = Stream(inp, binary=True, integer_8=True, unformatted=True)
            new_data = stream.read_ints(data.size, full_record=True)
        numpy.testing.assert_array_equal(new_data, data)

        # Write as 4-byte integers.
        with open(self.filename, 'wb') as out:
            stream = Stream(out, binary=True)
            stream.write_ints(data)
        self.assertEqual(os.path.getsize(self.filename), 32)
        with open(self.filename, 'rb') as inp:
            stream = Stream(inp, binary=True)
            new_data = stream.read_ints(data.size)
        numpy.testing.assert_array_equal(new_data, data)

        # Row-major.
        data = numpy.arange(0, 10, dtype=numpy.int64)
        arr2d = data.reshape((5, 2))
        with open(self.filename, 'wb') as out:
            stream = Stream(out, binary=True, integer_8=True)
            stream.write_ints(arr2d)
        self.assertEqual(os.path.getsize(self.filename), 80)
        with open(self.filename, 'rb') as inp:
            stream = Stream(inp, binary=True, integer_8=True)
            new_data = stream.read_ints((5, 2))
        numpy.testing.assert_array_equal(new_data, arr2d)

        # Row-major text.
        with open(self.filename, 'w') as out:
            stream = Stream(out)
            stream.write_ints(arr2d, linecount=4)
        with open(self.filename, 'r') as inp:
            stream = Stream(inp)
            new_data = stream.read_ints((5, 2), order='Fortran')
        try:
            numpy.testing.assert_array_equal(new_data, arr2d)
        except AssertionError:
            pass
        else:
            self.fail('Expected AssertionError')
        with open(self.filename, 'r') as inp:
            stream = Stream(inp)
            new_data = stream.read_ints((5, 2), order='C')
        numpy.testing.assert_array_equal(new_data, arr2d)

        # Column-major.
        with open(self.filename, 'wb') as out:
            stream = Stream(out, binary=True, integer_8=True)
            stream.write_ints(arr2d, order='Fortran')
        self.assertEqual(os.path.getsize(self.filename), 80)
        with open(self.filename, 'rb') as inp:
            stream = Stream(inp, binary=True, integer_8=True)
            new_data = stream.read_ints((5, 2))
        try:
            numpy.testing.assert_array_equal(new_data, arr2d)
        except AssertionError:
            pass
        else:
            self.fail('Expected AssertionError')
        with open(self.filename, 'rb') as inp:
            stream = Stream(inp, binary=True, integer_8=True)
            new_data = stream.read_ints((5, 2), order='Fortran')
        numpy.testing.assert_array_equal(new_data, arr2d)

        # Column-major text.
        with open(self.filename, 'w') as out:
            stream = Stream(out)
            stream.write_ints(arr2d, order='Fortran', linecount=4)
        with open(self.filename, 'r') as inp:
            stream = Stream(inp)
            new_data = stream.read_ints((5, 2))
        try:
            numpy.testing.assert_array_equal(new_data, arr2d)
        except AssertionError:
            pass
        else:
            self.fail('Expected AssertionError')
        with open(self.filename, 'r') as inp:
            stream = Stream(inp)
            new_data = stream.read_ints((5, 2), order='Fortran')
        numpy.testing.assert_array_equal(new_data, arr2d)

        # Illegal-order text.
        with open(self.filename, 'w') as out:
            stream = Stream(out)
            assert_raises(self, "stream.write_ints(arr2d, order='Unknown')",
                          globals(), locals(), ValueError,
                          "order must be 'C' or 'Fortran'")
Beispiel #11
0
def read(casename, logger, suffix='.restart.new'):
    """ Return domain read from ADPAC .input, .mesh, and .restart files. """
    # Read input.
    input = Input()
    input.read(casename)

    # Read mesh.
    domain = read_plot3d_grid(casename + '.mesh',
                              big_endian=True,
                              unformatted=False,
                              logger=logger)

    # Set global reference state.
    domain.reference_state = {
        'ideal_gas_constant': PhysicalQuantity(input.rgas,
                                               'ft*lbf/(slug*degR)'),
        'length_reference': PhysicalQuantity(input.diam, 'ft'),
        'pressure_reference': PhysicalQuantity(input.pref, 'lbf/ft**2'),
        'specific_heat_ratio': PhysicalQuantity(input.gamma, 'unitless'),
        'temperature_reference': PhysicalQuantity(input.tref, 'degR'),
    }

    # Set zone handedness and symmetry.  Also make cylindrical if necessary.
    for i, zone in enumerate(domain.zones):
        zone.right_handed = False
        try:
            nbld = input.nbld[i]
        except IndexError:
            nbld = 1  # Default.
        if nbld > 1:
            zone.symmetry = 'rotational'
            zone.symmetry_axis = 'x'
            zone.symmetry_instances = input.nbld[i]
        try:
            fcarb = input.fcarb[i]
        except IndexError:
            fcarb = input.fcart  # Default
        else:
            if fcarb == -1:
                fcarb = input.fcart
        if not fcarb:
            zone.make_cylindrical(axis='x')

    # Read restart.
    restart = casename + suffix
    with open(restart, 'rb') as inp:
        logger.info('reading restart file %r', restart)
        stream = Stream(inp,
                        binary=True,
                        big_endian=True,
                        single_precision=True,
                        integer_8=False,
                        unformatted=False,
                        recordmark_8=False)

        # Read number of zones.
        nblocks = stream.read_int()
        if nblocks != len(domain.zones):
            raise RuntimeError('nblocks (%d) in %r != #Mesh zones (%d)' %
                               (nblocks, restart, len(domain.zones)))

        # Read zone dimensions.
        for zone in domain.zones:
            name = domain.zone_name(zone)
            imax, jmax, kmax = stream.read_ints(3)
            logger.debug('    %s: %dx%dx%d', name, imax, jmax, kmax)
            zone_i, zone_j, zone_k = zone.shape
            if imax != zone_i + 1 or jmax != zone_j + 1 or kmax != zone_k + 1:
                raise RuntimeError('%s: Restart %dx%dx%d != Mesh %dx%dx%d' \
                                   % (name, imax, jmax, kmax,
                                      zone_i, zone_j, zone_k))
        # Read zone variables.
        for i, zone in enumerate(domain.zones):
            name = domain.zone_name(zone)
            zone_i, zone_j, zone_k = zone.shape
            shape = (zone_i + 1, zone_j + 1, zone_k + 1)
            logger.debug('reading data for %s', name)

            zone.flow_solution.grid_location = 'CellCenter'
            zone.flow_solution.ghosts = [1, 1, 1, 1, 1, 1]

            name = 'density'
            arr = stream.read_floats(shape, order='Fortran')
            logger.debug('    %s min %g, max %g', name, arr.min(), arr.max())
            zone.flow_solution.add_array(name, arr)

            vec = Vector()
            if zone.coordinate_system == 'Cartesian':
                vec.x = stream.read_floats(shape, order='Fortran')
                logger.debug('    momentum.x min %g, max %g', vec.x.min(),
                             vec.x.max())
                vec.y = stream.read_floats(shape, order='Fortran')
                logger.debug('    momentum.y min %g, max %g', vec.y.min(),
                             vec.y.max())
                vec.z = stream.read_floats(shape, order='Fortran')
                logger.debug('    momentum.z min %g, max %g', vec.z.min(),
                             vec.z.max())
            else:
                vec.z = stream.read_floats(shape, order='Fortran')
                logger.debug('    momentum.z min %g, max %g', vec.z.min(),
                             vec.z.max())
                vec.r = stream.read_floats(shape, order='Fortran')
                logger.debug('    momentum.r min %g, max %g', vec.r.min(),
                             vec.r.max())
                vec.t = stream.read_floats(shape, order='Fortran')
                logger.debug('    momentum.t min %g, max %g', vec.t.min(),
                             vec.t.max())
            zone.flow_solution.add_vector('momentum', vec)

            name = 'energy_stagnation_density'
            arr = stream.read_floats(shape, order='Fortran')
            logger.debug('    %s min %g, max %g', name, arr.min(), arr.max())
            zone.flow_solution.add_array(name, arr)

            name = 'pressure'
            arr = stream.read_floats(shape, order='Fortran')
            logger.debug('    %s min %g, max %g', name, arr.min(), arr.max())
            zone.flow_solution.add_array(name, arr)

        # Read zone scalars.
        ncyc = stream.read_ints(len(domain.zones))
        dtheta = stream.read_floats(len(domain.zones))
        omegal = stream.read_floats(len(domain.zones))
        logger.debug('    ncyc %s', str(ncyc))
        logger.debug('    dtheta %s', str(dtheta))
        logger.debug('    omegal %s', str(omegal))
        for i, zone in enumerate(domain.zones):
            zone.flow_solution.ncyc = ncyc[i]
            zone.flow_solution.dtheta = dtheta[i]
            zone.flow_solution.omegal = omegal[i]

        # Implicit calculation data not supported.

    return domain