diff --git a/tools/make_mat/make_mat.py b/tools/make_mat/make_mat.py index a2748376..4c1c7056 100644 --- a/tools/make_mat/make_mat.py +++ b/tools/make_mat/make_mat.py @@ -1,5 +1,7 @@ import os +import re import sys +import tempfile sys.path.append(os.path.join(os.path.dirname(__file__), "..", "..", "src")) @@ -296,7 +298,9 @@ def read_topologies(mego_top, target_top): """ Reads the input topologies using parmed. Ignores warnings to prevent printing of GromacsWarnings regarding 1-4 interactions commonly seen when using - parmed in combination with multi-eGO topologies. + parmed in combination with multi-eGO topologies. In the case of the reference + topology, the last atom number is changed to 1 to prevent parmed from allocating + unnecessary memory. Parameters ---------- @@ -313,7 +317,31 @@ def read_topologies(mego_top, target_top): print(f"ERROR {e} in read_topologies while reading {mego_top}") exit(1) try: - topology_ref = pmd.load_file(target_top) + dirname, basename = os.path.split(target_top) + temp_ref = tempfile.NamedTemporaryFile(prefix=basename, dir=dirname) + temp_ref.write(open(target_top, "rb").read()) + temp_ref.seek(0) + molecules_tag = False + with open(temp_ref.name, "r") as f: + lines = f.readlines() + lines = [x for x in lines if x.strip()] + + for i, line in enumerate(lines): + if line.strip() == "" or line[0] == ";": + continue + if line.strip() == "[ molecules ]": + molecules_tag = True + continue + if line.strip().startswith("["): + molecules_tag = False + if molecules_tag and re.match(r"\s*.+\s+\d+", lines[i]): + print(f"Changing molecule number in line {i} that is {lines[i].strip()} to 1") + lines[i] = re.sub(r"(\s*.+\s+)(\d+)", r"\g<1>1", lines[i]) + + with open(temp_ref.name, "w") as f: + f.writelines(lines) + topology_ref = pmd.load_file(temp_ref.name) + except Exception as e: print(f"ERROR {e} in read_topologies while reading {target_top}") exit(2)