From 02b65fe783d47ce04f4d967485c70e691c43e444 Mon Sep 17 00:00:00 2001
From: Chris Maffeo <cmaffeo2@illinois.edu>
Date: Wed, 9 Oct 2019 12:19:56 -0500
Subject: [PATCH] Brought in changes from mrdna package to arbdmodel.py from
 commits 8dafa9f..205bb86

---
 arbdmodel/__init__.py | 27 +++++++++++++++++++++------
 1 file changed, 21 insertions(+), 6 deletions(-)

diff --git a/arbdmodel/__init__.py b/arbdmodel/__init__.py
index 7e66eab..c49ad1c 100644
--- a/arbdmodel/__init__.py
+++ b/arbdmodel/__init__.py
@@ -99,17 +99,21 @@ class Parent():
             x.parent = None
 
     def add_bond(self, i,j, bond, exclude=False):
+        assert( i is not j )
         ## TODO: how to handle duplicating and cloning bonds
         # beads = [b for b in self]
         # for b in (i,j): assert(b in beads)
         self.bonds.append( (i,j, bond, exclude) )
 
     def add_angle(self, i,j,k, angle):
+        assert( len(set((i,j,k))) == 3 )
         # beads = [b for b in self]
         # for b in (i,j,k): assert(b in beads)
         self.angles.append( (i,j,k, angle) )
 
     def add_dihedral(self, i,j,k,l, dihedral):
+        assert( len(set((i,j,k,l))) == 4 )
+
         # beads = [b for b in self]
         # for b in (i,j,k,l): assert(b in beads)
         self.dihedrals.append( (i,j,k,l, dihedral) )
@@ -326,6 +330,7 @@ class ParticleType():
     def __hash_key(self):
         l = [self.name,self.charge]
         for keyval in sorted(self.__dict__.items()):
+            if isinstance(keyval[1], list): keyval = (keyval[0],tuple(keyval[1]))
             l.extend(keyval)
         return tuple(l)
 
@@ -622,7 +627,7 @@ class PdbModel(Transformable, Parent):
 
 
 class ArbdModel(PdbModel):
-    def __init__(self, children, dimensions=(1000,1000,1000), temperature=291, timestep=50e-6, cutoff=50, decompPeriod=10000, pairlistDistance=None, nonbondedResolution=0.1, remove_duplicate_bonded_terms=True):
+    def __init__(self, children, dimensions=(1000,1000,1000), temperature=291, timestep=50e-6, cutoff=50, decompPeriod=10000, pairlistDistance=None, nonbondedResolution=0.1, remove_duplicate_bonded_terms=True, extra_bd_file_lines=""):
         PdbModel.__init__(self, children, dimensions, remove_duplicate_bonded_terms)
         self.temperature = temperature
 
@@ -635,6 +640,8 @@ class ArbdModel(PdbModel):
         self.decompPeriod = decompPeriod
         self.pairlistDistance = pairlistDistance
 
+        self.extra_bd_file_lines = extra_bd_file_lines
+
         self.numParticles = 0
         self.particles = []
         self.type_counts = None
@@ -697,7 +704,7 @@ class ArbdModel(PdbModel):
         if typeA != typeB:
             self.nbSchemes.append( (nbScheme, typeB, typeA) )
 
-    def simulate(self, output_name, output_directory='output', num_steps=100000000, timestep=None, gpu=0, output_period=1e4, arbd=None, directory='.', replicas=1):
+    def simulate(self, output_name, output_directory='output', num_steps=100000000, timestep=None, gpu=0, output_period=1e4, arbd=None, directory='.', replicas=1, log_file=None):
         assert(type(gpu) is int)
         num_steps = int(num_steps)
 
@@ -753,10 +760,16 @@ class ArbdModel(PdbModel):
             cmd = tuple(str(x) for x in cmd)
 
             print("Running ARBD with: %s" % " ".join(cmd))
-            process = subprocess.Popen(cmd, stdout=subprocess.PIPE, universal_newlines=True)
-            for line in process.stdout:
-                sys.stdout.write(line)
-                sys.stdout.flush()
+            if log_file is None or (hasattr(log_file,'write') and callable(log_file.write)):
+                fd = sys.stdout if log_file is None else log_file
+                process = subprocess.Popen(cmd, stdout=subprocess.PIPE, universal_newlines=True)
+                for line in process.stdout:
+                    fd.write(line)
+                    fd.flush()
+            else:
+                with open(log_file,'w') as fd:
+                    process = subprocess.Popen(cmd, stdout=log_file, universal_newlines=True)
+                    process.communicate()
 
         except:
             raise
@@ -851,6 +864,8 @@ pairlistDistance {pairlistDistance}
 
 origin {originX} {originY} {originZ}
 systemSize {dimX} {dimY} {dimZ}
+
+{extra_bd_file_lines}
 \n""".format(**params))
             
             ## Write entries for each type of particle
-- 
GitLab