def radial_density(r, mass, N=100, dim=3): """ Took this from amuse.ext.radial_profile and changed it so that it returns a VectorQuantity.""" if dim == 3: volfac = numpy.pi * 4. / 3. elif dim == 2: volfac = numpy.pi else: volfac = 1 n = len(r) a = r.argsort() i = 0 r_a = [] dens = [] oldrshell = 0. * r[0] while i != n - 1: i1 = i + N if (n - i1 < N): i1 = n - 1 rshell = (r[a[i1]] + r[a[i1 - 1]]) / 2 ra = r[a[i:i1]].sum() / (i1 - i) / 2 + (oldrshell + rshell) / 4 da = mass[a[i:i1]].sum() / (rshell**dim - oldrshell**dim) oldrshell = rshell r_a.append(ra) dens.append(da) i = i1 radii = numpy.array(r_a) densities = numpy.array(dens) / volfac radii_vq = VectorQuantity.new_from_scalar_quantities(*radii) densities_vq = VectorQuantity.new_from_scalar_quantities(*densities) return (radii_vq, densities_vq)
def radial_density(r,mass,N=100,dim=3): """ Took this from amuse.ext.radial_profile and changed it so that it returns a VectorQuantity.""" if dim==3: volfac=numpy.pi*4./3. elif dim==2: volfac=numpy.pi else: volfac=1 n=len(r) a=r.argsort() i=0 r_a=[] dens=[] oldrshell=0.*r[0] while i != n-1: i1=i+N if( n-i1 < N ): i1=n-1 rshell=(r[a[i1]]+r[a[i1-1]])/2 ra=r[a[i:i1]].sum()/(i1-i)/2+(oldrshell+rshell)/4 da=mass[a[i:i1]].sum()/(rshell**dim-oldrshell**dim) oldrshell=rshell r_a.append(ra) dens.append(da) i=i1 radii = numpy.array(r_a) densities = numpy.array(dens)/volfac radii_vq = VectorQuantity.new_from_scalar_quantities(*radii) densities_vq = VectorQuantity.new_from_scalar_quantities(*densities) return (radii_vq, densities_vq)
def tidal_tensor(t, x, y, z, galaxy): Fxx, Fyx, Fzx, Fxy, Fyy, Fzy, Fxz, Fyz, Fzz = galaxy.get_tidal_tensor( t, x, y, z) return VectorQuantity.new_from_scalar_quantities( Fxx, Fyx, Fzx, Fxy, Fyy, Fzy, Fxz, Fyz, Fzz)
def _get_array_of_positions_from_arguments(axes_names, **kwargs): if kwargs.get('pos',None): return kwargs['pos'] if kwargs.get('position',None): return kwargs['position'] coordinates=[kwargs[x] for x in axes_names] if numpy.ndim(coordinates[0])==0: return VectorQuantity.new_from_scalar_quantities(*coordinates) return column_stack(coordinates)
def _get_array_of_positions_from_arguments(axes_names, **kwargs): if kwargs.get('pos',None): return kwargs['pos'] if kwargs.get('position',None): return kwargs['position'] coordinates=[kwargs[x] for x in axes_names] ndim=numpy.ndim(coordinates[0]) if ndim==0: return VectorQuantity.new_from_scalar_quantities(*coordinates) result=stack(coordinates) order=tuple(range(1,ndim+1))+(0,) return result.transpose(order)
def densitycentre_coreradius_coredens(particles, unit_converter=None, number_of_neighbours=7, reuse_hop=False, hop=HopContainer()): """ calculate position of the density centre, coreradius and coredensity >>> import numpy >>> from amuse.ic.plummer import new_plummer_sphere >>> numpy.random.seed(1234) >>> particles=new_plummer_sphere(100) >>> pos,coreradius,coredens=particles.densitycentre_coreradius_coredens() >>> print coreradius 0.404120092331 length """ if isinstance(hop, HopContainer): hop.initialize(unit_converter) hop = hop.code try: hop.particles.add_particles(particles) except Exception as ex: hop.stop() raise exceptions.AmuseException( str(ex) + " (note: check whether Hop needs a converter here)") hop.parameters.density_method = 2 hop.parameters.number_of_neighbors_for_local_density = number_of_neighbours hop.calculate_densities() density = hop.particles.density x = hop.particles.x y = hop.particles.y z = hop.particles.z rho = density.amax() total_density = numpy.sum(density) x_core = numpy.sum(density * x) / total_density y_core = numpy.sum(density * y) / total_density z_core = numpy.sum(density * z) / total_density rc = (density * ((x - x_core)**2 + (y - y_core)**2 + (z - z_core)**2).sqrt()).sum() / total_density if not reuse_hop: hop.stop() return VectorQuantity.new_from_scalar_quantities(x_core, y_core, z_core), rc, rho
def densitycentre_coreradius_coredens( particles, unit_converter=None, number_of_neighbours=7, reuse_hop=False, hop=HopContainer() ): """ calculate position of the density centre, coreradius and coredensity >>> import numpy >>> from amuse.ic.plummer import new_plummer_sphere >>> numpy.random.seed(1234) >>> particles=new_plummer_sphere(100) >>> pos,coreradius,coredens=particles.densitycentre_coreradius_coredens() >>> print coreradius 0.404120092331 length """ if isinstance(hop, HopContainer): hop.initialize(unit_converter) hop = hop.code hop.particles.add_particles(particles) hop.parameters.density_method = 2 hop.parameters.number_of_neighbors_for_local_density = number_of_neighbours hop.calculate_densities() density = hop.particles.density x = hop.particles.x y = hop.particles.y z = hop.particles.z rho = density.amax() total_density = numpy.sum(density) x_core = numpy.sum(density * x) / total_density y_core = numpy.sum(density * y) / total_density z_core = numpy.sum(density * z) / total_density rc = (density * ((x - x_core) ** 2 + (y - y_core) ** 2 + (z - z_core) ** 2).sqrt()).sum() / total_density if not reuse_hop: hop.stop() return VectorQuantity.new_from_scalar_quantities(x_core, y_core, z_core), rc, rho
def eigen_values(t, x, y, z, galaxy): l1, l2, l3 = galaxy.get_eigen_values(t, x, y, z) return VectorQuantity.new_from_scalar_quantities(l1, l2, l3)
x = hop.particles.x y = hop.particles.y z = hop.particles.z rho = density.amax() total_density = numpy.sum(density) x_core = numpy.sum(density * x) / total_density y_core = numpy.sum(density * y) / total_density z_core = numpy.sum(density * z) / total_density rc = (density * ((x - x_core)**2 + (y - y_core)**2 + (z - z_core)**2).sqrt()).sum() / total_density if not reuse_hop: hop.stop() return VectorQuantity.new_from_scalar_quantities(x_core, y_core, z_core), rc, rho def new_particle_from_cluster_core(particles, unit_converter=None, density_weighting_power=2, cm=None, reuse_hop=False, hop=HopContainer()): """ Uses Hop to find the density centre (core) of a particle distribution and stores the properties of this core on a particle: position, velocity, (core) radius and (core) density. Particles are assigned weights that depend on the density (as determined by Hop) to a certain power.
def eigen_values(t, x, y, z, galaxy): l1, l2, l3 = galaxy.get_eigen_values(t, x, y, z) return VectorQuantity.new_from_scalar_quantities(l1, l2, l3)
def tidal_tensor(t, x, y, z, galaxy): Fxx, Fyx, Fzx, Fxy, Fyy, Fzy, Fxz, Fyz, Fzz = galaxy.get_tidal_tensor( t, x, y, z) return VectorQuantity.new_from_scalar_quantities(Fxx, Fyx, Fzx, Fxy, Fyy, Fzy, Fxz, Fyz, Fzz)