diff --git a/MATLAB/Call_EPED_NN.c b/MATLAB/Call_EPED_NN.c new file mode 100644 index 0000000..4641a56 --- /dev/null +++ b/MATLAB/Call_EPED_NN.c @@ -0,0 +1,257 @@ +/********************************************************************* + * call_eped_nn.c + * + * + ********************************************************************/ +#include +#include +#include +//#include "brainfusetf_lib.h" + +/* brainfusetf_lib.c */ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +int btf_sendline_n=65507; + +//Get ip from domain name +int btf_hostname_to_ip(char * hostname , char* ip){ + struct hostent *he; + struct in_addr **addr_list; + int i; + + if ( (he = gethostbyname( hostname ) ) == NULL){ + // get the host info + strcpy(ip , "127.0.0.1" ); + return 1; + } + + addr_list = (struct in_addr **) he->h_addr_list; + for(i = 0; addr_list[i] != NULL; i++){ + //Return the first one; + strcpy(ip , inet_ntoa(*addr_list[i]) ); + return 0; + } + + return 1; +} + +// This assumes buffer is at least x bytes long, +// and that the socket is blocking. +int ReadXBytes(int socket, unsigned int x, void* buffer){ + int bytesRead = 0; + int result; + while (bytesRead < x){ + result = read(socket, buffer + bytesRead, x - bytesRead); + if (result < 1 ){ + printf("error on read\n"); + return -1; + } + bytesRead += result; + } + return 0; +} + +int WriteXBytes(int socket, unsigned int x, void* buffer){ + int bytesWrite = 0; + int result; + while (bytesWrite < x) { + result = write(socket, buffer + bytesWrite, x - bytesWrite); + if (result < 1 ){ + printf("error on write\n"); + return -1; + } + bytesWrite += result; + } + return 0; +} + +int parse_string(char pInputString[btf_sendline_n],char *Delimiter, char **pToken){ + int i=0; + pToken[i] = strtok(pInputString, Delimiter); + i++; + while ((pToken[i] = strtok(NULL, Delimiter)) != NULL){ + i++; + } + return i; +} + +//send +int btf_run(char *model, double *input, int input_len, double *output, int output_len){ + int sockfd; + int i,n; + int ack; + struct sockaddr_in servaddr,cliaddr; + char message1[btf_sendline_n],message2[btf_sendline_n]; + unsigned int length = 0; + char *pToken[100]; + char btf_host[100]; + char btf_ip[15]; + int btf_port=8883; + int btf_verbose=0; + int btf_initialized=0; + + if (model[0]=='\0'){ + perror("BTF_MODEL not set"); + return -1; + } + + if (btf_initialized!=1){ + if (getenv("BTF_VERBOSE")!=NULL) + btf_verbose=atoi(getenv("BTF_VERBOSE")); + + if (getenv("BTF_HOST")!=NULL){ + sprintf(btf_host,"%s",getenv("BTF_HOST")); + }else{ + sprintf(btf_host,"gadb-harvest.ddns.net"); + } + btf_hostname_to_ip(btf_host, btf_ip); + + if (getenv("BTF_PORT")!=NULL){ + btf_port=atoi(getenv("BTF_PORT")); + } + + btf_initialized=1; + } + + srand(time(NULL)); + + bzero(&servaddr,sizeof(servaddr)); + servaddr.sin_family = AF_INET; + servaddr.sin_addr.s_addr=inet_addr(btf_ip); + servaddr.sin_port=htons(btf_port); + sockfd=socket(AF_INET,SOCK_STREAM,0); + if (connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) <0){ + printf("HOST:%s PORT:%d\n",btf_host,btf_port); + perror("ERROR connecting"); + return -1; + } + + //compose request message + sprintf(message1,"%s&(1,%d)&[",model,input_len); + for(i = 0; i < input_len-1; i++){ + sprintf(message1,"%s%g,",message1,*(input+i)); + } + sprintf(message1,"%s%g]",message1,*(input+input_len-1)); + //send request + for(i = 0; i < 10; i++){ + ack=0; + + length=strlen(message1); + if (btf_verbose) + printf("%s:%d >>>>>>> %s\n",btf_ip,btf_port,message1); + ack+=WriteXBytes(sockfd, sizeof(length), (void*)(&length)); + ack+=WriteXBytes(sockfd, length, (void*)message1); + + //receive answer + length=0; + memset(message1+sizeof(length), 0, btf_sendline_n); + ack+=ReadXBytes(sockfd, sizeof(length), (void*)(&length)); + ack+=ReadXBytes(sockfd, length, (void*)message1); + if (btf_verbose) + printf("%s:%d <<<<<<< %s\n",btf_ip,btf_port,message1); + + if (ack==0) + break; + usleep(10*i); + } + + //parse answer message + parse_string(message1,"&",pToken); + memset(message2, 0, btf_sendline_n); + snprintf(message2,strlen(pToken[2]),",%s",pToken[2]+1); + n=parse_string(message2,",",pToken); + for(i = 0; i +#include +#include +//#include "brainfusetf_lib.h" + +/* brainfusetf_lib.c */ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +int btf_sendline_n=65507; + +//Get ip from domain name +int btf_hostname_to_ip(char * hostname , char* ip){ + struct hostent *he; + struct in_addr **addr_list; + int i; + + if ( (he = gethostbyname( hostname ) ) == NULL){ + // get the host info + strcpy(ip , "127.0.0.1" ); + return 1; + } + + addr_list = (struct in_addr **) he->h_addr_list; + for(i = 0; addr_list[i] != NULL; i++){ + //Return the first one; + strcpy(ip , inet_ntoa(*addr_list[i]) ); + return 0; + } + + return 1; +} + +// This assumes buffer is at least x bytes long, +// and that the socket is blocking. +int ReadXBytes(int socket, unsigned int x, void* buffer){ + int bytesRead = 0; + int result; + while (bytesRead < x){ + result = read(socket, buffer + bytesRead, x - bytesRead); + if (result < 1 ){ + printf("error on read\n"); + return -1; + } + bytesRead += result; + } + return 0; +} + +int WriteXBytes(int socket, unsigned int x, void* buffer){ + int bytesWrite = 0; + int result; + while (bytesWrite < x) { + result = write(socket, buffer + bytesWrite, x - bytesWrite); + if (result < 1 ){ + printf("error on write\n"); + return -1; + } + bytesWrite += result; + } + return 0; +} + +int parse_string(char pInputString[btf_sendline_n],char *Delimiter, char **pToken){ + int i=0; + pToken[i] = strtok(pInputString, Delimiter); + i++; + while ((pToken[i] = strtok(NULL, Delimiter)) != NULL){ + i++; + } + return i; +} + +//send +int btf_run(char *model, double *input, int input_len, double *output, int output_len){ + int sockfd; + int i,n; + int ack; + struct sockaddr_in servaddr,cliaddr; + char message1[btf_sendline_n],message2[btf_sendline_n]; + unsigned int length = 0; + char *pToken[100]; + char btf_host[100]; + char btf_ip[15]; + int btf_port=8883; + int btf_verbose=0; + int btf_initialized=0; + + if (model[0]=='\0'){ + perror("BTF_MODEL not set"); + return -1; + } + + if (btf_initialized!=1){ + if (getenv("BTF_VERBOSE")!=NULL) + btf_verbose=atoi(getenv("BTF_VERBOSE")); + + if (getenv("BTF_HOST")!=NULL){ + sprintf(btf_host,"%s",getenv("BTF_HOST")); + }else{ + sprintf(btf_host,"gadb-harvest.ddns.net"); + } + btf_hostname_to_ip(btf_host, btf_ip); + + if (getenv("BTF_PORT")!=NULL){ + btf_port=atoi(getenv("BTF_PORT")); + } + + btf_initialized=1; + } + + srand(time(NULL)); + + bzero(&servaddr,sizeof(servaddr)); + servaddr.sin_family = AF_INET; + servaddr.sin_addr.s_addr=inet_addr(btf_ip); + servaddr.sin_port=htons(btf_port); + sockfd=socket(AF_INET,SOCK_STREAM,0); + if (connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) <0){ + printf("HOST:%s PORT:%d\n",btf_host,btf_port); + perror("ERROR connecting"); + return -1; + } + + //compose request message + sprintf(message1,"%s&(1,%d)&[",model,input_len); + for(i = 0; i < input_len-1; i++){ + sprintf(message1,"%s%g,",message1,*(input+i)); + } + sprintf(message1,"%s%g]",message1,*(input+input_len-1)); + //send request + for(i = 0; i < 10; i++){ + ack=0; + + length=strlen(message1); + if (btf_verbose) + printf("%s:%d >>>>>>> %s\n",btf_ip,btf_port,message1); + ack+=WriteXBytes(sockfd, sizeof(length), (void*)(&length)); + ack+=WriteXBytes(sockfd, length, (void*)message1); + + //receive answer + length=0; + memset(message1+sizeof(length), 0, btf_sendline_n); + ack+=ReadXBytes(sockfd, sizeof(length), (void*)(&length)); + ack+=ReadXBytes(sockfd, length, (void*)message1); + if (btf_verbose) + printf("%s:%d <<<<<<< %s\n",btf_ip,btf_port,message1); + + if (ack==0) + break; + usleep(10*i); + } + + //parse answer message + parse_string(message1,"&",pToken); + memset(message2, 0, btf_sendline_n); + snprintf(message2,strlen(pToken[2]),",%s",pToken[2]+1); + n=parse_string(message2,",",pToken); + for(i = 0; i + +#endif /* call_eped_nn_h */ diff --git a/MATLAB/call_eped_nn_matlab.m b/MATLAB/call_eped_nn_matlab.m new file mode 100644 index 0000000..b142bba --- /dev/null +++ b/MATLAB/call_eped_nn_matlab.m @@ -0,0 +1,150 @@ +%% Eped FANN Model +clc +clear all + +t = cputime; + +% Calculate input values +a = 0.5778; +betan = 1.8034; +bt = 2.0995; +delta = 0.2075; +ip = 1.1621; +kappa = 1.8017; +m = 2; +neped = 4.0101; +r = 1.6984; +zeffped = 1.4429; + +inputs = [a betan bt delta ip kappa m neped r zeffped]; + +% Write inputs to file +input_file = fopen('inputs.dat', 'w'); +fprintf(input_file, '%f\n', 1); +fprintf(input_file, '%f\n', inputs); +fclose(input_file); + +% Run neural network +% Figure out fann.h files to run fann model +system('sh eped_fann_test.sh'); + +output_file = fopen('output.avg', 'r'); +output_avg = fscanf(output_file, '%f\n'); +fclose(output_file); + +% Extract outputs +OUT_p_E1_0 = output_avg(2); +OUT_p_E1_2 = output_avg(3); +OUT_wid_E1_0 = output_avg(4); +OUT_wid_E1_2 = output_avg(5); + +e = cputime - t + + +%% Eped TENSORFLOW Model +clc +clear all + +% Generate MEX function +% Run once +% mex Call_EPED_NN.c + +t = cputime; + +% Calculate input values +a = 0.5778; +betan = 1.8034; +bt = 2.0995; +delta = 0.2075; +ip = 1.1621; +kappa = 1.8017; +m = 2; +neped = 4.0101; +r = 1.6984; +zeffped = 1.4429; + +inputs = [a betan bt delta ip kappa m neped r zeffped]; + +% Run neural network +outputs = Call_EPED_NN(inputs); + +% Extract outputs +o1 = outputs(1); +o2 = outputs(2); +o3 = outputs(3); +o4 = outputs(4); +o5 = outputs(5); +o6 = outputs(6); +o7 = outputs(7); +o8 = outputs(8); +o9 = outputs(9); +o10 = outputs(10); +o11 = outputs(11); +o12 = outputs(12); +o13 = outputs(13); +o14 = outputs(14); +o15 = outputs(15); +o16 = outputs(16); +o17 = outputs(17); +o18 = outputs(18); + +format long +e = cputime - t + + +%% TGLF TENSORFLOW Model +clc +clear all + +% Generate MEX function +% Run once +% mex Call_TGLF_NN.c + +t = cputime; + +% Calculate input values +as_2 = 2.52925; +as_3 = 0.00205474; +betae = 0.195821; +debye = 0.015207; +delta_loc = 0.10706; +drmajdx_loc = 0.709042; +kappa_loc = 0.00879205; +p_prime_loc = 0.0509749; +q_loc = 0.0837711; +q_prime_loc = 0.0153145; +rlns_1 = 1.40512; +rlns_2 = -0.05166936; +rlns_3 = 1.17357; +rlts_1 = -0.0039616; +rlts_2 = 0.512115; +rmaj_loc = 5.26647; +rmin_loc = 0.59511; +s_kappa_loc = 0.44139; +taus_2 = 1.87827; +vexb_shear = 1.78045; +vpar_1 = 0.510876; +vpar_shear_1 = 3.22174; +xnue = 1.01135; +zeff = 0.107648; + + +inputs = [as_2 as_3 betae debye delta_loc drmajdx_loc kappa_loc p_prime_loc q_loc q_prime_loc... + rlns_1 rlns_2 rlns_3 rlts_1 rlts_2 rmaj_loc rmin_loc s_kappa_loc taus_2 vexb_shear vpar_1... + vpar_shear_1 xnue zeff]; + +% Run neural network +outputs = Call_TGLF_NN(inputs) + +% Extract outputs +% energy_flux_1 = outputs(1); +% energy_flux_i = outputs(2); +% particle_flux_1 = outputs(3); +% particle_flux_2 = outputs(4); +% particle_flux_3 = outputs(5); +% stress_tor_i = outputs(6); + +format long +e = cputime - t + + diff --git a/MATLAB/eped_fann_test.sh b/MATLAB/eped_fann_test.sh new file mode 100644 index 0000000..52764c5 --- /dev/null +++ b/MATLAB/eped_fann_test.sh @@ -0,0 +1 @@ +../brainfuse_run.exe ../eped1nn/models/EPED1_H_superH inputs.dat diff --git a/MATLAB/inputs.dat b/MATLAB/inputs.dat new file mode 100644 index 0000000..fd2bc9c --- /dev/null +++ b/MATLAB/inputs.dat @@ -0,0 +1,11 @@ +1.000000 +0.577800 +1.803400 +2.099500 +0.207500 +1.162100 +1.801700 +2.000000 +4.010100 +1.698400 +1.442900