Ejemplo n.º 1
0
    def runTest(self):
        nx, ny, nz, str_f, pt0, pt1 = self.args

        slidx = common.slices_two_points(pt0, pt1)
        str_fs = common.convert_to_tuple(str_f)

        # instance
        fields = Fields(0, nx, ny, nz, '', 'single')
        getf = GetFields(fields, str_f, pt0, pt1) 
        
        # host allocations
        ehs = common_random.generate_ehs(nx, ny, nz, fields.dtype)
        eh_dict = dict( zip(['ex', 'ey', 'ez', 'hx', 'hy', 'hz'], ehs) )
        fields.set_eh_bufs(*ehs)

        # verify
        getf.wait()

        for str_f in str_fs:
            original = eh_dict[str_f][slidx]
            copy = getf.get_fields(str_f)
            norm = np.linalg.norm(original - copy)
            self.assertEqual(norm, 0, '%s, %g' % (self.args, norm))

        fields.context_pop()
    def runTest(self):
        nx, ny, nz, str_f, pt0, pt1 = self.args

        slidx = common.slices_two_points(pt0, pt1)
        str_fs = common.convert_to_tuple(str_f)

        # instance
        gpu_devices = common_gpu.gpu_device_list(print_info=False)
        context = cl.Context(gpu_devices)
        device = gpu_devices[0]
        fields = Fields(context, device, nx, ny, nz, '', 'single')
        getf = GetFields(fields, str_f, pt0, pt1) 
        
        # host allocations
        ehs = common_random.generate_ehs(nx, ny, nz, fields.dtype)
        eh_dict = dict( zip(['ex', 'ey', 'ez', 'hx', 'hy', 'hz'], ehs) )
        fields.set_eh_bufs(*ehs)

        # verify
        getf.get_event().wait()

        for str_f in str_fs:
            original = eh_dict[str_f][slidx]
            copy = getf.get_fields(str_f)
            norm = np.linalg.norm(original - copy)
            self.assertEqual(norm, 0, '%s, %g' % (self.args, norm))
Ejemplo n.º 3
0
    def runTest(self):
        nx, ny, nz, str_f, pt0, pt1 = self.args

        slidx = common.slice_index_two_points(pt0, pt1)
        str_fs = common.convert_to_tuple(str_f)

        # instance
        gpu_devices = common_gpu.gpu_device_list(print_info=False)
        context = cl.Context(gpu_devices)
        device = gpu_devices[0]
        fields = Fields(context, device, nx, ny, nz, '', 'single')
        getf = GetFields(fields, str_f, pt0, pt1) 
        
        # host allocations
        ehs = common_update.generate_random_ehs(nx, ny, nz, fields.dtype)
        eh_dict = dict( zip(['ex', 'ey', 'ez', 'hx', 'hy', 'hz'], ehs) )
        fields.set_eh_bufs(*ehs)

        # verify
        getf.get_event().wait()

        for str_f in str_fs:
            original = eh_dict[str_f][slidx]
            copy = getf.get_fields(str_f)
            norm = np.linalg.norm(original - copy)
            self.assertEqual(norm, 0, '%s, %g' % (self.args, norm))
Ejemplo n.º 4
0
    def verify(s, pt0, pt1):
        print('pt0 = %s, pt1 = %s' % (pt0, pt1))
        slidx = s.get_slidx(pt0, pt1)

        for strf in s.strf_list:
            # non-spatial
            fset = SetFields(s.fdtd, strf, pt0, pt1)
            value = np.random.rand()
            fset.set_fields(value)

            fget = GetFields(s.fdtd, strf, pt0, pt1)
            fget.get_event().wait()
            copy = fget.get_fields(strf)

            assert np.linalg.norm(value - copy) == 0

        if pt0 != pt1:
            for strf in s.strf_list:
                # spatial
                fset = SetFields(s.fdtd, strf, pt0, pt1, np.ndarray)
                values = np.random.rand(*fset.rplist['shape']).astype(
                    s.fdtd.dtype)
                fset.set_fields(values)

                fget = GetFields(s.fdtd, strf, pt0, pt1)
                fget.get_event().wait()
                copy = fget.get_fields(strf)

                assert np.linalg.norm(values - copy) == 0
Ejemplo n.º 5
0
    def __init__(self, gpuf, core, direction):
        common.check_type('gpuf', gpuf, Fields)
        common.check_value('direction', direction, ('+', '-', '+-'))

        if '+' in direction:
            self.gf_h = gf_h = GetFields(gpuf, ['hy', 'hz'], (-1, 0, 0),
                                         (-1, -1, -1))
            self.sf_e = SetFields(gpuf, ['ey', 'ez'], (-1, 0, 0), (-1, -1, -1),
                                  True)

            target = 0 if rank == size - 1 else rank + 1
            self.req_send_h = comm.Send_init(gf_h.host_array, target, tag=0)
            self.tmp_recv_e = np.zeros(gf_h.host_array.shape, gpuf.dtype)
            self.req_recv_e = comm.Recv_init(self.tmp_recv_e, target, tag=1)

        if '-' in direction:
            self.gf_e = gf_e = GetFields(gpuf, ['ey', 'ez'], (0, 0, 0),
                                         (0, -1, -1))
            self.sf_h = SetFields(gpuf, ['hy', 'hz'], (0, 0, 0), (0, -1, -1),
                                  True)

            target = size - 1 if rank == 0 else rank - 1
            self.req_send_e = comm.Send_init(gf_e.host_array, target, tag=1)
            self.tmp_recv_h = np.zeros(gf_e.host_array.shape, gpuf.dtype)
            self.req_recv_h = comm.Recv_init(self.tmp_recv_h, target, tag=0)

        # global variables
        self.core = core
        self.direction = direction
