From 41a5a4ba3fbbe108cd2bb4d046ade93daa5f164f Mon Sep 17 00:00:00 2001 From: Ian Jauslin Date: Wed, 13 Mar 2024 11:24:47 +0100 Subject: [PATCH] save_state to its own function --- src/io.c | 117 ++++++++++++++++++++++++++++++++++++++++++++ src/io.h | 5 ++ src/navier-stokes.c | 60 ++--------------------- src/navier-stokes.h | 2 +- 4 files changed, 128 insertions(+), 56 deletions(-) diff --git a/src/io.c b/src/io.c index fd05679..3287233 100644 --- a/src/io.c +++ b/src/io.c @@ -17,6 +17,7 @@ limitations under the License. #include #include #include +#include "constants.cpp" #include "io.h" #include "navier-stokes.h" @@ -225,3 +226,119 @@ int remove_entry( 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; +} diff --git a/src/io.h b/src/io.h index 2d7fd89..f2cc4b9 100644 --- a/src/io.h +++ b/src/io.h @@ -30,4 +30,9 @@ int read_vec_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); +// 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 diff --git a/src/navier-stokes.c b/src/navier-stokes.c index 28f1fcd..ae366e0 100644 --- a/src/navier-stokes.c +++ b/src/navier-stokes.c @@ -149,10 +149,10 @@ int enstrophy( FILE* savefile, FILE* utfile, // for interrupt recovery - char* cmd_string, - char* params_string, - char* savefile_string, - char* utfile_string + const char* cmd_string, + const char* params_string, + const char* savefile_string, + const char* utfile_string ){ _Complex double* u; _Complex double* tmp1; @@ -248,57 +248,7 @@ int enstrophy( } if(savefile!=NULL){ - 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); - } - - 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_state(u, savefile, K1, K2, cmd_string, params_string, savefile_string, utfile_string, utfile, COMMAND_ENSTROPHY, algorithm, step, time, nthreads); } // save final u to utfile in txt format diff --git a/src/navier-stokes.h b/src/navier-stokes.h index 68c7caa..8579eb7 100644 --- a/src/navier-stokes.h +++ b/src/navier-stokes.h @@ -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); // 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) 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);