Move driving and init to parameter string

This commit is contained in:
Ian Jauslin 2023-04-12 18:22:40 -04:00
parent 80c79f79c7
commit 0b0894839d

View File

@ -29,22 +29,25 @@ typedef struct nstrophy_parameters {
unsigned int avg_window;
int seed;
unsigned int starting_time;
unsigned int driving;
unsigned int init;
FILE* initfile;
} nstrophy_parameters;
// usage message
int print_usage();
// print parameters
int print_params(nstrophy_parameters parameters, unsigned int driving, unsigned int init, char* initfile_str, FILE* file);
int print_params(nstrophy_parameters parameters, char* initfile_str, FILE* file);
// read command line arguments
int read_args(int argc, const char* argv[], char** params, unsigned int* driving_force, unsigned int* command, unsigned int* init, unsigned int* nthreads, char** savefile_str, char** initfile_str);
int read_params(char* param_str, nstrophy_parameters* parameters);
int set_parameter(char* lhs, char* rhs, nstrophy_parameters* parameters, bool* setN1, bool* setN2, bool* setavg_window);
int read_args(int argc, const char* argv[], char** params, unsigned int* command, unsigned int* nthreads, char** savefile_str);
int read_params(char* param_str, nstrophy_parameters* parameters, char** initfile_str);
int set_parameter(char* lhs, char* rhs, nstrophy_parameters* parameters, bool* setN1, bool* setN2, bool* setavg_window, char** initfile_str);
// set driving force
_Complex double* set_driving(unsigned int driving, nstrophy_parameters parameters);
_Complex double* set_driving(nstrophy_parameters parameters);
// set initial condition
_Complex double* set_init(unsigned int init, nstrophy_parameters parameters, FILE* initfile);
_Complex double* set_init(nstrophy_parameters parameters);
// signal handler
void sig_handler (int signo);
@ -78,48 +81,45 @@ int main (
char* param_str=NULL;
nstrophy_parameters parameters;
int ret;
unsigned int driving,command,init;
unsigned int command;
unsigned int nthreads=1;
_Complex double* u0;
_Complex double *g;
char* savefile_str=NULL;
char* initfile_str=NULL;
FILE* savefile=NULL;
FILE* initfile=NULL;
command=0;
driving=0;
init=0;
// read command line arguments
ret=read_args(argc, argv, &param_str, &driving, &init, &command, &nthreads, &savefile_str, &initfile_str);
ret=read_args(argc, argv, &param_str, &command, &nthreads, &savefile_str);
if(ret<0){
return(-1);
}
// read params
ret=read_params(param_str, &parameters);
ret=read_params(param_str, &parameters, &initfile_str);
if(ret<0){
return(-1);
}
// open initfile
if(initfile_str!=NULL){
initfile=fopen(initfile_str,"r");
if(initfile==NULL){
parameters.initfile=fopen(initfile_str,"r");
if(parameters.initfile==NULL){
fprintf(stderr,"Error opening file '%s' for reading: %s\n", initfile_str, strerror(errno));
return(-1);
}
}
// set driving force
g=set_driving(driving, parameters);
g=set_driving(parameters);
// set initial condition
u0=set_init(init, parameters, initfile);
u0=set_init(parameters);
// close initfile (do this early, so that it is possible to use the same file for init and save)
if (initfile!=NULL){
fclose(initfile);
if (parameters.initfile!=NULL){
fclose(parameters.initfile);
}
// open savefile (do this after closing init file)
@ -132,8 +132,8 @@ int main (
}
// print parameters
print_params(parameters, driving, init, initfile_str, stderr);
print_params(parameters, driving, init, initfile_str, stdout);
print_params(parameters, initfile_str, stderr);
print_params(parameters, initfile_str, stdout);
// run command
if (command==COMMAND_UK){
@ -165,15 +165,13 @@ int main (
// usage message
int print_usage(){
fprintf(stderr, "usage:\n nstrophy [-t nthreads] [-p parameters] [-g driving_force] [-i initial_condition] [-s savefile] <command>\n\n");
fprintf(stderr, "usage:\n nstrophy [-t nthreads] [-p parameters] [-s savefile] <command>\n\n");
return(0);
}
// print parameters
int print_params(
nstrophy_parameters parameters,
unsigned int driving,
unsigned int init,
char* initfile_str,
FILE* file
){
@ -187,7 +185,7 @@ int print_params(
fprintf(file,", K1=%d, K2=%d, N1=%d, N2=%d, nu=%.15e, delta=%.15e, L=%.15e, init_en=%.15e", parameters.K1, parameters.K2, parameters.N1, parameters.N2, parameters.nu, parameters.delta, parameters.L, parameters.init_en);
switch(driving){
switch(parameters.driving){
case DRIVING_TEST:
fprintf(file,", driving=test");
break;
@ -199,7 +197,7 @@ int print_params(
break;
}
switch(init){
switch(parameters.init){
case INIT_RANDOM:
fprintf(file,", init=random");
break;
@ -221,20 +219,15 @@ int print_params(
// read command line arguments
#define CP_FLAG_PARAMS 1
#define CP_FLAG_DRIVING 2
#define CP_FLAG_NTHREADS 3
#define CP_FLAG_INIT 4
#define CP_FLAG_SAVEFILE 5
#define CP_FLAG_NTHREADS 2
#define CP_FLAG_SAVEFILE 3
int read_args(
int argc,
const char* argv[],
char** params,
unsigned int* driving_force,
unsigned int* init,
unsigned int* command,
unsigned int* nthreads,
char** savefile_str,
char** initfile_str
char** savefile_str
){
int i;
int ret;
@ -252,15 +245,9 @@ int read_args(
case 'p':
flag=CP_FLAG_PARAMS;
break;
case 'g':
flag=CP_FLAG_DRIVING;
break;
case 't':
flag=CP_FLAG_NTHREADS;
break;
case 'i':
flag=CP_FLAG_INIT;
break;
case 's':
flag=CP_FLAG_SAVEFILE;
break;
@ -277,20 +264,6 @@ int read_args(
*params=(char*)argv[i];
flag=0;
}
// driving force
else if(flag==CP_FLAG_DRIVING){
if (strcmp(argv[i],"zero")==0){
*driving_force=DRIVING_ZERO;
}
else if (strcmp(argv[i],"test")==0){
*driving_force=DRIVING_TEST;
}
else{
fprintf(stderr, "error: unrecognized driving force '%s'\n",argv[i]);
return(-1);
}
flag=0;
}
// nthreads
else if(flag==CP_FLAG_NTHREADS){
ret=sscanf(argv[i],"%u",nthreads);
@ -300,25 +273,6 @@ int read_args(
}
flag=0;
}
// initial condition
else if(flag==CP_FLAG_INIT){
if (strcmp(argv[i],"random")==0){
*init=INIT_RANDOM;
}
else if (strcmp(argv[i],"gaussian")==0){
*init=INIT_GAUSSIAN;
}
// matches any argument that starts with 'file:'
else if (strncmp(argv[i],"file:",5)==0){
*init=INIT_FILE;
*initfile_str=(char*)argv[i]+5;
}
else{
fprintf(stderr, "error: unrecognized initial condition '%s'\n",argv[i]);
return(-1);
}
flag=0;
}
// savefile
else if(flag==CP_FLAG_SAVEFILE){
*savefile_str=(char*)argv[i];
@ -349,7 +303,8 @@ int read_args(
// read parameters string
int read_params(
char* param_str,
nstrophy_parameters* parameters
nstrophy_parameters* parameters,
char** initfile_str
){
int ret;
// pointer in params
@ -379,6 +334,9 @@ int read_params(
parameters->print_freq=1000;
parameters->starting_time=0;
parameters->seed=17;
parameters->driving=DRIVING_TEST;
parameters->init=INIT_GAUSSIAN;
parameters->initfile=NULL;
if (param_str!=NULL){
// init
@ -399,7 +357,7 @@ int read_params(
break;
case ';':
//set parameter
ret=set_parameter(buffer_lhs, buffer_rhs, parameters, &setN1, &setN2, &setavg_window);
ret=set_parameter(buffer_lhs, buffer_rhs, parameters, &setN1, &setN2, &setavg_window, initfile_str);
if(ret<0){
return ret;
}
@ -426,7 +384,7 @@ int read_params(
// set last param
if (*param_str!='\0'){
ret=set_parameter(buffer_lhs, buffer_rhs, parameters, &setN1, &setN2, &setavg_window);
ret=set_parameter(buffer_lhs, buffer_rhs, parameters, &setN1, &setN2, &setavg_window, initfile_str);
if(ret<0){
return ret;
}
@ -460,7 +418,8 @@ int set_parameter(
nstrophy_parameters* parameters,
bool* setN1,
bool* setN2,
bool* setavg_window
bool* setavg_window,
char** initfile_str
){
int ret;
@ -588,6 +547,36 @@ int set_parameter(
return(-1);
}
}
else if (strcmp(lhs,"driving")==0){
if (strcmp(rhs,"zero")==0){
parameters->driving=DRIVING_ZERO;
}
else if (strcmp(rhs,"test")==0){
parameters->driving=DRIVING_TEST;
}
else{
fprintf(stderr, "error: unrecognized driving force '%s'\n",rhs);
return(-1);
}
}
// initial condition
else if (strcmp(lhs,"init")==0){
if (strcmp(rhs,"random")==0){
parameters->init=INIT_RANDOM;
}
else if (strcmp(rhs,"gaussian")==0){
parameters->init=INIT_GAUSSIAN;
}
// matches any argument that starts with 'file:'
else if (strncmp(rhs,"file:",5)==0){
parameters->init=INIT_FILE;
*initfile_str=(char*)rhs+5;
}
else{
fprintf(stderr, "error: unrecognized initial condition '%s'\n",rhs);
return(-1);
}
}
else{
fprintf(stderr, "error: unrecognized parameter '%s'\n",lhs);
return(-1);
@ -598,12 +587,11 @@ int set_parameter(
// set driving force
_Complex double* set_driving(
unsigned int driving,
nstrophy_parameters parameters
){
_Complex double* g=calloc(sizeof(_Complex double),(2*parameters.K1+1)*(2*parameters.K2+1));
switch(driving){
switch(parameters.driving){
case DRIVING_ZERO:
g_zero(g, parameters.K1, parameters.K2);
break;
@ -620,13 +608,11 @@ _Complex double* set_driving(
// set initial condition
_Complex double* set_init(
unsigned int init,
nstrophy_parameters parameters,
FILE* initfile
nstrophy_parameters parameters
){
_Complex double* u0=calloc(sizeof(_Complex double),(2*parameters.K1+1)*(2*parameters.K2+1));
switch(init){
switch(parameters.init){
case INIT_RANDOM:
init_random(u0, parameters.init_en, parameters.K1, parameters.K2, parameters.L, parameters.seed, parameters.irreversible);
break;
@ -636,7 +622,7 @@ _Complex double* set_init(
break;
case INIT_FILE:
init_file(u0, parameters.K1, parameters.K2, initfile);
init_file(u0, parameters.K1, parameters.K2, parameters.initfile);
break;
default: