Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Creating a spherical mass distribution

Subject: Creating a spherical mass distribution

From: Christian Schultz

Date: 3 Nov, 2010 17:46:04

Message: 1 of 8

I'm doing this simulation on a spherical cloud of gas with a 1/r density profile. Of course in order to do any simulations in the first place I need some sample particles distributed on a spherical lattice in a symmetric, uniform way. How do you make a discrete, spherical symmetric cloud in Matlab? If I just needed a spherical surface I could just approximate the spherical surface with a regular polygon, but I need one more dimension since the cloud is a ball.

Subject: Creating a spherical mass distribution

From: Roger Stafford

Date: 3 Nov, 2010 18:29:21

Message: 2 of 8

"Christian Schultz" <spamcatcher@remove.this.easy.com> wrote in message <ias74s$i04$1@fred.mathworks.com>...
> I'm doing this simulation on a spherical cloud of gas with a 1/r density profile. Of course in order to do any simulations in the first place I need some sample particles distributed on a spherical lattice in a symmetric, uniform way. How do you make a discrete, spherical symmetric cloud in Matlab? If I just needed a spherical surface I could just approximate the spherical surface with a regular polygon, but I need one more dimension since the cloud is a ball.
- - - - - - - - -
  It would not be easy to space points in a strictly "symmetric, uniform way," especially since the required density, 1/r, approaches infinity at the sphere's center. Would you settle for points spaced according to a probability density of 1/r in a finite sphere using matlab's random number generators?

Roger Stafford

Subject: Creating a spherical mass distribution

From: Bruno Luong

Date: 3 Nov, 2010 18:52:04

Message: 3 of 8

"Christian Schultz" <spamcatcher@remove.this.easy.com> wrote in message <ias74s$i04$1@fred.mathworks.com>...
> I'm doing this simulation on a spherical cloud of gas with a 1/r density profile. Of course in order to do any simulations in the first place I need some sample particles distributed on a spherical lattice in a symmetric, uniform way. How do you make a discrete, spherical symmetric cloud in Matlab? If I just needed a spherical surface I could just approximate the spherical surface with a regular polygon, but I need one more dimension since the cloud is a ball.

% Generate a n 3D random points with 1/r distribution within the radius
% interval [r1,r2]
r1=1;
r2=2;
n = 10000;

s = randn(3,n);
r = sqrt(rand(1,n)*(r2^2-r1^2)+r1^2);
c = r./sqrt(sum(s.^2,1));
s = bsxfun(@times, s, c);

% Bruno

Subject: Creating a spherical mass distribution

From: Christian Schultz

Date: 3 Nov, 2010 21:11:03

Message: 4 of 8

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <ias9m1$47d$1@fred.mathworks.com>...
> "Christian Schultz" <spamcatcher@remove.this.easy.com> wrote in message <ias74s$i04$1@fred.mathworks.com>...
> > I'm doing this simulation on a spherical cloud of gas with a 1/r density profile. Of course in order to do any simulations in the first place I need some sample particles distributed on a spherical lattice in a symmetric, uniform way. How do you make a discrete, spherical symmetric cloud in Matlab? If I just needed a spherical surface I could just approximate the spherical surface with a regular polygon, but I need one more dimension since the cloud is a ball.
> - - - - - - - - -
> It would not be easy to space points in a strictly "symmetric, uniform way," especially since the required density, 1/r, approaches infinity at the sphere's center. Would you settle for points spaced according to a probability density of 1/r in a finite sphere using matlab's random number generators?
>
> Roger Stafford

I'm testing my simulation with the so called Evrard test known from numerical hydrodynamics, and as far as I could see in the papers I read so far, they seem to use a symmetric, polygonal lattice-like distribution. Even though I am still analyzing the problem (and therefore I'm not completely confident that it is true yet), it seems that they use a spherical lattice with a fixed length to all neighbours.

