Srivatsan
Srivatsan

Reputation: 9363

Slicing and finding the volume

I have four columns, namely x,y,z,zcosmo. The range of zcosmo is 0.0<zcosmo<0.5.

For each x,y,z, there is a zcosmo.

When x,y,z are plotted, this is how they look. enter image description here

I would like to find the volume of this figure. If I slice it into 50 parts (in ascending zcosmo order), so that each part resembles a cylinder, I can add them up to get the final volume.

The volume of the sliced cylinders would be pi*r^2*h, in my case r = z/2 & h = x

The slicing for example would be like, x,z for 0.0<zcosmo<0.01 find this volume V1. Then x,z for 0.01<zcosmo<0.02 find this volume V2 and so on until zcosmo=0.5

I know to do this manually (which of course is time consuming) by saying:

r1 = z[np.logical_and(zcosmo>0.0,zcosmo<0.01)] / 2 #gives me z within the range 0.0<zcosmo<0.01
h1 = x[np.logical_and(zcosmo>0.0,zcosmo<0.01)] #gives me x within the range 0.0<zcosmo<0.01

V1 = math.pi*(r1**2)*(h1)

Here r1 and h1 should be r1 = ( min(z) + max(z) ) / 2.0 and h1 = max(x) - min(x), i.e the max and min values so that I get one volume for each slice

How should I create a code that calculates the 50 volume slices within the zcosmo sliced ranges??

Upvotes: 1

Views: 484

Answers (1)

Ionut Hulub
Ionut Hulub

Reputation: 6326

Use a for loop:

volumes = list()
for index in range(0, 50):
    r = z[np.logical_and(zcosmo>index * 0.01, zcosmo<index * 0.01 + 0.01)] / 2
    h = x[np.logical_and(zcosmo>index * 0.01, zcosmo<index * 0.01 + 0.01)]

    volumes.append(math.pi*(r**2)*(h))

At the end, volumes will be a list containing the volumes of the 50 cylinders.

You can use volume = sum(volumes) to get the final volume of the shape.

Upvotes: 1

Related Questions