From 487bf8840846b5d4d694b38985268c308aadb36e Mon Sep 17 00:00:00 2001 From: David Luevano <55825613+luevano@users.noreply.github.com> Date: Wed, 18 Dec 2019 07:21:35 -0700 Subject: Refactor files --- lj_matrix/lj_matrix.py | 207 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 207 insertions(+) create mode 100644 lj_matrix/lj_matrix.py (limited to 'lj_matrix/lj_matrix.py') diff --git a/lj_matrix/lj_matrix.py b/lj_matrix/lj_matrix.py new file mode 100644 index 000000000..4f63e95ca --- /dev/null +++ b/lj_matrix/lj_matrix.py @@ -0,0 +1,207 @@ +"""MIT License + +Copyright (c) 2019 David Luevano Alvarado + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +""" +import time +from lj_matrix.misc import printc +import math +import numpy as np +from numpy.linalg import eig + + +def lj_matrix(mol_data, + nc_data, + sigma=1.0, + epsilon=1.0, + max_len=25, + as_eig=True, + bohr_radius_units=False): + """ + Creates the Lennard-Jones Matrix from the molecule data given. + mol_data: molecule data, matrix of atom coordinates. + nc_data: nuclear charge data, array of atom data. + max_len: maximum amount of atoms in molecule. + as_eig: if data should be returned as matrix or array of eigenvalues. + bohr_radius_units: if units should be in bohr's radius units. + """ + if bohr_radius_units: + conversion_rate = 0.52917721067 + else: + conversion_rate = 1 + + mol_n = len(mol_data) + mol_nr = range(mol_n) + + if not mol_n == len(nc_data): + print(''.join(['Error. Molecule matrix dimension is different ', + 'than the nuclear charge array dimension.'])) + else: + if max_len < mol_n: + print(''.join(['Error. Molecule matrix dimension (mol_n) is ', + 'greater than max_len. Using mol_n.'])) + max_len = None + + if max_len: + lj = np.zeros((max_len, max_len)) + ml_r = range(max_len) + + # Actual calculation of the coulomb matrix. + for i in ml_r: + if i < mol_n: + x_i = mol_data[i, 0] + y_i = mol_data[i, 1] + z_i = mol_data[i, 2] + Z_i = nc_data[i] + else: + break + + for j in ml_r: + if j < mol_n: + x_j = mol_data[j, 0] + y_j = mol_data[j, 1] + z_j = mol_data[j, 2] + + x = (x_i-x_j)**2 + y = (y_i-y_j)**2 + z = (z_i-z_j)**2 + + if i == j: + lj[i, j] = (0.5*Z_i**2.4) + else: + # Calculations are done after i==j is checked + # so no division by zero is done. + + # A little play with r exponents + # so no square root is calculated. + # Conversion factor is included in r^2. + + # 1/r^2 + r_2 = sigma**2/(conversion_rate**2*(x + y + z)) + + r_6 = math.pow(r_2, 3) + r_12 = math.pow(r_6, 2) + lj[i, j] = (4*epsilon*(r_12 - r_6)) + else: + break + + # Now the value will be returned. + if as_eig: + lj_sorted = np.sort(eig(lj)[0])[::-1] + # Thanks to SO for the following lines of code. + # https://stackoverflow.com/a/43011036 + + # Keep zeros at the end. + mask = lj_sorted != 0. + f_mask = mask.sum(0, keepdims=1) >\ + np.arange(lj_sorted.shape[0]-1, -1, -1) + + f_mask = f_mask[::-1] + lj_sorted[f_mask] = lj_sorted[mask] + lj_sorted[~f_mask] = 0. + + return lj_sorted + + else: + return lj + + else: + lj_temp = [] + # Actual calculation of the coulomb matrix. + for i in mol_nr: + x_i = mol_data[i, 0] + y_i = mol_data[i, 1] + z_i = mol_data[i, 2] + Z_i = nc_data[i] + + lj_row = [] + for j in mol_nr: + x_j = mol_data[j, 0] + y_j = mol_data[j, 1] + z_j = mol_data[j, 2] + + x = (x_i-x_j)**2 + y = (y_i-y_j)**2 + z = (z_i-z_j)**2 + + if i == j: + lj_row.append(0.5*Z_i**2.4) + else: + # Calculations are done after i==j is checked + # so no division by zero is done. + + # A little play with r exponents + # so no square root is calculated. + # Conversion factor is included in r^2. + + # 1/r^2 + r_2 = sigma**2/(conversion_rate**2*(x + y + z)) + + r_6 = math.pow(r_2, 3) + r_12 = math.pow(r_6, 2) + lj_row.append(4*epsilon*(r_12 - r_6)) + + lj_temp.append(np.array(lj_row)) + + lj = np.array(lj_temp) + # Now the value will be returned. + if as_eig: + return np.sort(eig(lj)[0])[::-1] + else: + return lj + + +def lj_matrix_multiple(mol_data, + nc_data, + pipe=None, + sigma=1, + epsilon=1, + max_len=25, + as_eig=True, + bohr_radius_units=False): + """ + Calculates the Lennard-Jones Matrix of multiple molecules. + mol_data: molecule data, matrix of atom coordinates. + nc_data: nuclear charge data, array of atom data. + pipe: for multiprocessing purposes. Sends the data calculated + through a pipe. + max_len: maximum amount of atoms in molecule. + as_eig: if data should be returned as matrix or array of eigenvalues. + bohr_radius_units: if units should be in bohr's radius units. + """ + printc('L-J Matrices calculation started.', 'CYAN') + tic = time.perf_counter() + + ljm_data = np.array([lj_matrix(mol, + nc, + sigma, + epsilon, + max_len, + as_eig, + bohr_radius_units) + for mol, nc in zip(mol_data, nc_data)]) + + toc = time.perf_counter() + printc('\tL-JM calculation took {:.4f} seconds.'.format(toc-tic), 'GREEN') + + if pipe: + pipe.send(ljm_data) + + return ljm_data -- cgit v1.2.3-54-g00ecf From 124c3c5eb77c807b8a8a78413f3800720914c8e1 Mon Sep 17 00:00:00 2001 From: David Luevano <55825613+luevano@users.noreply.github.com> Date: Wed, 18 Dec 2019 08:15:18 -0700 Subject: Fix bugs --- lj_matrix/__init__.py | 23 ++++++++++ lj_matrix/__main__.py | 13 +++--- lj_matrix/c_matrix.py | 2 +- lj_matrix/do_ml.py | 4 +- lj_matrix/gauss_kernel.py | 2 +- lj_matrix/lj_matrix.py | 2 +- lj_matrix/read_qm7_data.py | 2 +- lj_matrix/version.py | 23 ++++++++++ setup.py | 102 +++++++++++++++++++++++++++++++++++++++++++++ 9 files changed, 161 insertions(+), 12 deletions(-) create mode 100644 lj_matrix/version.py create mode 100644 setup.py (limited to 'lj_matrix/lj_matrix.py') diff --git a/lj_matrix/__init__.py b/lj_matrix/__init__.py index 48cd14913..47d7e5013 100644 --- a/lj_matrix/__init__.py +++ b/lj_matrix/__init__.py @@ -20,3 +20,26 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ +from misc import printc +from read_qm7_data import read_qm7_data, read_nc_data, reas_db_data +from c_matrix import c_matrix, c_matrix_multiple +from cholesky_solve import cholesky_solve +from do_ml import do_ml +from frob_norm import frob_norm +from gauss_kernel import gauss_kernel +from lj_matrix import lj_matrix, lj_matrix_multiple + +# If somebody does "from package import *", this is what they will +# be able to access: +__all__ = ['printc', + 'read_qm7_data', + 'read_nc_data', + 'reas_db_data', + 'c_matrix', + 'c_matrix_multiple', + 'cholesky_solve', + 'do_ml', + 'frob_norm', + 'gauss_kernel', + 'lj_matrix', + 'lj_matrix_multiple'] diff --git a/lj_matrix/__main__.py b/lj_matrix/__main__.py index 4e13f4995..5a0e95b94 100644 --- a/lj_matrix/__main__.py +++ b/lj_matrix/__main__.py @@ -24,11 +24,11 @@ import time from multiprocessing import Process, Pipe # import matplotlib.pyplot as plt import pandas as pd -from lj_matrix.misc import printc -from lj_matrix.read_qm7_data import read_qm7_data -from lj_matrix.c_matrix import c_matrix_multiple -from lj_matrix.lj_matrix import lj_matrix_multiple -from lj_matrix.do_ml import do_ml +from misc import printc +from read_qm7_data import read_qm7_data +from c_matrix import c_matrix_multiple +from lj_matrix import lj_matrix_multiple +from do_ml import do_ml # Test @@ -235,4 +235,5 @@ def pl(): if __name__ == '__main__': # ml() - pl() + # pl() + print('OK!') diff --git a/lj_matrix/c_matrix.py b/lj_matrix/c_matrix.py index f40a18c68..4de711a1b 100644 --- a/lj_matrix/c_matrix.py +++ b/lj_matrix/c_matrix.py @@ -21,7 +21,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ import time -from lj_matrix.misc import printc +from misc import printc import math import numpy as np from numpy.linalg import eig diff --git a/lj_matrix/do_ml.py b/lj_matrix/do_ml.py index acf5455f4..c88533e68 100644 --- a/lj_matrix/do_ml.py +++ b/lj_matrix/do_ml.py @@ -23,8 +23,8 @@ SOFTWARE. import time from misc import printc import numpy as np -from lj_matrix.gauss_kernel import gauss_kernel -from lj_matrix.cholesky_solve import cholesky_solve +from gauss_kernel import gauss_kernel +from cholesky_solve import cholesky_solve def do_ml(desc_data, diff --git a/lj_matrix/gauss_kernel.py b/lj_matrix/gauss_kernel.py index 5dd8e6406..0dfc65d59 100644 --- a/lj_matrix/gauss_kernel.py +++ b/lj_matrix/gauss_kernel.py @@ -22,7 +22,7 @@ SOFTWARE. """ import math import numpy as np -from lj_matrix.frob_norm import frob_norm +from frob_norm import frob_norm def gauss_kernel(X_1, X_2, sigma): diff --git a/lj_matrix/lj_matrix.py b/lj_matrix/lj_matrix.py index 4f63e95ca..2a8e0d956 100644 --- a/lj_matrix/lj_matrix.py +++ b/lj_matrix/lj_matrix.py @@ -21,7 +21,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ import time -from lj_matrix.misc import printc +from misc import printc import math import numpy as np from numpy.linalg import eig diff --git a/lj_matrix/read_qm7_data.py b/lj_matrix/read_qm7_data.py index b54691fb0..068ea1a42 100644 --- a/lj_matrix/read_qm7_data.py +++ b/lj_matrix/read_qm7_data.py @@ -24,7 +24,7 @@ import os import time import numpy as np import random -from lj_matrix.misc import printc +from misc import printc # 'periodic_table_of_elements.txt' retrieved from diff --git a/lj_matrix/version.py b/lj_matrix/version.py new file mode 100644 index 000000000..fab58433d --- /dev/null +++ b/lj_matrix/version.py @@ -0,0 +1,23 @@ +"""MIT License + +Copyright (c) 2019 David Luevano Alvarado + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +""" +__version__ = '0.0.1' diff --git a/setup.py b/setup.py new file mode 100644 index 000000000..719ef3ce0 --- /dev/null +++ b/setup.py @@ -0,0 +1,102 @@ +"""MIT License + +Copyright (c) 2019 David Luevano Alvarado + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +""" +# This setup.py template was obtained from +# https://github.com/navdeep-G/setup.py/blob/master/setup.py +# ---------------------------------------------------------------------- +# Note: To use the 'upload' functionality of this file, you must: +# $ pipenv install twine --dev + +import io +import os + +from setuptools import find_packages, setup + +from lj_matrix.version import __version__ + +# Package meta-data. +NAME = 'lj_matrix' +DESCRIPTION = 'A Lennard Jones matrix exploration.' +URL = 'https://github.com/luevano/lj_matrix' +EMAIL = 'a301436@uach.mx' +AUTHOR = 'David Luevano Alvarado' +REQUIRES_PYTHON = '>=3.7' +VERSION = __version__ +# VERSION = '0.0.1' + +# What packages are required for this module to be executed? +REQUIRED = [ + # 'requests', 'maya', 'records', +] + +# What packages are optional? +EXTRAS = { + # 'fancy feature': ['django'], +} + +# The rest you shouldn't have to touch too much :) +# ------------------------------------------------ +# Except, perhaps the License and Trove Classifiers! +# If you do change the License, remember to change +# the Trove Classifier for that! + +here = os.path.abspath(os.path.dirname(__file__)) + +# Import the README and use it as the long-description. +# Note: this will only work if 'README.md' +# is present in your MANIFEST.in file! +try: + with io.open(os.path.join(here, 'README.md'), encoding='utf-8') as f: + long_description = '\n' + f.read() +except FileNotFoundError: + long_description = DESCRIPTION + +# Where the magic happens: +setup( + name=NAME, + version=VERSION, + description=DESCRIPTION, + long_description=long_description, + long_description_content_type='text/markdown', + author=AUTHOR, + author_email=EMAIL, + python_requires=REQUIRES_PYTHON, + url=URL, + packages=find_packages(exclude=["tests", + "*.tests", + "*.tests.*", + "tests.*"]), + # If your package is a single module, use this instead of 'packages': + # py_modules=['mypackage'], + install_requires=REQUIRED, + extras_require=EXTRAS, + include_package_data=True, + license='MIT', + classifiers=[ + # Trove classifiers + # Full list: https://pypi.python.org/pypi?%3Aaction=list_classifiers + 'License :: OSI Approved :: MIT License', + 'Programming Language :: Python', + 'Programming Language :: Python :: 3', + 'Programming Language :: Python :: 3.7' + ] +) -- cgit v1.2.3-54-g00ecf From a50d424d0ab7dd4cc6a2d6fc94371fa65a0d89b2 Mon Sep 17 00:00:00 2001 From: David Luevano <55825613+luevano@users.noreply.github.com> Date: Wed, 18 Dec 2019 09:53:44 -0700 Subject: Fix test issues --- lj_matrix/__init__.py | 27 ++++++++++++++------------- lj_matrix/__main__.py | 10 +++++----- lj_matrix/c_matrix.py | 2 +- lj_matrix/do_ml.py | 6 +++--- lj_matrix/gauss_kernel.py | 2 +- lj_matrix/lj_matrix.py | 2 +- lj_matrix/read_qm7_data.py | 2 +- test/__init__.py | 22 ++++++++++++++++++++++ test/test_c_matrix.py | 33 +++++++++++++++++++++++++++++++++ 9 files changed, 81 insertions(+), 25 deletions(-) create mode 100644 test/__init__.py create mode 100644 test/test_c_matrix.py (limited to 'lj_matrix/lj_matrix.py') diff --git a/lj_matrix/__init__.py b/lj_matrix/__init__.py index 47d7e5013..5019bd51d 100644 --- a/lj_matrix/__init__.py +++ b/lj_matrix/__init__.py @@ -20,26 +20,27 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ -from misc import printc -from read_qm7_data import read_qm7_data, read_nc_data, reas_db_data -from c_matrix import c_matrix, c_matrix_multiple -from cholesky_solve import cholesky_solve -from do_ml import do_ml -from frob_norm import frob_norm -from gauss_kernel import gauss_kernel -from lj_matrix import lj_matrix, lj_matrix_multiple +from lj_matrix.misc import printc +from lj_matrix.read_qm7_data import read_nc_data, reas_db_data, read_qm7_data +from lj_matrix.c_matrix import c_matrix, c_matrix_multiple +from lj_matrix.lj_matrix import lj_matrix, lj_matrix_multiple +from lj_matrix.frob_norm import frob_norm +from lj_matrix.gauss_kernel import gauss_kernel +from lj_matrix.cholesky_solve import cholesky_solve +from lj_matrix.do_ml import do_ml + # If somebody does "from package import *", this is what they will # be able to access: __all__ = ['printc', - 'read_qm7_data', 'read_nc_data', 'reas_db_data', + 'read_qm7_data', 'c_matrix', 'c_matrix_multiple', - 'cholesky_solve', - 'do_ml', + 'lj_matrix', + 'lj_matrix_multiple', 'frob_norm', 'gauss_kernel', - 'lj_matrix', - 'lj_matrix_multiple'] + 'cholesky_solve', + 'do_ml'] diff --git a/lj_matrix/__main__.py b/lj_matrix/__main__.py index 5a0e95b94..0b2a7c6f8 100644 --- a/lj_matrix/__main__.py +++ b/lj_matrix/__main__.py @@ -24,11 +24,11 @@ import time from multiprocessing import Process, Pipe # import matplotlib.pyplot as plt import pandas as pd -from misc import printc -from read_qm7_data import read_qm7_data -from c_matrix import c_matrix_multiple -from lj_matrix import lj_matrix_multiple -from do_ml import do_ml +from lj_matrix.misc import printc +from lj_matrix.read_qm7_data import read_qm7_data +from lj_matrix.c_matrix import c_matrix_multiple +from lj_matrix.lj_matrix import lj_matrix_multiple +from lj_matrix.do_ml import do_ml # Test diff --git a/lj_matrix/c_matrix.py b/lj_matrix/c_matrix.py index 4de711a1b..f21ccfd8c 100644 --- a/lj_matrix/c_matrix.py +++ b/lj_matrix/c_matrix.py @@ -21,10 +21,10 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ import time -from misc import printc import math import numpy as np from numpy.linalg import eig +from lj_matrix.misc import printc def c_matrix(mol_data, diff --git a/lj_matrix/do_ml.py b/lj_matrix/do_ml.py index c88533e68..ba88a6fd8 100644 --- a/lj_matrix/do_ml.py +++ b/lj_matrix/do_ml.py @@ -21,10 +21,10 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ import time -from misc import printc import numpy as np -from gauss_kernel import gauss_kernel -from cholesky_solve import cholesky_solve +from lj_matrix.misc import printc +from lj_matrix.gauss_kernel import gauss_kernel +from lj_matrix.cholesky_solve import cholesky_solve def do_ml(desc_data, diff --git a/lj_matrix/gauss_kernel.py b/lj_matrix/gauss_kernel.py index 0dfc65d59..5dd8e6406 100644 --- a/lj_matrix/gauss_kernel.py +++ b/lj_matrix/gauss_kernel.py @@ -22,7 +22,7 @@ SOFTWARE. """ import math import numpy as np -from frob_norm import frob_norm +from lj_matrix.frob_norm import frob_norm def gauss_kernel(X_1, X_2, sigma): diff --git a/lj_matrix/lj_matrix.py b/lj_matrix/lj_matrix.py index 2a8e0d956..2a56a3cdf 100644 --- a/lj_matrix/lj_matrix.py +++ b/lj_matrix/lj_matrix.py @@ -21,10 +21,10 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ import time -from misc import printc import math import numpy as np from numpy.linalg import eig +from lj_matrix.misc import printc def lj_matrix(mol_data, diff --git a/lj_matrix/read_qm7_data.py b/lj_matrix/read_qm7_data.py index 068ea1a42..b54691fb0 100644 --- a/lj_matrix/read_qm7_data.py +++ b/lj_matrix/read_qm7_data.py @@ -24,7 +24,7 @@ import os import time import numpy as np import random -from misc import printc +from lj_matrix.misc import printc # 'periodic_table_of_elements.txt' retrieved from diff --git a/test/__init__.py b/test/__init__.py new file mode 100644 index 000000000..8b866e928 --- /dev/null +++ b/test/__init__.py @@ -0,0 +1,22 @@ +"""MIT License + +Copyright (c) 2019 David Luevano Alvarado + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +""" \ No newline at end of file diff --git a/test/test_c_matrix.py b/test/test_c_matrix.py new file mode 100644 index 000000000..a8bb5ae34 --- /dev/null +++ b/test/test_c_matrix.py @@ -0,0 +1,33 @@ +"""MIT License + +Copyright (c) 2019 David Luevano Alvarado + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +""" +import unittest +from lj_matrix.c_matrix import c_matrix + + +class TestCMatrix(unittest.TestCase): + def test_c_matrix(self): + self.assertAlmostEqual(1, 1) + + +if __name__ == '__main__': + unittest.main() -- cgit v1.2.3-54-g00ecf From db64425a5580a49312e313a6e75e7a296eb93b35 Mon Sep 17 00:00:00 2001 From: David Luevano <55825613+luevano@users.noreply.github.com> Date: Mon, 23 Dec 2019 12:23:46 -0700 Subject: Restructure code and bug fix --- lj_matrix/__init__.py | 4 +- lj_matrix/__main__.py | 31 ++-------------- lj_matrix/lj_matrix.py | 6 ++- lj_matrix/parallel_create_matrices.py | 70 +++++++++++++++++++++++++++++++++++ lj_matrix/read_qm7_data.py | 6 +-- 5 files changed, 83 insertions(+), 34 deletions(-) create mode 100644 lj_matrix/parallel_create_matrices.py (limited to 'lj_matrix/lj_matrix.py') diff --git a/lj_matrix/__init__.py b/lj_matrix/__init__.py index 5019bd51d..d7794d3be 100644 --- a/lj_matrix/__init__.py +++ b/lj_matrix/__init__.py @@ -21,7 +21,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ from lj_matrix.misc import printc -from lj_matrix.read_qm7_data import read_nc_data, reas_db_data, read_qm7_data +from lj_matrix.read_qm7_data import read_nc_data, read_db_data, read_qm7_data from lj_matrix.c_matrix import c_matrix, c_matrix_multiple from lj_matrix.lj_matrix import lj_matrix, lj_matrix_multiple from lj_matrix.frob_norm import frob_norm @@ -34,7 +34,7 @@ from lj_matrix.do_ml import do_ml # be able to access: __all__ = ['printc', 'read_nc_data', - 'reas_db_data', + 'read_db_data', 'read_qm7_data', 'c_matrix', 'c_matrix_multiple', diff --git a/lj_matrix/__main__.py b/lj_matrix/__main__.py index 0b2a7c6f8..8e52031f1 100644 --- a/lj_matrix/__main__.py +++ b/lj_matrix/__main__.py @@ -26,8 +26,7 @@ from multiprocessing import Process, Pipe import pandas as pd from lj_matrix.misc import printc from lj_matrix.read_qm7_data import read_qm7_data -from lj_matrix.c_matrix import c_matrix_multiple -from lj_matrix.lj_matrix import lj_matrix_multiple +from lj_matrix.parallel_create_matrices import parallel_create_matrices from lj_matrix.do_ml import do_ml @@ -40,32 +39,10 @@ def ml(): init_time = time.perf_counter() # Data reading. - zi_data, molecules, nuclear_charge, energy_pbe0, energy_delta =\ - read_qm7_data() + molecules, nuclear_charge, energy_pbe0, energy_delta = read_qm7_data() # Matrices calculation. - procs = [] - pipes = [] - - # cm_recv, cm_send = Pipe(False) - # p1 = Process(target=c_matrix_multiple, - # args=(molecules, nuclear_charge, cm_send)) - # procs.append(p1) - # pipes.append(cm_recv) - # p1.start() - - ljm_recv, ljm_send = Pipe(False) - p2 = Process(target=lj_matrix_multiple, - args=(molecules, nuclear_charge, ljm_send, 1, 0.25)) - procs.append(p2) - pipes.append(ljm_recv) - p2.start() - - # cm_data = pipes[0].recv() - ljm_data = pipes[0].recv() - - for proc in procs: - proc.join() + cm_data, ljm_data = parallel_create_matrices(molecules, nuclear_charge) # ML calculation. procs = [] @@ -234,6 +211,6 @@ def pl(): if __name__ == '__main__': - # ml() + ml() # pl() print('OK!') diff --git a/lj_matrix/lj_matrix.py b/lj_matrix/lj_matrix.py index 2a56a3cdf..0c16b5686 100644 --- a/lj_matrix/lj_matrix.py +++ b/lj_matrix/lj_matrix.py @@ -38,6 +38,8 @@ def lj_matrix(mol_data, Creates the Lennard-Jones Matrix from the molecule data given. mol_data: molecule data, matrix of atom coordinates. nc_data: nuclear charge data, array of atom data. + sigma: sigma value. + epsilon: epsilon value. max_len: maximum amount of atoms in molecule. as_eig: if data should be returned as matrix or array of eigenvalues. bohr_radius_units: if units should be in bohr's radius units. @@ -171,8 +173,8 @@ def lj_matrix(mol_data, def lj_matrix_multiple(mol_data, nc_data, pipe=None, - sigma=1, - epsilon=1, + sigma=1.0, + epsilon=1.0, max_len=25, as_eig=True, bohr_radius_units=False): diff --git a/lj_matrix/parallel_create_matrices.py b/lj_matrix/parallel_create_matrices.py new file mode 100644 index 000000000..0ab691525 --- /dev/null +++ b/lj_matrix/parallel_create_matrices.py @@ -0,0 +1,70 @@ +"""MIT License + +Copyright (c) 2019 David Luevano Alvarado + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +""" +from multiprocessing import Process, Pipe +from lj_matrix.c_matrix import c_matrix_multiple +from lj_matrix.lj_matrix import lj_matrix_multiple + + +def parallel_create_matrices(mol_data, + nc_data, + sigma=1.0, + epsilon=1.0, + max_len=25, + as_eig=True, + bohr_radius_units=False): + """ + Creates the Coulomb and L-J matrices in parallel. + mol_data: molecule data, matrix of atom coordinates. + nc_data: nuclear charge data, array of atom data. + sigma: sigma value for L-J matrix. + epsilon: epsilon value for L-J matrix. + max_len: maximum amount of atoms in molecule. + as_eig: if data should be returned as matrix or array of eigenvalues. + bohr_radius_units: if units should be in bohr's radius units. + """ + + # Matrices calculation. + procs = [] + pipes = [] + + cm_recv, cm_send = Pipe(False) + p1 = Process(target=c_matrix_multiple, + args=(mol_data, nc_data, cm_send)) + procs.append(p1) + pipes.append(cm_recv) + p1.start() + + ljm_recv, ljm_send = Pipe(False) + p2 = Process(target=lj_matrix_multiple, + args=(mol_data, nc_data, ljm_send, sigma, epsilon)) + procs.append(p2) + pipes.append(ljm_recv) + p2.start() + + cm_data = pipes[0].recv() + ljm_data = pipes[1].recv() + + for proc in procs: + proc.join() + + return cm_data, ljm_data diff --git a/lj_matrix/read_qm7_data.py b/lj_matrix/read_qm7_data.py index b54691fb0..9bb7629ca 100644 --- a/lj_matrix/read_qm7_data.py +++ b/lj_matrix/read_qm7_data.py @@ -51,7 +51,7 @@ def read_nc_data(data_path): # 'hof_qm7.txt.txt' retrieved from # https://github.com/qmlcode/tutorial -def reas_db_data(zi_data, +def read_db_data(zi_data, data_path, r_seed=111): """ @@ -135,10 +135,10 @@ def read_qm7_data(): zi_data = read_nc_data(data_path) molecules, nuclear_charge, energy_pbe0, energy_delta = \ - reas_db_data(zi_data, data_path) + read_db_data(zi_data, data_path) os.chdir(init_path) toc = time.perf_counter() printc('\tData reading took {:.4f} seconds.'.format(toc-tic), 'GREEN') - return zi_data, molecules, nuclear_charge, energy_pbe0, energy_delta + return molecules, nuclear_charge, energy_pbe0, energy_delta -- cgit v1.2.3-54-g00ecf From c1e7b327655ebaa5c44e4bef5b9b675b23782952 Mon Sep 17 00:00:00 2001 From: David Luevano <55825613+luevano@users.noreply.github.com> Date: Sat, 28 Dec 2019 11:05:39 -0700 Subject: Refactor code and fix bug --- lj_matrix/do_ml.py | 3 +++ lj_matrix/lj_matrix.py | 17 +++++++++++++++-- lj_matrix/parallel_create_matrices.py | 27 +++++++++++++++++++++------ 3 files changed, 39 insertions(+), 8 deletions(-) (limited to 'lj_matrix/lj_matrix.py') diff --git a/lj_matrix/do_ml.py b/lj_matrix/do_ml.py index 8724e6831..45dc7a5f0 100644 --- a/lj_matrix/do_ml.py +++ b/lj_matrix/do_ml.py @@ -114,6 +114,7 @@ def ml(desc_data, def do_ml(min_training_size, max_training_size=None, training_increment_size=500, + ljm_diag_value=None, ljm_sigma=1.0, ljm_epsilon=1.0, save_benchmarks=False, @@ -127,6 +128,7 @@ def do_ml(min_training_size, min_training_size: minimum training size. max_training_size: maximum training size. training_increment_size: training increment size. + ljm_diag_value: if a special diagonal value should be used in lj matrix. ljm_sigma: sigma value for lj matrix. ljm_epsilon: epsilon value for lj matrix. save_benchmarks: if benchmarks should be saved. @@ -147,6 +149,7 @@ def do_ml(min_training_size, # Matrices calculation. cm_data, ljm_data = parallel_create_matrices(molecules, nuclear_charge, + ljm_diag_value, ljm_sigma, ljm_epsilon, max_len, diff --git a/lj_matrix/lj_matrix.py b/lj_matrix/lj_matrix.py index 0c16b5686..c3b61becb 100644 --- a/lj_matrix/lj_matrix.py +++ b/lj_matrix/lj_matrix.py @@ -29,6 +29,7 @@ from lj_matrix.misc import printc def lj_matrix(mol_data, nc_data, + diag_value=None, sigma=1.0, epsilon=1.0, max_len=25, @@ -38,6 +39,7 @@ def lj_matrix(mol_data, Creates the Lennard-Jones Matrix from the molecule data given. mol_data: molecule data, matrix of atom coordinates. nc_data: nuclear charge data, array of atom data. + diag_value: if special diagonal value is to be used. sigma: sigma value. epsilon: epsilon value. max_len: maximum amount of atoms in molecule. @@ -86,7 +88,10 @@ def lj_matrix(mol_data, z = (z_i-z_j)**2 if i == j: - lj[i, j] = (0.5*Z_i**2.4) + if not diag_value: + lj[i, j] = (0.5*Z_i**2.4) + else: + lj[i, j] = diag_value else: # Calculations are done after i==j is checked # so no division by zero is done. @@ -144,7 +149,10 @@ def lj_matrix(mol_data, z = (z_i-z_j)**2 if i == j: - lj_row.append(0.5*Z_i**2.4) + if not diag_value: + lj_row.append(0.5*Z_i**2.4) + else: + lj_row.append(diag_value) else: # Calculations are done after i==j is checked # so no division by zero is done. @@ -173,6 +181,7 @@ def lj_matrix(mol_data, def lj_matrix_multiple(mol_data, nc_data, pipe=None, + diag_value=None, sigma=1.0, epsilon=1.0, max_len=25, @@ -184,6 +193,9 @@ def lj_matrix_multiple(mol_data, nc_data: nuclear charge data, array of atom data. pipe: for multiprocessing purposes. Sends the data calculated through a pipe. + diag_value: if special diagonal value is to be used. + sigma: sigma value. + epsilon: epsilon value. max_len: maximum amount of atoms in molecule. as_eig: if data should be returned as matrix or array of eigenvalues. bohr_radius_units: if units should be in bohr's radius units. @@ -193,6 +205,7 @@ def lj_matrix_multiple(mol_data, ljm_data = np.array([lj_matrix(mol, nc, + diag_value, sigma, epsilon, max_len, diff --git a/lj_matrix/parallel_create_matrices.py b/lj_matrix/parallel_create_matrices.py index 0ab691525..cd5ef5c8e 100644 --- a/lj_matrix/parallel_create_matrices.py +++ b/lj_matrix/parallel_create_matrices.py @@ -27,8 +27,9 @@ from lj_matrix.lj_matrix import lj_matrix_multiple def parallel_create_matrices(mol_data, nc_data, - sigma=1.0, - epsilon=1.0, + ljm_diag_value=None, + ljm_sigma=1.0, + ljm_epsilon=1.0, max_len=25, as_eig=True, bohr_radius_units=False): @@ -36,8 +37,9 @@ def parallel_create_matrices(mol_data, Creates the Coulomb and L-J matrices in parallel. mol_data: molecule data, matrix of atom coordinates. nc_data: nuclear charge data, array of atom data. - sigma: sigma value for L-J matrix. - epsilon: epsilon value for L-J matrix. + ljm_diag_value: if special diagonal value is to be used for lj matrix. + ljm_sigma: sigma value for lj matrix. + ljm_epsilon: psilon value for lj matrix. max_len: maximum amount of atoms in molecule. as_eig: if data should be returned as matrix or array of eigenvalues. bohr_radius_units: if units should be in bohr's radius units. @@ -49,14 +51,27 @@ def parallel_create_matrices(mol_data, cm_recv, cm_send = Pipe(False) p1 = Process(target=c_matrix_multiple, - args=(mol_data, nc_data, cm_send)) + args=(mol_data, + nc_data, + cm_send, + max_len, + as_eig, + bohr_radius_units)) procs.append(p1) pipes.append(cm_recv) p1.start() ljm_recv, ljm_send = Pipe(False) p2 = Process(target=lj_matrix_multiple, - args=(mol_data, nc_data, ljm_send, sigma, epsilon)) + args=(mol_data, + nc_data, + ljm_send, + ljm_diag_value, + ljm_sigma, + ljm_epsilon, + max_len, + as_eig, + bohr_radius_units)) procs.append(p2) pipes.append(ljm_recv) p2.start() -- cgit v1.2.3-54-g00ecf From 4704314c9b4d1066383da5c3d6ca87bba9067c8d Mon Sep 17 00:00:00 2001 From: David Luevano <55825613+luevano@users.noreply.github.com> Date: Sat, 28 Dec 2019 11:37:22 -0700 Subject: Refactor code --- lj_matrix/__main__.py | 1 + lj_matrix/do_ml.py | 5 ++++- lj_matrix/lj_matrix.py | 2 +- lj_matrix/read_qm7_data.py | 7 ++++--- 4 files changed, 10 insertions(+), 5 deletions(-) (limited to 'lj_matrix/lj_matrix.py') diff --git a/lj_matrix/__main__.py b/lj_matrix/__main__.py index 811024ff0..688e5adcc 100644 --- a/lj_matrix/__main__.py +++ b/lj_matrix/__main__.py @@ -31,6 +31,7 @@ if __name__ == '__main__': ljm_diag_value=None, ljm_sigma=1.0, ljm_epsilon=1.0, + r_seed=111, save_benchmarks=False, show_msgs=True) # plot_benchmarks() diff --git a/lj_matrix/do_ml.py b/lj_matrix/do_ml.py index da9386bf7..25a55e823 100644 --- a/lj_matrix/do_ml.py +++ b/lj_matrix/do_ml.py @@ -118,6 +118,7 @@ def do_ml(min_training_size, ljm_diag_value=None, ljm_sigma=1.0, ljm_epsilon=1.0, + r_seed=111, save_benchmarks=False, max_len=25, as_eig=True, @@ -134,6 +135,7 @@ def do_ml(min_training_size, ljm_diag_value: if a special diagonal value should be used in lj matrix. ljm_sigma: sigma value for lj matrix. ljm_epsilon: epsilon value for lj matrix. + r_seed: random seed to use for the shuffling. save_benchmarks: if benchmarks should be saved. max_len: maximum amount of atoms in molecule. as_eig: if data should be returned as matrix or array of eigenvalues. @@ -147,7 +149,8 @@ def do_ml(min_training_size, max_training_size = min_training_size + training_increment_size # Data reading. - molecules, nuclear_charge, energy_pbe0, energy_delta = read_qm7_data() + molecules, nuclear_charge, energy_pbe0, energy_delta =\ + read_qm7_data(r_seed) # Matrices calculation. cm_data, ljm_data = parallel_create_matrices(molecules, diff --git a/lj_matrix/lj_matrix.py b/lj_matrix/lj_matrix.py index c3b61becb..6739ae283 100644 --- a/lj_matrix/lj_matrix.py +++ b/lj_matrix/lj_matrix.py @@ -88,7 +88,7 @@ def lj_matrix(mol_data, z = (z_i-z_j)**2 if i == j: - if not diag_value: + if diag_value is None: lj[i, j] = (0.5*Z_i**2.4) else: lj[i, j] = diag_value diff --git a/lj_matrix/read_qm7_data.py b/lj_matrix/read_qm7_data.py index 9bb7629ca..4401ca1c0 100644 --- a/lj_matrix/read_qm7_data.py +++ b/lj_matrix/read_qm7_data.py @@ -59,7 +59,7 @@ def read_db_data(zi_data, its contents as usable variables. zi_data: dictionary containing nuclear charge data. data_path: path to the data directory. - r_seed: random seed. + r_seed: random seed to use for the shuffling. """ os.chdir(data_path) @@ -122,9 +122,10 @@ def read_db_data(zi_data, return molecules, nuclear_charge, energy_pbe0, energy_delta -def read_qm7_data(): +def read_qm7_data(r_seed=111): """ Reads all the qm7 data. + r_seed: random seed to use for the shuffling. """ tic = time.perf_counter() printc('Data reading started.', 'CYAN') @@ -135,7 +136,7 @@ def read_qm7_data(): zi_data = read_nc_data(data_path) molecules, nuclear_charge, energy_pbe0, energy_delta = \ - read_db_data(zi_data, data_path) + read_db_data(zi_data, data_path, r_seed) os.chdir(init_path) toc = time.perf_counter() -- cgit v1.2.3-54-g00ecf