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++;
}
dstring_init(output, str_len);
for(ptr=str;*ptr!='\0';ptr++){
dstring_append(*ptr,output);
}
str_to_dstring_noinit(str, output);
return(0);
}
int str_to_dstring_noinit(char* str, dstring* output){
// reset length
output->length=0;
dstring_append_str(str, output);
return(0);
}

View File

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

View File

@ -24,6 +24,7 @@ limitations under the License.
#include "constants.cpp"
#include "driving.h"
#include "dstring.h"
#include "init.h"
#include "int_tools.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);
// 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 read_params(char* param_str, nstrophy_parameters* parameters, char** initfile_str, char** drivingfile_str);
int set_parameter(char* lhs, char* rhs, nstrophy_parameters* parameters, bool* setN1, bool* setN2, 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, dstring* initfile_str, dstring* drivingfile_str);
// 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
_Complex double* set_driving(nstrophy_parameters parameters);
@ -99,23 +100,31 @@ int main (
int argc,
const char* argv[]
){
char* param_str=NULL;
dstring param_str;
nstrophy_parameters parameters;
int ret;
unsigned int command;
unsigned int nthreads=1;
_Complex double* u0;
_Complex double *g;
char* savefile_str=NULL;
char* utfile_str=NULL;
char* initfile_str=NULL;
char* drivingfile_str=NULL;
char* resumefile_str=NULL;
dstring savefile_str;
dstring utfile_str;
dstring initfile_str;
dstring drivingfile_str;
dstring resumefile_str;
FILE* savefile=NULL;
FILE* utfile=NULL;
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
ret=read_args(argc, argv, &param_str, &command, &nthreads, &savefile_str, &utfile_str, &resumefile_str);
if(ret<0){
@ -125,19 +134,19 @@ int main (
// set default params
set_default_params(&parameters);
// 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){
return(-1);
}
// if command is 'resume', then read args from file
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){
return(-1);
}
// 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){
return(-1);
}
@ -148,25 +157,25 @@ int main (
return(-1);
}
// 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){
return(-1);
}
}
// open initfile
if(initfile_str!=NULL){
parameters.initfile=fopen(initfile_str,"r");
if(initfile_str.length!=0){
parameters.initfile=fopen(dstring_to_str_noinit(&initfile_str),"r");
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);
}
}
// open drivingfile
if(drivingfile_str!=NULL){
parameters.drivingfile=fopen(drivingfile_str,"r");
if(drivingfile_str.length!=0){
parameters.drivingfile=fopen(dstring_to_str_noinit(&drivingfile_str),"r");
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);
}
}
@ -186,35 +195,30 @@ int main (
}
// open savefile (do this after closing init file)
if(savefile_str!=NULL){
savefile=fopen(savefile_str,"w");
if(savefile_str.length!=0){
savefile=fopen(dstring_to_str_noinit(&savefile_str),"w");
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);
}
}
// open utfile (do this after closing init file)
if(utfile_str!=NULL){
utfile=fopen(utfile_str,"w");
if(utfile_str.length!=0){
utfile=fopen(dstring_to_str_noinit(&utfile_str),"w");
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);
}
}
// print parameters
print_params(parameters, initfile_str, 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), stderr);
print_params(parameters, dstring_to_str_noinit(&initfile_str), dstring_to_str_noinit(&drivingfile_str), stdout);
// free initfile_str
if(initfile_str!=NULL){
free(initfile_str);
}
// free drivingfile_str
if(drivingfile_str!=NULL){
free(drivingfile_str);
}
// free strings
dstring_free(initfile_str);
dstring_free(drivingfile_str);
// run command
if (command==COMMAND_UK){
@ -224,7 +228,7 @@ int main (
// register signal handler to handle aborts
signal(SIGINT, 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){
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(u0);
// free strings
dstring_free(param_str);
dstring_free(savefile_str);
dstring_free(utfile_str);
// close savefile
if (savefile!=NULL){
fclose(savefile);
@ -362,12 +371,12 @@ int print_params(
int read_args(
int argc,
const char* argv[],
char** params,
dstring* params,
unsigned int* command,
unsigned int* nthreads,
char** savefile_str,
char** utfile_str,
char** resumefile_str
dstring* savefile_str,
dstring* utfile_str,
dstring* resumefile_str
){
int i;
int ret;
@ -404,7 +413,7 @@ int read_args(
}
// params
else if(flag==CP_FLAG_PARAMS){
*params=(char*)argv[i];
str_to_dstring_noinit((char*)argv[i], params);
flag=0;
}
// nthreads
@ -418,17 +427,17 @@ int read_args(
}
// savefile
else if(flag==CP_FLAG_SAVEFILE){
*savefile_str=(char*)argv[i];
str_to_dstring_noinit((char*)argv[i], savefile_str);
flag=0;
}
// savefile
else if(flag==CP_FLAG_UTFILE){
*utfile_str=(char*)argv[i];
str_to_dstring_noinit((char*)argv[i], utfile_str);
flag=0;
}
// resume file
else if(flag==CP_FLAG_RESUME){
*resumefile_str=(char*)argv[i];
str_to_dstring_noinit((char*)argv[i], resumefile_str);
flag=0;
}
// computation to run
@ -454,7 +463,7 @@ int read_args(
}
// 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");
print_usage();
return(-1);
@ -498,8 +507,8 @@ int set_default_params(
int read_params(
char* param_str,
nstrophy_parameters* parameters,
char** initfile_str,
char** drivingfile_str
dstring* initfile_str,
dstring* drivingfile_str
){
int ret;
// pointer in params
@ -513,63 +522,61 @@ int read_params(
// whether lhs (false is rhs)
bool lhs=true;
if (param_str!=NULL){
// init
buffer_lhs=calloc(sizeof(char),strlen(param_str));
lhs_ptr=buffer_lhs;
*lhs_ptr='\0';
buffer_rhs=calloc(sizeof(char),strlen(param_str));
rhs_ptr=buffer_rhs;
*rhs_ptr='\0';
// init
buffer_lhs=calloc(sizeof(char),strlen(param_str));
lhs_ptr=buffer_lhs;
*lhs_ptr='\0';
buffer_rhs=calloc(sizeof(char),strlen(param_str));
rhs_ptr=buffer_rhs;
*rhs_ptr='\0';
for(ptr=param_str;*ptr!='\0';ptr++){
switch(*ptr){
case '=':
// reset buffer
rhs_ptr=buffer_rhs;
*rhs_ptr='\0';
lhs=false;
break;
case ';':
//set parameter
ret=set_parameter(buffer_lhs, buffer_rhs, parameters, &setN1, &setN2, initfile_str, drivingfile_str);
if(ret<0){
return ret;
}
// reset buffer
lhs_ptr=buffer_lhs;
*lhs_ptr='\0';
lhs=true;
break;
default:
// add to buffer
if (lhs){
*lhs_ptr=*ptr;
lhs_ptr++;
*lhs_ptr='\0';
}
else{
*rhs_ptr=*ptr;
rhs_ptr++;
*rhs_ptr='\0';
}
break;
}
}
// set last param
if (*param_str!='\0'){
for(ptr=param_str;*ptr!='\0';ptr++){
switch(*ptr){
case '=':
// reset buffer
rhs_ptr=buffer_rhs;
*rhs_ptr='\0';
lhs=false;
break;
case ';':
//set parameter
ret=set_parameter(buffer_lhs, buffer_rhs, parameters, &setN1, &setN2, initfile_str, drivingfile_str);
if(ret<0){
return ret;
}
// reset buffer
lhs_ptr=buffer_lhs;
*lhs_ptr='\0';
lhs=true;
break;
default:
// add to buffer
if (lhs){
*lhs_ptr=*ptr;
lhs_ptr++;
*lhs_ptr='\0';
}
else{
*rhs_ptr=*ptr;
rhs_ptr++;
*rhs_ptr='\0';
}
break;
}
// free vects
free(buffer_lhs);
free(buffer_rhs);
}
// set last param
if (*param_str!='\0'){
ret=set_parameter(buffer_lhs, buffer_rhs, parameters, &setN1, &setN2, initfile_str, drivingfile_str);
if(ret<0){
return ret;
}
}
// free vects
free(buffer_lhs);
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 (!setN1){
parameters->N1=smallest_pow2(3*(parameters->K1));
@ -589,8 +596,8 @@ int set_parameter(
nstrophy_parameters* parameters,
bool* setN1,
bool* setN2,
char** initfile_str,
char** drivingfile_str
dstring* initfile_str,
dstring* drivingfile_str
){
int ret;
@ -759,14 +766,12 @@ int set_parameter(
// matches any argument that starts with 'file:'
else if (strncmp(rhs,"file:",5)==0){
parameters->driving=DRIVING_FILE;
*drivingfile_str=calloc(sizeof(char), strlen(rhs)-5+1);
strcpy(*drivingfile_str, rhs+5);
str_to_dstring_noinit(rhs+5,drivingfile_str);
}
// matches any argument that starts with 'file_txt:'
else if (strncmp(rhs,"file_txt:",9)==0){
parameters->driving=DRIVING_FILE_TXT;
*drivingfile_str=calloc(sizeof(char), strlen(rhs)-9+1);
strcpy(*drivingfile_str, rhs+9);
str_to_dstring_noinit(rhs+9,drivingfile_str);
}
else{
fprintf(stderr, "error: unrecognized driving force '%s'\n",rhs);
@ -784,14 +789,12 @@ int set_parameter(
// matches any argument that starts with 'file:'
else if (strncmp(rhs,"file:",5)==0){
parameters->init=INIT_FILE;
*initfile_str=calloc(sizeof(char), strlen(rhs)-5+1);
strcpy(*initfile_str, rhs+5);
str_to_dstring_noinit(rhs+5,initfile_str);
}
// matches any argument that starts with 'file_txt:'
else if (strncmp(rhs,"file_txt:",9)==0){
parameters->init=INIT_FILE_TXT;
*initfile_str=calloc(sizeof(char), strlen(rhs)-9+1);
strcpy(*initfile_str, rhs+9);
str_to_dstring_noinit(rhs+9,initfile_str);
}
else{
fprintf(stderr, "error: unrecognized initial condition '%s'\n",rhs);
@ -839,11 +842,11 @@ int set_parameter(
// read args from file
int args_from_file(
char** params,
dstring* params,
unsigned int* command,
unsigned int* nthreads,
char** savefile_str,
char** utfile_str,
dstring* savefile_str,
dstring* utfile_str,
char* file_str
){
char* line;