-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubroutines.f03
More file actions
4531 lines (4135 loc) · 184 KB
/
subroutines.f03
File metadata and controls
4531 lines (4135 loc) · 184 KB
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 subroutines
USE parameters
USE typedefs
USE functiondefs
USE mc_toolbox
USE specdata
CONTAINS
! *********************************************************
! ******* SUBROUTINES *************************************
! *********************************************************
SUBROUTINE lookup(string, nlines, array, n)
!
! Purpose:
! This is a subroutine that compares a string value to values
! in a list and gives the index of a matching result and an
! error if there is no match.
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! LOOKUP !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
IMPLICIT NONE
!*****************
! Input and output
!*****************
INTEGER , INTENT(IN) :: nlines
INTEGER , INTENT(OUT) :: n
CHARACTER(*) , INTENT(IN) :: string
CHARACTER(len=10) , INTENT(IN), DIMENSION(nlines) :: array
!****************
! Local variables
!****************
INTEGER(KIND=SHORT) :: i
CHARACTER(len=10) , DIMENSION(1) :: string_arr
! Go through the array and compare the supplied string with the
! strings in the array
n = 0
string_arr = (/ string /)
DO i=1,nlines
IF ( TRIM(string_arr(1)) .EQ. TRIM(array(i)) )THEN
n=i
END IF
END DO
END SUBROUTINE lookup
SUBROUTINE linecount(unitnum, errcode, lines, header_num)
!
! Purpose:
! This subroutine simply counts the number of lines in a file.
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! LINECOUNT !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
IMPLICIT NONE
!*****************
! Input and output
!*****************
INTEGER , INTENT(IN) :: unitnum
INTEGER , INTENT(OUT) :: errcode
INTEGER(KIND=SHORT), INTENT(OUT) :: lines
INTEGER(KIND=SHORT) :: adv
CHARACTER(LEN=100) :: line
INTEGER :: header_num
adv = 1
lines = 0
header_num = 0
DO
READ(unitnum,*,IOSTAT=errcode) line
! PRINT *, line
IF ( errcode .NE. 0 ) EXIT
IF ( line(1:1) .EQ. '!' ) header_num = header_num + 1
lines = lines + adv
END DO
REWIND(unitnum)
END SUBROUTINE linecount
SUBROUTINE hopping ( i_in, j_in, k_in, i_out, j_out, k_out, prob, dimens )
!
! Purpose:
! The purpose of this is to move a species from one site to another.
! It can move fr/ba/le/ri and up/down. Periodic boundary conditions are in
! place such that lateral motion moves to the other side of the lattice if
! it goes "overboard"
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! HOPPING !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
IMPLICIT NONE
!*****************
! Input and output
!*****************
INTEGER , INTENT(IN) :: i_in, j_in, k_in
INTEGER , INTENT(OUT) :: i_out, j_out, k_out
INTEGER , INTENT(IN) :: prob
INTEGER , INTENT(IN), DIMENSION(3) :: dimens !dimension of matrix
! Determine the direction of travel based on input number
! note that the second and third indices, j and k, are
! incremented by +- 2
SELECT CASE (prob)
CASE (1)
! hop back => k-2
IF ( k_in .EQ. 1 .OR. k_in .EQ. 2 ) THEN
IF ( MOD(dimens(3),2) .EQ. 1 ) THEN
IF ( k_in .EQ. 1 ) k_out = dimens(3)
IF ( k_in .EQ. 2 ) k_out = dimens(3)-1
ELSE
IF ( k_in .EQ. 1 ) k_out = dimens(3)-1
IF ( k_in .EQ. 2 ) k_out = dimens(3)
END IF
ELSE
k_out = k_in-2
END IF
i_out = i_in
j_out = j_in
CASE (2)
! hop forward => k+2
IF ( k_in .EQ. dimens(3) .OR. k_in .EQ. dimens(3)-1 ) THEN
IF ( MOD(dimens(3),2) .EQ. 1 ) THEN
IF ( k_in .EQ. dimens(3) ) k_out = 1
IF ( k_in .EQ. dimens(3)-1 ) k_out = 2
ELSE
IF ( k_in .EQ. dimens(3) ) k_out = 2
IF ( k_in .EQ. dimens(3)-1) k_out = 1
END IF
ELSE
k_out = k_in + 2
END IF
i_out = i_in
j_out = j_in
CASE (3)
! hop left => j-2
IF ( j_in .EQ. 1 .OR. j_in .EQ. 2 ) THEN
IF ( MOD(dimens(2),2) .EQ. 1 ) THEN
IF ( j_in .EQ. 1 ) j_out = dimens(2)
IF ( j_in .EQ. 2 ) j_out = dimens(2)-1
ELSE
IF ( j_in .EQ. 1 ) j_out = dimens(2)-1
IF ( j_in .EQ. 2 ) j_out = dimens(2)
END IF
ELSE
j_out = j_in-2
END IF
i_out = i_in
k_out = k_in
CASE (4)
! hop right => j+2
IF ( j_in .EQ. dimens(2) .OR. j_in .EQ. dimens(2)-1 ) THEN
IF ( MOD(dimens(2),2) .EQ. 1 ) THEN
IF ( j_in .EQ. dimens(2) ) j_out = 1
IF ( j_in .EQ. dimens(2)-1) j_out = 2
ELSE
IF ( j_in .EQ. dimens(2)) j_out = 2
IF ( j_in .EQ. dimens(2)-1) j_out = 1
END IF
ELSE
j_out = j_in+2
END IF
i_out = i_in
k_out = k_in
CASE (5)
! hop down => i+1
! Hopping to the monolayer above or below the current
! one involves +- 1 to the first dimension (i)
IF ( i_in .EQ. dimens(1) ) THEN
i_out = i_in
ELSE
i_out = i_in+1
END IF
j_out = j_in
k_out = k_in
CASE (6)
! hop up => i-1
IF ( i_in .EQ. 1 ) THEN
i_out = i_in
ELSE
i_out = i_in-1
END IF
j_out = j_in
k_out = k_in
END SELECT
END SUBROUTINE hopping
SUBROUTINE lookaroundyou ( react_cube, matrix, coords, null, small_count, &
large_count, small_arr, large_arr, wait_list )
!
! Purpose:
! This subroutine is designed to look at the surrounding spaces in a
! matrix and determine if any of them are possible co-reactants for
! any reactions in the network used. If there is a match, null=0,
! if there is no match found, i.e. no reacting partners, null=1.
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! LOOKAROUNDYOU !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
IMPLICIT NONE
!*****************
! Input and output
!*****************
INTEGER(KIND=SHORT), INTENT(OUT) :: null
INTEGER , INTENT(OUT) :: small_count
INTEGER , INTENT(OUT) :: large_count
INTEGER(KIND=SHORT) , DIMENSION(:,:,:), POINTER :: react_cube
INTEGER , DIMENSION(:,:,:), POINTER :: matrix
INTEGER , INTENT(OUT), DIMENSION(6,4) :: large_arr
INTEGER , INTENT(OUT), DIMENSION(4,4) :: small_arr
INTEGER , INTENT(IN) , DIMENSION(3) :: coords
INTEGER , DIMENSION(3) :: new_coords
TYPE(wait_info) , DIMENSION(:) , POINTER :: wait_list
!****************
! Local variables
!****************
INTEGER :: r1, r2
INTEGER :: i_re,j_re,k_re
INTEGER :: i_re2,j_re2,k_re2
INTEGER :: n
INTEGER , DIMENSION(3) :: dimens
! Initialize dimensions
dimens(1) = SIZE(matrix,1)
dimens(2) = SIZE(matrix,2)
dimens(3) = SIZE(matrix,3)
! Initialize coordinates
i_re = coords(1)
j_re = coords(2)
k_re = coords(3)
! Initialize counters
large_count = 0
small_count = 0
! Initialize arrays
large_arr = 0
small_arr = 0
! Make sure the reactant isn't a zero
! PRINT *, "In lookaroundyou, the dimensions of the matrix are:"
! PRINT *, dimens
! PRINT *, "The value of coords is:"
! PRINT *, coords
! PRINT *, "The value of matrix in lookaroundyou is:",matrix(i_re,j_re,k_re)
! IF ( matrix(i_re,j_re,k_re) .EQ. 0 ) THEN
! PRINT *, "***************************************"
! PRINT *, "ERROR in lookaroundyou at matrix lookup"
! PRINT *, " Product is zero"
! PRINT *, "***************************************"
! END IF
! If the site is on the top or bottom layers, limit the hopping
n=0
initial_loop: DO n=1,6
top_bottom: IF ( i_re .EQ. 1 .AND. ( n .EQ. 5 .OR. n .EQ. 6 ) ) THEN
! If on top layer, stay on top layer
CONTINUE
ELSE IF ( i_re .EQ. dimens(1) .AND. n .EQ. 6 ) THEN
! Don't hop down if on bottom layer
CONTINUE
ELSE
CALL hopping(i_re,j_re,k_re,i_re2,j_re2,k_re2,n,dimens)
IF ( DEBUG .EQV. .TRUE. ) THEN
PRINT *, 'i_re,j_re,k_re=',i_re,j_re,k_re
PRINT *, 'i_re2,j_re2,k_re2=',i_re2,j_re2,k_re2
END IF
new_coords(1) = i_re2
new_coords(2) = j_re2
new_coords(3) = k_re2
IF ( matrix(i_re2,j_re2,k_re2) .NE. 0 ) THEN
IF ( ALL(coords .EQ. new_coords) .EQV. .FALSE.) THEN
! Determine if the hopped to species can react with the hopping species
r1 = matrix(i_re,j_re,k_re)
r2 = matrix(i_re2,j_re2,k_re2)
CALL canreact(r1,r2,react_cube,wait_list,null)
! Determine if site is occupied and if species can react
IF ( null .EQ. 0 ) THEN
large_arr(n,4) = 1
large_count = large_count + 1
ELSE
large_arr(n,4) = 0
END IF
large_arr(n,1)=i_re2
large_arr(n,2)=j_re2
large_arr(n,3)=k_re2
END IF
END IF
END IF top_bottom
END DO initial_loop
! Only look at normal sites if there are no interstitial reactants
IF ( large_count .GT. 0 ) THEN
CONTINUE
ELSE
DO n=1,4 ! Go to a phantom position
IF ( n .EQ. 1 ) THEN
IF ( j_re-1 .GT. 0 .AND. &
k_re-1 .GT. 0 .AND. &
k_re+1 .LE. dimens(3) ) THEN
CALL hopping(i_re,j_re-1,k_re+1,i_re2,j_re2,k_re2,1,dimens)
ELSE
i_re2 = i_re
j_re2 = 1
k_re2 = 2
END IF
ELSE IF ( n .EQ. 2 ) THEN
IF ( j_re-1 .GT. 0 .AND. &
k_re-1 .GT. 0 .AND. &
k_re+1 .LE. dimens(3) ) THEN
CALL hopping(i_re,j_re-1,k_re-1,i_re2,j_re2,k_re2,2,dimens)
ELSE
i_re2 = i_re
j_re2 = 1
k_re2 = 2
END IF
ELSE IF ( n .EQ. 3 ) THEN
IF ( j_re+1 .LE. dimens(2) .AND. &
k_re-1 .GT. 0 .AND. &
k_re+1 .LE. dimens(3) ) THEN
CALL hopping(i_re,j_re+1,k_re+1,i_re2,j_re2,k_re2,1,dimens)
ELSE
i_re2 = i_re
j_re2 = 1
k_re2 = 2
END IF
ELSE
IF ( j_re+1 .LE. dimens(2) .AND. &
k_re-1 .GT. 0 .AND. &
k_re+1 .LE. dimens(3) ) THEN
CALL hopping(i_re,j_re+1,k_re-1,i_re2,j_re2,k_re2,2,dimens)
ELSE
i_re2 = i_re
j_re2 = 1
k_re2 = 2
END IF
END IF
! Determine if the matrix site is occupied and can react
new_coords(1) = i_re2
new_coords(2) = j_re2
new_coords(3) = k_re2
IF ( matrix(i_re2,j_re2,k_re2) .NE. 0 ) THEN
IF ( ALL(coords .EQ. new_coords) .EQV. .FALSE. ) THEN
r1 = matrix(i_re,j_re,k_re)
r2 = matrix(i_re2,j_re2,k_re2)
! PRINT *, 'r1=',r1,'and r2=',r2
CALL canreact(r1,r2,react_cube,wait_list,null)
IF ( null .EQ. 0 ) THEN
small_arr(n,4) = 1
small_count = small_count + 1
ELSE
small_arr(n,4) = 0
END IF
small_arr(n,1)=i_re2
small_arr(n,2)=j_re2
small_arr(n,3)=k_re2
ELSE
small_arr(n,1) = i_re
small_arr(n,2) = j_re
small_arr(n,3) = k_re
small_arr(n,4) = 0
END IF
END IF
END DO
END IF
! Determine if there has been a null event
IF ( large_count .EQ. 0 .AND. small_count .EQ. 0 ) THEN
null = 1
ELSE
null = 0
END IF
END SUBROUTINE lookaroundyou
SUBROUTINE solarlottery( small_count,large_count, small_temp,large_temp, coords )
!
! Purpose:
! This subroutine looks at either the interstitial or normal neighbors
! that contain a potential reacion partner and chooses one at random.
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! SOLARLOTTERY !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
IMPLICIT NONE
!*****************
! Input and output
!*****************
INTEGER , INTENT(IN) :: large_count !number of interstitial reactants
INTEGER , INTENT(IN) :: small_count !number of normal reactants
INTEGER , INTENT(OUT), DIMENSION(3) :: coords !coordinates of selected site
INTEGER , DIMENSION(6,4) :: large_temp !array of interstitial neighbor coords
INTEGER , DIMENSION(4,4) :: small_temp !array of normal neighbor coords
!****************
! Local variables
!****************
INTEGER , ALLOCATABLE, DIMENSION(:,:) :: temp_arr !temporary array of coordinates
INTEGER :: i,n !counters
INTEGER :: lucky !index of selected coords
REAL :: rand !random number
! PRINT *, "Started solarlottery"
! PRINT *, "large_count is: ", large_count
! PRINT *, "small_count is: ", small_count
i = 1
IF ( large_count .NE. 0 ) THEN ! Check if any same type reactants exist
ALLOCATE ( temp_arr(large_count,3) )
DO n=1,SIZE(large_temp,1)
IF ( large_temp(n,4) .NE. 0 ) THEN
temp_arr(i,1) = large_temp(n,1)
temp_arr(i,2) = large_temp(n,2)
temp_arr(i,3) = large_temp(n,3)
i = i + 1
ELSE
CONTINUE
END IF
END DO
ELSE ! See if any opposite type reactants exist
ALLOCATE ( temp_arr(small_count,3) )
DO n=1,SIZE(small_temp,1)
IF ( small_temp(n,4) .NE. 0 ) THEN
temp_arr(i,1) = small_temp(n,1)
temp_arr(i,2) = small_temp(n,2)
temp_arr(i,3) = small_temp(n,3)
i = i + 1
ELSE
CONTINUE
END IF
END DO
END IF
! PRINT *, "temp_arr size is: ", SIZE(temp_arr)
! PRINT *, "The contents of temp_arr are: "
! DO n=1,SIZE(temp_arr,1)
! PRINT *, temp_arr(n,:)
! END DO
CALL RANDOM_NUMBER(rand) ! Choose a random temp_arr element
coords = 0
IF ( SIZE(temp_arr,1) .EQ. 1 ) THEN
coords = temp_arr(1,:)
! DO n=1,3
! coords(n) = temp_arr(1,n)
! END DO
ELSE
lucky = INT(rand*SIZE(temp_arr,1)) + 1
DO n=1,3
coords(n) = temp_arr(lucky,n)
END DO
END IF
! PRINT *, "The coords are: ",coords, "ending Solarlottery"
END SUBROUTINE solarlottery
SUBROUTINE thirdman( prod,i_re,j_re,k_re, null, matrix, en_list, time, &
wait_list, wait_len, prod_coords)
!
! Purpose:
! This subroutine attempts to find an empty site to place a third product
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! THIRDMAN !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
IMPLICIT NONE
!******************!
! Input and output !
!******************!
INTEGER , INTENT(IN) :: prod !product to be placed
INTEGER(KIND=SHORT), INTENT(OUT) :: null !error flag
INTEGER , POINTER :: wait_len
INTEGER , INTENT(IN) :: i_re,j_re,k_re !coords of original site
INTEGER , INTENT(OUT), DIMENSION(3) , OPTIONAL :: prod_coords !product placement coords
INTEGER , DIMENSION(:,:,:), POINTER :: matrix !ice-mantle matrix
TYPE (wait_info) , DIMENSION(:) , POINTER :: wait_list
REAL , DIMENSION(:) , POINTER :: en_list
REAL(KIND=DBL) , POINTER :: time
!*****************!
! Local variables !
!*****************!
INTEGER :: n,m !counters
INTEGER :: i_pr,j_pr,k_pr !product coords
INTEGER , DIMENSION(3) :: dimens
! Allocate and assign dimens pointer
dimens(1) = SIZE(matrix,1)
dimens(2) = SIZE(matrix,2)
dimens(3) = SIZE(matrix,3)
DO n=1,6
IF ( i_re .EQ. 1 .AND. ( n .EQ. 5 .OR. n .EQ. 6 ) ) THEN
CONTINUE
ELSE IF ( i_re .EQ. dimens(1) .AND. n .EQ. 6 ) THEN
! Don't hop down if on bottom layer
CONTINUE
ELSE
CALL hopping(i_re,j_re,k_re,i_pr,j_pr,k_pr,n,dimens)
IF ( matrix(i_pr,j_pr,k_pr) .EQ. 0 ) THEN
GOTO 1985
END IF
END IF
END DO
DO m=1,4 ! Go to a phantom position
SELECT CASE (m)
CASE (1)
IF ( j_re-1 .GT. 0 .AND. &
k_re-1 .GT. 0 .AND. &
k_re+1 .LE. dimens(3) ) THEN
CALL hopping(i_re,j_re-1,k_re+1,i_pr,j_pr,k_pr,1,dimens)
IF (matrix(i_pr,j_pr,k_pr) .EQ. 0 ) THEN
GOTO 1985
ELSE
CONTINUE
END IF
ELSE
CONTINUE
END IF
CASE (2)
IF ( j_re-1 .GT. 0 .AND. &
k_re-1 .GT. 0 .AND. &
k_re+1 .LE. dimens(3) ) THEN
CALL hopping(i_re,j_re-1,k_re-1,i_pr,j_pr,k_pr,2,dimens)
IF (matrix(i_pr,j_pr,k_pr) .EQ. 0 ) THEN
GOTO 1985
ELSE
CONTINUE
END IF
ELSE
CONTINUE
END IF
CASE (3)
IF ( j_re+1 .LE. dimens(2) .AND. &
k_re-1 .GT. 0 .AND. &
k_re+1 .LE. dimens(3) ) THEN
CALL hopping(i_re,j_re+1,k_re+1,i_pr,j_pr,k_pr,1,dimens)
IF (matrix(i_pr,j_pr,k_pr) .EQ. 0 ) THEN
GOTO 1985
ELSE
CONTINUE
END IF
ELSE
CONTINUE
END IF
CASE (4)
IF ( j_re+1 .LE. dimens(2) .AND. &
k_re-1 .GT. 0 .AND. &
k_re+1 .LE. dimens(3) ) THEN
CALL hopping(i_re,j_re+1,k_re-1,i_pr,j_pr,k_pr,2,dimens)
IF (matrix(i_pr,j_pr,k_pr) .EQ. 0 ) THEN
GOTO 1985
ELSE
GOTO 2001
END IF
ELSE
GOTO 2001
END IF
END SELECT
2001 IF ( m .EQ. 4 ) THEN
null = 1
! PRINT *, 'No reaction possible! ERROR!!!'
RETURN
END IF
END DO
! Place reactant at chosen site
1985 IF ( ANY( MOBILE_LIST .EQ. prod ) ) THEN
wait_len = wait_len + 1
wait_list(wait_len)%i = i_pr
wait_list(wait_len)%j = j_pr
wait_list(wait_len)%k = k_pr
wait_list(wait_len)%sp_num = prod
CALL wait_calc(wait_list,wait_len,en_list,time)
matrix(i_pr,j_pr,k_pr) = wait_len
! PRINT *, "Placing ",prod,"at ",i_pr,j_pr,k_pr,"at index ",wait_len,&
! "and ",matrix(i_pr,j_pr,k_pr),"should be ",wait_len
ELSE
matrix(i_pr,j_pr,k_pr) = -1*prod
! PRINT *, "Placing ",prod,"at ",i_pr,j_pr,k_pr,&
! "and ",matrix(i_pr,j_pr,k_pr),"should be ",-1*prod
END IF
! Assign output coordinates array
IF ( PRESENT(prod_coords) ) THEN
prod_coords(1) = i_pr
prod_coords(2) = j_pr
prod_coords(3) = k_pr
END IF
END SUBROUTINE thirdman
SUBROUTINE bresenham( x1,y1,x2,y2,track )
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
! BRESENHAM !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
! This subroutine uses the bresenham line algorithm results to simulate the
! track of a cosmic ray or other form of irradiation in a solid represented by
! a 3D crystal lattice structure with both normal and interstitial sites.
! Note that here, the axes are changed such that a "slice" from top to bottom
! is in the x-y plane
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
IMPLICIT NONE
!*****************
! Input and output
!*****************
INTEGER, INTENT(IN) :: x1,y1,x2,y2
INTEGER, INTENT(OUT), ALLOCATABLE, DIMENSION(:,:) :: track
!****************
! Local variables
!****************
INTEGER :: dx, dy, i, e
INTEGER :: incx,incy,inc1,inc2
INTEGER :: x,y
dx = x2 - x1
dy = y2 - y1
IF ( dx .LT. 0 ) dx = -dx
IF ( dy .LT. 0 ) dy = -dy
incx=1
IF ( x2 .LT. x1 ) incx = -1
incy = 1
IF ( y2 .LT. y1 ) incy = -1
x = x1
y = y1
IF ( dx .GT. dy ) THEN
ALLOCATE(track(dx+1,2))
! PRINT *, x,y
track(1,1) = x
track(1,2) = y
e = 2*dy - dx
inc1 = 2*(dy-dx)
inc2 = 2*dy
DO i=0,dx-1
IF ( e .GE. 0 ) THEN
y = y + incy
e = e + inc1
ELSE
e = e + inc2
END IF
x = x + incx
! PRINT *, x,y
track(i+2,1) = x
track(i+2,2) = y
END DO
ELSE
ALLOCATE(track(dy+1,2))
! PRINT *, x,y
track(1,1) = x
track(1,2) = y
e = 2*dx - dy
inc1 = 2*(dx-dy)
inc2 = 2*dx
DO i=0,dy-1
IF ( e .GE. 0 ) THEN
x = x + incx
e = e + inc1
ELSE
e = e + inc2
END IF
y = y + incy
! PRINT *, x,y
track(i+2,1) = x
track(i+2,2) = y
END DO
END IF
END SUBROUTINE bresenham
SUBROUTINE cern ( o3_prod,o3_dest,null,en_list, react_cube, matrix, event_num, &
event_coords, switch, wait_list, wait_len, time, elec_coords )
!
! Purpose:
! This subroutine handles interaction events between ionizing radiation and a
! target species in a solid
!
! Note:
! The array event_num contains the species number for pseudo reactants,
! i.e. excitations, excitations, and electrons. Specifically:
!
! -- event_num(1) = exc_num
! -- event_num(2) = ion_num
! -- event_num(3) = e_num
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! CERN !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
IMPLICIT NONE
!******************!
! Input and output !
!******************!
INTEGER , POINTER :: o3_prod,o3_dest
INTEGER(KIND=SHORT), INTENT(INOUT) :: null
INTEGER , INTENT(IN) , DIMENSION(3) :: event_coords !coordinated of collision
INTEGER , INTENT(IN) :: switch !type of event
INTEGER , INTENT(IN) , DIMENSION(3) :: event_num !pseudo species numbers
INTEGER(KIND=SHORT) , DIMENSION(:,:,:), POINTER :: react_cube !cube of reactions
INTEGER , DIMENSION(:,:,:), POINTER :: matrix !ice-mantle matrix
INTEGER , POINTER :: wait_len
INTEGER , INTENT(OUT) , DIMENSION(3) , OPTIONAL :: elec_coords
REAL(KIND=DBL) , POINTER :: time
REAL , DIMENSION(:) , POINTER :: en_list !list of binding energies
TYPE(wait_info) , DIMENSION(:) , POINTER :: wait_list
!*****************!
! Local variables !
!*****************!
INTEGER :: i,j,k
INTEGER :: i_re2,j_re2,k_re2 !reactant coordinates
INTEGER :: i_pr,j_pr,k_pr !product coordinates
INTEGER :: index,index2
INTEGER :: matrix_num
INTEGER :: r1, r2 !reactants 1 and 2
INTEGER :: prods_case
INTEGER , DIMENSION(3) :: prods !array of reaction products
INTEGER , DIMENSION(3) :: coords !temporary coordinates array
INTEGER , DIMENSION(3) :: dimens
INTEGER :: n
INTEGER :: original_value
REAL :: rnum
INTEGER :: case_num
! DEBUG = .TRUE.
IF ( DEBUG .EQV. .TRUE. ) PRINT *, '*****STARTING Cern*****'
r1 = 0
r2 = 0
i_pr = 0
j_pr = 0
k_pr = 0
! Assign the coordinates
i_re2 = event_coords(1)
j_re2 = event_coords(2)
k_re2 = event_coords(3)
original_value = matrix(i_re2,j_re2,k_re2)
IF ( DEBUG .EQV. .TRUE. ) THEN
PRINT *, 'Event_coords are:', event_coords
PRINT *, 'Original value is:',original_value
PRINT *, 'Switch is:',switch
END IF
! If switch equals 2, then the first reactant, r1, equals an excitation,
! otherwise, it equals and ionizing CRP
SELECT CASE (switch)
CASE (2) ! Ionization
r1 = event_num(2)
CASE (1) ! Excitation
r1 = event_num(1)
END SELECT
index = 0
matrix_num = matrix(i_re2,j_re2,k_re2)
IF ( matrix_num .LT. 0 ) THEN
r2 = ABS(matrix_num)
ELSE IF ( matrix_num .GT. 0 ) THEN
r2 = wait_list(matrix_num)%sp_num
index2 = matrix_num
! IF ( index .EQ. 53 ) PRINT *, 'The wait_list at',index,'is:'
! IF ( index .EQ. 53 ) PRINT *, wait_list(index)
END IF
!****************************************************************************
!**** ERROR CHECKING ********************************************************
!****************************************************************************
IF ( r2 .EQ. 0 ) THEN
! matrix(event_num(1),event_num(2),event_num(3)) = 0
! PRINT *, 'WHOOPS! FIXING ERROR!'
PRINT *, 'r1 =',r1
PRINT *, 'r2 =',r2
PRINT *, 'original_value =',original_value
PRINT *, 'event_coords =',event_coords
dimens(1) = SIZE(matrix,1)
dimens(2) = SIZE(matrix,2)
dimens(3) = SIZE(matrix,3)
OPEN(UNIT=1013,FILE="cern_test_wrong_spaces.txt")
DO k=1,dimens(3)
DO j=1,dimens(2)
DO i=1,dimens(1)
IF ( matrix(i,j,k) .GT. 0 ) THEN
IF( ANY(MOBILE_LIST .NE. wait_list(matrix(i,j,k))%sp_num ) ) THEN
WRITE(1013,*) 'Matrix=',matrix(i,j,k),'wait_list=',wait_list(matrix(i,j,k))
PRINT *, 'We have a wrong space:',matrix(i,j,k),' at',i,j,k
END IF
END IF
END DO
END DO
END DO
CLOSE(1013)
PRINT *, "BEEP BOOP 1 0"
PRINT *, "ERROR: wait_len =",wait_len,"< matrix_num =",matrix_num
CALL EXIT()
END IF
prods = 0
prods = react_cube(r1,r2,:) ! populate the product array
!****************************************************************************
!**** BRANCHING RATIOS ******************************************************
!****************************************************************************
!Determine if there is dissociation
IF ( r1 .EQ. 7 .AND. r2 .EQ. event_num(1) .OR. r1 .EQ. event_num(1) .AND. r2 .EQ. 7 ) THEN
! PRINT *, "Weve got branching"
rnum = RAND()
IF ( rnum .GT. O3_DIS_BRANCHING ) THEN
prods = (/ 1, 4, 0 /)
END IF
END IF
!****************************************************************************
!****ANALYTICS***************************************************************
!****************************************************************************
IF ( r1 .EQ. 7 .OR. r2 .EQ. 7 ) o3_dest = o3_dest + 1
IF ( ANY(prods .EQ. 7 ) ) o3_prod = o3_prod + 1
!****************************************************************************
!****ERROR CHECKING**********************************************************
!****************************************************************************
IF ( DEBUG .EQV. .TRUE. ) THEN
PRINT *, "wait_len in cern is ",wait_len
PRINT *, r1, r2, prods
WRITE(777,*) r1,',',r2,',',prods(1),',',prods(2),',',prods(3), ',',wait_len
IF ( r1 .EQ. 7 .OR. r2 .EQ. 7 ) THEN
PRINT *, '\/ In cern \/'
PRINT *, r1,',',r2,',',prods(1),',',prods(2),',',prods(3)
END IF
IF ( ANY(prods .EQ. 7 ) ) THEN
PRINT *, '\/ In cern \/'
PRINT *, r1,',',r2,',',prods(1),',',prods(2),',',prods(3)
END IF
END IF
IF ( prods(1) .EQ. 0 ) THEN
PRINT *, 'Uh-oh, we have a problem in Cern!'
PRINT *, 'r1=',r1,'r2=',r2
PRINT *, 'prods=',prods
OPEN(UNIT=1013,FILE="test_wait_list.txt")
DO n=1,wait_len+2
WRITE(1013,*) wait_list(n)
END DO
CLOSE(1013)
OPEN(UNIT=1014,FILE="wait_list_flaw.txt")
DO n=1,wait_len
IF ( wait_list(n)%sp_num .NE. 20 ) THEN
IF ( matrix(wait_list(n)%i,wait_list(n)%j,wait_list(n)%k) .NE. n ) THEN
WRITE(1014,*) matrix(wait_list(n)%i,wait_list(n)%j,wait_list(n)%k),", ",n,",",wait_len
END IF
END IF
END DO
CLOSE(1014)
CALL EXIT()
END IF
!****************************************************************************
!**** SANITY CHECK **********************************************************
!****************************************************************************
! If there are two or more products: do a sanity check to see if there are
! sufficient empty sites
!****************************************************************************
original_value = matrix(i_re2,j_re2,k_re2)
null = 0
IF ( prods(2) .NE. 0 ) THEN
! NB: Unlike in the reaction subroutine, in Krell, one needs to
! find a suitable location for the second product
! NB: In the event of an ionization, the electron should ALWAYS be
! stored as prods(2), doing otherwise will result in errors
CALL krell( event_coords, coords, matrix,null )
IF (null .EQ. 1 ) THEN ! Can't place 2nd product: no good sites
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!TEMPORARY KLUDGE
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
! DEBUG = .FALSE.
RETURN
ELSE ! Save new coordinates
i_pr = coords(1)
j_pr = coords(2)
k_pr = coords(3)
END IF
END IF
!*****************************************************************************
! Save the coords of the electron, if necessary
!*****************************************************************************
IF ( prods(2) .EQ. event_num(3) .AND. PRESENT(elec_coords) ) THEN
elec_coords(1) = i_pr
elec_coords(2) = j_pr
elec_coords(3) = k_pr
END IF
!*****************************************************************************
! Determine the product case
!*****************************************************************************
CALL rtype_tree(r1,r2,prods,case_num)
!*****************************************************************************
! Place the products
!*****************************************************************************
CALL place_product(i_re2,j_re2,k_re2,i_pr,j_pr,k_pr,index,index2,r1,r2,&
prods,case_num,matrix, wait_list,wait_len,en_list,time)
IF ( DEBUG .EQV. .TRUE. ) PRINT *, '*****ENDING Cern*****'
! DEBUG = .FALSE.
END SUBROUTINE cern
SUBROUTINE fallout ( o3_prod,o3_dest,react_cube, matrix, en_list, ionlist, &
wait_list, wait_len, time, ev_nums)
! Purpose:
! To calculate the track of a particle of ionizing radiation through a
! crystaline solid.
!
! Note:
! The input array, sigmas, contains the proton collision cross-sections.
!
! Note:
! The array ev_nums contains the following values for pseudo-reactants:
!
! -- ev_nums(1) = cosmic-ray species number
! -- ev_nums(2) = excitation species number
! -- ev_nums(3) = electron species number
!
! Note:
! sgse is short for second-generation secondary eletron.
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! FALLOUT !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
IMPLICIT NONE
!******************!
! Input and output !
!******************!
INTEGER , POINTER :: o3_prod,o3_dest
INTEGER , POINTER :: wait_len
INTEGER , DIMENSION(:) , POINTER :: ionlist !list of anionic species
INTEGER(KIND=SHORT), DIMENSION(:,:,:), POINTER :: react_cube !array of products/reactions
INTEGER , DIMENSION(:,:,:), POINTER :: matrix !ice-mantle matrix
REAL , DIMENSION(:) , POINTER :: en_list !list of binding and desorption energies
REAL(KIND=DBL) , POINTER :: time
TYPE(wait_info) , DIMENSION(:) , POINTER :: wait_list
INTEGER , DIMENSION(3) :: ev_nums !values of special pseudoreactants
!*****************!
! Local variables !
!*****************!
INTEGER(KIND=SHORT) :: null
INTEGER :: n,nn,jj
INTEGER :: num_elecs ! number of secondary electrons pruduced
INTEGER :: num_izns