-
Notifications
You must be signed in to change notification settings - Fork 4
/
discrect.f90
1841 lines (1486 loc) · 49.4 KB
/
discrect.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
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
! This module contains code for constructing and manipulating piecewise Legendre
! expansions given on rectangles. Such an expansion is a sum of the form
!
! n-1 n-1-i ( 2 x1+x2 ) ( 2 y1+y2 )
! \sum \sum a \tilde{P} ( ----- x + ----- ) \tilde{P} ( ---- y + ----- )
! i=0 j=0 ij i ( x2-x1 x1-x2 ) j ( y2-y1 y1-y2 )
!
! 2
! ------------------------- ,
! sqrt(x2-x1) sqrt(y2-y1)
!
! where \tilde{P}_i denotes the L^2 normalized Legendre polynomial given on the
! interval [-1,1] and (x1,x2) x (y1,
!
! Each bivariate Legendre expansion can be represented in two different ways, via the
! vector of expansion coefficients or via the scaled values of the expansion at the
! nodes of a generalized Gaussian quadrature rule (see bilege.f90 for details).
! Piecewise expansions over the entire restricted quadtree are represented by
! amalgamating these vectors. The leaves are ordered according to a depth-first
! traversal of the tree with child boxes ordered as follows: upper left, followed
! by upper right, then lower right and, finally, lower left.
!
! The order of the expansions used and various other parameters are hard-coded
! in the routine discinit. Precomputed near-interaction, generated by the
! precomp.f90 program and stored in nearmats.f90, are used by this code and
! need to be recalculated if the order of expansions changes.
!
! For the most part, these routines are not multithreaded. There are a handful
! of utility routines which have multithreaded versions which end with the _mt
! postfix.
!
! The following subroutines are publicly callable:
!
! discrect_init - initialize this discretization code; this routine MUST be called
! before any of the other subroutines here
!
! discrect_info - return some basic information about the quadrature scheme used
! by this discretization code
!
! discrect_adap - adaptively refine a restricted quadtree until a user-specified
! functions is accurately represented as a piecewise bivariate Legendre expansion
! subordinate to the leaf nodes of the quadtree
!
! discrect_uniform - repeatedly subdivide each leaf box until all leaf
! boxes have side length smaller than a specified size
!
! discrect_reorder_leaves - reorder the list of leaves in the tree so that
! every box has the property that the discretization nodes of all
! leaves descended from it are sequential
!
! discrect_quad - return the piecewise tensor product Legendre quadrature
! used to discretize functions (also referred to as the "discretization
! quadrature"
!
! discrect_isnear - determine whether a specified target box is "near"
! a specified source box or not
!
! discrect_quadleaf - return the quadrature used to discretize the expansion
! on one specified leaf node
!
! discrect_coefs - given the vector of *scaled* values of a function at the
! nodes of the discretization quadrature rule, compute the coefficients in each
! of the biviarate Legendre expansions used to represent the function
!
! discrect_eval - given the vector of expansion coefficients, evaluate a
! piecewise bivariate expansion on the quadtree at a specified point
!
! discrect_evalop - evaluate an arbitrary submatrix of the matrix discretizing
! one of the operators (1), (2) or (3)
!
! discrect_evalop_sorted - evaluate an arbitrary submatrix of the matrix
! discretizing one of the operators (1), (2) or (3) --- with the lists of
! rows and columns sorted
!
! discrect_evalopt_sorted - evaluate the transpose of an arbitrary submatrix of
! the matrix discretizing one of the operators (1), (2) or (3)
!
! discrect_evalop_matrix - this is a convenience routine which evaluates the entire
! matrix discretizing one of the operators (1), (2) or (3)
!
! THERE IS A MULTITHEADED VERSION OF THIS CODE WITH THE _mt POSTFIX
!
! discrect_applyop - this is a convenience routine for applying one the of operators
! (1), (2) or (3) to a user-specified vector
!
! THERE IS A MULTITHEADED VERSION OF THIS CODE WITH THE _mt POSTFIX
!
! disrect_evalop_outgoing - construct the matrix taking the scaled values of an
! input function on a specified set of discretization nodes to the scaled values
! of its image under (1), (2) or (3) at the nodes of an user-specified quadrature
! rule PROVIDED the target quadrature nodes are sufficiently distant from the
! source quadrature rules (sufficiently distance is defined by "dnear" parameter)
!
! disrect_evalop_incoming - construct the transpose of the matrix taking the scaled
! values of an input function at the nodes of an user-specified quadrature in the
! far-field to the scaled values of its image under one (1), (2) or (3) at a specified
! set of interpolation nodes
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
! Reasonable settings for the quadrature parameters
! -------------------------------------------------
!
! The order of discretization quadrature and the desired accuracy for the discretization
! control the notion of "near" interaction. Below are some reasonable settings of the
! parameters in disc_init for desired levels of precision. The program farq can be
! used to test these settings.
!
! WARNING: WHEN THE ORDER OF THE QUADRATURE RULE IS CHANGED, THE PROGRAM PRECOMP MUST
! BE RERUN TO GENERATE THE DATA IN nearmats.f90!! NOTE TAHT ONE MUST MANUALLY SET THE
! ORDER OF THE DISCRETIZATION QUADRATURE IN precomp.f90.
!
!
! 5-digit precision: norder = 10, dnear = 0.50, nlege = N/A
! 7-digit precision: norder = 10, dnear = 1.00, nlege = 9
!
! 6-digit precision: norder = 12 dnear = 0.50, nlege = N/A
! 9-digit precision: norder = 12, dnear = 1.00, nlege = 12
! 12-digit precision: norder = 12, dnear = 2.50, nlege = 15
!
! 9-digit precision: norder = 16, dnear = 0.50, nlege = N/A
! 12-digit precision: norder = 16, dnear = 1.00, nlege = 16
! 14-digit precision: norder = 16, dnear = 1.50, nlege = 18
!
! 10-digit precision: norder = 20, dnear = 0.50, nlege = N/A
! 12-digit precision: norder = 20, dnear = 0.75, nlege = 16
! 14-digit precision: norder = 20, dnear = 1.00, nlege = 18
!
! 12-digit precision: norder = 24, dnear = 0.50, nlege = N/A
! 14-digit precision: norder = 24, dnear = 0.75, nlege = 19
!
! 14-digit precision: norder = 30, dnear = 0.50, nlege = N/A
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
module discrect
use utils
use legendre
use sqquads
use bilege
use resquad
use nearmats
! these variables are used by the
integer, private :: norder, nquad0, nquadnear
double precision, private :: dnear
double precision, allocatable, private :: xs0(:),ys0(:),whts0(:)
double precision, allocatable, private :: umatr0(:,:), amatrnear(:,:)
double precision, allocatable, private :: xsnear(:),ysnear(:),whtsnear(:)
integer, private :: nconfigs
double precision, allocatable, private :: configs(:,:)
interface
subroutine discrect_adapfun(n,xs,ys,vals)
double precision :: xs(n),ys(n)
double complex :: vals(n)
end subroutine
end interface
contains
subroutine discrect_init()
implicit double precision (a-h,o-z)
!
! Initialize this discretization code. This subroutine must be called before
! any other subroutine in this module.
!
! Input parameters:
! N/A
!
! Output parameters:
! N.A
!
double precision, allocatable :: xslege(:),whtslege(:)
double precision, allocatable :: amatr(:,:),bmatr(:,:)
integer, allocatable :: idxs(:)
! this should get about 10-digit accuracy or thereabouts
norder = 20
dnear = 0.5d0
nlege = 18
call bilege_quad(norder,nquad0,xs0,ys0,whts0)
call bilege_coefsmatrix(norder,umatr0)
call legendre_quad(nlege,xslege,whtslege)
nquadnear = nlege*nlege
if (allocated(xsnear)) deallocate(xsnear)
if (allocated(ysnear)) deallocate(ysnear)
if (allocated(whtsnear)) deallocate(whtsnear)
allocate(xsnear(nquadnear),ysnear(nquadnear),whtsnear(nquadnear))
idx = 0
do i=1,nlege
do j=1,nlege
idx = idx+1
xsnear(idx) = xslege(i)
ysnear(idx) = xslege(j)
whtsnear(idx) = whtslege(i)*whtslege(j)
end do
end do
! build the near interpolation matrix
call bilege_intmatrix(norder,nquadnear,xsnear,ysnear,whtsnear,amatrnear)
! Fetch the possible configuations of neighbors
call resquad_configs(nconfigs,configs)
call prini("in discrect_init, norder = ",norder)
call prini("in discrect_init, nquad0 = ",nquad0)
call prin2("in discrect_init, dnear = ",dnear)
call prini("in discrect_init, nquadnear = ",nquadnear)
end subroutine
subroutine discrect_info(norder0,nquad00,dnear00)
implicit double precision (a-h,o-z)
!
! Return basic information about the quadrature scheme used by this
! discretization code.
!
! Input parameters:
! N/A
!
! Output parameters:
! norder0 - the order of the bivariate Legendre expansions on
! each leaf node
! nquad00 - the number of nodes in the quadrature used to
! represent those expansions
! dnear00 - the parameter used to determine the notion of near
! interaction for the purposes of discretization
!
norder0 = norder
nquad00 = nquad0
dnear00 = dnear
end subroutine
subroutine discrect_adap(eps,tree,fun)
implicit double precision (a-h,o-z)
type(resquadtree) :: tree
procedure(discrect_adapfun) :: fun
!
! Adaptively refine the restricted quadtree until it represents an user-supplied
! external function to a specified accuracy.
!
! Input parameters:
! eps - the desired accuracy for the adaptive discretization
! tree - the restricted quadtree structure
! fun - an external subroutine which supplies the user-specified function
!
! Output parameters:
! tree - the data structure describing the newly refined restriceted
! quadtree
!
integer, allocatable :: istack(:)
double precision, allocatable :: xs(:),ys(:),whts(:)
double complex, allocatable :: vals(:),coefs(:)
maxstack = 1000000
allocate(istack(maxstack))
allocate(xs(nquad0),ys(nquad0),whts(nquad0))
allocate(vals(nquad0),coefs(nquad0))
nleaves = tree%nleaves
nstack = nleaves
istack(1:nleaves) = tree%ileaves(1:nleaves)
do while (nstack .gt. 0)
ibox = istack(nstack)
nstack = nstack-1
call resquad_boxinfo(tree,ibox,idx,iparent,ilevel,x1,y1,x2,y2,iul,iur,ilr,ill)
if (idx .eq. 0) then
if (nstack .gt. maxstack) then
call prina("in discrect_adap, stack overflow")
stop
endif
nstack = nstack+1
istack(nstack) = iul
nstack = nstack+1
istack(nstack) = iur
nstack = nstack+1
istack(nstack) = ilr
nstack = nstack+1
istack(nstack) = ill
cycle
endif
xs = xs0*(x2-x1)/2 + (x2+x1)/2
ys = ys0*(y2-y1)/2 + (y2+y1)/2
whts = whts0*(y2-y1)/2 * (x2-x1)/2
call fun(nquad0,xs,ys,vals)
vals = vals * sqrt(whts)
call bilege_coefs(norder,vals,coefs)
coefs = coefs / maxval(abs((coefs)))
idx1 = nquad0-norder*3
idx2 = nquad0
dd = maxval(abs(coefs(idx1:idx2)))
if (dd .gt. eps) then
call resquad_split(tree,ibox,iul,iur,ilr,ill)
nstack = nstack+1
istack(nstack) = iul
nstack = nstack+1
istack(nstack) = iur
nstack = nstack+1
istack(nstack) = ilr
nstack = nstack+1
istack(nstack) = ill
endif
end do
end subroutine
subroutine discrect_uniform(tree,dsize)
implicit double precision (a-h,o-z)
type(resquadtree) :: tree
!
! Repeatedly subdivide all leaf boxes in the tree until the side lengths
! of each leaf box is less than or equal to dsize.
!
! Input parameters:
! tree - the data structure describing the restricted quadtree
! dsize - the maximum permissible size for a leaf box
!
! Output parameters:
! tree - the restricted quadtree is updated
!
integer, allocatable :: istack(:)
maxboxes = tree%maxboxes
allocate(istack(maxboxes))
nstack = tree%nleaves
istack(1:nstack) = tree%ileaves
do while( nstack > 0 )
ibox = istack(nstack)
nstack = nstack-1
ileaf = tree%boxes(ibox)%ileaf
x1 = tree%boxes(ibox)%x1
y1 = tree%boxes(ibox)%y1
x2 = tree%boxes(ibox)%x2
y2 = tree%boxes(ibox)%y2
iul = tree%boxes(ibox)%ichildul
iur = tree%boxes(ibox)%ichildur
ilr = tree%boxes(ibox)%ichildlr
ill = tree%boxes(ibox)%ichildll
dlen = x2-x1
if (ileaf .eq. 0) then
nstack = nstack+1
istack(nstack) = iul
nstack = nstack+1
istack(nstack) = iur
nstack = nstack+1
istack(nstack) = ilr
nstack = nstack+1
istack(nstack) = ill
cycle
endif
if (dlen .gt. dsize) then
call resquad_split(tree,ibox,iul,iur,ilr,ill)
nstack = nstack+1
istack(nstack) = iul
nstack = nstack+1
istack(nstack) = iur
nstack = nstack+1
istack(nstack) = ilr
nstack = nstack+1
istack(nstack) = ill
endif
end do
end subroutine
subroutine discrect_quad(tree,nquad,xs,ys,whts)
implicit double precision (a-h,o-z)
type(resquadtree) :: tree
double precision, allocatable, intent(out) :: xs(:),ys(:),whts(:)
!
! Return the quadrature used to represent the piecewise bivariate Legendre
! expansions on the entire domain R.
!
! Input parameters:
! tree - the data structure describing the restricted quadtree
!
! Output parameters:
! nquad - the number of nodes in the quadrature rules
! (xs,ys) - the coordinates of the quadrature nodes
! whts - the array of quadrature weights
!
nleaves = tree%nleaves
nquad = nleaves*nquad0
allocate(xs(nquad),ys(nquad),whts(nquad))
i1 = 1
i2 = nquad0
do idx=1,nleaves
call resquad_leafinfo(tree,idx,ibox,ilevel,x1,y1,x2,y2)
xs(i1:i2) = xs0*(x2-x1)/2 + (x2+x1)/2
ys(i1:i2) = ys0*(y2-y1)/2 + (y2+y1)/2
whts(i1:i2) = whts0*(x2-x1)/2 * (y2-y1)/2
i1 = i1+nquad0
i2 = i2+nquad0
end do
end subroutine
subroutine discrect_quadleaf(tree,idx,nquad,xs,ys,whts)
implicit double precision (a-h,o-z)
type(resquadtree) :: tree
double precision, allocatable, intent(out) :: xs(:),ys(:),whts(:)
!
! Return the quadrature used to represent expansions on a specified
! leaf node.
!
! Input parameters:
! tree - the data structure describing the restricted quadtree
!
! Output parameters:
! nquad - the number of nodes in the quadrature rules
! (xs,ys) - the coordinates of the quadrature nodes
! whts - the array of quadrature weights
!
allocate(xs(nquad0),ys(nquad0),whts(nquad0))
call resquad_leafinfo(tree,idx,ibox,ilevel,x1,y1,x2,y2)
xs = xs0*(x2-x1)/2 + (x2+x1)/2
ys = ys0*(y2-y1)/2 + (y2+y1)/2
whts = whts0*(x2-x1)/2 * (y2-y1)/2
end subroutine
subroutine discrect_coefs(tree,vals,coefs)
implicit double precision (a-h,o-z)
type(resquadtree) :: tree
double complex :: vals(:), coefs(:)
!
! Given the vector of scaled values of a piecewise bivariate expansion at
! the nodes of the discretization quadrature, compute the piecewise
! coefficient expansions.
!
! Input parameters:
! tree - the data structure describing the restricted quadtree
! vals - the vector of scaled values
!
! Output parameters:
! coefs - the vector of expansion coefficients
!
call resquad_info(tree,nlevels,nboxes,nleaves)
do idx=1,nleaves
i1 = 1 + (idx-1)*nquad0
i2 = idx*nquad0
call resquad_leafinfo(tree,idx,ibox,ilevel,x1,y1,x2,y2)
call bilege_coefs(norder,vals(i1:i2),coefs(i1:i2))
end do
end subroutine
subroutine discrect_eval(tree,coefs,x,y,val)
implicit double precision (a-h,o-z)
type(resquadtree) :: tree
double complex :: coefs(:), val
!
! Given the coefficients in a piecewise expansion, compute its value at
! a specified point.
!
! Input parameters:
! tree - the data structure describing the restricted quadtree
! coefs - the vector of expansion coefficients
! (x,y) - the point at which to evaluate the expansion
!
! Output parameters:
! val - the value of the expansion at the point (x,y)
!
double complex, allocatable :: vals0(:),coefs0(:)
call resquad_search(tree,x,y,idx)
call resquad_leafinfo(tree,idx,ibox,ilevel,x1,y1,x2,y2)
i1 = 1+nquad0*(idx-1)
i2 = nquad0*idx
xx = (2*x - (x2+x1) ) /(x2-x1)
yy = (2*y - (y2+y1) ) /(y2-y1)
call bilege_eval(norder,coefs(i1:i2),xx,yy,val)
val = val * 2/(x2-x1)
end subroutine
subroutine discrect_isnear(tree,itarbox,isrcbox,ifnear)
implicit double precision (a-h,o-z)
type(resquadtree) :: tree
!
! Determine whether or not a specified target box (which need not be a leaf) is near to
! a specified leaf box in the sense that a special quadrature is used to evaluate
! the interaction.
!
! Input parameter:
! tree - a data structure describing the restricted quadtree
! itarbox - the index of the target box
! isrcbox - the index of the source leaf box
!
! Ouptut parameter:
! ifnear - 0 if the target box is not near and 1 if it is
!
!
x1 = tree%boxes(isrcbox)%x1
y1 = tree%boxes(isrcbox)%y1
x2 = tree%boxes(isrcbox)%x2
y2 = tree%boxes(isrcbox)%y2
dlen = x2-x1
x1 = x1 - dlen*dnear
x2 = x2 + dlen*dnear
y1 = y1 - dlen*dnear
y2 = y2 + dlen*dnear
z1 = tree%boxes(itarbox)%x1
w1 = tree%boxes(itarbox)%y1
z2 = tree%boxes(itarbox)%x2
w2 = tree%boxes(itarbox)%y2
ifnear = 1
if (z2 .le. x1) ifnear = 0
if (z1 .ge. x2) ifnear = 0
if (w1 .ge. y2) ifnear = 0
if (w2 .le. y1) ifnear = 0
end subroutine
subroutine discrect_evalop_matrix(tree,ikernel,amatr)
implicit double precision (a-h,o-z)
type(resquadtree) :: tree
double precision, allocatable, intent(out) :: amatr(:,:)
!
! Return the entire matrix discretizing one of the operators (1), (2) or (3).
!
! Input parameters:
! tree - the data structure describing the restricted quadtree
! ikernel - an integer parameter indicating which operator is to be
! evaluated:
!
! ikernel = 1 means evaluate a submatrix of the discretization of (1)
! ikernel = 2 means evaluate a submatrix of the discretization of (2)
! ikernel = 3 means evaluate a submatrix of the discretization of (3)
!
! Output parameters:
! amatr - the desired discretization matrix
!
!
integer, allocatable :: idxs(:)
call resquad_info(tree,nlevels,nboxes,nleaves)
nquad = nquad0*nleaves
allocate(amatr(nquad,nquad))
do itar=1,nleaves
do isrc=1,nleaves
i1 = 1 + (itar-1)*nquad0
i2 = itar*nquad0
j1 = 1 + (isrc-1)*nquad0
j2 = isrc*nquad0
call discrect_evalop_block(tree,ikernel,itar,isrc,amatr(i1:i2,j1:j2))
end do
end do
end subroutine
subroutine discrect_evalop_matrix_mt(tree,ikernel,amatr)
implicit double precision (a-h,o-z)
type(resquadtree) :: tree
double precision, allocatable, intent(out) :: amatr(:,:)
!
! Return the entire matrix discretizing one of the operators (1), (2) or (3).
! This routine is multithreaded.
!
! Input parameters:
! tree - the data structure describing the restricted quadtree
! ikernel - an integer parameter indicating which operator is to be
! evaluated:
!
! ikernel = 1 means evaluate a submatrix of the discretization of (1)
! ikernel = 2 means evaluate a submatrix of the discretization of (2)
! ikernel = 3 means evaluate a submatrix of the discretization of (3)
!
! Output parameters:
! amatr - the desired discretization matrix
!
call resquad_info(tree,nlevels,nboxes,nleaves)
nquad = nquad0*nleaves
allocate(amatr(nquad,nquad))
!$OMP PARALLEL DEFAULT(SHARED) PRIVATE(idx,isrc,itar,i1,i2,j1,j2)
!$OMP DO
do idx=0,nleaves*nleaves-1
isrc = mod(idx,nleaves)
itar = (idx-isrc)/nleaves
isrc = isrc+1
itar = itar+1
i1 = 1 + (itar-1)*nquad0
i2 = itar*nquad0
j1 = 1 + (isrc-1)*nquad0
j2 = isrc*nquad0
call discrect_evalop_block(tree,ikernel,itar,isrc,amatr(i1:i2,j1:j2))
end do
!$OMP END DO
!$OMP END PARALLEL
end subroutine
subroutine discrect_applyop(tree,ikernel,xx,yy)
implicit double precision (a-h,o-z)
type(resquadtree) :: tree
double complex :: xx(:),yy(:)
!
! Apply the matrix discretizing one of the operators (1), (2) or (3) to a
! user-supplied vector.
!
! Input parameters:
! tree - the data structure describing the restricted quadtree
! ikernel - an integer parameter indicating which operator is to be
! evaluated:
!
! ikernel = 1 means evaluate a submatrix of the discretization of (1)
! ikernel = 2 means evaluate a submatrix of the discretization of (2)
! ikernel = 3 means evaluate a submatrix of the discretization of (3)
!
! xx - the vector to which the discretization matrix should be applied
!
! Output parameters:
! yy - the resulting vector
!
!
double precision, allocatable :: amatr(:,:)
double complex, allocatable :: zz(:)
call resquad_info(tree,nlevels,nboxes,nleaves)
allocate(amatr(nquad0,nquad0), zz(nquad0) )
yy = 0
do itar=1,nleaves
do isrc=1,nleaves
i1 = 1 + (itar-1)*nquad0
i2 = itar*nquad0
j1 = 1 + (isrc-1)*nquad0
j2 = isrc*nquad0
call discrect_evalop_block(tree,ikernel,itar,isrc,amatr)
zz = matmul(amatr,xx(j1:j2))
yy(i1:i2) = yy(i1:i2) + zz
end do
end do
end subroutine
subroutine discrect_applyop_mt(tree,ikernel,xx,yy)
implicit double precision (a-h,o-z)
type(resquadtree) :: tree
double complex :: xx(:),yy(:)
!
! Apply the matrix discretizing one of the operators (1), (2) or (3) to a
! user-supplied vector.
!
! Input parameters:
! tree - the data structure describing the restricted quadtree
! ikernel - an integer parameter indicating which operator is to be
! evaluated:
!
! ikernel = 1 means evaluate a submatrix of the discretization of (1)
! ikernel = 2 means evaluate a submatrix of the discretization of (2)
! ikernel = 3 means evaluate a submatrix of the discretization of (3)
!
! xx - the vector to which the discretization matrix should be applied
!
! Output parameters:
! yy - the resulting vector
!
!
double precision, allocatable :: amatr(:,:)
integer, allocatable :: itars(:),isrcs(:)
double complex, allocatable :: zz(:)
call resquad_info(tree,nlevels,nboxes,nleaves)
yy = 0
!$OMP PARALLEL DEFAULT(SHARED) PRIVATE(amatr,idx,isrc,itar,i1,i2,j1,j2,i,zz)
allocate(amatr(nquad0,nquad0), zz(nquad0) )
!$OMP DO REDUCTION(+:yy)
do idx=0,nleaves*nleaves-1
isrc = mod(idx,nleaves)
itar = (idx-isrc)/nleaves
isrc = isrc+1
itar = itar+1
i1 = 1 + (itar-1)*nquad0
i2 = itar*nquad0
j1 = 1 + (isrc-1)*nquad0
j2 = isrc*nquad0
call discrect_evalop_block(tree,ikernel,itar,isrc,amatr)
zz = matmul(amatr,xx(j1:j2))
yy(i1:i2) = yy(i1:i2) + zz
end do
!$OMP END DO
!$OMP END PARALLEL
end subroutine
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
! Operator evaluation routines
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
subroutine discrect_evalop(tree,ikernel,ntars,itars,nsrcs,isrcs,amatr)
implicit double precision (a-h,o-z)
type(resquadtree) :: tree
integer :: itars(ntars), isrcs(nsrcs)
double precision :: amatr(:,:)
!
! Evaluate an arbitrary submatrix of the matrix discretizing one of the
! operators (1), (2) or (3).
!
! Input parameters:
! tree - the data structure describing the restricted quadtree
! ikernel - an integer parameter indicating which operator is to be
! evaluated:
!
! ikernel = 1 means evaluate a submatrix of the discretization of (1)
! ikernel = 2 means evaluate a submatrix of the discretization of (2)
! ikernel = 3 means evaluate a submatrix of the discretization of (3)
!
! ntars - the number of targets
! itars - the list of targets
! nsrcs - the number of sources
! isrcs - the list of sources
!
! Output parameters:
! amatr - the (ntars,nsrcs) matrix
data pi / 3.14159265358979323846264338327950288d0 /
integer, allocatable :: itars00(:), itars0(:), itarsidxs(:)
integer, allocatable :: isrcs00(:), isrcs0(:), isrcsidxs(:)
double precision, allocatable :: cmatr(:,:)
!allocate(cmatr(nquad0,nquad0))
!
! Sort the lists of target and source points; keep track of the
! ordering.
!
allocate(itars00(ntars), itars0(ntars), itarsidxs(ntars))
allocate(isrcs00(nsrcs), isrcs0(nsrcs), isrcsidxs(nsrcs))
itars0 = itars
isrcs0 = isrcs
call quicksorti2(ntars,itars0,itarsidxs)
call quicksorti2(nsrcs,isrcs0,isrcsidxs)
i1 = 0
i2 = 0
!
! Loop over all target boxes
!
do while(i2 .lt. ntars)
i1 = i2+1
itarbox = (itars0(i1)-1)/nquad0+1
i3 = 1+(itarbox-1)*nquad0
i4 = (itarbox)*nquad0
do while(i2 .lt. ntars)
if( itars0(i2+1) .le. i4) then
i2=i2+1
else
exit
endif
end do
!
! Loop over all source boxes
!
j1 = 0
j2 = 0
do while(j2 .lt. nsrcs)
j1 = j2+1
isrcbox = (isrcs0(j1)-1)/nquad0+1
j3 = 1+(isrcbox-1)*nquad0
j4 = (isrcbox)*nquad0
do while(j2 .lt. nsrcs)
if( isrcs0(j2+1) .le. j4) then
j2=j2+1
else
exit
endif
end do
!
! Call a utility routine to evaluate the appropriate submatrix for this target
! and this source
!
nn = i2-i1+1
mm = j2-j1+1
itars00(1:nn) = itars0(i1:i2)-i3+1
isrcs00(1:mm) = isrcs0(j1:j2)-j3+1
allocate(cmatr(nn,mm))
call discrect_evalop0(tree,ikernel,itarbox,isrcbox,nn,itars00,mm,isrcs00,cmatr)
amatr(itarsidxs(i1:i2),isrcsidxs(j1:j2)) = cmatr
deallocate(cmatr)
end do
end do
end subroutine
subroutine discrect_evalopt(tree,ikernel,ntars,itars,nsrcs,isrcs,amatr)
implicit double precision (a-h,o-z)
type(resquadtree) :: tree
integer :: itars(ntars), isrcs(nsrcs)
double precision :: amatr(:,:)
!
! Evaluate the transpose of an arbitrary submatrix of the matrix discretizing
! one of the operators (1), (2) or (3).
!
! Input parameters:
! tree - the data structure describing the restricted quadtree
! ikernel - an integer parameter indicating which operator is to be
! evaluated:
!
! ikernel = 1 means evaluate a submatrix of the discretization of (1)
! ikernel = 2 means evaluate a submatrix of the discretization of (2)
! ikernel = 3 means evaluate a submatrix of the discretization of (3)
!
! ntars - the number of targets
! itars - the list of targets
! nsrcs - the number of sources
! isrcs - the list of sources
!
! Output parameters:
! amatr - the (ntars,nsrcs) matrix
data pi / 3.14159265358979323846264338327950288d0 /
integer, allocatable :: itars00(:), itars0(:), itarsidxs(:)
integer, allocatable :: isrcs00(:), isrcs0(:), isrcsidxs(:)
double precision, allocatable :: cmatr(:,:)
!
! Sort the lists of target and source points; keep track of the
! ordering.
!
allocate(itars00(ntars), itars0(ntars), itarsidxs(ntars))
allocate(isrcs00(nsrcs), isrcs0(nsrcs), isrcsidxs(nsrcs))
itars0 = itars
isrcs0 = isrcs
call quicksorti2(ntars,itars0,itarsidxs)
call quicksorti2(nsrcs,isrcs0,isrcsidxs)
i1 = 0
i2 = 0
!
! Loop over all target boxes
!
do while(i2 .lt. ntars)
i1 = i2+1
itarbox = (itars0(i1)-1)/nquad0+1
i3 = 1+(itarbox-1)*nquad0
i4 = (itarbox)*nquad0
do while(i2 .lt. ntars)
if( itars0(i2+1) .le. i4) then
i2=i2+1
else
exit
endif
end do
!
! Loop over all source boxes
!
j1 = 0
j2 = 0
do while(j2 .lt. nsrcs)
j1 = j2+1
isrcbox = (isrcs0(j1)-1)/nquad0+1
j3 = 1+(isrcbox-1)*nquad0
j4 = (isrcbox)*nquad0
do while(j2 .lt. nsrcs)
if( isrcs0(j2+1) .le. j4) then
j2=j2+1
else
exit
endif