added vornoi plotting routine

This commit is contained in:
Paul M. Sutter 2024-06-08 16:18:33 +01:00
parent 7f3afca2d7
commit 62dd66be79

View file

@ -17,13 +17,16 @@
# with this program; if not, write to the Free Software Foundation, Inc., # with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#+ #+
__all__=['plotNumberFunction','plotEllipDist','plotVoidCells'] __all__=['plotNumberFunction','plotEllipDist','plotVoidCells','plotVoronoi']
from backend.classes import * from backend.classes import *
from .plotDefs import * from .plotDefs import *
import numpy as np import numpy as np
import os import os
import pylab as plt import pylab as plt
from scipy.spatial import ConvexHull
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
rng = np.random.default_rng(11)
import backend.cosmologyTools as vp import backend.cosmologyTools as vp
import backend.surveyTools as sp import backend.surveyTools as sp
from voidUtil import getArray, shiftPart, getVoidPart from voidUtil import getArray, shiftPart, getVoidPart
@ -298,3 +301,87 @@ def plotVoidCells(catalog,
plt.savefig(figDir+"/fig_"+plotName+".png", bbox_inches="tight") plt.savefig(figDir+"/fig_"+plotName+".png", bbox_inches="tight")
return return
# -----------------------------------------------------------------------------
def plotVoronoi(catalog,
voidID,
figDir="./",
plotName="voronoi",
plotCenter = (0,0,0),
plotWidth = (100,100,25),
sliceWidth=250):
# plots the voronoi cells belonging to a void (or a slice if no void given)
# catalog: void catalog
# voidID: ID of void to plot (-1 to use all particles)
# figDir: output directory for figures
# plotName: name to prefix to all outputs
print("Plotting voronoi cells...")
sample = catalog.sampleInfo
plt.clf()
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
print(" Selecting particles...")
if (voidID == -1):
xmin = plotCenter[0]-plotWidth[0]/2.
xmax = plotCenter[0]+plotWidth[0]/2.
ymin = plotCenter[1]-plotWidth[1]/2.
ymax = plotCenter[1]+plotWidth[1]/2.
zmin = plotCenter[2]-plotWidth[2]/2.
zmax = plotCenter[2]+plotWidth[2]/2.
partList = []
for p in catalog.part:
#print(p.x, p.y, p.z)
#print(xmin,xmax,ymin,ymax,zmin,zmax)
if (p.x >= xmin and p.x <= xmax and \
p.y >= ymin and p.y <= ymax and \
p.z >= zmin and p.z <= zmax):
partList.append(p)
#ax.set_xlim(xmin,xmax)
#ax.set_ylim(ymin,ymax)
#ax_set.zlim(zmin,zmax)
else:
partList = getVoidPart(catalog, voidID)
plotCenter = catalog.voids[voidID].macrocenter
plotRadius = catalog.voids[voidID].radius
xmin = plotCenter[0]-plotRadius
xmax = plotCenter[0]+plotRadius
ymin = plotCenter[1]-plotRadius
ymax = plotCenter[1]+plotRadius
zmin = plotCenter[2]-plotRadius
zmax = plotCenter[2]+plotRadius
#ax.set_xlim(xmin,xmax)
#ax.set_ylim(ymin,ymax)
#ax_set_zlim(zmin,zmax)
print(" Working with %d particles..." % len(partList))
print(" Computing convex hulls and creating polygons...")
for part in partList:
adjsPart = []
for adj in part.adjs:
adjsPart.append(catalog.partPos[adj])
hull = ConvexHull(adjsPart)
polygon = Poly3DCollection(hull.points[hull.simplices], alpha=0.25,
facecolors=rng.uniform(0,1,3),
linewidths=0.01, edgecolors='gray')
ax.add_collection3d(polygon)
ax.scatter(part.x, part.y, part.z, s=0.01, color='black')
print(" Saving...")
plt.savefig(figDir+"/fig_"+plotName+".pdf", bbox_inches="tight")
plt.savefig(figDir+"/fig_"+plotName+".eps", bbox_inches="tight")
plt.savefig(figDir+"/fig_"+plotName+".png", bbox_inches="tight")
print(" Done!")
return