Ian Jauslin
summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorIan Jauslin <ian.jauslin@rutgers.edu>2023-04-14 15:01:52 -0400
committerIan Jauslin <ian.jauslin@rutgers.edu>2023-04-14 15:01:52 -0400
commitd5d5c15b7e582106b7394e492267bf4f16d4f47a (patch)
treeab6436d8e8eeca228296ddae9871edc2600feea0 /src
parentc00c3115284691e98d724e630aca7a41087502eb (diff)
binary io
Diffstat (limited to 'src')
-rw-r--r--src/init.c2
-rw-r--r--src/io.c54
-rw-r--r--src/io.h2
-rw-r--r--src/navier-stokes.c9
4 files changed, 64 insertions, 3 deletions
diff --git a/src/init.c b/src/init.c
index ca693f9..12646ea 100644
--- a/src/init.c
+++ b/src/init.c
@@ -88,6 +88,6 @@ int init_file (
int K2,
FILE* initfile
){
- read_u(u0, K1, K2, initfile);
+ read_u_bin(u0, K1, K2, initfile);
return 0;
}
diff --git a/src/io.c b/src/io.c
index cb14029..de52972 100644
--- a/src/io.c
+++ b/src/io.c
@@ -115,6 +115,60 @@ int read_u(_Complex double* u, int K1, int K2, FILE* file){
return 0;
}
+// write final entry to file in binary format
+int write_u_bin(_Complex double* u, int K1, int K2, FILE* file){
+ // do nothing if there is no file
+ if(file==NULL){
+ return 0;
+ }
+
+ fwrite(u, sizeof(_Complex double), (K1+1)*(2*K2+1), file);
+
+ return 0;
+}
+
+// read u from file in binary format
+int read_u_bin(_Complex double* u, 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(u, sizeof(_Complex double), (K1+1)*(2*K2+1), file);
+
+ return 0;
+}
+
// remove an entry from params string (inplace)
int remove_entry(
char* param_str,
diff --git a/src/io.h b/src/io.h
index 27110b7..bbf20ae 100644
--- a/src/io.h
+++ b/src/io.h
@@ -5,9 +5,11 @@
// write u to file
int write_u(_Complex double* u, int K1, int K2, FILE* file);
+int write_u_bin(_Complex double* u, int K1, int K2, FILE* file);
// read u from file
int read_u(_Complex double* u, int K1, int K2, FILE* file);
+int read_u_bin(_Complex double* u, int K1, int K2, FILE* file);
// remove an entry from params string (inplace)
int remove_entry(char* param_str, char* entry);
diff --git a/src/navier-stokes.c b/src/navier-stokes.c
index 5cee434..c751276 100644
--- a/src/navier-stokes.c
+++ b/src/navier-stokes.c
@@ -71,7 +71,7 @@ int uk(
}
// save final entry to savefile
- write_u(u, K1, K2, savefile);
+ write_u_bin(u, K1, K2, savefile);
ns_free_tmps(u, tmp1, tmp2, tmp3, fft1, fft2, ifft);
return(0);
@@ -242,6 +242,7 @@ int eea(
if (savefile==NULL){
savefile=stderr;
}
+
fprintf(savefile,"# Interrupted computation. Resume with\n");
// command to resume
fprintf(savefile,"#! ");
@@ -265,7 +266,11 @@ int eea(
}
// save final entry to savefile
- write_u(u, K1, K2, savefile);
+ if(savefile==stderr || savefile==stdout){
+ write_u(u, K1, K2, savefile);
+ } else {
+ write_u_bin(u, K1, K2, savefile);
+ }
if(running_avg_window!=0){
free(save_print_e);