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
|
/*********************************************************************************
** **
** Program : solvate.c (c) 1996-2010 by Helmut Grubmuller, Gottingen **
** **
*********************************************************************************/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <malloc.h>
#define PI 3.14159265358979323846
#define XPLOR_SCRIPT_NAME "mkpsf.inp"
#define XPLOR_TOPOLOGY_FILE_WATER "toph19.sol"
#define XPLOR_PARAMETER_FILE_WATER "param19.sol"
#define XPLOR_TOPOLOGY_FILE_IONS "toph19.nacl"
#define XPLOR_PARAMETER_FILE_IONS "param19.nacl"
#define MAX_STRING_SIZE 255
#define MAX_NR_OF_PDB_ATOMS 999999
#define MAX_NR_OF_PDB_GROUPS 9999
#define MAX_NR_OF_PDB_SEGMENTS 999
#define SURFACE_LEVEL 0.25
#define TIMAX_FACTOR 10
#define SIG_H2O 3.04166
#define BOND_LENGTH_H2O 0.9572 /* as in XPLOR TIP3W; otherwise problems with SHAKE */
#define BOND_ANGLE_H2O (105.0*PI/180.0)
#define INITIAL_STEP_WIDTH_FOR_CALC_BOUNDING_SPHERE 1.0
#define STEP_WIDTH_DECREASE_FOR_CALC_BOUNDING_SPHERE 1.25
#define MINIMAL_STEP_WIDTH_FOR_CALC_BOUNDING_SPHERE 0.000001
#define MAX_NR_OF_GRID_POINTS_FACTOR 1.2
#define ANGLE_STEP_FOR_CUT_AWAY 5.0
#define NR_OF_DICE_DATA 100000
#define FACTOR_FOR_INITIAL_LAMBDA_FOR_LEARNS_GAUSSIANS 2.0
#define ENTROPY_AVERAGE_LENGTH 1000.0
#define NOISE_FOR_LEARN_CODEBOOK_POSITIONS 0.1
#define RHOAVERSTART_FACTOR 2.0
#define RHOAVEREND_FACTOR (1.0/20.0)
#define EPS_CONST_FOR_LEARN_POSITIONS 0.02
#define EPS_CONST_FOR_LEARN_DIRECTIONS 0.01
#define EPS_CONST_FOR_LEARN_VARIANCES 0.01
#define EPS_START_FOR_LEARN_DIRECTIONS 0.01
#define EPS_END_FOR_LEARN_DIRECTIONS 0.0001
#define MU_FOR_LEARN_VARIANCES_INDIVIDUAL 0.1
#define RESOLUTION_FOR_GET_MIN_DISTANCE 1.0
#define SCALE_FACTOR_FOR_GET_MIN_DISTANCE 100.0
#define ACCURACY_OF_GET_MIN_DISTANCE 0.01
#define INITIAL_RHOFAC 5.0
#define INITIAL_RHOFAC_SHRINK_FACTOR 0.5
#define ACCURACY_OF_ADJUST_SURFACE_DISTANCE 0.05
#define ADD_LAYER_FOR_CREATE_VOLUME 10.0
#define INITIAL_STEPSIZE_FOR_SURFACE_STATISTICS 0.1
#define ACCURACY_FOR_SURFACE_STATISTICS 0.00001
#define DISTANCE_TIMES_WATER_SHELL_THICKNESS 2.0
#define BOUNDARY_SURFACE_THICKNESS_FACTOR 0.05
#define INITIAL_STEPSIZE_FOR_PLACE_WATER 0.5
#define FINAL_STEPSIZE_FOR_PLACE_WATER 0.001
#define DEFAULT_WATER_SHELL_THICKNESS 10.0
#define DEFAULT_BOUNDARY_RADIUS 100000.0
#define DEFAULT_NR_OF_GAUSSIANS 1
#define MAXNWATER_FACTOR 1.0
#define KT (1.380662e-23 * 300.0)
#define ANGSTROM_PER_METER 1.0e10
#define ELEMENTARY_CHARGE 1.6021892e-19
#define AVOGADRO 6.022045e23
#define ISOTONIC_CONCENTRATION 0.154 /* [mol/liter] */
#define LITER_PER_CUBIC_METER 1000.0
#define DEBYE_HUECKEL_LENGTH (ANGSTROM_PER_METER/sqrt(2.0*AVOGADRO*ISOTONIC_CONCENTRATION*LITER_PER_CUBIC_METER*ELEMENTARY_CHARGE*ELEMENTARY_CHARGE/8.85418782e-12/80.0/KT)) /* [A] */
#define ISOTONIC_WATER_PER_ION 361.0
#define FOUR_PI_EPS_EPSR (4.0*PI*8.85418782e-12*80.0)
#define NR_OF_DEBYE_HUECKEL_MONTE_CARLO_MOVES 2000000
#define PDB_P_ATOMNR 5
#define PDB_L_ATOMNR 6
#define PDB_P_ATOMSTR 12
#define PDB_L_ATOMSTR 4
#define PDB_P_RESIDSTR 17
#define PDB_L_RESIDSTR 4
#define PDB_P_RESIDNR 22
#define PDB_L_RESIDNR 5
#define PDB_P_XKO 30
#define PDB_L_XKO 8
#define PDB_P_YKO 38
#define PDB_L_YKO 8
#define PDB_P_ZKO 46
#define PDB_L_ZKO 8
#define PDB_P_BETA 54
#define PDB_L_BETA 6
#define PDB_P_HARMON 60
#define PDB_L_HARMON 6
#define PDB_P_SEGIDSTR 72
#define PDB_L_SEGIDSTR 4
#define PSF_P_ATOMNR 0
#define PSF_L_ATOMNR 8
#define PSF_P_SEGIDSTR 9
#define PSF_L_SEGIDSTR 4
#define PSF_P_RESIDNR 13
#define PSF_L_RESIDNR 6
#define PSF_P_RESIDSTR 19
#define PSF_L_RESIDSTR 4
#define PSF_P_ATOMSTR 24
#define PSF_L_ATOMSTR 4
#define PSF_P_ATOMTYPSTR 29
#define PSF_L_ATOMTYPSTR 4
#define PSF_P_CHARGE 34
#define PSF_L_CHARGE 15
#define PSF_P_MASS 50
#define PSF_L_MASS 9
/**************** functions ***************************************************/
static void error(int err_nr);
static void terminate(void);
static void here(int here_nr);
static void skip_line(FILE *fp);
static int read_string(FILE *f,char *str);
static int strup(char *str);
static int erase_spaces(char *str);
static int read_next_pdbdata(FILE *fh,int *atom_nr,double *ko,double *beta,double *harmon,char *atomstr,char *residstr,int *residnr,char *segidstr);
static int patch_pdb_coords(FILE *fh,FILE *fh1,double x,double y,double z);
static void add_pdb_tip(FILE *fp,int flag,double x,double y,double z,long int atom_nr,int res_id,int seg_id);
static void add_pdb_ion(FILE *fp,int iontype,double x,double y,double z,long int atom_nr,int res_id);
static int goto_psf_atomdata(FILE *h_psf);
static int read_next_psfatom(FILE *fh,int *atom_nr,char *atom_typestr,double *charge,double *mass,char *atomstr,char *residstr,int *residnr,char *segidstr);
static void read_pdb_file(char *infile);
static void read_atom_charges_from_psf_file(char *infile);
static void calc_bounding_sphere(void);
static void cut_away(double theta, double phi);
static void create_convex_volume(void);
static double calc_maxdist(double xr, double yr, double zr);
static void dice_data(void);
static void do_pca(void);
static void init_codebook_positions(void);
static void init_codebook_directions(void);
static void init_codebook_variances(void);
static void compute_activities(void);
static void learn_codebook_positions(double eps);
static void learn_codebook_directions(double eps);
static void learn_codebook_variances_soft_individual(double eps, double mu);
static void learn_codebook_variances_soft_global(double eps, double mu);
static void learn_codebook_variances_hard(double eps);
static void equalize_codebook_variances(void);
static void outputs(void);
static double get_rhoaver2(double rhoaverstart, double rhoaverend, long int timax, long int ti);
static double get_eps(double epsstart, double epsend, long int timax, long int ti);
static void learn_gaussians(void);
static double density_varrho(double x, double y, double z, double rhofac);
static double density(double x, double y, double z);
static double density_gradient(double x, double y, double z, double *gx, double *gy, double *gz);
static double density_grad_curve(double x, double y, double z, double *gx, double *gy, double *gz, double *g, double *cr, double *curvature);
static double get_min_distance(double rhofac, double level);
static void adjust_surface_distance(void);
static void create_volume(void);
static void save_gaussian_parameters(void);
static void save_boundary_parameters(void);
static void surface_statistics(void);
static void place_ions(void);
static void dice_hydrogens(double x0, double y0, double z0, double *x1, double *y1, double *z1, double *x2, double *y2, double *z2);
static double vdw_energy_force(double x, double y, double z, double *fx, double *fy, double *fz);
static void place_water_molecules(void);
static void check_max_atom_nr(long int nr);
static void check_max_segment_nr(int nr);
static void write_xplor_segment(FILE *fp, int segid, long int nr, int diff);
static void write_xplor_segment_na(FILE *fp, long int nr);
static void write_xplor_segment_cl(FILE *fp, long int nr);
static void write_water_molecules(char *infile, char *outfile);
static void group_water_molecules(void);
void *malloc(size_t size);
void *realloc(void* ptr, size_t size);
void free(void *ptr);
static int *ivector(int nl, int nh);
static void free_ivector(int *v, int nl, int nh);
static double *dvector(int nl, int nh);
static void free_dvector(double *v, int nl, int nh);
static double **dmatrix(int nrl, int nrh, int ncl, int nch);
static void free_dmatrix(double **m, int nrl, int nrh, int ncl, int nch);
static double ***dcube(int nrl, int nrh, int ncl, int nch, int npl, int nph);
static void free_dcube(double ***m, int nrl, int nrh, int ncl, int nch, int npl, int nph);
static void tred2(double **a,int n,double *d,double *e);
static void tqli(double *d,double *e,int n,double **z);
static void sort(int n, double *ra);
static void sort2(int n, double *ra, double *rb);
static void sort5(int n, double *ra, double *rb, double *rc, double *rd, double *re);
static int *alloc_ints(int len);
static double *alloc_doubles(int len);
static int **alloc_ptrs(int len);
static double ran2(long *idum);
static double rang(void);
static void bad_input(void);
/**************** variables ***************************************************/
static int ndata;
static int ncodebook;
static int natoms;
static int nr_of_water_groups;
static long int nwater;
static long int maxnwater;
static long int nions;
static long int nbulkwater;
static long int ngrid,ngrid1;
static long int ti;
static long int timax;
static long int seed;
static long int max_atom_nr_in_pdb;
static double total_charge;
static double eps,epsstart,epsend,min_rhoaver2,rhoaver2,rhoav2,temp,distmin,rho0;
static double vol2,atot,prod,prod1,prod2,rhoaverstart,rhoaverend;
static double min_entropy,entropy,enttfac;
static double a_vdw,b_vdw;
static double x_sphere;
static double y_sphere;
static double z_sphere;
static double r_sphere;
static double boundary_radius;
static double water_shell_thickness;
static double *xdata,*ydata,*zdata;
static double *gauss_xm;
static double *lambda;
static double *gauss_dist;
static double **ev;
static double **cov;
static double **gauss_y; /* codebook (r) vectors gauss_y[][r] */
static double ***gauss_w; /* codebook (r) directions w[][j][r] (j-th ev) */
static double **gauss_rho2; /* codebook (r) variances rho2[][r] */
static double *gauss_vol;
static double *gauss_height;
static double *p; /* codebook (r) weight p[r] */
static double *g; /* unnormalized activities g[r] */
static double *a; /* normalized activities a[r] */
static double *xx,*xt,*xtt;
static double *xwater;
static double *ywater;
static double *zwater;
static double *xatom;
static double *yatom;
static double *zatom;
static double *qatom;
static double *sigatom;
static double *epsatom;
static double *a_vdw_atom;
static double *b_vdw_atom;
static double *xgrid;
static double *ygrid;
static double *zgrid;
static double *dgrid;
static double *densgrid;
static int *cutflags;
static double *distgrid;
static double *sdistgrid;
static double *gradgrid;
static double *curvegrid;
static double *curvaturegrid;
static int *replace_water_by_ion;
static int *groupflags;
static int write_surface_statistics_flag;
static int write_volume_statistics_flag;
static int use_gaussians_flag;
static int use_boundary_flag;
static int output_bulk_water_only_flag;
static int no_solute_present_flag;
static int output_no_solute_flag;
static int add_ions_flag;
static int output_mkpsf_script;
static int water_shell_thickness_flag;
static int ncodebook_flag;
static int enforced_total_charge_flag;
static int enforced_total_charge;
static void error(int errornr)
{
printf("error nr. %d\n",errornr);
exit(1);
}
static void terminate()
{
fprintf(stderr,"\n\nSolvate terminated with fatal error.\n\n");
exit(1);
}
static void here(int herenr)
{
printf("here %d\n",herenr);
return;
}
static void skip_line(FILE *fp)
{
fgetc(fp);
for ( ; fgetc(fp)!='\n' ; ) ;
return;
}
static int read_string(FILE *f,char *str)
{
int i,ch;
i=0;
str[0]=0;
do {
ch=fgetc(f);
if ((ch==EOF)||(ch=='\n')) {
str[i]=0;
if (ch==EOF) return -1;
else return 0;
} else {
str[i++]= (char)ch;
}
if (i>=MAX_STRING_SIZE-2) {
fprintf(stderr,"\n\nERROR: Line too long for string-buffer (read_string).\n");
fprintf(stderr,"Actual stringbuffer-size is %d.\n",MAX_STRING_SIZE);
str[i]=0;
return -1;
}
} while (ch!=EOF);
str[i]=0;
return -1;
}
static int strup(char *str)
{
int i=0;
int toupper(int i);
while (str[i]!=0) {
str[i]=(char)toupper((int)str[i]);
i++;
}
return 0;
}
static int erase_spaces(char *str)
{
int i,slen=(int)strlen(str);
int j,k;
for (i=0;i<slen;i++) {
if (str[i]!=32) break;
}
if (str[i]==0) {
str[0]=0;
return 0;
}
if (i==0) return 0;
for (j=i,k=0;j<slen;j++,k++) {
str[k]=str[j];
}
for(j=0;j<i;j++) {
str[slen-1-j]=32;
}
return 0;
}
/* read_next_pdbdata is from the EGO_VIII-distribution, (c) M. Eichinger */
static int read_next_pdbdata(FILE *fh,int *atom_nr,double *ko,double *beta,double *harmon,char *atomstr,char *residstr,int *residnr,char *segidstr)
{
int res;
double resd;
char linestr[MAX_STRING_SIZE+1];
char tmpstr[MAX_STRING_SIZE+1];
/* search for next line starting with 'ATOM' */
while (read_string(fh,linestr)!=-1) {
strup(linestr);
strncpy(tmpstr,linestr,4);
tmpstr[4]=0;
if (strcmp(tmpstr,"ATOM")==0) goto perform_atom_data; /* found one */
tmpstr[3]=0;
if (strcmp(tmpstr,"END")==0) return -1;
}
return -1;
perform_atom_data:
/********** get the atom-number ******/
strncpy(tmpstr,linestr+PDB_P_ATOMNR,PDB_L_ATOMNR);
tmpstr[PDB_L_ATOMNR]=0;
if (sscanf(tmpstr,"%d",&res)!=1) goto illegal_pdb_line;
if (res<1) goto illegal_pdb_line;
*atom_nr=res;
/********** get the atom-string *************/
strncpy(tmpstr,linestr+PDB_P_ATOMSTR,PDB_L_ATOMSTR);
tmpstr[PDB_L_ATOMSTR]=0;
erase_spaces(tmpstr);
strcpy(atomstr,tmpstr);
/********** get the resid-string *************/
strncpy(tmpstr,linestr+PDB_P_RESIDSTR,PDB_L_RESIDSTR);
tmpstr[PDB_L_RESIDSTR]=0;
erase_spaces(tmpstr);
strcpy(residstr,tmpstr);
/********** get the resid-number *************/
strncpy(tmpstr,linestr+PDB_P_RESIDNR,PDB_L_RESIDNR);
tmpstr[PDB_L_RESIDNR]=0;
if (sscanf(tmpstr,"%d",&res)!=1) goto illegal_pdb_line;
if (res<1) goto illegal_pdb_line;
*residnr=res;
/********* get the x-coordinate ************/
strncpy(tmpstr,linestr+PDB_P_XKO,PDB_L_XKO);
tmpstr[PDB_L_XKO]=0;
if (sscanf(tmpstr,"%lf",&resd)!=1) goto illegal_pdb_line;
if (resd<-990.0) goto unknown_coords;
ko[0]=resd;
/********* get the y-coordinate ************/
strncpy(tmpstr,linestr+PDB_P_YKO,PDB_L_YKO);
tmpstr[PDB_L_YKO]=0;
if (sscanf(tmpstr,"%lf",&resd)!=1) goto illegal_pdb_line;
if (resd<-990.0) goto unknown_coords;
ko[1]=resd;
/********* get the z-coordinate ************/
strncpy(tmpstr,linestr+PDB_P_ZKO,PDB_L_ZKO);
tmpstr[PDB_L_ZKO]=0;
if (sscanf(tmpstr,"%lf",&resd)!=1) goto illegal_pdb_line;
if (resd<-990.0) goto unknown_coords;
ko[2]=resd;
/********* get the beta-value ***************/
strncpy(tmpstr,linestr+PDB_P_BETA,PDB_L_BETA);
tmpstr[PDB_L_BETA]=0;
if (sscanf(tmpstr,"%lf",&resd)!=1) goto illegal_pdb_line;
*beta=resd;
/******* get the harmonic constant ***********/
strncpy(tmpstr,linestr+PDB_P_HARMON,PDB_L_HARMON);
tmpstr[PDB_L_HARMON]=0;
if (sscanf(tmpstr,"%lf",&resd)!=1) goto illegal_pdb_line;
*harmon=resd;
/********** get the segid-string *************/
if ((int)strlen(linestr)<PDB_P_SEGIDSTR) {
segidstr[0]=0;
} else {
strncpy(tmpstr,linestr+PDB_P_SEGIDSTR,PDB_L_SEGIDSTR);
tmpstr[PDB_L_SEGIDSTR]=0;
erase_spaces(tmpstr);
strcpy(segidstr,tmpstr);
}
return 0;
illegal_pdb_line:
fprintf(stderr,"\n\nFatal error: Illegal PDB-Line. Check row-positions\n");
fprintf(stderr,"ATOM!atomn! !st! !re!!resnr! !xko !!yko !!zko !!beta!!harmon! !se!\n");
fprintf(stderr,linestr);
return -2;
unknown_coords:
fprintf(stderr,"\n\nFatal error: Unkown coordinates in PDB-File.\n");
fprintf(stderr,linestr);
return -2;
}
/* patch_pdb_coords is from the EGO_VIII-distribution, (c) M. Eichinger */
static int patch_pdb_coords(FILE *fh,FILE *fh1,double x,double y,double z)
{
int res;
long curpos;
char linestr[MAX_STRING_SIZE+1];
char tmpstr[MAX_STRING_SIZE+1];
/* search for next line starting with 'ATOM' */
do {
curpos=ftell(fh);
res=read_string(fh,linestr);
strup(linestr);
strncpy(tmpstr,linestr,4);
tmpstr[4]=0;
if (strcmp(tmpstr,"ATOM")==0) goto set_atom_data; /* found one */
tmpstr[3]=0;
fprintf(fh1,"%s\n",linestr);
fflush(fh1);
if (strcmp(tmpstr,"END")==0) return -1;
} while(res!=-1);
return -1;
set_atom_data:
sprintf(tmpstr,"%8.3f ",x);
memcpy(linestr+PDB_P_XKO,tmpstr,PDB_L_XKO);
sprintf(tmpstr,"%8.3f ",y);
memcpy(linestr+PDB_P_YKO,tmpstr,PDB_L_YKO);
sprintf(tmpstr,"%8.3f ",z);
memcpy(linestr+PDB_P_ZKO,tmpstr,PDB_L_ZKO);
fprintf(fh1,"%s\n",linestr);
fflush(fh1);
return 0;
}
static void add_pdb_tip(FILE *fp,int flag,double x,double y,double z,long int atom_nr,int res_id,int seg_id)
{
char tmpstr[MAX_STRING_SIZE+1];
char linestr[MAX_STRING_SIZE+1];
if (flag==0) {
strcpy(linestr,"ATOM 000000 OH2 TIP3 0 0.000 0.000 0.000 0.00 0.00 W000");
}
if (flag==1) {
strcpy(linestr,"ATOM 000000 H1 TIP3 0 0.000 0.000 0.000 0.00 0.00 W000");
}
if (flag==2) {
strcpy(linestr,"ATOM 000000 H2 TIP3 0 0.000 0.000 0.000 0.00 0.00 W000");
}
sprintf(tmpstr,"%6ld ",atom_nr);
memcpy(linestr+PDB_P_ATOMNR,tmpstr,PDB_L_ATOMNR);
sprintf(tmpstr,"%8.3f ",x);
memcpy(linestr+PDB_P_XKO,tmpstr,PDB_L_XKO);
sprintf(tmpstr,"%8.3f ",y);
memcpy(linestr+PDB_P_YKO,tmpstr,PDB_L_YKO);
sprintf(tmpstr,"%8.3f ",z);
memcpy(linestr+PDB_P_ZKO,tmpstr,PDB_L_ZKO);
sprintf(tmpstr,"%5d ",res_id);
memcpy(linestr+PDB_P_RESIDNR-1,tmpstr,PDB_L_RESIDNR);
sprintf(tmpstr,"%3d ",seg_id);
memcpy(linestr+PDB_P_SEGIDSTR+1,tmpstr,PDB_L_SEGIDSTR-1);
fprintf(fp,"%s\n",linestr);
fflush(fp);
return;
}
static void add_pdb_ion(FILE *fp,int iontype,double x,double y,double z,long int atom_nr,int res_id)
{
char tmpstr[MAX_STRING_SIZE+1];
char linestr[MAX_STRING_SIZE+1];
if (iontype==1) {
strcpy(linestr,"ATOM 000000 NA INA 0 0.000 0.000 0.000 0.00 0.00 NA ");
}
else if (iontype==-1) {
strcpy(linestr,"ATOM 000000 CL ICL 0 0.000 0.000 0.000 0.00 0.00 CL ");
}
else {
fprintf(stderr,"\n\nERROR: Only sodium and chloride ions are supported here.\n");
terminate();
}
sprintf(tmpstr,"%6ld ",atom_nr);
memcpy(linestr+PDB_P_ATOMNR,tmpstr,PDB_L_ATOMNR);
sprintf(tmpstr,"%8.3f ",x);
memcpy(linestr+PDB_P_XKO,tmpstr,PDB_L_XKO);
sprintf(tmpstr,"%8.3f ",y);
memcpy(linestr+PDB_P_YKO,tmpstr,PDB_L_YKO);
sprintf(tmpstr,"%8.3f ",z);
memcpy(linestr+PDB_P_ZKO,tmpstr,PDB_L_ZKO);
sprintf(tmpstr,"%5d ",res_id);
memcpy(linestr+PDB_P_RESIDNR-1,tmpstr,PDB_L_RESIDNR);
fprintf(fp,"%s\n",linestr);
fflush(fp);
return;
}
static int goto_psf_atomdata(FILE *h_psf)
{
static char lstr[MAX_STRING_SIZE];
static char tmpstr[MAX_STRING_SIZE];
int nr_of_atoms;
while (read_string(h_psf,lstr)!=-1) {
strup(lstr);
if (sscanf(lstr,"%d %s",&nr_of_atoms,tmpstr)==2) {
if (strcmp(tmpstr,"!NATOM")==0) {
if (nr_of_atoms!=natoms) {
fprintf(stderr,"\n\nERROR: Atom counts in pdb- and psf-file differ.\n");
terminate();
}
return 0;
}
}
}
fprintf(stderr,"\n\nERROR: No atom-data-section (!NATOM) in PSF-file.\n");
terminate();
return -1;
}
static int read_next_psfatom(FILE *fh,int *atom_nr, char *atom_typestr, double *charge, double *mass, char *atomstr, char *residstr, int *residnr, char *segidstr)
{
int res;
double resd;
static char lstr[MAX_STRING_SIZE];
static char tmpstr[MAX_STRING_SIZE];
if (read_string(fh,lstr)==-1) return -1;
strup(lstr);
strncpy(tmpstr,lstr+PSF_P_ATOMNR,PSF_L_ATOMNR);
tmpstr[PSF_L_ATOMNR]=0;
if (sscanf(tmpstr,"%d",&res)!=1) goto illegal_psf_atomline;
if (res<0) goto illegal_psf_atomline;
*atom_nr=res;
/********** get the segid-string *************/
strncpy(tmpstr,lstr+PSF_P_SEGIDSTR,PSF_L_SEGIDSTR);
tmpstr[PSF_L_SEGIDSTR]=0;
erase_spaces(tmpstr);
strcpy(segidstr,tmpstr);
/********** get the resid-number *************/
strncpy(tmpstr,lstr+PSF_P_RESIDNR,PSF_L_RESIDNR);
tmpstr[PSF_L_RESIDNR]=0;
if (sscanf(tmpstr,"%d",&res)!=1) goto illegal_psf_atomline;
if (res<1) goto illegal_psf_atomline;
*residnr=res;
/********** get the resid-string *************/
strncpy(tmpstr,lstr+PSF_P_RESIDSTR,PSF_L_RESIDSTR);
tmpstr[PSF_L_RESIDSTR]=0;
erase_spaces(tmpstr);
strcpy(residstr,tmpstr);
/********** get the atom-string *************/
strncpy(tmpstr,lstr+PSF_P_ATOMSTR,PSF_L_ATOMSTR);
tmpstr[PSF_L_ATOMSTR]=0;
erase_spaces(tmpstr);
strcpy(atomstr,tmpstr);
/********** get the atomtype-string ********/
strncpy(tmpstr,lstr+PSF_P_ATOMTYPSTR,PSF_L_ATOMTYPSTR);
tmpstr[PSF_L_ATOMTYPSTR]=0;
erase_spaces(tmpstr);
strcpy(atom_typestr,tmpstr);
/********** get the charge *************/
strncpy(tmpstr,lstr+PSF_P_CHARGE,PSF_L_CHARGE);
tmpstr[PSF_L_CHARGE]=0;
if (sscanf(tmpstr,"%lf",&resd)!=1) goto illegal_psf_atomline;
*charge=resd;
/********** get the mass *************/
strncpy(tmpstr,lstr+PSF_P_MASS,PSF_L_MASS);
tmpstr[PSF_L_MASS]=0;
if (sscanf(tmpstr,"%lf",&resd)!=1) goto illegal_psf_atomline;
if (resd<0.5) goto illegal_psf_atomline;
*mass=resd;
return 0;
illegal_psf_atomline:
fprintf(stderr,"\n\nFatal error: Illegal PSF-atom-data-line. Check row-positions !\n");
fprintf(stderr,"!atomnr! !se!!resd!!re! !as! !ts! !charge ! !mass !\n");
fprintf(stderr,lstr);
return -2;
}
static void read_pdb_file(char *infile)
{
int i,res,resid_nr,atom_nr;
double beta,harmon;
double ko[3];
char atomstr[MAX_STRING_SIZE+1];
char residstr[MAX_STRING_SIZE+1];
char segidstr[MAX_STRING_SIZE+1];
char infile1[MAX_STRING_SIZE+1];
FILE *fp;
strcpy(infile1,infile);
strcat(infile1,".pdb");
max_atom_nr_in_pdb=0;
if (no_solute_present_flag==0) {
fprintf(stderr,"Reading solute from file %s ... ",infile1);
if ((fp=fopen(infile1,"r"))==NULL) {
fprintf(stderr,"\n\nERROR: Could not read from file %s\n",infile1);
terminate();
}
natoms=0;
do {
res=read_next_pdbdata(fp,&atom_nr,ko,&beta,&harmon,atomstr,residstr,&resid_nr,segidstr);
if (res==-2) {
fprintf(stderr,"\n\nERROR: Error in pdb-file.\n");
terminate();
}
if (max_atom_nr_in_pdb<atom_nr) max_atom_nr_in_pdb=atom_nr;
natoms++;
} while (res!=-1);
fclose(fp);
natoms--;
}
else {
natoms=0;
}
if (natoms>=1) {
xatom=dvector(1,natoms);
yatom=dvector(1,natoms);
zatom=dvector(1,natoms);
qatom=dvector(1,natoms);
sigatom=dvector(1,natoms);
epsatom=dvector(1,natoms);
if ((fp=fopen(infile1,"r"))==NULL) {
fprintf(stderr,"\n\nERROR: Could not read from file %s\n",infile1);
terminate();
}
for (i=1;i<=natoms;i++) {
res=read_next_pdbdata(fp,&atom_nr,ko,&beta,&harmon,atomstr,residstr,&resid_nr,segidstr);
if (res==-2) {
terminate();
}
xatom[i]=ko[0];
yatom[i]=ko[1];
zatom[i]=ko[2];
/* non-bonded vdw parameters are taken from XPLOR, param19.pro */
if (strncmp(atomstr,"H",1)==0) {
epsatom[i]=0.0498;
sigatom[i]=1.4254;
}
else if (strncmp(atomstr,"CH1",3)==0) {
epsatom[i]=0.0486;
sigatom[i]=4.2140;
}
else if (strncmp(atomstr,"CH2",3)==0) {
epsatom[i]=0.1142;
sigatom[i]=3.9823;
}
else if (strncmp(atomstr,"CH3",3)==0) {
epsatom[i]=0.1811;
sigatom[i]=3.8576;
}
else if (strncmp(atomstr,"C",1)==0) {
epsatom[i]=0.1200;
sigatom[i]=3.7418;
}
else if (strncmp(atomstr,"OC",2)==0) {
epsatom[i]=0.6469;
sigatom[i]=2.8509;
}
else if (strncmp(atomstr,"O",1)==0) {
epsatom[i]=0.1591;
sigatom[i]=2.8509;
}
else if (strncmp(atomstr,"N",1)==0) {
epsatom[i]=0.2384;
sigatom[i]=2.8509;
}
else if (strncmp(atomstr,"S",1)==0) {
epsatom[i]=0.0430;
sigatom[i]=3.3676;
}
else {
epsatom[i]=SIG_H2O;
sigatom[i]=0.15;
}
}
fclose(fp);
if (natoms==1) fprintf(stderr,"%d atom read.\n",natoms);
if (natoms!=1) fprintf(stderr,"%d atoms read.\n",natoms);
}
else {
if (no_solute_present_flag==0) {
fprintf(stderr,"No solute found; creating a pure water droplet at (0,0,0):\n\n");
}
else {
fprintf(stderr,"No solute given; creating a pure water droplet at (0,0,0):\n\n");
}
natoms=1;
xatom=dvector(1,natoms);
yatom=dvector(1,natoms);
zatom=dvector(1,natoms);
sigatom=dvector(1,natoms);
epsatom=dvector(1,natoms);
xatom[1] = 0.0;
yatom[1] = 0.0;
zatom[1] = 0.0;
sigatom[1] = 0.0;
epsatom[1] = 0.15;
}
return;
}
static void read_atom_charges_from_psf_file(char *infile)
{
int i,res,atom_index,residnr;
double atom_charge,atom_mass;
char atomtypestr[MAX_STRING_SIZE+1];
char atomstr[MAX_STRING_SIZE+1];
char residstr[MAX_STRING_SIZE+1];
char segidstr[MAX_STRING_SIZE+1];
char infile1[MAX_STRING_SIZE+1];
FILE *fp;
strcpy(infile1,infile);
strcat(infile1,".psf");
if (no_solute_present_flag==0) {
total_charge = 0.0;
fprintf(stderr,"Reading atomic charges from file %s ... ",infile1);
if ((fp=fopen(infile1,"r"))==NULL) {
fprintf(stderr,"\n\nERROR: Could not read from file %s\n",infile1);
terminate();
}
goto_psf_atomdata(fp);
for (i=1; i<=natoms; i++) {
res=read_next_psfatom(fp,&atom_index,atomtypestr,&atom_charge,&atom_mass,atomstr,residstr,&residnr,segidstr);
if (res==-2) {
fprintf(stderr,"\n\nERROR: Error in psf-file.\n");
terminate();
}
qatom[i]=atom_charge;
total_charge += atom_charge;
}
fclose(fp);
fprintf(stderr,"total charge is %+5.2f e.\n",total_charge);
}
return;
}
static double calc_maxdist(double xr, double yr, double zr)
{
int ii;
double dx,dy,dz,maxd,d2;
maxd=0.0;
for (ii=1; ii<=natoms; ii++) {
dx=xr-xatom[ii];
dy=yr-yatom[ii];
dz=zr-zatom[ii];
d2=dx*dx+dy*dy+dz*dz;
if (d2>maxd) {
maxd=d2;
}
}
if (maxd==0.0) maxd=1.0e-10;
return sqrt(maxd);
}
static void calc_bounding_sphere()
{
int i,flag;
double stepwidth;
double xmin,xmax;
double ymin,ymax;
double zmin,zmax;
double xr,xrnew;
double yr,yrnew;
double zr,zrnew;
double mr,mrnew;
fprintf(stderr,"\nCalculating bounding sphere ... ");
xmin=1.0e20;
ymin=1.0e20;
zmin=1.0e20;
xmax=-1.0e20;
ymax=-1.0e20;
zmax=-1.0e20;
for (i=1;i<=natoms;i++) {
if (xatom[i]<xmin) xmin=xatom[i];
if (xatom[i]>xmax) xmax=xatom[i];
if (yatom[i]<ymin) ymin=yatom[i];
if (yatom[i]>ymax) ymax=yatom[i];
if (zatom[i]<zmin) zmin=zatom[i];
if (zatom[i]>zmax) zmax=zatom[i];
}
xr=(xmin+xmax)/2.0;
yr=(ymin+ymax)/2.0;
zr=(zmin+zmax)/2.0;
mr=calc_maxdist(xr,yr,zr);
stepwidth=INITIAL_STEP_WIDTH_FOR_CALC_BOUNDING_SPHERE;
do {
do {
flag=0;
for (i=0; i<50; i++) {
xrnew=xr-stepwidth*rang();
yrnew=yr-stepwidth*rang();
zrnew=zr-stepwidth*rang();
mrnew=calc_maxdist(xrnew,yrnew,zrnew);
if (mrnew<mr) {
mr=mrnew;
xr=xrnew;
yr=yrnew;
zr=zrnew;
flag=1;
}
}
} while (flag==1);
stepwidth /= STEP_WIDTH_DECREASE_FOR_CALC_BOUNDING_SPHERE;
} while (stepwidth>=MINIMAL_STEP_WIDTH_FOR_CALC_BOUNDING_SPHERE);
x_sphere=xr;
y_sphere=yr;
z_sphere=zr;
r_sphere=mr;
if (r_sphere >= boundary_radius) {
fprintf(stderr,"\n\nERROR: Selected maximum boundary radius (%f) is smaller\n",boundary_radius);
fprintf(stderr," than calculated bounding sphere radius (%f).\n",r_sphere);
fprintf(stderr," Use a larger boundary radius!\n");
terminate();
}
fprintf(stderr,"done.\n");
return;
}
static void cut_away(double theta, double phi)
{
long int i,j,k;
double xx,yy,zz,dis;
double d11,d12,d13;
double d21,d22,d23;
double d31,d32,d33;
double dis_closest;
double **d1, **d2, **dd;
d1=dmatrix(1,3,1,3);
d2=dmatrix(1,3,1,3);
dd=dmatrix(1,3,1,3);
d1[1][1]=cos(theta/180.0*PI);
d1[1][2]=0.0;
d1[1][3]=sin(theta/180.0*PI);
d1[2][1]=0.0;
d1[2][2]=1.0;
d1[2][3]=0.0;
d1[3][1]=-sin(theta/180.0*PI);
d1[3][2]=0.0;
d1[3][3]=cos(theta/180.0*PI);
d2[1][1]=cos(phi/180.0*PI);
d2[1][2]=sin(phi/180.0*PI);
d2[1][3]=0.0;
d2[2][1]=-sin(phi/180.0*PI);
d2[2][2]=cos(phi/180.0*PI);
d2[2][3]=0.0;
d2[3][1]=0.0;
d2[3][2]=0.0;
d2[3][3]=1.0;
for (i=1;i<=3;i++) {
for (j=1;j<=3;j++) {
dd[i][j]=0.0;
for (k=1;k<=3;k++) {
dd[i][j] += (d1[i][k]*d2[k][j]);
}
}
}
d11=dd[1][1];
d12=dd[1][2];
d13=dd[1][3];
d21=dd[2][1];
d22=dd[2][2];
d23=dd[2][3];
d31=dd[3][1];
d32=dd[3][2];
d33=dd[3][3];
free_dmatrix(d1,1,3,1,3);
free_dmatrix(d2,1,3,1,3);
free_dmatrix(dd,1,3,1,3);
dis_closest=-1.0e20;
for (i=1; i<=natoms; i++) {
xx = d11*xatom[i] + d12*yatom[i] + d13*zatom[i];
yy = d21*xatom[i] + d22*yatom[i] + d23*zatom[i];
zz = d31*xatom[i] + d32*yatom[i] + d33*zatom[i];
dis=zz+(xx*xx+yy*yy)/(2.0*boundary_radius);
if (dis >= dis_closest) dis_closest=dis;
}
dis_closest += 0.5;
for (i=1; i<=ngrid; i++) {
if (cutflags[i]==1) {
xx = d11*xgrid[i] + d12*ygrid[i] + d13*zgrid[i];
yy = d21*xgrid[i] + d22*ygrid[i] + d23*zgrid[i];
zz = d31*xgrid[i] + d32*ygrid[i] + d33*zgrid[i];
dis=zz+(xx*xx+yy*yy)/(2.0*boundary_radius);
if (dis>dis_closest) cutflags[i]=0;
}
}
return;
}
static void create_convex_volume(void)
{
long int i,ii,maxn;
double x,y,z;
double gridmax;
double theta,phi,r2,phistep;
double *xgrid1;
double *ygrid1;
double *zgrid1;
fprintf(stderr,"Creating convex volume (max. radius is %f) ... ",boundary_radius);
for (i=1; i<=natoms; i++) {
xatom[i] -= x_sphere;
yatom[i] -= y_sphere;
zatom[i] -= z_sphere;
}
gridmax=floor(r_sphere)+2.0;
maxn=floor(MAX_NR_OF_GRID_POINTS_FACTOR*4.0/3.0*PI*gridmax*gridmax*gridmax);
xgrid = dvector(1,maxn);
ygrid = dvector(1,maxn);
zgrid = dvector(1,maxn);
cutflags = ivector(1,maxn);
i=1;
r2=gridmax*gridmax;
for (x=-gridmax; x<gridmax+0.1; x++) {
for (y=-gridmax; y<gridmax+0.1; y++) {
for (z=-gridmax; z<gridmax+0.1; z++) {
if ((x*x+y*y+z*z)<=r2) {
xgrid[i]=x;
ygrid[i]=y;
zgrid[i]=z;
cutflags[i]=1;
i++;
if (i>=maxn) {
fprintf(stderr,"\n\nERROR: Maximum number of grid points exceeded.\n");
fprintf(stderr," Increase MAX_NR_OF_GRID_POINTS_FACTOR!\n");
terminate();
}
}
}
}
}
ngrid=i-1;
cut_away(0.0,0.0);
cut_away(90.0,0.0);
cut_away(90.0,90.0);
cut_away(90.0,180.0);
cut_away(90.0,270.0);
cut_away(180.0,0.0);
ngrid1=0;
for (i=1; i<=ngrid; i++) {
if (cutflags[i]==1) {
ngrid1++;
}
}
xgrid1=dvector(1,ngrid1);
ygrid1=dvector(1,ngrid1);
zgrid1=dvector(1,ngrid1);
ii=1;
for (i=1; i<=ngrid; i++) {
if (cutflags[i]==1) {
xgrid1[ii] = xgrid[i];
ygrid1[ii] = ygrid[i];
zgrid1[ii] = zgrid[i];
ii++;
}
}
free_dvector(xgrid,1,maxn);
free_dvector(ygrid,1,maxn);
free_dvector(zgrid,1,maxn);
free_ivector(cutflags,1,maxn);
xgrid=xgrid1;
ygrid=ygrid1;
zgrid=zgrid1;
ngrid=ii-1;
cutflags = ivector(1,ngrid);
for (i=1; i<=ngrid; i++) cutflags[i]=1;
for (theta=ANGLE_STEP_FOR_CUT_AWAY; theta<180.1-ANGLE_STEP_FOR_CUT_AWAY; theta+=ANGLE_STEP_FOR_CUT_AWAY) {
phistep=360.0/floor((360.0-10.0*ANGLE_STEP_FOR_CUT_AWAY)/ANGLE_STEP_FOR_CUT_AWAY*sin(theta*PI/180.0)+10.0);
for (phi=0.0; phi<360.1-phistep; phi+=phistep) {
cut_away(theta,phi);
}
}
ngrid1=0;
for (i=1; i<=ngrid; i++) {
if (cutflags[i]==1) {
ngrid1++;
}
}
xgrid1=dvector(1,ngrid1);
ygrid1=dvector(1,ngrid1);
zgrid1=dvector(1,ngrid1);
ii=1;
for (i=1; i<=ngrid; i++) {
if (cutflags[i]==1) {
xgrid1[ii] = xgrid[i];
ygrid1[ii] = ygrid[i];
zgrid1[ii] = zgrid[i];
ii++;
}
}
free_dvector(xgrid,1,ngrid);
free_dvector(ygrid,1,ngrid);
free_dvector(zgrid,1,ngrid);
free_ivector(cutflags,1,ngrid);
xgrid=xgrid1;
ygrid=ygrid1;
zgrid=zgrid1;
ngrid=ii-1;
for (i=1; i<=natoms; i++) {
xatom[i] += x_sphere;
yatom[i] += y_sphere;
zatom[i] += z_sphere;
}
for (i=1; i<=ngrid; i++) {
xgrid[i] += x_sphere;
ygrid[i] += y_sphere;
zgrid[i] += z_sphere;
}
fprintf(stderr,"done.\n");
return;
}
static void dice_data()
{
int i,gridnr;
double sigma;
fprintf(stderr,"Gauss-sampling convex volume ... ");
ndata=NR_OF_DICE_DATA;
sigma=water_shell_thickness;
xdata = dvector(1,ndata);
ydata = dvector(1,ndata);
zdata = dvector(1,ndata);
for (i=1;i<=ndata;i++) {
gridnr=1+floor(ngrid*ran2(&seed));
if ((gridnr<1)||(gridnr>ngrid)) {
fprintf(stderr,"\n\nERROR: This should not happen. Sorry.\n");
terminate();
}
xdata[i]=xgrid[gridnr]+sigma*rang();
ydata[i]=ygrid[gridnr]+sigma*rang();
zdata[i]=zgrid[gridnr]+sigma*rang();
}
free_dvector(xgrid,1,ngrid);
free_dvector(ygrid,1,ngrid);
free_dvector(zgrid,1,ngrid);
fprintf(stderr,"done.\n");
return;
}
static void do_pca()
{
int i,j;
double *e;
cov = dmatrix(1,3,1,3);
gauss_xm = dvector(1,3);
gauss_y = dmatrix(1,3,1,ncodebook);
gauss_w = dcube(1,3,1,3,1,ncodebook);
gauss_rho2 = dmatrix(1,3,1,ncodebook);
p = dvector(1,ncodebook);
ev = dmatrix(1,3,1,3);
lambda = dvector(1,3);
g=dvector(1,ncodebook);
a=dvector(1,ncodebook);
gauss_dist=dvector(1,ncodebook);
xx=dvector(1,3);
xt=dvector(1,3);
xtt=dvector(1,3);
/*** calculate covariance matrix: ***/
for (i=1;i<=3;i++) {
gauss_xm[i]=0.0;
}
for (i=1;i<=ndata;i++) {
gauss_xm[1] += xdata[i];
gauss_xm[2] += ydata[i];
gauss_xm[3] += zdata[i];
}
for (i=1;i<=3;i++) {
gauss_xm[i] /= (1.0*ndata);
}
for (i=1;i<=3;i++) {
for (j=1;j<=3;j++) {
cov[i][j]=0.0;
}
}
for (i=1;i<=ndata;i++) {
cov[1][1] += (xdata[i]-gauss_xm[1])*(xdata[i]-gauss_xm[1]);
cov[1][2] += (xdata[i]-gauss_xm[1])*(ydata[i]-gauss_xm[2]);
cov[1][3] += (xdata[i]-gauss_xm[1])*(zdata[i]-gauss_xm[3]);
cov[2][1] += (ydata[i]-gauss_xm[2])*(xdata[i]-gauss_xm[1]);
cov[2][2] += (ydata[i]-gauss_xm[2])*(ydata[i]-gauss_xm[2]);
cov[2][3] += (ydata[i]-gauss_xm[2])*(zdata[i]-gauss_xm[3]);
cov[3][1] += (zdata[i]-gauss_xm[3])*(xdata[i]-gauss_xm[1]);
cov[3][2] += (zdata[i]-gauss_xm[3])*(ydata[i]-gauss_xm[2]);
cov[3][3] += (zdata[i]-gauss_xm[3])*(zdata[i]-gauss_xm[3]);
}
for (i=1;i<=3;i++) {
for (j=1;j<=3;j++) {
cov[i][j] /= (1.0*ndata);
}
}
/*** diagonalize covariance-matrix: ***/
e = dvector(1,3);
for (i=1;i<=3;i++) {
for (j=1;j<=3;j++) {
ev[i][j] = cov[i][j];
}
}
tred2(ev,3,lambda,e);
tqli(lambda,e,3,ev); /* the eigenvectors j are now in ev[][j] */
free_dvector(e,1,3);
/* ugly sort */
if (lambda[1]<lambda[2]) {
temp=lambda[1]; lambda[1]=lambda[2]; lambda[2]=temp;
temp=ev[1][1]; ev[1][1]=ev[1][2]; ev[1][2]=temp;
temp=ev[2][1]; ev[2][1]=ev[2][2]; ev[2][2]=temp;
temp=ev[3][1]; ev[3][1]=ev[3][2]; ev[3][2]=temp;
}
if (lambda[1]<lambda[3]) {
temp=lambda[1]; lambda[1]=lambda[3]; lambda[3]=temp;
temp=ev[1][1]; ev[1][1]=ev[1][3]; ev[1][3]=temp;
temp=ev[2][1]; ev[2][1]=ev[2][3]; ev[2][3]=temp;
temp=ev[3][1]; ev[3][1]=ev[3][3]; ev[3][3]=temp;
}
if (lambda[2]<lambda[3]) {
temp=lambda[2]; lambda[2]=lambda[3]; lambda[3]=temp;
temp=ev[1][2]; ev[1][2]=ev[1][3]; ev[1][3]=temp;
temp=ev[2][2]; ev[2][2]=ev[2][3]; ev[2][3]=temp;
temp=ev[3][2]; ev[3][2]=ev[3][3]; ev[3][3]=temp;
}
return;
}
static void init_codebook_positions()
{
int i,r;
for (r=1;r<=ncodebook;r++) {
for (i=1;i<=3;i++) {
gauss_y[i][r]=gauss_xm[i];
}
}
return;
}
static void init_codebook_directions()
{
int i,j,r;
for (r=1;r<=ncodebook;r++) {
for (i=1;i<=3;i++) {
for (j=1;j<=3;j++) {
gauss_w[i][j][r]=ev[i][j];
}
}
}
return;
}
static void init_codebook_variances()
{
int i,r;
rho0=sqrt(lambda[1]);
for (r=1;r<=ncodebook;r++) {
for (i=1;i<=3;i++) {
gauss_rho2[i][r]=FACTOR_FOR_INITIAL_LAMBDA_FOR_LEARNS_GAUSSIANS*lambda[i];
}
}
return;
}
static void init_parameters()
{
int r;
entropy=1.0e10;
min_entropy=1.0e10;
min_rhoaver2=1.0e10;
enttfac=ENTROPY_AVERAGE_LENGTH/(1.0*timax);
/*** init gauss weights p[r] ***/
for (r=1;r<=ncodebook;r++) {
p[r]=1.0/(1.0*ncodebook);
}
return;
}
static void compute_activities()
{
int r,i,j;
for (r=1;r<=ncodebook;r++) {
for (i=1;i<=3;i++) {
xt[i]=0.0;
}
for (i=1;i<=3;i++) {
for (j=1;j<=3;j++) {
xt[i] += (gauss_w[j][i][r]*(xx[j]-gauss_y[j][r])); /* XT=W^T*(x-y_r) */
}
}
for (i=1;i<=3;i++) {
xt[i] /= gauss_rho2[i][r]; /* XT=GT*XT */
}
for (i=1;i<=3;i++) {
xtt[i]=0.0;
}
for (i=1;i<=3;i++) {
for (j=1;j<=3;j++) {
xtt[i] += (gauss_w[i][j][r]*xt[j]); /* XTT=W*XT */
}
}
gauss_dist[r]=0.0;
for (j=1;j<=3;j++) {
gauss_dist[r] += ((xx[j]-gauss_y[j][r])*xtt[j]); /* dist_r=(x-y_r)^T*XTT */
}
gauss_dist[r] *= 0.5;
}
distmin=1.0e200;
for (r=1;r<=ncodebook;r++) {
if (gauss_dist[r] <= distmin) distmin=gauss_dist[r];
}
for (r=1;r<=ncodebook;r++) {
gauss_dist[r] -= distmin;
}
for (r=1;r<=ncodebook;r++) {
if (gauss_dist[r]>100.0) {
g[r]=0.0;
}
else {
g[r]=exp(-gauss_dist[r]);
}
vol2=pow(2.0*PI,3.0/2.0);
for (i=1;i<=3;i++) {
vol2 *= sqrt(gauss_rho2[i][r]);
}
g[r] /= vol2;
}
atot=0.0;
for (r=1;r<=ncodebook;r++) {
atot += (p[r]*g[r]);
}
for (r=1;r<=ncodebook;r++) {
a[r] = (p[r]*g[r])/atot;
}
/* sum moving-averaged kulbak-entropy: */
entropy = (1.0-enttfac)*entropy - enttfac*(log(atot)-distmin);
if (entropy < min_entropy) {
min_entropy=entropy;
min_rhoaver2=rhoaver2;
}
return;
}
static void learn_codebook_positions(double eps)
{
int i,r;
/* learn codebook vectors y[][r] */
/* y_r := y_r + eps*[a_r*(x-y_r) + noise] */
for (r=1;r<=ncodebook;r++) {
for (i=1;i<=3;i++) {
gauss_y[i][r] += (eps*(a[r]*(xx[i]-gauss_y[i][r])
+NOISE_FOR_LEARN_CODEBOOK_POSITIONS*rang()/sqrt(gauss_rho2[1][r])));
}
}
return;
}
static void learn_codebook_directions(double eps)
{
int i,j,k,r;
/* learn codebook directions w[][j][r] */
/* w_j = w_j + eps*a_r*(w_j*(x-y))*(x-y) */
for (r=1;r<=ncodebook;r++) {
for (j=1;j<=3;j++) {
prod=0.0;
for (i=1;i<=3;i++) {
prod += (gauss_w[i][j][r]*(xx[i]-gauss_y[i][r]));
}
for (i=1;i<=3;i++) {
gauss_w[i][j][r] += ((eps*a[r]*prod*(xx[i]-gauss_y[i][r]))/(rho0*rho0));
}
}
}
/* ortho-normalize codebook directions w[][j][r] (schmidt) */
for (r=1;r<=ncodebook;r++) {
for (k=2;k<=3;k++) {
for (j=1;j<k;j++) {
prod1=0.0;
prod2=0.0;
for (i=1;i<=3;i++) {
prod1 += (gauss_w[i][k][r]*gauss_w[i][j][r]);
prod2 += (gauss_w[i][j][r]*gauss_w[i][j][r]);
}
for (i=1;i<=3;i++) {
gauss_w[i][k][r] -= (prod1/prod2*gauss_w[i][j][r]);
}
}
}
for (j=1;j<=3;j++) {
prod=0.0;
for (i=1;i<=3;i++) {
prod += (gauss_w[i][j][r]*gauss_w[i][j][r]);
}
prod=1.0/sqrt(prod);
for (i=1;i<=3;i++) {
gauss_w[i][j][r] *= prod;
}
}
}
return;
}
static void learn_codebook_variances_soft_individual(double eps, double mu)
{
int r,i,j;
/* soft volume (2): individual constraint to rhoaver */
for (r=1;r<=ncodebook;r++) {
for (j=1;j<=3;j++) {
prod=0.0;
for (i=1;i<=3;i++) {
prod += (gauss_w[i][j][r]*(xx[i]-gauss_y[i][r]));
}
prod2=prod*prod;
gauss_rho2[j][r] += (eps*a[r]*((prod2-gauss_rho2[j][r])-mu*(gauss_rho2[j][r]-rhoaver2)));
}
}
return;
}
static void learn_codebook_variances_soft_global(double eps, double mu)
{
int r,i,j;
double mufactor;
/* soft volume (1): global */
for (r=1;r<=ncodebook;r++) {
rhoav2=1.0;
for (j=1;j<=3;j++) {
rhoav2 *= (gauss_rho2[j][r]);
}
rhoav2=pow(rhoav2,1.0/3.0);
mufactor=mu*(1.0-rhoaver2/rhoav2);
for (j=1;j<=3;j++) {
prod=0.0;
for (i=1;i<=3;i++) {
prod += (gauss_w[i][j][r]*(xx[i]-gauss_y[i][r]));
}
prod2=prod*prod;
gauss_rho2[j][r] += (eps*a[r]*((prod2-gauss_rho2[j][r])-mufactor*gauss_rho2[j][r]));
}
}
return;
}
static void equalize_codebook_variances()
{
int r,j;
/* this routine is only used if natoms==1, so that we have *really* a sphere */
for (r=1;r<=ncodebook;r++) {
rhoav2=1.0;
for (j=1;j<=3;j++) {
rhoav2 *= (gauss_rho2[j][r]);
}
rhoav2=pow(rhoav2,1.0/3.0);
for (j=1;j<=3;j++) {
gauss_rho2[j][r] = rhoav2;
}
}
return;
}
static void learn_codebook_variances_hard(double eps)
{
int r,i,j;
double mufactor;
/* hard volume */
for (r=1;r<=ncodebook;r++) {
for (j=1;j<=3;j++) {
prod=0.0;
for (i=1;i<=3;i++) {
prod += (gauss_w[i][j][r]*(xx[i]-gauss_y[i][r]));
}
prod2=prod*prod;
gauss_rho2[j][r] += eps*a[r]*(prod2-gauss_rho2[j][r]);
}
rhoav2=1.0;
for (j=1;j<=3;j++) {
rhoav2 *= (gauss_rho2[j][r]);
}
rhoav2=pow(rhoav2,1.0/3.0);
mufactor=rhoaver2/rhoav2;
for (j=1;j<=3;j++) {
gauss_rho2[j][r] *= mufactor;
}
}
return;
}
static void outputs()
{
int r,i,j;
/*** output y[][r], sqrt(rho2[j][r]), w[i][j][r] ***/
printf("%f\n",sqrt(rhoaver2));
printf("%f\n",entropy);
for (r=1;r<=ncodebook;r++) {
for (j=1;j<=3;j++) {
printf("%f\n",gauss_y[j][r]);
}
for (j=1;j<=3;j++) {
printf("%f\n",sqrt(gauss_rho2[j][r]));
}
for (j=1;j<=3;j++) {
for (i=1;i<=3;i++) {
printf("%f ",gauss_w[i][j][r]);
}
printf("\n");
}
}
return;
}
static double get_rhoaver2(double r1, double r2, long int tmax, long int t)
{
double rav;
rav = r1*pow((r2/r1),((1.0*t)/(1.0*tmax)));
return (rav*rav);
}
static double get_eps(double e1, double e2, long int tmax, long int t)
{
return (e1*pow((e2/e1),((1.0*t)/(1.0*tmax))));
}
static void learn_gaussians()
{
if (ncodebook==1) {
fprintf(stderr,"Approximating volume with 1 gaussian: Phase 1/4...");
}
else {
fprintf(stderr,"Approximating volume with %d gaussians: Phase 1/4...",ncodebook);
}
do_pca();
timax=TIMAX_FACTOR*ndata;
init_parameters();
init_codebook_positions();
init_codebook_directions();
init_codebook_variances();
rhoaver2 = pow((gauss_rho2[1][1]*gauss_rho2[2][1]*gauss_rho2[3][1]),1.0/(1.0*3.0));
rhoaverstart=sqrt(rhoaver2)*RHOAVERSTART_FACTOR;
rhoaverend=sqrt(rhoaver2)*RHOAVEREND_FACTOR;
for (ti=0;ti<timax;ti++) {
xx[1]=xdata[1+ti%ndata];
xx[2]=ydata[1+ti%ndata];
xx[3]=zdata[1+ti%ndata];
rhoaver2 = get_rhoaver2(rhoaverstart,rhoaverend,timax,ti);
compute_activities();
learn_codebook_positions(EPS_CONST_FOR_LEARN_POSITIONS);
learn_codebook_directions(EPS_CONST_FOR_LEARN_DIRECTIONS);
learn_codebook_variances_hard(EPS_CONST_FOR_LEARN_VARIANCES);
}
fprintf(stderr,"2/4...");
rhoaverend = sqrt(min_rhoaver2);
timax=TIMAX_FACTOR*ndata;
init_parameters();
init_codebook_positions();
init_codebook_directions();
init_codebook_variances();
rhoaver2 = pow((gauss_rho2[1][1]*gauss_rho2[2][1]*gauss_rho2[3][1]),1.0/(1.0*3.0));
rhoaverstart=sqrt(rhoaver2)*RHOAVERSTART_FACTOR;
for (ti=0;ti<timax;ti++) {
xx[1]=xdata[1+ti%ndata];
xx[2]=ydata[1+ti%ndata];
xx[3]=zdata[1+ti%ndata];
rhoaver2 = get_rhoaver2(rhoaverstart,rhoaverend,timax,ti);
compute_activities();
learn_codebook_positions(EPS_CONST_FOR_LEARN_POSITIONS);
learn_codebook_directions(EPS_CONST_FOR_LEARN_DIRECTIONS);
learn_codebook_variances_hard(EPS_CONST_FOR_LEARN_VARIANCES);
}
fprintf(stderr,"3/4...");
timax=TIMAX_FACTOR*ndata;
init_parameters();
rhoaver2 = rhoaverend*rhoaverend;
epsstart=EPS_START_FOR_LEARN_DIRECTIONS;
epsend =EPS_END_FOR_LEARN_DIRECTIONS;
for (ti=0;ti<timax;ti++) {
xx[1]=xdata[1+ti%ndata];
xx[2]=ydata[1+ti%ndata];
xx[3]=zdata[1+ti%ndata];
compute_activities();
eps = get_eps(epsstart,epsend,timax,ti);
learn_codebook_positions(eps*EPS_CONST_FOR_LEARN_POSITIONS/EPS_CONST_FOR_LEARN_DIRECTIONS);
learn_codebook_directions(eps);
learn_codebook_variances_hard(eps);
}
fprintf(stderr,"4/4...");
timax=TIMAX_FACTOR*ndata;
init_parameters();
rhoaver2 = rhoaverend*rhoaverend;
epsstart=EPS_START_FOR_LEARN_DIRECTIONS;
epsend =EPS_END_FOR_LEARN_DIRECTIONS;
for (ti=0;ti<timax;ti++) {
xx[1]=xdata[1+ti%ndata];
xx[2]=ydata[1+ti%ndata];
xx[3]=zdata[1+ti%ndata];
compute_activities();
eps = get_eps(epsstart,epsend,timax,ti);
learn_codebook_directions(eps);
learn_codebook_variances_soft_individual(eps,MU_FOR_LEARN_VARIANCES_INDIVIDUAL);
}
if (natoms<=1) {
equalize_codebook_variances();
}
free_dvector(xdata,1,ndata);
free_dvector(ydata,1,ndata);
free_dvector(zdata,1,ndata);
free_dvector(gauss_xm,1,3);
free_dmatrix(cov,1,3,1,3);
free_dvector(p,1,ncodebook);
free_dmatrix(ev,1,3,1,3);
free_dvector(lambda,1,3);
free_dvector(g,1,ncodebook);
free_dvector(a,1,ncodebook);
free_dvector(gauss_dist,1,ncodebook);
free_dvector(xx,1,3);
free_dvector(xt,1,3);
free_dvector(xtt,1,3);
fprintf(stderr,"done.\n");
return;
}
static double density_varrho(double x, double y, double z, double rhofac)
{
int r;
double sum,rhoq,di;
double w11,w12,w13;
double w21,w22,w23;
double w31,w32,w33;
double xt1,xt2,xt3,xtt1,xtt2,xtt3;
double dx,dy,dz;
sum=0.0;
rhoq=2.0*rhofac*rhofac;
for (r=1; r<=ncodebook; r++) {
dx=x-gauss_y[1][r];
dy=y-gauss_y[2][r];
dz=z-gauss_y[3][r];
w11=gauss_w[1][1][r];
w12=gauss_w[1][2][r];
w13=gauss_w[1][3][r];
w21=gauss_w[2][1][r];
w22=gauss_w[2][2][r];
w23=gauss_w[2][3][r];
w31=gauss_w[3][1][r];
w32=gauss_w[3][2][r];
w33=gauss_w[3][3][r];
xt1 = (w11*dx + w21*dy + w31*dz)/gauss_rho2[1][r];
xt2 = (w12*dx + w22*dy + w32*dz)/gauss_rho2[2][r];
xt3 = (w13*dx + w23*dy + w33*dz)/gauss_rho2[3][r];
xtt1 = (w11*xt1) + (w12*xt2) + (w13*xt3);
xtt2 = (w21*xt1) + (w22*xt2) + (w23*xt3);
xtt3 = (w31*xt1) + (w32*xt2) + (w33*xt3);
di=(dx*xtt1 + dy*xtt2 + dz*xtt3)/rhoq;
if (di<100.0) sum += (exp(-di)/gauss_vol[r]);
}
return (sum);
}
static double density(double x, double y, double z)
{
int r;
double sum,di;
double w11,w12,w13;
double w21,w22,w23;
double w31,w32,w33;
double xt1,xt2,xt3,xtt1,xtt2,xtt3;
double dx,dy,dz;
sum=0.0;
for (r=1; r<=ncodebook; r++) {
dx=x-gauss_y[1][r];
dy=y-gauss_y[2][r];
dz=z-gauss_y[3][r];
w11=gauss_w[1][1][r];
w12=gauss_w[1][2][r];
w13=gauss_w[1][3][r];
w21=gauss_w[2][1][r];
w22=gauss_w[2][2][r];
w23=gauss_w[2][3][r];
w31=gauss_w[3][1][r];
w32=gauss_w[3][2][r];
w33=gauss_w[3][3][r];
xt1 = (w11*dx + w21*dy + w31*dz)/gauss_rho2[1][r];
xt2 = (w12*dx + w22*dy + w32*dz)/gauss_rho2[2][r];
xt3 = (w13*dx + w23*dy + w33*dz)/gauss_rho2[3][r];
xtt1 = (w11*xt1) + (w12*xt2) + (w13*xt3);
xtt2 = (w21*xt1) + (w22*xt2) + (w23*xt3);
xtt3 = (w31*xt1) + (w32*xt2) + (w33*xt3);
di=0.5*(dx*xtt1 + dy*xtt2 + dz*xtt3);
if (di<100.0) sum += (gauss_height[r]*exp(-di));
}
return (sum);
}
static double density_grad_curve(double x, double y, double z, double *gx, double *gy, double *gz, double *g, double *cr, double *curvature)
{
int r;
double sum,prod;
double w11,w12,w13;
double w21,w22,w23;
double w31,w32,w33;
double t11,t12,t13;
double t21,t22,t23;
double t31,t32,t33;
double a11,a12,a13;
double a21,a22,a23;
double a31,a32,a33;
double m11,m12,m13;
double m21,m22,m23;
double m31,m32,m33;
double cxx,cxy,cxz,cyx,cyy,cyz,czx,czy,czz;
double dx,dy,dz,ex;
double g0x,g0y,g0z;
double v1,v2,v3;
double temp;
sum=0.0;
*gx = 0.0;
*gy = 0.0;
*gz = 0.0;
cxx = 0.0;
cxy = 0.0;
cxz = 0.0;
cyx = 0.0;
cyy = 0.0;
cyz = 0.0;
czx = 0.0;
czy = 0.0;
czz = 0.0;
for (r=1; r<=ncodebook; r++) {
dx=x-gauss_y[1][r];
dy=y-gauss_y[2][r];
dz=z-gauss_y[3][r];
w11=gauss_w[1][1][r];
w12=gauss_w[1][2][r];
w13=gauss_w[1][3][r];
w21=gauss_w[2][1][r];
w22=gauss_w[2][2][r];
w23=gauss_w[2][3][r];
w31=gauss_w[3][1][r];
w32=gauss_w[3][2][r];
w33=gauss_w[3][3][r];
t11 = w11/gauss_rho2[1][r]; /* T := rho2^(-1) * W^T */
t12 = w21/gauss_rho2[1][r];
t13 = w31/gauss_rho2[1][r];
t21 = w12/gauss_rho2[2][r];
t22 = w22/gauss_rho2[2][r];
t23 = w32/gauss_rho2[2][r];
t31 = w13/gauss_rho2[3][r];
t32 = w23/gauss_rho2[3][r];
t33 = w33/gauss_rho2[3][r];
a11 = w11*t11 + w12*t21 + w13*t31; /* A := W * T ( == W * rho2^(-1) * W^T) */
a12 = w11*t12 + w12*t22 + w13*t32;
a13 = w11*t13 + w12*t23 + w13*t33;
a21 = w21*t11 + w22*t21 + w23*t31;
a22 = w21*t12 + w22*t22 + w23*t32;
a23 = w21*t13 + w22*t23 + w23*t33;
a31 = w31*t11 + w32*t21 + w33*t31;
a32 = w31*t12 + w32*t22 + w33*t32;
a33 = w31*t13 + w32*t23 + w33*t33;
v1 = a11*dx + a12*dy + a13*dz; /* v := A*x */
v2 = a21*dx + a22*dy + a23*dz;
v3 = a31*dx + a32*dy + a33*dz;
prod = dx*v1 + dy*v2 + dz*v3; /* prod := x^T * v ( == x^T * A * x ) */
if (prod<100.0) {
m11 = -a11 + v1*v1; /* M := -A + (Ax)(Ax)^T */
m12 = -a12 + v1*v2;
m13 = -a13 + v1*v3;
m21 = -a21 + v2*v1;
m22 = -a22 + v2*v2;
m23 = -a23 + v2*v3;
m31 = -a31 + v3*v1;
m32 = -a32 + v3*v2;
m33 = -a33 + v3*v3;
ex = gauss_height[r]*exp(-prod/2.0);
sum += ex;
*gx -= (v1*ex);
*gy -= (v2*ex);
*gz -= (v3*ex);
cxx += (m11*ex);
cxy += (m12*ex);
cxz += (m13*ex);
cyx += (m21*ex);
cyy += (m22*ex);
cyz += (m23*ex);
czx += (m31*ex);
czy += (m32*ex);
czz += (m33*ex);
}
}
*g =sqrt((*gx)*(*gx) + (*gy)*(*gy) + (*gz)*(*gz));
if ((*g)!=0.0) {
g0x = (*gx)/(*g);
g0y = (*gy)/(*g);
g0z = (*gz)/(*g);
}
else {
g0x = 0.0;
g0y = 0.0;
g0z = 0.0;
*gx = 1.0e-50;
*gy = 0.0;
*gz = 0.0;
*g = 1.0e-50;
}
v1 = cxx*g0x + cxy*g0y + cxz*g0z;
v2 = cyx*g0x + cyy*g0y + cyz*g0z;
v3 = czx*g0x + czy*g0y + czz*g0z;
*cr = v1*g0x + v2*g0y + v3*g0z;
temp=sqrt((*gx)*(*gx)+(*gy)*(*gy)+(*gz)*(*gz));
if (temp!=0.0) {
*curvature=-0.5*((*gx)*(*gx)*(cyy+czz) + (*gy)*(*gy)*(cxx+czz) + (*gz)*(*gz)*(cxx+cyy)
-2.0*((*gx)*(*gy)*cxy + (*gx)*(*gz)*cxz + (*gy)*(*gz)*cyz))/(temp*temp*temp);
}
else {
*curvature = 0.0;
}
return (sum);
}
static double density_gradient(double x, double y, double z, double *gx, double *gy, double *gz)
{
int r;
double sum,di;
double w11,w12,w13;
double w21,w22,w23;
double w31,w32,w33;
double xt1,xt2,xt3,xtt1,xtt2,xtt3;
double dx,dy,dz,ex;
sum=0.0;
*gx = 0.0;
*gy = 0.0;
*gz = 0.0;
for (r=1; r<=ncodebook; r++) {
dx=x-gauss_y[1][r];
dy=y-gauss_y[2][r];
dz=z-gauss_y[3][r];
w11=gauss_w[1][1][r];
w12=gauss_w[1][2][r];
w13=gauss_w[1][3][r];
w21=gauss_w[2][1][r];
w22=gauss_w[2][2][r];
w23=gauss_w[2][3][r];
w31=gauss_w[3][1][r];
w32=gauss_w[3][2][r];
w33=gauss_w[3][3][r];
xt1 = (w11*dx + w21*dy + w31*dz)/gauss_rho2[1][r];
xt2 = (w12*dx + w22*dy + w32*dz)/gauss_rho2[2][r];
xt3 = (w13*dx + w23*dy + w33*dz)/gauss_rho2[3][r];
xtt1 = (w11*xt1) + (w12*xt2) + (w13*xt3);
xtt2 = (w21*xt1) + (w22*xt2) + (w23*xt3);
xtt3 = (w31*xt1) + (w32*xt2) + (w33*xt3);
di=0.5*(dx*xtt1 + dy*xtt2 + dz*xtt3);
if (di<100.0) {
ex = gauss_height[r]*exp(-di);
sum += ex;
*gx -= xtt1*ex;
*gy -= xtt2*ex;
*gz -= xtt3*ex;
}
}
return (sum);
}
static double get_min_distance(double rhofac, double level)
{
int i;
double dx,dy,dz,rr,rrr;
double x0,y0,z0;
double x1,y1,z1;
double x2,y2,z2;
double x3,y3,z3;
double dens1,dens2,dens3;
double dmin;
double theta,thetastep;
double phi,phistep;
double cosphi,sinphi;
dmin=1.0e20;
thetastep=2.0*PI/(1.0+floor(2.0*PI/
atan(RESOLUTION_FOR_GET_MIN_DISTANCE/r_sphere)));
for (theta=thetastep/2.0; theta<PI-thetastep/2.0+thetastep*0.1; theta+=thetastep) {
x0=sin(theta)*SCALE_FACTOR_FOR_GET_MIN_DISTANCE*(r_sphere+2.0*water_shell_thickness);
y0=0.0;
z0=cos(theta)*SCALE_FACTOR_FOR_GET_MIN_DISTANCE*(r_sphere+2.0*water_shell_thickness);
rrr=r_sphere*sin(theta);
phistep=2.0*PI/(1.0+floor(2.0*PI/atan(RESOLUTION_FOR_GET_MIN_DISTANCE/rrr)));
for (phi=0.0; phi<2.0*PI-phistep*0.1; phi+=phistep) {
cosphi=cos(phi);
sinphi=sin(phi);
x1=x_sphere + cosphi*x0 - sinphi*y0;
y1=y_sphere + sinphi*x0 + cosphi*y0;
z1=z_sphere + z0;
x3=x_sphere;
y3=y_sphere;
z3=z_sphere;
dens1=density_varrho(x1,y1,z1,rhofac);
dens3=density_varrho(x3,y3,z3,rhofac);
if (dens1>level) {
fprintf(stderr,"\n\nERROR: dens1>level (%f > %f)\n",dens1,level);
fprintf(stderr," Increasing SCALE_FACTOR_FOR_GET_MIN_DISTANCE should help.\n");
terminate();
}
if (dens3<level) {
return(0.0); /* to enforce an increase of rho_factor */
}
else {
do {
dx=x3-x1;
dy=y3-y1;
dz=z3-z1;
rr=dx*dx+dy*dy+dz*dz;
x2=(x1+x3)/2.0;
y2=(y1+y3)/2.0;
z2=(z1+z3)/2.0;
dens2=density_varrho(x2,y2,z2,rhofac);
if (dens2>level) {
dens3=dens2;
x3=x2;
y3=y2;
z3=z2;
}
else {
dens1=dens2;
x1=x2;
y1=y2;
z1=z2;
}
} while (rr>ACCURACY_OF_GET_MIN_DISTANCE*ACCURACY_OF_GET_MIN_DISTANCE);
x2=(x1+x3)/2.0;
y2=(y1+y3)/2.0;
z2=(z1+z3)/2.0;
for (i=1;i<=natoms;i++) {
dx=x2-xatom[i];
dy=y2-yatom[i];
dz=z2-zatom[i];
rr=dx*dx+dy*dy+dz*dz;
if (rr<dmin) dmin=rr;
}
}
}
}
return(sqrt(dmin));
}
static void adjust_surface_distance(void)
{
int i;
double volaver,rhofac,rhofac_try,rhofac_shrink_factor;
double d_now,d_try;
fprintf(stderr,"\nAdjusting boundary distance by varying gaussian widths ... ");
volaver=0.0;
gauss_vol=dvector(1,ncodebook);
gauss_height=dvector(1,ncodebook);
for (i=1; i<=ncodebook; i++) {
gauss_vol[i]=pow((gauss_rho2[1][i]*gauss_rho2[2][i]*gauss_rho2[3][i]),1.0/(2.0*3.0));
volaver += gauss_vol[i];
}
volaver /= (1.0*ncodebook);
rhofac=INITIAL_RHOFAC;
d_now = get_min_distance(rhofac,SURFACE_LEVEL/volaver);
if (water_shell_thickness > d_now) {
fprintf(stderr,"\n\nERROR: water_shell_thickness > d_now (%f > %f)\n",water_shell_thickness,d_now);
fprintf(stderr," Increasing INITIAL_RHOFAC should help.\n");
terminate();
}
rhofac_shrink_factor = INITIAL_RHOFAC_SHRINK_FACTOR;
do {
rhofac_try=rhofac*rhofac_shrink_factor;
d_try=get_min_distance(rhofac_try,SURFACE_LEVEL/volaver);
if (water_shell_thickness<d_try) {
rhofac=rhofac_try;
d_now=d_try;
}
else {
rhofac_shrink_factor = (5.0+3.0*rhofac_shrink_factor)/(5.0+3.0);
}
fprintf(stderr,"\b\b\b\b\b\b\b\b\b\b%8.3f A",d_try);
} while (fabs(d_now-water_shell_thickness)>ACCURACY_OF_ADJUST_SURFACE_DISTANCE);
fprintf(stderr,"\n");
fprintf(stderr,"Gaussian widths are being scaled by a factor of %f\n",rhofac);
for (i=1; i<=ncodebook; i++) {
gauss_rho2[1][i] *= (rhofac*rhofac);
gauss_rho2[2][i] *= (rhofac*rhofac);
gauss_rho2[3][i] *= (rhofac*rhofac);
gauss_height[i] = volaver/(gauss_vol[i]);
}
return;
}
static void save_gaussian_parameters()
{
int r;
FILE *fp;
if (ncodebook==1) fprintf(stderr,"Saving parameter for one gaussian to file gaussians.lis ... ");
if (ncodebook!=1) fprintf(stderr,"Saving parameter for %d gaussians to file gaussians.lis ... ",ncodebook);
if ((fp=fopen("gaussians.lis","w"))==NULL) {
fprintf(stderr,"\n\nERROR: Could not write to file gaussians.lis\n");
terminate();
}
fprintf(fp,"[ENCLOSING SPHERE (X,Y,Z,R)]\n");
fprintf(fp,"%20e\n",x_sphere);
fprintf(fp,"%20e\n",y_sphere);
fprintf(fp,"%20e\n",z_sphere);
fprintf(fp,"%20e\n",r_sphere);
fprintf(fp,"[NR OF GAUSSIANS]\n");
fprintf(fp,"%d\n",ncodebook);
fprintf(fp,"[SURFACE LEVEL]\n");
fprintf(fp,"%20e\n",SURFACE_LEVEL);
fprintf(fp,"[GAUSSIAN CENTERS]\n");
for (r=1; r<=ncodebook; r++) {
fprintf(fp,"%20e %20e %20e\n",gauss_y[1][r],gauss_y[2][r],gauss_y[3][r]);
}
fprintf(fp,"[GAUSSIAN VARIANCES]\n");
for (r=1; r<=ncodebook; r++) {
fprintf(fp,"%20e %20e %20e\n",gauss_rho2[1][r],gauss_rho2[2][r],gauss_rho2[3][r]);
}
fprintf(fp,"[GAUSSIAN ORIENTATIONS]\n");
for (r=1; r<=ncodebook; r++) {
fprintf(fp,"%20e %20e %20e\n",gauss_w[1][1][r],gauss_w[1][2][r],gauss_w[1][3][r]);
fprintf(fp,"%20e %20e %20e\n",gauss_w[2][1][r],gauss_w[2][2][r],gauss_w[2][3][r]);
fprintf(fp,"%20e %20e %20e\n",gauss_w[3][1][r],gauss_w[3][2][r],gauss_w[3][3][r]);
}
fclose(fp);
fprintf(stderr,"done.\n");
return;
}
static void read_gaussian_parameters()
{
int r;
float fdummy;
FILE *fp;
fprintf(stderr,"Reading parameter for gaussians from file gaussians.lis ... ");
if ((fp=fopen("gaussians.lis","r"))==NULL) {
fprintf(stderr,"\n\nERROR: Could not read from file gaussians.lis\n");
terminate();
}
skip_line(fp);
fscanf(fp,"%f",&fdummy);
x_sphere=fdummy;
fscanf(fp,"%f",&fdummy);
y_sphere=fdummy;
fscanf(fp,"%f",&fdummy);
z_sphere=fdummy;
fscanf(fp,"%f",&fdummy);
r_sphere=fdummy;
skip_line(fp);
fscanf(fp,"%d",&ncodebook);
if (ncodebook>10000) {
fprintf(stderr,"\n\nERROR: Found too many gaussians (%d).\n",ncodebook);
fprintf(stderr," Probably file gaussians.lis is corrupted.\n");
terminate();
}
if (ncodebook <1) {
fprintf(stderr,"\n\nERROR: Found too few gaussians (%d).\n",ncodebook);
fprintf(stderr," Probably file gaussians.lis is corrupted.\n");
terminate();
}
skip_line(fp);
fscanf(fp,"%f",&fdummy);
if (fabs(fdummy-SURFACE_LEVEL)>1.0e-5) {
fprintf(stderr,"\n\nERROR: The surface level found in gaussians.lis (%f)\n",fdummy);
fprintf(stderr," differs from the one used here (%f).\n",SURFACE_LEVEL);
fprintf(stderr," Probably file gaussians.lis is corrupted.\n");
terminate();
}
skip_line(fp);
gauss_y = dmatrix(1,3,1,ncodebook);
gauss_w = dcube(1,3,1,3,1,ncodebook);
gauss_rho2 = dmatrix(1,3,1,ncodebook);
for (r=1; r<=ncodebook; r++) {
fscanf(fp,"%f",&fdummy);
gauss_y[1][r]=fdummy;
fscanf(fp,"%f",&fdummy);
gauss_y[2][r]=fdummy;
fscanf(fp,"%f",&fdummy);
gauss_y[3][r]=fdummy;
}
skip_line(fp);
for (r=1; r<=ncodebook; r++) {
fscanf(fp,"%f",&fdummy);
gauss_rho2[1][r]=fdummy;
fscanf(fp,"%f",&fdummy);
gauss_rho2[2][r]=fdummy;
fscanf(fp,"%f",&fdummy);
gauss_rho2[3][r]=fdummy;
}
skip_line(fp);
for (r=1; r<=ncodebook; r++) {
fscanf(fp,"%f",&fdummy);
gauss_w[1][1][r]=fdummy;
fscanf(fp,"%f",&fdummy);
gauss_w[1][2][r]=fdummy;
fscanf(fp,"%f",&fdummy);
gauss_w[1][3][r]=fdummy;
fscanf(fp,"%f",&fdummy);
gauss_w[2][1][r]=fdummy;
fscanf(fp,"%f",&fdummy);
gauss_w[2][2][r]=fdummy;
fscanf(fp,"%f",&fdummy);
gauss_w[2][3][r]=fdummy;
fscanf(fp,"%f",&fdummy);
gauss_w[3][1][r]=fdummy;
fscanf(fp,"%f",&fdummy);
gauss_w[3][2][r]=fdummy;
fscanf(fp,"%f",&fdummy);
gauss_w[3][3][r]=fdummy;
}
fclose(fp);
if (ncodebook==1) fprintf(stderr,"found %d gaussian.\n",ncodebook);
if (ncodebook!=1) fprintf(stderr,"found %d gaussians.\n",ncodebook);
return;
}
static void read_boundary_parameters()
{
int r;
float fdummy;
FILE *fp;
fprintf(stderr,"Reading boundary parameters from file boundary.lis ... ");
if ((fp=fopen("boundary.lis","r"))==NULL) {
fprintf(stderr,"\n\nERROR: Could not read from file boundary.lis\n");
terminate();
}
skip_line(fp);
fscanf(fp,"%f",&fdummy);
water_shell_thickness=fdummy;
skip_line(fp);
fscanf(fp,"%f",&fdummy);
x_sphere=fdummy;
fscanf(fp,"%f",&fdummy);
y_sphere=fdummy;
fscanf(fp,"%f",&fdummy);
z_sphere=fdummy;
fscanf(fp,"%f",&fdummy);
r_sphere=fdummy;
skip_line(fp);
fscanf(fp,"%d",&ncodebook);
if (ncodebook>10000) {
fprintf(stderr,"\n\nERROR: Found too many gaussians (%d).\n",ncodebook);
fprintf(stderr," Probably file boundary.lis is corrupted.\n");
terminate();
}
if (ncodebook <1) {
fprintf(stderr,"\n\nERROR: Found too few gaussians (%d).\n",ncodebook);
fprintf(stderr," Probably file boundary.lis is corrupted.\n");
terminate();
}
skip_line(fp);
fscanf(fp,"%f",&fdummy);
if (fabs(fdummy-SURFACE_LEVEL)>1.0e-5) {
fprintf(stderr,"\n\nERROR: The surface level found in boundary.lis (%f)\n",fdummy);
fprintf(stderr," differs from the one used here (%f).\n",SURFACE_LEVEL);
fprintf(stderr," Probably file boundary.lis is corrupted.\n");
terminate();
}
skip_line(fp);
gauss_y = dmatrix(1,3,1,ncodebook);
gauss_w = dcube(1,3,1,3,1,ncodebook);
gauss_rho2 = dmatrix(1,3,1,ncodebook);
gauss_height = dvector(1,ncodebook);
for (r=1; r<=ncodebook; r++) {
fscanf(fp,"%f",&fdummy);
gauss_height[r]=fdummy;
}
skip_line(fp);
for (r=1; r<=ncodebook; r++) {
fscanf(fp,"%f",&fdummy);
gauss_y[1][r]=fdummy;
fscanf(fp,"%f",&fdummy);
gauss_y[2][r]=fdummy;
fscanf(fp,"%f",&fdummy);
gauss_y[3][r]=fdummy;
}
skip_line(fp);
for (r=1; r<=ncodebook; r++) {
fscanf(fp,"%f",&fdummy);
gauss_rho2[1][r]=fdummy;
fscanf(fp,"%f",&fdummy);
gauss_rho2[2][r]=fdummy;
fscanf(fp,"%f",&fdummy);
gauss_rho2[3][r]=fdummy;
}
skip_line(fp);
for (r=1; r<=ncodebook; r++) {
fscanf(fp,"%f",&fdummy);
gauss_w[1][1][r]=fdummy;
fscanf(fp,"%f",&fdummy);
gauss_w[1][2][r]=fdummy;
fscanf(fp,"%f",&fdummy);
gauss_w[1][3][r]=fdummy;
fscanf(fp,"%f",&fdummy);
gauss_w[2][1][r]=fdummy;
fscanf(fp,"%f",&fdummy);
gauss_w[2][2][r]=fdummy;
fscanf(fp,"%f",&fdummy);
gauss_w[2][3][r]=fdummy;
fscanf(fp,"%f",&fdummy);
gauss_w[3][1][r]=fdummy;
fscanf(fp,"%f",&fdummy);
gauss_w[3][2][r]=fdummy;
fscanf(fp,"%f",&fdummy);
gauss_w[3][3][r]=fdummy;
}
fclose(fp);
if (ncodebook==1) fprintf(stderr,"\n found %d gaussian;\n",ncodebook);
if (ncodebook!=1) fprintf(stderr,"\n found %d gaussians;\n",ncodebook);
fprintf(stderr," minimum water shell thickness is %5.2f A.\n",water_shell_thickness);
return;
}
static void save_boundary_parameters()
{
int r;
FILE *fp;
fprintf(stderr,"Saving boundary parameters to file boundary.lis ... ");
if ((fp=fopen("boundary.lis","w"))==NULL) {
fprintf(stderr,"\n\nERROR: Could not write to file boundary.lis\n");
terminate();
}
fprintf(fp,"[MINIMUM WATER SHELL THICKNESS]\n");
fprintf(fp,"%20e\n",water_shell_thickness);
fprintf(fp,"[ENCLOSING SPHERE (X,Y,Z,R)]\n");
fprintf(fp,"%20e\n",x_sphere);
fprintf(fp,"%20e\n",y_sphere);
fprintf(fp,"%20e\n",z_sphere);
fprintf(fp,"%20e\n",r_sphere);
fprintf(fp,"[NR OF GAUSSIANS]\n");
fprintf(fp,"%d\n",ncodebook);
fprintf(fp,"[SURFACE LEVEL]\n");
fprintf(fp,"%20e\n",SURFACE_LEVEL);
fprintf(fp,"[GAUSSIAN HEIGHTS]\n");
for (r=1; r<=ncodebook; r++) {
fprintf(fp,"%20e\n",gauss_height[r]);
}
fprintf(fp,"[GAUSSIAN CENTERS]\n");
for (r=1; r<=ncodebook; r++) {
fprintf(fp,"%20e %20e %20e\n",gauss_y[1][r],gauss_y[2][r],gauss_y[3][r]);
}
fprintf(fp,"[GAUSSIAN VARIANCES]\n");
for (r=1; r<=ncodebook; r++) {
fprintf(fp,"%20e %20e %20e\n",gauss_rho2[1][r],gauss_rho2[2][r],gauss_rho2[3][r]);
}
fprintf(fp,"[GAUSSIAN ORIENTATIONS]\n");
for (r=1; r<=ncodebook; r++) {
fprintf(fp,"%20e %20e %20e\n",gauss_w[1][1][r],gauss_w[1][2][r],gauss_w[1][3][r]);
fprintf(fp,"%20e %20e %20e\n",gauss_w[2][1][r],gauss_w[2][2][r],gauss_w[2][3][r]);
fprintf(fp,"%20e %20e %20e\n",gauss_w[3][1][r],gauss_w[3][2][r],gauss_w[3][3][r]);
}
fclose(fp);
fprintf(stderr,"done.\n");
return;
}
static void create_volume(void)
{
long int ii,i;
double xmin,xmax,ymin,ymax,zmin,zmax;
double rr,rr2,dmin,dd;
double x,y,z;
double dx,dy,dz,ddd;
fprintf(stderr,"\nCreating volume within boundary ... ");
xmin=floor(x_sphere-r_sphere-water_shell_thickness-ADD_LAYER_FOR_CREATE_VOLUME);
xmax=floor(x_sphere+r_sphere+water_shell_thickness+ADD_LAYER_FOR_CREATE_VOLUME+1.0);
ymin=floor(y_sphere-r_sphere-water_shell_thickness-ADD_LAYER_FOR_CREATE_VOLUME);
ymax=floor(y_sphere+r_sphere+water_shell_thickness+ADD_LAYER_FOR_CREATE_VOLUME+1.0);
zmin=floor(z_sphere-r_sphere-water_shell_thickness-ADD_LAYER_FOR_CREATE_VOLUME);
zmax=floor(z_sphere+r_sphere+water_shell_thickness+ADD_LAYER_FOR_CREATE_VOLUME+1.0);
rr=r_sphere+water_shell_thickness+ADD_LAYER_FOR_CREATE_VOLUME+1.0;
rr2=rr*rr;
ngrid=0;
for (x=xmin; x<xmax+0.1; x++) {
for (y=ymin; y<ymax+0.1; y++) {
for (z=zmin; z<zmax+0.1; z++) {
dx=x-x_sphere;
dy=y-y_sphere;
dz=z-z_sphere;
if (dx*dx+dy*dy+dz*dz<=rr2) {
if (density(x,y,z)>=SURFACE_LEVEL) ngrid++;
}
}
}
}
xgrid=dvector(1,ngrid);
ygrid=dvector(1,ngrid);
zgrid=dvector(1,ngrid);
dgrid=dvector(1,ngrid);
densgrid=dvector(1,ngrid);
ii=1;
for (x=xmin; x<xmax+0.1; x++) {
for (y=ymin; y<ymax+0.1; y++) {
for (z=zmin; z<zmax+0.1; z++) {
dx=x-x_sphere;
dy=y-y_sphere;
dz=z-z_sphere;
if (dx*dx+dy*dy+dz*dz<=rr2) {
ddd=density(x,y,z);
if (ddd>=SURFACE_LEVEL) {
xgrid[ii]=x;
ygrid[ii]=y;
zgrid[ii]=z;
densgrid[ii]=ddd;
dmin=1.0e20;
for (i=1; i<=natoms; i++) {
dx=x-xatom[i];
dy=y-yatom[i];
dz=z-zatom[i];
dd=dx*dx+dy*dy+dz*dz;
if (dd<dmin) dmin=dd;
}
dgrid[ii]=sqrt(dmin);
ii++;
}
}
}
}
}
fprintf(stderr,"done.\n");
fprintf(stderr,"Sorting grid points according to distance from solute ... ");
sort5(ngrid,dgrid,xgrid,ygrid,zgrid,densgrid);
fprintf(stderr,"done.\n");
return;
}
static void surface_statistics(void)
{
int count,countmax;
long int i;
double gx,gy,gz,gx0,gy0,gz0,dd,dd0,dx,dy,dz,x0,y0,z0,x1,y1,z1,r;
double initial_stepsize,stepsize;
double xtry,ytry,ztry;
double gxnew,gynew,gznew,ddnew;
double estimated_distance,true_distance;
double gr,cur,s2,kk,xx0,yy0,yy1,yy2,err;
double ymin1,ymin2,ymin3,ymin4,ymin5,ymin6,ymin7;
double dmin1,dmin2,dmin3,dmin4,dmin5,dmin6,dmin7;
double curvature;
double dist_0,dist_1,dens_min,dd_0;
FILE *fp;
fprintf(stderr,"Performing distance approximation statistics with %ld grid points ... ",ngrid);
sdistgrid=dvector(1,ngrid);
distgrid=dvector(1,ngrid);
gradgrid=dvector(1,ngrid);
curvegrid=dvector(1,ngrid);
curvaturegrid=dvector(1,ngrid);
/* (for benchmark) */
/*
fprintf(stderr,"\n\nstart benchmark.\n\n");
system("date");
for (i=1; i<=ngrid; i++) {
x0=xgrid[i];
y0=ygrid[i];
z0=zgrid[i];
dd=density_grad_curve(x0,y0,z0,&gx,&gy,&gz,&gr,&cur,&curvature);
yy0 = dd;
yy1 = gr;
yy2 = cur;
s2 = yy0*yy0/(yy1*yy1-yy0*yy2);
kk = yy0*exp(0.5*yy1*yy1/(yy1*yy1-yy0*yy2));
xx0 = yy0*yy1/(yy1*yy1-yy0*yy2);
estimated_distance = fabs(xx0 - sqrt(2.0*s2*log(kk/SURFACE_LEVEL)));
}
system("date");
fprintf(stderr,"\nend benchmark.\n\n");
*/
for (i=1; i<=ngrid; i++) {
if (floor(((i-1.0)*100.0)/(1.0*ngrid))!=floor((i*100.0)/(1.0*ngrid))) {
fprintf(stderr,"\b\b\b\b%3d%%",(int)(floor((i*100.0)/(1.0*ngrid))));
}
x0=xgrid[i];
y0=ygrid[i];
z0=zgrid[i];
dd=density_grad_curve(x0,y0,z0,&gx,&gy,&gz,&gr,&cur,&curvature);
dd0=dd;
gx0=gx;
gy0=gy;
gz0=gz;
initial_stepsize=INITIAL_STEPSIZE_FOR_SURFACE_STATISTICS;
stepsize=initial_stepsize;
countmax=2*floor(water_shell_thickness/initial_stepsize);
while (stepsize>ACCURACY_FOR_SURFACE_STATISTICS) {
count=0;
do {
count++;
r=sqrt(gx*gx+gy*gy+gz*gz);
xtry = x0 - (stepsize*gx/r);
ytry = y0 - (stepsize*gy/r);
ztry = z0 - (stepsize*gz/r);
ddnew = density_gradient(xtry,ytry,ztry,&gxnew,&gynew,&gznew);
if (ddnew >= SURFACE_LEVEL) {
x0=xtry;
y0=ytry;
z0=ztry;
gx=gxnew;
gy=gynew;
gz=gznew;
dd=ddnew;
}
} while ((ddnew >= SURFACE_LEVEL) && (count<countmax));
stepsize /= 2.0;
countmax=3;
}
dx=x0-xgrid[i];
dy=y0-ygrid[i];
dz=z0-zgrid[i];
densgrid[i]=dd0;
gradgrid[i]=sqrt(gx0*gx0+gy0*gy0+gz0*gz0);
curvegrid[i]=cur;
curvaturegrid[i]=curvature;
sdistgrid[i]=sqrt(dx*dx+dy*dy+dz*dz);
if ((sdistgrid[i]>DISTANCE_TIMES_WATER_SHELL_THICKNESS*water_shell_thickness) || (fabs(ddnew-SURFACE_LEVEL)>SURFACE_LEVEL/1000.0)) {
sdistgrid[i]=DISTANCE_TIMES_WATER_SHELL_THICKNESS*water_shell_thickness;
}
}
fprintf(stderr,"\n");
if (write_surface_statistics_flag==1) {
if ((fp=fopen("surface_stat.lis","w"))==NULL) {
fprintf(stderr,"\n\nERROR: Could not write to file surface_stat.lis\n");
terminate();
}
fprintf(stderr,"Writing boundary coordinates to file surface_stat.lis ... ");
fprintf(fp,"[SURFACE COORDINATES (x,y,z,curvature)]\n");
for (i=1; i<=ngrid; i++) {
if (densgrid[i]<(1.0+BOUNDARY_SURFACE_THICKNESS_FACTOR)*SURFACE_LEVEL) {
fprintf(fp,"%f %f %f %f\n",xgrid[i],ygrid[i],zgrid[i],curvaturegrid[i]);
}
}
fclose(fp);
fprintf(stderr,"done.\n");
}
/* Now let's check whether our distance computation via */
/* densgrid[],gradgrid[], and curvegrid[] is correct: */
fprintf(stderr,"Checking approximation for distance from boundary ... ");
ymin1=1.0e20;
ymin2=1.0e20;
ymin3=1.0e20;
ymin4=1.0e20;
ymin5=1.0e20;
ymin6=1.0e20;
ymin7=1.0e20;
dens_min=1.0e10;
for (i=1; i<=ngrid; i++) {
if (floor(((i-1.0)*100.0)/(1.0*ngrid))!=floor((i*100.0)/(1.0*ngrid))) {
fprintf(stderr,"\b\b\b\b%3d%%",(int)(floor((i*100.0)/(1.0*ngrid))));
}
x0=xgrid[i];
y0=ygrid[i];
z0=zgrid[i];
dd=density_grad_curve(x0,y0,z0,&gx,&gy,&gz,&gr,&cur,&curvature);
yy0 = dd;
dd_0 = dd;
yy1 = sqrt(gx*gx+gy*gy+gz*gz);
yy2 = cur;
s2 = yy0*yy0/(yy1*yy1-yy0*yy2);
kk = yy0*exp(0.5*yy1*yy1/(yy1*yy1-yy0*yy2));
xx0 = yy0*yy1/(yy1*yy1-yy0*yy2);
dist_0 = fabs(xx0 - sqrt(2.0*s2*log(kk/SURFACE_LEVEL)));
if (sdistgrid[i]<DISTANCE_TIMES_WATER_SHELL_THICKNESS*water_shell_thickness) {
x1 = x0 - dist_0*gx/yy1;
y1 = y0 - dist_0*gy/yy1;
z1 = z0 - dist_0*gz/yy1;
dd=density_grad_curve(x1,y1,z1,&gx,&gy,&gz,&gr,&cur,&curvature);
curvaturegrid[i]=curvature;
yy0 = dd;
yy1 = sqrt(gx*gx+gy*gy+gz*gz);
yy2 = cur;
s2 = yy0*yy0/(yy1*yy1-yy0*yy2);
kk = yy0*exp(0.5*yy1*yy1/(yy1*yy1-yy0*yy2));
xx0 = yy0*yy1/(yy1*yy1-yy0*yy2);
dist_1 = fabs(xx0 - sqrt(2.0*s2*log(kk/SURFACE_LEVEL)));
estimated_distance = dist_0 + dist_1;
distgrid[i]=estimated_distance;
true_distance = sdistgrid[i];
err=fabs(true_distance-estimated_distance);
if (err>0.01) {
if (dd_0<ymin1) {
ymin1=dd_0;
dmin1=true_distance;
}
}
if (err>0.02) {
if (dd_0<ymin2) {
ymin2=dd_0;
dmin2=true_distance;
}
}
if (err>0.05) {
if (dd_0<ymin3) {
ymin3=dd_0;
dmin3=true_distance;
}
}
if (err>0.1) {
if (dd_0<ymin4) {
ymin4=dd_0;
dmin4=true_distance;
}
}
if (err>0.2) {
if (dd_0<ymin5) {
ymin5=dd_0;
dmin5=true_distance;
}
}
if (err>0.5) {
if (dd_0<ymin6) {
ymin6=dd_0;
dmin6=true_distance;
}
}
if (err>1.0) {
if (dd_0<ymin7) {
ymin7=dd_0;
dmin7=true_distance;
}
}
}
else {
curvaturegrid[i]=0.0;
distgrid[i]=10.0;
if (dens_min > dd) dens_min=dd;
}
}
fprintf(stderr,"\n");
if (write_surface_statistics_flag==1) {
if ((fp=fopen("surface_stat.lis","a"))==NULL) {
fprintf(stderr,"\n\nERROR: Could not append to file surface_stat.lis\n");
terminate();
}
fprintf(fp,"[MINIMUM INVALID DENSITY]\n");
fprintf(fp,"%f\n",dens_min);
fprintf(fp,"[DISTANCE ERROR STATISTICS (ABS. ERR / DENSITY / DISTANCE)]\n");
fprintf(fp,"0.01 %f %f\n",ymin1,dmin1);
fprintf(fp,"0.02 %f %f\n",ymin2,dmin2);
fprintf(fp,"0.05 %f %f\n",ymin3,dmin3);
fprintf(fp,"0.10 %f %f\n",ymin4,dmin4);
fprintf(fp,"0.20 %f %f\n",ymin5,dmin5);
fprintf(fp,"0.50 %f %f\n",ymin6,dmin6);
fprintf(fp,"1.00 %f %f\n",ymin7,dmin7);
fclose(fp);
}
if (write_volume_statistics_flag==1) {
fprintf(stderr,"Writing grid points within boundary to file volume_stat.lis ... ");
if ((fp=fopen("volume_stat.lis","w"))==NULL) {
fprintf(stderr,"\n\nERROR: Could not write to file volume_stat.lis\n");
terminate();
}
for (i=1; i<=ngrid; i++) {
fprintf(fp,"%f %f %f %f %f %f %f\n",xgrid[i],ygrid[i],zgrid[i],densgrid[i],sdistgrid[i],distgrid[i],curvaturegrid[i]);
}
fclose(fp);
fprintf(stderr,"done.\n");
}
free_dvector(sdistgrid,1,ngrid);
free_dvector(gradgrid,1,ngrid);
free_dvector(curvegrid,1,ngrid);
free_dvector(curvaturegrid,1,ngrid);
return;
}
static double vdw_energy_force(double x, double y, double z, double *fx, double *fy, double *fz)
{
double evdw,dx,dy,dz,dd,fvdw,r_2,r_6;
int i;
*fx = 0.0;
*fy = 0.0;
*fz = 0.0;
evdw=0.0;
for (i=1; i<=natoms; i++) {
dx=x-xatom[i];
dy=y-yatom[i];
dz=z-zatom[i];
dd=dx*dx+dy*dy+dz*dz;
if (dd<=100.0) {
r_2=1.0/dd;
r_6=r_2*r_2*r_2;
evdw += (r_6* (r_6*a_vdw_atom[i]-b_vdw_atom[i]));
fvdw = 6.0 * r_2 * r_6* (2.0*r_6*a_vdw_atom[i]-b_vdw_atom[i]);
*fx += (fvdw * dx);
*fy += (fvdw * dy);
*fz += (fvdw * dz);
}
}
if (nwater>0) {
for (i=1; i<=nwater; i++) {
dx=x-xwater[i];
dy=y-ywater[i];
dz=z-zwater[i];
dd=dx*dx+dy*dy+dz*dz;
if (dd<=100.0) {
r_2=1.0/dd;
r_6=r_2*r_2*r_2;
evdw += (r_6* (r_6*a_vdw-b_vdw));
fvdw = 6.0 * r_2 * r_6* (2.0*r_6*a_vdw-b_vdw);
*fx += (fvdw * dx);
*fy += (fvdw * dy);
*fz += (fvdw * dz);
}
}
}
return(evdw);
}
static void place_water_molecules(void)
{
long int ii,jj,i;
double sig,eps;
double sig_6,sig_12;
double dx,dy,dz;
double xx,yy,zz;
double r_min,r_aver,fx,fy,fz,evdw,stepsize,dd;
double xxnew,yynew,zznew,evdwnew,fxnew,fynew,fznew,dummy;
sig=SIG_H2O;
eps=0.15;
sig_6 = sig*sig*sig*sig*sig*sig;
sig_12 = sig_6*sig_6;
a_vdw = 4.0*sig_12*eps;
b_vdw = 4.0*sig_6*eps;
r_min=sig-0.5; /* 0.5: half grid spacing */
r_aver=sig*pow(2.0,1.0/6.0);
a_vdw_atom=dvector(1,natoms);
b_vdw_atom=dvector(1,natoms);
for (i=1; i<=natoms; i++) {
sig = 0.5*(sigatom[i]+SIG_H2O);
eps = sqrt(0.15*epsatom[i]);
sig_6 = sig*sig*sig*sig*sig*sig;
sig_12 = sig_6*sig_6;
a_vdw_atom[i] = 4.0*sig_12*eps;
b_vdw_atom[i] = 4.0*sig_6*eps;
}
/* estimate number of water molecules: */
maxnwater=0;
for (ii=1; ii<=ngrid; ii++) {
if (dgrid[ii]>r_min) maxnwater++;
}
maxnwater=floor((MAXNWATER_FACTOR*maxnwater)/(4.0/3.0*PI*r_aver*r_aver*r_aver/(2.0*2.0*2.0)))+1;
fprintf(stderr,"\nEstimated number of water molecules: %ld\n",maxnwater);
fprintf(stderr,"Placing water molecules ... ");
xwater=dvector(1,maxnwater);
ywater=dvector(1,maxnwater);
zwater=dvector(1,maxnwater);
replace_water_by_ion=ivector(1,maxnwater);
for (ii=1; ii<=maxnwater; ii++) replace_water_by_ion[ii]=0;
nwater=0;
for (ii=1; ii<=ngrid; ii++) {
if (dgrid[ii]>r_min) {
xx=xgrid[ii];
yy=ygrid[ii];
zz=zgrid[ii];
stepsize=INITIAL_STEPSIZE_FOR_PLACE_WATER;
evdw = vdw_energy_force(xx, yy, zz, &fx, &fy, &fz);
dd=sqrt(fx*fx+fy*fy+fz*fz);
fx /= dd;
fy /= dd;
fz /= dd;
xxnew=xx+stepsize*fx;
yynew=yy+stepsize*fy;
zznew=zz+stepsize*fz;
do {
evdwnew = vdw_energy_force(xxnew, yynew, zznew, &fxnew, &fynew, &fznew);
if (evdwnew<evdw) {
evdw=evdwnew;
dd=sqrt(fxnew*fxnew+fynew*fynew+fznew*fznew);
fx=fxnew/dd;
fy=fynew/dd;
fz=fznew/dd;
xx=xxnew;
yy=yynew;
zz=zznew;
}
else {
stepsize /= 2.0;
}
xxnew=xx+stepsize*fx;
yynew=yy+stepsize*fy;
zznew=zz+stepsize*fz;
} while (stepsize>FINAL_STEPSIZE_FOR_PLACE_WATER);
if (evdw<=0.0) {
dd = density_gradient(xx,yy,zz,&dummy,&dummy,&dummy);
if (dd >= SURFACE_LEVEL) {
nwater++;
fprintf(stderr,"\b\b\b\b\b\b\b%-7ld",nwater);
if (nwater>=maxnwater) {
fprintf(stderr,"\n\nERROR: Too many water molecules (%ld)\n",nwater);
fprintf(stderr," Increase MAXNWATER_FACTOR!\n");
terminate();
}
xwater[nwater] = xx;
ywater[nwater] = yy;
zwater[nwater] = zz;
/* mask grid points around new water molecule: */
if (ii<ngrid) {
for (jj=ii+1; jj<=ngrid; jj++) {
dx=xgrid[jj]-xx;
dy=ygrid[jj]-yy;
dz=zgrid[jj]-zz;
if (dx*dx+dy*dy+dz*dz<=r_min*r_min) dgrid[jj]=0.0;
}
}
}
}
}
}
fprintf(stderr,"\b\b\b\b\b\b\bfound %ld\n",nwater);
free_dvector(xgrid,1,ngrid);
free_dvector(ygrid,1,ngrid);
free_dvector(zgrid,1,ngrid);
free_dvector(dgrid,1,ngrid);
free_dvector(densgrid,1,ngrid);
return;
}
/*
EPS SIGMA
NONBonded C 0.1200 3.7418
NONBonded CH1E 0.0486 4.2140
NONBonded CH2E 0.1142 3.9823
NONBonded CH3E 0.1811 3.8576
NONBonded N 0.2384 2.8509
NONBonded O 0.1591 2.8509
NONBonded OC 0.6469 2.8509
NONBonded S 0.0430 3.3676
sig_ij =(sigma[i]+sigma[j])/2.0;
eps_ij =sqrt(eps[i]*eps[j]);
a_vdw b_vdw
nbfix ot ot 581980.4948 595.0436396
nbfix ht ht 3.085665E-06 7.533363E-04
nbfix ht ot 327.8404792 10.47230620
sig_6 = sig*sig*sig*sig*sig*sig;
sig_12 = sig_6*sig_6;
a_vdw = 4.0*sig_12*eps;
b_vdw = 4.0*sig_6*eps;
r_6 = r_2*r_2*r_2;
vdw_loc_energy = r_6 * (r_6 * a_vdw - b_vdw) / 2.0;
fvdw = r_2 * (24.0*vdw_loc_energy + 6.0*r_6*b_vdw);
fx_h = fvdw * rx;
fy_h = fvdw * ry;
fz_h = fvdw * rz;
*/
static void group_water_molecules(void)
{
long int i,j,count,n1,n2,n3;
int foundflag,finishflag;
double dx,dy,dz,x1,y1,z1,r_aver,rcontact2;
FILE *fp;
fprintf(stderr,"Grouping water molecules ... ");
r_aver=SIG_H2O*pow(2.0,1.0/6.0);
rcontact2=r_aver*r_aver*1.2*1.2;
groupflags = ivector(1,nwater);
nr_of_water_groups=1; /* 1: bulk water group */
for (i=1; i<=nwater; i++) groupflags[i]=0;
groupflags[nwater]=nr_of_water_groups; /* (the outermost water defines group nr.one) */
count=1;
do {
do {
foundflag=0;
for (i=1; i<=nwater; i++) {
if (groupflags[i]==nr_of_water_groups) {
x1=xwater[i];
y1=ywater[i];
z1=zwater[i];
for (j=1; j<=nwater; j++) {
if (groupflags[j]==0) {
dx = x1 - xwater[j];
dy = y1 - ywater[j];
dz = z1 - zwater[j];
if ((dx*dx + dy*dy + dz*dz)<=rcontact2) {
groupflags[j]=nr_of_water_groups;
foundflag=1;
count++;
if (floor(((count+1)*100.0+0.5)/(1.0*nwater))!=floor((count*100.0+0.5)/(1.0*nwater))) {
fprintf(stderr,"\b\b\b\b%3d%%",(int)(floor((count*100.0+1.0)/(1.0*nwater))));
}
}
}
}
}
}
} while (foundflag==1);
finishflag=1;
for (i=nwater; i>=1; i--) {
if (groupflags[i]==0) {
nr_of_water_groups++;
groupflags[i]=nr_of_water_groups;
finishflag=0;
i=0;
}
}
} while (finishflag==0);
fprintf(stderr,"\b\b\b\b100%%\n\n");
n1=0;
n2=0;
n3=0;
if (nr_of_water_groups==1) {
fprintf(stderr,"The water molecules form one group.\n");
}
else {
fprintf(stderr,"The water molecules form %d groups.\n",nr_of_water_groups);
for (i=1; i<=nr_of_water_groups; i++) {
count=0;
for (j=1; j<=nwater; j++) {
if (groupflags[j]==i) count++;
}
if (count==1) fprintf(stderr,"Group %ld contains 1 water molecule\n",i);
if (count!=1) fprintf(stderr,"Group %ld contains %ld water molecules\n",i,count);
if (count==1) n1++;
if (count==2) n2++;
if (count==3) n3++;
}
if (n1==1) fprintf(stderr,"So we have one single, ");
if (n1!=1) fprintf(stderr,"So we have %ld singles, ",n1);
if (n2==1) fprintf(stderr,"one twin, and ");
if (n2!=1) fprintf(stderr,"%ld twins, and ",n2);
if (n3==1) fprintf(stderr,"one triplet\n");
if (n3!=1) fprintf(stderr,"%ld triplets\n",n3);
}
fprintf(stderr,"\n");
if (write_surface_statistics_flag==1) {
fprintf(stderr,"Appending grouping statistics to file surface_stat.lis ... ");
if ((fp=fopen("surface_stat.lis","a"))==NULL) {
fprintf(stderr,"\n\nERROR: Could not append to file surface_stat.lis\n");
terminate();
}
fprintf(fp,"[NUMBER OF GROUPS]\n");
fprintf(fp,"%d\n",nr_of_water_groups);
fprintf(fp,"[NUMBER OF SINGLES]\n");
fprintf(fp,"%ld\n",n1);
fprintf(fp,"[NUMBER OF TWINS]\n");
fprintf(fp,"%ld\n",n2);
fprintf(fp,"[NUMBER OF TRIPLETS]\n");
fprintf(fp,"%ld\n",n3);
fprintf(fp,"[GROUP OCCUPANCY]\n");
for (i=1; i<=nr_of_water_groups; i++) {
count=0;
for (j=1; j<=nwater; j++) {
if (groupflags[j]==i) count++;
}
fprintf(fp,"%ld %ld\n",i,count);
}
fclose(fp);
fprintf(stderr,"done.\n");
}
return;
}
static void place_ions()
{
int number_cl, number_na;
int number_ions;
long int i0,i1,i2,i,j,j0;
double xx,yy,zz,q,sum,dx,dy,dz,dd,r,delta_e,ex;
double total_positive_charge,total_negative_charge;
double total_positive_charge2,total_negative_charge2;
double sum_na,sum_cl;
double *xion;
double *yion;
double *zion;
double *dens_na;
double *dens_cl;
double *prob_na;
double *prob_cl;
double *prob_na_total;
double *prob_cl_total;
double *protein_e_field;
double *debye_hueckel_potential;
int *positions_na;
int *positions_cl;
int *ion_is_here;
int *positions_ion;
int *charge_ion;
fprintf(stderr,"Placing ions ... ");
replace_water_by_ion=ivector(1,nwater);
nbulkwater=0;
for (i=1; i<=nwater; i++) {
if (groupflags[i]==1) nbulkwater++;
}
xion=dvector(1,nbulkwater);
yion=dvector(1,nbulkwater);
zion=dvector(1,nbulkwater);
debye_hueckel_potential=dvector(1,nbulkwater);
protein_e_field=dvector(1,nbulkwater);
dens_na=dvector(1,nbulkwater);
dens_cl=dvector(1,nbulkwater);
prob_na=dvector(1,nbulkwater);
prob_cl=dvector(1,nbulkwater);
prob_na_total=dvector(1,nbulkwater);
prob_cl_total=dvector(1,nbulkwater);
j=1;
for (i=1; i<=nwater; i++) {
if (groupflags[i]==1) {
xion[j] = xwater[i];
yion[j] = ywater[i];
zion[j] = zwater[i];
j++;
}
}
for (j=1; j<=nbulkwater; j++) {
protein_e_field[j] = 0.0;
debye_hueckel_potential[j] = 0.0;
prob_na_total[j]=0.0;
prob_cl_total[j]=0.0;
}
for (i=1; i<=natoms; i++) {
q=qatom[i];
if (q!=0.0) {
xx=xatom[i];
yy=yatom[i];
zz=zatom[i];
for (j=1; j<=nbulkwater; j++) {
dx=xion[j]-xx;
dy=yion[j]-yy;
dz=zion[j]-zz;
dd=sqrt(dx*dx+dy*dy+dz*dz);
protein_e_field[j] += q/dd;
debye_hueckel_potential[j] = (q*exp(-dd/DEBYE_HUECKEL_LENGTH)/dd);
}
sum_na = 0.0;
sum_cl = 0.0;
for (j=1; j<=nbulkwater; j++) {
prob_na[j]=-(ANGSTROM_PER_METER*ELEMENTARY_CHARGE*ELEMENTARY_CHARGE*debye_hueckel_potential[j]/KT/FOUR_PI_EPS_EPSR);
if (prob_na[j]<-1.0) prob_na[j]=-1.0;
prob_cl[j]= (ANGSTROM_PER_METER*ELEMENTARY_CHARGE*ELEMENTARY_CHARGE*debye_hueckel_potential[j]/KT/FOUR_PI_EPS_EPSR);
if (prob_cl[j]<-1.0) prob_cl[j]=-1.0;
sum_na += prob_na[j];
sum_cl += prob_cl[j];
}
for (j=1; j<=nbulkwater; j++) {
prob_na_total[j] -= (prob_na[j]*q/(2.0*sum_na));
prob_cl_total[j] += (prob_cl[j]*q/(2.0*sum_cl));
/* (why 1/2? because, analytically, the integral
over ion density change caused by one unit charge
is equal to 1/2 unit charges for sodium and also
1/2 (opposite sign) for chloride. that makes a total
of one unit charge, which shields the ion.) */
}
}
}
for (j=1; j<=nbulkwater; j++) {
dens_na[j] = 1.0/ISOTONIC_WATER_PER_ION + prob_na_total[j];
if (dens_na[j]<0.0) dens_na[j]=0.0;
dens_cl[j] = 1.0/ISOTONIC_WATER_PER_ION + prob_cl_total[j];
if (dens_cl[j]<0.0) dens_cl[j]=0.0;
}
total_positive_charge=0.0;
total_negative_charge=0.0;
for (j=1; j<=nbulkwater; j++) {
total_positive_charge += dens_na[j];
total_negative_charge += dens_cl[j];
}
number_na = floor(total_positive_charge+0.5);
number_cl = floor(total_negative_charge+0.5);
/* The user might have requested a total charge with the -q flag.
* If so, we adjust the number of ions to place here.
*
* Do not modify total_positive_charge and total_negative_charge here,
* since then the ion placement in the next code block cannot work properly */
if (enforced_total_charge_flag==1)
{
total_positive_charge2 = total_positive_charge;
total_negative_charge2 = total_negative_charge;
if (number_cl-number_na>floor(total_charge-enforced_total_charge+0.5)) {
do {
total_positive_charge2 += 0.001;
total_negative_charge2 -= 0.001;
number_na = floor(total_positive_charge2+0.5);
number_cl = floor(total_negative_charge2+0.5);
} while (number_cl-number_na>floor(total_charge-enforced_total_charge+0.5));
}
if (number_cl-number_na<floor(total_charge-enforced_total_charge+0.5)) {
do {
total_positive_charge2 -= 0.001;
total_negative_charge2 += 0.001;
number_na = floor(total_positive_charge2+0.5);
number_cl = floor(total_negative_charge2+0.5);
} while (number_cl-number_na<floor(total_charge-enforced_total_charge+0.5));
}
if (number_cl < 0 || number_na < 0)
{
fprintf(stderr, "\nERROR: Cannot match the requested total charge of %d.\n",
enforced_total_charge);
exit(1);
}
}
if (number_na>0) positions_na = ivector(1,number_na);
if (number_cl>0) positions_cl = ivector(1,number_cl);
ion_is_here=ivector(1,nbulkwater);
for (j=1; j<=nbulkwater; j++) {
ion_is_here[j]=0;
}
/* place debye-hueckel chloride ions: */
if (number_cl>0) {
for (i=1; i<=number_cl; i++) {
do {
r=total_negative_charge*ran2(&seed);
sum=0.0;
for (j=1; sum<r; j++) {
i0=j;
if (dens_cl[j]>0.0) sum += dens_cl[j];
}
} while (ion_is_here[i0]==1);
ion_is_here[i0]=1;
positions_cl[i]=i0;
}
}
/* place debye-hueckel sodium ions: */
if (number_na>0) {
for (i=1; i<=number_na; i++) {
do {
r=total_positive_charge*ran2(&seed);
sum=0.0;
for (j=1; sum<r; j++) {
i0=j;
if (dens_na[j]>0.0) sum += dens_na[j];
}
} while (ion_is_here[i0]==1);
ion_is_here[i0]=1;
positions_na[i]=i0;
}
}
if ((number_na>0) && (number_cl>0)) {
fprintf(stderr,"placed %d sodium ion",number_na);
if (number_na!=1) fprintf(stderr,"s");
fprintf(stderr," and %d chloride ion",number_cl);
if (number_cl!=1) fprintf(stderr,"s");
fprintf(stderr,".\n");
}
if ((number_na>0) && (number_cl==0)) {
fprintf(stderr,"placed %d sodium ion",number_na);
if (number_na!=1) fprintf(stderr,"s");
fprintf(stderr,".\n");
}
if ((number_na==0) && (number_cl>0)) {
fprintf(stderr,"placed %d chloride ion",number_cl);
if (number_cl!=1) fprintf(stderr,"s");
fprintf(stderr,".\n");
}
if ((number_na==0) && (number_cl==0)) {
fprintf(stderr,"placed no ions.\n");
}
/* make a series of monte-carlo moves to equilibrate the ion cloud: */
number_ions=0;
if (number_na+number_cl>0) {
fprintf(stderr,"Equilibrating ions (%ld Monte Carlo moves) ... ",(long int)NR_OF_DEBYE_HUECKEL_MONTE_CARLO_MOVES);
number_ions=number_na+number_cl;
positions_ion = ivector(1,number_ions);
charge_ion = ivector(1,number_ions);
if (number_na>0) {
for (i=1; i<=number_na; i++) {
positions_ion[i] = positions_na[i];
charge_ion[i]=1;
}
}
if (number_cl>0) {
for (i=1; i<=number_cl; i++) {
positions_ion[i+number_na] = positions_cl[i];
charge_ion[i+number_na]=-1;
}
}
for (i=1; i<=NR_OF_DEBYE_HUECKEL_MONTE_CARLO_MOVES; i++) {
if (floor(((i-1.0)*100.0)/(1.0*NR_OF_DEBYE_HUECKEL_MONTE_CARLO_MOVES))!=floor((i*100.0)/(1.0*NR_OF_DEBYE_HUECKEL_MONTE_CARLO_MOVES))) {
fprintf(stderr,"\b\b\b\b%3d%%",(int)(floor((i*100.0)/(1.0*NR_OF_DEBYE_HUECKEL_MONTE_CARLO_MOVES))));
}
i0=1+floor(((float)number_ions)*ran2(&seed));
i1=positions_ion[i0];
do {
i2=1+floor(((float)nbulkwater)*ran2(&seed));
} while (ion_is_here[i2]==1);
delta_e = charge_ion[i0]*(protein_e_field[i2] - protein_e_field[i1]);
for (j=1; j<=number_ions; j++) {
j0=positions_ion[j];
if (j!=i0) {
dx = xion[i2] - xion[j0];
dy = yion[i2] - yion[j0];
dz = zion[i2] - zion[j0];
dd=sqrt(dx*dx+dy*dy+dz*dz);
delta_e += (charge_ion[i0]*charge_ion[j]/dd);
}
if (j0!=i1) {
dx = xion[i1] - xion[j0];
dy = yion[i1] - yion[j0];
dz = zion[i1] - zion[j0];
dd=sqrt(dx*dx+dy*dy+dz*dz);
delta_e -= (charge_ion[i0]*charge_ion[j]/dd);
}
}
if (delta_e<0.0) {
ion_is_here[i1]=0;
positions_ion[i0]=i2;
ion_is_here[i2]=1;
}
else {
ex=(ANGSTROM_PER_METER*ELEMENTARY_CHARGE*ELEMENTARY_CHARGE/(KT*FOUR_PI_EPS_EPSR))*delta_e;
if (ex<100.0) {
if (exp(-ex)>ran2(&seed)) {
ion_is_here[i1]=0;
positions_ion[i0]=i2;
ion_is_here[i2]=1;
}
}
}
}
}
fprintf(stderr,"\n\n");
j=1;
for (i=1; i<=nwater; i++) replace_water_by_ion[i]=0;
if (number_na+number_cl>0) {
for (i=1; i<=nwater; i++) {
if (groupflags[i]==1) {
if (ion_is_here[j]) {
for (i0=1; i0<=number_ions; i0++) {
if (positions_ion[i0]==j) replace_water_by_ion[i]=charge_ion[i0];
}
}
j++;
}
}
}
free_dvector(xion,1,nbulkwater);
free_dvector(yion,1,nbulkwater);
free_dvector(zion,1,nbulkwater);
free_dvector(dens_na,1,nbulkwater);
free_dvector(dens_cl,1,nbulkwater);
free_dvector(prob_na,1,nbulkwater);
free_dvector(prob_cl,1,nbulkwater);
free_dvector(prob_na_total,1,nbulkwater);
free_dvector(prob_cl_total,1,nbulkwater);
free_dvector(debye_hueckel_potential,1,nbulkwater);
free_dvector(protein_e_field,1,nbulkwater);
free_ivector(ion_is_here,1,nbulkwater);
if (number_na+number_cl>0) {
if (number_na>0) free_ivector(positions_na,1,number_na);
if (number_cl>0) free_ivector(positions_cl,1,number_cl);
free_ivector(positions_ion,1,number_ions);
free_ivector(charge_ion,1,number_ions);
}
nions=number_ions;
return;
}
static void rotate_x(double *x1, double *y1, double *z1, double w)
{
double dummy;
dummy = (*y1)*cos(w) - (*z1)*sin(w);
*z1 = (*y1)*sin(w) + (*z1)*cos(w);
*y1 = dummy;
return;
}
static void rotate_y(double *x1, double *y1, double *z1, double w)
{
double dummy;
dummy = (*z1)*cos(w) - (*x1)*sin(w);
*x1 = (*z1)*sin(w) + (*x1)*cos(w);
*z1 = dummy;
return;
}
static void rotate_z(double *x1, double *y1, double *z1, double w)
{
double dummy;
dummy = (*x1)*cos(w) - (*y1)*sin(w);
*y1 = (*x1)*sin(w) + (*y1)*cos(w);
*x1 = dummy;
return;
}
static void dice_hydrogens(double x0, double y0, double z0, double *x1, double *y1, double *z1, double *x2, double *y2, double *z2)
{
double alpha,theta,phi;
*x1 = BOND_LENGTH_H2O * sin(BOND_ANGLE_H2O/2.0);
*x2 = -(*x1);
*y1 = 0.0;
*y2 = 0.0;
*z1 = BOND_LENGTH_H2O * cos(BOND_ANGLE_H2O/2.0);
*z2 = *z1;
alpha = 2.0*PI*ran2(&seed);
rotate_z(x1,y1,z1,alpha);
rotate_z(x2,y2,z2,alpha);
theta = acos(2.0*ran2(&seed)-1.0);
rotate_y(x1,y1,z1,theta);
rotate_y(x2,y2,z2,theta);
phi = 2.0*PI*ran2(&seed);
rotate_z(x1,y1,z1,phi);
rotate_z(x2,y2,z2,phi);
*x1 += x0;
*y1 += y0;
*z1 += z0;
*x2 += x0;
*y2 += y0;
*z2 += z0;
return;
}
static void check_max_atom_nr(long int nr)
{
if (nr>MAX_NR_OF_PDB_ATOMS-2) {
fprintf(stderr,"\n\nERROR: Too many atoms for pdb-file. Maximum is %ld.\n",(long int)MAX_NR_OF_PDB_ATOMS);
terminate();
}
return;
}
static void check_max_segment_nr(int nr)
{
if (nr>MAX_NR_OF_PDB_SEGMENTS) {
fprintf(stderr,"\n\nERROR: Too many segments for pdb-file. Maximum is %d.\n",MAX_NR_OF_PDB_SEGMENTS);
fprintf(stderr," You seem to have encountered the unlikely situation\n");
fprintf(stderr," that there are more than 900 isolated water groups,\n");
fprintf(stderr," each of which is given an individual segment-id.\n");
fprintf(stderr," Look for lines 'res_id++;' in the source and remove them!\n");
terminate();
}
return;
}
static void write_xplor_segment(FILE *fp, int segid, long int nr, int diff)
{
fprintf(fp,"segment\n");
fprintf(fp," name=W%3d\n",segid);
fprintf(fp," molecule\n");
fprintf(fp," name=TIP3\n");
fprintf(fp," number=%ld\n",nr);
fprintf(fp," end\n");
fprintf(fp,"end\n");
fprintf(fp,"vector do (resid = encode(decode(resid) + %d)) (segid W%3d)\n\n",diff,segid);
return;
}
static void write_xplor_segment_na(FILE *fp, long int nr)
{
fprintf(fp,"segment\n");
fprintf(fp," name=NA\n");
fprintf(fp," molecule\n");
fprintf(fp," name=INA\n");
fprintf(fp," number=%ld\n",nr);
fprintf(fp," end\n");
fprintf(fp,"end\n");
return;
}
static void write_xplor_segment_cl(FILE *fp, long int nr)
{
fprintf(fp,"segment\n");
fprintf(fp," name=CL\n");
fprintf(fp," molecule\n");
fprintf(fp," name=ICL\n");
fprintf(fp," number=%ld\n",nr);
fprintf(fp," end\n");
fprintf(fp,"end\n");
return;
}
static void write_water_molecules(char *infile, char *outfile)
{
long int i,count;
int j,prev_res_id,res_id,seg_id;
double x0,y0,z0,x1,y1,z1,x2,y2,z2;
char infile1[MAX_STRING_SIZE+1];
char outfile1[MAX_STRING_SIZE+1];
char xplorfile[MAX_STRING_SIZE+1];
char psfinfile[MAX_STRING_SIZE+1];
char psfoutfile[MAX_STRING_SIZE+1];
FILE *fp_in, *fp_out, *fp_xplor;
strcpy(infile1,infile);
strcat(infile1,".pdb");
strcpy(outfile1,outfile);
strcat(outfile1,".pdb");
strcpy(psfinfile,infile);
strcat(psfinfile,".psf");
strcpy(psfoutfile,outfile);
strcat(psfoutfile,".psf");
strcpy(xplorfile,XPLOR_SCRIPT_NAME);
if (output_bulk_water_only_flag==0) {
if ((output_no_solute_flag==0) && (no_solute_present_flag==0)) {
if (add_ions_flag==0) {
fprintf(stderr,"Saving solute and %ld water molecules to file %s ... ",nwater,outfile1);
}
else {
fprintf(stderr,"Saving solute, %ld water molecules, and %ld ions to file %s ... ",nwater-nions,nions,outfile1);
}
}
else {
if (add_ions_flag==0) {
fprintf(stderr,"Saving %ld water molecules to file %s ... ",nwater,outfile1);
}
else {
fprintf(stderr,"Saving %ld water molecules and %ld ions to file %s ... ",nwater-nions,nions,outfile1);
}
}
}
else {
count=0;
for (i=1; i<=nwater; i++) {if (groupflags[i]==1) count++;}
if ((output_no_solute_flag==0) && (no_solute_present_flag==0)) {
if (add_ions_flag==0) {
fprintf(stderr,"Saving solute and %ld bulk water molecules to file %s ... ",count,outfile1);
}
else {
fprintf(stderr,"Saving solute, %ld bulk water molecules, and %ld ions to file %s ... ",count-nions,nions,outfile1);
}
}
else {
if (add_ions_flag==0) {
fprintf(stderr,"Saving %ld bulk water molecules to file %s ... ",count,outfile1);
}
else {
fprintf(stderr,"Saving %ld bulk water molecules and %ld ions to file %s ... ",count-nions,nions,outfile1);
}
}
}
if ((fp_out=fopen(outfile1,"w"))==NULL) {
fprintf(stderr,"\n\nERROR: Could not write to file %s .\n",outfile1);
terminate();
}
fprintf(fp_out,"REMARK Created by SOLVATE 1.0.1 (C) 1996-2010 Helmut Grubmuller\n");
fprintf(fp_out,"REMARK http://www.imo.physik.uni-muenchen.de/~grubi\n");
fprintf(fp_out,"REMARK Helmut.Grubmueller@Physik.uni-muenchen.de\n");
fprintf(fp_out,"REMARK \n");
if ((output_no_solute_flag==0) && (no_solute_present_flag==0)) {
if ((fp_in=fopen(infile1,"r"))==NULL) {
fprintf(stderr,"\n\nERROR: Could not read from file %s .\n",outfile1);
fprintf(stderr," (Yes, we still need the file as a template!)\n");
terminate();
}
for (i=1; i<=natoms; i++) {
patch_pdb_coords(fp_in,fp_out,xatom[i],yatom[i],zatom[i]);
}
}
else {
max_atom_nr_in_pdb=0;
}
seg_id=100;
res_id=1;
if ((output_bulk_water_only_flag==0) && (nr_of_water_groups>1)) {
for (j=2; j<=nr_of_water_groups; j++) {
for (i=1; i<=nwater; i++) {
if (groupflags[i]==j) {
max_atom_nr_in_pdb++;
check_max_atom_nr(max_atom_nr_in_pdb);
x0=xwater[i];
y0=ywater[i];
z0=zwater[i];
dice_hydrogens(x0,y0,z0,&x1,&y1,&z1,&x2,&y2,&z2);
add_pdb_tip(fp_out,0,x0,y0,z0,max_atom_nr_in_pdb,res_id,seg_id);
max_atom_nr_in_pdb++;
add_pdb_tip(fp_out,1,x1,y1,z1,max_atom_nr_in_pdb,res_id,seg_id);
max_atom_nr_in_pdb++;
add_pdb_tip(fp_out,2,x2,y2,z2,max_atom_nr_in_pdb,res_id,seg_id);
res_id++;
if (res_id>MAX_NR_OF_PDB_GROUPS) {
res_id=1;
seg_id++;
check_max_segment_nr(seg_id);
}
}
}
if (res_id!=1) {
seg_id++;
check_max_segment_nr(seg_id);
}
}
}
for (i=1; i<=nwater; i++) {
if ((groupflags[i]==1) && (replace_water_by_ion[i]==0)) {
max_atom_nr_in_pdb++;
check_max_atom_nr(max_atom_nr_in_pdb);
x0=xwater[i];
y0=ywater[i];
z0=zwater[i];
dice_hydrogens(x0,y0,z0,&x1,&y1,&z1,&x2,&y2,&z2);
add_pdb_tip(fp_out,0,x0,y0,z0,max_atom_nr_in_pdb,res_id,seg_id);
max_atom_nr_in_pdb++;
add_pdb_tip(fp_out,1,x1,y1,z1,max_atom_nr_in_pdb,res_id,seg_id);
max_atom_nr_in_pdb++;
add_pdb_tip(fp_out,2,x2,y2,z2,max_atom_nr_in_pdb,res_id,seg_id);
res_id++;
if (res_id>MAX_NR_OF_PDB_GROUPS) {
res_id=1;
seg_id++;
check_max_segment_nr(seg_id);
}
}
}
res_id=1;
for (i=1; i<=nwater; i++) {
if (replace_water_by_ion[i]>0) {
max_atom_nr_in_pdb++;
check_max_atom_nr(max_atom_nr_in_pdb);
x0=xwater[i];
y0=ywater[i];
z0=zwater[i];
add_pdb_ion(fp_out,replace_water_by_ion[i],x0,y0,z0,max_atom_nr_in_pdb,res_id);
res_id++;
if (res_id>MAX_NR_OF_PDB_GROUPS) { /* very unlikely */
res_id=1;
}
}
}
res_id=1;
for (i=1; i<=nwater; i++) {
if (replace_water_by_ion[i]<0) {
max_atom_nr_in_pdb++;
check_max_atom_nr(max_atom_nr_in_pdb);
x0=xwater[i];
y0=ywater[i];
z0=zwater[i];
add_pdb_ion(fp_out,replace_water_by_ion[i],x0,y0,z0,max_atom_nr_in_pdb,res_id);
res_id++;
if (res_id>MAX_NR_OF_PDB_GROUPS) { /* very unlikely */
res_id=1;
}
}
}
if ((output_no_solute_flag==0) && (no_solute_present_flag==0)) {
fclose(fp_in);
}
fprintf(fp_out,"END\n");
fclose(fp_out);
fprintf(stderr,"done.\n");
/* output xplor-script */
if (output_mkpsf_script==1) {
fprintf(stderr,"Writing XPLOR-script %s ... ",xplorfile);
if ((fp_xplor=fopen(xplorfile,"w"))==NULL) {
fprintf(stderr,"\n\nERROR: Could not write to file %s .\n",xplorfile);
terminate();
}
fprintf(fp_xplor,"!***************************************************************\n");
fprintf(fp_xplor,"!* Make structure file for water shell *\n");
fprintf(fp_xplor,"!* Created by SOLVATE 1.0.1 (C) 1996-2010 Helmut Grubmuller *\n");
fprintf(fp_xplor,"!* http://www.mpibpc.mpg.de/home/grubmueller/downloads/solvate *\n");
fprintf(fp_xplor,"!* hgrubmu@gwdg.de *\n");
fprintf(fp_xplor,"!***************************************************************\n\n\n");
fprintf(fp_xplor,"struct @%s end\n\n",psfinfile);
fprintf(fp_xplor,"topo @%s end\n",XPLOR_TOPOLOGY_FILE_WATER);
fprintf(fp_xplor,"topo @%s end\n",XPLOR_TOPOLOGY_FILE_IONS);
fprintf(fp_xplor,"param @%s end\n\n",XPLOR_PARAMETER_FILE_WATER);
fprintf(fp_xplor,"param @%s end\n\n",XPLOR_PARAMETER_FILE_IONS);
seg_id=100;
res_id=1;
count=0;
if ((output_bulk_water_only_flag==0) && (nr_of_water_groups>1)) {
for (j=2; j<=nr_of_water_groups; j++) {
prev_res_id=res_id;
for (i=1; i<=nwater; i++) {
if (groupflags[i]==j) {
count++;
res_id++;
if (res_id>MAX_NR_OF_PDB_GROUPS) {
write_xplor_segment(fp_xplor,seg_id,count,prev_res_id-1);
res_id=1;
prev_res_id=res_id;
seg_id++;
count=0;
}
}
}
if (res_id!=1) {
write_xplor_segment(fp_xplor,seg_id,count,prev_res_id-1);
seg_id++;
count=0;
}
}
}
prev_res_id=res_id;
for (i=1; i<=nwater; i++) {
if ((groupflags[i]==1) && (replace_water_by_ion[i]==0)) {
count++;
res_id++;
if (res_id>MAX_NR_OF_PDB_GROUPS) {
write_xplor_segment(fp_xplor,seg_id,count,prev_res_id-1);
res_id=1;
prev_res_id=res_id;
seg_id++;
count=0;
}
}
}
write_xplor_segment(fp_xplor,seg_id,count,prev_res_id-1);
count=0;
for (i=1; i<=nwater; i++) {
if (replace_water_by_ion[i]==1) count++;
}
if (count>0) {
write_xplor_segment_na(fp_xplor,count);
}
count=0;
for (i=1; i<=nwater; i++) {
if (replace_water_by_ion[i]==-1) count++;
}
if (count>0) {
write_xplor_segment_cl(fp_xplor,count);
}
fprintf(fp_xplor,"coord @%s\n\n",outfile1);
fprintf(fp_xplor,"vector do (q = 0.0) (resname TIP3)\n");
fprintf(fp_xplor,"vector do (b = 0.0) (resname TIP3)\n\n");
fprintf(fp_xplor,"vector do (b = 0.0) (resname INA)\n\n");
fprintf(fp_xplor,"vector do (b = 0.0) (resname ICL)\n\n");
fprintf(fp_xplor,"write struct output=%s end\n",psfoutfile);
fprintf(fp_xplor,"write coors output=%s end\n\n",outfile1);
fprintf(fp_xplor,"stop\n");
fclose(fp_xplor);
fprintf(stderr,"done.\n");
fprintf(stderr,"(Use `xplor < %s' to create structure fiele %s.)\n",xplorfile,psfoutfile);
}
free_ivector(groupflags,1,nwater);
free_dvector(xwater,1,maxnwater);
free_dvector(ywater,1,maxnwater);
free_dvector(zwater,1,maxnwater);
free_ivector(replace_water_by_ion,1,maxnwater);
return;
}
/*****************************************/
/*** came from numerical recipies ********/
/*****************************************/
static double ***dcube(int nrl, int nrh, int ncl, int nch, int npl, int nph)
{
int i,j;
double ***m;
m=(double ***) alloc_ptrs(nrh-nrl+1);
if (!m) {
fprintf(stderr,"\n\nERROR: allocation failure 1 in dmatrix()\n");
terminate();
}
m -= nrl;
for(i=nrl;i<=nrh;i++) {
m[i]= (double **) alloc_ptrs(nch-ncl+1);
if (!m[i]) {
fprintf(stderr,"\n\nERROR: allocation failure 2 in dmatrix()\n");
terminate();
}
m[i] -= ncl;
}
for(i=nrl;i<=nrh;i++) {
for(j=ncl;j<=nch;j++) {
m[i][j]= alloc_doubles(nph-npl+1);
if (!m[i][j]) {
fprintf(stderr,"\n\nERROR: allocation failure 3 in dmatrix()\n");
terminate();
}
m[i][j] -= npl;
}
}
return m;
}
static void free_dcube(double ***m, int nrl, int nrh, int ncl, int nch, int npl, int nph)
{
int i=nph; /*** no warning/complete confusion? ***/
int j=nch; /*** no warning/complete confusion? ***/
for(i=nrh;i>=nrl;i--) {
for(j=nch;j>=ncl;j--) {
free((char*) (m[i][j]+npl));
}
}
for(i=nrh;i>=nrl;i--) {
free((char*) (m[i]+ncl));
}
free((char*) (m+nrl));
}
static double **dmatrix(int nrl, int nrh, int ncl, int nch)
{
int i;
double **m;
m=(double **) alloc_ptrs(nrh-nrl+1);
if (!m) {
fprintf(stderr,"\n\nERROR: allocation failure 1 in dmatrix()\n");
terminate();
}
m -= nrl;
for(i=nrl;i<=nrh;i++) {
m[i]= alloc_doubles(nch-ncl+1);
if (!m[i]) {
fprintf(stderr,"\n\nERROR: allocation failure 2 in dmatrix()\n");
terminate();
}
m[i] -= ncl;
}
return m;
}
static void free_dmatrix(double **m, int nrl, int nrh, int ncl, int nch)
{
int i=nch; /*** no warning ***/
for(i=nrh;i>=nrl;i--) free((char*) (m[i]+ncl));
free((char*) (m+nrl));
}
/********************************************/
static int *ivector(int nl,int nh)
{
int *v;
v=alloc_ints(nh-nl+1);
if (!v) {
fprintf(stderr,"\n\nERROR: allocation failure in ivector()\n");
terminate();
}
return v-nl;
}
static void free_ivector(int *v, int nl, int nh)
{
int i = nh; /*** no warning ***/
i++;
free((char*) (v+nl));
}
/********************************************/
static double *dvector(int nl,int nh)
{
double *v;
v=alloc_doubles(nh-nl+1);
if (!v) {
fprintf(stderr,"\n\nERROR: allocation failure in dvector()\n");
terminate();
}
return v-nl;
}
static void free_dvector(double *v, int nl, int nh)
{
int i = nh; /*** no warning ***/
i++;
free((char*) (v+nl));
}
/*********************************************/
#define SIGN(a,b) ((b)<0 ? -fabs(a) : fabs(a))
static void tred2(double **a,int n,double *d,double *e)
{
int l,k,j,i;
double scale,hh,h,g,f;
for (i=n;i>=2;i--) {
l=i-1;
h=scale=0.0;
if (l > 1) {
for (k=1;k<=l;k++)
scale += fabs(a[i][k]);
if (scale == 0.0)
e[i]=a[i][l];
else {
for (k=1;k<=l;k++) {
a[i][k] /= scale;
h += a[i][k]*a[i][k];
}
f=a[i][l];
g = f>0 ? -sqrt(h) : sqrt(h);
e[i]=scale*g;
h -= f*g;
a[i][l]=f-g;
f=0.0;
for (j=1;j<=l;j++) {
/* Next statement can be omitted if eigenvectors not wanted */
a[j][i]=a[i][j]/h;
g=0.0;
for (k=1;k<=j;k++)
g += a[j][k]*a[i][k];
for (k=j+1;k<=l;k++)
g += a[k][j]*a[i][k];
e[j]=g/h;
f += e[j]*a[i][j];
}
hh=f/(h+h);
for (j=1;j<=l;j++) {
f=a[i][j];
e[j]=g=e[j]-hh*f;
for (k=1;k<=j;k++)
a[j][k] -= (f*e[k]+g*a[i][k]);
}
}
} else
e[i]=a[i][l];
d[i]=h;
}
/* Next statement can be omitted if eigenvectors not wanted */
d[1]=0.0;
e[1]=0.0;
/* Contents of this loop can be omitted if eigenvectors not
wanted except for statement d[i]=a[i][i]; */
for (i=1;i<=n;i++) {
l=i-1;
if (d[i]) {
for (j=1;j<=l;j++) {
g=0.0;
for (k=1;k<=l;k++)
g += a[i][k]*a[k][j];
for (k=1;k<=l;k++)
a[k][j] -= g*a[k][i];
}
}
d[i]=a[i][i];
a[i][i]=1.0;
for (j=1;j<=l;j++) a[j][i]=a[i][j]=0.0;
}
}
/****************************************************************/
static void tqli(double *d,double *e,int n,double **z)
{
int m,l,iter,i,k;
double s,r,p,g,f,dd,c,b;
for (i=2;i<=n;i++) e[i-1]=e[i];
e[n]=0.0;
for (l=1;l<=n;l++) {
iter=0;
do {
for (m=l;m<=n-1;m++) {
dd=fabs(d[m])+fabs(d[m+1]);
if (fabs(e[m])+dd == dd) break;
}
if (m != l) {
if (iter++ == 30) {
fprintf(stderr,"\n\nERROR: Too many iterations in TQLI\n");
terminate();
}
g=(d[l+1]-d[l])/(2.0*e[l]);
r=sqrt((g*g)+1.0);
g=d[m]-d[l]+e[l]/(g+SIGN(r,g));
s=c=1.0;
p=0.0;
for (i=m-1;i>=l;i--) {
f=s*e[i];
b=c*e[i];
if (fabs(f) >= fabs(g)) {
c=g/f;
r=sqrt((c*c)+1.0);
e[i+1]=f*r;
c *= (s=1.0/r);
} else {
s=f/g;
r=sqrt((s*s)+1.0);
e[i+1]=g*r;
s *= (c=1.0/r);
}
g=d[i+1]-p;
r=(d[i]-g)*s+2.0*c*b;
p=s*r;
d[i+1]=g+p;
g=c*r-b;
/* Next loop can be omitted if eigenvectors not wanted */
for (k=1;k<=n;k++) {
f=z[k][i+1];
z[k][i+1]=s*z[k][i]+c*f;
z[k][i]=c*z[k][i]-s*f;
}
}
d[l]=d[l]-p;
e[l]=g;
e[m]=0.0;
}
} while (m != l);
}
}
/***************************************************************************************/
static void sort(int n, double *ra)
{
int l,j,ir,i;
double rra;
l=(n >> 1)+1;
ir=n;
for (;;) {
if (l > 1) {
rra=ra[--l];
} else {
rra=ra[ir];
ra[ir]=ra[1];
if (--ir == 1) {
ra[1]=rra;
return;
}
}
i=l;
j=l << 1;
while (j <= ir) {
if (j < ir && ra[j] < ra[j+1]) ++j;
if (rra < ra[j]) {
ra[i]=ra[j];
j += (i=j);
}
else j=ir+1;
}
ra[i]=rra;
}
}
static void sort2(int n, double *ra, double *rb)
{
int l,j,ir,i;
double rra;
int rrb;
l=(n >> 1)+1;
ir=n;
for (;;) {
if (l > 1) {
rra=ra[--l];
rrb=rb[l];
} else {
rra=ra[ir];
rrb=rb[ir];
ra[ir]=ra[1];
rb[ir]=rb[1];
if (--ir == 1) {
ra[1]=rra;
rb[1]=rrb;
return;
}
}
i=l;
j=l << 1;
while (j <= ir) {
if (j < ir && ra[j] < ra[j+1]) ++j;
if (rra < ra[j]) {
ra[i]=ra[j];
rb[i]=rb[j];
j += (i=j);
}
else j=ir+1;
}
ra[i]=rra;
rb[i]=rrb;
}
}
static void sort5(int n, double *ra, double *rb, double *rc, double *rd, double *re)
{
int l,j,ir,i;
float rre,rrd,rrc,rrb,rra;
l=(n >> 1)+1;
ir=n;
for (;;) {
if (l > 1) {
rra=ra[--l];
rrb=rb[l];
rrc=rc[l];
rrd=rd[l];
rre=re[l];
} else {
rra=ra[ir];
rrb=rb[ir];
rrc=rc[ir];
rrd=rd[ir];
rre=re[ir];
ra[ir]=ra[1];
rb[ir]=rb[1];
rc[ir]=rc[1];
rd[ir]=rd[1];
re[ir]=re[1];
if (--ir == 1) {
ra[1]=rra;
rb[1]=rrb;
rc[1]=rrc;
rd[1]=rrd;
re[1]=rre;
return;
}
}
i=l;
j=l << 1;
while (j <= ir) {
if (j < ir && ra[j] < ra[j+1]) ++j;
if (rra < ra[j]) {
ra[i]=ra[j];
rb[i]=rb[j];
rc[i]=rc[j];
rd[i]=rd[j];
re[i]=re[j];
j += (i=j);
}
else j=ir+1;
}
ra[i]=rra;
rb[i]=rrb;
rc[i]=rrc;
rd[i]=rrd;
re[i]=rre;
}
return;
}
static int *alloc_ints(int len)
{
int *ptr;
size_t nobytes;
if (len<1) len = 1;
nobytes = (size_t) (len * sizeof(int));
ptr = (int *)malloc(nobytes);
return (ptr);
}
static double *alloc_doubles(int len)
{
double *ptr;
size_t nobytes;
if (len<1) len = 1;
nobytes = (size_t) (len * sizeof(double));
ptr = (double *)malloc(nobytes);
return (ptr);
}
static int **alloc_ptrs(int len)
{
int **ptr;
size_t nobytes;
if (len < 1) len = 1;
nobytes = (size_t) (len * sizeof(int*));
ptr = (int **)malloc(nobytes);
return (ptr);
}
#define M 714025
#define IA 1366
#define IC 150889
static double ran2(long *idum)
{
static long iy,ir[98];
static int iff=0;
int j;
if (*idum < 0 || iff == 0) {
iff=1;
if ((*idum=(IC-(*idum)) % M) < 0) *idum = -(*idum);
for (j=1;j<=97;j++) {
*idum=(IA*(*idum)+IC) % M;
ir[j]=(*idum);
}
*idum=(IA*(*idum)+IC) % M;
iy=(*idum);
}
j=1 + 97.0*iy/M;
if (j > 97 || j < 1) {
fprintf(stderr,"\n\nERROR: This should really not happen. Sorry.\n");
terminate();
}
iy=ir[j];
*idum=(IA*(*idum)+IC) % M;
ir[j]=(*idum);
return (double) iy/M;
}
#undef M
#undef IA
#undef IC
static double rang(void)
/* Gibt Zufallsvariable mit in 8.Ordnung angenaeherter Gaussverteilung
um 0.0 und Varianz 1.0 */
{
int i;
double r;
r=0.0;
for (i=1; i<=8; i++) {
r=r+ran2(&seed);
}
/*
Zentrieren:
v=v-8*1/2
Mitteln:
v=v/8
1/(2*sqrt(3)) ist Varianz der Gleichverteilung in [0,1]:
v=v*2*sqrt(3)
Varianz der Summe von n Zufallszahlen geht mit 1/sqrt(n):
v=v*sqrt(8)
daher:
v=(v-4)/sqrt(3/2)
*/
return (r-4.0)*1.2247448713;
}
static void bad_input(void)
{
fprintf(stderr,"usage: solvate [-t thick] [-r radius] [-n ngauss] [-ug] [-ub]\n");
fprintf(stderr," [-s] [-v] [-bulk] [-w] [-ion] [-q charge] [-psf] [infile] outfile\n");
fprintf(stderr,"\n");
fprintf(stderr," thick: minimum water shell thickness in Angstrom (default: 10.0)\n");
fprintf(stderr," radius: maximum boundary curvature radius in Angstrom (default: 100000.0)\n");
fprintf(stderr," ngauss: use ngauss gaussians to define solvent boundary (default: 1)\n");
fprintf(stderr," -s write surface statistics to file surface_stat.lis\n");
fprintf(stderr," -v write volume statistics to file volume_stat.lis\n");
fprintf(stderr," -ug use gaussians from file gaussians.lis\n");
fprintf(stderr," (skip gaussian learning phase)\n");
fprintf(stderr," -ub use boundary definition from file boundary.lis\n");
fprintf(stderr," (skip gaussian learning phase and boundary adjustment)\n");
fprintf(stderr," -bulk output only bulk water (i.e., no buried water molecules)\n");
fprintf(stderr," -w output only water molecules (i.e., not the solute)\n");
fprintf(stderr," -ion add ions (na+/cl-) in isotonic/Debye-Hueckel concentration\n");
fprintf(stderr," charge: force total charge of output system after ion placement\n");
fprintf(stderr," (Debye-Hueckel ion placement in small systems may lead to a net charge)\n");
fprintf(stderr," -psf output XPLOR-script %s for generating a psf-file\n",XPLOR_SCRIPT_NAME);
fprintf(stderr," infile: pdb-file (no extension) of solute\n");
fprintf(stderr," (if omitted, a water droplet is created)\n");
fprintf(stderr," outfile: pdb-file (no extension) of water shell\n");
fprintf(stderr,"\n");
exit(1);
}
/****************************************************************************/
/****************************************************************************/
/****************************************************************************/
int main(int argc,char *argv[])
{
int i;
double dummy;
char infile[MAX_STRING_SIZE+1];
char outfile[MAX_STRING_SIZE+1];
fprintf(stderr,"\n\n\n");
fprintf(stderr," .:::::.. SOLVATE 1.0.1 (C) 1996-2010 Helmut Grubmuller\n");
fprintf(stderr," ::oOOOoo:: Purpose: make a water shell around solutes\n");
fprintf(stderr," ::OOO:::: http://www.mpibpc.mpg.de/home/grubmueller/downloads/solvate\n");
fprintf(stderr," ::::: hgrubmu@gwdg.de\n\n");
if ((argc<2) ||(argc>16)) bad_input();
water_shell_thickness = DEFAULT_WATER_SHELL_THICKNESS;
boundary_radius = DEFAULT_BOUNDARY_RADIUS;
ncodebook = DEFAULT_NR_OF_GAUSSIANS;
write_surface_statistics_flag = 0;
write_volume_statistics_flag = 0;
use_gaussians_flag = 0;
use_boundary_flag = 0;
output_bulk_water_only_flag = 0;
no_solute_present_flag = 0;
output_no_solute_flag = 0;
add_ions_flag = 0;
output_mkpsf_script = 0;
water_shell_thickness_flag = 0;
ncodebook_flag = 0;
sprintf(infile," ");
sprintf(outfile," ");
for (i=1; i<argc; i++) {
if (strcmp(argv[i],"-t") == 0) {
if (i==argc-1) {
fprintf(stderr,"\n\nERROR: Missing water shell thickness.\n");
exit(1);
}
water_shell_thickness=atof(argv[i+1]);
i++;
if (water_shell_thickness<1.0) {
fprintf(stderr,"\n\nERROR: Water shell thickness must be at least 1 A.\n");
exit(1);
}
if (water_shell_thickness>1000.0) {
fprintf(stderr,"\n\nERROR: Water shell thickness must be < 1000 A.\n");
exit(1);
}
water_shell_thickness_flag=1;
}
else if (strcmp(argv[i],"-r") == 0) {
if (i==argc-1) {
fprintf(stderr,"\n\nERROR: Missing boundary curvature radius.\n");
exit(1);
}
boundary_radius=atof(argv[i+1]);
i++;
if (boundary_radius<=0.0) {
fprintf(stderr,"\n\nERROR: A positive boundary curvature radius is recommended.\n");
exit(1);
}
}
else if (strcmp(argv[i],"-n") == 0) {
if (i==argc-1) {
fprintf(stderr,"\n\nERROR: Missing number of gaussians.\n");
exit(1);
}
dummy=atof(argv[i+1]);
ncodebook=(int)(floor(dummy));
i++;
if (((double)ncodebook)!=dummy) {
fprintf(stderr,"\n\nERROR: Have you ever seen %f gaussians ?\n",dummy-ncodebook);
exit(1);
}
if (ncodebook<1) {
fprintf(stderr,"\n\nERROR: You should at least use *one* gaussian.\n");
exit(1);
}
if (ncodebook>1000) {
fprintf(stderr,"\n\nERROR: Use of more than 1000 gaussians is nonsense.\n");
exit(1);
}
ncodebook_flag=1;
}
else if (strcmp(argv[i],"-s") == 0) {
write_surface_statistics_flag = 1;
}
else if (strcmp(argv[i],"-v") == 0) {
write_volume_statistics_flag = 1;
}
else if (strcmp(argv[i],"-ug") == 0) {
use_gaussians_flag = 1;
}
else if (strcmp(argv[i],"-ub") == 0) {
use_boundary_flag = 1;
}
else if (strcmp(argv[i],"-bulk") == 0) {
output_bulk_water_only_flag = 1;
}
else if (strcmp(argv[i],"-w") == 0) {
output_no_solute_flag = 1;
}
else if (strcmp(argv[i],"-ion") == 0) {
add_ions_flag = 1;
}
else if (strcmp(argv[i],"-q") == 0) {
if (i==argc-1) {
fprintf(stderr,"\n\nERROR: Missing value for the total charge after ion placement.\n");
exit(1);
}
enforced_total_charge=atof(argv[i+1]);
i++;
enforced_total_charge_flag=1;
}
else if (strcmp(argv[i],"-psf") == 0) {
output_mkpsf_script = 1;
}
else {
if (strncmp(argv[i],"-",1) == 0) {
fprintf(stderr,"\n\nERROR: Illegal option %s\n\n",argv[i]);
bad_input();
}
else {
if (strcmp(infile," ") == 0) {
strcpy(infile,argv[i]);
}
else {
if (strcmp(outfile," ") == 0) {
strcpy(outfile,argv[i]);
}
else bad_input();
}
}
}
}
if (strcmp(infile," ") == 0) {
fprintf(stderr,"\n\nERROR: Missing output filename\n\n");
bad_input();
}
if (strcmp(outfile," ") == 0) {
strcpy(outfile,infile);
strcpy(infile," ");
no_solute_present_flag=1;
}
if ((use_boundary_flag == 1) && (water_shell_thickness_flag==1)) {
fprintf(stderr,"\n\nWARNING: Given water shell thickness is ignored,\n");
fprintf(stderr," since it is beeing read from file boundary.lis\n\n");
}
if (((use_boundary_flag == 1) || (use_gaussians_flag==1)) && (ncodebook_flag==1)) {
fprintf(stderr,"\n\nWARNING: Given number of gaussians is ignored,\n");
fprintf(stderr," since it is beeing read from file.\n\n");
}
if ((output_mkpsf_script == 1) && (output_no_solute_flag==1)) {
fprintf(stderr,"\n\nWARNING: You should not use both -w and -psf.\n");
fprintf(stderr," (XPLOR-script will not work if solute is not output.)\n\n");
}
if (enforced_total_charge_flag==1 && add_ions_flag==0) {
fprintf(stderr,"\n\nWARNING: -q will only take effect together with -ion.\n\n");
}
/*
printf("\n");
printf("water_shell_thickness: %f\n",water_shell_thickness);
printf("boundary_radius: %f\n",boundary_radius);
printf("ncodebook: %d\n",ncodebook);
printf("write_surface_statistics_flag: %d\n",write_surface_statistics_flag);
printf("write_volume_statistics_flag: %d\n",write_volume_statistics_flag);
printf("use_gaussians_flag: %d\n",use_gaussians_flag);
printf("use_boundary_flag: %d\n",use_boundary_flag);
printf("output_bulk_water_only_flag: %d\n",output_bulk_water_only_flag);
printf("no_solute_present_flag: %d\n",no_solute_present_flag);
printf("output_no_solute_flag: %d\n",output_no_solute_flag);
printf("output_mkpsf_script: %d\n",output_mkpsf_script);
printf("infile: |%s|\n",infile);
printf("outfile: |%s|\n",outfile);
printf("\n");
*/
seed=123;
read_pdb_file(infile);
if (add_ions_flag==1) read_atom_charges_from_psf_file(infile);
if ((use_gaussians_flag==0) && (use_boundary_flag==0)) {
calc_bounding_sphere();
create_convex_volume();
dice_data();
learn_gaussians();
save_gaussian_parameters();
adjust_surface_distance();
save_boundary_parameters();
}
if ((use_gaussians_flag==1) && (use_boundary_flag==0)) {
read_gaussian_parameters();
adjust_surface_distance();
save_boundary_parameters();
}
if ((use_gaussians_flag==0) && (use_boundary_flag==1)) {
read_boundary_parameters();
}
if ((use_gaussians_flag==1) && (use_boundary_flag==1)) {
fprintf(stderr,"ERROR: You cannot use both -ug and -ub\n\n");
exit(1);
}
create_volume();
surface_statistics();
place_water_molecules();
group_water_molecules();
if (add_ions_flag==1) place_ions();
write_water_molecules(infile,outfile);
free_dmatrix(gauss_y,1,3,1,ncodebook);
free_dcube(gauss_w,1,3,1,3,1,ncodebook);
free_dmatrix(gauss_rho2,1,3,1,ncodebook);
free_dvector(xatom,1,natoms);
free_dvector(yatom,1,natoms);
free_dvector(zatom,1,natoms);
free_dvector(qatom,1,natoms);
free_dvector(sigatom,1,natoms);
free_dvector(epsatom,1,natoms);
free_dvector(a_vdw_atom,1,natoms);
free_dvector(b_vdw_atom,1,natoms);
fprintf(stderr,"\nSolvate ready. No errors.\n\n");
return 0;
}
|