bio_polynow.F90 141 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 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
!$id: bio_polynow.F90,v 1.11 2016-05-05 11:49:15 dd Exp $
#include"cppdefs.h"
!-----------------------------------------------------------------------
!BOP
!
! !MODULE: bio_fasham --- Fasham et al. biological model \label{sec:bio-fasham}
!
! !INTERFACE:
   module bio_polynow
!
!----DESCRIPTION:--------------------------------------------------------------
!  The model developed by Fashametal1990 
!  uses nitrogen as 'currency' according to the evidence that in
!  most cases nitrogen is the limiting macronutrient. It consists of
!  seven state variables: phytoplankton, zooplankton, bacteria,
!  particulate organic matter (detritus), dissolved organic matter
!  and the nutrients nitrate and ammonium.
!  The structure of the biogeochemical model in XXX states variables
! bacteria (bac),
! phytoplankton (phy), 
! detritus (det), 
! zooplankton (zoo), 
! labile dissolved
! organic nitrogen (don), 
! ammonium (amm) and nitrate (nit)
! XXX

! The concentrations are in mmol N\m^-3,and all fluxes are conservative.
!---------------------------------------------------------------------------------

! !USES:
!  default: all is private.
   use bio_var       ! S,T,zlev,rho
   use output
   use observations, only : aa,g2
   use turbulence,   only : eps  
   use meanflow,     only : Rho_0
 

 !  use eqstate,      only : eqstate1   ! In situ density in kg.m-3    

 
   private
!
! !PUBLIC MEMBER FUNCTIONS:
   public init_bio_polynow, init_var_polynow, var_info_polynow, &
          light_polynow, do_bio_polynow, end_bio_polynow
   REALTYPE, public,parameter   :: pi= 3.141592654
   REALTYPE, public,parameter   :: g= 9.80665 !m/s2 
   REALTYPE                     :: pres = 10.1325!  gauge pressure (absolute pressure - 10.1325 bar)
   REALTYPE,public    ::gauss_p



!-----LOCAL VARIABLES:---------- from a namelist : bio_polynow.nml----------------------

 REALTYPE                  :: dt_bio 
 REALTYPE                  :: splitfac_bio
 REALTYPE                  :: depth_bio
!INITIAL and Minimum concentration for the variable 

   REALTYPE                  ::  p_init_value=1.0
   REALTYPE                  ::  p_initial=0.01
   REALTYPE                  ::  z_p_gauss_init= 2.0
   REALTYPE                  ::  sigma_p=2.0

   REALTYPE                ::  zoo_init_value=1.0
   REALTYPE                  ::  z_initial=0.01
   REALTYPE                  ::  z_zoo_gauss_init= 2.0
   REALTYPE                  ::  sigma_zoo=2.0

   REALTYPE                  ::  b_initial= 0.001

!  Nitrate and ammonium are initialized within the GOTM observation module
!   REALTYPE                  ::  n_initial= 8.3
!   REALTYPE                  ::  a_initial= 0.22
!Classes de détritus et LDON

   REALTYPE                  ::  dph_init_value=1.0
   REALTYPE                  ::  dph_initial=0.01
   REALTYPE                  ::  z_dph_gauss_init= 2.0
   REALTYPE                  ::  sigma_dph=2.0

   REALTYPE                  ::  dzo_init_value=1.0
   REALTYPE                  ::  dzo_initial=0.01
   REALTYPE                  ::  z_dzo_gauss_init= 2.0
   REALTYPE                  ::  sigma_dzo=2.0

   REALTYPE                  ::  fp_init_value=1.0
   REALTYPE                  ::  fp_initial=0.01
   REALTYPE                  ::  z_fp_gauss_init= 2.0
   REALTYPE                  ::  sigma_fp=2.0

   REALTYPE                  ::  msn_init_value=1.0
   REALTYPE                  ::  msn_initial=0.01
   REALTYPE                  ::  z_msn_gauss_init= 2.0
   REALTYPE                  ::  sigma_msn=2.0

   REALTYPE                  ::  l_initial= 0.14
   REALTYPE                  ::  p0       = 0.0
   REALTYPE                  ::  z0       = 0.0
   REALTYPE                  ::  b0       = 0.0
   REALTYPE                  ::  mu5      = 0.02  !! GG-CHG1
!Phytoplankton
   REALTYPE                  ::  vp       = 1.5
   REALTYPE                  ::  alpha    = 0.065
   REALTYPE                  ::  inib     = 0.05
   REALTYPE, public          ::  kc       = 0.03
   REALTYPE                  ::  k1       = 0.2
   REALTYPE                  ::  k2       = 0.8
   REALTYPE                  ::  mu1      = 0.05
   REALTYPE                  ::  k5       = 0.2
   REALTYPE                  ::  gamma    = 0.05
   REALTYPE                  ::  txloss_p = 0.7  ! GG-D
   REALTYPE                  ::  txloss_dph = 0.05  ! GG-D

!Zooplankton
   REALTYPE                  ::  gmax     = 1.0
   REALTYPE                  ::  k3       = 1.0
   REALTYPE                  ::  beta     = 0.625
   REALTYPE                  ::  mu2      = 0.3
   REALTYPE                  ::  k6       = 0.2
   REALTYPE                  ::  delta    = 0.1
   REALTYPE                  ::  epsi     = 0.70
   REALTYPE                  ::  eg       = 0.05   ! GG-D
   REALTYPE                  ::  r1       = 0.55
   REALTYPE                  ::  r2       = 0.4
   REALTYPE                  ::  r3       = 0.05   !dph  GG-D
   REALTYPE                  ::  r4       = 0.05   !dzo  GG-D
   REALTYPE                  ::  r5       = 0.05   !fp   GG-D
   REALTYPE                  ::  r6       = 0.05   !msn  GG-D
   REALTYPE                  ::  txloss_dzo = 0.05  ! GG-D
   REALTYPE                  ::  txloss_fp = 0.05  ! GG-D
! Vertical migration(From Ariadna Nocera)
  !LOGICAL  and integer doesn't work                  ::  Migra_zoo=.true.
   REALTYPE                   ::   Migra_zoo= 1.0   
   REALTYPE                  ::  pmin     = 0.05
   REALTYPE                  ::  w_zmax   = 100.0  !! GG-B
   REALTYPE                  ::  bertha   = 0.05   !! GG-B
   REALTYPE                  ::  parcrit  = 0.02   !! GG-B
!Bacterias
   REALTYPE                  ::  vb       = 1.2
   REALTYPE                  ::  remi = 0.1
   REALTYPE                  ::  k4       = 0.5
   REALTYPE                  ::  mu3      = 0.15
   REALTYPE                  ::  eta      = 0.0
   REALTYPE                  ::  mbac     = 0.0    ! GG-D
   REALTYPE                  ::dphlossb =0.4
   REALTYPE                  ::  dphlossl =  10.0  ! GG-D
   REALTYPE                  ::  dzolossl =  10.0  ! GG-D
   REALTYPE                  ::  dzolossb =  10.0  ! GG-D
   REALTYPE                  ::  fplossl  =   10.0 ! GG-D
   REALTYPE                  ::  fplossb  =   10.0 ! GG-D
   REALTYPE                  :: msnlossb =0.6
!LDON
   REALTYPE                  :: leak=0.1
   REALTYPE                  ::  mldon    = 0.02    ! GG-D
   REALTYPE                  ::  lmin     = 0.02    ! GG-D
!detritus/Settling
  ! LOGICAL                  ::  Phys_w= .true. 
  REALTYPE                   ::   size_w=0.0
  REALTYPE                   ::   Phys_w= 0.0 
  REALTYPE                   ::   w_msnow= 0.0 
   REALTYPE                  ::  w_p      = -0.5
   REALTYPE                  ::  w_dph    = -1.0    !!GG-C
   REALTYPE                  ::  w_dzo    = -10.0   !!GG-C
   REALTYPE                  ::  w_fp     = -100.0  !!GG-C
   REALTYPE                  ::  w_msn    = -2.0    !!GG-C

   REALTYPE                  ::  rho_p   = 0.02 
   REALTYPE                  ::  rho_dph      = 0.02 
   REALTYPE                  ::  rho_dzo      = 0.02 
   REALTYPE                  ::  rho_fp      = 0.02 
   REALTYPE                  ::  rho_msn      = 0.02 
!Aggregation
!   LOGICAL                   ::  Coag_coef=.true.   
   REALTYPE                    ::  Coag_coef=0.0      ! If true : utilisation of given parameters / False : utilisation of model calculate coefficient
! Coefficient de Kernel sans calcul via la physique  
   REALTYPE                  :: betap_p      = 0.2
   REALTYPE                  :: betap_dph=  1
   REALTYPE                  :: betap_dzo= 1
   REALTYPE                  :: betap_fp= 1 

   REALTYPE                  ::  betadph_dph = 0.2
   REALTYPE                  ::  betadph_dzo=   1
   REALTYPE                  ::  betadph_fp=   1
 
   REALTYPE                  ::  betadzo_dzo  = 0.02    
   REALTYPE                  ::  betadzo_fp  = 0.02  
   REALTYPE                  ::  betafp_fp   = 0.02    
!Calcul de la collision avec la physique
   REALTYPE                  ::  sti_cst = 0.0
   REALTYPE                  ::  stip_p     = -0.5  
   REALTYPE                  ::  stip_dph   = -0.5  
   REALTYPE                  ::  stip_dzo   = -0.5   
   REALTYPE                  ::  stip_fp    = -0.5   
   REALTYPE                  ::  stidph_dph     = -0.5  
   REALTYPE                  ::  stidph_dzo   = -0.5  
   REALTYPE                  ::  stidph_fp   = -0.5   
   REALTYPE                  ::  stidzo_dzo    = -0.5   
   REALTYPE                  ::  stidzo_fp   = -0.5   
   REALTYPE                  ::  stifp_fp   = -0.5   
!Collision rate and Settling
 REALTYPE                     :: CSF =1.0
 REALTYPE                     :: size_rand = 1.0
   REALTYPE                  ::  size_phy_us     = -0.5  
  REALTYPE                  ::  size_phy_up     = -0.5 
  REALTYPE                  ::   size_dph_us =  0.00001
  REALTYPE                  ::   size_dph_up =  0.00001
  REALTYPE                  ::   size_dzo_us=   0.000100
  REALTYPE                  ::   size_dzo_up=   0.000100
  REALTYPE                  ::   size_fp_us=    0.000050
  REALTYPE                  ::   size_fp_up=    0.000050 
   REALTYPE                  ::  size_msn     = -0.5  
   REALTYPE                  ::  dm_msn     = 0.0
   REALTYPE                  ::  diam_msn_us     = 0.01   
 REALTYPE                  ::  litt_msn_w     = 0.01 
!Parametres pour l'aggregation physique
   REALTYPE                  ::  dynvis      = -0.5  
   REALTYPE                  ::  kinvis      = -0.5  
   REALTYPE                  ::  kB          = -0.5  
!Fragmentation - Concentration seuil d'agrégat avant qu'un pourcentage parte dans le dph
  !logical                   ::  Frag_meth=.true.  !Utilisation de la physique ou pas pour la fragmentation
!  integer                   ::  Frag_meth=0.0
  REALTYPE                   ::  Frag_meth=0.0 
  REALTYPE                 ::  swim_brk = 0.8
REALTYPE                 ::Floc_coef = 0.1
!Tests parameter 
  REALTYPE                 ::betaBr=1.0
  REALTYPE                 ::betaSh =1.0 
  REALTYPE                 ::betaDs =1.0 
  REALTYPE                 ::eps_const =1.0 
  REALTYPE                 ::eps_n =1.0 
  REALTYPE                 ::cons_min= 0.00001

 REALTYPE                  ::write_screen


REALTYPE                  ::coef1
REALTYPE                  ::coef2
REALTYPE                  ::RFV
REALTYPE                  ::coef3
REALTYPE                  ::coef4
REALTYPE                  ::coef5

REALTYPE                  ::cons_max

   integer                   ::  out_unit
!   integer, parameter        ::  p=1,z=2,b=3,d1=4,n=5,a=6,l=7,d2=8,d3=9,d4=10,aug_si_d4=11,taille_msn=12 
! GG  d1= dph , d2= dzo , d3= fp , d4 = msn, aug_si_d4= size_msn
 integer, parameter        ::  p=1,z=2,b=3,d1=4,n=5,a=6,l=7,d2=8,d3=9,d4=10, &
                               taille_intrm=11,taille_coag=12, taille_frag=13,taille_msn=14,settl_msn=15 


!EOP
!-----------------------------------------------------------------------

   contains

!-----------------------------------------------------------------------
!BOP
!
! !IROUTINE: Initialise the bio module
!
! !INTERFACE:
   subroutine init_bio_polynow(namlst,fname,unit)
!
! !DESCRIPTION:
!  Here, the bio namelist bio_polynow.nml is read and
!  various variables (rates and settling velocities)
!  are transformed into SI units.
!
! !USES:
   IMPLICIT NONE
!
! !INPUT PARAMETERS:
   integer,          intent(in)   :: namlst
   character(len=*), intent(in)   :: fname
   character(len=20)              :: pfile
   integer,          intent(in)   :: unit
!
! !REVISION HISTORY:
!  Original author(s): Hans Burchard & Karsten Bolding
!
! !LOCAL VARIABLES:
   namelist /bio_polynow_nml/ numc, &
                                                        
                       dt_bio,splitfac_bio,depth_bio,p_init_value,p_initial,z_p_gauss_init,sigma_p,   &
zoo_init_value,z_initial,z_zoo_gauss_init,sigma_zoo, &
b_initial,                       &
dph_init_value,dph_initial,z_dph_gauss_init,sigma_dph,  &
dzo_init_value,dzo_initial,z_dzo_gauss_init,sigma_dzo,  &
fp_init_value,fp_initial,z_fp_gauss_init,sigma_fp, &
                        msn_init_value,msn_initial,z_msn_gauss_init,sigma_msn,                                     & 
                        l_initial,p0,z0,b0,mu5,                                                                  &
                        vp,alpha,inib,kc,k1,k2,mu1,k5,gamma,txloss_p,txloss_dph,             & ! phytoplankton 
                        gmax,k3,beta,mu2,k6,delta,epsi,eg,r1,r2,r3,r4,r5,r6,                             & ! zooplankton   
                        txloss_dzo,txloss_fp, &
                        Migra_zoo,pmin,w_zmax,bertha,parcrit,                                                    & !Vertical migration from Nocera 
                        vb,remi,k4,mu3,eta,mbac,dphlossb,dphlossl,dzolossl,dzolossb,fplossl,fplossb,msnlossb, & !Bacterias 
                        leak,mldon,lmin,                                                                              & !LDON      
                         size_w,Phys_w,w_msnow,w_p,w_dph,w_dzo,w_fp,w_msn,                                                       & !Detritus
rho_p,rho_dph,rho_dzo,rho_fp,rho_msn,                                                    &    
                        Coag_coef,     &
                        betap_p,betap_dph,betap_dzo,betap_fp, &
                        betadph_dph,betadph_dzo,betadph_fp, &
                        betadzo_dzo,betadzo_fp,betafp_fp  ,                                                & !Aggregation
                        sti_cst, &
                        stip_p,stip_dph,stip_dzo,stip_fp,stidph_dph,stidph_dzo,stidph_fp,stidzo_dzo,stidzo_fp,stifp_fp, &
                     CSF,size_rand,size_phy_us,size_phy_up,size_dph_us,size_dph_up,size_dzo_us,size_dzo_up,&
  size_fp_us,size_fp_up,size_msn,dm_msn,diam_msn_us,litt_msn_w,&
                        dynvis,kinvis,kB,                                                                         &
                        Frag_meth,swim_brk,                                                           &
Floc_coef,                &
                        betaBr,betaSh,betaDs,                                                                     &
                        eps_const,eps_n,cons_min, &
                        coef1,coef2,RFV,coef3,coef4,coef5,cons_max,&
                        write_screen                                             

!EOP
!-----------------------------------------------------------------------
!BOC
   LEVEL2 'init_bio_polynow'

   open(namlst,file=fname,action='read',status='old',err=98)
   read(namlst,nml=bio_polynow_nml,err=99)
   close(namlst)

   numcc=numc
!----Print some parameter values in standard output and save them in a separate file [out_fn]_polynow.par
   pfile = trim(out_fn) // '_polynow.par'
   open(10,status='unknown',action='write',file=pfile)
   LEVEL3 'Biogeochemical parameters saved in ', pfile

!dt_bio
!splitfac_bio
  !p_init_value=1.0
   write(*,900) '                p_initial     = ',p_initial
   write(10,901) p_initial
  !z_p_gauss_init= -10
  !sigma_p= 2.0
!zoo_init_value      
   write(*,900) '                z_initial     = ',z_initial
   write(10,901) z_initial  
! z_zoo_gauss_init   
!  sigma_zoo         
   write(*,900) '                b_initial     = ',b_initial
   write(10,901) b_initial
!dph_init_value=1.0
   write(*,900) '                dph_initial     = ',dph_initial
   write(10,901) dph_initial
  !z_dph_gauss_init= -10
  !sigma_dph= 2.0
!dzo_init_value=1.0
   write(*,900) '                dzo_initial     = ',dzo_initial
   write(10,901) dzo_initial
 !z_dzo_gauss_init= -10
  !sigma_dzo= 2.0
!fp_init_value=1.0
   write(*,900) '                fp_initial     = ',fp_initial
   write(10,901) fp_initial
 !z_fp_gauss_init= -10
  !sigma_fp= 2.0
!msn_init_value=1.0
   write(*,900) '                msn_initial     = ',msn_initial
   write(10,901) msn_initial
 !z_msn_gauss_init= -10
  !sigma_msn= 2.0
   write(*,900) '                l_initial     = ',l_initial
   write(10,901) l_initial
   write(*,900) '                p0     = ',p0
   write(10,901) p0
   write(*,900) '                z0     = ',z0
   write(10,901) z0
   write(*,900) '                b0     = ',b0
   write(10,901) b0
   write(*,900) '                mu5     = ',mu5
   write(10,901) mu5
   write(*,900) '                vp     = ',vp
   write(10,901) vp
   write(*,900) '                alpha  = ',alpha
   write(10,901) alpha
   write(*,900) '                inib   = ',inib
   write(10,901) inib
   write(*,900) '                kc     = ',kc
   write(10,901) kc
   write(*,900) '                k1     = ',k1
   write(10,901) k1
   write(*,900) '                k2     = ',k2
   write(10,901) k2
   write(*,900) '                mu1     = ',mu1
   write(10,901) mu1
   write(*,900) '                k5     = ',k5
   write(10,901) k5
   write(*,900) '               gamma     = ',gamma
   write(10,901) gamma
   write(*,900) '               txloss_p = ',txloss_p
   write(10,901) txloss_p
   write(*,900) '               txloss_dph = ',txloss_dph
   write(10,901) txloss_dph
   write(*,900) '                gmax  = ',gmax
   write(10,901) gmax
   write(*,900) '                k3     = ',k3
   write(10,901) k3
   write(*,900) '                beta     = ',beta
   write(10,901) beta
   write(*,900) '                mu2      = ',mu2
   write(10,901) mu2
   write(*,900) '                k6     = ',k6
   write(10,901) k6
   write(*,900) '                delta    = ',delta
   write(10,901) delta
   write(*,900) '                epsi    = ',epsi
   write(10,901) epsi
   write(*,900) '                eg     = ',eg
   write(10,901) eg
   write(*,900) '                r1     = ',r1
   write(10,901) r1
   write(*,900) '                r2   = ',r2
   write(10,901) r2
   write(*,900) '                r3    = ',r3
   write(10,901) r3
   write(*,900) '                r4    = ',r4
   write(10,901) r4
   write(*,900) '                r5     = ',r5
   write(10,901) r5
   write(*,900) '                r6     = ',r6
   write(10,901) r6
   write(*,900) '               txloss_dzo = ',txloss_dzo
   write(10,901) txloss_dzo
   write(*,900) '               txloss_fp = ',txloss_fp
   write(10,901) txloss_fp
   write(*,900) '               Migra_zoo = ',Migra_zoo
   write(10,901) Migra_zoo
   write(*,900) '                pmin     = ',pmin
   write(10,901) pmin
   write(*,900) '                w_zmax    = ',w_zmax
   write(10,901) w_zmax
   write(*,900) '                bertha    = ',bertha
   write(10,901) bertha
   write(*,900) '                parcrit     = ',parcrit
   write(10,901) parcrit
   write(*,900) '                vb    = ',vb
   write(10,901) vb
