Nstrophy/src/init.c

95 lines
1.8 KiB
C
Raw Normal View History

2022-05-26 15:05:30 -04:00
#include "init.h"
#include "navier-stokes.h"
2022-05-27 16:09:17 -04:00
#include "io.h"
2022-05-26 15:05:30 -04:00
#include <stdlib.h>
#include <math.h>
// random initial condition
int init_random (
_Complex double* u0,
2023-04-05 22:41:19 -04:00
double init_en,
2022-05-26 15:05:30 -04:00
int K1,
int K2,
2023-04-05 22:41:19 -04:00
double L,
int seed,
bool irreversible
2022-05-26 15:05:30 -04:00
){
int kx,ky;
double rescale;
double x,y;
srand(seed);
// random init (set half, then the other half are the conjugates)
for(kx=0;kx<=K1;kx++){
for(ky=-K2;ky<=K2;ky++){
if (kx!=0 || ky>0){
x=-0.5+((double) rand())/RAND_MAX;
y=-0.5+((double) rand())/RAND_MAX;
u0[klookup(kx,ky,2*K1+1,2*K2+1)]=x+y*I;
u0[klookup(-kx,-ky,2*K1+1,2*K2+1)]=conj(u0[klookup(kx,ky,2*K1+1,2*K2+1)]);
}
}
}
2023-04-05 22:41:19 -04:00
if (irreversible) {
// fix the energy
rescale=compute_energy(u0, K1, K2);
} else {
// fix the enstrophy
rescale=compute_enstrophy(u0, K1, K2, L);
2022-05-26 15:05:30 -04:00
}
for(kx=-K1;kx<=K1;kx++){
for(ky=-K2;ky<=K2;ky++){
2023-04-05 22:41:19 -04:00
u0[klookup(kx,ky,2*K1+1,2*K2+1)]=u0[klookup(kx,ky,2*K1+1,2*K2+1)]*sqrt(init_en/rescale);
2022-05-26 15:05:30 -04:00
}
}
return 0;
}
// Gaussian initial condition
int init_gaussian (
_Complex double* u0,
2023-04-05 22:41:19 -04:00
double init_en,
2022-05-26 15:05:30 -04:00
int K1,
2023-04-05 22:41:19 -04:00
int K2,
double L,
bool irreversible
2022-05-26 15:05:30 -04:00
){
int kx,ky;
2023-04-05 22:41:19 -04:00
double rescale;
2022-05-26 15:05:30 -04:00
for(kx=-K1;kx<=K1;kx++){
for(ky=-K2;ky<=K2;ky++){
u0[klookup(kx,ky,2*K1+1,2*K2+1)]=(kx*kx+ky*ky)*exp(-(kx*kx+ky*ky));
}
}
2023-04-05 22:41:19 -04:00
if (irreversible) {
// fix the energy
rescale=compute_energy(u0, K1, K2);
} else {
// fix the enstrophy
rescale=compute_enstrophy(u0, K1, K2, L);
}
for(kx=-K1;kx<=K1;kx++){
for(ky=-K2;ky<=K2;ky++){
u0[klookup(kx,ky,2*K1+1,2*K2+1)]=u0[klookup(kx,ky,2*K1+1,2*K2+1)]*sqrt(init_en/rescale);
}
}
2022-05-26 15:05:30 -04:00
return 0;
}
2022-05-27 16:09:17 -04:00
// Initialize from file
int init_file (
_Complex double* u0,
int K1,
int K2,
FILE* initfile
){
read_u(u0, K1, K2, initfile);
return 0;
}