def LoadLosCameraFromFile(self, file_path): #{{{ fname = basename(file_path) ext = fname[-3:] if (ext == ".h5" or ext == ".hdf5"): self.LoadLosCamera(Camera.from_HDF5(file_path)) else: self.LoadLosCamera(Camera.from_csv(file_path))
def update_cameras_dict(self, nxys): #{{{ uaxis, vaxis = self.GetUVAxes() waxis = self.GetLosAxis() self.region_center[:] = self.cross_center[:] cc = self.region_center if (self.zoom_size is not None): self.region_size = self.region_size * self.zoom_size rs = self.region_size self.cam_dict['los'] = Camera(center=cc, line_of_sight_axis=waxis, up_vector=vaxis, region_size=[rs, rs], \ distance=(rs/2.), far_cut_depth=(rs/2.), map_max_size=nxys["los"]) self.cam_dict['u'] = Camera(center=cc, line_of_sight_axis=uaxis, up_vector=vaxis, region_size=[rs, rs], \ distance=(rs/2.), far_cut_depth=(rs/2.), map_max_size=nxys["u"]) self.cam_dict['v'] = Camera(center=cc, line_of_sight_axis=-vaxis, up_vector=waxis, region_size=[rs, rs], \ distance=(rs/2.), far_cut_depth=(rs/2.), map_max_size=nxys["v"])
def edit_cam(self, evt): self.model.update_cameras_dict(self.GetWindowSizesDict()) EditCameraDlg = EditCameraDialog(self.model.cam_dict["los"]) EditCameraDlg.ShowModal() if EditCameraDlg.ok: center = eval(EditCameraDlg.center.GetValue()) line_of_sight_axis = eval( EditCameraDlg.line_of_sight_axis.GetValue()) rs = float(EditCameraDlg.region_size.GetValue()) new_cam = Camera(center=center, line_of_sight_axis=line_of_sight_axis, \ region_size=[rs, rs], \ distance=(rs/2.), far_cut_depth=(rs/2.), \ map_max_size=self.model.cam_dict["los"].map_max_size) self.model.LoadLosCamera(new_cam) self.controller.gui.reset() EditCameraDlg.Destroy()
def myplot(path='.',output=-1,sink=False,center=[0.5,0.5,0.5],los='x',size=[0.5,0.5], \ minimum=None,maximum=None,typlot='rho',record=False,igrp=-1,plot_velocity=False,slice=False): filerep = path if (record): savedir = os.path.join(filerep, "results") if not os.path.exists(savedir): os.makedirs(savedir) #mu,alpha,lmax = parseDirSink(directory) #savedir = checkDir(savedir, "mu" + mu) #savedir = checkDir(savedir, "lmax" + lmax) # if output=-1, take last output if output == -1: outputname = np.sort(glob.glob(os.path.join(filerep, "output_?????")))[-1] output = int(outputname.split('_')[-1]) # Define the file ro = pm.RamsesOutput(filerep, output) if igrp != -1 and (igrp > ro.info["ngrp"] or igrp < 0): print(bold + 'PROBLEM:' + reset + ' igrp=' + str(igrp) + ' but simulation with ngrp=' + str(ro.info["ngrp"])) sys.exit(1) # Define the output data structure (to use with pymses5) ro.define_amr_scalar_field("hydro", "rho", 0) ro.define_amr_vector_field("hydro", "vel", [1, 2, 3]) ro.define_amr_vector_field("hydro", "Bl", [4, 5, 6]) ro.define_amr_vector_field("hydro", "Br", [7, 8, 9]) ro.define_amr_scalar_field("hydro", "P", 10) ro.define_amr_multivalued_field("hydro", "Er", 11, ro.info["ngrp"]) ro.define_amr_vector_field( "hydro", "J", [11 + ro.info["ngrp"], 12 + ro.info["ngrp"], 13 + ro.info["ngrp"]]) ro.define_amr_scalar_field("hydro", "eint", 14 + ro.info["ngrp"]) if ro.info["eos"]: ro.define_amr_scalar_field("hydro", "T", 15 + ro.info["ngrp"]) ro.define_amr_vector_field("grav", "g", [0, 1, 2]) time = ro.info["time"] * ro.info["unit_time"].express(ct.kyr) if (sink): # Read sink particles sink_filename = os.path.join( filerep, 'output_%05d' % output + '/sink_%05d' % output + '.csv') print("Reading sinks : " + reset + sink_filename) sinkp = np.loadtxt(sink_filename, dtype={ 'names': ('Id', 'mass', 'x', 'y', 'z', 'vx', 'vy', 'vz', 'rot_period', 'lx', 'ly', 'lz', 'acc_rate', 'acc_lum', 'age', 'int_lum', 'Teff'), 'formats': ('i', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f') }, delimiter=',') sinkp = np.atleast_1d(sinkp) # to work even if only 1 sink nsink = sinkp.size # Define the accretion radius if ("ncell_racc" in ro.info): ncell_racc = ro.info['ncell_racc'] else: ncell_racc = 4 dxmin = 0.5**ro.info['levelmax'] * ro.info["unit_length"].express( ct.au) r_acc = dxmin * ncell_racc #area = np.pi*r_acc**2 xc, yc, zc = sinkp['x'] / ro.info['boxlen'], sinkp['y'] / ro.info[ 'boxlen'], sinkp['z'] / ro.info['boxlen'] else: nsink = 0 if nsink == 0: nsink = 1 # Center xc, yc, zc = np.atleast_1d(center[0]), np.atleast_1d( center[1]), np.atleast_1d(center[2]) cmap = pl.cm.get_cmap('seismic', 100) if typlot == 'rho': cmap = pl.cm.get_cmap('jet', 100) elif typlot == 'Tr': cmap = pl.cm.get_cmap('hot', 100) elif typlot == 'B': cmap = pl.cm.get_cmap('PuOr', 100) elif typlot == 'beta_plasma': cmap = pl.cm.get_cmap('seismic', 100) else: cmap = pl.cm.get_cmap('jet', 100) # Width of the region to plot xr, yr = size[0], size[1] xbl = (-xr / 2. * ro.info["unit_length"]).express(ct.au) xbr = (+xr / 2. * ro.info["unit_length"]).express(ct.au) ybl = (-yr / 2. * ro.info["unit_length"]).express(ct.au) ybr = (+yr / 2. * ro.info["unit_length"]).express(ct.au) extent = [xbl, xbr, ybl, ybr] # Define camera if los == 'x': upvec = "z" vx = 1 vy = 2 labelx = 'Y (AU)' labely = 'Z (AU)' plane = 'yz' elif los == 'y': upvec = "x" vx = 2 vy = 0 labelx = 'Z (AU)' labely = 'X (AU)' plane = 'zx' elif los == 'z': upvec = "y" vx = 0 vy = 1 labelx = 'X (AU)' labely = 'Y (AU)' plane = 'xy' def plot_func(dset): if typlot == 'rho': return dset['rho'] * ro.info["unit_density"].express( ct.g / ct.cm**3) if typlot == 'eint': return dset['eint'] * (ro.info["unit_density"] * ro.info["unit_velocity"]**2).express( ct.erg / ct.cm**3) if typlot == 'T': if ro.info["eos"]: return dset['T'] else: return dset['P'] / dset['rho'] * ro.info[ "unit_temperature"].express(ct.K) * ro.info["mu_gas"] if typlot == 'B': B = 1./4*( (dset['Bl'][:,0]+dset['Br'][:,0])**2 \ +(dset['Bl'][:,1]+dset['Br'][:,1])**2 \ +(dset['Bl'][:,2]+dset['Br'][:,2])**2) return B # To use with pymses5 if typlot == 'Er' or typlot == 'Tr': if igrp == -1: Er = np.sum(dset['Er'], axis=-1) * ( ro.info["unit_density"] * ro.info["unit_velocity"]**2).express(ct.erg / ct.cm**3) else: Er = dset['Er'][:, igrp - 1] * ( ro.info["unit_density"] * ro.info["unit_velocity"]**2).express(ct.erg / ct.cm**3) if typlot == 'Er': return Er else: return (Er / ct.a_R.express(ct.erg / ct.cm**3 / ct.K**4))**0.25 if typlot == 'entropy': return dset['P'] / dset['rho']**ro.hydro_info["gamma"] if typlot == 'Pth_Pdyn': return dset['P'] / (0.5 * dset['rho'] * np.sum(dset['vel']**2, axis=-1)) if typlot == 'beta_plasma': B = 1./4.*( (dset['Bl'][:,0]+dset['Br'][:,0])**2 \ +(dset['Bl'][:,1]+dset['Br'][:,1])**2 \ +(dset['Bl'][:,2]+dset['Br'][:,2])**2) if igrp == -1: Er = np.sum( dset['Er'], axis=-1 ) #*(ro.info["unit_density"]*ro.info["unit_velocity"]**2).express(ct.erg/ct.cm**3) else: Er = dset[ 'Er'][:, igrp - 1] #*(ro.info["unit_density"]*ro.info["unit_velocity"]**2).express(ct.erg/ct.cm**3) return dset['P'] / (B) #/dset['rho']) else: print(bold + 'Problem in typlot') sys.exit() # Fields to be read fields_to_read = [] if (not ro.info["eos"] and typlot == 'T') or typlot == 'entropy': fields_to_read = ["rho", "P"] elif typlot == 'Tr': fields_to_read = ["Er"] elif typlot == 'Pth_Pdyn': fields_to_read = ["P", "rho", "vel"] elif typlot == 'B': fields_to_read = ["Bl", "Br"] elif typlot == 'beta_plasma': fields_to_read = ["Bl", "Br", "rho", "P", "Er"] else: fields_to_read = [typlot] if (plot_velocity): fields_to_read.append("vel") if not slice: fields_to_read.append("rho") fields_to_read = list(set(fields_to_read)) #to remove duplicates if slice: source = ro.amr_source(fields_to_read) varplot_op = ScalarOperator(plot_func) else: rt = raytracing.RayTracer(ro, fields_to_read) if typlot == 'rho': func = lambda dset: plot_func(dset) * ro.info["unit_length" ].express(ct.cm) varplot_op = ScalarOperator(func) else: up_func = lambda dset: (dset["rho"] * ro.info["unit_density"]. express(ct.g / ct.cm**3) * plot_func(dset)) down_func = lambda dset: (dset["rho"] * ro.info["unit_density"]. express(ct.g / ct.cm**3)) varplot_op = FractionOperator(up_func, down_func) for i in range(nsink): fig = pl.figure() ax = pl.subplot(111) pl.xlabel(labelx) pl.ylabel(labely) cam = Camera(center=[xc[i], yc[i], zc[i]],line_of_sight_axis=los,region_size=[xr, yr] \ , up_vector=upvec,map_max_size=512, log_sensitive=True) if slice: mapx = SliceMap(source, cam, varplot_op, z=0.) else: # WARNING! surf_qty=True to get an integral over dz and not dSdz # so that if typlot=='rho', get surface density map # and if not, get a density-weighted map (and not a mass-weighted map) mapx = rt.process(varplot_op, cam, surf_qty=True) if typlot == 'Pth_Pdyn': imx = pl.imshow(mapx.transpose(), extent = extent, origin='lower' \ , vmin = minimum, vmax = maximum,cmap=cmap) #imx = pl.contourf(mapx.transpose(), extent = extent, origin='lower' \ # , vmin = minimum, vmax = maximum) cbar = pl.colorbar() cbar.set_label(typlot) else: imx = pl.imshow(np.log10(mapx.transpose()), extent = extent, origin='lower' \ , vmin = minimum, vmax = maximum,cmap=cmap) #imx = pl.contourf(np.log10(mapx.transpose()), extent = extent, origin='lower' \ # , vmin = minimum, vmax = maximum) cbar = pl.colorbar() # cmap = pl.cm.get_cmap('jet',100) if typlot == 'rho': cbar.set_label(r'$\mathrm{log(}\rho)$', fontsize=15) elif typlot == 'Tr': cbar.set_label(r'$\mathrm{log(T_r)}$', fontsize=15) elif typlot == 'Pth_Pdyn': cbar.set_label('Pth/Pdyn') elif typlot == 'B': cbar.set_label('log(|B|)') elif typlot == 'beta_plasma': cbar.set_label(r'$\mathrm{log(}\beta)$', fontsize=15) else: cbar.set_label('Log(' + typlot + ')') if (sink): for isink in range(nsink): # Plot sinks position mass = sinkp[isink]["mass"] col = cmap(int(mass * 100)) xp = (sinkp[isink]['x'] / ro.info['boxlen'] - xc[i]) * ro.info["unit_length"].express(ct.au) yp = (sinkp[isink]['y'] / ro.info['boxlen'] - yc[i]) * ro.info["unit_length"].express(ct.au) zp = (sinkp[isink]['z'] / ro.info['boxlen'] - zc[i]) * ro.info["unit_length"].express(ct.au) if los == 'x': ax.add_patch( pl.Circle((yp, zp), radius=r_acc, fc='white', alpha=0.65)) ax.add_patch( pl.Circle((yp, zp), radius=(r_acc / ncell_racc), fc=col, ec=col)) #pl.plot(yp,zp,'k+',mew=2,ms=10) elif los == 'y': ax.add_patch( pl.Circle((zp, xp), radius=r_acc, fc='white', alpha=0.65)) ax.add_patch( pl.Circle((zp, xp), radius=(r_acc / ncell_racc), fc=col, ec=col)) #pl.plot(zp,xp,'k+',mew=2,ms=10) elif los == 'z': ax.add_patch( pl.Circle((xp, yp), radius=r_acc, fc='white', alpha=0.65)) ax.add_patch( pl.Circle((xp, yp), radius=(r_acc / ncell_racc), fc=col, ec=col)) #pl.plot(xp,yp,'k+',mew=2,ms=10) # Plot velocity field if (slice and plot_velocity): p = cam.get_slice_points(0.0) nx, ny = cam.get_map_size() dset = pm.analysis.sample_points(source, p) vel = dset["vel"] * ro.info["unit_velocity"].express(ct.km / ct.s) rs = 32 x = np.linspace(xbl, xbr, nx) y = np.linspace(ybl, ybr, ny) u, v = np.zeros((nx, ny)), np.zeros((nx, ny)) mask = np.zeros((nx, ny)) for ii in range(nx): for jj in range(ny): if (ii % rs == 0 and jj % rs == 0): u[ii, jj] = vel[:, vx].reshape(nx, ny)[ii, jj] v[ii, jj] = vel[:, vy].reshape(nx, ny)[ii, jj] else: u[ii, jj] = 'Inf' v[ii, jj] = 'Inf' mask[ii, jj] = 1 u2 = u[::rs, ::rs] v2 = v[::rs, ::rs] x2 = x[::rs] y2 = y[::rs] vel_mean = np.mean(np.sqrt(u2**2 + v2**2)) vel_max = np.max(np.sqrt(u2**2 + v2**2)) #pl.quiver(x,y,u.transpose(),v.transpose(),scale=20*nx) #Q=pl.quiver(x,y,u.transpose(),v.transpose(),scale=100,pivot='mid') #u_masked=np.ma.masked_array(u,mask=mask) #v_masked=np.ma.masked_array(v,mask=mask) #vel_mean=np.mean(np.sqrt(u_masked**2+v_masked**2)) Q = pl.quiver(x2, y2, u2.transpose(), v2.transpose(), scale=100, pivot='mid') #pl.quiverkey(Q,0.7,0.92,vel_mean,r'%.2f km/s'%vel_mean,coordinates='figure') pl.quiverkey(Q, 0.7, 0.92, vel_max, r'%.2f km/s' % vel_max, coordinates='figure') pl.axis(extent) del (u, v, x, y, u2, v2, x2, y2) if (sink): # Print the mass of the sinks mass = 0 for j in range(nsink): mass = mass + sinkp["mass"][j] pl.figtext(0.01, 00.01, "$\mathrm{M}_*=$" + "%4.1f " % mass + "$M_\odot$", fontsize=15) pl.suptitle("%.3f kyr" % time) if (record): for format in ("png", "eps", "pdf"): if slice: namefig = os.path.join( savedir, "slice_" + typlot + "_" + plane + "_eps" + "_%.3f" % size[0] + "_out" + "_%05d" % output + "_isink_%05d." % sinkp['Id'][i] + format) else: namefig = os.path.join( savedir, "proj_" + typlot + "_" + plane + "_eps" + "_%.3f" % size[0] + "_out" + "_%05d" % output + "_isink_%05d." % sinkp['Id'][i] + format) print(bold + "Save figure: " + reset + namefig) pl.savefig(namefig) pl.close() else: pl.ion() pl.show() if (sink): # Print the mass of the sinks for i in range(nsink): mass = sinkp["mass"][i] print(bold + "Mass(" + str(i) + "): " + reset + "%.2e Msun" % mass)
(dset["rho"])) # Colormap initialisation (ran) # the first iteration of the following "for" loop is done separately # to save the colormap used for the whole movie i = 0 angle = N.pi * i / nbImgRot + N.pi * .5 / 180 # add .5 to avoid grid alignment artifact problem axe = [N.sin(angle), 0, N.cos(angle)] center = center_init + (zoom_focus - center_init) * (i * 1. / nbImgRot) region_size = region_size_init * ((1. / zoom_factor - 1) / nbImgRot * i + 1) distance = distance_init * ((1. / zoom_factor - 1) / nbImgRot * i + 1) far_cut_depth = far_cut_depth_init * ( (1. / zoom_factor - 1) / nbImgRot * i + 1) cam = Camera(center=center, line_of_sight_axis=axe, up_vector="y", region_size=region_size, distance=distance, far_cut_depth=far_cut_depth, map_max_size=mms) # Octree source creation : source = ro.amr_source(["rho"]) # We need to add an extension to the octree box loaded in memory, # or not (if min(distance,far_cut_depth) > max(region_size)) # as the rotation is around the "y" axis, # to allow rotation without empty starting point ray bugs esize = 0.5**(ro.info["levelmin"] + 1) + max(region_size) camOctSource = cam.copy() fullOctreeDataSource = CameraOctreeDatasource(camOctSource, esize, source,
# a RAMSES output in the current directory fileDir = None outNumber = None mms = 256#1920 ro = RamsesOutput(fileDir, outNumber) outNumber = ro.iout #if myrank == 0:time0 = time() #if myrank == 0: print "time RamsesOutput=", (time()-time0) center = [ 0.5, 0.5, 0.5 ] rt = RayTracerMPI(ro, ["rho"], remember_data = True) #if myrank == 0: print "time RamsesOutput+rt=", (time()-time0) op = FractionOperator(lambda dset: (dset["rho"]**2), lambda dset: (dset["rho"])) i = 0 # first iteration of the following "for" loop done separately to save the colormap angle = N.pi*i/NB_IMG + N.pi*.5/180# add .5 to avoid grid alignment artifact problem axe = [N.sin(angle),0,N.cos(angle)] cam = Camera(center=[0.5, 0.5, 0.5], line_of_sight_axis=axe, up_vector="y", region_size=[.5, .28125], distance=0.2, far_cut_depth=0.2, map_max_size=mms) t0 = time() map=rt.process(op, cam, use_balanced_cpu_list=True) t1=time() if myrank == 0: print("myrank", myrank, "rt total time = %.1f s"%(t1-t0), "mms = ", \ mms, "max AMR read = ", cam.get_required_resolution()) save_map_HDF5(map, cam, map_name="img%s"%(i)) # ran used to fix the colormap during the movie (= use first frame colormap for each frame) ran = save_HDF5_to_img("./img%s.h5"%(i), cmap="jet",img_path="./") os.remove(("./img%s.h5"%(i))) for i in range(1, NB_IMG): angle = N.pi*i/NB_IMG + N.pi*.5/180 axe = [N.sin(angle),0,N.cos(angle)] cam = Camera(center=[0.5, 0.5, 0.5], line_of_sight_axis=axe, up_vector="y", region_size=[.5, .28125], distance=0.2,
# Colormap initialisation (ran) i = 0.5 # We add .5 degree to avoid the 30/60/120... degree graphic bug # inclined the galaxy by galaxy_angle (°) : galaxy_angle = galaxy_angle_init angle = N.pi * i / 180 axe = [N.cos(N.pi*galaxy_angle/180)*N.cos(angle),N.cos(N.pi*galaxy_angle/180)*N.sin(angle),\ -N.sin(N.pi*galaxy_angle/180)] # = Rz(angle) * (rotation matrix Ry(galaxy_angle) * (1,0,0)) up_vector = [N.sin(N.pi*galaxy_angle/180)*N.cos(angle),N.sin(N.pi*galaxy_angle/180)*N.sin(angle),\ -N.cos(N.pi*galaxy_angle/180)] # = Rz(angle) * (rotation matrix Ry(galaxy_angle) * (0,0,1)) region_size = region_size_init distance = distance_init far_cut_depth = far_cut_depth_init cam = Camera(center=center, line_of_sight_axis=axe, up_vector=up_vector, region_size=region_size, distance=distance, far_cut_depth=far_cut_depth, map_max_size=mms, perspectiveAngle=perspectiveAngle) # Octree source creation : source = ro.amr_source(["rho"]) # extended by max(region_size) to allow rotation : esize = 0.5**(ro.info["levelmin"] + 1) + max(max(region_size), distance, far_cut_depth) camOctSource = cam.copy() fullOctreeDataSource = CameraOctreeDatasource(camOctSource, esize, source, ngrid_max=ngrid_max, include_split_cells=True).dset OctreeRT = OctreeRayTracer(fullOctreeDataSource)
#print zoom #cam = Camera(center=center, line_of_sight_axis=los, # region_size=[.25*zoom, .140625*zoom], distance=0.2*zoom, # far_cut_depth=0.2*zoom, map_max_size=mms) #cam.get_required_resolution() if myrank == 0:time0 = time() outnumber = 42 ro = RamsesOutput("/home/labadens", outnumber) if myrank == 0: print("RamsesOutput time=", (time()-time0)) op = FractionOperator(lambda dset: (dset["rho"]**2), lambda dset: (dset["rho"])) # Colormap initialisation (ran) cam = Camera(center=center, line_of_sight_axis=los, region_size=[.25*zoom, .140625*zoom], distance=0.2*zoom, far_cut_depth=0.2*zoom, map_max_size=mms) source = ro.amr_source(["rho"]) esize = 0.5**(ro.info["levelmin"]+1) camOctSource = cam.copy() fullOctreeDataSource = CameraOctreeDatasource(camOctSource, esize, source, ngrid_max=ngrid_max, include_split_cells=True).dset OctreeRT = OctreeRayTracer(fullOctreeDataSource) dataset_already_loaded = True t0 = time() map, levelmax_map = OctreeRT.process(op, cam, rgb=False, use_openCL=use_openCL) t1=time() if myrank == 0: i,img_number=0,0