Replace argv strings with dstrings

This commit is contained in:
Ian Jauslin 2023-11-03 16:04:53 -04:00
parent dd9bd74c83
commit 04a15dd2c7
3 changed files with 121 additions and 113 deletions

View File

@ -185,9 +185,13 @@ int str_to_dstring(char* str, dstring* output){
str_len++; str_len++;
} }
dstring_init(output, str_len); dstring_init(output, str_len);
for(ptr=str;*ptr!='\0';ptr++){ str_to_dstring_noinit(str, output);
dstring_append(*ptr,output); return(0);
} }
int str_to_dstring_noinit(char* str, dstring* output){
// reset length
output->length=0;
dstring_append_str(str, output);
return(0); return(0);
} }

View File

@ -66,6 +66,7 @@ char* dstring_to_str_noinit(dstring* input);
// convert from char* // convert from char*
int str_to_dstring(char* str, dstring* output); int str_to_dstring(char* str, dstring* output);
int str_to_dstring_noinit(char* str, dstring* output);
// compare a dstring and a char* // compare a dstring and a char*
int dstring_cmp_str(dstring dstring, char* str); int dstring_cmp_str(dstring dstring, char* str);

View File

@ -24,6 +24,7 @@ limitations under the License.
#include "constants.cpp" #include "constants.cpp"
#include "driving.h" #include "driving.h"
#include "dstring.h"
#include "init.h" #include "init.h"
#include "int_tools.h" #include "int_tools.h"
#include "navier-stokes.h" #include "navier-stokes.h"
@ -62,12 +63,12 @@ int print_usage();
int print_params(nstrophy_parameters parameters, char* initfile_str, char* drivingfile_str, FILE* file); int print_params(nstrophy_parameters parameters, char* initfile_str, char* drivingfile_str, FILE* file);
// read command line arguments // read command line arguments
int read_args(int argc, const char* argv[], char** params, unsigned int* command, unsigned int* nthreads, char** savefile_str, char** utfile_str, char** resumefile_str); int read_args(int argc, const char* argv[], dstring* params, unsigned int* command, unsigned int* nthreads, dstring* savefile_str, dstring* utfile_str, dstring* resumefile_str);
int set_default_params(nstrophy_parameters* parameters); int set_default_params(nstrophy_parameters* parameters);
int read_params(char* param_str, nstrophy_parameters* parameters, char** initfile_str, char** drivingfile_str); int read_params(char* param_str, nstrophy_parameters* parameters, dstring* initfile_str, dstring* drivingfile_str);
int set_parameter(char* lhs, char* rhs, nstrophy_parameters* parameters, bool* setN1, bool* setN2, char** initfile_str, char** drivingfile_str); int set_parameter(char* lhs, char* rhs, nstrophy_parameters* parameters, bool* setN1, bool* setN2, dstring* initfile_str, dstring* drivingfile_str);
// read args from file // read args from file
int args_from_file(char** params, unsigned int* command, unsigned int* nthreads, char** savefile_str, char** utfile_str, char* file_str); int args_from_file(dstring* params, unsigned int* command, unsigned int* nthreads, dstring* savefile_str, dstring* utfile_str, char* file_str);
// set driving force // set driving force
_Complex double* set_driving(nstrophy_parameters parameters); _Complex double* set_driving(nstrophy_parameters parameters);
@ -99,23 +100,31 @@ int main (
int argc, int argc,
const char* argv[] const char* argv[]
){ ){
char* param_str=NULL; dstring param_str;
nstrophy_parameters parameters; nstrophy_parameters parameters;
int ret; int ret;
unsigned int command; unsigned int command;
unsigned int nthreads=1; unsigned int nthreads=1;
_Complex double* u0; _Complex double* u0;
_Complex double *g; _Complex double *g;
char* savefile_str=NULL; dstring savefile_str;
char* utfile_str=NULL; dstring utfile_str;
char* initfile_str=NULL; dstring initfile_str;
char* drivingfile_str=NULL; dstring drivingfile_str;
char* resumefile_str=NULL; dstring resumefile_str;
FILE* savefile=NULL; FILE* savefile=NULL;
FILE* utfile=NULL; FILE* utfile=NULL;
command=0; command=0;
// init strings
dstring_init(&param_str, 64);
dstring_init(&savefile_str, 64);
dstring_init(&utfile_str, 64);
dstring_init(&initfile_str, 64);
dstring_init(&drivingfile_str, 64);
dstring_init(&resumefile_str, 64);
// read command line arguments // read command line arguments
ret=read_args(argc, argv, &param_str, &command, &nthreads, &savefile_str, &utfile_str, &resumefile_str); ret=read_args(argc, argv, &param_str, &command, &nthreads, &savefile_str, &utfile_str, &resumefile_str);
if(ret<0){ if(ret<0){
@ -125,19 +134,19 @@ int main (
// set default params // set default params
set_default_params(&parameters); set_default_params(&parameters);
// read params // read params
ret=read_params(param_str, &parameters, &initfile_str, &drivingfile_str); ret=read_params(dstring_to_str_noinit(&param_str), &parameters, &initfile_str, &drivingfile_str);
if(ret<0){ if(ret<0){
return(-1); return(-1);
} }
// if command is 'resume', then read args from file // if command is 'resume', then read args from file
if(command==COMMAND_RESUME){ if(command==COMMAND_RESUME){
ret=args_from_file(&param_str, &command, &nthreads, &savefile_str, &utfile_str, resumefile_str); ret=args_from_file(&param_str, &command, &nthreads, &savefile_str, &utfile_str, dstring_to_str_noinit(&resumefile_str));
if(ret<0){ if(ret<0){
return(-1); return(-1);
} }
// read params // read params
ret=read_params(param_str, &parameters, &initfile_str, &drivingfile_str); ret=read_params(dstring_to_str_noinit(&param_str), &parameters, &initfile_str, &drivingfile_str);
if(ret<0){ if(ret<0){
return(-1); return(-1);
} }
@ -148,25 +157,25 @@ int main (
return(-1); return(-1);
} }
// reread params // reread params
ret=read_params(param_str, &parameters, &initfile_str, &drivingfile_str); ret=read_params(dstring_to_str_noinit(&param_str), &parameters, &initfile_str, &drivingfile_str);
if(ret<0){ if(ret<0){
return(-1); return(-1);
} }
} }
// open initfile // open initfile
if(initfile_str!=NULL){ if(initfile_str.length!=0){
parameters.initfile=fopen(initfile_str,"r"); parameters.initfile=fopen(dstring_to_str_noinit(&initfile_str),"r");
if(parameters.initfile==NULL){ if(parameters.initfile==NULL){
fprintf(stderr,"Error opening file '%s' for reading: %s\n", initfile_str, strerror(errno)); fprintf(stderr,"Error opening file '%s' for reading: %s\n", dstring_to_str_noinit(&initfile_str), strerror(errno));
return(-1); return(-1);
} }
} }
// open drivingfile // open drivingfile
if(drivingfile_str!=NULL){ if(drivingfile_str.length!=0){
parameters.drivingfile=fopen(drivingfile_str,"r"); parameters.drivingfile=fopen(dstring_to_str_noinit(&drivingfile_str),"r");
if(parameters.drivingfile==NULL){ if(parameters.drivingfile==NULL){
fprintf(stderr,"Error opening file '%s' for reading: %s\n", drivingfile_str, strerror(errno)); fprintf(stderr,"Error opening file '%s' for reading: %s\n", dstring_to_str_noinit(&drivingfile_str), strerror(errno));
return(-1); return(-1);
} }
} }
@ -186,35 +195,30 @@ int main (
} }
// open savefile (do this after closing init file) // open savefile (do this after closing init file)
if(savefile_str!=NULL){ if(savefile_str.length!=0){
savefile=fopen(savefile_str,"w"); savefile=fopen(dstring_to_str_noinit(&savefile_str),"w");
if(savefile==NULL){ if(savefile==NULL){
fprintf(stderr,"Error opening file '%s' for writing: %s\n", savefile_str, strerror(errno)); fprintf(stderr,"Error opening file '%s' for writing: %s\n", dstring_to_str_noinit(&savefile_str), strerror(errno));
return(-1); return(-1);
} }
} }
// open utfile (do this after closing init file) // open utfile (do this after closing init file)
if(utfile_str!=NULL){ if(utfile_str.length!=0){
utfile=fopen(utfile_str,"w"); utfile=fopen(dstring_to_str_noinit(&utfile_str),"w");
if(utfile==NULL){ if(utfile==NULL){
fprintf(stderr,"Error opening file '%s' for writing: %s\n", utfile_str, strerror(errno)); fprintf(stderr,"Error opening file '%s' for writing: %s\n", dstring_to_str_noinit(&utfile_str), strerror(errno));
return(-1); return(-1);
} }
} }
// print parameters // print parameters
print_params(parameters, initfile_str, drivingfile_str, stderr); print_params(parameters, dstring_to_str_noinit(&initfile_str), dstring_to_str_noinit(&drivingfile_str), stderr);
print_params(parameters, initfile_str, drivingfile_str, stdout); print_params(parameters, dstring_to_str_noinit(&initfile_str), dstring_to_str_noinit(&drivingfile_str), stdout);
// free initfile_str // free strings
if(initfile_str!=NULL){ dstring_free(initfile_str);
free(initfile_str); dstring_free(drivingfile_str);
}
// free drivingfile_str
if(drivingfile_str!=NULL){
free(drivingfile_str);
}
// run command // run command
if (command==COMMAND_UK){ if (command==COMMAND_UK){
@ -224,7 +228,7 @@ int main (
// register signal handler to handle aborts // register signal handler to handle aborts
signal(SIGINT, sig_handler); signal(SIGINT, sig_handler);
signal(SIGTERM, sig_handler); signal(SIGTERM, sig_handler);
enstrophy(parameters.K1, parameters.K2, parameters.N1, parameters.N2, parameters.final_time, parameters.nu, parameters.delta, parameters.L, parameters.adaptive_tolerance, parameters.adaptive_factor, parameters.max_delta, parameters.adaptive_norm, u0, g, parameters.irreversible, parameters.keep_en_cst, parameters.init_en, parameters.algorithm, parameters.print_freq, parameters.starting_time, nthreads, savefile, utfile, (char*)argv[0], param_str, savefile_str, utfile_str); enstrophy(parameters.K1, parameters.K2, parameters.N1, parameters.N2, parameters.final_time, parameters.nu, parameters.delta, parameters.L, parameters.adaptive_tolerance, parameters.adaptive_factor, parameters.max_delta, parameters.adaptive_norm, u0, g, parameters.irreversible, parameters.keep_en_cst, parameters.init_en, parameters.algorithm, parameters.print_freq, parameters.starting_time, nthreads, savefile, utfile, (char*)argv[0], dstring_to_str_noinit(&param_str), dstring_to_str_noinit(&savefile_str), dstring_to_str_noinit(&utfile_str));
} }
else if(command==COMMAND_QUIET){ else if(command==COMMAND_QUIET){
quiet(parameters.K1, parameters.K2, parameters.N1, parameters.N2, parameters.final_time, parameters.nu, parameters.delta, parameters.L, parameters.adaptive_tolerance, parameters.adaptive_factor, parameters.max_delta, parameters.adaptive_norm, parameters.starting_time, u0, g, parameters.irreversible, parameters.keep_en_cst, parameters.init_en, parameters.algorithm, nthreads, savefile); quiet(parameters.K1, parameters.K2, parameters.N1, parameters.N2, parameters.final_time, parameters.nu, parameters.delta, parameters.L, parameters.adaptive_tolerance, parameters.adaptive_factor, parameters.max_delta, parameters.adaptive_norm, parameters.starting_time, u0, g, parameters.irreversible, parameters.keep_en_cst, parameters.init_en, parameters.algorithm, nthreads, savefile);
@ -237,6 +241,11 @@ int main (
free(g); free(g);
free(u0); free(u0);
// free strings
dstring_free(param_str);
dstring_free(savefile_str);
dstring_free(utfile_str);
// close savefile // close savefile
if (savefile!=NULL){ if (savefile!=NULL){
fclose(savefile); fclose(savefile);
@ -362,12 +371,12 @@ int print_params(
int read_args( int read_args(
int argc, int argc,
const char* argv[], const char* argv[],
char** params, dstring* params,
unsigned int* command, unsigned int* command,
unsigned int* nthreads, unsigned int* nthreads,
char** savefile_str, dstring* savefile_str,
char** utfile_str, dstring* utfile_str,
char** resumefile_str dstring* resumefile_str
){ ){
int i; int i;
int ret; int ret;
@ -404,7 +413,7 @@ int read_args(
} }
// params // params
else if(flag==CP_FLAG_PARAMS){ else if(flag==CP_FLAG_PARAMS){
*params=(char*)argv[i]; str_to_dstring_noinit((char*)argv[i], params);
flag=0; flag=0;
} }
// nthreads // nthreads
@ -418,17 +427,17 @@ int read_args(
} }
// savefile // savefile
else if(flag==CP_FLAG_SAVEFILE){ else if(flag==CP_FLAG_SAVEFILE){
*savefile_str=(char*)argv[i]; str_to_dstring_noinit((char*)argv[i], savefile_str);
flag=0; flag=0;
} }
// savefile // savefile
else if(flag==CP_FLAG_UTFILE){ else if(flag==CP_FLAG_UTFILE){
*utfile_str=(char*)argv[i]; str_to_dstring_noinit((char*)argv[i], utfile_str);
flag=0; flag=0;
} }
// resume file // resume file
else if(flag==CP_FLAG_RESUME){ else if(flag==CP_FLAG_RESUME){
*resumefile_str=(char*)argv[i]; str_to_dstring_noinit((char*)argv[i], resumefile_str);
flag=0; flag=0;
} }
// computation to run // computation to run
@ -454,7 +463,7 @@ int read_args(
} }
// check that if the command is 'resume', then resumefile has been set // check that if the command is 'resume', then resumefile has been set
if(*command==COMMAND_RESUME && *resumefile_str==NULL){ if(*command==COMMAND_RESUME && resumefile_str->length==0){
fprintf(stderr, "error: 'resume' command used, but no resume file\n"); fprintf(stderr, "error: 'resume' command used, but no resume file\n");
print_usage(); print_usage();
return(-1); return(-1);
@ -498,8 +507,8 @@ int set_default_params(
int read_params( int read_params(
char* param_str, char* param_str,
nstrophy_parameters* parameters, nstrophy_parameters* parameters,
char** initfile_str, dstring* initfile_str,
char** drivingfile_str dstring* drivingfile_str
){ ){
int ret; int ret;
// pointer in params // pointer in params
@ -513,7 +522,6 @@ int read_params(
// whether lhs (false is rhs) // whether lhs (false is rhs)
bool lhs=true; bool lhs=true;
if (param_str!=NULL){
// init // init
buffer_lhs=calloc(sizeof(char),strlen(param_str)); buffer_lhs=calloc(sizeof(char),strlen(param_str));
lhs_ptr=buffer_lhs; lhs_ptr=buffer_lhs;
@ -568,7 +576,6 @@ int read_params(
// free vects // free vects
free(buffer_lhs); free(buffer_lhs);
free(buffer_rhs); free(buffer_rhs);
}
// if N not set explicitly, set it to the smallest power of 2 that is >3*K+1 (the fft is faster on vectors whose length is a power of 2) // if N not set explicitly, set it to the smallest power of 2 that is >3*K+1 (the fft is faster on vectors whose length is a power of 2)
if (!setN1){ if (!setN1){
@ -589,8 +596,8 @@ int set_parameter(
nstrophy_parameters* parameters, nstrophy_parameters* parameters,
bool* setN1, bool* setN1,
bool* setN2, bool* setN2,
char** initfile_str, dstring* initfile_str,
char** drivingfile_str dstring* drivingfile_str
){ ){
int ret; int ret;
@ -759,14 +766,12 @@ 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->driving=DRIVING_FILE; parameters->driving=DRIVING_FILE;
*drivingfile_str=calloc(sizeof(char), strlen(rhs)-5+1); str_to_dstring_noinit(rhs+5,drivingfile_str);
strcpy(*drivingfile_str, rhs+5);
} }
// matches any argument that starts with 'file_txt:' // matches any argument that starts with 'file_txt:'
else if (strncmp(rhs,"file_txt:",9)==0){ else if (strncmp(rhs,"file_txt:",9)==0){
parameters->driving=DRIVING_FILE_TXT; parameters->driving=DRIVING_FILE_TXT;
*drivingfile_str=calloc(sizeof(char), strlen(rhs)-9+1); str_to_dstring_noinit(rhs+9,drivingfile_str);
strcpy(*drivingfile_str, rhs+9);
} }
else{ else{
fprintf(stderr, "error: unrecognized driving force '%s'\n",rhs); fprintf(stderr, "error: unrecognized driving force '%s'\n",rhs);
@ -784,14 +789,12 @@ 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=calloc(sizeof(char), strlen(rhs)-5+1); str_to_dstring_noinit(rhs+5,initfile_str);
strcpy(*initfile_str, rhs+5);
} }
// matches any argument that starts with 'file_txt:' // matches any argument that starts with 'file_txt:'
else if (strncmp(rhs,"file_txt:",9)==0){ else if (strncmp(rhs,"file_txt:",9)==0){
parameters->init=INIT_FILE_TXT; parameters->init=INIT_FILE_TXT;
*initfile_str=calloc(sizeof(char), strlen(rhs)-9+1); str_to_dstring_noinit(rhs+9,initfile_str);
strcpy(*initfile_str, rhs+9);
} }
else{ else{
fprintf(stderr, "error: unrecognized initial condition '%s'\n",rhs); fprintf(stderr, "error: unrecognized initial condition '%s'\n",rhs);
@ -839,11 +842,11 @@ int set_parameter(
// read args from file // read args from file
int args_from_file( int args_from_file(
char** params, dstring* params,
unsigned int* command, unsigned int* command,
unsigned int* nthreads, unsigned int* nthreads,
char** savefile_str, dstring* savefile_str,
char** utfile_str, dstring* utfile_str,
char* file_str char* file_str
){ ){
char* line; char* line;