!remi
   write(*,900) '                k4    = ',k4
   write(10,901) k4
   write(*,900) '                mu3    = ',mu3
   write(10,901) mu3
   write(*,900) '                eta    = ',eta
   write(10,901) eta
   write(*,900) '                mbac    = ',mbac
   write(10,901) mbac
   write(*,900) '                dphlossb    = ',dphlossb
   write(10,901) dphlossb
   write(*,900) '               dphlossl    = ',dphlossl
   write(10,901) dphlossl
   write(*,900) '               dzolossl    = ',dzolossl
   write(10,901) dzolossl
   write(*,900) '               dzolossb   = ',dzolossb
   write(10,901) dzolossb
   write(*,900) '               fplossl    = ',fplossl
   write(10,901) fplossl
   write(*,900) '               fplossb   = ',fplossb
   write(10,901) fplossb
   write(*,900) '               msnlossb   = ',msnlossb
   write(10,901) msnlossb
!leak
   write(*,900) '               mldon   = ',mldon
   write(10,901) mldon
   write(*,900) '               lmin   = ',lmin
   write(10,901) lmin
! size_w
   write(*,900) '               Phys_w   = ',Phys_w
   write(10,901) Phys_w
!w_msnow
   write(*,900) '               w_p   = ',w_p
   write(10,901) w_p
   write(*,900) '               w_dph    = ',w_dph
   write(10,901) w_dph
   write(*,900) '               w_dzo    = ',w_dzo
   write(10,901) w_dzo
   write(*,900) '               w_fp  = ',w_fp
   write(10,901) w_fp
   write(*,900) '               w_msn    = ',w_msn
   write(10,901) w_msn

  ! write(*,900) '               rho_p    = ',rho_p
 !  write(10,901) rho_p
!   write(*,900) '               rho_dph    = ',rho_dph
!   write(10,901) rho_dph
   write(*,900) '               rho_dzo    = ',rho_dzo
   write(10,901) rho_dzo
   write(*,900) '               rho_fp    = ',rho_fp
   write(10,901) rho_fp
   write(*,900) '               rho_msn    = ',rho_msn
   write(10,901) rho_msn
   write(*,900) '               coag_coef    = ',coag_coef
   write(10,901) coag_coef
   write(*,900) '               betap_p   = ',betap_p
   write(10,901) betap_p

!betap_dph,betap_dzo,betap_fp

   write(*,900) '               betadph_dph   = ',betadph_dph
   write(10,901) betadph_dph

! betadph_dzo,betadph_fp,

   write(*,900) '               betadzo_dzo   = ',betadzo_dzo
   write(10,901) betadzo_dzo
!betadzo_fp
   write(*,900) '               betafp_fp   = ',betafp_fp
   write(10,901) betafp_fp
!sti_cst
   write(*,900) '               stip_p   = ',stip_p
   write(10,901) stip_p
   write(*,900) '               stip_dph   = ',stip_dph
   write(10,901) stip_dph
   write(*,900) '               stip_dzo   = ',stip_dzo
   write(10,901) stip_dzo
   write(*,900) '               stip_fp   = ',stip_fp
   write(10,901) stip_fp
   write(*,900) '               stidph_dph   = ',stidph_dph
   write(10,901) stidph_dph
   write(*,900) '               stidph_dzo   = ',stidph_dzo
   write(10,901) stidph_dzo
   write(*,900) '               stidph_fp   = ',stidph_fp
   write(10,901) stidph_fp
   write(*,900) '               stidzo_dzo   = ',stidzo_dzo
   write(10,901) stidzo_dzo
   write(*,900) '               stidzo_fp   = ',stidzo_fp
   write(10,901) stidzo_fp
   write(*,900) '               stifp_fp   = ',stifp_fp
   write(10,901) stifp_fp
   write(*,900) '               size_phy_us   = ',size_phy_us
   write(10,901) size_phy_us
!size_phy_up
!CSF
   write(*,900) '               size_dph_us   = ',size_dph_us
   write(10,901) size_dph_us
   write(*,900) '               size_dzo_us   = ',size_dzo_us
   write(10,901) size_dzo_us
   write(*,900) '               size_fp_us  = ',size_fp_us
   write(10,901) size_fp_us

 !size_dph_up =  0.00001
 !size_dzo_up=   0.000100
 !size_fp_up=    0.000050
   write(*,900) '               size_msn   = ',size_msn
   write(10,901) size_msn
!dm_msn
!diam_msn_us
!litt_msn_w 
   write(*,900) '               dynvis   = ',dynvis
   write(10,901) dynvis
   write(*,900) '               kinvis   = ',kinvis
   write(10,901) kinvis
   write(*,900) '               kB   = ',kB
   write(10,901) kB
   write(*,900) '               Frag_meth   = ',Frag_meth
   write(10,901) Frag_meth
   write(*,900) '                swim_brk   = ', swim_brk
   write(10,901)  swim_brk
!betaBr
!betaSh
!betaDs
!eps_const
!eps
!cons_min
!Floc_coef 
!coef1,coef2,coef3,coef4,coef5
!RFV
!cons_max

900 format (a,f8.5)
901 format (f8.5)

!  Conversion from day to second
   mu5      = mu5 /secs_pr_day       
   vp       = vp /secs_pr_day
   alpha    = alpha /secs_pr_day                
   inib     = inib /secs_pr_day         
   mu1      = mu1 /secs_pr_day

   gmax     = gmax /secs_pr_day
   mu2      = mu2 /secs_pr_day
   w_zmax   = w_zmax /secs_pr_day       

   vb       = vb /secs_pr_day
  remi = remi/secs_pr_day
   mu3      = mu3 /secs_pr_day
   mbac     = mbac /secs_pr_day       
 dphlossb    =dphlossb/secs_pr_day 
  dphlossl  = dphlossl/secs_pr_day 
  dzolossl  = dzolossl/secs_pr_day 
  dzolossb  = dzolossb/secs_pr_day 
  fplossl  = fplossl/secs_pr_day
  fplossb  = fplossb/secs_pr_day
  msnlossb= msnlossb/secs_pr_day
leak=leak/secs_pr_day 
  mldon    = mldon /secs_pr_day       

   w_p      = w_p /secs_pr_day
   w_dph    = w_dph /secs_pr_day    
   w_dzo    = w_dzo /secs_pr_day    
   w_fp     = w_fp /secs_pr_day    
   w_msn    = w_msn /secs_pr_day    

litt_msn_w =litt_msn_w /secs_pr_day
!Phytoplankton
txloss_p = txloss_p /secs_pr_day   
betap_p    = betap_p /secs_pr_day    ! GG-D
betap_dph    = betap_dph /secs_pr_day    ! GG-D
betap_dzo    = betap_dzo /secs_pr_day    ! GG-D
betap_fp    = betap_fp /secs_pr_day    ! GG-D

!Dead-Phytoplankton
txloss_dph= txloss_dph /secs_pr_day 
betadph_dph =betadph_dph/secs_pr_day 
betadph_dzo=betadph_dzo/secs_pr_day 
betadph_fp=betadph_fp/secs_pr_day 

!Dead-Zooplankton
txloss_dzo= txloss_dzo /secs_pr_day 
betadzo_dzo  = betadzo_dzo /secs_pr_day    
betadzo_fp  = betadzo_fp /secs_pr_day    

!Fecal pellets
txloss_fp= txloss_fp /secs_pr_day 
betafp_fp   = betafp_fp /secs_pr_day    


 swim_brk   =  swim_brk /secs_pr_day
 !eps cm2. sec-3

   out_unit=unit

   LEVEL3 'polynow bio module initialised ...'

   return

98 LEVEL2 'I could not open bio_polynow.nml'
   LEVEL2 'If thats not what you want you have to supply bio_polynow.nml'
   LEVEL2 'See the bio example on www.gotm.net for a working bio_polynow.nml'
   return
99 FATAL 'I could not read bio_polynow.nml'
   stop 'init_bio_polynow'
   end subroutine init_bio_polynow
!EOC

!-----------------------------------------------------------------------
!BOP
!
! !IROUTINE: Initialise the concentration variables
!
! !INTERFACE:
   subroutine init_var_polynow(nlev)
!
! !DESCRIPTION:
!  Here, the the initial conditions are set and the settling velocities are
!  transferred to all vertical levels. All concentrations are declared
!  as non-negative variables, and it is defined which variables would be
!  taken up by benthic filter feeders.
!
! !USES:
   use observations,    only: nprof,aprof              !CHG3-5
   use meanflow,        only: nit,amm,T,S               !CHG3-5
 
   IMPLICIT NONE

!
! !INPUT PARAMETERS:
   integer, intent(in)                 :: nlev

!
! !REVISION HISTORY:
!  Original author(s): Hans Burchard & Karsten Bolding

! !LOCAL VARIABLES:
  integer                    :: i,j
  REALTYPE    ::gauss_p,gauss_dph,gauss_dzo,gauss_fp,gauss_msn,gauss_zoo

!EOP
!-----------------------------------------------------------------------
!BOC

!-----------------------------------------------------------------------------------
!-----------------------------------------------------------------------------------
!   Variable INITIAL PROFILES
!-----------------------------------------------------------------------------------
!-----------------------------------------------------------------------------------

   do i=1,nlev

!-----------------------------------------------------------------------------------
!   Phytoplankton initial profile
!-----------------------------------------------------------------------------------

!!       ---If p_init_value .eq.1 --» Cst initial value for all the depth as mentionned in the nml.
if (p_init_value .eq. 1.0) then
      cc(p,i)=p_initial
!!       ---If p_init_value .eq.0 --» Initial profile describe according to gaussian curve (Mean + standard error)
else
  gauss_p=p_initial*exp(-((i-z_p_gauss_init)**2/(2*(sigma_p**2))))
     cc(p,i)= gauss_p
endif

!-----------------------------------------------------------------------------------
!   Zooplankton initial profiles
!-----------------------------------------------------------------------------------

!       ---If zoo_init_value .eq.1 --» Cst initial value for all the depth as mentionned in the nml.
if (zoo_init_value .eq. 1.0) then
      cc(z,i)=z_initial
!       ---If zoo_init_value.eq.0 --» Initial profile describe according to gaussian curve (Mean + standard error)
else
  gauss_zoo=z_initial*exp(-((i-z_zoo_gauss_init)**2/(2*(sigma_zoo**2))))
     cc(z,i)= gauss_zoo
endif
!-----------------------------------------------------------------------------------
!   Dead- Phytoplankton initial profile
!-----------------------------------------------------------------------------------

!!       ---If dph_init_value .eq.1 --» Cst initial value for all the depth as mentionned in the nml.
if (dph_init_value .eq. 1.0) then
      cc(d1,i)=dph_initial
!!       ---If dph_init_value .eq.0 --» Initial profile describe according to gaussian curve (Mean + standard error)
else
  gauss_dph=dph_initial*exp(-((i-z_dph_gauss_init)**2/(2*(sigma_dph**2))))
     cc(d1,i)= gauss_dph
endif

!-----------------------------------------------------------------------------------
!   Dead- Zooplankton initial profile
!-----------------------------------------------------------------------------------

!!       ---If dzo_init_value .eq.1 --» Cst initial value for all the depth as mentionned in the nml.
if (dzo_init_value .eq. 1.0) then
      cc(d2,i)=dzo_initial
!!       ---If dzo_init_value .eq.0 --» Initial profile describe according to gaussian curve (Mean + standard error)
else
  gauss_dzo=dzo_initial*exp(-((i-z_dzo_gauss_init)**2/(2*(sigma_dzo**2))))
     cc(d2,i)= gauss_dzo
endif

!-----------------------------------------------------------------------------------
!   Fecal pellets initial profile
!-----------------------------------------------------------------------------------

!!       ---If fp_init_value .eq.1 --» Cst initial value for all the depth as mentionned in the nml.
if (fp_init_value .eq. 1.0) then
      cc(d3,i)=fp_initial
!!       ---If fp_init_value .eq.0 --» Initial profile describe according to gaussian curve (Mean + standard error)
else
  gauss_fp=fp_initial*exp(-((i-z_fp_gauss_init)**2/(2*(sigma_fp**2))))
     cc(d3,i)= gauss_fp
endif

!-----------------------------------------------------------------------------------
!   Marine snow initial profile
!-----------------------------------------------------------------------------------

!!       ---If msn_init_value .eq.1 --» Cst initial value for all the depth as mentionned in the nml.
if (msn_init_value .eq. 1.0) then
      cc(d4,i)=msn_initial
!!       ---If msn_init_value.eq.0 --» Initial profile describe according to gaussian curve (Mean + standard error)
else
  gauss_msn=msn_initial*exp(-((i-z_msn_gauss_init)**2/(2*(sigma_msn**2))))
     cc(d4,i)= gauss_msn
endif


!-----------------------------------------------------------------------------------
!  Bacteria- Nitrate/ammonium / LDON initial profiles
!-----------------------------------------------------------------------------------
      cc(b,i)=b_initial
      cc(n,i)=nprof(i)       
      cc(a,i)=aprof(i)       
      cc(l,i)=l_initial

   end do

!-----------------------------------------------------------------------------------
!   Settling/swimming velocity
!-----------------------------------------------------------------------------------

   do i=0,nlev
      ws(z,i) = _ZERO_
      ws(b,i) = _ZERO_
      ws(n,i) = _ZERO_
      ws(a,i) = _ZERO_
      ws(l,i) = _ZERO_

if(Phys_w .eq. 0.0) then    
      ws(p,i) = w_p
      ws(d1,i)= w_dph   
      ws(d2,i)= w_dzo   
      ws(d3,i)= w_fp    
      ws(d4,i)= w_msn 
else
     ws(p,i) =_ZERO_           
     ws(d1,i)=_ZERO_
     ws(d2,i)=_ZERO_ 
     ws(d3,i)=_ZERO_ 
     ws(d4,i)=_ZERO_ 

end if

!-----------------------------------------------------------------------------------
!   Marine Snow Size and settling velocity
!-----------------------------------------------------------------------------------
  cc(taille_intrm,i) =_ZERO_
  cc(taille_coag,i)=_ZERO_
  cc(taille_frag,i)=_ZERO_

  cc(taille_msn,i)=_ZERO_
  ws(taille_msn,i)=_ZERO_

 cc(settl_msn,i)= _ZERO_
 ws(settl_msn,i) =_ZERO_

   end do

   posconc(p) = 1
   posconc(z) = 1
   posconc(b) = 1
   posconc(n) = 1
   posconc(a) = 1
   posconc(l) = 1
  
  posconc(d1) = 1 !GG-C
  posconc(d2) = 1 !GG-C
  posconc(d3) = 1 !GG-C
  posconc(d4) = 1 !GG-C

#if 0
   mussels_inhale(p) = .true.
   mussels_inhale(z) = .true.
   mussels_inhale(b) = .false.
   mussels_inhale(n) = .false.
   mussels_inhale(a) = .false.
   mussels_inhale(l) = .true.
 
   mussels_inhale(d1) = .true.  
   mussels_inhale(d2) = .true.  
   mussels_inhale(d3) = .true.  
   mussels_inhale(d4) = .true.  

#endif

   LEVEL3 'polynow variables initialised ...'

   return

   end subroutine init_var_polynow
!EOC

!-----------------------------------------------------------------------
!BOP
!
! !IROUTINE: Providing info on variables
!
! !INTERFACE:
   subroutine var_info_polynow()
!
! !DESCRIPTION:
!  This subroutine provides information about the variable names as they
!  will be used when storing data in NetCDF files.
!
! !USES:
   IMPLICIT NONE
!
! !REVISION HISTORY:
!  Original author(s): Hans Burchard & Karsten Bolding
!
! !LOCAL VARIABLES:
!EOP
!-----------------------------------------------------------------------
!BOC
   var_names(1) = 'phy'
   var_units(1) = 'mmol/m**3'
   var_long(1)  = 'phytoplankton'

   var_names(2) = 'zoo'
   var_units(2) = 'mmol/m**3'
   var_long(2)  = 'zooplankton'

   var_names(3) = 'bac'
   var_units(3) = 'mmol/m**3'
   var_long(3)  = 'bacteria'

   var_names(4) = 'dph'                
   var_units(4) = 'mmol/m**3'
   var_long(4)  = 'dead phytoplankton'

   var_names(5) = 'nit'
   var_units(5) = 'mmol/m**3'
   var_long(5)  = 'nitrate'

   var_names(6) = 'amm'
   var_units(6) = 'mmol/m**3'
   var_long(6)  = 'ammonium'

   var_names(7) = 'ldn'
   var_units(7) = 'mmol/m**3'
   var_long(7)  = 'labile_dissolved_organic_nitrogen'

   var_names(8) = 'dzo'                            
   var_units(8) = 'mmol/m**3'                      
   var_long(8)  = 'dead zooplankton'           

   var_names(9) = 'fp'                              
   var_units(9) = 'mmol/m**3'
   var_long(9)  = 'fecal pellets'

   var_names(10) = 'msn'                          
   var_units(10) = 'mmol/m**3'
   var_long(10)  = 'marine snow'

!--------------------------------------------------

   var_names(11) = 'taille_intrm'                          
   var_units(11) = 'm'
   var_long(11)  = 'Intermediate size of marine snow'


   var_names(12) = 'taille_coag'                          
   var_units(12) = 'm'
   var_long(12)  = 'Size augmentation of msn due to Coag.'


   var_names(13) = 'taille_frag'                          
   var_units(13) = 'm'
   var_long(13)  = 'Size of msn due to Frag.'


   var_names(14) = 'taille_msn'                          
   var_units(14) = 'm'
   var_long(14)  = 'Final size of marine snow'

   var_names(15) = 'settl_msn'                          
   var_units(15) = 'm/s'
   var_long(15)  = 'Settling Velocity of marine snow'



   return
   end subroutine var_info_polynow
!EOC

!-----------------------------------------------------------------------
!BOP
!
! !IROUTINE: Light properties for the Fasham model
!
! !INTERFACE:
   subroutine light_polynow(nlev,bioshade_feedback)
!
! !DESCRIPTION:
! Here, the photosynthetically available radiation is calculated
! by simply assuming that the short wave part of the total
! radiation is available for photosynthesis. 
! The photosynthetically
! available radiation, $I_{PAR}$, follows from (\ref{light}).
! The user should make
! sure that this is consistent with the light class given in the
! {\tt extinct} namelist of the {\tt obs.nml} file.
! The self-shading effect is also calculated in this subroutine,
! which may be used to consider the effect of bio-turbidity also
! in the temperature equation (if {\tt bioshade\_feedback} is set
! to true in {\tt bio.nml}).
! For details, see section \ref{sec:do-bio}.
!
! !USES:
   IMPLICIT NONE
!
! !INPUT PARAMETERS:
  integer, intent(in)                  :: nlev
  logical, intent(in)                  :: bioshade_feedback
!
! !REVISION HISTORY:
!  Original author(s): Hans Burchard, Karsten Bolding
!
! !LOCAL VARIABLES:
   integer                   :: i
   REALTYPE                  :: zz,add
!EOP
!-----------------------------------------------------------------------
!BOC
   zz = _ZERO_
   add = _ZERO_
   do i=nlev,1,-1
      add=add+0.5*h(i)*(cc(p,i)+p0)
      zz=zz+0.5*h(i)
      par(i)=rad(nlev)*(1.-aa)*exp(-zz/g2)*exp(-kc*add)
      add=add+0.5*h(i)*(cc(p,i)+p0)
      zz=zz+0.5*h(i)
      if (bioshade_feedback) bioshade_(i)=exp(-kc*add)
   end do


   return
   end subroutine light_polynow
!EOC

!-----------------------------------------------------------------------
!BOP
!
! !IROUTINE: Right hand sides of geobiochemical model \label{sec:bio-fasham-rhs}
!
! !INTERFACE:
   subroutine do_bio_polynow(first,numc,nlev,cc,pp,dd)
!
! !DESCRIPTION:
! The model consisting of the following state variable:
! phytoplankton, bacteria, detritus (4 types), zooplankton, 
! nitrate, ammonium and dissolved organic nitrogen.
!
! !BIBLIOGRAPHY:
! Platt et al., 1980- Moore et al., 2002-Alldredge and Gotschalk 1988 -Kajihara 1971
!Komar 1981-Ghosh 2013 - Mc Donnell 2010 - Alldredge 1990

! Biddanda 1998
! Ploug and Grossart 2000
! Jokulsdottir 2011
! Jackson 1990
!

! !USES: Elle force l'utilisateur à déclarer toutes les variables
   IMPLICIT NONE
!
!Dans ce cas, le type des variables non déclarées correspond à la règle suivante :
   ! Les variables commençant par une lettre comprise entre I et N sont des integer,
   !Toutes les autres sont des real.


! !INPUT PARAMETERS:
   logical, intent(in)                 :: first
   integer, intent(in)                 :: numc,nlev
   REALTYPE   ::gauss_p
 
   REALTYPE, intent(out)                :: cc(1:numc,0:nlev)
!With INTENT(IN), you are promising to the compiler that argument cc is not modified nor its definition status changed. 

