From f3a82f454eeae73fe62fe722585bf4f0c58eac99 Mon Sep 17 00:00:00 2001
From: Michele Ceriotti <michele.ceriotti@gmail.com>
Date: Mon, 16 Jan 2017 08:42:23 +0100
Subject: [PATCH] Included a flag to prevent multiple open_socket calls if run
 is included multiple times in the LAMMPS input

---
 src/USER-MISC/fix_ipi.cpp | 10 ++++++++--
 src/USER-MISC/fix_ipi.h   |  2 +-
 2 files changed, 9 insertions(+), 3 deletions(-)

diff --git a/src/USER-MISC/fix_ipi.cpp b/src/USER-MISC/fix_ipi.cpp
index 67c9cc8ee2..2715746139 100644
--- a/src/USER-MISC/fix_ipi.cpp
+++ b/src/USER-MISC/fix_ipi.cpp
@@ -219,6 +219,9 @@ FixIPI::FixIPI(LAMMPS *lmp, int narg, char **arg) :
 
   // create instance of Irregular class
   irregular = new Irregular(lmp);
+  
+  // yet, we have not assigned a socket
+  socketflag = 0;
 }
 
 /* ---------------------------------------------------------------------- */
@@ -248,9 +251,12 @@ int FixIPI::setmask()
 void FixIPI::init()
 {
   //only opens socket on master process
-  if (master) open_socket(ipisock, inet, port, host, error);
-  else ipisock=0;
+  if (master) {
+	if (!socketflag) open_socket(ipisock, inet, port, host, error);
+  } else ipisock=0;
   //! should check for success in socket opening -- but the current open_socket routine dies brutally if unsuccessful
+  // tell lammps we have assigned a socket
+  socketflag = 1;
 
   // asks for evaluation of PE at first step
   modify->compute[modify->find_compute("thermo_pe")]->invoked_scalar = -1;
diff --git a/src/USER-MISC/fix_ipi.h b/src/USER-MISC/fix_ipi.h
index 0bb3717de4..191b6c280d 100644
--- a/src/USER-MISC/fix_ipi.h
+++ b/src/USER-MISC/fix_ipi.h
@@ -35,7 +35,7 @@ class FixIPI : public Fix {
 
  protected:
   char *host; int port; int inet, master, hasdata;
-  int ipisock, me; double *buffer; long bsize;
+  int ipisock, me, socketflag; double *buffer; long bsize;
   int kspace_flag;
   int reset_flag;
 
-- 
GitLab