save_state to its own function

This commit is contained in:
Ian Jauslin 2024-03-13 11:24:47 +01:00
parent 0599f69dc7
commit 41a5a4ba3f
4 changed files with 128 additions and 56 deletions

117
src/io.c
View File

@ -17,6 +17,7 @@ limitations under the License.
#include <stdbool.h> #include <stdbool.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "constants.cpp"
#include "io.h" #include "io.h"
#include "navier-stokes.h" #include "navier-stokes.h"
@ -225,3 +226,119 @@ int remove_entry(
return 0; return 0;
} }
// save state to savefile
int save_state(
_Complex double* u,
FILE* savefile,
int K1,
int K2,
const char* cmd_string,
const char* params_string,
const char* savefile_string,
const char* utfile_string,
FILE* utfile,
unsigned int command,
unsigned int algorithm,
double step,
double time,
unsigned int nthreads
){
if(savefile==NULL){
fprintf(stderr, "error while writing savefile: savefile not open\n");
return -1;
}
fprintf(savefile,"# Continue computation with\n");
// command to resume
fprintf(savefile,"#! ");
fprintf(savefile, cmd_string);
// params
// allocate buffer for params
if(params_string!=NULL) {
char* params=calloc(sizeof(char), strlen(params_string)+1);
strcpy(params, params_string);
remove_entry(params, "starting_time");
remove_entry(params, "init");
if(algorithm>ALGORITHM_ADAPTIVE_THRESHOLD){
remove_entry(params, "delta");
}
fprintf(savefile," -p \"%s;init=file:%s", params, savefile_string);
free(params);
// write delta if adaptive, and not writing binary
if(algorithm>ALGORITHM_ADAPTIVE_THRESHOLD && (savefile==stderr || savefile==stdout)){
fprintf(savefile,";delta=%.15e", step);
}
// write starting_time if not writing binary
if(savefile==stderr || savefile==stdout){
fprintf(savefile,";starting_time=%.15e", time);
}
fprintf(savefile,"\"");
}
// utfile
if(utfile!=NULL){
fprintf(savefile," -u \"%s\"", utfile_string);
}
// threads
if(nthreads!=1){
fprintf(savefile," -t %d", nthreads);
}
switch (command){
case COMMAND_UK:
fprintf(savefile," uk\n");
break;
case COMMAND_ENSTROPHY:
fprintf(savefile," enstrophy\n");
break;
case COMMAND_QUIET:
fprintf(savefile," quiet\n");
break;
case COMMAND_LYAPUNOV:
fprintf(savefile," lyapunov\n");
break;
case COMMAND_RESUME:
fprintf(savefile," resume\n");
break;
default:
fprintf(stderr,"error while writing savefile: unrecognized command %d\n", command);
return(-1);
break;
}
// save final u to savefile
if(savefile==stderr || savefile==stdout){
write_vec(u, K1, K2, savefile);
} else {
write_vec_bin(u, K1, K2, savefile);
// last binary entry: starting time
fwrite(&time, sizeof(double), 1, savefile);
// extra binary data for adaptive algorithm
if(algorithm>ALGORITHM_ADAPTIVE_THRESHOLD){
fwrite(&step, sizeof(double), 1, savefile);
}
}
return 0;
}
// write u to utfile in plain text
int write_utfile(
_Complex double* u,
FILE* utfile,
int K1,
int K2
){
if(utfile==NULL){
fprintf(stderr, "error while writing utfile: utfile is not open\n");
return -1;
}
write_vec(u, K1, K2, utfile);
return 0;
}

View File

@ -30,4 +30,9 @@ int read_vec_bin(_Complex double* u, int K1, int K2, FILE* file);
// remove an entry from params string (inplace) // remove an entry from params string (inplace)
int remove_entry(char* param_str, char* entry); int remove_entry(char* param_str, char* entry);
// save state to savefile
int save_state(_Complex double* u, FILE* savefile, int K1, int K2, const char* cmd_string, const char* params_string, const char* savefile_string, const char* utfile_string, FILE* utfile, unsigned int command, unsigned int algorithm, double step, double time, unsigned int nthreads);
// write u to utfile in plain text
int write_utfile(_Complex double* u, FILE* utfile, int K1, int K2);
#endif #endif

View File

