def run_mp(json_path, pyd_path, verbose=False, n_core=4): start_time = time.time() config = readjson(json_path=json_path) transducer_config = config['transducer'] coor_file_path = "data/transducer_position.txt" textarray = np.loadtxt(coor_file_path) coordinates = textarray[::, 1:] T = Transducer(element_coordinates=coordinates, **transducer_config) mc = MediaComplex(config_json=config) init_medium_config = config['init_medium'] init_medium = InitMedium.new_markoil(init_medium_config['boundary']) T.initialize( init_medium, **transducer_config["element_init_paras"], verbose=verbose, n_core=n_core) end_time = time.time() if verbose: print("--- initialization:", end_time - start_time, "seconds ---") # start sampling start_time = end_time box_config = config['box'] B = Box(*box_config['min'], *box_config['max'], box_config['step']) # parallelization, give transducer casting and measuring job to process pool pool = Pool(n_core) # process worker pool, 10 CPU cores async_results = [] for te in T: ar = pool.apply_async(measure_kernel, args=(te, B, mc, verbose)) async_results.append(ar) pool.close() pool.join() if verbose: print("--- all process finished ---") complex_pressure = np.zeros(B.nxyz, dtype=np.complex128) for r in async_results: complex_pressure += r.get() end_time = time.time() if verbose: print("--- casting time:", end_time - start_time, "seconds ---") # save ndarray to file if pyd_path is None: pyd_path = 'pressure' + '_l' + str(B.lx) + "_n" + str(len( T[0])) + "_t" + str(int(time.time())) real_pressure = np.abs(complex_pressure) np.save('npydata/' + pyd_path, real_pressure) np.save('npydata/' + 'c' + pyd_path, complex_pressure) if verbose: # TODO plot surface plot_sliced_tensor(real_pressure, slicing_axis=2)
def run_hifu(config_path, pyd_path, verbose=False, n_core=4): start_time = time.time() config = readjson(json_path=config_path) transducer_config = config['transducer'] coor_file_path = "data/transducer_position.txt" textarray = np.loadtxt(coor_file_path) coordinates = textarray[::, 1:] # coordinates = coordinates[9] # coordinates = np.reshape(coordinates, (1, 3)) T = Transducer(element_coordinates=coordinates, **transducer_config) mc = MediaComplex(medium_list=config["medium_list"]) box_config = config['box'] B = Box(*box_config['min'], *box_config['max'], box_config['step']) esp = transducer_config["element_init_paras"] myhifu = HIFU(T) cp = myhifu.run(mc, B, esp['n_rays'], esp['trident_angle'], esp['theta_max'], n_core=n_core) end_time = time.time() print(f'use {end_time - start_time} seconds') np.save(f'npydata/complex_pressure_{start_time}', cp) plot_sliced_tensor(np.abs(cp), slicing_axis=2)
def run(json_path, pyd_path, verbose=False): start_time = time.time() config = readjson(json_path=json_path) transducer_config = config['transducer'] coor_file_path = "data/transducer_position.txt" textarray = np.loadtxt(coor_file_path) coordinates = textarray[::, 1:] T = Transducer(element_coordinates=coordinates, **transducer_config) mc = MediaComplex(config_json=config) init_medium_config = config['init_medium'] init_medium = InitMedium.new_markoil(init_medium_config['boundary']) T.initialize( init_medium, **transducer_config["element_init_paras"], verbose=verbose) end_time = time.time() if verbose: print("--- initialization:", end_time - start_time, "seconds ---") start_time = end_time bundle_dict = T.cast(mc) end_time = time.time() if verbose: print("--- casting time:", end_time - start_time, "seconds ---") # start sampling start_time = end_time box_config = config['box'] B = Box(*box_config['min'], *box_config['max'], box_config['step']) # print(len(B.lattrix)) complex_pressure = measure(B, bundle_dict, verbose=verbose) end_time = time.time() if verbose: print("--- sampling time:", end_time - start_time, "seconds ---") if pyd_path is None: pyd_path = 'pressure' + '_l' + str(B.lx) + "_n" + str(len( T[0])) + "_t" + str(int(time.time())) real_pressure = np.abs(complex_pressure) np.save('npydata/' + pyd_path, real_pressure) np.save('npydata/' + 'c' + pyd_path, complex_pressure) if verbose: # TODO plot surface plot_sliced_tensor(real_pressure, slicing_axis=2)
def run_hifu(config_path, pyd_path, verbose=False, n_core=4): start_time = time.time() config = readjson(json_path=config_path) logging.basicConfig(filename="run_hifu"+str(start_time)+".log", filemode="w", level=logging.INFO) transducer_config = config['transducer'] coor_file_path = "data/transducer_position.txt" textarray = np.loadtxt(coor_file_path) coordinates = textarray[::, 1:] # coordinates = coordinates[36] # coordinates = np.reshape(coordinates, (1, 3)) T = Transducer(element_coordinates=coordinates, **transducer_config) mc = MediaComplex(medium_list=config["medium_list"]) box_config = config['box'] B = Box(*box_config['min'], *box_config['max'], box_config['step']) fig = plt.figure() ax = fig.add_subplot(111, projection='3d') plot_transducer(T, ax) plot_box(B, ax) plt.show() return esp = transducer_config["element_init_paras"] myhifu = HIFU(T) cp = myhifu.run( mc, B, esp['n_rays'], esp['trident_angle'], esp['theta_max'], n_core=n_core) end_time = time.time() print(f'use {end_time - start_time} seconds') np.save(f'npydata/complex_pressure_{start_time}', cp) plot_sliced_tensor(np.abs(cp), slicing_axis=1)
def __init__(self, medium_list=None, config_file_path=None): super().__init__() if medium_list is None: medium_list = readjson(json_path=config_file_path)["medium_list"] for item in medium_list: if 'is_init' in item and item['is_init']: for m in self: m.id += 1 self.insert(0, Medium(**item, med_id=0)) else: n = len(self) self.append(Medium(**item, med_id=n)) # adj_mtx[i,j] = [1,2] : 1,2 are the indices of the faces of self[i] # adjacent to self[j] self.adj_mtx = np.ones((len(self), len(self))) * -1 # traverse all media (double compare loop) for i, item1 in enumerate(self): for j, item2 in enumerate(self): if i != j: self.adj_mtx[i, j] = item1.shape.adj_at(item2.shape)
import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D from pyHIFU.transducer import Transducer, TElement from pyHIFU.io.config import readjson from pyHIFU.physics.medium import InitMedium config = readjson(json_path='HIFU.py/data/test_case1.json') transducer_config = config['transducer'] coor_file_path = "HIFU.py/data/transducer_position.txt" textarray = np.loadtxt(coor_file_path) coordinates = textarray[::, 1:] init_medium_config = config['init_medium'] init_medium = InitMedium.new_markoil(init_medium_config['boundary']) te = TElement(0, [0, 0, 0], 3.5e-3, 40, 1.2e6, np.array([0.14, 0, 0])) te.initialize(init_medium, 0, n=3000, trident_angle=5e-2, theta_max=np.pi / 20) from pyHIFU.visualization.mkplots import plot_TElements from pyHIFU.visualization.figure import create_ax fig = plt.figure() ax = create_ax(fig) plot_TElements(te, ax) plt.show()
import matplotlib.pyplot as plt import numpy as np from pyHIFU.io.config import readjson from matplotlib.ticker import FormatStrFormatter n_rays_data = readjson(json_path="data/d_v_n_rays.json") theta_max_data = readjson(json_path="data/d_v_theta_max.json") trident_angle_data = readjson(json_path="data/d_v_trident_angle.json") n_rays_l = list(n_rays_data.keys()) n_rays_l = np.array(n_rays_l, dtype=np.float64) n_rays_v = n_rays_data.values() theta_max_l = list(theta_max_data.keys()) theta_max_l = np.array(theta_max_l, dtype=np.float64) theta_max_v = theta_max_data.values() trident_angle_l = list(trident_angle_data.keys()) trident_angle_l = np.array(trident_angle_l, dtype=np.float64) trident_angle_v = trident_angle_data.values() fig, ax = plt.subplots() # ax.yaxis.set_major_formatter(FormatStrFormatter('%.2f')) # ax.plot(n_rays_l, n_rays_v) # ax.title.set_text("number of rays") # ax.set_xlabel("n_rays") # ax.set_ylabel("pnorm distance") # ax.yaxis.set_major_formatter(FormatStrFormatter('%.2f')) # ax.plot(theta_max_l, theta_max_v)
import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D from pyHIFU.transducer import Transducer, TElement from pyHIFU.io.config import readjson from pyHIFU.physics.medium import Medium config = readjson(json_path='../data/case1.json') transducer_config = config['transducer'] coor_file_path = "../data/transducer_position.txt" textarray = np.loadtxt(coor_file_path) coordinates = textarray[::, 1:] init_medium_config = config['medium_list'][0] init_medium = Medium(**init_medium_config) te = TElement(0, [0,0,0], 3.5e-3, 40, 1.2e6, np.array([0.14, 0,0])) te.initialize(init_medium, 0, n=1, trident_angle=5e-2, theta_max=np.pi/20) delta = 0.001 x = np.arange(0.02, 0.15, delta) y = np.arange(-0.03, 0.03, delta) X, Y = np.meshgrid(x, y) z = np.zeros(X.shape, dtype=np.complex128) for i in range(len(x)): for j in range(len(y)): coor = [x[i], y[j], 0] pc = te.ffa(coor)