def LDL(param, X, pm, Nstep, baryon=True): fac = 1.0 * pm.Nmesh.prod() / pm.comm.allreduce(len(X), op=MPI.SUM) X = Displacement(param, X, pm, Nstep) #paint particle overdensity field layout = fastpm.decompose(X, pm) Xl = fastpm.exchange(X, layout) delta = fac * fastpm.paint(Xl, 1., None, pm) if baryon: #take parameters mu = linalg.take(param, 5*Nstep, axis=0) b1 = linalg.take(param, 5*Nstep+1, axis=0) b0 = linalg.take(param, 5*Nstep+2, axis=0) mu = mpi.allbcast(mu, comm=pm.comm) mu = linalg.broadcast_to(mu, eval(delta, lambda x : x.shape)) b1 = mpi.allbcast(b1, comm=pm.comm) b1 = linalg.broadcast_to(b1, eval(delta, lambda x : x.shape)) b0 = mpi.allbcast(b0, comm=pm.comm) b0 = linalg.broadcast_to(b0, eval(delta, lambda x : x.shape)) #Field transformation F = ReLU(b1 * (delta+1e-8) ** mu + b0) #definition of b0 is different from the paper else: F = delta return F
def model(self, x): layout = fastpm.decompose(x, pm=self.pm) x1 = fastpm.exchange(x, layout) y1 = fastpm.paint(x1, mass=1.0, layout=None, pm=self.pm) y = linalg.add(y1, self.mesh) # biasing a bit to get non-zero derivatives. return y
def LDL(param, X, pm, Nstep, baryon=True): fac = 1.0 * pm.Nmesh.prod() / pm.comm.allreduce(len(X), op=MPI.SUM) X = Displacement(param, X, pm, Nstep) #paint particle overdensity field layout = fastpm.decompose(X, pm) Xl = fastpm.exchange(X, layout) delta = fac * fastpm.paint(Xl, 1., None, pm) if baryon: #take parameters gamma = linalg.take(param, 5*Nstep, axis=0) bias0 = linalg.take(param, 5*Nstep+1, axis=0) bias1 = linalg.take(param, 5*Nstep+2, axis=0) gamma = mpi.allbcast(gamma, comm=pm.comm) gamma = linalg.broadcast_to(gamma, eval(delta, lambda x : x.shape)) bias0 = mpi.allbcast(bias0, comm=pm.comm) bias0 = linalg.broadcast_to(bias0, eval(delta, lambda x : x.shape)) bias1 = mpi.allbcast(bias1, comm=pm.comm) bias1 = linalg.broadcast_to(bias1, eval(delta, lambda x : x.shape)) #Field transformation F = ReLU(bias0 * (delta+1e-8) ** gamma + bias1) else: F = delta return F
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): y = fastpm.paint(self.pos, layout=None, mass=x, pm=self.pm) return y
def model(self, x): layout = fastpm.decompose(x, pm=self.pm) y = fastpm.paint(x, layout=layout, mass=1.0, pm=self.pm) y = linalg.add(y, self.mesh) # biasing a bit to get non-zero derivatives. return y