def read_plot3d_grid(grid_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`. grid_file: string Grid filename. """ logger = logger or NullLogger() domain = DomainObj() mode = 'rb' if binary else 'r' with open(grid_file, mode) as inp: logger.info('reading grid file %r', grid_file) stream = Stream(inp, binary, big_endian, single_precision, False, unformatted, False) # Read zone dimensions. shape = _read_plot3d_shape(stream, multiblock, dim, logger) # Read zone coordinates. for i in range(len(shape)): zone = domain.add_zone('', Zone()) name = domain.zone_name(zone) logger.debug('reading coordinates for %s', name) _read_plot3d_coords(zone, stream, shape[i], blanking, planes, logger) return domain
def read_plot3d_shape(grid_file, multiblock=True, dim=3, binary=True, big_endian=False, unformatted=True, logger=None): """ Returns a list of zone dimensions from Plot3D `grid_file`. grid_file: string Grid filename. """ logger = logger or NullLogger() mode = 'rb' if binary else 'r' with open(grid_file, mode) as inp: logger.info('reading grid file %r', grid_file) stream = Stream(inp, binary, big_endian, True, False, unformatted, False) return _read_plot3d_shape(stream, multiblock, dim, logger)
def write_plot3d_f(domain, grid_file, f_file, varnames=None, planes=False, binary=True, big_endian=False, single_precision=True, unformatted=True, logger=None): """ Writes `domain` to `grid_file` and `f_file` in Plot3D format. If `varnames` is None, then all arrays and then all vectors are written. Ghost data is not written. domain: :class:`DomainObj` or :class:`Zone` The domain or zone to be written. grid_file: string Grid filename. f_file: string Function data filename. """ logger = logger or NullLogger() if isinstance(domain, DomainObj): writing_domain = True zones = domain.zones elif isinstance(domain, Zone): writing_domain = False zones = [domain] else: raise TypeError("'domain' argument must be a DomainObj or Zone") if varnames is None: flow = zones[0].flow_solution varnames = [flow.name_of_obj(obj) for obj in flow.arrays] varnames.extend([flow.name_of_obj(obj) for obj in flow.vectors]) # Verify we have the needed data. for zone in zones: flow = zone.flow_solution missing = [] for name in varnames: if not hasattr(flow, name): missing.append(name) if missing: if writing_domain: name = domain.zone_name(zone) else: name = '' raise AttributeError('zone %s flow_solution is missing %s' % (name, missing)) # Write grid file. write_plot3d_grid(domain, grid_file, planes, binary, big_endian, single_precision, unformatted, logger) # Write F file. mode = 'wb' if binary else 'w' with open(f_file, mode) as out: logger.info('writing F file %r', f_file) stream = Stream(out, binary, big_endian, single_precision, False, unformatted, False) if len(zones) > 1: # Write number of zones. stream.write_int(len(zones), full_record=True) # Write zone dimensions. _write_plot3d_dims(domain, stream, logger, varnames) # Write zone variables. for zone in zones: if writing_domain: name = domain.zone_name(zone) else: name = 'zone' logger.debug('writing data for %s', name) _write_plot3d_vars(zone, stream, varnames, planes, logger)
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 write_plot3d_q(domain, grid_file, q_file, planes=False, binary=True, big_endian=False, single_precision=True, unformatted=True, logger=None): """ Writes `domain` to `grid_file` and `q_file` in Plot3D format. Requires 'density', 'momentum', and 'energy_stagnation_density' variables as well as 'mach', 'alpha', 'reynolds', and 'time' scalars. Ghost data is not written. domain: :class:`DomainObj` or :class:`Zone` The domain or zone to be written. grid_file: string Grid filename. q_file: string Q data filename. """ logger = logger or NullLogger() if isinstance(domain, DomainObj): writing_domain = True zones = domain.zones elif isinstance(domain, Zone): writing_domain = False zones = [domain] else: raise TypeError("'domain' argument must be a DomainObj or Zone") # Verify we have the needed data. for zone in zones: flow = zone.flow_solution missing = [] for name in ('mach', 'alpha', 'reynolds', 'time', 'density', 'momentum', 'energy_stagnation_density'): if not hasattr(flow, name): missing.append(name) if missing: if writing_domain: name = domain.zone_name(zone) else: name = '' raise AttributeError('zone %s flow_solution is missing %s' % (name, missing)) # Write grid file. write_plot3d_grid(domain, grid_file, planes, binary, big_endian, single_precision, unformatted, logger) # Write Q file. mode = 'wb' if binary else 'w' with open(q_file, mode) as out: logger.info('writing Q file %r', q_file) stream = Stream(out, binary, big_endian, single_precision, False, unformatted, False) if len(zones) > 1: # Write number of zones. stream.write_int(len(zones), full_record=True) # Write zone dimensions. _write_plot3d_dims(domain, stream, logger) # Write zone scalars and variables. varnames = ('density', 'momentum', 'energy_stagnation_density') for zone in zones: if writing_domain: name = domain.zone_name(zone) else: name = 'zone' logger.debug('writing data for %s', name) _write_plot3d_qscalars(zone, stream, logger) _write_plot3d_vars(zone, stream, varnames, planes, logger)
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
def test_misc(self): logging.debug('') logging.debug('test_misc') out = open(self.filename, 'w') stream = Stream(out) stream.close() # Unformatted recordlength errors. data = '\x42' + UNF_I4[1:] with open(self.filename, 'wb') as out: out.write(data) with open(self.filename, 'rb') as inp: stream = Stream(inp, binary=True, unformatted=True) assert_raises(self, 'stream.read_int(full_record=True)', globals(), locals(), RuntimeError, 'unexpected recordlength 66') data = UNF_I4[:-1] + '\x42' with open(self.filename, 'wb') as out: out.write(data) with open(self.filename, 'rb') as inp: stream = Stream(inp, binary=True, unformatted=True) assert_raises(self, 'stream.read_int(full_record=True)', globals(), locals(), RuntimeError, 'mismatched recordlength 1107296260 vs. 4') data = '\x42' + UNF_I4A[1:] with open(self.filename, 'wb') as out: out.write(data) with open(self.filename, 'rb') as inp: stream = Stream(inp, binary=True, unformatted=True) assert_raises(self, 'stream.read_ints(8, full_record=True)', globals(), locals(), RuntimeError, 'unexpected recordlength 66') data = UNF_I4A[:-1] + '\x42' with open(self.filename, 'wb') as out: out.write(data) with open(self.filename, 'rb') as inp: stream = Stream(inp, binary=True, unformatted=True) assert_raises(self, 'stream.read_ints(8, full_record=True)', globals(), locals(), RuntimeError, 'mismatched recordlength 1107296288 vs. 32') data = '\x42' + UNF_R8[1:] with open(self.filename, 'wb') as out: out.write(data) with open(self.filename, 'rb') as inp: stream = Stream(inp, binary=True, unformatted=True) assert_raises(self, 'stream.read_float(full_record=True)', globals(), locals(), RuntimeError, 'unexpected recordlength 66') data = UNF_R8[:-1] + '\x42' with open(self.filename, 'wb') as out: out.write(data) with open(self.filename, 'rb') as inp: stream = Stream(inp, binary=True, unformatted=True) assert_raises(self, 'stream.read_float(full_record=True)', globals(), locals(), RuntimeError, 'mismatched recordlength 1107296264 vs. 8') data = '\x42' + UNF_R8A[1:] with open(self.filename, 'wb') as out: out.write(data) with open(self.filename, 'rb') as inp: stream = Stream(inp, binary=True, unformatted=True) assert_raises(self, 'stream.read_floats(8, full_record=True)', globals(), locals(), RuntimeError, 'unexpected recordlength 66') data = UNF_R8A[:-1] + '\x42' with open(self.filename, 'wb') as out: out.write(data) with open(self.filename, 'rb') as inp: stream = Stream(inp, binary=True, unformatted=True) assert_raises(self, 'stream.read_floats(8, full_record=True)', globals(), locals(), RuntimeError, 'mismatched recordlength 1107296320 vs. 64')
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_float64(self): logging.debug('') logging.debug('test_float64') # Double precision. data = numpy.arange(0, 10, dtype=numpy.float64) with open(self.filename, 'wb') as out: stream = Stream(out, binary=True) stream.write_floats(data) self.assertEqual(os.path.getsize(self.filename), 80) with open(self.filename, 'rb') as inp: stream = Stream(inp, binary=True) new_data = stream.read_floats(data.size) numpy.testing.assert_array_equal(new_data, data) # Unformatted scalar. with open(self.filename, 'wb') as out: stream = Stream(out, binary=True, unformatted=True) stream.write_float(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_R8) with open(self.filename, 'rb') as inp: stream = Stream(inp, binary=True) new_data = stream.read_float() 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_float(full_record=True) self.assertEqual(new_data, 1.) # Unformatted array. data = numpy.arange(1, 9, dtype=numpy.float64) with open(self.filename, 'wb') as out: stream = Stream(out, binary=True, unformatted=True) stream.write_floats(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_R8A) with open(self.filename, 'rb') as inp: stream = Stream(inp, binary=True) new_data = stream.read_floats(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_floats(data.size, full_record=True) numpy.testing.assert_array_equal(new_data, data) # Write as single precision. with open(self.filename, 'wb') as out: stream = Stream(out, binary=True, single_precision=True) stream.write_floats(data) self.assertEqual(os.path.getsize(self.filename), 32) with open(self.filename, 'rb') as inp: stream = Stream(inp, binary=True, single_precision=True) new_data = stream.read_floats(data.size) numpy.testing.assert_array_equal(new_data, data) # Row-major. data = numpy.arange(0, 10, dtype=numpy.float64) arr2d = data.reshape((5, 2)) with open(self.filename, 'wb') as out: stream = Stream(out, binary=True) stream.write_floats(arr2d) self.assertEqual(os.path.getsize(self.filename), 80) with open(self.filename, 'rb') as inp: stream = Stream(inp, binary=True) new_data = stream.read_floats((5, 2)) numpy.testing.assert_array_equal(new_data, arr2d) # Column-major. with open(self.filename, 'wb') as out: stream = Stream(out, binary=True) stream.write_floats(arr2d, order='Fortran') self.assertEqual(os.path.getsize(self.filename), 80) with open(self.filename, 'rb') as inp: stream = Stream(inp, binary=True) new_data = stream.read_floats((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) new_data = stream.read_floats((5, 2), order='Fortran') numpy.testing.assert_array_equal(new_data, arr2d) # Text. with open(self.filename, 'w') as out: stream = Stream(out) stream.write_floats(arr2d, order='Fortran', linecount=4) with open(self.filename, 'r') as inp: stream = Stream(inp) new_data = stream.read_floats((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_floats((5, 2), order='Fortran') numpy.testing.assert_array_equal(new_data, arr2d)
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'")
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 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 write(domain, casename, logger, suffix='restart.new'): """ Write domain as ADPAC .mesh and .restart files. NOTE: if any zones are cylindrical, their grid_coordinates are changed to cartesian and then back to cylindrical. This will affect the coordinate values slightly. """ # FIXME: don't mess up mesh! # Write (cartesian) mesh. cylindricals = [] for zone in domain.zones: if zone.coordinate_system == 'Cylindrical': logger.debug('Converting %s to cartesian coordinates', domain.zone_name(zone)) cylindricals.append(zone) zone.grid_coordinates.make_cartesian(axis='x') try: write_plot3d_grid(domain, casename + '.mesh', big_endian=True, unformatted=False, logger=logger) finally: for zone in cylindricals: logger.debug('Converting %s back to cylindrical coordinates', domain.zone_name(zone)) zone.grid_coordinates.make_cylindrical(axis='x') # Write restart. restart = casename + suffix with open(restart, 'wb') as out: logger.info('writing restart file %r', restart) stream = Stream(out, binary=True, big_endian=True, single_precision=True, integer_8=False, unformatted=False, recordmark_8=False) # Write number of zones. stream.write_int(len(domain.zones)) # Write zone dimensions. for zone in domain.zones: name = domain.zone_name(zone) imax, jmax, kmax = zone.shape logger.debug(' %s: %dx%dx%d', name, imax + 1, jmax + 1, kmax + 1) stream.write_ints((imax + 1, jmax + 1, kmax + 1)) # Write zone variables. for zone in domain.zones: name = domain.zone_name(zone) logger.debug('writing data for %s', name) arr = zone.flow_solution.density logger.debug(' density min %g, max %g', arr.min(), arr.max()) stream.write_floats(arr, order='Fortran') if zone.coordinate_system == 'Cartesian': arr = zone.flow_solution.momentum.x logger.debug(' momentum.x min %g, max %g', arr.min(), arr.max()) stream.write_floats(zone.flow_solution.momentum.x, order='Fortran') arr = zone.flow_solution.momentum.y logger.debug(' momentum.y min %g, max %g', arr.min(), arr.max()) stream.write_floats(zone.flow_solution.momentum.y, order='Fortran') arr = zone.flow_solution.momentum.z logger.debug(' momentum.z min %g, max %g', arr.min(), arr.max()) stream.write_floats(zone.flow_solution.momentum.z, order='Fortran') else: arr = zone.flow_solution.momentum.z logger.debug(' momentum.z min %g, max %g', arr.min(), arr.max()) stream.write_floats(zone.flow_solution.momentum.z, order='Fortran') arr = zone.flow_solution.momentum.r logger.debug(' momentum.r min %g, max %g', arr.min(), arr.max()) stream.write_floats(zone.flow_solution.momentum.r, order='Fortran') arr = zone.flow_solution.momentum.t logger.debug(' momentum.t min %g, max %g', arr.min(), arr.max()) stream.write_floats(zone.flow_solution.momentum.t, order='Fortran') arr = zone.flow_solution.energy_stagnation_density logger.debug(' energy_stagnation_density min %g, max %g', arr.min(), arr.max()) stream.write_floats(zone.flow_solution.energy_stagnation_density, order='Fortran') arr = zone.flow_solution.pressure logger.debug(' pressure min %g, max %g', arr.min(), arr.max()) stream.write_floats(zone.flow_solution.pressure, order='Fortran') # Write zone scalars. ncyc = [] dtheta = [] omegal = [] for zone in domain.zones: ncyc.append(zone.flow_solution.ncyc) dtheta.append(zone.flow_solution.dtheta) omegal.append(zone.flow_solution.omegal) logger.debug(' ncyc %s', str(ncyc)) logger.debug(' dtheta %s', str(dtheta)) logger.debug(' omegal %s', str(omegal)) stream.write_ints(ncyc) stream.write_floats(dtheta) stream.write_floats(omegal) # Implicit calculation data not supported. stream.write_int(0)