212 lines
4.3 KiB
C
212 lines
4.3 KiB
C
#include <stdbool.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include "io.h"
|
|
#include "navier-stokes.h"
|
|
|
|
// write complex vector indexed by k1,k2 to file
|
|
int write_vec(_Complex double* vec, int K1, int K2, FILE* file){
|
|
int kx,ky;
|
|
|
|
// do nothing if there is no file
|
|
if(file==NULL){
|
|
return 0;
|
|
}
|
|
|
|
for(kx=0;kx<=K1;kx++){
|
|
for(ky=(kx>0 ? -K2 : 1);ky<=K2;ky++){
|
|
fprintf(file,"% 3d % 3d % .15e % .15e\n",kx,ky,__real__ vec[klookup_sym(kx,ky,K2)],__imag__ vec[klookup_sym(kx,ky,K2)]);
|
|
}
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
// write complex vector indexed by k1,k2 to file in binary format
|
|
int write_vec_bin(_Complex double* vec, int K1, int K2, FILE* file){
|
|
// do nothing if there is no file
|
|
if(file==NULL){
|
|
return 0;
|
|
}
|
|
|
|
fwrite(vec, sizeof(_Complex double), K1*(2*K2+1)+K2, file);
|
|
|
|
return 0;
|
|
}
|
|
|
|
// read complex vector indexed by k1,k2 from file
|
|
int read_vec(_Complex double* out, int K1, int K2, FILE* file){
|
|
int kx,ky;
|
|
double r,i;
|
|
char* line;
|
|
unsigned int len=256;
|
|
unsigned int pos=0;
|
|
char* line_realloc;
|
|
char c;
|
|
int ret;
|
|
unsigned int counter=0;
|
|
bool commented=false;
|
|
|
|
// error if there is no file (this should not happen)
|
|
if (file==NULL){
|
|
fprintf(stderr,"error reading input from file (this is a bug, contact Ian at ian.jauslin@rutgers.edu!)\n");
|
|
return -1;
|
|
}
|
|
|
|
// allocate line buffer
|
|
line=calloc(sizeof(char), len);
|
|
|
|
while(1){
|
|
c=fgetc(file);
|
|
|
|
// end of file
|
|
if (feof(file)){
|
|
break;
|
|
}
|
|
|
|
// newline: read line and reset buffer
|
|
if(c=='\n' || c=='\r'){
|
|
// increment line counter
|
|
counter++;
|
|
|
|
// read entry
|
|
// ignore empty lines
|
|
if(pos>0){
|
|
ret=sscanf(line, "%d %d %le %le", &kx, &ky, &r, &i);
|
|
|
|
// errors
|
|
if(ret!=4){
|
|
fprintf(stderr, "warning: line %d does not match the input format: '%s', skipping\n", counter, line);
|
|
}
|
|
else{
|
|
if(kx>K1 || kx<-K1 || ky>K2 || ky<-K2){
|
|
fprintf(stderr, "warning: reading line %d: kx or ky out of bounds: %d,%d, skipping\n", counter, kx, ky);
|
|
}
|
|
else if (kx<0){
|
|
fprintf(stderr, "warning: reading line %d: kx should be >=0, got %d, skipping\n", counter, kx);
|
|
}
|
|
else if (kx==0 && ky<=0){
|
|
fprintf(stderr, "warning: reading line %d: if kx==0 then ky should be >0, got kx=%d ky=%d, skipping\n", counter, kx, ky);
|
|
}
|
|
else{
|
|
// set output
|
|
out[klookup_sym(kx, ky, K2)]=r+i*I;
|
|
}
|
|
}
|
|
}
|
|
|
|
// reset buffer
|
|
pos=0;
|
|
line[pos]='\0';
|
|
commented=false;
|
|
}
|
|
// comment: stop reading
|
|
else if (c=='#'){
|
|
commented=true;
|
|
}
|
|
// add to buffer (unless we are in a comment)
|
|
else if (!(commented)){
|
|
// check that there is room in buffer
|
|
if(pos==len){
|
|
// too short: reallocate
|
|
line_realloc=calloc(sizeof(char), 2*len);
|
|
for(pos=0;pos<len;pos++){
|
|
line_realloc[pos]=line[pos];
|
|
}
|
|
free(line);
|
|
line=line_realloc;
|
|
len=2*len;
|
|
}
|
|
|
|
// add c to line
|
|
line[pos]=c;
|
|
pos++;
|
|
line[pos]='\0';
|
|
}
|
|
}
|
|
|
|
free(line);
|
|
|
|
return 0;
|
|
}
|
|
|
|
// read complex vector indexed by k1,k2 from file in binary format
|
|
int read_vec_bin(_Complex double* out, int K1, int K2, FILE* file){
|
|
char c;
|
|
int ret;
|
|
|
|
// do nothing if there is no file
|
|
if(file==NULL){
|
|
return 0;
|
|
}
|
|
|
|
// seek past initial comments
|
|
while(true){
|
|
ret=fscanf(file, "%c", &c);
|
|
if (ret==1 && c=='#'){
|
|
// find endline
|
|
while(true){
|
|
ret=fscanf(file, "%c", &c);
|
|
// end of file
|
|
if (ret==0) {
|
|
// no data
|
|
return 0;
|
|
}
|
|
|
|
if (c=='\n'){
|
|
break;
|
|
}
|
|
}
|
|
} else {
|
|
if (ret==1){
|
|
// backtrack
|
|
fseek(file, -sizeof(char), SEEK_CUR);
|
|
}
|
|
// past comments
|
|
break;
|
|
}
|
|
}
|
|
|
|
fread(out, sizeof(_Complex double), K1*(2*K2+1)+K2, file);
|
|
|
|
return 0;
|
|
}
|
|
|
|
// remove an entry from params string (inplace)
|
|
int remove_entry(
|
|
char* param_str,
|
|
char* entry
|
|
){
|
|
char* ptr;
|
|
char* rw_ptr;
|
|
char* bfr;
|
|
char* entry_ptr=entry;
|
|
int go=1;
|
|
|
|
for(ptr=param_str, rw_ptr=ptr; *ptr!='\0'; ptr++){
|
|
for(bfr=ptr,entry_ptr=entry; *bfr==*entry_ptr; bfr++, entry_ptr++){
|
|
// check if reached end of entry
|
|
if(*(bfr+1)=='=' && *(entry_ptr+1)=='\0'){
|
|
go=0;
|
|
break;
|
|
}
|
|
}
|
|
if(go==1){
|
|
*rw_ptr=*ptr;
|
|
rw_ptr++;
|
|
}
|
|
//reset
|
|
if(*ptr==';'){
|
|
go=1;
|
|
}
|
|
}
|
|
*rw_ptr='\0';
|
|
|
|
// remove trailing ';'
|
|
if (param_str[strlen(param_str)-1]==';'){
|
|
*(param_str+(strlen(param_str)-1))='\0';
|
|
}
|
|
|
|
return 0;
|
|
}
|