def PGD_correction(X, alpha, kl, ks, pm, q): layout = fastpm.decompose(X, pm) xl = fastpm.exchange(X, layout) rho = fastpm.paint(xl, 1.0, None, pm) fac = 1.0 * pm.Nmesh.prod() / pm.comm.allreduce(len(q)) rho = rho * fac rhok = fastpm.r2c(rho) p = fastpm.apply_transfer(rhok, PGDkernel(kl, ks)) r1 = [] for d in range(pm.ndim): dx1_c = fastpm.apply_transfer( p, fastpm.fourier_space_neg_gradient(d, pm, order=1)) dx1_r = fastpm.c2r(dx1_c) dx1l = fastpm.readout(dx1_r, xl, None) dx1 = fastpm.gather(dx1l, layout) r1.append(dx1) S = linalg.stack(r1, axis=-1) S = S * alpha return S
def model(self, x): compensation = self.pm.resampler.get_compensation() layout = vmadfastpm.decompose(self.w, self.pm) map = vmadfastpm.paint(self.w, x, layout, self.pm) y = map + self.xx # bias needed to avoid zero derivative # compensation for cic window c = vmadfastpm.r2c(y) c = vmadfastpm.apply_transfer(c, lambda k: compensation(k, 1.0), kind='circular') map = vmadfastpm.c2r(c) return map
def makemap(self, xy, w): """ paint projected particles to 2D mesh xy: particle positions in radians w: weighting = projection kernel """ if (self.mappm.affine.period != 0).any(): raise RuntimeError("The ParticeMesh object must be non-periodic") if self.mappm.ndim != 2: raise RuntimeError( "The ParticeMesh object must be 2 dimensional. ") compensation = self.mappm.resampler.get_compensation() layout = fastpm.decompose(xy, self.mappm) map = fastpm.paint(xy, w, layout, self.mappm) # compensation for cic window c = fastpm.r2c(map) c = fastpm.apply_transfer(c, lambda k: compensation(k, 1.0), kind='circular') map = fastpm.c2r(c) return map
def Displacement(param, X, pm, Nstep): #Lagrangian displacement #normalization constant for overdensity fac = 1.0 * pm.Nmesh.prod() / pm.comm.allreduce(len(X), op=MPI.SUM) for i in range(Nstep): #move the particles across different MPI ranks layout = fastpm.decompose(X, pm) xl = fastpm.exchange(X, layout) delta = fac * fastpm.paint(xl, 1.0, None, pm) #take parameters alpha = linalg.take(param, 5*i, axis=0) gamma = linalg.take(param, 5*i+1, axis=0) kh = linalg.take(param, 5*i+2, axis=0) kl = linalg.take(param, 5*i+3, axis=0) n = linalg.take(param, 5*i+4, axis=0) #delta**gamma gamma = mpi.allbcast(gamma, comm=pm.comm) gamma = linalg.broadcast_to(gamma, eval(delta, lambda x : x.shape)) delta = (delta+1e-8) ** gamma #Fourier transform deltak = fastpm.r2c(delta) #Green's operator in Fourier space Filter = Literal(pm.create(type='complex', value=1).apply(lambda k, v: k.normp(2, zeromode=1e-8) ** 0.5)) kh = mpi.allbcast(kh, comm=pm.comm) kh = linalg.broadcast_to(kh, eval(Filter, lambda x : x.shape)) kl = mpi.allbcast(kl, comm=pm.comm) kl = linalg.broadcast_to(kl, eval(Filter, lambda x : x.shape)) n = mpi.allbcast(n, comm=pm.comm) n = linalg.broadcast_to(n, eval(Filter, lambda x : x.shape)) Filter = - unary.exp(-Filter**2/kl**2) * unary.exp(-kh**2/Filter**2) * Filter**n Filter = compensate2factor(Filter) p = complex_mul(deltak, Filter) #gradient of potential r1 = [] for d in range(pm.ndim): dx1_c = fastpm.apply_transfer(p, fastpm.fourier_space_neg_gradient(d, pm, order=1)) dx1_r = fastpm.c2r(dx1_c) dx1l = fastpm.readout(dx1_r, xl, None) dx1 = fastpm.gather(dx1l, layout) r1.append(dx1) #displacement S = linalg.stack(r1, axis=-1) alpha = mpi.allbcast(alpha, comm=pm.comm) alpha = linalg.broadcast_to(alpha, eval(S, lambda x : x.shape)) S = S * alpha X = X+S return X
def model(self, x): c = fastpm.r2c(x) c = fastpm.apply_transfer(c, tf=transfer) r = fastpm.c2r(c) return r