os.chdir('../../test') call(['mv', '../prob/' + PROBLEM + '/' + TMP_DIR, './']) # Since this test is designed to run on a single machine (no batch scripts) # set openmpi to only use a few threads. Let MPI handle the rest. if MPI: import multiprocessing num_mpi = 8 num_cpus = multiprocessing.cpu_count() os.environ['OMP_NUM_THREADS'] = str(int(np.max([2, num_cpus / num_mpi]))) # RUN EXECUTABLE os.chdir(TMP_DIR) args = ['./bhlight', '-p', 'param_template.dat'] if MPI: bcall(args, str(num_mpi)) else: bcall(args) os.chdir('../') # READ SIMULATION OUTPUT dfiles = np.sort(glob.glob(os.path.join(TMP_DIR, '') + '/dumps/dump*.h5')) Nd = len(dfiles) hdr = io.load_hdr(dfiles[0]) geom = io.load_geom(hdr) dump = io.load_dump(dfiles[-1], geom) nuLnu = dump['nuLnu'] if np.any(np.abs(nuLnu[1:, :, :, :]) >= 1e-15): nuLnu = np.sum(nuLnu, axis=0) else:
'../../test/' + TMP_DIR + '/param.dat') util.safe_remove(TMP_BUILD) util.safe_remove(TMP_DIR) os.chdir('../../test/') NVAR = 8 L1 = np.zeros(len(RES)) os.chdir(TMP_DIR) # RUN PROBLEM FOR EACH RESOLUTION AND ANALYZE RESULT for m in xrange(len(RES)): call_string = ['./bhlight_' + str(RES[m]), '-p', 'param.dat'] print(call_string) bcall(call_string) dfiles = np.sort(glob.glob('dumps/dump*.h5')) hdr = io.load_hdr(dfiles[0]) geom = io.load_geom(hdr) dump0 = io.load_dump(dfiles[0], geom) dump1 = io.load_dump(dfiles[-1], geom) r = geom['r'][:, 0, 0] imin = 0 while r[imin] < hdr['Reh']: imin += 1 rho0 = np.mean(dump0['RHO'][imin:, :, 0], axis=1) rho1 = np.mean(dump1['RHO'][imin:, :, 0], axis=1) L1[m] = np.mean(np.fabs(rho1 - rho0))
if MODES[n] == 2: # ALFVEN dvar[4] = 0.480384461415 dvar[7] = 0.877058019307 if MODES[n] == 3: # FAST dvar[0] = 0.476395427447 dvar[1] = 0.635193903263 dvar[2] = -0.102965815319 dvar[3] = -0.316873207561 dvar[5] = 0.359559114174 dvar[6] = -0.359559114174 dvar *= amp # RUN PROBLEM FOR EACH RESOLUTION AND ANALYZE RESULT for m in range(len(RES)): print(['./bhlight_' + str(RES[m]), '-p', 'param_template.dat']) bcall(['./bhlight_' + str(RES[m]), '-p', 'param_template.dat']) dfiles = np.sort(glob.glob('dumps/dump*.h5')) hdr = io.load_hdr(dfiles[-1]) geom = io.load_geom(hdr, recalc=True) dump = io.load_dump(dfiles[-1], geom) X1 = geom['x'][:, :, 0] X2 = geom['y'][:, :, 0] dvar_code = [] dvar_code.append(dump['RHO'][:, :, 0] - var0[0]) dvar_code.append(dump['UU'][:, :, 0] - var0[1]) dvar_code.append(dump['U1'][:, :, 0] - var0[2]) dvar_code.append(dump['U2'][:, :, 0] - var0[3]) dvar_code.append(dump['U3'][:, :, 0] - var0[4]) dvar_code.append(dump['B1'][:, :, 0] - var0[5]) dvar_code.append(dump['B2'][:, :, 0] - var0[6])
if MPI: compile_args += ['-mpi'] call(compile_args) os.chdir('../../test') call(['mv', '../prob/' + PROBLEM + '/' + TMP_DIR, './']) # RUN EXECUTABLE os.chdir(TMP_DIR) run_args = ['./bhlight', '-p', 'param_template.dat'] if MPI: NPROC = 4 import psutil NCORE = psutil.cpu_count(logical=False) NTHREADS = (max(NCORE / NPROC, 1)) os.environ['OMP_NUM_THREADS'] = '%d' % NTHREADS bcall(run_args, int(NPROC), stdin=DEVNULL) else: bcall(run_args) os.chdir('../') # load simulation output dumps = sorted(glob.glob(os.path.join(TMP_DIR, '') + '/dumps/dump*.h5')) hdr = io.load_hdr(dumps[0]) geom = io.load_geom(hdr) N1, N2, N3 = [hdr[i] for i in ['N1', 'N2', 'N3']] times = np.empty(len(dumps)) Yes = np.empty((len(dumps), N1, N2)) dump = io.load_dump(dumps[0]) x, y = geom['xf'][:, :, 0], geom['yf'][:, :, 0]
if AUTO: raise ValueError("No auto for this test") os.chdir('../prob/' + PROBLEM) # COMPILE CODE call([ 'python', 'build.py', '-dir', TMP_DIR, '-classic', '-nob', '-norenorm', '-tracertest' ]) os.chdir('../../test/') call(['mv', '../prob/' + PROBLEM + '/' + TMP_DIR, './']) # RUN EXECUTABLE os.chdir(TMP_DIR) bcall(['./bhlight', '-p', 'param_template.dat'], int(num_mpi)) os.chdir('../') # READ DATA tracers = io.TracerData.fromdir(os.path.join(TMP_DIR, 'dumps', 'tracers')) t0 = tracers.filter(tracers['time'] == 0.0) rcyl0 = np.sqrt(t0['Xcart'][:, 0]**2 + t0['Xcart'][:, 1]**2) print("Making movie") fig, ax = plt.subplots() ax.set_xlim(0, 40) ax.set_ylim(-17, 17) ax.set_xlabel(r'$r_{cyl}$') ax.set_ylabel(r'$z$') dots, = ax.plot(rcyl0, t0['Xcart'][:, 2], 'bo', ms=1)
'../../test/' + TMP_DIR + '/param.dat') util.safe_remove(TMP_BUILD) util.safe_remove(TMP_DIR) os.chdir('../../test/') os.chdir(TMP_DIR) def phi1_true(t, x): return amp * np.sin(2 * np.pi * (x - kcenter - cadv * t)) # and convergence plot print("Convergence test...") errs = [None for res in RES] for n, res in enumerate(RES): bcall(['./bhlight_' + str(res), '-p', 'param.dat']) dfiles = sorted(glob.glob('dumps/dump*.h5')) hdr = io.load_hdr(dfiles[-1]) geom = io.load_geom(hdr, recalc=True) dump = io.load_dump(dfiles[-1], geom) N = hdr['N{}'.format(IDIM)] t = dump['t'] x = geom[['x', 'y', 'z'][IDIM - 1]].reshape(N) phi1 = dump['var1'].reshape(N) error = phi1 - phi1_true(t, x) max_error = np.max(np.abs(error)) errs[n] = max_error print("Richardson extrapolating...") errf = lambda h, alpha, p: alpha * (h**p) p0 = 2.0
dvar[0] = 0.481846076323 dvar[1] = 0.642461435098 dvar[2] = -0.0832240462505 dvar[3] = -0.224080007379 dvar[4] = -0.224080007379 dvar[5] = 0.406380545676 dvar[6] = -0.203190272838 dvar[7] = -0.203190272838 dvar *= amp # RUN PROBLEM FOR EACH RESOLUTION AND ANALYZE RESULT for m in range(len(RES)): print("Res = {}".format(RES[m])) args = ['./bhlight_' + str(RES[m]), '-p', 'param.dat'] if MPI: bcall(args, int(num_mpi)) else: call(args) dfiles = np.sort(glob.glob('dumps/dump*.h5')) hdr = io.load_hdr(dfiles[-1]) geom = io.load_geom(hdr, recalc=True) dump = io.load_dump(dfiles[-1], geom) X1 = geom['x'][:, :, :] X2 = geom['y'][:, :, :] X3 = geom['z'][:, :, :] dvar_code = [] dvar_code.append(dump['RHO'][:, :, :] - var0[0]) dvar_code.append(dump['UU'][:, :, :] - var0[1]) dvar_code.append(dump['U1'][:, :, :] - var0[2]) dvar_code.append(dump['U2'][:, :, :] - var0[3])
if FORCE: args.append('-force') if RELTABLE: args.append('-reltable') tscale = 1.0 call(args) if TABLE: call([ 'mv', 'sc_eos_gamma_{}.h5'.format(str(gam).replace('.', 'p')), TMP_DIR ]) os.chdir('../../test/') call(['mv', '../prob/' + PROBLEM + '/' + TMP_DIR, './']) # RUN EXECUTABLE os.chdir(TMP_DIR) bcall(['./bhlight', '-p', 'param_template.dat']) os.chdir('../') # READ SIMULATION OUTPUT dfiles = np.sort(glob.glob(os.path.join(TMP_DIR, '') + '/dumps/dump*.h5')) hdr = io.load_hdr(dfiles[0]) geom = io.load_geom(hdr) dump = io.load_dump(dfiles[-1], geom) x_code = geom['x'][:, 0, 0] rho_code = dump['RHO'][:, 0, 0] P_code = dump['PRESS'][:, 0, 0] / (tscale * tscale) u_code = dump['U1'][:, 0, 0] / (tscale) eps_code = (dump['UU'][:, 0, 0] / dump['RHO'][:, 0, 0]) / (tscale * tscale) if RELTABLE: ye_code = dump['Ye'][:, 0, 0]
call([sys.executable, 'build_mpi.py', '-dir', TMP_DIR]) os.chdir('../../test') call(['mv', '../prob/' + 'thermalization' + '/' + TMP_DIR, './']) # RUN EXECUTABLE NPROC = 8 import psutil NCORE = psutil.cpu_count(logical=False) NTHREADS = (max(NCORE / NPROC, 1)) os.environ['OMP_NUM_THREADS'] = '%d' % NTHREADS os.chdir(TMP_DIR) if FAST: util.change_rparm('tf', str(TF), 'param_template.dat') # stdin=DEVNULL is required for MPI tests run with test_all.py bcall(['./bhlight', '-p', 'param_template.dat'], int(NPROC), stdin=DEVNULL) os.chdir('../') # READ SIMULATION OUTPUT dfiles = np.sort(glob.glob(os.path.join(TMP_DIR, '') + '/dumps/dump*.h5')) Nd = len(dfiles) hdr = io.load_hdr(dfiles[0]) geom = io.load_geom(hdr) t_code = np.zeros(Nd) Te_code = np.zeros(Nd) Tr_code = np.zeros(Nd) #Tr2_code = np.zeros(Nd) Etot = np.zeros(Nd) for n in range(Nd): dump = io.load_dump(dfiles[n], geom) t_code[n] = dump['t'] * hdr['T_unit']
util.change_rparm('tf', 0.1, parm_dest) util.change_rparm('DTd', 0.05, parm_dest) util.change_rparm('DTl', 0.05, parm_dest) util.safe_remove(TMP_BUILD) util.safe_remove(TMP_DIR) os.chdir('../../test/') os.chdir(TMP_DIR) # and convergence plot print("Convergence test...") errs = [None for res in RES] for n, res in enumerate(RES): print("Res = {}".format(res)) call_string = ['./bhlight_' + str(res), '-p', 'param.dat'] if MPI: bcall(call_string, int(num_mpi)) else: bcall(call_string) dfiles = sorted(glob.glob('dumps/dump*.h5')) hdr = io.load_hdr(dfiles[-1]) geom = io.load_geom(hdr, recalc=True) dump = io.load_dump(dfiles[-1], geom) N1, N2, N3 = [hdr['N{}'.format(d)] for d in range(1, 4)] mshape = (N1, N2, N3) t = dump['t'] x, y, z = [geom[d].reshape(mshape) for d in ['x', 'y', 'z']] phi = dump['var0'].reshape(mshape) error = phi - phi_true(t, x, y, z) max_error = np.max(np.abs(error)) errs[n] = max_error
os.chdir('../../test') call(['mv', '../prob/' + PROBLEM + '/' + TMP_DIR, './']) # Since this test is designed to run on a single machine (no batch scripts) # set openmpi to only use a few threads. Let MPI handle the rest. if MPI: import multiprocessing num_mpi = 8 num_cpus = multiprocessing.cpu_count() os.environ['OMP_NUM_THREADS'] = str(int(np.max([2, num_cpus / num_mpi]))) # RUN EXECUTABLE os.chdir(TMP_DIR) args = ['./bhlight', '-p', 'param_template.dat'] if MPI: bcall(args, int(num_mpi), stdin=DEVNULL) else: bcall(args) os.chdir('../') # READ SIMULATION OUTPUT dfiles = np.sort(glob.glob(os.path.join(TMP_DIR, '') + '/dumps/dump*.h5')) hdr = io.load_hdr(dfiles[0]) geom = io.load_geom(hdr) dump = io.load_dump(dfiles[-1], geom) nuLnu = dump['nuLnu'] onescatts = nuLnu[1].sum(axis=-1) NTH, NPHI = onescatts.shape[0], onescatts.shape[1] theta = np.linspace(0, np.pi, NTH + 1) theta = 0.5 * (theta[1:] + theta[:-1]) onescatts_th = onescatts.sum(axis=-1)
if MODES[n] == 3: # FAST dvar[0] = 0.476395427447 dvar[1] = 0.635193903263 dvar[2] = -0.102965815319 dvar[3] = -0.316873207561 dvar[5] = 0.359559114174 dvar[6] = -0.359559114174 dvar *= amp # RUN PROBLEM FOR EACH RESOLUTION AND ANALYZE RESULT for m in range(len(RES)): NPROC = 4 import psutil NCORE = psutil.cpu_count(logical=False) os.environ['OMP_NUM_THREADS'] = '%d' % (NCORE/NPROC) bcall(['./bhlight_' + str(RES[m]), '-p', 'param_template.dat'],int(NPROC)) dfiles = np.sort(glob.glob('dumps/dump*.h5')) hdr = io.load_hdr(dfiles[-1]) geom = io.load_geom(hdr, recalc=True) dump = io.load_dump(dfiles[-1],geom) X1 = geom['x'][:,:,0] X2 = geom['y'][:,:,0] dvar_code = [] dvar_code.append(dump['RHO'][:,:,0] - var0[0]) dvar_code.append(dump['UU'][:,:,0] - var0[1]) dvar_code.append(dump['U1'][:,:,0] - var0[2]) dvar_code.append(dump['U2'][:,:,0] - var0[3]) dvar_code.append(dump['U3'][:,:,0] - var0[4]) dvar_code.append(dump['B1'][:,:,0] - var0[5]) dvar_code.append(dump['B2'][:,:,0] - var0[6])