Write restart command to savefile

This commit is contained in:
Ian Jauslin 2023-04-12 19:05:01 -04:00
parent 0b0894839d
commit c00c311528
5 changed files with 81 additions and 7 deletions

View File

@ -1,5 +1,6 @@
#include <stdbool.h> #include <stdbool.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include "io.h" #include "io.h"
#include "navier-stokes.h" #include "navier-stokes.h"
@ -113,3 +114,41 @@ int read_u(_Complex double* u, int K1, int K2, FILE* file){
return 0; 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;
}

View File

@ -9,4 +9,7 @@ int write_u(_Complex double* u, int K1, int K2, FILE* file);
// read u from file // read u from file
int read_u(_Complex double* u, int K1, int K2, FILE* file); int read_u(_Complex double* u, int K1, int K2, FILE* file);
// remove an entry from params string (inplace)
int remove_entry(char* param_str, char* entry);
#endif #endif

View File

@ -13,6 +13,8 @@
#include "init.h" #include "init.h"
#include "int_tools.h" #include "int_tools.h"
#include "io.h"
// structure to store parameters, to make it easier to pass parameters to CLI functions // structure to store parameters, to make it easier to pass parameters to CLI functions
typedef struct nstrophy_parameters { typedef struct nstrophy_parameters {
double init_en; // initial value for the energy for ins and enstrophy for rns double init_en; // initial value for the energy for ins and enstrophy for rns
@ -135,6 +137,11 @@ int main (
print_params(parameters, initfile_str, stderr); print_params(parameters, initfile_str, stderr);
print_params(parameters, initfile_str, stdout); print_params(parameters, initfile_str, stdout);
// free initfile_str
if(initfile_str!=NULL){
free(initfile_str);
}
// run command // run command
if (command==COMMAND_UK){ if (command==COMMAND_UK){
uk(parameters.K1, parameters.K2, parameters.N1, parameters.N2, parameters.nsteps, parameters.nu, parameters.delta, parameters.L, u0, g, parameters.irreversible, parameters.print_freq, parameters.starting_time, nthreads, savefile); uk(parameters.K1, parameters.K2, parameters.N1, parameters.N2, parameters.nsteps, parameters.nu, parameters.delta, parameters.L, u0, g, parameters.irreversible, parameters.print_freq, parameters.starting_time, nthreads, savefile);
@ -142,7 +149,7 @@ int main (
else if(command==COMMAND_EEA){ else if(command==COMMAND_EEA){
// register signal handler to handle aborts // register signal handler to handle aborts
signal(SIGINT, sig_handler); signal(SIGINT, sig_handler);
eea(parameters.K1, parameters.K2, parameters.N1, parameters.N2, parameters.nsteps, parameters.nu, parameters.delta, parameters.L, u0, g, parameters.irreversible, parameters.print_freq, parameters.avg_window, parameters.starting_time, nthreads, savefile); eea(parameters.K1, parameters.K2, parameters.N1, parameters.N2, parameters.nsteps, parameters.nu, parameters.delta, parameters.L, u0, g, parameters.irreversible, parameters.print_freq, parameters.avg_window, parameters.starting_time, nthreads, savefile, (char*)argv[0], param_str, savefile_str);
} }
else if(command==COMMAND_QUIET){ else if(command==COMMAND_QUIET){
quiet(parameters.K1, parameters.K2, parameters.N1, parameters.N2, parameters.nsteps, parameters.nu, parameters.delta, parameters.L, u0, g, parameters.irreversible, nthreads, savefile); quiet(parameters.K1, parameters.K2, parameters.N1, parameters.N2, parameters.nsteps, parameters.nu, parameters.delta, parameters.L, u0, g, parameters.irreversible, nthreads, savefile);
@ -217,6 +224,7 @@ int print_params(
return 0; return 0;
} }
// read command line arguments // read command line arguments
#define CP_FLAG_PARAMS 1 #define CP_FLAG_PARAMS 1
#define CP_FLAG_NTHREADS 2 #define CP_FLAG_NTHREADS 2
@ -570,7 +578,8 @@ int set_parameter(
// matches any argument that starts with 'file:' // matches any argument that starts with 'file:'
else if (strncmp(rhs,"file:",5)==0){ else if (strncmp(rhs,"file:",5)==0){
parameters->init=INIT_FILE; parameters->init=INIT_FILE;
*initfile_str=(char*)rhs+5; *initfile_str=calloc(sizeof(char), strlen(rhs)-5);
strcpy(*initfile_str, rhs+5);
} }
else{ else{
fprintf(stderr, "error: unrecognized initial condition '%s'\n",rhs); fprintf(stderr, "error: unrecognized initial condition '%s'\n",rhs);

View File

@ -2,6 +2,7 @@
#include "io.h" #include "io.h"
#include <math.h> #include <math.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
// compute solution as a function of time // compute solution as a function of time
int uk( int uk(
@ -93,7 +94,11 @@ int eea(
unsigned int running_avg_window, unsigned int running_avg_window,
unsigned int starting_time, unsigned int starting_time,
unsigned int nthreads, unsigned int nthreads,
FILE* savefile FILE* savefile,
// for interrupt recovery
char* cmd_string,
char* params_string,
char* savefile_string
){ ){
_Complex double* u; _Complex double* u;
_Complex double* tmp1; _Complex double* tmp1;
@ -186,9 +191,9 @@ int eea(
avg_print_short_en+=enstrophy/short_len; avg_print_short_en+=enstrophy/short_len;
} }
if(t % print_freq ==0 && t>0){ if(t % print_freq ==0 && t>starting_time){
// compute averages // compute averages
if (t > running_avg_window) { if (t > running_avg_window + starting_time) {
avg_e=save_print_short_e[nr_print_in_window]*((double)short_len/running_avg_window); avg_e=save_print_short_e[nr_print_in_window]*((double)short_len/running_avg_window);
avg_a=save_print_short_a[nr_print_in_window]*((double)short_len/running_avg_window); avg_a=save_print_short_a[nr_print_in_window]*((double)short_len/running_avg_window);
avg_en=save_print_short_en[nr_print_in_window]*((double)short_len/running_avg_window); avg_en=save_print_short_en[nr_print_in_window]*((double)short_len/running_avg_window);
@ -226,7 +231,7 @@ int eea(
} }
} }
if(t>running_avg_window && t%print_freq==0){ if(t>running_avg_window + starting_time && t%print_freq==0){
fprintf(stderr,"%d % .8e % .8e % .8e % .8e % .8e % .8e % .8e\n",t,t*delta, avg_a, avg_e, avg_en, alpha, energy, enstrophy); fprintf(stderr,"%d % .8e % .8e % .8e % .8e % .8e % .8e % .8e\n",t,t*delta, avg_a, avg_e, avg_en, alpha, energy, enstrophy);
printf("%8d % .15e % .15e % .15e % .15e % .15e % .15e % .15e\n",t,t*delta, avg_a, avg_e, avg_en, alpha, energy, enstrophy); printf("%8d % .15e % .15e % .15e % .15e % .15e % .15e % .15e\n",t,t*delta, avg_a, avg_e, avg_en, alpha, energy, enstrophy);
} }
@ -237,6 +242,24 @@ int eea(
if (savefile==NULL){ if (savefile==NULL){
savefile=stderr; savefile=stderr;
} }
fprintf(savefile,"# Interrupted computation. Resume with\n");
// command to resume
fprintf(savefile,"#! ");
fprintf(savefile, cmd_string);
// params
// allocate buffer for params
char* params=calloc(sizeof(char), strlen(params_string));
strcpy(params, params_string);
remove_entry(params, "starting_time");
remove_entry(params, "init");
remove_entry(params, "nsteps");
fprintf(savefile," -p \"%s;starting_time=%u;nsteps=%u;init=file:%s\"", params, t+1, nsteps-t-1, savefile_string);
free(params);
fprintf(savefile," energy\n");
break; break;
} }
} }

View File

@ -20,7 +20,7 @@ typedef struct fft_vects {
int uk( int K1, int K2, int N1, int N2, unsigned int nsteps, double nu, double delta, double L, _Complex double* u0, _Complex double* g, bool irreversible, unsigned int print_freq, unsigned int starting_time, unsigned int nthreadsl, FILE* savefile); int uk( int K1, int K2, int N1, int N2, unsigned int nsteps, double nu, double delta, double L, _Complex double* u0, _Complex double* g, bool irreversible, unsigned int print_freq, unsigned int starting_time, unsigned int nthreadsl, FILE* savefile);
// compute energy, enstrophy and alpha // compute energy, enstrophy and alpha
int eea( int K1, int K2, int N1, int N2, unsigned int nsteps, double nu, double delta, double L, _Complex double* u0, _Complex double* g, bool irreversible, unsigned int print_freq, unsigned int running_avg_window, unsigned int starting_time, unsigned int nthreads, FILE* savefile); int eea( int K1, int K2, int N1, int N2, unsigned int nsteps, double nu, double delta, double L, _Complex double* u0, _Complex double* g, bool irreversible, unsigned int print_freq, unsigned int running_avg_window, unsigned int starting_time, unsigned int nthreads, FILE* savefile, char* cmd_string, char* params_string, char* savefile_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, unsigned int nsteps, double nu, double delta, double L, _Complex double* u0, _Complex double* g, bool irreversible, unsigned int nthreads, FILE* savefile); int quiet( int K1, int K2, int N1, int N2, unsigned int nsteps, double nu, double delta, double L, _Complex double* u0, _Complex double* g, bool irreversible, unsigned int nthreads, FILE* savefile);