Skip to content

Commit

Permalink
Implement defining and storing a separate PRNG for the random rotations
Browse files Browse the repository at this point in the history
  • Loading branch information
ceriottm committed Nov 30, 2024
1 parent 7702288 commit 4318ae3
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 16 deletions.
3 changes: 2 additions & 1 deletion examples/features/o3_averaging/random_liquid_water/input.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
<ffsocket name='base-noo3' mode='unix' pbc='false'>
<address>h2o-noo3</address>
</ffsocket>
<random> True </random>
<random> True </random>
<prng> 54321 </prng>
</ffrotations>
<system>
<initialize nbeads='1'>
Expand Down
43 changes: 28 additions & 15 deletions ipi/engine/forcefields.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,10 @@ def _poll_loop(self):
finished.
"""

info(f" @ForceField ({self.name}): Starting the polling thread main loop.", verbosity.low)
info(
f" @ForceField ({self.name}): Starting the polling thread main loop.",
verbosity.low,
)
while self._doloop[0]:
time.sleep(self.latency)
if len(self.requests) > 0:
Expand All @@ -242,7 +245,7 @@ def release(self, request, lock=True):
Args:
request: The id of the job to release.
lock: whether we should apply a threadlock here
lock: whether we should apply a threadlock here
"""

"""Frees up a request."""
Expand Down Expand Up @@ -429,7 +432,7 @@ def __init__(
super().__init__(latency, offset, name, pars, dopbc, active, threaded)

if pars is None:
pars = {} # defaults no pars
pars = {} # defaults no pars
self.pes = pes
try:
self.driver = __drivers__[self.pes](verbose=verbosity.high, **pars)
Expand Down Expand Up @@ -1170,7 +1173,7 @@ def __init__(
pars=pars,
dopbc=dopbc,
active=active,
threaded=True, # hardcoded, otherwise won't work!
threaded=True, # hardcoded, otherwise won't work!
)
if len(fflist) == 0:
raise ValueError(
Expand Down Expand Up @@ -1467,30 +1470,40 @@ def __init__(
dopbc=True,
active=np.array([-1]),
threaded=True,
ffsocket = None,
ffdirect = None,
prng=None,
ffsocket=None,
ffdirect=None,
random=False,
inversion=False,
grid_order=1,
grid_mode="lebedev",
):
super(FFRotations, self).__init__( # force threaded execution to handle sub-ffield
latency, offset, name, pars, dopbc, active, threaded=True
super(
FFRotations, self
).__init__( # force threaded execution to handle sub-ffield
latency, offset, name, pars, dopbc, active, threaded=True
)

# TODO: initialize and save (probably better to use different PRNG than the one from the simulation)
self.prng = Random()
if prng is None:
warning("No PRNG provided, will initialize one", verbosity.low)
self.prng = Random()
else:
self.prng = prng
self.ffsocket = ffsocket
self.ffdirect = ffdirect
if ffsocket is None or self.ffsocket.name == "__DUMMY__":
if ffdirect is None or self.ffdirect.name == "__DUMMY__":
raise ValueError("Must specify a non-default value for either `ffsocket` or `ffdirect` into `ffrotations`")
raise ValueError(
"Must specify a non-default value for either `ffsocket` or `ffdirect` into `ffrotations`"
)
else:
self.ff=self.ffdirect
self.ff = self.ffdirect
elif ffdirect is None or self.ffdirect.name == "__DUMMY__":
self.ff = self.ffsocket
else:
raise ValueError("Cannot specify both `ffsocket` and `ffdirect` into `ffrotations`")
raise ValueError(
"Cannot specify both `ffsocket` and `ffdirect` into `ffrotations`"
)

self.random = random
self.inversion = inversion
Expand Down Expand Up @@ -1632,12 +1645,12 @@ def gather(self, r):

for ff_r in r["ff_handles"]:
self.ff.release(ff_r)

def poll(self):
"""Polls the forcefield object to check if it has finished."""

with self._threadlock:
for r in self.requests:
for r in self.requests:
if "ff_handles" in r and r["status"] != "Done" and self.check_finish(r):
r["t_finished"] = time.time()
self.gather(r)
Expand Down
12 changes: 12 additions & 0 deletions ipi/inputs/forcefields.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
from ipi.inputs.initializer import *
from ipi.utils.inputvalue import *
from ipi.utils.messages import verbosity, warning
from ipi.utils.prng import Random
from ipi.inputs.prng import InputRandom

__all__ = [
"InputFFSocket",
Expand Down Expand Up @@ -980,6 +982,14 @@ class InputFFRotations(InputForceField):
},
)

fields["prng"] = (
InputRandom,
{
"help": InputRandom.default_help,
"default": input_default(factory=Random),
},
)

fields["ffsocket"] = (
InputFFSocket,
{
Expand All @@ -1004,6 +1014,7 @@ def store(self, ff):
self.grid_order.store(ff.grid_order)
self.grid_mode.store(ff.grid_mode)
self.random.store(ff.random)
self.prng.store(ff.prng)
self.ffsocket.store(ff.ffsocket)
self.ffdirect.store(ff.ffdirect)

Expand All @@ -1018,6 +1029,7 @@ def fetch(self):
dopbc=self.pbc.fetch(),
active=self.activelist.fetch(),
threaded=self.threaded.fetch(),
prng=self.prng.fetch(),
ffsocket=self.ffsocket.fetch(),
ffdirect=self.ffdirect.fetch(),
grid_order=self.grid_order.fetch(),
Expand Down

0 comments on commit 4318ae3

Please sign in to comment.