Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

run_optimizer initialization process takes too long and consumes excessive memory #210

Open
L1yuu opened this issue Nov 29, 2024 · 1 comment

Comments

@L1yuu
Copy link

L1yuu commented Nov 29, 2024

Hi, I am experiencing an issue when trying to optimize a specific structure using geomeTRIC with custom engine. After calling the run_optimizer function, the program hangs after printing the message:
Bonds will be generated from interatomic distances less than 1.20 times sum of covalent radii.
this initialization process took several hours and still couldn't finish, and consumed over 20 GB memory。

A simple script that reproduces this problem.

#!/usr/bin/env python

import tempfile
import numpy as np
import geometric
import geometric.molecule

xyz_file = 'mgo.xyz'

def xyz2mol(xyz):
    f = open(xyz)
    lines = f.readlines()
    natm = int(lines[0])
    elem = []
    coord = []
    for i in range(2, natm+2):
        line = lines[i].split()
        elem.append(line[0])
        coord.append([float(x) for x in line[1:]])
    mol = geometric.molecule.Molecule()
    mol.elem = elem
    mol.xyzs = np.array([coord])
    return mol

class CustomEngine(geometric.engine.Engine):
    def __init__(self, molecule):
        super(CustomEngine, self).__init__(molecule)

    def calc_new(self, coords, dirname):
        energy = 0
        gradient = np.zeros(coords.shape)
        return {'energy': energy, 'gradient': gradient.ravel()}


def test_customengine():
    molecule = xyz2mol(xyz_file)
    customengine = CustomEngine(molecule)

    with tempfile.NamedTemporaryFile(mode="w+", delete=False) as tmpf:
        m = geometric.optimize.run_optimizer(customengine=customengine, check=1, input=tmpf.name)

if __name__ == '__main__':
    test_customengine()

the structure (mgo.xyz)

