MODULE WBDevFunctionPiecewise1;

	IMPORT
		WBDevScalar,
		Math,
		MathFunc;
		
	TYPE
		Function = POINTER TO RECORD (WBDevScalar.Node) END;
		Factory = POINTER TO RECORD (WBDevScalar.Factory) END;
		
	VAR
		fact-: WBDevScalar.Factory;
		
	PROCEDURE (func: Function) DeclareArgTypes (OUT args: ARRAY OF CHAR);
	BEGIN
		args := "v";
	END DeclareArgTypes;
	
	PROCEDURE (func: Function) Evaluate (OUT value: REAL);
	CONST
		 
	VAR
		T: REAL;
		TLAG_UNIPV: REAL;
		D: REAL;
		V_UNIPV: REAL;
		KA_UNIPV: REAL;
		k_UNIPV: REAL;
		ETA_TLAG: REAL;
		POP_TLAG: REAL;
		ETA_V: REAL;
		POP_V: REAL;
		BETA_V_WT: REAL;
		logtWT_UNIPV: REAL;
		ETA_KA: REAL;
		POP_KA: REAL;
		CL_UNIPV: REAL;
		WT: REAL;
		ETA_CL: REAL;
		POP_CL: REAL;
		BETA_CL_WT: REAL;
		
	BEGIN
		POP_TLAG := func.arguments[0][0].Value();
		POP_V := func.arguments[0][1].Value();
		D := func.arguments[0][2].Value();
		ETA_V := func.arguments[0][3].Value();
		T := func.arguments[0][4].Value();
		BETA_V_WT := func.arguments[0][5].Value();
		ETA_TLAG := func.arguments[0][6].Value();
		ETA_CL := func.arguments[0][7].Value();
		WT := func.arguments[0][8].Value();
		BETA_CL_WT := func.arguments[0][9].Value();
		ETA_KA := func.arguments[0][10].Value();
		POP_CL := func.arguments[0][11].Value();
		POP_KA := func.arguments[0][12].Value();
		

		KA_UNIPV := Math.Exp(Math.Ln(POP_KA) + ETA_KA);
		logtWT_UNIPV := (Math.Ln((WT / 70)));
		TLAG_UNIPV := Math.Exp(Math.Ln(POP_TLAG) + ETA_TLAG);
		CL_UNIPV := Math.Exp(Math.Ln(POP_CL) + BETA_CL_WT * logtWT_UNIPV + ETA_CL);
		V_UNIPV := Math.Exp(Math.Ln(POP_V) + BETA_V_WT * logtWT_UNIPV + ETA_V);
		k_UNIPV := (CL_UNIPV / V_UNIPV);

		IF T < TLAG_UNIPV  THEN;
			value := 0; 
		ELSE;
			value := ((((D / V_UNIPV) * KA_UNIPV) / (KA_UNIPV - k_UNIPV)) * ((Math.Exp(((-(k_UNIPV)) * (T - TLAG_UNIPV)))) - (Math.Exp(((-(KA_UNIPV)) * (T - TLAG_UNIPV))))));
		END;

	END Evaluate;
	
	PROCEDURE (f: Factory) New (option: INTEGER): Function;
	VAR
		func: Function;
	BEGIN
		NEW(func); func.Initialize; RETURN func;
	END New;
	
	PROCEDURE Install*;
	BEGIN
		WBDevScalar.Install(fact);
	END Install;
	
	PROCEDURE Init;
	VAR
		f: Factory;
	BEGIN
		NEW(f); fact := f;
	END Init;
	
BEGIN
	Init;
END WBDevFunctionPiecewise1.

