Skip to content

Commit

Permalink
samples: Reduce Gibbs Ensembe code complexity
Browse files Browse the repository at this point in the history
  • Loading branch information
jngrad committed Jun 3, 2021
1 parent 7fe8f51 commit 684a12d
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 24 deletions.
11 changes: 4 additions & 7 deletions samples/gibbs_ensemble/create_fits.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
)

# Warmup length to skip at beginning
WARMUP_LENGTH = int(3e3)
WARMUP_LENGTH = 3000

# Lists for (unsorted) densities and temperature
dens_liquid = []
Expand All @@ -69,7 +69,7 @@ def autocorr(x):
""" Compute the normalized autocorrelation function """
_x = x - np.mean(x)
return (signal.convolve(
_x, _x[::-1], mode='full', method='auto')[(np.size(_x) - 1):]) / (np.sum(_x * _x))
_x, _x[::-1], mode='full', method='auto')[(np.size(_x) - 1):]) / np.sum(_x * _x)


def relaxation_time(x):
Expand Down Expand Up @@ -101,11 +101,8 @@ def rectilinear_law(kT, p_c, A, kT_c):
for f in args.files:

logging.info("Loading {}...".format(f))
try:
with gzip.open(f, 'rb') as _f:
data = pickle.load(_f)
except FileNotFoundError:
raise FileNotFoundError("File {} not found.".format(f))
with gzip.open(f, 'rb') as _f:
data = pickle.load(_f)

kT = data["temperature"]

Expand Down
3 changes: 2 additions & 1 deletion samples/gibbs_ensemble/gibbs_ensemble.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,9 @@ def run(self):
MessageID.END: self.end_simulation
}

# callback loop, we can simply terminate the process
# via process.terminate() in the parent process
while True:
""" Can be left at while True, we can simply terminate the process via process.terminate() in the parent process """
# Catch errors during handling
msg = self.pipe.recv()

Expand Down
2 changes: 1 addition & 1 deletion samples/gibbs_ensemble/run_for_several_kT.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pypresso=../../build/pypresso

if [ ! -e "${pypresso}" ]
then
echo Invalid path to pypresso script: $pypresso
echo "Invalid path to pypresso script: ${pypresso}" 1>&2
exit 1
fi

Expand Down
21 changes: 6 additions & 15 deletions samples/gibbs_ensemble/run_sim.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,7 @@ class Moves(Enum):

# Global number of particles
GLOBAL_NUM_PART = args.N
try:
assert((GLOBAL_NUM_PART % 2) == 0)
except AssertionError:
raise TypeError("Wrong number of particles, must be even")
assert (GLOBAL_NUM_PART % 2) == 0, "The number of particles must be even"

# Init particle density
init_density = args.density
Expand Down Expand Up @@ -189,8 +186,7 @@ def diff_energy(self):
return np.clip((self.energy - self.old_energy), -100 * kT, 100 * kT)

def send_data(self, data):
# For debugging
#assert(type(data) == list)
assert isinstance(data, list)
logging.debug("Send to {}: {}".format(self.box_name, data))
self.pipe.send(data)

Expand All @@ -202,9 +198,7 @@ def recv_data(self):
def recv_energy(self):
msg = self.recv_data()
# Debugging
try:
assert(msg[0] == MessageID.ENERGY)
except BaseException:
if msg[0] != MessageID.ENERGY:
raise ConnectionError(
"Error during energy return of {}, got this instead: \n{}".format(
self.box_name, msg))
Expand All @@ -217,9 +211,7 @@ def validate_info(boxes):
[box.send_data([MessageID.INFO]) for box in boxes]
for box in boxes:
msg = box.pipe.recv()
try:
assert(msg[0] == MessageID.INFO)
except BaseException:
if msg[0] != MessageID.INFO:
raise ConnectionError(
"Connection to {} seems to be broken.".format(
box.box_name))
Expand All @@ -239,10 +231,9 @@ def validate_info(boxes):
def choose_random_box(boxes):
""" Choose a box at random (Assumes 2 Clients) """
random_int = np.random.randint(2)
try:
assert(boxes[random_int].num_part > 0)
if boxes[random_int].num_part > 0:
return random_int
except BaseException:
else:
return (random_int + 1) % 2


Expand Down

0 comments on commit 684a12d

Please sign in to comment.