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