/*-----------------------------------------------------------------------------------------
 LIBRARY: REACTOR_AB
 FILE: Reactor_AB
 AUTHOR: ISA
 COMPANY: UVa
 CREATION DATE: 24/02/2017
-----------------------------------------------------------------------------------------*/

USE MATH

COMPONENT reactorAB
	"Reactor básico de dos componentes"
	DATA
		REAL Cp			= 0.968		UNITS "kJ/(kg*K)"
		REAL Cpr			= 1.291		UNITS "kJ/(kg*K)"
		REAL D			= 3.03		UNITS "m"
		REAL DH			= -75755.88	UNITS "kJ/kmol"
		REAL L			= 3.03		UNITS "m"
		REAL LiminfT	= 62			UNITS "ºC"
		REAL LimsupT	= 73			UNITS "ºC"
		REAL Liminfx	= 0.7
		REAL T0			= 68			UNITS "ºC"
		REAL Te			= 21.11		UNITS "ºC"
		REAL Tre			= 21.11		UNITS "ºC"
		REAL U			= 6129		UNITS "kJ/(h*m^2*K)"
		REAL cA0			= 14.46		UNITS "kmol/m^3"
		REAL dc			= 0.1			UNITS "m"
		REAL rho 		= 800.8		UNITS "kg/m^3"
		REAL rhor		= 1041.1		UNITS "kg/m^3"
		
	DECLS
		DISCR REAL A			= 0	UNITS "m^2"
		REAL Fr			= 67			UNITS "m^3/h"			"Caudal de refrigerante"
		REAL Q							UNITS "kJ/h"
		REAL T							UNITS "K"				"Temperatura del tanque"
		REAL Tr							UNITS "K"				"Temperatura de la camisa de refrigeracion"
		DISCR REAL V			= 0	UNITS "m^3"				"Volumen"
		DISCR REAL Vr			= 0	UNITS "m^3"
		REAL cA							UNITS "kmol/m^3"		"Concentración de elemento A"
		REAL cB							UNITS "kmol/m^3"		"Concentracion de elemento B"
		REAL k							UNITS "l/h"
		REAL q			= 4.1			UNITS "m^3/h"			"Caudal de entrada"
		REAL x
--		REAL W
		
		-- El índice que se minimiza
		REAL J_coste
		-- Vector de la funcion de costo y las restricciones 
		REAL F_optim[4]

	INIT
		T  = T0
		Tr = 51.5
		cA = 2.891

		J_coste =0
		F_optim[1] = 0
		F_optim[2] = 0
		F_optim[3] = 0
		F_optim[4] = 0
		
		V = MATH.PI * D * D * L / 4
		A = MATH.PI * D * L
		Vr = MATH.PI * (D + 2 * dc) * (D + 2 * dc) * L / 4 - MATH.PI * D * D * L / 4

	CONTINUOUS


		cB = cA0 - cA
		x = (cA0 - cA) / cA0
		k = 5.967 * exp(-826. / (T + 273.14))
		V*cA' = (q * (cA0 - cA) - V * k * cA) 
		Q = U * A * (T - Tr)
		V * rho * Cp* T' = q * rho * Cp * (Te - T) - V * k * cA * DH - Q 
		Vr * rhor * Cpr* Tr' = Fr * rhor * Cpr * (Tre - Tr) + Q 

		-- calculo de la funcion de costo a minimizar
		-- cantidad de producto B en estado estacionario
		J_coste = - q*(cA0 - cA)
		
		-- vector con la funcion de costo y las restricciones no lineales (expresiones g,   a < g(x) < b)
		F_optim[1] = J_coste
		F_optim[2] = x
		F_optim[3]' = MATH.min(T,LiminfT) - LiminfT
		F_optim[4]' = MATH.max(T,LimsupT) - LimsupT

END COMPONENT