/*-----------------------------------------------------------------------------------------
 LIBRARY: CLASE
 FILE: Stiff
 CREATION DATE: 08/10/2016
-----------------------------------------------------------------------------------------*/
USE MATH

COMPONENT Stiff

-- Ejemplo de integracion de un sistema  stiff con Euler explicito e implicito
-- Las ecuaciones continuas se resuelven ademas con DASSL

		DATA
			REAL mu = 100				-- define la constante de tiempo
			REAL delta_t = 0.005		-- paso de integracion
			BOOLEAN explicitmethod = TRUE
		DECLS
			REAL x1,x2
			DISCR REAL x1_next, x2_next
			DISCR REAL x1_t, x2_t
			BOOLEAN sample
		
			
		INIT
		   sample = TRUE
			x1 = 2
			x2 = 3
		   x1_t = x1
			x2_t = x2
		 --  x_next = x
		
		DISCRETE
		
			WHEN ( sample == TRUE AND explicitmethod == TRUE ) THEN
			
				x1_next = x1_t + (-mu*x1_t+10)*delta_t
				x1_t = x1_next
				x2_next = x2_t + (x1_t - x2_t)*delta_t
				x2_t = x2_next
				
				sample = FALSE
				sample = TRUE AFTER delta_t
			END WHEN
			
			WHEN ( sample == TRUE AND explicitmethod == FALSE ) THEN
			
				x1_next = (x1_t + 10*delta_t)/(1 + mu*delta_t)
				x1_t = x1_next
				x2_next = (x2_t + x1_next *delta_t)/(1+delta_t)
				x2_t = x2_next
				
				sample = FALSE
				sample = TRUE AFTER delta_t
			END WHEN
			
		CONTINUOUS
			x1' = -mu*x1 + 10 
			x2' =  x1 - x2
		
END COMPONENT