[3756] | 1 | // Classes to compute 3D power spectrum
|
---|
| 2 | // R. Ansari - Nov 2008, May 2010
|
---|
| 3 |
|
---|
| 4 | #include "specpk.h"
|
---|
| 5 | #include "randr48.h"
|
---|
| 6 |
|
---|
| 7 | //------------------------------------
|
---|
| 8 | // Class SpectralShape
|
---|
| 9 | // -----------------------------------
|
---|
| 10 |
|
---|
| 11 | double Pnu1(double nu)
|
---|
| 12 | {
|
---|
| 13 | return ( sqrt(sqrt(nu)) / ((nu+1.0)/0.2) *
|
---|
| 14 | (1+0.2*cos(2*M_PI*(nu-2.)*0.15)*exp(-nu/50.)) );
|
---|
| 15 | }
|
---|
| 16 |
|
---|
| 17 | double Pnu2(double nu)
|
---|
| 18 | {
|
---|
| 19 | if (nu < 1.e-9) return 0.;
|
---|
| 20 | return ((1.-exp(-nu/0.5))/nu*(1+0.25*cos(2*M_PI*nu*0.1)*exp(-nu/20.)) );
|
---|
| 21 | }
|
---|
| 22 |
|
---|
| 23 |
|
---|
| 24 | double Pnu3(double nu)
|
---|
| 25 | {
|
---|
| 26 | return ( log(nu/100.+1)*(1+sin(2*M_PI*nu/300))*exp(-nu/4000) );
|
---|
| 27 | }
|
---|
| 28 |
|
---|
| 29 |
|
---|
| 30 | double Pnu4(double nu)
|
---|
| 31 | {
|
---|
| 32 | double x = (nu-0.5)/0.05;
|
---|
| 33 | double rc = 2*exp(-x*x);
|
---|
| 34 | x = (nu-3.1)/0.27;
|
---|
| 35 | rc += exp(-x*x);
|
---|
| 36 | x = (nu-7.6)/1.4;
|
---|
| 37 | rc += 0.5*exp(-x*x);
|
---|
| 38 | return ( rc+2.*exp(-x*x) );
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | //--------------------------------------------------
|
---|
| 42 | // -- SpectralShape class : test P(k) class
|
---|
| 43 | //--------------------------------------------------
|
---|
| 44 | // Constructor
|
---|
| 45 | SpectralShape::SpectralShape(int typ)
|
---|
| 46 | {
|
---|
| 47 | typ_=typ;
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | // Return the spectral power for a given wave number wk
|
---|
| 51 | double SpectralShape::operator() (double wk)
|
---|
| 52 | {
|
---|
| 53 | wk/=DeuxPI;
|
---|
| 54 | switch (typ_)
|
---|
| 55 | {
|
---|
| 56 | case 1:
|
---|
| 57 | return Pnu1(wk);
|
---|
| 58 | break;
|
---|
| 59 | case 2:
|
---|
| 60 | return Pnu2(wk);
|
---|
| 61 | break;
|
---|
| 62 | case 3:
|
---|
| 63 | return Pnu3(wk);
|
---|
| 64 | break;
|
---|
| 65 | case 4:
|
---|
| 66 | return Pnu4(wk);
|
---|
| 67 | break;
|
---|
| 68 | default :
|
---|
| 69 | {
|
---|
| 70 | // global shape
|
---|
| 71 | double csp = pow( (2*sin(sqrt(sqrt(wk/7.)))),2.);
|
---|
| 72 | if (csp < 0.) return 0.;
|
---|
| 73 |
|
---|
| 74 | // Adding some pics
|
---|
| 75 | double picpos[5] = {75.,150.,225.,300.,375.,};
|
---|
| 76 |
|
---|
| 77 | for(int k=0; k<5; k++) {
|
---|
| 78 | double x0 = picpos[k];
|
---|
| 79 | if ( (wk > x0-25.) && (wk < x0+25.) ) {
|
---|
| 80 | double x = (wk-x0);
|
---|
| 81 | csp *= (1.+0.5*exp(-(x*x)/(2.*5*5)));
|
---|
| 82 | break;
|
---|
| 83 | }
|
---|
| 84 | }
|
---|
| 85 | return csp;
|
---|
| 86 | }
|
---|
| 87 | break;
|
---|
| 88 | }
|
---|
| 89 | }
|
---|
| 90 | // Return a vector representing the power spectrum (for checking)
|
---|
| 91 | Histo SpectralShape::GetPk(int n)
|
---|
| 92 | {
|
---|
| 93 | if (n<16) n = 256;
|
---|
| 94 | Histo h(0.,1024.*DeuxPI,n);
|
---|
| 95 | for(int k=0; k<h.NBins(); k++) h(k) = Value((k+0.5)*h.BinWidth());
|
---|
| 96 | return h;
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | //--------------------------------------------------
|
---|
| 100 | // -- Four2DResponse class : test P(k) class
|
---|
| 101 |
|
---|
| 102 | //---------------------------------------------------------------
|
---|
| 103 | // -- Four3DPk class : 3D fourier amplitudes and power spectrum
|
---|
| 104 | //---------------------------------------------------------------
|
---|
| 105 | // Constructeur avec Tableau des coeff. de Fourier en argument
|
---|
| 106 | Four3DPk::Four3DPk(TArray< complex<TF> > & fourcoedd, RandomGeneratorInterface& rg)
|
---|
| 107 | : rg_(rg), fourAmp(fourcoedd)
|
---|
| 108 | {
|
---|
| 109 | SetPrtLevel();
|
---|
| 110 | SetCellSize();
|
---|
| 111 | }
|
---|
| 112 | // Constructor
|
---|
| 113 | Four3DPk::Four3DPk(RandomGeneratorInterface& rg, sa_size_t szx, sa_size_t szy, sa_size_t szz)
|
---|
| 114 | : rg_(rg), fourAmp(szx, szy, szz)
|
---|
| 115 | {
|
---|
| 116 | SetPrtLevel();
|
---|
| 117 | SetCellSize();
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 |
|
---|
| 121 | // Generate mass field Fourier Coefficient
|
---|
| 122 | void Four3DPk::ComputeFourierAmp(SpectralShape& pk)
|
---|
| 123 | {
|
---|
| 124 | // We generate a random gaussian real field
|
---|
| 125 | // fourAmp represent 3-D fourier transform of a real input array.
|
---|
| 126 | // The second half of the array along Y and Z contain negative frequencies
|
---|
| 127 | // double fnorm = 1./sqrt(2.*fourAmp.Size());
|
---|
| 128 | double fnorm = 1.;
|
---|
| 129 | double kxx, kyy, kzz;
|
---|
| 130 | // sa_size_t is large integer type
|
---|
| 131 | for(sa_size_t kz=0; kz<fourAmp.SizeZ(); kz++) {
|
---|
| 132 | kzz = (kz>fourAmp.SizeZ()/2) ? (double)(fourAmp.SizeZ()-kz)*dkz_ : (double)kz*dkz_;
|
---|
| 133 | for(sa_size_t ky=0; ky<fourAmp.SizeY(); ky++) {
|
---|
| 134 | kyy = (ky>fourAmp.SizeY()/2) ? (double)(fourAmp.SizeY()-ky)*dky_ : (double)ky*dky_;
|
---|
| 135 | for(sa_size_t kx=0; kx<fourAmp.SizeX(); kx++) {
|
---|
| 136 | double kxx=(double)kx*dkx_;
|
---|
| 137 | double wk = sqrt(kxx*kxx+kyy*kyy+kzz*kzz);
|
---|
| 138 | double amp = sqrt(pk(wk)*fnorm/2.);
|
---|
| 139 | fourAmp(kx, ky, kz) = complex<TF>(rg_.Gaussian(amp), rg_.Gaussian(amp)); // renormalize fourier coeff usin
|
---|
| 140 | }
|
---|
| 141 | }
|
---|
| 142 | }
|
---|
| 143 | if (prtlev_>0)
|
---|
| 144 | cout << " Four3DPk::ComputeFourierAmp() done ..." << endl;
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 | // Generate mass field Fourier Coefficient
|
---|
| 148 | void Four3DPk::ComputeNoiseFourierAmp(Four2DResponse& resp, bool crmask)
|
---|
| 149 | {
|
---|
| 150 | TMatrix<r_4> mask(fourAmp.SizeY(), fourAmp.SizeX());
|
---|
| 151 | // fourAmp represent 3-D fourier transform of a real input array.
|
---|
| 152 | // The second half of the array along Y and Z contain negative frequencies
|
---|
| 153 | double kxx, kyy, kzz;
|
---|
| 154 | // sa_size_t is large integer type
|
---|
| 155 | for(sa_size_t kz=0; kz<fourAmp.SizeZ(); kz++) {
|
---|
[3769] | 156 | kzz = (kz>fourAmp.SizeZ()/2) ? -(double)(fourAmp.SizeZ()-kz)*dkz_ : (double)kz*dkz_;
|
---|
[3756] | 157 | for(sa_size_t ky=0; ky<fourAmp.SizeY(); ky++) {
|
---|
[3769] | 158 | kyy = (ky>fourAmp.SizeY()/2) ? -(double)(fourAmp.SizeY()-ky)*dky_ : (double)ky*dky_;
|
---|
[3756] | 159 | for(sa_size_t kx=0; kx<fourAmp.SizeX(); kx++) {
|
---|
| 160 | double kxx=(double)kx*dkx_;
|
---|
| 161 | double rep = resp(kxx, kyy);
|
---|
| 162 | if (crmask&&(kz==0)) mask(ky,kx)=((rep<1.e-8)?9.e9:(1./rep));
|
---|
| 163 | if (rep<1.e-8) fourAmp(kx, ky, kz) = complex<TF>(9.e9,0.);
|
---|
| 164 | else {
|
---|
| 165 | double amp = 1./sqrt(rep)/sqrt(2.);
|
---|
| 166 | fourAmp(kx, ky, kz) = complex<TF>(rg_.Gaussian(amp), rg_.Gaussian(amp));
|
---|
| 167 | }
|
---|
| 168 | }
|
---|
| 169 | }
|
---|
| 170 | }
|
---|
| 171 | if (prtlev_>1) fourAmp.Show();
|
---|
| 172 | if (crmask) {
|
---|
| 173 | POutPersist po("mask.ppf");
|
---|
| 174 | po << mask;
|
---|
| 175 | }
|
---|
| 176 | if (prtlev_>0)
|
---|
| 177 | cout << " Four3DPk::ComputeNoiseFourierAmp() done ..." << endl;
|
---|
| 178 | }
|
---|
| 179 |
|
---|
| 180 | // Compute mass field from its Fourier Coefficient
|
---|
| 181 | TArray<TF> Four3DPk::ComputeMassDens()
|
---|
| 182 | {
|
---|
| 183 | TArray<TF> massdens;
|
---|
| 184 | // Backward fourier transform of the fourierAmp array
|
---|
| 185 | FFTWServer ffts(true);
|
---|
| 186 | ffts.setNormalize(true);
|
---|
| 187 | ffts.FFTBackward(fourAmp, massdens, true);
|
---|
| 188 | // cout << " Four3DPk::ComputeMassDens() done NbNeg=" << npbz << " / NPix=" << massDens.Size() << endl;
|
---|
| 189 | cout << " Four3DPk::ComputeMassDens() done NPix=" << massdens.Size() << endl;
|
---|
| 190 | return massdens;
|
---|
| 191 | }
|
---|
| 192 |
|
---|
| 193 | // Compute power spectrum as a function of wave number k
|
---|
| 194 | // cells with amp^2=re^2+im^2>s2cut are ignored
|
---|
| 195 | // Output : power spectrum (profile histogram)
|
---|
[3769] | 196 | HProf Four3DPk::ComputePk(double s2cut, int nbin, double kmin, double kmax)
|
---|
[3756] | 197 | {
|
---|
| 198 | // The second half of the array along Y (matrix rows) contain
|
---|
| 199 | // negative frequencies
|
---|
| 200 | // int nbh = sqrt(fourAmp.SizeX()*fourAmp.SizeX()+fourAmp.SizeY()*fourAmp.SizeY()/4.+fourAmp.SizeZ()*fourAmp.SizeY()/4.);
|
---|
| 201 | // The profile histogram will contain the mean value of FFT amplitude
|
---|
| 202 | // as a function of wave-number k = sqrt((double)(kx*kx+ky*ky))
|
---|
| 203 | // if (nbin < 1) nbin = nbh/2;
|
---|
[3769] | 204 | if ((kmax<0.)||(kmax<kmin)) {
|
---|
| 205 | kmin=0.;
|
---|
| 206 | double maxx=fourAmp.SizeX()*dkx_;
|
---|
| 207 | double maxy=fourAmp.SizeY()*dky_/2;
|
---|
| 208 | double maxz=fourAmp.SizeZ()*dkz_/2;
|
---|
| 209 | kmax=sqrt(maxx*maxx+maxy*maxy+maxz*maxz);
|
---|
| 210 | }
|
---|
| 211 | if (nbin<2) nbin=128;
|
---|
[3756] | 212 | HProf hp(kmin, kmax, nbin);
|
---|
| 213 | hp.SetErrOpt(false);
|
---|
| 214 | ComputePkCumul(hp, s2cut);
|
---|
| 215 | return hp;
|
---|
| 216 | }
|
---|
| 217 |
|
---|
| 218 | // Compute power spectrum as a function of wave number k
|
---|
| 219 | // Cumul dans hp - cells with amp^2=re^2+im^2>s2cut are ignored
|
---|
| 220 | void Four3DPk::ComputePkCumul(HProf& hp, double s2cut)
|
---|
| 221 | {
|
---|
| 222 |
|
---|
| 223 | // fourAmp represent 3-D fourier transform of a real input array.
|
---|
| 224 | // The second half of the array along Y and Z contain negative frequencies
|
---|
| 225 | double kxx, kyy, kzz;
|
---|
| 226 | // sa_size_t is large integer type
|
---|
| 227 | for(sa_size_t kz=0; kz<fourAmp.SizeZ(); kz++) {
|
---|
| 228 | kzz = (kz > fourAmp.SizeZ()/2) ? (double)(fourAmp.SizeZ()-kz)*dkz_ : (double)kz*dkz_;
|
---|
| 229 | for(sa_size_t ky=0; ky<fourAmp.SizeY(); ky++) {
|
---|
| 230 | kyy = (ky > fourAmp.SizeY()/2) ? (double)(fourAmp.SizeY()-ky)*dky_ : (double)ky*dky_;
|
---|
| 231 | for(sa_size_t kx=0; kx<fourAmp.SizeX(); kx++) {
|
---|
| 232 | double kxx=(double)kx*dkx_;
|
---|
| 233 | complex<TF> za = fourAmp(kx, ky, kz);
|
---|
| 234 | if (za.real()>8.e9) continue;
|
---|
| 235 | double wk = sqrt(kxx*kxx+kyy*kyy+kzz*kzz);
|
---|
| 236 | double amp2 = za.real()*za.real()+za.imag()*za.imag();
|
---|
| 237 | if ((s2cut>1.e-9)&&(amp2>s2cut)) continue;
|
---|
| 238 | hp.Add(wk, amp2);
|
---|
| 239 | }
|
---|
| 240 | }
|
---|
| 241 | }
|
---|
| 242 | return;
|
---|
| 243 | }
|
---|
| 244 |
|
---|
| 245 |
|
---|
| 246 |
|
---|
| 247 | //-----------------------------------------------------
|
---|
| 248 | // -- MassDist2D class : 2D mass distribution
|
---|
| 249 | //-----------------------------------------------------
|
---|
| 250 | // Constructor
|
---|
| 251 | MassDist2D::MassDist2D(GenericFunc& pk, int size, double meandens)
|
---|
| 252 | : pkSpec(pk) , sizeA((size>16)?size:16) , massDens(sizeA, sizeA),
|
---|
| 253 | meanRho(meandens) , fg_fourAmp(false) , fg_massDens(false)
|
---|
| 254 | {
|
---|
| 255 | }
|
---|
| 256 |
|
---|
| 257 | // To the computation job
|
---|
| 258 | void MassDist2D::Compute()
|
---|
| 259 | {
|
---|
| 260 | ComputeFourierAmp();
|
---|
| 261 | ComputeMassDens();
|
---|
| 262 | }
|
---|
| 263 |
|
---|
| 264 | // Generate mass field Fourier Coefficient
|
---|
| 265 | void MassDist2D::ComputeFourierAmp()
|
---|
| 266 | {
|
---|
| 267 | if (fg_fourAmp) return; // job already done
|
---|
| 268 | // We generate a random gaussian real field
|
---|
| 269 | double sigma = 1.;
|
---|
| 270 | // The following line fills the array by gaussian random numbers
|
---|
| 271 | //--Replaced-- massDens = RandomSequence(RandomSequence::Gaussian, 0., sigma);
|
---|
| 272 | // Can be replaced by
|
---|
| 273 | DR48RandGen rg;
|
---|
| 274 | for(sa_size_t ir=0; ir<massDens.NRows(); ir++) {
|
---|
| 275 | for(sa_size_t jc=0; jc<massDens.NCols(); jc++) {
|
---|
| 276 | massDens(ir, jc) = rg.Gaussian(sigma);
|
---|
| 277 | }
|
---|
| 278 | }
|
---|
| 279 | // --- End of random filling
|
---|
| 280 |
|
---|
| 281 | // Compute fourier transform of the random gaussian field -> white noise
|
---|
| 282 | FFTWServer ffts(true);
|
---|
| 283 | ffts.setNormalize(true);
|
---|
| 284 | ffts.FFTForward(massDens, fourAmp);
|
---|
| 285 |
|
---|
| 286 | // fourAmp represent 2-D fourier transform of a real input array.
|
---|
| 287 | // The second half of the array along Y (matrix rows) contain
|
---|
| 288 | // negative frequencies
|
---|
| 289 | // double fnorm = 1./sqrt(2.*fourAmp.Size());
|
---|
| 290 | // PUT smaller value for fnorm and check number of zeros
|
---|
| 291 | double fnorm = 1.;
|
---|
| 292 | // sa_size_t is large integer type
|
---|
| 293 | for(sa_size_t ky=0; ky<fourAmp.NRows(); ky++) {
|
---|
| 294 | double kyy = ky;
|
---|
| 295 | if (ky > fourAmp.NRows()/2) kyy = fourAmp.NRows()-ky; // negative frequencies
|
---|
| 296 | for(sa_size_t kx=0; kx<fourAmp.NCols(); kx++) {
|
---|
| 297 | double wk = sqrt((double)(kx*kx+kyy*kyy));
|
---|
| 298 | double amp = pkSpec(wk)*fnorm;
|
---|
| 299 | fourAmp(ky, kx) *= amp; // renormalize fourier coeff using
|
---|
| 300 | }
|
---|
| 301 | }
|
---|
| 302 | fg_fourAmp = true;
|
---|
| 303 | cout << " MassDist2D::ComputeFourierAmp() done ..." << endl;
|
---|
| 304 | }
|
---|
| 305 |
|
---|
| 306 | // Compute mass field from its Fourier Coefficient
|
---|
| 307 | void MassDist2D::ComputeMassDens()
|
---|
| 308 | {
|
---|
| 309 | if (fg_massDens) return; // job already done
|
---|
| 310 | if (!fg_fourAmp) ComputeFourierAmp(); // Check fourier amp generation
|
---|
| 311 |
|
---|
| 312 | // Backward fourier transform of the fourierAmp array
|
---|
| 313 | FFTWServer ffts(true);
|
---|
| 314 | ffts.setNormalize(true);
|
---|
| 315 | ffts.FFTBackward(fourAmp, massDens, true);
|
---|
| 316 | // We consider that massDens represents delta rho/rho
|
---|
| 317 | // rho = (delta rho/rho + 1) * MeanDensity
|
---|
| 318 | massDens += 1.;
|
---|
| 319 | // We remove negative values
|
---|
| 320 | sa_size_t npbz = 0;
|
---|
| 321 | for (sa_size_t i=0; i<massDens.NRows(); i++)
|
---|
| 322 | for (sa_size_t j=0; j<massDens.NCols(); j++)
|
---|
| 323 | if (massDens(i,j) < 0.) { npbz++; massDens(i,j) = 0.; }
|
---|
| 324 | massDens *= meanRho;
|
---|
| 325 | cout << " MassDist2D::ComputeMassDens() done NbNeg=" << npbz << " / NPix=" << massDens.Size() << endl;
|
---|
| 326 | }
|
---|
| 327 |
|
---|
| 328 | // Compute power spectrum as a function of wave number k
|
---|
| 329 | // Output : power spectrum (profile histogram)
|
---|
| 330 | HProf MassDist2D::ReconstructPk(int nbin)
|
---|
| 331 | {
|
---|
| 332 | // The second half of the array along Y (matrix rows) contain
|
---|
| 333 | // negative frequencies
|
---|
| 334 | int nbh = sqrt(2.0)*fourAmp.NCols();
|
---|
| 335 | // The profile histogram will contain the mean value of FFT amplitude
|
---|
| 336 | // as a function of wave-number k = sqrt((double)(kx*kx+ky*ky))
|
---|
| 337 | if (nbin < 1) nbin = nbh/2;
|
---|
| 338 | HProf hp(-0.5, nbh-0.5, nbin);
|
---|
| 339 | hp.SetErrOpt(false);
|
---|
| 340 |
|
---|
| 341 | for(int ky=0; ky<fourAmp.NRows(); ky++) {
|
---|
| 342 | double kyy = ky;
|
---|
| 343 | if (ky > fourAmp.NRows()/2) kyy = fourAmp.NRows()-ky; // negative frequencies
|
---|
| 344 | for(int kx=0; kx<fourAmp.NCols(); kx++) {
|
---|
| 345 | double wk = sqrt((double)(kx*kx+kyy*kyy));
|
---|
| 346 | complex<r_8> za = fourAmp(ky, kx);
|
---|
| 347 | double amp = sqrt(za.real()*za.real()+za.imag()*za.imag());
|
---|
| 348 | hp.Add(wk, amp);
|
---|
| 349 | }
|
---|
| 350 | }
|
---|
| 351 | return hp;
|
---|
| 352 | }
|
---|
| 353 |
|
---|