Ejemplo n.º 6
0
    def test_boundary(s):
        print('\n-- test boundary (two fields) --')
        shape_dict = {
            'x': (s.ny * 2, s.nz),
            'y': (s.nx * 2, s.nz),
            'z': (s.nx * 2, s.ny)
        }

        print('E fields')
        str_fs_dict = {'x': ['ey', 'ez'], 'y': ['ex', 'ez'], 'z': ['ex', 'ey']}
        pt0_dict = {
            'x': (s.nx - 1, 0, 0),
            'y': (0, s.ny - 1, 0),
            'z': (0, 0, s.nz - 1)
        }
        pt1 = (s.nx - 1, s.ny - 1, s.nz - 1)

        for axis in str_fs_dict.keys():
            print('direction : %s' % axis)
            str_fs = str_fs_dict[axis]
            pt0 = pt0_dict[axis]
            slidx = s.get_slidx(pt0, pt1)
            fset = SetFields(s.fdtd, str_fs, pt0, pt1, np.ndarray)
            values = np.random.rand(*shape_dict[axis]).astype(s.fdtd.dtype)
            fset.set_fields(values)

            fget = GetFields(s.fdtd, str_fs, pt0, pt1)
            fget.get_event().wait()
            copy = fget.get_fields()

            assert np.linalg.norm(values - copy) == 0

        print('H fields')
        str_fs_dict = {'x': ['hy', 'hz'], 'y': ['hx', 'hz'], 'z': ['hx', 'hy']}
        pt0 = (0, 0, 0)
        pt1_dict = {
            'x': (0, s.ny - 1, s.nz - 1),
            'y': (s.nx - 1, 0, s.nz - 1),
            'z': (s.nx - 1, s.ny - 1, 0)
        }

        for axis in str_fs_dict.keys():
            print('direction : %s' % axis)
            str_fs = str_fs_dict[axis]
            pt1 = pt1_dict[axis]
            slidx = s.get_slidx(pt0, pt1)
            fset = SetFields(s.fdtd, str_fs, pt0, pt1, np.ndarray)
            values = np.random.rand(*shape_dict[axis]).astype(s.fdtd.dtype)
            fset.set_fields(values)

            fget = GetFields(s.fdtd, str_fs, pt0, pt1)
            fget.get_event().wait()
            copy = fget.get_fields()

            assert np.linalg.norm(values - copy) == 0
Ejemplo n.º 7
0
    def verify(s, pt0, pt1):
        print('pt0 = %s, pt1 = %s' % (pt0, pt1))
        slidx = s.get_slidx(pt0, pt1)

        for strf in s.strf_list:
            fget = GetFields(s.fdtd, strf, pt0, pt1)
            fget.get_event().wait()
            original = s.fhosts[strf][slidx]
            copy = fget.get_fields(strf)
            #print original, copy
            assert np.linalg.norm(original - copy) == 0
Ejemplo n.º 8
0
	def verify(s, pt0, pt1):
		print('pt0 = %s, pt1 = %s' % (pt0, pt1))
		slidx = s.get_slidx(pt0, pt1)

		for strf in s.strf_list:
			fget = GetFields(s.fdtd, strf, pt0, pt1)
			fget.get_event().wait()
			original = s.fhosts[strf][slidx]
			copy = fget.get_fields(strf)
			#print original, copy
			assert np.linalg.norm(original - copy) == 0
Ejemplo n.º 9
0
    def __init__(self, node_fields, target_rank, direction):
        common.check_type('node_fields', node_fields, Fields)
        common.check_type('target_rank', target_rank, int)

        # local variables
        nodef = node_fields
        dtype = nodef.dtype

        assert rank != target_rank, 'The target_rank %d is same as the my_rank %d.' % (
            target_rank, rank)

        # create instances (getf, setf and mpi requests)
        if '+' in direction:
            mainf = nodef.mainf_list[-1]
            nx, ny, nz = mainf.ns
            getf = GetFields(mainf, ['hy', 'hz'], (nx - 1, 0, 0),
                             (nx - 1, ny - 1, nz - 1))
            setf = SetFields(mainf, ['ey', 'ez'], (nx - 1, 0, 0),
                             (nx - 1, ny - 1, nz - 1), True)

            if rank < target_rank:
                tag_send, tag_recv = 0, 1
            elif rank > target_rank:
                tag_send, tag_recv = 2, 3

        elif '-' in direction:
            mainf = nodef.mainf_list[0]
            nx, ny, nz = mainf.ns
            getf = GetFields(mainf, ['ey', 'ez'], (0, 0, 0),
                             (0, ny - 1, nz - 1))
            setf = SetFields(mainf, ['hy', 'hz'], (0, 0, 0),
                             (0, ny - 1, nz - 1), True)

            if rank > target_rank:
                tag_send, tag_recv = 1, 0
            elif rank < target_rank:  # pbc
                tag_send, tag_recv = 3, 2

        # global variables
        self.target_rank = target_rank
        self.getf = getf
        self.setf = setf
        self.tag_send = tag_send
        self.tag_recv = tag_recv
        self.tmp_recv = np.zeros(getf.host_array.shape, dtype)

        # global functions
        self.update_e = self.recv if '+' in direction else self.send
        self.update_h = self.send if '+' in direction else self.recv

        # append instance
        self.priority_type = 'mpi'
        nodef.append_instance(self)
Ejemplo n.º 10
0
    def __init__(s, fields_list, axis):
        emf_list = fields_list

        e_strfs, h_strfs, pt0, pt1 = common_gpu.get_strfs_pts(
            axis, *emf_list[0].ns)

        s.e_getf = GetFields(emf_list[0], e_strfs, pt0['-'], pt1['-'])
        s.h_setf = SetFields(emf_list[0], h_strfs, pt0['-'], pt1['-'],
                             np.ndarray, True)

        s.h_getf = GetFields(emf_list[-1], h_strfs, pt0['+'], pt1['+'])
        s.e_setf = SetFields(emf_list[-1], e_strfs, pt0['+'], pt1['+'],
                             np.ndarray, True)
