
#define CALCULA_PSTD  (double)101325

  #define CALCULA_CP       (double)4186 
  #define CALCULA_CPA      (double)1005 
  #define CALCULA_PMA      0.0289645 
  #define CALCULA_PMWG     0.01801534 
  #define CALCULA_PMCC      110.986 
  #define CALCULA_R        8.31441 
  #define CALCULA_G         9.80665 
/*  9.78 a 9.83*/

#define CALCULA_KO 273.15

#define CALCULA_PI  (double)3.1415926


  #define PSICRO_SWE      (double)9155.299999999999 
  #define PSICRO_HWE      (double)2500770 
  #define PSICRO_LAMBDA   (double)2500770 

  #define PSICRO_HWR  502553.8088162648
  #define PSICRO_SWR 12671.44847681913
  #define PSICRO_SAR 6771.39668959704




extern double psicro_inicial[PSICRO_NV]={
                    20,0.01,CALCULA_PSTD,0.922,195406,1500,0.2,0.5,\
                    0,71,10,15,0.5,100,.012,40000,150,0.9}; 

extern char psicro_tipos[PSICRO_NV][4]={"T","X","P","V","H","PV","XS","WR" \
                 ,"ALT","S","TD","TBH","GS","TH","XBH","HA","SA","VA"};




funciones

//pw

 psicro_prop_pw(t, &pw);  
  tk = t + CALCULA_KO;
 /* if (t<260) {*resul=4.23e-66; return 0;}*/
  if (tk<CALCULA_KO) {
         res=-5674.5359 / tk + 6.3925247 - 9.677843E-03 * tk + 
           .000000622115701 * (tk2) + .0000000020747825 * (tk3) 
           - 9.484024E-13 * (tk4) + 4.1635019 * log(tk) ;
      }
      else {  
	res=-5800.2206 / tk + 1.3914993 - .048640239 * tk 
           +.000041764768 * tk2 - .000000014452093 * tk3  
           + 6.5459673 * log(tk);
      };

  pw=exp(res)*101325/101418.71683





//h_aw
psicro_prop_h_aw(double t, double xw, double p, int pre,double  *resul)  
 tk = t + CALCULA_KO;
 	
    if (pre==-1) {      /*     <------  ********************************/
	       fc=.9998;
	       ww=xw/(1-xw)*CALCULA_PMWG/CALCULA_PMA; 
	       /*error en la saturacion inadmisible usar w en la formula
		 fprintf(stderr," %f, %f ",xw,ww);*/ 
     	       h_aw= ((1006.05 + 1805.0 * ww  * fc) * t + PSICRO_HWE * fc * ww)
           
    }else{
	      /*Entalpias a presion 0. */
             
	      ha0 = 42.89510766 + .22881637 * t + .00001861 * t2  + 235.87232451 / tk + 3.84444182 * log(tk);
              hw0 = -24.27107566 + .37621851 * t + .0001057 * t2 + 1675.81819168 / tk + 24.6462724 * log(tk);
              har = 273231.0581312534;
              hm = (4186.8 * ha0 - har) * CALCULA_PMA * xa + (4186.8 * hw0 - PSICRO_HWR + PSICRO_HWE) * CALCULA_PMWG * xw;
               
              h_aw= (hm + dif);  
	 	}

//h_wl
 
psicro_prop_h_wl(double t,double *resul)  

	    h_wl = 4186. * t;
 
//xs
psicro_prop_xs(double pw, double p, double *resul)
 

    *resul= pw/p;








    if (xws==0) {fprintf(stderr,"Error XWS es 0\n"); exit(-1);} 
    if (p<=0) {fprintf(stderr,"Error P es=< 0, pw=%f\n",pw); exit(-1);}
 /* if (pw>=p) *resul= (double) 1; else 
  */ 

	      
 /*
  if (xw>1)  xw=1;
          if (xa>1)  xa=1;*/
   

  double factor=1;
  if (pre==1) factor=1;

  tk = t + CALCULA_KO;
  tk2=tk*tk;
  tk3=tk2*tk;
  tk4=tk3*tk;
  t2=t*t;             
  xa=1-xw;


 /* VER BWWW********************************************/
       /*Coeficientes viriales.*/
	      die = pow(10,72000 / tk2);
              ded = log(10) / (tk2) * 72000;
              bww = -33.97 + 55306 * die * 2 / tk * (1 + ded);
              baw = -29.53 + .00669 / tk * (2 - exp(-4416.5 / tk) * (2 + 4416.5 / tk)) + 2 / tk * 17540 + 3 * 95300 / tk2 + 4 * 85150000 / tk3;
              baa = -40.7 + 2 * 13116 / tk + 4 * 120000000 / tk3;
              jjj=(-33.97 + 55306 / tk * die);
              bwww= .0348 * 3 * bww / tk2 * jjj*jjj;
              baa = baa * .000001;
              bww = bww * .000001;
              baw = baw * .000001;
              bwww = bwww * .000001 / CALCULA_PSTD;

