Python Forum
Random coordinate generator speed improvement
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Random coordinate generator speed improvement
#1
I want to generate random coordinates for spheres in a box geometry. I'm using while loop and i have 2 condition. First one is the distance. Coordinates should not be closer to each other than the diameter of the sphere. Second one is the porosity. When the porosity is less than 0.42 generating should stop. Code is working correctly but when i reduce porosity condition less than 0.70 the algorithm stucks but I need 0.42 porosity. It cannot reach that porosity even after hours. How can I improve it to generate coordinates faster? Any suggestions are appreciated.

import math
import random
import numpy as np
import matplotlib.pyplot as plt

#dist = math.sqrt(((x2-x1)**2) + ((y2-y1)**2) + ((z2-z1)**2))

A = 0.04       # x border.
B = 0.04       # y border.
C = 0.125      # z border.
V_total = A*B*C # volume
r = 0.006    # min distance of spheres.
r_square = r**2
radius = 0.003  # radius of spheres.
wall_distance = 0.003
sphere_Val = (4/3) * (np.pi) * (radius**3) # part of porosity formula

Porosity = 1.0
coordinates = np.empty((0,3)) # initialize array with correct shape
while Porosity >= 0.70:
    # coordinates
    x = random.uniform(wall_distance, A-wall_distance)
    y = random.uniform(wall_distance, B-wall_distance)
    z = random.uniform(wall_distance, C-wall_distance)
    is_invalid = (True in [
                                      ((x - coordinates[i_coor,0])**2) + 
                                      ((y - coordinates[i_coor,1])**2) + 
                                      ((z - coordinates[i_coor,2])**2) <= r_square
                            for i_coor in range(coordinates.shape[0]) ])
    if not is_invalid:
        coordinates = np.append(coordinates,[[x,y,z]], axis = 0)
    else:
        continue
    Porosity = (V_total - (sphere_Val * len(coordinates))) / V_total
    print(f"Placed coordinates: {len(coordinates)}, Porosity: = {Porosity}")

print("Porosity: {}".format(Porosity))
print("Number of spheres: {}".format(len(coordinates)))
    
fig = plt.figure()
ax = plt.axes(projection='3d')
ax.set_xlim([0, A])
ax.set_ylim([0, B])
ax.set_zlim([0, C])
ax.set_title('Coordinates for spheres')
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
p = ax.scatter(coordinates[:,0], coordinates[:,1], coordinates[:,2])
np.savetxt('Coordinates.csv', coordinates)
fig.colorbar(p)
plt.show()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Random number generator charlottelol 5 3,139 Nov-10-2020, 10:51 PM
Last Post: deanhystad
  basic random number generator in replace function krug123 2 2,007 Jul-31-2020, 01:02 PM
Last Post: deanhystad
  Help with a random number generator dpcalder 2 2,264 Jun-20-2020, 03:50 AM
Last Post: buran
  Function Improvement Santino 1 1,768 May-23-2020, 03:30 PM
Last Post: jefsummers
  Return draw.rectangle pixel coordinate data to .csv file CephloRhod 0 2,362 May-20-2020, 10:37 AM
Last Post: CephloRhod
  Python module speed or python speed in general Enrique6 1 1,793 May-04-2020, 06:21 PM
Last Post: micseydel
  Name Mashup Program Improvement in Python rhat398 3 2,517 Apr-05-2020, 12:09 PM
Last Post: perfringo
  Can i prevent the random generator to generate already used numbers? MauserMan 3 2,809 Jan-05-2020, 04:44 PM
Last Post: MauserMan
  first try with python class, suggestion for improvement please anna 18 5,733 Nov-01-2019, 11:16 AM
Last Post: anna
  image grid coordinate system Bmart6969 0 1,360 Oct-12-2019, 05:20 AM
Last Post: Bmart6969

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020