PLUTO
Turbulence.c
Go to the documentation of this file.
1 /* ***************************************************************
2  * Routine that generates a turbulent
3  * density field.
4  *
5  *
6  * ************************************************************** */
7 
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <math.h>
11 
12 #define Points_X 128
13 #define Points_Y 128
14 #define x_end 1.
15 #define Norm .1 /* Normalization of the spectrum power-law */
16 #define Spectral_Index -5./3. /* Index of the spectrum power-law: Kolmogorov -> 5/3 */
17 #define IR_Cutoff 2. /* Needed to avoid an IR divergence; 2 is a good value */
18 #define Minimum_Lambda 4 /* Grid points corresponding to minimum wavelength */
19 
20 #define frand()((double)(rand()+1)/(RAND_MAX+1.0))
21 
22 double **array_2D(int nx, int ny);
23 
24 int main(void)
25 {
26  int i, j, k, i2, j2, k2, te0, NXLim, NYLim;
27  double y_end, xdbl, NXLim_2, NYLim_2;
28  double twoPI, x1r, x2r, twopi_a, twopi_b, twopi_c, scrh1, scrh2;
29  double xL, xR;
30  double **Amplitude, **Phase;
31 
32  FILE *Out;
33 
34 /* --------------------------------------------------------
35  Grid generation
36  -------------------------------------------------------- */
37 
38  Out = fopen("grid0.out", "w");
39  fprintf(Out, "# GEOMETRY: CARTESIAN\n");
40 
41  fprintf(Out, "%d\n",Points_X);
42  for(i2 = 0; i2 < Points_X; i2++){
43  xL = -0.5 + (double)(x_end*(i2-0.5)/(Points_X - 1.));
44  xR = -0.5 + (double)(x_end*(i2+0.5)/(Points_X - 1.));
45  fprintf (Out, "%d %12.6e %12.6e \n",i2+1, xL, xR);
46  }
47 
48  fprintf(Out, "%d\n",Points_Y);
49  for(j2 = 0; j2 < Points_Y; j2++){
50  xL = -0.5 + (double)(x_end*(j2-0.5)/(Points_Y - 1.));
51  xR = -0.5 + (double)(x_end*(j2+0.5)/(Points_Y - 1.));
52  fprintf (Out, "%d %12.6e %12.6e \n",j2+1, xL, xR);
53  }
54 
55  fprintf(Out, "%d\n",1); /* write 3rd coordinates as well */
56  fprintf (Out, "1 0.0 1.0\n");
57  fclose(Out);
58 
59 /* --------------------------------------------------------
60  Data generation
61  -------------------------------------------------------- */
62 
63  te0 = time(NULL);
64  srand(te0);
65  NXLim = 2*((int)(Points_X/2./Minimum_Lambda));
66  NYLim = 2*((int)(Points_Y/2./Minimum_Lambda));
67  NXLim_2 = NXLim/2.;
68  NYLim_2 = NYLim/2.;
69  y_end = x_end*(Points_Y - 1.)/(Points_X - 1.);
70 
71  Amplitude = array_2D(NXLim + 1, NYLim + 1);
72  Phase = array_2D(NXLim + 1, NYLim + 1);
73  twoPI = 2.*3.14159265359;
74 
75  for(i = 0; i <= NXLim; i++) for(j = 0; j <= NYLim; j++){
76  x1r = frand();
77  x2r = frand();
78  xdbl = sqrt(-2.*log(x1r))*cos(twoPI*x2r);
79  Phase[i][j] = twoPI*frand();
80  scrh1 = IR_Cutoff*IR_Cutoff + pow(twoPI*(i - NXLim_2)/x_end,2.)
81  + pow(twoPI*(j - NYLim_2)/y_end,2.);
82  Amplitude[i][j] = sqrt(Norm*pow(scrh1, Spectral_Index/2.))*xdbl;
83  }
84  Out = fopen("rho0.dbl", "wb");
85 
86  for(j2 = 0; j2 < Points_Y; j2++){
87  twopi_b = twoPI/(Points_Y - 1.)*j2;
88  for(i2 = 0; i2 < Points_X; i2++){
89  twopi_a = twoPI/(Points_X - 1.)*i2;
90  xdbl = 0.;
91  for(i = NXLim + 1; i--; ){
92  scrh1 = twopi_a*(i - NXLim_2);
93  for(j = NYLim + 1; j--; ){
94  scrh2 = twopi_b*(j - NYLim_2);
95  xdbl += Amplitude[i][j]*cos(scrh1 + scrh2 + Phase[i][j]);
96  }
97  }
98  xdbl = exp(xdbl);
99  fwrite (&xdbl, sizeof(double), 1, Out);
100  }
101  }
102 
103  fclose(Out);
104  return;
105 }
106 
107 /* ************************************************************ *
108  * Functions for memmory Allocation
109  * ************************************************************ */
110 
111  double **array_2D(int nx, int ny)
112  {
113  int i;
114  double **m;
115 
116  m = (double **)malloc((size_t)nx*sizeof(double *));
117  m[0] = (double *)malloc((size_t)nx*ny*sizeof(double));
118 
119  for(i = 1; i < nx; i++) m[i] = m[i - 1] + ny;
120 
121  return m;
122  }
123 
124 
#define IR_Cutoff
Definition: Turbulence.c:17
#define Points_X
Definition: Turbulence.c:12
#define x_end
Definition: Turbulence.c:14
int main(void)
Definition: Turbulence.c:24
#define Minimum_Lambda
Definition: Turbulence.c:18
#define frand()
Definition: Turbulence.c:20
int j
Definition: analysis.c:2
int k
Definition: analysis.c:2
#define Points_Y
Definition: Turbulence.c:13
#define Spectral_Index
Definition: Turbulence.c:16
#define Norm
Definition: Turbulence.c:15
double ** array_2D(int nx, int ny)
Definition: Turbulence.c:111
int i
Definition: analysis.c:2