
CREATE FUNCTION "phi" (double precision,double precision) RETURNS double precision AS 'select 0.5*(1+ $1 *( $2 - 0.2)+ $2 * $2 )
' LANGUAGE 'sql';


CREATE FUNCTION "min" (double precision,double precision) RETURNS double precision AS 'select case when $1 < $2 then $1 else $2 end
' LANGUAGE 'sql';


CREATE FUNCTION "chi" (double precision,double precision) RETURNS double precision AS 'select min(1, 1/ ( phi($1, $2) + sqrt( phi($1,$2)^2 - $2 ^2 )::float8 ))
' LANGUAGE 'sql';


CREATE FUNCTION "beta_mph" (double precision) RETURNS double precision AS 'select 1.8 - 0.7 * $1
' LANGUAGE 'sql';


CREATE FUNCTION "kpand" (double precision,double precision,double precision,double precision) RETURNS double precision AS 'select min(1.5, 1- $1 * $3 / $2 / $4)
' LANGUAGE 'sql';


CREATE FUNCTION "mupand" (double precision,double precision,double precision,double precision) RETURNS double precision AS 'select min(0.9, $1 *(2 * beta_mph($2) - 4) + $3 / $4 - 1)
' LANGUAGE 'sql';


CREATE FUNCTION "rnd2" (double precision) RETURNS double precision AS 'select float8(int4(($1+0.005)*100))/100 ' LANGUAGE 'sql';


CREATE FUNCTION "lamb" (double precision) RETURNS double precision AS 'select $1/(76.3986) ' LANGUAGE 'sql';


CREATE FUNCTION "klt" (double precision,double precision,double precision,double precision) RETURNS double precision AS 'select min (1, 1 - $1 * $3 / $2 / $4) ' LANGUAGE 'sql';


CREATE FUNCTION "mult" (double precision,double precision) RETURNS double precision AS 'select min (0.9, 0.15* $1 * beta_mph( $2 )-0.15) ' LANGUAGE 'sql';


CREATE FUNCTION "acrt" (double precision,double precision) RETURNS double precision AS 'select 9.8696 * $2 / $1 / $1 ' LANGUAGE 'sql';


CREATE FUNCTION "csub1" (double precision) RETURNS double precision AS 'select min(2.7,1.88 - 1.4 * $1 + 0.52 * $1 * $1 ) ' LANGUAGE 'sql';


CREATE FUNCTION "lamblt" (double precision,double precision,double precision,double precision,double precision,double precision) RETURNS double precision AS 'select   sqrt(9.8696 * $2 /     ( csub1($6)*acrt(100 * $1 , $3)*        sqrt( $4 / $3 + $5/  (2.56*acrt(100* $1 , $3) )             )::float8     ))::float8/76.3986   ' LANGUAGE 'sql';


CREATE TABLE "esfuerzos_tartan" (
	"pilar" character varying(50),
	"combinacion" character varying(50),
	"posicion" character varying(50),
	"nsd" double precision,
	"vyd" double precision,
	"vzd" double precision,
	"mt" double precision,
	"msyd" double precision,
	"mszd" double precision
);


CREATE FUNCTION "NSd/NRd+MSd/MRd" (double precision,double precision,double precision,double precision,double precision,double precision) RETURNS double precision AS 'DECLARE
               Nsd   ALIAS FOR $1;
               Npl   ALIAS FOR $2;
               Msdy  ALIAS FOR $3;
               Mply  ALIAS FOR $4;
               Msdz  ALIAS FOR $5;
               Mplz  ALIAS FOR $6;
         BEGIN
               RETURN abs(NSd) /Npl  +
                      abs(Msdy)/Mply +
                      abs(Msdz)/Mplz; 
         END
' LANGUAGE 'plpgsql';


CREATE FUNCTION "psi" (double precision,double precision) RETURNS double precision AS ' 
DECLARE
    M1   ALIAS FOR $1;
    M2   ALIAS FOR $2;
    M1M2 float8;
BEGIN
    M1M2=M1/M2;
    IF (abs(M1M2)<1) THEN
        RETURN M1M2;
    END IF;
    RETURN 1/M1M2;
    END
' LANGUAGE 'plpgsql';


CREATE FUNCTION "beta_mpsi" (double precision) RETURNS double precision AS '
  DECLARE
      psi ALIAS FOR $1;
  BEGIN
      RETURN 1.8-0.7*psi;
  END
' LANGUAGE 'plpgsql';


CREATE FUNCTION "beta_mpsi" (double precision,double precision) RETURNS double precision AS '
DECLARE
    M1 ALIAS FOR $1;
    M2 ALIAS FOR $2;
BEGIN
    RETURN 1.8-0.7*psi(M1,M2);
END
' LANGUAGE 'plpgsql';


CREATE FUNCTION "muyz" (double precision,double precision,double precision,double precision) RETURNS double precision AS '
DECLARE
    lambda   ALIAS FOR $1;
    beta     ALIAS FOR $2;
    Wpl      ALIAS FOR $3;
    Wel      ALIAS FOR $4;
BEGIN
    RETURN min(lambda*(2*beta-4)+(Wpl-Wel)/Wel,
               0.9);
END
' LANGUAGE 'plpgsql';


CREATE FUNCTION "kyz" (double precision,double precision,double precision,double precision) RETURNS double precision AS '
DECLARE
    mu   ALIAS FOR $1;
    chi  ALIAS FOR $2;
    Nsd  ALIAS FOR $3;
    Nrd  ALIAS FOR $4;
BEGIN
    RETURN min(1-mu*Nsd/(chi*Nrd),
               1.5);
END
' LANGUAGE 'plpgsql';