@ -149,10 +149,10 @@ int enstrophy(
FILE* savefile, FILE* savefile,
FILE* utfile, FILE* utfile,
// for interrupt recovery // for interrupt recovery
char* cmd_string, const char* cmd_string,
char* params_string, const char* params_string,
char* savefile_string, const char* savefile_string,
char* utfile_string const char* utfile_string
){ ){
_Complex double* u; _Complex double* u;
_Complex double* tmp1; _Complex double* tmp1;
@ -248,57 +248,7 @@ int enstrophy(
} }
if(savefile!=NULL){ if(savefile!=NULL){
fprintf(savefile,"# Continue computation with\n"); save_state(u, savefile, K1, K2, cmd_string, params_string, savefile_string, utfile_string, utfile, COMMAND_ENSTROPHY, algorithm, step, time, nthreads);
// command to resume
fprintf(savefile,"#! ");
fprintf(savefile, cmd_string);
// params
// allocate buffer for params
if(params_string!=NULL) {
char* params=calloc(sizeof(char), strlen(params_string)+1);
strcpy(params, params_string);
remove_entry(params, "starting_time");
remove_entry(params, "init");
if(algorithm>ALGORITHM_ADAPTIVE_THRESHOLD){
remove_entry(params, "delta");
}
fprintf(savefile," -p \"%s;init=file:%s", params, savefile_string);
free(params);
// write delta if adaptive, and not writing binary
if(algorithm>ALGORITHM_ADAPTIVE_THRESHOLD && (savefile==stderr || savefile==stdout)){
fprintf(savefile,";delta=%.15e", step);
}
// write starting_time if not writing binary
if(savefile==stderr || savefile==stdout){
fprintf(savefile,";starting_time=%.15e", time);
}
fprintf(savefile,"\"");
}
// utfile
if(utfile!=NULL){
fprintf(savefile," -u \"%s\"", utfile_string);
}
// threads
if(nthreads!=1){
fprintf(savefile," -t %d", nthreads);
}
fprintf(savefile," enstrophy\n");
// save final u to savefile
if(savefile==stderr || savefile==stdout){
write_vec(u, K1, K2, savefile);
} else {
write_vec_bin(u, K1, K2, savefile);
// last binary entry: starting time
fwrite(&time, sizeof(double), 1, savefile);
// extra binary data for adaptive algorithm
if(algorithm>ALGORITHM_ADAPTIVE_THRESHOLD){
fwrite(&step, sizeof(double), 1, savefile);
}
}
} }
// save final u to utfile in txt format // save final u to utfile in txt format

View File

@ -34,7 +34,7 @@ typedef struct fft_vects {
int uk( int K1, int K2, int N1, int N2, double final_time, double nu, double delta, double L, double adaptive_tolerance, double adaptive_factor, double max_delta, unsigned int adaptive_norm, _Complex double* u0, _Complex double* g, bool irreversible, bool keep_en_cst, double target_en, unsigned int algorithm, double print_freq, double starting_time, unsigned int nthreadsl, FILE* savefile); int uk( int K1, int K2, int N1, int N2, double final_time, double nu, double delta, double L, double adaptive_tolerance, double adaptive_factor, double max_delta, unsigned int adaptive_norm, _Complex double* u0, _Complex double* g, bool irreversible, bool keep_en_cst, double target_en, unsigned int algorithm, double print_freq, double starting_time, unsigned int nthreadsl, FILE* savefile);
// compute enstrophy and alpha // compute enstrophy and alpha
int enstrophy( int K1, int K2, int N1, int N2, double final_time, double nu, double delta, double L, double adaptive_tolerance, double adaptive_factor, double max_delta, unsigned int adaptive_norm, _Complex double* u0, _Complex double* g, bool irreversible, bool keep_en_cst, double target_en, unsigned int algorithm, double print_freq, double starting_time, unsigned int nthreads, FILE* savefile, FILE* utfile, char* cmd_string, char* params_string, char* savefile_string, char* utfile_string); int enstrophy( int K1, int K2, int N1, int N2, double final_time, double nu, double delta, double L, double adaptive_tolerance, double adaptive_factor, double max_delta, unsigned int adaptive_norm, _Complex double* u0, _Complex double* g, bool irreversible, bool keep_en_cst, double target_en, unsigned int algorithm, double print_freq, double starting_time, unsigned int nthreads, FILE* savefile, FILE* utfile, const char* cmd_string, const char* params_string, const char* savefile_string, const char* utfile_string);
// compute solution as a function of time, but do not print anything (useful for debugging) // compute solution as a function of time, but do not print anything (useful for debugging)
int quiet( int K1, int K2, int N1, int N2, double final_time, double nu, double delta, double L, double adaptive_tolerance, double adaptive_factor, double max_delta, unsigned int adaptive_norm, double starting_time, _Complex double* u0, _Complex double* g, bool irreversible, bool keep_en_cst, double target_en, unsigned int algorithm, unsigned int nthreads, FILE* savefile); int quiet( int K1, int K2, int N1, int N2, double final_time, double nu, double delta, double L, double adaptive_tolerance, double adaptive_factor, double max_delta, unsigned int adaptive_norm, double starting_time, _Complex double* u0, _Complex double* g, bool irreversible, bool keep_en_cst, double target_en, unsigned int algorithm, unsigned int nthreads, FILE* savefile);