!
!     Absorption Refrigeration model - Proj. 2.4 my book - 6 Feb 2016
!
      implicit real *8 (a-h,o-z)
      external ftest,f2,j1 
      common /jose/ maxit,iflag
      common /zy/ xmin,xmax

common /param1/ rm,cpref,cliqref,cpair,t0,rntu,hfg
common /param3/ rmu,rmu_prime
common /param4/ tau_b,tau_1,tau_w

      open (unit=1,file='inp1.txt',status='old') 
      open (unit=2,file='out-read.txt',status='unknown') 
      open (unit=3,file='func_tau2.txt',status='unknown') 
!
!****************************************
!
      read (1,*) maxit
      write(2,*)'maxit=',maxit
      read (1,*) iflag
      write(2,*)'iflag=',iflag
      read (1,*) xmin
      write(2,*)'xmin=',xmin
      read (1,*) xmax
      write(2,*)'xmax=',xmax
      read (1,*) tol
      write(2,*)'tol=',tol
      read (1,*) s2
      write(2,*)'s2=',s2
      read(1,*) tau_st
      write(2,*)'tau_st= ',tau_st
      read(1,*)tau_H
      write(2,*)'tau_H= ',tau_H
      read(1,*)rmu_w
      write(2,*)'rmu_w= ',rmu_w
      read(1,*)rntu
      write(2,*)'rntu= ',rntu
      read(1,*) tau_b
      write(2,*)'tau_b= ',tau_b
      read(1,*)cliqref
      write(2,*)'cliqref= ',cliqref
      read(1,*) tau_1
      write(2,*)'tau_1= ',tau_1
      read(1,*)hfg
      write(2,*)'hfg= ',hfg
      read(1,*)cpref
      write(2,*)'cpref= ',cpref
      read(1,*)t0
      write(2,*)'t0= ',t0
      read(1,*)rm
      write(2,*)'rm= ',rm
      read(1,*)cw
      write(2,*)'cw= ',cw

      read(1,*)ndiv
      write(2,*)'ndiv= ',ndiv
      read(1,*)tau_min
      write(2,*)'tau_min= ',tau_min
      read(1,*)tau_max
      write(2,*)'tau_max= ',tau_max

! Calculation of design and operating parameters

!	Evaluate specific heat of ammonia vapor

	tb=taub*t0  ! -273.15d0
	cpref_calc=4.07d0-1.108d-3*tb+4.152d-6*tb**2-2.964d-9*tb**3+0.807d-12*tb**4
    write(2,*)'cpref_calc= ',cpref_calc

	tau_w=1.d0+(tau_st-tau_H)/rmu_w
    write(2,*)'tau_w= ',tau_w

	rmu=rm*cpref/cw
    write(2,*)'rmu= ',rmu

	rmu_prime=rm*cliqref/cw
    write(2,*)'rmu_prime= ',rmu_prime


!*******************************************
!	Solve for outlet HX air outlet temperature
!**************************************************

!----------plot function

	dtau=(tau_max-tau_min)/ndiv
	tau2=tau_min
	auxant=0.d0

	do i=1,ndiv+1
		auxfunc=f2(tau2)
		write(3,*)tau2,auxfunc
		prod=auxant*auxfunc
		if(prod.lt.0.d0) then
			x1=tau2ant
			x2=tau2
			goto 456
		endif
		tau2ant=tau2
		auxant=auxfunc
		tau2=tau2+dtau
	enddo
	write(*,*) "there is no solution in the interval"
	stop

	456	continue
!
!       solucao
!
	call bissec	(f2,x1,x2,maxit,tol,c)
    write(*,*)'************************************'
    write(*,*)'SOLUTION'
    write(*,*)'************************************'
    write(*,*)'tau_2=',c
    write(2,*)'************************************'
    write(2,*)'SOLUTION'
    write(2,*)'************************************'
    write(2,*)'tau_2=',c


	close(1)
	close(2)
	close(3)

      stop
      end
!      
!----------------------------------------------------------------
      function rnorm2(n,x)
!
!	compute euclidean norm of a vector
!
      implicit real *8 (a-h,o-z)
      dimension x(n)
      sum=0.d0
      do i=1,n
      	sum=sum+x(i)*x(i)
      enddo
      aux=sqrt(sum)
      rnorm2=aux
      return
      end
!------------------------------------------------------------











