
//TITLE: Chemical Reactor Design in C++//
//Introduction and Description: Please read my separate file.//


#include <iostream.h>

typedef double real;

//First, the Rate Equations Base Class will be the basic of //
//the all the Rate Equations that are going to be entered into// 
//the program.//
//***********BASE RATE EQUATIONS***********//


class RateEquations
{
public:
  virtual real operator() () = 0;
  virtual ~RateEquations() {};
};


//Derived class of Zero Order Rate Equations from the Rate //
//Equation Base Class.This contains the elements of the zero //
//order rate equations.//
//***********ZERO ORDER DERIVED CLASS***********//


class ZeroOrder: public RateEquations
{
public:
  virtual real operator() ()
    {
      return k;
    }
  ZeroOrder(real kinput) : k(kinput)
    {
    }
  
private:
  real k;
};


//Derived Class of Rate Equations Base Class. Contains the //
//elements of First Order Rate Law with respect to species A.//
//***********FIRST ORDER DERIVED CLASS***********//



class FirstOrder: public RateEquations
{
public:
  virtual real operator() ()
    {
      return k * CA;
    }
  FirstOrder (real kinput, real CAinput):k(kinput),CA(CAinput)
    {
    }
private:    
  real k;
  real CA;
};



//Second order Rate Equations Class from the Rate Equations//
//Base Class, contains the elements of Second Order Rate Law.//
//***********SECOND ORDER DERIVED CLASS***********//


class SecondOrder: public RateEquations
{
public:
  virtual real operator() ()
    {
      return k * CA * CA;
    }
  SecondOrder (real kinput, real CAinput):k(kinput),CA(CAinput)
    {
    }
private:
  real k;
  real CA;
};                 


//External Function to print out the value of rate of change// 
//of reactant species at a point in the reactions.//
//***********EXTERNAL FUNCTIONS #1************//


void ExtFunc(RateEquations &f)
{ 
  cout <<endl;
  cout <<"The Rate of change of species A at the outlet is: ";
  cout <<endl<< f() << " moles/(hour m^3)" << endl;
  cout <<endl;
}


//External Function to give the actual number of the rate of //
//decrease of the reactant that will be used in the calculations//
//in the Reactor Class.//
//***********EXTERNAL FUNCTIONS #2***********//


real ExtFunc3(RateEquations &h)
{
  return h();
}


//Reactor Base Class.//
//***********REACTOR BASE CLASS***********//

                 
class Reactors
{
public:
  virtual real operator() () = 0;
  virtual ~Reactors() {};
};



//Continuous Stirred Tank Reactor Class, derived from Reactor //
//Base Class. This will contain calculations necessary for the// 
//volume.//
//***********CSTR DERIVED CLASS***********//


class CSTR: public Reactors
{
public:
  virtual real operator() ()
    { CA=CA0 * (1-x)/ (1+eps*x);

    //Calculations are done based on which rate laws the //
    //reactions follow.//

      if(type==1)
	{ ZeroOrder ZO(k);
	  ExtFunc (ZO);
	  raout=ExtFunc3(ZO);}
      if(type==2)
	{ FirstOrder FO(k,CA);
	  ExtFunc (FO);
	  raout=ExtFunc3(FO);}
      if(type==3)
	{ SecondOrder SO(k,CA);
	  ExtFunc (SO);
	  raout=ExtFunc3(SO);}
      return Fax * x /raout;
    }
  CSTR(real Faxinput, real typeinput, real kinput, real 
    xinput, real epsinput, real CA0input): Fax(Faxinput),
    type(typeinput), k(kinput), x(xinput), eps(epsinput),
    CA0(CA0input)
    {
    }
private:
  real Fax, k, x, type, raout;
  real CA0, CA, eps;
  };



//Plug Flow Reactor Class derived from the Reactor Base Class.//
// Contains calculations for volume calculations of PFR.//
//***********PFR DERIVED CLASS***********//


