The following code draws a random distribution of particles in 3D. The Manipulate box allows to change a few parameters (number of particles, size of clusters, ...). I would like to define the local number of particles per unit volume $V = \ell^3$, on a variable scale $\ell$, and plot it as level curves (i.e surfaces) in 3D. How can this be done in Mathematica?
galaxies[p_, q_, r_] := Module[
{pts = RandomReal[{-1, 1}, {p, 3}]},
pts = Nest[Join[#, {RandomChoice[#] + r RandomReal[{-1, 1}, 3]}] &, pts, q];
pts
]
graph[p_, q_, r_] := Graphics3D[{RGBColor[{0.5, 0.4, 1.0}], PointSize[0.004], Point[galaxies[p, q, r]]}]
view[p_, q_, r_] := Show[{graph[p, q, r]},
PlotRange -> {{-1, 1}, {-1, 1}, {-1, 1}},
Boxed -> True,
Background -> Black,
ImageSize -> {700, 700},
SphericalRegion -> True,
Method -> {"RotationControl" -> "Globe"}
]
Manipulate[
view[p, q, r],
{{p, 500, "Number of clusters"}, 1, 1000, 1},
{{q, 10000, "Number of galaxies"}, 0, 10000, 1},
{{r, 0.05, "Scale"}, 0, 0.2, 0.001},
ControlPlacement -> Bottom
]
The scale $\ell$ should be a variable, from a small value up to the full box size, say : $0.01 \le \ell \le 2$, that could be changed in another Manipulate box. The idea is to plot the density of the particles in the box as a scalar function: $$\tag{1} \rho(x, y, z, \ell) = \frac{N(x, y, z, \ell)}{\ell^3}, $$ where $N(x, y, z, \ell)$ gives the total number of particles inside the cube of volume $V = \ell^3$, as a function of the cube center $\{x, y, z \}$ (with -1 < x, y, z < 1). I really don't know how to start to implement the idea, especially since the sampling volume should always stay inside the whole box of size $\ell_{max} = 2$ (with center located at $\{0, 0, 0 \}$). The sampling near the edges of the whole box may also be a problem (maybe the random distribution should be defined on a box two times larger than the display box).
EDIT: The exact "microscopic" density of the $N$ particles can be defined using Dirac's deltas: $$\tag{2} \rho(x, y, z) = \sum_{k \,=\, 1}^N \delta (x - x_k) \, \delta (y - y_k) \, \delta (z - z_k). $$ This density is 0 everywhere, except at the location of a particle. We could then introduce the averaged density on a volume $\ell^3$ (around a point of coordinates $\{x, y, z\}$), by integration: $$\tag{3} \bar{\rho}_{\ell}(x, y, z) = \frac{1}{\ell^3} \int_{x - \ell/2}^{x + \ell/2} \int_{y - \ell/2}^{y + \ell/2} \int_{z - \ell/2}^{z + \ell/2} \rho(x, y, z) \, dx \, dy \, dz. $$ That density could be called "fine grained" when $\ell$ is small, and "coarse grained" when $\ell$ is close to the size of the whole box. In all cases: $0.0001 \le \ell \le 2$ (since the random distribution above is enclosed in a box of size $2$).
I should have stated this already from the start in my question, sorry about that. The parameter $\ell$ should be a variable in a Manipulate box. The function (3) is what I would like to calculate for my random distribution (code above), and plot as a 3D graphics (level curves or surfaces?). Of course, we should get $\bar{\rho}_1(x, y, z) = N/8$ when $\ell = 2$.
Ideally, the density $\bar{\rho}_{\ell}(x, y, z)$ should be smoothed/interpolated to give some smooth level curves/surfaces in a plot, instead of ugly discontinuous jumps. So how should I do this, with Mathematica?
SmoothKernelDistribution
$\endgroup$