Blame view

mstm-modules-v2.2.f90 237 KB
de89d3bc   dmayerich   Initial commit.
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
  !

  !  numerical constants

  !

  !

  !  last revised: 15 January 2011

  !

        module numconstants

        implicit none

        integer :: print_intermediate_results

        integer, allocatable :: monen(:)

        integer, private :: nmax=0

        real(8) :: pi

        real(8), allocatable :: bcof(:,:),fnr(:),vwh_coef(:,:,:,:)

        real(8), allocatable :: vcc_const(:,:,:),fnm1_const(:,:),fn_const(:,:),fnp1_const(:,:)

        data pi/3.141592653589793/

  

        contains

  

           subroutine init(notd)

           implicit none

           integer :: notd,l,n,ierr,nbc,m,mm1,mp1,np1,nm1,nn1,mn

           real(8) :: fnorm1,fnorm2

  !

  !  bcof(n,l)=((n+l)!/(n!l!))^(1/2)

  !

           if(notd.le.nmax) return

           nmax=max(nmax,notd)

           nbc=6*notd+6

           if(allocated(fnr)) deallocate(monen,fnr,bcof)

           allocate (monen(0:2*notd),bcof(0:nbc,0:nbc),fnr(0:2*nbc),stat=ierr)

  !         write(*,'('' nmax, bcof status:'',2i5)') nmax,ierr

           do n=0,2*notd

              monen(n)=(-1)**n

           enddo

           fnr(0)=0.d0

           do n=1,2*nbc

              fnr(n)=dsqrt(dble(n))

           enddo

           bcof(0,0)=1.d0

           do n=0,nbc-1

              do l=n+1,nbc

                 bcof(n,l)=fnr(n+l)*bcof(n,l-1)/fnr(l)

                 bcof(l,n)=bcof(n,l)

              enddo

              bcof(n+1,n+1)=fnr(n+n+2)*fnr(n+n+1)*bcof(n,n)/fnr(n+1)/fnr(n+1)

           enddo

           if(allocated(vwh_coef)) deallocate(vwh_coef)

           allocate(vwh_coef(-notd:notd,1:notd,-1:1,-1:1))

  !

  !  constants used for calculation of svwf functions.

  !

           do n=1,notd

              nn1=n*(n+1)

              np1=n+1

              nm1=n-1

              fnorm1=-.5d0/fnr(n+n+1)/fnr(n)/fnr(n+1)

              fnorm2=-.5d0*fnr(n+n+1)/fnr(n)/fnr(n+1)

              m=-n

              mp1=m+1

              mm1=m-1

              vwh_coef(m,n, 1, 1)=-fnorm1*n*fnr(np1+m)*fnr(np1+mp1)

              vwh_coef(m,n, 1,-1)=fnorm1*np1*fnr(n-m)*fnr(nm1-m)

              vwh_coef(m,n,-1, 1)=fnorm1*n*fnr(np1-m)*fnr(np1-mm1)

              vwh_coef(m,n,-1,-1)=0.d0

              vwh_coef(m,n, 0, 1)=fnorm1*n*fnr(np1+m)*fnr(np1-m)

              vwh_coef(m,n, 0,-1)=0.d0

              vwh_coef(m,n, 1, 0)=-fnorm2*fnr(n-m)*fnr(np1+m)

              vwh_coef(m,n,-1, 0)=-0.d0

              vwh_coef(m,n, 0, 0)=-fnorm2*m

              do m=-n+1,-1

                 mp1=m+1

                 mm1=m-1

                 vwh_coef(m,n, 1, 1)=-fnorm1*n*fnr(np1+m)*fnr(np1+mp1)

                 vwh_coef(m,n, 1,-1)=fnorm1*np1*fnr(n-m)*fnr(nm1-m)

                 vwh_coef(m,n,-1, 1)=fnorm1*n*fnr(np1-m)*fnr(np1-mm1)

                 vwh_coef(m,n,-1,-1)=-fnorm1*np1*fnr(n+m)*fnr(nm1+m)

                 vwh_coef(m,n, 0, 1)=fnorm1*n*fnr(np1+m)*fnr(np1-m)

                 vwh_coef(m,n, 0,-1)=fnorm1*np1*fnr(n+m)*fnr(n-m)

                 vwh_coef(m,n, 1, 0)=-fnorm2*fnr(n-m)*fnr(np1+m)

                 vwh_coef(m,n,-1, 0)=-fnorm2*fnr(n+m)*fnr(np1-m)

                 vwh_coef(m,n, 0, 0)=-fnorm2*m

              enddo

              do m=0,n-1

                 mp1=m+1

                 mm1=m-1

                 vwh_coef(m,n, 1, 1)=-fnorm1*n*fnr(np1+m)*fnr(np1+mp1)

                 vwh_coef(m,n, 1,-1)=fnorm1*np1*fnr(n-m)*fnr(nm1-m)

                 vwh_coef(m,n,-1, 1)=fnorm1*n*fnr(np1-m)*fnr(np1-mm1)

                 vwh_coef(m,n,-1,-1)=-fnorm1*np1*fnr(n+m)*fnr(nm1+m)

                 vwh_coef(m,n, 0, 1)=fnorm1*n*fnr(np1+m)*fnr(np1-m)

                 vwh_coef(m,n, 0,-1)=fnorm1*np1*fnr(n+m)*fnr(n-m)

                 vwh_coef(m,n, 1, 0)=-fnorm2*fnr(n-m)*fnr(np1+m)

                 vwh_coef(m,n,-1, 0)=-fnorm2*fnr(n+m)*fnr(np1-m)

                 vwh_coef(m,n, 0, 0)=-fnorm2*m

              enddo

              m=n

              mp1=m+1

              mm1=m-1

              vwh_coef(m,n, 1, 1)=-fnorm1*n*fnr(np1+m)*fnr(np1+mp1)

              vwh_coef(m,n, 1,-1)=0.d0

              vwh_coef(m,n,-1, 1)=fnorm1*n*fnr(np1-m)*fnr(np1-mm1)

              vwh_coef(m,n,-1,-1)=-fnorm1*np1*fnr(n+m)*fnr(nm1+m)

              vwh_coef(m,n, 0, 1)=fnorm1*n*fnr(np1+m)*fnr(np1-m)

              vwh_coef(m,n, 0,-1)=0.d0

              vwh_coef(m,n, 1, 0)=-0.d0

              vwh_coef(m,n,-1, 0)=-fnorm2*fnr(n+m)*fnr(np1-m)

              vwh_coef(m,n, 0, 0)=-fnorm2*m

           enddo

           end subroutine init

  

        end module numconstants

  !

  !  special function for the multiple sphere problem

  !

        module specialfuncs

        implicit none

        contains

  

           subroutine timewrite(iunit,char1,time)

           use intrinsics

           implicit none

           integer :: iunit

           real(8) :: time,time2

           character(*) :: char1

           if(time.gt.3600.d0) then

              time2=time/3600.d0

              write(iunit,'(a,f9.3,'' hours'')') char1,time2

           elseif(time.gt.60.d0) then

              time2=time/60.d0

              write(iunit,'(a,f9.2,'' min'')') char1,time2

           else

              write(iunit,'(a,f9.2,'' sec'')') char1,time

           endif

           call flush(iunit)

           end subroutine timewrite

  !

  !  ricatti-bessel function psi(n), real argument

  !

           subroutine ricbessel(n,ds,eps,nmax,psi)

           implicit none

           integer :: n,nmax,ns,i

           real(8) :: ds,dns,sn,psi(0:n),psit,ds2,sum,eps,err

           if(int(ds).lt.n) then

              ns=nint(ds+4.*(ds**.3333d0)+17)

              ns=max(n+10,ns)

              dns=0.d0

              do i=ns-1,n,-1

                 sn=dble(i+1)/ds

                 dns=sn-1.d0/(dns+sn)

              enddo

              psi(n)=dns

              psi(n-1)=dble(n)/ds-1.d0/(dns+dble(n)/ds)

              do i=n-2,1,-1

                 sn=dble(i+1)/ds

                 psi(i)=sn-1.d0/(psi(i+1)+sn)

              enddo

              psit=dsin(ds)

              psi(0)=psit

              ds2=ds*ds

              sum=psit*psit/ds2

              do i=1,n

                 psit=psit/(dble(i)/ds+psi(i))

                 sum=sum+dble(i+i+1)*psit*psit/ds2

                 err=dabs(1.d0-sum)

                 psi(i)=psit

                 if(err.lt.eps) then

                    nmax=i

                    return

                 endif

              enddo

              nmax=n

           else

              psi(0)=dsin(ds)

              psi(1)=psi(0)/ds-dcos(ds)

              do i=1,n-1

                 sn=dble(i+i+1)/ds

                 psi(i+1)=sn*psi(i)-psi(i-1)

              enddo

              nmax=n

           endif

           end subroutine ricbessel

  !

  !  ricatti-hankel function xi(n), real argument

  !

  !

  !  last revised: 15 January 2011

  !

           subroutine richankel(n,ds,xi)

           implicit none

           integer :: n,i,ns

           real(8) :: ds,dns,sn,chi0,chi1,chi2,psi,psi0,psi1

           complex(8) :: xi(0:n)

           if(int(ds).lt.n) then

              ns=nint(ds+4.*(ds**.3333)+17)

              ns=max(n+10,ns)

              dns=0.d0

              do i=ns-1,n,-1

                 sn=dble(i+1)/ds

                 dns=sn-1.d0/(dns+sn)

              enddo

              xi(n)=dns

              xi(n-1)=dble(n)/ds-1.d0/(dns+dble(n)/ds)

              do i=n-2,1,-1

                 sn=dble(i+1)/ds

                 xi(i)=sn-1.d0/(xi(i+1)+sn)

              enddo

              chi0=-dcos(ds)

              psi=dsin(ds)

              chi1=chi0/ds-psi

              xi(0)=dcmplx(psi,chi0)

              do i=1,n

                 chi2=dble(i+i+1)/ds*chi1-chi0

                 psi=psi/(dble(i)/ds+xi(i))

                 xi(i)=dcmplx(psi,chi1)

                 chi0=chi1

                 chi1=chi2

              enddo

              return

           else

              chi0=-dcos(ds)

              psi0=dsin(ds)

              chi1=chi0/ds-psi0

              psi1=psi0/ds+chi0

              xi(0)=dcmplx(psi0,chi0)

              xi(1)=dcmplx(psi1,chi1)

              do i=1,n-1

                 sn=dble(i+i+1)/ds

                 xi(i+1)=sn*xi(i)-xi(i-1)

              enddo

              return

           endif

           end subroutine richankel

  !

  !  ricatti-bessel function psi(n), complex argument

  !

  !

  !  last revised: 15 January 2011

  !

           subroutine cricbessel(n,ds,psi)

           implicit none

           integer :: n,i

           complex(8) :: ds,psi(0:n),chi(0:n)

           call cspherebessel(n,ds,psi,chi)

           do i=0,n

              psi(i)=psi(i)*ds

           enddo

           return

           end subroutine cricbessel

  !

  !  ricatti-hankel function psi(n), complex argument

  !

  !

  !  last revised: 15 January 2011

  !  7 october 2011: forces upwards recurrence for real argument ds

  !

           subroutine crichankel(n,ds,xi)

           implicit none

           integer :: n,i,i1

           complex(8) :: ds,psi(0:n),chi(0:n),xi(0:n),ci

           data ci/(0.d0,1.d0)/

           xi(0)=-ci*cdexp(ci*ds)

           xi(1)=-cdexp(ci*ds)*(ci+ds)/ds

           if(dimag(ds).eq.0.d0) then

              do i=1,n-1

                 i1=i+1

                 xi(i1)=dble(i+i1)/ds*xi(i)-xi(i-1)

              enddo

              return

           endif

           if(cdabs(xi(0)).lt.1.d-10) then

              do i=1,n-1

                 i1=i+1

                 xi(i1)=dble(i+i1)/ds*xi(i)-xi(i-1)

              enddo

              return

           else

              call cspherebessel(n,ds,psi,chi)

              do i=1,n-1

                 i1=i+1

                 xi(i1)=(psi(i1)+ci*chi(i1))*ds

              enddo

              return

           endif

           end subroutine crichankel

  !

  !     ==========================================================

  !     Purpose: Compute spherical Bessel functions jn(z) & yn(z)

  !              for a complex argument

  !     Input :  z --- Complex argument

  !              n --- Order of jn(z) & yn(z) ( n = 0,1,2,... )

  !     Output:  CSJ(n) --- jn(z)

  !              CSY(n) --- yn(z)

  !              NM --- Highest order computed

  !     Routines called:

  !              MSTA1 and MSTA2 for computing the starting

  !              point for backward recurrence

  !     ==========================================================

  !

  !    obtained from, and copywrited by, Jian-Ming Jin

  !    http://jin.ece.uiuc.edu/

  !

  !

  !  last revised: 15 January 2011

  !

           subroutine cspherebessel(n,z,csj,csy)

           implicit none

           integer :: n,nm,k,m

           real(8) :: a0

           complex(8) :: z,csj(0:n),csy(0:n),csa,csb,cs,cf0,cf1,cf

           a0=cdabs(z)

           nm=n

           if (a0.lt.1.0d-60) then

              csj=(0.d0,0.d0)

              csy=(-1.d300,0.d0)

              csy(0)=(1.d0,0.d0)

              return

           endif

           csj=(0.d0,0.d0)

           csj(0)=cdsin(z)/z

           csj(1)=(csj(0)-cdcos(z))/z

           if (n.ge.2) then

              csa=csj(0)

              csb=csj(1)

              m=msta1(a0,200)

              if (m.lt.n) then

                 nm=m

              else

                 m=msta2(a0,n,15)

              endif

              cf0=0.0d0

              cf1=1.0d0-100

              do k=m,0,-1

                 cf=(2.0d0*k+3.0d0)*cf1/z-cf0

                 if (k.le.nm) csj(k)=cf

                 cf0=cf1

                 cf1=cf

              enddo

              if (cdabs(csa).gt.cdabs(csb)) cs=csa/cf

              if (cdabs(csa).le.cdabs(csb)) cs=csb/cf0

              do k=0,min(nm,n)

                 csj(k)=cs*csj(k)

              enddo

           endif

           csy=(1.d200,0.d0)

           csy(0)=-cdcos(z)/z

           csy(1)=(csy(0)-cdsin(z))/z

           do k=2,min(nm,n)

              if (cdabs(csj(k-1)).gt.cdabs(csj(k-2))) then

                 csy(k)=(csj(k)*csy(k-1)-1.0d0/(z*z))/csj(k-1)

              else

                 csy(k)=(csj(k)*csy(k-2)-(2.0d0*k-1.0d0)/z**3)/csj(k-2)

              endif

           enddo

           end subroutine cspherebessel

  !

  !     ===================================================

  !     Purpose: Determine the starting point for backward

  !              recurrence such that the magnitude of

  !              Jn(x) at that point is about 10^(-MP)

  !     Input :  x     --- Argument of Jn(x)

  !              MP    --- Value of magnitude

  !     Output:  MSTA1 --- Starting point

  !     ===================================================

  !

  !

  !  last revised: 15 January 2011

  !

           integer function msta1(x,mp)

           implicit none

           integer :: mp,n0,n1,it,nn

           real(8) :: x, a0,f1,f,f0

           a0=dabs(x)

           n0=int(1.1*a0)+1

           f0=envj(n0,a0)-mp

           n1=n0+5

           f1=envj(n1,a0)-mp

           do it=1,20

              nn=n1-(n1-n0)/(1.0d0-f0/f1)

              f=envj(nn,a0)-mp

              if(abs(nn-n1).lt.1) exit

              n0=n1

              f0=f1

              n1=nn

              f1=f

           enddo

           msta1=nn

           end function msta1

  !

  !     ===================================================

  !     Purpose: Determine the starting point for backward

  !              recurrence such that all Jn(x) has MP

  !              significant digits

  !     Input :  x  --- Argument of Jn(x)

  !              n  --- Order of Jn(x)

  !              MP --- Significant digit

  !     Output:  MSTA2 --- Starting point

  !     ===================================================

  !

  !

  !  last revised: 15 January 2011

  !

           integer function msta2(x,n,mp)

           implicit none

           integer :: n,mp,n0,n1,it,nn

           real(8) :: x,a0,hmp,ejn,obj,f0,f1,f

           a0=dabs(x)

           hmp=0.5d0*dble(mp)

           ejn=envj(n,a0)

           if (ejn.le.hmp) then

              obj=mp

              n0=int(1.1*a0)

           else

              obj=hmp+ejn

              n0=n

           endif

           f0=envj(n0,a0)-obj

           n1=n0+5

           f1=envj(n1,a0)-obj

           do it=1,20

              nn=n1-(n1-n0)/(1.0d0-f0/f1)

              f=envj(nn,a0)-obj

              if (abs(nn-n1).lt.1) exit

              n0=n1

              f0=f1

              n1=nn

              f1=f

           enddo

           msta2=nn+10

           end function msta2

  

           real(8) function envj(n,x)

           implicit none

           integer :: n

           real(8) :: x

           n=max(1,abs(n))

           envj=0.5d0*dlog10(6.28d0*n)-n*dlog10(1.36d0*x/n)

           end function envj

  !

  !    vector coupling coefficients vc(w) = C(m,n|k,l|m+k,w), w = |n-l|,... n+l

  !    uses downwards and upwards recurrence

  !

  !

  !  last revised: 15 January 2011

  !

           subroutine vcfunc(m,n,k,l,vcn)

           use numconstants

           implicit none

           integer :: m,n,k,l,wmax,wmin,w,mk

           real(8) :: vcn(0:n+l),t1,t2,t3,vcmax,vctest,rat

           vcn=0.d0

           wmax=n+l

           wmin=max(abs(n-l),abs(m+k))

           vcn(wmax)=bcof(n+m,l+k)*bcof(n-m,l-k)/bcof(n+n,l+l)

           if(wmin.eq.wmax) return

           vcn(wmax-1)=vcn(wmax)*(l*m-k*n)*fnr(2*(l+n)-1)/fnr(l)/fnr(n)&

          &  /fnr(n+l+m+k)/fnr(n+l-m-k)

           if(wmin.eq.wmax-1) return

           mk=m+k

           vcmax=abs(vcn(wmax))+abs(vcn(wmax-1))

  !

  !  a downwards recurrence is used initially

  !

           do w=wmax,wmin+2,-1

              t1=2*w*fnr(w+w+1)*fnr(w+w-1)/(fnr(w+mk)*fnr(w-mk)&

          &     *fnr(n-l+w)*fnr(l-n+w)*fnr(n+l-w+1)*fnr(n+l+w+1))

              t2=dble((m-k)*w*(w-1)-mk*n*(n+1)+mk*l*(l+1))&

          &    /dble(2*w*(w-1))

              t3=fnr(w-mk-1)*fnr(w+mk-1)*fnr(l-n+w-1)*fnr(n-l+w-1)&

          &     *fnr(n+l-w+2)*fnr(n+l+w)/(dble(2*(w-1))*fnr(2*w-3)&

          &     *fnr(2*w-1))

              vcn(w-2)=(t2*vcn(w-1)-vcn(w)/t1)/t3

              if(mod(wmax-w,2).eq.1) then

                 vctest=abs(vcn(w-2))+abs(vcn(w-1))

                 vcmax=max(vcmax,vctest)

                 rat=vctest/vcmax

  !

  !  if/when the coefficients start to decrease in magnitude, an upwards recurrence takes over

  !

                 if(rat.lt.0.01d0) exit

              endif

           enddo

           if(w-2.gt.wmin) then

              wmax=w-3

              call vcfuncuprec(m,n,k,l,wmax,vcn)

           endif

           end subroutine vcfunc

  !

  !  upwards VC coefficient recurrence

  !

  !

  !  last revised: 15 January 2011

  !

           subroutine vcfuncuprec(m,n,k,l,wmax,vcn)

           use numconstants

           implicit none

           integer :: m,n,k,l,wmax,wmin,w,mk,nl,m1,n1,l1,k1,w1,w2

           real(8) :: vcn(0:n+l),t1,t2,t3,vc1

           mk=abs(m+k)

           nl=abs(n-l)

           if(nl.ge.mk) then

              w=nl

              if(n.ge.l) then

                 m1=m

                 n1=n

                 l1=l

                 k1=k

              else

                 m1=k

                 n1=l

                 k1=m

                 l1=n

              endif

              vc1=(-1)**(k1+l1)*bcof(l1+k1,w-m1-k1) &

                 *bcof(l1-k1,w+m1+k1)/bcof(l1+l1,w+w+1)

           else

              w=mk

              if(m+k.ge.0) then

                 vc1=(-1)**(n+m)*bcof(n-l+w,l-k)*bcof(l-n+w,n-m) &

                    /bcof(w+w+1,n+l-w)

              else

                 vc1=(-1)**(l+k)*bcof(n-l+w,l+k)*bcof(l-n+w,n+m) &

                   /bcof(w+w+1,n+l-w)

              endif

           endif

           w1=w

           vcn(w)=vc1

           w=w1+1

           mk=m+k

           w2=min(wmax,n+l)

           if(w2.gt.w1) then

              t1=2*w*fnr(w+w+1)*fnr(w+w-1)/(fnr(w+mk)*fnr(w-mk) &

                *fnr(n-l+w)*fnr(l-n+w)*fnr(n+l-w+1)*fnr(n+l+w+1))

              if(w1.eq.0) then

                 t2=.5*dble(m-k)

              else

                 t2=dble((m-k)*w*(w-1)-mk*n*(n+1)+mk*l*(l+1)) &

                   /dble(2*w*(w-1))

              endif

              vcn(w)=t1*t2*vcn(w1)

           endif

           do w=w1+2,w2

              t1=2*w*fnr(w+w+1)*fnr(w+w-1)/(fnr(w+mk)*fnr(w-mk) &

                *fnr(n-l+w)*fnr(l-n+w)*fnr(n+l-w+1)*fnr(n+l+w+1))

              t2=dble((m-k)*w*(w-1)-mk*n*(n+1)+mk*l*(l+1)) &

               /dble(2*w*(w-1))

              t3=fnr(w-mk-1)*fnr(w+mk-1)*fnr(l-n+w-1)*fnr(n-l+w-1) &

                *fnr(n+l-w+2)*fnr(n+l+w)/(dble(2*(w-1))*fnr(2*w-3) &

                *fnr(2*w-1))

              vcn(w)=t1*(t2*vcn(w-1)-t3*vcn(w-2))

           enddo

           end subroutine vcfuncuprec

  !

  !  Normalized associated legendre functions

  !

  !

  !  last revised: 15 January 2011

  !

           subroutine normalizedlegendre(cbe,mmax,nmax,dc)

           use numconstants

           implicit none

           integer :: nmax,mmax,m,n,np1,nm1,im

           real(8) :: dc(-mmax:mmax,0:nmax),cbe,sbe

           sbe=dsqrt((1.d0+cbe)*(1.d0-cbe))

           dc=0.d0

           do m=0,mmax

              dc(m,m)=(-1)**m*(0.5d0*sbe)**m*bcof(m,m)

              if(m.eq.nmax) exit

              dc(m,m+1)=fnr(m+m+1)*cbe*dc(m,m)

              do n=m+1,nmax-1

                 dc(m,n+1)=(-fnr(n-m)*fnr(n+m)*dc(m,n-1)+dble(n+n+1)*cbe*dc(m,n)) &

                           /(fnr(n+1-m)*fnr(n+1+m))

              enddo

           enddo

           do m=1,mmax

              im=(-1)**m

              do n=m,nmax

                 dc(-m,n)=im*dc(m,n)

              enddo

           enddo

           end subroutine normalizedlegendre

  !

  !  Generalized spherical functions

  !

  !  dc(m,n*(n+1)+k)=(-1)^(m + k)((n - k)!(n + k)!/(n - m)!/(n + m)!)^(1/2)

  !  ((1 + x)/2)^((m + k)/2)((1 - x)/2)^((k - m)/2)JacobiP[n - k, k - m, k + m, x]

  !

  !  for |m| <= kmax, n=0,1,...nmax, |k| <= n

  !

  !

  !  last revised: 15 January 2011

  !

           subroutine rotcoef(cbe,kmax,nmax,dc)

           use numconstants

           implicit none

           integer :: kmax,nmax,k,m,in,n,knmax,nn1,kn,im,m1

           real(8) :: cbe,sbe,dc(-kmax:kmax,0:nmax*(nmax+2)),cbe2,sbe2,dk0(-nmax-1:nmax+1),&

                      dk01(-nmax-1:nmax+1),sben,dkt,fmn,dkm0,dkm1,dkn1

           sbe=dsqrt((1.d0+cbe)*(1.d0-cbe))

           cbe2=.5d0*(1.d0+cbe)

           sbe2=.5d0*(1.d0-cbe)

           in=1

           dk0(0)=1.d0

           sben=1.d0

           dc(0,0)=1.d0

           dk01(0)=0.

           do n=1,nmax

              knmax=min(n,kmax)

              nn1=n*(n+1)

              in=-in

              sben=sben*sbe/2.d0

              dk0(n)=in*sben*bcof(n,n)

              dk0(-n)=in*dk0(n)

              dk01(n)=0.

              dk01(-n)=0.

              dc(0,nn1+n)=dk0(n)

              dc(0,nn1-n)=dk0(-n)

              do k=-n+1,n-1

                 kn=nn1+k

                 dkt=dk01(k)

                 dk01(k)=dk0(k)

                 dk0(k)=(cbe*dble(n+n-1)*dk01(k)-fnr(n-k-1)*fnr(n+k-1)*dkt)&

                       /(fnr(n+k)*fnr(n-k))

                 dc(0,kn)=dk0(k)

              enddo

              im=1

              do m=1,knmax

                 im=-im

                 fmn=1.d0/fnr(n-m+1)/fnr(n+m)

                 m1=m-1

                 dkm0=0.

                 do k=-n,n

                    kn=nn1+k

                    dkm1=dkm0

                    dkm0=dc(m1,kn)

                    if(k.eq.n) then

                       dkn1=0.

                    else

                       dkn1=dc(m1,kn+1)

                    endif

                    dc(m,kn)=(fnr(n+k)*fnr(n-k+1)*cbe2*dkm1 &

                            -fnr(n-k)*fnr(n+k+1)*sbe2*dkn1  &

                            -dble(k)*sbe*dc(m1,kn))*fmn

                    dc(-m,nn1-k)=dc(m,kn)*(-1)**(k)*im

                 enddo

              enddo

           enddo

           end subroutine rotcoef

  

           subroutine rotcoefvecarg(narg,cbe,kmax,nmax,dc)

           use numconstants

           implicit none

           integer :: kmax,nmax,k,m,in,n,knmax,nn1,kn,im,m1,narg

           real(8) :: cbe(narg),sbe(narg),dc(-kmax:kmax,0:nmax*(nmax+2),narg), &

                      cbe2(narg),sbe2(narg),dk0(-nmax-1:nmax+1,narg),&

                      dk01(-nmax-1:nmax+1,narg),sben(narg),dkt(narg), &

                      fmn,dkm0(narg),dkm1(narg),dkn1(narg)

           sbe=sqrt((1.d0+cbe)*(1.d0-cbe))

           cbe2=.5d0*(1.d0+cbe)

           sbe2=.5d0*(1.d0-cbe)

           in=1

           dk0(0,:)=1.d0

           sben=1.d0

           dc(0,0,:)=1.d0

           dk01(0,:)=0.

           do n=1,nmax

              knmax=min(n,kmax)

              nn1=n*(n+1)

              in=-in

              sben=sben*sbe/2.d0

              dk0(n,:)=in*sben(:)*bcof(n,n)

              dk0(-n,:)=in*dk0(n,:)

              dk01(n,:)=0.

              dk01(-n,:)=0.

              dc(0,nn1+n,:)=dk0(n,:)

              dc(0,nn1-n,:)=dk0(-n,:)

              do k=-n+1,n-1

                 kn=nn1+k

                 dkt(:)=dk01(k,:)

                 dk01(k,:)=dk0(k,:)

                 dk0(k,:)=(cbe(:)*dble(n+n-1)*dk01(k,:)-fnr(n-k-1)*fnr(n+k-1)*dkt(:)) &

                       /(fnr(n+k)*fnr(n-k))

                 dc(0,kn,:)=dk0(k,:)

              enddo

              im=1

              do m=1,knmax

                 im=-im

                 fmn=1.d0/fnr(n-m+1)/fnr(n+m)

                 m1=m-1

                 dkm0=0.

                 do k=-n,n

                    kn=nn1+k

                    dkm1=dkm0

                    dkm0(:)=dc(m1,kn,:)

                    if(k.eq.n) then

                       dkn1=0.

                    else

                       dkn1(:)=dc(m1,kn+1,:)

                    endif

                    dc(m,kn,:)=(fnr(n+k)*fnr(n-k+1)*cbe2(:)*dkm1(:) &

                            -fnr(n-k)*fnr(n+k+1)*sbe2(:)*dkn1(:)  &

                            -dble(k)*sbe(:)*dc(m1,kn,:))*fmn

                    dc(-m,nn1-k,:)=dc(m,kn,:)*(-1)**(k)*im

                 enddo

              enddo

           enddo

           end subroutine rotcoefvecarg

  !

  !  tau are the vector spherical harmonic functions, normalized

  !

  !

  !  last revised: 15 January 2011

  !

           subroutine taufunc(cb,nmax,tau)

           use numconstants

           implicit none

           integer :: nmax,n,m,p,nn1,mn

           real(8) :: drot(-1:1,0:nmax*(nmax+2)),tau(0:nmax+1,nmax,2),cb,fnm

           call rotcoef(cb,1,nmax,drot)

           do n=1,nmax

              nn1=n*(n+1)

              fnm=sqrt(dble(n+n+1)/2.d0)/4.d0

              do m=-n,-1

                 mn=nn1+m

                 tau(n+1,-m,1)=-fnm*(-drot(-1,mn)+drot(1,mn))

                 tau(n+1,-m,2)=-fnm*(drot(-1,mn)+drot(1,mn))

              enddo

              do m=0,n

                 mn=nn1+m

                 tau(m,n,1)=-fnm*(-drot(-1,mn)+drot(1,mn))

                 tau(m,n,2)=-fnm*(drot(-1,mn)+drot(1,mn))

              enddo

           enddo

           end subroutine taufunc

  !

  ! vector spherical harmonic function

  ! november 2011

  !

  

           subroutine pifunc(cb,ephi,nmax,ndim,pivec)

           use numconstants

           implicit none

           integer :: nmax,n,m,p,nn1,mn,ndim

           real(8) :: drot(-1:1,0:nmax*(nmax+2)),tau(2),cb,fnm

           complex(8) :: pivec(0:ndim+1,ndim,2),ephi,ephim(-nmax:nmax),cin

           call rotcoef(cb,1,nmax,drot)

           ephim(0)=1.d0

           do m=1,nmax

              ephim(m)=ephi*ephim(m-1)

              ephim(-m)=dconjg(ephim(m))

           enddo

           do n=1,nmax

              cin=(0.d0,-1.d0)**(n+1)

              nn1=n*(n+1)

              fnm=sqrt(dble(n+n+1)/2.d0)/4.d0

              do m=-n,-1

                 mn=nn1+m

                 tau(1)=-fnm*(-drot(-1,mn)+drot(1,mn))

                 tau(2)=-fnm*(drot(-1,mn)+drot(1,mn))

                 pivec(n+1,-m,1)=cin*tau(1)*ephim(m)

                 pivec(n+1,-m,2)=cin*tau(2)*ephim(m)

              enddo

              do m=0,n

                 mn=nn1+m

                 tau(1)=-fnm*(-drot(-1,mn)+drot(1,mn))

                 tau(2)=-fnm*(drot(-1,mn)+drot(1,mn))

                 pivec(m,n,1)=cin*tau(1)*ephim(m)

                 pivec(m,n,2)=cin*tau(2)*ephim(m)

              enddo

           enddo

           end subroutine pifunc

  !

  !  regular vswf expansion coefficients for a plane wave.

  !  alpha, beta: incident azimuth and polar angles.

  !

  !

  !  last revised: 15 January 2011

  !

           subroutine planewavecoef(alpha,beta,nodr,pmnp0)

           use numconstants

           implicit none

           integer :: nodr,m,n,p,k,ierr

           real(8) :: alpha,beta,cb,sb,ca,sa

           real(8), allocatable :: tau(:,:,:)

           complex(8) :: ealpha,ci,cin

           complex(8), allocatable :: ealpham(:)

           complex(8) :: pmnp0(0:nodr+1,nodr,2,2)

           data ci/(0.d0,1.d0)/

           call init(nodr)

           allocate(ealpham(-nodr:nodr))

           allocate(tau(0:nodr+1,nodr,2))

           cb=cos(beta)

           sb=sqrt((1.d0-cb)*(1.d0+cb))

           ca=cos(alpha)

           sa=sin(alpha)

           ealpha=dcmplx(ca,sa)

           call taufunc(cb,nodr,tau)

           call ephicoef(ealpha,nodr,ealpham)

           do n=1,nodr

              cin=4.d0*ci**(n+1)

              do p=1,2

                 do m=-n,-1

                    pmnp0(n+1,-m,p,1)=-cin*tau(n+1,-m,p)*ealpham(-m)

                    pmnp0(n+1,-m,p,2)=ci*cin*tau(n+1,-m,3-p)*ealpham(-m)

                 enddo

                 do m=0,n

                    pmnp0(m,n,p,1)=-cin*tau(m,n,p)*ealpham(-m)

                    pmnp0(m,n,p,2)=ci*cin*tau(m,n,3-p)*ealpham(-m)

                 enddo

              enddo

           enddo

           deallocate(ealpham,tau)

           end subroutine planewavecoef

  !

  !  regular vswf expansion coefficients for a gaussian beam, localized approximation.

  !  cbeam = 1/(k omega)

  !

  !

  !  last revised: 15 January 2011

  !

           subroutine gaussianbeamcoef(alpha,beta,cbeam,nodr,pmnp0)

           use numconstants

           implicit none

           integer :: nodr,m,n,p,k,ierr

           real(8) :: alpha,beta,cbeam,gbn

           complex(8) :: pmnp0(0:nodr+1,nodr,2,2)

           call planewavecoef(alpha,beta,nodr,pmnp0)

           do n=1,nodr

              gbn=dexp(-((dble(n)+.5d0)*cbeam)**2.)

              do p=1,2

                 do k=1,2

                    do m=-n,-1

                       pmnp0(n+1,-m,p,k)=pmnp0(n+1,-m,p,k)*gbn

                    enddo

                    do m=0,n

                       pmnp0(m,n,p,k)=pmnp0(m,n,p,k)*gbn

                    enddo

                 enddo

              enddo

           enddo

           end subroutine gaussianbeamcoef

  !

  !  plane wave expansion coefficients at sphere origins.  uses a phase shift.

  !

  !

  !  last revised: 15 January 2011

  !

           subroutine sphereplanewavecoef(nsphere,neqns,nodr,nodrmax,alpha,beta,rpos,pmnp)

           implicit none

           integer :: m,n,p,nsphere,i,l,nodr(nsphere),nblk,nboff,nodrmax,neqns,k

           real(8) :: alpha,beta,cb,sb,ca,sa,rpos(3,nsphere)

           complex(8) :: ci,phasefac, pmnp(neqns,2)

           complex(8) :: pmnp0(0:nodrmax+1,nodrmax,2,2)

           data ci/(0.d0,1.d0)/

           call planewavecoef(alpha,beta,nodrmax,pmnp0)

           cb=cos(beta)

           sb=sqrt((1.d0-cb)*(1.d0+cb))

           ca=cos(alpha)

           sa=sin(alpha)

           l=0

           do i=1,nsphere

              phasefac=cdexp(ci*((ca*rpos(1,i)+sa*rpos(2,i))*sb+rpos(3,i)*cb))

              do p=1,2

                 do n=1,nodr(i)

                    do m=0,nodr(i)+1

                       l=l+1

                       do k=1,2

                          pmnp(l,k)=phasefac*pmnp0(m,n,p,k)

                       enddo

                    enddo

                 enddo

              enddo

           enddo

           end subroutine sphereplanewavecoef

  !

  ! this computes the normalized translation coefficients for an

  ! axial translation of positive distance r.  For itype=1 or 3, the translation

  ! uses the spherical Bessel or Hankel functions as a basis function,

  ! respectively.    They are related to the coefficients appearing in

  ! M&M JOSA 96 by

  !

  ! J^{ij}_{mnp mlq} = (E_{ml}/E_{mn})^(1/2) ac(s,n,l*(l+1)+m)

  !

  ! where

  !

  !   E_{mn} = n(n+1)(n+m)!/((2n+1)(n-m)!)

  !   s=mod(p+q,2)+1 (i.e., s=1 for the A coefficient, =2 for the B

  !   coefficient)

  !

  !  The calculation procedure is based on the derivation

  !  of the addition theorem for vector harmonics, appearing in

  !  Fuller and Mackowski, proc. Light Scattering by Nonspherical

  !  Particles, NASA/GISS Sept. 1998.

  !

  !  revised: 10 october 2011: used F90 vector arithmetic and precalculation

  !           of various constants.

  !

           subroutine axialtrancoef(itype,r,ri,nmax,lmax,ac)

           use numconstants

           implicit none

           integer :: itype,nmax,lmax,n,l,m,p,w,n21,ll1,nlmin,lblk,wmin,wmax,ml

           integer :: iadd,nlmax

           integer, save :: nlmax0

           real(8) :: r

           complex(8) :: ri,ci,z,xi(0:nmax+lmax)

           complex(8) :: ac(nmax,lmax*(lmax+3)/2,2)

           data ci,nlmax0/(0.d0,1.d0),0/

           nlmax=max(nmax,lmax)

           if(nlmax.gt.nlmax0) then

              nlmax0=nlmax

              call axialtrancoefinit(nlmax)

           endif

           if(r.eq.0.d0) then

              ac=(0.d0,0.d0)

              if(itype.ne.1) return

              do m=0,min(nmax,lmax)

                 do n=max(1,m),min(nmax,lmax)

                    iadd=atcadd(m,n,lmax)

                    ac(n,iadd,l)=1.

                 enddo

              enddo

              return

           endif

           z=r*ri

           if(itype.eq.1) then

              call cricbessel(nmax+lmax,z,xi)

           else

              call crichankel(nmax+lmax,z,xi)

           endif

           xi=xi/z

           do n=1,nmax

              do l=1,lmax

                 wmin=abs(n-l)

                 wmax=n+l

                 do m=0,min(n,l)

                    iadd=atcadd(m,l,lmax)

                    ml=l*(l+1)/2+m

                    ac(n,iadd,1)=sum(vcc_const(n,ml,wmin:wmax:2)*xi(wmin:wmax:2))

                    ac(n,iadd,2)=ci*sum(vcc_const(n,ml,wmin+1:wmax-1:2)*xi(wmin+1:wmax-1:2))

                 enddo

              enddo

           enddo

           end subroutine axialtrancoef

  !

  !  axial translation coefficients calculated by the diamond recurrence formula

  !  new: 10 october 2011

  !

           subroutine axialtrancoefrecurrence(itype,r,ri,nmax,lmax,ac)

           use numconstants

           implicit none

           integer :: itype,nmax,lmax,n,l,m,p,q,w,n21,ll1,nlmin,lblk, &

                      wmin,wmax,ml,m1,np1,nm1,iaddp1,iaddm1,lm1,lp1

           integer :: iadd,nlmax,iadd0,iadd1

           integer, save :: nlmax0

           real(8) :: r,fnp1,fn,fnm1,flp1,fl,flm1

           complex(8) :: ri,ci,z,xi(0:nmax+lmax)

           complex(8) :: ac(nmax,lmax*(lmax+3)/2,2)

           data ci,nlmax0/(0.d0,1.d0),0/

           nlmax=max(nmax,lmax)

           nlmin=min(nmax,lmax)

           if(nlmax.gt.nlmax0) then

              nlmax0=nlmax

              call axialtrancoefinit(nlmax)

           endif

  

           if(r.eq.0.d0) then

              ac=(0.d0,0.d0)

              if(itype.ne.1) return

              do m=0,nlmin

                 m1=max(1,m)

                 do n=m1,nlmin

                    iadd=atcadd(m,n,lmax)

                    ac(n,iadd,l)=1.

                 enddo

              enddo

              return

           endif

           z=r*ri

           if(itype.eq.1) then

              call cricbessel(nmax+lmax,z,xi)

           else

              call crichankel(nmax+lmax,z,xi)

           endif

           xi=xi/z

  

           lm1=lmax-1

           do m=0,nlmin

              m1=max(1,abs(m))

              lp1=m1+1

              iadd0=atcadd(m,m1,lmax)

              iadd1=atcadd(m,lmax,lmax)

              iaddp1=iadd0+1

              iaddm1=iadd1-1

  

              iadd=iadd0-1

              n=m1

              do l=m1,lmax

                 wmin=abs(n-l)

                 wmax=n+l

                 iadd=iadd+1

                 ml=l*(l+1)/2+m

                 ac(n,iadd,1)=sum(vcc_const(n,ml,wmin:wmax:2)*xi(wmin:wmax:2))

                 ac(n,iadd,2)=ci*sum(vcc_const(n,ml,wmin+1:wmax-1:2)*xi(wmin+1:wmax-1:2))

              enddo

              l=lmax

              iadd=iadd1

              ml=l*(l+1)/2+m

              do n=m1+1,nmax

                 wmin=abs(n-l)

                 wmax=n+l

                 ac(n,iadd,1)=sum(vcc_const(n,ml,wmin:wmax:2)*xi(wmin:wmax:2))

                 ac(n,iadd,2)=ci*sum(vcc_const(n,ml,wmin+1:wmax-1:2)*xi(wmin+1:wmax-1:2))

              enddo

              if(m1.eq.nlmin) cycle

  

              do n=m1,nmax-1

                 np1=n+1

                 nm1=n-1

                 do p=1,2

                    q=3-p

                    ac(np1,iadd0:iaddm1,p)= &

                      - ac(n,iaddp1:iadd1,p)*fnp1_const(m,m1:lm1) &

                      + (fn_const(m,m1:lm1)-fn_const(m,n))*ci*ac(n,iadd0:iaddm1,q)

                    ac(np1,iaddp1:iaddm1,p)=ac(np1,iaddp1:iaddm1,p) &

                      + ac(n,iadd0:iadd1-2,p)*fnm1_const(m,lp1:lm1)

                    if(n.gt.m1) then

                       ac(np1,iadd0:iaddm1,p)=ac(np1,iadd0:iaddm1,p) &

                         + ac(nm1,iadd0:iaddm1,p)*fnm1_const(m,n)

                    endif

                    ac(np1,iadd0:iaddm1,p)=ac(np1,iadd0:iaddm1,p)/fnp1_const(m,n)

                 enddo

              enddo

           enddo

           end subroutine axialtrancoefrecurrence

  !

  !  constants for translation coefficient calculation

  !

           subroutine axialtrancoefinit(nmax)

           use numconstants

           implicit none

           integer :: nmax,m,n,l,w,n21,ml,ll1,wmin,wmax,nlmin,lp1,lm1

           real(8) :: c1,c2,vc1(0:2*nmax),vc2(0:2*nmax),alnw

           complex(8) :: ci,inlw

           data ci/(0.d0,1.d0)/

           if(allocated(vcc_const)) deallocate(vcc_const,fnm1_const,fn_const,fnp1_const)

           allocate(vcc_const(nmax,nmax*(nmax+1)/2+nmax,0:2*nmax),fnm1_const(0:nmax,nmax), &

                    fn_const(0:nmax,nmax),fnp1_const(0:nmax,nmax))

           do n=1,nmax

              n21=n+n+1

              do l=1,nmax

                 c1=fnr(n21)*fnr(l+l+1)

                 ll1=l*(l+1)/2

                 call vcfunc(-1,n,1,l,vc2)

                 wmin=abs(n-l)

                 wmax=n+l

                 nlmin=min(l,n)

                 do m=0,nlmin

                    ml=ll1+m

                    c2=-c1*(-1)**m

                    call vcfunc(-m,n,m,l,vc1)

                    do w=wmin,wmax

                       inlw=ci**(n-l+w)

                       vcc_const(n,ml,w)=c2*vc1(w)*vc2(w)*(dble(inlw)+dimag(inlw))

                    enddo

                 enddo

              enddo

           enddo

           fnm1_const=0.

           fn_const=0.

           fnp1_const=0.

           do m=0,nmax

              do l=max(1,m),nmax

                 lp1=l+1

                 lm1=l-1

                 fnm1_const(m,l)=fnr(lm1)*fnr(lp1)*fnr(l-m)*fnr(l+m)/fnr(lm1+l)/fnr(l+lp1)/dble(l)

                 fn_const(m,l)=dble(m)/dble(l)/dble(lp1)

                 fnp1_const(m,l)=fnr(l)*fnr(l+2)*fnr(lp1-m)*fnr(lp1+m)/fnr(l+lp1)/fnr(l+l+3)/dble(lp1)

              enddo

           enddo

           end subroutine axialtrancoefinit

  !

  !  test to determine convergence of regular vswf addition theorem for max. order lmax

  !  and translation distance r w/ refractive index ri.

  !

  !

  !  last revised: 15 January 2011

  !

           subroutine tranordertest(r,ri,lmax,eps,nmax)

           use numconstants

           implicit none

           integer :: itype,nmax,lmax,n,l,m,p,w,n21,ll1,nlmin,lblk,wmin,wmax

           integer, parameter :: nlim=200

           integer :: iadd

           real(8) :: r,alnw,sum,eps

           real(8) :: vc1(0:nlim+lmax)

           complex(8) :: ri,ci,z,a,b,c

           complex(8) :: xi(0:nlim+lmax)

           data ci/(0.d0,1.d0)/

           if(r.eq.0.d0) then

              nmax=lmax

              return

           endif

           z=r*ri

           sum=0.d0

           do n=1,nlim

              call init(n+lmax)

              call cricbessel(n+lmax,z,xi)

              do l=0,n+lmax

                 xi(l)=xi(l)/z*ci**l

              enddo

              n21=n+n+1

              l=lmax

              c=fnr(n21)*fnr(l+l+1)*ci**(n-l)

              call vcfunc(-1,n,1,l,vc1)

              wmin=abs(n-l)

              wmax=n+l

              m=1

              a=0.

              b=0.

              do w=wmin,wmax

                 alnw=vc1(w)*vc1(w)

                 if(mod(n+l+w,2).eq.0) then

                    a=a+alnw*xi(w)

                 else

                    b=b+alnw*xi(w)

                 endif

              enddo

              a=c*a

              b=c*b

              sum=sum+a*conjg(a)+b*conjg(b)

              if(abs(1.d0-sum).lt.eps) exit

           enddo

           nmax=min(n,nlim)

           nmax=max(nmax,lmax)

           end subroutine tranordertest

  !

  !  address for axial translation coefficient

  !

  !

  !  last revised: 15 January 2011

  !

           integer function atcadd(m,n,ntot)

           implicit none

           integer :: m,n,ntot

           atcadd=n-ntot+(max(1,m)*(1+2*ntot-max(1,m)))/2+ntot*min(1,m)

           end function atcadd

  !

  !   gentrancoef: calculates the vwh translation coefficients for

  !   a general translation from one origin to another

  !

  !   input: itype: integer, =1, regular, =3, outgoing type harmonics

  !          xptran: real, dim 3 vector: x,y,z components of translation, in units

  !                   of 1/k

  !          ri: complex, refractive index of medium

  !          nrow0,nrow1,ncol0,ncol1: integer, starting and stopping row and column order

  !          iaddrow0,iaddcol0: address offset for row and column order (see below)

  !   output: ac(p,mn,kl): complex translation matrix.  calculated for mode p=1,2 (A or B type),

  !           order n=nrow0,nrow1, degree m=-n,n

  !           order l=ncol0,ncol1, degree k=-n,n

  !           address is given by

  !           mn=m+n*(n+1)-(nrow0-1)*(nrow0+1)+iaddrow0

  !           kl=k+l*(l+1)-(ncol0-1)*(ncol0+1)+iaddcol0

  !           that is, if iaddrow0=0 the address is mn=1 for n=nrow0 and m=-n.

  !

  !

  !  last revised: 15 January 2011

  !

           subroutine gentrancoef(itype,xptran,ri,nrow0,nrow1,ncol0,ncol1, &

                                 iaddrow0,iaddcol0,ac)

           use numconstants

           implicit none

           integer :: itype,nrow0,nrow1,ncol0,ncol1,iaddrow0,iaddcol0,kmax

           integer :: ntot,nblkr0,nblkr1,nblkc0,nblkc1

           integer :: v,vw,w,wmax,wmin,n,l,m,k,p,nn1,ll1,mn,kl,m1m

           real(8) :: vc1(0:nrow1+ncol1),vc2(0:nrow1+ncol1),&

                      xptran(3),r,ct,ct0

           real(8) :: drot(0:0,0:(nrow1+ncol1)*(nrow1+ncol1+2))

           complex(8) :: ri,ci,ephi,ac(2,nrow1*(nrow1+2)-(nrow0-1)*(nrow0+1)-iaddrow0,&

                         ncol1*(ncol1+2)-(ncol0-1)*(ncol0+1)-iaddcol0),&

                         z,c,a,b

           complex(8) :: ephim(-(nrow1+ncol1):nrow1+ncol1),jnc(0:nrow1+ncol1)

           data ci/(0.d0,1.d0)/

           call cartosphere(xptran,r,ct,ephi)

           ntot=nrow1+ncol1

           nblkr0=(nrow0-1)*(nrow0+1)

           nblkr1=nrow1*(nrow1+2)

           nblkc0=(ncol0-1)*(ncol0+1)

           nblkc1=ncol1*(ncol1+2)

           if(r.eq.0.d0) then

              do n=nblkr0+1,nblkr1

                 mn=n-nblkr0+iaddrow0

                 do l=nblkc0+1,nblkc1

                    kl=l-nblkc0+iaddcol0

                    do p=1,2

                       ac(p,mn,kl)=0.d0

                    enddo

                 enddo

                 if(n.gt.nblkc0.and.n.le.nblkc1.and.itype.eq.1) then

                    ac(1,mn,n-nblkc0+iaddcol0)=1.d0

                 endif

              enddo

              return

           endif

           kmax=0

           ct0=ct

           call rotcoef(ct0,kmax,ntot,drot)

           call ephicoef(ephi,ntot,ephim)

           z=ri*r

           if(itype.eq.1) then

              call cricbessel(ntot,z,jnc)

           else

              call crichankel(ntot,z,jnc)

           endif

           do n=0,ntot

              c=ci**n

              jnc(n)=c*jnc(n)/z

           enddo

           do l=ncol0,ncol1

              ll1=l*(l+1)

              do n=nrow0,nrow1

                 nn1=n*(n+1)

                 wmax=n+l

                 call vcfunc(-1,n,1,l,vc2)

                 c=-ci**(n-l)*fnr(n+n+1)*fnr(l+l+1)

                 do k=-l,l

                    kl=ll1+k-nblkc0+iaddcol0

                    do m=-n,n

                       m1m=(-1)**m

                       mn=nn1+m-nblkr0+iaddrow0

                       v=k-m

                       call vcfunc(-m,n,k,l,vc1)

                       a=0.

                       b=0.

                       wmin=max(abs(v),abs(n-l))

                       do w=wmax,wmin,-1

                          vw=w*(w+1)+v

                          if(mod(wmax-w,2).eq.0) then

                             a=a+vc1(w)*vc2(w)*jnc(w)*drot(0,vw)

                          else

                             b=b+vc1(w)*vc2(w)*jnc(w)*drot(0,vw)

                          endif

                       enddo

                       ac(1,mn,kl)=a*c*m1m*ephim(v)

                       ac(2,mn,kl)=b*c*m1m*ephim(v)

                    enddo

                 enddo

              enddo

           enddo

           return

           end subroutine gentrancoef

  !

  ! cartosphere takes the cartesian point (x,y,z) = xp(1), xp(2), xp(3)

  ! and converts to polar form: r: radius, ct: cos(theta), ep = exp(i phi)

  !

  !

  !  last revised: 15 January 2011

  !

           subroutine cartosphere(xp,r,ct,ep)

           implicit none

           real(8) :: xp(3),r,ct

           complex(8) :: ep

           r=xp(1)*xp(1)+xp(2)*xp(2)+xp(3)*xp(3)

           if(r.eq.0.d0) then

              ct=1.d0

              ep=(1.d0,0.d0)

              return

           endif

           r=sqrt(r)

           ct=xp(3)/r

           if(xp(1).eq.0.d0.and.xp(2).eq.0.d0) then

              ep=(1.d0,0.d0)

           else

              ep=dcmplx(xp(1),xp(2))/sqrt(xp(1)*xp(1)+xp(2)*xp(2))

           endif

           return

           end subroutine cartosphere

  !

  ! ephicoef returns the complex array epm(m) = exp(i m phi) for

  ! m=-nodr,nodr.   ep =exp(i phi), and epm is dimensioned epm(-nd:nd)

  !

  !

  !  last revised: 15 January 2011

  !

           subroutine ephicoef(ep,nodr,epm)

           implicit none

           integer :: nodr,m

           complex(8) :: ep,epm(-nodr:nodr)

           epm(0)=(1.d0,0.d0)

           do m=1,nodr

              epm(m)=ep*epm(m-1)

              epm(-m)=dconjg(epm(m))

           enddo

           return

           end subroutine ephicoef

  !

  !  test to determine max order of vswf expansion of a plane wave at distance r

  !

  !

  !  last revised: 15 January 2011

  !

           subroutine planewavetruncationorder(r,eps,nodr)

           implicit none

           integer :: nodr,n1,n

           real(8) :: r,eps,err

           real(8), allocatable :: jn(:)

           complex(8) :: sum, ci,eir

           data ci/(0.d0,1.d0)/

           n1=max(10,int(3.*r+1))

           allocate(jn(0:n1))

           call ricbessel(n1,r,-1.d0,n1,jn)

           jn(0:n1)=jn(0:n1)/r

           eir=cdexp(-ci*r)

           sum=jn(0)*eir

           do n=1,n1

              sum=sum+ci**n*dble(n+n+1)*jn(n)*eir

              err=cdabs(1.d0-sum)

              if(err.lt.eps) then

                 nodr=n

                 deallocate(jn)

                 return

              endif

           enddo

           nodr=n1

           deallocate(jn)

           end subroutine planewavetruncationorder

  !

  !  calculates the cartesian components of the vswf at position rpos, in ref. index ri.

  !

  !

  !  original: 15 January 2011

  !  revised: 23 February 2011: multiplied by root 2

  !

           subroutine vwhcalc(rpos,ri,nodr,itype,vwh)

           use numconstants

           implicit none

           integer :: nodr,itype,m,n,p,nodrp1,nodrm1,nn1,mn,np1,nm1,mp1,mm1,ndim, &

                      nblkp

           integer, save :: nodrmax

           real(8) ::  rpos(3),r,ct,fnorm1,fnorm2

           real(8) pmn(0:0,0:(nodr+1)*(nodr+3))

           complex(8) :: ci,vwh(3,2,1:*),ri,ephi,a,b,a1,b1,z1,a2,b2,z2

           complex(8)  :: a1vec(-nodr:nodr), &

                         b1vec(-nodr:nodr),z1vec(-nodr:nodr),a2vec(-nodr:nodr), &

                         b2vec(-nodr:nodr),z2vec(-nodr:nodr)

           complex(8) :: umn(-nodr-2:nodr+2,0:nodr+1), hn(0:nodr+1), ephim(-nodr-1:nodr+1)

           data ci,nodrmax/(0.d0,1.d0),0/

           if(nodr.gt.nodrmax) then

              nodrmax=nodr

              call init(nodr+2)

           endif

           call cartosphere(rpos,r,ct,ephi)

           if(r.le.1.d-4) then

              vwh(:,:,1:nodr*(nodr+1))=(0.d0,0.d0)

              if(itype.eq.3) return

              vwh(1,1,1)=.5d0*fnr(2)/fnr(3)

              vwh(2,1,1)=-.5d0*ci*fnr(2)/fnr(3)

              vwh(3,1,2)=1.d0*fnr(2)/fnr(6)

              vwh(1,1,3)=-.5d0*fnr(2)/fnr(3)

              vwh(2,1,3)=-.5d0*ci*fnr(2)/fnr(3)

              return

           endif

           nodrp1=nodr+1

           nodrm1=nodr-1

           a=ri*r

           if(itype.eq.1) then

              call cricbessel(nodrp1,a,hn)

           else

              call crichankel(nodrp1,a,hn)

           endif

           hn(0:nodrp1)=hn(0:nodrp1)/a

           call rotcoef(ct,0,nodrp1,pmn)

           call ephicoef(ephi,nodrp1,ephim)

           umn=0.d0

           umn(0,0)=hn(0)*fnr(2)

           do n=1,nodrp1

              nn1=n*(n+1)

              umn(-n:n,n)=fnr(2)*pmn(0,nn1-n:nn1+n)*ephim(-n:n)*hn(n)

              umn(-n-1,n)=0.d0

              umn(n+1,n)=0.d0

           enddo

           do n=1,nodr

              nn1=n*(n+1)

              np1=n+1

              nm1=n-1

              a1vec(-n:n)=vwh_coef(-n:n,n,1,1)*umn(-nm1:np1,np1) &

                 +vwh_coef(-n:n,n,1,-1)*umn(-nm1:np1,nm1)

              b1vec(-n:n)=vwh_coef(-n:n,n,-1,1)*umn(-np1:nm1,np1) &

                 +vwh_coef(-n:n,n,-1,-1)*umn(-np1:nm1,nm1)

              z1vec(-n:n)=vwh_coef(-n:n,n,0,1)*umn(-n:n,np1) &

                 +vwh_coef(-n:n,n,0,-1)*umn(-n:n,nm1)

              a2vec(-n:n)=vwh_coef(-n:n,n,1,0)*umn(-nm1:np1,n)

              b2vec(-n:n)=vwh_coef(-n:n,n,-1,0)*umn(-np1:nm1,n)

              z2vec(-n:n)=vwh_coef(-n:n,n,0,0)*umn(-n:n,n)

              vwh(1,1,nn1-n:nn1+n)=-0.5d0*(a1vec(-n:n)+b1vec(-n:n))

              vwh(2,1,nn1-n:nn1+n)=-0.5d0*ci*(-a1vec(-n:n)+b1vec(-n:n))

              vwh(3,1,nn1-n:nn1+n)=-z1vec(-n:n)

              vwh(1,2,nn1-n:nn1+n)=-0.5d0*ci*(a2vec(-n:n)+b2vec(-n:n))

              vwh(2,2,nn1-n:nn1+n)=-0.5d0*(a2vec(-n:n)-b2vec(-n:n))

              vwh(3,2,nn1-n:nn1+n)=-ci*z2vec(-n:n)

           enddo

           end subroutine vwhcalc

  !

  !  svwf calculation for an axial translation

  !

  !

  !  original: 15 January 2011

  !  revised: 23 February 2011: multiplied by root 2

  !

           subroutine vwhaxialcalc(rpos,ri,nodr,itype,vwh)

           use numconstants

           implicit none

           integer :: nodr,itype,m,n,p,nodrp1,nodrm1,nn1,mn,np1,nm1,mp1,mm1,ndim, &

                      nblkp

           integer, save :: nodrmax

           real(8) ::  rpos(3),r,ct

           real(8) pmn(-2:2,0:nodr+1)

           complex(8) :: ci,vwh(3,2,2,1:nodr),ri,ephi,a,b,a1,b1,z1,a2,b2,z2

           complex(8) :: umn(-2:2,0:nodr+1), hn(0:nodr+1), ephim(-2:2)

           data ci,nodrmax/(0.d0,1.d0),0/

           if(nodr.gt.nodrmax) then

              nodrmax=nodr

              call init(nodr+2)

           endif

           call cartosphere(rpos,r,ct,ephi)

           if(r.le.1.d-4) then

              vwh(:,:,:,1:nodr)=(0.d0,0.d0)

              if(itype.eq.3) return

              vwh(1,1,1,1)=.5d0*fnr(2)/fnr(3)

              vwh(2,1,1,1)=-.5d0*ci*fnr(2)/fnr(3)

              vwh(1,1,2,1)=-.5d0*fnr(2)/fnr(3)

              vwh(2,1,2,1)=-.5d0*ci*fnr(2)/fnr(3)

              return

           endif

           nodrp1=nodr+1

           nodrm1=nodr-1

           a=ri*r

           if(itype.eq.1) then

              call cricbessel(nodrp1,a,hn)

           else

              call crichankel(nodrp1,a,hn)

           endif

           hn(0:nodrp1)=hn(0:nodrp1)/a

           call normalizedlegendre(ct,2,nodrp1,pmn)

           call ephicoef(ephi,2,ephim)

           umn(-2:2,0:nodrp1)=0.d0

           umn(0,0)=hn(0)*fnr(2)

           do n=1,nodrp1

              p=min(n,2)

              do m=-p,p

                 umn(m,n)=fnr(2)*pmn(m,n)*ephim(m)*hn(n)

              enddo

           enddo

           vwh(:,:,:,1:nodr)=0.d0

           do n=1,nodr

              np1=n+1

              nm1=n-1

              m=-1

              mp1=m+1

              mm1=m-1

              a1=vwh_coef(m,n,1,1)*umn(mp1,np1) &

                 +vwh_coef(m,n,1,-1)*umn(mp1,nm1)

              b1=vwh_coef(m,n,-1,1)*umn(mm1,np1) &

                 +vwh_coef(m,n,-1,-1)*umn(mm1,nm1)

              z1=vwh_coef(m,n,0,1)*umn(m,np1) &

                 +vwh_coef(m,n,0,-1)*umn(m,nm1)

              a2=vwh_coef(m,n,1,0)*umn(mp1,n)

              b2=vwh_coef(m,n,-1,0)*umn(mm1,n)

              z2=vwh_coef(m,n,0,0)*umn(m,n)

              vwh(1,1,1,n)=-0.5d0*(a1+b1)

              vwh(2,1,1,n)=-0.5d0*ci*(-a1+b1)

              vwh(3,1,1,n)=-z1

              vwh(1,2,1,n)=-0.5d0*ci*(a2+b2)

              vwh(2,2,1,n)=-0.5d0*(a2-b2)

              vwh(3,2,1,n)=-ci*z2

              m=1

              mp1=m+1

              mm1=m-1

              a1=vwh_coef(m,n,1,1)*umn(mp1,np1) &

                 +vwh_coef(m,n,1,-1)*umn(mp1,nm1)

              b1=vwh_coef(m,n,-1,1)*umn(mm1,np1) &

                 +vwh_coef(m,n,-1,-1)*umn(mm1,nm1)

              z1=vwh_coef(m,n,0,1)*umn(m,np1) &

                 +vwh_coef(m,n,0,-1)*umn(m,nm1)

              a2=vwh_coef(m,n,1,0)*umn(mp1,n)

              b2=vwh_coef(m,n,-1,0)*umn(mm1,n)

              z2=vwh_coef(m,n,0,0)*umn(m,n)

              vwh(1,1,2,n)=-0.5d0*(a1+b1)

              vwh(2,1,2,n)=-0.5d0*ci*(-a1+b1)

              vwh(3,1,2,n)=-z1

              vwh(1,2,2,n)=-0.5d0*ci*(a2+b2)

              vwh(2,2,2,n)=-0.5d0*(a2-b2)

              vwh(3,2,2,n)=-ci*z2

           enddo

           return

           end subroutine vwhaxialcalc

  

        end module specialfuncs

  !

  !  module mpidata

  !

  !

  !  last revised: 15 January 2011

  !

        module mpidata

        implicit none

        integer :: group_comm,root_group_comm,base_rank,group_rank,root_group_rank, &

                   base_group,number_groups,proc_per_group,number_proc

        integer, allocatable :: mpi_sphere_index(:), mpi_sphere_number(:)

  

        contains

  !

  ! allocates the processors into groups

  !

  !  last revised: 15 January 2011: original

  !  20 April 2011: fixedorran=0 now looks for 2 groups.

  !  10 october 2011: option for not storing matrices.  If fixorran=0, 2 groups, else

  !     nproc groups

  !  november 2011: near and far field translation differentiation

  !

           subroutine mpisetup(nsphere,nodr,rpos,fixorran,maxmbperproc,istore, &

                      nfdistance,fftranpresent,iunit)

           use mpidefs

           use intrinsics

           use specialfuncs

           implicit none

           integer :: nsphere,numprocs,ierr,i,iunit,nodr(nsphere),fixorran, &

                      nodrmax,nodrmin,temp_comm,newgroup,j,rank,maxmbperproc, &

                      istore,nfspheres,fftranpresent,ffspheres

           integer, allocatable :: grouplist1(:),grouplist2(:)

           real(8) :: memrow(nsphere),memtot,maxmemproc,memperproc

           real(8) :: fp,sum,rpos(3,*),nfdistance,rij(3),r,avenfspheres,rmax, &

                      nfdistancei,aveffspheres

           maxmemproc=maxmbperproc*1.d6

           call ms_mpi(mpi_command='size',mpi_size=numprocs)

           call ms_mpi(mpi_command='rank',mpi_rank=rank)

           call ms_mpi(mpi_command='group',mpi_group=base_group)

           base_rank=rank

           number_proc=numprocs

           memrow=0.d0

           memtot=0.d0

  !

  !  compute the memory storage requirements

  !

           avenfspheres=0.

           aveffspheres=0.

           rmax=0.

           if(1.eq.1) then

              do i=1,nsphere

                 nfspheres=0

                 do j=1,nsphere

                    rij(:)=rpos(:,i)-rpos(:,j)

                    if(j.ne.i) then

                       if(nfdistance.lt.0.) then

                          nfdistancei=(.5*dble(nodr(i)+nodr(j)))**2.

                       else

                          nfdistancei=nfdistance

                       endif

                       r=sqrt(dot_product(rij,rij))

                       rmax=max(rmax,r)

                       if(r.le.nfdistancei) then

                          nfspheres=nfspheres+1

                          nodrmax=max(nodr(j),nodr(i))

                          nodrmin=min(nodr(j),nodr(i))

                          memrow(i)=memrow(i)+(2*nodrmin+1)*(1+nodrmax*(nodrmax+2))*8.d0

                          memrow(i)=memrow(i)+nodr(i)*nodr(j)*(nodr(j)+3)*16.d0

                          memrow(i)=memrow(i)+(2*nodrmax+1)*16.d0

                       endif

                       if(r.gt.nfdistancei.and.istore.eq.2) then

                          memrow(i)=memrow(i)+2*nodrmax*(nodrmax+2)*16.d0

                       endif

                    endif

                 enddo

                 ffspheres=nsphere-1-nfspheres

                 avenfspheres=avenfspheres+nfspheres

                 aveffspheres=aveffspheres+ffspheres

                 memtot=memtot+memrow(i)

              enddo

              if(aveffspheres.eq.0) then

                 fftranpresent=0

              else

                 fftranpresent=1

              endif

              proc_per_group=ceiling(memtot/maxmemproc)

              proc_per_group=min(proc_per_group,numprocs)

              proc_per_group=max(proc_per_group,1)

              do

                 if(mod(numprocs,proc_per_group).eq.0) exit

                 if(proc_per_group.eq.numprocs) exit

                 proc_per_group=proc_per_group+1

              enddo

           endif

           avenfspheres=avenfspheres/dble(nsphere)

           if(rank.eq.0) then

              write(iunit,'('' average near field translations per sphere:'', f10.1)') avenfspheres

              call flush(iunit)

           endif

  !

  ! no-store option

  !

           if(istore.eq.0) then

              if(fixorran.eq.0) then

                 proc_per_group=max(floor(dble(numprocs)/2.),1)

              else

                 proc_per_group=1

              endif

              memrow=1.d0

              memtot=dble(nsphere)

           else

  !

  ! only one or two groups for fixed orientation

  !

              if(fixorran.eq.0) proc_per_group=max(floor(dble(numprocs)/2.),proc_per_group)

           endif

           number_groups=numprocs/proc_per_group

           if(allocated(mpi_sphere_index)) deallocate(mpi_sphere_index)

           if(allocated(mpi_sphere_number)) deallocate(mpi_sphere_number)

           allocate(mpi_sphere_index(0:proc_per_group-1),mpi_sphere_number(0:proc_per_group-1), &

                    grouplist1(proc_per_group),grouplist2(number_groups))

           memperproc=memtot/dble(proc_per_group)

  !

  !  associate the spheres with the processors in a group

  !

           mpi_sphere_index(0)=0

           do j=1,proc_per_group-1

              memtot=0.d0

              do i=1,nsphere

                 memtot=memtot+memrow(i)

                 if(memtot.gt.dble(j)*memperproc) then

                    mpi_sphere_index(j)=i-1

                    exit

                 endif

              enddo

           enddo

           do i=0,proc_per_group-2

              mpi_sphere_number(i)=mpi_sphere_index(i+1)-mpi_sphere_index(i)

           enddo

           mpi_sphere_number(proc_per_group-1)=nsphere-mpi_sphere_index(proc_per_group-1)

  !

  !  assign the sphere-based groups

  !

           do i=0,number_groups-1

              do j=0,proc_per_group-1

                 grouplist1(j+1)=i*proc_per_group+j

              enddo

              call ms_mpi(mpi_command='incl',mpi_group=base_group,mpi_size=proc_per_group, &

                          mpi_new_group_list=grouplist1,mpi_new_group=newgroup)

              call ms_mpi(mpi_command='create',mpi_group=newgroup,mpi_new_comm=temp_comm)

              if(rank.ge.grouplist1(1).and.rank.le.grouplist1(proc_per_group)) then

                 group_comm=temp_comm

              endif

              grouplist2(i+1)=i*proc_per_group

           enddo

  !

  !  make a group associated with the rank 0 members of the sphere groups

  !

           call ms_mpi(mpi_command='incl',mpi_group=base_group,mpi_size=number_groups, &

                       mpi_new_group_list=grouplist2,mpi_new_group=newgroup)

           call ms_mpi(mpi_command='create',mpi_group=newgroup,mpi_new_comm=temp_comm)

           group_rank=mod(rank,proc_per_group)

           root_group_rank=floor(dble(rank)/dble(proc_per_group))

           if(group_rank.eq.0) root_group_comm=temp_comm

           if(rank.eq.0) then

              if(istore.ge.1) then

                 write(iunit,'('' number of processors, number groups, mb mem/processor:'',2i5,f9.3)') &

                   numprocs,number_groups,memperproc*1.d-6

                 if(memperproc.gt.maxmemproc) then

                    write(iunit,'('' warning: set maximum memory/processor is exceeded!!'')')

                 endif

              else

                 write(iunit,'('' number of processors, number groups:'',2i5)') &

                   numprocs,number_groups

              endif

              call flush(iunit)

           endif

           deallocate(grouplist1,grouplist2)

           end subroutine mpisetup

  

        end module mpidata

  

  !

  ! module spheredata: used to 1) input sphere data, 2) dimension sphere data

  ! arrays, and 3) provide common access to the data in other subroutines.

  !

  !

  !  last revised: 15 January 2011

  !

  !  30 March 2011: added optical activity

  !

        module spheredata

        use specialfuncs

        use mpidata

        implicit none

        integer, private :: numberspheres,numberiterations,fixedorrandom,numbertheta, &

                 calcnf,nfplane,calctmatrix,runprintunit,calcamn,maxmemperproc, &

                 trackiterations,nfoutdata,normalizesm,storetranmat,niterstep, &

                 fftranpresent

        real(8), private :: lengthscalefactor,realriscalefactor,imriscalefactor,epsmie, &

                   epstran,epssoln,phideg,thetamindeg,thetamaxdeg,alphadeg, &

                   betadeg,epstcon,nfplanepos,nfplanevert(2,2),deltax,gammadeg,epspw, &

                   cgaussbeam,gaussbeamfocus(3),realchiralfactor,imchiralfactor,nfdistance

        character(30), private :: positionfile,outputfile,nfoutputfile,tmatrixfile,printfile, &

                                  amnfile

        real(8), private :: xspmax,xvsp

        real(8), private, allocatable :: rpos(:,:),xsp(:)

        complex(8), private, allocatable :: ri(:,:)

        data numberiterations,fixedorrandom,numbertheta/2000,0,181/

        data calcamn,trackiterations,niterstep/1,1,20/

        data lengthscalefactor,realriscalefactor,imriscalefactor,epsmie, &

                   epstran,epssoln,phideg,thetamindeg,thetamaxdeg,alphadeg, &

                   betadeg,epstcon/1.d0,1.d0,1.d0,1.d-4,1.d-6,1.d-10,0.d0,0.d0, &

                   180.d0,0.d0,0.d0,1.d-6/

        data realchiralfactor,imchiralfactor/0.d0,0.d0/

        data normalizesm,storetranmat,nfdistance/0,1,-1.d0/

        data maxmemperproc/1500/

        data cgaussbeam/0.d0/

        data gaussbeamfocus/0.d0,0.d0,0.d0/

        data calcnf,calctmatrix,nfoutdata/0,1,1/

        data runprintunit/6/

        data positionfile,outputfile,tmatrixfile,printfile/'at_bottom','test.dat','tmatrix-temp.dat',' '/

        data nfoutputfile/'nf-temp.dat'/

        data amnfile/'amn-temp.dat'/

  

        contains

  !

  !  Find the number of data points in input unit iunit, and reposition the unit to the

  !  point after record containing parmid

  !

  !

  !  last revised: 15 January 2011

  !

           subroutine numberinrecord(iunit,parmid,numrec)

           implicit none

           integer :: numrec,iunit

           character*1 :: a

           character*35 :: parmid

           character*10 :: rec

           numrec=0

           do

              read(iunit,"(a)",advance="no",err=100,eor=100) a

              if(a.ne.' '.and.a.ne.',') then

  !

  ! start of a number

  !

                 numrec=numrec+1

  !

  ! look for the delimeter

  !

                 do

                    read(iunit,"(a)",advance="no",err=100,eor=100) a

                    if(a.eq.' '.or.a.eq.',') exit

                 enddo

              endif

           enddo

  100      if(parmid.eq.'rewind') then

              rewind(iunit)

           else

              backspace(iunit)

              backspace(iunit)

              backspace(iunit)

              do

                 read(iunit,'(a10)') rec

                 if(rec.eq.parmid(1:10)) exit

              enddo

           endif

           end subroutine numberinrecord

  !

  !  inputdata:  reads parameters from inputfile

  !              reads sphere data from position file

  !

  !

  !  original: 15 January 2011

  !  revised: 21 February 2011: fix output file initialization.

  !  30 March 2011: added optical activity

  !

  !

           subroutine inputdata(inputfile,printdata)

           integer :: imax,i,j,ierr,iunit,numrec,nsphere,printdata

           real(8) :: rmax,rtoi,rposmean(3),rireal,riimag,dtemp,betareal,betaimag, &

                      rij,xij(3),rijmax

           real(8), allocatable :: sdat(:)

           complex(8) :: ribulk,beta

           character*35 :: parmid

           character*30 :: inputfile

  !

  !  cycle through parameter input operations

  !

           open(1,file=inputfile)

           do

              read(1,'(a)',end=10) parmid

              parmid=parmid(:index(parmid,' '))

              if(parmid.eq.'number_spheres') then

                 read(1,*) numberspheres

                 cycle

              endif

              if(parmid.eq.'sphere_position_file') then

                 read(1,'(a)') positionfile

                 positionfile=positionfile(:index(positionfile,' '))

                 cycle

              endif

              if(parmid.eq.'output_file') then

                 read(1,'(a)') outputfile

                 outputfile=outputfile(:index(outputfile,' '))

                 cycle

              endif

              if(parmid.eq.'run_print_file') then

                 read(1,'(a)') printfile

                 printfile=printfile(:index(printfile,' '))

                 if(printdata.eq.1) then

                    if((printfile.eq.' '.or.printfile.eq.'console')) then

                       printfile=' '

                       runprintunit=6

                    else

                       runprintunit=4

                       open(runprintunit,file=printfile)

                    endif

                 else

                    runprintunit=6

                 endif

                 cycle

              endif

              if(parmid.eq.'length_scale_factor') then

                 read(1,*) lengthscalefactor

                 cycle

              endif

              if(parmid.eq.'real_ref_index_scale_factor') then

                 read(1,*) realriscalefactor

                 cycle

              endif

              if(parmid.eq.'imag_ref_index_scale_factor') then

                 read(1,*) imriscalefactor

                 cycle

              endif

              if(parmid.eq.'real_chiral_factor') then

                 read(1,*) realchiralfactor

                 cycle

              endif

              if(parmid.eq.'imag_chiral_factor') then

                 read(1,*) imchiralfactor

                 cycle

              endif

              if(parmid.eq.'mie_epsilon') then

                 read(1,*) epsmie

                 cycle

              endif

              if(parmid.eq.'translation_epsilon') then

                 read(1,*) epstran

                 cycle

              endif

              if(parmid.eq.'solution_epsilon') then

                 read(1,*) epssoln

                 cycle

              endif

              if(parmid.eq.'max_number_iterations') then

                 read(1,*) numberiterations

                 cycle

              endif

              if(parmid.eq.'max_memory_per_processor') then

                 read(1,*) maxmemperproc

                 cycle

              endif

              if(parmid.eq.'store_translation_matrix') then

                 read(1,*) storetranmat

                 cycle

              endif

              if(parmid.eq.'near_field_distance') then

                 read(1,*) nfdistance

                 cycle

              endif

              if(parmid.eq.'iterations_per_correction') then

                 read(1,*) niterstep

                 cycle

              endif

              if(parmid.eq.'fixed_or_random_orientation') then

                 read(1,*) fixedorrandom

                 cycle

              endif

              if(parmid.eq.'scattering_plane_angle_deg') then

                 read(1,*) phideg

                 cycle

              endif

              if(parmid.eq.'min_scattering_angle_deg') then

                 read(1,*) thetamindeg

                 cycle

              endif

              if(parmid.eq.'max_scattering_angle_deg') then

                 read(1,*) thetamaxdeg

                 cycle

              endif

              if(parmid.eq.'number_scattering_angles') then

                 read(1,*) numbertheta

                 cycle

              endif

              if(parmid.eq.'normalize_scattering_matrix') then

                 read(1,*) normalizesm

                 cycle

              endif

              if(parmid.eq.'incident_azimuth_angle_deg') then

                 read(1,*) alphadeg

                 cycle

              endif

              if(parmid.eq.'incident_polar_angle_deg') then

                 read(1,*) betadeg

                 cycle

              endif

              if(parmid.eq.'calculate_scattering_coefficients') then

                 read(1,*) calcamn

                 cycle

              endif

              if(parmid.eq.'scattering_coefficient_file') then

                 read(1,'(a)') amnfile

                 if(amnfile.eq.' ') then

                    amnfile='amn-temp.dat'

                 else

                    amnfile=amnfile(:index(amnfile,' '))

                 endif

                 cycle

              endif

              if(parmid.eq.'track_iterations') then

                 read(1,*) trackiterations

                 cycle

              endif

              if(parmid.eq.'calculate_near_field') then

                 read(1,*) calcnf

                 cycle

              endif

              if(parmid.eq.'near_field_plane_coord') then

                 read(1,*) nfplane

                 cycle

              endif

              if(parmid.eq.'near_field_plane_position') then

                 read(1,*) nfplanepos

                 cycle

              endif

              if(parmid.eq.'near_field_plane_vertices') then

                 read(1,*) nfplanevert

                 cycle

              endif

              if(parmid.eq.'spacial_step_size') then

                 read(1,*) deltax

                 cycle

              endif

              if(parmid.eq.'polarization_angle_deg') then

                 read(1,*) gammadeg

                 cycle

              endif

              if(parmid.eq.'near_field_output_file') then

                 read(1,'(a)') nfoutputfile

                 if(nfoutputfile.eq.' ') then

                    nfoutputfile='nf-temp.dat'

                 else

                    nfoutputfile=nfoutputfile(:index(nfoutputfile,' '))

                 endif

                 cycle

              endif

              if(parmid.eq.'near_field_output_data') then

                 read(1,*) nfoutdata

                 cycle

              endif

              if(parmid.eq.'plane_wave_epsilon') then

                 read(1,*) epspw

                 cycle

              endif

              if(parmid.eq.'gaussian_beam_constant') then

                 read(1,*) cgaussbeam

                 cycle

              endif

              if(parmid.eq.'gaussian_beam_focal_point') then

                 read(1,*) gaussbeamfocus

                 cycle

              endif

              if(parmid.eq.'t_matrix_convergence_epsilon') then

                 read(1,*) epstcon

                 cycle

              endif

              if(parmid.eq.'calculate_t_matrix') then

                 read(1,*) calctmatrix

                 cycle

              endif

              if(parmid.eq.'t_matrix_file') then

                 read(1,'(a)') tmatrixfile

                 if(tmatrixfile.eq.' ') then

                    tmatrixfile='tmatrix-temp.dat'

                 else

                    tmatrixfile=tmatrixfile(:index(tmatrixfile,' '))

                 endif

                 cycle

              endif

              if(parmid.eq.'sphere_sizes_and_positions') exit

              if(parmid.eq.'end_of_options') exit

              write(*,'('' warning: unknown parameter ID:'',a35)') parmid

           enddo

  !

  !  end of parameter input options.   Input of sphere data follows

  !

  10       write(runprintunit,'('' input file is '',a30)') inputfile

           if(positionfile.ne.'at_bottom'.and.positionfile.ne.' ') then

              close(1)

              open(1,file=positionfile)

              parmid='rewind'

           endif

  !

  !  find number of records in position file

  !

           call numberinrecord(1,parmid,numrec)

           if(printdata.eq.1) write(runprintunit,'('' position data has '',i3,'' records'')') numrec

           nsphere=numberspheres

           iunit=1

           allocate(sdat(numrec))

           allocate(xsp(0:nsphere),rpos(3,0:nsphere),ri(2,0:nsphere),stat=ierr)

           xvsp=0.d0

           do i=1,nsphere

              read(iunit,*,end=20) sdat

              xsp(i)=sdat(1)*lengthscalefactor

              rpos(1:3,i)=sdat(2:4)*lengthscalefactor

              if(numrec.gt.4) then

                 rireal=sdat(5)*realriscalefactor

                 riimag=sdat(6)*imriscalefactor

              else

                 rireal=realriscalefactor

                 riimag=imriscalefactor

              endif

              if(numrec.gt.6) then

                 betareal=sdat(7)*realchiralfactor

                 betaimag=sdat(8)*imchiralfactor

              else

                 betareal=realchiralfactor

                 betaimag=imchiralfactor

              endif

              ribulk=dcmplx(rireal,riimag)

              beta=dcmplx(betareal,betaimag)

              if(beta.eq.(0.d0,0.d0)) then

                 ri(1,i)=ribulk

                 ri(2,i)=ribulk

              else

                 ri(1,i)=ribulk/(1.d0-beta*ribulk)

                 ri(2,i)=ribulk/(1.d0+beta*ribulk)

              endif

              xvsp=xvsp+xsp(i)**3.d0

           enddo

  20       nsphere=min(nsphere,i-1)

           close(iunit)

           deallocate(sdat)

           if(nsphere.ne.numberspheres.and.printdata.eq.1) then

              write(runprintunit,'('' warning: insufficient position points in file.'')')

              write(runprintunit,'('' number of spheres truncated to:'',i5)') nsphere

           endif

  !

  ! check for overlapping spheres, and find maximum translation

  !

           rijmax=0.

           do i=1,nsphere

              do j=i+1,nsphere

                 xij=rpos(:,i)-rpos(:,j)

                 rij=sqrt(dot_product(xij,xij))

                 rijmax=max(rijmax,rij)

                 if(rij/(xsp(i)+xsp(j)).lt..999d0) then

                    write(runprintunit,'('' warning: spheres '',i4,'' and '',i4 '' overlap. '',&

                    & '' scaled distance:'' f8.4)') i,j,rij/(xsp(i)+xsp(j))

                 endif

              enddo

           enddo

           if(rijmax.gt.nfdistance) then

              fftranpresent=1

           else

              fftranpresent=0

           endif

           numberspheres=nsphere

           xvsp=xvsp**(1.d0/3.d0)

           gaussbeamfocus=gaussbeamfocus*lengthscalefactor

           if(nsphere.eq.1) then

              rposmean=rpos(:,1)

              rpos(:,1)=0.d0

              xspmax=xsp(1)

           else

              rposmean=0.d0

              do i=1,nsphere

                 rposmean=rposmean+rpos(:,i)

              enddo

              rposmean=rposmean/dble(nsphere)

              rmax=0.d0

  !

  !  the target origin is defined as the GB focal point.

  !

              do i=1,nsphere

  !               rpos(1:3,i)=rpos(1:3,i)-rposmean(1:3)

                 rpos(1:3,i)=rpos(1:3,i)-gaussbeamfocus(1:3)

                 rtoi=dot_product(rpos(:,i),rpos(:,i))

                 if(rtoi.gt.rmax) then

                    rmax=rtoi

                    imax=i

                 endif

              enddo

              xspmax=sqrt(rmax)+xsp(imax)

           endif

  !

  !  xsp(0) is the circumscribing sphere size parameter

  !

           xsp(0)=xspmax

           ri(1,0)=(1.d0,0.d0)

           ri(2,0)=(1.d0,0.d0)

           rpos(:,0)=0.d0

  !

  !  write run data to run file and output file

  !

           if(printdata.eq.1) then

              call writerundata(runprintunit)

              call flush(runprintunit)

              open(1,file=outputfile,status='replace',action='write')

              call writerundata(1)

              close(1)

           endif

           end subroutine inputdata

  !

  !  writes run data to output unit iunit

  !

  !

  !  last revised: 15 January 2011

  !  30 March 2011: added optical activity

  !

           subroutine writerundata(iunit)

           implicit none

           integer :: iunit,i

           character*1 :: lf

           if(iunit.ne.1) then

              lf = ' '

           else

              lf = '/'

           endif

           write(iunit,'('' number of spheres, volume size parameter:'' '//lf//',i5,e13.5)') &

                         numberspheres,xvsp

           write(iunit,'('' position file:'' '//lf//',a)') positionfile

           write(iunit,'('' output file:'' '//lf//',a)') outputfile

           write(iunit,'('' length, ref. indx. scale factors:'' '//lf//',3f8.3)') lengthscalefactor, &

                        realriscalefactor,imriscalefactor

           write(iunit,'('' chiral factors:'' '//lf//',2e13.5)')  &

                        realchiralfactor,imchiralfactor

           write(iunit,'('' thetamin, thetamax, num. theta:'' '//lf//',2f9.1,i5)') &

                        thetamindeg,thetamaxdeg,numbertheta

           write(iunit,'('' epsmie, epssoln, max number iterations:'' '//lf//',2e12.4,i5)') epsmie, &

                        epssoln, numberiterations

           if(fftranpresent.eq.1) then

              write(iunit,'('' far field kr, iterations/correction:'' '//lf//',e12.4,i5)') &

                   nfdistance,niterstep

           else

              write(iunit,'('' all translations computed exactly'' '//lf//')')

           endif

           if(cgaussbeam.ne.0.d0) then

              write(iunit,'('' gaussian incident beam: 1/width:'' '//lf//',f9.4,)') cgaussbeam

              write(iunit,'('' beam focal point:'' '//lf//',3f9.3,)') gaussbeamfocus

           else

              write(iunit,'('' plane wave incidence'')')

           endif

           if(fixedorrandom.eq.0) then

              write(iunit,'('' fixed orientation calculations'')')

              write(iunit,'('' scattering plane, incident alpha, beta:'' '//lf//',3f9.2)') &

                       phideg,alphadeg,betadeg

              write(iunit,'('' common expansion epsilon:'' '//lf//',e12.4)') epstran

              if(calcamn.eq.0) then

                 write(iunit,'('' scattering coefficients read from file '' '//lf//',a)') amnfile

              else

                 write(iunit,'('' scattering coefficients calculated, stored in file '' '//lf//',a)') amnfile

              endif

              if(calcnf.eq.1) then

                 write(iunit,'('' near field calculated, stored in file '' '//lf//',a)') nfoutputfile

                 write(iunit,'('' near field data output option: '' '//lf//',i4)') nfoutdata

                 write(iunit,'('' near field plane, position: '' '//lf//', i4,f9.3)') nfplane, nfplanepos

                 write(iunit,'('' near field plane vertices: '' '//lf//',4f9.3)') nfplanevert

                 write(iunit,'('' spacial step size:'' '//lf//',f9.4)') deltax

                 write(iunit,'('' polarization angle, deg.:'' '//lf//',f9.2)') gammadeg

                 write(iunit,'('' plane wave epsilon:'' '//lf//',e13.5)') epspw

              endif

           else

              write(iunit,'('' random orientation calculations'')')

              if(calctmatrix.eq.0) then

                 write(iunit,'('' t matrix read from file '' '//lf//',a)') tmatrixfile

              elseif(calctmatrix.eq.1) then

                 write(iunit,'('' t matrix calculated, stored in file '' '//lf//',a)') tmatrixfile

                 write(iunit,'('' t matrix convergence epsilon:'' '//lf//',e12.4)') epstcon

              else

                 write(iunit,'('' t matrix calculated from end of file '' '//lf//',a)') tmatrixfile

                 write(iunit,'('' t matrix convergence epsilon:'' '//lf//',e12.4)') epstcon

              endif

           endif

           end subroutine writerundata

  !

  !  getspheredata: retrieves sphere data

  !

  !

  !  last revised: 15 January 2011

  !  30 March 2011: added optical activity

  !

           subroutine getspheredata(number_spheres, sphere_size_parameters, sphere_positions, &

                sphere_refractive_indices, volume_size_parameter)

           implicit none

           integer, optional :: number_spheres

           real(8), optional :: sphere_size_parameters(numberspheres), &

                                sphere_positions(3,numberspheres), volume_size_parameter

           complex(8), optional :: sphere_refractive_indices(2,numberspheres)

           if (present(number_spheres)) number_spheres=numberspheres

           if (present(sphere_size_parameters)) sphere_size_parameters(1:numberspheres)=xsp(1:numberspheres)

           if (present(sphere_positions)) sphere_positions(:,1:numberspheres)=rpos(:,1:numberspheres)

           if (present(sphere_refractive_indices)) &

                 sphere_refractive_indices(:,1:numberspheres)=ri(:,1:numberspheres)

           if (present(volume_size_parameter)) volume_size_parameter=xvsp

           end subroutine getspheredata

  

           subroutine getspheredataone(sphere,sphere_size_parameter, sphere_position, &

                sphere_refractive_index)

           implicit none

           integer :: sphere

           real(8), optional :: sphere_size_parameter,sphere_position(3)

           complex(8), optional :: sphere_refractive_index(2)

           if (present(sphere_size_parameter)) sphere_size_parameter=xsp(sphere)

           if (present(sphere_position)) sphere_position(:)=rpos(:,sphere)

           if (present(sphere_refractive_index)) &

                 sphere_refractive_index(:)=ri(:,sphere)

           end subroutine getspheredataone

  !

  !  setspheredata: sets sphere data

  !

           subroutine setspheredata(number_spheres, sphere_size_parameters, sphere_positions, &

                sphere_refractive_indices, volume_size_parameter)

           implicit none

           integer :: i

           integer, optional :: number_spheres

           real(8), optional :: sphere_size_parameters(*), &

                                sphere_positions(3,*), volume_size_parameter

           complex(8), optional :: sphere_refractive_indices(2,*)

           if (present(number_spheres)) then

              numberspheres=number_spheres

              if(allocated(xsp)) deallocate(xsp,rpos,ri)

              allocate(xsp(0:numberspheres),rpos(3,0:numberspheres),ri(2,0:numberspheres))

           endif

           if (present(sphere_size_parameters))    xsp(1:numberspheres)       =sphere_size_parameters(1:numberspheres)

           if (present(sphere_positions))          rpos(:,1:numberspheres)    =sphere_positions(:,1:numberspheres)

           if (present(sphere_refractive_indices)) ri(:,1:numberspheres)      =sphere_refractive_indices(:,1:numberspheres)

           if (present(volume_size_parameter))     xvsp                       =volume_size_parameter

           end subroutine setspheredata

  !

  !  getrunparameters: retrieves run parameters read from input file

  !

  !

  !  last revised: 15 January 2011

  !  30 March 2011: added optical activity

  !

           subroutine getrunparameters(number_spheres,sphere_position_file,output_file, &

                         length_scale_factor,real_ref_index_scale_factor, &

                         imag_ref_index_scale_factor,mie_epsilon,translation_epsilon,solution_epsilon, &

                         max_number_iterations,fixed_or_random_orientation,scattering_plane_angle_deg, &

                         min_scattering_angle_deg,max_scattering_angle_deg,number_scattering_angles, &

                         incident_azimuth_angle_deg,incident_polar_angle_deg,calculate_near_field, &

                         near_field_plane_coord,near_field_plane_position,near_field_plane_vertices, &

                         spacial_step_size,polarization_angle_deg,near_field_output_file, &

                         plane_wave_epsilon,t_matrix_convergence_epsilon,gaussian_beam_constant, &

                         gaussian_beam_focal_point,calculate_t_matrix,t_matrix_file,run_print_file, &

                         run_print_unit,calculate_scattering_coefficients,scattering_coefficient_file, &

                         max_memory_per_processor,track_iterations,near_field_output_data, &

                         real_chiral_factor,imag_chiral_factor,normalize_scattering_matrix, &

                         store_translation_matrix,near_field_distance, &

                         iterations_per_correction)

           implicit none

           integer, optional :: number_spheres,max_number_iterations,fixed_or_random_orientation, &

                                number_scattering_angles,calculate_near_field,near_field_plane_coord, &

                                calculate_t_matrix,run_print_unit,calculate_scattering_coefficients, &

                                max_memory_per_processor,track_iterations,near_field_output_data, &

                                normalize_scattering_matrix,store_translation_matrix, &

                                iterations_per_correction

           real(8), optional :: length_scale_factor,real_ref_index_scale_factor, &

                         imag_ref_index_scale_factor,mie_epsilon,translation_epsilon,solution_epsilon, &

                         scattering_plane_angle_deg, &

                         min_scattering_angle_deg,max_scattering_angle_deg, &

                         incident_azimuth_angle_deg,incident_polar_angle_deg,t_matrix_convergence_epsilon, &

                         near_field_plane_position,near_field_plane_vertices(2,2),spacial_step_size, &

                         polarization_angle_deg,plane_wave_epsilon,gaussian_beam_constant, &

                         gaussian_beam_focal_point(3),real_chiral_factor,imag_chiral_factor, &

                         near_field_distance

           character*30, optional :: sphere_position_file,output_file,near_field_output_file, &

                                     t_matrix_file,run_print_file,scattering_coefficient_file

           if(present(number_spheres))                    number_spheres                    =numberspheres

           if(present(sphere_position_file))              sphere_position_file              =positionfile

           if(present(output_file))                       output_file                       =outputfile

           if(present(length_scale_factor))               length_scale_factor               =lengthscalefactor

           if(present(real_ref_index_scale_factor))       real_ref_index_scale_factor       =realriscalefactor

           if(present(imag_ref_index_scale_factor))       imag_ref_index_scale_factor       =imriscalefactor

           if(present(mie_epsilon))                       mie_epsilon                       =epsmie

           if(present(translation_epsilon))               translation_epsilon               =epstran

           if(present(solution_epsilon))                  solution_epsilon                  =epssoln

           if(present(max_number_iterations))             max_number_iterations             =numberiterations

           if(present(track_iterations))                  track_iterations                  =trackiterations

           if(present(max_memory_per_processor))          max_memory_per_processor          =maxmemperproc

           if(present(fixed_or_random_orientation))       fixed_or_random_orientation       =fixedorrandom

           if(present(scattering_plane_angle_deg))        scattering_plane_angle_deg        =phideg

           if(present(min_scattering_angle_deg))          min_scattering_angle_deg          =thetamindeg

           if(present(max_scattering_angle_deg))          max_scattering_angle_deg          =thetamaxdeg

           if(present(number_scattering_angles))          number_scattering_angles          =numbertheta

           if(present(normalize_scattering_matrix))       normalize_scattering_matrix       =normalizesm

           if(present(incident_azimuth_angle_deg))        incident_azimuth_angle_deg        =alphadeg

           if(present(incident_polar_angle_deg))          incident_polar_angle_deg          =betadeg

           if(present(t_matrix_convergence_epsilon))      t_matrix_convergence_epsilon      =epstcon

           if(present(calculate_near_field))              calculate_near_field              =calcnf

           if(present(near_field_plane_coord))            near_field_plane_coord            =nfplane

           if(present(near_field_plane_position))         near_field_plane_position         =nfplanepos

           if(present(near_field_plane_vertices))         near_field_plane_vertices         =nfplanevert

           if(present(spacial_step_size))                 spacial_step_size                 =deltax

           if(present(polarization_angle_deg))            polarization_angle_deg            =gammadeg

           if(present(near_field_output_file))            near_field_output_file            =nfoutputfile

           if(present(near_field_output_data))            near_field_output_data            =nfoutdata

           if(present(plane_wave_epsilon))                plane_wave_epsilon                =epspw

           if(present(gaussian_beam_constant))            gaussian_beam_constant            =cgaussbeam

           if(present(gaussian_beam_focal_point))         gaussian_beam_focal_point         =gaussbeamfocus

           if(present(t_matrix_file))                     t_matrix_file                     =tmatrixfile

           if(present(calculate_t_matrix))                calculate_t_matrix                =calctmatrix

           if(present(run_print_file))                    run_print_file                    =printfile

           if(present(run_print_unit))                    run_print_unit                    =runprintunit

           if(present(calculate_scattering_coefficients)) calculate_scattering_coefficients =calcamn

           if(present(scattering_coefficient_file))       scattering_coefficient_file       =amnfile

           if(present(real_chiral_factor))                real_chiral_factor                =realchiralfactor

           if(present(imag_chiral_factor))                imag_chiral_factor                =imchiralfactor

           if(present(store_translation_matrix))          store_translation_matrix          =storetranmat

           if(present(near_field_distance))               near_field_distance               =nfdistance

           if(present(iterations_per_correction))         iterations_per_correction         =niterstep

           end subroutine getrunparameters

  !

  !  set run parameters: set run parameters

  !

  !

  !  last revised: 15 January 2011

  !  30 March 2011: added optical activity

  !

           subroutine setrunparameters(number_spheres,sphere_position_file,output_file, &

                         length_scale_factor,real_ref_index_scale_factor, &

                         imag_ref_index_scale_factor,mie_epsilon,translation_epsilon,solution_epsilon, &

                         max_number_iterations,fixed_or_random_orientation,scattering_plane_angle_deg, &

                         min_scattering_angle_deg,max_scattering_angle_deg,number_scattering_angles, &

                         incident_azimuth_angle_deg,incident_polar_angle_deg,calculate_near_field, &

                         near_field_plane_coord,near_field_plane_position,near_field_plane_vertices, &

                         spacial_step_size,polarization_angle_deg,near_field_output_file, &

                         plane_wave_epsilon,t_matrix_convergence_epsilon,gaussian_beam_constant, &

                         gaussian_beam_focal_point,calculate_t_matrix,t_matrix_file,run_print_file, &

                         run_print_unit,calculate_scattering_coefficients,scattering_coefficient_file, &

                         max_memory_per_processor,track_iterations,near_field_output_data, &

                         real_chiral_factor,imag_chiral_factor,store_translation_matrix, &

                         near_field_distance,iterations_per_correction)

           implicit none

           integer, optional :: number_spheres,max_number_iterations,fixed_or_random_orientation, &

                                number_scattering_angles,calculate_near_field,near_field_plane_coord, &

                                calculate_t_matrix,run_print_unit,calculate_scattering_coefficients, &

                                max_memory_per_processor,track_iterations,near_field_output_data, &

                                store_translation_matrix,iterations_per_correction

           real(8), optional :: length_scale_factor,real_ref_index_scale_factor, &

                         imag_ref_index_scale_factor,mie_epsilon,translation_epsilon,solution_epsilon, &

                         scattering_plane_angle_deg,near_field_distance,&

                         min_scattering_angle_deg,max_scattering_angle_deg, &

                         incident_azimuth_angle_deg,incident_polar_angle_deg,t_matrix_convergence_epsilon, &

                         near_field_plane_position,near_field_plane_vertices(2,2),spacial_step_size, &

                         polarization_angle_deg,plane_wave_epsilon,gaussian_beam_constant, &

                         gaussian_beam_focal_point(3),real_chiral_factor,imag_chiral_factor

           character*30, optional :: sphere_position_file,output_file,near_field_output_file, &

                                     t_matrix_file,run_print_file,scattering_coefficient_file

  

           if(present(number_spheres))                     numberspheres        =number_spheres

           if(present(sphere_position_file))               positionfile         =sphere_position_file

           if(present(output_file))                        outputfile           =output_file

           if(present(length_scale_factor))                lengthscalefactor    =length_scale_factor

           if(present(real_ref_index_scale_factor))        realriscalefactor    =real_ref_index_scale_factor

           if(present(imag_ref_index_scale_factor))        imriscalefactor      =imag_ref_index_scale_factor

           if(present(mie_epsilon))                        epsmie               =mie_epsilon

           if(present(translation_epsilon))                epstran              =translation_epsilon

           if(present(solution_epsilon))                   epssoln              =solution_epsilon

           if(present(max_number_iterations))              numberiterations     =max_number_iterations

           if(present(track_iterations))                   trackiterations      =track_iterations

           if(present(max_memory_per_processor))           maxmemperproc        =max_memory_per_processor

           if(present(fixed_or_random_orientation))        fixedorrandom        =fixed_or_random_orientation

           if(present(scattering_plane_angle_deg))         phideg               =scattering_plane_angle_deg

           if(present(min_scattering_angle_deg))           thetamindeg          =min_scattering_angle_deg

           if(present(max_scattering_angle_deg))           thetamaxdeg          =max_scattering_angle_deg

           if(present(number_scattering_angles))           numbertheta          =number_scattering_angles

           if(present(incident_azimuth_angle_deg))         alphadeg             =incident_azimuth_angle_deg

           if(present(incident_polar_angle_deg))           betadeg              =incident_polar_angle_deg

           if(present(t_matrix_convergence_epsilon))       epstcon              =t_matrix_convergence_epsilon

           if(present(calculate_near_field))               calcnf               =calculate_near_field

           if(present(near_field_plane_coord))             nfplane              =near_field_plane_coord

           if(present(near_field_plane_position))          nfplanepos           =near_field_plane_position

           if(present(near_field_plane_vertices))          nfplanevert          =near_field_plane_vertices

           if(present(spacial_step_size))                  deltax               =spacial_step_size

           if(present(polarization_angle_deg))             gammadeg             =polarization_angle_deg

           if(present(near_field_output_file))             nfoutputfile         =near_field_output_file

           if(present(near_field_output_data))             nfoutdata            =near_field_output_data

           if(present(plane_wave_epsilon))                 epspw                =plane_wave_epsilon

           if(present(gaussian_beam_constant))             cgaussbeam           =gaussian_beam_constant

           if(present(gaussian_beam_focal_point))          gaussbeamfocus       =gaussian_beam_focal_point

           if(present(t_matrix_file))                      tmatrixfile          =t_matrix_file

           if(present(calculate_t_matrix))                 calctmatrix          =calculate_t_matrix

           if(present(run_print_file))                     printfile            =run_print_file

           if(present(run_print_unit))                     runprintunit         =run_print_unit

           if(present(calculate_scattering_coefficients))  calcamn              =calculate_scattering_coefficients

           if(present(scattering_coefficient_file))        amnfile              =scattering_coefficient_file

           if(present(real_chiral_factor))                 realchiralfactor     =real_chiral_factor

           if(present(imag_chiral_factor))                 imchiralfactor       =imag_chiral_factor

           if(present(store_translation_matrix))           storetranmat         =store_translation_matrix

           if(present(near_field_distance))                nfdistance           =near_field_distance

           if(present(iterations_per_correction))          niterstep            =iterations_per_correction

           end subroutine setrunparameters

  

        end module spheredata

  !

  ! module miecoefdata: used to 1) calculate single sphere mie coefficient values,

  ! 2) store values in an allocated array, 3) provide common access to values, and

  ! 4) perform multiplication of coefficient values with vectors containing VWH scattering

  ! coefficients.

  !

  !

  !  last revised: 15 January 2011

  !  30 March 2011: added optical activity

  !

        module miecoefdata

        implicit none

        integer, private :: numeqns,maxorder

        integer, allocatable, private :: nodr(:),nodroffset(:),nblk(:),nblkoffset(:)

        real(8), allocatable, private :: qextmie(:),qabsmie(:)

        complex(8), allocatable, private :: anmie(:,:,:),cnmie(:,:,:)

        interface getmiedata

              module procedure getmiedataall, getmiedataone

        end interface getmiedata

  

        contains

  !

  !  calculation of the max order of sphere expansions and storage of mie coefficients

  !

  !

  !  last revised: 15 January 2011

  !  30 March 2011: added optical activity

  !

           subroutine miecoefcalc(nsphere,xsp,ri,qeps)

           implicit none

           integer :: n,nodrn,nsphere,nodrtot,ierr,nblktot

           real(8) :: qext,qabs,qsca,qeps,xsp(nsphere)

           complex(8) :: ri(2,nsphere)

           complex(8), allocatable :: anp(:,:,:),cnp(:,:,:)

           if(allocated(nodr)) deallocate(nodr,nodroffset,nblk, &

                        nblkoffset,qextmie,qabsmie)

           allocate(nodr(nsphere),nodroffset(nsphere+1), &

                    nblk(nsphere),nblkoffset(nsphere+1),  &

                    qextmie(nsphere),qabsmie(nsphere),stat=ierr)

           if(ierr.ne.0) then

              write(*,'('' bad allocation in nodr: stat:'',i4)') ierr

           endif

           nodrtot=0

           nblktot=0

           maxorder=0

  !

  !  calculate the order limits and efficiencies

  !

           do n=1,nsphere

              call mieoa(xsp(n),ri(1,n),nodrn,qeps,qext,qsca)

              nodroffset(n)=nodrtot

              nblkoffset(n)=nblktot

              nodr(n)=nodrn

              maxorder=max(maxorder,nodrn)

              nblk(n)=nodrn*(nodrn+2)*2

              nodrtot=nodrtot+nodrn

              nblktot=nblktot+nblk(n)

              qextmie(n)=qext

              qabsmie(n)=qext-qsca

           enddo

           nodroffset(nsphere+1)=nodrtot

           nblkoffset(nsphere+1)=nblktot

           numeqns=nblktot

  !

  ! calculate the mie coefficients, and store in memory

  !

           if(allocated(anmie)) deallocate(anmie,cnmie)

           allocate(anmie(2,2,nodrtot),cnmie(2,2,nodrtot),stat=ierr)

           if(ierr.ne.0) then

              write(*,'('' bad allocation in anmie: stat:'',i4)') ierr

           endif

           do n=1,nsphere

              if(abs(ri(1,n)-ri(2,n)).eq.0) then

                 allocate(anp(2,1,nodr(n)),cnp(2,1,nodr(n)))

                 call mieregular(xsp(n),ri(1,n),nodrn,qeps,qext,qsca,anp_mie=anp,cnp_mie=cnp)

                 anmie(1,1,nodroffset(n)+1:nodroffset(n+1))=anp(1,1,1:nodr(n))

                 anmie(2,2,nodroffset(n)+1:nodroffset(n+1))=anp(2,1,1:nodr(n))

                 anmie(1,2,nodroffset(n)+1:nodroffset(n+1))=0.d0

                 anmie(2,1,nodroffset(n)+1:nodroffset(n+1))=0.d0

                 cnmie(1,1,nodroffset(n)+1:nodroffset(n+1))=cnp(1,1,1:nodr(n))

                 cnmie(2,2,nodroffset(n)+1:nodroffset(n+1))=cnp(2,1,1:nodr(n))

                 cnmie(1,2,nodroffset(n)+1:nodroffset(n+1))=0.d0

                 cnmie(2,1,nodroffset(n)+1:nodroffset(n+1))=0.d0

                 deallocate(anp,cnp)

              else

                 allocate(anp(2,2,nodr(n)),cnp(2,2,nodr(n)))

                 call mieoa(xsp(n),ri(1,n),nodrn,qeps,qext,qsca,anp_mie=anp,cnp_mie=cnp)

                 anmie(1:2,1:2,nodroffset(n)+1:nodroffset(n+1))=anp(1:2,1:2,1:nodr(n))

                 cnmie(1:2,1:2,nodroffset(n)+1:nodroffset(n+1))=cnp(1:2,1:2,1:nodr(n))

                 deallocate(anp,cnp)

              endif

           enddo

           end subroutine miecoefcalc

  !

  !  retrieve the array of mie data

  !

  !

  !  last revised: 15 January 2011

  !  30 March 2011: added optical activity

  !

           subroutine getmiedataall(sphere_order, sphere_block, &

                       sphere_order_offset, sphere_block_offset, sphere_qext, &

                       sphere_qabs, sphere_mie_coefficients, sphere_int_mie_coefficients, &

                       number_equations, max_order)

           use spheredata

           implicit none

           integer, optional :: sphere_order(:), sphere_block(:), sphere_order_offset(:), &

                       sphere_block_offset(:),number_equations, max_order

           integer :: i,nsphere

           real(8), optional :: sphere_qext(:), sphere_qabs(:)

           complex(8), optional :: sphere_mie_coefficients(:,:,:,:), &

                         sphere_int_mie_coefficients(:,:,:,:)

           call getspheredata(number_spheres=nsphere)

           if(present(sphere_order)) sphere_order=nodr

           if(present(sphere_block)) sphere_block=nblk

           if(present(sphere_order_offset)) sphere_order_offset=nodroffset

           if(present(sphere_block_offset)) sphere_block_offset=nblkoffset

           if(present(sphere_qext)) sphere_qext=qextmie

           if(present(sphere_qabs)) sphere_qabs=qabsmie

           if(present(number_equations)) number_equations=numeqns

           if(present(max_order)) max_order=maxorder

           if(present(sphere_mie_coefficients)) then

              do i=1,nsphere

                 sphere_mie_coefficients(1:2,1:2,1:nodr(i),i)  &

                  =anmie(1:2,1:2,nodroffset(i)+1:nodroffset(i+1))

              enddo

           endif

           if(present(sphere_int_mie_coefficients)) then

              do i=1,nsphere

                 sphere_int_mie_coefficients(1:2,1:2,1:nodr(i),i)  &

                  =cnmie(1:2,1:2,nodroffset(i)+1:nodroffset(i+1))

              enddo

           endif

           end subroutine getmiedataall

  !

  !  retrieve mie data for a single sphere

  !

  !

  !  last revised: 15 January 2011

  !  30 March 2011: added optical activity

  !

           subroutine getmiedataone(which_sphere, sphere_order, sphere_block, &

                       sphere_order_offset, sphere_block_offset, sphere_qext, &

                       sphere_qabs, sphere_mie_coefficients, sphere_int_mie_coefficients, &

                       number_equations, max_order)

           use spheredata

           implicit none

           integer, optional :: sphere_order, sphere_block, sphere_order_offset, &

                       sphere_block_offset, number_equations, max_order

           integer :: which_sphere

           integer :: i,nsphere

           real(8), optional :: sphere_qext, sphere_qabs

           complex(8), optional :: sphere_mie_coefficients(:,:,:), sphere_int_mie_coefficients(:,:,:)

           i=which_sphere

           if(present(sphere_order)) sphere_order=nodr(i)

           if(present(sphere_block)) sphere_block=nblk(i)

           if(present(sphere_order_offset)) sphere_order_offset=nodroffset(i)

           if(present(sphere_block_offset)) sphere_block_offset=nblkoffset(i)

           if(present(sphere_qext)) sphere_qext=qextmie(i)

           if(present(sphere_qabs)) sphere_qabs=qabsmie(i)

           if(present(number_equations)) number_equations=numeqns

           if(present(max_order)) max_order=maxorder

           if(present(sphere_mie_coefficients)) &

              sphere_mie_coefficients(1:2,1:2,1:nodr(i))  &

                  =anmie(1:2,1:2,nodroffset(i)+1:nodroffset(i+1))

           if(present(sphere_int_mie_coefficients)) &

              sphere_int_mie_coefficients(1:2,1:2,1:nodr(i))  &

                  =cnmie(1:2,1:2,nodroffset(i)+1:nodroffset(i+1))

           end subroutine getmiedataone

  !

  !  retrieve mie coefficients for sphere n

  !  30 March 2011: added optical activity

  !

           function miecoef(n)

           implicit none

           integer :: n

           complex(8), dimension(2,2,nodr(n)) :: miecoef

           miecoef=anmie(1:2,1:2,nodroffset(n)+1:nodroffset(n+1))

           end function miecoef

  

           function internalmiecoef(n)

           implicit none

           integer :: n

           complex(8), dimension(2,2,nodr(n)) :: internalmiecoef

           internalmiecoef=cnmie(1:2,1:2,nodroffset(n)+1:nodroffset(n+1))

           end function internalmiecoef

  !

  !  multiples the solution vector cx by mie coefficients and returns in y

  !  i1: starting sphere, i2: ending sphere

  !

  !

  !  last revised: 15 January 2011

  !  30 March 2011: added optical activity

  !

           subroutine miecoeffmult(i1,i2,neqns,cx,cy)

           implicit none

           integer :: i1,i2,neqns,i,n,p,nodrvec(3),nodri,nblki,noffi,icon,q

           complex(8) :: cx(neqns),cy(neqns)

           complex(8), allocatable :: cxt(:,:,:),an1(:,:,:),cxtt(:,:,:)

  

           do i=i1,i2

              nodri=nodr(i)

              nblki=nblk(i)

              noffi=nblkoffset(i)

              allocate(cxt(0:nodri+1,nodri,2),cxtt(0:nodri+1,nodri,2),an1(2,2,nodri))

              cxt=reshape(cx(noffi+1:noffi+nblki),(/nodri+2,nodri,2/))

              cxtt=0.d0

              an1=miecoef(i)

              do n=1,nodri

                 do p=1,2

                    cxtt(n+1,n:1:-1,p)=an1(p,1,n)*cxt(n+1,n:1:-1,1)+an1(p,2,n)*cxt(n+1,n:1:-1,2)

                    cxtt(0:n,n,p)=an1(p,1,n)*cxt(0:n,n,1)+an1(p,2,n)*cxt(0:n,n,2)

                 enddo

              enddo

              cy(noffi+1:noffi+nblki)=reshape(cxtt(0:nodri+1,1:nodri,1:2),(/nblki/))

              deallocate(cxt,cxtt,an1)

           enddo

           end subroutine miecoeffmult

  

           subroutine internalmiecoeffmult(i1,i2,neqns,cx,cy)

           implicit none

           integer :: i1,i2,neqns,i,n,p,nodrvec(3),nodri,nblki,noffi,icon,q

           complex(8) :: cx(neqns),cy(neqns)

           complex(8), allocatable :: cxt(:,:,:),an1(:,:,:),cxtt(:,:,:)

  

           do i=i1,i2

              nodri=nodr(i)

              nblki=nblk(i)

              noffi=nblkoffset(i)

              allocate(cxt(0:nodri+1,nodri,2),cxtt(0:nodri+1,nodri,2),an1(2,2,nodri))

              cxt=reshape(cx(noffi+1:noffi+nblki),(/nodri+2,nodri,2/))

              cxtt=0.d0

              an1=internalmiecoef(n)

              do n=1,nodri

                 do p=1,2

                    cxtt(n+1,n:1:-1,p)=an1(p,1,n)*cxt(n+1,n:1:-1,1)+an1(p,2,n)*cxt(n+1,n:1:-1,2)

                    cxtt(0:n,n,p)=an1(p,1,n)*cxt(0:n,n,1)+an1(p,2,n)*cxt(0:n,n,2)

                 enddo

              enddo

              cy(noffi+1:noffi+nblki)=reshape(cxtt(0:nodri+1,1:nodri,1:2),(/nblki/))

              deallocate(cxt,cxtt,an1)

           enddo

           end subroutine internalmiecoeffmult

  !

  ! single-sphere lorenz/mie coefficients

  !

  !

  !  last revised: 15 January 2011

  !

           subroutine mieregular(x,ri,nstop,qeps,qext,qsca,anp_mie,cnp_mie)

           use specialfuncs

           implicit none

           integer :: nstop,n,iancalc

           real(8) :: x,qeps,qext,qsca,prn,prp,qext1,err

           complex(8), optional :: anp_mie(2,*), cnp_mie(2,*)

           complex(8) :: ri,y,pcp,xip,da,db,na,nb,an1,an2,cn1,cn2

           complex(8), allocatable :: pc(:),xi(:)

  !

  !  modified LM criterion

  !

           if(qeps.gt.0.) nstop=nint(x+4.*x**(1./3.))+15

  !

  !  user-set order limit

  !

           if(qeps.lt.0) nstop=-qeps

  !

  !  basic calculations follow

  !

           allocate(pc(0:nstop),xi(0:nstop))

           y=x*ri

           call cricbessel(nstop,y,pc)

           call richankel(nstop,x,xi)

           qsca=0.0

           qext=0.0

           do n=1,nstop

              prn=dble(xi(n))

              pcp=pc(n-1)-n*pc(n)/y

              xip=xi(n-1)-n*xi(n)/x

              prp=dble(xip)

              da=ri*xip*pc(n)-xi(n)*pcp

              db=ri*xi(n)*pcp-xip*pc(n)

              na=ri*prp*pc(n)-prn*pcp

              nb=ri*prn*pcp-prp*pc(n)

              an1=-na/da

              an2=-nb/db

              cn1=-dcmplx(0.d0,1.d0)*ri/na

              cn2=dcmplx(0.d0,1.d0)*ri/nb

              if(present(anp_mie)) then

                 anp_mie(1,n)=an1

                 anp_mie(2,n)=an2

              endif

              if(present(cnp_mie)) then

                 cnp_mie(1,n)=cn1

                 cnp_mie(2,n)=cn2

              endif

              qsca=qsca+(n+n+1)*(cdabs(an1)*cdabs(an1) &

                       +cdabs(an2)*cdabs(an2))

              qext1=-(n+n+1)*dble(an1+an2)

              qext=qext+qext1

              err=abs(qext1)/abs(qext)

              if(err.lt.qeps.or.n.eq.nstop) exit

           enddo

           nstop=n

           qsca=2./x/x*qsca

           qext=2./x/x*qext

           deallocate(pc,xi)

           end subroutine mieregular

  !

  ! optically active lorenz/mie coefficients

  ! 30 March 2011

  !

           subroutine mieoa(x,ri,nstop,qeps,qext,qsca,anp_mie,cnp_mie)

           use specialfuncs

           implicit none

           integer :: nstop

           real(8) :: x,qeps,qext,qsca,fn1,err

           complex(8) :: ri(2)

           complex(8), optional :: anp_mie(2,2,*),cnp_mie(2,2,*)

           integer :: n,i,p,q

           real(8) :: psi,psip,qext1

           complex (8) :: xri(2),xip,psicp,psic,wn(2),vn(2),an(2),bn(2), &

                          den,xi,anct(2,2),cnct(2,2),ri0,ci

           complex(8), allocatable :: psicn(:,:),xin(:)

           data ci/(0.d0,1.d0)/

  

           ri0=2.d0/(1.d0/ri(1)+1.d0/ri(2))

           if(qeps.ge.0.) then

              nstop=nint(x+4.*x**(1./3.))+5.

           else

              nstop=-qeps

           endif

           allocate(psicn(0:nstop+1,2),xin(0:nstop+1))

           do i=1,2

              xri(i)=x*ri(i)

              call cricbessel(nstop+1,xri(i),psicn(0,i))

           enddo

           call richankel(nstop+1,x,xin)

           qsca=0.0

           qext=0.0

           do n=1,nstop

              do i=1,2

                 psic=psicn(n,i)

                 psicp=psicn(n-1,i)-dble(n)*psic/xri(i)

                 xi=xin(n)

                 xip=xin(n-1)-dble(n)*xi/x

                 psi=dble(xi)

                 psip=dble(xip)

                 wn(i)=ri0*psic*xip-xi*psicp

                 vn(i)=psic*xip-ri0*xi*psicp

                 an(i)=ri0*psic*psip-psi*psicp

                 bn(i)=psic*psip-ri0*psi*psicp

              enddo

              den=wn(1)*vn(2)+wn(2)*vn(1)

              anct(1,1)=-(vn(1)*an(2)+vn(2)*an(1))/den

              anct(2,2)=-(wn(1)*bn(2)+wn(2)*bn(1))/den

              anct(1,2)=(wn(1)*an(2)-wn(2)*an(1))/den

              anct(2,1)=anct(1,2)

              den=an(1)*bn(2)+an(2)*bn(1)

              cnct(1,1)=-ci*ri(1)*bn(2)/den

              cnct(1,2)=-ci*ri(1)*an(2)/den

              cnct(2,1)=ri(2)*ri0*bn(1)/den

              cnct(2,2)=-ri(2)*ri0*an(1)/den

              if(present(anp_mie)) then

                 do p=1,2

                    do q=1,2

                       anp_mie(p,q,n)=anct(p,q)

                       cnp_mie(p,q,n)=cnct(p,q)

                    enddo

                 enddo

              endif

              qext1=0.d0

              fn1=n+n+1

              do p=1,2

                 do q=1,2

                    qsca=qsca+fn1*cdabs(anct(p,q))*cdabs(anct(p,q))

                 enddo

                 qext1=qext1-fn1*dble(anct(p,p))

              enddo

              qext=qext+qext1

              err=abs(qext1)/abs(qext)

              if(err.lt.qeps.or.n.eq.nstop) exit

           enddo

           nstop=min(n,nstop)

           qsca=2./x/x*qsca

           qext=2./x/x*qext

           return

           end subroutine mieoa

  

        end module miecoefdata

  !

  !  module translation contains subroutines for VSWF translation and rotation

  !

  !

  !  last revised: 15 January 2011

  !

        module translation

        implicit none

        integer, private :: stored_max_order,store_tran_mat

        integer, allocatable, private :: nsizerot(:,:),nsizetran(:,:),nsizeephi(:,:), &

                   noffrot(:,:),nofftran(:,:),noffephi(:,:)

        real(8), private :: near_field_distance

        real(8), allocatable, private :: sphere_position(:,:)

        real(8), target, allocatable, private :: rotmatstore(:)

        complex(8), target, allocatable, private :: tranmatstore(:), ephimatstore(:)

        complex(8), allocatable, private :: rvec_temp(:,:),tvec_temp(:,:),c_temp(:,:,:), &

                    ct_temp(:,:,:),rvec2_temp(:,:),tvec2_temp(:,:),c2_temp(:,:,:), &

                                  ct2_temp(:,:,:)

  

        contains

  !

  !  rotation of expansion coefficients amn by euler angles alpha,beta,gamma

  !  idir=1: forward rotation, idir=-1, reverse rotation.

  !

  !

  !  last revised: 15 January 2011

  !

           subroutine rotvec(alpha,beta,gamma,nmax,mmax,amn,idir)

           use numconstants

           use specialfuncs

           implicit none

           integer :: nmax,mmax,idir,k,n,m,in,kmax,kn,ka,na,p,im,m1

           real(8) :: dc(-nmax-1:nmax+1,-nmax-1:nmax+1),dk0(-nmax-1:nmax+1), &

                      dk01(-nmax-1:nmax+1),sbe,cbe,sbe2,cbe2,sben,dkt, &

                      fmn,dkm0,dkm1,alpha,beta,gamma

           complex(8) :: ealpha,amn(0:nmax+1,nmax,2),ealpham(-nmax:nmax), &

                         amnt(2,-nmax:nmax),a,b,ci,egamma,egammam(-nmax:nmax)

           data ci/(0.d0,1.d0)/

           call init(nmax)

           dc=0.d0

           dk01=0.d0

           dk0=0.d0

           ealpha=cdexp(ci*alpha)

           egamma=cdexp(ci*gamma)

           cbe=cos(beta)

           sbe=sqrt((1.d0+cbe)*(1.d0-cbe))

           cbe2=.5d0*(1.d0+cbe)

           sbe2=.5d0*(1.d0-cbe)

           call ephicoef(ealpha,nmax,ealpham)

           call ephicoef(egamma,nmax,egammam)

           in=1

           dk0(0)=1.d0

           sben=1.d0

           dk01(0)=0.d0

           do n=1,nmax

              kmax=min(n,mmax)

              do k=-kmax,kmax

                 if(k.le.-1) then

                    ka=n+1

                    na=-k

                 else

                    ka=k

                    na=n

                 endif

                 if(idir.eq.1) then

                    amnt(1,k)=amn(ka,na,1)*ealpham(k)

                    amnt(2,k)=amn(ka,na,2)*ealpham(k)

                 else

                    amnt(1,-k)=amn(ka,na,1)*egammam(k)

                    amnt(2,-k)=amn(ka,na,2)*egammam(k)

                 endif

              enddo

              in=-in

              sben=sben*sbe/2.d0

              dk0(n)=in*sben*bcof(n,n)

              dk0(-n)=in*dk0(n)

              dk01(n)=0.d0

              dk01(-n)=0.d0

              dc(0,n)=dk0(n)

              dc(0,-n)=dk0(-n)

              do k=-n+1,n-1

                 dkt=dk01(k)

                 dk01(k)=dk0(k)

                 dk0(k)=(cbe*(n+n-1)*dk01(k)-fnr(n-k-1)*fnr(n+k-1)*dkt) &

                       /(fnr(n+k)*fnr(n-k))

                 dc(0,k)=dk0(k)

              enddo

              im=1

              do m=1,kmax

                 im=-im

                 fmn=1./fnr(n-m+1)/fnr(n+m)

                 m1=m-1

                 dkm0=0.

                 do k=-n,n

                    dkm1=dkm0

                    dkm0=dc(m1,k)

                    dc(m,k)=(fnr(n+k)*fnr(n-k+1)*cbe2*dkm1 &

                           -fnr(n-k)*fnr(n+k+1)*sbe2*dc(m1,k+1) &

                           -k*sbe*dc(m1,k))*fmn

                    dc(-m,-k)=dc(m,k)*(-1)**(k)*im

                 enddo

              enddo

              do m=-n,n

                 if(m.le.-1) then

                    ka=n+1

                    na=-m

                 else

                    ka=m

                    na=n

                 endif

                 a=0.

                 b=0.

                 do k=-kmax,kmax

                    a=a+dc(-k,-m)*amnt(1,k)

                    b=b+dc(-k,-m)*amnt(2,k)

                 enddo

                 if(idir.eq.1) then

                    amn(ka,na,1)=a*egammam(m)

                    amn(ka,na,2)=b*egammam(m)

                 else

                    amn(ka,na,1)=a*ealpham(m)

                    amn(ka,na,2)=b*ealpham(m)

                 endif

              enddo

           enddo

           end subroutine rotvec

  !

  !  sets up the stored translation matrices for mpi

  !

  !

  !  last revised: 15 January 2011

  !  november 2011: added near and far field translation

  !

           subroutine mpirottranmtrxsetup(nsphere,nodr,rpos,ri,istore,nfdistance,&

                      runprintunit)

           use mpidefs

           use mpidata

           use intrinsics

           use numconstants

           use specialfuncs

           implicit none

           integer :: nsphere,nodr(nsphere),i,j,nodrmax,nodrmin,n,ntotrot,ntottran,ntotephi, &

                      ierr,n1,n2,nt,rank,nsrank,runprintunit,isendok,tag,sendrank,numprocs,brank, &

                      nsend,istore

           real(8) :: rpos(3,nsphere),xij(3),r,ct,memused(1),memusedmax(1),memusedmin(1), &

                      nfdistance,nfdistancei

           real(8), allocatable :: rotmat(:,:)

           complex(8) :: ri,ephi

           complex(8), allocatable :: tranmat(:,:,:),ephimat(:),pivec(:,:,:)

           data isendok,tag/0,1/

           numprocs=proc_per_group

           rank=group_rank

           brank=base_rank

           nsrank=mpi_sphere_number(rank)

           nodrmax=maxval(nodr)

           call init(nodrmax)

           store_tran_mat=istore

           near_field_distance=nfdistance

           if(allocated(sphere_position)) deallocate(sphere_position)

           allocate(sphere_position(3,nsphere))

           sphere_position=rpos

           if(istore.eq.0) then

              return

           endif

           if(allocated(nsizerot)) deallocate(nsizerot,nsizetran,nsizeephi,noffrot,nofftran,noffephi)

           allocate(nsizerot(nsphere,nsphere),nsizetran(nsphere,nsphere),nsizeephi(nsphere,nsphere), &

                    noffrot(nsphere,nsphere),nofftran(nsphere,nsphere),noffephi(nsphere,nsphere))

           if(allocated(rvec_temp)) deallocate(rvec_temp,tvec_temp,c_temp,ct_temp, &

                       rvec2_temp,tvec2_temp,c2_temp,ct2_temp)

           allocate(rvec_temp(-nodrmax:nodrmax,2),tvec_temp(nodrmax,2), &

                    c_temp(-nodrmax:nodrmax,nodrmax,2),ct_temp(nodrmax,2,2), &

                         rvec2_temp(-nodrmax:nodrmax,2),tvec2_temp(nodrmax,2), &

                         c2_temp(-nodrmax:nodrmax,nodrmax,2),ct2_temp(nodrmax,2,2))

           stored_max_order=nodrmax

  !

  !  determine the memory requirements

  !

           ntotrot=0

           ntottran=0

           ntotephi=0

           do i=mpi_sphere_index(rank)+1,mpi_sphere_index(rank)+nsrank

              do j=1,nsphere

                 xij(:)=rpos(:,i)-rpos(:,j)

                 if(j.ne.i) then

                    if(nfdistance.lt.0.) then

                       nfdistancei=(.5*dble(nodr(i)+nodr(j)))**2.

                    else

                       nfdistancei=nfdistance

                    endif

                    r=sqrt(dot_product(xij,xij))

                    if(r.le.nfdistancei) then

                       nodrmax=max(nodr(j),nodr(i))

                       nodrmin=min(nodr(j),nodr(i))

                       noffrot(i,j)=ntotrot

                       nofftran(i,j)=ntottran

                       noffephi(i,j)=ntotephi

                       nsizerot(i,j)=(2*nodrmin+1)*(1+nodrmax*(nodrmax+2))

                       nsizetran(i,j)=nodr(i)*nodr(j)*(nodr(j)+3)

                       nsizeephi(i,j)=2*nodrmax+1

                       ntotrot=ntotrot+nsizerot(i,j)

                       ntottran=ntottran+nsizetran(i,j)

                       ntotephi=ntotephi+nsizeephi(i,j)

                    endif

                    if(r.gt.nfdistancei.and.istore.eq.2) then

                       nodrmax=max(nodr(j),nodr(i))

                       nofftran(i,j)=ntottran

                       nsizetran(i,j)=2*nodrmax*(nodrmax+2)

                       ntottran=ntottran+nsizetran(i,j)

                    endif

                 endif

              enddo

           enddo

           memused(1)=dble(8*ntotrot+16*(ntottran+ntotephi))*1.d-6

           nsend=1

           call ms_mpi(mpi_command='reduce',mpi_send_buf_dp=memused,mpi_recv_buf_dp=memusedmax,&

                       mpi_number=1,mpi_rank=0,mpi_operation=ms_mpi_max)

           call ms_mpi(mpi_command='reduce',mpi_send_buf_dp=memused,mpi_recv_buf_dp=memusedmin,&

                       mpi_number=1,mpi_rank=0,mpi_operation=ms_mpi_min)

           call ms_mpi(mpi_command='barrier')

           if(brank.eq.0) then

              write(runprintunit,'('' maximum translation matrix storage:'',f9.4,'' MB'')') memusedmax

              write(runprintunit,'('' minimum translation matrix storage:'',f9.4,'' MB'')') memusedmin

              call flush(runprintunit)

           endif

  !

  !  calculate the matrices and store in memory

  !

           if(allocated(rotmatstore)) deallocate(rotmatstore,tranmatstore,ephimatstore)

           allocate(rotmatstore(ntotrot),stat=ierr)

           allocate(tranmatstore(ntottran),stat=ierr)

           allocate(ephimatstore(ntotephi),stat=ierr)

           do i=mpi_sphere_index(rank)+1,mpi_sphere_index(rank)+nsrank

              do j=1,nsphere

                 if(j.ne.i) then

                    nodrmax=max(nodr(j),nodr(i))

                    nodrmin=min(nodr(j),nodr(i))

                    xij=rpos(:,i)-rpos(:,j)

                    call cartosphere(xij,r,ct,ephi)

                    if(nfdistance.lt.0.) then

                       nfdistancei=(.5*dble(nodr(i)+nodr(j)))**2.

                    else

                       nfdistancei=nfdistance

                    endif

                    if(r.le.nfdistancei) then

  !

  !  rotation matrix

  !

                       n1=noffrot(i,j)+1

                       nt=nsizerot(i,j)

                       n2=n1+nt-1

                       allocate(rotmat(-nodrmin:nodrmin,0:nodrmax*(nodrmax+2)))

                       call rotcoef(ct,nodrmin,nodrmax,rotmat)

                       rotmatstore(n1:n2)=reshape(rotmat,(/nt/))

                       deallocate(rotmat)

  !

  !  axial translation matrix

  !

                       n1=nofftran(i,j)+1

                       nt=nsizetran(i,j)

                       n2=n1+nt-1

                       allocate(tranmat(nodr(i),nodr(j)*(nodr(j)+3)/2,2))

                       call axialtrancoef(3,r,ri,nodr(i),nodr(j),tranmat)

                       tranmatstore(n1:n2)=reshape(tranmat,(/nt/))

                       deallocate(tranmat)

  !

  !  ephi matrix

  !

                       n1=noffephi(i,j)+1

                       nt=nsizeephi(i,j)

                       n2=n1+nt-1

                       allocate(ephimat(-nodrmax:nodrmax))

                       call ephicoef(ephi,nodrmax,ephimat)

                       ephimatstore(n1:n2)=ephimat(-nodrmax:nodrmax)

                       deallocate(ephimat)

  !

  !  ff translation matrix storage

  !

                    elseif(istore.eq.2) then

                       n1=nofftran(i,j)+1

                       nt=nsizetran(i,j)

                       n2=n1+nt-1

                       nodrmax=max(nodr(j),nodr(i))

                       allocate(pivec(0:nodrmax+1,nodrmax,2))

                       call pifunc(ct,ephi,nodrmax,nodrmax,pivec)

                       tranmatstore(n1:n2)=reshape(pivec,(/nt/))

                       deallocate(pivec)

                    endif

                 endif

              enddo

           enddo

           end subroutine mpirottranmtrxsetup

  !

  !  clear the stored translation matrices

  !

  !

  !  last revised: 15 January 2011

  !

           subroutine rottranmtrxclear()

           implicit none

           if(allocated(rotmatstore)) deallocate(rotmatstore,tranmatstore,ephimatstore)

           if(allocated(sphere_position)) deallocate(sphere_position)

           end subroutine rottranmtrxclear

  !

  !  translation coefficient vector cx by xij in medium with ri by rotation-translation

  !  itype: 1 or 3

  !  icalc: =1, calculate matrices; = 0, use stored matrix

  !  idir: =1, translation of xij, =-1, -xij (reverse)

  !  itran=1, A(i-j) a(j), = -1, a(j) A(i-j)

  !

  !

  !  last revised: 15 January 2011

  !

           subroutine rottran(cx,cy,xij,ri,nodrx,nodry,itype,icalc,idir,itran)

           use numconstants

           use specialfuncs

           implicit none

           integer :: nodrx,nodry,itype,icalc,idir,itran,nmax,nmin,n,m,p,nblk

           real(8) :: xij(3),r,ct

           complex(8) :: ri,ephi,cx(0:nodrx+1,nodrx,2),cy(0:nodry+1,nodry,2)

           real(8), allocatable, save :: rotmat(:,:)

           complex(8), allocatable, save  :: ephimat(:), tranmat(:,:,:)

           if(icalc.eq.1) then

              nmax=max(nodrx,nodry)

              nmin=min(nodrx,nodry)

              call cartosphere(xij,r,ct,ephi)

              if(r.lt.1.d-4) then

                 do p=1,2

                    do n=1,nmin

                       do m=0,nmin+1

                          cy(m,n,p)=cy(m,n,p)+cx(m,n,p)

                       enddo

                    enddo

                 enddo

                 return

              endif

              if(allocated(ephimat)) deallocate(rotmat,ephimat,tranmat)

              if(nmax.gt.stored_max_order) then

                 if(allocated(rvec_temp)) deallocate(rvec_temp,tvec_temp, &

                         c_temp,ct_temp,rvec2_temp,tvec2_temp, &

                         c2_temp,ct2_temp)

                 allocate(rvec_temp(-nmax:nmax,2),tvec_temp(nmax,2), &

                         c_temp(-nmax:nmax,nmax,2),ct_temp(nmax,2,2), &

                         rvec2_temp(-nmax:nmax,2),tvec2_temp(nmax,2), &

                         c2_temp(-nmax:nmax,nmax,2),ct2_temp(nmax,2,2))

                 stored_max_order=nmax

              endif

              nblk=(nodrx*(nodrx+3))/2

              allocate(rotmat(-nmin:nmin,0:nmax*(nmax+2)))

              allocate(ephimat(-nmax:nmax))

              allocate(tranmat(1:nodry,1:nblk,1:2))

              call rotcoef(ct,nmin,nmax,rotmat)

  !            call axialtrancoef(itype,r,ri,nodry,nodrx,tranmat)

              call axialtrancoefrecurrence(itype,r,ri,nodry,nodrx,tranmat)

              call ephicoef(ephi,nmax,ephimat)

           endif

           call rottranmtrx(cx,cy,idir,itran,nodrx,nodry,ephimat,rotmat,tranmat)

           return

           end subroutine rottran

  !

  !  far field formula for outgoing SVWF translation

  !  October 2011

  !

           subroutine farfieldtranslation(cx,cy,xij,ri,nodrx,nodry,icase, &

                      stored_pivec_matrix)

           use numconstants

           use specialfuncs

           implicit none

           integer :: nodrx,nodry,itype,icalc,icase,nmax,nmin,n,m,p,nblk,im

           real(8) :: xij(3),r,ct,xijt(3)

           complex(8) :: ri,ephi,cx(0:nodrx+1,nodrx,2),cy(0:nodry+1,nodry,2), &

                         cxt(0:nodrx+1,nodrx,2),cyt(0:nodry+1,nodry,2), &

                         sumx(2),c1,pivec(0:max(nodrx,nodry)+1,max(nodrx,nodry),2)

           complex(8), optional :: stored_pivec_matrix(0:max(nodrx,nodry)+1,max(nodrx,nodry),2)

  

           call cartosphere(xij,r,ct,ephi)

           nmax=max(nodrx,nodry)

           if(present(stored_pivec_matrix)) then

              pivec=stored_pivec_matrix

           else

              call pifunc(ct,ephi,nmax,nmax,pivec)

           endif

           if(icase.eq.1) then

              sumx(1)=sum(pivec(0:nodrx+1,1:nodrx,1:2)*cx(0:nodrx+1,1:nodrx,1:2))

              sumx(2)=sum(pivec(0:nodrx+1,1:nodrx,2:1:-1)*cx(0:nodrx+1,1:nodrx,1:2))

              sumx=sumx*cdexp((0.d0,1.d0)*ri*r)/((0.d0,1.d0)*ri*r)*8.d0

              cyt(0:nodry+1,1:nodry,1) = conjg(pivec(0:nodry+1,1:nodry,1))*sumx(1) &

                    +conjg(pivec(0:nodry+1,1:nodry,2))*sumx(2)

              cyt(0:nodry+1,1:nodry,2) = conjg(pivec(0:nodry+1,1:nodry,2))*sumx(1) &

                    +conjg(pivec(0:nodry+1,1:nodry,1))*sumx(2)

           else

              do n=1,nodrx

                 do p=1,2

                    im=(-1)**(n+p)

                    cxt(n+1,1:n,p)=im*cx(n+1,1:n,p)

                    cxt(0:n,n,p)=im*cx(0:n,n,p)

                 enddo

              enddo

              sumx(1)=sum(conjg(pivec(0:nodrx+1,1:nodrx,1:2))*cxt(0:nodrx+1,1:nodrx,1:2))

              sumx(2)=sum(conjg(pivec(0:nodrx+1,1:nodrx,2:1:-1))*cxt(0:nodrx+1,1:nodrx,1:2))

              sumx=sumx*cdexp((0.d0,1.d0)*ri*r)/((0.d0,1.d0)*ri*r)*8.d0

              cyt(0:nodry+1,1:nodry,1) = pivec(0:nodry+1,1:nodry,1)*sumx(1) &

                    +pivec(0:nodry+1,1:nodry,2)*sumx(2)

              cyt(0:nodry+1,1:nodry,2) = pivec(0:nodry+1,1:nodry,2)*sumx(1) &

                    +pivec(0:nodry+1,1:nodry,1)*sumx(2)

              do n=1,nodry

                 do p=1,2

                    im=(-1)**(n+p)

                    cyt(n+1,1:n,p)=im*cyt(n+1,1:n,p)

                    cyt(0:n,n,p)=im*cyt(0:n,n,p)

                 enddo

              enddo

           endif

           cy=cy+cyt

           end subroutine farfieldtranslation

  !

  ! far field translation: normal and transpose, for bcgm solution

  ! october 2011

  !

           subroutine farfieldtranslationtwovec(cx1,cx2,cy1,cy2,xij,ri,nodrx,nodry, &

                      stored_pivec_matrix)

           use numconstants

           use specialfuncs

           implicit none

           integer :: nodrx,nodry,itype,icalc,icase,nmax,nmin,n,m,p,nblk,im

           real(8) :: xij(3),r,ct,xijt(3)

           complex(8) :: ri,ephi,cx1(0:nodrx+1,nodrx,2),cy1(0:nodry+1,nodry,2), &

                         cx2(0:nodrx+1,nodrx,2),cy2(0:nodry+1,nodry,2), &

                         cxt(0:nodrx+1,nodrx,2),cyt1(0:nodry+1,nodry,2), &

                         cyt2(0:nodry+1,nodry,2), &

                         sumx(2),c1,phasefunc, &

                         pivec(0:max(nodrx,nodry)+1,max(nodrx,nodry),2)

           complex(8), optional :: stored_pivec_matrix(0:max(nodrx,nodry)+1,max(nodrx,nodry),2)

  

           call cartosphere(xij,r,ct,ephi)

           nmax=max(nodrx,nodry)

           if(present(stored_pivec_matrix)) then

              pivec=stored_pivec_matrix

           else

              call pifunc(ct,ephi,nmax,nmax,pivec)

           endif

           phasefunc=cdexp((0.d0,1.d0)*ri*r)/((0.d0,1.d0)*ri*r)*8.d0

           sumx(1)=sum(pivec(0:nodrx+1,1:nodrx,1:2)*cx1(0:nodrx+1,1:nodrx,1:2))

           sumx(2)=sum(pivec(0:nodrx+1,1:nodrx,2:1:-1)*cx1(0:nodrx+1,1:nodrx,1:2))

           sumx=sumx*phasefunc

           cyt1(0:nodry+1,1:nodry,1) = conjg(pivec(0:nodry+1,1:nodry,1))*sumx(1) &

                 +conjg(pivec(0:nodry+1,1:nodry,2))*sumx(2)

           cyt1(0:nodry+1,1:nodry,2) = conjg(pivec(0:nodry+1,1:nodry,2))*sumx(1) &

                 +conjg(pivec(0:nodry+1,1:nodry,1))*sumx(2)

           do n=1,nodrx

              do p=1,2

                 im=(-1)**(n+p)

                 cxt(n+1,1:n,p)=im*cx2(n+1,1:n,p)

                 cxt(0:n,n,p)=im*cx2(0:n,n,p)

              enddo

           enddo

           sumx(1)=sum(conjg(pivec(0:nodrx+1,1:nodrx,1:2))*cxt(0:nodrx+1,1:nodrx,1:2))

           sumx(2)=sum(conjg(pivec(0:nodrx+1,1:nodrx,2:1:-1))*cxt(0:nodrx+1,1:nodrx,1:2))

           sumx=sumx*phasefunc

           cyt2(0:nodry+1,1:nodry,1) = pivec(0:nodry+1,1:nodry,1)*sumx(1) &

                 +pivec(0:nodry+1,1:nodry,2)*sumx(2)

           cyt2(0:nodry+1,1:nodry,2) = pivec(0:nodry+1,1:nodry,2)*sumx(1) &

                 +pivec(0:nodry+1,1:nodry,1)*sumx(2)

           do n=1,nodry

              do p=1,2

                 im=(-1)**(n+p)

                 cyt2(n+1,1:n,p)=im*cyt2(n+1,1:n,p)

                 cyt2(0:n,n,p)=im*cyt2(0:n,n,p)

              enddo

           enddo

           cy1=cy1+cyt1

           cy2=cy2+cyt2

           end subroutine farfieldtranslationtwovec

  !

  ! correction term for hybrid bcgm solution: difference between exact and

  ! ff translation field

  ! november 2011

  !

           subroutine fftranslationerror(cx,cy,jx,iy,nodrx,nodry)

           use numconstants

           use specialfuncs

           implicit none

           integer :: nodrx,nodry,idir,itran,iy,jx,istore

           integer :: nr1,nr2,nt1,nt2,ne1,ne2

           real(8) :: xj(3),xi(3),xij(3),rij,nfdist

           complex(8) :: cx(0:nodrx+1,nodrx,2),cy(0:nodry+1,nodry,2), &

                         cyt(0:nodry+1,nodry,2)

           xj(:)=sphere_position(:,jx)

           xi(:)=sphere_position(:,iy)

           xij=xi-xj

           rij=sqrt(dot_product(xij,xij))

           if(near_field_distance.lt.0.) then

              nfdist=(.5*(nodrx+nodry))**2.

           else

              nfdist=near_field_distance

           endif

           if(rij.gt.nfdist) then

              cyt=0.d0

              call farfieldtranslation(cx,cyt,xij,(1.d0,0.d0),nodrx,nodry,1)

              cyt=-cyt

              call rottran(cx,cyt,xij,(1.d0,0.d0),nodrx,nodry,3,1,1,1)

              cy=cy+cyt

           endif

           end subroutine fftranslationerror

  !

  !  translation via stored or calculated matrices (replaces rottranstoredmatrix)

  !

  !  12 October 2011.

  !     if rij> near_field_distance, the far field formula is

  !     applied.

  !

           subroutine rottranjtoi(cx,cy,jx,iy,nodrx,nodry,idir,itran)

           use numconstants

           use specialfuncs

           implicit none

           integer :: nodrx,nodry,idir,itran,iy,jx,istore

           integer :: nr1,nr2,nt1,nt2,ne1,ne2

           real(8) :: xj(3),xi(3),xij(3),rij,nfdist

           complex(8) :: cx(0:nodrx+1,nodrx,2),cy(0:nodry+1,nodry,2)

           xj(:)=sphere_position(:,jx)

           xi(:)=sphere_position(:,iy)

           xij=xi-xj

           rij=sqrt(dot_product(xij,xij))

           if(near_field_distance.lt.0.) then

              nfdist=(.5*(nodrx+nodry))**2.

           else

              nfdist=near_field_distance

           endif

           if(rij.gt.nfdist) then

              if(store_tran_mat.eq.2) then

                 nt1=nofftran(iy,jx)+1

                 nt2=nt1+nsizetran(iy,jx)-1

                 call farfieldtranslation(cx,cy,xij,(1.d0,0.d0),nodrx,nodry,itran, &

                      stored_pivec_matrix=tranmatstore(nt1:nt2))

              else

                 call farfieldtranslation(cx,cy,xij,(1.d0,0.d0),nodrx,nodry,itran)

              endif

           else

              if(store_tran_mat.eq.0) then

                 call rottran(cx,cy,xij,(1.d0,0.d0),nodrx,nodry,3,1,idir,itran)

              else

                 nr1=noffrot(iy,jx)+1

                 nr2=nr1+nsizerot(iy,jx)-1

                 nt1=nofftran(iy,jx)+1

                 nt2=nt1+nsizetran(iy,jx)-1

                 ne1=noffephi(iy,jx)+1

                 ne2=ne1+nsizeephi(iy,jx)-1

                 call rottranmtrx(cx,cy,idir,itran,nodrx,nodry,ephimatstore(ne1:ne2), &

                                  rotmatstore(nr1:nr2),tranmatstore(nt1:nt2))

              endif

           endif

           end subroutine rottranjtoi

  !

  ! normal and transpose translation, for bcgm

  ! november 2011

  !

           subroutine rottrantwojtoi(cx1,cx2,cy1,cy2,jx,iy,nodrx,nodry)

           use numconstants

           use specialfuncs

           implicit none

           integer :: nodrx,nodry,idir,itran,iy,jx,istore

           integer :: nr1,nr2,nt1,nt2,ne1,ne2

           real(8) :: xj(3),xi(3),xij(3),rij,nfdist

           complex(8) :: cx1(0:nodrx+1,nodrx,2),cy1(0:nodry+1,nodry,2), &

                         cx2(0:nodrx+1,nodrx,2),cy2(0:nodry+1,nodry,2)

           xj(:)=sphere_position(:,jx)

           xi(:)=sphere_position(:,iy)

           xij=xi-xj

           rij=sqrt(dot_product(xij,xij))

           if(near_field_distance.lt.0.) then

              nfdist=(.5*(nodrx+nodry))**2.

           else

              nfdist=near_field_distance

           endif

           if(rij.gt.nfdist) then

              if(store_tran_mat.eq.2) then

                 nt1=nofftran(iy,jx)+1

                 nt2=nt1+nsizetran(iy,jx)-1

                 call farfieldtranslationtwovec(cx1,cx2,cy1,cy2,xij,(1.d0,0.d0),nodrx,nodry, &

                 stored_pivec_matrix=tranmatstore(nt1:nt2))

              else

                 call farfieldtranslationtwovec(cx1,cx2,cy1,cy2,xij,(1.d0,0.d0),nodrx,nodry)

              endif

           else

              if(store_tran_mat.eq.0) then

                 call rottran(cx1,cy1,xij,(1.d0,0.d0),nodrx,nodry,3,1,1,1)

                 call rottran(cx2,cy2,xij,(1.d0,0.d0),nodrx,nodry,3,0,-1,-1)

              else

                 nr1=noffrot(iy,jx)+1

                 nr2=nr1+nsizerot(iy,jx)-1

                 nt1=nofftran(iy,jx)+1

                 nt2=nt1+nsizetran(iy,jx)-1

                 ne1=noffephi(iy,jx)+1

                 ne2=ne1+nsizeephi(iy,jx)-1

                 call rottranmtrxtwovec(cx1,cx2,cy1,cy2,nodrx,nodry,ephimatstore(ne1:ne2), &

                                  rotmatstore(nr1:nr2),tranmatstore(nt1:nt2))

              endif

           endif

           end subroutine rottrantwojtoi

  !

  !  the vectorized rotation-translation-rotation operation

  !

  !

  !  last revised: 15 January 2011

  !

           subroutine rottranmtrx(cx,cy,idir,itran,nodrx,nodry,ephimat,rotmat,tranmat)

           use numconstants

           use specialfuncs

           implicit none

           integer :: nodrx,nodry,itype,icalc,idir,itran,nmax,nmin

           integer :: m,n,k,l,nn1,nn2,ll1,mn,kl,m1,p,n1,addr(2)

           real(8), target :: rotmat(-min(nodrx,nodry):min(nodrx,nodry), &

                              0:max(nodrx,nodry)*(max(nodrx,nodry)+2))

           real(8), pointer :: rmat(:,:)

           complex(8) :: cx(0:nodrx+1,nodrx,2),cy(0:nodry+1,nodry,2), &

                         ephimat(-max(nodrx,nodry):max(nodrx,nodry))

           complex(8), target :: tranmat(nodry,nodrx*(nodrx+3)/2,2)

           complex(8), pointer :: tmat1(:,:),tmat2(:,:)

           c_temp=(0.d0,0.d0)

           nmin=min(nodrx,nodry)

  !

  !  rotation to origin of target

  !

           do n=1,nodrx

              nn1=n*(n+1)-n

              nn2=nn1+(2*n+1)-1

              n1=min(n,nodry)

              rmat=>rotmat(-n1:n1,nn1:nn2)

              do p=1,2

                 rvec_temp(-n:-1,p)=cx(n+1,n:1:-1,p)

                 rvec_temp(0:n,p)=cx(0:n,n,p)

                 if(itran.eq.1) then

                    rvec_temp(-n:n,p)=rvec_temp(-n:n,p)*ephimat(-n:n)

                 else

                    rvec_temp(-n:n,p)=rvec_temp(-n:n,p)*conjg(ephimat(-n:n))

                 endif

              enddo

              c_temp(-n1:n1,n,1:2)=matmul(rmat,rvec_temp(-n:n,1:2))

           enddo

  !

  !  axial translation to target

  !

           do m=0,nmin

              m1=max(1,m)

              nn1=atcadd(m,m1,nodrx)

              nn2=atcadd(m,nodrx,nodrx)

              tmat1=>tranmat(m1:nodry,nn1:nn2,1)

              tmat2=>tranmat(m1:nodry,nn1:nn2,2)

              tvec_temp(m1:nodrx,1)=idir*c_temp(m,m1:nodrx,1)

              tvec_temp(m1:nodrx,2)=c_temp(m,m1:nodrx,2)

              if(itran*idir.eq.-1) then

                 tvec_temp(m1:nodrx,1)=tvec_temp(m1:nodrx,1)*monen(m1:nodrx)

                 tvec_temp(m1:nodrx,2)=tvec_temp(m1:nodrx,2)*monen(m1:nodrx)

              endif

              ct_temp=(0.d0,0.d0)

              ct_temp(m1:nodry,1,1:2)=matmul(tmat1,tvec_temp(m1:nodrx,1:2))

              ct_temp(m1:nodry,2,1:2)=matmul(tmat2,tvec_temp(m1:nodrx,1:2))

              c_temp(m,m1:nodry,1)=idir*(ct_temp(m1:nodry,1,1)+ct_temp(m1:nodry,2,2))

              c_temp(m,m1:nodry,2)=ct_temp(m1:nodry,2,1)+ct_temp(m1:nodry,1,2)

              if(itran*idir.eq.-1) then

                 c_temp(m,m1:nodry,1)=c_temp(m,m1:nodry,1)*monen(m1:nodry)

                 c_temp(m,m1:nodry,2)=c_temp(m,m1:nodry,2)*monen(m1:nodry)

              endif

              if(m.gt.0) then

                 tvec_temp(m1:nodrx,1)=idir*c_temp(-m,m1:nodrx,1)

                 tvec_temp(m1:nodrx,2)=c_temp(-m,m1:nodrx,2)

                 if(itran*idir.eq.-1) then

                    tvec_temp(m1:nodrx,1)=tvec_temp(m1:nodrx,1)*monen(m1:nodrx)

                    tvec_temp(m1:nodrx,2)=tvec_temp(m1:nodrx,2)*monen(m1:nodrx)

                 endif

                 ct_temp=(0.d0,0.d0)

                 ct_temp(m1:nodry,1,1:2)=matmul(tmat1,tvec_temp(m1:nodrx,1:2))

                 ct_temp(m1:nodry,2,1:2)=matmul(tmat2,tvec_temp(m1:nodrx,1:2))

                 c_temp(-m,m1:nodry,1)=idir*(ct_temp(m1:nodry,1,1)-ct_temp(m1:nodry,2,2))

                 c_temp(-m,m1:nodry,2)=-ct_temp(m1:nodry,2,1)+ct_temp(m1:nodry,1,2)

                 if(itran*idir.eq.-1) then

                    c_temp(-m,m1:nodry,1)=c_temp(-m,m1:nodry,1)*monen(m1:nodry)

                    c_temp(-m,m1:nodry,2)=c_temp(-m,m1:nodry,2)*monen(m1:nodry)

                 endif

              endif

           enddo

  !

  !  rotation back to original frame

  !

           do n=1,nodry

              rvec_temp=(0.d0,0.d0)

              m1=min(n,nmin)

              nn1=n*(n+1)-n

              nn2=n*(n+1)+n

              rmat=>rotmat(-m1:m1,nn1:nn2)

              do p=1,2

                 if(itran.eq.1) then

                    rvec_temp(-n:n,p)=matmul(c_temp(-m1:m1,n,p),rmat)*conjg(ephimat(-n:n))

                 else

                    rvec_temp(-n:n,p)=matmul(c_temp(-m1:m1,n,p),rmat)*ephimat(-n:n)

                 endif

                 cy(n+1,n:1:-1,p)=cy(n+1,n:1:-1,p)+rvec_temp(-n:-1,p)

                 cy(0:n,n,p)=cy(0:n,n,p)+rvec_temp(0:n,p)

              enddo

           enddo

           end subroutine rottranmtrx

  !

  ! two vector rotation: normal and transpose

  ! november 2011

  !

           subroutine rottranmtrxtwovec(cx1,cx2,cy1,cy2,nodrx,nodry, &

            ephimat,rotmat,tranmat)

           use numconstants

           use specialfuncs

           implicit none

           integer :: nodrx,nodry,itype,icalc,idir,itran,nmax,nmin

           integer :: m,n,k,l,nn1,nn2,ll1,mn,kl,m1,p,n1,addr(2)

           real(8), target :: rotmat(-min(nodrx,nodry):min(nodrx,nodry), &

                              0:max(nodrx,nodry)*(max(nodrx,nodry)+2))

           real(8), pointer :: rmat(:,:)

           complex(8) :: cx1(0:nodrx+1,nodrx,2),cy1(0:nodry+1,nodry,2), &

                         cx2(0:nodrx+1,nodrx,2),cy2(0:nodry+1,nodry,2), &

                         ephimat(-max(nodrx,nodry):max(nodrx,nodry))

           complex(8), target :: tranmat(nodry,nodrx*(nodrx+3)/2,2)

           complex(8), pointer :: tmat1(:,:),tmat2(:,:)

           c_temp=(0.d0,0.d0)

           nmin=min(nodrx,nodry)

  !

  !  rotation to origin of target

  !

           do n=1,nodrx

              nn1=n*(n+1)-n

              nn2=nn1+(2*n+1)-1

              n1=min(n,nodry)

              rmat=>rotmat(-n1:n1,nn1:nn2)

              do p=1,2

                 rvec_temp(-n:-1,p)=cx1(n+1,n:1:-1,p)

                 rvec_temp(0:n,p)=cx1(0:n,n,p)

                 rvec2_temp(-n:-1,p)=cx2(n+1,n:1:-1,p)

                 rvec2_temp(0:n,p)=cx2(0:n,n,p)

                 rvec_temp(-n:n,p)=rvec_temp(-n:n,p)*ephimat(-n:n)

                 rvec2_temp(-n:n,p)=rvec2_temp(-n:n,p)*conjg(ephimat(-n:n))

              enddo

              c_temp(-n1:n1,n,1:2)=matmul(rmat,rvec_temp(-n:n,1:2))

              c2_temp(-n1:n1,n,1:2)=matmul(rmat,rvec2_temp(-n:n,1:2))

           enddo

  !

  !  axial translation to target

  !

           do m=0,nmin

              m1=max(1,m)

              nn1=atcadd(m,m1,nodrx)

              nn2=atcadd(m,nodrx,nodrx)

              tmat1=>tranmat(m1:nodry,nn1:nn2,1)

              tmat2=>tranmat(m1:nodry,nn1:nn2,2)

              tvec_temp(m1:nodrx,1)=c_temp(m,m1:nodrx,1)

              tvec_temp(m1:nodrx,2)=c_temp(m,m1:nodrx,2)

              tvec2_temp(m1:nodrx,1)=-c2_temp(m,m1:nodrx,1)

              tvec2_temp(m1:nodrx,2)=c2_temp(m,m1:nodrx,2)

              ct_temp=(0.d0,0.d0)

              ct2_temp=(0.d0,0.d0)

              ct_temp(m1:nodry,1,1:2)=matmul(tmat1,tvec_temp(m1:nodrx,1:2))

              ct_temp(m1:nodry,2,1:2)=matmul(tmat2,tvec_temp(m1:nodrx,1:2))

              ct2_temp(m1:nodry,1,1:2)=matmul(tmat1,tvec2_temp(m1:nodrx,1:2))

              ct2_temp(m1:nodry,2,1:2)=matmul(tmat2,tvec2_temp(m1:nodrx,1:2))

              c_temp(m,m1:nodry,1)=(ct_temp(m1:nodry,1,1)+ct_temp(m1:nodry,2,2))

              c_temp(m,m1:nodry,2)=ct_temp(m1:nodry,2,1)+ct_temp(m1:nodry,1,2)

              c2_temp(m,m1:nodry,1)=-(ct2_temp(m1:nodry,1,1)+ct2_temp(m1:nodry,2,2))

              c2_temp(m,m1:nodry,2)=ct2_temp(m1:nodry,2,1)+ct2_temp(m1:nodry,1,2)

              if(m.gt.0) then

                 tvec_temp(m1:nodrx,1)=c_temp(-m,m1:nodrx,1)

                 tvec_temp(m1:nodrx,2)=c_temp(-m,m1:nodrx,2)

                 tvec2_temp(m1:nodrx,1)=-c2_temp(-m,m1:nodrx,1)

                 tvec2_temp(m1:nodrx,2)=c2_temp(-m,m1:nodrx,2)

                 ct_temp=(0.d0,0.d0)

                 ct2_temp=(0.d0,0.d0)

                 ct_temp(m1:nodry,1,1:2)=matmul(tmat1,tvec_temp(m1:nodrx,1:2))

                 ct_temp(m1:nodry,2,1:2)=matmul(tmat2,tvec_temp(m1:nodrx,1:2))

                 ct2_temp(m1:nodry,1,1:2)=matmul(tmat1,tvec2_temp(m1:nodrx,1:2))

                 ct2_temp(m1:nodry,2,1:2)=matmul(tmat2,tvec2_temp(m1:nodrx,1:2))

                 c_temp(-m,m1:nodry,1)=(ct_temp(m1:nodry,1,1)-ct_temp(m1:nodry,2,2))

                 c_temp(-m,m1:nodry,2)=-ct_temp(m1:nodry,2,1)+ct_temp(m1:nodry,1,2)

                 c2_temp(-m,m1:nodry,1)=-(ct2_temp(m1:nodry,1,1)-ct2_temp(m1:nodry,2,2))

                 c2_temp(-m,m1:nodry,2)=-ct2_temp(m1:nodry,2,1)+ct2_temp(m1:nodry,1,2)

              endif

           enddo

  !

  !  rotation back to original frame

  !

           do n=1,nodry

              rvec_temp=(0.d0,0.d0)

              rvec2_temp=(0.d0,0.d0)

              m1=min(n,nmin)

              nn1=n*(n+1)-n

              nn2=n*(n+1)+n

              rmat=>rotmat(-m1:m1,nn1:nn2)

              do p=1,2

                 rvec_temp(-n:n,p)=matmul(c_temp(-m1:m1,n,p),rmat)*conjg(ephimat(-n:n))

                 rvec2_temp(-n:n,p)=matmul(c2_temp(-m1:m1,n,p),rmat)*ephimat(-n:n)

                 cy1(n+1,n:1:-1,p)=cy1(n+1,n:1:-1,p)+rvec_temp(-n:-1,p)

                 cy1(0:n,n,p)=cy1(0:n,n,p)+rvec_temp(0:n,p)

                 cy2(n+1,n:1:-1,p)=cy2(n+1,n:1:-1,p)+rvec2_temp(-n:-1,p)

                 cy2(0:n,n,p)=cy2(0:n,n,p)+rvec2_temp(0:n,p)

              enddo

           enddo

           end subroutine rottranmtrxtwovec

  !

  !  GB coefficients for sphere-centered expansions, obtained via translation

  !

  !  last revised: 15 January 2011

  !

           subroutine spheregaussianbeamcoef(nsphere,neqns,nodr,alpha,beta,cbeam, &

                      rpos,rbeam,epstran,pmnp)

           use specialfuncs

           implicit none

           integer :: m,n,p,nsphere,i,l,nodr(nsphere),nblk,noff,nodrgb,neqns,k

           real(8) :: alpha,beta,cb,sb,ca,sa,rpos(3,nsphere),rmax,rbeam(3),xib(3),rib, &

                      cbeam,epstran

           complex(8) :: pmnp(neqns,2)

           complex(8), allocatable :: pmnp0(:,:,:,:)

           nodrgb=0

           rmax=0.d0

           do i=1,nsphere

              xib(:)=rpos(:,i)-rbeam(:)

              rib=sqrt(dot_product(xib,xib))

              rmax=max(rmax,rib)

              call tranordertest(rib,(1.d0,0.d0),nodr(i),epstran,n)

              nodrgb=max(n,nodrgb)

           enddo

           allocate(pmnp0(0:nodrgb+1,nodrgb,2,2))

           call gaussianbeamcoef(alpha,beta,cbeam,nodrgb,pmnp0)

           pmnp=0.d0

           noff=0

           do i=1,nsphere

              nblk=2*nodr(i)*(nodr(i)+2)

              xib(:)=rpos(:,i)-rbeam(:)

              do k=1,2

                 call rottran(pmnp0(0:nodrgb+1,1:nodrgb,1:2,k),pmnp(noff+1:noff+nblk,k),xib, &

                      (1.d0,0.d0),nodrgb,nodr(i),1,1,1,1)

              enddo

              noff=noff+nblk

           enddo

           deallocate(pmnp0)

           end subroutine spheregaussianbeamcoef

  

        end module translation

  !

  ! scatprops module: various subroutines for calculation of observables from the solution

  !

  !

  !  last revised: 15 January 2011

  !

        module scatprops

        implicit none

        contains

  !

  !  determination of maximum orders for target--based expansions

  !

  !

  !  last revised: 15 January 2011

  !

           subroutine tranorders(nsphere,nodr,rpos,eps,ntran,nodrt)

           use numconstants

           use specialfuncs

           use translation

           implicit none

           integer :: nsphere,nodr(nsphere),nodrt,ntran(nsphere),i

           real(8) :: rpos(3,nsphere),r,eps

           nodrt=0

           do i=1,nsphere

              r=sqrt(dot_product(rpos(:,i),rpos(:,i)))

              call tranordertest(r,(1.d0,0.d0),nodr(i),eps,ntran(i))

              if(print_intermediate_results.eq.1) &

                 write(*,'('' i, nodr, ntran:'',3i7)') i,nodr(i),ntran(i)

              nodrt=max(nodrt,ntran(i))

           enddo

           end subroutine tranorders

  !

  !  translation of sphere-based expansions to common target origin

  !

  !

  !  last revised: 15 January 2011

  !

           subroutine amncommonorigin(neqns,nsphere,nodr,ntran,nodrt,rpos,amnp,amnp0)

           use specialfuncs

           use translation

           implicit none

           integer :: neqns,nsphere,nodr(nsphere),nodrt,i,m,n,p,nblk,ntran(nsphere),noff

           real(8) :: rpos(3,nsphere),r,eps,xij(3)

           complex(8) :: amnp(neqns),amnp0(0:nodrt+1,nodrt,2)

           complex(8), allocatable :: amnpt(:,:,:)

           amnp0=(0.d0,0.d0)

           noff=0

           do i=1,nsphere

              allocate(amnpt(0:ntran(i)+1,ntran(i),2))

              amnpt=(0.d0,0.d0)

              nblk=nodr(i)*(nodr(i)+2)*2

              xij=-rpos(:,i)

              call rottran(amnp(noff+1:noff+nblk),amnpt,xij,(1.d0,0.d0), &

                   nodr(i),ntran(i),1,1,1,1)

              do p=1,2

                 do n=1,ntran(i)

                    do m=0,ntran(i)+1

                       amnp0(m,n,p)=amnp0(m,n,p)+amnpt(m,n,p)

                    enddo

                 enddo

              enddo

              deallocate(amnpt)

              noff=noff+nblk

           enddo

           end subroutine amncommonorigin

  !

  !  sphereqeff computes the efficiency factors for the sphere, given an1: mie coefficients,

  !  anp: scattering coefficients, pnp: incident field coefficients.

  !

  ! This subroutine is specific to the OA model for the sphere.

  !

  !

  !  original: 15 January 2011

  !  revised: 21 February 2011: polarized and cross-polarized efficiency calculation

  !  30 March 2011: added optical activity

  !

           subroutine sphereqeff(nsphere,neqns,nodr,nodrmax,xsp,anp1,anp2,&

                      pnp1,pnp2,qext,qabs,qsca)

           use miecoefdata

           use spheredata

           implicit none

           integer :: nsphere,m,n,p,i,nodr(nsphere),nblk,noff,neqns,nodrmax

           real(8) :: xsp(nsphere),qext(nsphere),qabs(nsphere),qsca(nsphere), &

                      qe,qa,qs

           complex(8) :: anp1(neqns),pnp1(neqns),anp2(neqns),pnp2(neqns)

           complex(8) :: anmie(2,2,nodrmax)

           qext=0.d0

           qabs=0.d0

           qsca=0.d0

           noff=0

           do i=1,nsphere

              nblk=nodr(i)*(nodr(i)+2)*2

              call getmiedata(which_sphere=i,sphere_mie_coefficients=anmie)

              call qeffcalc(nodr(i),anp1(noff+1:noff+nblk),anp2(noff+1:noff+nblk), &

                      pnp1(noff+1:noff+nblk),pnp2(noff+1:noff+nblk),anmie,qe,qa,qs)

              noff=noff+nblk

              qext(i)=2.d0*qe/xsp(i)/xsp(i)

              qabs(i)=2.d0*qa/xsp(i)/xsp(i)

              qsca(i)=2.d0*qs/xsp(i)/xsp(i)

           enddo

           end subroutine sphereqeff

  !

  !  calculation of sphere efficiency factors for scattered and incident field

  !  coefficient anp1, pnp1, anp2, pnp2  and mie coefficients anmie

  !

  !  original: 15 January 2011

  !  revised: 21 February 2011: polarized and cross-polarized efficiency calculation

  !  30 March 2011: added optical activity

  !

           subroutine qeffcalc(nodr,anp1,anp2,pnp1,pnp2,anmie,qe,qa,qs)

           implicit none

           integer :: nodr,m,n,p,q

           real(8) :: qe,qa,qs,babs,aninv(2,2)

           complex(8) :: anp1(0:nodr+1,nodr,2),pnp1(0:nodr+1,nodr,2), &

                         anp2(0:nodr+1,nodr,2),pnp2(0:nodr+1,nodr,2),anmie(2,2,nodr), &

                         a

           qe=0.d0

           qa=0.d0

           qs=0.d0

           do n=1,nodr

              a=anmie(1,1,n)*anmie(2,2,n)-anmie(1,2,n)*anmie(1,2,n)

              do p=1,2

                 do q=1,2

                    aninv(p,q)=(-1)**(p+q)*anmie(3-p,3-q,n)/a

                 enddo

                 aninv(p,p)=aninv(p,p)+1.d0

              enddo

              do p=1,2

  !               babs=-(1.d0/anmie(p,n)+1.d0)

                 do m=-n,-1

                    qe=qe-(anp1(n+1,-m,p)*conjg(pnp2(n+1,-m,p)) &

                         + anp2(n+1,-m,p)*conjg(pnp1(n+1,-m,p)))*.5d0

                    qs=qs+anp1(n+1,-m,p)*conjg(anp2(n+1,-m,p))

                    do q=1,2

                       qa=qa-conjg(anp1(n+1,-m,p))*aninv(p,q)*anp2(n+1,-m,q)

                    enddo

                 enddo

                 do m=0,n

                    qe=qe-(anp1(m,n,p)*conjg(pnp2(m,n,p)) &

                         +anp2(m,n,p)*conjg(pnp1(m,n,p)))*.5d0

                    qs=qs+anp1(m,n,p)*conjg(anp2(m,n,p))

                    do q=1,2

                       qa=qa-conjg(anp1(m,n,p))*aninv(p,q)*anp2(m,n,q)

                    enddo

                 enddo

              enddo

           enddo

           end subroutine qeffcalc

  !

  !  scattering amplitude sa and matrix sm calculation

  !

  !  original: 15 January 2011

  !  revised: 21 February 2011: S11 normalization changed

  !

           subroutine scatteringmatrix(amn0,nodrt,xv,ct,phi,sa,sm)

           use specialfuncs

           use numconstants

           implicit none

           integer :: nodrt,m,n,p,m1,n1,i,j

           real(8) :: xv,ct,phi,sm(4,4),tau(0:nodrt+1,nodrt,2),cphi,sphi,qsca

           complex(8) :: amn0(0:nodrt+1,nodrt,2,2),sa(4),ephi,ephim(-nodrt:nodrt), &

                         ci,cin,a,b,sp(4,4)

           data ci/(0.d0,1.d0)/

  

  

           call taufunc(ct,nodrt,tau)

           cphi=cos(phi)

           sphi=sin(phi)

           ephi=dcmplx(cphi,sphi)

           call ephicoef(ephi,nodrt,ephim)

           sa=(0.d0,0.d0)

           qsca=0.d0

           do n=1,nodrt

              cin=(-ci)**n

              do m=-n,n

                 if(m.le.-1) then

                    m1=n+1

                    n1=-m

                 else

                    m1=m

                    n1=n

                 endif

                 do p=1,2

                    qsca=qsca+amn0(m1,n1,p,1)*dconjg(amn0(m1,n1,p,1)) &

                             + amn0(m1,n1,p,2)*dconjg(amn0(m1,n1,p,2))

                    a=amn0(m1,n1,p,1)*cphi+amn0(m1,n1,p,2)*sphi

                    b=amn0(m1,n1,p,1)*sphi-amn0(m1,n1,p,2)*cphi

                    sa(1)=sa(1)+cin*tau(m1,n1,3-p)*b*ephim(m)

                    sa(2)=sa(2)+ci*cin*tau(m1,n1,p)*a*ephim(m)

                    sa(3)=sa(3)+ci*cin*tau(m1,n1,p)*b*ephim(m)

                    sa(4)=sa(4)+cin*tau(m1,n1,3-p)*a*ephim(m)

                 enddo

              enddo

           enddo

           qsca=qsca*2.d0

           do i=1,4

              do j=1,4

                 sp(i,j)=sa(i)*dconjg(sa(j))*16.d0/qsca

              enddo

           enddo

           sm(1,1)=sp(1,1)+sp(2,2)+sp(3,3)+sp(4,4)

           sm(1,2)=-sp(1,1)+sp(2,2)-sp(3,3)+sp(4,4)

           sm(2,1)=-sp(1,1)+sp(2,2)+sp(3,3)-sp(4,4)

           sm(2,2)=sp(1,1)+sp(2,2)-sp(3,3)-sp(4,4)

           sm(3,3)=2.*(sp(1,2)+sp(3,4))

           sm(3,4)=-2.*dimag(sp(1,2)+sp(3,4))

           sm(4,3)=2.*dimag(sp(1,2)-sp(3,4))

           sm(4,4)=2.*(sp(1,2)-sp(3,4))

           sm(1,3)=2.*(sp(2,3)+sp(1,4))

           sm(3,1)=2.*(sp(2,4)+sp(1,3))

           sm(1,4)=2.*dimag(sp(2,3)-sp(1,4))

           sm(4,1)=-2.*dimag(sp(2,4)+sp(1,3))

           sm(2,3)=2.*(sp(2,3)-sp(1,4))

           sm(3,2)=2.*(sp(2,4)-sp(1,3))

           sm(2,4)=2.*dimag(sp(2,3)+sp(1,4))

           sm(4,2)=-2.*dimag(sp(2,4)-sp(1,3))

  !         do i=1,4

  !            do j=1,4

  !               if(i.ne.1.or.j.ne.1) then

  !                  sm(i,j)=sm(i,j)/sm(1,1)

  !               endif

  !            enddo

  !         enddo

           end subroutine scatteringmatrix

  !   c                                                                               c

  !   c  subroutine scatexp(amn0,nodrt,nodrg,gmn) computes the expansion coefficients c

  !   c  for the spherical harmonic expansion of the scattering phase function from   c

  !   c  the scattering coefficients amn0.  For a complete expansion, the max. order  c

  !   c  of the phase function expansion (nodrg) will be 2*nodrt, where nodrt is      c

  !   c  the max. order of the scattered field expansion.   In this code nodrg is     c

  !   c  typically set to 1, so that the subroutine returns the first moments         c

  !   c  of the phase function; gmn(1) and gmn(2).                                    c

  !   c                                                                               c

  !   c  The expansion coefficients are normalized so that gmn(0)=1                   c

  !   c                                                                               c

  !   c  gmn(1)/3 is the asymmetry parameter.                                         c

  !   c                                                                               c

           subroutine s11expansion(amn0,nodrt,mmax,nodrg,gmn)

           use specialfuncs

           use numconstants

           implicit none

           integer :: nodrt,m,n,p,ma,na,mmax,nodrg,w,w1,w2,u,uw,ww1, &

                      l1,l2,ka,la,k,l,q,ik

           real(8) :: vc1(0:nodrt*2+1),vc2(0:nodrt*2+1),g0

           complex(8) :: amn0(0:nodrt+1,nodrt,2,2),gmn(0:nodrg*(nodrg+3)/2), &

                         a(2,2),c,c2

           gmn=(0.d0,0.d0)

           do n=1,nodrt

              l1=max(1,n-nodrg)

              l2=min(nodrt,n+nodrg)

              do l=l1,l2

                 c=sqrt(dble((n+n+1)*(l+l+1)))*dcmplx(0.d0,1.d0)**(l-n)

                 w2=min(n+l,nodrg)

                 call vcfunc(-1,l,1,n,vc2)

                 do m=-n,n

                    if(m.le.-1) then

                       ma=n+1

                       na=-m

                    else

                       ma=m

                       na=n

                    endif

                    do k=-l,min(l,m)

                       if(k.le.-1) then

                          ka=l+1

                          la=-k

                       else

                          ka=k

                          la=l

                       endif

                       u=m-k

                       if(u.le.mmax) then

                          ik=(-1)**k

                          c2=ik*c

                          do p=1,2

                             do q=1,2

                                a(p,q)=c2*(amn0(ma,na,p,1)*conjg(amn0(ka,la,q,1)) &

                                      +amn0(ma,na,p,2)*conjg(amn0(ka,la,q,2)))

                             enddo

                          enddo

                          w1=max(abs(n-l),abs(u))

                          w2=min(n+l,nodrg)

                          call vcfunc(-k,l,m,n,vc1)

                          do w=w1,w2

                             uw=(w*(w+1))/2+u

                             do p=1,2

                                if(mod(n+l+w,2).eq.0) then

                                   q=p

                                else

                                   q=3-p

                                endif

                                gmn(uw)=gmn(uw)-vc1(w)*vc2(w)*a(p,q)

                             enddo

                          enddo

                       endif

                    enddo

                 enddo

              enddo

           enddo

           g0=dble(gmn(0))

           gmn(0)=1.d0

           do w=1,nodrg

              ww1=(w*(w+1))/2

              gmn(ww1)=dcmplx(dble(gmn(ww1)),0.d0)/g0

              do u=1,min(mmax,w)

                 uw=ww1+u

                 gmn(uw)=(-1)**u*2.d0*gmn(uw)/g0

              enddo

           enddo

           end subroutine s11expansion

  !

  !  calculate azimuth--averaged scattering matrix from expansion, for cos(theta) = ct

  !

  !

  !  original: 15 January 2011

  !  revised: 21 February 2011: changed normalization on S11

  !

           subroutine fosmcalc(ntot,s00,s02,sp22,sm22,ct,sm)

           use numconstants

           use specialfuncs

           integer :: ntot,w,i,j,ww1

           real(8) :: s00(4,4,0:ntot*2),s02(4,4,0:ntot*2),sp22(4,4,0:ntot*2),sm22(4,4,0:ntot*2), &

                      sm(4,4),dc(-2:2,0:2*ntot*(2*ntot+2)),ct

           call rotcoef(ct,2,2*ntot,dc)

           sm=0.d0

           do w=0,2*ntot

              ww1=w*(w+1)

              sm(:,:)=sm(:,:)+s00(:,:,w)*dc(0,ww1)+s02(:,:,w)*dc(0,ww1+2) &

                     +sp22(:,:,w)*dc(2,ww1+2)+sm22(:,:,w)*dc(-2,ww1+2)

           enddo

           sm=sm/s00(1,1,0)

  !         do i=1,4

  !            do j=1,4

  !               if(i.ne.1.or.j.ne.1) then

  !                  sm(i,j)=sm(i,j)/sm(1,1)

  !               endif

  !            enddo

  !         enddo

           end subroutine fosmcalc

  !

  !  determine the generalized spherical function expansion for the azimuth-averaged scattering matrix

  !  corresponding to the target-based scattering field expansion of amnp.

  !

  !

  !  original: 15 January 2011

  !  revised: 21 February 2011: fixed flush call.

  !

           subroutine fosmexpansion(ntot,amnp,s00,s02,sp22,sm22)

           use mpidefs

           use mpidata

           use specialfuncs

           use numconstants

           use spheredata

           integer :: ntot,n,p,m,l,wmin,wmax,m1m,q,m1mq,m1mnpl,w,m1w,fe,fo,i,j,wtot

           integer :: rank,numprocs,nl,nsend,runprintunit

           integer, allocatable :: nlindex(:),nlnum(:)

           real(8) :: s00(4,4,0:ntot*2),s02(4,4,0:ntot*2),sp22(4,4,0:ntot*2),sm22(4,4,0:ntot*2), &

                      cm1p1(0:ntot*2),cm1m1(0:ntot*2),cmmpm(0:ntot*2),cmmm2pm(0:ntot*2), &

                      cmmp2pm(0:ntot*2),sum,nlperproc

           complex(8) :: amnp(0:ntot+1,ntot,2,2),a1(-ntot-2:ntot+2,ntot,2),a2(-ntot-2:ntot+2,ntot,2), &

                         ci,fnl,a1122,a2112,a1p2,a1m2

           data ci/(0.d0,1.d0)/

           call init(2*ntot)

           call getrunparameters(run_print_unit=runprintunit)

           call ms_mpi(mpi_command='rank',mpi_rank=rank)

           call ms_mpi(mpi_command='size',mpi_size=numprocs)

           allocate(nlindex(0:numprocs-1),nlnum(0:numprocs-1))

           nlperproc=dble(ntot*ntot)/dble(numprocs)

           sum=0.

           do i=0,numprocs-1

              nlindex(i)=floor(sum)

              sum=sum+nlperproc

           enddo

           do i=0,numprocs-2

              nlnum(i)=nlindex(i+1)-nlindex(i)

           enddo

           nlnum(numprocs-1)=ntot*ntot-nlindex(numprocs-1)

           if(rank.eq.0) then

              write(runprintunit,'('' SM calc, orders per processor:'',f10.4)') nlperproc

              call flush(runprintunit)

           endif

           a1=(0.d0,0.d0)

           a2=(0.d0,0.d0)

           s00=0.d0

           s02=0.d0

           sp22=0.d0

           sm22=0.d0

           wtot=ntot+ntot

           do n=1,ntot

              do p=1,2

                 do m=-n,-1

                    a1(m,n,p)=amnp(n+1,-m,p,1)

                    a2(m,n,p)=amnp(n+1,-m,p,2)

                 enddo

                 do m=0,n

                    a1(m,n,p)=amnp(m,n,p,1)

                    a2(m,n,p)=amnp(m,n,p,2)

                 enddo

              enddo

           enddo

           do nl=nlindex(rank)+1,nlindex(rank)+nlnum(rank)

              n=floor((nl-1)/dble(ntot))+1

              l=mod(nl-1,ntot)+1

              wmin=abs(n-l)

              wmax=n+l

              fnl=sqrt(dble((n+n+1)*(l+l+1)))*ci**(l-n)

              call vcfunc(-1,n,1,l,cm1p1)

              call vcfunc(-1,n,-1,l,cm1m1)

              do m=-min(n,l+2),min(n,l+2)

                 m1m=(-1)**m

                 if(abs(m).le.l) then

                    call vcfunc(-m,n,m,l,cmmpm)

                 else

                    cmmpm=0.d0

                 endif

                 if(abs(-2+m).le.l) then

                    call vcfunc(-m,n,-2+m,l,cmmm2pm)

                 else

                    cmmm2pm=0.d0

                 endif

                 if(abs(2+m).le.l) then

                    call vcfunc(-m,n,2+m,l,cmmp2pm)

                 else

                    cmmp2pm=0.d0

                 endif

                 do p=1,2

                    do q=1,2

                       m1mq=(-1)**(m+q)

                       m1mnpl=(-1)**(m+n+p+l)

                       a1122=(a1(m,n,p)*conjg(a1(m,l,q)) + a2(m,n,p)*conjg(a2(m,l,q)))

                       a2112=(a2(m,n,p)*conjg(a1(m,l,q)) - a1(m,n,p)*conjg(a2(m,l,q)))

                       a1p2=(a1(m,n,p)+ci*a2(m,n,p))*conjg(a1(m-2,l,q)-ci*a2(m-2,l,q))

                       a1m2=(a1(m,n,p)-ci*a2(m,n,p))*conjg(a1(m+2,l,q)+ci*a2(m+2,l,q))

                       do w=wmin,wmax

                          m1w=(-1)**w

                          if(mod(n+l+w+p+q,2).eq.0) then

                             fe=1

                             fo=0

                          else

                             fe=0

                             fo=1

                          endif

                          s00(1,1,w) = s00(1,1,w)-(m1m*fe*fnl*a1122*cm1p1(w)*cmmpm(w))/2.

                          s00(3,2,w) = s00(3,2,w)+ (ci/2.*m1m*fnl*fo*a1122*cm1p1(w)*cmmpm(w))

                          s00(4,2,w) = s00(4,2,w)+ dimag(-ci/2.*m1m*fnl*fo*a1122*cm1p1(w)*cmmpm(w))

                          s00(1,4,w) = s00(1,4,w)+ dimag(m1m*fe*fnl*(-a2112)*cm1p1(w)*cmmpm(w))/2.

                          s00(2,3,w) = s00(2,3,w)+ (m1m*fe*fnl*(-a2112)*cm1p1(w)*cmmpm(w))/2.

                          s00(4,3,w) = s00(4,3,w)+ dimag(ci/2.*m1m*fnl*fo*a2112*cm1p1(w)*cmmpm(w))

                          s00(4,4,w) = s00(4,4,w)+ (ci/2.*m1m*fnl*fo*a2112*cm1p1(w)*cmmpm(w))

  

                          if(w.lt.2) cycle

  

                          s02(2,1,w) = s02(2,1,w)-(m1mq*a1122*fe*fnl*cm1m1(w)*cmmpm(w))/2.

                          s02(3,1,w) = s02(3,1,w)+ (-ci/2.*m1mq*a1122*fnl*fo*cm1m1(w)*cmmpm(w))

                          s02(4,1,w) = s02(4,1,w)+ dimag(ci/2.*m1mq*a1122*fnl*fo*cm1m1(w)*cmmpm(w))

                          s02(1,3,w) = s02(1,3,w)-(m1mq*a2112*fe*fnl*cm1m1(w)*cmmpm(w))/2.

                          s02(2,4,w) = s02(2,4,w)-dimag(m1mq*a2112*fe*fnl*cm1m1(w)*cmmpm(w))/2.

                          s02(3,3,w) = s02(3,3,w)+ (ci/2.*m1mq*a2112*fnl*fo*cm1m1(w)*cmmpm(w))

                          s02(3,4,w) = s02(3,4,w)+ dimag(-ci/2.*m1mq*a2112*fnl*fo*cm1m1(w)*cmmpm(w))

  

                          s02(1,2,w) = s02(1,2,w)-(m1m*a1p2*fe*fnl*cm1p1(w)*cmmm2pm(w))/4.

                          s02(1,3,w) = s02(1,3,w)+ (-ci/4.*m1m*a1p2*fe*fnl*cm1p1(w)*cmmm2pm(w))

                          s02(2,4,w) = s02(2,4,w)+ dimag(-ci/4.*m1m*a1p2*fe*fnl*cm1p1(w)*cmmm2pm(w))

                          s02(3,1,w) = s02(3,1,w)+ (ci/4.*m1m*a1p2*fnl*fo*cm1p1(w)*cmmm2pm(w))

                          s02(4,1,w) = s02(4,1,w)+ dimag(-ci/4.*m1m*a1p2*fnl*fo*cm1p1(w)*cmmm2pm(w))

                          s02(4,3,w) = s02(4,3,w)+ dimag(m1m*a1p2*fnl*fo*cm1p1(w)*cmmm2pm(w))/4.

                          s02(4,4,w) = s02(4,4,w)+ (m1m*a1p2*fnl*fo*cm1p1(w)*cmmm2pm(w))/4.

  

                          sm22(1,4,w) = sm22(1,4,w)+ dimag(-ci/8.*m1mnpl*m1w*a1p2*fnl*cm1m1(w)*cmmm2pm(w))

                          sm22(2,2,w) = sm22(2,2,w)-(m1mnpl*m1w*a1p2*fnl*cm1m1(w)*cmmm2pm(w))/8.

                          sm22(2,3,w) = sm22(2,3,w)+ (-ci/8.*m1mnpl*m1w*a1p2*fnl*cm1m1(w)*cmmm2pm(w))

                          sm22(3,2,w) = sm22(3,2,w)+ (ci/8.*m1mnpl*m1w*a1p2*fnl*cm1m1(w)*cmmm2pm(w))

                          sm22(3,3,w) = sm22(3,3,w)-(m1mnpl*m1w*a1p2*fnl*cm1m1(w)*cmmm2pm(w))/8.

                          sm22(3,4,w) = sm22(3,4,w)+ dimag(m1mnpl*m1w*a1p2*fnl*cm1m1(w)*cmmm2pm(w))/8.

                          sm22(4,2,w) = sm22(4,2,w)+ dimag(-ci/8.*m1mnpl*m1w*a1p2*fnl*cm1m1(w)*cmmm2pm(w))

  

                          sp22(1,4,w) = sp22(1,4,w)+ dimag(-ci/8.*m1mq*a1p2*fnl*cm1m1(w)*cmmm2pm(w))

                          sp22(2,2,w) = sp22(2,2,w)-(m1mq*a1p2*fnl*cm1m1(w)*cmmm2pm(w))/8.

                          sp22(2,3,w) = sp22(2,3,w)+ (-ci/8.*m1mq*a1p2*fnl*cm1m1(w)*cmmm2pm(w))

                          sp22(3,2,w) = sp22(3,2,w)+ (-ci/8.*m1mq*a1p2*fnl*cm1m1(w)*cmmm2pm(w))

                          sp22(3,3,w) = sp22(3,3,w)+ (m1mq*a1p2*fnl*cm1m1(w)*cmmm2pm(w))/8.

                          sp22(3,4,w) = sp22(3,4,w)-dimag(m1mq*a1p2*fnl*cm1m1(w)*cmmm2pm(w))/8.

                          sp22(4,2,w) = sp22(4,2,w)+ dimag(ci/8.*m1mq*a1p2*fnl*cm1m1(w)*cmmm2pm(w))

  

                          s02(1,2,w) = s02(1,2,w)-(m1m*a1m2*fe*fnl*cm1p1(w)*cmmp2pm(w))/4.

                          s02(1,3,w) = s02(1,3,w)+ (ci/4.*m1m*a1m2*fe*fnl*cm1p1(w)*cmmp2pm(w))

                          s02(2,4,w) = s02(2,4,w)+ dimag(ci/4.*m1m*a1m2*fe*fnl*cm1p1(w)*cmmp2pm(w))

                          s02(3,1,w) = s02(3,1,w)+ (ci/4.*m1m*a1m2*fnl*fo*cm1p1(w)*cmmp2pm(w))

                          s02(4,1,w) = s02(4,1,w)+ dimag(-ci/4.*m1m*a1m2*fnl*fo*cm1p1(w)*cmmp2pm(w))

                          s02(4,3,w) = s02(4,3,w)-dimag(m1m*a1m2*fnl*fo*cm1p1(w)*cmmp2pm(w))/4.

                          s02(4,4,w) = s02(4,4,w)-(m1m*a1m2*fnl*fo*cm1p1(w)*cmmp2pm(w))/4.

  

                          sm22(1,4,w) = sm22(1,4,w)+ dimag(ci/8.*m1mq*a1m2*fnl*cm1m1(w)*cmmp2pm(w))

                          sm22(2,2,w) = sm22(2,2,w)-(m1mq*a1m2*fnl*cm1m1(w)*cmmp2pm(w))/8.

                          sm22(2,3,w) = sm22(2,3,w)+ (ci/8.*m1mq*a1m2*fnl*cm1m1(w)*cmmp2pm(w))

                          sm22(3,2,w) = sm22(3,2,w)+ (-ci/8.*m1mq*a1m2*fnl*cm1m1(w)*cmmp2pm(w))

                          sm22(3,3,w) = sm22(3,3,w)-(m1mq*a1m2*fnl*cm1m1(w)*cmmp2pm(w))/8.

                          sm22(3,4,w) = sm22(3,4,w)+ dimag(m1mq*a1m2*fnl*cm1m1(w)*cmmp2pm(w))/8.

                          sm22(4,2,w) = sm22(4,2,w)+ dimag(ci/8.*m1mq*a1m2*fnl*cm1m1(w)*cmmp2pm(w))

  

                          sp22(1,4,w) = sp22(1,4,w)+ dimag(ci/8.*m1mnpl*m1w*a1m2*fnl*cm1m1(w)*cmmp2pm(w))

                          sp22(2,2,w) = sp22(2,2,w)-(m1mnpl*m1w*a1m2*fnl*cm1m1(w)*cmmp2pm(w))/8.

                          sp22(2,3,w) = sp22(2,3,w)+ (ci/8.*m1mnpl*m1w*a1m2*fnl*cm1m1(w)*cmmp2pm(w))

                          sp22(3,2,w) = sp22(3,2,w)+ (ci/8.*m1mnpl*m1w*a1m2*fnl*cm1m1(w)*cmmp2pm(w))

                          sp22(3,3,w) = sp22(3,3,w)+ (m1mnpl*m1w*a1m2*fnl*cm1m1(w)*cmmp2pm(w))/8.

                          sp22(3,4,w) = sp22(3,4,w)-dimag(m1mnpl*m1w*a1m2*fnl*cm1m1(w)*cmmp2pm(w))/8.

                          sp22(4,2,w) = sp22(4,2,w)+ dimag(-ci/8.*m1mnpl*m1w*a1m2*fnl*cm1m1(w)*cmmp2pm(w))

                       enddo

                    enddo

                 enddo

              enddo

           enddo

           call ms_mpi(mpi_command='barrier')

           nsend=4*4*(2*ntot+1)

           call ms_mpi(mpi_command='allreduce',mpi_recv_buf_dp=s00,&

                mpi_number=nsend,mpi_operation=ms_mpi_sum)

           call ms_mpi(mpi_command='allreduce',mpi_recv_buf_dp=s02,&

                mpi_number=nsend,mpi_operation=ms_mpi_sum)

           call ms_mpi(mpi_command='allreduce',mpi_recv_buf_dp=sp22,&

                mpi_number=nsend,mpi_operation=ms_mpi_sum)

           call ms_mpi(mpi_command='allreduce',mpi_recv_buf_dp=sm22,&

                mpi_number=nsend,mpi_operation=ms_mpi_sum)

  !

  !  a patch

  !

           do i=3,4

              do j=1,i

                 s00(j,i,0:wtot)=-s00(j,i,0:wtot)

                 s02(j,i,0:wtot)=-s02(j,i,0:wtot)

                 sm22(j,i,0:wtot)=-sm22(j,i,0:wtot)

                 sp22(j,i,0:wtot)=-sp22(j,i,0:wtot)

              enddo

           enddo

           deallocate(nlindex,nlnum)

           end subroutine fosmexpansion

  !

  !  compute the coefficients for the GSF expansion of the random orientation

  !  scattering matrix.

  !

  !

  !  original: 15 January 2011

  !  revised: 21 February 2011: changed normalization on S11

  !

           subroutine ranorientscatmatrix(xv,nsphere,nodr,nodrw,cbeam,tmatrixfile,&

                      sm,qext,qabs,qsca)

           use mpidefs

           use mpidata

           use intrinsics

           use specialfuncs

           use spheredata

           use numconstants

           implicit none

           integer :: nodr,nodrw,nodr2,m,n,p,k,l,q,s,t,v,u,w,nblk,kl,mn,nn1,tn, &

                      lmax,ll1,tvl,ku,k1,ns,ik,ik1,m1,nu,n1s,n1e,nu1,p1,n1max, &

                      in,n1,i,lt,kt,qt,nt,mt,ikm,klm,mnm,nodrt,nsphere, &

                      rank,iunit,numprocs

           real(8) :: sm(4,4,0:nodrw),fl,vc(0:4*nodr+2),xv,fl2,fc1,fc2,fc3,fc4, &

                      cbeam,gbn,qext(nsphere),qabs(nsphere),qsca(nsphere),qel, &

                      qal,qsl,fc(4),time1,time2,qsca0

           complex(8) :: ci,cin,a

           complex(8) :: aw(0:2,-1:1,0:nodrw),bw(0:2,-1:1,0:nodrw),cw(0:nodrw), &

                         dw(0:nodrw),pp(nodr,2,2), &

                         bm(2,nodr*(nodr+2),2),am(2,nodr+1,2),fm(3,nodr,2,nodr,2)

           complex(8), allocatable :: dm(:,:,:,:,:,:)

           complex(4), allocatable :: tc(:,:,:,:)

           integer :: nblkw,wv,sizedm,ierr,sizetm,nsend

           integer, allocatable :: windex(:),vindex(:),wvindex(:),wvnum(:)

           real(8) :: wvperproc,sum

           character*30 :: tmatrixfile

           data ci/(0.d0,1.d0)/

           call ms_mpi(mpi_command='rank',mpi_rank=rank)

           call ms_mpi(mpi_command='size',mpi_size=numprocs)

           call getrunparameters(run_print_unit=iunit)

           if(rank.eq.0) time1=mytime()

  !

  !  read the T matrix from the file

  !

           if(rank.eq.0) then

              open(3,file=tmatrixfile)

              read(3,*) nodrt

           endif

           nodrt=nodr

           nblk=nodr*(nodr+2)

           sizetm=4*nblk*nblk

           allocate(tc(2,nblk,2,nblk))

           tc=(0.,0.)

           if(rank.eq.0) then

              qext=0.d0

              qabs=0.d0

              qsca=0.d0

              do l=1,nodr

                 gbn=dexp(-((dble(l)+.5d0)*cbeam)**2.)

                 do k=-l,l

                    kl=l*(l+1)+k

                    klm=l*(l+1)-k

                    do q=1,2

                       read(3,*) lt,kt,qt

                       do n=1,l

                          do m=-n,n

                             mn=n*(n+1)+m

                             mnm=n*(n+1)-m

                             read(3,*) nt,mt,fc

                             tc(1,mn,q,kl)=cmplx(fc(1),fc(2))

                             tc(2,mn,q,kl)=cmplx(fc(3),fc(4))

                             if(n.lt.l) then

                                ikm=(-1)**(m+k)

                                do p=1,2

                                   tc(q,klm,p,mnm)=tc(p,mn,q,kl)*ikm

                                enddo

                             endif

                          enddo

                       enddo

                    enddo

                 enddo

                 do i=1,nsphere

                    read(3,*) n,qel,qal,qsl

                    qext(i)=qext(i)+qel*gbn*gbn

                    qabs(i)=qabs(i)+qal*gbn*gbn

                    qsca(i)=qsca(i)+qsl*gbn*gbn

                 enddo

              enddo

              close(3)

           endif

  !

  !  send to the other processors

  !

           if(numprocs.gt.1) then

              call ms_mpi(mpi_command='barrier')

              call ms_mpi(mpi_command='bcast',mpi_send_buf_dp=qext,mpi_number=nsphere,mpi_rank=0)

              call ms_mpi(mpi_command='bcast',mpi_send_buf_dp=qabs,mpi_number=nsphere,mpi_rank=0)

              call ms_mpi(mpi_command='bcast',mpi_send_buf_dp=qsca,mpi_number=nsphere,mpi_rank=0)

              call ms_mpi(mpi_command='bcast',mpi_send_buf_c=tc,mpi_number=sizetm,mpi_rank=0)

           endif

           allocate(dm(-nodr-1:nodr+1,3,nodr,2,nodr,2))

           if(rank.eq.0) then

              time2=mytime()-time1

              call timewrite(iunit,' t matrix read time:',time2)

              time1=mytime()

           endif

           nodr2=nodr+nodr

           nblk=nodr*(nodr+2)

           dm=(0.d0,0.d0)

           sizedm=size(dm)

           call init(nodr2)

  !

  !  compute the GB modified T matrix

  !

           do n=1,nodr

              gbn=dexp(-((dble(n)+.5d0)*cbeam)**2.)

              cin=ci**(n+1)

              pp(n,1,1) =-.5d0*cin*fnr(n+n+1)*gbn

              pp(n,2,1) =-pp(n,1,1)

              pp(n,1,2)=-pp(n,1,1)

              pp(n,2,2)=pp(n,2,1)

           enddo

           do n=1,nodr

              nn1=n*(n+1)

              do m=-n,n

                 mn=nn1+m

                 do p=1,2

                    do l=1,nodr

                       do k=-l,l

                          kl=l*(l+1)+k

                          a=tc(p,mn,1,kl)

                          tc(p,mn,1,kl)=tc(p,mn,1,kl)*pp(l,1,1)&

                             +tc(p,mn,2,kl)*pp(l,1,2)

                          tc(p,mn,2,kl)=a*pp(l,2,1)+tc(p,mn,2,kl)*pp(l,2,2)

                       enddo

                    enddo

                 enddo

              enddo

           enddo

  !

  !  determine the distribution of work load among the processors

  !

           nblkw=nodr2*(nodr2+2)+1

           allocate(windex(nblkw),vindex(nblkw),wvindex(0:numprocs-1),wvnum(0:numprocs-1))

           w=0

           do n=0,nodr2

              do m=-n,n

                 w=w+1

                 windex(w)=n

                 vindex(w)=m

              enddo

           enddo

           wvperproc=dble(nblkw)/dble(numprocs)

           sum=0.

           do i=0,numprocs-1

              wvindex(i)=floor(sum)

              sum=sum+wvperproc

           enddo

           do i=0,numprocs-2

              wvnum(i)=wvindex(i+1)-wvindex(i)

           enddo

           wvnum(numprocs-1)=nblkw-wvindex(numprocs-1)

           if(rank.eq.0) then

              write(iunit,'('' d matrix calculation, order+degree per proc.:'',f9.2)') &

                  wvperproc

              call flush(iunit)

           endif

  !

  !  the big loop

  !

           do wv=wvindex(rank)+1,wvindex(rank)+wvnum(rank)

              w=windex(wv)

              v=vindex(wv)

              bm=(0.d0,0.d0)

              do n=1,nodr

                 nn1=n*(n+1)

                 do l=max(1,abs(w-n)),min(nodr,w+n)

                    am(1,l,1)=0.d0

                    am(1,l,2)=0.d0

                    am(2,l,1)=0.d0

                    am(2,l,2)=0.d0

                 enddo

                 do t=-n,n

                    tn=nn1+t

                    lmax=min(nodr,w+n)

                    call vcfunc(v,w,-t,n,vc)

                    do l=max(1,abs(v-t),abs(n-w)),lmax

                       ll1=l*(l+1)

                       tvl=ll1+t-v

                       do k=1,2

                          do p=1,2

                             am(k,l,p)=am(k,l,p)+vc(l)*tc(p,tn,k,tvl)

                          enddo

                       enddo

                    enddo

                 enddo

                 do m=-n,n

                    mn=nn1+m

                    do k=1,2

                       u=m-(-3+2*k)

                       if(abs(u).le.w) then

                          lmax=min(nodr,w+n)

                          call vcfunc(-u,w,m,n,vc)

                          do l=max(1,abs(w-n)),lmax

                             fl=-(-1)**l*vc(l)/dble(l+l+1)

                             do p=1,2

                                bm(k,mn,p)=bm(k,mn,p)+am(k,l,p)*fl

                             enddo

                          enddo

                       endif

                    enddo

                 enddo

              enddo

              do u=-min(w,nodr+1),min(w,nodr+1)

                 do ku=1,3

                    if(ku.eq.1) then

                       k=-1

                       k1=-1

                    elseif(ku.eq.2) then

                       k=1

                       k1=1

                    else

                       k=1

                       k1=-1

                    endif

                    m=u+k

                    ns=max(1,abs(m))

                    ik=(k+1)/2+1

                    ik1=(k1+1)/2+1

                    m1=u+k1

                    do n=ns,nodr

                       nu=n*(n+1)+m

                       n1s=max(1,abs(m1),n-nodrw)

                       n1e=min(nodr,n+nodrw)

                       do n1=n1s,n1e

                          cin=ci**(n-n1)

                          nu1=n1*(n1+1)+m1

                          fl=-fnr(n+n+1)*fnr(n1+n1+1)*dble(w+w+1)

                          do p=1,2

                             do p1=1,2

                                a=bm(ik,nu,p)*cin*fl*conjg(bm(ik1,nu1,p1))

                                dm(u,ku,n,p,n1,p1)=dm(u,ku,n,p,n1,p1)+a

                             enddo

                          enddo

                       enddo

                    enddo

                 enddo

              enddo

           enddo

           deallocate(tc)

           call ms_mpi(mpi_command='barrier')

           call ms_mpi(mpi_command='allreduce',mpi_recv_buf_dc=dm,&

                mpi_number=sizedm,mpi_operation=ms_mpi_sum)

           if(rank.eq.0) then

              time2=mytime()-time1

              call timewrite(iunit,' d matrix time:',time2)

              time1=mytime()

           endif

  !

  !  compute the expansion coefficients

  !

           aw=0.d0

           bw=0.d0

           cw=0.d0

           dw=0.d0

           do w=0,nodrw

              do n=1,nodr

                 n1s=max(1,abs(n-w))

                 n1e=min(nodr,n+w)

                 do n1=n1s,n1e

                    do k=1,3

                       do p=1,2

                          do p1=1,2

                             fm(k,n,p,n1,p1)=0.

                          enddo

                       enddo

                    enddo

                 enddo

              enddo

              do u=-nodr-1,nodr+1

                 do k=-1,1,2

                    m=u+k

                    ik=(k+1)/2+1

                    ns=max(1,abs(m))

                    do n=ns,nodr

                       n1max=min(w+n,nodr)

                       call vcfunc(m,n,0,w,vc)

                       do n1=ns,nodr

                          if((n+n1.lt.w).or.(abs(n-n1).gt.w)) cycle

                          fl=-(-1)**n*vc(n1)*fnr(w+w+1)/fnr(n1+n1+1)

                          do p=1,2

                             do p1=1,2

                                fm(ik,n,p,n1,p1)=fm(ik,n,p,n1,p1)+dm(u,ik,n,p,n1,p1)*fl

                             enddo

                          enddo

                       enddo

                    enddo

                 enddo

                 if(w.lt.2) cycle

                 m=u+1

                 m1=u-1

                 ns=max(1,abs(m))

                 n1s=max(1,abs(m1))

                 do n=ns,nodr

                    n1max=min(w+n,nodr)

                    call vcfunc(m,n,-2,w,vc)

                    do n1=n1s,nodr

                       if((n+n1.lt.w).or.(abs(n-n1).gt.w)) cycle

                       fl=-(-1)**n*vc(n1)*fnr(w+w+1)/fnr(n1+n1+1)

                       do p=1,2

                          do p1=1,2

                             fm(3,n,p,n1,p1)=fm(3,n,p,n1,p1)+dm(u,3,n,p,n1,p1)*fl

                          enddo

                       enddo

                    enddo

                 enddo

              enddo

              do n=1,nodr

                 n1s=max(1,abs(n-w))

                 n1e=min(nodr,n+w)

                 in=(-1)**n

                 n1max=min(w+n,nodr)

                 call vcfunc(1,n,0,w,vc)

                 do n1=n1s,n1e

                    fl=2.d0*in*vc(n1)*fnr(w+w+1)/fnr(n1+n1+1)

                    i=mod(n+n1-w,2)+1

                    do p=1,2

                       p1=(2-i)*p+(i-1)*(3-p)

                       do k=-1,1,2

                          ik=(k+1)/2+1

                          aw(0,k,w)=aw(0,k,w)+fm(ik,n,p,n1,p1)*fl

                          bw(0,k,w)=bw(0,k,w)+fm(ik,n,p,n1,3-p1)*fl

                       enddo

                       bw(2,0,w)=bw(2,0,w)+fm(3,n,p,n1,3-p1)*fl

                       aw(2,0,w)=aw(2,0,w)+fm(3,n,p,n1,p1)*fl

                    enddo

                 enddo

                 if(w.lt.2) cycle

                 call vcfunc(1,n,-2,w,vc)

                 do n1=n1s,n1e

                    fl=2.d0*in*vc(n1)*fnr(w+w+1)/fnr(n1+n1+1)

                    i=mod(n+n1-w,2)+1

                    do p=1,2

                       p1=(2-i)*p+(i-1)*(3-p)

                       do k=-1,1,2

                          ik=(k+1)/2+1

                          aw(2,k,w)=aw(2,k,w)+fm(ik,n,p,n1,p1)*fl*(-1)**p1

                          bw(2,k,w)=bw(2,k,w)+fm(ik,n,p,n1,3-p1)*fl*(-1)**(3-p1)

                       enddo

                    enddo

                    fl2=2.*(-1)**(n1+w)*vc(n1)*fnr(w+w+1)/fnr(n1+n1+1)

                    do p=1,2

                       do p1=1,2

                          cw(w)=cw(w)+fm(3,n,p,n1,p1)*fl*(-1)**p1

                          dw(w)=dw(w)+fm(3,n,p,n1,p1)*fl2*(-1)**p

                       enddo

                    enddo

                 enddo

              enddo

           enddo

           do w=0,nodrw

              do k=-1,1

                 do i=0,2

                    aw(i,k,w)=aw(i,k,w)*2./xv/xv

                    bw(i,k,w)=bw(i,k,w)*2./xv/xv

                 enddo

              enddo

              cw(w)=cw(w)*2./xv/xv

              dw(w)=dw(w)*2./xv/xv

           enddo

           do n=0,nodrw

              sm(1,1,n)=aw(0,-1,n)+aw(0,1,n)

              sm(1,2,n)=aw(2,-1,n)+aw(2,1,n)

              sm(1,3,n)=2.d0*dimag(aw(2,0,n))

              sm(1,4,n)=aw(0,1,n)-aw(0,-1,n)

              sm(2,2,n)=dw(n)

              sm(2,3,n)=dimag(dw(n))

              sm(2,4,n)=aw(2,1,n)-aw(2,-1,n)

              sm(3,2,n)=dimag(cw(n))

              sm(3,3,n)=cw(n)

              sm(3,4,n)=dimag(bw(2,-1,n)-bw(2,1,n))

              sm(4,4,n)=bw(0,1,n)-bw(0,-1,n)

           enddo

  !

  !  normalization

  !

           qsca0=sm(1,1,0)

           do n=0,nodrw

              sm(1,1,n)=sm(1,1,n)/qsca0

              sm(1,2,n)=sm(1,2,n)/qsca0

              sm(1,3,n)=sm(1,3,n)/qsca0

              sm(1,4,n)=sm(1,4,n)/qsca0

              sm(2,2,n)=sm(2,2,n)/qsca0

              sm(2,3,n)=sm(2,3,n)/qsca0

              sm(2,4,n)=sm(2,4,n)/qsca0

              sm(3,2,n)=sm(3,2,n)/qsca0

              sm(3,3,n)=sm(3,3,n)/qsca0

              sm(3,4,n)=sm(3,4,n)/qsca0

              sm(4,4,n)=sm(4,4,n)/qsca0

           enddo

           call ms_mpi(mpi_command='barrier')

           if(rank.eq.0) then

              time2=mytime()-time1

              call timewrite(iunit,' scat matrix coef time:',time2)

           endif

           deallocate(windex,vindex,wvindex,wvnum,dm)

           end subroutine ranorientscatmatrix

  !

  !  calculation of the RO scattering matrix from the GSF expansion

  !

  !

  !  original: 15 January 2011

  !  revised: 21 February 2011: changed normalization on S11

  !

           subroutine ranorienscatmatrixcalc(nt,tmin,tmax,iscale,smc,nodrexp,sm)

           use specialfuncs

           use numconstants

           implicit none

           integer :: nt,iscale,nodrexp,i,j,k,n,nn0,nnp2,nnm2

           real(8) :: tmin,tmax,smc(4,4,0:nodrexp),sm(4,4,nt),dc(-2:2,0:nodrexp*(nodrexp+2)), &

                      ct,th,qsca

           do i=1,nt

              if(nt.eq.1) then

                 th=tmin

              else

                 th=tmin+(tmax-tmin)*dble(i-1)/dble(nt-1)

              endif

              ct=cos(th*pi/180.d0)

  !

  !     dc is the normalized generalized spherical function

  !     dc(k,n*(n+1)+m) = ((n-k)!(n+m)!/(n+k)!/(n-m)!)^(1/2) D^k_{mn},

  !     where D^k_{mn} is defined in M&M JOSA 96

  !

              call rotcoef(ct,2,nodrexp,dc)

              do j=1,4

                 do k=j,4

                    sm(j,k,i)=0.d0

                 enddo

              enddo

              do n=0,nodrexp

                 nn0=n*(n+1)

                 nnp2=nn0+2

                 nnm2=nn0-2

                 sm(1,1,i)=sm(1,1,i)+dc(0,nn0)*smc(1,1,n)

                 sm(1,4,i)=sm(1,4,i)+dc(0,nn0)*smc(1,4,n)

                 sm(4,4,i)=sm(4,4,i)+dc(0,nn0)*smc(4,4,n)

                 if(n.ge.2) then

                    sm(1,2,i)=sm(1,2,i)+dc(2,nn0)*smc(1,2,n)

                    sm(2,4,i)=sm(2,4,i)+dc(2,nn0)*smc(2,4,n)

                    sm(3,4,i)=sm(3,4,i)+dc(2,nn0)*smc(3,4,n)

                    sm(1,3,i)=sm(1,3,i)+dc(2,nn0)*smc(1,3,n)

                    sm(2,2,i)=sm(2,2,i)+dc(2,nnm2)*smc(2,2,n)+dc(2,nnp2)*smc(3,3,n)

                    sm(2,3,i)=sm(2,3,i)+dc(2,nnp2)*smc(2,3,n)+dc(2,nnp2)*smc(3,2,n)

                    sm(3,3,i)=sm(3,3,i)-dc(2,nnm2)*smc(2,2,n)+dc(2,nnp2)*smc(3,3,n)

                 endif

              enddo

  !

  !  discontiued scaling option: now done in main program

  !

  !            if(iscale.eq.1) then

  !               do j=1,4

  !                  do k=j,4

  !                     if(j.ne.1.or.k.ne.1) then

  !                        sm(j,k,i)=sm(j,k,i)/sm(1,1,i)

  !                     endif

  !                  enddo

  !               enddo

  !            endif

  !

  !    here are the VV and HH differential cross sections

  !

  !            gvv=.25*(sm(1,1)+sm(2,2)-2.*sm(1,2))

  !            ghh=.25*(sm(1,1)+sm(2,2)+2.*sm(1,2))

  !

           enddo

           return

           end subroutine ranorienscatmatrixcalc

        end module scatprops

  !

  ! module nearfield contains local data and subroutines for near field calculation

  !

  !

  !  last revised: 15 January 2011

  !  30 March 2011: added optical activity

  !

        module nearfield

        implicit none

        integer, private :: axialinc,ndimpw,nodrpwmax

        integer, private, allocatable :: nblk_nf(:),noff_nf(:)

        real(8), private :: rplotmax

        complex(8), allocatable, private :: amnp_nf(:),cmnp_nf(:),pmnp_nf(:), &

                 amn3mp_nf(:),cmn3mp_nf(:),pmn3mp_nf(:)

        contains

  !

  !  nearfieldspherepart calculates the field at point xg generated by the spheres

  !

  !

  !  last revised: 15 January 2011

  !  30 March 2011: added optical activity

  !

           subroutine nearfieldspherepart(xg,nsphere,xsp,rpos,ri,&

                      nodr,neqns,insphere,efield,hfield)

           use specialfuncs

           use numconstants

           implicit none

           integer :: nsphere,nodr(nsphere),neqns,i,insphere,nblki,n

           real(8) :: xg(3),xsp(nsphere),rpos(3,nsphere),x(3),r

           complex(8) :: ri(2,nsphere),vwh(3,neqns),efield(3),hfield(3),ri0,cn1,cn2

           complex(8), allocatable :: vwhleft(:,:,:),vwhright(:,:,:)

  

  !

  !  find if the point is inside a sphere

  !

           insphere=0

           do i=1,nsphere

              x=xg(:)-rpos(:,i)

              r=sqrt(dot_product(x,x))

              if(r.le.xsp(i)) then

                 insphere=i

                 exit

              endif

           enddo

  !

  !  do the calculations

  !

           if(insphere.eq.0) then

  !

  !  outside a sphere: field = scattered

  !

              do i=1,nsphere

                 x=xg(:)-rpos(:,i)

                 ri0=(1.d0,0.d0)

                 call vwhcalc(x,ri0,nodr(i),3,vwh(1:3,noff_nf(i)+1:noff_nf(i)+nblk_nf(i)))

              enddo

              efield(:)=matmul(vwh(:,1:neqns),amnp_nf(1:neqns))

              hfield(:)=matmul(vwh(:,1:neqns),amn3mp_nf(1:neqns))/dcmplx(0.d0,1.d0)

           else

  !

  !  inside a sphere: field = internal

  !

              i=insphere

              if(abs(ri(1,i)-ri(2,i)).eq.0) then

                 x=xg(:)-rpos(:,i)

                 call vwhcalc(x,ri(1,i),nodr(i),1,vwh)

                 efield(:)=matmul(vwh(:,1:nblk_nf(i)), &

                     cmnp_nf(noff_nf(i)+1:noff_nf(i)+nblk_nf(i)))

                 hfield(:)=matmul(vwh(:,1:nblk_nf(i)), &

                     cmn3mp_nf(noff_nf(i)+1:noff_nf(i)+nblk_nf(i)))*ri(1,i)/dcmplx(0.d0,1.d0)

              else

                 nblki=nodr(i)*(nodr(i)+2)

                 allocate(vwhleft(3,2,nblki),vwhright(3,2,nblki))

                 x=xg(:)-rpos(:,i)

                 call vwhcalc(x,ri(1,i),nodr(i),1,vwhleft)

                 call vwhcalc(x,ri(2,i),nodr(i),1,vwhright)

                 efield=0.d0

                 hfield=0.d0

                 do n=1,nblki

                    cn1=cmnp_nf(noff_nf(i)+2*n-1)

                    cn2=cmnp_nf(noff_nf(i)+2*n)

                    efield(:)=efield(:)+(vwhleft(:,1,n)+vwhleft(:,2,n))*cn1 &

                           +(vwhright(:,1,n)-vwhright(:,2,n))*cn2

                    hfield(:)=hfield(:)+((vwhleft(:,2,n)+vwhleft(:,1,n))*cn1*ri(1,i) &

                           +(vwhright(:,2,n)-vwhright(:,1,n))*cn2*ri(2,i))/dcmplx(0.d0,1.d0)

                 enddo

                 deallocate(vwhleft,vwhright)

              endif

           endif

           end subroutine nearfieldspherepart

  !

  !  nearfieldincidentpart calculates the incident field at point xg using a regular

  !  vswh expansion

  !

  !

  !  last revised: 15 January 2011

  !

           subroutine nearfieldincidentpart(xg,nodrpw,efield,hfield)

           use specialfuncs

           use numconstants

           implicit none

           integer :: nblkpw,nodrpw

           real(8) :: xg(3),r,epspw

           complex(8) :: vwhpw(3,nodrpw*(nodrpw+2)*2),vwhpwaxial(3,4*nodrpw), &

                         efield(3),hfield(3)

  !

  !  oblique incidence: use the full expansion

  !

           if(axialinc.eq.0) then

              call vwhcalc(xg,(1.d0,0.d0),nodrpw,1,vwhpw)

              nblkpw=nodrpw*(nodrpw+2)*2

              efield(:)=matmul(vwhpw(:,1:nblkpw),pmnp_nf(1:nblkpw))

              hfield(:)=matmul(vwhpw(:,1:nblkpw),pmn3mp_nf(1:nblkpw))/dcmplx(0.d0,1.d0)

           else

  !

  !  axial incidence: use the shortcut

  !

              call vwhaxialcalc(xg,(1.d0,0.d0),nodrpw,1,vwhpwaxial)

              nblkpw=4*nodrpw

              efield(:)=matmul(vwhpwaxial(:,1:nblkpw),pmnp_nf(1:nblkpw))

              hfield(:)=matmul(vwhpwaxial(:,1:nblkpw),pmn3mp_nf(1:nblkpw))/dcmplx(0.d0,1.d0)

           endif

           end subroutine nearfieldincidentpart

  !

  !  nearfieldincidentcoef generates the reshaped array of incident field coefficients

  !

  !

  !  last revised: 15 January 2011

  !

           subroutine nearfieldincidentcoef(nodrpw,alpha,beta,gamma,cbeam,epspw)

           use specialfuncs

           use spheredata

           use miecoefdata

           use numconstants

           implicit none

           integer :: m,n,p,nn1,mn,mnp,nodrpw

           real (8) :: alpha,beta,cbeam,gamma,epspw,cgamma,sgamma

           complex(8), allocatable :: pmnp0(:,:,:,:)

           allocate(pmnp0(0:nodrpw+1,nodrpw,2,2))

           if(allocated(pmnp_nf)) deallocate(pmnp_nf,pmn3mp_nf)

           if(beta.ne.0.d0) then

              axialinc=0

              ndimpw=2*nodrpw*(nodrpw+2)

           else

              axialinc=1

              ndimpw=4*nodrpw

           endif

           allocate(pmnp_nf(ndimpw),pmn3mp_nf(ndimpw))

           if(cbeam.eq.0.d0) then

              call planewavecoef(alpha,beta,nodrpw,pmnp0)

           else

              call gaussianbeamcoef(alpha,beta,cbeam,nodrpw,pmnp0)

           endif

           cgamma=cos(gamma)

           sgamma=sin(gamma)

           if(axialinc.eq.0) then

              do n=1,nodrpw

                 nn1=n*(n+1)

                 do p=1,2

                    do m=-n,-1

                       mn=nn1+m

                       mnp=2*(mn-1)+p

                       pmnp_nf(mnp)=pmnp0(n+1,-m,p,1)*cgamma+pmnp0(n+1,-m,p,2)*sgamma

                       pmn3mp_nf(mnp)=pmnp0(n+1,-m,3-p,1)*cgamma+pmnp0(n+1,-m,3-p,2)*sgamma

                    enddo

                    do m=0,n

                       mn=nn1+m

                       mnp=2*(mn-1)+p

                       pmnp_nf(mnp)=pmnp0(m,n,p,1)*cgamma+pmnp0(m,n,p,2)*sgamma

                       pmn3mp_nf(mnp)=pmnp0(m,n,3-p,1)*cgamma+pmnp0(m,n,3-p,2)*sgamma

                    enddo

                 enddo

              enddo

           else

              do n=1,nodrpw

                 do p=1,2

                    mnp=4*(n-1)+p

                    pmnp_nf(mnp)=pmnp0(n+1,1,p,1)*cgamma+pmnp0(n+1,1,p,2)*sgamma

                    pmn3mp_nf(mnp)=pmnp0(n+1,1,3-p,1)*cgamma+pmnp0(n+1,1,3-p,2)*sgamma

                    pmnp_nf(mnp+2)=pmnp0(1,n,p,1)*cgamma+pmnp0(1,n,p,2)*sgamma

                    pmn3mp_nf(mnp+2)=pmnp0(1,n,3-p,1)*cgamma+pmnp0(1,n,3-p,2)*sgamma

                 enddo

              enddo

           endif

           deallocate(pmnp0)

           end subroutine nearfieldincidentcoef

  !

  !  nearfieldpointcalc: if newcalc = 1, generates the reshaped incident, scattered, and

  !                      internal field coefficients, and returns with newcalc=0

  !                      if newcalc = 0, generates the field at point xg

  !

  !

  !  last revised: 15 January 2011

  !  30 March 2011: added optical activity

  !

           subroutine nearfieldpointcalc(neqns,nsphere,nodr,alpha,beta,cbeam,xsp,rpos,ri,amnp, &

                      gamma,epspw,xg,newcalc,efield,hfield)

           use specialfuncs

           use spheredata

           use miecoefdata

           use numconstants

           implicit none

           integer :: nsphere,neqns,nodr(nsphere),i,j,k,m,n,p,nn1,mn,nodrpw,newcalc, &

                      insphere

           real (8) :: alpha,beta,cbeam,xsp(nsphere),rpos(3,nsphere),xg(3),xgp(3), &

                       gamma,epspw,rplot,cgamma,sgamma

           complex(8) :: amnp(neqns,2),ri(2,nsphere),efield(3),hfield(3),einc(3),hinc(3),ri0, &

                         ct1,ct2

           complex(8), allocatable :: pmnp0(:,:,:,:),cnmie(:,:,:),amnpt(:,:),cmnpt(:,:)

  !

  !  initialization operations: newcalc=1

  !

           if(newcalc.eq.1) then

              if(allocated(amnp_nf)) deallocate(amnp_nf,cmnp_nf,amn3mp_nf,cmn3mp_nf, &

                       noff_nf,nblk_nf)

              allocate(amnp_nf(neqns),cmnp_nf(neqns),amn3mp_nf(neqns),cmn3mp_nf(neqns), &

                       noff_nf(nsphere),nblk_nf(nsphere))

              noff_nf(1)=0

              do i=1,nsphere

                 nblk_nf(i)=2*nodr(i)*(nodr(i)+2)

                 if(i.lt.nsphere) noff_nf(i+1)=noff_nf(i)+nblk_nf(i)

              enddo

              cgamma=cos(gamma)

              sgamma=sin(gamma)

              do i=1,nsphere

                 ri0=2.d0/(1.d0/ri(1,i)+1.d0/ri(2,i))

                 allocate(pmnp0(0:nodr(i)+1,nodr(i),2,2),cnmie(2,2,nodr(i)),&

                          amnpt(2,nodr(i)*(nodr(i)+2)),cmnpt(2,nodr(i)*(nodr(i)+2)))

                 call getmiedata(which_sphere=i,sphere_int_mie_coefficients=cnmie)

                 do p=1,2

                    pmnp0(0:nodr(i)+1,1:nodr(i),1:2,p) &

                        =reshape(amnp(noff_nf(i)+1:noff_nf(i)+nblk_nf(i),p),(/nodr(i)+2,nodr(i),2/))

                 enddo

                 if(abs(ri(1,i)-ri(2,i)).gt.1.d-10) then

                    do n=1,nodr(i)

                       nn1=n*(n+1)

                       do p=1,2

                          do m=-n,-1

                             mn=nn1+m

                             amnpt(p,mn)=pmnp0(n+1,-m,p,1)*cgamma+pmnp0(n+1,-m,p,2)*sgamma

                          enddo

                          do m=0,n

                             mn=nn1+m

                             amnpt(p,mn)=pmnp0(m,n,p,1)*cgamma+pmnp0(m,n,p,2)*sgamma

                          enddo

                       enddo

                       do m=-n,n

                          mn=nn1+m

                          ct1=amnpt(1,mn)*cnmie(1,1,n)+amnpt(2,mn)*cnmie(1,2,n)

                          ct2=amnpt(1,mn)*cnmie(2,1,n)+amnpt(2,mn)*cnmie(2,2,n)

                          cmnpt(1,mn)=ct1

                          cmnpt(2,mn)=-(0.d0,1.d0)/ri0*ct2

                       enddo

                    enddo

                 else

                    do n=1,nodr(i)

                       nn1=n*(n+1)

                       do p=1,2

                          do m=-n,-1

                             mn=nn1+m

                             amnpt(p,mn)=pmnp0(n+1,-m,p,1)*cgamma+pmnp0(n+1,-m,p,2)*sgamma

                             cmnpt(p,mn)=amnpt(p,mn)*cnmie(p,p,n)

                          enddo

                          do m=0,n

                             mn=nn1+m

                             amnpt(p,mn)=pmnp0(m,n,p,1)*cgamma+pmnp0(m,n,p,2)*sgamma

                             cmnpt(p,mn)=amnpt(p,mn)*cnmie(p,p,n)

                          enddo

                       enddo

                    enddo

                 endif

                 amnp_nf(noff_nf(i)+1:noff_nf(i)+nblk_nf(i))= &

                        reshape(amnpt(1:2,1:nodr(i)*(nodr(i)+2)),(/nblk_nf(i)/))

                 cmnp_nf(noff_nf(i)+1:noff_nf(i)+nblk_nf(i))= &

                        reshape(cmnpt(1:2,1:nodr(i)*(nodr(i)+2)),(/nblk_nf(i)/))

                 amn3mp_nf(noff_nf(i)+1:noff_nf(i)+nblk_nf(i))= &

                        reshape(amnpt(2:1:-1,1:nodr(i)*(nodr(i)+2)),(/nblk_nf(i)/))

                 cmn3mp_nf(noff_nf(i)+1:noff_nf(i)+nblk_nf(i))= &

                        reshape(cmnpt(2:1:-1,1:nodr(i)*(nodr(i)+2)),(/nblk_nf(i)/))

                 deallocate(pmnp0,cnmie,amnpt,cmnpt)

              enddo

              rplot=sqrt(dot_product(xg,xg))

              rplotmax=rplot

              call planewavetruncationorder(rplot,epspw,nodrpw)

              nodrpwmax=nodrpw

              call nearfieldincidentcoef(nodrpw,alpha,beta,gamma,cbeam,epspw)

              newcalc=0

              return

           endif

  !

  !  point calculation operations: newcalc=0

  !  first determine the required order of the incident field expansion, and recalculate

  !  field coefficients, if necessary

  !

           rplot=sqrt(dot_product(xg,xg))

           rplotmax=max(rplot,rplotmax)

           call planewavetruncationorder(rplot,epspw,nodrpw)

           if(nodrpw.gt.nodrpwmax) then

              nodrpwmax=nodrpw

              call nearfieldincidentcoef(nodrpw,alpha,beta,gamma,cbeam,epspw)

           endif

           efield=0.d0

           hfield=0.d0

  !

  !  calculate the sphere contribution to the field

  !

           call nearfieldspherepart(xg,nsphere,xsp,rpos,ri,&

                      nodr,neqns,insphere,efield,hfield)

  !

  !  if the point is external to the spheres, calculate the incident field

  !

           if(insphere.eq.0) then

              call nearfieldincidentpart(xg,nodrpw,einc,hinc)

              efield=efield+einc

              hfield=hfield+hinc

           endif

           end subroutine nearfieldpointcalc

  !

  !  nearfieldgridcalc is an MPI--enabled subroutine for calculating field points on a

  !  rectangular grid.   Writes the data to nfoutunit.

  !

  !

  !  last revised: 15 January 2011

  !  30 March 2011: added optical activity

  !  changed so that input positions are defined relative to sphere position file origin, and

  !  not the gb focal point.

  !

           subroutine nearfieldgridcalc(neqns,nsphere,nodr,alpha,beta,cbeam,xsp,rpos,ri,amnp, &

                      nfplane,nfplanepos0,nfplanevert0,gbfocus,deltax,gamma,nfoutunit,epspw, &

                      nfoutdata,runprintunit)

           use mpidefs

           use mpidata

           use intrinsics

           use specialfuncs

           use spheredata

           use miecoefdata

           use numconstants

           implicit none

           integer :: nsphere,neqns,nodr(nsphere),nfplane,runprintunit,npoints1,npoints2, &

                      npoints,i,j,k,np23,gcoord(3),rank,numprocs,nrowperproc,nrowrem, &

                      npoints1by5,plotincfield,nsp,nfoutunit,nfoutdata,newcalc

           real (8) :: alpha,beta,cbeam,xsp(nsphere),rpos(3,nsphere),nfplanepos,&

                       nfplanevert(2,2),frowperproc,rowsum,xg(3),xgp(3),deltax,gamma,epspw, &

                       time1,time2,xplot(3,nsphere),xi0,ri0,esquare,xgpmax(3),rplot, &

                       gbfocus(3),nfplanepos0,nfplanevert0(2,2)

           complex(8) :: amnp(neqns,2),ri(2,nsphere),efield(3),hfield(3)

           integer, allocatable :: efindex(:),efnum(:)

           complex(8), allocatable :: efieldrow(:,:),efieldrowt(:,:),hfieldrow(:,:),hfieldrowt(:,:)

           call ms_mpi(mpi_command='size',mpi_size=numprocs)

           call ms_mpi(mpi_command='rank',mpi_rank=rank)

  !

  !  determine the plane

  !

           if(nfplane.eq.1) then

              gcoord=(/2,3,1/)

           elseif(nfplane.eq.2) then

              gcoord=(/3,1,2/)

           else

              gcoord=(/1,2,3/)

           endif

  !

  !  shift the coordinates to gb focal origin

  !

           nfplanevert(1,1)=nfplanevert0(1,1)-gbfocus(gcoord(1))

           nfplanevert(1,2)=nfplanevert0(1,2)-gbfocus(gcoord(1))

           nfplanevert(2,1)=nfplanevert0(2,1)-gbfocus(gcoord(2))

           nfplanevert(2,2)=nfplanevert0(2,2)-gbfocus(gcoord(2))

           nfplanepos=nfplanepos0-gbfocus(gcoord(3))

           xg(gcoord(3))=nfplanepos

  !

  !  determine the number of points

  !

           npoints1=nint((nfplanevert(1,2)-nfplanevert(1,1))/deltax)+1

           npoints2=nint((nfplanevert(2,2)-nfplanevert(2,1))/deltax)+1

           npoints=npoints1*npoints2

  !

  !  find the maximum point-to-target origin distance and initialize the field calculation

  !

           xgp(3)=nfplanepos

           rplotmax=0.d0

           xgpmax=0.d0

           do i=1,npoints1

              xgp(1)=nfplanevert(1,1)+deltax*dble(i-1)

              do j=1,npoints2

                 xgp(2)=nfplanevert(2,1)+deltax*dble(j-1)

                 rplot=sqrt(dot_product(xgp,xgp))

                 if(rplot.gt.rplotmax) then

                    rplotmax=rplot

                    xgpmax=xgp

                 endif

              enddo

           enddo

           newcalc=1

           call nearfieldpointcalc(neqns,nsphere,nodr,alpha,beta,cbeam,xsp,rpos,ri,amnp, &

                      gamma,epspw,xgpmax,newcalc,efield,hfield)

  !

  !  determine the intersecting spheres

  !

           nsp=0

           do i=1,nsphere

              xi0=abs(rpos(gcoord(3),i)-xg(gcoord(3)))

              if(xi0.le.xsp(i)) then

                 nsp=nsp+1

                 xplot(1,nsp)=rpos(gcoord(1),i)+gbfocus(gcoord(1))

                 xplot(2,nsp)=rpos(gcoord(2),i)+gbfocus(gcoord(2))

                 ri0=xsp(i)*xsp(i)-xi0*xi0

                 if(ri0.ne.0.) ri0=sqrt(ri0)

                 xplot(3,nsp)=ri0

              endif

           enddo

  !

  !  report to runprintunit

  !

           if(rank.eq.0) then

              write(runprintunit,'('' near field calculations'')')

              write(runprintunit,'('' plane, position:'',i5,f9.3)') nfplane,nfplanepos0

              write(runprintunit,'('' rectangular plot vertices:'')')

              write(runprintunit,'('' min:'',3f9.3)') nfplanevert0(1:2,1)

              write(runprintunit,'('' max:'',3f9.3)') nfplanevert0(1:2,2)

              write(runprintunit,'('' number of plotting points, step size:'',i8,f8.3)') npoints, deltax

              write(runprintunit,'('' max plane wave order:'',i5)') nodrpwmax

           endif

  !

  !  determine the distribution of work among the processors

  !

           allocate(efindex(0:numprocs-1),efnum(0:numprocs-1), &

                    efieldrow(3,npoints2),efieldrowt(3,npoints2), &

                    hfieldrow(3,npoints2),hfieldrowt(3,npoints2))

           np23=3*npoints2

           frowperproc=dble(npoints2)/dble(numprocs)

           rowsum=0.

           do i=0,numprocs-1

              efindex(i)=floor(rowsum)

              rowsum=rowsum+frowperproc

           enddo

           do i=0,numprocs-2

              efnum(i)=efindex(i+1)-efindex(i)

           enddo

           efnum(numprocs-1)=npoints2-efindex(numprocs-1)

           npoints1by5=int(npoints1/5.+.5)

  !

  !  do the calculations and write the results to the file

  !

           if(rank.eq.0) then

              write(nfoutunit,*) npoints1,npoints2

              write(nfoutunit,*) nsp

              do i=1,nsp

                 write(nfoutunit,'(3e13.5)') xplot(1,i),xplot(2,i),xplot(3,i)

              enddo

              time1=mytime()

           endif

           xg(gcoord(3))=nfplanepos

           newcalc=0

           do i=1,npoints1

              xg(gcoord(1))=nfplanevert(1,1)+deltax*dble(i-1)

              xgp(gcoord(1))=nfplanevert0(1,1)+deltax*dble(i-1)

              efieldrowt=0.d0

              efieldrow=0.d0

              hfieldrowt=0.d0

              hfieldrow=0.d0

              do j=efindex(rank)+1,efindex(rank)+efnum(rank)

                 xg(gcoord(2))=nfplanevert(2,1)+deltax*dble(j-1)

                 call nearfieldpointcalc(neqns,nsphere,nodr,alpha,beta,cbeam,xsp,rpos,ri,amnp, &

                      gamma,epspw,xg,newcalc,efieldrowt(:,j),hfieldrowt(:,j))

              enddo

              call ms_mpi(mpi_command='barrier')

              call ms_mpi(mpi_command='reduce',mpi_send_buf_dc=efieldrowt,mpi_recv_buf_dc=efieldrow,&

                   mpi_number=np23,mpi_rank=0,mpi_operation=ms_mpi_sum)

              if(nfoutdata.ge.2) then

                 call ms_mpi(mpi_command='reduce',mpi_send_buf_dc=hfieldrowt,mpi_recv_buf_dc=hfieldrow,&

                      mpi_number=np23,mpi_rank=0,mpi_operation=ms_mpi_sum)

              endif

              if(rank.eq.0) then

                 do j=1,npoints2

                    xgp(gcoord(2))=nfplanevert0(2,1)+deltax*dble(j-1)

                    if(nfoutdata.eq.0) then

                       esquare=dot_product(efieldrow(:,j),efieldrow(:,j))

                       write(nfoutunit,'(2f9.4,e13.5)') xgp(gcoord(1)),xgp(gcoord(2)),esquare

                    elseif(nfoutdata.eq.1) then

                       write(nfoutunit,'(2f9.4,6e13.5)') xgp(gcoord(1)),xgp(gcoord(2)),efieldrow(:,j)

                    else

                       write(nfoutunit,'(2f9.4,12e13.5)') xgp(gcoord(1)),xgp(gcoord(2)),efieldrow(:,j), &

                                                          hfieldrow(:,j)

                    endif

                 enddo

                 if(mod(i,npoints1by5).eq.0) then

                    k=i/npoints1by5

                    time2=(mytime()-time1)*dble(5-k)/dble(k)

                    call timewrite(runprintunit,' estimated time remaining:',time2)

                 endif

              endif

           enddo

           deallocate(efindex,efnum,efieldrow,efieldrowt,hfieldrow,hfieldrowt)

           end subroutine nearfieldgridcalc

  !

  !  nearfieldaverage is an MPI--enabled subroutine for calculating average field

  !  values along a line.

  !

           subroutine nearfieldaverage(neqns,nsphere,nodr,alpha,beta,cbeam,xsp,rpos,ri,amnp, &

                      nfplane,nfplaneposstart0,nfplaneposend0,numberplanes,nfplanevert0,gbfocus, &

                      deltax,gamma,epspw,runprintunit,efieldavez,hfieldavez,svecavez)

           use mpidefs

           use mpidata

           use intrinsics

           use specialfuncs

           use spheredata

           use miecoefdata

           use numconstants

           implicit none

           integer :: nsphere,neqns,nodr(nsphere),nfplane,runprintunit,npoints1,npoints2, &

                      npoints,i,j,k,np23,gcoord(3),rank,numprocs,nrowperproc,nrowrem, &

                      npoints1by5,plotincfield,nsp,nfoutunit,nfoutdata,newcalc,nsend

           integer :: numberplanes

           real (8) :: alpha,beta,cbeam,xsp(nsphere),rpos(3,nsphere),nfplanepos,&

                       nfplanevert(2,2),frowperproc,rowsum,xg(3),xgp(3),deltax,gamma,epspw, &

                       time1,time2,xplot(3,nsphere),xi0,ri0,esquare,xgpmax(3),rplot, &

                       gbfocus(3),nfplanepos0,nfplanevert0(2,2),nfplaneposstart,nfplaneposend, &

                       deltanfplane,nfplaneposstart0,nfplaneposend0,svec(3),svecave(3)

           real (8) :: svecavez(3,numberplanes)

           real(8), allocatable :: xypoint(:,:)

           complex(8) :: amnp(neqns,2),ri(2,nsphere),efield(3),hfield(3),hfieldave(3),efieldave(3)

           complex(8) :: efieldavez(3,numberplanes),hfieldavez(3,numberplanes)

           integer, allocatable :: efindex(:),efnum(:)

           call ms_mpi(mpi_command='size',mpi_size=numprocs)

           call ms_mpi(mpi_command='rank',mpi_rank=rank)

  !

  !  determine the plane

  !

           if(nfplane.eq.1) then

              gcoord=(/2,3,1/)

           elseif(nfplane.eq.2) then

              gcoord=(/3,1,2/)

           else

              gcoord=(/1,2,3/)

           endif

  !

  !  shift the coordinates to gb focal origin

  !

           nfplanevert(1,1)=nfplanevert0(1,1)-gbfocus(gcoord(1))

           nfplanevert(1,2)=nfplanevert0(1,2)-gbfocus(gcoord(1))

           nfplanevert(2,1)=nfplanevert0(2,1)-gbfocus(gcoord(2))

           nfplanevert(2,2)=nfplanevert0(2,2)-gbfocus(gcoord(2))

           nfplaneposstart=nfplaneposstart0-gbfocus(gcoord(3))

           nfplaneposend=nfplaneposend0-gbfocus(gcoord(3))

           xg(gcoord(3))=nfplanepos

  !

  !  determine the number of points

  !

           npoints1=nint((nfplanevert(1,2)-nfplanevert(1,1))/deltax)+1

           npoints2=nint((nfplanevert(2,2)-nfplanevert(2,1))/deltax)+1

           npoints=npoints1*npoints2

           allocate(efindex(0:numprocs-1),efnum(0:numprocs-1),xypoint(2,npoints))

           frowperproc=dble(npoints)/dble(numprocs)

           rowsum=0.

           do i=0,numprocs-1

              efindex(i)=floor(rowsum)

              rowsum=rowsum+frowperproc

           enddo

           do i=0,numprocs-2

              efnum(i)=efindex(i+1)-efindex(i)

           enddo

           efnum(numprocs-1)=npoints-efindex(numprocs-1)

  

           xgp(gcoord(3))=max(abs(nfplaneposstart),abs(nfplaneposend))

           rplotmax=0.d0

           xgpmax=0.d0

           k=0

           do i=1,npoints1

              xgp(gcoord(1))=nfplanevert(1,1)+deltax*dble(i-1)

              do j=1,npoints2

                 xgp(gcoord(2))=nfplanevert(2,1)+deltax*dble(j-1)

                 k=k+1

                 xypoint(1,k)=xgp(gcoord(1))

                 xypoint(2,k)=xgp(gcoord(2))

                 rplot=sqrt(dot_product(xgp,xgp))

                 if(rplot.gt.rplotmax) then

                    rplotmax=rplot

                    xgpmax=xgp

                 endif

              enddo

           enddo

           newcalc=1

           call nearfieldpointcalc(neqns,nsphere,nodr,alpha,beta,cbeam,xsp,rpos,ri,amnp, &

                      gamma,epspw,xgpmax,newcalc,efield,hfield)

           newcalc=0

  

           do k=1,numberplanes

              if(numberplanes.eq.1) then

                 nfplanepos=nfplaneposstart

              else

                 nfplanepos=nfplaneposstart+(nfplaneposend-nfplaneposstart)*(k-1)/dble(numberplanes-1)

              endif

  !

  !  find the maximum point-to-target origin distance and initialize the field calculation

  !

              xg(3)=nfplanepos

              efieldave=0.

              hfieldave=0.

              svecave=0.

              do i=efindex(rank)+1,efindex(rank)+efnum(rank)

                 xg(gcoord(1))=xypoint(1,i)

                 xg(gcoord(2))=xypoint(2,i)

                 efield=0.d0

                 hfield=0.d0

                 call nearfieldpointcalc(neqns,nsphere,nodr,alpha,beta,cbeam,xsp,rpos,ri,amnp, &

                      gamma,epspw,xg,newcalc,efield,hfield)

                 efieldave=efieldave+efield

                 hfieldave=hfieldave+hfield

                 hfield=conjg(hfield)/2.d0

                 svec(1)=-efield(3)*hfield(2)+efield(2)*hfield(3)

                 svec(2)=efield(3)*hfield(1)-efield(1)*hfield(3)

                 svec(3)=-efield(2)*hfield(1)+efield(1)*hfield(2)

                 svecave=svecave+svec

              enddo

              call ms_mpi(mpi_command='barrier')

              efield=0.d0

              hfield=0.d0

              svec=0.d0

              call ms_mpi(mpi_command='reduce',mpi_send_buf_dc=efieldave,mpi_recv_buf_dc=efield,&

                      mpi_number=3,mpi_rank=0,mpi_operation=ms_mpi_sum)

              call ms_mpi(mpi_command='reduce',mpi_send_buf_dc=hfieldave,mpi_recv_buf_dc=hfield,&

                      mpi_number=3,mpi_rank=0,mpi_operation=ms_mpi_sum)

              call ms_mpi(mpi_command='reduce',mpi_send_buf_dp=svecave,mpi_recv_buf_dp=svec,&

                      mpi_number=3,mpi_rank=0,mpi_operation=ms_mpi_sum)

              call ms_mpi(mpi_command='barrier')

              if(rank.eq.0) then

                 efield=efield/dble(npoints)

                 hfield=hfield/dble(npoints)

                 svec=svec/dble(npoints)

                 efieldavez(:,k)=efield

                 hfieldavez(:,k)=hfield

                 svecavez(:,k)=svec

                 i=gcoord(3)

                 write(runprintunit,'('' plane:'',i5,f9.3,2e12.4)') k,nfplanepos, &

                     svec(i)

                 call flush(runprintunit)

              endif

           enddo

           nsend=3*numberplanes

           call ms_mpi(mpi_command='bcast',mpi_send_buf_dc=efieldavez, &

                mpi_number=nsend,mpi_rank=0)

           call ms_mpi(mpi_command='bcast',mpi_send_buf_dc=hfieldavez, &

                mpi_number=nsend,mpi_rank=0)

           call ms_mpi(mpi_command='bcast',mpi_send_buf_dp=svecavez, &

                mpi_number=nsend,mpi_rank=0)

           call ms_mpi(mpi_command='barrier')

  

           deallocate(efindex,efnum,xypoint)

           end subroutine nearfieldaverage

  

  

        end module nearfield

  !

  !  module solver: subroutines for solving interaction equations for fixed orientation

  !  and T matrix problems

  !

  !

  !  last revised: 15 January 2011

  !

        module solver

        implicit none

  

        contains

  !

  !  tmatrixsoln: calculation of T matrix via solution of interaction equations for

  !  a generalized plane wave expansion

  !

  !

  !  original: 15 January 2011

  !  revised: 21 February 2011: call for sphereqeff changed

  !

           subroutine tmatrixsoln(neqns,nsphere,nodr,nodrt,xsp,rpos,epssoln,epscon,niter,&

                      calctmatrix,tmatrixfile,fftranpresent,niterstep,qext,qabs,qsca,istat)

           use mpidefs

           use mpidata

           use intrinsics

           use numconstants

           use specialfuncs

           use miecoefdata

           use spheredata

           use translation

           use scatprops

           implicit none

           integer :: iter,niter,neqns,nsphere,nodr(nsphere),ntran(nsphere),nodrt, &

                      nodrmax,i,ierr,istat,m,n,p,k,l,q,noff,nblk,ma,na,ka,la,mn,istart,iunit, &

                      rank,iexit(1),calctmatrix,lt,kt,qt,nt,mt,it,nodrtt,lstart(1),numsolns,isoln, &

                      isolnstart,igroup,ngroup,rgrank,lold,grank,nsend,nodrta(1), &

                      fftranpresent,niterstep

           integer, allocatable :: lindex(:),kindex(:)

           real(8) :: eps,err,qext(nsphere),qabs(nsphere),qsca(nsphere),xsp(nsphere),xv, &

                      rpos(3,nsphere),xij(3),qabsklq(nsphere),qscaklq(nsphere),qextklq(nsphere), &

                      f2,qexttot,qabstot,qscatot,qextold(1),qscaold(1),errqe,errqs, &

                      timetran,timesolve,time1,time2,epssoln,epscon,dtemp(4),timeorder,&

                      at1,at2,at3,at4

           real(8) :: qextl(nsphere),qabsl(nsphere),qscal(nsphere)

           real(8), allocatable :: qextgroup(:,:),qabsgroup(:,:),qscagroup(:,:)

           complex(8) :: amnp(neqns),pmnp(neqns),pmnpan(neqns)

           complex(8), allocatable :: pmnp0(:,:,:),ac(:,:,:,:),pmnpt(:,:,:),amnp0(:,:,:), &

                      amnp0group(:,:,:)

           character*30 :: tmatrixfile

           character*4 :: timeunit

           data istart,iexit/1,0/

           rank=base_rank

           rgrank=root_group_rank

           grank=group_rank

           ngroup=number_groups

           call getrunparameters(run_print_unit=iunit)

           xv=(sum(xsp**3.d0))**(1.d0/3.d0)

           nodrmax=maxval(nodr)

           qext=0.d0

           qabs=0.d0

           qsca=0.d0

           qextold=0.d0

           qscaold=0.d0

  !

  !  perform T matrix file operations as needed

  !

           if(rank.eq.0) then

              if(calctmatrix.eq.1) then

                 open(3,file=tmatrixfile)

                 write(3,'(i4)') nodrt

                 lstart(1)=1

              else

                 open(3,file=tmatrixfile)

                 write(iunit,'('' finding end of record to file '',a)') tmatrixfile

                 read(3,*) nodrtt

                 do l=1,nodrt

                    do k=-l,l

                       do q=1,2

                          read(3,'(3i5)',end=20,err=20) lt,kt,qt

                          do n=1,l

                             do m=-n,n

                                read(3,'(2i5,4e17.9)',end=20,err=20) nt,mt,at1,at2,at3,at4

                             enddo

                          enddo

                       enddo

                    enddo

                    do i=1,nsphere

                       read(3,'(i5,3e17.9)',end=20,err=20) it,qextl(i),qabsl(i),qscal(i)

                    enddo

                    qext=qext+qextl

                    qabs=qabs+qabsl

                    qsca=qsca+qscal

                 enddo

  20             close(3)

                 open(3,file=tmatrixfile)

                 qextold(1)=0.d0

                 qabstot=0.d0

                 do i=1,nsphere

                    qextold=qextold+qext(i)*xsp(i)*xsp(i)/xv/xv

                    qabstot=qabstot+qabs(i)*xsp(i)*xsp(i)/xv/xv

                 enddo

                 qscaold(1)=qextold(1)-qabstot

                 lstart(1)=lt

                 read(3,*) nodrtt

                 write(iunit,'('' calculations begin with order '',i5)') lstart(1)

                 do l=1,lstart(1)-1

                    do k=-l,l

                       do q=1,2

                          read(3,'(3i5)') lt,kt,qt

                          do n=1,l

                             do m=-n,n

                                read(3,'(2i5,4e17.9)') nt,mt,at1,at2,at3,at4

                             enddo

                          enddo

                       enddo

                    enddo

                    do i=1,nsphere

                       read(3,'(i5,3e17.9)') it,at1,at2,at3

                    enddo

                 enddo

              endif

           endif

           call ms_mpi(mpi_command='bcast',mpi_send_buf_i=lstart,mpi_number=1,mpi_rank=0)

           call ms_mpi(mpi_command='bcast',mpi_send_buf_dp=qextold,mpi_number=1,mpi_rank=0)

           call ms_mpi(mpi_command='bcast',mpi_send_buf_dp=qscaold,mpi_number=1,mpi_rank=0)

           call ms_mpi(mpi_command='bcast',mpi_send_buf_dp=qext,mpi_number=nsphere,mpi_rank=0)

           call ms_mpi(mpi_command='bcast',mpi_send_buf_dp=qabs,mpi_number=nsphere,mpi_rank=0)

           call ms_mpi(mpi_command='bcast',mpi_send_buf_dp=qsca,mpi_number=nsphere,mpi_rank=0)

           call ms_mpi(mpi_command='barrier')

           allocate(amnp0group(2,nodrt*(nodrt+2),0:ngroup-1),qextgroup(nsphere,0:ngroup-1), &

                    qabsgroup(nsphere,0:ngroup-1),qscagroup(nsphere,0:ngroup-1))

           numsolns=2*nodrt*(nodrt+2)

           allocate(lindex(numsolns),kindex(numsolns))

  !

  !  find the starting point

  !

           i=0

           do l=1,nodrt

              do k=-l,l

                 do q=1,2

                    i=i+1

                    lindex(i)=l

                    kindex(i)=k

                 enddo

              enddo

           enddo

           do i=1,numsolns

              if(lindex(i).eq.lstart(1).and.kindex(i).eq.-lstart(1)) exit

           enddo

           isolnstart=i

           qextl=0.d0

           qabsl=0.d0

           qscal=0.d0

           lold=0

  !

  !  begin the loop over RHS of the interaction equations.   The solutions are distributed

  !  among ngroup groups of processors

  !

           do isoln=isolnstart,numsolns,ngroup

              if(rank.eq.0) timeorder=mytime()

              do igroup=0,ngroup-1

                 l=lindex(isoln+igroup)

                 k=kindex(isoln+igroup)

                 q=mod(isoln+igroup-1,2)+1

  !

  !  calculate the RHS

  !

                 if(l.eq.-k.and.q.eq.1) then

                    if(allocated(ac)) deallocate(ac,amnp0)

                    allocate(ac(2,nodrmax*(nodrmax+2),-l:l,nsphere),amnp0(0:l+1,l,2))

                    do i=1,nsphere

                       xij=rpos(:,i)

                       call gentrancoef(1,xij,(1.d0,0.d0),1,nodrmax,l,l,0,0,ac(1,1,-l,i))

                    enddo

                 endif

                 if(igroup.eq.rgrank) then

                    if(k.le.-1) then

                       ka=l+1

                       la=-k

                    else

                       ka=k

                       la=l

                    endif

                    noff=0

                    do i=1,nsphere

                       nblk=nodr(i)*(nodr(i)+2)*2

                       allocate(pmnp0(0:nodr(i)+1,nodr(i),2))

                       do p=1,2

                          do n=1,nodr(i)

                             do m=-n,n

                                mn=n*(n+1)+m

                                if(m.le.-1) then

                                   ma=n+1

                                   na=-m

                                else

                                   ma=m

                                   na=n

                                endif

                                pmnp0(ma,na,p)=ac(abs(p-q)+1,mn,k,i)

                             enddo

                          enddo

                       enddo

                       pmnp(noff+1:noff+nblk)=reshape(pmnp0,(/nblk/))

                       deallocate(pmnp0)

                       noff=noff+nblk

                    enddo

  !

  !  multiply RHS by mie coefficients

  !

                    call miecoeffmult(1,nsphere,neqns,pmnp,pmnpan)

                    amnp=pmnpan

  !

  !  call the solver

  !

                    if(fftranpresent.eq.1) then

                       call cbicgff(neqns,nsphere,niter,epssoln,pmnpan,amnp,0, &

                            niterstep,iter,err)

                    else

                       call cbicg(neqns,nsphere,niter,epssoln,pmnpan,amnp,0,iter,err)

                    endif

                    if(iter.gt.niter.or.err.gt.epssoln) istat=1

                    call sphereqeff(nsphere,neqns,nodr,nodrmax,xsp,amnp,amnp, &

                           pmnp,pmnp,qextklq,qabsklq,qscaklq)

                    qextgroup(1:nsphere,igroup)=qextklq(1:nsphere)

                    qabsgroup(1:nsphere,igroup)=qabsklq(1:nsphere)

                    qscagroup(1:nsphere,igroup)=qscaklq(1:nsphere)

  !

  !  compute the target-based expansion

  !

                    ntran=l

                    amnp0=0.d0

                    call amncommonorigin(neqns,nsphere,nodr,ntran,l,rpos, &

                                         amnp,amnp0)

                    do n=1,l

                       do m=-n,n

                          if(m.le.-1) then

                             ma=n+1

                             na=-m

                          else

                             ma=m

                             na=n

                          endif

                          mn=n*(n+1)+m

                          do p=1,2

                             amnp0group(p,mn,igroup)=amnp0(ma,na,p)

                          enddo

                       enddo

                    enddo

                 endif

              enddo

  !

  !  send the solutions to the rank 0 processor

  !

              call ms_mpi(mpi_command='barrier')

              if(grank.eq.0) then

                 if(rank.ne.0) then

                    l=lindex(isoln+rgrank)

                    nblk=l*(l+2)

                    nsend=2*nblk

                    call ms_mpi(mpi_command='send',mpi_send_buf_dc=amnp0group(1,1,rgrank),&

                         mpi_number=nsend,mpi_rank=0,mpi_comm=root_group_comm)

                    call ms_mpi(mpi_command='send',mpi_send_buf_dp=qextgroup(1,rgrank),&

                         mpi_number=nsphere,mpi_rank=0,mpi_comm=root_group_comm)

                    call ms_mpi(mpi_command='send',mpi_send_buf_dp=qabsgroup(1,rgrank),&

                         mpi_number=nsphere,mpi_rank=0,mpi_comm=root_group_comm)

                    call ms_mpi(mpi_command='send',mpi_send_buf_dp=qscagroup(1,rgrank),&

                         mpi_number=nsphere,mpi_rank=0,mpi_comm=root_group_comm)

                 else

                    do igroup=1,ngroup-1

                       l=lindex(isoln+igroup)

                       nblk=l*(l+2)

                       nsend=2*nblk

                       call ms_mpi(mpi_command='recv',mpi_recv_buf_dc=amnp0group(1,1,igroup),&

                            mpi_number=nsend,mpi_rank=igroup,mpi_comm=root_group_comm)

                       call ms_mpi(mpi_command='recv',mpi_recv_buf_dp=qextgroup(1,igroup),&

                            mpi_number=nsphere,mpi_rank=igroup,mpi_comm=root_group_comm)

                       call ms_mpi(mpi_command='recv',mpi_recv_buf_dp=qabsgroup(1,igroup),&

                            mpi_number=nsphere,mpi_rank=igroup,mpi_comm=root_group_comm)

                       call ms_mpi(mpi_command='recv',mpi_recv_buf_dp=qscagroup(1,igroup),&

                            mpi_number=nsphere,mpi_rank=igroup,mpi_comm=root_group_comm)

                    enddo

                 endif

              endif

              call ms_mpi(mpi_command='barrier')

  !

  !  write results, check for convergence

  !

              if(rank.eq.0) then

                 do igroup=0,ngroup-1

                    l=lindex(isoln+igroup)

                    k=kindex(isoln+igroup)

                    q=mod(isoln+igroup-1,2)+1

                    qextl=qextl+qextgroup(1:nsphere,igroup)

                    qabsl=qabsl+qabsgroup(1:nsphere,igroup)

                    qscal=qscal+qscagroup(1:nsphere,igroup)

                    qext=qext+qextgroup(1:nsphere,igroup)

                    qabs=qabs+qabsgroup(1:nsphere,igroup)

                    qsca=qsca+qscagroup(1:nsphere,igroup)

                    write(3,'(3i5)') l,k,q

                    do n=1,l

                       do m=-n,n

                          mn=n*(n+1)+m

                          write(3,'(2i5,4e17.9)') n,m,amnp0group(1,mn,igroup), &

                                                  amnp0group(2,mn,igroup)

                       enddo

                    enddo

                    if(istart.eq.1.and.igroup.eq.0) then

                       time1=mytime()-timeorder

                       call timewrite(iunit,' time per group solution:',time1)

                       time2=time1*dble(numsolns-isolnstart)/dble(ngroup)

                       call timewrite(iunit,' estimated t matrix calcuation time:',time2)

                       write(iunit,'(''  n   # its  qext         qabs'',&

                           &''         qsca      error     est. time rem.'')')

                       call flush(iunit)

                       istart=0

                    endif

                    if(igroup.eq.0) then

                       timeorder=mytime()-timeorder

                       time2=timeorder*dble(numsolns-isoln)/dble(ngroup)

                       if(time2.gt.3600.d0) then

                          time2=time2/3600.d0

                          timeunit=' hrs'

                       elseif(time2.gt.60.d0) then

                          time2=time2/60.d0

                          timeunit=' min'

                       else

                          timeunit=' sec'

                       endif

                    endif

                    iexit(1)=0

                    if(k.eq.l.and.q.eq.2) then

                       qexttot=0.d0

                       qabstot=0.d0

                       do i=1,nsphere

                          qexttot=qexttot+qext(i)*xsp(i)*xsp(i)/xv/xv

                          qabstot=qabstot+qabs(i)*xsp(i)*xsp(i)/xv/xv

                          write(3,'(i5,3e17.9)') i,qextl(i),qabsl(i),qscal(i)

                       enddo

                       qextl=0.d0

                       qabsl=0.d0

                       qscal=0.d0

                       qscatot=qexttot-qabstot

                       errqe=qexttot-qextold(1)

                       errqs=qscatot-qscaold(1)

                       err=max(errqe,errqs)

                       write(iunit,'(i4,i5,4e13.5,f8.2,a4)') l,iter,qexttot,qabstot, &

                          qscatot,err,time2,timeunit

                       call flush(iunit)

                       qextold(1)=qexttot

                       qscaold(1)=qscatot

                       if(err.le.epscon) iexit(1)=1

                    endif

                    if(iexit(1).eq.1) then

                       nodrt=l

                       exit

                    endif

                 enddo

              endif

              call ms_mpi(mpi_command='bcast',mpi_send_buf_i=iexit,mpi_number=1,mpi_rank=0)

              call ms_mpi(mpi_command='barrier')

              if(iexit(1).eq.1) then

  !

  !  solution has converged

  !

                 deallocate(amnp0group,qextgroup,qabsgroup,qscagroup,lindex,kindex,ac,amnp0)

                 nodrta(1)=nodrt

                 call ms_mpi(mpi_command='bcast',mpi_send_buf_i=nodrta,mpi_number=1,mpi_rank=0)

                 nodrt=nodrta(1)

                 if(rank.eq.0) then

                    write(iunit,'('' T matrix converged, order:'',i5)') nodrt

                    close(3)

                    open(3,file=tmatrixfile,form='formatted',access='direct',recl=4)

                    write(3,'(i4)',rec=1) nodrt

                    close(3)

                 endif

                 return

              endif

           enddo

           deallocate(amnp0group,qextgroup,qabsgroup,qscagroup,lindex,kindex,ac,amnp0)

           if(rank.eq.0) then

              write(*,'('' T matrix did not converge to set epsilon'')')

              close(3)

           endif

           end subroutine tmatrixsoln

  !

  !  solution of interaction equations for a fixed orientation

  !

  !

  !  original: 15 January 2011

  !  revised: 21 February 2011: modification of efficiency calculation, to calculate

  !           polarized components

  !  30 March 2011: took out gbfocus argument: this is not needed since positions are defined

  !  relative to the gb focus.

  !  20 April 2011: used 2-group MPI formulation

  !

           subroutine fixedorsoln(neqns,nsphere,nodr,alpha,beta,cbeam,xsp,rpos,&

                      eps,epstran,niter,amnp,qext,qabs,qsca,maxerr,maxiter,iterwrite, &

                      fftranpresent,niterstep,istat)

           use mpidefs

           use mpidata

           use intrinsics

           use numconstants

           use specialfuncs

           use miecoefdata

           use translation

           use scatprops

           implicit none

           integer :: iter,niter,neqns,nodrmax,k,nsphere,i,ierr,istat,rank,maxiter,iterwrite

           integer :: nodr(nsphere),m1,n1,p,rgrank,grank,ngroup,sendrank,numprocs, &

                      fftranpresent,niterstep

           real(8) :: alpha,beta,eps,err,qext(nsphere,3),maxerr,&

                      qabs(nsphere,3),qsca(nsphere,3),cbeam,gbfocus(3),epstran

           real(8) :: xsp(nsphere), rpos(3,nsphere),maxerra(1)

           complex(8) :: amnp(neqns,2)

           complex(8), allocatable :: pmnp(:,:),pmnpan(:)

           rank=base_rank

           rgrank=root_group_rank

           grank=group_rank

           ngroup=number_groups

           numprocs=number_proc

           sendrank=numprocs/2

           nodrmax=maxval(nodr)

           allocate(pmnp(neqns,2))

           gbfocus=0.d0

           if(cbeam.eq.0.d0) then

              call sphereplanewavecoef(nsphere,neqns,nodr,nodrmax,alpha,beta,rpos,pmnp)

           else

              call spheregaussianbeamcoef(nsphere,neqns,nodr,alpha,beta,cbeam, &

                      rpos,gbfocus,epstran,pmnp)

           endif

           istat=0

           maxiter=0

           maxerr=0.

  !

  !  calculate the two solutions

  !

           allocate(pmnpan(neqns))

           if(ngroup.eq.1) then

              do k=1,2

                 call miecoeffmult(1,nsphere,neqns,pmnp(1,k),pmnpan)

                 amnp(1:neqns,k)=pmnpan(1:neqns)

                 if(fftranpresent.eq.1) then

                    call cbicgff(neqns,nsphere,niter,eps,pmnpan,amnp(1,k),iterwrite, &

                                 niterstep,iter,err)

                 else

                    call cbicg(neqns,nsphere,niter,eps,pmnpan,amnp(1,k),iterwrite, &

                                 iter,err)

                 endif

                 maxiter=max(iter,maxiter)

                 maxerr=max(err,maxerr)

                 if(iter.gt.niter.or.err.gt.eps) istat=1

                 call ms_mpi(mpi_command='barrier')

              enddo

           else

              k=rgrank+1

              call miecoeffmult(1,nsphere,neqns,pmnp(1,k),pmnpan)

              amnp(1:neqns,k)=pmnpan(1:neqns)

              if(fftranpresent.eq.1) then

                 call cbicgff(neqns,nsphere,niter,eps,pmnpan,amnp(1,k),iterwrite, &

                              niterstep,iter,err)

              else

                 call cbicg(neqns,nsphere,niter,eps,pmnpan,amnp(1,k),iterwrite, &

                              iter,err)

              endif

              maxiter=max(iter,maxiter)

              maxerr=max(err,maxerr)

              call ms_mpi(mpi_command='barrier')

              call ms_mpi(mpi_command='bcast',mpi_send_buf_dc=amnp(1,2),&

                    mpi_number=neqns,mpi_rank=sendrank)

           endif

           deallocate(pmnpan)

  !

  !  efficiency factor calculations

  !

           call sphereqeff(nsphere,neqns,nodr,nodrmax,xsp,amnp(1,1),amnp(1,1), &

                        pmnp(1,1),pmnp(1,1),qext(1,1),qabs(1,1),qsca(1,1))

           call sphereqeff(nsphere,neqns,nodr,nodrmax,xsp,amnp(1,2),amnp(1,2), &

                        pmnp(1,2),pmnp(1,2),qext(1,2),qabs(1,2),qsca(1,2))

           call sphereqeff(nsphere,neqns,nodr,nodrmax,xsp,amnp(1,1),amnp(1,2), &

                        pmnp(1,1),pmnp(1,2),qext(1,3),qabs(1,3),qsca(1,3))

           call ms_mpi(mpi_command='barrier')

           deallocate(pmnp)

           end subroutine fixedorsoln

  !

  ! hybrid bcgm, using far field translation

  ! november 2011

  !

           subroutine cbicgff(neqns,nsphere,niter,eps,pnp,anp,iterwrite,niterstep,iter,err)

           use mpidefs

           use mpidata

           use intrinsics

           use spheredata

           use miecoefdata

           use numconstants

           use specialfuncs

           use translation

           implicit none

           integer :: neqns,niter,iter,nsphere,writetime,nodr(nsphere),noff(nsphere+1),&

                      nblk(nsphere),rank,ierr,iexit,i,j,m,n,p,iunit,iterwrite,ip1,ip2, &

                      np1,np2,nsend,numprocs,grank,istore,itermax,istep,niterstep

           real(8) :: eps,err,erra(1),enorm,time1,time2,epsstep,errstep

           complex(8)  :: pnp(neqns),anp(neqns),gnp(neqns),gnpold(neqns),pgnp(neqns), &

                          cr(neqns)

           data writetime/0/

           rank=base_rank

           grank=group_rank

           numprocs=proc_per_group

           call getmiedata(sphere_order=nodr,sphere_block=nblk,sphere_block_offset=noff)

           if(rank.eq.0) then

              call getrunparameters(run_print_unit=iunit)

           endif

           err=0.d0

           iter=0

           enorm=dot_product(pnp,pnp)

           gnpold=0.d0

           if(enorm.eq.0.d0) return

           gnp=0.d0

           ip1=mpi_sphere_index(grank)+1

           ip2=mpi_sphere_index(grank)+mpi_sphere_number(grank)

           do i=ip1,ip2

              do j=1,nsphere

                 if(i.ne.j) then

                    call fftranslationerror(anp(noff(j)+1:noff(j)+nblk(j)), &

                     gnp(noff(i)+1:noff(i)+nblk(i)),j,i,nodr(j),nodr(i))

                 endif

              enddo

           enddo

           do i=0,numprocs-1

              ip1=mpi_sphere_index(i)+1

              ip2=mpi_sphere_index(i)+mpi_sphere_number(i)

              np1=noff(ip1)+1

              np2=noff(ip2)+nblk(ip2)

              nsend=np2-np1+1

              call ms_mpi(mpi_command='bcast',mpi_send_buf_dc=gnp(np1:np2),mpi_number=nsend, &

                   mpi_rank=i,mpi_comm=group_comm)

           enddo

           call miecoeffmult(1,nsphere,neqns,gnp,gnp)

  

           iter=0

           epsstep=eps

           istep=0

           do

              istep=istep+1

              gnpold=gnp

              pgnp=pnp+gnp

              call cbicg(neqns,nsphere,niterstep,epsstep,pgnp,anp,0,itermax,errstep)

              iter=iter+min(itermax,niterstep)

              gnp=0.d0

              ip1=mpi_sphere_index(grank)+1

              ip2=mpi_sphere_index(grank)+mpi_sphere_number(grank)

              do i=ip1,ip2

                 do j=1,nsphere

                    if(i.ne.j) then

                       call fftranslationerror(anp(noff(j)+1:noff(j)+nblk(j)), &

                        gnp(noff(i)+1:noff(i)+nblk(i)),j,i,nodr(j),nodr(i))

                    endif

                 enddo

              enddo

              do i=0,numprocs-1

                 ip1=mpi_sphere_index(i)+1

                 ip2=mpi_sphere_index(i)+mpi_sphere_number(i)

                 np1=noff(ip1)+1

                 np2=noff(ip2)+nblk(ip2)

                 nsend=np2-np1+1

                 call ms_mpi(mpi_command='bcast',mpi_send_buf_dc=gnp(np1:np2),mpi_number=nsend, &

                      mpi_rank=i,mpi_comm=group_comm)

              enddo

              call miecoeffmult(1,nsphere,neqns,gnp,gnp)

              err=dot_product(gnp-gnpold,gnp-gnpold)/enorm

              if(rank.eq.0.and.iterwrite.eq.1) then

                 write(iunit,'('' step,iteration,bcgm err,correc err:'',2i5,2e13.5)') &

                             istep,iter,errstep,err

                 call flush(iunit)

              endif

              epsstep=eps

              err=max(err,errstep)

              if((err.lt.eps).or.iter.gt.niter) exit

           enddo

           end subroutine cbicgff

  

  !

  ! iteration solver

  ! generalized complex biconjugate gradient method

  ! original code: Piotr Flatau, although not much remains.

  ! specialized to the multiple sphere problem

  !

  !

  !  last revised: 15 January 2011

  !  october 2011: translation calls modified

  !

           subroutine cbicg(neqns,nsphere,niter,eps,pnp,anp,iterwrite,iter,err)

           use mpidefs

           use mpidata

           use intrinsics

           use spheredata

           use miecoefdata

           use numconstants

           use specialfuncs

           use translation

           implicit none

           integer :: neqns,niter,iter,nsphere,writetime,nodr(nsphere),noff(nsphere+1),&

                      nblk(nsphere),rank,ierr,iexit,i,j,m,n,p,iunit,iterwrite,ip1,ip2, &

                      np1,np2,nsend,numprocs,grank,istore

           real(8) :: eps,err,erra(1),enorm,time1,time2

           complex(8)  :: pnp(neqns),anp(neqns)

           complex(8) :: cak(1),csk,cbk,csk2(1)

           complex(8) :: cr(neqns),cp(neqns),cw(neqns),cq(neqns),cap(neqns),caw(neqns), &

                         crt(neqns),capt(neqns),cawt(neqns),ccw(neqns)

           data writetime/0/

           rank=base_rank

           grank=group_rank

           numprocs=proc_per_group

           call getmiedata(sphere_order=nodr,sphere_block=nblk,sphere_block_offset=noff)

           ip1=mpi_sphere_index(grank)+1

           ip2=mpi_sphere_index(grank)+mpi_sphere_number(grank)

           np1=noff(ip1)+1

           np2=noff(ip2)+nblk(ip2)

           nsend=np2-np1+1

           crt=0.d0

           iexit=0

           if(rank.eq.0) then

              call getrunparameters(run_print_unit=iunit)

           endif

           err=0.d0

           iter=0

           enorm=dot_product(pnp,pnp)

           cr=0.d0

           if(enorm.eq.0.d0) return

           do i=ip1,ip2

              do j=1,nsphere

                 if(i.ne.j) then

                    call rottranjtoi(anp(noff(j)+1:noff(j)+nblk(j)), &

                         cr(noff(i)+1:noff(i)+nblk(i)),j,i,nodr(j),nodr(i),1,1)

                 endif

              enddo

           enddo

           do i=0,numprocs-1

              ip1=mpi_sphere_index(i)+1

              ip2=mpi_sphere_index(i)+mpi_sphere_number(i)

              np1=noff(ip1)+1

              np2=noff(ip2)+nblk(ip2)

              nsend=np2-np1+1

              call ms_mpi(mpi_command='bcast',mpi_send_buf_dc=cr(np1:np2),mpi_number=nsend, &

                   mpi_rank=i,mpi_comm=group_comm)

           enddo

           call miecoeffmult(1,nsphere,neqns,cr,cr)

           cr=pnp-anp+cr

           cq=conjg(cr)

           cw=cq

           cp=cr

           csk=dot_product(conjg(cr),cr)

           if(cdabs(csk).eq.0.d0) return

  !

  !  here starts the main iteration loop

  !

           do iter=1,niter

              ip1=mpi_sphere_index(grank)+1

              ip2=mpi_sphere_index(grank)+mpi_sphere_number(grank)

              np1=noff(ip1)+1

              np2=noff(ip2)+nblk(ip2)

              nsend=np2-np1+1

              cak(1)=(0.d0,0.d0)

              cawt=(0.d0,0.d0)

              capt=(0.d0,0.d0)

              if(rank.eq.0) then

                 if(writetime.eq.0) time1=mytime()

              endif

              ccw=conjg(cw)

              cap=0.d0

              caw=0.d0

              call miecoeffmult(1,nsphere,neqns,ccw,ccw)

              do i=ip1,ip2

                 do j=1,nsphere

                    if(i.ne.j) then

                       call rottrantwojtoi(cp(noff(j)+1:noff(j)+nblk(j)), &

                         ccw(noff(j)+1:noff(j)+nblk(j)), &

                         cap(noff(i)+1:noff(i)+nblk(i)), &

                         caw(noff(i)+1:noff(i)+nblk(i)), &

                         j,i,nodr(j),nodr(i))

  !                     call rottranjtoi(cp(noff(j)+1:noff(j)+nblk(j)), &

  !                       cap(noff(i)+1:noff(i)+nblk(i)),j,i,nodr(j),nodr(i),1,1)

  !                     call rottranjtoi(ccw(noff(j)+1:noff(j)+nblk(j)), &

  !                       caw(noff(i)+1:noff(i)+nblk(i)),j,i,nodr(j),nodr(i),-1,-1)

                    endif

                 enddo

              enddo

              call miecoeffmult(ip1,ip2,neqns,cap,cap)

              cap(np1:np2)=cp(np1:np2)-cap(np1:np2)

              caw(np1:np2)=cw(np1:np2)-conjg(caw(np1:np2))

              cak(1)=dot_product(cw(np1:np2),cap(np1:np2))

              call ms_mpi(mpi_command='allreduce',mpi_recv_buf_dc=cak,mpi_number=1, &

                      mpi_operation=ms_mpi_sum,mpi_comm=group_comm)

              cak(1)=csk/cak(1)

              anp(np1:np2)=anp(np1:np2)+cak(1)*cp(np1:np2)

              cr(np1:np2)=cr(np1:np2)-cak(1)*cap(np1:np2)

              cq(np1:np2)=cq(np1:np2)-conjg(cak(1))*caw(np1:np2)

              csk2(1)=dot_product(cq(np1:np2),cr(np1:np2))

              err=dot_product(cr(np1:np2),cr(np1:np2))

              erra(1)=err

              call ms_mpi(mpi_command='allreduce',mpi_recv_buf_dc=csk2,mpi_number=1, &

                      mpi_operation=ms_mpi_sum,mpi_comm=group_comm)

              call ms_mpi(mpi_command='allreduce',mpi_recv_buf_dp=erra,mpi_number=1, &

                      mpi_operation=ms_mpi_sum,mpi_comm=group_comm)

              err=erra(1)

              err=err/enorm

              if(err.lt. eps) exit

              cbk=csk2(1)/csk

              cp(np1:np2)=cr(np1:np2)+cbk*cp(np1:np2)

              cw(np1:np2)=cq(np1:np2)+conjg(cbk)*cw(np1:np2)

              csk=csk2(1)

              do i=0,numprocs-1

                 ip1=mpi_sphere_index(i)+1

                 ip2=mpi_sphere_index(i)+mpi_sphere_number(i)

                 np1=noff(ip1)+1

                 np2=noff(ip2)+nblk(ip2)

                 nsend=np2-np1+1

                 call ms_mpi(mpi_command='bcast',mpi_send_buf_dc=cp(np1:np2),mpi_number=nsend, &

                      mpi_rank=i,mpi_comm=group_comm)

                 call ms_mpi(mpi_command='bcast',mpi_send_buf_dc=cw(np1:np2),mpi_number=nsend, &

                      mpi_rank=i,mpi_comm=group_comm)

              enddo

              if(rank.eq.0.and.iter.eq.1.and.writetime.eq.0) then

                 time2=mytime()-time1

                 call timewrite(iunit,' time per iteration:',time2)

                 writetime=1

              endif

              if(rank.eq.0.and.iterwrite.eq.1) then

                  write(iunit,'('' iter, err:'',i5,e13.5)') iter,err

                  call flush(iunit)

              endif

           enddo

  !

  !  arrive here with a converged solution

  !

           do i=0,numprocs-1

              ip1=mpi_sphere_index(i)+1

              ip2=mpi_sphere_index(i)+mpi_sphere_number(i)

              np1=noff(ip1)+1

              np2=noff(ip2)+nblk(ip2)

              nsend=np2-np1+1

              call ms_mpi(mpi_command='bcast',mpi_send_buf_dc=anp(np1:np2),mpi_number=nsend, &

                      mpi_rank=i,mpi_comm=group_comm)

           enddo

           end subroutine cbicg

  

        end module solver