Exemplo n.º 1
0
    def __init__(self, config):
        setup = SubfolderHandle(config['folder'], config['subfolder'])

        counter = 0
        for file in setup.fileiter(config['dbfiles']):
            if counter == 0:
                copied = False
                if not exists_and_isfile(config['mother_db']):
                    copy(file, config['mother_db'])
                    copied = True
                info = PySurfDB.info_database(config['mother_db'])
                if 'natoms' not in info['dimensions']:
                    model = True
                else:
                    model = False
                mother_db = PySurfDB.load_database(
                    config['mother_db'],
                    data=info['variables'],
                    dimensions=info['dimensions'],
                    model=model,
                    sp=False)
                counter += 1
                if copied is True:
                    continue

            CombineDBs(mother_db, file, start=config['start_value'])
            print(f"Added file {file} to DB")
Exemplo n.º 2
0
    def __init__(self, config):
        dbin = PySurfDB.load_database(config['db_in'], read_only=True)
        info = PySurfDB.info_database(config['db_in'])

        self.thresh = config['energy_threshold']
        self.trust_radius_general = config['trust_radius_general']
        self.trust_radius_ci = config['trust_radius_ci']
        self.crd_mode = config['crd_mode']

        if 'natoms' in info['dimensions']:
            model = False
        else:
            model = True
        dbout = PySurfDB.generate_database(config['db_out'],
                                           data=info['variables'],
                                           dimensions=info['dimensions'],
                                           model=model)

        self.crds = None
        for i, crd in enumerate(dbin['crd']):
            if self.crd_mode == 'internal':
                crd = internal(np.copy(crd))
            else:
                crd = np.copy(crd)
            if i % 1000 == 0:
                print(f"Processing point {i}")
            crd_shape = crd.shape
            crd.resize((1, crd.size))
            if self.crds is None:
                self.crds = crd.reshape((1, crd.size))
            else:
                diff = np.diff(dbin.get('energy', i))

                _, (trust_general, trust_ci) = within_trust_radius(
                    crd,
                    self.crds,
                    radius=self.trust_radius_general,
                    radius_ci=self.trust_radius_ci,
                    metric='euclidean')
                if np.min(diff) < self.thresh:
                    if trust_ci is True:
                        continue
                else:
                    if trust_general is True:
                        continue
                self.crds = np.concatenate((self.crds, crd))

            crd.resize(crd_shape)
            for prop in info['variables']:
                dbout.append(prop, dbin.get(prop, i))
            dbout.increase
Exemplo n.º 3
0
def fill_db(filename, npoints):
    if exists_and_isfile(filename) is True:
        os.remove(filename)

    ho = HarmonicOscillator()
    model = PyrazineSala({'n_states': 3})
    nmodes = len(model.crd)
    nstates = 3
    db = PySurfDB.generate_database(filename,
                                    data=['crd', 'energy'],
                                    dimensions={
                                        'nmodes': nmodes,
                                        'nstates': nstates
                                    },
                                    model=True)
    x = (np.random.random((npoints, nmodes)) - 0.5) * 20
    y = []
    for r in x:
        db.append('crd', r)
        y += [
            model.get(Request(r, ['energy'],
                              [i for i in range(nstates)]))['energy']
        ]
        db.append(
            'energy',
            model.get(Request(r, ['energy'],
                              [i for i in range(nstates)]))['energy'])
        db.increase
    return db
Exemplo n.º 4
0
 def _get_db_info(self, database):
     db = PySurfDB.load_database(database, read_only=True)
     rep = db.dbrep
     natoms = rep.dimensions.get('natoms', None)
     if natoms is None:
         natoms = rep.dimensions['nmodes']
     nstates = rep.dimensions['nstates']
     return natoms, nstates, db.saved_properties
Exemplo n.º 5
0
    def __init__(self, main_db, added_db, start=0):

        if not isinstance(main_db, PySurfDB):
            info = PySurfDB.info_database(main_db)
            if 'natoms' in info['dimensions']:
                model = False
            else:
                model = True
            main_db = PySurfDB.load_database(main_db,
                                             dimensions=info['dimensions'],
                                             data=info['variables'],
                                             model=model)
        if not isinstance(added_db, PySurfDB):
            added_db = PySurfDB.load_database(added_db, read_only=True)

        keys_raw = main_db.get_keys()
        keys = []
        for key in keys_raw:
            if main_db.get_dimension(key)[0].isunlimited() is True:
                keys += [key]

        #check whether dbs fit together
        check = True
        for key in keys:
            if key in added_db.get_keys():
                for dim1, dim2 in zip(
                        main_db.get_dimension(key)[1:],
                        added_db.get_dimension(key)[1:]):
                    if dim1.size != dim2.size:
                        check = False
            else:
                check = False
        if check is False:
            print('Error DBs do not fit together by dimensions')
            exit()