! !OUTPUT PARAMETERS:
   REALTYPE, intent(out)               :: pp(1:numc,1:numc,0:nlev)
   REALTYPE, intent(out)               :: dd(1:numc,1:numc,0:nlev)

!Similarly, INTENT(OUT) says that variables pp and dd are always assigned a value in the routine.

 !! REALTYPE               :: rho_(0:nlev)

! !LOCAL VARIABLES:
   integer                    :: i,j,ci
   REAL                       ::w_depth
!   Light and nutrient limitation factors & Zooplankton and bacterias needed factors
   REALTYPE                   :: Ps,ff
   REALTYPE                   :: q1,q2
   REALTYPE                   :: fac,min67
!!   Size variation for each particle type
   REALTYPE                   ::size_phy,size_dph,size_dzo,size_fp
   REALTYPE                   ::diam_phy,diam_dph,diam_dzo,diam_fp,diam_msn 
!Collision & Aggregation
!!   Sedimentation rate for each particle type
     REALTYPE                    ::w_p_m,w_dph_m,w_dzo_m,w_fp_m
     REALTYPE                     ::densFlu
     REALTYPE                    ::Rp,Rdph,Rdzo,Rfp
      REALTYPE                   ::Re_phy,Re_dph,Re_dzo,Re_fp
     REALTYPE                    ::CSF_phy,CSF_dph,CSF_dzo,CSF_fp,CSF_msn
!!   Maximum loss for aggregation for each particle type
      REALTYPE                    ::pprime   
      REALTYPE                    ::d1rime  
      REALTYPE                    ::d2rime
      REALTYPE                    ::d3rime
!!   Stickiness for each particle type interaction
      REAL                        :: r_phy, r_dph, r_dzo, r_fp
      REALTYPE                     ::stip ,stidph,stidzo,stifp
!'No physical coagulation happens here [Constant Beta are set]'
! Phytoplankton loss by aggregation
      REALTYPE                    ::coli_pp_NoPhys,agg_pp
      REALTYPE                    ::coli_pdph_NoPhys,agg_pdph
      REALTYPE                    ::coli_pdzo_NoPhys,agg_pdzo
      REALTYPE                    ::coli_pfp_NoPhys,agg_pfp
! Dead Phytoplankton loss by aggregation
      REALTYPE                    ::coli_dphdph_NoPhys,agg_dphdph
      REALTYPE                    ::coli_dphdzo_NoPhys,agg_dphdzo
      REALTYPE                    :: coli_dphfp_NoPhys,agg_dphfp
! Dead Zooplankton loss by aggregation
      REALTYPE                    ::coli_dzodzo_NoPhys,agg_dzodzo
      REALTYPE                    ::coli_dzofp_NoPhys,agg_dzofp
! Fecal pellets loss by aggregation
      REALTYPE                    ::coli_fpfp_NoPhys,agg_fpfp
!Coagulate only with : BetaBR'
      REALTYPE                    ::T_degK
      REALTYPE                    ::physic_Br
  !Phytoplankton
      REALTYPE                    :: betaBr_pp,coli_pp
      REALTYPE                    :: betaBr_pdph,coli_pdph
      REALTYPE                    :: betaBr_pdzo,coli_pdzo
      REALTYPE                    :: betaBr_pfp,coli_pfp
  !Dead Phytoplankton
      REALTYPE                    :: betaBr_dphdph,coli_dphdph
      REALTYPE                    :: betaBr_dphdzo,coli_dphdzo
      REALTYPE                    :: betaBr_dphfp,coli_dphfp
  !Dead Zooplankton
      REALTYPE                    :: betaBr_dzodzo,coli_dzodzo
      REALTYPE                    :: betaBr_dzofp,coli_dzofp
  !Fecal pellets
      REALTYPE                    :: betaBr_fpfp,coli_fpfp
!Coagulate only with : BetaSh'
      REALTYPE                    ::bio_eps
  !Phytoplankton
      REALTYPE                    ::q_pp,betaSh_pp
      REALTYPE                    ::q_pdph,betaSh_pdph
      REALTYPE                    ::q_pdzo,betaSh_pdzo
      REALTYPE                    ::q_pfp,betaSh_pfp
  !Dead Phytoplankton
      REALTYPE                    ::q_dphdph,betaSh_dphdph
      REALTYPE                    ::q_dphdzo,betaSh_dphdzo
      REALTYPE                    ::q_dphfp,betaSh_dphfp
  !Dead Zooplankton
      REALTYPE                    ::q_dzodzo,betaSh_dzodzo
      REALTYPE                    ::q_dzofp,betaSh_dzofp
  !Fecal pellets
      REALTYPE                    ::q_fpfp,betaSh_fpfp
!Coagulate only with : BetaDs'
  !Phytoplankton
      REALTYPE                    ::abs_pp, betaDs_pp
      REALTYPE                    ::abs_pdph,betaDs_pdph
      REALTYPE                    ::abs_pdzo,betaDs_pdzo
      REALTYPE                    ::abs_pfp,betaDs_pfp
  !Dead Phytoplankton
      REALTYPE                    :: abs_dphdph,betaDs_dphdph
      REALTYPE                    :: abs_dphdzo,betaDs_dphdzo
      REALTYPE                    :: abs_dphfp,betaDs_dphfp
  !Dead Zooplankton
      REALTYPE                    :: abs_dzodzo,betaDs_dzodzo
      REALTYPE                    :: abs_dzofp,betaDs_dzofp
  !Fecal pellets
      REALTYPE                    :: abs_fpfp,betaDs_fpfp
!'Physical Coagulation happens here ![Beta are calculated via physic]'
  !Phytoplankton
      REALTYPE                    ::kernel_coef_pp
      REALTYPE                    ::kernel_coef_pdph
      REALTYPE                    ::kernel_coef_pdzo
      REALTYPE                    ::kernel_coef_pfp
  !Dead Phytoplankton
      REALTYPE                    :: kernel_coef_dphdph
      REALTYPE                    :: kernel_coef_dphdzo
      REALTYPE                    :: kernel_coef_dphfp
  !Dead Zooplankton
      REALTYPE                    :: kernel_coef_dzodzo
      REALTYPE                    :: kernel_coef_dzofp
  !Fecal pellets
      REALTYPE                    :: kernel_coef_fpfp
!SIZE Marine Snow with aggregation
      REALTYPE                    :: Ratio_P,Ratio_D1,Ratio_D2,Ratio_D3
      REALTYPE                    :: size_msn_m
      REALTYPE                    :: max_flux
!FRAGMENTATION
      REALTYPE                    ::Frag_bio,Frag_phys,leackage,remineralization
      REALTYPE                    ::kolmog,stable_size
      REALTYPE                    ::prob_break_msn,r_msn
      REALTYPE                    :: diam_msn_max,RFV_msn,min_size_msn
!   Sedimentation rate msn
      REALTYPE                    :: w_msn_m,Re_msn,Rmsn,w_min_m,CFL,max_w_msn

!EOP
!-----------------------------------------------------------------------
!BOC
!KBK - is it necessary to initialise every time - expensive in a 3D model
   pp = _ZERO_
   dd = _ZERO_

   do ci=1,nlev   !#A 
!write(*,*)'---',ci
!-----------------------------------------------------------------------------------
!   Light and nutrient limitation factors & Zooplankton and bacterias needed factors
!-----------------------------------------------------------------------------------

!!       ---Light : Platt et al. (1980) - inhibition
      Ps= vp/((alpha/(alpha+inib))*(alpha/(alpha+inib))**(inib/alpha))
      ff= Ps*(1.-exp(-1.*alpha*par(ci)/Ps))*exp(-1.*inib*par(ci)/Ps)
      lumlim1(ci)=ff
!!        ---Nitrate :
      q1=(cc(n,ci)/k1)/(1.+cc(n,ci)/k1+cc(a,ci)/k2)
      nitlim1(ci)=q1
!!        ---Ammonium :
      q2=(cc(a,ci)/k2)/(1.+cc(n,ci)/k1+cc(a,ci)/k2)
      ammlim1(ci)=q2
!!        ---Zooplankton grazing preference normalization factor  
      fac=(cc(z,ci)+z0)/(k3*(r1*cc(p,ci)+r2*cc(b,ci)+  &
             r3*cc(d1,ci)+r4*cc(d2,ci)+r5*cc(d3,ci)+r6*cc(d4,ci))+  &          
                      r1*cc(p,ci)**2+r2*cc(b,ci)**2+   &
             r3*cc(d1,ci)**2+r4*cc(d2,ci)**2+r5*cc(d3,ci)**2+r6*cc(d4,ci)**2)  
!!        --- ???
      min67=min(cc(a,ci),eta*cc(l,ci))  

!-----------------------------------------------------------------------------------
!   Nutrients 
!-----------------------------------------------------------------------------------

      dd(n,p,ci)=ff*q1*(cc(p,ci)+p0)
      dd(a,p,ci)=ff*q2*(cc(p,ci)+p0)
!!    ---Bacteria respiration [Bacteria biomass growth]
      dd(a,b,ci)=vb*min67/(k4+min67+cc(l,ci))*(cc(b,ci)+b0)
      dd(l,b,ci)=vb*cc(l,ci)/(k4+min67+cc(l,ci))*(cc(b,ci)+b0)


!!    ---Condition of [ldon] to allow ldon to become a matrix for the aggregate 
!  ----> Il faudra ajouter plus tard une fonction de turbulence
if (cc(l,ci) .ge. lmin) then        !#1                           
     dd(l,d4,ci)=mldon*cc(l,ci)                            
else                                !#1                       
     dd(l,d4,ci)= 0.0                                         
end if                              !#1                 
!!    ---Nitrification rate 
     dd(a,n,ci)=mu5*cc(a,ci)                                 
      
!-----------------------------------------------------------------------------------
!   Phytoplankton [LOSSES]
!-----------------------------------------------------------------------------------

!!    ---Grazing losses (if beta =1 zooplankton eat everything)
      dd(p,z,ci)=beta*gmax*r1*cc(p,ci)**2*fac

!!    ---Non grazing losses : Excretion
      dd(p,l,ci)=gamma*ff*(q1+q2)*cc(p,ci)

!!    ---Non grazing losses : 
!!            -viral lysis (not taken into acount here)
!!            -internal respiration degradation
!In Moore et al,.2002, all Non grazing losses are estimated at 10% for diatom and small phytoplankton.
     dd(p,d1,ci)=mu1*(cc(p,ci)+p0)/(k5+cc(p,ci)+p0)*cc(p,ci)  &  
                 +(1.-beta)*gmax*r1*cc(p,ci)**2*fac

!!    ---Aggregation losses (See section COLLISION & AGGREGATION PROCESSES)

!-----------------------------------------------------------------------------------
!   Zooplankton [LOSSES]
!-----------------------------------------------------------------------------------

!!    ---Excretion
       dd(z,a,ci)=epsi*mu2*(cc(z,ci)+z0)/(k6+cc(z,ci)+z0)*cc(z,ci)
       dd(z,l,ci)=delta*mu2*(cc(z,ci)+z0)/(k6+cc(z,ci)+z0)*cc(z,ci)
!!    ---Sloppy feeding (z to dph or d1) :Taken into account in dd(p,d1,ci) see above.
!!    ---Fecal pelets
      dd(z,d3,ci)=eg*mu2*(cc(z,ci)+z0)/(k6+cc(z,ci)+z0)*cc(z,ci)
!!    ---Dead Zoo
      dd(z,d2,ci)=(1.-epsi-delta-eg)  &                       
        *mu2*(cc(z,ci)+z0)/(k6+cc(z,ci)+z0)*cc(z,ci) 

!-----------------------------------------------------------------------------------
!   Zooplankton [Diurnal migration]
!               -----------
! As a function of light and [phytoplancton](food)
!-----------------------------------------------------------------------------------

  if (Migra_zoo.eq. 1.0) then  !#2
      if (cc(p,ci) .lt. pmin .or. cc(d1,ci) .lt. dph_initial )  then        !#3    
          ws(z,ci) = -1.0*w_zmax*tanh(bertha*(par(ci)-parcrit)) 
      else !#3
          ws(z,ci) = 0.0
      end if !#3
 else            !#2
  ws(z,ci) = 0.0
 end if          !#2

!-----------------------------------------------------------------------------------
!   Bacterias [LOSSES]
!-----------------------------------------------------------------------------------
!!    ---Bacteria remineralisation    
       dd(b,a,ci)=mu3*cc(b,ci)   
!!    ---Bacteria grazed by zooplankton     
       dd(b,z,ci)=beta*gmax*r2*cc(b,ci)**2*fac
!!    ---Bacteria in aggregate (D4)
       dd(b,d4,ci)=mbac*cc(b,ci)                        
!!    ---Bacteria to detritus-non grazing mortality (D1)
       dd(b,d1,ci)=(1.-beta)*gmax*r2*cc(b,ci)**2*fac   

!-----------------------------------------------------------------------------------
!   Detritus [LOSSES]
!-----------------------------------------------------------------------------------
 
!!    ---Exsudation (see Moore et al., 2002)
      dd(d1,l,ci)=dphlossl*cc(d1,ci)            
      dd(d2,l,ci)=dzolossl*cc(d2,ci)            
      dd(d3,l,ci)=fplossl*cc(d3,ci)             

!!    ---Grazing
      dd(d1,z,ci)=beta*gmax*r3*cc(d1,ci)**2*fac       !broutage-Saprophagie
      dd(d2,z,ci)=beta*gmax*r4*cc(d2,ci)**2*fac       !Carnivorie/cannibalisme/necrophagie
      dd(d3,z,ci)=beta*gmax*r5*cc(d3,ci)**2*fac       !Copprophagy/scatophagie
      dd(d4,z,ci)=beta*gmax*r6*cc(d4,ci)**2*fac       !broutage-Macrophagie


!-------------------------------------------------------------------------------------
!!   Size (radius in m) variation for each particle type 
!-------------------------------------------------------------------------------------

if (size_rand .eq. 1.0) then!#4

  call random_number(r_phy)
size_phy=size_phy_us+((size_phy_up-size_phy_us)*r_phy)
   call random_number(r_dph)
size_dph=size_dph_us+((size_dph_up-size_dph_us)*r_dph)
   call random_number(r_dzo)
size_dzo=size_dzo_us+((size_dzo_up-size_dzo_us)*r_dzo)
  call random_number(r_fp)
size_fp=size_fp_us+((size_fp_up-size_fp_us)*r_fp)
   
else  !#4
size_phy=size_phy_us
size_dph=size_dph_us
size_dzo=size_dzo_us
size_fp=size_fp_us

endif !#4


!-----------------------------------------------------------------------------------
!   COLLISION & AGGREGATION 
!               -----------
! Read following Documentation at the end of the code
!-----------------------------------------------------------------------------------
 
!-----------------------------------------------------------------------------------

!-------------------------------------------------------------------------------------
!!   Sedimentation rate for each particle type 
!------------------------------------------------------------------------------------- 

!----------Definition of the equivalent diameter of our particles [Bagheri 2015]-Unit of size [m]
!dS,i=racine3(LIS)   
!With L the longest dimension of the particle, I the longest dimension perpendicular to L, and S the longest dimension perpendicular to L and I (Bagheri 2015).
!With for phytoplankton and dead phytoplankton : L=I=S= size_X*2, and for dead zoo plankton and fecal pellets : L=I=size_x*4 and S= size_X*2 --» With size_X which represents the radius of our particle.
!We consider size_X the value of size that we can multiply to have the dimension we are looking for.
!Calculation of the cubic racin of X --» SIGN(ABS(X)**(1./3.),X) [FORTRAN, le langage normalisé, Michel Dubesset,Jean Vignes]

diam_phy = sign(abs(((size_phy*2.)*(size_phy*2.)*(size_phy*2.)))**(1./3.),((size_phy*2.)*(size_phy*2.)*(size_phy*2.)))

diam_dph = sign(abs(((size_dph*2.)*(size_dph*2.)*(size_dph*2.)))**(1./3.),((size_dph*2.)*(size_dph*2.)*(size_dph*2.))) 

diam_dzo = sign(abs(((size_dzo*4.)*(size_dzo*4.)*(size_dzo*2.)))**(1./3.),((size_dzo*4.)*(size_dzo*4.)*(size_dzo*2.)))

diam_fp  = sign(abs(((size_fp*4.)*(size_fp*4.)*(size_fp*2.)))**(1./3.),((size_fp*4.)*(size_fp*4.)*(size_fp*2.)))


!----------Calcul of densities
!Fluid density
densFlu = rho_0
!Difference of densities
Rp = (rho_p-densFlu)
Rdph= (rho_dph-densFlu)
Rdzo =(rho_dzo-densFlu)
Rfp =(rho_fp-densFlu)


!!--------!Calculated by value from the .nml
if (Phys_w .eq. 0.0 ) then  !#5

      w_p_m   = w_p
      w_dph_m = w_dph
      w_dzo_m = w_dzo
      w_fp_m  = w_fp

!!--------Stokes's law from Ghosh et al 2013
!     all particles are considered as sphere,so the value of diameter needed in the equation = radius*2                     
else if (Phys_w .eq. 1.0) then  !#5

      !--Phytoplankton
      w_p_m=(g*((2*size_phy)**2)*Rp)/(18*dynvis) 
      w_p_m=w_p_m/secs_pr_day 

      !--Dead phytoplankton
      w_dph_m=(g*((2*size_dph)**2)*Rdph)/(18*dynvis) 
      w_dph_m=w_dph_m/secs_pr_day 

      !--Dead Zooplankton
      w_dzo_m=(g*((2*size_dzo)**2)*Rdzo)/(18*dynvis) 
      w_dzo_m = w_dzo_m/secs_pr_day    

      !--Fecal pellets
      w_fp_m=(g*((2*size_fp)**2)*Rfp)/(18*dynvis) 
      w_fp_m = w_fp_m/secs_pr_day 


!!--------Modified Stoke's law (For cylindrical particles) from McDonnell et al 2010
!   Utilisation of spherical diameter as D of particles, and respective values of L,I,S to represent particle shapes
!Even if the values of L,I,S of phy and dph represents a spehrical particles, it will be used in the same way following this special stoke's law for cylindrical
!    [w=((0.079*(r_X-r_F)*L^2*g)/dynvis)*(L/D)^-1.664].

