From 684a12d830e9ba8b20758d993a4c8ca1ef2153d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-No=C3=ABl=20Grad?= Date: Thu, 3 Jun 2021 18:47:57 +0200 Subject: [PATCH] samples: Reduce Gibbs Ensembe code complexity --- samples/gibbs_ensemble/create_fits.py | 11 ++++------ samples/gibbs_ensemble/gibbs_ensemble.py | 3 ++- samples/gibbs_ensemble/run_for_several_kT.sh | 2 +- samples/gibbs_ensemble/run_sim.py | 21 ++++++-------------- 4 files changed, 13 insertions(+), 24 deletions(-) diff --git a/samples/gibbs_ensemble/create_fits.py b/samples/gibbs_ensemble/create_fits.py index 74e1dc922e8..c3468163441 100644 --- a/samples/gibbs_ensemble/create_fits.py +++ b/samples/gibbs_ensemble/create_fits.py @@ -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 = [] @@ -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): @@ -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"] diff --git a/samples/gibbs_ensemble/gibbs_ensemble.py b/samples/gibbs_ensemble/gibbs_ensemble.py index 8a66ee39878..dda9eb6d06f 100644 --- a/samples/gibbs_ensemble/gibbs_ensemble.py +++ b/samples/gibbs_ensemble/gibbs_ensemble.py @@ -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() diff --git a/samples/gibbs_ensemble/run_for_several_kT.sh b/samples/gibbs_ensemble/run_for_several_kT.sh index 5b42ae9e73f..22be134f3bf 100755 --- a/samples/gibbs_ensemble/run_for_several_kT.sh +++ b/samples/gibbs_ensemble/run_for_several_kT.sh @@ -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 diff --git a/samples/gibbs_ensemble/run_sim.py b/samples/gibbs_ensemble/run_sim.py index c004d09e692..b3b8e887132 100644 --- a/samples/gibbs_ensemble/run_sim.py +++ b/samples/gibbs_ensemble/run_sim.py @@ -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 @@ -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) @@ -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)) @@ -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)) @@ -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