Ejemplo n.º 11
0
	def verify(s, pt0, pt1):
		print('pt0 = %s, pt1 = %s' % (pt0, pt1))
		slidx = s.get_slidx(pt0, pt1)

		for strf in s.strf_list:
			# non-spatial
			fset = SetFields(s.fdtd, strf, pt0, pt1)
			value = np.random.rand()
			fset.set_fields(value)

			fget = GetFields(s.fdtd, strf, pt0, pt1)
			fget.get_event().wait()
			copy = fget.get_fields(strf)

			assert np.linalg.norm(value - copy) == 0

		if pt0 != pt1:
			for strf in s.strf_list:
				# spatial
				fset = SetFields(s.fdtd, strf, pt0, pt1, np.ndarray)
				values = np.random.rand(*fset.rplist['shape']).astype(s.fdtd.dtype)
				fset.set_fields(values)

				fget = GetFields(s.fdtd, strf, pt0, pt1)
				fget.get_event().wait()
				copy = fget.get_fields(strf)

				assert np.linalg.norm(values - copy) == 0
Ejemplo n.º 12
0
    def test_boundary(s):
        print('\n-- test boundary (two fields) --')

        print('E fields')
        str_fs_dict = {'x': ['ey', 'ez'], 'y': ['ex', 'ez'], 'z': ['ex', 'ey']}
        pt0 = (0, 0, 0)
        pt1_dict = {
            'x': (0, s.ny - 1, s.nz - 1),
            'y': (s.nx - 1, 0, s.nz - 1),
            'z': (s.nx - 1, s.ny - 1, 0)
        }

        for axis in str_fs_dict.keys():
            print('direction : %s' % axis)
            str_fs = str_fs_dict[axis]
            pt1 = pt1_dict[axis]
            slidx = s.get_slidx(pt0, pt1)
            fget = GetFields(s.fdtd, str_fs, pt0, pt1)
            fget.get_event().wait()

            for strf in str_fs:
                original = s.fhosts[strf][slidx]
                copy = fget.get_fields(strf)
                assert np.linalg.norm(original - copy) == 0

        print('H fields')
        str_fs_dict = {'x': ['hy', 'hz'], 'y': ['hx', 'hz'], 'z': ['hx', 'hy']}
        pt0_dict = {
            'x': (s.nx - 1, 0, 0),
            'y': (0, s.ny - 1, 0),
            'z': (0, 0, s.nz - 1)
        }
        pt1 = (s.nx - 1, s.ny - 1, s.nz - 1)

        for axis in str_fs_dict.keys():
            print('direction : %s' % axis)
            str_fs = str_fs_dict[axis]
            pt0 = pt0_dict[axis]
            slidx = s.get_slidx(pt0, pt1)
            fget = GetFields(s.fdtd, str_fs, pt0, pt1)
            fget.get_event().wait()

            for strf in str_fs:
                original = s.fhosts[strf][slidx]
                copy = fget.get_fields(strf)
                assert np.linalg.norm(original - copy) == 0
Ejemplo n.º 13
0
    def __init__(s, fields_list, axis):
        emf_list = fields_list

        e_strfs, h_strfs, pt0, pt1 = common.get_strfs_pts_for_boundary(
            axis, *emf_list[0].ns)

        s.e_getfs, s.h_getfs = [], []
        s.e_setfs, s.h_setfs = [], []
        for emf in emf_list[1:]:
            s.e_getfs.append(GetFields(emf, e_strfs, pt0['-'], pt1['-']))
            s.h_setfs.append(
                SetFields(emf, h_strfs, pt0['-'], pt1['-'], np.ndarray))

        for emf in emf_list[:-1]:
            s.h_getfs.append(GetFields(emf, h_strfs, pt0['+'], pt1['+']))
            s.e_setfs.append(
                SetFields(emf, e_strfs, pt0['+'], pt1['+'], np.ndarray))
Ejemplo n.º 14
0
    def __init__(self, gpuf, core, direction, tmax):
        common.check_type('gpuf', gpuf, Fields)
        common.check_value('direction', direction, ('+', '-', '+-'))

        if '+' in direction:
            self.gf_h = gf_h = GetFields(gpuf, ['hy', 'hz'], (-1, 0, 0),
                                         (-1, -1, -1))
            self.sf_e = SetFields(gpuf, ['ey', 'ez'], (-1, 0, 0), (-1, -1, -1),
                                  True)

            self.req_send_h = comm.Send_init(gf_h.host_array, rank + 1, tag=0)
            self.tmp_recv_e_list = [
                np.zeros(gf_h.host_array.shape, gpuf.dtype) for i in range(2)
            ]
            self.req_recv_e_list = [
                comm.Recv_init(tmp_recv_e, rank + 1, tag=1)
                for tmp_recv_e in self.tmp_recv_e_list
            ]
            self.switch_e = 0

        if '-' in direction:
            self.gf_e = gf_e = GetFields(gpuf, ['ey', 'ez'], (0, 0, 0),
                                         (0, -1, -1))
            self.sf_h = SetFields(gpuf, ['hy', 'hz'], (0, 0, 0), (0, -1, -1),
                                  True)

            self.req_send_e = comm.Send_init(gf_e.host_array, rank - 1, tag=1)
            self.tmp_recv_h_list = [
                np.zeros(gf_e.host_array.shape, gpuf.dtype) for i in range(2)
            ]
            self.req_recv_h_list = [
                comm.Recv_init(tmp_recv_h, rank - 1, tag=0)
                for tmp_recv_h in self.tmp_recv_h_list
            ]
            self.switch_h = 0

        # global variables
        self.gpuf = gpuf
        self.core = core
        self.direction = direction

        self.tmax = tmax
        self.tstep = 1
