Ejemplo n.º 1
0
def read_grid_from_file(f):
    x0, y0, z0, x1, y1, z1 = struct.unpack("dddddd", f.read(48))
    nx, ny, nz = struct.unpack("iii", f.read(12))
    num_objects = struct.unpack("I", f.read(4))[0]
    num_arrays = struct.unpack("I", f.read(4))[0]

    nbytes = nx * ny * nz * 4
    arr = array("B")
    arr.fromfile(f, nbytes)
    address = arr.buffer_info()[0]
    asm_cells = x86.MemData(nbytes)
    memcpy(asm_cells.ptr(), address, nbytes)

    nbytes = num_arrays * 4 + num_objects * 4 + 4
    arr = array("B")
    arr.fromfile(f, nbytes)
    address = arr.buffer_info()[0]
    lin_array = x86.MemData(nbytes)
    memcpy(lin_array.ptr(), address, nbytes)

    bbox = BBox(Vector3(x0, y0, z0), Vector3(x1, y1, z1))
    grid = GridMesh()
    grid.bbox = bbox
    grid.nx = nx
    grid.ny = ny
    grid.nz = nz
    grid.num_objects = num_objects
    grid.num_arrays = num_arrays
    grid.asm_cells = asm_cells
    grid.lin_array = lin_array
    return grid
Ejemplo n.º 2
0
def read_buf_from_file(f, buf_id):
    nitems = struct.unpack("Q", f.read(8))[0]
    buf = create_buffer(buf_id, nitems)
    nbytes = buf.item_size() * nitems
    arr = array("B")
    arr.fromfile(f, nbytes)

    address = arr.buffer_info()[0]
    memcpy(buf._address.ptr(), address, nbytes)
    buf._size = nitems
    return buf
Ejemplo n.º 3
0
    def _resize(self):
        if self._size >= 0 and self._size <= 100:
            self._reserve += 1
        elif self._size > 100 and self._size <= 10000:
            self._reserve += 100
        elif self._size > 10000 and self._size <= 1000000:
            self._reserve += 10000
        else:
            self._reserve += 100000

        temp = x86.MemData(self._item_size*self._reserve)
        memcpy(temp.ptr(), self._address.ptr(), self._size*self._item_size) 
        self._address = temp
Ejemplo n.º 4
0
    def _resize(self):
        if self._size >= 0 and self._size <= 100:
            self._reserve += 1
        elif self._size > 100 and self._size <= 10000:
            self._reserve += 100
        elif self._size > 10000 and self._size <= 1000000:
            self._reserve += 10000
        else:
            self._reserve += 100000

        temp = x86.MemData(self._item_size * self._reserve)
        memcpy(temp.ptr(), self._address.ptr(), self._size * self._item_size)
        self._address = temp
Ejemplo n.º 5
0
    def read_bytes(self, offset=0, nbytes=-1):
        buff_size = self._size * self._item_size
        n = nbytes
        if n == -1:
            n = buff_size - offset
        n = min(n, buff_size - offset)
        n = max(n, 0)

        arr = array('B', [0]*n)

        address = arr.buffer_info()[0]
        off_addr = self._address.ptr() + offset
        memcpy(address, off_addr, n) 
        return arr
Ejemplo n.º 6
0
    def read_bytes(self, offset=0, nbytes=-1):
        buff_size = self._size * self._item_size
        n = nbytes
        if n == -1:
            n = buff_size - offset
        n = min(n, buff_size - offset)
        n = max(n, 0)

        arr = array('B', [0] * n)

        address = arr.buffer_info()[0]
        off_addr = self._address.ptr() + offset
        memcpy(address, off_addr, n)
        return arr
Ejemplo n.º 7
0
def write_grid_to_file(f, grid):
    b = grid.bbox
    bbox = struct.pack("dddddd", b.x0, b.y0, b.z0, b.x1, b.y1, b.z1)
    f.write(bbox)
    dim = struct.pack("iii", grid.nx, grid.ny, grid.nz)
    f.write(dim)
    f.write(struct.pack("I", grid.num_objects))
    f.write(struct.pack("I", grid.num_arrays))

    nbytes = int(grid.nx * grid.ny * grid.nz) * 4

    arr = array("B", [0] * nbytes)
    address = arr.buffer_info()[0]
    memcpy(address, grid.asm_cells.ptr(), nbytes)
    arr.tofile(f)

    nbytes = grid.num_arrays * 4 + grid.num_objects * 4 + 4
    arr = array("B", [0] * nbytes)
    address = arr.buffer_info()[0]
    memcpy(address, grid.lin_array.ptr(), nbytes)
    arr.tofile(f)