3D volume plot for spherical coordinate grid points

How to get volume plot for the following spherical coordinate positions? :child:
I have a lot of blocks each having such r, θ and ϕ spherical coordinate position vectors and i want to create 3D volume plot for each block vectors and finally combine them to create a single 3D volume plot.

r = Float32[1.677044, 1.8385518, 2.0156136, 2.2097273, 2.4225352, 2.6558375, 2.911608, 3.1920104, 3.499417, 3.8364286, 4.205896, 4.6109447, 5.055002, 5.5418243, 6.0755296, 6.660634, 7.3020864, 8.005314]
θ = Float32[0.049087387, 0.14726216, 0.24543692, 0.3436117]
ϕ = Float32[0.19634955, 0.5890486, 0.9817477, 1.3744467, 1.7671459, 2.1598449, 2.552544, 2.9452431, 3.3379421, 3.7306414, 4.12334, 4.5160394, 4.9087386, 5.3014374, 5.6941366, 6.086836]

What do you mean by “volume plot”? Your vectors do not have the same length, so what do they represent ?

Here is one way to define a RectilinearGrid with Spherical coordinates:

using Meshes
using CoordRefSystems

import GLMakie as Mke

r = Float32[1.677044, 1.8385518, 2.0156136, 2.2097273, 2.4225352, 2.6558375, 2.911608, 3.1920104, 3.499417, 3.8364286, 4.205896, 4.6109447, 5.055002, 5.5418243, 6.0755296, 6.660634, 7.3020864, 8.005314]
θ = Float32[0.049087387, 0.14726216, 0.24543692, 0.3436117]
ϕ = Float32[0.19634955, 0.5890486, 0.9817477, 1.3744467, 1.7671459, 2.1598449, 2.552544, 2.9452431, 3.3379421, 3.7306414, 4.12334, 4.5160394, 4.9087386, 5.3014374, 5.6941366, 6.086836]

g = RectilinearGrid{𝔼,typeof(Spherical(0,0,0))}(r, θ, ϕ)

viz(g)

The type parameters in the constructor are a bit low-level, but I think they match what you have in mind. The first type parameter 𝔼 refers to the manifold where the geometries live. In this case this is the Euclidean manifold. The second type parameter is the coordinate reference system type, which I am producing with a typeof(Spherical(0,0,0)) call.

You can also define the periodicity of each coordinate. Below you can find an alternative code that considers one of the angular coordinates to be periodic. First, it creates a GridTopology with the number of elements in the grid and the periodicity information, and then it feeds the topology as the second argument of the RectilinearGrid:

# grid topology with given number of elements per dimension
# the last coordinate is periodic so we set (false, false, true)
t = GridTopology((length(r)-1, length(θ)-1, length(ϕ)-1), (false, false, true))

g = RectilinearGrid{𝔼,typeof(Spherical(0,0,0))}((r, θ, ϕ), t)

viz(g)

Notice how the cone is now full-filled compared with the previous one.

3 Likes

I’ve updated the answer to include a variation that you might be interested in.

@juliohm Thank you very much. I will also map Density on these 3D points.

For me first plot would be fine.

You can map colors to every element of the grid with

viz(g, color=1:nelements(g))

Notice the length of the vector. It must match the number of elements in the grid, not the number of vertices. Some viz methods also support colors for every vertex, but I don’t remember if this is the case with RectilinearGrid. You can try and see.

2 Likes

I got this error below. Please update you reply and replace first line with

using Meshes, CoordRefSystems, GLMakie

so that people don’t have misunderstanding while reading your solution later.

julia> g = RectilinearGrid{𝔼,typeof(Spherical(0,0,0))}(r, θ, ϕ)
ERROR: UndefVarError: Spherical not defined in Main
Suggestion: check for spelling errors or missing imports.
Hint: a global variable of this name may be made accessible by importing CoordRefSystems in the current active module Main

1 Like

Sure, just create another RectilinearGrid in the same way with the other coordinates and call viz! instead of viz to plot it in the same figure.

3 Likes

Thanks to you all. I got this plot of Athena++ Black Hole simulation but color of density plot looks not so smooth and real. :hear_no_evil_monkey:


and for alpha = 0.3 it looks

Is there any way to interpolate color to smooth them?