Ejemplo n.º 15
0
	def test_boundary(s):
		print('\n-- test boundary (two fields) --')
		shape_dict = {'x':(s.ny*2, s.nz), 'y':(s.nx*2, s.nz), 'z':(s.nx*2, s.ny)}

		print('E fields')
		str_fs_dict = {'x':['ey','ez'], 'y':['ex','ez'], 'z':['ex','ey']}
		pt0_dict = {'x':(s.nx-1, 0, 0), 'y':(0, s.ny-1, 0), 'z':(0, 0, s.nz-1)}
		pt1 = (s.nx-1, s.ny-1, s.nz-1)

		for axis in str_fs_dict.keys():
			print('direction : %s' % axis)
			str_fs = str_fs_dict[axis]
			pt0 = pt0_dict[axis]
			slidx = s.get_slidx(pt0, pt1)
			fset = SetFields(s.fdtd, str_fs, pt0, pt1, np.ndarray)
			values = np.random.rand(*shape_dict[axis]).astype(s.fdtd.dtype)
			fset.set_fields(values)

			fget = GetFields(s.fdtd, str_fs, pt0, pt1)
			fget.get_event().wait()
			copy = fget.get_fields()

			assert np.linalg.norm(values - copy) == 0


		print('H fields')
		str_fs_dict = {'x':['hy','hz'], 'y':['hx','hz'], 'z':['hx','hy']}
		pt0 = (0, 0, 0)
		pt1_dict = {'x':(0, s.ny-1, s.nz-1), 'y':(s.nx-1, 0, s.nz-1), 'z':(s.nx-1, s.ny-1, 0)}

		for axis in str_fs_dict.keys():
			print('direction : %s' % axis)
			str_fs = str_fs_dict[axis]
			pt1 = pt1_dict[axis]
			slidx = s.get_slidx(pt0, pt1)
			fset = SetFields(s.fdtd, str_fs, pt0, pt1, np.ndarray)
			values = np.random.rand(*shape_dict[axis]).astype(s.fdtd.dtype)
			fset.set_fields(values)

			fget = GetFields(s.fdtd, str_fs, pt0, pt1)
			fget.get_event().wait()
			copy = fget.get_fields()

			assert np.linalg.norm(values - copy) == 0
Ejemplo n.º 16
0
	def test_boundary(s):
		print('\n-- test boundary (two fields) --')

		print('E fields')
		str_fs_dict = {'x':['ey','ez'], 'y':['ex','ez'], 'z':['ex','ey']}
		pt0 = (0, 0, 0)
		pt1_dict = {'x':(0, s.ny-1, s.nz-1), 'y':(s.nx-1, 0, s.nz-1), 'z':(s.nx-1, s.ny-1, 0)}

		for axis in str_fs_dict.keys():
			print('direction : %s' % axis)
			str_fs = str_fs_dict[axis]
			pt1 = pt1_dict[axis]
			slidx = s.get_slidx(pt0, pt1)
			fget = GetFields(s.fdtd, str_fs, pt0, pt1)
			fget.get_event().wait()

			for strf in str_fs:
				original = s.fhosts[strf][slidx]
				copy = fget.get_fields(strf)
				assert np.linalg.norm(original - copy) == 0


		print('H fields')
		str_fs_dict = {'x':['hy','hz'], 'y':['hx','hz'], 'z':['hx','hy']}
		pt0_dict = {'x':(s.nx-1, 0, 0), 'y':(0, s.ny-1, 0), 'z':(0, 0, s.nz-1)}
		pt1 = (s.nx-1, s.ny-1, s.nz-1)

		for axis in str_fs_dict.keys():
			print('direction : %s' % axis)
			str_fs = str_fs_dict[axis]
			pt0 = pt0_dict[axis]
			slidx = s.get_slidx(pt0, pt1)
			fget = GetFields(s.fdtd, str_fs, pt0, pt1)
			fget.get_event().wait()

			for strf in str_fs:
				original = s.fhosts[strf][slidx]
				copy = fget.get_fields(strf)
				assert np.linalg.norm(original - copy) == 0
Ejemplo n.º 17
0
# plot
import matplotlib.pyplot as plt
plt.ion()
fig = plt.figure(figsize=(12,8))
imag = plt.imshow(np.zeros((nx, ny), fields.dtype).T, interpolation='nearest', origin='lower', vmin=-1.1, vmax=1.1)
plt.colorbar()
'''

# main loop
from datetime import datetime
from time import time
t0 = datetime.now()
t00 = time()

gtmp = GetFields(fields, 'ez', (0, 0, 0), (0, 0, 0))
for tstep in xrange(1, tmax + 1):
    fields.update_e()
    fields.update_h()
    '''
    if tstep % tgap == 0:
        print('[%s] %d/%d (%d %%)\r' % (datetime.now() - t0, tstep, tmax, float(tstep)/tmax*100)),
        sys.stdout.flush()

        getf.get_event().wait()
        imag.set_array( getf.get_fields().T )
        #plt.savefig('./png/%.6d.png' % tstep)
        plt.draw()
    '''

gtmp.get_event().wait()
Ejemplo n.º 18
0
# plot
import matplotlib.pyplot as plt
plt.ion()
fig = plt.figure(figsize=(12,8))
imag = plt.imshow(np.zeros((nx, ny), fields.dtype).T, interpolation='nearest', origin='lower', vmin=-1.1, vmax=1.1)
plt.colorbar()
'''

# main loop
from datetime import datetime
from time import time
t0 = datetime.now()
t00 = time()

gtmp = GetFields(fields, 'ez', (0, 0, 0), (0, 0, 0))
for tstep in xrange(1, tmax+1):
    fields.update_e()
    fields.update_h()

    '''
    if tstep % tgap == 0:
        print('[%s] %d/%d (%d %%)\r' % (datetime.now() - t0, tstep, tmax, float(tstep)/tmax*100)),
        sys.stdout.flush()

        getf.get_event().wait()
        imag.set_array( getf.get_fields().T )
        #plt.savefig('./png/%.6d.png' % tstep)
        plt.draw()
    '''
Ejemplo n.º 19
0
device = gpu_devices[0]

# z-axis
nx, ny, nz = 180, 160, 2
fields = Fields(context, device, nx, ny, nz)
Core(fields)
Pbc(fields, 'xyz')
IncidentDirect(fields, 'ey', (20, 0, 0), (20, ny-1, nz-1), tfunc) 
IncidentDirect(fields, 'ex', (0, 20, 0), (nx-1, 20, nz-1), tfunc) 

