Skip to content

Commit

Permalink
Revise RNG Handling (#124)
Browse files Browse the repository at this point in the history
Closes #114:
- Using better seed scheme to minimize chance of RNG collision
- Reference: https://numpy.org/doc/stable/reference/random/parallel.html
  • Loading branch information
rdoddanavar authored Dec 8, 2024
1 parent 6ce237e commit 4d63da0
Show file tree
Hide file tree
Showing 33 changed files with 355 additions and 355 deletions.
18 changes: 9 additions & 9 deletions src/exec/exec.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,16 @@ def run(inputParams: dict, outputPath: pathlib.Path, callback=None) -> None:
callback_parallel = functools.partial(cli_status, progBar)
callback_serial = functools.partial(callback_parallel, None)

# Get RNG seeds
seedMaster = inputParams["exec"]["seed"]["value"]
philox = np.random.Philox(seedMaster)
rng = np.random.Generator(philox)
seedRuns = [int(seed) for seed in rng.integers(2**63,size=numMC)]

if procMode == "serial":

for iRun in range(numMC):
run_sim_mc(inputParams, outputPath, modelData, iRun)
run_sim_mc(inputParams, outputPath, modelData, iRun, seedRuns[iRun])
callback_serial()

elif procMode == "parallel":
Expand All @@ -110,7 +116,7 @@ def run(inputParams: dict, outputPath: pathlib.Path, callback=None) -> None:

# Execute parallel runs
for iRun in range(numMC):
pool.apply_async(run_sim_mc, (inputParams, outputPath, modelData, iRun), callback=callback_parallel)
pool.apply_async(run_sim_mc, (inputParams, outputPath, modelData, iRun, seedRuns[iRun]), callback=callback_parallel)

# Pool cleanup
pool.close()
Expand Down Expand Up @@ -152,17 +158,11 @@ def cli_status(progBar, result):

#------------------------------------------------------------------------------#

def run_sim_mc(inputParams, outputPath, modelData, iRun):
def run_sim_mc(inputParams, outputPath, modelData, iRun, seedRun):

inputParamsMC = copy.deepcopy(inputParams)

seedMaster = inputParams["exec"]["seed"]["value"]
seedRun = seedMaster + iRun

inputParamsMC["exec"]["seed"]["value"] = seedRun

exec_rand.mc_draw(inputParamsMC)

run_sim(inputParamsMC, outputPath, modelData, iRun)

#------------------------------------------------------------------------------#
Expand Down
6 changes: 3 additions & 3 deletions tools/unit_test/ubuntu/run1/input.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# 2024-12-07 10:28:49
# 2024-12-07 14:52:10
# hpr-sim v0.0.0
# Run: 1/5
---
exec:
mcMode:
value: nominal
seed:
value: 0
value: 129745503399974868
numMC:
value: 1
procMode:
Expand All @@ -29,7 +29,7 @@ engine:
value: input/AeroTech_J450DM.eng
mass:
massBody:
value: 4.897012985685388
value: 5.443846838408149
unit: lbm
dist:
name: normal
Expand Down
36 changes: 18 additions & 18 deletions tools/unit_test/ubuntu/run1/stats.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# 2024-12-07 10:28:49
# 2024-12-07 14:52:10
# hpr-sim v0.0.0
# Run: 1/5
---
time:
unit: s
min: 0.000
max: 67.081
max: 62.129
thrust:
unit: N
min: 0.000
Expand All @@ -16,64 +16,64 @@ massEng:
max: 1.223
mass:
unit: kg
min: 2.934
max: 3.444
min: 3.182
max: 3.692
gravity:
unit: m/s^2
min: 9.779
min: 9.781
max: 9.795
temperature:
unit: K
min: 253.641
min: 258.620
max: 288.150
pressure:
unit: Pa
min: 51866.551
min: 57439.551
max: 101325.000
density:
unit: kg/m^3
min: 0.712
min: 0.774
max: 1.225
dynamicPressure:
unit: Pa
min: 0.000
max: 63702.160
max: 54504.676
mach:
unit: null
min: 0.000
max: 0.948
max: 0.877
alphaT:
unit: rad
min: 0.000
max: 0.000
dragCoeff:
unit: null
min: 0.455
max: 0.600
max: 0.522
dragForce:
unit: N
min: 0.000
max: 309.725
max: 230.512
liftForce:
unit: N
min: 0.000
max: 0.000
forceZ:
unit: N
min: -28.741
max: 509.855
min: -31.171
max: 507.425
linAccZ:
unit: m/s^2
min: -9.795
max: 161.351
max: 148.825
linVelZ:
unit: m/s
min: -322.499
max: 311.390
min: -298.327
max: 287.230
linPosZ:
unit: m
min: 0.000
max: 5313.541
max: 4546.311
isBurnout:
unit: null
min: 0.000
Expand Down
Binary file modified tools/unit_test/ubuntu/run1/telem.npy
Binary file not shown.
6 changes: 3 additions & 3 deletions tools/unit_test/ubuntu/run2/input.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# 2024-12-07 10:28:49
# 2024-12-07 14:52:10
# hpr-sim v0.0.0
# Run: 2/5
---
exec:
mcMode:
value: nominal
seed:
value: 1
value: 2377483205311176162
numMC:
value: 1
procMode:
Expand All @@ -29,7 +29,7 @@ engine:
value: input/AeroTech_J450DM.eng
mass:
massBody:
value: 5.280727605592823
value: 5.439992288858991
unit: lbm
dist:
name: normal
Expand Down
38 changes: 19 additions & 19 deletions tools/unit_test/ubuntu/run2/stats.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# 2024-12-07 10:28:49
# 2024-12-07 14:52:10
# hpr-sim v0.0.0
# Run: 2/5
---
time:
unit: s
min: 0.000
max: 63.529
max: 62.161
thrust:
unit: N
min: 0.000
Expand All @@ -16,64 +16,64 @@ massEng:
max: 1.223
mass:
unit: kg
min: 3.108
max: 3.618
min: 3.181
max: 3.691
gravity:
unit: m/s^2
min: 9.780
min: 9.781
max: 9.795
temperature:
unit: K
min: 257.252
min: 258.589
max: 288.150
pressure:
unit: Pa
min: 55861.797
min: 57403.797
max: 101325.000
density:
unit: kg/m^3
min: 0.756
min: 0.773
max: 1.225
dynamicPressure:
unit: Pa
min: 0.000
max: 57030.645
max: 54567.008
mach:
unit: null
min: 0.000
max: 0.897
max: 0.877
alphaT:
unit: rad
min: 0.000
max: 0.000
dragCoeff:
unit: null
min: 0.454
max: 0.524
min: 0.455
max: 0.522
dragForce:
unit: N
min: 0.000
max: 242.123
max: 230.795
liftForce:
unit: N
min: 0.000
max: 0.000
forceZ:
unit: N
min: -30.446
max: 508.150
min: -31.154
max: 507.442
linAccZ:
unit: m/s^2
min: -9.795
max: 152.365
max: 148.906
linVelZ:
unit: m/s
min: -305.161
max: 294.060
min: -298.482
max: 287.384
linPosZ:
unit: m
min: 0.000
max: 4757.155
max: 4551.037
isBurnout:
unit: null
min: 0.000
Expand Down
Binary file modified tools/unit_test/ubuntu/run2/telem.npy
Binary file not shown.
6 changes: 3 additions & 3 deletions tools/unit_test/ubuntu/run3/input.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# 2024-12-07 10:28:49
# 2024-12-07 14:52:10
# hpr-sim v0.0.0
# Run: 3/5
---
exec:
mcMode:
value: nominal
seed:
value: 2
value: 4349422948805191298
numMC:
value: 1
procMode:
Expand All @@ -29,7 +29,7 @@ engine:
value: input/AeroTech_J450DM.eng
mass:
massBody:
value: 4.466468913532913
value: 4.516166478011944
unit: lbm
dist:
name: normal
Expand Down
Loading

0 comments on commit 4d63da0

Please sign in to comment.