dif = -p * (xa *xa * baa + xw * xa * baw * 2 + xw*xw * bww + p / 2 * xw *xw*xw * bwww);
              dif=0; 
 
///s_aw
	/*Coeficientes viriales.*/
    aww = -33.97 + 55306 / tk * pow(10,72000 / tk2);
    cww = -55306 / tk2 * pow(10,72000 / tk2) * (1 + 2 * 72000 / tk2 * log(10));
    caw = .00669 / tk2 * (1 - exp(-4416.5 / tk) * (1 + 4416.5 / tk)) - 17540 / tk2 - 2 * 95300 / tk3 - 3 * 85150000 / tk4;
    caa = -13116 / tk2 - 3 * 120000000 / tk4;
    cwww = .0348 / tk2 * (3 * aww *aww * cww - 2 / tk * aww *aww*aww);
            
    /*Imprime CAA#, CWW#, CAW#, CWWW#, AWW#*/
    caa = caa * .000001;
    cww = cww * .000001;
    caw = caw * .000001;
    cwww = cwww * .000001 / CALCULA_PSTD;


	/* truco para bypasear el  x*log(x)  mas alla del 0 y el 1*/      
        faw =0;
        if (xw<=0.99999||xw>=0.00001) faw=  xw * log(xw)+(1-xw)*log(1-xw);
	if (xw<0.00001) faw= -.0001251292046498/0.00001*xw;
        if (xw>0.99999)	faw= -.0001251292046498/0.00001*(1-xw);

	/*	fprintf(stderr,"faw %g %g\n",xw, faw);  */    




      


//p
   cero= CALCULA_PSTD * pow(288.15/(288.15-0.006500*alt),-5.255977)-p

//t
   cero= 288.15-0.006500*alt-t
//wr
   cero = xw - wr *xws
// xs
psicro_prop_xs( pw, p, &xsc)
   cero= xs -xsc

//gs
    cero=gs * xws *(1-xw)-xw * (1-xws)

 
//pw



 psicro_prop_pw(t, &pw);  
  cero =exp(res)*101325/101418.71683 -pw

//h_aw
  psicro_prop_h_aw(double t, double xw, double p, int pre,double  *resul)  
  cero=h_aw-H

/* cambiar vol a vol_aw*/
/*vol*/

  cero = factor * CALCULA_R * tk -vol * p;

//s_aw



            
	/*Entropias a presion 0. */ 
        sw0 = -.153302928 - .000061635 * t + .000000164 * t2 + 1.28522117 / tk + .465553696 * log(tk);
	sa0 = .100182446 - 7.607599999999999E-05 * t + 9.536569128E-08 * t2 + 2.307481401 / tk + .268927736 * log(tk);
	sm = (4186.8 * sa0 - PSICRO_SAR) * CALCULA_PMA * xa + (4186.8 * sw0 - PSICRO_SWR + PSICRO_SWE) * CALCULA_PMWG * xw - CALCULA_R * (log(p / 101325) + faw);

       	s_aw= (sm + dif);



//tbh

//psicro_prop_tbh(double t,double xw, double p,double h, double tbh, int pre,double *resul,double *resul2) 

  

    psicro_prop_pw(tbh, &pwbh);  
    psicro_prop_xs(pwbh,  p,  &xwsbh);  
    psicro_prop_h_aw(tbh,xwsbh,p,pre,&hbh);
    psicro_prop_h_wl(tbh,&hw);


    cero=   xw *(2501+1.805*t -4.186 * tbh)*(1-xwsbh)-(2501-2.381*tbh)*xwsbh*(1-xw)+(t-tbh)*CALCULA_PMA/CALCULA_PMWG*(1-xw)*(1-xwsbh);
    
//     *resul2=xwsbh;
    cero2= XWSBH - xwsbh 


//th

  
  psicro_prop_pw(th, &pwd);  
  cero=p-pwd;



  //td
  
  psicro_prop_pw(td, &pwd);  
  psicro_prop_xs(pwd,  p,  &xwd); 
  /*  fprintf(stderr,"TD %f %f ",xwd-xw,xwd);*/
  cero=xw-xwd;


inrange, iclamp,var, avg, max, min, mul, sum, 
  and, mand, mor, nand, nor, not, or, shl, shr,
  xnor, xor, true, false, 
abs, avg, ceil, clamp, equal, erf, erfc,  exp,
                       expm1, floor, frac,  log, log10, log1p,  log2,
                       logn,  max,  min,  mul,  ncdf,  nequal,  root,
  round, roundn, sgn, sqrt, sum, swap, trunc,
acos, acosh, asin, asinh, atan, atanh,  atan2,
                       cos,  cosh, cot,  csc, sec,  sin, sinc,  sinh,
                       tan, tanh, hypot, rad2deg, deg2grad,  deg2rad,
  grad2deg,
  if, then, else,if-else, switch, case,
  while, for, repeat,until, break, continue,
in, like, ilike, concatenation

