Exemple #1
0
void cosmo_init_particles( unsigned seed )
{
    fftwf_real *data = new fftwf_real[ nres * (nres+2) ];
    fftwf_complex *cdata = reinterpret_cast<fftwf_complex*>(data);
    
    fftwf_real *data2 = new fftwf_real[ nres * (nres+2) ];
    fftwf_complex *cdata2 = reinterpret_cast<fftwf_complex*>(data2);
    
    gsl_rng	*RNG = gsl_rng_alloc( gsl_rng_mt19937 );
	gsl_rng_set( RNG, seed );
	
    
    
    fftwf_plan plan, iplan, plan2, iplan2;
    plan  = fftwf_plan_dft_r2c_2d( nres, nres, data, cdata, FFTW_MEASURE ),
    iplan = fftwf_plan_dft_c2r_2d( nres, nres, cdata, data, FFTW_MEASURE );
    
    plan2  = fftwf_plan_dft_r2c_2d( nres, nres, data2, cdata2, FFTW_MEASURE ),
    iplan2 = fftwf_plan_dft_c2r_2d( nres, nres, cdata2, data2, FFTW_MEASURE );
    
    /////////////////////////////////
    
    int nresp = nres/2+1;
    float kfac = 2.0*M_PI/boxlength;
    float gaussran1, gaussran2;
    
    float fftnorm = 1.0f / (float)nres * (2.0f*M_PI/boxlength);
    
    for( int i=0; i<nres; ++i )
        for( int j=0; j<nres; ++j )
        {
            int idx = i*(nres+2)+j;
            data[idx] = gsl_ran_ugaussian_ratio_method( RNG ) / nres;
        }
    fftwf_execute( plan );
    
    /////////////////////////////////
    
    
    for( int i=0; i<nres; ++i )
        for( int j=0; j<nresp; ++j )
        {
            float kx = i>=nresp? (float)(i-nres)*kfac : (float)i*kfac;
            float ky = (float)j*kfac;
            
            float kk = sqrtf(kx*kx+ky*ky);
            
            int idx = i*nresp+j;
            
            float ampk = cosmo_get_amp_k( kk ); //*sqrtf(kk);
            
            if( kk >= nresp*kfac )
                ampk = 0.0;
            
            cdata[idx][0] *= ampk * fftnorm;
            cdata[idx][1] *= ampk * fftnorm;
            
            
        }
      
    
    // insert code to make random numbers independent of resolution (have rectangle outliens)
    
    
    float dx = boxlength / nres;
    float vfact = ComputeVFact( 1.0f/(1.0f+g_zstart));
    
    /////////////////////////////////
    // generate x-component
    for( int i=0; i<nres; ++i )
        for( int j=0; j<nresp; ++j )
        {
            float kx = i>=nresp? (float)(i-nres)*kfac : (float)i*kfac;
            float ky = (float)j*kfac;
            
            float kk = sqrtf(kx*kx+ky*ky);
            
            int idx = i*nresp+j; // (a+ib) * ik = iak -bk
            
            cdata2[idx][0] = kx/kk/kk * cdata[idx][1];
            cdata2[idx][1] = -kx/kk/kk * cdata[idx][0];
        }
    
    cdata2[0][0] = 0.0f;
    cdata2[0][1] = 0.0f;
    
    fftwf_execute( iplan2 );
    
    for( int i=0; i<nres; ++i )
        for( int j=0; j<nres; ++j )
        {
            int idx = i*(nres+2)+j;
            int ii = i*nres+j;
            P[ii].x = (float)i*dx + data2[idx];
            P[ii].vx = data2[idx] * vfact;
            P[ii].id = ii;
            P[ii].acc[0] = 0.0f;
            
        }
    
    /////////////////////////////////
    // generate y-component
    for( int i=0; i<nres; ++i )
        for( int j=0; j<nresp; ++j )
        {
            float kx = i>=nresp? (float)(i-nres)*kfac : (float)i*kfac;
            float ky = (float)j*kfac;
            
            float kk = sqrtf(kx*kx+ky*ky);
            
            int idx = i*nresp+j;
            
            cdata2[idx][0] = ky/kk/kk * cdata[idx][1];
            cdata2[idx][1] = -ky/kk/kk * cdata[idx][0];
        }
    
    cdata2[0][0] = 0.0f;
    cdata2[0][1] = 0.0f;
    
    fftwf_execute( iplan2 );
    
    for( int i=0; i<nres; ++i )
        for( int j=0; j<nres; ++j )
        {
            int idx = i*(nres+2)+j;
            int ii = i*nres+j;
            P[ii].y = (float)j*dx + data2[idx];
            P[ii].vy = data2[idx] * vfact;
            P[ii].acc[1] = 0.0f;
        }
    
    /////////////////////////////////
    
    delete[] data;
    delete[] data2;
    fftwf_destroy_plan(plan);
    fftwf_destroy_plan(iplan);
    fftwf_destroy_plan(plan2);
    fftwf_destroy_plan(iplan2);
    gsl_rng_free( RNG );
}
double
test_ugaussian_ratio_method (void)
{
  return gsl_ran_ugaussian_ratio_method (r_global);
}