5 !#define DEBUG_OUT_FUEL_LEFT
7 module module_fr_fire_core
9 use module_fr_fire_phys, only: fire_params , fire_ros
10 use module_fr_fire_util
12 ! The mathematical core of the fire spread model. No physical constants here.
14 ! subroutine fire_core: only this routine should be called from the outside.
15 ! subroutine fuel_left: compute remaining fuel from time of ignition.
16 ! subroutine prop_ls: propagation of curve in normal direction.
18 ! describe one ignition line
19 type ignition_line_type
20 REAL ros, & ! subscale rate of spread during the ignition process
21 stop_time, & ! when the ignition process stops from ignition start (s)
22 wind_red, & ! wind reduction factor at the ignition line
23 wrdist, & ! distance from the ignition line when the wind reduction stops
24 wrupwind, & ! use distance interpolated between 0. = nearest 1. = upwind
25 start_x, & ! x coordinate of the ignition line start point (m, or long/lat)
26 start_y, & ! y coordinate of the ignition line start point
27 end_x, & ! x coordinate of the ignition line end point
28 end_y, & ! y coordinate of the ignition line end point
29 start_time, & ! ignition time for the start point from simulation start (s)
30 end_time, & ! ignition time for the end poin from simulation start (s)
31 radius ! all within this radius ignites immediately
32 end type ignition_line_type
37 !****************************************
40 subroutine init_no_fire(&
41 ifds,ifde,jfds,jfde, &
42 ifms,ifme,jfms,jfme, &
43 ifts,ifte,jfts,jfte, &
44 fdx,fdy,time_now, & ! scalars in
45 fuel_frac,fire_area,lfn,tign) ! arrays out
48 !*** purpose: initialize model to no fire
51 integer, intent(in):: ifds,ifde,jfds,jfde ! fire domain bounds
52 integer, intent(in):: ifts,ifte,jfts,jfte ! fire tile bounds
53 integer, intent(in):: ifms,ifme,jfms,jfme ! array bounds
54 real, intent(in) :: fdx,fdy,time_now ! mesh spacing, time
55 real, intent(out), dimension (ifms:ifme,jfms:jfme) :: &
56 fuel_frac,fire_area,lfn,tign ! model state
63 real lfn_init,time_init
65 lfn_init = 2*max((ifde-ifds+1)*fdx,(jfde-jfds+1)*fdy) ! more than domain diameter
66 time_init=time_now + max(time_now,1.0)*epsilon(time_now) ! a bit in future
70 fuel_frac(i,j)=1. ! fuel at start is 1 by definition
71 fire_area(i,j)=0. ! nothing burning
72 tign(i,j) = time_init ! ignition in future
73 lfn(i,j) = lfn_init ! no fire
76 call message('init_no_fire: state set to no fire')
78 end subroutine init_no_fire
85 subroutine ignite_fire( ifds,ifde,jfds,jfde, & ! fire domain dims - the whole domain
86 ifms,ifme,jfms,jfme, &
87 ifts,ifte,jfts,jfte, &
95 !*** purpose: ignite a circular/line/prescribed fire
98 ! ignite fire in the region within radius r from the line (sx,sy) to (ex,ey).
99 ! the coordinates of nodes are given as the arrays coord_xf and coord_yf
101 ! one unit of coord_xf is unit_xf m
102 ! one unit of coord_yf is unit_yf m
103 ! so a node (i,j) will be ignited iff for some (x,y) on the line
104 ! || ( (coord_xf(i,j) - x)*unit_xf , (coord_yf(i,j) - y)*unit_yf ) || <= r
108 integer, intent(in):: ifds,ifde,jfds,jfde ! fire domain bounds
109 integer, intent(in):: ifts,ifte,jfts,jfte ! fire tile bounds
110 integer, intent(in):: ifms,ifme,jfms,jfme ! array bounds
111 type(ignition_line_type), intent(in):: ignition_line ! description of the ignition line
112 real, intent(in):: start_ts,end_ts ! the time step start and end
113 real, dimension(ifms:ifme, jfms:jfme), intent(in):: &
114 coord_xf,coord_yf ! node coordinates
115 real, intent(in):: unit_xf,unit_yf ! coordinate units in m
116 real, intent(inout), dimension (ifms:ifme,jfms:jfme) :: &
117 lfn, tign ! level function, ignition time (state)
118 integer, intent(out):: ignited ! number of nodes newly ignited
122 real::lfn_new,time_ign,ax,ay,rels,rele,d
123 real:: sx,sy ! start of ignition line, from lower left corner
124 real:: ex,ey ! end of ignition line, or zero
125 real:: st,et ! start and end of time of the ignition line
126 character(len=128):: msg
127 real::cx2,cy2,dmax,axmin,axmax,aymin,aymax,dmin
128 real:: start_x,start_y ! start of ignition line, from lower left corner
129 real:: end_x,end_y ! end of ignition line, or zero
130 real:: radius ! all within the radius of the line will ignite
131 real:: start_time,end_time ! the ignition time for the start and the end of the line
132 real:: ros,tos ! ignition rate and time of spread
136 ! copy ignition line fields to local variables
137 start_x = ignition_line%start_x ! x coordinate of the ignition line start point (m, or long/lat)
138 start_y = ignition_line%start_y ! y coordinate of the ignition line start point
139 end_x = ignition_line%end_x ! x coordinate of the ignition line end point
140 end_y = ignition_line%end_y ! y coordinate of the ignition line end point
141 start_time = ignition_line%start_time ! ignition time for the start point from simulation start (s)
142 end_time = ignition_line%end_time! ignition time for the end poin from simulation start (s)
143 radius = ignition_line%radius ! all within this radius ignites immediately
144 ros = ignition_line%ros ! rate of spread
145 tos = radius/ros ! time of spread to the given radius
146 st = start_time ! the start time of ignition considered in this time step
147 et = min(end_ts,end_time) ! the end time of the ignition segment in this time step
149 ! this should be called whenever (start_ts, end_ts) \subset (start_time, end_time + tos)
150 if(start_ts>et+tos .or. end_ts<st)return ! too late or too early, nothing to do
152 if(start_time < end_time)then ! we really want to test start_time .ne. end_time, but avoiding test of floats on equality
153 ! segment of nonzero length
154 !rels = (st - start_time) / (end_time - start_time) ! relative position of st in the segment (start,end)
155 !sx = start_x + rels * (end_x - start_x)
156 !sy = start_y + rels * (end_y - start_y)
160 rele = (et - start_time) / (end_time - start_time) ! relative position of et in the segment (start,end)
161 ex = start_x + rele * (end_x - start_x)
162 ey = start_y + rele * (end_y - start_y)
177 axmin=coord_xf(ifts,jfts)
178 aymin=coord_yf(ifts,jfts)
179 axmax=coord_xf(ifte,jfte)
180 aymax=coord_yf(ifte,jfte)
181 !$OMP CRITICAL(FIRE_CORE_CRIT)
182 write(msg,'(a,2g13.6,a,2g13.6)')'IGN from ',sx,sy,' to ',ex,ey
184 write(msg,'(a,2f10.2,a,2f10.2,a)')'IGN timestep [',start_ts,end_ts,'] in [',start_time,end_time,']'
186 write(msg,'(a,2g13.6,a,2g13.6)')'IGN tile coord from ',axmin,aymin,' to ',axmax,aymax
188 !$OMP END CRITICAL(FIRE_CORE_CRIT)
192 11 format('IGN ',6(a,g17.7,1x))
193 12 format('IGN ',4(a,2g13.7,1x))
199 ! get d= distance from the nearest point on the ignition segment
200 ! and time_ign = the ignition time there
201 call nearest(d,time_ign,ax,ay,sx,sy,st,ex,ey,et,cx2,cy2)
205 lfn_new=d - min( radius, ros*(end_ts - time_ign) ) ! lft at end_ts
206 if(fire_print_msg.ge.3)then
207 !$OMP CRITICAL(FIRE_CORE_CRIT)
208 write(msg,*)'IGN1 i,j=',i,j,' lfn(i,j)=',lfn(i,j),' tign(i,j)=',tign(i,j)
210 write(msg,*)'IGN2 i,j=',i,j,' lfn_new= ',lfn_new, ' time_ign= ',time_ign,' d=',d
212 !$OMP END CRITICAL(FIRE_CORE_CRIT)
214 if(.not.lfn_new>0.) then
215 ignited=ignited+1 ! count
217 if(lfn(i,j)>0. .and. .not. lfn_new > 0.) then
218 tign(i,j)=time_ign + d/ros ! newly ignited now
219 if(fire_print_msg.ge.3)then
220 !$OMP CRITICAL(FIRE_CORE_CRIT)
221 write(msg,'(a,2i6,a,2g13.6,a,f10.2,a,2f10.2,a)')'IGN ignited cell ',i,j,' at',ax,ay, &
222 ' time',tign(i,j),' in [',start_ts,end_ts,']'
224 write(msg,'(a,g10.3,a,f10.2,a,2f10.2,a)')'IGN distance',d,' from ignition line at',time_ign,' in [',st,et,']'
226 !$OMP END CRITICAL(FIRE_CORE_CRIT)
228 if(tign(i,j) < start_ts .or. tign(i,j) > end_ts )then
229 !$OMP CRITICAL(FIRE_CORE_CRIT)
230 write(msg,'(a,2i6,a,f11.6,a,2f11.6,a)')'WARNING ',i,j, &
231 ' fixing ignition time ',tign(i,j),' outside of the time step [',start_ts,end_ts,']'
233 !$OMP END CRITICAL(FIRE_CORE_CRIT)
234 tign(i,j) = min(max(tign(i,j),start_ts),end_ts)
237 lfn(i,j)=min(lfn(i,j),lfn_new) ! update the level set function
238 if(fire_print_msg.ge.3)then
239 !$OMP CRITICAL(FIRE_CORE_CRIT)
240 write(msg,*)'IGN3 i,j=',i,j,' lfn(i,j)=',lfn(i,j),' tign(i,j)=',tign(i,j)
242 !$OMP END CRITICAL(FIRE_CORE_CRIT)
246 !$OMP CRITICAL(FIRE_CORE_CRIT)
247 write(msg,'(a,2g13.2,a,g10.2,a,g10.2)')'IGN units ',unit_xf,unit_yf,' m max dist ',dmax,' min',dmin
249 write(msg,'(a,f6.1,a,f8.1,a,i10)')'IGN radius ',radius,' time of spread',tos,' ignited nodes',ignited
251 !$OMP END CRITICAL(FIRE_CORE_CRIT)
256 end subroutine ignite_fire
258 ! called from the inside of a loop, inline if possible
259 !DEC$ ATTRIBUTES FORCEINLINE
260 SUBROUTINE nearest(d,t,ax,ay,sx,sy,st,ex,ey,et,cx2,cy2)
263 real, intent(out):: d,t
264 real, intent(in):: ax,ay,sx,sy,st,ex,ey,et,cx2,cy2
266 ! ax, ay coordinates of point a
267 ! sx,sy,ex,ey coordinates of endpoints of segment [x,y]
268 ! st,et values at the endpoints x,y
269 ! cx2,cy2 x and y unit squared for computing distance
272 ! d the distance of a and the nearest point z on the segment [x,y]
273 ! t linear interpolation from the values st,et to the point z
275 ! method: compute d as the distance (ax,ay) from the midpoint (mx,my)
276 ! minus a correction (because of rounding errors): |a-c|^2 = |a-m|^2 - |m-c|^2
277 ! when |m-c| >= |s-e|/2 the nearest point is one of the endpoints
278 ! the computation work also for the case when s=e exactly or approximately
285 ! |m-c| = |a-m| cos (a-m,e-s)
286 ! = |a-m| (a-m).(e-s))/(|a-m|*|e-s|)
288 real:: mx,my,dam2,dames,am_es,cos2,dmc2,mcrel,mid_t,dif_t,des2,cx,cy
289 character(len=128):: msg
292 11 format('IGN ',6(a,g17.7,1x))
293 12 format('IGN ',4(a,2g13.7,1x))
295 ! midpoint m = (mx,my)
298 dam2=(ax-mx)*(ax-mx)*cx2+(ay-my)*(ay-my)*cy2 ! |a-m|^2
299 des2 = (ex-sx)*(ex-sx)*cx2+(ey-sy)*(ey-sy)*cy2 ! des2 = |e-s|^2
301 am_es=(ax-mx)*(ex-sx)*cx2+(ay-my)*(ey-sy)*cy2 ! am_es = (a-m).(e-s)
303 cos2 = (am_es*am_es)/dames ! cos2 = cos^2 (a-m,e-s)
304 else ! point a already is the midpoint
307 dmc2 = dam2*cos2 ! dmc2 = |m-c|^2
308 if(4.*dmc2 < des2)then ! if |m-c|<=|e-s|/2
309 ! d = sqrt(max(dam2 - dmc2,0.)) ! d=|a-m|^2 - |m-c|^2, guard rounding
310 mcrel = sign(sqrt(4.*dmc2/des2),am_es) ! relative distance of c from m
311 elseif(am_es>0)then ! if cos > 0, closest is e
316 cx = (ex + sx)*0.5 + mcrel*(ex - sx)*0.5 ! interpolate to c by going from m
317 cy = (ey + sy)*0.5 + mcrel*(ey - sy)*0.5 ! interpolate to c by going from m
318 d=sqrt((ax-cx)*(ax-cx)*cx2+(ay-cy)*(ay-cy)*cy2) ! |a-c|^2
319 t = (et + st)*0.5 + mcrel*(et - st)*0.5 ! interpolate to c by going from m
320 if(fire_print_msg.ge.3)then
321 !$OMP CRITICAL(FIRE_CORE_CRIT)
322 write(msg,12)'find nearest to [',ax,ay,'] from [',sx,sy,'] [',ex,ey,']' ! DEB
324 write(msg,12)'end times',st,et,' scale squared',cx2,cy2 ! DEB
326 write(msg,11)'nearest at mcrel=',mcrel,'from the midpoint, t=',t ! DEB
328 write(msg,12)'nearest is [',cx,cy,'] d=',d ! DEB
330 write(msg,11)'dam2=',dam2,'des2=',des2,'dames=',dames
332 write(msg,11)'am_es=',am_es,'cos2=',cos2,'dmc2=',dmc2 ! DEB
334 !$OMP END CRITICAL(FIRE_CORE_CRIT)
336 END SUBROUTINE nearest
340 !**********************
343 subroutine fuel_left(&
347 lfn, tign, fuel_time, time_now, fuel_frac, fire_area)
350 !*** purpose: determine fraction of fuel remaining
351 !*** NOTE: because variables are cell centered, need halo/sync width 1 before
355 integer, intent(in) :: its,ite,jts,jte,ims,ime,jms,jme,ifs,ife,jfs,jfe
356 real, intent(in), dimension(ims:ime,jms:jme)::lfn,tign,fuel_time
357 real, intent(in):: time_now
358 real, intent(out), dimension(ifs:ife,jfs:jfe)::fuel_frac
359 real, intent(out), dimension(ims:ime,jms:jme):: fire_area
361 ! ims,ime,jms,jme in memory dimensions
362 ! its,ite,jts,jte in tile dimensions (cells where fuel_frac computed)
363 ! ifs,ife,jfs,jfe in fuel_frac memory dimensions
364 ! lfn in level function, at nodes at midpoints of cells
365 ! tign in ignition time, at nodes at nodes at midpoints of cells
366 ! fuel_time in time constant of fuel, per cell
367 ! time_now in time now
368 ! fuel_frac out fraction of fuel remaining, per cell
369 ! fire_area out fraction of cell area on fire
373 integer::i,j,ir,jr,icl,jcl,isubcl,jsubcl,i2,j2,ii,jj
374 real::fmax,frat,helpsum1,helpsum2,fuel_left_ff,fire_area_ff,rx,ry,tignf(2,2)
375 ! help variables instead of arrays fuel_left_f and fire_area_f
376 real::lffij,lffi1j,lffij1,lffi1j1,tifij,tifi1j,tifij1,tifi1j1,tx,ty,txx,tyy
377 ! variables for calculation instead of lff(i,j) and tif(i,j)is lffij,tifij etc..#define IFCELLS (ite-its+1)*fuel_left_irl
378 #define JFCELLS (jte-jts+1)*fuel_left_jrl
379 character(len=128)::msg
380 integer::m,omp_get_thread_num
387 if ((ir.ne.2).or.(jr.ne.2)) then
388 call crash('fuel_left: ir.ne.2 or jr.ne.2 ')
394 ! interpolate level set function to finer grid
395 #ifdef DEBUG_OUT_FUEL_LEFT
396 call write_array_m(1,IFCELLS+1,1,JFCELLS+1,1,IFCELLS+1,1,JFCELLS+1,lff,'lff',0)
397 call write_array_m(1,IFCELLS+1,1,JFCELLS+1,1,IFCELLS+1,1,JFCELLS+1,tif,'tif',0)
404 ! its-1 its ite ite+1
405 ! -------X------------|-----.-----X-----.-----|--........----|----------X----------|---------X
406 ! fine node 1 2 3 2*(ite-its+1)
407 ! fine cell 1 2 cell 2*(ite-its+1)
411 ! Loop over cells in Tile
412 ! Changes made by Volodymyr Kondratenko 09/24/2009
417 ! Loop over subcells in cell #(icl,jcl)
420 i=(icl-its)*ir+isubcl
421 j=(jcl-jts)*jr+jsubcl
423 ! Direct calculation tif and lff, avoiding arrays, just for case ir=jr=2
424 if ((isubcl.eq.1).and.(jsubcl.eq.1)) then
431 else if ((isubcl.eq.2).and.(jsubcl.eq.1)) then
438 else if ((isubcl.eq.1).and.(jsubcl.eq.2)) then
445 else if ((isubcl.eq.2).and.(jsubcl.eq.2)) then
453 call crash('fuel_left: isubcl,jsubcl should be only 1 or 2')
456 ! calculation lff and tif in 4 endpoints of subcell
458 (1-tx)*(1-ty)*lfn(i2,j2) &
459 + (1-tx)*ty *lfn(i2,j2+1) &
460 + tx*(1-ty)*lfn(i2+1,j2) &
461 + tx*ty *lfn(i2+1,j2+1)
463 (1-txx)*(1-ty)*lfn(i2,j2) &
464 + (1-txx)*ty *lfn(i2,j2+1) &
465 + (txx)*(1-ty)*lfn(i2+1,j2) &
466 + (txx)*ty *lfn(i2+1,j2+1)
468 (1-tx)*(1-tyy)*lfn(i2,j2) &
469 + (1-tx)*(tyy) *lfn(i2,j2+1) &
470 + tx*(1-tyy)*lfn(i2+1,j2) &
471 + tx*(tyy) *lfn(i2+1,j2+1)
473 (1-txx)*(1-tyy)*lfn(i2,j2) &
474 + (1-txx)*(tyy) *lfn(i2,j2+1) &
475 + (txx)*(1-tyy)*lfn(i2+1,j2) &
476 + (txx)*(tyy) *lfn(i2+1,j2+1)
478 ! get ready to fix up tign values to be interpolated
481 tignf(ii,jj)=tign(i2+ii-1,j2+jj-1)
485 (1-tx)*(1-ty)*tignf(1,1) &
486 + (1-tx)*ty*tignf(1,1+1) &
487 + tx*(1-ty)*tignf(1+1,1) &
488 + tx*ty*tignf(1+1,1+1)
490 (1-txx)*(1-ty)*tignf(1,1) &
491 + (1-txx)*ty*tignf(1,1+1) &
492 + (txx)*(1-ty)*tignf(1+1,1) &
493 + (txx)*(ty)*tignf(1+1,1+1)
495 (1-tx)*(1-tyy)*tignf(1,1) &
496 + (1-tx)*(tyy)*tignf(1,1+1) &
497 + tx*(1-tyy)*tignf(1+1,1) &
498 + tx*(tyy)*tignf(1+1,1+1)
500 (1-txx)*(1-tyy)*tignf(1,1) &
501 + (1-txx)*(tyy)*tignf(1,1+1) &
502 + (txx)*(1-tyy)*tignf(1+1,1) &
503 + (txx)*(tyy)*tignf(1+1,1+1)
506 if(fuel_left_method.eq.1)then
507 call fuel_left_cell_1( fuel_left_ff, fire_area_ff, &
508 lffij,lffij1,lffi1j,lffi1j1,&
509 tifij,tifij1,tifi1j,tifi1j1,&
510 time_now, fuel_time(icl,jcl))
511 elseif(fuel_left_method.eq.2)then
512 fire_area_ff=0 ! initialize to something until computed in fuel_left_f(i,j)
513 fuel_left_ff=fuel_left_cell_2( &
514 lffij,lffij1,lffi1j,lffi1j1,&
515 tifij,tifij1,tifi1j,tifi1j1,&
516 time_now, fuel_time(icl,jcl))
518 call crash('fuel_left: unknown fuel_left_method')
522 if(fire_area_ff.lt.-1e-6 .or. &
523 (fire_area_ff.eq.0. .and. fuel_left_ff.lt.1.-1e-6))then
524 !$OMP CRITICAL(FIRE_CORE_CRIT)
525 write(msg,'(a,2i6,2(a,f11.8))')'fuel_left: at node',i,j, &
526 ' of refined mesh fuel burnt',1-fuel_left_ff,' fire area',fire_area_ff
527 !$OMP END CRITICAL(FIRE_CORE_CRIT)
531 helpsum1=helpsum1+fuel_left_ff
532 helpsum2=helpsum2+fire_area_ff
535 fuel_frac(icl,jcl)=helpsum1
536 fire_area(icl,jcl)=helpsum2
543 #ifdef DEBUG_OUT_FUEL_LEFT
544 call write_array_m(its,ite,jts,jte,ims,ime,jms,jme,fire_area,'fire_area',0)
545 call write_array_m(its,ite,jts,jte,ims,ime,jms,jme,fuel_frac,'fuel_frac',0)
546 call write_array_m(1,IFCELLS,1,JFCELLS,1,IFCELLS,1,JFCELLS,fuel_left_f,'fuel_left_f',0)
547 call write_array_m(1,IFCELLS,1,JFCELLS,1,IFCELLS,1,JFCELLS,fire_area_f,'fire_area_f',0)
550 ! finish the averaging
553 fuel_frac(i,j) = fuel_frac(i,j) /(ir*jr) ! multiply by weight for averaging over coarse cell
554 fire_area(i,j) = fire_area(i,j) /(ir*jr) !
558 ! consistency check after sum
562 if(fire_area(i,j).eq.0.)then
563 if(fuel_frac(i,j).lt.1.-1e-6)then
564 !$OMP CRITICAL(FIRE_CORE_CRIT)
565 write(msg,'(a,2i6,2(a,f11.8))')'fuel_left: at node',i,j, &
566 ' fuel burnt',1-fuel_frac(i,j),' but fire area',fire_area(i,j)
567 !$OMP END CRITICAL(FIRE_CORE_CRIT)
571 frat=(1-fuel_frac(i,j))/fire_area(i,j)
576 !$OMP CRITICAL(FIRE_CORE_CRIT)
577 write(msg,'(a,4i6,a,f10.7)')'fuel_left: tile',its,ite,jts,jte,' max fuel burnt/area',fmax
578 !$OMP END CRITICAL(FIRE_CORE_CRIT)
583 end subroutine fuel_left
586 !************************
589 subroutine fuel_left_cell_1( fuel_frac_left, fire_frac_area, &
590 lfn00,lfn01,lfn10,lfn11, &
591 tign00,tign01,tign10,tign11,&
592 time_now, fuel_time_cell)
593 !*** purpose: compute the fuel fraction left in one cell
596 real, intent(out):: fuel_frac_left, fire_frac_area !
597 real, intent(in)::lfn00,lfn01,lfn10,lfn11 ! level set function at 4 corners of the cell
598 real, intent(in)::tign00,tign01,tign10,tign11! ignition time at the 4 corners of the cell
599 real, intent(in)::time_now ! the time now
600 real, intent(in)::fuel_time_cell ! time to burns off to 1/e
602 ! The area burning is given by the condition L <= 0, where the function P is
603 ! interpolated from lfn(i,j)
605 ! The time since ignition is the function T, interpolated in from tign(i,j)-time_now.
606 ! The values of tign(i,j) where lfn(i,j)>=0 are ignored, tign(i,j)=0 is taken
609 ! The function computes an approxmation of the integral
614 ! fuel_frac_left = 1 - | 1 - exp(-T(x,y)/fuel_time_cell)) dxdy
621 ! When the cell is not burning at all (all lfn>=0), then fuel_frac(i,j)=1.
622 ! Because of symmetries, the result should not depend on the mesh spacing dx dy
623 ! so dx=1 and dy=1 assumed.
628 ! (0,1) -----O--(1,1) O = points on the fireline, T=tnow
629 ! | \ | A = the burning area for computing
630 ! | \| fuel_frac(i,j)
634 ! (0,0)---------(1,0)
637 ! Approximations allowed:
638 ! The fireline can be approximated by straight line(s).
639 ! When all cell is burning, approximation by 1 point Gaussian quadrature is OK.
642 ! 1. The output should be a continuous function of the arrays lfn and
643 ! tign whenever lfn(i,j)=0 implies tign(i,j)=tnow.
644 ! 2. The output should be invariant to the symmetries of the input in each cell.
645 ! 3. Arbitrary combinations of the signs of lfn(i,j) should work.
646 ! 4. The result should be at least 1st order accurate in the sense that it is
647 ! exact if the time from ignition is a linear function.
649 ! If time from ignition is approximated by polynomial in the burnt
650 ! region of the cell, this is integral of polynomial times exponential
651 ! over a polygon, which can be computed exactly.
653 ! Requirement 4 is particularly important when there is a significant decrease
654 ! of the fuel fraction behind the fireline on the mesh scale, because the
655 ! rate of fuel decrease right behind the fireline is much larger
656 ! (exponential...). This will happen when
658 ! change of time from ignition within one mesh cell / fuel_time_cell is not << 1
660 ! This is the same as
663 ! X = ------------------------- is not << 1
664 ! fireline speed * fuel_time_cell
667 ! When X is large then the fuel burnt in one timestep in the cell is
668 ! approximately proportional to length of fireline in that cell.
670 ! When X is small then the fuel burnt in one timestep in the cell is
671 ! approximately proportional to the area of the burning region.
678 real::ps,aps,area,ta,out
679 real::t00,t01,t10,t11
680 real,parameter::safe=tiny(aps)
681 character(len=128)::msg
683 ! the following algorithm is a very crude approximation
685 ! minus time since ignition, 0 if no ignition yet
686 ! it is possible to have 0 in fire region when ignitin time falls in
687 ! inside the time step because lfn is updated at the beginning of the time step
690 if(lfn00>0. .or. t00>0.)t00=0.
692 if(lfn01>0. .or. t01>0.)t01=0.
694 if(lfn10>0. .or. t10>0.)t10=0.
696 if(lfn11>0. .or. t11>0.)t11=0.
698 ! approximate burning area, between 0 and 1
699 ps = lfn00+lfn01+lfn10+lfn11
700 aps = abs(lfn00)+abs(lfn01)+abs(lfn10)+abs(lfn11)
702 area =(-ps/aps+1.)/2.
703 area = max(area,0.) ! make sure area is between 0 and 1
706 ! average negative time since ignition
707 ta=0.25*(t00+t01+t10+t11)
709 ! exp decay in the burning area
711 !if(area>0.)out=1. - area*(1. - exp(ta/fuel_time_cell))
712 if(area>0)out=area*exp(ta/fuel_time_cell) + (1. - area)
715 !$OMP CRITICAL(FIRE_CORE_CRIT)
716 write(msg,*)'out=',out,'>1 area=',area,' ta=',ta
718 write(msg,*)'tign=',tign00,tign01,tign10,tign11,' time_now=',time_now
719 !$OMP END CRITICAL(FIRE_CORE_CRIT)
721 !call message('WARNING: fuel_left_cell_1: fuel fraction > 1')
722 call crash('fuel_left_cell_1: fuel fraction > 1')
725 !out = max(out,0.) ! make sure out is between 0 and 1
729 fire_frac_area = area
731 end subroutine fuel_left_cell_1
734 !****************************************
737 real function fuel_left_cell_2( &
738 lfn00,lfn01,lfn10,lfn11, &
739 tign00,tign01,tign10,tign11,&
740 time_now, fuel_time_cell)
741 !*** purpose: compute the fuel fraction left in one cell
744 real, intent(in)::lfn00,lfn01,lfn10,lfn11 ! level set function at 4 corners of the cell
745 real, intent(in)::tign00,tign01,tign10,tign11! ignition time at the 4 corners of the cell
746 real, intent(in)::time_now ! the time now
747 real, intent(in)::fuel_time_cell ! time to burns off to 1/e
749 ! The area burning is given by the condition L <= 0, where the function P is
750 ! interpolated from lfn(i,j)
752 ! The time since ignition is the function T, interpolated in from tign(i,j)-time_now.
753 ! The values of tign(i,j) where lfn(i,j)>=0 are ignored, tign(i,j)=0 is taken
756 ! The function computes an approxmation of the integral
761 ! fuel_frac_left = 1 - | 1 - exp(-T(x,y)/fuel_time_cell)) dxdy
768 ! When the cell is not burning at all (all lfn>=0), then fuel_frac(i,j)=1.
769 ! Because of symmetries, the result should not depend on the mesh spacing dx dy
770 ! so dx=1 and dy=1 assumed.
775 ! (0,1) -----O--(1,1) O = points on the fireline, T=tnow
776 ! | \ | A = the burning area for computing
777 ! | \| fuel_frac(i,j)
781 ! (0,0)---------(1,0)
784 ! Approximations allowed:
785 ! The fireline can be approximated by straight line(s).
786 ! When all cell is burning, approximation by 1 point Gaussian quadrature is OK.
789 ! 1. The output should be a continuous function of the arrays lfn and
790 ! tign whenever lfn(i,j)=0 implies tign(i,j)=tnow.
791 ! 2. The output should be invariant to the symmetries of the input in each cell.
792 ! 3. Arbitrary combinations of the signs of lfn(i,j) should work.
793 ! 4. The result should be at least 1st order accurate in the sense that it is
794 ! exact if the time from ignition is a linear function.
796 ! If time from ignition is approximated by polynomial in the burnt
797 ! region of the cell, this is integral of polynomial times exponential
798 ! over a polygon, which can be computed exactly.
800 ! Requirement 4 is particularly important when there is a significant decrease
801 ! of the fuel fraction behind the fireline on the mesh scale, because the
802 ! rate of fuel decrease right behind the fireline is much larger
803 ! (exponential...). This will happen when
805 ! change of time from ignition within one mesh cell * fuel speed is not << 1
807 ! This is the same as
809 ! mesh cell size*fuel_speed
810 ! ------------------------- is not << 1
814 ! When X is large then the fuel burnt in one timestep in the cell is
815 ! approximately proportional to length of fireline in that cell.
817 ! When X is small then the fuel burnt in one timestep in the cell is
818 ! approximately proportional to the area of the burning region.
822 call crash('fuel_left_cell_2: not implemented, please use fire_fuel_left_method=1')
823 fuel_left_cell_2=0. ! to avoid compiler warning about value not set
824 end function fuel_left_cell_2
831 real::ps,aps,area,ta,out
832 real::t00,t01,t10,t11
833 real,parameter::safe=tiny(aps)
834 character(len=128)::msg
840 integer::mmax,nb,nmax,pmax,nin,nout
841 parameter(mmax=3,nb=64,nmax=8,pmax=8)
842 integer lda, ldb, lwork, info
843 parameter (lda=nmax, ldb=nmax, lwork=mmax+nmax+nb*(nmax+pmax))
845 real,dimension(lda,mmax):: mA
846 real,dimension(nmax):: vecD
847 real,dimension(lwork):: WORK
848 real,dimension(pmax):: vecY
849 real,dimension(mmax):: vecX
850 real,dimension(ldb,pmax)::mB
851 real,dimension(mmax)::u
856 real,dimension(8,2)::xylist,xytlist
857 real,dimension(8)::tlist,llist,xt
858 real,dimension(5)::xx,yy
859 real,dimension(5)::lfn,tign
862 real::tt,x0,y0,xts,xte,yts,yte,xt1,xt2
863 real::lfn0,lfn1,dist,nr,c,s,errQ,ae,ce,ceae,a0,a1,a2,d,cet
865 real::upper,lower,ah,ch,aa,cc,aupp,cupp,alow,clow
866 real,dimension(2,2)::mQ
867 real,dimension(2)::ut
872 real, parameter:: zero=0.,one=1.,eps=epsilon(zero)
877 double precision :: dnrm2
880 ! external subroutines
883 !executable statements
885 ! a very crude approximation - replace by a better code
886 ! call check_mesh_2dim(ids,ide+1,jds,jde+1,ims,ime,jms,jme)
888 if(lfn00>=0. .or. t00>0.)t00=0.
890 if(lfn01>=0. .or. t01>0.)t01=0.
892 if(lfn10>=0. .or. t10>0.)t10=0.
894 if(lfn11>=0. .or. t11>0.)t11=0.
896 !if (t00<0 .or. t01 <0 .or. t10<0 .or. t11<0) then
897 ! print *,'tign00=',tign00,'tign10=',tign10,&
898 ! 'tign01=',tign01,'tign11=',tign11
903 !*** case0 Do nothing
904 if ( lfn00>=0 .and. lfn10>=0 .and. lfn01>=0 .and. lfn11>=0 ) then
905 out = 1.0 ! fuel_left, no burning
906 !*** case4 all four coners are burning
907 else if (lfn00<=0 .and. lfn10<=0 .and. lfn01<=0 .and. lfn11<=0) then
908 ! least squares, A matrix for points
921 ! D vector, time from ignition
922 vecD(1)=time_now-tign00
923 vecD(2)=time_now-tign10
924 vecD(3)=time_now-tign01
925 vecD(4)=time_now-tign11
934 n=4 ! rows of matrix A and B
935 m=3 ! columns of matrix A
936 p=4 ! columns of matrix B
937 ! call least squqres in LAPACK
938 call SGGGLM(N,M,P,mA,LDA,mB,LDB,vecD,vecX,vecY, &
940 rnorm=snrm2(p,vecY,1)
942 u(1)=-vecX(1)/fuel_time_cell
943 u(2)=-vecX(2)/fuel_time_cell
944 u(3)=-vecX(3)/fuel_time_cell
945 !fuel_burn(i,j)=1-exp(u(3))*intexp(u(1)*dx)*intexp(u(2)*dy)
948 out=1-exp(u(3))*intexp(s1)*intexp(s2)
950 if ( out<0 .or. out>1.0 ) then
951 print *,'case4, out should be between 0 and 1'
955 ! set xx, yy for the coner points
956 ! move these values out of i and j loop to speed up
977 npoint = 0 ! number of points in polygon
978 !print *,'time_now=',time_now
979 !print *,'lfn00=',lfn00,'lfn10=',lfn10,&
980 ! 'lfn01=',lfn01,'lfn11=',lfn11
981 !print *,'t00=',t00,'t10=',t10,&
982 ! 't01=',t01,'t11=',t11
987 if ( lfn0 <= 0.0 ) then
989 xylist(npoint,1)=xx(k)
990 xylist(npoint,2)=yy(k)
991 tlist(npoint)=-tign(k)
994 if ( lfn0*lfn1 < 0 ) then
997 x0=xx(k)+( xx(k+1)-xx(k) )*tt
998 y0=yy(k)+( yy(k+1)-yy(k) )*tt
1001 tlist(npoint)=0 ! on fireline
1006 ! make the list circular
1007 tlist(npoint+1)=tlist(1)
1008 llist(npoint+1)=llist(1)
1009 xylist(npoint+1,1)=xylist(1,1)
1010 xylist(npoint+1,2)=xylist(1,2)
1011 !* least squares, A matrix for points
1013 mA(kk,1)=xylist(kk,1)
1014 mA(kk,2)=xylist(kk,2)
1016 vecD(kk)=tlist(kk) ! D vector,time from ignition
1021 mB(kk,ll)=0.0 ! clear
1026 mb(kk,kk) = 10 ! large enough
1028 if ( kk .ne. ll ) then
1029 dist = sqrt( (xylist(kk,1)-xylist(ll,1))**2+ &
1030 (xylist(kk,2)-xylist(ll,2))**2 )
1031 mB(kk,kk)=min( mB(kk,kk) , dist )
1034 mB(kk,kk)=mB(kk,kk)+1.
1037 n=npoint ! rows of matrix A and B
1038 m=3 ! columns of matrix A
1039 p=npoint ! columns of matrix B
1040 !* call least squqres in LAPACK
1041 call SGGGLM(N,M,P,mA,LDA,mB,LDB,vecD,vecX,vecY, &
1043 !print *,'after LS in case3'
1044 !print *,'vecX from LS',vecX
1045 !print *,'tign inputed',tign00,tign10,tign11,tign01
1046 rnorm=snrm2(p,vecY,1)
1050 ! rotate to gradient on x only
1051 nr = sqrt(u(1)**2+u(2)**2)
1052 if(.not.nr.gt.eps)then
1062 ! mat vec multiplication
1063 call matvec(mQ,2,2,u,3,ut,2,2,2)
1064 errQ = ut(2) ! should be zero
1065 ae = -ut(1)/fuel_time_cell
1066 ce = -u(3)/fuel_time_cell
1068 call matmatp(xylist,8,2,mQ,2,2,xytlist,8,2,npoint+1,2,2)
1069 call sortxt( xytlist, 8,2, xt,8,npoint )
1082 if ( xt2-xt1 > eps*100 ) then
1090 if ( (xts>xt1 .and. xte>xt1) .or. &
1091 (xts<xt2 .and. xte<xt2) ) then
1095 aa = (yts-yte)/(xts-xte)
1096 cc = (xts*yte-xte*yts)/(xts-xte)
1108 end if!(xts>xt1 .and. xte>xt1)
1110 ce=cet !use stored ce
1111 if (ae*xt1+ce > 0 ) then
1112 ce=ce-(ae*xt1+ce)!shift small amounts exp(-**)
1114 if (ae*xt2+ce > 0) then
1120 ! integrate (ah*x+ch)*(1-exp(ae*x+ce) from xt1 to xt2
1121 ! numerically sound for ae->0, ae -> infty
1122 ! this can be important for different model scales
1123 ! esp. if someone runs the model in single precision!!
1124 ! s1=int((ah*x+ch),x,xt1,xt2)
1125 s1 = (xt2-xt1)*((1./2.)*ah*(xt2+xt1)+ch)
1126 ! s2=int((ch)*(-exp(ae*x+ce)),x,xt1,xt2)
1128 s2 = -ch*exp(ae*(xt1+ceae))*(xt2-xt1)*intexp(ae*(xt2-xt1))
1129 ! s3=int((ah*x)*(-exp(ae*x+ce)),x,xt1,xt2)
1130 ! s3=int((ah*x)*(-exp(ae*(x+ceae))),x,xt1,xt2)
1131 ! expand in Taylor series around ae=0
1132 ! collect(expand(taylor(int(x*(-exp(ae*(x+ceae))),x,xt1,xt2)*ae^2,ae,4)/ae^2),ae)
1133 ! =(1/8*xt1^4+1/3*xt1^3*ceae+1/4*xt1^2*ceae^2-1/8*xt2^4-1/3*xt2^3*ceae-1/4*xt2^2*ceae^2)*ae^2
1134 ! + (-1/3*xt2^3-1/2*xt2^2*ceae+1/3*xt1^3+1/2*xt1^2*ceae)*ae
1135 ! + 1/2*xt1^2-1/2*xt2^2
1137 ! coefficient at ae^2 in the expansion, after some algebra
1138 a2=(xt1-xt2)*((1./4.)*(xt1+xt2)*ceae**2+(1./3.)* &
1139 (xt1**2+xt1*xt2+xt2**2)*ceae+(1./8.)* &
1140 (xt1**3+xt1*(xt2**2)+xt1**2*xt2+xt2**3))
1143 if (abs(d)>eps) then
1144 ! since ae*xt1+ce<=0 ae*xt2+ce<=0 all fine for large ae
1145 ! for ae, ce -> 0 rounding error approx eps/ae^2
1146 s3=( exp(ae*(xt1+ceae))*(ae*xt1-1)-&
1147 exp(ae*(xt2+ceae))*(ae*xt2-1) )/(ae**2)
1149 !we do not worry about rounding as xt1 -> xt2, then s3 -> 0
1151 ! coefficient at ae^1 in the expansion
1152 a1=(xt1-xt2)*((1./2.)*ceae*(xt1+xt2)+(1./3.)*&
1153 (xt1**2+xt1*xt2+xt2**2))
1154 ! coefficient at ae^0 in the expansion for ae->0
1155 a0=(1./2.)*(xt1-xt2)*(xt1+xt2)
1156 s3=a0+a1*ae+a2*ae**2; ! approximate the integral
1161 out=1-out !fuel_left
1162 if(out<0 .or. out>1) then
1163 print *,':fuel_fraction should be between 0 and 1'
1164 !print *, 'eps= ', eps
1165 !print *, 'xt1= ', xt1, 'xt2= ', xt2
1166 !print *,'ae= ',ae,'ce= ',ce,'ah= ',ah,'ch= ',ch
1167 !print *,'a0= ', a0,'a1= ', a1,'a2= ', a2
1168 print *,'s1= ', s1,'s2= ', s2,'s3= ', s3
1169 print *,':fuel_fraction =',out
1175 end if ! if case0, elseif case4 ,else case123
1178 if(out>1. .or. out<0.)call crash('fuel_left_cell_2: fuel fraction out of bounds [0,1]')
1179 fuel_left_cell_2 = out
1180 end function fuel_left_cell_2
1183 !****************************************
1185 real function intexp(ab)
1191 real, parameter:: zero=0.,one=1.,eps=epsilon(zero)
1193 !eps = 2.2204*(10.0**(-8))!from matlab
1194 if ( eps < abs(ab)**3/6. ) then
1195 intexp=(exp(ab)-1)/ab
1201 !****************************************
1203 subroutine sortxt(xytlist,nrow,ncolumn,xt,nxt,nvec)
1205 integer::nrow,ncolumn,nxt,nvec
1206 real,dimension(nrow,ncolumn)::xytlist
1207 real,dimension(nxt)::xt
1218 if ( xt(i) > xt(j) ) then
1226 end subroutine !sortxt
1228 !****************************************
1230 subroutine matvec(A,m,n,V,nv,out,nout,nrow,ncolumn)
1232 integer::m,n,nv,nout,nrow,ncolumn
1233 real,dimension(m,n)::A ! allocated m by n
1234 real,dimension(nv)::V ! allocated nv
1235 real,dimension(nout)::out! allocated nout
1242 out(i)=out(i)+A(i,j)*V(j)
1247 !****************************************
1249 subroutine matmatp(A,mA,nA,B,mB,nB,C,mC,nC,nrow,ncolumn,nP)
1251 integer::mA,nA,mB,nB,mC,nC,nrow,ncolumn,nP
1252 real,dimension(mA,nA)::A ! allocated m by n
1253 real,dimension(mB,nB)::B ! allocated m by n
1254 real,dimension(mC,nC)::C ! allocated m by n
1260 C(i,j)=C(i,j)+A(i,k)*B(j,k) ! B'
1267 !****************************************
1271 subroutine prop_ls_rk3(id, &
1272 ifds,ifde,jfds,jfde, &
1273 ifms,ifme,jfms,jfme, &
1274 ifps,ifpe,jfps,jfpe, &
1275 ifts,ifte,jfts,jfte, &
1279 lfn_0,lfn_1,lfn_2, &
1283 ids,ide,jds,jde,kds,kde, &
1284 ims,ime,jms,jme,kms,kme, &
1285 ips,ipe,jps,jpe,kps,kpe &
1290 USE module_dm , ONLY : ntasks_x,ntasks_y,local_communicator,mytask,ntasks
1291 USE module_comm_dm , ONLY : halo_fire_lfn_1_sub, halo_fire_lfn_2_sub, halo_fire_lfn_0_sub
1293 USE module_domain , only: domain
1297 !*** purpose: advance level function in time
1299 !***************************************************************************************!
1300 !*** Level-set method recoded for true mpi communications and added ***!
1301 !*** 3rd-order Runge-Kutta time integration and 3rd/5th-order WENO advection schemes ***!
1302 !*** Implemented by: Domingo Munoz-Esparza (NCAR/RAL, April 2016) ***!
1303 !*** Reference: D. Munoz-Esparza, B. Kosovic, P. Jimenez, J. Coen: "An accurate ***!
1304 !*** fire-spread algorithm in the Weather Research and Forecasting model using the ***!
1305 !*** level-set method", Journal of Advances in Modeling Earth Systems, 2018 ***!
1306 !*** https://doi.org/10.1002/2017MS001108 ***!
1307 !***************************************************************************************!
1311 ! Propagation of closed curve by a level function method. The level function
1312 ! lfn is defined by its values at the nodes of a rectangular grid.
1313 ! The area where lfn < 0 is inside the curve. The curve is
1314 ! described implicitly by lfn=0. Points where the curve intersects gridlines
1315 ! can be found by linear interpolation from nodes.
1317 ! The level function is advanced from time ts to time ts + dt.
1319 ! The level function should be initialized to (an approximation of) the signed
1320 ! distance from the curve. If the initial curve is a circle, the initial level
1321 ! function is simply the distance from the center minus the radius.
1323 ! The curve moves outside with speed given by function speed_func.
1325 ! Method: Godunov/ENO method for the normal motion. The timestep is checked for
1326 ! CFL condition. For a straight segment in a constant field and locally linear
1327 ! level function, the method reduces to the exact normal motion. The advantage of
1328 ! the level set method is that it treats automatically special cases such as
1329 ! the curve approaching itself and merging components of the area inside the curve.
1331 ! Based on S. Osher and R. Fedkiw, Level set methods and dynamic implicit surfaces,
1332 ! Springer, 2003, Sec. 6.4, as implemented in toolboxLS for Matlab by
1333 ! I. Mitchell, A toolbox of Level Set Methods (Version 1.1), TR-2007-11,
1334 ! Dept. Computer Science, University of British Columbia, 2007
1335 ! http://www.cs.ubc.ca/\~mitchell/Toolbo\LS
1340 ! id in unique identification for prints and dumps
1341 ! ids,ide,jds,jde in domain dimensions
1342 ! ims,ime,jms,jme in memory dimensions
1343 ! its,ite,jts,jte in tile dimensions
1346 ! dx,dy in grid spacing
1347 ! tbound out bound on stable time step from CFL condition, if tbound>=dt then OK
1348 ! lfn_in,lfn_out inout,out the level set function at nodes
1349 ! tign inout the ignition time at nodes
1353 ! DME added for halo update
1354 type(domain) , target :: grid
1355 integer, intent(in):: ids,ide,jds,jde,kds,kde, &
1356 ims,ime,jms,jme,kms,kme, &
1357 ips,ipe,jps,jpe,kps,kpe
1360 integer,intent(in)::id,ifms,ifme,jfms,jfme,ifds,ifde,jfds,jfde,ifts,ifte,jfts,jfte,ifps,ifpe,jfps,jfpe
1361 real,dimension(ifms:ifme,jfms:jfme),intent(inout)::lfn_in,tign
1362 real,dimension(ifms:ifme,jfms:jfme),intent(inout)::lfn_1,lfn_2,lfn_0
1363 real,dimension(ifms:ifme,jfms:jfme),intent(out)::lfn_out,ros
1364 real,intent(in)::dx,dy,ts,dt
1365 real,intent(out)::tbound
1366 type(fire_params),intent(in)::fp
1368 real,dimension(ifms:ifme,jfms:jfme):: tend
1370 real::grad2,rr,tbound2,tbound3
1372 real::gradx,grady,aspeed,err,aerr,time_now
1373 integer::i,j,its1,ite1,jts1,jte1,k,kk,id1
1374 character(len=128)::msg
1375 integer::nfirenodes,nfireline
1376 real::sum_err,min_err,max_err,sum_aerr,min_aerr,max_aerr
1379 integer,parameter :: mstep=1000, printl=1
1380 real, parameter:: zero=0.,one=1.,eps=epsilon(zero),tol=100*eps, &
1381 safe=2.,rmin=safe*tiny(zero),rmax=huge(zero)/safe
1383 ! f90 intrinsic function
1385 intrinsic max,min,sqrt,nint,epsilon,tiny,huge
1389 !$OMP CRITICAL(FIRE_CORE_CRIT)
1390 write(msg,'(a8,i5,a6,i5,3(a1,i5))')'prop_ls:',id,' tile ',ifts,':',ifte,',',jfts,':',jfte
1391 !$OMP END CRITICAL(FIRE_CORE_CRIT)
1394 !!!!!!!!!!!!!!!!!!!!!!
1395 ! Runge-Kutta step 1 !
1396 !!!!!!!!!!!!!!!!!!!!!!
1400 lfn_0(i,j) = lfn_in(i,j)
1405 # include "HALO_FIRE_LFN_0.inc"
1408 id1 = id ! for debug prints
1409 if(id1.ne.0)id1=id1+1000
1411 ifds,ifde,jfds,jfde, & ! domain dims
1412 ifps,ifpe,jfps,jfpe, & ! patch dims
1413 ifts,ifte,jfts,jfte, & ! tile dims
1414 ifms,ifme,jfms,jfme, & ! memory dims
1415 ts,dt,dx,dy, & ! scalars in
1416 lfn_0, & ! arrays in
1417 tbound, & ! scalars out
1418 tend, ros, & ! arrays out
1424 lfn_1(i,j) = lfn_0(i,j) + (dt/3.0)*tend(i,j)
1429 # include "HALO_FIRE_LFN_1.inc"
1432 !!!!!!!!!!!!!!!!!!!!!!
1433 ! Runge-Kutta step 2 !
1434 !!!!!!!!!!!!!!!!!!!!!!
1436 if(id1.ne.0)id1=id1+1000
1438 ifds,ifde,jfds,jfde, &
1439 ifps,ifpe,jfps,jfpe, &
1440 ifts,ifte,jfts,jfte, &
1441 ifms,ifme,jfms,jfme, &
1451 lfn_2(i,j) = lfn_0(i,j) + (dt/2.0)*tend(i,j)
1456 # include "HALO_FIRE_LFN_2.inc"
1459 !!!!!!!!!!!!!!!!!!!!!!
1460 ! Runge-Kutta step 3 !
1461 !!!!!!!!!!!!!!!!!!!!!!
1463 if(id1.ne.0)id1=id1+1000
1465 ifds,ifde,jfds,jfde, &
1466 ifps,ifpe,jfps,jfpe, &
1467 ifts,ifte,jfts,jfte, &
1468 ifms,ifme,jfms,jfme, &
1478 lfn_out(i,j) = lfn_0(i,j) + dt*tend(i,j)
1479 lfn_2(i,j) = lfn_out(i,j) ! lfn_2=lfn_out (needed for reinitialization purposes)
1484 # include "HALO_FIRE_LFN_2.inc"
1489 tbound=min(tbound,tbound2,tbound3)
1491 !$OMP CRITICAL(FIRE_CORE_CRIT)
1492 write(msg,'(a,f10.2,4(a,f7.2))')'prop_ls: time',ts,' dt=',dt,' bound',min(tbound,999.99), &
1494 !$OMP END CRITICAL(FIRE_CORE_CRIT)
1497 !$OMP CRITICAL(FIRE_CORE_CRIT)
1498 write(msg,'(2(a,f10.2))')'prop_ls: WARNING: time step ',dt, &
1500 !$OMP END CRITICAL(FIRE_CORE_CRIT)
1504 end subroutine prop_ls_rk3
1507 !*****************************
1510 subroutine reinit_ls_rk3(id, &
1511 ifts,ifte,jfts,jfte, &
1512 ifms,ifme,jfms,jfme, &
1513 ifds,ifde,jfds,jfde, &
1514 ifps,ifpe,jfps,jfpe, &
1517 lfn_2,lfn_s0,lfn_s1,lfn_s2,lfn_s3, &
1520 ids,ide,jds,jde,kds,kde, &
1521 ims,ime,jms,jme,kms,kme, &
1522 ips,ipe,jps,jpe,kps,kpe &
1527 USE module_dm , ONLY : ntasks_x,ntasks_y,local_communicator,mytask,ntasks
1528 USE module_comm_dm , ONLY : halo_fire_lfn_s1_sub,halo_fire_lfn_s2_sub,halo_fire_lfn_s3_sub
1530 USE module_domain , only: domain
1531 USE module_configure, only: grid_config_rec_type
1535 !*************************************************************************************!
1536 !*** Level-set function reinitialization following: ***!
1537 !*** Sussman, Smereka, Osher. Journal of Computational Physics 114, 146-159 (1994) ***!
1538 !*** implemented by Domingo Munoz-Esparza (NCAR/RAL, April 2016) ***!
1539 !*** Reference: D. Munoz-Esparza, B. Kosovic, P. Jimenez, J. Coen: "An accurate ***!
1540 !*** fire-spread algorithm in the Weather Research and Forecasting model using the ***!
1541 !*** level-set method", Journal of Advances in Modeling Earth Systems, 2018 ***!
1542 !*** https://doi.org/10.1002/2017MS001108 ***!
1543 !*************************************************************************************!
1545 !*** purpose: reinitialize the level-set function
1547 type(domain) , target :: grid
1548 integer, intent(in):: ids,ide,jds,jde,kds,kde, &
1549 ims,ime,jms,jme,kms,kme, &
1550 ips,ipe,jps,jpe,kps,kpe
1552 integer,intent(in)::ifts,ifte,jfts,jfte,ifms,ifme,jfms,jfme,id
1553 integer,intent(in)::ifds,ifde,jfds,jfde,ifps,ifpe,jfps,jfpe
1554 real,dimension(ifms:ifme,jfms:jfme),intent(inout)::lfn_in,tign
1555 real,dimension(ifms:ifme,jfms:jfme),intent(inout)::lfn_2,lfn_s0,lfn_s1,lfn_s2,lfn_s3
1556 real,dimension(ifms:ifme,jfms:jfme),intent(inout)::lfn_out
1557 real,intent(in)::dx,dy,ts,dt
1559 real,dimension(ifts:ifte,jfts:jfte):: tend_1,tend_2,tend_3
1560 real::diffLx,diffLy,diffRx,diffRy,diff2x,diff2y,grad,time_now
1561 integer::nts,i,j,k,kk
1563 character(len=128)::msg
1564 integer::itso,iteo,jtso,jteo
1565 real::threshold_HLl,threshold_HLu
1566 integer,parameter::bdy_eno1=10
1569 threshold_HLl=-fire_lsm_band_ngp*dx
1570 threshold_HLu=fire_lsm_band_ngp*dx
1572 ! Define S0 based on current lfn values
1576 lfn_s0(i,j) = lfn_2(i,j)/sqrt(lfn_2(i,j)**2.0+dx**2.0)
1577 lfn_s3(i,j) = lfn_2(i,j)
1581 # include "HALO_FIRE_LFN_S3.inc"
1584 call continue_at_boundary(1,1,fire_lfn_ext_up, & !extend by extrapolation but never down
1585 ifms,ifme,jfms,jfme, & ! memory dims
1586 ifds,ifde,jfds,jfde, & ! domain
1587 ifps,ifpe,jfps,jfpe, & ! patch
1588 ifts,ifte,jfts,jfte, & ! tile
1589 itso,iteo,jtso,jteo, & ! where set now
1594 do nts=1,fire_lsm_reinit_iter ! iterate to solve to steady state (1 iter each time step is enoguh)
1595 ! Runge-Kutta step 1 !
1597 call advance_ls_reinit( &
1598 ifms,ifme,jfms,jfme, &
1599 ifds,ifde,jfds,jfde, &
1600 ifts,ifte,jfts,jfte, &
1601 dx,dy,dt_s,bdy_eno1,threshold_HLu, &
1602 lfn_s0,lfn_s3,lfn_s3,lfn_s1,1.0/3.0) ! sign funcition, initial ls, current stage ls, next stage advanced ls, RK coefficient
1605 # include "HALO_FIRE_LFN_S1.inc"
1608 call continue_at_boundary(1,1,fire_lfn_ext_up, &
1609 ifms,ifme,jfms,jfme, &
1610 ifds,ifde,jfds,jfde, &
1611 ifps,ifpe,jfps,jfpe, &
1612 ifts,ifte,jfts,jfte, &
1613 itso,iteo,jtso,jteo, &
1616 ! Runge-Kutta step 2 !
1618 call advance_ls_reinit( &
1619 ifms,ifme,jfms,jfme, &
1620 ifds,ifde,jfds,jfde, &
1621 ifts,ifte,jfts,jfte, &
1622 dx,dy,dt_s,bdy_eno1,threshold_HLu, &
1623 lfn_s0,lfn_s3,lfn_s1,lfn_s2,1.0/2.0)
1626 # include "HALO_FIRE_LFN_S2.inc"
1629 call continue_at_boundary(1,1,fire_lfn_ext_up, &
1630 ifms,ifme,jfms,jfme, &
1631 ifds,ifde,jfds,jfde, &
1632 ifps,ifpe,jfps,jfpe, &
1633 ifts,ifte,jfts,jfte, &
1634 itso,iteo,jtso,jteo, &
1637 ! Runge-Kutta step 3 !
1639 call advance_ls_reinit( &
1640 ifms,ifme,jfms,jfme, &
1641 ifds,ifde,jfds,jfde, &
1642 ifts,ifte,jfts,jfte, &
1643 dx,dy,dt_s,bdy_eno1,threshold_HLu, &
1644 lfn_s0,lfn_s3,lfn_s2,lfn_s3,1.0)
1647 # include "HALO_FIRE_LFN_S3.inc"
1650 call continue_at_boundary(1,1,fire_lfn_ext_up, &
1651 ifms,ifme,jfms,jfme, &
1652 ifds,ifde,jfds,jfde, &
1653 ifps,ifpe,jfps,jfpe, &
1654 ifts,ifte,jfts,jfte, &
1655 itso,iteo,jtso,jteo, &
1658 enddo ! end iterations for steady state solution of reinitialization PDE
1662 lfn_out(i,j)=lfn_s3(i,j) ! assing to lfn_out the reinitialized level-set function
1663 lfn_out(i,j)=min(lfn_out(i,j),lfn_in(i,j)) ! fire area can only increase
1667 end subroutine reinit_ls_rk3
1670 !*****************************
1673 subroutine advance_ls_reinit(ifms,ifme,jfms,jfme, &
1674 ifds,ifde,jfds,jfde, &
1675 ifts,ifte,jfts,jfte, &
1676 dx,dy,dt_s,bdy_eno1,threshold_HLu, &
1677 lfn_s0,lfn_ini,lfn_curr,lfn_fin,rk_coeff)
1679 ! Calculates right-hand-side forcing and advances a RK-stage the level-set reinitialization PDE
1680 ! Domingo Munoz-Esparza, NCAR/RAL
1681 ! Reference: D. Munoz-Esparza, B. Kosovic, P. Jimenez, J. Coen: "An accurate
1682 ! fire-spread algorithm in the Weather Research and Forecasting model using the
1683 ! level-set method", Journal of Advances in Modeling Earth Systems, 2018
1684 ! https://doi.org/10.1002/2017MS001108
1689 integer,intent(in)::ifms,ifme,jfms,jfme,ifts,ifte,jfts,jfte,ifds,ifde,jfds,jfde,bdy_eno1
1690 real,dimension(ifms:ifme,jfms:jfme),intent(in)::lfn_s0,lfn_ini,lfn_curr
1691 real,dimension(ifms:ifme,jfms:jfme),intent(inout)::lfn_fin
1692 real,intent(in)::dx,dy,dt_s,threshold_HLu,rk_coeff
1695 real::diffLx,diffLy,diffRx,diffRy,diff2x,diff2y,grad,tend_r
1700 if (i.lt.ifds+bdy_eno1 .OR. i.gt.ifde-bdy_eno1 .OR. j.lt.jfds+bdy_eno1 .OR. j.gt.jfde-bdy_eno1) then !
1701 diffLx=(lfn_curr(i,j)-lfn_curr(i-1,j))/dx
1702 diffLy=(lfn_curr(i,j)-lfn_curr(i,j-1))/dy
1703 diffRx=(lfn_curr(i+1,j)-lfn_curr(i,j))/dx
1704 diffRy=(lfn_curr(i,j+1)-lfn_curr(i,j))/dy
1705 diff2x=select_eno(diffLx,diffRx)
1706 diff2y=select_eno(diffLy,diffRy)
1708 select case(fire_upwinding_reinit)
1710 diff2x=select_4th(dx,lfn_curr(i,j),lfn_curr(i-1,j),lfn_curr(i-2,j),lfn_curr(i+1,j),lfn_curr(i+2,j))
1711 diff2y=select_4th(dy,lfn_curr(i,j),lfn_curr(i,j-1),lfn_curr(i,j-2),lfn_curr(i,j+1),lfn_curr(i,j+2))
1712 diff2x=select_weno3(dx,lfn_curr(i,j),lfn_curr(i-1,j),lfn_curr(i-2,j),lfn_curr(i+1,j),lfn_curr(i+2,j),lfn_s0(i,j)*diff2x)
1713 diff2y=select_weno3(dy,lfn_curr(i,j),lfn_curr(i,j-1),lfn_curr(i,j-2),lfn_curr(i,j+1),lfn_curr(i,j+2),lfn_s0(i,j)*diff2y)
1715 diff2x=select_4th(dx,lfn_curr(i,j),lfn_curr(i-1,j),lfn_curr(i-2,j),lfn_curr(i+1,j),lfn_curr(i+2,j))
1716 diff2y=select_4th(dy,lfn_curr(i,j),lfn_curr(i,j-1),lfn_curr(i,j-2),lfn_curr(i,j+1),lfn_curr(i,j+2))
1717 diff2x=select_weno5(dx,lfn_curr(i,j),lfn_curr(i-1,j),lfn_curr(i-2,j),lfn_curr(i-3,j),lfn_curr(i+1,j),lfn_curr(i+2,j),lfn_curr(i+3,j),lfn_s0(i,j)*diff2x)
1718 diff2y=select_weno5(dy,lfn_curr(i,j),lfn_curr(i,j-1),lfn_curr(i,j-2),lfn_curr(i,j-3),lfn_curr(i,j+1),lfn_curr(i,j+2),lfn_curr(i,j+3),lfn_s0(i,j)*diff2y)
1720 if (lfn_curr(i,j).lt.threshold_HLu) then
1721 diff2x=select_4th(dx,lfn_curr(i,j),lfn_curr(i-1,j),lfn_curr(i-2,j),lfn_curr(i+1,j),lfn_curr(i+2,j))
1722 diff2y=select_4th(dy,lfn_curr(i,j),lfn_curr(i,j-1),lfn_curr(i,j-2),lfn_curr(i,j+1),lfn_curr(i,j+2))
1723 diff2x=select_weno3(dx,lfn_curr(i,j),lfn_curr(i-1,j),lfn_curr(i-2,j),lfn_curr(i+1,j),lfn_curr(i+2,j),lfn_s0(i,j)*diff2x)
1724 diff2y=select_weno3(dy,lfn_curr(i,j),lfn_curr(i,j-1),lfn_curr(i,j-2),lfn_curr(i,j+1),lfn_curr(i,j+2),lfn_s0(i,j)*diff2y)
1726 diffLx=(lfn_curr(i,j)-lfn_curr(i-1,j))/dx
1727 diffLy=(lfn_curr(i,j)-lfn_curr(i,j-1))/dy
1728 diffRx=(lfn_curr(i+1,j)-lfn_curr(i,j))/dx
1729 diffRy=(lfn_curr(i,j+1)-lfn_curr(i,j))/dy
1730 diff2x=select_eno(diffLx,diffRx)
1731 diff2y=select_eno(diffLy,diffRy)
1734 if (lfn_curr(i,j).lt.threshold_HLu) then
1735 diff2x=select_4th(dx,lfn_curr(i,j),lfn_curr(i-1,j),lfn_curr(i-2,j),lfn_curr(i+1,j),lfn_curr(i+2,j))
1736 diff2y=select_4th(dy,lfn_curr(i,j),lfn_curr(i,j-1),lfn_curr(i,j-2),lfn_curr(i,j+1),lfn_curr(i,j+2))
1737 diff2x=select_weno5(dx,lfn_curr(i,j),lfn_curr(i-1,j),lfn_curr(i-2,j),lfn_curr(i-3,j),lfn_curr(i+1,j),lfn_curr(i+2,j),lfn_curr(i+3,j),lfn_s0(i,j)*diff2x)
1738 diff2y=select_weno5(dy,lfn_curr(i,j),lfn_curr(i,j-1),lfn_curr(i,j-2),lfn_curr(i,j-3),lfn_curr(i,j+1),lfn_curr(i,j+2),lfn_curr(i,j+3),lfn_s0(i,j)*diff2y)
1740 diffLx=(lfn_curr(i,j)-lfn_curr(i-1,j))/dx
1741 diffLy=(lfn_curr(i,j)-lfn_curr(i,j-1))/dy
1742 diffRx=(lfn_curr(i+1,j)-lfn_curr(i,j))/dx
1743 diffRy=(lfn_curr(i,j+1)-lfn_curr(i,j))/dy
1744 diff2x=select_eno(diffLx,diffRx)
1745 diff2y=select_eno(diffLy,diffRy)
1748 if (lfn_curr(i,j).lt.threshold_HLu) then
1749 diff2x=select_4th(dx,lfn_curr(i,j),lfn_curr(i-1,j),lfn_curr(i-2,j),lfn_curr(i+1,j),lfn_curr(i+2,j))
1750 diff2y=select_4th(dy,lfn_curr(i,j),lfn_curr(i,j-1),lfn_curr(i,j-2),lfn_curr(i,j+1),lfn_curr(i,j+2))
1751 diff2x=select_weno5(dx,lfn_curr(i,j),lfn_curr(i-1,j),lfn_curr(i-2,j),lfn_curr(i-3,j),lfn_curr(i+1,j),lfn_curr(i+2,j),lfn_curr(i+3,j),lfn_s0(i,j)*diff2x)
1752 diff2y=select_weno5(dy,lfn_curr(i,j),lfn_curr(i,j-1),lfn_curr(i,j-2),lfn_curr(i,j-3),lfn_curr(i,j+1),lfn_curr(i,j+2),lfn_curr(i,j+3),lfn_s0(i,j)*diff2y)
1754 diffLx=(lfn_curr(i,j)-lfn_curr(i-1,j))/dx
1755 diffLy=(lfn_curr(i,j)-lfn_curr(i,j-1))/dy
1756 diffRx=(lfn_curr(i+1,j)-lfn_curr(i,j))/dx
1757 diffRy=(lfn_curr(i,j+1)-lfn_curr(i,j))/dy
1758 diff2x=select_eno(diffLx,diffRx)
1759 diff2y=select_eno(diffLy,diffRy)
1763 grad=sqrt(diff2x*diff2x+diff2y*diff2y)
1764 tend_r=lfn_s0(i,j)*(1.0-grad)
1765 lfn_fin(i,j)=lfn_ini(i,j)+(dt_s*rk_coeff)*tend_r
1769 end subroutine advance_ls_reinit
1772 !*****************************
1775 subroutine tign_update(ifts,ifte,jfts,jfte, & ! tile dims
1776 ifms,ifme,jfms,jfme, & ! memory dims
1777 ifds,jfds,ifde,jfde, & ! domain dims
1778 ts,dt, & ! scalars in
1779 lfn_in,lfn_out,tign & ! arrays inout
1784 integer,intent(in)::ifts,ifte,jfts,jfte,ifms,ifme,jfms,jfme,ifds,jfds,ifde,jfde
1785 real,dimension(ifms:ifme,jfms:jfme),intent(inout)::tign
1786 real,dimension(ifms:ifme,jfms:jfme),intent(in)::lfn_in,lfn_out
1787 real,intent(in)::ts,dt
1791 character(len=128)::msg
1794 ! compute ignition time by interpolation
1795 ! the node was not burning at start but it is burning at end
1796 ! interpolate from the level functions at start and at end
1797 ! lfn_in==lfn is the level set function value at time ts
1798 ! lfn_out is the level set function value at time ts+dt (after reinitialization)
1799 ! 0 is the level set function value at time tign(i,j)
1800 ! thus assuming the level function is approximately linear =>
1801 ! tign(i,j)= ts + ((ts + td) - ts) * lfn_in / (lfn_in - lfn_out)
1802 ! = ts + dt * lfn_in / (lfn_in - lfn_out)
1805 time_now = time_now + abs(time_now)*epsilon(time_now)*2.
1808 ! interpolate the cross-over time
1809 if (.not. lfn_out(i,j)>0 .and. lfn_in(i,j)>0)then
1810 tign(i,j) = ts + dt * lfn_in(i,j) / (lfn_in(i,j) - lfn_out(i,j))
1812 ! set the ignition time outside of burning region
1813 if(lfn_out(i,j)>0.)tign(i,j)=time_now
1816 ! stop simulation if fire is within boundary_guard grid points from the domain boundaries
1818 if (j.le.boundary_guard .or. j.gt.(jfde-boundary_guard)) then
1820 if (lfn_out(i,j).lt.0.) then
1821 WRITE(msg,*)'j-boundary reached'
1822 CALL wrf_debug(0,msg)
1823 WRITE(msg,*)'i,j,lfn_out=',i,j,lfn_out(i,j)
1824 CALL wrf_debug(0,msg)
1825 call crash('wrf: SUCCESS COMPLETE WRF. Fire has reached domain boundary.')
1831 if (i.le.boundary_guard .or. i.gt.(ifde-boundary_guard)) then
1833 if (lfn_out(i,j).lt.0.) then
1834 WRITE(msg,*)'i-boundary reached'
1835 CALL wrf_debug(0,msg)
1836 WRITE(msg,*)'i,j,lfn_out=',i,j,lfn_out(i,j)
1837 CALL wrf_debug(0,msg)
1838 call crash('wrf: SUCCESS COMPLETE WRF. Fire has reached domain boundary.')
1844 call print_2d_stats(ifts,ifte,jfts,jfte,ifms,ifme,jfms,jfme, &
1845 lfn_out,'prop_ls: lfn out')
1847 end subroutine tign_update
1850 !*****************************
1853 subroutine calc_flame_length(ifts,ifte,jfts,jfte, &
1854 ifms,ifme,jfms,jfme, &
1855 ros,iboros,flame_length,ros_fl,fire_area)
1857 ! Diagnoses flame length
1858 ! Domingo Munoz-Esparza, NCAR/RAL
1862 integer,intent(in)::ifts,ifte,jfts,jfte,ifms,ifme,jfms,jfme
1863 real,dimension(ifms:ifme,jfms:jfme),intent(in)::ros,iboros,fire_area
1864 real,dimension(ifms:ifme,jfms:jfme),intent(out)::flame_length,ros_fl
1870 if (fire_area(i,j).gt.0.0 .AND. fire_area(i,j).lt.1.0) then
1871 flame_length(i,j)=0.0775*(iboros(i,j)*ros(i,j))**0.46 ! flame length according to Byram (1959)
1872 ros_fl(i,j)=ros(i,j)
1874 flame_length(i,j)=0.0
1880 end subroutine calc_flame_length
1883 !*****************************
1886 subroutine tend_ls( id, &
1887 ids,ide,jds,jde, & ! domain - nodes where lfn defined
1888 ips,ipe,jps,jpe, & ! patch - nodes owned by this process
1889 its,ite,jts,jte, & ! region - nodes where tend computed
1890 ims,ime,jms,jme, & ! memory dims for ros
1891 t,dt,dx,dy, & ! scalars in
1893 tbound, & ! scalars out
1894 tend, ros, & ! arrays out
1900 ! compute the right hand side of the level set equation
1903 integer,intent(in)::id
1904 integer,intent(in)::ims,ime,jms,jme,its,ite,jts,jte
1905 integer, intent(in)::ids,ide,jds,jde,ips,ipe,jps,jpe
1906 real,intent(in)::t ! time
1907 real,intent(in)::dt,dx,dy ! mesh step
1908 real,dimension(ims:ime,jms:jme),intent(inout)::lfn ! level set function
1909 real,dimension(ims:ime,jms:jme),intent(out)::tend ! tendency (rhs of the level set pde)
1910 real,dimension(ims:ime,jms:jme),intent(out)::ros ! rate of spread
1911 real,intent(out)::tbound ! max allowed time step
1912 type(fire_params),intent(in)::fp
1915 real:: te,diffLx,diffLy,diffRx,diffRy, &
1916 diffCx,diffCy,diff2x,diff2y,grad,rr, &
1917 ros_base,ros_wind,ros_slope,ros_back,advx,advy,scale,nvx,nvy, &
1919 integer::i,j,itso,iteo,jtso,jteo
1920 character(len=128)msg
1923 real, parameter:: eps=epsilon(0.0)
1925 real, parameter:: zero=0.,one=1.,tol=100*eps, &
1926 safe=2.,rmin=safe*tiny(zero),rmax=huge(zero)/safe
1928 real :: diff2xn, diff2yn
1929 real :: a_valor, signo_x, signo_y
1930 real :: threshold_HLl,threshold_HLu
1931 real :: threshold_AV,fire_viscosity_var
1932 integer,parameter :: bdy_eno1=10
1934 ! f90 intrinsic function
1936 intrinsic max,min,sqrt,nint,tiny,huge
1941 threshold_HLl=-fire_lsm_band_ngp*dx
1942 threshold_HLu=fire_lsm_band_ngp*dx
1943 threshold_AV=fire_viscosity_ngp*dx
1945 call continue_at_boundary(1,1,fire_lfn_ext_up, & !extend by extrapolation but never down
1946 ims,ime,jms,jme, & ! memory dims
1947 ids,ide,jds,jde, & ! domain
1948 ips,ipe,jps,jpe, & ! patch
1949 its,ite,jts,jte, & ! tile
1950 itso,iteo,jtso,jteo, & ! where set now
1956 ! one sided differences
1957 diffRx = (lfn(i+1,j)-lfn(i,j))/dx
1958 diffLx = (lfn(i,j)-lfn(i-1,j))/dx
1959 diffRy = (lfn(i,j+1)-lfn(i,j))/dy
1960 diffLy = (lfn(i,j)-lfn(i,j-1))/dy
1961 diffCx = diffLx+diffRx ! TWICE CENTRAL DIFFERENCE
1962 diffCy = diffLy+diffRy
1964 if (i.lt.ids+bdy_eno1 .OR. i.gt.ide-bdy_eno1 .OR. j.lt.jds+bdy_eno1 .OR. j.gt.jde-bdy_eno1) then ! DME: use eno1 near domain boundaries
1965 diff2x=select_eno(diffLx,diffRx)
1966 diff2y=select_eno(diffLy,diffRy)
1967 grad=sqrt(diff2x*diff2x + diff2y*diff2y)
1969 select case(fire_upwinding)
1971 grad=sqrt(diffCx**2 + diffCy**2)
1973 diff2x=select_upwind(diffLx,diffRx)
1974 diff2y=select_upwind(diffLy,diffRy)
1975 grad=sqrt(diff2x*diff2x + diff2y*diff2y)
1976 case(2) ! godunov per osher/fedkiw
1977 diff2x=select_godunov(diffLx,diffRx)
1978 diff2y=select_godunov(diffLy,diffRy)
1979 grad=sqrt(diff2x*diff2x + diff2y*diff2y)
1981 diff2x=select_eno(diffLx,diffRx)
1982 diff2y=select_eno(diffLy,diffRy)
1983 grad=sqrt(diff2x*diff2x + diff2y*diff2y)
1984 case(4) ! Sethian - twice stronger pushdown of bumps
1985 grad=sqrt(max(diffLx,0.)**2+min(diffRx,0.)**2 &
1986 + max(diffLy,0.)**2+min(diffRy,0.)**2)
1987 case(5) ! 2nd order (DME)
1988 diff2x=select_2nd(rr,dx,lfn(i,j),lfn(i-1,j),lfn(i+1,j))
1989 diff2y=select_2nd(rr,dy,lfn(i,j),lfn(i,j-1),lfn(i,j+1))
1990 grad=sqrt(diff2x*diff2x + diff2y*diff2y)
1991 case(6) ! 3rd-order WENO (DME)
1992 a_valor=select_4th(dx,lfn(i,j),lfn(i-1,j),lfn(i-2,j),lfn(i+1,j),lfn(i+2,j))*fp%vx(i,j)+ &
1993 select_4th(dy,lfn(i,j),lfn(i,j-1),lfn(i,j-2),lfn(i,j+1),lfn(i,j+2))*fp%vy(i,j)
1994 signo_x=a_valor*select_4th(dx,lfn(i,j),lfn(i-1,j),lfn(i-2,j),lfn(i+1,j),lfn(i+2,j))
1995 signo_y=a_valor*select_4th(dy,lfn(i,j),lfn(i,j-1),lfn(i,j-2),lfn(i,j+1),lfn(i,j+2))
1996 diff2x=select_weno3(dx,lfn(i,j),lfn(i-1,j),lfn(i-2,j),lfn(i+1,j),lfn(i+2,j),signo_x)
1997 diff2y=select_weno3(dy,lfn(i,j),lfn(i,j-1),lfn(i,j-2),lfn(i,j+1),lfn(i,j+2),signo_y)
1998 grad=sqrt(diff2x*diff2x + diff2y*diff2y)
1999 case(7) ! 5th-order WENO (DME)
2000 a_valor=select_4th(dx,lfn(i,j),lfn(i-1,j),lfn(i-2,j),lfn(i+1,j),lfn(i+2,j))*fp%vx(i,j)+ &
2001 select_4th(dy,lfn(i,j),lfn(i,j-1),lfn(i,j-2),lfn(i,j+1),lfn(i,j+2))*fp%vy(i,j)
2002 signo_x=a_valor*select_4th(dx,lfn(i,j),lfn(i-1,j),lfn(i-2,j),lfn(i+1,j),lfn(i+2,j))
2003 signo_y=a_valor*select_4th(dy,lfn(i,j),lfn(i,j-1),lfn(i,j-2),lfn(i,j+1),lfn(i,j+2))
2004 diff2x=select_weno5(dx,lfn(i,j),lfn(i-1,j),lfn(i-2,j),lfn(i-3,j),lfn(i+1,j),lfn(i+2,j),lfn(i+3,j),signo_x)
2005 diff2y=select_weno5(dy,lfn(i,j),lfn(i,j-1),lfn(i,j-2),lfn(i,j-3),lfn(i,j+1),lfn(i,j+2),lfn(i,j+3),signo_y)
2006 grad=sqrt(diff2x*diff2x + diff2y*diff2y)
2007 case(8) ! high-order local band method WENO3/ENO1 (DME)
2008 if (abs(lfn(i,j)).lt.threshold_HLu) then
2009 a_valor=select_4th(dx,lfn(i,j),lfn(i-1,j),lfn(i-2,j),lfn(i+1,j),lfn(i+2,j))*fp%vx(i,j)+ &
2010 select_4th(dy,lfn(i,j),lfn(i,j-1),lfn(i,j-2),lfn(i,j+1),lfn(i,j+2))*fp%vy(i,j)
2011 signo_x=a_valor*select_4th(dx,lfn(i,j),lfn(i-1,j),lfn(i-2,j),lfn(i+1,j),lfn(i+2,j))
2012 signo_y=a_valor*select_4th(dy,lfn(i,j),lfn(i,j-1),lfn(i,j-2),lfn(i,j+1),lfn(i,j+2))
2013 diff2x=select_weno3(dx,lfn(i,j),lfn(i-1,j),lfn(i-2,j),lfn(i+1,j),lfn(i+2,j),signo_x)
2014 diff2y=select_weno3(dy,lfn(i,j),lfn(i,j-1),lfn(i,j-2),lfn(i,j+1),lfn(i,j+2),signo_y)
2015 grad=sqrt(diff2x*diff2x + diff2y*diff2y)
2017 diff2x=select_eno(diffLx,diffRx)
2018 diff2y=select_eno(diffLy,diffRy)
2019 grad=sqrt(diff2x*diff2x + diff2y*diff2y)
2021 case(9) ! high-order local band method WENO5/ENO1 (DME)
2022 if (abs(lfn(i,j)).lt.threshold_HLu) then
2023 a_valor=select_4th(dx,lfn(i,j),lfn(i-1,j),lfn(i-2,j),lfn(i+1,j),lfn(i+2,j))*fp%vx(i,j)+ &
2024 select_4th(dy,lfn(i,j),lfn(i,j-1),lfn(i,j-2),lfn(i,j+1),lfn(i,j+2))*fp%vy(i,j)
2025 signo_x=a_valor*select_4th(dx,lfn(i,j),lfn(i-1,j),lfn(i-2,j),lfn(i+1,j),lfn(i+2,j))
2026 signo_y=a_valor*select_4th(dy,lfn(i,j),lfn(i,j-1),lfn(i,j-2),lfn(i,j+1),lfn(i,j+2))
2027 diff2x=select_weno5(dx,lfn(i,j),lfn(i-1,j),lfn(i-2,j),lfn(i-3,j),lfn(i+1,j),lfn(i+2,j),lfn(i+3,j),signo_x)
2028 diff2y=select_weno5(dy,lfn(i,j),lfn(i,j-1),lfn(i,j-2),lfn(i,j-3),lfn(i,j+1),lfn(i,j+2),lfn(i,j+3),signo_y)
2029 grad=sqrt(diff2x*diff2x + diff2y*diff2y)
2031 diff2x=select_eno(diffLx,diffRx)
2032 diff2y=select_eno(diffLy,diffRy)
2033 grad=sqrt(diff2x*diff2x + diff2y*diff2y)
2039 ! DME use the same discretization scheme from fire_upwinding option to calculate the normals
2040 scale=sqrt(grad**2.0+eps)
2044 ! wind speed in direction of spread
2045 speed = fp%vx(i,j)*nvx + fp%vy(i,j)*nvy
2046 ! get rate of spread from wind speed and slope
2047 call fire_ros(ros_base,ros_wind,ros_slope, &
2049 rr=ros_base + ros_wind + fire_slope_factor*ros_slope
2051 if(fire_grows_only.gt.0)rr=max(rr,0.)
2052 ! set ros for output
2053 if(i.ge.its.and.i.le.ite.and.j.ge.jts.and.j.le.jte)ros(i,j)=rr
2055 if(fire_upwind_split.eq.0)then
2057 ! get rate of spread
2058 te = -rr*grad ! normal term
2062 ! normal direction backing rate only
2063 te = - ros_base*grad
2065 ! advection in wind direction
2066 if (abs(speed)> eps) then
2067 advx=fp%vx(i,j)*ros_wind/speed
2068 advy=fp%vy(i,j)*ros_wind/speed
2074 tanphi = fp%dzdxf(i,j)*nvx + fp%dzdyf(i,j)*nvy
2075 ! advection from slope direction
2076 if(abs(tanphi)>eps) then
2077 advx=advx+fp%dzdxf(i,j)*ros_slope/tanphi
2078 advy=advy+fp%dzdyf(i,j)*ros_slope/tanphi
2081 if(fire_upwind_split.eq.1)then
2083 ! one-sided upwinding
2084 te = te - max(advx,0.)*diffLx - min(advx,0.)*diffRy &
2085 - max(advy,0.)*diffLy - min(advy,0.)*diffRy
2088 elseif(fire_upwind_split.eq.2)then
2091 call crash('prop_ls: bad fire_upwind_split, Lax-Friedrichs not done yet')
2095 call crash('prop_ls: bad fire_upwind_split')
2102 tbound = max(tbound,rr*(abs(diff2x)/dx+abs(diff2y)/dy)/grad)
2105 ! DME: adds spatially variable artificial viscosity
2106 if (abs(lfn(i,j)).lt.threshold_AV .AND. (i.gt.ids+bdy_eno1 .and. i.lt.ide-bdy_eno1) .AND. (j.gt.jds+bdy_eno1 .and. j.lt.jde-bdy_eno1)) then
2107 fire_viscosity_var=fire_viscosity_bg
2108 elseif (abs(lfn(i,j)).ge.threshold_AV .AND. abs(lfn(i,j)).lt.threshold_AV*(1.0+fire_viscosity_band) .AND. (i.gt.ids+10 .and. i.lt.ide-10) .AND. (j.gt.jds+10 .and. j.lt.jde-10)) then
2109 fire_viscosity_var=min(fire_viscosity_bg+(fire_viscosity-fire_viscosity_bg)*(abs(lfn(i,j))-threshold_AV)/(fire_viscosity_band*threshold_AV),fire_viscosity)
2111 fire_viscosity_var=fire_viscosity
2113 te=te + fire_viscosity_var*abs(rr)*((diffRx-diffLx)+(diffRy-diffLy))
2118 call print_2d_stats(its,ite,jts,jte,ims,ime,jms,jme, &
2119 tend,'tend_ls: tend out')
2121 ! the final CFL bound
2122 tbound = 1/(tbound+tol)
2124 end subroutine tend_ls
2127 !**************************
2131 real function select_upwind(diffLx,diffRx)
2133 real, intent(in):: diffLx, diffRx
2136 ! upwind differences, L or R if bith same sign, otherwise zero
2139 if (diffLx>0.and.diffRx>0.)diff2x=diffLx
2140 if (diffLx<0.and.diffRx<0.)diff2x=diffRx
2142 select_upwind=diff2x
2143 end function select_upwind
2147 !**************************
2150 real function select_godunov(diffLx,diffRx)
2152 real, intent(in):: diffLx, diffRx
2155 ! Godunov scheme: upwind differences, L or R or none
2156 ! always test on > or < never = , much faster because of IEEE
2157 ! central diff >= 0 => take left diff if >0, ortherwise 0
2158 ! central diff <= 0 => take right diff if <0, ortherwise 0
2161 diffCx=diffRx+diffLx
2162 if (diffLx>0.and..not.diffCx<0)diff2x=diffLx
2163 if (diffRx<0.and. diffCx<0)diff2x=diffRx
2165 select_godunov=diff2x
2166 end function select_godunov
2169 !**************************
2172 real function select_eno(diffLx,diffRx)
2174 real, intent(in):: diffLx, diffRx
2177 ! 1st order ENO scheme
2179 if (.not.diffLx>0 .and. .not.diffRx>0)then
2181 elseif(.not.diffLx<0 .and. .not.diffRx<0)then
2183 elseif(.not.diffLx<0 .and. .not.diffRx>0)then
2184 if(.not. abs(diffRx) < abs(diffLx))then
2194 end function select_eno
2197 !**************************
2201 real function select_2nd(ros,dx,lfn_i,lfn_im1,lfn_ip1)
2203 real, intent(in):: lfn_i, lfn_im1, lfn_ip1
2204 real, intent(in):: ros, dx
2205 real:: diff2x_p, diff2x_m
2207 ! 2nd-order advection scheme in the x,y-direction (DME)
2211 diff2x_p=(lfn_ip1+lfn_i)/(2.*dx)
2212 diff2x_m=(lfn_i+lfn_im1)/(2.*dx)
2213 select_2nd=diff2x_p-diff2x_m
2214 end function select_2nd
2217 !**************************
2221 real function select_4th(dx,lfn_i,lfn_im1,lfn_im2,lfn_ip1,lfn_ip2)
2223 real, intent(in):: lfn_i,lfn_im1,lfn_im2,lfn_ip1,lfn_ip2
2224 real, intent(in):: dx
2225 real:: diff2x_p,diff2x_m
2227 ! 4th-order advection scheme in the x,y-direction (DME)
2231 diff2x_p=(7.0*lfn_ip1+7.0*lfn_i-lfn_ip2-lfn_im1)/(12.*dx)
2232 diff2x_m=(7.0*lfn_i+7.0*lfn_im1-lfn_ip1-lfn_im2)/(12.*dx)
2233 select_4th=diff2x_p-diff2x_m
2234 end function select_4th
2237 !**************************
2241 real function select_weno3(dx,lfn_it,lfn_im1t,lfn_im2t,lfn_ip1t,lfn_ip2t,uf)
2243 real, intent(in)::lfn_it,lfn_im1t,lfn_im2t,lfn_ip1t,lfn_ip2t,uf
2244 real:: lfn_i,lfn_im1,lfn_im2,lfn_ip1,lfn_ip2
2245 real, intent(in):: dx
2246 real:: flux_p,flux_m
2247 real, parameter:: gamma1=1./3.,gamma2=2./3.,tol=1e-6
2248 real:: w1,w2,w1t,w2t,beta1,beta2
2251 ! 3rd-order advection WENO scheme in the x,y-direction (DME)
2256 if (uf .ge. 0.0 ) then
2268 ! numerical flux at i,j+1/2 face
2269 fh_1=-0.5*lfn_im1+1.5*lfn_i
2270 fh_2=0.5*lfn_i+0.5*lfn_ip1
2271 beta1=(lfn_i-lfn_im1)**2
2272 beta2=(lfn_ip1-lfn_i)**2
2273 w1t=gamma1/(beta1+tol)**2
2274 w2t=gamma2/(beta2+tol)**2
2277 flux_p=w1*fh_1+w2*fh_2
2278 ! numerical flux at i,j-1/2 face
2279 fh_1=-0.5*lfn_im2+1.5*lfn_im1
2280 fh_2=0.5*lfn_im1+0.5*lfn_i
2281 beta1=(lfn_im1-lfn_im2)**2
2282 beta2=(lfn_i-lfn_im1)**2
2283 w1t=gamma1/(beta1+tol)**2
2284 w2t=gamma2/(beta2+tol)**2
2287 flux_m=w1*fh_1+w2*fh_2
2289 if (uf .ge. 0.0 ) then
2290 select_weno3=(flux_p-flux_m)/dx
2292 select_weno3=(flux_m-flux_p)/dx
2294 end function select_weno3
2297 !**************************
2301 real function select_weno5(dx,lfn_it,lfn_im1t,lfn_im2t,lfn_im3t,lfn_ip1t,lfn_ip2t,lfn_ip3t,uf)
2303 real, intent(in):: lfn_it,lfn_im1t,lfn_im2t,lfn_im3t,lfn_ip1t,lfn_ip2t,lfn_ip3t,uf
2304 real:: lfn_i,lfn_im1,lfn_im2,lfn_im3,lfn_ip1,lfn_ip2,lfn_ip3
2305 real, intent(in):: dx
2306 real:: flux_p,flux_m
2307 real, parameter:: gamma1=1./10.,gamma2=3./5.,gamma3=3./10.,tol=1e-6
2308 real:: w1,w2,w3,w1t,w2t,w3t,beta1,beta2,beta3
2309 real:: fh_1,fh_2,fh_3
2311 ! 5th-order advection WENO scheme in the x,y-direction (DME)
2316 if (uf .ge. 0.0 ) then
2332 ! numerical flux at i,j+1/2 face
2333 fh_1=(2.0*lfn_im2-7.0*lfn_im1+11.0*lfn_i)/6.0
2334 fh_2=(-1.0*lfn_im1+5.0*lfn_i+2.0*lfn_ip1)/6.0
2335 fh_3=(2.0*lfn_i+5.0*lfn_ip1-1.0*lfn_ip2)/6.0
2336 beta1=(13.0/12.0)*(lfn_im2-2.0*lfn_im1+lfn_i)**2+0.25*(lfn_im2-4.0*lfn_im1+3.0*lfn_i)**2
2337 beta2=(13.0/12.0)*(lfn_im1-2.0*lfn_i+lfn_ip1)**2+0.25*(lfn_im1-lfn_ip1)**2
2338 beta3=(13.0/12.0)*(lfn_i-2.0*lfn_ip1+lfn_ip2)**2+0.25*(3.0*lfn_i-4.0*lfn_ip1+lfn_ip2)**2
2339 w1t=gamma1/(beta1+tol)**2
2340 w2t=gamma2/(beta2+tol)**2
2341 w3t=gamma3/(beta3+tol)**2
2342 w1=w1t/(w1t+w2t+w3t)
2343 w2=w2t/(w1t+w2t+w3t)
2344 w3=w3t/(w1t+w2t+w3t)
2345 flux_p=w1*fh_1+w2*fh_2+w3*fh_3
2346 ! numerical flux at i,j-1/2 face
2347 fh_1=(2.0*lfn_im3-7.0*lfn_im2+11.0*lfn_im1)/6.0
2348 fh_2=(-1.0*lfn_im2+5.0*lfn_im1+2.0*lfn_i)/6.0
2349 fh_3=(2.0*lfn_im1+5.0*lfn_i-1.0*lfn_ip1)/6.0
2350 beta1=(13.0/12.0)*(lfn_im3-2.0*lfn_im2+lfn_im1)**2+0.25*(lfn_im3-4.0*lfn_im2+3.0*lfn_im1)**2
2351 beta2=(13.0/12.0)*(lfn_im2-2.0*lfn_im1+lfn_i)**2+0.25*(lfn_im2-lfn_i)**2
2352 beta3=(13.0/12.0)*(lfn_im1-2.0*lfn_i+lfn_ip1)**2+0.25*(3.0*lfn_im1-4.0*lfn_i+lfn_ip1)**2
2353 w1t=gamma1/(beta1+tol)**2
2354 w2t=gamma2/(beta2+tol)**2
2355 w3t=gamma3/(beta3+tol)**2
2356 w1=w1t/(w1t+w2t+w3t)
2357 w2=w2t/(w1t+w2t+w3t)
2358 w3=w3t/(w1t+w2t+w3t)
2359 flux_m=w1*fh_1+w2*fh_2+w3*fh_3
2361 if (uf .ge. 0.0 ) then
2362 select_weno5=(flux_p-flux_m)/dx
2364 select_weno5=(flux_m-flux_p)/dx
2366 end function select_weno5
2369 !**************************
2372 real function speed_func(diffCx,diffCy,dx,dy,i,j,fp)
2374 ! the level set method speed function
2377 real, intent(in)::diffCx,diffCy ! x and y coordinates of the direction of propagation
2378 real, intent(in)::dx,dy ! x and y coordinates of the direction of propagation
2379 integer, intent(in)::i,j ! indices of the node to compute the speed at
2380 type(fire_params),intent(in)::fp
2382 real::scale,nvx,nvy,r
2383 real::ros_base , ros_wind , ros_slope
2384 real, parameter:: eps=epsilon(0.0)
2386 ! normal direction, from central differences
2387 scale=sqrt(diffCx*diffCx+diffCy*diffCy+eps)
2391 ! get rate of spread from wind speed and slope
2393 call fire_ros(ros_base,ros_wind,ros_slope, &
2396 r=ros_base + ros_wind + ros_slope
2397 if(fire_grows_only.gt.0)r=max(r,0.)
2400 end function speed_func
2402 end module module_fr_fire_core