Merge branch 'fixf'
[wrf-fire-matlab.git] / detection / fuel_left_cell.m
blob98f1b3d87f86fc810a2a19648a1402b2a6a2f807
1 function varargout=fuel_left_cell(t,fuel_time)
2 % frac=fuel_left(t,fuel_time)
3 % [frac,dfracdt]=fuel_left(t,fuel_time)
4 % [frac,dfracdt,dfrac]=fuel_left(t,fuel_time)
5
6 % compute fuel fraction left in one cell
7
8 % in: 
9 %   t           fire arrival time (0=now) at corners, size (2,2)
10 %   fuel_time   time the fuel takes to burn to 1/e = 0.36
11 % out:
12 %   frac        the fuel fraction
13 %   dfracdt     derivative the all components of t change by the same
14 %   dfrac       partial derivatives wrf to components of t
16 % to test:
17 % t=[-1,0;0,1.1], s=0.1, f=1, dt=0.01
18 % p1=fuel_left_cell(t-dt-s,f);p2=fuel_left_cell(t+dt-s,f);
19 % [p,dp]=fuel_left_cell(t-s,f);d=(p2-p1)/(2*dt);err=d-dp
21 ps=ssum(t);
22 aps=ssum(abs(t))+realmin;
23 area=0.5*(1-ps/aps);
24 t0=min(t,0);
25 ta=0.25*ssum(t0);
26 frac=area*exp(ta/fuel_time) + (1. - area);
28 varargout(1)={frac};
30 if nargout < 2, return, end
32 % derivative wrt same increment in all 4 values of t
33 dpsdt=4;
34 dapsdt=ssum(sign(t));
35 dareadt=0.5*(dapsdt*ps-dpsdt*aps)./(aps.*aps);
36 dtadt=0.25*ssum(0.5*(1-sign(t)));
37 dfracdt=(dareadt+(area/fuel_time)*dtadt)*exp(ta/fuel_time)-dareadt;
39 varargout(2)={dfracdt};
41 if nargout < 3, return, end
43 % for heat flux, need derivative wrt same increment
44 dps=ones(2);
45 daps=sign(t);
46 darea=0.5*(daps*ps-dps*aps)./(aps.*aps);    
47 dta=0.25*(0.5*(1-sign(t)));
48 dfrac=((area/fuel_time)*dta+darea)*exp(ta/fuel_time)-darea;
50 varargout(3)={dfrac};
52 end