for tstep in xrange(1, tmax+1):
    fields.update_e()
    fields.update_h()

ax1 = fig.add_subplot(2, 3, 1)
getf = GetFields(fields, 'ey', (0, 0, nz/2), (nx-1, ny-1, nz/2))
getf.get_event().wait()
ax1.imshow(getf.get_fields().T, vmin=-1.1, vmax=1.1)
ax1.set_title('%s, ey[20,:,:]' % repr(fields.ns))
ax1.set_xlabel('x')
ax1.set_ylabel('y')

ax2 = fig.add_subplot(2, 3, 4)
getf = GetFields(fields, 'ex', (0, 0, nz/2), (nx-1, ny-1, nz/2))
getf.get_event().wait()
ax2.imshow(getf.get_fields().T, vmin=-1.1, vmax=1.1)
ax2.set_title('%s, ex[:,20,:]' % repr(fields.ns))
ax2.set_xlabel('x')
ax2.set_ylabel('y')

# y-axis 
Ejemplo n.º 20
0
import pyopencl as cl
from kemp.fdtd3d import common_gpu
from kemp.fdtd3d.gpu import Fields, GetFields, Npml

nx, ny, nz = 100, 110, 128
gpu_id = 0

gpu_devices = common_gpu.get_gpu_devices()
context = cl.Context(gpu_devices)
device = gpu_devices[gpu_id]
fdtd = Fields(context, device, nx, ny, nz, coeff_use='')

fhosts = {}
pml = Npml(fdtd, -0.428571428571, 0.714285714286, 0.6, 0.2, 0.6, 0.2)

fhost = np.random.rand(nx, nz).astype(fdtd.dtype)
cl.enqueue_write_buffer(fdtd.queue, pml.pex, fhost)

pml.update_h()
fget = GetFields(fdtd, ['hz', 'hx'], (0, ny - 1, 0), (nx - 1, ny - 1, nz - 1))
fget.get_event().wait()
hz = fget.get_fields('hz')
hx = fget.get_fields('hx')

print fhost
print hz
print fhost.shape
print hz.shape

assert np.linalg.norm(fhost - hz) == 0
Ejemplo n.º 21
0
import numpy as np
import pyopencl as cl

nx, ny, nz = 2, 640, 640
tmax, tgap = 1000, 10
gpu_id = 0

gpu_devices = common_gpu.get_gpu_devices()
context = cl.Context(gpu_devices)
device = gpu_devices[gpu_id]

fdtd = Fields(context, device, nx, ny, nz, coeff_use='')
src = DirectSrc(fdtd, 'ex', (1, ny / 5 * 4, nz / 5 * 3),
                (1, ny / 5 * 4, nz / 5 * 3), lambda tstep: np.sin(0.1 * tstep))
pbc = PbcInt(fdtd, 'x')
output = GetFields(fdtd, 'ex', (1, 0, 0), (1, ny - 1, nz - 1))

# Plot
import matplotlib.pyplot as plt
plt.ion()
imag = plt.imshow(output.get_fields().T,
                  cmap=plt.cm.hot,
                  origin='lower',
                  vmin=0,
                  vmax=0.05)
plt.colorbar()

# Main loop
from datetime import datetime
t0 = datetime.now()
Ejemplo n.º 22
0
import pyopencl as cl
from kemp.fdtd3d import common_gpu
from kemp.fdtd3d.gpu import Fields, GetFields, Npml

nx, ny, nz = 100, 110, 128
gpu_id = 0

gpu_devices = common_gpu.get_gpu_devices()
context = cl.Context(gpu_devices)
device = gpu_devices[gpu_id]
fdtd = Fields(context, device, nx, ny, nz, coeff_use='')

fhosts = {}
pml = Npml(fdtd, -0.428571428571, 0.714285714286, 0.6, 0.2, 0.6, 0.2)

fhost = np.random.rand(nx, nz).astype(fdtd.dtype)
cl.enqueue_write_buffer(fdtd.queue, pml.pex, fhost)

pml.update_h()
fget = GetFields(fdtd, ['hz','hx'], (0, ny-1, 0), (nx-1, ny-1, nz-1))
fget.get_event().wait()
hz = fget.get_fields('hz')
hx = fget.get_fields('hx')

print fhost
print hz
print fhost.shape
print hz.shape

assert np.linalg.norm(fhost - hz) == 0
Ejemplo n.º 23
0
import numpy as np
import pyopencl as cl

nx, ny, nz = 120, 320, 320
tmax, tgap = 300, 5
divide_axes = 'x'

gpu_devices = common_gpu.get_gpu_devices()
context = cl.Context(gpu_devices)
ngpu = len(gpu_devices)

fdtds = [
    Fields(context, device, nx, ny, nz, coeff_use='') for device in gpu_devices
]
outputs = [
    GetFields(fdtd, 'ez', (0, 0, nz / 2), (nx - 2, ny - 1, nz / 2))
    for fdtd in fdtds
]
src_e = DirectSrc(fdtds[1], 'ez', (nx / 3 * 2, ny / 2, 0),
                  (nx / 3 * 2, ny / 2, nz - 1),
                  lambda tstep: np.sin(0.1 * tstep))
exch = ExchangeInternal(fdtds, 'x')

# Plot
import matplotlib.pyplot as plt
plt.ion()
idxs = [0] + [i * nx - i for i in range(1, ngpu + 1)]  # [0, 239, 478, 717]
for idx in idxs[1:]:
    plt.plot((idx, idx), (0, ny), color='w', linewidth=0.2)

global_ez = np.ones((idxs[-1], ny), dtype=fdtds[0].dtype)
Ejemplo n.º 24
0
#nx, ny, nz = 240, 256, 256		# 540 MB
#nx, ny, nz = 512, 480, 480		# 3.96 GB
#nx, ny, nz = 480, 480, 480		# 3.71 GB
nx, ny, nz = 240, 640, 640
tmax, tgap = 200, 10
gpu_id = 0

