!****************************************************************************
!
!   Program: Main
!
!   Purpose: *Uses Policy Function Iteration with Smolyak Interpolation
! 	      and Integration by a Monomial formula (7.5.11 in Judd)
! 	      to solve the IRBC model specified by Krueger/Kubler/Malin.
! 	     *Specification A2 as described by den Haan, Judd, and Juillard.
!            *The production function represents net output. 
!            *Technological shocks are generated by an AR(1) process 
! 	      with innovations drawn from a std. normal distribution. There
!             is an aggregate shock as well as country-specific shocks.
!            *The code is general in that it does not assume symmetric
! 	      Pareto weights for the countries.  However, the countries
! 	      are in other ways ex-ante identical.
!            *State variables: k & ln(a)
!            *Policy functions: k' & c
!           
!   Author:  Ben Malin
!
!   Date:    6/2004
!   Revised: 4/2009
!*****************************************************************************

program Main
  
  use Params	!Holds parameter specifications for program
  ! use msimsl	!Library contains nle solver, DNEQNF, and random number generator, DRNNOA.
  
  implicit none
  
  ! Sends all output to csv files, which are then transfered to excel file OutputN_x_y_z_w
  ! N = number of countries
  ! x = 1 corresponds to gamma = 0.25, x = 2 to  gamma = 1.
  ! y = 1 corresp to var low (sigma = 0.001, rho = 0.8), y = 2 to var high (sigma = 0.01, rho = 0.95).
  ! z = 1 corresp to acc = .50, z = 2 to  acc = 1, z = 3 to acc = 1.5 (if z is missing, acc = 1.5).
  ! w = 1 corr to eta = 0.1, w = 2 to eta = 0.25, w=3 to eta = 1.
 
  !Variable Declaration
  character(len=4) :: outfile
  character(len=6) :: cofile
  
  call ERSET(0,-1,1)    !Sets printing and stopping rules conditional on an error in IMSL routine.
  
  call Grid		!Constructs grid of smolyak points, weights for integrations, etc.

  call PolicyInitial	!Sets initial guess for policy function (kappa = 0)
  
!  call Policy		!Solves policy functions using time iteration 
!  call DecRule		!Computes policy function at specified points
!  call AccCheck1(1,1)     !Runs accuracy check #1 (described by den Haan, Judd, Juillard)
!  call AccCheck2(1,1)	!Runs accuracy check #2, using monomial formula integration
  !call AccCheck2(2,1)	!Runs accuracy check #2, using monte carlo integration
  !call AccCheck3	!Runs accuracy check #3.
!  outfile = "out1"
!  call Output(outfile)  !Outputs results to file
  
  !kappa = 0.01	 !Used only if program struggles with solving kappa = 0.1
  ! call policy
  
!  kappa = 0.1	!Solve for policy function at intermediate values of kappa in order to have 
!  call Policy   !a good initial guess for the nonlinear equation solver in Policy subroutine.
  
  kappa = 0.5
  call Policy
  cofile = "coeff8"
  call OutCoeff(cofile) !Outputs coefficients of policy functions to file
  call DecRule		!Computes policy function at specified points
  call AccCheck1(1,1)     !Runs accuracy check #1 (described by den Haan, Judd, Juillard)
  call AccCheck2(1,1)	!Runs accuracy check #2, using monomial formula integration
  !call AccCheck2(2,1)	!Runs accuracy check #2, using monte carlo integration
  !call AccCheck3	!Runs accuracy check #3.
  outfile = "out4"
  call Output(outfile)  !Outputs results to file
  
!  kappa = 2.0
!  call Policy
  !call DecRule		!Computes policy function at specified points
  !call Simul(1)	!Simulation with error check by monomial formula integration
  !call Simul(2)	!Simulation with error check by monte carlo integration
  
!  kappa = 10.0
!  call Policy
!  call DecRule		!Computes policy function at specified points
!  call AccCheck1(1,1)     !Runs accuracy check #1 (described by den Haan, Judd, Juillard)
!  call AccCheck2(1,1)	!Runs accuracy check #2, using monomial formula integration
  !call AccCheck2(2,1)	!Runs accuracy check #2, using monte carlo integration
!  call AccCheck3	!Runs accuracy check #3.
!  outfile = "out3"
!  call Output(outfile)  !Outputs results to file
!  cofile = "coeff3"
!  call OutCoeff(cofile) !Outputs coefficients of policy functions to file

end program Main
