Nstrophy/src/init.c

119 lines
2.4 KiB
C
Raw Normal View History

2023-05-10 19:33:29 -04:00
/*
Copyright 2017-2023 Ian Jauslin
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
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++){
2023-04-25 17:51:14 -04:00
for(ky=(kx>0 ? -K2 : 1);ky<=K2;ky++){
x=-0.5+((double) rand())/RAND_MAX;
y=-0.5+((double) rand())/RAND_MAX;
u0[klookup_sym(kx,ky,K2)]=x+y*I;
2022-05-26 15:05:30 -04:00
}
}
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
}
2023-04-11 18:45:45 -04:00
for(kx=0;kx<=K1;kx++){
2023-04-25 17:51:14 -04:00
for(ky=(kx>0 ? -K2 : 1);ky<=K2;ky++){
2023-04-11 18:45:45 -04:00
u0[klookup_sym(kx,ky,K2)]=u0[klookup_sym(kx,ky,K2)]*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
2023-04-11 18:45:45 -04:00
for(kx=0;kx<=K1;kx++){
2023-04-25 17:51:14 -04:00
for(ky=(kx>0 ? -K2 : 1);ky<=K2;ky++){
2023-04-11 18:45:45 -04:00
u0[klookup_sym(kx,ky,K2)]=(kx*kx+ky*ky)*exp(-(kx*kx+ky*ky));
2022-05-26 15:05:30 -04:00
}
}
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);
}
2023-04-11 18:45:45 -04:00
for(kx=0;kx<=K1;kx++){
2023-04-25 17:51:14 -04:00
for(ky=(kx>0 ? -K2 : 1);ky<=K2;ky++){
2023-04-11 18:45:45 -04:00
u0[klookup_sym(kx,ky,K2)]=u0[klookup_sym(kx,ky,K2)]*sqrt(init_en/rescale);
2023-04-05 22:41:19 -04:00
}
}
2022-05-26 15:05:30 -04:00
return 0;
}
2022-05-27 16:09:17 -04:00
// Initialize from file
// txt input
int init_file_txt (
_Complex double* u0,
int K1,
int K2,
FILE* initfile
){
read_vec(u0, K1, K2, initfile);
return 0;
}
// binary input
int init_file_bin (
2022-05-27 16:09:17 -04:00
_Complex double* u0,
int K1,
int K2,
FILE* initfile
){
read_vec_bin(u0, K1, K2, initfile);
2022-05-27 16:09:17 -04:00
return 0;
}