%%writefileProgram_2_1.c/*****************************************************************ThisistheCversionofprogram2.1frompage19of"Modeling Infectious Disease in humans and animals"byKeeling&Rohani.ItisthesimpleSIRepidemicwithoutbirthsordeaths.Thiscodeiswrittentobesimple,transparentandreadilycompiled.Farmoreelegantandefficientcodecanbewritten.ThiscodecanbecompiledusinggnuorintelCcompilers:icc-oProgram_2_1Program_2_1.c-lmg++-oProgram_2_1Program_2_1.c-lmIfyouhaveanydifficulties,wesuggestyousetDEBUGto1or-1******************************************************************/#include <stdio.h>#include <stdlib.h>#include <math.h>#include <string.h>//SetDEBUGto1tooutputsomedebugginginformation//SetDEBUGto-1tooutputmoredebugginginformation#define DEBUG 0//Setupbasicparametersdoublebeta=520.0/365.0;doublegamm=1.0/7.0;doubleS0=1.0-1e-6;doubleI0=1e-6;doubleMaxTime=70;//Setupvariablesandratesofchangedoublet,S,I,R,Pop[3];doubledPop[3];//InitialisetheequationsandRunge-KuttaintegrationvoidDiff(doublePop[3]){//SetuptemporaryvariablestomaketheequationslookneaterdoubletmpS,tmpI,tmpR;tmpS=Pop[0];tmpI=Pop[1];tmpR=Pop[2];/*Thedifferentialequations*/dPop[0]=-beta*tmpS*tmpI;//dS/dtdPop[1]=beta*tmpS*tmpI-gamm*tmpI;//dI/dtdPop[2]=gamm*tmpI;//dR/dtreturn;}voidRunge_Kutta(double);voidRead_in_parameters(FILE*);voidPerform_Checks();voidOutput_Data(FILE*);//Themainprogramintmain(intargc,char**argv){charFileName[1000];doublestep,Every;FILE*in,*out;/*Findthegivenparameterfilenameandopenthefileotherwiseuseparameterssetattopofprogram*/if(argc>1){strcpy(FileName,*(argv+1));if((in=fopen(FileName,"r"))==NULL){printf("Cannot read file %s\n",FileName);exit(1);}/*Readintheparameters*/if(DEBUG)printf("\nReading parameters from file %s\n",FileName);Read_in_parameters(in);}else{if(DEBUG)printf("\nUsing default parameters\n");}if(DEBUG)printf("beta=%g\ngamma=%g\nInitial S=%g\nInitial I=%g\nRuns until time %g\n\n",beta,gamm,S0,I0,MaxTime);/*CheckallparametersareOK&setupintitialconditions*/Perform_Checks();S=S0;I=I0;R=1-S-I;/*Findasuitabletime-scaleforoutputs*/step=0.01/((beta+gamm)*S0);Every=pow(10,floor(log10((1.0/((beta+gamm)*S0)))));while(MaxTime/Every>10000){Every*=10.0;}if(DEBUG)printf("Using a time step of %g and outputing data every %g\n\n",step,Every);if((out=fopen("Program_2_1_c.out","w"))==NULL){printf("Cannot open file Output to write data\n");exit(1);}/*Themainiterationroutine*/t=0;Output_Data(out);do{Runge_Kutta(step);t+=step;/*Iftimehasmovedonsufficiently,outputthecurrentdata*/if(floor(t/Every)>floor((t-step)/Every)){Output_Data(out);}else{if(DEBUG<0)Output_Data(stdout);}}while(t<MaxTime);Output_Data(out);fclose(out);}voidRead_in_parameters(FILE*in){charstr[200];fscanf(in,"%s",str);fscanf(in,"%lf",&beta);fscanf(in,"%s",str);fscanf(in,"%lf",&gamm);fscanf(in,"%s",str);fscanf(in,"%lf",&S0);fscanf(in,"%s",str);fscanf(in,"%lf",&I0);fscanf(in,"%s",str);fscanf(in,"%lf",&MaxTime);fclose(in);}voidPerform_Checks(){if(S0<=0){printf("ERROR: Initial level of susceptibles (%g) is less than or equal to zero\n",S0);exit(1);}if(I0<=0){printf("ERROR: Initial level of infecteds (%g) is less than or equal to zero\n",I0);exit(1);}if(beta<=0){printf("ERROR: Transmission rate beta (%g) is less than or equal to zero\n",beta);exit(1);}if(gamm<=0){printf("ERROR: Recovery rate gamma (%g) is less than or equal to zero\n",gamm);exit(1);}if(MaxTime<=0){printf("ERROR: Maximum run time (%g) is less than or equal to zero\n",MaxTime);exit(1);}if(S0+I0>1){printf("WARNING: Initial level of susceptibles+infecteds (%g+%g=%g) is greater than one\n",S0,I0,S0+I0);}if(beta<gamm){printf("WARNING: Basic reproductive ratio (R_0=%g) is less than one\n",beta/gamm);}}voidOutput_Data(FILE*out){if(out!=stdout)fprintf(out,"%g\t%g\t%g\t%g\n",t,S,I,R);if(DEBUG)printf("%g\t%g\t%g\t%g\n",t,S,I,R);}voidRunge_Kutta(doublestep){inti;doubledPop1[3],dPop2[3],dPop3[3],dPop4[3];doubletmpPop[3],initialPop[3];/*Integratestheequationsonestep,usingRunge-Kutta4Note:weworkwitharraysratherthanvariablestomakethecodingeasier*/initialPop[0]=S;initialPop[1]=I;initialPop[2]=R;Diff(initialPop);for(i=0;i<3;i++){dPop1[i]=dPop[i];tmpPop[i]=initialPop[i]+step*dPop1[i]/2;}Diff(tmpPop);for(i=0;i<3;i++){dPop2[i]=dPop[i];tmpPop[i]=initialPop[i]+step*dPop2[i]/2;}Diff(tmpPop);for(i=0;i<3;i++){dPop3[i]=dPop[i];tmpPop[i]=initialPop[i]+step*dPop3[i];}Diff(tmpPop);for(i=0;i<3;i++){dPop4[i]=dPop[i];tmpPop[i]=initialPop[i]+(dPop1[i]/6+dPop2[i]/3+dPop3[i]/3+dPop4[i]/6)*step;}S=tmpPop[0];I=tmpPop[1];R=tmpPop[2];return;}