hmesh -= 1. if rank == 0: print('RSD factor is : ', rsdfac) ## ##rankweight = sum(masswt.compute()) ##totweight = comm.allreduce(rankweight) ##rankweight = sum((masswt**2).compute()) ##totweight2 = comm.allreduce(rankweight) #noise = bs**3 / (hmesh.csum()**2 / (hmesh**2).csum()) noise = None if rank == 0: print('Noise : ', noise) ######################################### #dynamics stages = numpy.linspace(0.01, aa, nsteps, endpoint=True) if pmdisp: dynamic_model = NBodyModel(cosmo, truth_pm, B=B, steps=stages) else: dynamic_model = ZAModel(cosmo, truth_pm, B=B, steps=stages) if rank == 0: print(dynamic_model) #noise if stage2 is not None: truth_noise_model = mapnoise.ThermalNoise(truth_pm, seed=100, aa=aa, att=stage2, spread=spread, hex=hex, limk=2, Ns=Ndish) else: truth_noise_model = mapnoise.ThermalNoise(truth_pm, seed=None,
def solve(Nmesh, x0, rtol, run, Nsm): pm = truth_pm.resize(Nmesh=(Nmesh, Nmesh, Nmesh)) atol = pm.Nmesh.prod() * rtol x0 = pm.upsample(x0, keep_mean=True) #data = data_n.downsample(pm) #IDEAL no noise limit data = data_p.downsample(pm) # smooth the data. This breaks the noise model but we don't need it # for lower resolution anyways. sml = pm.BoxSize[0] / Nmesh * Nsm dynamic_model = ZAModel(cosmo, truth_pm, B=B, steps=stages) mock_model = map.MockModel(dynamic_model) # an approximate noise model, due to smoothing this is correct only at large scale. noise_model = truth_noise_model.downsample(pm) obj = map.SmoothedObjective(mock_model, noise_model, data, prior_ps=pk, sml=sml)#, noised=noised) prior, chi2 = obj.get_code().compute(['prior', 'chi2'], init={'parameters': data.s}) if pm.comm.rank == 0: print(prior, chi2) # for 2d chi2 is close to total pixels. fit_p = mock_model.make_observable(data.s) r = obj.evaluate(fit_p, data) try: os.makedirs(optfolder + '%s' % run) except: pass try: os.makedirs(optfolder + '%s/2pt' % run) except: pass obj.save_report(r, optfolder + "%s/truth.png" % run) dg.save_2ptreport(r, optfolder + "%s/2pt/truth.png" % run, pm) optimizer = LBFGS(m=10, diag_update=scalar_diag) prob = obj.get_problem(atol=atol, precond=UseComplexSpaceOptimizer) def monitor(state): if pm.comm.rank == 0: print(state) if state.nit % 5 == 0: fit_p = mock_model.make_observable(state['x']) if state.nit % 20 == 0: fit_p.save(optfolder + '%s/%04d/fit_p' % (run, state['nit'])) r = obj.evaluate(fit_p, data) #obj.save_report(r, optfolder + "%s/%s%02d-%04d.png"% (run, prefix, int(Nsm*10), state['nit'])) dg.save_report(r, optfolder + "%s/%s_N%02d-%04d.png"% (run, prefix, int(Nsm*10), state['nit']), pm) dg.save_2ptreport(r, optfolder + "%s/2pt/%s_N%02d-%04d.png"% (run, prefix, int(Nsm*10), state['nit']), pm) if pm.comm.rank == 0: print('saved') state = optimizer.minimize(prob, x0=x0, monitor=monitor) fit_p = mock_model.make_observable(state['x']) fit_p.save(optfolder + '%s/best-fit' % run) r = obj.evaluate(fit_p, data) obj.save_report(r, optfolder + "%s/%s%02d-best-fit.png" % (run, prefix, int(Nsm*10))) dg.save_2ptreport(r, optfolder + "%s/2pt/%s_N%02d-best-fit.png" % (run, prefix, int(Nsm*10)), pm) return state.x