gpu_devices = common_gpu.get_gpu_devices()
context = cl.Context(gpu_devices)
device = gpu_devices[gpu_id]

fdtd = Fields(context, device, nx, ny, nz, coeff_use='')
src = DirectSrc(fdtd, 'ez', (nx/5*4, ny/2, 0), (nx/5*4, ny/2, nz-1), lambda tstep: np.sin(0.1 * tstep))
pbc = PbcInt(fdtd, 'x')
output = GetFields(fdtd, 'ez', (0, 0, nz/2), (nx-1, ny-1, nz/2))


# Plot
import matplotlib.pyplot as plt
plt.ion()
imag = plt.imshow(output.get_fields('ez').T, cmap=plt.cm.hot, origin='lower', vmin=0, vmax=0.05)
plt.colorbar()


# Main loop
from datetime import datetime
t0 = datetime.now()

for tstep in xrange(1, tmax+1):
	fdtd.update_e()
Ejemplo n.º 25
0
elif rank == size - 1: direction = '-'
else: direction = '+-'

#exch = node.ExchangeMpiNonBlock(fields, direction)

#exch = node.ExchangeMpiBufferBlock(fields, direction)
#exch = node.ExchangeMpiBufferBlockSplit(fields, direction)
exch = node.ExchangeMpiBufferNonBlockSplitEnqueue(fields, direction, tmax)
if '+' in direction: cpu.Core(exch.cpuf_p)
if '-' in direction: cpu.Core(exch.cpuf_m)

is_master = True if rank == 0 else False

if is_plot:
    Pbc(fields, 'yz')
    getf = GetFields(fields, 'ez', (0, 0, 0.5), (-1, -1, 0.5))

    tfunc = lambda tstep: 40 * np.sin(0.05 * tstep)
    if rank < size - 1:
        IncidentDirect(fields, 'ez', (220, 0.5, 0), (220, 0.5, -1), tfunc)
    #if rank > 0:
    #    IncidentDirect(fields, 'ez', (20, 0.5, 0), (20, 0.5, -1), tfunc)

    if is_master:
        # plot
        import matplotlib.pyplot as plt
        plt.ion()
        fig = plt.figure(figsize=(12, 8))
        arr = np.zeros((size * nx, ny), fields.dtype)
        for i in range(1, size):
            plt.plot((i * nx, i * nx), (0, ny), color='k', linewidth=1.2)
Ejemplo n.º 26
0
from kemp.fdtd3d.gpu import Fields, DirectSrc, GetFields, ExchangeFields
import numpy as np
import pyopencl as cl

nx, ny, nz = 240, 640, 640
tmax, tgap = 200, 10
divide_axes = 'x'

gpu_devices = common_gpu.get_gpu_devices()
context = cl.Context(gpu_devices)
ngpu = len(gpu_devices)

fdtds = [
    Fields(context, device, nx, ny, nz, coeff_use='') for device in gpu_devices
]
outputs = [GetFields(fdtds[0], 'ez', (0, 0, nz / 2), (nx - 1, ny - 1, nz / 2))]
outputs += [
    GetFields(fdtd, 'ez', (1, 0, nz / 2), (nx - 1, ny - 1, nz / 2))
    for fdtd in fdtds[1:]
]
src = DirectSrc(fdtds[1], 'ez', (nx / 5 * 4, ny / 2, 0),
                (nx / 5 * 4, ny / 2, nz - 1),
                lambda tstep: np.sin(0.1 * tstep))
exch = ExchangeFields(fdtds, 'x')

# Plot
import matplotlib.pyplot as plt

