/
module_fr_sfire_core.F
2050 lines (1756 loc) · 68.4 KB
/
module_fr_sfire_core.F
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
!
!*** Jan Mandel August-October 2007 email: jmandel@ucar.edu or Jan.Mandel@gmail.com
!
! With contributions by Minjeong Kim.
#define DEBUG_OUT
#define DEBUG_PRINT
!#define FUEL_LEFT_2
!#define DEBUG_OUT_FUEL_LEFT
module module_fr_sfire_core
use module_fr_sfire_phys
use module_fr_sfire_util
! The mathematical core of the fire spread model. No physical constants here.
!
! subroutine sfire_core: only this routine should be called from the outside.
! subroutine fuel_left: compute remaining fuel from time of ignition.
! subroutine prop_ls: propagation of curve in normal direction.
contains
!
!****************************************
!
subroutine init_no_fire(&
ifds,ifde,jfds,jfde, &
ifms,ifme,jfms,jfme, &
ifts,ifte,jfts,jfte, &
fdx,fdy,time_now, & ! scalars in
fuel_frac,fire_area,lfn,tign) ! arrays out
implicit none
!*** purpose: initialize model to no fire
!*** arguments
integer, intent(in):: ifds,ifde,jfds,jfde ! fire domain bounds
integer, intent(in):: ifts,ifte,jfts,jfte ! fire tile bounds
integer, intent(in):: ifms,ifme,jfms,jfme ! array bounds
real, intent(in) :: fdx,fdy,time_now ! mesh spacing, time
real, intent(out), dimension (ifms:ifme,jfms:jfme) :: &
fuel_frac,fire_area,lfn,tign ! model state
!*** calls
intrinsic epsilon
!*** local
integer:: i,j
real lfn_init,time_init
lfn_init = 2*max((ifde-ifds+1)*fdx,(jfde-jfds+1)*fdy) ! more than domain diameter
time_init=time_now + max(time_now,1.0)*epsilon(time_now) ! a bit in future
do j=jfts,jfte
do i=ifts,ifte
fuel_frac(i,j)=1. ! fuel at start is 1 by definition
fire_area(i,j)=0. ! nothing burning
tign(i,j) = time_init ! ignition in future
lfn(i,j) = lfn_init ! no fire
enddo
enddo
call message('init_model_no_fire: state set to no fire')
end subroutine init_no_fire
!
!******************
!
subroutine ignite_fire( ifds,ifde,jfds,jfde, & ! fire domain dims - the whole domain
ifms,ifme,jfms,jfme, &
ifts,ifte,jfts,jfte, &
start_x,start_y,end_x,end_y, &
r,start_t,end_t, &
start_ts,end_ts, &
coord_xf,coord_yf, &
unit_xf,unit_yf, &
lfn,tign,ignited)
implicit none
!*** purpose: ignite a circular/line fire
!*** description
! ignite fire in the region within radius r from the line (sx,sy) to (ex,ey).
! the coordinates of nodes are given as the arrays coord_xf and coord_yf
! r is given in m
! one unit of coord_xf is unit_xf m
! one unit of coord_yf is unit_yf m
! so a node (i,j) will be ignited iff for some (x,y) on the line
! || ( (coord_xf(i,j) - x)*unit_xf , (coord_yf(i,j) - y)*unit_yf ) || <= r
!*** arguments
integer, intent(in):: ifds,ifde,jfds,jfde ! fire domain bounds
integer, intent(in):: ifts,ifte,jfts,jfte ! fire tile bounds
integer, intent(in):: ifms,ifme,jfms,jfme ! array bounds
real, intent(in):: start_x,start_y ! start of ignition line, from lower left corner
real, intent(in):: end_x,end_y ! end of ignition line, or zero
real, intent(in):: r ! all within the radius of the line will ignite
real, intent(in):: start_t,end_t ! the ignition time for the start and the end of the line
real, intent(in):: start_ts,end_ts ! the time step start and end
real, dimension(ifms:ifme, jfms:jfme), intent(in):: &
coord_xf,coord_yf ! node coordinates
real, intent(in):: unit_xf,unit_yf ! coordinate units in m
real, intent(inout), dimension (ifms:ifme,jfms:jfme) :: &
lfn, tign ! level function, ignition time (state)
integer, intent(out):: ignited ! number of nodes newly ignited
!*** local
integer:: i,j
real::mx,my,ax,ay,dam2,d,dames,des2,am_es,cos2,lfn_new,dmc2,time_ign,rels,rele,mid_t,cos_ame,dif_th
real:: sx,sy ! start of ignition line, from lower left corner
real:: ex,ey ! end of ignition line, or zero
real:: st,et ! start and end of time of the ignition line
character(len=128):: msg
real::cx2,cy2,dmax
st=max(start_ts,start_t) ! the start time of ignition in this time step
et=min(end_ts,end_t) ! the end time of ignition in this time step
if(st>et)return ! no ignition in this time step, nothing to do
! find the points where the ignition line starts and ends this time step
! compute in a way that is stable when end_t - start_t is small
if(start_t < end_t)then
rels = (st - start_t) / (end_t - start_t)
sx = start_x + rels * (end_x - start_x)
sy = start_y + rels * (end_y - start_y)
rele = (et - end_t) / (end_t - start_t)
ex = end_x + rele * (end_x - start_x)
ey = end_y + rele * (end_y - start_y)
else
sx = start_x
sy = start_y
ex = end_x
ey = end_y
endif
mid_t = (end_t + start_t)*0.5 ! ignition time in the middle
dif_th = (end_t - start_t)*0.5
cx2=unit_xf*unit_xf
cy2=unit_yf*unit_yf
ignited=0
dmax=0
! midpoint m = (mx,my)
mx = (sx + ex)/2
my = (sy + ey)/2
do j=jfts,jfte
do i=ifts,ifte
! coordinates of the node (i,j), the lower left corner of the domain is (0 0)
! ax = fdx*(i - ifds + 0.5)
! ay = fdy*(j - jfds + 0.5)
ax=coord_xf(i,j)
ay=coord_yf(i,j)
! the following computation of distance is also for the case
! when s=e exactly or approximately
dam2=(ax-mx)*(ax-mx)*cx2+(ay-my)*(ay-my)*cy2 ! |a-m|^2
! compute distance of a=(ax,ay) and the nearest point on the segment
! [(sx,sy), (ex,ey)] as the distance of (ax,ay) from the midpoint (mx,my)
! minus a correction (because of rounding errors): |a-c|^2 = |a-m|^2 - |m-c|^2
! when |m-c| >= |s-e|/2 the nearest point is one of the endpoints
!
! a
! /| \
! s---m-c--e
!
! |m-c| = |a-m| cos (a-m,e-s)
! = |a-m| (a-m).(e-s))/(|a-m|*|e-s|)
des2 = (ex-sx)*(ex-sx)*cx2+(ey-sy)*(ey-sy)*cy2 ! des2 = |e-s|^2
dames = dam2*des2
am_es=(ax-mx)*(ex-sx)*cx2+(ay-my)*(ey-sy)*cy2 ! am_es = (a-m).(e-s)
if(dames>0)then
cos2 = (am_es*am_es)/dames ! cos2 = cos^2 (a-m,e-s)
else ! point a already is the midpoint
cos2 = 0.
endif
dmc2 = dam2*cos2 ! dmc2 = |m-c|^2
if(4.*dmc2 <= des2)then ! if |m-c|<=|e-s|/2
d = sqrt(max(dam2 - dmc2,0.)) ! d=|a-m|^2 - |m-c|^2, guard rounding
elseif(am_es>0)then ! if cos > 0, closest is e
d = sqrt((ax-ex)*(ax-ex)*cx2+(ay-ey)*(ay-ey)*cy2) ! |a-e|
else ! closest is s
d = sqrt((ax-sx)*(ax-sx)*cx2+(ay-sy)*(ay-sy)*cy2) ! |a-s|
endif
dmax=max(d,dmax)
lfn_new=d-r
if(lfn_new<=0) then
ignited=ignited+1 ! count
endif
if(lfn(i,j)>0 .and. lfn_new<=0) then
cos_ame = sqrt(cos2) ! relative distance of c from m
time_ign = mid_t + sign(cos_ame,am_es)*dif_th ! ignition time at c by going from m
tign(i,j)=time_ign ! newly ignited now
endif
lfn(i,j)=min(lfn(i,j),lfn_new) ! update the level set function
! debug
! write(msg,'(2i4,10f12.6)')i,j,d,sx,ex,sy,ey
! call message(msg)
! write(msg,'(10f12.6)')ax,ay,dmc2,des2,cos2
! call message(msg)
! write(msg,'(10f16.6)')unit_xf,unit_yf,cx2,cy2,dam2
! call message(msg)
! write(msg,'(10f16.6)')des2,dames,am_es,mx,my
! call message(msg)
enddo
enddo
!$OMP CRITICAL(SFIRE_CORE_CRIT)
write(msg,'(a,2f11.6,a,2f11.6)')'ignite_fire: from',sx,sy,' to ',ex,ey
call message(msg)
write(msg,'(a,2f11.2,a,f8.1,a)')'units ',unit_xf,unit_yf,' m max dist ',dmax,' m'
call message(msg)
write(msg,'(a,f4.1,a,f8.1,a,i10)')' radius ',r,' time',time_ign,' ignited nodes',ignited
call message(msg)
!$OMP END CRITICAL(SFIRE_CORE_CRIT)
end subroutine ignite_fire
!
!**********************
!
subroutine fuel_left( &
ifds,ifde,jfds,jfde, &
ims,ime,jms,jme, &
its,ite,jts,jte, &
ifs,ife,jfs,jfe, &
lfn, tign, fuel_time, time_now, fuel_frac, fire_area)
implicit none
!*** purpose: determine fraction of fuel remaining
!*** NOTE: because variables are cell centered, need halo/sync width 1 before
!*** Jan Mandel August 2007 email: jmandel@ucar.edu or Jan.Mandel@gmail.com
!*** arguments
integer, intent(in) ::ifds,ifde,jfds,jfde,its,ite,jts,jte,ims,ime &
,jms,jme,ifs,ife,jfs,jfe
real, intent(in), dimension(ims:ime,jms:jme)::lfn,tign,fuel_time
real, intent(in):: time_now
real, intent(out), dimension(ifs:ife,jfs:jfe)::fuel_frac
real, intent(out), dimension(ims:ime,jms:jme):: fire_area
! ims,ime,jms,jme in memory dimensions
! its,ite,jts,jte in tile dimensions (cells where fuel_frac computed)
! ifs,ife,jfs,jfe in fuel_frac memory dimensions
! lfn in level function, at nodes at midpoints of cells
! tign in ignition time, at nodes at nodes at midpoints of cells
! fuel_time in time constant of fuel, per cell
! time_now in time now
! fuel_frac out fraction of fuel remaining, per cell
! fire_area out fraction of cell area on fire
!*** local
integer::i,j,ir,jr,icl,jcl,isubcl,jsubcl,i2,j2,ii,jj,its1,jts1,ite1,jte1
real::fmax,frat,helpsum1,helpsum2,fuel_left_ff,fire_area_ff,rx,ry,tignf(2,2)
real,dimension(3,3)::tff,lff
! help variables instead of arrays fuel_left_f and fire_area_f
real::lffij,lffi1j,lffij1,lffi1j1,tifij,tifi1j,tifij1,tifi1j1,tx,ty,txx,tyy
! variables for calculation instead of lff(i,j) and tif(i,j)is lffij,tifij etc..#define IFCELLS (ite-its+1)*fuel_left_irl
#define JFCELLS (jte-jts+1)*fuel_left_jrl
character(len=128)::msg
integer::m,omp_get_thread_num
call check_mesh_2dim(its-1,ite+1,jts-1,jte+1,ims,ime,jms,jme)
call check_mesh_2dim(its,ite,jts,jte,ifs,ife,jfs,jfe)
! refinement
ir=fuel_left_irl
jr=fuel_left_jrl
if ((ir.ne.2).or.(jr.ne.2)) then
call crash('fuel_left: ir.ne.2 or jr.ne.2 ')
endif
rx=1./ir
ry=1./jr
! interpolate level set function to finer grid
#ifdef DEBUG_OUT_FUEL_LEFT
call write_array_m(1,IFCELLS+1,1,JFCELLS+1,1,IFCELLS+1,1,JFCELLS+1,lff,'lff',0)
call write_array_m(1,IFCELLS+1,1,JFCELLS+1,1,IFCELLS+1,1,JFCELLS+1,tif,'tif',0)
#endif
!
! example for ir=2:
!
! | coarse cell |
! its-1 its ite ite+1
! -------X------------|-----.-----X-----.-----|--........----|----------X----------|---------X
! fine node 1 2 3 2*(ite-its+1)
! fine cell 1 2 cell 2*(ite-its+1)
! Loop over cells in Tile
! Changes made by Volodymyr Kondratenko 09/24/2009
its1=max(its,ifds+1)
ite1=min(ite,ifde-1)
jts1=max(jts,jfds+1)
jte1=min(jte,jfde-1)
write(*,*)"fuel_left_method",fuel_left_method
do icl=its1,ite1
do jcl=jts1,jte1
helpsum1=0
helpsum2=0
! Loop over subcells in cell #(icl,jcl)
write(*,*)"ifds,ifde",ifds,ifde
write(*,*)"jfds,jfde",jfds,jfde
write(*,*)"its,ite,jts,jte",its,ite,jts,jte
write(*,*)"icl,jcl",icl,jcl
call tign_lfn_interpolation(time_now,icl,jcl,ims,ime,jms,jme, &
tign,lfn,tff,lff)
!endif
do isubcl=1,ir
do jsubcl=1,jr
!we use 4 points of each subcell in fuel_left_cell_1
! and in fuel_left_cell_2: bottome left are (1,1), (1,2), (2,1), (2,2)
if(fuel_left_method.eq.1)then
call fuel_left_cell_1( fuel_left_ff, fire_area_ff, &
lff(isubcl,jsubcl),lff(isubcl,jsubcl+1),lff(isubcl+1,jsubcl),lff(isubcl+1,jsubcl+1), &
tff(isubcl,jsubcl),tff(isubcl,jsubcl+1),tff(isubcl+1,jsubcl),tff(isubcl+1,jsubcl+1), &
time_now, fuel_time(icl,jcl))
elseif(fuel_left_method.eq.2)then
fire_area_ff=0 ! initialize to something until computed in fuel_left_f(i,j)
fuel_left_ff=fuel_left_cell_3( &
lff(isubcl,jsubcl),lff(isubcl,jsubcl+1),lff(isubcl+1,jsubcl),lff(isubcl+1,jsubcl+1), &
tff(isubcl,jsubcl),tff(isubcl,jsubcl+1),tff(isubcl+1,jsubcl),tff(isubcl+1,jsubcl+1), &
time_now, fuel_time(icl,jcl))
! dont forget to change fire_area_ff here
else
call crash('fuel_left: unknown fuel_left_method')
endif
! consistency check
if(fire_area_ff.lt.-1e-6 .or. &
(fire_area_ff.eq.0. .and. fuel_left_ff.lt.1.-1e-6))then
!$OMP CRITICAL(SFIRE_CORE_CRIT)
write(msg,'(a,2i6,2(a,f11.8))')'fuel_left: at node',i,j, &
' of refined mesh fuel burnt',1-fuel_left_ff,' fire area',fire_area_ff
!$OMP END CRITICAL(SFIRE_CORE_CRIT)
call crash(msg)
endif
helpsum1=helpsum1+fuel_left_ff
helpsum2=helpsum2+fire_area_ff
enddo
enddo
write(*,*)"fuel_frac",helpsum1
fuel_frac(icl,jcl)=helpsum1
fire_area(icl,jcl)=helpsum2
enddo
enddo
write(*,*)"finish"
#ifdef DEBUG_OUT_FUEL_LEFT
call write_array_m(its,ite,jts,jte,ims,ime,jms,jme,fire_area,'fire_area',0)
call write_array_m(its,ite,jts,jte,ims,ime,jms,jme,fuel_frac,'fuel_frac',0)
call write_array_m(1,IFCELLS,1,JFCELLS,1,IFCELLS,1,JFCELLS,fuel_left_f,'fuel_left_f',0)
call write_array_m(1,IFCELLS,1,JFCELLS,1,IFCELLS,1,JFCELLS,fire_area_f,'fire_area_f',0)
#endif
! finish the averaging
do j=jts,jte
do i=its,ite
fuel_frac(i,j) = fuel_frac(i,j) /(ir*jr) ! multiply by weight for averaging over coarse cell
fire_area(i,j) = fire_area(i,j) /(ir*jr) !
enddo
enddo
! consistency check after sum
!fmax=0
!do j=jts,jte
! do i=its,ite
! if(fire_area(i,j).eq.0.)then
! if(fuel_frac(i,j).lt.1.-1e-6)then
!!$OMP CRITICAL(SFIRE_CORE_CRIT)
! write(msg,'(a,2i6,2(a,f11.8))')'fuel_left: at node',i,j, &
! ' fuel burnt',1-fuel_frac(i,j),' but fire area',fire_area(i,j)
!!$OMP END CRITICAL(SFIRE_CORE_CRIT)
! call crash(msg)
! endif
! else
! frat=(1-fuel_frac(i,j))/fire_area(i,j)
! fmax=max(fmax,frat)
! endif
! enddo
!enddo
!$OMP CRITICAL(SFIRE_CORE_CRIT)
write(msg,'(a,4i6,a,f10.7)')'fuel_left: tile',its,ite,jts,jte,' max fuel burnt/area',fmax
!$OMP END CRITICAL(SFIRE_CORE_CRIT)
call message(msg)
return
end subroutine fuel_left
!
!************************
!
!
!*************************
!Subroutine that is calculating tign and lfn of four endpoints of the subcell
! that is located at isubcl,jsubcl of the cell -(icl,jcl)
!
subroutine tign_lfn_interpolation(time_now,icl,jcl,ims,ime,jms,jme, &
tign,lfn,tff,lff)
real, intent(in):: time_now ! not ignited nodes will have tign set to >= time_now
integer, intent(in) :: icl,jcl
integer, intent(in) :: ims,ime,jms,jme ! memory dimensions of all arrays
real, intent(in), dimension(ims:ime,jms:jme)::lfn,tign
real, intent(out),dimension(3,3)::tff,lff
! (3,1)-------------(3,2)-------------(3,3)
! | | |
! | (2,1) | (2,2) |
! | | |
! | | |
! | | |
! | | |
! (2,1)--------node-(icl,jcl)---------(2,3)-----------(icl,jcl+1)-------------|
! | sub-node (2,2) |
! | | |
! | (1,1) | (1,2) | each fire mesh cell is decomposed in 4
! | | | tff,lff is computed at the nodes of
! | | | the subcells, numbered (1,1)...(3,3)
! (1,1)-------------(1,2)-------------(1,3)--
! | | |
! | (2,1) | (2,2) |
! | | |
! | | |
! | node |
! -------------(icl-1,jcl------------------
! | | | )
!
!**********************
! Direct calculation tif and lff, avoiding arrays, just for case ir=jr=2
! Checking whether icl or jcl is on the boundary
lff(1,1)=0.25*(lfn(icl-1,jcl-1)+lfn(icl-1,jcl)+lfn(icl,jcl-1)+lfn(icl,jcl))
call tign_four_pnts_interp(tign(icl-1,jcl-1),tign(icl-1,jcl),tign(icl,jcl-1), &
tign(icl,jcl),lfn(icl-1,jcl-1),lfn(icl-1,jcl), &
lfn(icl,jcl-1),lfn(icl,jcl),lff(1,1),tff(1,1),time_now)
lff(1,2)=0.5*(lfn(icl-1,jcl)+lfn(icl,jcl))
call tign_line_interp(tign(icl-1,jcl),tign(icl,jcl),lfn(icl-1,jcl),lfn(icl,jcl), &
lff(1,2),tff(1,2),time_now)
lff(1,3)=0.25*(lfn(icl-1,jcl+1)+lfn(icl-1,jcl)+lfn(icl,jcl+1)+lfn(icl,jcl))
call tign_four_pnts_interp(tign(icl-1,jcl),tign(icl-1,jcl+1),tign(icl,jcl), &
tign(icl,jcl+1),lfn(icl-1,jcl),lfn(icl-1,jcl+1), &
lfn(icl,jcl),lfn(icl,jcl+1),lff(1,3),tff(1,3),time_now)
lff(2,1)=0.5*(lfn(icl,jcl-1)+lfn(icl,jcl))
call tign_line_interp(tign(icl,jcl-1),tign(icl,jcl),lfn(icl,jcl-1),lfn(icl,jcl), &
lff(2,1),tff(2,1),time_now)
lff(2,2)=lfn(icl,jcl)
tff(2,2)=tign(icl,jcl)
lff(2,3)=0.5*(lfn(icl,jcl+1)+lfn(icl,jcl))
call tign_line_interp(tign(icl,jcl+1),tign(icl,jcl),lfn(icl,jcl+1),lfn(icl,jcl), &
lff(2,3),tff(2,3),time_now)
lff(3,1)=0.25*(lfn(icl,jcl-1)+lfn(icl,jcl)+lfn(icl+1,jcl-1)+lfn(icl+1,jcl))
call tign_four_pnts_interp(tign(icl,jcl-1),tign(icl,jcl),tign(icl+1,jcl-1), &
tign(icl+1,jcl),lfn(icl,jcl-1),lfn(icl,jcl), &
lfn(icl+1,jcl-1),lfn(icl+1,jcl),lff(3,1),tff(3,1),time_now)
lff(3,2)=0.5*(lfn(icl+1,jcl)+lfn(icl,jcl))
call tign_line_interp(tign(icl+1,jcl),tign(icl,jcl),lfn(icl+1,jcl),lfn(icl,jcl), &
lff(3,2),tff(3,2),time_now)
lff(3,3)=0.25*(lfn(icl,jcl)+lfn(icl,jcl+1)+lfn(icl+1,jcl)+lfn(icl+1,jcl+1))
call tign_four_pnts_interp(tign(icl,jcl),tign(icl,jcl+1),tign(icl+1,jcl), &
tign(icl+1,jcl+1),lfn(icl,jcl),lfn(icl,jcl+1), &
lfn(icl+1,jcl),lfn(icl+1,jcl+1),lff(3,3),tff(3,3),time_now)
end subroutine tign_lfn_interpolation
subroutine tign_line_interp(tign1,tign2,lfn1,lfn2,lfn_subcl,tign_subcl,time_now)
real,intent(in) :: tign1,tign2,lfn1,lfn2,lfn_subcl,time_now
real,intent(out) :: tign_subcl
real :: a,b,c,err
err=0.1
! write(*,*)"lfn1,lfn2,tign1,tign2,lfn_subcl,timenow",lfn1,lfn2,tign1,tign2,lfn_subcl,time_now
if((lfn1.le.0).AND.(lfn2.le.0)) then
tign_subcl=0.5*(tign1+tign2)
elseif((lfn1.gt.0).AND.(lfn2.gt.0))then
if ((abs(tign1-time_now).gt.err).or.(abs(tign2-time_now).gt.err)) then
write(*,*)"lfn1,lfn2,tign1,tign2,timenow",lfn1,lfn2,tign1,tign2,time_now
call crash('line_interp: when lfn1(2)>0 their tign1(2) should = time_now')
else
tign_subcl=time_now;
endif
elseif(lfn_subcl.gt.0) then
if ((abs(tign1-time_now).gt.err).OR.(abs(tign2-time_now).gt.err)) then
call crash('tign_line_interp one of tign1,2 should be equal time_now')
else
tign_subcl=time_now;
endif
else
!lfn_subcl<=0;
!case when E is on fire
! tign_subcl~=c*lfn_subcl+time_now;
if (lfn1.le.0) then
a=lfn1;
b=tign1-time_now;
elseif (lfn2.le.0) then
a=lfn2;
b=tign2-time_now;
else
call crash('tign_line_interp: if E is on fire then one of lfn1 or lfn2 &
should be negative');
endif
if (b.gt.0) then
call crash('tign_ should be less than time_now');
else
c=b/a;
tign_subcl=c*lfn_subcl+time_now;
endif
endif
end subroutine tign_line_interp
!
!************************
!
subroutine tign_four_pnts_interp(tign1,tign2,tign3,tign4,lfn1,lfn2, &
lfn3,lfn4,lfn_subcl,tign_subcl,time_now)
real,intent(in) :: tign1,tign2,tign3,tign4
real,intent(in) :: lfn1,lfn2,lfn3,lfn4,lfn_subcl,time_now
real,intent(out) :: tign_subcl
real :: a,b,c,err
err=0.0001
if((lfn1.le.0).AND.(lfn2.le.0).AND.(lfn3.le.0).AND.(lfn4.le.0)) then
tign_subcl=0.25*(tign1+tign2+tign3+tign4)
elseif((lfn1.gt.0).AND.(lfn2.gt.0).AND.(lfn3.gt.0).AND.(lfn4.gt.0))then
!if ((abs(tign1-time_now).gt.err).OR.(abs(tign2-time_now).gt.err).OR.(abs(tign3-time_now).gt.err).OR.(abs(tign4-time_now).gt.err)) then
! call crash('tign_four_pnts_interp: when lfn1(2,3,4)>0 their tign1(2,3,4) should = time_now')
!else
tign_subcl=time_now;
!endif
elseif(lfn_subcl.gt.0) then
! if ((abs(tign1-time_now).gt.err).OR.(abs(tign2-time_now).gt.err).OR.(abs(tign3-time_now).gt.err).OR.(abs(tign4-time_now).gt.err)) then
! call crash('tign_line_interp one of tign1(2,3,4) should be equal time_now')
! else
tign_subcl=time_now;
! endif
else
!lfn_subcl<=0;
!case when E is on fire
! tign_subcl~=c*lfn_subcl+time_now;
a=0;
b=0;
if (lfn1.le.0) then
! if (tign1.gt.time_now)
! call crash('tign_four_pnts_interp tign1 should be less then time_now');
! else
! Can not assign to a named constant
a=a+lfn1*lfn1;
b=b+(tign1-time_now)*lfn1;
! endif
endif
if (lfn2.le.0) then
! if (tign2.gt.time_now)
! call crash('tign_four_pnts_interp tign2 should be less then time_now');
! else
! Can not assign to a named constant
a=a+lfn2*lfn2;
b=b+(tign2-time_now)*lfn2;
! endif
endif
if (lfn3.le.0) then
! if (tign3.gt.time_now)
! call crash('tign_four_pnts_interp tign3 should be less then time_now');
! else
! Can not assign to a named constant
a=a+lfn3*lfn3;
b=b+(tign3-time_now)*lfn3;
! endif
endif
if (lfn4.le.0) then
! if (tign4.gt.time_now)
! call crash('tign_four_pnts_interp tign4 should be less then time_now');
! else
! Can not assign to a named constant
a=a+lfn4*lfn4;
b=b+(tign4-time_now)*lfn4;
! endif
endif
if ((abs(a).lt.err).or.(b.lt.0)) then
call crash('tign_four_pnts_interp: if E is on fire then one of cells &
should be on fire or tign_ should be less than time_now')
else
c=b/a;
tign_subcl=c*lfn_subcl+time_now;
endif
endif
end subroutine tign_four_pnts_interp
!
!************************
!
subroutine fuel_left_cell_1( fuel_frac_left, fire_frac_area, &
lfn00,lfn01,lfn10,lfn11, &
tign00,tign01,tign10,tign11,&
time_now, fuel_time_cell)
!*** purpose: compute the fuel fraction left in one cell
implicit none
!*** arguments
real, intent(out):: fuel_frac_left, fire_frac_area !
real, intent(in)::lfn00,lfn01,lfn10,lfn11 ! level set function at 4 corners of the cell
real, intent(in)::tign00,tign01,tign10,tign11! ignition time at the 4 corners of the cell
real, intent(in)::time_now ! the time now
real, intent(in)::fuel_time_cell ! time to burns off to 1/e
!*** Description
! The area burning is given by the condition L <= 0, where the function P is
! interpolated from lfn(i,j)
!
! The time since ignition is the function T, interpolated in from tign(i,j)-time_now.
! The values of tign(i,j) where lfn(i,j)>=0 are ignored, tign(i,j)=0 is taken
! when lfn(i,j)=0.
!
! The function computes an approxmation of the integral
!
!
! /\
! |
! fuel_frac_left = 1 - | 1 - exp(-T(x,y)/fuel_time_cell)) dxdy
! |
! \/
! 0<x<1
! 0<y<1
! L(x,y)<=0
!
! When the cell is not burning at all (all lfn>=0), then fuel_frac(i,j)=1.
! Because of symmetries, the result should not depend on the mesh spacing dx dy
! so dx=1 and dy=1 assumed.
!
! Example:
!
! lfn<0 lfn>0
! (0,1) -----O--(1,1) O = points on the fireline, T=tnow
! | \ | A = the burning area for computing
! | \| fuel_frac(i,j)
! | A O
! | |
! | |
! (0,0)---------(1,0)
! lfn<0 lfn<0
!
! Approximations allowed:
! The fireline can be approximated by straight line(s).
! When all cell is burning, approximation by 1 point Gaussian quadrature is OK.
!
! Requirements:
! 1. The output should be a continuous function of the arrays lfn and
! tign whenever lfn(i,j)=0 implies tign(i,j)=tnow.
! 2. The output should be invariant to the symmetries of the input in each cell.
! 3. Arbitrary combinations of the signs of lfn(i,j) should work.
! 4. The result should be at least 1st order accurate in the sense that it is
! exact if the time from ignition is a linear function.
!
! If time from ignition is approximated by polynomial in the burnt
! region of the cell, this is integral of polynomial times exponential
! over a polygon, which can be computed exactly.
!
! Requirement 4 is particularly important when there is a significant decrease
! of the fuel fraction behind the fireline on the mesh scale, because the
! rate of fuel decrease right behind the fireline is much larger
! (exponential...). This will happen when
!
! change of time from ignition within one mesh cell / fuel_time_cell is not << 1
!
! This is the same as
!
! mesh cell size
! X = ------------------------- is not << 1
! fireline speed * fuel_time_cell
!
!
! When X is large then the fuel burnt in one timestep in the cell is
! approximately proportional to length of fireline in that cell.
!
! When X is small then the fuel burnt in one timestep in the cell is
! approximately proportional to the area of the burning region.
!
!*** calls
intrinsic tiny
!*** local
real::ps,aps,area,ta,out
real::t00,t01,t10,t11
real,parameter::safe=tiny(aps)
character(len=128)::msg
! the following algorithm is a very crude approximation
! minus time since ignition, 0 if no ignition yet
! it is possible to have 0 in fire region when ignitin time falls in
! inside the time step because lfn is updated at the beginning of the time step
t00=tign00-time_now
if(lfn00>0. .or. t00>0.)t00=0.
t01=tign01-time_now
if(lfn01>0. .or. t01>0.)t01=0.
t10=tign10-time_now
if(lfn10>0. .or. t10>0.)t10=0.
t11=tign11-time_now
if(lfn11>0. .or. t11>0.)t11=0.
! approximate burning area, between 0 and 1
ps = lfn00+lfn01+lfn10+lfn11
aps = abs(lfn00)+abs(lfn01)+abs(lfn10)+abs(lfn11)
aps=max(aps,safe)
area =(-ps/aps+1.)/2.
area = max(area,0.) ! make sure area is between 0 and 1
area = min(area,1.)
! average negative time since ignition
ta=0.25*(t00+t01+t10+t11)
! exp decay in the burning area
out=1.
!if(area>0.)out=1. - area*(1. - exp(ta/fuel_time_cell))
if(area>0)out=area*exp(ta/fuel_time_cell) + (1. - area)
if(out>1.)then
!$OMP CRITICAL(SFIRE_CORE_CRIT)
write(msg,*)'out=',out,'>1 area=',area,' ta=',ta
call message(msg)
write(msg,*)'tign=',tign00,tign01,tign10,tign11,' time_now=',time_now
!$OMP END CRITICAL(SFIRE_CORE_CRIT)
call message(msg)
!call message('WARNING: fuel_left_cell_1: fuel fraction > 1')
call crash('fuel_left_cell_1: fuel fraction > 1')
endif
!out = max(out,0.) ! make sure out is between 0 and 1
!out = min(out,1.)
fuel_frac_left = out
fire_frac_area = area
end subroutine fuel_left_cell_1
!
!****************************************
!
! function calculation fuel_frac made by Volodymyr Kondratenko on the base of
! the code made by Jan Mandel and Minjeong
real function fuel_left_cell_3( &
lfn00,lfn01,lfn10,lfn11, &
tign00,tign01,tign10,tign11,&
time_now, fuel_time_cell)
!*** purpose: compute the fuel fraction left in one cell
implicit none
!*** arguments
real, intent(in)::lfn00,lfn01,lfn10,lfn11 ! level set function at 4 corners of the cell
real, intent(in)::tign00,tign01,tign10,tign11! ignition time at the 4 corners of the cell
real, intent(in)::time_now ! the time now
real, intent(in)::fuel_time_cell ! time to burns off to 1/e
!*** Description
! The area burning is given by the condition L <= 0, where the function P is
! interpolated from lfn(i,j)
!
! The time since ignition is the function T, interpolated in from tign(i,j)-time_now.
! The values of tign(i,j) where lfn(i,j)>=0 are ignored, tign(i,j)=0 is taken
! when lfn(i,j)=0.
!
! The function computes an approxmation of the integral
!
!
! /\
! |
! fuel_frac_left = 1 - | 1 - exp(-T(x,y)/fuel_time_cell)) dxdy
! |
! \/
! 0<x<1
! 0<y<1
! L(x,y)<=0
!
! When the cell is not burning at all (all lfn>=0), then fuel_frac(i,j)=1.
! Because of symmetries, the result should not depend on the mesh spacing dx dy
! so dx=1 and dy=1 assumed.
!
! Example:
!
! lfn<0 lfn>0
! (0,1) -----O--(1,1) O = points on the fireline, T=tnow
! | \ | A = the burning area for computing
! | \| fuel_frac(i,j)
! | A O
! | |
! | |
! (0,0)---------(1,0)
! lfn<0 lfn<0
!
! Approximations allowed:
! The fireline can be approximated by straight line(s).
! When all cell is burning, approximation by 1 point Gaussian quadrature is OK.
!
! Requirements:
! 1. The output should be a continuous function of the arrays lfn and
! tign whenever lfn(i,j)=0 implies tign(i,j)=tnow.
! 2. The output should be invariant to the symmetries of the input in each cell.
! 3. Arbitrary combinations of the signs of lfn(i,j) should work.
! 4. The result should be at least 1st order accurate in the sense that it is
! exact if the time from ignition is a linear function.
!
! If time from ignition is approximated by polynomial in the burnt
! region of the cell, this is integral of polynomial times exponential
! over a polygon, which can be computed exactly.
!
! Requirement 4 is particularly important when there is a significant decrease
! of the fuel fraction behind the fireline on the mesh scale, because the
! rate of fuel decrease right behind the fireline is much larger
! (exponential...). This will happen when
!
! change of time from ignition within one mesh cell * fuel speed is not << 1
!
! This is the same as
!
! mesh cell size*fuel_speed
! ------------------------- is not << 1
! fireline speed
!
!
! When X is large then the fuel burnt in one timestep in the cell is
! approximately proportional to length of fireline in that cell.
!
! When X is small then the fuel burnt in one timestep in the cell is
! approximately proportional to the area of the burning region.
!#ifndef FUEL_LEFT
!call crash('fuel_left_cell_3: not implemented, please use fire_fuel_left_method=1')
!fuel_left_cell_3=0. ! to avoid compiler warning about value not set
!end function fuel_left_cell_3
!#else
!*** calls
intrinsic tiny
!*** local
real::ps,aps,area,ta,out
real::t00,t01,t10,t11
real,parameter::safe=tiny(aps)
character(len=128)::msg
real::dx,dy ! mesh sizes
!*** local
integer::i,j,k
! least squares
integer::mmax,nb,nmax,pmax,nin,nout
parameter(mmax=3,nb=64,nmax=8,pmax=8)
integer lda, ldb, lwork, info
parameter (lda=nmax, ldb=nmax, lwork=mmax+nmax+nb*(nmax+pmax))
integer n,m,p
real,dimension(lda,mmax):: mA
real,dimension(nmax):: vecD
real,dimension(lwork):: WORK
real,dimension(pmax):: vecY
real,dimension(mmax):: vecX
real,dimension(ldb,pmax)::mB
real,dimension(mmax)::u
real::tweight,tdist
integer::kk,ll,ss
real::rnorm
real,dimension(8,2)::xylist,xytlist
real,dimension(8)::tlist,llist,xt
real,dimension(5)::xx,yy
real,dimension(5)::lfn,tign
integer:: npoint
real::tt,x0,y0,xts,xte,yts,yte,xt1,xt2
real::lfn0,lfn1,dist,nr,s,errQ,ae,ce,ceae,a0,a1,a2,d,cet
real::s1,s2,s3
real::upper,lower,ah,ch,aa,cc,aupp,cupp,alow,clow
real,dimension(2,2)::mQ
real,dimension(2)::ut
!calls
intrinsic epsilon
real, parameter:: zero=0.,one=1.,eps=epsilon(zero)
!!!! For finite differences by VK
real::tign_middle,dt_dx,dt_dy,lfn_middle,a,b,c
! external functions
real::snrm2
double precision :: dnrm2
external dnrm2
external snrm2
! external subroutines
external sggglm
external dggglm
!executable statements
! a very crude approximation - replace by a better code
! call check_mesh_2dim(ids,ide+1,jds,jde+1,ims,ime,jms,jme)
dx=1
dy=1
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!comment - changed tign-time_now to time_now-tign
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
t00=time_now-tign00
if(lfn00>=0. .or. t00<0.)t00=0.
t01=time_now-tign01
if(lfn01>=0. .or. t01<0.)t01=0.
t10=time_now-tign10
if(lfn10>=0. .or. t10<0.)t10=0.
t11=time_now-tign11
if(lfn11>=0. .or. t11<0.)t11=0.
!*** case0 Do nothing
if ( lfn00>=0 .and. lfn10>=0 .and. lfn01>=0 .and. lfn11>=0 ) then
print*,"Case 0"
out = 1.0 ! fuel_left, no burning
!*** case4 all four coners are burning
else if (lfn00<=0 .and. lfn10<=0 .and. lfn01<=0 .and. lfn11<=0) then
!!!!!!!!!!!
print*,"Case 4"
! All burning
! T=u(1)*x+u(2)*y+u(3)
! t(0,0)=tign(1,1)
! t(0,fd(2))=tign(1,2)
! t(fd(1),0)=tign(2,1)
! t(fd(1),fd(2))=tign(2,2)
! t(g/2,h/2)=sum(tign(i,i))/4
! dt/dx=(1/2h)*(t10-t00+t11-t01)
! dt/dy=(1/2h)*(t01-t00+t11-t10)
! approximate T(x,y)=u(1)*x+u(2)*y+u(3) Using finite differences
! t(x,y)=t(h/2,h/2)+(x-h/2)*dt/dx+(y-h/2)*dt/dy
! u(1)=dt/dx
! u(2)=dt/dy
! u(3)=t(h/2,h/2)-h/2(dt/dx+dt/dy)
tign_middle=(t00+t01+t10+t11)/4
write(*,*)"tign_middle",tign_middle
write(*,*)"t00,t01,t10,t11",t00,t01,t10,t11
! since mesh_size is 1 we replace fd(1) and fd(2) by 1
dt_dx=(t10-t00+t11-t01)/2
dt_dy=(t01-t00+t11-t10)/2
write(*,*)"dt_dx,dt_dy",dt_dx,dt_dy
write(*,*)"dx,dy",dx,dy
u(1)=dt_dx
u(2)=dt_dy
u(3)=tign_middle-(dt_dx+dt_dy)/2
Write(*,*)"u=",u(1),u(2),u(3)
! integrate
u(1)=-u(1)/fuel_time_cell
u(2)=-u(2)/fuel_time_cell
u(3)=-u(3)/fuel_time_cell
write(*,*)"u/fuel_time_cell",u(1),u(2),u(3)