-
Notifications
You must be signed in to change notification settings - Fork 5
/
adjac.f95
3021 lines (2612 loc) · 79.4 KB
/
adjac.f95
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
!! NOTE: this file is autogenerated from adjac.f95.in: do not edit manually
! -*-f90-*-
!
! adjac: Automatic Differentiation for generating Jacobians.
!
! Copyright (c) 2014, Pauli Virtanen <[email protected]>
! All rights reserved.
!
! Redistribution and use in source and binary forms, with or without
! modification, are permitted provided that the following conditions
! are met:
!
! 1. Redistributions of source code must retain the above copyright
! notice, this list of conditions and the following disclaimer.
!
! 2. Redistributions in binary form must reproduce the above copyright
! notice, this list of conditions and the following disclaimer in the
! documentation and/or other materials provided with the distribution.
!
! 3. Neither the name of the copyright holder nor the names of its
! contributors may be used to endorse or promote products derived from
! this software without specific prior written permission.
!
! THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
! "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
! LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
! FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
! COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
! INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
! BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
! LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
! CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
! LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
! ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
! POSSIBILITY OF SUCH DAMAGE.
module adjac
private
! NOTE: we would like to use derived type finalizers for memory
! deallocation. However, as of 2015-03-12, these are not fully
! implemented in gfortran, and will not be called e.g. on function
! returns that the code here extensively relies on.
type, public :: adjac_double
double precision :: value, vmul
integer :: i = 0
end type adjac_double
type, public :: adjac_complexan
complex(kind=kind(0d0)) :: value, vmul
integer :: i = 0
end type adjac_complexan
type, public :: adjac_complex
type(adjac_double) :: re, im
end type adjac_complex
logical :: jac_product_mode = .false.
! DAG of differentials
!
! if (sum_map(1 + 2*(i-1)) .ne. 0) then
! ! dependent variable
! d_{i} = sum_mul(1+2*(i-1)) * d_{sum_map(1+2*(i-1))} + sum_mul(2+2*(i-1)) * d_{sum_map(2+2*(i-1))}
! else
! ! independent variable
! d_{i} = D_{i}
!
integer, parameter :: block_size = 16
integer :: free_a = 1, free_q = 1
integer, dimension(:), allocatable :: sum_map_a, sum_map_q
double precision, dimension(:), allocatable :: sum_mul_a
complex(kind=kind(0d0)), dimension(:), allocatable :: sum_mul_q
public assignment(=)
interface assignment(=)
module procedure assign_ai, assign_ad
module procedure assign_bi, assign_bd, assign_bz, assign_ba
module procedure assign_qi, assign_qd, assign_qz
end interface
public operator(+)
interface operator(+)
module procedure add_aa
module procedure add_bb
module procedure add_qq
module procedure add_ab
module procedure add_ba
module procedure add_ai
module procedure add_bi
module procedure add_qi
module procedure add_ia
module procedure add_ib
module procedure add_iq
module procedure add_ad
module procedure add_bd
module procedure add_qd
module procedure add_da
module procedure add_db
module procedure add_dq
module procedure add_az
module procedure add_bz
module procedure add_qz
module procedure add_za
module procedure add_zb
module procedure add_zq
module procedure pos_a, pos_b, pos_q
end interface
public operator(-)
interface operator(-)
module procedure sub_aa
module procedure sub_bb
module procedure sub_qq
module procedure sub_ab
module procedure sub_ba
module procedure sub_ai
module procedure sub_bi
module procedure sub_qi
module procedure sub_ia
module procedure sub_ib
module procedure sub_iq
module procedure sub_ad
module procedure sub_bd
module procedure sub_qd
module procedure sub_da
module procedure sub_db
module procedure sub_dq
module procedure sub_az
module procedure sub_bz
module procedure sub_qz
module procedure sub_za
module procedure sub_zb
module procedure sub_zq
module procedure neg_a, neg_b, neg_q
end interface
public operator(*)
interface operator(*)
module procedure mul_aa
module procedure mul_bb
module procedure mul_qq
module procedure mul_ab
module procedure mul_ba
module procedure mul_ai
module procedure mul_bi
module procedure mul_qi
module procedure mul_ia
module procedure mul_ib
module procedure mul_iq
module procedure mul_ad
module procedure mul_bd
module procedure mul_qd
module procedure mul_da
module procedure mul_db
module procedure mul_dq
module procedure mul_az
module procedure mul_bz
module procedure mul_qz
module procedure mul_za
module procedure mul_zb
module procedure mul_zq
end interface operator(*)
public operator(/)
interface operator(/)
module procedure div_aa
module procedure div_bb
module procedure div_qq
module procedure div_ab
module procedure div_ba
module procedure div_ai
module procedure div_bi
module procedure div_qi
module procedure div_ia
module procedure div_ib
module procedure div_iq
module procedure div_ad
module procedure div_bd
module procedure div_qd
module procedure div_da
module procedure div_db
module procedure div_dq
module procedure div_az
module procedure div_bz
module procedure div_qz
module procedure div_za
module procedure div_zb
module procedure div_zq
end interface operator(/)
public operator(**)
interface operator(**)
module procedure pow_ai, pow_ad, pow_qi, pow_qd, pow_qz
end interface operator(**)
public matmul
interface matmul
module procedure matmul_aa, matmul_ai, matmul_ia, matmul_ad, matmul_da
module procedure matmul_bb, matmul_bz, matmul_zb
module procedure matmul_qq, matmul_qi, matmul_iq, matmul_qd, matmul_dq, matmul_qz, matmul_zq
end interface matmul
public dble
interface dble
module procedure dble_a, dble_b
end interface dble
public aimag
interface aimag
module procedure aimag_b
end interface aimag
public conjg
interface conjg
module procedure conjg_b
end interface conjg
public abs
interface abs
module procedure abs_a, abs_b
end interface abs
public exp
interface exp
module procedure exp_a, exp_b, exp_q
end interface exp
public sin
interface sin
module procedure sin_a, sin_b, sin_q
end interface sin
public cos
interface cos
module procedure cos_a, cos_b, cos_q
end interface cos
public log
interface log
module procedure log_a, log_b, log_q
end interface log
public sqrt
interface sqrt
module procedure sqrt_a, sqrt_b, sqrt_q
end interface sqrt
interface adjac_set_independent
module procedure set_independent_a, set_independent_q
module procedure set_independent_many_a, set_independent_many_q
end interface adjac_set_independent
interface sum_taylor
module procedure sum_taylor_a, sum_taylor_q
end interface sum_taylor
interface adjac_get_value
module procedure get_value_one_a, get_value_one_q
module procedure get_value_many_a, get_value_many_q
end interface adjac_get_value
interface adjac_get_dense_jacobian
module procedure get_dense_jacobian_a, get_dense_jacobian_q
end interface adjac_get_dense_jacobian
interface adjac_get_coo_jacobian
module procedure get_coo_jacobian_a, get_coo_jacobian_q
end interface adjac_get_coo_jacobian
public adjac_set_independent, adjac_get_value, &
adjac_get_dense_jacobian, adjac_get_coo_jacobian, &
adjac_reset, adjac_free
contains
subroutine fatal_error(msg)
implicit none
character(len=*), intent(in) :: msg
write(*,*) 'adjac: error: ', trim(msg)
stop
end subroutine fatal_error
subroutine adjac_reset(product_mode)
implicit none
logical, optional, intent(in) :: product_mode
if (present(product_mode)) then
jac_product_mode = product_mode
end if
free_a = 1
free_q = 1
end subroutine adjac_reset
subroutine adjac_free()
implicit none
free_a = 1
free_q = 1
if (allocated(sum_map_a)) then
deallocate(sum_map_a)
deallocate(sum_mul_a)
end if
if (allocated(sum_map_q)) then
deallocate(sum_map_q)
deallocate(sum_mul_q)
end if
end subroutine adjac_free
pure subroutine heap_siftup(heap, nheap, initpos)
implicit none
integer, intent(in) :: nheap
integer, dimension(*), intent(inout) :: heap
integer, intent(in) :: initpos
integer :: pos, item, pos2, pos3
pos = initpos
pos2 = 2*pos
item = heap(pos)
do while (pos2 <= nheap)
pos3 = pos2 + 1
if (pos3 <= nheap .and. heap(pos3) >= heap(pos2)) then
pos2 = pos3
end if
heap(pos) = heap(pos2)
pos = pos2
pos2 = 2*pos
end do
heap(pos) = item
call heap_siftdown(heap, initpos, pos)
end subroutine heap_siftup
pure subroutine heap_siftdown(heap, initpos, pos0)
implicit none
integer, dimension(*), intent(inout) :: heap
integer, intent(in) :: initpos, pos0
integer :: item, pos, pos2
pos = pos0
item = heap(pos)
do while (pos > initpos)
pos2 = pos/2
if (heap(pos2) < item) then
heap(pos) = heap(pos2)
else
exit
end if
pos = pos2
end do
heap(pos) = item
end subroutine heap_siftdown
pure subroutine heap_push(heap, nheap, item)
implicit none
integer, intent(inout) :: nheap
integer, dimension(*), intent(inout) :: heap
integer, intent(in) :: item
nheap = nheap + 1
heap(nheap) = item
call heap_siftdown(heap, 1, nheap)
end subroutine heap_push
pure subroutine heap_pop(heap, nheap, item)
implicit none
integer, intent(inout) :: nheap
integer, dimension(*), intent(inout) :: heap
integer, intent(out) :: item
item = heap(1)
heap(1) = heap(nheap)
nheap = nheap - 1
if (nheap.gt.1) then
call heap_siftup(heap, nheap, 1)
end if
end subroutine heap_pop
pure subroutine heap_pushpop(heap, nheap, item, item_out)
implicit none
integer, intent(inout) :: nheap
integer, dimension(*), intent(inout) :: heap
integer, intent(in) :: item
integer, intent(out) :: item_out
if (nheap > 0 .and. item < heap(1)) then
item_out = heap(1)
heap(1) = item
if (nheap.gt.1) then
call heap_siftup(heap, nheap, 1)
end if
else
item_out = item
end if
end subroutine heap_pushpop
subroutine alloc_mem_a(x)
implicit none
type(adjac_double), intent(inout) :: x
integer, dimension(:), allocatable :: itmp
double precision, dimension(:), allocatable :: tmp
integer :: sz
if (jac_product_mode) then
return
end if
if (.not.allocated(sum_map_a)) then
sz = 0
else
sz = size(sum_map_a)
end if
if (sz < 2*free_a + 1) then
! Enlarge work space
if (allocated(sum_map_a)) then
sz = sz + 2*free_a + 1
allocate(itmp(sz), tmp(sz))
itmp(1:size(sum_map_a)) = sum_map_a(:)
tmp(1:size(sum_mul_a)) = sum_mul_a(:)
call move_alloc(itmp, sum_map_a)
call move_alloc(tmp, sum_mul_a)
else
allocate(sum_map_a(100), sum_mul_a(100))
end if
end if
x%i = free_a
free_a = free_a + 1
end subroutine alloc_mem_a
pure subroutine link_mem_a(dst, src)
implicit none
type(adjac_double), intent(inout) :: dst
type(adjac_double), intent(in) :: src
dst%i = src%i
end subroutine link_mem_a
pure subroutine free_mem_a(x)
implicit none
type(adjac_double), intent(inout) :: x
x%i = 0
end subroutine free_mem_a
subroutine set_independent_a(x, xval, j, dx)
implicit none
type(adjac_double), intent(out) :: x
double precision, intent(in) :: xval
double precision, optional, intent(in) :: dx
integer, intent(in) :: j
x%value = xval
if (jac_product_mode) then
if (.not.present(dx)) then
call fatal_error('no dx given to adjac_set_independent when jacobian product mode is active')
end if
x%vmul = dx
else
x%vmul = 1
call alloc_mem_a(x)
sum_map_a(1 + 2*(x%i-1)) = 0
sum_map_a(2 + 2*(x%i-1)) = j
end if
end subroutine set_independent_a
subroutine set_independent_many_a(x, xval, dx)
implicit none
type(adjac_double), dimension(:), intent(inout) :: x
double precision, dimension(size(x)), intent(in) :: xval
double precision, dimension(size(x)), optional, intent(in) :: dx
integer :: j
if (present(dx)) then
do j = 1, size(x,1)
call set_independent_a(x(j), xval(j), j, dx(j))
end do
else
do j = 1, size(x,1)
call set_independent_a(x(j), xval(j), j)
end do
end if
end subroutine set_independent_many_a
subroutine get_value_one_a(y, val, dy)
implicit none
type(adjac_double), intent(in) :: y
double precision, intent(out) :: val
double precision, optional, intent(out) :: dy
val = y%value
if (present(dy)) then
if (.not. jac_product_mode) then
call fatal_error('call to adjac_get_value with dy when jacobian product mode is not active')
end if
dy = y%vmul
end if
end subroutine get_value_one_a
subroutine get_value_many_a(y, val, dy)
implicit none
type(adjac_double), dimension(:), intent(in) :: y
double precision, dimension(size(y,1)), intent(out) :: val
double precision, dimension(size(y,1)), optional, intent(out) :: dy
integer :: j
do j = 1, size(val,1)
val(j) = y(j)%value
end do
if (present(dy)) then
if (.not. jac_product_mode) then
call fatal_error('call to adjac_get_value with dy when jacobian product mode is not active')
end if
do j = 1, size(val,1)
dy(j) = y(j)%vmul
end do
end if
end subroutine get_value_many_a
subroutine get_dense_jacobian_a(y, jac_dense)
implicit none
type(adjac_double), dimension(:), intent(inout) :: y
double precision, dimension(:,:), intent(out) :: jac_dense
double precision, dimension(block_size,free_a) :: work
integer, dimension(free_a) :: iwork, imask
integer :: k, j, ia, ib, kmin, kmax, nwork, j_next
if (jac_product_mode) then
call fatal_error('call to adjac_get_dense_jacobian when jacobian product mode is active')
end if
jac_dense = 0
work = 0
imask = 0
do kmin = 1, size(y,1), block_size
kmax = min(kmin + block_size - 1, size(y,1))
nwork = 0
do k = kmin, kmax, 1
if (y(k)%i == 0) cycle
work(k-kmin+1, y(k)%i) = y(k)%vmul
call heap_push(iwork, nwork, y(k)%i)
imask(y(k)%i) = 1
end do
! Traverse the tape
j_next = 0
if (nwork > 0) then
call heap_pop(iwork, nwork, j_next)
end if
do while (j_next > 0)
j = j_next
j_next = 0
if (256*nwork > j) then
! Heap is too big, probably contains nearly all j values,
! and we are better off just looping through them
nwork = j
exit
end if
ia = sum_map_a(1+2*(j-1))
ib = sum_map_a(2+2*(j-1))
if (ia == 0) then
jac_dense(kmin:kmax,ib) = work(1:(kmax-kmin+1),j)
else
do concurrent (k=1:block_size)
work(k,ia) = work(k,ia) + sum_mul_a(1+2*(j-1)) * work(k,j)
end do
do concurrent (k=1:block_size)
work(k,ib) = work(k,ib) + sum_mul_a(2+2*(j-1)) * work(k,j)
end do
if (imask(ia) == 0 .and. imask(ib) == 0) then
call heap_push(iwork, nwork, ia)
call heap_pushpop(iwork, nwork, ib, j_next)
imask(ia) = 1
imask(ib) = 1
else if (imask(ia) == 0) then
call heap_pushpop(iwork, nwork, ia, j_next)
imask(ia) = 1
else if (imask(ib) == 0) then
call heap_pushpop(iwork, nwork, ib, j_next)
imask(ib) = 1
end if
end if
do concurrent (k=1:block_size)
work(k,j) = 0
end do
imask(j) = 0
if (nwork > 0 .and. j_next == 0) then
call heap_pop(iwork, nwork, j_next)
end if
end do
do j = nwork, 1, -1
if (imask(j).ne.0) then
ia = sum_map_a(1+2*(j-1))
ib = sum_map_a(2+2*(j-1))
if (ia == 0) then
jac_dense(kmin:kmax,ib) = work(1:(kmax-kmin+1),j)
else
do concurrent (k=1:block_size)
work(k,ia) = work(k,ia) + sum_mul_a(1+2*(j-1)) * work(k,j)
end do
do concurrent (k=1:block_size)
work(k,ib) = work(k,ib) + sum_mul_a(2+2*(j-1)) * work(k,j)
end do
imask(ia) = 1
imask(ib) = 1
end if
do concurrent (k=1:block_size)
work(k,j) = 0
end do
imask(j) = 0
end if
end do
end do
end subroutine get_dense_jacobian_a
subroutine get_coo_jacobian_a(y, nnz, jac_val, jac_i, jac_j)
implicit none
type(adjac_double), dimension(:), intent(inout) :: y
double precision, dimension(:), allocatable, intent(inout) :: jac_val
integer, dimension(:), allocatable, intent(inout) :: jac_i, jac_j
integer, intent(out) :: nnz
double precision, dimension(block_size,free_a) :: work
integer, dimension(free_a) :: iwork, imask
integer, dimension(:), allocatable :: itmp
double precision, dimension(:), allocatable :: vtmp
integer :: kmin, kmax, k, j, ia, ib, nwork, j_next, sz
if (jac_product_mode) then
call fatal_error('call to adjac_get_coo_jacobian when jacobian product mode is active')
end if
if (allocated(jac_val)) deallocate(jac_val)
if (allocated(jac_i)) deallocate(jac_i)
if (allocated(jac_j)) deallocate(jac_j)
sz = free_a + 10
allocate(jac_val(sz), jac_i(sz), jac_j(sz))
nnz = 0
work = 0
imask = 0
do kmin = 1, size(y,1), block_size
kmax = min(kmin + block_size - 1, size(y,1))
nwork = 0
do k = kmin, kmax, 1
if (y(k)%i == 0) cycle
work(k-kmin+1, y(k)%i) = y(k)%vmul
call heap_push(iwork, nwork, y(k)%i)
imask(y(k)%i) = 1
end do
! Traverse the tape
j_next = 0
if (nwork > 0) then
call heap_pop(iwork, nwork, j_next)
end if
do while (j_next > 0)
j = j_next
j_next = 0
if (256*nwork > j) then
! Heap is too big, probably contains nearly all j values,
! and we are better off just looping through them
nwork = j
exit
end if
ia = sum_map_a(1+2*(j-1))
ib = sum_map_a(2+2*(j-1))
if (ia == 0) then
if (nnz + (kmax-kmin) + 1 >= sz) then
! Exponential overallocation
sz = 2*sz + (kmax-kmin) + 1
allocate(itmp(sz))
itmp(1:nnz) = jac_i(1:nnz)
call move_alloc(itmp, jac_i)
allocate(itmp(sz))
itmp(1:nnz) = jac_j(1:nnz)
call move_alloc(itmp, jac_j)
allocate(vtmp(sz))
vtmp(1:nnz) = jac_val(1:nnz)
call move_alloc(vtmp, jac_val)
end if
do k = kmin, kmax
if (work(k-kmin+1,j).ne.0) then
nnz = nnz + 1
jac_i(nnz) = k
jac_j(nnz) = ib
jac_val(nnz) = work(k-kmin+1,j)
end if
end do
else
do concurrent (k=1:block_size)
work(k,ia) = work(k,ia) + sum_mul_a(1+2*(j-1)) * work(k,j)
end do
do concurrent (k=1:block_size)
work(k,ib) = work(k,ib) + sum_mul_a(2+2*(j-1)) * work(k,j)
end do
if (imask(ia) == 0 .and. imask(ib) == 0) then
call heap_push(iwork, nwork, ia)
call heap_pushpop(iwork, nwork, ib, j_next)
imask(ia) = 1
imask(ib) = 1
else if (imask(ia) == 0) then
call heap_pushpop(iwork, nwork, ia, j_next)
imask(ia) = 1
else if (imask(ib) == 0) then
call heap_pushpop(iwork, nwork, ib, j_next)
imask(ib) = 1
end if
end if
do concurrent (k=1:block_size)
work(k,j) = 0
end do
imask(j) = 0
if (nwork > 0 .and. j_next == 0) then
call heap_pop(iwork, nwork, j_next)
end if
end do
do j = nwork, 1, -1
if (imask(j).ne.0) then
ia = sum_map_a(1+2*(j-1))
ib = sum_map_a(2+2*(j-1))
if (ia == 0) then
if (nnz + (kmax-kmin) + 1 >= sz) then
! Exponential overallocation
sz = 2*sz + (kmax-kmin) + 1
allocate(itmp(sz))
itmp(1:nnz) = jac_i(1:nnz)
call move_alloc(itmp, jac_i)
allocate(itmp(sz))
itmp(1:nnz) = jac_j(1:nnz)
call move_alloc(itmp, jac_j)
allocate(vtmp(sz))
vtmp(1:nnz) = jac_val(1:nnz)
call move_alloc(vtmp, jac_val)
end if
do k = kmin, kmax
if (work(k-kmin+1,j).ne.0) then
nnz = nnz + 1
jac_i(nnz) = k
jac_j(nnz) = ib
jac_val(nnz) = work(k-kmin+1,j)
end if
end do
else
do concurrent (k=1:block_size)
work(k,ia) = work(k,ia) + sum_mul_a(1+2*(j-1)) * work(k,j)
end do
do concurrent (k=1:block_size)
work(k,ib) = work(k,ib) + sum_mul_a(2+2*(j-1)) * work(k,j)
end do
imask(ia) = 1
imask(ib) = 1
end if
do concurrent (k=1:block_size)
work(k,j) = 0
end do
imask(j) = 0
end if
end do
end do
if (nnz .eq. 0) then
if (allocated(jac_val)) deallocate(jac_val)
if (allocated(jac_i)) deallocate(jac_i)
if (allocated(jac_j)) deallocate(jac_j)
else if (nnz < sz) then
! Shrink to size
allocate(itmp(nnz))
itmp(1:nnz) = jac_i(1:nnz)
call move_alloc(itmp, jac_i)
allocate(itmp(nnz))
itmp(1:nnz) = jac_j(1:nnz)
call move_alloc(itmp, jac_j)
allocate(vtmp(nnz))
vtmp(1:nnz) = jac_val(1:nnz)
call move_alloc(vtmp, jac_val)
end if
end subroutine get_coo_jacobian_a
subroutine sum_taylor_a(alphap, betap, a, b, c)
! c := alpha*a + beta*b
use iso_c_binding
implicit none
double precision, intent(in) :: alphap, betap
type(adjac_double), intent(in) :: a, b
type(adjac_double), intent(inout) :: c
if (jac_product_mode) then
c%vmul = alphap * a%vmul + betap * b%vmul
else
if (a%vmul == 0 .or. a%i == 0 .or. alphap == 0) then
c%vmul = betap * b%vmul
c%i = b%i
else if (b%vmul == 0 .or. b%i == 0 .or. betap == 0) then
c%vmul = alphap * a%vmul
c%i = a%i
else if (a%i == b%i) then
c%vmul = alphap * a%vmul + betap * b%vmul
c%i = a%i
else
call alloc_mem_a(c)
c%vmul = 1
sum_map_a(1 + 2*(c%i-1)) = a%i
sum_map_a(2 + 2*(c%i-1)) = b%i
sum_mul_a(1 + 2*(c%i-1)) = alphap * a%vmul
sum_mul_a(2 + 2*(c%i-1)) = betap * b%vmul
end if
end if
end subroutine sum_taylor_a
!--------------------------------------------------------------------------
! Overloaded operators
!--------------------------------------------------------------------------
!!
!! assignment(=)
!!
pure elemental subroutine assign_ai(x, y)
implicit none
type(adjac_double), intent(inout) :: x
integer, intent(in) :: y
call free_mem_a(x)
x%value = y
x%vmul = 0
end subroutine assign_ai
pure elemental subroutine assign_ad(x, y)
implicit none
type(adjac_double), intent(inout) :: x
double precision, intent(in) :: y
call free_mem_a(x)
x%value = y
x%vmul = 0
end subroutine assign_ad
pure elemental subroutine assign_bi(x, y)
implicit none
type(adjac_complex), intent(inout) :: x
integer, intent(in) :: y
x%re = dble(y)
x%im = 0d0
end subroutine assign_bi
pure elemental subroutine assign_bd(x, y)
implicit none
type(adjac_complex), intent(inout) :: x
double precision, intent(in) :: y
x%re = dble(y)
x%im = 0d0
end subroutine assign_bd
pure elemental subroutine assign_bz(x, y)
implicit none
type(adjac_complex), intent(inout) :: x
complex(kind=kind(0d0)), intent(in) :: y
x%re = dble(y)
x%im = aimag(y)
end subroutine assign_bz
pure elemental subroutine assign_ba(x, y)
implicit none
type(adjac_complex), intent(inout) :: x
type(adjac_double), intent(in) :: y
x%re = y
x%im = 0d0
end subroutine assign_ba
!!
!! operator(+)
!!
! X + Y = x + y + (x_j + y_j) dj
impure elemental function add_aa(x, y) result(z)
implicit none
type(adjac_double), intent(in) :: x, y
type(adjac_double) :: z
z%value = x%value + y%value
call sum_taylor(real(1d0, kind=kind(0d0)), real(1d0, kind=kind(0d0)), x, y, z)
end function add_aa
pure elemental function add_ai(x, y) result(z)
implicit none
type(adjac_double), intent(in) :: x
integer, intent(in) :: y
type(adjac_double) :: z
z%value = x%value + y
z%vmul = x%vmul
call link_mem_a(z, x)
end function add_ai
pure elemental function add_ia(x, y) result(z)
implicit none
integer, intent(in) :: x
type(adjac_double), intent(in) :: y
type(adjac_double) :: z
z = y + x
end function add_ia
pure elemental function add_ad(x, y) result(z)
implicit none
type(adjac_double), intent(in) :: x
double precision, intent(in) :: y
type(adjac_double) :: z
z%value = x%value + y
z%vmul = x%vmul
call link_mem_a(z, x)
end function add_ad
pure elemental function add_da(x, y) result(z)
implicit none
double precision, intent(in) :: x
type(adjac_double), intent(in) :: y
type(adjac_double) :: z
z = y + x
end function add_da
pure elemental function add_az(x, y) result(z)
implicit none
type(adjac_double), intent(in) :: x
complex(kind=kind(0d0)), intent(in) :: y
type(adjac_complex) :: z
z%re = x + dble(y)
z%im = aimag(y)
end function add_az
pure elemental function add_za(x, y) result(z)
implicit none
complex(kind=kind(0d0)), intent(in) :: x
type(adjac_double), intent(in) :: y
type(adjac_complex) :: z
z%re = dble(x) + y
z%im = aimag(x)
end function add_za
impure elemental function add_bb(x, y) result(z)
implicit none
type(adjac_complex), intent(in) :: x
type(adjac_complex), intent(in) :: y
type(adjac_complex) :: z
z%re = x%re + y%re
z%im = x%im + y%im
end function add_bb
pure elemental function add_bz(x, y) result(z)
implicit none
type(adjac_complex), intent(in) :: x
complex(kind=kind(0d0)), intent(in) :: y
type(adjac_complex) :: z
z%re = x%re + dble(y)
z%im = x%im + aimag(y)
end function add_bz
pure elemental function add_zb(x, y) result(z)
implicit none
complex(kind=kind(0d0)), intent(in) :: x
type(adjac_complex), intent(in) :: y
type(adjac_complex) :: z
z%re = dble(x) + y%re
z%im = aimag(x) + y%im
end function add_zb