Allow driving to be specified by file
This commit is contained in:
parent
2b1b66f8f1
commit
b35be9ea88
@ -1,5 +1,6 @@
|
|||||||
#include "driving.h"
|
#include "driving.h"
|
||||||
#include "navier-stokes.h"
|
#include "navier-stokes.h"
|
||||||
|
#include "io.h"
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
|
||||||
// test driving function
|
// test driving function
|
||||||
@ -35,3 +36,25 @@ int g_zero(
|
|||||||
|
|
||||||
return 0;
|
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;
|
||||||
|
}
|
||||||
|
@ -1,7 +1,13 @@
|
|||||||
#ifndef DRIVING_H
|
#ifndef DRIVING_H
|
||||||
#define DRIVING_H
|
#define DRIVING_H
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
int g_zero(_Complex double* g, int K1, int K2);
|
int g_zero(_Complex double* g, int K1, int K2);
|
||||||
int g_test(_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
|
#endif
|
||||||
|
70
src/main.c
70
src/main.c
@ -34,17 +34,18 @@ typedef struct nstrophy_parameters {
|
|||||||
unsigned int driving;
|
unsigned int driving;
|
||||||
unsigned int init;
|
unsigned int init;
|
||||||
FILE* initfile;
|
FILE* initfile;
|
||||||
|
FILE* drivingfile;
|
||||||
} nstrophy_parameters;
|
} nstrophy_parameters;
|
||||||
|
|
||||||
// usage message
|
// usage message
|
||||||
int print_usage();
|
int print_usage();
|
||||||
// print parameters
|
// 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
|
// 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_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 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);
|
int set_parameter(char* lhs, char* rhs, nstrophy_parameters* parameters, bool* setN1, bool* setN2, char** initfile_str, char** drivingfile_str);
|
||||||
|
|
||||||
// set driving force
|
// set driving force
|
||||||
_Complex double* set_driving(nstrophy_parameters parameters);
|
_Complex double* set_driving(nstrophy_parameters parameters);
|
||||||
@ -60,6 +61,8 @@ void sig_handler (int signo);
|
|||||||
|
|
||||||
#define DRIVING_ZERO 1
|
#define DRIVING_ZERO 1
|
||||||
#define DRIVING_TEST 2
|
#define DRIVING_TEST 2
|
||||||
|
#define DRIVING_FILE 3
|
||||||
|
#define DRIVING_FILE_TXT 4
|
||||||
|
|
||||||
#define INIT_RANDOM 1
|
#define INIT_RANDOM 1
|
||||||
#define INIT_GAUSSIAN 2
|
#define INIT_GAUSSIAN 2
|
||||||
@ -90,6 +93,7 @@ int main (
|
|||||||
_Complex double *g;
|
_Complex double *g;
|
||||||
char* savefile_str=NULL;
|
char* savefile_str=NULL;
|
||||||
char* initfile_str=NULL;
|
char* initfile_str=NULL;
|
||||||
|
char* drivingfile_str=NULL;
|
||||||
FILE* savefile=NULL;
|
FILE* savefile=NULL;
|
||||||
|
|
||||||
command=0;
|
command=0;
|
||||||
@ -101,7 +105,7 @@ int main (
|
|||||||
}
|
}
|
||||||
|
|
||||||
// read params
|
// read params
|
||||||
ret=read_params(param_str, ¶meters, &initfile_str);
|
ret=read_params(param_str, ¶meters, &initfile_str, &drivingfile_str);
|
||||||
if(ret<0){
|
if(ret<0){
|
||||||
return(-1);
|
return(-1);
|
||||||
}
|
}
|
||||||
@ -114,6 +118,14 @@ int main (
|
|||||||
return(-1);
|
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
|
// set driving force
|
||||||
g=set_driving(parameters);
|
g=set_driving(parameters);
|
||||||
@ -124,6 +136,10 @@ int main (
|
|||||||
if (parameters.initfile!=NULL){
|
if (parameters.initfile!=NULL){
|
||||||
fclose(parameters.initfile);
|
fclose(parameters.initfile);
|
||||||
}
|
}
|
||||||
|
// close drivingfile
|
||||||
|
if (parameters.drivingfile!=NULL){
|
||||||
|
fclose(parameters.drivingfile);
|
||||||
|
}
|
||||||
|
|
||||||
// open savefile (do this after closing init file)
|
// open savefile (do this after closing init file)
|
||||||
if(savefile_str!=NULL){
|
if(savefile_str!=NULL){
|
||||||
@ -135,13 +151,17 @@ int main (
|
|||||||
}
|
}
|
||||||
|
|
||||||
// print parameters
|
// print parameters
|
||||||
print_params(parameters, initfile_str, stderr);
|
print_params(parameters, initfile_str, drivingfile_str, stderr);
|
||||||
print_params(parameters, initfile_str, stdout);
|
print_params(parameters, initfile_str, drivingfile_str, stdout);
|
||||||
|
|
||||||
// free initfile_str
|
// free initfile_str
|
||||||
if(initfile_str!=NULL){
|
if(initfile_str!=NULL){
|
||||||
free(initfile_str);
|
free(initfile_str);
|
||||||
}
|
}
|
||||||
|
// free drivingfile_str
|
||||||
|
if(drivingfile_str!=NULL){
|
||||||
|
free(drivingfile_str);
|
||||||
|
}
|
||||||
|
|
||||||
// run command
|
// run command
|
||||||
if (command==COMMAND_UK){
|
if (command==COMMAND_UK){
|
||||||
@ -181,6 +201,7 @@ int print_usage(){
|
|||||||
int print_params(
|
int print_params(
|
||||||
nstrophy_parameters parameters,
|
nstrophy_parameters parameters,
|
||||||
char* initfile_str,
|
char* initfile_str,
|
||||||
|
char* drivingfile_str,
|
||||||
FILE* file
|
FILE* file
|
||||||
){
|
){
|
||||||
fprintf(file,"# ");
|
fprintf(file,"# ");
|
||||||
@ -200,6 +221,12 @@ int print_params(
|
|||||||
case DRIVING_ZERO:
|
case DRIVING_ZERO:
|
||||||
fprintf(file,", driving=zero");
|
fprintf(file,", driving=zero");
|
||||||
break;
|
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:
|
default:
|
||||||
fprintf(file,", driving=test");
|
fprintf(file,", driving=test");
|
||||||
break;
|
break;
|
||||||
@ -316,7 +343,8 @@ int read_args(
|
|||||||
int read_params(
|
int read_params(
|
||||||
char* param_str,
|
char* param_str,
|
||||||
nstrophy_parameters* parameters,
|
nstrophy_parameters* parameters,
|
||||||
char** initfile_str
|
char** initfile_str,
|
||||||
|
char** drivingfile_str
|
||||||
){
|
){
|
||||||
int ret;
|
int ret;
|
||||||
// pointer in params
|
// pointer in params
|
||||||
@ -345,6 +373,7 @@ int read_params(
|
|||||||
parameters->starting_time=0;
|
parameters->starting_time=0;
|
||||||
parameters->seed=17;
|
parameters->seed=17;
|
||||||
parameters->driving=DRIVING_TEST;
|
parameters->driving=DRIVING_TEST;
|
||||||
|
parameters->drivingfile=NULL;
|
||||||
parameters->init=INIT_GAUSSIAN;
|
parameters->init=INIT_GAUSSIAN;
|
||||||
parameters->initfile=NULL;
|
parameters->initfile=NULL;
|
||||||
|
|
||||||
@ -367,7 +396,7 @@ int read_params(
|
|||||||
break;
|
break;
|
||||||
case ';':
|
case ';':
|
||||||
//set parameter
|
//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){
|
if(ret<0){
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
@ -394,7 +423,7 @@ int read_params(
|
|||||||
|
|
||||||
// set last param
|
// set last param
|
||||||
if (*param_str!='\0'){
|
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){
|
if(ret<0){
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
@ -424,7 +453,8 @@ int set_parameter(
|
|||||||
nstrophy_parameters* parameters,
|
nstrophy_parameters* parameters,
|
||||||
bool* setN1,
|
bool* setN1,
|
||||||
bool* setN2,
|
bool* setN2,
|
||||||
char** initfile_str
|
char** initfile_str,
|
||||||
|
char** drivingfile_str
|
||||||
){
|
){
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
@ -551,6 +581,18 @@ int set_parameter(
|
|||||||
else if (strcmp(rhs,"test")==0){
|
else if (strcmp(rhs,"test")==0){
|
||||||
parameters->driving=DRIVING_TEST;
|
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{
|
else{
|
||||||
fprintf(stderr, "error: unrecognized driving force '%s'\n",rhs);
|
fprintf(stderr, "error: unrecognized driving force '%s'\n",rhs);
|
||||||
return(-1);
|
return(-1);
|
||||||
@ -603,6 +645,14 @@ _Complex double* set_driving(
|
|||||||
g_test(g, parameters.K1, parameters.K2);
|
g_test(g, parameters.K1, parameters.K2);
|
||||||
break;
|
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:
|
default:
|
||||||
g_test(g, parameters.K1, parameters.K2);
|
g_test(g, parameters.K1, parameters.K2);
|
||||||
break;
|
break;
|
||||||
|
Loading…
Reference in New Issue
Block a user