summaryrefslogtreecommitdiff
path: root/main.py
blob: ab8d5024417c4bfdd966648dab4f3982a7bf1abf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
"""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 os
import time
from colorama import init, Fore, Style
# import math
# import random
import numpy as np
# from numpy.linalg import cholesky, eig
# import matplotlib.pyplot as plt
from read_nc_data import read_nc_data
from read_db_edata import read_db_edata
from c_matrix import c_matrix
from lj_matrix import lj_matrix
# from frob_norm import frob_norm
from gauss_kernel import gauss_kernel
from cholesky_solve import cholesky_solve


def printc(text, color):
    """
    Prints texts normaly, but in color. Using colorama.
    text: string with the text to print.
    """
    print(color + text + Style.RESET_ALL)


# Initialization.
init_time = time.perf_counter()
init()

# Move to data folder.
init_path = os.getcwd()
os.chdir('data')
data_path = os.getcwd()

printc('Data reading started.', Fore.CYAN)
tic = time.perf_counter()

# Read nuclear charge data.
zi_data = read_nc_data(data_path)

# Read molecule data.
molecules, nuclear_charge, energy_pbe0, energy_delta = \
    read_db_edata(zi_data, data_path)

toc = time.perf_counter()
printc('\tData reading took {:.4f} seconds.'.format(toc-tic), Fore.GREEN)

# Go back to main folder.
os.chdir(init_path)

printc('Coulomb Matrices calculation started.', Fore.CYAN)
tic = time.perf_counter()

cm_data = np.array([c_matrix(mol, nc, as_eig=True)
                    for mol, nc in zip(molecules, nuclear_charge)])

toc = time.perf_counter()
printc('\tCoulomb matrices calculation took {:.4f} seconds.'.format(toc-tic),
       Fore.GREEN)

printc('L-J Matrices calculation started.', Fore.CYAN)
tic = time.perf_counter()

ljm_data = np.array([lj_matrix(mol, nc, as_eig=True)
                     for mol, nc in zip(molecules, nuclear_charge)])

toc = time.perf_counter()
printc('\tL-J matrices calculation took {:.4f} seconds.'.format(toc-tic),
       Fore.GREEN)

#
# Problem solving with Coulomb Matrix.
#
printc('CM ML started.', Fore.CYAN)
tic = time.perf_counter()

sigma = 1000.0

Xcm_training = cm_data[:6000]
Ycm_training = energy_pbe0[:6000]
Kcm_training = gauss_kernel(Xcm_training, Xcm_training, sigma)
alpha_cm = cholesky_solve(Kcm_training, Ycm_training)

Xcm_test = cm_data[-1000:]
Ycm_test = energy_pbe0[-1000:]
Kcm_test = gauss_kernel(Xcm_test, Xcm_training, sigma)
Ycm_predicted = np.dot(Kcm_test, alpha_cm)

print('\tMean absolute error for CM: {}'.format(np.mean(np.abs(Ycm_predicted
                                                               - Ycm_test))))

toc = time.perf_counter()
printc('\tCM ML took {:.4f} seconds.'.format(toc-tic), Fore.GREEN)


#
# Problem solving with L-J Matrix.
#
printc('L-JM ML started.', Fore.CYAN)
tic = time.perf_counter()

sigma = 1000.0

Xljm_training = ljm_data[:6000]
Yljm_training = energy_pbe0[:6000]
Kljm_training = gauss_kernel(Xljm_training, Xljm_training, sigma)
alpha_ljm = cholesky_solve(Kljm_training, Yljm_training)

Xljm_test = ljm_data[-1000:]
Yljm_test = energy_pbe0[-1000:]
Kljm_test = gauss_kernel(Xljm_test, Xljm_training, sigma)
Yljm_predicted = np.dot(Kljm_test, alpha_ljm)

print('\tMean absolute error for LJM: {}'.format(np.mean(np.abs(Yljm_predicted
                                                                - Yljm_test))))

toc = time.perf_counter()
printc('\tL-JM ML took {:.4f} seconds.'.format(toc-tic), Fore.GREEN)


# End of program
end_time = time.perf_counter()
printc('Program took {:.4f} seconds of runtime.'.format(end_time - init_time),
       Fore.CYAN)