-
Notifications
You must be signed in to change notification settings - Fork 67
/
solve.f90
1657 lines (1419 loc) · 63.5 KB
/
solve.f90
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
module solve_routines
use precision_parameters
use conduction_routines, only: conduction
use convection_routines, only: convection
use debug_routines, only: output_spreadsheet_residuals
use exit_routines, only: cfastexit, delete_output_files
use fire_routines, only: fire, vent_jets, integrate_mass, update_species, collect_fire_data_for_smokeview, update_fire_ignition
use isosurface, only: output_isodata
use hflow_routines, only: wall_flow, leakage_flow
use mflow_routines, only: mechanical_flow
use numerics_routines, only : ddassl, jac, setderv, snsqe, gjac
use opening_fractions, only : get_vent_opening
use output_routines, only: output_results, output_status, output_debug, write_error_component
use radiation_routines, only: radiation
use smokeview_routines, only: output_smokeview, output_smokeview_header, output_smokeview_plot_data, output_slicedata
use spreadsheet_routines, only: output_spreadsheet, output_spreadsheet_smokeview
use target_routines, only: target, update_detectors, get_detector_temp_and_velocity
use utility_routines, only: mat2mult, interp, shellsort, cptime, get_filenumber
use vflow_routines, only: vertical_flow
use compartment_routines, only: layer_mixing, synchronize_species_mass, room_connections, wall_opening_fraction
use cfast_types, only: fire_type, ramp_type, room_type, target_type, vent_type
use cenviro, only: odevara, odevarb, odevarc, constvar, cp, rgas, gamma
use cparams, only: u, l, m, q, mxrooms, mxtarg, mxnode, mxbranch, mxdiscon, maxeq, ns, check_state, set_state, update_state, &
nwal, ns_mass, vminfrac, n2, o2, co2, co, h2o, w_from_room, w_to_room, w_from_wall, w_to_wall, w_boundary_condition, &
radiation_fix
use devc_data, only: n_detectors, n_targets, targetinfo, idset
use diag_data, only: radi_verification_flag, verification_time_step, upper_layer_thickness, dbtime, gas_temperature, &
partial_pressure_co2, partial_pressure_h2o, residfile, ioresid, residcsv, residfirst, residprn, ioslab, slabcsv, prnslab
use fire_data, only: n_fires, fireinfo, n_furn, furn_time, furn_temp, qfurnout
use option_data, only: option, mxopt, on, off, iprtalg, ovtime, tovtime, tottime, prttime, numjac, numstep, numresd, fpdassl, &
stptime, total_steps, fpsteady, foxygen, fdebug, fresidprn, fkeyeval
use ramp_data, only: n_ramps, rampinfo
use room_data, only: n_rooms, roominfo, n_cons, surface_connections, n_vcons, vertical_connections, &
exterior_ambient_temperature, exterior_abs_pressure, pressure_ref, pressure_offset, relative_humidity, iwbound
use setup_data, only: iofilo, iofill, initializeonly, stime, i_time_step, time_end, deltat, print_out_interval, &
smv_out_interval, ss_out_interval, nokbd, stopfile, queryfile, cfast_version, errormessage
use smkview_data, only: smv_room, smv_xfire, smv_yfire, smv_zfire, smv_relp, smv_zlay, smv_tu, smv_tl, smv_qdot, smv_height
use solver_data, only: maxteq, rpar2, ipar2, p, pold, pdold, pinit, told, dt, aptol, atol, rtol, rptol, awtol, rwtol, algtol, &
nofp, nequals, nofprd, nofwt, noftu, noftl, nofvu, nofoxyu, nofoxyl, ndisc, discon, stpmin, stpminflag, stpmin_cnt, &
stpmin_cnt_max, stpmax, stpfirst, jacdim, i_speciesmap, I_wallmap, stp_cnt_max
use vent_data, only: n_hvents, hventinfo, n_vvents, vventinfo, n_mvents, mventinfo
implicit none
external grabky
private
public solve_simulation, calculate_residuals, output_interactive_help, update_data
contains
! --------------------------- initial_solution -------------------------------------------
subroutine initial_solution(t,pdold,pdzero,rpar,ipar)
! determines an initial solution to the zone fire modeling equations. A non-linear
! algebraic solver (SNSQE) is used to calculate initial room pressures that make dP/dt zero. If an HVAC system
! is modeled then HVAC node pressures and hvac duct temperatures are also determined to force mass and energy conservation.
integer, intent(in) :: ipar(*)
real(eb), intent(in) :: t,pdzero(*), rpar(*)
real(eb), intent(out) :: pdold(*)
integer, parameter :: mxalg = 4*mxrooms+mxnode+mxbranch
real(eb) deltamv(mxalg), hhvp(mxalg)
integer, parameter :: lrwork = (3*mxalg**2+13*mxalg)/2
real(eb) :: work(lrwork)
integer :: ires, iopt, nalg1, nprint, i, info, n_odes
real(eb) :: tol
type(room_type), pointer :: roomptr
ires = 0
1 continue
call room_connections
rpar2(1) = rpar(1)
ipar2(1) = ipar(1)
ipar2(2) = ipar(2)
call setderv(-1)
call calculate_residuals(t,p,pdzero,pdold,ires,rpar2,ipar2)
iopt = 2
tol = algtol
nalg1 = n_rooms
nprint = -1
! room pressures
do i = 1, n_rooms
hhvp(i) = p(i+nofp)
end do
do i = 1, nequals
pinit(i) = p(i)
end do
if (option(fpsteady)==1) then
call snsqe(gres,gjac,iopt,nalg1,hhvp,deltamv,tol,nprint,info, work,lrwork)
else
info = 1
end if
! couldn't find a solution. either try to recover or stop
if (info/=1) then
if (option(fpsteady)/=off) then
option(fpsteady) = off
write(iofill, '(a)') '***Error in initial_solution: Trying non-steady initial guess.'
go to 1
end if
write(errormessage, '(a)') '***Error in initial_solution: Solver could not find an initial solution.'
call cfastexit('initial_solution',1)
end if
! if a room is not connected to any other room via a horizontal or
! vertical vent then do not use the snsqe pressure solution,
! use the original pressure solution that was based on rho*g*h.
do i = 1, n_rooms
roomptr => roominfo(i)
if (roomptr%is_connection) p(i+nofp) = hhvp(i)
end do
call calculate_residuals(t,p,pdzero,pdold,ires,rpar,ipar)
! Added to synchronize_species_mass the species mass with the total mass of each layer at the new pressure
n_odes = nofprd+1
call synchronize_species_mass (p,n_odes)
do i = 1, n_cons
pdold(i+nofwt) = 0.0_eb
end do
return
end subroutine initial_solution
! --------------------------- gres -------------------------------------------
subroutine gres (nnn,hvpsolv,deltamv,iflag)
! calculates residuals for initial solution by snsqe
! arguments: nnn
! hvpsolv
! deltamv
! iflag
integer, intent(in) :: nnn
real(eb), intent(in) :: hvpsolv(nnn)
integer, intent(out) :: iflag
real(eb), intent(out) :: deltamv(*)
integer :: nalg, i, ires
real(eb) :: p2(maxteq), delta(maxteq), pdzero(maxteq), t
type(room_type), pointer :: roomptr
data pdzero /maxteq*0.0_eb/
if (1.eq.2) iflag=-1 ! dummy statement to eliminate compiler warnings
nalg = n_rooms
do i = 1, nalg
p2(i) = hvpsolv(i)
end do
do i = nalg + 1, nequals
p2(i) = pinit(i)
end do
if (iprtalg/=0) then
write (iofilo,*) 'room pressures'
do i = 1, n_rooms
write (iofilo,*) i,p2(i)
end do
end if
t = stime
ires = 0
call calculate_residuals(t,p2,pdzero,delta,ires,rpar2,ipar2)
do i = 1, nalg
deltamv(i) = delta(i)
end do
do i = 1, n_rooms
roomptr => roominfo(i)
if (.not.roomptr%is_connection) deltamv(i) = 0.0_eb
end do
if (iprtalg/=0) then
write (iofilo,*)'room pressure residuals'
do i = 1, n_rooms
write (iofilo,*)i,delta(i)
end do
write (iofilo,*)' '
read (*,*)
end if
return
end subroutine gres
! --------------------------- solve_simulation -------------------------------------------
subroutine solve_simulation (tstop)
! main solution loop for the model
! Arguments: TSTOP The final time to which CFAST should run
! The structure of the solver array is
! NOFP = offset for the main pressure; the array of base pressures for each compartment
! NOFTU = upper layer temperature
! NOFVU = upper layer volume
! NOFTL = lower layer temperature
! NOFWT = wall surface temperatures (equivalent to the number of profiles)
! NOFPRD = species
! NEQUALS = last element in the array.
! The arrays which use this structure are VATOL, VRTOL, P, PDOLD, PPRIME and PDZERO
! An important note - solve sets the last variable to be solved to NOFPRD
! which is the beginning of the species (-1) and the end of the array which
! is presently used by DASSL. The important point is that N_ODES is set to
! NOFPRD
external post_process
real(eb), intent(in) :: tstop
integer, parameter :: maxord = 5
integer, parameter :: lrwork = 40+(maxord+4)*maxeq+maxeq**2
integer, parameter :: liw = 20+maxeq
integer, parameter :: all = 1, some = 0
real(eb) :: rwork(lrwork), rpar(1)
integer :: iwork(liw), info(15), ipar(3), info2(15)
real(eb) :: pprime(maxteq), pdnew(maxteq), vatol(maxeq), vrtol(maxeq)
real(eb) :: pdzero(maxteq) = 0.0_eb
logical :: iprint, ismv, exists, ispread,firstpassforsmokeview
integer :: idid, i, n_odes, nfires, icode, ieqmax, idisc, ires, idsave, ifdtect, ifobj, n
real(eb) :: ton, toff, tpaws, tstart, tdout, dprint, dplot, dspread, t, tprint, td, tsmv, tspread, tout, &
ostptime, tdtect, tobj
integer :: first_time
integer :: stopunit, ios
type(fire_type), pointer :: fireptr
call cptime(toff)
ires = 0
tpaws = tstop + 1.0_eb
tstart = i_time_step - 1
told = tstart
dt = tstop - tstart
dprint = abs(print_out_interval)
dplot = abs(smv_out_interval)
dspread = abs(ss_out_interval)
rpar(1) = rptol
! initialize print and output times
t = tstart
tprint = t
tsmv = t
tspread = t
idid = 1
firstpassforsmokeview = .true.
first_time = 1
! specific verification cases just do a calculation and stop then exit
if (radi_verification_flag) then
if (verification_time_step /= 0._eb) then
dt = verification_time_step
else
dt = tstop
end if
if (upper_layer_thickness /= -1001._eb) then
do while (t<=tstop)
call wall_opening_fraction(t)
call output_spreadsheet(t)
t = t + dt
end do
return
end if
end if
! Output options
if (dprint<0.0001_eb.or.print_out_interval==0) then
iprint = .false.
tprint = tstop + 1.0_eb
else
iprint = .true.
end if
if (smv_out_interval<=0) then
ismv = .false.
tsmv = tstop + 1.0_eb
else
ismv = .true.
end if
if (dspread<0.0001_eb.or.ss_out_interval<=0) then
ispread = .false.
tspread = tstop + 1.0_eb
else
ispread = .true.
end if
call set_info_flags (info, rwork)
! copy error tolerances into arrays. if the location of pressure is
! changed in the solver array then the following code has to be changed
do i = 1, n_rooms
vatol(i+nofp) = aptol
vrtol(i+nofp) = rptol
vatol(i+noftu) = atol
vrtol(i+noftu) = rtol
vatol(i+nofvu) = atol
vrtol(i+nofvu) = rtol
vatol(i+noftl) = atol
vrtol(i+noftl) = rtol
if (option(foxygen)==on) then
vatol(i+nofoxyu)=atol
vrtol(i+nofoxyu)=rtol
vatol(i+nofoxyl)=atol
vrtol(i+nofoxyl)=rtol
end if
end do
do i = 1, n_cons
vatol(i+nofwt) = awtol
vrtol(i+nofwt) = rwtol
end do
ovtime = 0.0_eb
tovtime = 0.0_eb
tottime = 0.0_eb
prttime = 0.0_eb
! Set number of equations solved by DASSL
n_odes = nofprd
ipar(1) = n_odes
ipar(2) = all
idset = 0
! construct initial solution
pdold(1:nequals) = 0.0_eb
pold(1:nequals) = p(1:nequals)
call initial_solution(t,pdold,pdzero,rpar,ipar)
pprime(1:nequals) = pdold(1:nequals)
pold(1:nequals) = p(1:nequals)
! Calculate the mass of objects that have been pyrolized
! at the moment we do only the total and the radiological species
! make sure that the INTEGRATE routine is called before update_species
call integrate_mass (dt)
call update_species (0.0_eb)
! If we are running only an initialization test then we do not need to solve anything
if (initializeonly) then
! normally, this only needs to be done while running. however, if we are doing an initialonly run
! then we need the output now
call collect_fire_data_for_smokeview (nfires)
call output_smokeview(pressure_ref, exterior_abs_pressure, exterior_ambient_temperature, n_rooms, &
nfires, smv_room, smv_xfire, smv_yfire, smv_zfire, 0.0_eb, 1)
icode = 0
write (*, '(a)') 'Initialize only'
write (iofill, '(a)') 'Initialize only'
return
end if
! main solve loop
numjac = 0
numstep = 0
numresd = 0
do while (idid>=0 .and. t+0.000001_eb<=tstop)
! DASSL equation with most error
ieqmax = 0
! Check for interactive commands
! if a key has been pressed (and we are watching the keyboard) figure out what to do
! The escape key returns a code of 1
if (.not.nokbd) call keyboard_interaction (t,icode,tpaws,tout,ieqmax)
! Check for stop file that overrides maximum iteration count
inquire (file=stopfile, exist=exists)
icode = 0
n = stp_cnt_max
if (exists) then
stopunit = get_filenumber()
open (stopunit,file=stopfile)
read (stopunit,*,iostat=ios) n
if (ios==0) then
stp_cnt_max = n
else
stp_cnt_max = 0
icode = 1
end if
close(unit=stopunit)
end if
! If the stop file exists or the esc key has been pressed, then quit
if (icode==1.and.stp_cnt_max.eq.0) then
call delete_output_files (stopfile)
write (*,'(a,1pg11.3,a,g11.3)') 'Stopped by request at T = ', t, ' DT = ', dt
write (iofill,'(a,1pg11.3,a,g11.3)') 'Stopped by request at T = ', t, ' DT = ', dt
return
end if
! Check the .query file. If it does not exist, do nothing. If if DOES exist, then
! rewind/write the status file and delete the query file (in that order).
! Ignore errors from deleting the file. It may not exist
inquire (file=queryfile, exist = exists)
if (exists) then
call output_status (t, dt)
call delete_output_files (queryfile)
end if
!Check to see if diagnostic files .resid and .jac exist. If they do exist
!set flags and open file, if needed, to print diagnositic information.
inquire (file=residfile, exist=exists)
if (exists .or. option(fresidprn) == on) then
residprn = .true.
if (residfirst) then
residfirst = .false.
ioresid = get_filenumber()
open (ioresid,file=residcsv)
ioslab = get_filenumber()
open (ioslab, file=slabcsv)
end if
else
residprn = .false.
end if
! now do normal output (printout, spreadsheets, ...)
if (idid>0) then
! printed output
if (t+0.0001_eb>min(tprint,tstop).and.iprint) then
i_time_step = tprint
call output_results (t)
call output_status (t, dt)
tprint = tprint + dprint
numjac = 0
numstep = 0
numresd = 0
prttime = 0.0_eb
end if
! smokeview output
if (t+0.0001_eb>min(tsmv,tstop).and.ismv) then
i_time_step = tsmv
! collect_fire_data_for_smokeview just puts all of the fire information in a single list
call collect_fire_data_for_smokeview (nfires)
if (firstpassforsmokeview) then
firstpassforsmokeview = .false.
! note: output_smokeview writes the .smv file. we do not close the file but only rewind so that smokeview
! can have the latest time step information.
call output_smokeview (pressure_ref, exterior_abs_pressure, exterior_ambient_temperature, n_rooms, &
nfires, smv_room, smv_xfire, smv_yfire, smv_zfire, t, i_time_step)
call output_smokeview_header (cfast_version,n_rooms,nfires)
end if
! Using the absolute room pressure minus the absolute ref pressure makes sure that the relp values going to
! smokeview are what is expected.
smv_relp(1:n_rooms) = roominfo(1:n_rooms)%absp - pressure_ref
smv_zlay(1:n_rooms) = roominfo(1:n_rooms)%depth(l)
smv_tu(1:n_rooms) = roominfo(1:n_rooms)%temp(u)
smv_tl(1:n_rooms) = roominfo(1:n_rooms)%temp(l)
call output_smokeview_plot_data(t,n_rooms,smv_relp,smv_zlay,smv_tl,smv_tu,nfires, smv_qdot,smv_height)
call output_spreadsheet_smokeview(t)
tsmv = tsmv + dplot
call output_status (t, dt)
call output_slicedata(t,first_time)
call output_isodata(t,first_time)
first_time = 0
end if
! spreadsheet output
if (t+0.0001_eb>min(tspread,tstop).and.ispread) then
call output_spreadsheet(t)
i_time_step = tspread
tspread =tspread + dspread
call output_status (t, dt)
! reset incremental FED data
targetinfo(1:mxtarg)%dfed_gas = 0.0_eb
targetinfo(1:mxtarg)%dfed_heat = 0.0_eb
end if
! diagnostic output
if (t+0.0001_eb>tpaws) then
i_time_step = tpaws
call output_results (t)
call output_debug (1,t,dt,ieqmax)
tpaws = tstop + 1.0_eb
call output_status (t, dt)
end if
! find the interval next discontinuity is in
idisc = 0
do i = 1, ndisc
if (t>=discon(i-1).and.t<discon(i)) then
idisc = i
exit
end if
end do
tout = min(tprint, tsmv, tspread, tpaws, tstop)
! if there is a discontinuity then tell DASSL
if (idisc/=0) then
tout = min(tout,discon(idisc))
rwork(1) = discon(idisc)
info(4) = 1
else
info(4) = 0
end if
end if
! Special case for radiation verification
if (radi_verification_flag) then
t = tstop
call target (1,t)
call output_spreadsheet(t)
return
end if
if (t<tstop) then
idset = 0
ipar(2) = some
told = t
call setderv(-1)
call cptime(ton)
call ddassl (calculate_residuals,n_odes,t,p,pprime,tout,info,vrtol,vatol,idid,rwork,lrwork,iwork,liw,rpar,ipar,jac)
! call cpu timer and measure, solver time within dassl and overhead time (everything else).
call setderv(-2)
ieqmax = ipar(3)
if (option(fpdassl)==on) call output_debug (3,t,dt,ieqmax)
ostptime = ton - toff
call cptime(toff)
stime = t
stptime = toff - ton
prttime = prttime + stptime
tottime = tottime + stptime
ovtime = ovtime + ostptime
tovtime = tovtime + ostptime
! make sure dassl is happy
if (idid<0) then
call write_error_component (ieqmax)
write (*,'(a,i0)') '***Error, dassl - idid = ', idid
write (iofill,'(a,i0)') '***Error, dassl - idid = ', idid
call post_process
stop
end if
dt = t - told
if (stpminflag) then
if (dt<stpmin) then
stpmin_cnt = stpmin_cnt + 1
if (stpmin_cnt>stpmin_cnt_max) then
! model has hung (stpmin_cnt_max consective time step sizes were below stpmin)
write (*,'(a,i0,a,e11.4,a,e11.4)') &
'***Error, ', stpmin_cnt_max, 'Consecutive time steps with size below ', stpmin, ' at t = ', t
write (iofill,'(a,i0,a,e11.4,a,e11.4)') &
'***Error, ', stpmin_cnt_max, 'Consecutive time steps with size below ', stpmin, ' at t = ', t
stop
end if
else
! this time step is above the critical size so reset counter
stpmin_cnt = 0
end if
end if
ipar(2) = all
call calculate_residuals (t,p,pdzero,pdnew,ires,rpar,ipar)
call update_solution (n_odes, nequals, t, told, p, pold, pdnew, pdold)
! advance the detector temperature solutions and check for object ignition
idsave = 0
call get_detector_temp_and_velocity
call update_detectors (check_state,told,dt,n_detectors,idset,ifdtect,tdtect)
call update_fire_ignition (check_state,told,dt,ifobj,tobj)
td = min(tdtect,tobj)
! a detector is the first one that went off
if (ifdtect>0.and.tdtect<=td) then
call update_detectors (set_state,told,dt,n_detectors,idset,ifdtect,tdtect)
idsave = ifobj
td = tobj
call calculate_residuals (t, p, pdzero, pdnew, ires, rpar, ipar)
idset = 0
else
call update_detectors (update_state,told,dt,n_detectors,idset,ifdtect,tdtect)
end if
! object ignition is the first thing to happen
if (ifobj>0.and.ifobj <=n_fires.and.tobj<=td) then
fireptr => fireinfo(ifobj)
call update_fire_ignition (set_state,told,dt,ifobj,tobj)
idsave = idset
td = tdtect
fireptr%ignited = .true.
call set_info_flags(info,rwork)
ifobj = 0
else
call update_fire_ignition (update_state,told,dt,ifobj,tobj)
end if
if (idsave/=0) then
! a detector has activated so call dassl to integrate backwards
! in time to t=td. this is better than using simple linear interpolation
! because in general dassl could be taking very big time steps
if (told<=td.and.td<t) then
call output_results (t)
ipar(2) = some
tdout = td
do i = 1, 11
info2(i) = 0
end do
info2(2) = 1
told = t
call ddassl (calculate_residuals, &
n_odes,t,p,pprime,tdout,info2,vrtol,vatol,idid,rwork,lrwork,iwork,liw,rpar,ipar,jac)
! make sure dassl is happy (again)
if (idid<0) then
call write_error_component (ipar(3))
write (*,'(a,i0)') '***Error, dassl - idid = ', idid
write (*,'(a,f10.5,1x,a,f10.5)') '***Error, Problem in DASSL backing from ',t,'to time ',tdout
write (iofill,'(a,i0)') '***Error, dassl - idid = ', idid
write (iofill,'(a,f10.5,1x,a,f10.5)') '***Error, Problem in DASSL backing from ',t,'to time ',tdout
call post_process
write (errormessage,'(a)') '***Error, Equation solver could not find a solution.'
call cfastexit ('solve_simulation', 3)
stop
end if
! reset dassl flags to integrate forward from t=td and
! call calculate_residuals to get product info at sprinkler activation time
if (ifdtect>0) idset = idsave
dt = t - told
ipar(2) = all
! call calculate_residuals to get product info at the correct time and
! to save fire release rates in room where detector has
! activated. (this happens because idset /= 0)
call calculate_residuals (t, p, pdzero, pdnew, ires, rpar, ipar)
call update_solution (n_odes, nequals, t, told, p, pold, pdnew, pdold)
call set_info_flags (info,rwork)
else if (td==t) then
call set_info_flags (info,rwork)
call calculate_residuals (t, p, pdzero, pdnew, ires, rpar, ipar)
else
! update_detectors said that a sprinkler has gone off but the time is wrong!!
write (*,'(a,f10.5,a,f10.5,a,f10.5)') '***Error, Back step too large in DASSL, Time = ', &
t,' Last time = ',told,' need to back step to ',td
write (iofill,'(a,f10.5,a,f10.5,a,f10.5)') '***Error, Back step too large in DASSL, Time = ', &
t,' Last time = ',told,' need to back step to ',td
write (errormessage,'(a)') '***Error, Equation solver could not find a solution.'
call cfastexit ('solve_simulation', 4)
stop
end if
end if
! calculate the mass of objects that have been pyrolized
! at the moment we do only the total and the radiological species
! It is important to call the routine to integrate the mass before call the toxicology calculation
call integrate_mass (dt)
! calculate gas dosage
call update_species (dt)
if (option(fdebug)==on) call output_debug (2,t,dt,ieqmax)
numstep = numstep + 1
total_steps = total_steps + 1
if (stp_cnt_max>=0.and.total_steps>stp_cnt_max) then
call delete_output_files (stopfile)
write (errormessage,'(a,1pg11.3,a,g11.3)') 'Stopped by user request at T = ', t, ' DT = ', dt
call cfastexit ('solve_simulation', 5)
end if
end if
end do
return
end subroutine solve_simulation
! --------------------------- update_solution -------------------------------------------
subroutine update_solution(n_odes, nequals, t, told, p, pold, pdnew, pdold)
! update solution returned by dassl
integer, intent(in) :: n_odes, nequals
real(eb), intent(in) :: t, told, pdnew(*)
real(eb), intent(inout) :: p(*), pdold(*)
real(eb), intent(out) :: pold(*)
integer :: i
real(eb) :: dt
dt = t - told
! advance species
do i = n_odes + 1, nequals
p(i) = p(i) + dt*pdold(i)
p(i) = max (0.0_eb, p(i))
pdold(i) = pdnew(i)
end do
! advance target temperatures
call target (1,dt)
! make sure species mass adds up to total mass
if (ns>0) call synchronize_species_mass (p,n_odes+1)
pold(1:nequals) = p(1:nequals)
return
end subroutine update_solution
! --------------------------- keyboard_interaction -------------------------------------------
subroutine keyboard_interaction (t,icode,tpaws,tout,ieqmax)
! keyboard routine for user interaction during simulation
integer, intent(in) :: ieqmax
real(eb), intent(in) :: t
integer, intent(out) :: icode
real(eb), intent(out) :: tpaws
real(eb), intent(inout) :: tout
integer(2) :: ch, hit
real(eb) :: rcode
icode = 0
call grabky(ch,hit)
if (hit>0) then
if (ch==27) then
icode = 1
return
else if (hit>1) then
if (option(fkeyeval)==on) then
if (ch==59) then
write (*,5010) t, dt
if (output_interactive_help ()) icode = 1
else if (ch==60) then
if (option(fdebug)==on) then
option(fdebug) = off
write (*,*) 'debug is now off'
write (*,*)
else
option(fdebug) = on
end if
else if (ch==62) then
call output_debug(1,t,dt,ieqmax)
else if (ch==63) then
write (*,5010) t, dt
else if (ch==64) then
write (*,5010) t, dt
write (*,*) 'enter time at which to pause: '
read (*,*) rcode
tpaws = rcode
tout = min(tpaws,tout)
else if (ch==65) then
if (option(fpdassl)==on) then
option(fpdassl) = off
write (*,*) 'dassl debug is now off'
else
option(fpdassl) = on
end if
end if
else
write (*,5010) t, dt
end if
end if
end if
return
5010 format (' time = ',1pg12.4,', dt = ',1pg12.4)
end subroutine keyboard_interaction
! --------------------------- output_interactive_help -------------------------------------------
logical function output_interactive_help()
! quick output of keyboard shortcuts available during simulaiton
integer(2) :: ch, hit
integer :: ii
write (iofilo,*) '***Options Set***'
write (iofilo,'(1x,20i3)') (option(ii),ii = 1,mxopt)
write (iofilo,*) '************************************************************'
write (iofilo,*) '1=Help,2=debug,3=flow,4=pause,5=time,6=pause time,7=dassl(t)'
write (iofilo,*) 'Press <esc> to quit, any other key to continue'
write (iofilo,*) '************************************************************'
10 call grabky(ch,hit)
if (hit==0) go to 10
if (ch==27) then
output_interactive_help = .true.
write (iofilo,*) 'Run terminated at user request'
else
output_interactive_help = .false.
write (iofilo,*) 'continuing'
write (iofilo,*)
end if
return
end function output_interactive_help
! --------------------------- set_info_flags -------------------------------------------
subroutine set_info_flags (info,rwork)
! update solution flags for dassl solver
integer, intent(out) :: info(*)
real(eb), intent(out) :: rwork(*)
info(1:11) = 0
info(3) = 1
info(2) = 1
if (stpmax<=0.0_eb) then
info(7) = 0
else
info(7) = 1
rwork(2) = stpmax
end if
if (stpfirst<0.0_eb) then
info(8) = 0
else
info(8) = 1
rwork(3) = stpfirst
end if
! setting jacobian flag
info(5) = 0
info(11) = 1
return
end subroutine set_info_flags
! --------------------------- calculate_residuals (resid) -------------------------------------------
subroutine calculate_residuals (tsec,y_vector,yprime_vector,f_vector,ires,rpar,ipar)
! Calculates the residual F(t,y,dy/dt) for CFAST differential and algebraic equations.
! For the gas differential equations (pressure, layer volume, upper/lower layer temperature) F(t,y,dy/dt) takes
! the form F(t,y,dy/dt) = dy/dt - f(t,y) where f(t,y) is related to the conservation of mass and and energy.
! For the wall temperature equations, F is just Fourier's law taking the form of
! F(t,y,dy/dt) = q''(t,y) + K dT/dx
! where q'' is the flux striking the wall, K is the wall's thermal conductivity and dT/dx is the surface wall
! temperature gradient.
! arguments: tsec Current simulation time (T above in s)
! y_vector Current guess at solution vector (Y above)
! yprime_vector Current guess at derivative of solution vector (Y' above)
! f_vector Residual or value of F(t,y,dy/dt)
! ires
! outputs ires Integer flag which is always equal to zero on input. calculate_residuals should alter IRES
! only if it encounters an illegal value of Y or a stop condition. Set IRES = -1 if an input
! value is illegal, and DDASSL will try to solve the problem without getting IRES = -1. If
! IRES = -2, DASSL return control to the calling program with IDID = -11.
! rpar real parameter arrays
! ipar integer parameter arrays
! These are used for communication between solve_simulation and
! calculate_residuals via DASSL. They are not altered by DASSL.
! Currently, only IPAR is used in calculate_residuals to pass
! a partial/total flag for solution of the species equations.
real(eb), intent(in) :: tsec, y_vector(*), yprime_vector(*), rpar(*)
integer, intent(in) :: ipar(*)
integer, intent(inout) :: ires
real(eb), intent(out) :: f_vector(*)
integer, parameter :: all = 1, some = 0
! data structures for dassl, the numerical solver
real(eb) :: yhatprime_vector(maxteq)
! data structures for rooms
type(room_type), pointer :: roomptr
! data structure for total flows and fluxes
real(eb) :: flows_total(mxrooms,ns+2,2), fluxes_total(mxrooms,nwal)
! data structures for flow through vents
real(eb) :: flows_hvents(mxrooms,ns+2,2)
real(eb) :: flows_leaks(mxrooms,ns+2,2)
real(eb) :: flows_vvents(mxrooms,ns+2,2)
real(eb) :: flows_mvents(mxrooms,ns+2,2), filtered(mxrooms,ns+2,2)
! data structures for fires
real(eb) :: flows_fires(mxrooms,ns+2,2)
real(eb) :: flows_doorjets(mxrooms,ns+2,2)
integer :: update
! data structures for convection, radiation, and ceiling jets
real(eb) :: flows_convection(mxrooms,2), fluxes_convection(mxrooms,nwal)
real(eb) :: flows_radiation(mxrooms,2), fluxes_radiation(mxrooms,nwal)
! data structures for heat and mass transfer between layers
real(eb) :: flows_layer_mixing(mxrooms, ns+2, 2)
logical :: djetflg
integer :: nprod, i, iroom, iprod, ip, iwall, nprodsv, iprodu, iprodl
real(eb) :: epsp, aroom, hceil, pabs, hinter, ql, qu, tmu, tml
real(eb) :: oxydu, oxydl, pdot, tlaydu, tlaydl, vlayd, prodl, produ
ires = ires ! just to get rid of a warning message
nprod = ns
dt = tsec - told
numresd = numresd + 1
stime = tsec
call update_data (y_vector,odevara)
call update_data (y_vector,odevarb)
! If calculate_residuals is called by solve_simulation then IPAR(2)==ALL all residuals
! are computed. If calculate_residuals is called by DASSL residuals are not
! computed for species. Further, temperature profiles are only
! updated when calculate_residuals is called by solve_simulation.
if (ipar(2)==some) then
update = 0
prnslab = .false.
else
update = 1
if (residprn) then
prnslab = .true.
else
prnslab = .false.
end if
dbtime = tsec
end if
epsp = rpar(1)
! calculate flow due to unforced vents (wall_flow for doors/windows
! and vertical_flow for ceiling/floor vents
call wall_flow (tsec,epsp,flows_hvents)
call leakage_flow (epsp,flows_leaks)
call vertical_flow (tsec,epsp,flows_vvents)
call mechanical_flow (tsec,epsp,flows_mvents,filtered)
! calculate heat and mass flows due to fires
call fire (tsec,flows_fires)
call vent_jets (flows_doorjets,djetflg)
! calculation opening fraction for radiation loss and etc
call wall_opening_fraction (tsec)
! calculate flow and flux due to heat transfer (ceiling jets, convection and radiation)
call convection (flows_convection,fluxes_convection)
call radiation (flows_radiation,fluxes_radiation)
call layer_mixing(flows_layer_mixing)
! sum flow for inside rooms
do iroom = 1, n_rooms
roomptr => roominfo(iroom)
do iprod = 1, nprod + 2
ip = i_speciesmap(iprod)
flows_total(iroom,iprod,l) = flows_hvents(iroom,iprod,l) + flows_leaks(iroom,iprod,l) + flows_fires(iroom,ip,l)
flows_total(iroom,iprod,u) = flows_hvents(iroom,iprod,u) + flows_leaks(iroom,iprod,u) + flows_fires(iroom,ip,u)
end do
do iprod = 1, nprod + 2
ip = i_speciesmap(iprod)
flows_total(iroom,iprod,l) = flows_total(iroom,iprod,l) + flows_vvents(iroom,ip,l)
flows_total(iroom,iprod,u) = flows_total(iroom,iprod,u) + flows_vvents(iroom,ip,u)
end do
do iprod = 1, nprod + 2
ip = i_speciesmap(iprod)
flows_total(iroom,iprod,l) = flows_total(iroom,iprod,l) + flows_mvents(iroom,ip,l) - filtered(iroom,iprod,l)
flows_total(iroom,iprod,u) = flows_total(iroom,iprod,u) + flows_mvents(iroom,ip,u) - filtered(iroom,iprod,u)
end do
do iprod = 1, nprod + 2
ip = i_speciesmap(iprod)
flows_total(iroom,iprod,l) = flows_total(iroom,iprod,l) + flows_layer_mixing(iroom,iprod,l)
flows_total(iroom,iprod,u) = flows_total(iroom,iprod,u) + flows_layer_mixing(iroom,iprod,u)
end do
if (djetflg) then
do iprod = 1, nprod + 2
ip = i_speciesmap(iprod)
flows_total(iroom,iprod,l) = flows_total(iroom,iprod,l) + flows_doorjets(iroom,ip,l)
flows_total(iroom,iprod,u) = flows_total(iroom,iprod,u) + flows_doorjets(iroom,ip,u)
end do
end if
flows_total(iroom,q,l) = flows_total(iroom,q,l) + flows_convection(iroom,l) + flows_radiation(iroom,l)
flows_total(iroom,q,u) = flows_total(iroom,q,u) + flows_convection(iroom,u) + flows_radiation(iroom,u)
! if this room is a shaft then solve for only one zone.
! this is done by combining flows from to both
! layers into upper layer flow and setting lower layer flow to
! zero.
if (roomptr%shaft) then
do iprod = 1, nprod + 2
flows_total(iroom,iprod,u) = flows_total(iroom,iprod,u) + flows_total(iroom,iprod,l)