#        nn = NextNeighbor(db1)
        for i in range(start, len(added_db)):
            #            min_dist = nn.get(crd)
            #            if min_dist[0] > 0.25:
            for key in keys:
                main_db.append(key, added_db[key][i])
            main_db.increase
Exemplo n.º 6
0
    def save_pes(self, filename, database):
        db = PySurfDB.load_database(database, read_only=True)
        results, _ = self._compute(db, ['energy'])

        def str_join(values):
            return ' '.join(str(val) for val in values)

        with open(filename, 'w') as f:
            f.write("\n".join(f"{i} {str_join(fitted)} {str_join(exact)}" 
                              for i, (fitted, exact) in enumerate(results['energy'])))
Exemplo n.º 7
0
    def __init__(self, config):
        db = PySurfDB.load_database(config['infile'], read_only=True)

        if config['property'] == 'len':
            print("Number of entries in database: ", len(db))
            return

        for idx, entry in enumerate(db[config['property']]):
            print('\nEntry: {}'.format(idx))
            print(entry)
Exemplo n.º 8
0
    def save_graddiff(self, filename, database):
        db = PySurfDB.load_database(database, read_only=True)
        results, _ = self._compute(db, ['gradient'])

        def str_join(values):
            return ' '.join(str(val) for val in values)

        with open(filename, 'w') as f:
            graddiff = [np.sqrt(np.mean((fitted-exact)**2)) for (fitted, exact) in results['gradient']]
            f.write("\n".join(f"{i} {diff}" 
                              for i, diff in enumerate(graddiff)))
Exemplo n.º 9
0
    def optimize(self, filename, properties):
        db = PySurfDB.load_database(filename, read_only=True)

        def _function(epsilon):
            print('opt cycle', epsilon)
            self.interpolator.epsilon = epsilon[0]
            self.interpolator.train()
            _, error = self._compute(db, properties)
            print(error)
            return error['rmsd']

        res = minimize(_function, self.interpolator.epsilon, method='nelder-mead', tol=1e-4, options={
            'maxiter': 25, 'disp': True, 'xatol': 0.0001})
        print(res)
        self.interpolator.epsilon = res.x[0]
        self.interpolator.train(self.weightsfile)
Exemplo n.º 10
0
def calc_2d_spec(files: "list", energy_start: "float", energy_end: "float",
                 en_points: "int", timesteps: "int") -> "meshplotdata":
    result = np.zeros((en_points, timesteps))
    for file in files:
        db = PySurfDB.load_database(file, read_only=True)
        energy = np.copy(db['energy'])
        fosc = np.copy(db['fosc'])
        currstate = np.copy(db['currstate']).flatten()
        for idx, en in enumerate(energy):
            state = int(currstate[idx])
            en_diff = en[state] - en[0]
            en_pos = (en_diff - energy_start) / (energy_end -
                                                 energy_start) * en_points
            en_pos = int(en_pos)
            if en_pos >= 0 and en_pos < en_points:
                #units are arbitrary, therefor not full Einstain A coefficient ist used, but just f*v**2
                result[en_pos, idx] += fosc[idx][state] * en_diff**2
    X = np.arange(timesteps) * 0.5
    Y = np.linspace(energy_start, energy_end, en_points) * 27.2114
    return (X, Y, result)
Exemplo n.º 11
0
 def validate(self, filename, properties):
     db = PySurfDB.load_database(filename, read_only=True)
     self._compute(db, properties)
Exemplo n.º 12
0
    for r in x:
        db.append('crd', r)
        y += [
            model.get(Request(r, ['energy'],
                              [i for i in range(nstates)]))['energy']
        ]
        db.append(
            'energy',
            model.get(Request(r, ['energy'],
                              [i for i in range(nstates)]))['energy'])
        db.increase
    return db


#db1 = fill_db('db1.dat', 5000)
db1 = PySurfDB.load_database('db.dat', read_only=True)
#db2 = fill_db('db2.dat', 1000)
db2 = PySurfDB.load_database('prop.db', read_only=True)

nstates = 4
nmodes = 9
logger = get_logger('test.log', 'test')
config = {
    'energy_threshold': 0.02,
    'trust_radius_ci': 0.5,
    'trust_radius_general': 1.0,
    'inverse_distance': False
}
rbf = RbfInterpolator(config, db1, ['energy'], nstates, nmodes, logger=logger)

counter = 0