def _compute(self): nsteps = len(self._pos) # Setup k vectors and tabulate rho k_sorted, k_selected = self._decimate_k() kmax = max(self.kvec.keys()) + self.dk cnt = [0 for k in k_sorted] # Note: actually rho_av is not calculated because it is negligible rho_av = [complex(0.,0.) for k in k_sorted] rho2_av = [complex(0.,0.) for k in k_sorted] cell_variable = is_cell_variable(self.trajectory) for i in range(0, nsteps, self.skip): # If cell changes we have to update if cell_variable: self._setup(i) k_sorted, k_selected = self._decimate_k() kmax = max(self.kvec.keys()) + self.dk expo = expo_sphere(self.k0, kmax, self._pos[i]) for kk, knorm in enumerate(k_sorted): for k in k_selected[kk]: ik = self.kvec[knorm][k] Ri = self._radius[i] mk = 4 * numpy.pi / knorm**3 * (numpy.sin(knorm*Ri) - (knorm*Ri) * numpy.cos(knorm*Ri)) rho = numpy.sum(mk*expo[...,0,ik[0]]*expo[...,1,ik[1]]*expo[...,2,ik[2]]) rho2_av[kk] += (rho * rho.conjugate()) cnt[kk] += 1 # Normalization. volume = numpy.average([s.cell.volume for s in self.trajectory]) self.grid = k_sorted self.value = [(rho2_av[kk] / cnt[kk] - rho_av[kk]*rho_av[kk].conjugate() / cnt[kk]**2).real / volume for kk in range(len(self.grid))] self.value_nonorm = [rho2_av[kk].real / cnt[kk] for kk in range(len(self.grid))]
def test_cell_variable(self): finp = '/tmp/test_utils.xyz' # with open(finp, 'w') as fh: # fh.write("""\ # 1 # step:0 cell:1.0,1.0,1.0 # A 1.0 -1.0 0.0 # 1 # step:1 cell:1.0,1.0,1.0 # A 1.0 -1.0 0.0 # 1 # step:2 cell:1.0,1.0,1.0 # A 1.0 -1.0 0.0 # 1 # step:3 cell:1.0,1.0,1.0 # A 1.0 -1.0 0.0 # """) # t = Trajectory(finp) # self.assertFalse(utils.is_cell_variable(t, tests=-1)) with open(finp, 'w') as fh: fh.write("""\ 1 step:0 cell:1.0,1.0,1.0 A 1.0 -1.0 0.0 1 step:1 cell:2.0,1.0,1.0 A 1.0 -1.0 0.0 1 step:2 cell:1.0,1.0,1.0 A 1.0 -1.0 0.0 1 step:3 cell:1.0,1.0,1.0 A 1.0 -1.0 0.0 """) with TrajectoryXYZ(finp) as th: self.assertTrue(utils.is_cell_variable(th, tests=-1)) self.assertFalse(utils.is_cell_variable(th, tests=1)) self.assertTrue(utils.is_cell_variable(th, tests=2))
def _compute(self): from atooms.trajectory.utils import is_cell_variable nsteps = len(self._pos_0) # Setup k vectors and tabulate rho k_sorted, k_selected = self._decimate_k() kmax = max(self.kvec.keys()) + self.dk cnt = [0 for k in k_sorted] rho_av = [complex(0., 0.) for k in k_sorted] rho2_av = [complex(0., 0.) for k in k_sorted] variable_cell = is_cell_variable(self.trajectory) for i in range(0, nsteps, self.skip): # If cell changes we have to update the wave vectors if variable_cell: self._setup(i) k_sorted, k_selected = self._decimate_k() kmax = max(self.kvec.keys()) + self.dk # Tabulate exponentials # Note: tabulating and computing takes about the same time if self._pos_0[i] is self._pos_1[i]: # Identical species expo_0 = expo_sphere(self.k0, kmax, self._pos_0[i]) expo_1 = expo_0 else: # Cross correlation expo_0 = expo_sphere(self.k0, kmax, self._pos_0[i]) expo_1 = expo_sphere(self.k0, kmax, self._pos_1[i]) for kk, knorm in enumerate(k_sorted): for k in k_selected[kk]: ik = self.kvec[knorm][k] # In the absence of a microscopic field, rho_av = (0, 0) if not self._field: if expo_0 is expo_1: # Identical species rho_0 = numpy.sum(expo_0[..., 0, ik[0]] * expo_0[..., 1, ik[1]] * expo_0[..., 2, ik[2]]) rho_1 = rho_0 else: # Cross correlation rho_0 = numpy.sum(expo_0[..., 0, ik[0]] * expo_0[..., 1, ik[1]] * expo_0[..., 2, ik[2]]) rho_1 = numpy.sum(expo_1[..., 0, ik[0]] * expo_1[..., 1, ik[1]] * expo_1[..., 2, ik[2]]) else: # We have a field as a weight rho_0 = numpy.sum( self._field[i] * expo_0[..., 0, ik[0]] * expo_0[..., 1, ik[1]] * expo_0[..., 2, ik[2]]) rho_1 = rho_0 rho_av[kk] += rho_0 rho2_av[kk] += (rho_0 * rho_1.conjugate()) cnt[kk] += 1 # Normalization. npart_0 = sum([p.shape[0] for p in self._pos_0]) / float(len(self._pos_0)) npart_1 = sum([p.shape[0] for p in self._pos_1]) / float(len(self._pos_1)) self.grid = k_sorted self.value, self.value_nonorm = [], [] for kk in range(len(self.grid)): norm = float(npart_0 * npart_1)**0.5 value = (rho2_av[kk] / cnt[kk] - rho_av[kk] * rho_av[kk].conjugate() / cnt[kk]**2).real self.value.append(value / norm) self.value_nonorm.append(value)
def _compute(self): from atooms.trajectory.utils import is_cell_variable nsteps = len(self._pos_0) # Setup k vectors and tabulate rho k_sorted, k_selected = self._decimate_k() kmax = max(self.kvec.keys()) + self.dk cnt = [0 for k in k_sorted] rho_av = [complex(0., 0.) for k in k_sorted] rho2_av = [complex(0., 0.) for k in k_sorted] variable_cell = is_cell_variable(self.trajectory) for i in range(0, nsteps, self.skip): # If cell changes we have to update the wave vectors if variable_cell: self._setup(i) k_sorted, k_selected = self._decimate_k() kmax = max(self.kvec.keys()) + self.dk # Tabulate exponentials # Note: tabulating and computing takes about the same time if self._pos_0[i] is self._pos_1[i]: # Identical species expo_0 = expo_sphere(self.k0, kmax, self._pos_0[i]) expo_1 = expo_0 else: # Cross correlation expo_0 = expo_sphere(self.k0, kmax, self._pos_0[i]) expo_1 = expo_sphere(self.k0, kmax, self._pos_1[i]) import atooms.postprocessing.fourierspace_wrap from atooms.postprocessing.fourierspace_wrap import fourierspace_module for kk, knorm in enumerate(k_sorted): ikvec = numpy.ndarray((3, len(k_selected[kk])), order='F', dtype=numpy.int32) i = 0 for k in k_selected[kk]: ikvec[:, i] = self.kvec[knorm][k] i += 1 rho = numpy.zeros(ikvec.shape[1], dtype=numpy.complex128) fourierspace_module.sk_bare(expo_0, ikvec, rho) rho_0 = rho rho_1 = rho rho2_av[kk] += numpy.sum(rho_0 * rho_1.conjugate()) cnt[kk] += rho.shape[0] # for kk, knorm in enumerate(k_sorted): # for k in k_selected[kk]: # ik = self.kvec[knorm][k] # # In the absence of a microscopic field, rho_av = (0, 0) # if not self._field: # if expo_0 is expo_1: # # Identical species # rho_0 = numpy.sum(expo_0[...,0,ik[0]] * # expo_0[...,1,ik[1]] * # expo_0[...,2,ik[2]]) # rho_1 = rho_0 # else: # # Cross correlation # rho_0 = numpy.sum(expo_0[...,0,ik[0]] * # expo_0[...,1,ik[1]] * # expo_0[...,2,ik[2]]) # rho_1 = numpy.sum(expo_1[...,0,ik[0]] * # expo_1[...,1,ik[1]] * # expo_1[...,2,ik[2]]) # else: # # We have a field as a weight # rho_0 = numpy.sum(self._field[i] * # expo_0[...,0,ik[0]] * # expo_0[...,1,ik[1]] * # expo_0[...,2,ik[2]]) # rho_1 = rho_0 # rho_av[kk] += rho_0 # rho2_av[kk] += (rho_0 * rho_1.conjugate()) # cnt[kk] += 1 # Normalization. npart_0 = sum([p.shape[0] for p in self._pos_0]) / float(len(self._pos_0)) npart_1 = sum([p.shape[0] for p in self._pos_1]) / float(len(self._pos_1)) self.grid = k_sorted self.value, self.value_nonorm = [], [] for kk in range(len(self.grid)): norm = float(npart_0 * npart_1)**0.5 value = (rho2_av[kk] / cnt[kk] - rho_av[kk] * rho_av[kk].conjugate() / cnt[kk]**2).real self.value.append(value / norm) self.value_nonorm.append(value)
def _compute(self): nsteps = len(self._pos_0) ndims = len(self.k0) # Setup k vectors and tabulate rho kgrid, selection = self.kgrid, self.selection kmax = max(self.kvector.keys()) + self.dk cnt = [0 for k in kgrid] rho_0_av = [complex(0., 0.) for k in kgrid] rho_1_av = [complex(0., 0.) for k in kgrid] rho2_av = [complex(0., 0.) for k in kgrid] variable_cell = is_cell_variable(self.trajectory) for i in progress(range(0, nsteps, self.skip), total=nsteps // self.skip): # If cell changes we have to update the wave vectors if variable_cell: self._setup(i) kgrid, selection = self._decimate_k() kmax = max(self.kvector.keys()) + self.dk # Tabulate exponentials # Note: tabulating and computing takes about the same time if self._pos_0[i] is self._pos_1[i]: # Identical species expo_0 = expo_sphere(self.k0, kmax, self._pos_0[i]) expo_1 = expo_0 else: # Cross correlation expo_0 = expo_sphere(self.k0, kmax, self._pos_0[i]) expo_1 = expo_sphere(self.k0, kmax, self._pos_1[i]) # Define weights if self._weight is None: weight_0, weight_1 = 1.0, 1.0 else: weight_0, weight_1 = self._weight_0[i], self._weight_1[i] # Nice spaghetti here for kk, knorm in enumerate(kgrid): for k in selection[kk]: ik = self.kvector[knorm][k] if expo_0 is expo_1: # Identical species if ndims == 3: rho_0 = numpy.sum( weight_0 * expo_0[..., 0, ik[0]] * expo_0[..., 1, ik[1]] * expo_0[..., 2, ik[2]]) elif ndims == 2: rho_0 = numpy.sum(weight_0 * expo_0[..., 0, ik[0]] * expo_0[..., 1, ik[1]]) else: tmp = weight_0 * expo_0[..., 0, ik[0]] for idim in range(1, ndims): tmp *= expo_0[..., idim, ik[idim]] rho_0 = numpy.sum(tmp) rho_1 = rho_0 else: # Cross correlation if ndims == 3: rho_0 = numpy.sum( weight_0 * expo_0[..., 0, ik[0]] * expo_0[..., 1, ik[1]] * expo_0[..., 2, ik[2]]) rho_1 = numpy.sum( weight_1 * expo_1[..., 0, ik[0]] * expo_1[..., 1, ik[1]] * expo_1[..., 2, ik[2]]) elif ndims == 2: rho_0 = numpy.sum(weight_0 * expo_0[..., 0, ik[0]] * expo_0[..., 1, ik[1]]) rho_1 = numpy.sum(weight_1 * expo_1[..., 0, ik[0]] * expo_1[..., 1, ik[1]]) else: tmp = weight_0 * expo_0[..., 0, ik[0]] for idim in range(ndims): tmp *= expo_0[..., idim, ik[idim]] rho_0 = numpy.sum(tmp) tmp = weight_0 * expo_1[..., 0, ik[0]] for idim in range(ndims): tmp *= expo_1[..., idim, ik[idim]] rho_1 = numpy.sum(tmp) # Cumulate averages rho_0_av[kk] += rho_0 rho_1_av[kk] += rho_1 rho2_av[kk] += (rho_0 * rho_1.conjugate()) cnt[kk] += 1 # In the absence of a microscopic field, the average density is zero. # We get rid of the average and compute <rho(k)rho*(k)>. if self._weight_0 is None: rho_0_av = [complex(0., 0.) for k in kgrid] rho_1_av = [complex(0., 0.) for k in kgrid] # Normalization npart_0 = sum([p.shape[0] for p in self._pos_0]) / float(len(self._pos_0)) npart_1 = sum([p.shape[0] for p in self._pos_1]) / float(len(self._pos_1)) self.grid = kgrid self.value, self.value_nonorm = [], [] for kk in range(len(self.grid)): norm = float(npart_0 * npart_1)**0.5 value = (rho2_av[kk] / cnt[kk] - rho_0_av[kk] * rho_1_av[kk].conjugate() / cnt[kk]**2).real self.value.append(value / norm) self.value_nonorm.append(value)
def _compute(self): from atooms.trajectory.utils import is_cell_variable try: from atooms.postprocessing.fourierspace_wrap import fourierspace_module except ImportError: _log.error('f90 wrapper missing or not functioning') raise nsteps = len(self._pos_0) # Setup k vectors and tabulate rho kgrid, selection = self.kgrid, self.selection kmax = max(self.kvector.keys()) + self.dk cnt = [0 for k in kgrid] rho_av = [complex(0., 0.) for k in kgrid] rho2_av = [complex(0., 0.) for k in kgrid] variable_cell = is_cell_variable(self.trajectory) for i in range(0, nsteps, self.skip): # If cell changes we have to update the wave vectors if variable_cell: self._setup(i) kgrid, selection = self._decimate_k() kmax = max(self.kvector.keys()) + self.dk # Tabulate exponentials # Note: tabulating and computing takes about the same time if self._pos_0[i] is self._pos_1[i]: # Identical species expo_0 = expo_sphere(self.k0, kmax, self._pos_0[i]) expo_1 = expo_0 else: # Cross correlation # TODO: cross correlation wont work expo_0 = expo_sphere(self.k0, kmax, self._pos_0[i]) expo_1 = expo_sphere(self.k0, kmax, self._pos_1[i]) for kk, knorm in enumerate(kgrid): ikvec = numpy.ndarray((3, len(selection[kk])), order='F', dtype=numpy.int32) i = 0 for k in selection[kk]: ikvec[:, i] = self.kvector[knorm][k] i += 1 rho = numpy.zeros(ikvec.shape[1], dtype=numpy.complex128) fourierspace_module.sk_bare(expo_0, ikvec, rho) rho_0 = rho rho_1 = rho rho2_av[kk] += numpy.sum(rho_0 * rho_1.conjugate()) cnt[kk] += rho.shape[0] # Normalization. npart_0 = sum([p.shape[0] for p in self._pos_0]) / float(len(self._pos_0)) npart_1 = sum([p.shape[0] for p in self._pos_1]) / float(len(self._pos_1)) self.grid = kgrid self.value, self.value_nonorm = [], [] for kk in range(len(self.grid)): norm = float(npart_0 * npart_1)**0.5 value = (rho2_av[kk] / cnt[kk] - rho_av[kk] * rho_av[kk].conjugate() / cnt[kk]**2).real self.value.append(value / norm) self.value_nonorm.append(value)