Source Code of micro-grid trading simulator #include #include #include #include void init_mm( ); int number_range( int from, int to ); int number_mm( void ); static int rgiState[2+55]; // for random number int main(void) { FILE *fp; /* file pointer */ int c,i,j,x,z,N,abc,vsz,effz,refz,dnpz,xyz,lmn,mno,opq,pqr,qrs,gsk,gh,gn,traderand; float mg[1000][48],vs,ngsp,mgsp,eff,ref,dnp[24],trade[1000][24],exp,grid,cost,costg,ghi,jk l,mnop,inc[1000][24]; if ((fp=fopen("mg1.txt", "r"))==NULL) { printf("Cannot open file.\n"); 71 } for (i = 0; i < 48; i++) { if (fscanf(fp, "%f", &mg[0][i]) != 1) { break; } } fclose(fp); x = i; printf("Supply & Demand data in text file is found for %d hours\n", (x/2)); for (i = 0; i < x; i++) { printf("mg[0][%d] = %6.2f\n", i, mg[0][i]); } // ask user how many dummy grids are required printf ("what are the number of dummy micro-grids do you want to simulate electricity trading with?\n"); scanf ("%d", &N); // ask user how much variability in size is required printf ("what should be the variability in size of dummy micro-grids from the given micro-grid?\n Enter 1 for low, 2 for medium and 3 for high.\n"); scanf ("%d", &vsz); while (vsz < 1) 72 { printf ("Error: Please Enter a value in the given range. \n what should be the variability in size of dummy micro-grids from the given micro-grid?\n Enter 1 for low, 2 for medium and 3 for high.\n"); scanf ("%d", &vsz); } while (vsz > 3) { printf ("Error: Please Enter a value in the given range. \n what should be the variability in size of dummy micro-grids from the given micro-grid?\n Enter 1 for low, 2 for medium and 3 for high.\n"); scanf ("%d", &vsz); } // ask user if equipment failure possibility is to be considered printf ("Do you want to consider the possibility of equipment failure in microgrids?\ n Enter 1 for Yes, 2 for No.\n"); scanf ("%d", &effz); while (effz < 1) { printf ("Error: Please Enter a value in the given range. \n Do you want to consider the possibility of equipment failure in micro-grids?\n Enter 1 for Yes, 2 for No.\n"); scanf ("%d", &effz); } 73 while (effz > 2) { printf ("Error: Please Enter a value in the given range. \n Do you want to consider the possibility of equipment failure in micro-grids?\n Enter 1 for Yes, 2 for No.\n"); scanf ("%d", &effz); } // ask user if intermittence of renewable energy sources is to be considered printf ("Do you want to consider the intermittence of renewable energy supplies in micro-grids?\n Enter 1 for Yes, 2 for No.\n"); scanf ("%d", &refz); while (refz < 1) { printf ("Error: Please Enter a value in the given range. \n Do you want to consider the intermittence of renewable energy supplies in micro-grids?\n Enter 1 for Yes, 2 for No.\n"); scanf ("%d", &refz); } while (refz > 2) { printf ("Error: Please Enter a value in the given range. \n Do you want to consider the intermittence of renewable energy supplies in micro-grids?\n Enter 1 for Yes, 2 for No.\n"); scanf ("%d", &refz); 74 } // ask user the national grid electricity price printf ("Please enter the national grid electricity price in pence/kWh\n"); scanf ("%f", &ngsp); while (ngsp < 0.001) { printf ("Error: Invalid Price entered \n Please enter the national grid electricity price in pence/kWh\n"); scanf ("%f", &ngsp); } // ask user the microgrid electricity price printf ("Please enter the micro-grid electricity price in pence/kWh\n"); scanf ("%f", &mgsp); while (mgsp >= ngsp) { printf ("Error: Invalid Price entered \n Price must be less than national grid electricity price\n Please enter the micro-grid electricity price in pence/kWh\n"); scanf ("%f", &mgsp); } // ask user ask user if the night money saver plan is to be considered for electricity price printf ("Do you want to consider different electricity rates in day & night?\n Enter 1 for Yes, 2 for No.\n"); scanf ("%d", &dnpz); 75 while (dnpz < 1) { printf ("Error: Please Enter a value in the given range. \n Do you want to consider different electricity rates in day & night?\n Enter 1 for Yes, 2 for No.\n"); scanf ("%d", &dnpz); } while (dnpz > 2) { printf ("Error: Please Enter a value in the given range. \n Do you want to consider different electricity rates in day & night?\n Enter 1 for Yes, 2 for No.\n"); scanf ("%d", &dnpz); } init_mm(); //seed the number generator for (j=1; j<=N; j++) for (i=0; i<48; i++) { if (vsz == 1) { abc = number_range( 0, 10 ); vs = (0.1*(5.0+abc)); } if (vsz == 2) 76 { abc = number_range( 0, 27 ); vs = (0.1*(3.0+abc)); } if (vsz == 3) { abc = number_range( 0, 100 ); vs = (0.1*abc); } if (effz == 1) { lmn = number_range( 0, 1 ); mno = number_range( 0, 1 ); opq = number_range( 0, 1 ); pqr = number_range( 0, 1 ); qrs = number_range( 0, 1 ); eff = (lmn+mno+opq+pqr+qrs)/(lmn+mno+opq+pqr+qrs+0.000001); } if (effz == 2) { eff = 1.0; } if (refz == 1) { 77 xyz = number_range( 7, 10 ); ref = (0.1*(xyz)); } if (refz == 2) { ref = 1.0; } if (dnpz == 1) { if ((fp=fopen("dnp1.txt", "r"))==NULL) { printf("Cannot open the night package file.\n"); } for (int dnlp = 1; dnlp <= 24; dnlp++) { if (fscanf(fp, "%f", &dnp[dnlp]) != 1) { break; } } fclose(fp); } if (dnpz == 2) { 78 if ((fp=fopen("dnp0.txt", "r"))==NULL) { printf("Cannot open the price package file.\n"); } for (int dnlp = 1; dnlp <= 24; dnlp++) { if (fscanf(fp, "%f", &dnp[dnlp]) != 1) { break; } } fclose(fp); } { if (i%2!=0) { mg[j][i]= (mg[0][i]*vs); } else mg[j][i]= (mg[0][i]*vs*eff*ref); printf ("mg[%d][%d] is %10.2f\n", j, i, mg[j][i]); } }; fp = fopen("results.txt", "w"); if (fp == NULL) 79 { printf("\nError opening write.txt\n"); exit(1); } else; for (gh=1; gh<=24; gh++) { exp = 0; grid = 0; mnop=0; for (gn=0; gn<=N; gn++) { trade [gn][gh]= (mg[gn][(gh*2)-2])-(mg[gn][(gh*2)-1]); printf ("Hour no %d: Energy available in Micro-grid No.%d is %10.2f kWh\n", gh, gn, trade[gn][gh]); fprintf(fp, "%d %d %7.2f ", gh, gn, trade[gn][gh]); if ((trade[gn][gh])>0) { exp = exp + (trade[gn][gh]); ghi = exp; fprintf(fp, "%9.2f\n", exp); } if (gn == N) { 80 printf ("Energy available in the microgrids pool is %10.2f\n", exp); for (gsk=0; gsk<(10*N); gsk++) { traderand = number_range( 0, gn ); if ((trade[traderand][gh])<0) { printf ("Random grid selected for electricity trading is %d\n", traderand); if (((trade[traderand][gh])+exp)>0) { cost = (trade[traderand][gh])*dnp[gh]*mgsp*0.01; printf ("Hour No. %d\nImporter: Microgrid No. %d\nImport: %10.2f kWh\n Cost/kWh: %3.2f\n", gh,traderand,(trade[traderand][gh]),cost); fprintf (fp, "%d %d %7.2f", gh,traderand,(trade[traderand][gh])); exp = exp + (trade[traderand][gh]); printf ("Energy still available: %10.2f kWh\n",exp); fprintf(fp, "%9.2f %6.2f\n", exp,cost); (trade[traderand][gh])= 0; } if (((trade[traderand][gh])+exp)<0) { costg = (trade[traderand][gh])*dnp[gh]*ngsp*0.01; printf ("Hour No. %d\nImporter: Microgrid No. %d\nImport: %10.2f kWh\n Cost/kWh: %3.2f\n", gh,traderand,(trade[traderand][gh]),costg); 81 fprintf (fp, "%d %d %7.2f", gh,traderand,(trade[traderand][gh])); grid = grid + (trade[traderand][gh]); fprintf(fp, "%9.2f %6.2f", grid,costg); printf ("Total electricity imported from the grid: %10.2f kWh\n",grid); (trade[traderand][gh])= 0; mnop=mnop+costg; fprintf(fp, "%7.2f\n", mnop); } fprintf(fp, "National Grid earns %7.2f\n", mnop); } } jkl=ghi-exp; } inc[i][gh]=0; for (i=0;i<=N;i++) { if ((trade[i][gh])>0) { inc[i][gh]=((trade[i][gh])*dnp[gh]*mgsp*0.01*jkl)/ghi; if (inc[i][gh]>0) { fprintf(fp, "Grid No:%d earns %6.2f\n", i,inc[i][gh]); inc[i][gh]=0; 82 } } } jkl=0; } }; fclose(fp); printf ("See the results.txt file in the program folder for simulation results\n Press C then Enter to close the program"); scanf ("%d",&c); return 0; } int number_mm( void ) //for random number generator (ref.16) // { int *piState; int iState1; int iState2; int iRand; piState = &rgiState[2]; iState1 = piState[-2]; iState2 = piState[-1]; iRand = ( piState[iState1] + piState[iState2] ) & ( ( 1 << 30 ) - 1 ); piState[iState1] = iRand; 83 if ( ++iState1 == 55 ) iState1 = 0; if ( ++iState2 == 55 ) iState2 = 0; piState[-2] = iState1; piState[-1] = iState2; return iRand >> 6; } /* * Generate a random number. */ int number_range( int from, int to ) //for random number generator { int power; int number; if ( ( to = to - from + 1 ) <= 1 ) return from; for ( power = 2; power < to; power <<= 1 ) ; while ( ( number = number_mm( ) & ( power - 1 ) ) >= to ) ; return from + number; } /* 84 * This is the Mitchell-Moore algorithm from Knuth Volume II. */ void init_mm( ) //for random number generator { int *piState; int iState; piState = &rgiState[2]; piState[-2] = 55 - 55; piState[-1] = 55 - 24; piState[0] = ( (int) time( NULL ) ) & ( ( 1 << 30 ) - 1 ); piState[1] = 1; for ( iState = 2; iState < 55; iState++ ) { piState[iState] = ( piState[iState-1] + piState[iState-2] ) & ( ( 1 << 30 ) - 1 ); } return ; //end of code for random number generator }