use parameters struct for CLI parsing

This commit is contained in:
Ian Jauslin 2022-05-26 14:25:45 -04:00
parent 11af4a8ece
commit 8877b63549

View File

@ -9,14 +9,26 @@
#include "navier-stokes.h" #include "navier-stokes.h"
#include "driving.h" #include "driving.h"
// structure to store parameters, to make it easier to pass parameters to CLI functions
typedef struct nstrophy_parameters {
int K1;
int K2;
int N1;
int N2;
unsigned int nsteps;
double nu;
double delta;
double L;
unsigned int print_freq;
} nstrophy_parameters;
// usage message // usage message
int print_usage(); int print_usage();
// read command line arguments // read command line arguments
int read_args(int argc, const char* argv[], char** params, unsigned int* driving_force, unsigned int* command, unsigned int* nthreads); int read_args(int argc, const char* argv[], char** params, unsigned int* driving_force, unsigned int* command, unsigned int* nthreads);
int read_params(char* params, int* K1, int* K2, int* N1, int* N2, unsigned int* nsteps, double* nu, double* delta, double* L, unsigned int* print_freq); int read_params(char* param_str, nstrophy_parameters* parameters);
int set_parameter(char* lhs, char* rhs, int* K1, int* K2, int* N1, int* N2, unsigned int* nsteps, double* nu, double* delta, double* L, unsigned int* print_freq, bool* setN1, bool* setN2); int set_parameter(char* lhs, char* rhs, nstrophy_parameters* parameters, bool* setN1, bool* setN2);
#define COMMAND_UK 1 #define COMMAND_UK 1
#define COMMAND_ENSTROPHY 2 #define COMMAND_ENSTROPHY 2
@ -31,27 +43,23 @@ int main (
int argc, int argc,
const char* argv[] const char* argv[]
){ ){
char* params=NULL; char* param_str=NULL;
int K1,K2; nstrophy_parameters parameters;
int N1,N2;
unsigned int nsteps;
double nu,delta,L;
_Complex double (*g)(int,int); _Complex double (*g)(int,int);
int ret; int ret;
unsigned int driving,command; unsigned int driving,command;
unsigned int print_freq;
unsigned int nthreads=1; unsigned int nthreads=1;
command=0; command=0;
driving=0; driving=0;
// read command line arguments // read command line arguments
ret=read_args(argc, argv, &params, &driving, &command, &nthreads); ret=read_args(argc, argv, &param_str, &driving, &command, &nthreads);
if(ret<0){ if(ret<0){
return(-1); return(-1);
} }
// read params // read params
ret=read_params(params, &K1, &K2, &N1, &N2, &nsteps, &nu, &delta, &L, &print_freq); ret=read_params(param_str, &parameters);
if(ret<0){ if(ret<0){
return(-1); return(-1);
} }
@ -71,16 +79,16 @@ int main (
// run command // run command
if (command==COMMAND_UK){ if (command==COMMAND_UK){
uk(K1, K2, N1, N2, nsteps, nu, delta, L, g, print_freq, nthreads); uk(parameters.K1, parameters.K2, parameters.N1, parameters.N2, parameters.nsteps, parameters.nu, parameters.delta, parameters.L, g, parameters.print_freq, nthreads);
} }
else if (command==COMMAND_ENERGY){ else if (command==COMMAND_ENERGY){
energy(K1, K2, N1, N2, nsteps, nu, delta, L, g, print_freq, nthreads); energy(parameters.K1, parameters.K2, parameters.N1, parameters.N2, parameters.nsteps, parameters.nu, parameters.delta, parameters.L, g, parameters.print_freq, nthreads);
} }
else if(command==COMMAND_ENSTROPHY){ else if(command==COMMAND_ENSTROPHY){
enstrophy(K1, K2, N1, N2, nsteps, nu, delta, L, g, print_freq, nthreads); enstrophy(parameters.K1, parameters.K2, parameters.N1, parameters.N2, parameters.nsteps, parameters.nu, parameters.delta, parameters.L, g, parameters.print_freq, nthreads);
} }
else if(command==COMMAND_QUIET){ else if(command==COMMAND_QUIET){
quiet(K1, K2, N1, N2, nsteps, nu, delta, L, g, nthreads); quiet(parameters.K1, parameters.K2, parameters.N1, parameters.N2, parameters.nsteps, parameters.nu, parameters.delta, parameters.L, g, nthreads);
} }
else if(command==0){ else if(command==0){
fprintf(stderr, "error: no command specified\n"); fprintf(stderr, "error: no command specified\n");
@ -193,16 +201,8 @@ int read_args(
// read parameters string // read parameters string
int read_params( int read_params(
char* params, char* param_str,
int* K1, nstrophy_parameters* parameters
int* K2,
int* N1,
int* N2,
unsigned int* nsteps,
double* nu,
double* delta,
double* L,
unsigned int* print_freq
){ ){
int ret; int ret;
// pointer in params // pointer in params
@ -217,26 +217,26 @@ int read_params(
bool lhs=true; bool lhs=true;
// defaults // defaults
*K1=16; parameters->K1=16;
*K2=*K1; parameters->K2=parameters->K1;
//delta=2^-13 //delta=2^-13
*delta=0.0001220703125; parameters->delta=0.0001220703125;
//nu=2^-11 //nu=2^-11
*nu=0.00048828125; parameters->nu=0.00048828125;
*L=2*M_PI; parameters->L=2*M_PI;
*nsteps=10000000; parameters->nsteps=10000000;
*print_freq=1000; parameters->print_freq=1000;
if (params!=NULL){ if (param_str!=NULL){
// init // init
buffer_lhs=calloc(sizeof(char),strlen(params)); buffer_lhs=calloc(sizeof(char),strlen(param_str));
lhs_ptr=buffer_lhs; lhs_ptr=buffer_lhs;
*lhs_ptr='\0'; *lhs_ptr='\0';
buffer_rhs=calloc(sizeof(char),strlen(params)); buffer_rhs=calloc(sizeof(char),strlen(param_str));
rhs_ptr=buffer_rhs; rhs_ptr=buffer_rhs;
*rhs_ptr='\0'; *rhs_ptr='\0';
for(ptr=params;*ptr!='\0';ptr++){ for(ptr=param_str;*ptr!='\0';ptr++){
switch(*ptr){ switch(*ptr){
case '=': case '=':
// reset buffer // reset buffer
@ -246,7 +246,7 @@ int read_params(
break; break;
case ';': case ';':
//set parameter //set parameter
ret=set_parameter(buffer_lhs,buffer_rhs,K1,K2,N1,N2,nsteps,nu,delta,L,print_freq,&setN1,&setN2); ret=set_parameter(buffer_lhs, buffer_rhs, parameters, &setN1, &setN2);
if(ret<0){ if(ret<0){
return ret; return ret;
} }
@ -272,8 +272,8 @@ int read_params(
} }
// set last param // set last param
if (*params!='\0'){ if (*param_str!='\0'){
ret=set_parameter(buffer_lhs,buffer_rhs,K1,K2,N1,N2,nsteps,nu,delta,L,print_freq,&setN1,&setN2); ret=set_parameter(buffer_lhs, buffer_rhs, parameters, &setN1, &setN2);
if(ret<0){ if(ret<0){
return ret; return ret;
} }
@ -286,10 +286,10 @@ int read_params(
// if N not set explicitly, set it // if N not set explicitly, set it
if (!setN1){ if (!setN1){
*N1=4*(*K1)+1; parameters->N1=4*(parameters->K1)+1;
} }
if (!setN2){ if (!setN2){
*N2=4*(*K2)+1; parameters->N2=4*(parameters->K2)+1;
} }
return(0); return(0);
@ -300,44 +300,36 @@ int read_params(
int set_parameter( int set_parameter(
char* lhs, char* lhs,
char* rhs, char* rhs,
int* K1, nstrophy_parameters* parameters,
int* K2,
int* N1,
int* N2,
unsigned int* nsteps,
double* nu,
double* delta,
double* L,
unsigned int* print_freq,
bool* setN1, bool* setN1,
bool* setN2 bool* setN2
){ ){
int ret; int ret;
if (strcmp(lhs,"K1")==0){ if (strcmp(lhs,"K1")==0){
ret=sscanf(rhs,"%d",K1); ret=sscanf(rhs,"%d",&(parameters->K1));
if(ret!=1){ if(ret!=1){
fprintf(stderr, "error: parameter 'K1' should be an integer\n got '%s'\n",rhs); fprintf(stderr, "error: parameter 'K1' should be an integer\n got '%s'\n",rhs);
return(-1); return(-1);
} }
} }
else if (strcmp(lhs,"K2")==0){ else if (strcmp(lhs,"K2")==0){
ret=sscanf(rhs,"%d",K2); ret=sscanf(rhs,"%d",&(parameters->K2));
if(ret!=1){ if(ret!=1){
fprintf(stderr, "error: parameter 'K2' should be an integer\n got '%s'\n",rhs); fprintf(stderr, "error: parameter 'K2' should be an integer\n got '%s'\n",rhs);
return(-1); return(-1);
} }
} }
else if (strcmp(lhs,"K")==0){ else if (strcmp(lhs,"K")==0){
ret=sscanf(rhs,"%d",K1); ret=sscanf(rhs,"%d",&(parameters->K1));
if(ret!=1){ if(ret!=1){
fprintf(stderr, "error: parameter 'K' should be an integer\n got '%s'\n",rhs); fprintf(stderr, "error: parameter 'K' should be an integer\n got '%s'\n",rhs);
return(-1); return(-1);
} }
*K2=*K1; parameters->K2=parameters->K1;
} }
else if (strcmp(lhs,"N1")==0){ else if (strcmp(lhs,"N1")==0){
ret=sscanf(rhs,"%d",N1); ret=sscanf(rhs,"%d",&(parameters->N1));
if(ret!=1){ if(ret!=1){
fprintf(stderr, "error: parameter 'N1' should be an integer\n got '%s'\n",rhs); fprintf(stderr, "error: parameter 'N1' should be an integer\n got '%s'\n",rhs);
return(-1); return(-1);
@ -345,7 +337,7 @@ int set_parameter(
*setN1=true; *setN1=true;
} }
else if (strcmp(lhs,"N2")==0){ else if (strcmp(lhs,"N2")==0){
ret=sscanf(rhs,"%d",N2); ret=sscanf(rhs,"%d",&(parameters->N2));
if(ret!=1){ if(ret!=1){
fprintf(stderr, "error: parameter 'N2' should be an integer\n got '%s'\n",rhs); fprintf(stderr, "error: parameter 'N2' should be an integer\n got '%s'\n",rhs);
return(-1); return(-1);
@ -353,45 +345,45 @@ int set_parameter(
*setN2=true; *setN2=true;
} }
else if (strcmp(lhs,"N")==0){ else if (strcmp(lhs,"N")==0){
ret=sscanf(rhs,"%d",N1); ret=sscanf(rhs,"%d",&(parameters->N1));
if(ret!=1){ if(ret!=1){
fprintf(stderr, "error: parameter 'N' should be an integer\n got '%s'\n",rhs); fprintf(stderr, "error: parameter 'N' should be an integer\n got '%s'\n",rhs);
return(-1); return(-1);
} }
*N2=*N1; parameters->N2=parameters->N1;
*setN1=true; *setN1=true;
*setN2=true; *setN2=true;
} }
else if (strcmp(lhs,"nsteps")==0){ else if (strcmp(lhs,"nsteps")==0){
ret=sscanf(rhs,"%u",nsteps); ret=sscanf(rhs,"%u",&(parameters->nsteps));
if(ret!=1){ if(ret!=1){
fprintf(stderr, "error: parameter 'nsteps' should be an unsigned integer\n got '%s'\n",rhs); fprintf(stderr, "error: parameter 'nsteps' should be an unsigned integer\n got '%s'\n",rhs);
return(-1); return(-1);
} }
} }
else if (strcmp(lhs,"nu")==0){ else if (strcmp(lhs,"nu")==0){
ret=sscanf(rhs,"%lf",nu); ret=sscanf(rhs,"%lf",&(parameters->nu));
if(ret!=1){ if(ret!=1){
fprintf(stderr, "error: parameter 'nu' should be a double\n got '%s'\n",rhs); fprintf(stderr, "error: parameter 'nu' should be a double\n got '%s'\n",rhs);
return(-1); return(-1);
} }
} }
else if (strcmp(lhs,"delta")==0){ else if (strcmp(lhs,"delta")==0){
ret=sscanf(rhs,"%lf",delta); ret=sscanf(rhs,"%lf",&(parameters->delta));
if(ret!=1){ if(ret!=1){
fprintf(stderr, "error: parameter 'delta' should be a double\n got '%s'\n",rhs); fprintf(stderr, "error: parameter 'delta' should be a double\n got '%s'\n",rhs);
return(-1); return(-1);
} }
} }
else if (strcmp(lhs,"L")==0){ else if (strcmp(lhs,"L")==0){
ret=sscanf(rhs,"%lf",L); ret=sscanf(rhs,"%lf",&(parameters->L));
if(ret!=1){ if(ret!=1){
fprintf(stderr, "error: parameter 'L' should be a double\n got '%s'\n",rhs); fprintf(stderr, "error: parameter 'L' should be a double\n got '%s'\n",rhs);
return(-1); return(-1);
} }
} }
else if (strcmp(lhs,"print_freq")==0){ else if (strcmp(lhs,"print_freq")==0){
ret=sscanf(rhs,"%u",print_freq); ret=sscanf(rhs,"%u",&(parameters->print_freq));
if(ret!=1){ if(ret!=1){
fprintf(stderr, "error: parameter 'print_freq' should be an unsigned integer\n got '%s'\n",rhs); fprintf(stderr, "error: parameter 'print_freq' should be an unsigned integer\n got '%s'\n",rhs);
return(-1); return(-1);