plt.ion()
global_ez = np.ones((ngpu * (nx - 1) + 1, ny), dtype=fdtds[0].dtype)
imag = plt.imshow(global_ez.T,
Ejemplo n.º 27
0
nx, ny, nz = 2, 250, 300
tmax, tgap = 1000, 10
npml = 10

# instances
gpu_devices = common_gpu.gpu_device_list(print_info=False)
context = cl.Context(gpu_devices)
device = gpu_devices[0]
fields = Fields(context, device, nx, ny, nz)
Pbc(fields, 'x')
Pml(fields, ('', '+-', '+-'), npml)
Core(fields)

tfunc = lambda tstep: 50 * np.sin(0.05 * tstep)
IncidentDirect(fields, 'ex', (0, 0.4, 0.3), (-1, 0.4, 0.3), tfunc)
getf = GetFields(fields, 'ex', (0.5, 0, 0), (0.5, -1, -1))

print fields.instance_list

# plot
import matplotlib.pyplot as plt
plt.ion()
fig = plt.figure(figsize=(12, 8))
imag = plt.imshow(np.zeros((ny, nz), fields.dtype).T,
                  interpolation='nearest',
                  origin='lower',
                  vmin=-1.1,
                  vmax=1.1)
plt.xlabel('y')
plt.ylabel('z')
plt.colorbar()
Ejemplo n.º 28
0
nx, ny, nz = 160, 140, 32
tmax, tgap = 150, 10 

# instances 
gpu_devices = common_gpu.gpu_device_list(print_info=False)
context = cl.Context(gpu_devices)
device = gpu_devices[0]
fields = Fields(context, device, nx, ny, nz)
Core(fields)
Pbc(fields, 'xyz')

tfunc = lambda tstep: np.sin(0.05 * tstep)
IncidentDirect(fields, 'ez', (20, 0, 0), (20, ny-1, nz-1), tfunc) 
#IncidentDirect(fields, 'ez', (0, 20, 0), (nx-1, 20, nz-1), tfunc) 
getf = GetFields(fields, 'ez', (0, 0, nz/2), (nx-1, ny-1, nz/2))

print fields.instance_list

# plot
import matplotlib.pyplot as plt
plt.ion()
fig = plt.figure(figsize=(12,8))
imag = plt.imshow(np.zeros((nx, ny), fields.dtype).T, interpolation='nearest', origin='lower', vmin=-1.1, vmax=1.1)
plt.colorbar()

# main loop
from datetime import datetime
t0 = datetime.now()

for tstep in xrange(1, tmax+1):
Ejemplo n.º 29
0
nx, ny, nz = 240, 256, 256  # 540 MB
#nx, ny, nz = 512, 480, 480		# 3.96 GB
#nx, ny, nz = 480, 480, 480		# 3.71 GB
tmax, tgap = 200, 10
gpu_id = 0

gpu_devices = common_gpu.get_gpu_devices()
context = cl.Context(gpu_devices)
device = gpu_devices[gpu_id]

fdtd = Fdtd(context, device, nx, ny, nz, coeff_use='')
src = DirectSrc(fdtd, 'ez', (nx / 3 * 2, ny / 2, 0),
                (nx / 3 * 2, ny / 2, nz - 1),
                lambda tstep: np.sin(0.1 * tstep))
output = GetFields(fdtd, 'ez', (0, 0, nz / 2), (nx - 1, ny - 1, nz / 2))

# Plot
import matplotlib.pyplot as plt
plt.ion()
imag = plt.imshow(output.get_fields('ez').T,
                  cmap=plt.cm.hot,
                  origin='lower',
                  vmin=0,
                  vmax=0.05)
plt.colorbar()

# Main loop
from datetime import datetime
t0 = datetime.now()
Ejemplo n.º 30
0
import pyopencl as cl


nx, ny, nz = 2, 640, 640
tmax, tgap = 1000, 10
gpu_id = 0

gpu_devices = common_gpu.get_gpu_devices()
context = cl.Context(gpu_devices)
device = gpu_devices[gpu_id]

fdtd = Fields(context, device, nx, ny, nz, coeff_use='')
src_e = DirectSrc(fdtd, 'ex', (1, ny/5*4, nz/5*1), (1, ny/5*4, nz/5*1), lambda tstep: np.sin(0.1 * tstep))
pbc = PbcInt(fdtd, 'x')
pml = Npml(fdtd, -0.428571428571, 0.714285714286, 0.6, 0.2, 0.6, 0.2)
output = GetFields(fdtd, 'ex', (1, 0, 0), (1, ny-1, nz-1))


# Plot
import matplotlib.pyplot as plt
plt.ion()
imag = plt.imshow(output.get_fields().T, cmap=plt.cm.hot, origin='lower', vmin=0, vmax=0.01)
plt.colorbar()


# Main loop
from datetime import datetime
t0 = datetime.now()

for tstep in xrange(1, tmax+1):
	fdtd.update_e()
Ejemplo n.º 31
0
import pyopencl as cl


nx, ny, nz = 240, 256, 256		# 540 MB
#nx, ny, nz = 512, 480, 480		# 3.96 GB
#nx, ny, nz = 480, 480, 480		# 3.71 GB
tmax, tgap = 200, 10
gpu_id = 0

gpu_devices = common_gpu.get_gpu_devices()
context = cl.Context(gpu_devices)
device = gpu_devices[gpu_id]

fdtd = Fdtd(context, device, nx, ny, nz, coeff_use='')
src = DirectSrc(fdtd, 'ez', (nx/3*2, ny/2, 0), (nx/3*2, ny/2, nz-1), lambda tstep: np.sin(0.1 * tstep))
output = GetFields(fdtd, 'ez', (0, 0, nz/2), (nx-1, ny-1, nz/2))


# Plot
import matplotlib.pyplot as plt
plt.ion()
imag = plt.imshow(output.get_fields('ez').T, cmap=plt.cm.hot, origin='lower', vmin=0, vmax=0.05)
plt.colorbar()


# Main loop
from datetime import datetime
t0 = datetime.now()

for tstep in xrange(1, tmax+1):
	fdtd.update_h()
Ejemplo n.º 32
0
nx, ny, nz = 160, 140, 32
tmax, tgap = 150, 10 

# instances 
gpu_devices = common_gpu.gpu_device_list(print_info=False)
context = cl.Context(gpu_devices)
device = gpu_devices[0]
qtask = QueueTask()
fields = Fields(context, device, qtask, nx, ny, nz)
Core(fields)
Pbc(fields, 'xyz')

tfunc = lambda tstep: np.sin(0.05 * tstep)
IncidentDirect(fields, 'ez', (20, 0, 0), (20, ny-1, nz-1), tfunc) 
#IncidentDirect(fields, 'ez', (0, 20, 0), (nx-1, 20, nz-1), tfunc) 
getf = GetFields(fields, 'ez', (0, 0, nz/2), (nx-1, ny-1, nz/2))

print fields.instance_list

# plot
import matplotlib.pyplot as plt
plt.ion()
fig = plt.figure(figsize=(12,8))
imag = plt.imshow(np.zeros((nx, ny), fields.dtype).T, interpolation='nearest', origin='lower', vmin=-1.1, vmax=1.1)
plt.colorbar()

# main loop
from datetime import datetime
t0 = datetime.now()

for tstep in xrange(1, tmax+1):
Ejemplo n.º 33
0
device = gpu_devices[0]

# z-axis
nx, ny, nz = 180, 160, 2
fields = Fields(context, device, nx, ny, nz)
Pbc(fields, 'xyz')
Core(fields)
IncidentDirect(fields, 'ey', (20, 0, 0), (20, ny - 1, nz - 1), tfunc)
IncidentDirect(fields, 'ex', (0, 20, 0), (nx - 1, 20, nz - 1), tfunc)

for tstep in xrange(1, tmax + 1):
    fields.update_e()
    fields.update_h()

ax1 = fig.add_subplot(2, 3, 1)
getf = GetFields(fields, 'ey', (0, 0, nz / 2), (nx - 1, ny - 1, nz / 2))
getf.get_event().wait()
ax1.imshow(getf.get_fields().T, vmin=-1.1, vmax=1.1)
ax1.set_title('%s, ey[20,:,:]' % repr(fields.ns))
ax1.set_xlabel('x')
ax1.set_ylabel('y')

ax2 = fig.add_subplot(2, 3, 4)
getf = GetFields(fields, 'ex', (0, 0, nz / 2), (nx - 1, ny - 1, nz / 2))
getf.get_event().wait()
ax2.imshow(getf.get_fields().T, vmin=-1.1, vmax=1.1)
ax2.set_title('%s, ex[:,20,:]' % repr(fields.ns))
ax2.set_xlabel('x')
ax2.set_ylabel('y')

# y-axis
Ejemplo n.º 34
0
ax2 = fig.add_subplot(1, 3, 2)
ax3 = fig.add_subplot(1, 3, 3)

# gpu device
gpu_devices = common_gpu.gpu_device_list(print_info=False)
context = cl.Context(gpu_devices)
device = gpu_devices[0]

# xy-plane
nx, ny, nz = 180, 160, 2
fields = Fields(context, device, nx, ny, nz)
Pbc(fields, 'z')
Pml(fields, ('+-', '+-', ''), npml)
Core(fields)
IncidentDirect(fields, 'ez', (0.4, 0.3, 0), (0.4, 0.3, -1), tfunc) 
getf = GetFields(fields, 'ez', (0, 0, 0.5), (-1, -1, 0.5))

for tstep in xrange(1, tmax+1):
    fields.update_e()
    fields.update_h()

getf.get_event().wait()
ax1.imshow(getf.get_fields().T, vmin=-1.1, vmax=1.1)
ax1.set_title('xy-plane')
ax1.set_xlabel('x')
ax1.set_ylabel('y')

# yz-plane
nx, ny, nz = 2, 180, 160
fields = Fields(context, device, nx, ny, nz)
Pbc(fields, 'x')
Ejemplo n.º 35
0
elif rank == size - 1: direction = '-'
else: direction = '+-'

#exch = node.ExchangeMpiNonBlock(fields, direction)

#exch = node.ExchangeMpiBufferBlock(fields, direction)
#exch = node.ExchangeMpiBufferBlockSplit(fields, direction)
exch = node.ExchangeMpiBufferNonBlockSplitEnqueue(fields, direction, tmax)
if '+' in direction: cpu.Core(exch.cpuf_p)
if '-' in direction: cpu.Core(exch.cpuf_m)

is_master = True if rank == 0 else False

if is_plot:
    Pbc(fields, 'yz')
    getf = GetFields(fields, 'ez', (0, 0, 0.5), (-1, -1, 0.5))

    tfunc = lambda tstep: 40 * np.sin(0.05 * tstep)
    if rank < size - 1:
        IncidentDirect(fields, 'ez', (220, 0.5, 0), (220, 0.5, -1), tfunc) 
    #if rank > 0:
    #    IncidentDirect(fields, 'ez', (20, 0.5, 0), (20, 0.5, -1), tfunc) 

    if is_master:
        # plot
        import matplotlib.pyplot as plt
        plt.ion()
        fig = plt.figure(figsize=(12,8))
        arr = np.zeros((size * nx, ny), fields.dtype)
        for i in range(1, size):
            plt.plot((i*nx, i*nx), (0, ny), color='k', linewidth=1.2)
Ejemplo n.º 36
0
    def runTest(self):
        axis, nx, ny, nz, precision_float = self.args

        gpu_devices = common_gpu.gpu_device_list(print_info=False)
        context = cl.Context(gpu_devices)
        device = gpu_devices[0]
        fields = Fields(context, device, nx, ny, nz, '', precision_float)
        pbc = Pbc(fields, axis)

        # allocations
        ehs = common_update.generate_random_ehs(nx, ny, nz, fields.dtype)
        fields.set_eh_bufs(*ehs)

        # update
        fields.update_e()
        fields.update_h()

        # verify
        getf0, getf1 = {}, {}
        strfs_e = {
            'x': ['ey', 'ez'],
            'y': ['ex', 'ez'],
            'z': ['ex', 'ey']
        }[axis]
        strfs_h = {
            'x': ['hy', 'hz'],
            'y': ['hx', 'hz'],
            'z': ['hx', 'hy']
        }[axis]

        pt0 = (0, 0, 0)
        pt1 = { 'x': (0, ny-2, nz-2), \
                'y': (nx-2, 0, nz-2), \
                'z': (nx-2, ny-2, 0) }[axis]
        getf0['e'] = GetFields(fields, strfs_e, pt0, pt1)

        pt0 = { 'x': (nx-1, 0, 0), \
                'y': (0, ny-1, 0), \
                'z': (0, 0, nz-1) }[axis]
        pt1 = { 'x': (nx-1, ny-2, nz-2), \
                'y': (nx-2, ny-1, nz-2), \
                'z': (nx-2, ny-2, nz-1) }[axis]
        getf1['e'] = GetFields(fields, strfs_e, pt0, pt1)

        pt0 = { 'x': (0, 1, 1), \
                'y': (1, 0, 1), \
                'z': (1, 1, 0) }[axis]
        pt1 = { 'x': (0, ny-1, nz-1), \
                'y': (nx-1, 0, nz-1), \
                'z': (nx-1, ny-1, 0) }[axis]
        getf0['h'] = GetFields(fields, strfs_h, pt0, pt1)

        pt0 = { 'x': (nx-1, 1, 1), \
                'y': (1, ny-1, 1), \
                'z': (1, 1, nz-1) }[axis]
        pt1 = (nx - 1, ny - 1, nz - 1)
        getf1['h'] = GetFields(fields, strfs_h, pt0, pt1)

        for getf in getf0.values() + getf1.values():
            getf.get_event().wait()

        for eh in ['e', 'h']:
            norm = np.linalg.norm( \
                    getf0[eh].get_fields() - getf1[eh].get_fields() )
            self.assertEqual(norm, 0, '%g, %s, %s' % (norm, self.args, eh))