def to_cluster(self): from cluster.base import Cluster cc = Cluster(self.n + self.surf.n) cc.surfnum = self.surf.n cc.atoms = np.array(list(self.surf.atoms) + list(self.atoms)) cc.elems = np.array(list(self.surf.elems) + list(self.elems)) if self.surf.forces is not None and self.forces is not None: cc.forces = np.array(list(self.surf.forces) + list(self.forces)) cc.energy = self.energy cc.mag = self.mag cc.label = self.label return cc
def read_contcar(fn): f = open(fn, 'r').readlines() ff = [l.strip() for l in f] cell = [[float(g) for g in f.split(' ') if len(g) != 0] for f in ff[2:5]] cell = np.array(cell) if np.linalg.norm(np.array([cell[0, 1], cell[1, 0]])) < TOLX: dcell = np.diag(cell) print(' CELL = %15.7f%15.7f%15.7f' % tuple(dcell)) else: dcell = np.array( [cell[0, 0], cell[0, 1], cell[1, 0], cell[1, 1], cell[2, 2]]) print(' CELL = [%15.7f, %15.7f] [%15.7f, %15.7f] %15.7f' % tuple(dcell)) elems = np.array([g for g in ff[5].split(' ') if len(g) != 0]) elemsn = np.array([int(g) for g in ff[6].split(' ') if len(g) != 0]) print(' ELEMS = ' + " ".join(["%s %d" % (i, j) for i, j in zip(elems, elemsn)])) print(' TOTAL = %d' % elemsn.sum()) gelem = [] for el, en in zip(elems, elemsn): gelem += [el] * en if ff[7].startswith("Selective"): lx = 9 else: lx = 8 assert ff[lx - 1] in ["Direct", "Cartesian"] gf = ff[lx - 1] == "Direct" gff = [[g for g in f.split(' ') if len(g) != 0] for f in ff[lx:lx + elemsn.sum()]] gff = [f[:6] for f in gff] coords = np.array([[float(g) if g not in ['T', 'F'] else 0.0 for g in f] for f in gff]) rf = np.array([["relax" if g == 'T' else "fix" for g in f] for f in gff]) fxl = [iif for iif, f in enumerate(rf) if len(f) >= 4 and f[5] == "fix"] lf = num_list(fxl) print(' FIX = %s' % lf) if gf: coords = coords[:, 0:3].dot(cell) else: coords = coords[:, 0:3] assert len(gelem) == len(coords) c = Cluster(elemsn.sum(), np.array(gelem), coords) c.label = "MAG:0:0.0:1" return c, dcell, fxl
def align(self): for c in self.clus: if c.surfnum == 0: c.center() reorient(c) else: dc = c.atoms[c.surfnum:] - c.atoms[c.surfnum:].mean( axis=0).reshape((1, 3)) v = reorient_2d(Cluster(n=len(dc), atoms=dc)) c.center() c.atoms = np.tensordot(c.atoms, v, axes=[1, 1])
def make_displacements(clu, freqs, disps, step=0.5, thres=None): k = imag(freqs, thres) if k == 0: return [] ki = np.zeros(k) rr = [] ws = np.array(map(lambda x: AtomicWeight.x[x], clu.elems)) ws = np.zeros(clu.atoms.shape) + 1.0 / np.sqrt(ws * 1822.88853).reshape(clu.n, 1) while True: r = Cluster(clu.n, elems=clu.elems) r.atoms = np.array(clu.atoms) for ii, i in enumerate(ki): fac = 1 if i == 1 else -1 dp = disps[ii].reshape(clu.atoms.shape) * ws dp = dp / np.linalg.norm(dp) vec = step * dp r.atoms += vec * fac r.tid = clu.tid r.label = "TRA:%d%s" % (r.tid, bin_tostr(ki)) r.energy = clu.energy rr.append(r) if not bin_next(ki): break return rr
def opt(self, nopt=-1): print('optimization ...') task = LBFGS(self.an * 3) cast_forward = np.cast[np.float64] cast_backword = np.cast[theano.config.floatX] cast = lambda f: lambda x: cast_forward(f(cast_backword(x))) task.p.eval = cast(self.opt_eval) task.p.evald = cast(self.opt_evald) task.log_file = 0 task.max_iter = 500 self.opts = [] self.finals = [] elems = self.clus[0].elems opt_data = self.opt_data nopt = opt_data[0].shape[0] if nopt == -1 else min( opt_data[0].shape[0], nopt) holder = 65 + 16 print('Number of structs: %d' % nopt) print(('%%%ds' % (holder)) % ('-' * holder, )) self.opts.append('#%9s %3s %15s %15s %15s %7s %7s ' % ('struct #', '%', 'start E', 'final E', 'delta E', 'steps', 'time')) print(self.opts[-1]) self.min_ener = None print(('%%%ds' % (holder)) % ('-' * holder, )) init_time = start_time = time.time() for idx, x, y in zip(range(0, len(opt_data[0])), opt_data[0], opt_data[1]): if idx == nopt: break task.start(cast_forward(x.flatten())) task.opt() fo, ff = task.traj[0][1], task.traj[-1][1] if self.clus[idx].label == 'Energy': label = 'OPT:{}'.format(idx) else: label = 'OPT:{}'.format(self.clus[idx].label) cc = Cluster(n=self.an, atoms=cast_backword(task.x.reshape(self.an, 3)), elems=elems, energy=ff, label=label) is_min = ' ' if self.test_opt(cc): self.finals.append(cc) if self.min_ener is None or ff < self.min_ener: is_min = '*' self.min_ener = ff else: is_min = 'F' finish_time = time.time() self.opts.append( '%10d %2d%% %15.8f %15.8f %15.8f %7d %7.2f%s' % (idx, idx * 100 / nopt, fo, ff, ff - fo, len( task.traj), finish_time - start_time, ' %s' % is_min)) print(self.opts[-1]) start_time = finish_time if idx == 9: total_time = (finish_time - init_time) / 10 * nopt print(('%%%ds' % (holder)) % ('estimated total time: %d:%02d:%02d' % (total_time / 3600, (total_time / 60) % 60, total_time % 60), )) print(('%%%ds' % (holder)) % ('-' * holder, )) print(('%%%ds' % (holder)) % ('-' * holder, )) total_time = finish_time - init_time print(('%%%ds' % (holder)) % ('total time used: %d:%02d:%02d' % (total_time / 3600, (total_time / 60) % 60, total_time % 60), )) print(('%%%ds' % (holder)) % ('-' * holder, ))
def build_gas_phase(self): if len(self.RotFunc) == 0: self.init_func() ca = self.clus[0] cb = self.clus[1] eles, ne = elem_num(ca.elems) eles2, ne2 = elem_num(list(ca.elems) * 2) nmax = self.nimages + 1 # initial measurement atomsa, atomsb = np.array(ca.atoms), np.array(cb.atoms) dd, _ = at_comp(atomsa, atomsb, ne, eles, self.max_diff + 0.1) dcl = np.array(self.max_trials) uph = self.trail_tolerance # build trial set dcf = None while dcl == self.max_trials and uph > 0.05: dcx = at_comp_list(atomsa, atomsb, ne, eles, dd + uph, dcl) dcx = dcx[:dcl - 1] if dcf is None or dcl == self.max_trials: dcf = np.array(dcx) uph /= np.sqrt(2) # solve rotation non-linear problem ntrial = len(dcf) nshort = 0 nhigh = 0 nsymm = 0 mdiff = self.max_diff finals = [] task = LBFGS(6) task.log_file = 0 task.max_iter = 500 for idx in dcf: caa, cab = np.array(atomsa), np.array(atomsb) caa = caa[idx - 1] xi = [0.0] * 6 task.p.eval = lambda x: self.RotFunc[0](caa, cab, x) task.p.evald = lambda x: self.RotFunc[1](caa, cab, x) task.start(xi) task.opt() caf = self.RotFunc[2](caa, task.x) hdiff = self.RotFunc[0](caa, cab, task.x) if hdiff > self.max_diff: nhigh += 1 continue if not check(ca.elems, caf, cab, nmax, self.acceptable_short_length_factor): nshort += 1 continue finals.append([caf, cab, hdiff]) if hdiff < mdiff: mdiff = hdiff finals.sort(key=lambda x: x[2]) if len(finals) > 1: ufinals = [finals[0]] ufinalsrf = [np.concatenate(finals[0][:2])] for ff in finals[1:]: crg = np.concatenate(ff[:2]) okay = True for crf in ufinalsrf: crd, _ = at_comp(crf, crg, ne2, eles2, self.path_min_diff + 0.02) if crd < self.path_min_diff: okay = False break if not okay: nsymm += 1 else: ufinals.append(ff) ufinalsrf.append(np.concatenate(ff[:2])) finals = ufinals # construct final images finalimgs = [] for caa, cab, _ in finals: imgs = [] for i in range(0, nmax + 1): cc = Cluster(ca.n, ca.elems) cc.atoms = caa + (cab - caa) * i / nmax cc.mag = None if i == 0: cc.mag, cc.energy = ca.mag, ca.energy elif i == nmax: cc.mag, cc.energy = cb.mag, cb.energy if cc.mag is None: cc.mag = 0.0 cc.label = "CONN-%d-%d:%%d.%d:%.2f" % (self.endp[0], self.endp[1], i, cc.mag) if i != 0 and i != nmax: pcc = PreOptimization(cc, self.adjusted_short_length_factor) pcc.opt() cc = pcc.traj[-1] imgs.append(cc) finalimgs.append(imgs) return finalimgs, [ self.endp[0], self.endp[1], len(finalimgs), ntrial, nshort, nhigh, nsymm, mdiff ]