However, it would also be interesting to see the simulation tested on a spherically symmetric 1/r distribution. Even though my ultimate goal is to create a regular grid, a random distribution would also be a nice test.
 I looked at your randsphere code, and it seems to almost do the trick (though I must admit I'm not much into the incomplete gamma function and probability distributions), it just needs some slight modifications.

Subject: Creating a spherical mass distribution

From: Christian Schultz

Date: 3 Nov, 2010 22:08:03

Message: 5 of 8

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <iasb0k$sr$1@fred.mathworks.com>...
> "Christian Schultz" <spamcatcher@remove.this.easy.com> wrote in message <ias74s$i04$1@fred.mathworks.com>...
> > I'm doing this simulation on a spherical cloud of gas with a 1/r density profile. Of course in order to do any simulations in the first place I need some sample particles distributed on a spherical lattice in a symmetric, uniform way. How do you make a discrete, spherical symmetric cloud in Matlab? If I just needed a spherical surface I could just approximate the spherical surface with a regular polygon, but I need one more dimension since the cloud is a ball.
>
> % Generate a n 3D random points with 1/r distribution within the radius
> % interval [r1,r2]
> r1=1;
> r2=2;
> n = 10000;
>
> s = randn(3,n);
> r = sqrt(rand(1,n)*(r2^2-r1^2)+r1^2);
> c = r./sqrt(sum(s.^2,1));
> s = bsxfun(@times, s, c);
>
> % Bruno

I tried your code, but it produces random numbers on a straight line (in 3-space), and not a spherical distribution.

Subject: Creating a spherical mass distribution

From: Walter Roberson

Date: 3 Nov, 2010 22:31:41

Message: 6 of 8

On 10-11-03 04:11 PM, Christian Schultz wrote:

> I'm testing my simulation with the so called Evrard test known from
> numerical hydrodynamics, and as far as I could see in the papers I read
> so far, they seem to use a symmetric, polygonal lattice-like
> distribution. Even though I am still analyzing the problem (and
> therefore I'm not completely confident that it is true yet), it seems
> that they use a spherical lattice with a fixed length to all neighbours.

I don't see how that could work? A "spherical lattice" to me implies a series
of concentric spheres as boundaries, with the shell between the boundaries
divided up into cells. In order for there to be "a fixed length to all
neighbours", it seems to me that the distance between the centers of adjacent
cells on any one shell would have to be the same as the distance between any
cell center and the adjacent cell "inward" of it. As the surface area of the
shell boundaries grows proportional to the square of the shell radius, in
order to maintain a constant distance between the centers, the number of cells
would have to grow proportional to the square of the shell radius -- but the
volume of the sphere is growing proportional to the cube of the shell radius
so the volume of each cell is going to have to grow proportional to the radius
in order to keep up. If you fix the distance between shells so as to maintain
a fixed length between neighbours as you go outwards, and you are growing the
volume of the cells as you go outwards, it seems to me that you arrive at a
contradiction, as the geometry would require that the distance between
adjacent cell centers would have to grow.


For example, consider the cell centers to be marked by '.' in this diagram:

  .
...
  .

Let the center dot be the center of a shell of radius 1, and the other four
dots be the centers of cells within the shell between radius 2 and radius 1.
The horizontal and vertical distance between the center and the adjacent cells
is constant at 1, and the number of cells in the outer shell had to be 2^2 (=
4) in order to maintain that constant distance as the surface area of the
outer shell expanded proportional to r^2. But consider the distance between
the left dot and the upper dot: it is sqrt(2) if you follow a direct euclidean
path, and it is (2*Pi*r)/4 = Pi*((1+2)/2)/2 = 3/4*Pi if you use Great Circle
distance as your measure instead. Neither measure can maintain the distance of
1 while growing the shell at a fixed rate such as typically implied by a
"spherical lattice".


Thus, I don't see how it would be possible to use a fixed length to *all*
neighbours in a spherical lattice.

Just the use of an integral number of cells while the radius expands is going
to cause problems if the cells are expected to be a particular (fixed) shape.

Subject: Creating a spherical mass distribution

From: Bruno Luong

Date: 4 Nov, 2010 06:03:04

Message: 7 of 8


>
> I tried your code, but it produces random numbers on a straight line (in 3-space), and not a spherical distribution.

It's odd, here is the result I get with 10 points

>> r1=1;
r2=2;
n = 10;

s = randn(3,n);
r = sqrt(rand(1,n)*(r2^2-r1^2)+r1^2);
c = r./sqrt(sum(s.^2,1));
s = bsxfun(@times, s, c);
>> s

s =

   -0.9060 -0.1235 1.1516 0.7192 0.1040 -0.6584 -0.5701 -1.0032 1.3177 0.7311
    0.7269 -0.6609 -0.8484 -0.4345 1.7098 -0.0874 0.3903 -0.7442 -0.6219 0.0003
   -0.8134 -0.8583 -0.0607 1.5519 0.0895 -0.9945 -0.7686 0.0756 -0.6188 -1.0419

Then when I do that

 plot3(s(1,:),s(2,:),s(3,:),'.')

There definitively not in the straight line.

Bruno

Subject: Creating a spherical mass distribution

From: Christian Schultz

Date: 7 Nov, 2010 15:34:04

Message: 8 of 8

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <iatiao$73b$1@fred.mathworks.com>...
>
> >
> > I tried your code, but it produces random numbers on a straight line (in 3-space), and not a spherical distribution.
>
> It's odd, here is the result I get with 10 points
>
> >> r1=1;
> r2=2;
> n = 10;
>
> s = randn(3,n);
> r = sqrt(rand(1,n)*(r2^2-r1^2)+r1^2);
> c = r./sqrt(sum(s.^2,1));
> s = bsxfun(@times, s, c);
> >> s
>
> s =
>
> -0.9060 -0.1235 1.1516 0.7192 0.1040 -0.6584 -0.5701 -1.0032 1.3177 0.7311
> 0.7269 -0.6609 -0.8484 -0.4345 1.7098 -0.0874 0.3903 -0.7442 -0.6219 0.0003
> -0.8134 -0.8583 -0.0607 1.5519 0.0895 -0.9945 -0.7686 0.0756 -0.6188 -1.0419
>
> Then when I do that
>
> plot3(s(1,:),s(2,:),s(3,:),'.')
>
> There definitively not in the straight line.
>
> Bruno


I don't know what went wrong, when I run it on my Ubuntu laptop it works just fine. Thank you

I figured out a way to do what I wanted. I guess I could have been a bit more specific in my description of the problem. I just made a regular uniform rectangular grid, and confined it to a sphere by discarding the part of the grid that was outside. Then I simply did a radial stretch of the grid by sending r_old into r_new=|r_old|r_old. That seems to do the trick.

Than you all for your contributions.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us