else if (Phys_w .eq. 2.0) then !#5

      !--Phytoplankton (Stoke's law for cylindrical) from McDonnell et al 2010
      w_p_m=((0.079*Rp*((2*size_phy)**2)*g)/dynvis)*((2*size_phy)/(diam_phy))**(-1.664) 
      w_p_m=w_p_m/secs_pr_day 

      !--Dead phytoplankton(Stoke's law for cylindrical) from McDonnell et al 2010
      w_dph_m=((0.079*Rdph*((2*size_dph)**2)*g)/dynvis)*((2*size_dph)/(diam_dph))**(-1.664)
      w_dph_m=w_dph_m/secs_pr_day 

      !--Dead Zooplankton(Stoke's law for cylindrical) from McDonnell et al 2010
      w_dzo_m = ((0.079*Rdzo*((4*size_dzo)**2)*g)/dynvis)*((4*size_dzo)/(diam_dzo))**(-1.664)  
      w_dzo_m = w_dzo_m/secs_pr_day    

      !--Fecal pellets(Stoke's law for cylindrical)from McDonnell et al 2010
      w_fp_m = ((0.079*Rfp*((4*size_fp)**2)*g)/dynvis)*((4*size_fp)/(diam_fp))**(-1.664) 
      w_fp_m = w_fp_m/secs_pr_day 

!!--------Power law evolution from Alldredge and Gotschalk, 1988 based on Kajihara 1971 and from Komar 1981
!                    Calculation of the settling velocity (m/j)/secs_pr_day = m/s - utilisation here of the diameters 
else if (Phys_w .eq. 3.0) then !#5

      !--Phytoplankton (Alldredge and Gotschalk, 1988 based on Kajihara 1971)
      if (diam_phy .gt. 0.001) then !#5.1
       w_p_m =-(50*((diam_phy)**0.26))/secs_pr_day 
      else if (diam_phy .lt. 0.001 .and. diam_phy .gt. 0.0) then !#5.1
       w_p_m =-(160*((diam_phy)**0.57))/secs_pr_day  
      else !#5.1
       w_p_m =0.0
      endif!#5.1

   !--Dead Phytoplankton (Alldredge and Gotschalk, 1988 based on Kajihara 1971)
      if (diam_dph .gt. 0.001) then !#5.2
      w_dph_m =-(50*((diam_dph)**0.26))/secs_pr_day 
      else if (diam_dph.lt. 0.001 .and. diam_dph.gt. 0.0) then !#5.2
      w_dph_m =-(160*((diam_dph)**0.57))/secs_pr_day  
      else !#5.2
      w_dph_m =0.0
      endif !#5.2

   !--Dead Zooplankton (Alldredge and Gotschalk, 1988 based on Kajihara 1971)
      if (diam_dzo .gt. 0.001) then !#5.3
      w_dzo_m =-(50*((diam_dzo)**0.26))/secs_pr_day 
      else if (diam_dzo.lt. 0.001 .and. diam_dzo.gt. 0.0) then !#5.3
      w_dzo_m =-(160*((diam_dzo)**0.57))/secs_pr_day  
      else!#5.3
      w_dzo_m =0.0
      endif !#5.3

   !-- Fecal pellets (Alldredge and Gotschalk, 1988 based on Kajihara 1971)
      if (diam_fp .gt. 0.001) then !#5.4
      w_fp_m =-(50*((diam_fp)**0.26))/secs_pr_day 
      else if (diam_fp.lt. 0.001 .and. diam_fp.gt. 0.0) then !#5.4
      w_fp_m =-(160*((diam_fp)**0.57))/secs_pr_day  
      else!#5.4
      w_fp_m =0.0
      endif!#5.4

!!--------Stokes law for high reynolds number + Corey Shape Factor [Komar 1978]
else if (Phys_w .eq. 4.0) then !#5

      !--Phytoplankton 
         !-- Calculation of the CSF factor
 CSF_phy = (size_phy*2)/sqrt((size_phy*2)*(size_phy*2))

       if(CSF_phy .ge. 0.0 .and. CSF_phy.lt. 0.4) then !#5.5
          CSF_phy = 2.18-(2.09*CSF_phy)
       else if (CSF_phy .ge. 0.4 .and. CSF_phy .lt. 0.8) then !#5.6
          CSF_phy = 0.946*(CSF_phy)**(-0.378)
       else if (CSF_phy .ge. 0.8 .and. CSF_phy .le. 1.0) then !#5.6
          CSF_phy = 1.0 ! consider as a sphere
        else  !#5.6
          write (*,*) 'Error in CSF values for phytoplankton : CSF .NE. [0-1]', CSF_phy
       endif !#5.6

      w_p_m=(1/(18*(kinvis*densFlu)))*(1/CSF_phy)*(Rp)*g*(diam_phy)**2 ![m/s]
     ! w_p_m=w_p_m/secs_pr_day 

      !--Dead phytoplankton
        !-- Calculation of the CSF factor
         CSF_dph = (size_dph*2)/sqrt((size_dph*2)*(size_dph*2))

       if(CSF_dph .ge. 0.0 .and. CSF_dph.lt. 0.4) then !#5.7
          CSF_dph = 2.18-(2.09*CSF_dph)
      else if (CSF_dph .ge. 0.4 .and. CSF_dph .lt. 0.8) then !#5.7
           CSF_dph = 0.946*(CSF_dph)**(-0.378)
      else if (CSF_dph .ge. 0.8 .and. CSF_dph .le. 1.0) then !#5.7
           CSF_dph = 1.0 ! consider as a sphere
      else !#5.7
           write (*,*) 'Error in CSF values for Dead phytoplankton : CSF .NE. [0-1]',CSF_dph
      endif !#5.7


       w_dph_m=(1/(18*(kinvis*densFlu)))*(1/CSF_dph)*(Rdph)*g*(diam_dph)**2 
    !   w_dph_m=w_dph_m/secs_pr_day 

      !--Dead Zooplankton
       !-- Calculation of the CSF factor

 CSF_dzo = (size_dzo*2)/sqrt((size_dzo*4)*(size_dzo*4))

       if(CSF_dzo .ge. 0.0 .and. CSF_dzo.lt. 0.4) then !#5.8
          CSF_dzo = 2.18-(2.09*CSF_dzo)
      else if (CSF_dzo .ge. 0.4 .and. CSF_dzo .lt. 0.8) then !#5.8
           CSF_dzo = 0.946*(CSF_dzo)**(-0.378)
      else if (CSF_dzo .ge. 0.8 .and. CSF_dzo .le. 1.0) then !#5.8
           CSF_dzo = 1.0 ! consider as a sphere
      else  !#5.8
           write (*,*) 'Error in CSF values for dead zooplankton : CSF .NE. [0-1]',CSF_dzo
      endif !#5.8

      w_dzo_m=(1/(18*(kinvis*densFlu)))*(1/CSF_dzo)*(Rdzo)*g*(diam_dzo)**2 
  !   w_dzo_m = w_dzo_m/secs_pr_day 

      !--Fecal pellets
       !-- Calculation of the CSF factor
 CSF_fp = (size_fp*2)/sqrt((size_fp*4)*(size_fp*4))

      if(CSF_fp .ge. 0.0 .and. CSF_fp.lt. 0.4) then !#5.9
          CSF_fp = 2.18-(2.09*CSF_fp)
      else if (CSF_fp .ge. 0.4 .and. CSF_fp .lt. 0.8) then !#5.9
           CSF_fp = 0.946*(CSF_fp)**(-0.378)
      else if (CSF_fp .ge. 0.8 .and. CSF_fp .le. 1.0) then !#5.9
           CSF_fp = 1.0 ! consider as a sphere
      else !#5.9
            write (*,*) 'Error in CSF values for fecal pellets : CSF .NE. [0-1]',CSF_fp
      endif !#5.9

      w_fp_m=(1/(18*(kinvis*densFlu)))*(1/CSF_fp)*(Rfp)*g*(diam_fp)**2 
 !     w_fp_m = w_fp_m/secs_pr_day 

else !#5
      write(*,*) 'No specification for settling velocity of particles,set to nml values'
       w_p_m   = w_p
       w_dph_m = w_dph
       w_dzo_m = w_dzo
       w_fp_m  = w_fp
endif !#5

 ! Comparaison with the Stokes range ! VanRijn 1993
 Re_phy = w_p_m*(diam_phy/kinvis)
 Re_dph = w_dph_m*(diam_dph/kinvis)
 Re_dzo = w_dzo_m*(diam_dzo/kinvis)
 Re_fp = w_fp_m*(diam_fp/kinvis)
 
if (Re_phy .gt. 1.0)then !#6
w_p_m = diam_phy**0.5
write(*,*) 'Reynolds number for phytoplankton > 1'
endif !#6

if (Re_dph .gt. 1.0)then !#7
w_dph_m = diam_dph**0.5
write(*,*) 'Reynolds number for Dead phytoplankton > 1'
endif !#7

if (Re_dzo .gt. 1.0)then !#8
w_dzo_m = diam_dzo**0.5
write(*,*) 'Reynolds number for Dead zooplankton > 1'
endif !#8

if (Re_fp .gt. 1.0)then !#9
w_fp_m = diam_fp**0.5
write(*,*) 'Reynolds number for fecal pellets > 1'
endif !#9


w_min_m = max(w_p_m,w_dph_m,w_dzo_m,w_fp_m)



!Now that we defined a settling velocity depending of the size, let's make it dependent of the depth (Martin et al 1987, Paul Nicot 2013 )

 !w_depth = ci ! As ci is an integer and that sqrt() needs a REAL, we created w_depth that is the REAL equivalent of integer ci
 !      w_p_m   = w_p_m*sqrt(w_depth/100)
 !      w_dph_m = w_dph*sqrt(w_depth/100)
 !      w_dzo_m = w_dzo*sqrt(w_depth/100)
 !      w_fp_m  = w_fp*sqrt(w_depth/100)

!Verification that all the setling velocities used respect : w< (1/2)*(delta(z)/delta(t))
!sensi_w = (1/2)*(h/t)

!Everyone will settle at the end of the loop (see below)


!!    ---Condition of the environment to allow collision and aggregation
if (cc(l,ci) .ge. lmin) then  ! #1
!  LEVEL1 'Let"s coagulate buddy ! [LDON Concentration greater than lmin]'

!-------------------------------------------------------------------------------------
!!   Maximum loss for aggregation for each particle type  ! Moore et al.2002
!-------------------------------------------------------------------------------------
!!    ---Phytoplankton 
  pprime=max(cc(p,ci),p0)  
!!    ---Dead Phytoplankton 
  d1rime=max(cc(d1,ci),p0)
!!    ---Dead zooplankton
  d2rime=max(cc(d2,ci),p0)
!!    ---Fecal pellets 
  d3rime=max(cc(d3,ci),p0)

!-------------------------------------------------------------------------------------
!!   Stickiness for each particle type interaction
!-------------------------------------------------------------------------------------

if(sti_cst .eq. 1.0) then ! #1.2
     !Stickiness equal the value given in the nml.
         sti_2p(ci)    = stip_p
         sti_pdph(ci)  = stip_dph
         sti_pdzo(ci)  = stip_dzo
         sti_pfp(ci)   = stip_fp 
         sti_2dph(ci)  = stidph_dph
         sti_dphdzo(ci)= stidph_dzo
         sti_dphfp(ci) = stidph_fp
         sti_2dzo(ci)  = stidzo_dzo
         sti_dzofp(ci) = stidzo_fp
         sti_2fp(ci)   = stifp_fp

else !#1.2
!Select a random value for the stickiness

   call random_number(r_phy)
   stip=r_phy
   call random_number(r_dph)
   stidph=r_dph
   call random_number(r_dzo)
   stidzo=r_dzo
  call random_number(r_fp)
   stifp=r_fp

!Kiorboe 1994
  sti_2p(ci)     = 0.5*(stip+stip)
  sti_pdph(ci)   = 0.5*(stip+stidph)
  sti_pdzo(ci)   = 0.5*(stip+stidzo)
  sti_pfp(ci)    = 0.5*(stip+stifp)
  sti_2dph(ci)   = 0.5*(stidph+stidph)
  sti_dphdzo(ci) = 0.5*(stidph+stidzo)
  sti_dphfp(ci)  = 0.5*(stidph+stifp)
  sti_2dzo(ci)   = 0.5*(stidzo+stidzo)
  sti_dzofp(ci)  = 0.5*(stidzo+stifp)
  sti_2fp(ci)    = 0.5*(stifp+stifp)

endif  ! #1.2

     if (Coag_coef .eq. 0.0) then !#2
!-------------------------------------------------------------------------------------
!        LEVEL2 'No physical coagulation happens here [Constant Beta are set]'
!-------------------------------------------------------------------------------------
             if(betaBr .eq. 0.0 .and. betaSh .eq. 0.0 .and. betaDs .eq. 0.0) then  ! #3
!-------------------------------------------------------------------------------------
!-------------------------------------------------------------------------------------
!Case 1 : Phytoplankton loss by aggregation
!-------------------------------------------------------------------------------------
!-------------------------------------------------------------------------------------
 !!    ---A - Aggregation with itself
    coli_pp_NoPhys=sti_2p(ci)*betap_p*cc(p,ci)*cc(p,ci)
    agg_pp=min((txloss_p*pprime),coli_pp_NoPhys)
!!    ---B - Aggregation with dead phytoplankton
   coli_pdph_NoPhys=sti_pdph(ci)*betap_dph*cc(p,ci)*cc(d1,ci)
   agg_pdph=min((txloss_p*pprime),coli_pdph_NoPhys)
!!    ---C - Aggregation with dead zooplankton
   coli_pdzo_NoPhys=sti_pdzo(ci)*betap_dzo*cc(p,ci)*cc(d2,ci)
   agg_pdzo=min((txloss_p*pprime),coli_pdzo_NoPhys)
!!    ---D - Aggregation with Fecal pellets
   coli_pfp_NoPhys=sti_pfp(ci)*betap_fp*cc(p,ci)*cc(d3,ci)
   agg_pfp=min((txloss_p*pprime),coli_pfp_NoPhys)

!*********
dd(p,d4,ci)= (agg_pp)+(agg_pdph)+(agg_pdzo)+(agg_pfp)
!*********

!-------------------------------------------------------------------------------------
!-------------------------------------------------------------------------------------
!Case 2 : Dead Phytoplankton loss by aggregation 
!-------------------------------------------------------------------------------------
!-------------------------------------------------------------------------------------
 !!    ---A - Aggregation with itself
    coli_dphdph_NoPhys=sti_2dph(ci)*betadph_dph*cc(d1,ci)*cc(d1,ci)
    agg_dphdph=min((txloss_dph*d1rime),coli_dphdph_NoPhys)
!!    ---B - Aggregation with dead zooplankton
   coli_dphdzo_NoPhys=sti_dphdzo(ci)*betap_dzo*cc(d1,ci)*cc(d2,ci)
   agg_dphdzo=min((txloss_dph*d1rime),coli_dphdzo_NoPhys)
!!    ---C - Aggregation with Fecal pellets
   coli_dphfp_NoPhys=sti_dphfp(ci)*betadph_fp*cc(d1,ci)*cc(d3,ci)
   agg_dphfp=min((txloss_dph*d1rime),coli_dphfp_NoPhys)

!*********
dd(d1,d4,ci)= (agg_pdph)+(agg_dphdph)+(agg_dphdzo)+(agg_dphfp)
!*********

!-------------------------------------------------------------------------------------
!-------------------------------------------------------------------------------------
!Case 3 : Dead Zooplankton loss by aggregation 
!-------------------------------------------------------------------------------------
!-------------------------------------------------------------------------------------
 !!    ---A - Aggregation with itself
    coli_dzodzo_NoPhys=sti_2dzo(ci)*betadzo_dzo*cc(d2,ci)*cc(d2,ci)
    agg_dzodzo=min((txloss_dzo*d2rime),coli_dzodzo_NoPhys)
!!    ---B - Aggregation with Fecal pellets
   coli_dzofp_NoPhys=sti_dzofp(ci)*betadzo_fp*cc(d2,ci)*cc(d3,ci)
   agg_dzofp=min((txloss_dzo*d2rime),coli_dzofp_NoPhys)

!*********
dd(d2,d4,ci)= (agg_pdzo)+(agg_dphdzo)+(agg_dzodzo)+(agg_dzofp)
!*********

!-------------------------------------------------------------------------------------
!-------------------------------------------------------------------------------------
!Case 4 : Fecal pellets loss by aggregation 
!-------------------------------------------------------------------------------------
!-------------------------------------------------------------------------------------
!!    ---A - Aggregation with itself
    coli_fpfp_NoPhys=sti_2fp(ci)*betafp_fp*cc(d3,ci)*cc(d3,ci)
    agg_fpfp=min((txloss_fp*d3rime),coli_fpfp_NoPhys)

!*********
dd(d3,d4,ci)= (agg_pfp)+(agg_dphfp)+(agg_dzofp)+(agg_fpfp)
!*********
endif !#3

!*************************************************************************************
!!    ---Choice from the user to use and study only betaBr for the calculation of the kernel coefficient

!-------------------------------------------------------------------------------------
!LEVEL3 'Nevertheless...I rather prefer Coagulate only with : BetaBR'
!-------------------------------------------------------------------------------------
if(betaBr .eq. 1.0 .and. betaSh .eq. 0.0 .and. betaDs .eq. 0.0) then !#4
!-------------------------------------------------------------------------------------
! Usefull data
!-------------------------------------------------------------------------------------

!!    ---Conversion of field C° temperature to K° - description of physic_Br for easier calcul
!LEVEL2 'Temperature bath is dependant of mother-ocean"s mood ! [Temp. variable due to field/physical]'
   T_degK=t(ci)+ 273.15 
   physic_Br = (2*kB*T_degK)/(3*dynvis)

!-------------------------------------------------------------------------------------
!-------------------------------------------------------------------------------------
!Case 1 : Phytoplankton loss by aggregation
!-------------------------------------------------------------------------------------
!-------------------------------------------------------------------------------------
!!    ---A - Aggregation with itself
    betaBr_pp=(physic_Br)*(((size_phy+size_phy)**2)/(size_phy*size_phy))
    coli_pp=sti_2p(ci)* betaBr_pp*cc(p,ci)*cc(p,ci)
    agg_pp=min((txloss_p*pprime),coli_pp)
!!    ---B - Aggregation with dead phytoplankton
   betaBr_pdph=(physic_Br)*(((size_phy+size_dph)**2)/(size_phy*size_dph))
   coli_pdph=sti_pdph(ci)*betaBr_pdph*cc(p,ci)*cc(d1,ci)
   agg_pdph=min((txloss_p*pprime),coli_pdph)
!!    ---C - Aggregation with dead zooplankton
   betaBr_pdzo=(physic_Br)*(((size_phy+size_dzo)**2)/(size_phy*size_dzo))
   coli_pdzo=sti_pdzo(ci)*betaBr_pdzo*cc(p,ci)*cc(d2,ci)
   agg_pdzo=min((txloss_p*pprime),coli_pdzo)
!!    ---D - Aggregation with Fecal pellets
   betaBr_pfp=(physic_Br)*(((size_phy+size_fp)**2)/(size_phy*size_fp))
   coli_pfp=sti_pfp(ci)*betaBr_pfp*cc(p,ci)*cc(d3,ci)
   agg_pfp=min((txloss_p*pprime),coli_pfp)

!*********
!!    ---Loss of p to msn
dd(p,d4,ci)= (agg_pp)+(agg_pdph)+(agg_pdzo)+(agg_pfp)
!*********

!-------------------------------------------------------------------------------------
!-------------------------------------------------------------------------------------
!Case 2 : Dead Phytoplankton loss by aggregation
!-------------------------------------------------------------------------------------
!-------------------------------------------------------------------------------------
!!    ---A - Aggregation with itself       
     betaBr_dphdph=(physic_Br)*(((size_dph+size_dph)**2)/(size_dph*size_dph))
     coli_dphdph=sti_2dph(ci)*betaBr_dphdph*cc(d1,ci)*cc(d1,ci)
     agg_dphdph=min((txloss_dph*d1rime),coli_dphdph)
!!    ---B - Aggregation with dead zooplankton           
     betaBr_dphdzo=(physic_Br)*(((size_dph+size_dzo)**2)/(size_dph*size_dzo))
     coli_dphdzo=sti_dphdzo(ci)*betaBr_dphdzo*cc(d1,ci)*cc(d2,ci)
     agg_dphdzo=min((txloss_dph*d1rime),coli_dphdzo)
!!    ---C - Aggregation with fecal pellets         
     betaBr_dphfp=(physic_Br)*(((size_dph+size_fp)**2)/(size_dph*size_fp))
     coli_dphfp=sti_dphfp(ci)*betaBr_dphfp*cc(d1,ci)*cc(d3,ci)
     agg_dphfp=min((txloss_dph*d1rime),coli_dphfp)

!*********
!!    ---Loss of D1 or dph to msn
dd(d1,d4,ci)= (agg_pdph)+(agg_dphdph)+(agg_dphdzo)+(agg_dphfp)
!*********

!-------------------------------------------------------------------------------------
!-------------------------------------------------------------------------------------
!Case 3 : Dead zoo loss by aggregation
!-------------------------------------------------------------------------------------
!-------------------------------------------------------------------------------------
!!    ---A - Aggregation with itself          
    betaBr_dzodzo=(physic_Br)*(((size_dzo+size_dzo)**2)/(size_dzo*size_dzo))
    coli_dzodzo=sti_2dzo(ci)*betaBr_dzodzo*cc(d2,ci)*cc(d2,ci)
    agg_dzodzo=min((txloss_dzo*d2rime),coli_dzodzo)
!!    ---B - Aggregation with fecal pellets
   betaBr_dzofp=(physic_Br)*(((size_dzo+size_fp)**2)/(size_dzo*size_fp))
   coli_dzofp=sti_dzofp(ci)*betaBr_dzofp*cc(d2,ci)*cc(d3,ci)
   agg_dzofp=min((txloss_dzo*d2rime),coli_dzofp)

!*********
!!    ---Loss of D2 or dzo to msn
dd(d2,d4,ci)= (agg_pdzo)+(agg_dphdzo)+(agg_dzodzo)+(agg_dzofp)
!*********

!-------------------------------------------------------------------------------------
!-------------------------------------------------------------------------------------
!Case 4 : Fecal pellets loss by aggregation
!-------------------------------------------------------------------------------------
!-------------------------------------------------------------------------------------
!!    ---A - Aggregation with itself             
      betaBr_fpfp=(physic_Br)*(((size_fp+size_fp)**2)/(size_fp*size_fp))
      coli_fpfp=sti_2fp(ci)*betaBr_fpfp*cc(d3,ci)*cc(d3,ci)
      agg_fpfp=min((txloss_fp*d3rime),coli_fpfp)

!*********
!!    ---Loss of D3 or fp to msn
dd(d3,d4,ci)= (agg_pfp)+(agg_dphfp)+(agg_dzofp)+(agg_fpfp)
!*********

endif !#4 

!*************************************************************************************
!!    ---Choice from the user to use and study only betaSh (Curvilinear kernel for turbulence) for the calculation of the kernel coefficient
!-------------------------------------------------------------------------------------
!LEVEL3 'Nevertheless...I rather prefer Coagulate only with : BetaSh'
!-------------------------------------------------------------------------------------
if (betaBr .eq. 0.0 .and. betaSh .eq. 1.0 .and. betaDs .eq. 0.0) then  ! #5

if(eps_const .eq. 1.0) then !#5.1
!LEVEL4 'Go and shake up the bath by an EPS constant value'
 bio_eps= eps_n
else !#5.1
!LEVEL4 'Go and shake up the bath by an EPS obtains via physical environment'
 bio_eps=eps(ci)
endif !#5.1

!-------------------------------------------------------------------------------------
!-------------------------------------------------------------------------------------
!Case 1 : Phytoplankton loss by aggregation
!-------------------------------------------------------------------------------------
!-------------------------------------------------------------------------------------
!!    ---A - Aggregation with itself
   q_pp=(min(size_phy,size_phy))/(max(size_phy,size_phy))
betaSh_pp=9.8*((q_pp**2)/(1+2*q_pp**2))*(sqrt(bio_eps/kinvis))*(size_phy+size_phy)**3
    coli_pp=sti_2p(ci)*betaSh_pp*cc(p,ci)*cc(p,ci)
    agg_pp=min((txloss_p*pprime),coli_pp)
!!    ---B - Aggregation with dead phytoplankton
   q_pdph=(min(size_phy,size_dph))/(max(size_phy,size_dph))
betaSh_pdph=9.8*((q_pdph**2)/(1+2*q_pdph**2))*(sqrt(bio_eps/kinvis))*(size_phy+size_dph)**3
   coli_pdph=sti_pdph(ci)*betaSh_pdph*cc(p,ci)*cc(d1,ci)
   agg_pdph=min((txloss_p*pprime),coli_pdph)
!!    ---C - Aggregation with dead zooplankton
   q_pdzo=(min(size_phy,size_dzo))/(max(size_phy,size_dzo))
betaSh_pdzo=9.8*((q_pdzo**2)/(1+2*q_pdzo**2))*(sqrt(bio_eps/kinvis))*(size_phy+size_dzo)**3
   coli_pdzo=sti_pdzo(ci)*betaSh_pdzo*cc(p,ci)*cc(d2,ci)
   agg_pdzo=min((txloss_p*pprime),coli_pdzo)
!!    ---D - Aggregation with Fecal pellets
   q_pfp=(min(size_phy,size_fp))/(max(size_phy,size_fp))
betaSh_pfp=9.8*((q_pfp**2)/(1+2*q_pfp**2))*(sqrt(bio_eps/kinvis))*(size_phy+size_fp)**3
   coli_pfp=sti_pfp(ci)*betaSh_pfp*cc(p,ci)*cc(d3,ci)
   agg_pfp=min((txloss_p*pprime),coli_pfp)

!*********
!!    ---Loss of p to msn
dd(p,d4,ci)= (agg_pp)+(agg_pdph)+(agg_pdzo)+(agg_pfp)
!*********

!-------------------------------------------------------------------------------------
!-------------------------------------------------------------------------------------
!Case 2 : Dead Phytoplankton loss by aggregation
!-------------------------------------------------------------------------------------
!-------------------------------------------------------------------------------------
!!    ---A - Aggregation with itself       
   q_dphdph=(min(size_dph,size_dph))/(max(size_dph,size_dph))
betaSh_dphdph=9.8*((q_dphdph**2)/(1+2*q_dphdph**2))*(sqrt(eps(ci)/kinvis))*(size_dph+size_dph)**3
     coli_dphdph=sti_2dph(ci)*betaSh_dphdph*cc(d1,ci)*cc(d1,ci)
     agg_dphdph=min((txloss_dph*d1rime),coli_dphdph)
!!    ---B - Aggregation with dead zooplankton           
   q_dphdzo=(min(size_dph,size_dzo))/(max(size_dph,size_dzo))
betaSh_dphdzo=9.8*((q_dphdzo**2)/(1+2*q_dphdzo**2))*(sqrt(eps(ci)/kinvis))*(size_dph+size_dzo)**3
     coli_dphdzo=sti_dphdzo(ci)*betaSh_dphdzo*cc(d1,ci)*cc(d2,ci)
     agg_dphdzo=min((txloss_dph*d1rime),coli_dphdzo)
!!    ---C - Aggregation with fecal pellets         
   q_dphfp=(min(size_dph,size_fp))/(max(size_dph,size_fp))
betaSh_dphfp=9.8*((q_dphfp**2)/(1+2*q_dphfp**2))*(sqrt(eps(ci)/kinvis))*(size_dph+size_fp)**3
     coli_dphfp=sti_dphfp(ci)*betaSh_dphfp*cc(d1,ci)*cc(d3,ci)
     agg_dphfp=min((txloss_dph*d1rime),coli_dphfp)

!*********
!!    ---Loss of D1 or dph to msn
dd(d1,d4,ci)= (agg_pdph)+(agg_dphdph)+(agg_dphdzo)+(agg_dphfp)
!*********

!-------------------------------------------------------------------------------------
!-------------------------------------------------------------------------------------
!Case 3 : Dead zoo loss by aggregation
!-------------------------------------------------------------------------------------
!-------------------------------------------------------------------------------------
!!    ---A - Aggregation with itself          
   q_dzodzo=(min(size_dzo,size_dzo))/(max(size_dzo,size_dzo))
betaSh_dzodzo=9.8*((q_dzodzo**2)/(1+2*q_dzodzo**2))*(sqrt(eps(ci)/kinvis))*(size_dzo+size_dzo)**3
 coli_dzodzo=sti_2dzo(ci)*betaSh_dzodzo*cc(d2,ci)*cc(d2,ci)
    agg_dzodzo=min((txloss_dzo*d2rime),coli_dzodzo)
!!    ---B - Aggregation with fecal pellets
   q_dzofp=(min(size_dzo,size_fp))/(max(size_dzo,size_fp))
betaSh_dzofp=9.8*((q_dzofp**2)/(1+2*q_dzofp**2))*(sqrt(eps(ci)/kinvis))*(size_dzo+size_fp)**3
   coli_dzofp=sti_dzofp(ci)*betaSh_dzofp*cc(d2,ci)*cc(d3,ci)
   agg_dzofp=min((txloss_dzo*d2rime),coli_dzofp)

!*********
!!    ---Loss of D2 or dzo to msn
dd(d2,d4,ci)= (agg_pdzo)+(agg_dphdzo)+(agg_dzodzo)+(agg_dzofp)
!*********

!-------------------------------------------------------------------------------------
!-------------------------------------------------------------------------------------
!Case 4 : Fecal pellets loss by aggregation
!-------------------------------------------------------------------------------------
!-------------------------------------------------------------------------------------
!!    ---A - Aggregation with itself             
   q_fpfp=(min(size_fp,size_fp))/(max(size_fp,size_fp))
betaSh_fpfp=9.8*((q_fpfp**2)/(1+2*q_fpfp**2))*(sqrt(eps(ci)/kinvis))*(size_fp+size_fp)**3
   coli_fpfp=sti_2fp(ci)*betaSh_fpfp*cc(d3,ci)*cc(d3,ci)
      agg_fpfp=min((txloss_fp*d3rime),coli_fpfp)

!*********
!!    ---Loss of D3 or fp to msn
dd(d3,d4,ci)= (agg_pfp)+(agg_dphfp)+(agg_dzofp)+(agg_fpfp)
!*********

endif ! #5

!*************************************************************************************
!!    ---Choice from the user to use and study only betaDs (curvilinear) for the calculation of the kernel coefficient
!-------------------------------------------------------------------------------------
!LEVEL3 'Nevertheless...I rather prefer Coagulate only with : BetaDs'
!-------------------------------------------------------------------------------------
if (betaBr .eq. 0.0 .and. betaSh .eq. 0.0 .and. betaDs .eq. 1.0) then ! #6

!-------------------------------------------------------------------------------------
!-------------------------------------------------------------------------------------
!Case 1 : Phytoplankton loss by aggregation
!-------------------------------------------------------------------------------------
!-------------------------------------------------------------------------------------
!!    ---A - Aggregation with itself
!abs_pp=ABS(w_p_m-w_p_m)
!betaDs_pp=0.5*pi*(size_phy**2)*abs_pp
! coli_pp=sti_2p(ci)*betaDs_pp*cc(p,ci)*cc(p,ci)
! agg_pp=(min((txloss_p*pprime),coli_pp))

!!    ---B - Aggregation with dead phytoplankton
 abs_pdph= ABS(w_p_m-w_dph_m)  
betaDs_pdph=0.5*pi*(min(size_phy,size_dph)**2)*abs_pdph
   coli_pdph=sti_pdph(ci)*betaDs_pdph*cc(p,ci)*cc(d1,ci)
   agg_pdph=min((txloss_p*pprime),coli_pdph)
!!    ---C - Aggregation with dead zooplankton
 abs_pdzo= ABS(w_p_m-w_dzo_m)  
 betaDs_pdzo=0.5*pi*(min(size_phy,size_dzo)**2)*abs_pdzo
   coli_pdzo=sti_pdzo(ci)* betaDs_pdzo*cc(p,ci)*cc(d2,ci)
   agg_pdzo=min((txloss_p*pprime),coli_pdzo)
!!    ---D - Aggregation with Fecal pellets
 abs_pfp= ABS(w_p_m-w_fp_m)  
 betaDs_pfp=0.5*pi*(min(size_phy,size_fp)**2)*abs_pfp
   coli_pfp=sti_pfp(ci)*betaDs_pfp*cc(p,ci)*cc(d3,ci)
   agg_pfp=min((txloss_p*pprime),coli_pfp)

!*********
!!    ---Loss of p to msn
!dd(p,d4,ci)=(agg_pp)+ (agg_pdph)+(agg_pdzo)+(agg_pfp)
dd(p,d4,ci)=(agg_pdph)+(agg_pdzo)+(agg_pfp)

!*********

!-------------------------------------------------------------------------------------
!-------------------------------------------------------------------------------------
!Case 2 : Dead Phytoplankton loss by aggregation
!-------------------------------------------------------------------------------------
!-------------------------------------------------------------------------------------
!!    ---A - Aggregation with itself       
!abs_dphdph=ABS(w_dph-w_dph)
!betaDs_dphdph=0.5*pi*(size_dph**2)*abs_dphdph
! coli_dphdph=sti_2dph(ci)*betaDs_dphdph*cc(d1,ci)*cc(d1,ci)
! agg_dphdph=(min((txloss_dph*d1rime),coli_dphdph))
!!    ---B - Aggregation with dead zooplankton           
 abs_dphdzo= ABS(w_dph_m-w_dzo_m) 
 betaDs_dphdzo=0.5*pi*(min(size_dph,size_dzo)**2)*abs_dphdzo
     coli_dphdzo=sti_dphdzo(ci)*betaDs_dphdzo*cc(d1,ci)*cc(d2,ci)
     agg_dphdzo=min((txloss_dph*d1rime),coli_dphdzo)
!!    ---C - Aggregation with fecal pellets         
 abs_dphfp= ABS(w_dph_m-w_fp_m) 
 betaDs_dphfp=0.5*pi*(min(size_dph,size_fp)**2)*abs_dphfp
     coli_dphfp=sti_dphfp(ci)*betaDs_dphfp*cc(d1,ci)*cc(d3,ci)
     agg_dphfp=min((txloss_dph*d1rime),coli_dphfp)

!*********
!!    ---Loss of D1 or dph to msn
!dd(d1,d4,ci)= (agg_dphdph)+(agg_pdph)+(agg_dphdzo)+(agg_dphfp)
dd(d1,d4,ci)= (agg_pdph)+(agg_dphdzo)+(agg_dphfp)
!*********

!-------------------------------------------------------------------------------------
!-------------------------------------------------------------------------------------
!Case 3 : Dead zoo loss by aggregation
!-------------------------------------------------------------------------------------
!-------------------------------------------------------------------------------------
!!    ---A - Aggregation with itself          
! abs_dzodzo= ABS(w_dzo_m-w_dzo_m) 
! betaDs_dzofp=0.5*pi*(size_dzo**2)*abs_dzodzo
!   coli_dzodzo=sti_2dzo(ci)*betaDs_dzodzo*cc(d2,ci)*cc(d2,ci)
!   agg_dzodzo=min((txloss_dzo*d2rime),coli_dzodzo)
!!    ---B - Aggregation with fecal pellets
 abs_dzofp= ABS(w_dzo_m-w_fp_m) 
 betaDs_dzofp=0.5*pi*(min(size_dzo,size_fp)**2)*abs_dzofp
   coli_dzofp=sti_dzofp(ci)*betaDs_dzofp*cc(d2,ci)*cc(d3,ci)
   agg_dzofp=min((txloss_dzo*d2rime),coli_dzofp)

!*********
!!    ---Loss of D2 or dzo to msn
dd(d2,d4,ci)=(agg_pdzo)+(agg_dphdzo)+(agg_dzofp)
!dd(d2,d4,ci)=(agg_dzodzo)+ (agg_pdzo)+(agg_dphdzo)+(agg_dzofp)
!*********

!-------------------------------------------------------------------------------------
!-------------------------------------------------------------------------------------
!Case 4 : Fecal pellets loss by aggregation
!-------------------------------------------------------------------------------------
!-------------------------------------------------------------------------------------
!!    ---A - Aggregation with itself             
!abs_fpfp=abs(w_fp_m-w_fp_m)
!betaDs_fpfp=0.5*pi*(size_fp**2)*abs_fpfp
! coli_fpfp=sti_2fp(ci)*betaDs_fpfp*cc(d3,ci)*cc(d3,ci)
! agg_fpfp=min((txloss_fp*d3rime),coli_fpfp)
!*********
!!    ---Loss of D3 or fp to msn
!dd(d3,d4,ci)= (agg_fpfp)+(agg_pfp)+(agg_dphfp)+(agg_dzofp)
dd(d3,d4,ci)= (agg_pfp)+(agg_dphfp)+(agg_dzofp)
!*********
endif !#6

!*************************************************************************************
else !#2 (if Coag.Coef .eq. 1.0)
!-------------------------------------------------------------------------------------
!LEVEL3 'Physical Coagulation happens here ![Beta are calculated via physic]'
!-------------------------------------------------------------------------------------

!-------------------------------------------------------------------------------------
! Usefull data
!-------------------------------------------------------------------------------------
!!    ---Conversion of field C° temperature to K°
   T_degK=t(ci)+ 273.15 

!!    ---Determination of the value of eps
if(eps_const .eq. 1.0) then !#2.1
 bio_eps= eps_n
else !#2.1
 bio_eps=eps(ci)
endif !#2.1
!!    ---Determination of physic_Br
   physic_Br = (2*kB*T_degK)/(3*dynvis)

!-------------------------------------------------------------------------------------
!-------------------------------------------------------------------------------------
!Case 1 : Phytoplankton loss by aggregation
!-------------------------------------------------------------------------------------
!-------------------------------------------------------------------------------------
!!    ---A - Aggregation with itself
betaBr_pp=(physic_Br)*(((size_phy+size_phy)**2)/(size_phy*size_phy))
 q_pp=(min(size_phy,size_phy))/(max(size_phy,size_phy))
betaSh_pp=9.8*((q_pp**2)/(1+2*q_pp**2))*(sqrt(bio_eps/kinvis))*(size_phy+size_phy)**3
!No BDs 

    kernel_coef_pp=betaBr_pp+betaSh_pp
    coli_pp=sti_2p(ci)*kernel_coef_pp*cc(p,ci)*cc(p,ci)
    agg_pp=min((txloss_p*pprime),coli_pp)

!!    ---B - Aggregation with dead phytoplankton
betaBr_pdph=(physic_Br)*(((size_phy+size_dph)**2)/(size_phy*size_dph))
 q_pdph=(min(size_phy,size_dph))/(max(size_phy,size_dph))
betaSh_pdph=9.8*((q_pdph**2)/(1+2*q_pdph**2))*(sqrt(bio_eps/kinvis))*(size_phy+size_dph)**3
 abs_pdph= ABS(w_p_m-w_dph_m)  
betaDs_pdph=(1/2)*pi*(min(size_phy,size_dph)**2)*abs_pdph
    
   kernel_coef_pdph=betaBr_pdph+betaSh_pdph+betaDs_pdph
   coli_pdph=sti_pdph(ci)*kernel_coef_pdph*cc(p,ci)*cc(d1,ci)
   agg_pdph=min((txloss_p*pprime),coli_pdph)

!!    ---C - Aggregation with dead zooplankton
betaBr_pdzo=(physic_Br)*(((size_phy+size_dzo)**2)/(size_phy*size_dzo))
 q_pdzo=(min(size_phy,size_dzo))/(max(size_phy,size_dzo))
betaSh_pdzo=9.8*((q_pdzo**2)/(1+2*q_pdzo**2))*(sqrt(bio_eps/kinvis))*(size_phy+size_dzo)**3
 abs_pdzo= ABS(w_p_m-w_dzo_m)  
betaDs_pdzo=(1/2)*pi*(min(size_phy,size_dzo)**2)*abs_pdzo

    kernel_coef_pdzo=betaBr_pdzo+betaSh_pdzo+betaDs_pdzo
    coli_pdzo=sti_pdzo(ci)*kernel_coef_pdzo*cc(p,ci)*cc(d2,ci)
    agg_pdzo=min((txloss_p*pprime),coli_pdzo)

!!    ---D - Aggregation with Fecal pellets
betaBr_pfp=(physic_Br)*(((size_phy+size_fp)**2)/(size_phy*size_fp))
 q_pfp=(min(size_phy,size_fp))/(max(size_phy,size_fp))
betaSh_pfp=9.8*((q_pfp**2)/(1+2*q_pfp**2))*(sqrt(eps(ci)/kinvis))*(size_phy+size_fp)**3
 abs_pfp= ABS(w_p_m-w_fp_m)  
betaDs_pfp=(1/2)*pi*(min(size_phy,size_fp)**2)*abs_pfp
    
    kernel_coef_pfp=betaBr_pfp+betaSh_pfp+betaDs_pfp
    coli_pfp=sti_pfp(ci)*kernel_coef_pfp*cc(p,ci)*cc(d3,ci)
    agg_pfp=min((txloss_p*pprime),coli_pfp)

!*********
!!    ---Loss of p to msn
dd(p,d4,ci)= (agg_pp)+(agg_pdph)+(agg_pdzo)+(agg_pfp)
!*********

!-------------------------------------------------------------------------------------
!-------------------------------------------------------------------------------------
!Case 2 : Dead Phytoplankton loss by aggregation
!-------------------------------------------------------------------------------------
!-------------------------------------------------------------------------------------

!!    ---A - Aggregation with itself       
betaBr_dphdph=(physic_Br)*(((size_dph+size_dph)**2)/(size_dph*size_dph))
   q_dphdph=(min(size_dph,size_dph))/(max(size_dph,size_dph))
betaSh_dphdph=9.8*((q_dphdph**2)/(1+2*q_dphdph**2))*(sqrt(bio_eps/kinvis))*(size_dph+size_dph)**3
!No BDs 

    kernel_coef_dphdph=betaBr_dphdph+betaSh_dphdph
     coli_dphdph=sti_2dph(ci)*kernel_coef_dphdph*cc(d1,ci)*cc(d1,ci)
     agg_dphdph=min((txloss_dph*d1rime),coli_dphdph)

!!    ---B - Aggregation with dead zooplankton           
betaBr_dphdzo=(physic_Br)*(((size_dph+size_dzo)**2)/(size_dph*size_dzo))
   q_dphdzo=(min(size_dph,size_dzo))/(max(size_dph,size_dzo))
betaSh_dphdzo=9.8*((q_dphdzo**2)/(1+2*q_dphdzo**2))*(sqrt(bio_eps/kinvis))*(size_dph+size_dzo)**3
 abs_dphdzo= ABS(w_dph_m-w_dzo_m) 
betaDs_dphdzo=(1/2)*pi*(min(size_dph,size_dzo)**2)*abs_dphdzo

    kernel_coef_dphdzo=betaBr_dphdzo+betaSh_dphdzo+betaDs_dphdzo
     coli_dphdzo=sti_dphdzo(ci)*kernel_coef_dphdzo*cc(d1,ci)*cc(d2,ci)
     agg_dphdzo=min((txloss_dph*d1rime),coli_dphdzo)

!!    ---C - Aggregation with fecal pellets         
betaBr_dphfp=(physic_Br)*(((size_dph+size_fp)**2)/(size_dph*size_fp))
   q_dphfp=(min(size_dph,size_fp))/(max(size_dph,size_fp))
betaSh_dphfp=9.8*((q_dphfp**2)/(1+2*q_dphfp**2))*(sqrt(bio_eps/kinvis))*(size_dph+size_fp)**3
 abs_dphfp= ABS(w_dph_m-w_fp_m) 
betaDs_dphfp=(1/2)*pi*(min(size_dph,size_fp)**2)*abs_dphfp

     kernel_coef_dphfp=betaBr_dphfp+betaSh_dphfp+betaDs_dphfp
     coli_dphfp=sti_dphfp(ci)*kernel_coef_dphfp*cc(d1,ci)*cc(d3,ci)
     agg_dphfp=min((txloss_dph*d1rime),coli_dphfp)

!*********
!!    ---Loss of d1 or dph to msn
dd(d1,d4,ci)= (agg_pdph)+(agg_dphdph)+(agg_dphdzo)+(agg_dphfp)
!*********

!-------------------------------------------------------------------------------------
!-------------------------------------------------------------------------------------
!Case 3 : Dead zoo loss by aggregation
!-------------------------------------------------------------------------------------
!-------------------------------------------------------------------------------------

!!    ---A - Aggregation with itself          
betaBr_dzodzo=(physic_Br)*(((size_dzo+size_dzo)**2)/(size_dzo*size_dzo))
   q_dzodzo=(min(size_dzo,size_dzo))/(max(size_dzo,size_dzo))
betaSh_dzodzo=9.8*((q_dzodzo**2)/(1+2*q_dzodzo**2))*(sqrt(bio_eps/kinvis))*(size_dzo+size_dzo)**3
! No BDs

    kernel_coef_dzodzo=betaBr_dzodzo+betaSh_dzodzo
    coli_dzodzo=sti_2dzo(ci)*kernel_coef_dzodzo*cc(d2,ci)*cc(d2,ci)
    agg_dzodzo=min((txloss_dzo*d2rime),coli_dzodzo)

!!    ---B - Aggregation with fecal pellets
betaBr_dzofp=(physic_Br)*(((size_dzo+size_fp)**2)/(size_dzo*size_fp))
   q_dzofp=(min(size_dzo,size_fp))/(max(size_dzo,size_fp))
betaSh_dzofp=9.8*((q_dzofp**2)/(1+2*q_dzofp**2))*(sqrt(bio_eps/kinvis))*(size_dzo+size_fp)**3
 abs_dzofp= ABS(w_dzo_m-w_fp_m) 
betaDs_dzofp=(1/2)*pi*(min(size_dzo,size_fp)**2)*abs_dzofp

    kernel_coef_dzofp=betaBr_dzofp+betaSh_dzofp+betaDs_dzofp
    coli_dzofp=sti_dzofp(ci)*kernel_coef_dzofp*cc(d2,ci)*cc(d3,ci)
    agg_dzofp=min((txloss_dzo*d2rime),coli_dzofp) 

!*********
!!    ---Loss of D2 or dzo to msn
dd(d2,d4,ci)= (agg_pdzo)+(agg_dphdzo)+(agg_dzodzo)+(agg_dzofp)
!*********

!-------------------------------------------------------------------------------------
!-------------------------------------------------------------------------------------
!Case 4 : Fecal pellets loss by aggregation
!-------------------------------------------------------------------------------------
!-------------------------------------------------------------------------------------

!!    ---A - Aggregation with itself             
betaBr_fpfp=(physic_Br)*(((size_fp+size_fp)**2)/(size_fp*size_fp))
   q_fpfp=(min(size_fp,size_fp))/(max(size_fp,size_fp))
betaSh_fpfp=9.8*((q_fpfp**2)/(1+2*q_fpfp**2))*(sqrt(bio_eps/kinvis))*(size_fp+size_fp)**3
!No BDs

    kernel_coef_fpfp=betaBr_fpfp+betaSh_fpfp
    coli_fpfp=sti_2fp(ci)*kernel_coef_fpfp*cc(d3,ci)*cc(d3,ci)
    agg_fpfp=min((txloss_fp*d3rime),coli_fpfp)

!*********
!!    ---Loss of D3 or fp to msn
dd(d3,d4,ci)= (agg_pfp)+(agg_dphfp)+(agg_dzofp)+(agg_fpfp)
!*********

endif !#2

!!    ---No aggregation due to [LDON] in the environment
else  ! #1  (if cc(l,ci « lmin))

!LEVEL1 'LDON Concentration smaller than lmin'

  dd(p,d4,ci) = 0.0
  dd(d1,d4,ci)= 0.0
  dd(d2,d4,ci)= 0.0
  dd(d3,d4,ci)= 0.0
   
endif !#1

!-----------------------------------------------------------------------------------
!             SIZE Marine Snow with aggregation
!-----------------------------------------------------------------------------------
               
              ! ---Maximum size at dissagregation (Alldredge 1990)
                if(dm_msn .eq. 1.0) then
                diam_msn_max = coef1*(eps(ci))**(-coef2) ! [m]
                   write(*,*)'At depth-----',ci
                write(*,*)'Maximum size diameter of Msn from eps :', diam_msn_max
                else
                diam_msn_max = diam_msn_us ! [m] 
                endif

            ! ---Flux from particules to msn [mmol/m3/s]
                Flux_P(ci)  = (agg_pp)+(agg_pdph)+(agg_pdzo)+(agg_pfp)
                Flux_D1(ci) = (agg_pdph)+(agg_dphdph)+(agg_dphdzo)+(agg_dphfp)
                Flux_D2(ci) =  (agg_pdzo)+(agg_dphdzo)+(agg_dzodzo)+(agg_dzofp)
                Flux_D3(ci) =  (agg_pfp)+(agg_dphfp)+(agg_dzofp)+(agg_fpfp)

            ! ---Maximum Flux of Particles to msn [mmol/m3/s]
                max_flux= max(Flux_P(ci),Flux_D1(ci),Flux_D2(ci),Flux_D3(ci))


            ! ---Total Flux received by msn [mmol/m3/s]
                flux_msn(ci) = Flux_P(ci)+Flux_D1(ci)+Flux_D2(ci)+Flux_D3(ci) 

        !-----------------------------------------------------------------------------------
        !!    ---Safety check
        !-----------------------------------------------------------------------------------

             ! ---Sum of ratio should equal 1
                if(Flux_P(ci)+Flux_D1(ci)+Flux_D2(ci)+Flux_D3(ci).gt. 1.0) then
                  write(*,*)'At depth-----',ci
                  write(*,*)'Warning: sum of Ratio > 1.0', Flux_P(ci)+Flux_D1(ci)+Flux_D2(ci)+Flux_D3(ci)
                endif

                if (flux_msn(ci) .ne. Flux_P(ci)+Flux_D1(ci)+Flux_D2(ci)+Flux_D3(ci)) then
                 write(*,*)'Warning :msn incomming flux .ne. to the sum of particles flux at depth ',ci
                endif


             ! ---Size modification due to Coagulation

if (Coag_coef .eq. 1.0) then  ! #1   Size modified only if coagulation happenned

             ! ---Initialize the size radius augmentation variable for the msn and the size for the coagulation
                cc(taille_coag,ci)  = 0.0
                cc(taille_intrm,ci) = 0.0
 
        !-----------------------------------------------------------------------------------
        !----------Definition of the size diameter augmentation of msn (m)
        !
        !Calculation of the cubic racin of X --» SIGN(ABS(X)**(1./3.),X) [FORTRAN, le langage normalisé, Michel Dubesset,Jean Vignes]
        !-----------------------------------------------------------------------------------

             ! ---Depends on what composed it
                cc(taille_coag,ci)= Flux_P(ci)*diam_phy+Flux_D1(ci)*diam_dph+Flux_D2(ci)*diam_dzo+Flux_D3(ci)*diam_fp ![m/s] 

        ! ---Definition of the diameter size of marine snow
        !-----------------------------------------------------------------------------------
             !At this stage 'taille_coag' is still the radius and diam_msn the equivalent spherical diameter
             !We defined L,I,S for Marine snow L=I=size_x*4 and S= size_X*2 
             !--» With size_X which represents the radius (cc(taille_coag,ci)) of our marine snow.
        !-----------------------------------------------------------------------------------
                 if (cc(taille_coag,ci) .ne. 0.0 .and. cc(taille_coag,ci).gt. 0.0) then !#2
                       diam_msn  = sign(abs(((cc(taille_coag,ci)*4.)*(cc(taille_coag,ci)*4.)*(cc(taille_coag,ci)*2.)))**(1./3.),&
                                   ((cc(taille_coag,ci)*4.)*(cc(taille_coag,ci)*4.)*(cc(taille_coag,ci)*2.))) ![m/s]
                 else !#2
                       diam_msn = 0.0
                 endif !#2

             ! ---The projected area equivalent diameter of our particles of marine snow 
             cc(taille_coag,ci) = (diam_msn**(CSF))*(dt_bio/splitfac_bio) ![m]

             !--» At this points 'taille_coag' is the projected area equivalent diameter in m.

             ! ---Elaboration of the size variable (diameter), available for the fragmentation eventually (taille_intrm).
                if(cc(d4,ci) .gt. cons_min) then
                 cc(taille_intrm,ci) = cc(taille_coag,ci) + cc(taille_msn,ci) ![m]
             !--» Msn size after coagulation will depend on the increase of size due to coagaluation (taille_coag) &
             !--» + the size at the previous time step (taille_msn) of the particles

        !-----------------------------------------------------------------------------------
        !!    ---Safety check
        !-----------------------------------------------------------------------------------
                      if(cc(taille_intrm,ci).lt. 0.0 .OR. &
                        cc(taille_coag,ci) .lt. 0.0 .OR. &
                        cc(taille_msn,ci) .lt. 0.0) then
                         write(*,*)'Taille _intrm - May be equal to 0 :', cc(taille_intrm,ci)
                         write(*,*)'Taille _coag - May be equal to 0 :', cc(taille_coag,ci)
                         write(*,*)'Taille _msn - May be equal to 0 :', cc(taille_msn,ci)
                     endif

               else
             !--» If there is not need to have a size augmentation due to low concentration of marine snow, the size of previous step remain
                  cc(taille_intrm,ci) = cc(taille_msn,ci) ![m]
             !   write(*,*)'No size augmentation as [msn] < cons_min - The size stays the same', cc(taille_msn,ci)
                endif

else !#1  Size not modified because no coagulation happenned
 cc(taille_coag,ci) = 0.0
 cc(taille_intrm,ci) = cc(taille_msn,ci) ![m]
 write(*,*)'Size not modified because no coagulation happenned'

endif !#1
 

        !-----------------------------------------------------------------------------------
        !!    ---Safety check
        !-----------------------------------------------------------------------------------
             !--» Msn size can not over pass the maximum diameter specified by the user.               
                   if(cc(taille_intrm,ci).gt.diam_msn_max) then
                      cc(taille_intrm,ci) = diam_msn_max
                      write(*,*)'Maximum size reach - Marine snow size equal :', cc(taille_intrm,ci)
                  endif

             !--» If there is no marine snow, there is no reason to set up a size, then the size will equal 0.
                   if (cc(d4,ci).lt. cons_min)then
                     cc(taille_msn,ci) = _ZERO_
                     cc(taille_intrm,ci) = _ZERO_
             !        write(*,*)'Size but no msn at :',ci
                  endif

!-----------------------------------------------------------------------------------
!              FRAGMENTATION
!               -----------
!!    ---Biological Fragmentation - Size reduction = Modification of particle type
!           -- Leackage in dissolved organic matter 
!           -- Bacterial remineralization 
!        Only in the euphotic zone where zooplankton are, following vertical migration:
!           -- Grazing (see before dd(d4,z,ci) = beta*gmax*r6*cc(d4,ci)**2*fac)).
!           -- By swimming zooplankton.
!!    ---Physical Fragmentation  [Alldredge 1990]
!-----------------------------------------------------------------------------------

if (Frag_meth .eq. 1.0) then !Fragmentation happens  

             ! ---Kolmogorov microscale of turbulence ! Alldredge 1990
                  kolmog = (kinvis**3/eps(ci))**0.25 ![m]
             ! ---Minimum size of what composed the marine snow - Be careful to be sure that their is this particles in the field
                  if (max_flux .eq. flux_P(ci)) then
                     min_size_msn = Ratio_P*diam_phy !size_phy
                  elseif (max_flux .eq. flux_D1(ci)) then
                     min_size_msn = Ratio_D1*diam_dph !size_dph
                  elseif (max_flux .eq. flux_D2(ci)) then
                     min_size_msn = Ratio_D2*diam_dzo !size_dzo
                  elseif (max_flux .eq. flux_D3(ci)) then
                     min_size_msn = Ratio_D3* diam_fp ! size_fp
                  else
                     write(*,*)'Error for estimation of min_size_msn'
                  endif

        !-----------------------------------------------------------------------------------
        !!    ---Biological Fragmentation rate
        !-----------------------------------------------------------------------------------
             !         -- Leackage in dissolved organic matter  -[mmolN/m3/d]
                  dd(d4,l,ci) = leak*cc(d4,ci) 
                  leackage = dd(d4,l,ci)
             !           -- Bacterial remineralization [Ploug 1999 - Ploug 2000 - Kiorboe 2001]-[mmolN/m3/d]
                  dd(d4,b,ci)=remi*cc(d4,ci)*cc(b,ci)
                  ! dd(d4,b,ci) = (0.61*cc(taille_msn,ci)**(-1.50))  
                  ! Do not work Yet :Ploug 1999 pour que le taux de remineralisation depende de la taille 
                  remineralization = dd(d4,b,ci)
             !           -- By swimming zooplankton ! Goldwaith 2005  [mmolN/m3/d] ?
                  Frag_bio=(swim_brk*cc(z,ci)*cc(d4,ci))
 

        !-----------------------------------------------------------------------------------
        !!    ---Physical Fragmentation rate
        !-----------------------------------------------------------------------------------
             !--» We decide if we have energy dissipation rates able to fragment our msn (Alldredge 1990)

                  if (cc(taille_intrm,ci) .ge. diam_msn_max .OR. eps(ci) .ge. 0.0001 ) then
             ! ---Value for the stable_size parameter(depending on the diameter)
                                    if (cc(taille_intrm,ci) .gt. kolmog) then 
                                       stable_size = 1
                                    else
                                      stable_size = 0.5
                                    endif
                     Frag_phys = Floc_coef*(sqrt(eps(ci)/kinvis)**(stable_size))*cc(d4,ci) ![Spicer et al. 1996]
                     write(*,*)'Physical fragmentation happens'
                  else
                     Frag_phys= 0.0
                     write(*,*)'No Physical fragmentation'
                  end if

        !-----------------------------------------------------------------------------------
        !!    ---FLUXES : ---Calculation of the flux of msn that will return 
        !!    ---when fragmentation happen to the "initial boxes" : dph, dzo, fp 
        !-----------------------------------------------------------------------------------
             !--» The return can happen only if the fragmentation rate is different of 0 otherwise &
             !--» NaN values will be attributed to the dd(d4,x,ci) flux.

                  if (Frag_phys+Frag_bio+leackage+remineralization .gt. flux_msn(ci) .OR. &
                      cc(taille_intrm,ci) .ge. diam_msn_max .OR. eps(ci) .ge. 0.0001 ) then
                     dd(d4,d1,ci)=  Frag_phys+Frag_bio+leackage+remineralization 
                     dd(d4,d2,ci)=  Frag_phys+Frag_bio+leackage+remineralization
                     dd(d4,d3,ci)=  Frag_phys+Frag_bio+leackage+remineralization
                     write(*,*) 'Return from msn to initial boxes due to :'
                         if(Frag_phys+Frag_bio+leackage+remineralization .gt. flux_msn(ci)) then
                            write(*,*) 'Frag_phys+Frag_bio+leackage+remineralization > Flux to msn'
                         endif
                         if(cc(taille_intrm,ci) .gt. diam_msn_max) then
                            write(*,*) 'cc(taille_intrm,ci) > diam_msn_max'
                         endif
                         if(eps(ci) .ge. 0.0001) then
                            write(*,*) 'eps(ci) >= 0.0001'
                         endif
                  else
                     write(*,*) 'Loss by fragmentation equal or lower than 0'
                     dd(d4,d1,ci)= 0.0
                     dd(d4,d2,ci)= 0.0
                     dd(d4,d3,ci)= 0.0
                  endif

        !-----------------------------------------------------------------------------------
        !!    ---SIZE : ---Elaboration of the size variable, available for the next timestep (the radius will be modified)
        !-----------------------------------------------------------------------------------
                  if (Frag_phys+Frag_bio+leackage+remineralization .gt. flux_msn(ci) .OR.&
                     cc(taille_intrm,ci) .ge. diam_msn_max .OR. eps(ci) .ge. 0.0001 ) then 

             !--» Choice for the Size reduction scheme
                                    if (RFV .eq. 1.0) then ! DOES NOT WORK !
             !--» Utilisation of the Relative fragment volume to estimate in how many fragment the marine snow will break on. (Alldredge 1990)
                                      RFV_msn = coef3*diam_msn_max**(coef4) 
             !--» Though the utilisation of diam_msn_max we have the influence on eps on the daugther particles formation 
                                      size_msn_m = (cc(taille_intrm,ci)/RFV_msn)

                                    elseif (RFV .eq. 2.0) then ! Utilisation of probability of break fragment
                                      call random_number(r_msn)
                                      prob_break_msn=r_msn
                                             if (r_msn .lt. 0.7) then ! (70% chance --» in 2 parts) (Alldredge 1990 - Dilling 2000)
                                                 size_msn_m = cc(taille_intrm,ci)/2
                                             elseif (r_msn .lt. 0.15) then ! (15% chance --» in 3 parts)
                                                 size_msn_m = cc(taille_intrm,ci)/3
                                             elseif (r_msn .lt. 0.115) then ! (11.5% chance --» in 10 parts)
                                                 size_msn_m = cc(taille_intrm,ci)/10
                                             else
                                                 size_msn_m = cc(taille_intrm,ci)
                                             endif
                                    else
                                      size_msn_m = cc(taille_intrm,ci)/3 !Average of (7*2 (70%), 2*3 (20%), 1*10 (11.5%))
                                    endif 

        !-----------------------------------------------------------------------------------
        !!    ---Safety check
        !-----------------------------------------------------------------------------------

             !--» If there is no marine snow, there is no reason to set up a size, then the size will equal 0.
                                                      if (cc(d4,ci).lt. cons_min)then
                                                         write(*,*)'Size but no msn'
                                                         size_msn_m = 0.0
                                                      endif

        !-----------------------------------------------------------------------------------
        !!    ---New values of size if fragmentation occurs
        !-----------------------------------------------------------------------------------
                    cc(taille_frag,ci) = size_msn_m ! Keep in memory for the next step
                    cc(taille_msn,ci) = cc(taille_frag,ci) 

                  else  ! no size modification
                    write(*,*)'No size modification due to fragmentation'
                    cc(taille_frag,ci) = 0.0
                    cc(taille_msn,ci) = cc(taille_intrm,ci)
                  endif 

elseif (Frag_meth .eq. 0.0) then   !No Fragmentation
        !-----------------------------------------------------------------------------------
        !!    --- FLUXES
        !-----------------------------------------------------------------------------------
                  dd(d4,l,ci)= 0.0
                  dd(d4,b,ci)= 0.0
                  dd(d4,d1,ci)= 0.0
                  dd(d4,d2,ci)= 0.0
                  dd(d4,d3,ci)= 0.0
        !-----------------------------------------------------------------------------------
        !!    --- SIZE - no size modification
        !-----------------------------------------------------------------------------------
                  !  write(*,*)'No size modification due to fragmentation'
                    cc(taille_frag,ci) = 0.0
                    cc(taille_msn,ci)= cc(taille_intrm,ci)
else 
                  write(*,*)'Wrong specification for fragmentation'
endif


!-------------------------------------------------------------------------------------
!   SEDIMENTATION rate msn
!     We want to use a calculated settling velocity for the msn
!     Settling velocity send to the advection scheme as well as the size of the msn
!-------------------------------------------------------------------------------------

             !--» Taille_msn --> is the diameter after coag and frag 
       Rmsn = (rho_msn-densFlu)

if (cc(d4,ci) .le. cons_min) then
      w_msn_m = 0.0
  !   write(*,*)'[msn] lt Cons_min, settling velocity set to :',w_msn_m

elseif (cc(d4,ci) .lt. cons_max) then   !#1 

             !--» We set here the concentration limit in the water column from free settling of marine snow vs flocculation settling (Metha 1989)

        !!    --- Calculated by value from the .nml
                  if (w_msnow .eq. 0.0 ) then !#2
                    !w_msn_m  = w_msn*sqrt(w_depth/100)
                    w_msn_m  = w_msn

        !!    --- Stokes's law from Ghosh et al 2013
            !--» msn is considered as sphere                    
                  else if (w_msnow .eq. 1.0) then !#2
                    w_msn_m= (g*((cc(taille_msn,ci))**2)*Rmsn)/(18*dynvis) 

        !!    --- Modified Stoke's law 
            !--» considered as cylindrical and will follow a specific Stoke's law [w=((0.079*(r_X-r_F)*L^2*g)/dynvis)*(L/D)^-1.664].
                  else if (w_msnow .eq. 2.0) then !#2
                    w_msn_m= (0.079*Rmsn*(cc(taille_msn,ci)+(1./2.))*g)/dynvis*((cc(taille_msn,ci)+(1./2.))/&
                             (cc(taille_msn,ci)))**(-1.664) 

        !!    --- Power law evolution from Alldredge and Gotschalk, 1988 based on Kajihara 1971 and from Komar 1981
            !--» Calculation of the settling velocity (m/j)/secs_pr_day = m/s - utilisation here of the diameters 
                  else if (w_msnow .eq. 3.0) then!#2

                                    if (cc(taille_msn,ci).gt. 0.001) then  !#2.1
                                       w_msn_m =-(50*((cc(taille_msn,ci))**0.26))
                                    else if (cc(taille_msn,ci) .lt. 0.001 .and. cc(taille_msn,ci) .gt. 0.0) then !#2.1
                                       w_msn_m =-(160*((cc(taille_msn,ci))**0.57)) 
                                    else !#2.1
                                       w_msn_m = 0.0
                                    endif !#2.1
        !!    --- Stokes law for high reynolds number + Corey Shape Factor [Komar 1978]
                  else if (w_msnow  .eq. 4.0) then !#2

                                    if(CSF .ge. 0.0 .and. CSF.lt. 0.4) then !#2.3
                                              CSF_msn = 2.18-(2.09*CSF)
                                    else if (CSF .ge. 0.4 .and. CSF .lt. 0.8) then!#2.3
                                              CSF_msn = 0.946*(CSF)**(-0.378)
                                    else if (CSF .ge. 0.8 .and. CSF .le. 1.0) then!#2.3
                                              CSF_msn = 1.0 ! consider as a sphere
                                     else !#2.3
                                             write (*,*) 'Error in CSF values for Marine snow : CSF .NE. [0-1]'
                                    endif !#2.3

                    w_msn_m=(1/(18*(kinvis*densFlu)))*(1/CSF_msn)*(Rmsn)*g*(cc(taille_msn,ci))**2 ![m/s]

                  else !#2! if (w_msnow .ne. 0.0 .and. w_msnow .ne. 1.0 .and. w_msnow .ne. 2.0 .and. w_msnow .ne. 3.0 .and. w_msnow .ne. 4.0) then
                    write(*,*) 'No settling velocity choice for msn, value set to 0'
                    w_msn_m = 0.0
                  endif!#2
elseif (cc(d4,ci) .ge. cons_max)then !#1
     w_msn_m=-(coef5*cc(d4,ci)**(1.6)) ! Metha 1989
!     write(*,*)'[msn] > cons_max at :',ci

else
  write(*,*)'At Depth : ',ci
  write(*,*)'Error with the specification of [msn] at',cc(d4,ci)
 w_msn_m = 0.0

endif !#1

        !-----------------------------------------------------------------------------------
        !!    --- Convertion & Comparaison with the Stokes range
        !-----------------------------------------------------------------------------------

            !--» Comparaison with the Stokes range ! VanRijn 1993
     Re_msn = abs(w_msn_m*(cc(taille_msn,ci)/kinvis))
 
if (Re_msn .lt. 1.0)then !#3
     w_msn_m = w_msn_m
else !#3
     write(*,*) 'Reynolds number for marine snow > 1'
 
     if(cc(taille_msn,ci) .gt. 0.000001 .and. cc(taille_msn,ci) .le. 0.0001 )then
! If the diameter of Msn (d) -->  1< d <= 100 micrometers (VanRijn 1993)
      w_msn_m = ((2.65-1)*g*(cc(taille_msn,ci)**2))/18*kinvis
 write(*,*) 'Diameter of msn :  1< d <= 100 micrometers, settling =' ,w_msn_m 

     elseif(cc(taille_msn,ci) .gt. 0.0001  .and. cc(taille_msn,ci) .lt. 0.001 ) then
! If the diameter of Msn (d) -->  100< d <= 1000 micrometers (VanRijn 1993)
      w_msn_m = ((10*kinvis)/cc(taille_msn,ci))*&
  ((1+((0.01*(2.65-1)*g*(cc(taille_msn,ci))**3.0) /kinvis**2.0)**0.5) -1)
 write(*,*) 'Diameter of msn :  100< d <= 1000 micrometers, settling =' ,w_msn_m 

     elseif(cc(taille_msn,ci) .gt. 0.001) then
! If the diameter of Msn (d) --> d > 1000 micrometers (VanRijn 1993)
      w_msn_m = 1.1*((2.65-1)*g*cc(taille_msn,ci))**0.5
   write(*,*) 'Diameter of msn : d > 1000 micrometers, settling =' ,w_msn_m 

     else
     write(*,*) 'Diameter of msn lower than 1 micrometers, and RE > 1 : Houston we have a problem ! '
     w_msn_m = 0.0
    endif

endif !#3


!-----------------------------------------------------------------------------------
!      ADVECTION (WS) OF THE PARTICLES
!-----------------------------------------------------------------------------------

        !-----------------------------------------------------------------------------------
        !!    ---Safety check
        !-----------------------------------------------------------------------------------
            !--» Does density difference with the fluid allow to go up ?

            if (rho_p .gt. densFlu .AND. w_p_m .gt. 0.0) then
            w_p_m = -w_p_m
            endif

            if (rho_dph .gt. densFlu .AND. w_dph_m .gt. 0.0) then
            w_dph_m = -w_dph_m
            endif

            if (rho_dzo .gt. densFlu .AND. w_dzo_m .gt. 0.0) then
            w_dzo_m = -w_dzo_m
            endif

            if (rho_fp .gt. densFlu .AND. w_fp_m .gt. 0.0) then
            w_fp_m = -w_fp_m
            endif

            if (rho_msn .gt. densFlu .AND. w_msn_m .gt. 0.0) then
            w_msn_m = -w_msn_m
            endif

            !--» Definition of the maximum diameter size from our model calculation
 

If (litt_msn_w .lt. max_w_msn) then
max_w_msn = litt_msn_w
else
max_w_msn = max_w_msn 
endif

            !--» Does CFL limit respected ?
 CFL = (depth_bio/nlev)/dt_bio

if(abs(w_msn_m) .gt. CFL) then
max_w_msn = max_w_msn 
else
    if(w_msn_m .lt. max_w_msn) then
    max_w_msn = w_msn_m
    else
     max_w_msn = max_w_msn 
    endif
endif

            !--» let them settling(m/s)
            !--» The ncdf output(bio_save) do ws(x,ci) *86400 to have  the ouptut in m/j.

        !-----------------------------------------------------------------------------------
        !!    ---Safety check
        !-----------------------------------------------------------------------------------

            !--» Does CFL limit respected ?

            !--» Case of Phytoplankton 
if (abs(w_p_m) .gt. (depth_bio/nlev)/dt_bio )then
 ws(p,ci)  = w_p
 write(*,*) 'CFL Constrain is OVERPASS for W-Phy, ws(P,ci) = nml value', ws(p,ci)
    if  (abs(w_p) .gt. (depth_bio/nlev)/dt_bio )then
      ws(p,ci)  = 0.0
      write(*,*) 'CFL Constrain is OVERPASS for W-Phy,from the nml value', ws(p,ci)
    endif
else
 ws(p,ci)  = w_p_m
endif

            !--» Case of Dead_Phytoplankton 
if (abs(w_dph_m) .gt. (depth_bio/nlev)/dt_bio )then
 ws(d1,ci)  = w_dph
 write(*,*) 'CFL Constrain is OVERPASS for W-Dph, ws(D1,ci) = nml value', ws(d1,ci)
    if  (abs(w_dph) .gt. (depth_bio/nlev)/dt_bio )then
      ws(d1,ci)  = 0.0
      write(*,*) 'CFL Constrain is OVERPASS for W-Dph,from the nml value', ws(d1,ci)
    endif
else
 ws(d1,ci)  = w_dph_m
endif

           !--» Case of Dead Zooplankton
if (abs(w_dzo_m) .gt. (depth_bio/nlev)/dt_bio )then
 ws(d2,ci)  = w_dzo
 write(*,*) 'CFL Constrain is OVERPASS for W-dzo, ws(d2,ci) = nml value', ws(d2,ci)
    if  (abs(w_dzo) .gt. (depth_bio/nlev)/dt_bio )then
      ws(d2,ci)  = 0.0
      write(*,*) 'CFL Constrain is OVERPASS for W-dzo,from the nml value', ws(d2,ci)
    endif
else
 ws(d2,ci)  = w_dzo_m
endif

            !--» Case of Fecal pellets 
if (abs(w_fp_m) .gt. (depth_bio/nlev)/dt_bio )then
 ws(d3,ci)  = w_fp
 write(*,*) 'CFL Constrain is OVERPASS for W-fp, ws(d3,ci) = nml value', ws(d3,ci)
    if  (abs(w_fp) .gt. (depth_bio/nlev)/dt_bio )then
      ws(d3,ci)  = 0.0
      write(*,*) 'CFL Constrain is OVERPASS for W-fp,from the nml value', ws(d3,ci)
    endif
else
 ws(d3,ci)  = w_fp_m
endif

            !--» Case of marine snow 
 !--Concentration - Vs CFL limit 
if (abs(w_msn_m) .gt. (depth_bio/nlev)/dt_bio )then

! -- As settling velocity overpass the CFL constrain we will attribute to the msn 
!    the maximum settling velocity found between our simulation and the litterature
  ! ws(d4,ci) = litt_msn_w  ![m/s]
    ws(d4,ci) = max_w_msn  ![m/s]
  write(*,*) 'CFL Constrain is OVERPASS, w_msn use from nml',ws(d4,ci)
    if  (abs(litt_msn_w) .gt. (depth_bio/nlev)/dt_bio )then
      ws(d4,ci)  = 0.0
      write(*,*) 'CFL Constrain is OVERPASS for w_msn,from the nml value', ws(d4,ci)
    endif
             
else 
ws(d4,ci) = w_msn_m
 write(*,*) 'CFL limit is respected, ws(D4,ci) = ',ws(d4,ci)
endif

!--Size - Vs CFL limit 
 ws(taille_msn,ci) =  ws(d4,ci)

!--Settling velocity trait - Vs CFL limit 
 
 cc(settl_msn,ci) = ws(d4,ci)
 ws(settl_msn,ci) =  ws(d4,ci)

 
!-----------------------------------------------------------------------------------
!      NET PRIMARY PRODUCTION
!-----------------------------------------------------------------------------------
 !orig ppnet(ci) =(dd(a,p,ci)+dd(n,p,ci)-dd(p,d,ci)-dd(p,z,ci)-dd(p,l,ci))*secs_pr_day
       ppnet(ci) =(dd(a,p,ci)+dd(n,p,ci)-dd(p,d1,ci)-dd(p,z,ci)-dd(p,l,ci)-dd(p,d4,ci))*secs_pr_day
!-----------------------------------------------------------------------



!-------------------------------------------------------------------------------------
!    INFORMATIONS BOX
!-------------------------------------------------------------------------------------
if (write_screen.eq. 1.0) then   !#A  

!  Size variation for each particle type
 if (size_rand .eq. 1.0) then
     write(*,*) 'Random size'
 else
   write(*,*) 'Constant size'
 endif
        write(*,*) 'size_phy:', size_phy
        write(*,*) 'size_dph:', size_dph
        write(*,*) 'size_dzo:', size_dzo
        write(*,*) 'size_fp:', size_fp
!Settling velocity
      write(*,*) '----Sedimentation rate-----------------'
      write(*,*) 'densFlu:', densFlu
      write(*,*) ' Value for: w_p',w_p_m
      write(*,*) ' Value for: w_dph',w_dph_m
      write(*,*) ' Value for: w_dzo',w_dzo_m
      write(*,*) ' Value for: w_fp',w_fp_m
      write(*,*) ' Value for: w_msn',w_msn_m
 
if (cc(l,ci) .gt. lmin) then       !#B
write(*,*) '----Aggregation-----------------'
write(*,*) '-------Maximum loss for aggregation-----------------'
write(*,*) ' pprime:',  pprime
write(*,*) ' d1rime:',  d1rime
write(*,*) ' d2rime:',  d2rime
write(*,*) ' d3rime:',  d3rime

!!   Stickiness for each particle type interaction
if(sti_cst .eq. 1.0) then  !#1.2
     write(*,*) '----Cst Stickiness-----------------' 
else
      write(*,*) '----Variable Stickiness-----------------'
endif!#1.2
     write(*,*) ' Value for stip_p', sti_2p(ci)   
     write(*,*) ' Value for stip_dph', sti_pdph(ci)
     write(*,*) ' Value for stip_dzo',  sti_pdzo(ci)
     write(*,*) ' Value for stip_fp',  sti_pfp(ci)
     write(*,*) ' Value for stidph_dph',  sti_2dph(ci)
     write(*,*) ' Value for stidph_dzo',  sti_dphdzo(ci)
     write(*,*) ' Value for stidph_fp',  sti_dphfp(ci)
     write(*,*) ' Value for stidzo_dzo',  sti_2dzo(ci)
     write(*,*) ' Value for stidzo_fp',  sti_dzofp(ci)
     write(*,*) ' Value for stifp_fp',  sti_2fp(ci)


if (Coag_coef .eq. 0.0) then !#2
   if(betaBr .eq. 0.0 .and. betaSh .eq. 0.0 .and. betaDs .eq. 0.0) then  ! #3
    write(*,*) '----Sti+Beta constants-----------------'
         write(*,*) 'txloss_p*pprime:', txloss_p*pprime
         write(*,*) 'coli_pp_NoPhys:', coli_pp_NoPhys
         write(*,*)'agg_pp:', agg_pp
         write(*,*) 'coli_pdph_NoPhys:', coli_pdph_NoPhys
         write(*,*)'agg_pdph:', agg_pdph
         write(*,*) 'coli_pdzo_NoPhys:',coli_pdzo_NoPhys
         write(*,*)'agg_pdzo:', agg_pdzo
         write(*,*) 'coli_pfp_NoPhys:', coli_pfp_NoPhys
         write(*,*)'agg_pfp:', agg_pfp
    write(*,*) 'P to d4:', (agg_pp)+(agg_pdph)+(agg_pdzo)+(agg_pfp)
    write(*,*) '-----------------------------'
         write(*,*) 'txloss_dph*d1rime:', txloss_dph*d1rime
         write(*,*) 'coli_dphdph_NoPhys:', coli_dphdph_NoPhys
         write(*,*)'agg_dphdph:', agg_dphdph
         write(*,*) 'coli_dphdzo_NoPhys:', coli_dphdzo_NoPhys
         write(*,*)'agg_dphdzo:', agg_dphdzo
         write(*,*) 'coli_dphfp_NoPhys:',  coli_dphfp_NoPhys
         write(*,*)'agg_dphfp:', agg_dphfp
    write(*,*) 'D1 to d4:', (agg_pdph)+(agg_dphdph)+(agg_dphdzo)+(agg_dphfp)
    write(*,*) '-----------------------------'
         write(*,*) 'txloss_dzo*d2rime:', txloss_dzo*d2rime
         write(*,*) 'coli_dzodzo_NoPhys:', coli_dzodzo_NoPhys
         write(*,*)'agg_dzodzo:', agg_dzodzo
         write(*,*) 'coli_dzofp_NoPhys:',coli_dzofp_NoPhys
         write(*,*)'agg_dzofp:', agg_dzofp
    write(*,*) 'D2 to d4:', (agg_pdzo)+(agg_dphdzo)+(agg_dzodzo)+(agg_dzofp)
    write(*,*) '-----------------------------'
         write(*,*) 'txloss_fp*d3rime:', txloss_fp*d3rime
         write(*,*) 'coli_fpfp_NoPhys:', coli_fpfp_NoPhys
         write(*,*)'agg_fpfp:', agg_fpfp
    write(*,*) 'D3 to d4:', (agg_pfp)+(agg_dphfp)+(agg_dzofp)+(agg_fpfp)
   endif !#3

   if(betaBr .eq. 1.0 .and. betaSh .eq. 0.0 .and. betaDs .eq. 0.0) then !#4
    write(*,*) '----Run with only BR-----------------'
         write(*,*) 'Temp:',T_degK
         write(*,*) 'physic_Br:',physic_Br     
    write(*,*) '----*****-----------------'
         write(*,*) 'txloss_p*pprime:', txloss_p*pprime
         write(*,*) 'coli_pp:', coli_pp
         write(*,*)'agg_pp:', agg_pp
         write(*,*) 'coli_pdph:', coli_pdph
         write(*,*)'agg_pdph:', agg_pdph
         write(*,*) 'coli_pdzo:',coli_pdzo
         write(*,*)'agg_pdzo:', agg_pdzo
         write(*,*) 'coli_pfp:', coli_pfp
         write(*,*)'agg_pfp:', agg_pfp
    write(*,*) 'P to d4:', (agg_pp)+(agg_pdph)+(agg_pdzo)+(agg_pfp)
    write(*,*) '-----------------------------'
         write(*,*) 'txloss_dph*d1rime:', txloss_dph*d1rime
         write(*,*) 'coli_dphdph:', coli_dphdph
         write(*,*)'agg_dphdph:', agg_dphdph
         write(*,*) 'coli_dphdzo:', coli_dphdzo
         write(*,*)'agg_dphdzo:', agg_dphdzo
         write(*,*) 'coli_dphfp:',  coli_dphfp
         write(*,*)'agg_dphfp:', agg_dphfp
    write(*,*) 'D1 to d4:', (agg_pdph)+(agg_dphdph)+(agg_dphdzo)+(agg_dphfp)
    write(*,*) '-----------------------------'
         write(*,*) 'txloss_dzo*d2rime:', txloss_dzo*d2rime
         write(*,*) 'coli_dzodzo:', coli_dzodzo
         write(*,*)'agg_dzodzo:', agg_dzodzo
         write(*,*) 'coli_dzofp:',coli_dzofp
         write(*,*)'agg_dzofp:', agg_dzofp
    write(*,*) 'D2 to d4:', (agg_pdzo)+(agg_dphdzo)+(agg_dzodzo)+(agg_dzofp)
    write(*,*) '-----------------------------'
         write(*,*) 'txloss_fp*d3rime:', txloss_fp*d3rime
         write(*,*) 'coli_fpfp:', coli_fpfp
         write(*,*)'agg_fpfp:', agg_fpfp
    write(*,*) 'D3 to d4:', (agg_pfp)+(agg_dphfp)+(agg_dzofp)+(agg_fpfp)
   endif !#4

   if (betaBr .eq. 0.0 .and. betaSh .eq. 1.0 .and. betaDs .eq. 0.0) then  ! #5
      write(*,*) '----Run with only BSh-----------------'   
         write(*,*) 'txloss_p*pprime:', txloss_p*pprime
         write(*,*) 'coli_pp:', coli_pp
         write(*,*)'agg_pp:', agg_pp
         write(*,*) 'coli_pdph:', coli_pdph
         write(*,*)'agg_pdph:', agg_pdph
         write(*,*) 'coli_pdzo:',coli_pdzo
         write(*,*)'agg_pdzo:', agg_pdzo
         write(*,*) 'coli_pfp:', coli_pfp
         write(*,*)'agg_pfp:', agg_pfp
    write(*,*) 'P to d4:', (agg_pp)+(agg_pdph)+(agg_pdzo)+(agg_pfp)
    write(*,*) '-----------------------------'
         write(*,*) 'txloss_dph*d1rime:', txloss_dph*d1rime
         write(*,*) 'coli_dphdph:', coli_dphdph
         write(*,*)'agg_dphdph:', agg_dphdph
         write(*,*) 'coli_dphdzo:', coli_dphdzo
         write(*,*)'agg_dphdzo:', agg_dphdzo
         write(*,*) 'coli_dphfp:',  coli_dphfp
         write(*,*)'agg_dphfp:', agg_dphfp
    write(*,*) 'D1 to d4:', (agg_pdph)+(agg_dphdph)+(agg_dphdzo)+(agg_dphfp)
    write(*,*) '-----------------------------'
         write(*,*) 'txloss_dzo*d2rime:', txloss_dzo*d2rime
         write(*,*) 'coli_dzodzo:', coli_dzodzo
         write(*,*)'agg_dzodzo:', agg_dzodzo
         write(*,*) 'coli_dzofp:',coli_dzofp
         write(*,*)'agg_dzofp:', agg_dzofp
    write(*,*) 'D2 to d4:', (agg_pdzo)+(agg_dphdzo)+(agg_dzodzo)+(agg_dzofp)
    write(*,*) '-----------------------------'
         write(*,*) 'txloss_fp*d3rime:', txloss_fp*d3rime
         write(*,*) 'coli_fpfp:', coli_fpfp
         write(*,*)'agg_fpfp:', agg_fpfp
    write(*,*) 'D3 to d4:', (agg_pfp)+(agg_dphfp)+(agg_dzofp)+(agg_fpfp)
   endif !#5
 
   if (betaBr .eq. 0.0 .and. betaSh .eq. 0.0 .and. betaDs .eq. 1.0) then ! #6
    write(*,*) '----Run with only BDs-----------------'
         write(*,*) 'calculated size phy:', size_phy
         write(*,*) 'calculated size dph:', size_dph
         write(*,*) 'calculated density of p:', rho_p
         write(*,*) 'calculated density of dph:', rho_dph
         write(*,*) 'Settling phy:', w_p_m
         write(*,*) 'Settling dph:', w_dph_m
         write(*,*) 'Settling dzo:', w_dzo_m
         write(*,*) 'Settling fp:', w_fp_m
    write(*,*) '-----------------------------'
         write(*,*) 'txloss_p*pprime:', txloss_p*pprime
         write(*,*) 'Abs (w_p-w_dph):', abs_pdph
         write(*,*) 'betaDs_pdph:',betaDs_pdph
         write(*,*) 'coli_pdph:', coli_pdph
         write(*,*)'agg_pdph:', agg_pdph
         write(*,*) 'Abs (w_p-w_dzo):', abs_pdzo
         write(*,*) 'coli_pdzo:',coli_pdzo
         write(*,*)'agg_pdzo:', agg_pdzo
         write(*,*) 'Abs (w_p-w_fp):', abs_pfp
         write(*,*) 'coli_pfp:', coli_pfp
         write(*,*)'agg_pfp:', agg_pfp
    write(*,*) 'P to d4:', (agg_pdph)+(agg_pdzo)+(agg_pfp)
    write(*,*) '-----------------------------'
         write(*,*) 'txloss_dph*d1rime:', txloss_dph*d1rime
         write(*,*) 'Abs (w_dph-w_dzo):', abs_dphdzo
         write(*,*) 'coli_dphdzo:', coli_dphdzo
         write(*,*)'agg_dphdzo:', agg_dphdzo
         write(*,*) 'Abs (w_dph-w_fp):', abs_dphfp
         write(*,*) 'coli_dphfp:',  coli_dphfp
         write(*,*)'agg_dphfp:', agg_dphfp
    write(*,*) 'D1 to d4:', (agg_pdph)+(agg_dphdzo)+(agg_dphfp)
    write(*,*) '-----------------------------'
         write(*,*) 'txloss_dzo*d2rime:', txloss_dzo*d2rime
         write(*,*) 'Abs (w_dzo-w_fp):', abs_dzofp
         write(*,*) 'coli_dzofp:',coli_dzofp
         write(*,*)'agg_dzofp:', agg_dzofp
    write(*,*) 'D2 to d4:', (agg_pdzo)+(agg_dphdzo)+(agg_dzofp)
    write(*,*) '-----------------------------'
         write(*,*) 'txloss_fp*d3rime:', txloss_fp*d3rime
    write(*,*) 'D3 to d4:', (agg_pfp)+(agg_dphfp)+(agg_dzofp)
  endif !#6

else !#2
        write(*,*) 'Temp:',T_degK
         write(*,*) 'physic_Br:',physic_Br  
         write(*,*) 'eps:', bio_eps     
    write(*,*) '----*****-----------------'
         write(*,*) 'txloss_p*pprime:', txloss_p*pprime
         write(*,*) 'coli_pp:', coli_pp
         write(*,*)'agg_pp:', agg_pp
         write(*,*) 'coli_pdph:', coli_pdph
         write(*,*)'agg_pdph:', agg_pdph
         write(*,*) 'coli_pdzo:',coli_pdzo
         write(*,*)'agg_pdzo:', agg_pdzo
         write(*,*) 'coli_pfp:', coli_pfp
         write(*,*)'agg_pfp:', agg_pfp
    write(*,*) 'P to d4:', (agg_pp)+(agg_pdph)+(agg_pdzo)+(agg_pfp)
    write(*,*) '-----------------------------'
         write(*,*) 'txloss_dph*d1rime:', txloss_dph*d1rime
         write(*,*) 'coli_dphdph:', coli_dphdph
         write(*,*)'agg_dphdph:', agg_dphdph
         write(*,*) 'coli_dphdzo:', coli_dphdzo
         write(*,*)'agg_dphdzo:', agg_dphdzo
         write(*,*) 'coli_dphfp:',  coli_dphfp
         write(*,*)'agg_dphfp:', agg_dphfp
    write(*,*) 'D1 to d4:', (agg_pdph)+(agg_dphdph)+(agg_dphdzo)+(agg_dphfp)
    write(*,*) '-----------------------------'
         write(*,*) 'txloss_dzo*d2rime:', txloss_dzo*d2rime
         write(*,*) 'coli_dzodzo:', coli_dzodzo
         write(*,*)'agg_dzodzo:', agg_dzodzo
         write(*,*) 'coli_dzofp:',coli_dzofp
         write(*,*)'agg_dzofp:', agg_dzofp
    write(*,*) 'D2 to d4:', (agg_pdzo)+(agg_dphdzo)+(agg_dzodzo)+(agg_dzofp)
    write(*,*) '-----------------------------'
         write(*,*) 'txloss_fp*d3rime:', txloss_fp*d3rime
         write(*,*) 'coli_fpfp:', coli_fpfp
         write(*,*)'agg_fpfp:', agg_fpfp
    write(*,*) 'D3 to d4:', (agg_pfp)+(agg_dphfp)+(agg_dzofp)+(agg_fpfp)
 write(*,*) '-------------------------'
   write(*,*)'kernel_coef_pp:', kernel_coef_pp
   write(*,*)'kernel_coef_pdph:', kernel_coef_pdph
   write(*,*)'kernel_coef_pdzo:', kernel_coef_pdzo
   write(*,*)'kernel_coef_pfp:', kernel_coef_pfp
   write(*,*)'kernel_coef_dphdph:', kernel_coef_dphdph
   write(*,*)'kernel_coef_dphdzo:', kernel_coef_dphdzo
   write(*,*)'kernel_coef_dphfp:', kernel_coef_dphfp
   write(*,*)'kernel_coef_dzodzo:', kernel_coef_dzodzo
   write(*,*)'kernel_coef_dzofp:', kernel_coef_dzofp
   write(*,*)'kernel_coef_fpfp:', kernel_coef_fpfp

    write(*,*) '-------------------------'
    write(*,*) '-------------------------'
endif !#2

!             SIZE Marine Snow with aggregation
  write(*,*) '----SIZE Marine Snow ----------------'
  write(*,*)'Flux_P', Flux_P(ci)
  write(*,*)'Flux_D1', Flux_D1(ci)
  write(*,*)'Flux_D2', Flux_D2(ci)
  write(*,*)'Flux_D3', Flux_D3(ci)
  write(*,*) 'flux_msn',flux_msn(ci)
  write(*,*) 'Ratio_P:', Ratio_P
  write(*,*) 'Ratio_D1:', Ratio_D1
  write(*,*) 'Ratio_D2:', Ratio_D2
  write(*,*) 'Ratio_D3:', Ratio_D3
 write(*,*) 'News size of msn :', size_msn_m
 ! write(*,*) ' Size increase:',  cc(aug_si_d4,ci)
  write(*,*) ' Size msn:',  cc(taille_msn,ci)

!FRAGMENTATION
if (Frag_meth .eq. 1.0) then  !#1
write(*,*) '----Fragmentation ----------------'
write(*,*) 'News size of msn :', size_msn_m
write(*,*)'kolmog', kolmog
write(*,*)'Frag_bio', Frag_bio
write(*,*) 'Frag_phys', Frag_phys
write(*,*) 'dd(d4,d1,ci)', dd(d4,d1,ci)
write(*,*) 'dd(d4,d2,ci)', dd(d4,d2,ci)
write(*,*) 'dd(d4,d3,ci)', dd(d4,d3,ci)
write(*,*) 'dd(d4,l,ci)', dd(d4,l,ci)
write(*,*) 'dd(d4,b,ci)', dd(d4,b,ci)
endif

endif !#B
!   Sedimentation rate msn
 write(*,*) 'Calculate w_msn :', w_msn_m
!      NET PRIMARY PRODUCTION
 write(*,*) 'ppnet(ci) :', ppnet(ci)


write(*,*) '--News Depth or timesteps--'

endif !#A
!-----------------------------------------------------------------------


      do i=1,numc
         do j=1,numc
            pp(i,j,ci)=dd(j,i,ci)
         end do
      end do
  end do

!-----------------------------------------------------------------------
! If you want to do something over this timestep do it here !
!-----------------------------------------------------------------------


write(*,*)'-------------------------NEW TIMESTEP-----------------------'

!-----------------------------------------------------------------------

   return
   end subroutine do_bio_polynow
!EOC

!-----------------------------------------------------------------------
!BOP
!
! !IROUTINE: Finish the bio calculations
!
! !INTERFACE:
   subroutine end_bio_polynow
!
! !DESCRIPTION:
!  Nothing done yet --- supplied for completeness.
!
! !USES:
   IMPLICIT NONE
!
! !REVISION HISTORY:
!  Original author(s): Hans Burchard & Karsten Bolding
!
!EOP
!-----------------------------------------------------------------------
!BOC

   return
   end subroutine end_bio_polynow
!EOC

!-----------------------------------------------------------------------

   end module bio_polynow

!-----------------------------------------------------------------------
! Copyright by the GOTM-team under the GNU Public License - www.gnu.org
!-----------------------------------------------------------------------
!
!DOCUMENTATION
!
!
!-----------------------------------------------------------------------
!!-----------------Maximum loss rate
!-----------------------------------------------------------------------
!
!pprime: Term used to limit phyto. mortality at very low biomass levels (see Moore et al.,2002)
! Minimum phyto biomass :
!0.001 mmol C/m3 for small phyto --» 0.00015094 mmol N/m3 (Comm. pers. Irene Schloss)
!0.005 mmol C/m3 for diatoms     --» 0.00075472 mmol N/m3 (Comm. pers. Irene Schloss)
!a minimum aggregation loss of 5% per day is set to account for direct sinking losses for diatoms.
!Otherwise, a maximum aggregation loss of 70% (txloss_) per day is imposed on all phytoplankton size classe.
!!If you choose to represent large phytoplankton please use in the loop: 
! sp_agg=sp_mort2*pprime*pprime/256.0            
! sp_agg=min((0.7*pprime),sp_agg)
! so_agg=max((0.05*pprime),sp_agg)   
!
!
!-----------------------------------------------------------------------
! COLLISION & AGGREGATION
!-----------------------------------------------------------------------
!
!
!Jackson 1990
!Collision rate is a function of sizes of colliding particles, their concentrations, and environmental parameters. Three mechanisms are used to describe particle collisions in aquatic systems.
!-----------------------------------------------------------------------
! The first is Brownian motion
!-----------------------------------------------------------------------
!in which the random motions of particles move some together. Because the diffusivity of particles varies inversely with their radii, small particles move the most and have the greatest collision rates.
!T = Température absolue en degrés K(temperature en degrés celsius récupéré dans bio_var)
!T_degK(ci)=T(ci)+ 273.15 !( On utilise la Température récupérée dans bio_var)
!size = rayons des particules considérées - micro metres
!dynvis= viscosité dynamique - en Pascal seconde (Pa.s = (N/m2).s)(Défini dans la liste des paramètres)(http://www.engineeringtoolbox.com/water-dynamic-kinematic-viscosity-d_596.html et http://www.formules-physique.com/categorie/1133)
!kB= Constante de Boltzmann (= 1.3806488e-23 joules / kelvin)(Défini dans la liste des paramètres)

!                        betaBr=((8*kB*T_degK(ci))/(6*dynvis))*(((size1+size2)**2)/(size1*size2))

!-----------------------------------------------------------------------
! The second collision mechanism is shear, either laminar or turbulent,
!-----------------------------------------------------------------------
! in which differences in fluid velocity cause two particles being carried by the fluid to approach each other and touch. 
!size = rayons des particules considérées - micro metres
!eps=the turbulent dissipation rate (m2.sec-3) - Disponible via bio_var /bio.F90(from Turbulence module).Unité dispo dans ncdfout.(https://www.cfd-online.com/Wiki/Turbulence_dissipation_rate)
!eps fro; physical model will work only if you choose in gotmturb.nml an another model for turbulence calculation than Kpp.Kpp does not provide a value for eps so you will found empty field leading to model to crash. You can choose the second order model as example.
!kinvis= kinematic viscosity (http://www.rheosense.com/basics/viscosity-units)(Défini dans la liste des paramètres)(http://www.engineeringtoolbox.com/water-dynamic-kinematic-viscosity-d_596.html et http://www.formules-physique.com/categorie/1133)

!                           q=(min(size1,size2))/(max(size1,size2))
!                          betaSh=9.8*((q**2)/(1+2*q**2))*(sqrt(eep/kinvis))*(size1+size2)***3

!-----------------------------------------------------------------------
!The third collision mechanism is differential sedimentation,
!-----------------------------------------------------------------------
! in which one particle falls faster than another, overtaking and colliding with it. Because particle fall velocity is usually a function of size, two particles of the same size and specific gravity fall at the same rate and do not overtake each other.
!w = settling velocities

!                         betaDs=(1/2)*pi*min(size1,size2)**2¦w1-w2¦
!-----------------------------------------------------------------------
!Coefficient de Kernel
!-----------------------------------------------------------------------
                         !kernel_coef=betaBr+betaSh+betaDs

! (See Jokulsdottir 2011)
! dd(x,d4,ci)= stidph*betadph*cc(x,ci)

!-----------------------------------------------------------------------
! PARTICLES SIZES
!-----------------------------------------------------------------------

! For settling velocities : read
!Turner, J. T. (2002). Zooplankton fecal pellets, marine snow and sinking phytoplankton blooms. Aquatic microbial ecology, 27(1), 57-102.

!Fecals pellets size:
!Gowing, M. M., & Silver, M. W. (1985). Minipellets: a new and abundant size class of marine fecal pellets. Journal of Marine Research, 43(2), 395-418.
! > to 50 micro.m  --> 50 µm = 0.000050 m

!Phytoplankton size
!Zoe V. Finkel, John Beardall, Kevin J. Flynn, Antonietta Quigg, T. Alwyn V. Rees, John A. Raven; Phytoplankton in a changing world: cell size and elemental stoichiometry. J Plankton Res 2010; 32 (1): 119-137. doi: 10.1093/plankt/fbp098
!2–20 micro m : for the nanoplankton (Phyto)
!20–200 micro m :for  the  microplankton (Zoo)


!-----------------------------------------------------------------------
! Fragmentation
!-----------------------------------------------------------------------
!Goldthwait, S., Yen, J., Brown, J., & Alldredge, A. (2004). Quantification of marine snow fragmentation by swimming euphausiids. Limnology and Oceanography, 49(4), 940-952.
!Euphausiids were capable of fragmenting all aggregate types and produced an
!average of 7.3 daughter particles, with 60% of these daughter particles remaining within the marine snow size class
!(>0.5 mm).

!Alldredge, A. L., Granata, T. C., Gotschalk, C. C., & Dickey, T. D. (1990). The physical strength of marine snow and its implications for particle disaggregation in the ocean. LIMNOLOGY, 35.
!Abiotic fragmentation of large, rapidly sinking aggregates into smaller, suspended particles by
!fluid shear has been suggested as an important process governing the particle size spectrum in the
!ocean and as one explanation for the exponential decrease of particulate flux with depth below the
!euphotic zone.
!Biological processes
!of disaggregation, such as animal grazing, appear far more likely to mediate the size spectrum of
!aggregated particulate matter in the ocean than abiotic fragmentation due to fluid motion.





!Calculate by Stokes Law : settling velocity (m/s)

!w_p_m   =(2*(rho_p-rho_F)*(size_phy**2)*g)/(9*dynvis)
!w_dph_m =(2*(rho_dph-rho_F)*(size_dph**2)*g)/(9*dynvis)
!w_dzo_m =(2*(rho_dzo-rho_F)*(size_dzo**2)*g)/(9*dynvis)
!w_fp_m  =(2*(rho_fp-rho_F)*(size_fp**2)*g)/(9*dynvis)




!http://infohost.nmt.edu/tcc/help/lang/fortran/scaling.html
!https://stackoverflow.com/questions/23057213/how-to-generate-integer-random-number-in-fortran-90-in-the-range-0-5


!---------

! ---Only Phytoplankton
!If (cc(p,ci) .gt. cons_min .and. &
!   cc(d1,ci) .lt. cons_min .and. cc(d2,ci) .lt. cons_min .and. cc(d3,ci) .lt. cons_min &
!   .and. flux_msn(ci) .gt. min_flux_msn) Then

!   cc(aug_si_d4,ci)=Ratio_P*diam_phy !Unit of size [m]

! ---Only Dead Phytoplankton
!else If (cc(d1,ci).gt. cons_min &
  ! .and. cc(p,ci) .lt. cons_min .and. cc(d2,ci) .lt. cons_min .and. cc(d3,ci) .lt. cons_min &
 !  .and. flux_msn(ci) .gt. min_flux_msn) Then

 ! cc(aug_si_d4,ci)=Ratio_D1*diam_dph !Unit of size [m]

! ---Only Dead Zooplankton
!else If (cc(d2,ci).gt. cons_min &
!.and. cc(p,ci) .lt. cons_min .and. cc(d1,ci) .lt. cons_min .and. cc(d3,ci) .lt. cons_min &
! .and. flux_msn(ci) .gt. min_flux_msn) Then

! cc(aug_si_d4,ci)=Ratio_D2*diam_dzo !Unit of size [m]

! ---Only fecal pellets
!else If (cc(d3,ci).gt. cons_min &
!.and. cc(p,ci) .lt. cons_min .and. cc(d1,ci) .lt. cons_min .and. cc(d2,ci) .lt. cons_min &
! .and. flux_msn(ci) .gt. min_flux_msn) Then

! cc(aug_si_d4,ci)=Ratio_D3*diam_fp  !Unit of size [m]

! ---All
!else If (cc(p,ci) .gt. cons_min .and. cc(d1,ci) .gt. cons_min .and. cc(d2,ci) .gt. cons_min &
!         .and. cc(d3,ci) .gt. cons_min .and. flux_msn(ci) .gt. min_flux_msn) Then

! cc(aug_si_d4,ci)= Ratio_P*diam_phy+Ratio_D1*diam_dph+Ratio_D2*diam_dzo+Ratio_D3*diam_fp
!Unit of size [m]
!else
!  cc(aug_si_d4,ci)= 0.0 !Unit of size [m]
!endif


!version initiale

          ! if (size_msn_m .le. 0.0) then !#6 !1e-4 If the size of msn is inferior to the minimum size of our initial particles
            !  cc(taille_msn,ci)= 0.0
            !   if (cc(d4,ci) .lt. cons_min) then
           !    write(*,*)'Size but no msn'
           !    endif
           !else !#6
          !    cc(taille_msn,ci) = size_msn_m ! Keep in memory for the next step
           !end if !#6



!if(cc(taille_msn,ci) .eq. 0.0) then !#3
!  size_msn_m = cc(aug_si_d4,ci)
!! cc(taille_msn,ci) = size_msn_m ! Keep in memory for the next step
! cc(taille_coag,ci) = size_msn_m 

!else if (cc(aug_si_d4,ci) .eq. 0.0) then !#3
!  size_msn_m = cc(taille_msn,ci)
!! cc(taille_msn,ci)= size_msn_m 
! cc(taille_coag,ci) = size_msn_m 

!else !Calculation of the news size from the previous one + the augmentation calculate via cc(aug_si_d4).
 !!size_msn_m = cc(taille_msn,ci)+ cc(aug_si_d4,ci)
 !!cc(taille_msn,ci)= size_msn_m ! Keep in memory for the next step
  !size_msn_m = cc(taille_msn,ci)+ cc(aug_si_d4,ci)
  !cc(taille_coag,ci)= size_msn_m 
!end if !#3



             !--» If the size of msn after the cut is lower thant the smallest particle that may composed it,
             !--» the minimum size of its constituant are set for the marine snow
                                        !              if(size_msn_m .lt. min_size_msn)then  
                                         !                write(*,*)'Smaller than the smallest initial particles'
                                          !               size_msn_m = min_size_msn
                                          !            endif


  
   !  ws(d4,ci)= min(w_msn_m,(depth_bio/nlev)/dt_bio)


! write(*,*)'flux_msn(ci) ',flux_msn(ci) 
             ! ---Calcul of the ratios [-]
              !  Ratio_P   = Flux_P(ci)/flux_msn(ci)
              !  Ratio_D1  = Flux_D1(ci)/flux_msn(ci)
              !  Ratio_D2  = Flux_D2(ci)/flux_msn(ci)
              !  Ratio_D3  = Flux_D3(ci)/flux_msn(ci)