192
mgo
O	0.00000 	0.00000 	0.00000 
Mg	2.12824 	0.00000 	0.00000 
Mg	0.00000 	2.12824 	0.00000 
O	2.12824 	2.12824 	0.00000 
O	0.00000 	4.25648 	0.00000 
Mg	2.12824 	4.25648 	0.00000 
Mg	0.00000 	6.38472 	0.00000 
O	2.12824 	6.38472 	0.00000 
O	0.00000 	8.51296 	0.00000 
Mg	2.12824 	8.51296 	0.00000 
Mg	0.00000 	10.64120 	0.00000 
O	2.12824 	10.64120 	0.00000 
O	0.00000 	12.76944 	0.00000 
Mg	2.12824 	12.76944 	0.00000 
Mg	0.00000 	14.89768 	0.00000 
O	2.12824 	14.89768 	0.00000 
O	4.25648 	0.00000 	0.00000 
Mg	6.38472 	0.00000 	0.00000 
Mg	4.25648 	2.12824 	0.00000 
O	6.38472 	2.12824 	0.00000 
O	4.25648 	4.25648 	0.00000 
Mg	6.38472 	4.25648 	0.00000 
Mg	4.25648 	6.38472 	0.00000 
O	6.38472 	6.38472 	0.00000 
O	4.25648 	8.51296 	0.00000 
Mg	6.38472 	8.51296 	0.00000 
Mg	4.25648 	10.64120 	0.00000 
O	6.38472 	10.64120 	0.00000 
O	4.25648 	12.76944 	0.00000 
Mg	6.38472 	12.76944 	0.00000 
Mg	4.25648 	14.89768 	0.00000 
O	6.38472 	14.89768 	0.00000 
O	8.51296 	0.00000 	0.00000 
Mg	10.64120 	0.00000 	0.00000 
Mg	8.51296 	2.12824 	0.00000 
O	10.64120 	2.12824 	0.00000 
O	8.51296 	4.25648 	0.00000 
Mg	10.64120 	4.25648 	0.00000 
Mg	8.51296 	6.38472 	0.00000 
O	10.64120 	6.38472 	0.00000 
O	8.51296 	8.51296 	0.00000 
Mg	10.64120 	8.51296 	0.00000 
Mg	8.51296 	10.64120 	0.00000 
O	10.64120 	10.64120 	0.00000 
O	8.51296 	12.76944 	0.00000 
Mg	10.64120 	12.76944 	0.00000 
Mg	8.51296 	14.89768 	0.00000 
O	10.64120 	14.89768 	0.00000 
O	12.76944 	0.00000 	0.00000 
Mg	14.89768 	0.00000 	0.00000 
Mg	12.76944 	2.12824 	0.00000 
O	14.89768 	2.12824 	0.00000 
O	12.76944 	4.25648 	0.00000 
Mg	14.89768 	4.25648 	0.00000 
Mg	12.76944 	6.38472 	0.00000 
O	14.89768 	6.38472 	0.00000 
O	12.76944 	8.51296 	0.00000 
Mg	14.89768 	8.51296 	0.00000 
Mg	12.76944 	10.64120 	0.00000 
O	14.89768 	10.64120 	0.00000 
O	12.76944 	12.76944 	0.00000 
Mg	14.89768 	12.76944 	0.00000 
Mg	12.76944 	14.89768 	0.00000 
O	14.89768 	14.89768 	0.00000 
O	0.00000 	0.00000 	2.12824 
Mg	2.12824 	0.00000 	2.12824 
Mg	0.00000 	2.12824 	2.12824 
O	2.12824 	2.12824 	2.12824 
O	0.00000 	4.25648 	2.12824 
Mg	2.12824 	4.25648 	2.12824 
Mg	0.00000 	6.38472 	2.12824 
O	2.12824 	6.38472 	2.12824 
O	0.00000 	8.51296 	2.12824 
Mg	2.12824 	8.51296 	2.12824 
Mg	0.00000 	10.64120 	2.12824 
O	2.12824 	10.64120 	2.12824 
O	0.00000 	12.76944 	2.12824 
Mg	2.12824 	12.76944 	2.12824 
Mg	0.00000 	14.89768 	2.12824 
O	2.12824 	14.89768 	2.12824 
O	4.25648 	0.00000 	2.12824 
Mg	6.38472 	0.00000 	2.12824 
Mg	4.25648 	2.12824 	2.12824 
O	6.38472 	2.12824 	2.12824 
O	4.25648 	4.25648 	2.12824 
Mg	6.38472 	4.25648 	2.12824 
Mg	4.25648 	6.38472 	2.12824 
O	6.38472 	6.38472 	2.12824 
O	4.25648 	8.51296 	2.12824 
Mg	6.38472 	8.51296 	2.12824 
Mg	4.25648 	10.64120 	2.12824 
O	6.38472 	10.64120 	2.12824 
O	4.25648 	12.76944 	2.12824 
Mg	6.38472 	12.76944 	2.12824 
Mg	4.25648 	14.89768 	2.12824 
O	6.38472 	14.89768 	2.12824 
O	8.51296 	0.00000 	2.12824 
Mg	10.64120 	0.00000 	2.12824 
Mg	8.51296 	2.12824 	2.12824 
O	10.64120 	2.12824 	2.12824 
O	8.51296 	4.25648 	2.12824 
Mg	10.64120 	4.25648 	2.12824 
Mg	8.51296 	6.38472 	2.12824 
O	10.64120 	6.38472 	2.12824 
O	8.51296 	8.51296 	2.12824 
Mg	10.64120 	8.51296 	2.12824 
Mg	8.51296 	10.64120 	2.12824 
O	10.64120 	10.64120 	2.12824 
O	8.51296 	12.76944 	2.12824 
Mg	10.64120 	12.76944 	2.12824 
Mg	8.51296 	14.89768 	2.12824 
O	10.64120 	14.89768 	2.12824 
O	12.76944 	0.00000 	2.12824 
Mg	14.89768 	0.00000 	2.12824 
Mg	12.76944 	2.12824 	2.12824 
O	14.89768 	2.12824 	2.12824 
O	12.76944 	4.25648 	2.12824 
Mg	14.89768 	4.25648 	2.12824 
Mg	12.76944 	6.38472 	2.12824 
O	14.89768 	6.38472 	2.12824 
O	12.76944 	8.51296 	2.12824 
Mg	14.89768 	8.51296 	2.12824 
Mg	12.76944 	10.64120 	2.12824 
O	14.89768 	10.64120 	2.12824 
O	12.76944 	12.76944 	2.12824 
Mg	14.89768 	12.76944 	2.12824 
Mg	12.76944 	14.89768 	2.12824 
O	14.89768 	14.89768 	2.12824 
O	0.00000 	0.00000 	4.25648 
Mg	2.12824 	0.00000 	4.25648 
Mg	0.00000 	2.12824 	4.25648 
O	2.12824 	2.12824 	4.25648 
O	0.00000 	4.25648 	4.25648 
Mg	2.12824 	4.25648 	4.25648 
Mg	0.00000 	6.38472 	4.25648 
O	2.12824 	6.38472 	4.25648 
O	0.00000 	8.51296 	4.25648 
Mg	2.12824 	8.51296 	4.25648 
Mg	0.00000 	10.64120 	4.25648 
O	2.12824 	10.64120 	4.25648 
O	0.00000 	12.76944 	4.25648 
Mg	2.12824 	12.76944 	4.25648 
Mg	0.00000 	14.89768 	4.25648 
O	2.12824 	14.89768 	4.25648 
O	4.25648 	0.00000 	4.25648 
Mg	6.38472 	0.00000 	4.25648 
Mg	4.25648 	2.12824 	4.25648 
O	6.38472 	2.12824 	4.25648 
O	4.25648 	4.25648 	4.25648 
Mg	6.38472 	4.25648 	4.25648 
Mg	4.25648 	6.38472 	4.25648 
O	6.38472 	6.38472 	4.25648 
O	4.25648 	8.51296 	4.25648 
Mg	6.38472 	8.51296 	4.25648 
Mg	4.25648 	10.64120 	4.25648 
O	6.38472 	10.64120 	4.25648 
O	4.25648 	12.76944 	4.25648 
Mg	6.38472 	12.76944 	4.25648 
Mg	4.25648 	14.89768 	4.25648 
O	6.38472 	14.89768 	4.25648 
O	8.51296 	0.00000 	4.25648 
Mg	10.64120 	0.00000 	4.25648 
Mg	8.51296 	2.12824 	4.25648 
O	10.64120 	2.12824 	4.25648 
O	8.51296 	4.25648 	4.25648 
Mg	10.64120 	4.25648 	4.25648 
Mg	8.51296 	6.38472 	4.25648 
O	10.64120 	6.38472 	4.25648 
O	8.51296 	8.51296 	4.25648 
Mg	10.64120 	8.51296 	4.25648 
Mg	8.51296 	10.64120 	4.25648 
O	10.64120 	10.64120 	4.25648 
O	8.51296 	12.76944 	4.25648 
Mg	10.64120 	12.76944 	4.25648 
Mg	8.51296 	14.89768 	4.25648 
O	10.64120 	14.89768 	4.25648 
O	12.76944 	0.00000 	4.25648 
Mg	14.89768 	0.00000 	4.25648 
Mg	12.76944 	2.12824 	4.25648 
O	14.89768 	2.12824 	4.25648 
O	12.76944 	4.25648 	4.25648 
Mg	14.89768 	4.25648 	4.25648 
Mg	12.76944 	6.38472 	4.25648 
O	14.89768 	6.38472 	4.25648 
O	12.76944 	8.51296 	4.25648 
Mg	14.89768 	8.51296 	4.25648 
Mg	12.76944 	10.64120 	4.25648 
O	14.89768 	10.64120 	4.25648 
O	12.76944 	12.76944 	4.25648 
Mg	14.89768 	12.76944 	4.25648 
Mg	12.76944 	14.89768 	4.25648 
O	14.89768 	14.89768 	4.25648 