class PFR: public Reactors
{
public:
  virtual real operator() ()
    {
      
      // The following values are necessary for doing numerical //
      // integration. //

      CA1= CA0;
      CA2= CA0*(1-x/3)/(1+eps*x/3);
      CA3= CA0*(1-2*x/3)/(1+eps*2*x/3);
      CA4= CA0*(1-x)/(1+eps*x);
      
      if(type==1)
        { ZeroOrder ZO(k);
	  ExtFunc(ZO);
	  invra=3/ExtFunc3(ZO);}
      
      if(type==2)

	// For higher rate equations order than zero, //
	//the numerical integration technique is done here.//

        { FirstOrder FO1(k,CA1);
          FirstOrder FO2(k,CA2);
	  FirstOrder FO3(k,CA3);
	  FirstOrder FO4(k,CA4);
	  ExtFunc (FO4);
	  invra=(3 / (8 * ExtFunc3(FO1))+9 / (8 * ExtFunc3(FO2))+ 9 / 
		 (8 * ExtFunc3(FO3)) + 3 / (8 * ExtFunc3(FO4)) );}

      if(type==3)
        { SecondOrder SO1(k,CA1);
	  SecondOrder SO2(k,CA2);
          SecondOrder SO3(k,CA3);
          SecondOrder SO4(k,CA4);
          ExtFunc (SO4);
          invra=(3/(8* ExtFunc3(SO1))+ 9/(8*ExtFunc3(SO2))+ 9/
		 (8 * ExtFunc3(SO3)) + 3 / (8 * ExtFunc3(SO4) ) );}     
      return Fax*invra*x/3;
    }
     

 PFR(real Faxinput, real typeinput, real kinput, real xinput, real 
   epsinput, real CA0input) : Fax(Faxinput), type(typeinput), 
   k(kinput), x(xinput), eps(epsinput), CA0(CA0input)
    {
    }

private:
  real Fax, k, x, type, invra, eps;
  real CA0, CA1, CA2, CA3, CA4;
};


//***********BATCH DERIVED CLASS***********//


class BATCH: public Reactors
{
public:
  virtual real operator() ()
    {

      // The following values are necessary for doing numerical //
      // integration. //

      CA1= CA0;
      CA2= CA0*(1-x/3)/(1+eps*x/3);
      CA3= CA0*(1-2*x/3)/(1+eps*2*x/3);
      CA4= CA0*(1-x)/(1+eps*x);                                                

      if(type==1)
        { ZeroOrder ZO(k);
          ExtFunc(ZO);
          invra=3/ExtFunc3(ZO);}

      if(type==2)
        { FirstOrder FO1(k,CA1);
          FirstOrder FO2(k,CA2);
          FirstOrder FO3(k,CA3);
          FirstOrder FO4(k,CA4);
          invra=(3 / (8 * ExtFunc3(FO1))+9 / (8 * ExtFunc3(FO2))+ 9 /
                 (8 * ExtFunc3(FO3)) + 3 / (8 * ExtFunc3(FO4)) );}

      if(type==3)
        { SecondOrder SO1(k,CA1);
          SecondOrder SO2(k,CA2);                                              
          SecondOrder SO3(k,CA3);
          SecondOrder SO4(k,CA4);
          invra=(3/(8* ExtFunc3(SO1))+ 9/(8*ExtFunc3(SO2))+ 9/
                 (8 * ExtFunc3(SO3)) + 3 / (8 * ExtFunc3(SO4) ) );}
      return Nax*invra*x/3*time;
    }


 BATCH(real Naxinput, real typeinput, real kinput, real xinput, real
     epsinput, real CA0input, real timeinput) : Nax(Naxinput), 
     type(typeinput), k(kinput), x(xinput), eps(epsinput), 
     CA0(CA0input), time(timeinput)
    {
    }

private:
  real Nax, k, x, type, invra, eps;
  real CA0, CA1, CA2, CA3, CA4, time;
};                                   



//External Functions to print out the results from//
//the Reactor Classes.//
//***********EXTERNAL FUNCTIONS #3***********//


void ExtFunc2(Reactors &g)
{
  cout<<endl;
  
  cout << g() << " m^3 is the volume needed for the Reactor 
to achieve the reaction according to the data entered." << endl;
  
  cout<<endl;
}                 













