collect_events.py 4.39 KB
Newer Older
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
import numpy as np
from scipy.spatial.distance import euclidean
import matplotlib.pyplot as plt

from ase.io import read, write

import os

def collect_events(a_gm, runs_path, featureCalculator, dmax=0.1, dE_max=0.2, N=100, Ninit=0, traj_name=None, save_dir='stats', check_finalPop=False):
    f_gm = featureCalculator.get_feature(a_gm)
    E_gm = a_gm.get_potential_energy()
    times = []
    events = []
    structures = []
    iter_max = 0
    for n in range(N):
        found = False
        try:
            traj = read(runs_path + 'run{}/structures.traj'.format(n,n), index=':')
            run_length = len(traj)
            if run_length > iter_max:
                iter_max = len(traj)

            E_all = np.array([a.get_potential_energy() for a in traj])
            for i, (E, a) in enumerate(zip(E_all, traj)):
                if E < E_gm + dE_max:
                    f = featureCalculator.get_feature(traj[i])
                    d = euclidean(f, f_gm)
                    if d < dmax:
                        number_SP = i + Ninit
                        E = a.get_potential_energy()
                        print('run:', n, '\t iter_found:', number_SP, '\t dist:', d, '\t energy:', E)
                        times.append(i)
                        events.append(1)
                        structures.append(a)
                        found = True
                        break
            if not found:
                times.append(run_length)
                events.append(0)
        except Exception as err:
            print('Exception caught:', err)
        

    if not os.path.exists(save_dir):
        os.makedirs(save_dir)
    if traj_name is not None:
        write(save_dir+'/'+traj_name, structures)

    return times, events

def collect_events_energy(a_gm, runs_path, dE_max=0.3, N=100, Ninit=0, traj_name=None, save_dir='stats', check_finalPop=False):
    E_gm = a_gm.get_potential_energy()
    times = []
    events = []
    structures = []
    iter_max = 0
    for n in range(N):
        found = False
        try:
            traj = read(runs_path + 'run{}/structures.traj'.format(n,n), index=':')
            run_length = len(traj)
            if run_length > iter_max:
                iter_max = len(traj)
            
            E_all = np.array([a.get_potential_energy() for a in traj])
            for i, (E, a) in enumerate(zip(E_all, traj)):
                if E < E_gm + dE_max:
                    number_SP = i + Ninit
                    print('run:', n, '\t iter_found:', number_SP, '\t Energy:', E)
                    times.append(number_SP)
                    events.append(number_SP)
                    structures.append(a)
                    found = True
                    break
            if not found:
                times.append(run_length)
                events.append(0)            
        except Exception as err:
            print('Exception caught:', err)
        

    if not os.path.exists(save_dir):
        os.makedirs(save_dir)
    if traj_name is not None:
        write(save_dir+'/'+traj_name, structures)

    return times, events

def collect_events_old(a_gm, runs_path, featureCalculator, dmax=0.1, N=100, Ninit=0, traj_name=None, save_dir='stats', check_finalPop=False):
    f_gm = featureCalculator.get_feature(a_gm)
    times = []
    events = []
    structures = []
    iter_max = 0
    for n in range(N):
        found = False
        try:
            traj = read(runs_path + 'run{}/structures.traj'.format(n,n), index=':')
            run_length = len(traj)
            if run_length > iter_max:
                iter_max = len(traj)

            fMat = featureCalculator.get_featureMat(traj)
            for i, (f, a) in enumerate(zip(fMat, traj)):
                d = euclidean(f, f_gm)
                if d < dmax:
                    number_SP = i + Ninit
                    E = a.get_potential_energy()
                    print('run:', n, '\t iter_found:', number_SP, '\t dist:', d, '\t energy:', E)
                    times.append(i)
                    events.append(1)
                    structures.append(a)
                    found = True
                    break
            if not found:
                times.append(run_length)
                events.append(0)            
        except:
            pass
        

    if not os.path.exists(save_dir):
        os.makedirs(save_dir)
    if traj_name is not None:
        write(save_dir+'/'+traj_name, structures)

    return times, events