-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMKT_function.sql
60 lines (54 loc) · 1.08 KB
/
MKT_function.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
create or replace function mkt_sfunc(agg_state point, TEMP numeric)
returns point
immutable
language plpgsql
as $$
declare
DH numeric; --activation energy
R numeric; --Gas Constant:
KV numeric; -- C° to Kelvin
Xs numeric;
Xn numeric; -- X current
n integer; -- number of temperature
begin
DH := 83.14472;
R := 0.008314472;
KV := 273.15;
Xn := exp((-1*DH)/(R*(TEMP+KV)));
Xs := agg_state[0] + Xn;
n := 1 + agg_state[1];
return point(Xs,n);
end;
$$;
create or replace function mkt_finalfunc(agg_state point)
returns numeric
immutable
strict
language plpgsql
as $$
declare
DH numeric; --activation energy
R numeric; --Gas Constant:
KV numeric;
Xs numeric;
n integer; -- number of temperature
dividend numeric;
divider numeric;
begin
DH := 83.14472;
R := 0.008314472;
KV := 273.15;
Xs := agg_state[0];
n := agg_state[1];
dividend := DH/R;
divider := (ln(Xs/n)) * -1;
return round((dividend/divider) - KV,2);
end;
$$;
create aggregate mkt(numeric)
(
sfunc = mkt_sfunc,
stype = point,
finalfunc = mkt_finalfunc,
initcond = '0,0'
);