Allow driving to be specified by file

This commit is contained in:
Ian Jauslin 2023-04-24 12:22:29 -04:00
parent 2b1b66f8f1
commit b35be9ea88
3 changed files with 89 additions and 10 deletions

View File

@ -1,5 +1,6 @@
#include "driving.h"
#include "navier-stokes.h"
#include "io.h"
#include <math.h>
// test driving function
@ -35,3 +36,25 @@ int g_zero(
return 0;
}
// From file
// txt input
int driving_file_txt (
_Complex double* g,
int K1,
int K2,
FILE* file
){
read_vec(g, K1, K2, file);
return 0;
}
// binary input
int driving_file_bin (
_Complex double* g,
int K1,
int K2,
FILE* file
){
read_vec_bin(g, K1, K2, file);
return 0;
}

View File

@ -1,7 +1,13 @@
#ifndef DRIVING_H
#define DRIVING_H
#include <stdio.h>
int g_zero(_Complex double* g, int K1, int K2);
int g_test(_Complex double* g, int K1, int K2);
// From file
int driving_file_txt ( _Complex double* g, int K1, int K2, FILE* file);
int driving_file_bin ( _Complex double* g, int K1, int K2, FILE* file);
#endif

View File

@ -34,17 +34,18 @@ typedef struct nstrophy_parameters {
unsigned int driving;
unsigned int init;
FILE* initfile;
FILE* drivingfile;
} nstrophy_parameters;
// usage message
int print_usage();
// print parameters
int print_params(nstrophy_parameters parameters, char* initfile_str, FILE* file);
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);
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, char** initfile_str);
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);
// set driving force
_Complex double* set_driving(nstrophy_parameters parameters);
@ -60,6 +61,8 @@ void sig_handler (int signo);
#define DRIVING_ZERO 1
#define DRIVING_TEST 2
#define DRIVING_FILE 3
#define DRIVING_FILE_TXT 4
#define INIT_RANDOM 1
#define INIT_GAUSSIAN 2
@ -90,6 +93,7 @@ int main (
_Complex double *g;
char* savefile_str=NULL;
char* initfile_str=NULL;
char* drivingfile_str=NULL;
FILE* savefile=NULL;
command=0;
@ -101,7 +105,7 @@ int main (
}
// read params
ret=read_params(param_str, &parameters, &initfile_str);
ret=read_params(param_str, &parameters, &initfile_str, &drivingfile_str);
if(ret<0){
return(-1);
}
@ -114,6 +118,14 @@ int main (
return(-1);
}
}
// open drivingfile
if(drivingfile_str!=NULL){
parameters.drivingfile=fopen(drivingfile_str,"r");
if(parameters.drivingfile==NULL){
fprintf(stderr,"Error opening file '%s' for reading: %s\n", drivingfile_str, strerror(errno));
return(-1);
}
}
// set driving force
g=set_driving(parameters);
@ -124,6 +136,10 @@ int main (
if (parameters.initfile!=NULL){
fclose(parameters.initfile);
}
// close drivingfile
if (parameters.drivingfile!=NULL){
fclose(parameters.drivingfile);
}
// open savefile (do this after closing init file)
if(savefile_str!=NULL){
@ -135,13 +151,17 @@ int main (
}
// print parameters
print_params(parameters, initfile_str, stderr);
print_params(parameters, initfile_str, stdout);
print_params(parameters, initfile_str, drivingfile_str, stderr);
print_params(parameters, initfile_str, drivingfile_str, stdout);
// free initfile_str
if(initfile_str!=NULL){
free(initfile_str);
}
// free drivingfile_str
if(drivingfile_str!=NULL){
free(drivingfile_str);
}
// run command
if (command==COMMAND_UK){
@ -181,6 +201,7 @@ int print_usage(){
int print_params(
nstrophy_parameters parameters,
char* initfile_str,
char* drivingfile_str,
FILE* file
){
fprintf(file,"# ");
@ -200,6 +221,12 @@ int print_params(
case DRIVING_ZERO:
fprintf(file,", driving=zero");
break;
case DRIVING_FILE:
fprintf(file,", driving=file:%s", drivingfile_str);
break;
case DRIVING_FILE_TXT:
fprintf(file,", driving=file_txt:%s", drivingfile_str);
break;
default:
fprintf(file,", driving=test");
break;
@ -316,7 +343,8 @@ int read_args(
int read_params(
char* param_str,
nstrophy_parameters* parameters,
char** initfile_str
char** initfile_str,
char** drivingfile_str
){
int ret;
// pointer in params
@ -345,6 +373,7 @@ int read_params(
parameters->starting_time=0;
parameters->seed=17;
parameters->driving=DRIVING_TEST;
parameters->drivingfile=NULL;
parameters->init=INIT_GAUSSIAN;
parameters->initfile=NULL;
@ -367,7 +396,7 @@ int read_params(
break;
case ';':
//set parameter
ret=set_parameter(buffer_lhs, buffer_rhs, parameters, &setN1, &setN2, initfile_str);
ret=set_parameter(buffer_lhs, buffer_rhs, parameters, &setN1, &setN2, initfile_str, drivingfile_str);
if(ret<0){
return ret;
}
@ -394,7 +423,7 @@ int read_params(
// set last param
if (*param_str!='\0'){
ret=set_parameter(buffer_lhs, buffer_rhs, parameters, &setN1, &setN2, initfile_str);
ret=set_parameter(buffer_lhs, buffer_rhs, parameters, &setN1, &setN2, initfile_str, drivingfile_str);
if(ret<0){
return ret;
}
@ -424,7 +453,8 @@ int set_parameter(
nstrophy_parameters* parameters,
bool* setN1,
bool* setN2,
char** initfile_str
char** initfile_str,
char** drivingfile_str
){
int ret;
@ -551,6 +581,18 @@ int set_parameter(
else if (strcmp(rhs,"test")==0){
parameters->driving=DRIVING_TEST;
}
// 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);
}
// 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);
}
else{
fprintf(stderr, "error: unrecognized driving force '%s'\n",rhs);
return(-1);
@ -603,6 +645,14 @@ _Complex double* set_driving(
g_test(g, parameters.K1, parameters.K2);
break;
case DRIVING_FILE:
init_file_bin(g, parameters.K1, parameters.K2, parameters.drivingfile);
break;
case DRIVING_FILE_TXT:
init_file_txt(g, parameters.K1, parameters.K2, parameters.drivingfile);
break;
default:
g_test(g, parameters.K1, parameters.K2);
break;