geomeTRIC version: 1.0.2
Python version: 3.11.5

@hjnpark
Copy link
Collaborator

hjnpark commented Nov 30, 2024

Hi @L1yuu,

Thank you for opening an issue. I checked your system, and it appears to be a compact lattice structure. geomeTRIC attempts to define all the internal coordinates (ICs) of the system before starting the optimization process. It first identifies bonds between atom pairs and then constructs other ICs, such as various angles. I ran a few tests and found that your system contains 547 bonds, 783 angles, 856 linear angles, and 3936 out-of-plane angles. After defining these ICs, geomeTRIC gets 'stuck' while adding dihedral angles because there are simply too many of them.

I suggest two possible solutions:

  1. You can use Cartesian coordinate instead of the TRIC (the default IC system) with the branch I just created. Call the optimizer with the coordsys='cart' ( m = geometric.optimize.run_optimizer(customengine=customengine, check=1, input=tmpf.name, coordsys='cart')) (To @leeping: I noticed that geomeTRIC tries to build primitive ICs even when the Cartesian coordinate system is selected. Is there a reason for this? Could you also review the changes in the new noprim branch in my repository? I can open a PR if the changes look good to you.)

  2. Or just comment out line 2190 and 2191 in internal.py and use Cartesian coordinate. Note that you will have to uncomment them later to use other IC systems.

Let me know if you have any other issues. Thank you!

Best,
Heejune Park

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants