Skip to content

Commit

Permalink
Cleaning up how we generate random numbers in our testing
Browse files Browse the repository at this point in the history
  • Loading branch information
jmikeowen committed Oct 8, 2024
1 parent 2e2a148 commit 5a40fec
Show file tree
Hide file tree
Showing 50 changed files with 289 additions and 569 deletions.
5 changes: 2 additions & 3 deletions src/NodeGenerators/InteriorGenerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from Spheral import Vector2d, Tensor2d, SymTensor2d

import random
rangen = random.Random()

#-------------------------------------------------------------------------------
# 2D
Expand All @@ -35,8 +34,8 @@ def __init__(self,
self.x, self.y = [], []
for iy in range(ny):
for ix in range(nx):
posi = Vector2d(xmin.x + (ix + 0.5 + jitter*rangen.uniform(0,1))*dx,
xmin.y + (iy + 0.5 + jitter*rangen.uniform(0,1))*dx)
posi = Vector2d(xmin.x + (ix + 0.5 + jitter*random.uniform(0,1))*dx,
xmin.y + (iy + 0.5 + jitter*random.uniform(0,1))*dx)
if boundary.contains(posi):
self.x.append(posi.x)
self.y.append(posi.y)
Expand Down
5 changes: 2 additions & 3 deletions tests/functional/Generators/centroidalRelaxation-1d.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ def gradrhofunc(posi):
# Create a random number generator.
#-------------------------------------------------------------------------------
import random
rangen = random.Random()
rangen.seed(seed)
random.seed(seed)

#-------------------------------------------------------------------------------
# Material properties.
Expand Down Expand Up @@ -100,7 +99,7 @@ def gradrhofunc(posi):
dx = (x1 - x0)/nx
pos = nodes.positions()
for i in range(nodes.numInternalNodes):
pos[i].x += ranfrac * dx * rangen.uniform(-1.0, 1.0)
pos[i].x += ranfrac * dx * random.uniform(-1.0, 1.0)

# Initialize the mass and densities.
m = nodes.mass()
Expand Down
7 changes: 3 additions & 4 deletions tests/functional/Generators/centroidalRelaxation-2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ def gradrhofunc(posi):
# Create a random number generator.
#-------------------------------------------------------------------------------
import random
rangen = random.Random()
rangen.seed(seed)
random.seed(seed)

#-------------------------------------------------------------------------------
# Material properties.
Expand Down Expand Up @@ -120,8 +119,8 @@ def gradrhofunc(posi):
dy = (y1 - y0)/ny
pos = nodes.positions()
for i in range(nodes.numInternalNodes):
pos[i].x += ranfrac * dx * rangen.uniform(-1.0, 1.0)
pos[i].y += ranfrac * dy * rangen.uniform(-1.0, 1.0)
pos[i].x += ranfrac * dx * random.uniform(-1.0, 1.0)
pos[i].y += ranfrac * dy * random.uniform(-1.0, 1.0)

# Initialize the mass and densities.
m = nodes.mass()
Expand Down
9 changes: 4 additions & 5 deletions tests/functional/Generators/centroidalRelaxation-3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ def gradrhofunc(posi):
# Create a random number generator.
#-------------------------------------------------------------------------------
import random
rangen = random.Random()
rangen.seed(seed)
random.seed(seed)

#-------------------------------------------------------------------------------
# Material properties.
Expand Down Expand Up @@ -120,9 +119,9 @@ def gradrhofunc(posi):
dz = (z1 - z0)/nz
pos = nodes.positions()
for i in range(nodes.numInternalNodes):
pos[i].x += ranfrac * dx * rangen.uniform(-1.0, 1.0)
pos[i].y += ranfrac * dy * rangen.uniform(-1.0, 1.0)
pos[i].z += ranfrac * dz * rangen.uniform(-1.0, 1.0)
pos[i].x += ranfrac * dx * random.uniform(-1.0, 1.0)
pos[i].y += ranfrac * dy * random.uniform(-1.0, 1.0)
pos[i].z += ranfrac * dz * random.uniform(-1.0, 1.0)

# Initialize the mass and densities.
m = nodes.mass()
Expand Down
15 changes: 7 additions & 8 deletions tests/functional/RK/RKInterpolation.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,7 @@
#-------------------------------------------------------------------------------
import random
seed = 459297849234
rangen = random.Random()
rangen.seed(seed)
random.seed(seed)

if randomizeNodes:
dx = (x1 - x0)/nx
Expand All @@ -243,14 +242,14 @@
pos = nodes.positions()
for i in range(nodes.numInternalNodes):
if dimension == 1:
pos[i].x += ranfrac * dx * rangen.uniform(-1.0, 1.0)
pos[i].x += ranfrac * dx * random.uniform(-1.0, 1.0)
elif dimension == 2:
pos[i].x += ranfrac * dx * rangen.uniform(-1.0, 1.0)
pos[i].y += ranfrac * dy * rangen.uniform(-1.0, 1.0)
pos[i].x += ranfrac * dx * random.uniform(-1.0, 1.0)
pos[i].y += ranfrac * dy * random.uniform(-1.0, 1.0)
elif dimension == 3:
pos[i].x += ranfrac * dx * rangen.uniform(-1.0, 1.0)
pos[i].y += ranfrac * dy * rangen.uniform(-1.0, 1.0)
pos[i].z += ranfrac * dz * rangen.uniform(-1.0, 1.0)
pos[i].x += ranfrac * dx * random.uniform(-1.0, 1.0)
pos[i].y += ranfrac * dy * random.uniform(-1.0, 1.0)
pos[i].z += ranfrac * dz * random.uniform(-1.0, 1.0)

#-------------------------------------------------------------------------------
# Iterate h
Expand Down
17 changes: 8 additions & 9 deletions tests/functional/RK/testRKIntegrals.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,9 +270,8 @@
# Randomize nodes
#-------------------------------------------------------------------------------
import random
seed = 2
rangen = random.Random()
rangen.seed(seed)
seed = 4898201204
random.seed(seed)

if randomizeNodes:
print("randomizing nodes")
Expand All @@ -282,14 +281,14 @@
pos = nodes.positions()
for i in range(nodes.numInternalNodes):
if dimension == 1:
pos[i].x += ranfrac * dx * rangen.uniform(-1.0, 1.0)
pos[i].x += ranfrac * dx * random.uniform(-1.0, 1.0)
elif dimension == 2:
pos[i].x += ranfrac * dx * rangen.uniform(-1.0, 1.0)
pos[i].y += ranfrac * dy * rangen.uniform(-1.0, 1.0)
pos[i].x += ranfrac * dx * random.uniform(-1.0, 1.0)
pos[i].y += ranfrac * dy * random.uniform(-1.0, 1.0)
elif dimension == 3:
pos[i].x += ranfrac * dx * rangen.uniform(-1.0, 1.0)
pos[i].y += ranfrac * dy * rangen.uniform(-1.0, 1.0)
pos[i].z += ranfrac * dz * rangen.uniform(-1.0, 1.0)
pos[i].x += ranfrac * dx * random.uniform(-1.0, 1.0)
pos[i].y += ranfrac * dy * random.uniform(-1.0, 1.0)
pos[i].z += ranfrac * dz * random.uniform(-1.0, 1.0)

#-------------------------------------------------------------------------------
# Iterate h
Expand Down
5 changes: 2 additions & 3 deletions tests/functional/RK/testVoronoiVolume.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@
# Create a random number generator.
#-------------------------------------------------------------------------------
import random
rangen = random.Random()
rangen.seed(seed)
random.seed(seed)

#-------------------------------------------------------------------------------
# Material properties.
Expand Down Expand Up @@ -127,7 +126,7 @@
#-------------------------------------------------------------------------------
dx = (x1 - x0)/nx1
for i in range(nodes1.numInternalNodes):
nodes1.positions()[i].x += ranfrac * dx * rangen.uniform(-1.0, 1.0)
nodes1.positions()[i].x += ranfrac * dx * random.uniform(-1.0, 1.0)

#-------------------------------------------------------------------------------
# Construct a DataBase to hold our node list
Expand Down
3 changes: 1 addition & 2 deletions tests/unit/CRKSPH/testCRKSPHHourglass-1d.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,6 @@ def __call__(self, x):
# Set the node positions, velocities, and densities.
dx = 1.0/nx1
import random
rangen = random.Random()
from newtonRaphson import *
cs = sqrt(cs2)
pos = nodes1.positions()
Expand All @@ -170,7 +169,7 @@ def __call__(self, x):
xi0 = newtonRaphsonFindRoot(func0, 0.0, 1.0, 1.0e-15, 1.0e-15)
xi1 = newtonRaphsonFindRoot(func1, 0.0, 1.0, 1.0e-15, 1.0e-15)
xi = x0 + (x1 - x0)*0.5*(xi0 + xi1)
pos[i].x = xi + ranfrac*dx*rangen.uniform(-1.0, 1.0)
pos[i].x = xi + ranfrac*dx*random.uniform(-1.0, 1.0)
vel[i].x = 0.5*(A*cs*sin(twopi*kfreq*(xi0 - x0)/(x1 - x0)) +
A*cs*sin(twopi*kfreq*(xi1 - x0)/(x1 - x0)))
rho[i] = rho1*0.5*((1.0 + A*sin(twopi*kfreq*(xi0 - x0)/(x1 - x0))) +
Expand Down
5 changes: 2 additions & 3 deletions tests/unit/CRKSPH/testCRKSPHSumDensity.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,7 @@
# Create a random number generator.
#-------------------------------------------------------------------------------
import random
rangen = random.Random()
rangen.seed(seed)
random.seed(seed)

#-------------------------------------------------------------------------------
# Material properties.
Expand Down Expand Up @@ -159,7 +158,7 @@
dx = dx1
else:
dx = dx2
nodes1.positions()[i].x += ranfrac * dx * rangen.uniform(-1.0, 1.0)
nodes1.positions()[i].x += ranfrac * dx * random.uniform(-1.0, 1.0)

#-------------------------------------------------------------------------------
# Construct a DataBase to hold our node list
Expand Down
15 changes: 7 additions & 8 deletions tests/unit/CRKSPH/testConsistency.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,7 @@
# Create a random number generator.
#-------------------------------------------------------------------------------
import random
rangen = random.Random()
rangen.seed(seed)
random.seed(seed)

#-------------------------------------------------------------------------------
# Material properties.
Expand Down Expand Up @@ -226,17 +225,17 @@
else:
dx = dx2
if testDim == "1d":
pos[i].x += ranfrac * dx * rangen.uniform(-1.0, 1.0)
pos[i].x += ranfrac * dx * random.uniform(-1.0, 1.0)
#pos[i].x = rposx[i]
elif testDim == "2d":
pos[i].x += ranfrac * dx * rangen.uniform(-1.0, 1.0)
pos[i].y += ranfrac * dy * rangen.uniform(-1.0, 1.0)
pos[i].x += ranfrac * dx * random.uniform(-1.0, 1.0)
pos[i].y += ranfrac * dy * random.uniform(-1.0, 1.0)
#pos[i].x = rposx[i]
#pos[i].y = rposy[i]
elif testDim == "3d":
pos[i].x += ranfrac * dx * rangen.uniform(-1.0, 1.0)
pos[i].y += ranfrac * dy * rangen.uniform(-1.0, 1.0)
pos[i].z += ranfrac * dz * rangen.uniform(-1.0, 1.0)
pos[i].x += ranfrac * dx * random.uniform(-1.0, 1.0)
pos[i].y += ranfrac * dy * random.uniform(-1.0, 1.0)
pos[i].z += ranfrac * dz * random.uniform(-1.0, 1.0)
#pos[i].x = rposx[i]
#pos[i].y = rposy[i]
#pos[i].z = rposz[i]
Expand Down
5 changes: 2 additions & 3 deletions tests/unit/CRKSPH/testDamagedInterpolation.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,7 @@
# Create a random number generator.
#-------------------------------------------------------------------------------
import random
rangen = random.Random()
rangen.seed(seed)
random.seed(seed)

#-------------------------------------------------------------------------------
# Material properties.
Expand Down Expand Up @@ -180,7 +179,7 @@
dx = dx1
else:
dx = dx2
nodes1.positions()[i].x += ranfrac * dx * rangen.uniform(-1.0, 1.0)
nodes1.positions()[i].x += ranfrac * dx * random.uniform(-1.0, 1.0)

#-------------------------------------------------------------------------------
# Construct a DataBase to hold our node list
Expand Down
15 changes: 7 additions & 8 deletions tests/unit/CRKSPH/testInterpolation.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,7 @@
# Create a random number generator.
#-------------------------------------------------------------------------------
import random
rangen = random.Random()
rangen.seed(seed)
random.seed(seed)

#-------------------------------------------------------------------------------
# Material properties.
Expand Down Expand Up @@ -191,14 +190,14 @@
pos = nodes.positions()
for i in range(nodes.numInternalNodes):
if testDim == "1d":
pos[i].x += ranfrac * dx * rangen.uniform(-1.0, 1.0)
pos[i].x += ranfrac * dx * random.uniform(-1.0, 1.0)
elif testDim == "2d":
pos[i].x += ranfrac * dx * rangen.uniform(-1.0, 1.0)
pos[i].y += ranfrac * dy * rangen.uniform(-1.0, 1.0)
pos[i].x += ranfrac * dx * random.uniform(-1.0, 1.0)
pos[i].y += ranfrac * dy * random.uniform(-1.0, 1.0)
elif testDim == "3d":
pos[i].x += ranfrac * dx * rangen.uniform(-1.0, 1.0)
pos[i].y += ranfrac * dy * rangen.uniform(-1.0, 1.0)
pos[i].z += ranfrac * dz * rangen.uniform(-1.0, 1.0)
pos[i].x += ranfrac * dx * random.uniform(-1.0, 1.0)
pos[i].y += ranfrac * dy * random.uniform(-1.0, 1.0)
pos[i].z += ranfrac * dz * random.uniform(-1.0, 1.0)

#-------------------------------------------------------------------------------
# Construct a DataBase to hold our node list
Expand Down
19 changes: 9 additions & 10 deletions tests/unit/Geometry/testEigen2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
# Create a global random number generator.
import random
random.seed(37549927891710)
rangen = random.Random()
ranrange = 1.0e8

#===============================================================================
Expand All @@ -31,12 +30,12 @@ def randomSymTensor2d(lam1 = None,
lam2 = None):

if lam1 is None:
lam1 = rangen.uniform(-ranrange, ranrange)
lam1 = random.uniform(-ranrange, ranrange)
if lam2 is None:
lam2 = rangen.uniform(-ranrange, ranrange)
lam2 = random.uniform(-ranrange, ranrange)

# Pick random Euler angles.
theta = rangen.uniform(0.0, 2.0*pi)
theta = random.uniform(0.0, 2.0*pi)

# Build the rotation matrix of eigen vectors to rotate from the principle to
# the lab frame (so transpose of what we usually mean)
Expand Down Expand Up @@ -99,7 +98,7 @@ def testRandomEigenValues(self):
#---------------------------------------------------------------------------
def testDoublyDegenerateEigenValues(self):
for i in range(self.ntests):
lam12 = rangen.uniform(-ranrange, ranrange)
lam12 = random.uniform(-ranrange, ranrange)
A, vlam0, vectors0 = randomSymTensor2d(lam1 = lam12,
lam2 = lam12)
lam0 = [x for x in vlam0]
Expand All @@ -117,8 +116,8 @@ def testDoublyDegenerateEigenValues(self):
#---------------------------------------------------------------------------
def testDiagonalEigenValues(self):
for i in range(self.ntests):
lam1 = rangen.uniform(-ranrange, ranrange)
lam2 = rangen.uniform(-ranrange, ranrange)
lam1 = random.uniform(-ranrange, ranrange)
lam2 = random.uniform(-ranrange, ranrange)
A = SymTensor2d(lam1, 0.0,
0.0, lam2)
lam0 = [lam1, lam2]
Expand Down Expand Up @@ -159,7 +158,7 @@ def testRandomEigenVectors(self):
#---------------------------------------------------------------------------
def testDoublyDegenerateEigenVectors(self):
for i in range(self.ntests):
lam12 = rangen.uniform(-ranrange, ranrange)
lam12 = random.uniform(-ranrange, ranrange)
A, vlam0, vectors0 = randomSymTensor2d(lam1 = lam12,
lam2 = lam12)
lam0 = [(vlam0(i), vectors0.getColumn(i)) for i in range(2)]
Expand Down Expand Up @@ -188,8 +187,8 @@ def testDoublyDegenerateEigenVectors(self):
#---------------------------------------------------------------------------
def testDiagonalEigenVectors(self):
for i in range(self.ntests):
lam1 = rangen.uniform(-ranrange, ranrange)
lam2 = rangen.uniform(-ranrange, ranrange)
lam1 = random.uniform(-ranrange, ranrange)
lam2 = random.uniform(-ranrange, ranrange)
A = SymTensor2d(lam1, 0.0,
0.0, lam2)
lam0 = [(lam1, Vector2d(1, 0)),
Expand Down
Loading

0 comments on commit 5a40fec

Please sign in to comment.