Codebase list gerris / HEAD src / fluid.c
HEAD

Tree @HEAD (Download .tar.gz)

fluid.c @HEADraw · history · blame

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
/* Gerris - The GNU Flow Solver
 * Copyright (C) 2001 National Institute of Water and Atmospheric Research
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.  
 */
/*! \file
 * \brief Low-level discrete operators.
 */

#include <math.h>

#include <stdlib.h>

#include "fluid.h"
#include "variable.h"
#include "domain.h"
#include "solid.h"

/**
 * gfs_cell_face:
 * @cell: a #FttCell.
 * @d: a direction.
 *
 * This function is different from ftt_cell_face() because it takes
 * into account the solid fractions.
 *
 * Returns: the face of @cell in direction @d.
 */
FttCellFace gfs_cell_face (FttCell * cell,
			   FttDirection d)
{
  FttCellFace f = {cell, NULL, d};

  g_return_val_if_fail (cell != NULL, f);

  if (!GFS_IS_MIXED (cell) || GFS_STATE (cell)->solid->s[d] > 0.)
    f.neighbor = ftt_cell_neighbor (cell, d);
  return f;
}

typedef struct _Gradient Gradient;

/* grad(p) = -a*p(cell) + b*p(neighbor) + c */
struct _Gradient {
  gdouble a, b, c;
};

/* get_average_neighbor_value_stencil() needs to be updated whenever 
 * this function is modified
 */
static gdouble average_neighbor_value (const FttCellFace * face,
				       guint v,
				       gdouble * x)
{
  /* check for corner refinement violation (topology.fig) */
  g_assert (ftt_cell_level (face->neighbor) == ftt_cell_level (face->cell));
  
  if (FTT_CELL_IS_LEAF (face->neighbor))
    return GFS_VALUEI (face->neighbor, v);
  else {
    FttCellChildren children;
    gdouble av = 0., a = 0.;
    FttDirection od = FTT_OPPOSITE_DIRECTION (face->d);
    guint i, n;
    
    n = ftt_cell_children_direction (face->neighbor, od, &children);
    for (i = 0; i < n; i++)
      if (children.c[i] && GFS_VALUEI (children.c[i], v) != GFS_NODATA) {
	gdouble w = GFS_IS_MIXED (children.c[i]) ? GFS_STATE (children.c[i])->solid->s[od] : 1.;
	a += w;
	av += w*GFS_VALUEI (children.c[i], v);
      }
    if (a > 0.) {
      *x = 3./4.;
      return av/a;
    }
    else
      return GFS_VALUEI (face->cell, v);
  }
}

static void average_neighbor_value_stencil (const FttCellFace * face, guint v)
{
  /* check for corner refinement violation (topology.fig) */
  g_assert (ftt_cell_level (face->neighbor) == ftt_cell_level (face->cell));
  
  if (FTT_CELL_IS_LEAF (face->neighbor))
    GFS_VALUEI (face->neighbor, v) = 1.;
  else {
    FttCellChildren children;
    gdouble a = 0.;
    guint i, n;
    
    n = ftt_cell_children_direction (face->neighbor,
				     FTT_OPPOSITE_DIRECTION (face->d),
				     &children);
    for (i = 0; i < n; i++)
      if (children.c[i]) {
	a += 1.;
	GFS_VALUEI (children.c[i], v) = 1.;
      }
    if (a == 0.)
      GFS_VALUEI (face->cell, v) = 1.;
  }
}

#if FTT_2D

/* v = a*v(cell) + b 
 * 
 * Second order 1D interpolation.
 */
static GfsGradient interpolate_1D2 (FttCell * cell,
				    FttDirection d,
				    gdouble x,
				    guint v)
{
  GfsGradient p;
  FttCellFace f1, f2;
  gdouble p1 = 0., p2 = 0.;
  gdouble x1 = 1., x2 = 1.;
  gdouble a1, a2;

  g_return_val_if_fail (cell != NULL, p);
  g_return_val_if_fail (!GFS_IS_MIXED (cell), p);
  
  f1 = gfs_cell_face (cell, FTT_OPPOSITE_DIRECTION (d));
  if (f1.neighbor)
    p1 = average_neighbor_value (&f1, v, &x1);
  f2 = gfs_cell_face (cell, d);
  if (f2.neighbor)
    p2 = average_neighbor_value (&f2, v, &x2);

  a1 = x*(x - x2)/(x1*(x1 + x2));
  a2 = x*(x + x1)/(x2*(x1 + x2));

  p.a = 1. - a1 - a2;
  p.b = 0.;
  if (f1.neighbor)
    p.b += a1*p1;
  else
    p.a += a1;
  if (f2.neighbor)
    p.b += a2*p2;
  else
    p.a += a2;

  return p;
}

/* v = a*v(cell) + b 
 * 
 * First order 1D interpolation.
 *
 * get_interpolate_1D1_stencil() needs to be updated whenever 
 * this function is modified
 */
static GfsGradient interpolate_1D1 (FttCell * cell,
				    FttDirection d,
				    gdouble x,
				    guint v)
{
  GfsGradient p = { 1., 0. };
  FttCellFace f;

  f = gfs_cell_face (cell, d);
  if (f.neighbor) {
    gdouble x2 = 1.;
    gdouble p2 = average_neighbor_value (&f, v, &x2);
    if (p2 != GFS_NODATA) {
      gdouble a2 = x/x2;
      p.b += a2*p2;
      p.a -= a2;
    }
  }

  return p;
}

static void interpolate_1D1_stencil (FttCell * cell,
				     FttDirection d,
				     guint v)
{
  FttCellFace f;

  GFS_VALUEI (cell, v) = 1.;
  f = ftt_cell_face (cell, d);
  if (f.neighbor)
    average_neighbor_value_stencil (&f, v);
}

#else /* not FTT_2D */

/* v = a*v(cell) + b 
 * 
 * First order 2D interpolation.
 *
 * get_interpolate_2D1_stencil() needs to be updated whenever 
 * this function is modified                                 
 */
static GfsGradient interpolate_2D1 (FttCell * cell,
				    FttDirection d1, FttDirection d2,
				    gdouble x, gdouble y,
				    guint v)
{
  GfsGradient p = { 1., 0. };
  FttCellFace f1, f2;

  f1 = gfs_cell_face (cell, d1);
  if (f1.neighbor) {
    gdouble y1 = 1.;
    gdouble p1 = average_neighbor_value (&f1, v, &y1);
    if (p1 != GFS_NODATA) {
      gdouble a1 = y/y1;
      p.b += a1*p1;
      p.a -= a1;
    }
  }

  f2 = gfs_cell_face (cell, d2);
  if (f2.neighbor) {
    gdouble x2 = 1.;
    gdouble p2 = average_neighbor_value (&f2, v, &x2);
    if (p2 != GFS_NODATA) {
      gdouble a2 = x/x2;
      p.b += a2*p2;
      p.a -= a2;
    }
  }
  
  return p;
}

static void interpolate_2D1_stencil (FttCell * cell,
				     FttDirection d1, FttDirection d2,
				     guint v)
{
  FttCellFace f1, f2;

  GFS_VALUEI (cell, v) = 1.;
  f1 = ftt_cell_face (cell, d1);
  if (f1.neighbor)
    average_neighbor_value_stencil (&f1, v);
  f2 = ftt_cell_face (cell, d2);
  if (f2.neighbor)
    average_neighbor_value_stencil (&f2, v);
}

#endif /* not FTT_2D */

#if FTT_2D
static gint perpendicular[FTT_NEIGHBORS][FTT_CELLS] = 
  {{-1,  2, -1,  3},
   { 2, -1,  3, -1},
   { 1,  0, -1, -1},
   {-1, -1,  1,  0}};
#else  /* FTT_3D */
static gint perpendicular[FTT_NEIGHBORS][FTT_CELLS][2] = 
  {{{-1,-1},{2,4},{-1,-1},{3,4},{-1,-1},{2,5},{-1,-1},{3,5}},
   {{2,4},{-1,-1},{3,4},{-1,-1},{2,5},{-1,-1},{3,5},{-1,-1}},
   {{4,1},{4,0},{-1,-1},{-1,-1},{5,1},{5,0},{-1,-1},{-1,-1}},
   {{-1,-1},{-1,-1},{4,1},{4,0},{-1,-1},{-1,-1},{5,1},{5,0}},
   {{1,2},{0,2},{1,3},{0,3},{-1,-1},{-1,-1},{-1,-1},{-1,-1}},
   {{-1,-1},{-1,-1},{-1,-1},{-1,-1},{1,2},{0,2},{1,3},{0,3}}};
#endif /* FTT_3D */

/* gradient_fine_coarse_stencil() needs to be updated whenever 
   this function is modified  */
static Gradient gradient_fine_coarse (const FttCellFace * face, guint v)
{
  Gradient g;
  GfsGradient p;
#if FTT_2D
  gint dp;
#else  /* FTT_3D */
  gint * dp;
#endif /* FTT_3D */

  g_assert (face != NULL);
  g_assert (ftt_face_type (face) == FTT_FINE_COARSE);

  dp = perpendicular[face->d][FTT_CELL_ID (face->cell)];
#if FTT_2D
  g_assert (dp >= 0);
  p = interpolate_1D1 (face->neighbor, dp, 1./4., v);
#else  /* FTT_3D */
  g_assert (dp[0] >= 0 && dp[1] >= 0);
  p = interpolate_2D1 (face->neighbor, dp[0], dp[1], 1./4., 1./4., v);
#endif /* FTT_3D */

  g.a = 2./3.;
  g.b = 2.*p.a/3.;
  g.c = 2.*p.b/3.;

  return g;
}

#define REFINE_CORNER(cell) {if (cell && FTT_CELL_IS_LEAF (cell) && \
                              ftt_cell_level (cell) < level - 1) \
	                    ftt_cell_refine_single (cell, init, data);}

void ftt_cell_refine_corners (FttCell * cell,
			      FttCellInitFunc init,
			      gpointer data)
{
  FttDirection d;
  FttCellNeighbors neighbor;
  guint level;

  g_return_if_fail (cell != NULL);

  level = ftt_cell_level (cell);
  ftt_cell_neighbors (cell, &neighbor);
  for (d = 0; d < FTT_NEIGHBORS; d++)
    if (neighbor.c[d] && ftt_cell_level (neighbor.c[d]) < level) {
      if (GFS_CELL_IS_BOUNDARY (neighbor.c[d]))
	ftt_cell_refine_single (neighbor.c[d], init, data);
      else {
	FttCell * n;
#if FTT_2D
	gint dp;
#else  /* FTT_3D */
	gint * dp;
#endif /* FTT_3D */
	
	dp = perpendicular[d][FTT_CELL_ID (cell)];
#if FTT_2D
	g_assert (dp >= 0);
	n = ftt_cell_neighbor (neighbor.c[d], dp);
	REFINE_CORNER (n)
#else  /* FTT_3D */
	g_assert (dp[0] >= 0 && dp[1] >= 0);
	n = ftt_cell_neighbor (neighbor.c[d], dp[0]);
	REFINE_CORNER (n)
	n = ftt_cell_neighbor (neighbor.c[d], dp[1]);
	REFINE_CORNER (n)
#endif /* FTT_3D */
      }
    }
}

/**
 * gfs_neighbor_value:
 * @face: a #FttCellFace.
 * @v: a variable index.
 * @x: relative position of the neigboring value.
 *
 * Returns: the (averaged) neighboring value of variable @v at @face
 * and its position @x.
 */
gdouble gfs_neighbor_value (const FttCellFace * face,
			    guint v,
			    gdouble * x)
{
  g_return_val_if_fail (face != NULL, 0.);
  g_return_val_if_fail (x != NULL, 0.);

  GfsGradient vc;
#if FTT_2D
  gint dp;
#else  /* FTT_3D */
  gint * dp;
#endif /* FTT_3D */

  if (ftt_cell_level (face->neighbor) == ftt_cell_level (face->cell))
    /* neighbor at same level */
    return average_neighbor_value (face, v, x);
  else {
    if (GFS_VALUEI (face->neighbor, v) == GFS_NODATA)
      return GFS_NODATA;
    /* neighbor at coarser level */
    dp = perpendicular[face->d][FTT_CELL_ID (face->cell)];
#if FTT_2D
    g_assert (dp >= 0);
    vc = interpolate_1D1 (face->neighbor, dp, 1./4., v);
#else  /* FTT_3D */
    g_assert (dp[0] >= 0 && dp[1] >= 0);
    vc = interpolate_2D1 (face->neighbor, dp[0], dp[1], 1./4., 1./4., v);
#endif /* FTT_3D */
    *x = 3./2.;
    return vc.a*GFS_VALUEI (face->neighbor, v) + vc.b;
  }
}

static void neighbor_value_stencil (const FttCellFace * face, guint v)
{
#if FTT_2D
  gint dp;
#else  /* FTT_3D */
  gint * dp;
#endif /* FTT_3D */

  if (ftt_cell_level (face->neighbor) == ftt_cell_level (face->cell))
    /* neighbor at same level */
    average_neighbor_value_stencil (face, v);
  else {
    /* neighbor at coarser level */
    dp = perpendicular[face->d][FTT_CELL_ID (face->cell)];
#if FTT_2D
    g_assert (dp >= 0);
    interpolate_1D1_stencil (face->neighbor, dp, v);
#else  /* FTT_3D */
    g_assert (dp[0] >= 0 && dp[1] >= 0);
    interpolate_2D1_stencil (face->neighbor, dp[0], dp[1], v);
#endif /* FTT_3D */
    GFS_VALUEI (face->neighbor, v) = 1.;
  }
}

/**
 * gfs_center_gradient:
 * @cell: a #FttCell.
 * @c: a component.
 * @v: a #GfsVariable index.
 *
 * The gradient is normalized by the size of the cell.
 *
 * Returns: the value of the @c component of the gradient of variable @v
 * at the center of the cell.  
 */
gdouble gfs_center_gradient (FttCell * cell,
			     FttComponent c,
			     guint v)
{
  FttDirection d = 2*c;
  FttCellFace f1;
  gdouble v0;

  g_return_val_if_fail (cell != NULL, 0.);
  g_return_val_if_fail (c < FTT_DIMENSION, 0.);

  f1 = gfs_cell_face (cell, FTT_OPPOSITE_DIRECTION (d));
  v0 = GFS_VALUEI (cell, v);
  if (f1.neighbor) {
    FttCellFace f2 = gfs_cell_face (cell, d);
    gdouble x1 = 1., v1;
    
    v1 = gfs_neighbor_value (&f1, v, &x1);
    if (f2.neighbor) {
      /* two neighbors: second-order differencing (parabola) */
      gdouble x2 = 1., v2;
      
      v2 = gfs_neighbor_value (&f2, v, &x2);
      return (x1*x1*(v2 - v0) + x2*x2*(v0 - v1))/(x1*x2*(x2 + x1));
    }
    else
      /* one neighbor: first-order differencing */
      return (v0 - v1)/x1;
  }
  else {
    FttCellFace f2 = gfs_cell_face (cell, d);

    if (f2.neighbor) {
      gdouble x2 = 1.;
      
      /* one neighbor: first-order differencing */
      return (gfs_neighbor_value (&f2, v, &x2) - v0)/x2;
    }
  }
  /* no neighbors */
  return 0.;
}

/**
 * gfs_center_gradient_stencil:
 * @cell: a #FttCell.
 * @c: a component.
 * @v: a #GfsVariable index.
 *
 * Sets to 1. the @v variable of all the cells which would be used if
 * gfs_center_gradient() was called with identical arguments.
 */
void gfs_center_gradient_stencil (FttCell * cell,
				  FttComponent c,
				  guint v)
{
  FttDirection d = 2*c;
  FttCellFace f1, f2;

  g_return_if_fail (cell != NULL);
  g_return_if_fail (c < FTT_DIMENSION);

  f1 = ftt_cell_face (cell, FTT_OPPOSITE_DIRECTION (d));
  if (f1.neighbor == cell) /* periodic */
    return;
  if (f1.neighbor) {
    GFS_VALUEI (cell, v) = 1.;
    neighbor_value_stencil (&f1, v);
  }
  f2 = ftt_cell_face (cell, d);
  if (f2.neighbor) {
    GFS_VALUEI (cell, v) = 1.;
    neighbor_value_stencil (&f2, v);
  }
}

/**
 * gfs_center_van_leer_gradient:
 * @cell: a #FttCell.
 * @c: a component.
 * @v: a #GfsVariable index.
 *
 * The gradient is normalized by the size of the cell and is limited
 * using van Leer's limiter.
 *
 * Returns: the value of the @c component of the gradient of variable @v
 * at the center of the cell.  
 */
gdouble gfs_center_van_leer_gradient (FttCell * cell,
				      FttComponent c,
				      guint v)
{
  FttDirection d = 2*c;
  FttCellFace f1;
  
  g_return_val_if_fail (cell != NULL, 0.);
  g_return_val_if_fail (c < FTT_DIMENSION, 0.);

  f1 = gfs_cell_face (cell, FTT_OPPOSITE_DIRECTION (d));
  if (f1.neighbor) {
    FttCellFace f2 = gfs_cell_face (cell, d);
    
    if (f2.neighbor) {
      /* two neighbors: second-order differencing (parabola)
	 + van Leer limiter. See http://en.wikipedia.org/wiki/Flux_limiter */
      gdouble x1 = 1., x2 = 1., v0, v1, v2;      
      v0 = GFS_VALUEI (cell, v);
      v1 = gfs_neighbor_value (&f1, v, &x1);
      v2 = gfs_neighbor_value (&f2, v, &x2);
      if (v1 == GFS_NODATA || v2 == GFS_NODATA)
	return 0.;

      gdouble s1 = 2.*(v0 - v1);
      gdouble s2 = 2.*(v2 - v0);

      if (s1*s2 <= 0.)
	return 0.;
      gdouble s0 = (x1*x1*(v2 - v0) + x2*x2*(v0 - v1))/(x1*x2*(x2 + x1));
      if (ABS (s2) < ABS (s1))
	s1 = s2;
      if (ABS (s0) < ABS (s1))
	return s0;
      return s1;
    }
  }
  /* only one or no neighbors */
  return 0.;
}

static gdouble generic_limiter (gdouble r, gdouble beta)
{
  gdouble v1 = MIN (r, beta), v2 = MIN (beta*r, 1.);
  v1 = MAX (0., v1);
  return MAX (v1, v2);
}

static gdouble minmod_limiter (gdouble r)
{
  return generic_limiter (r, 1.);
}

static gdouble superbee_limiter (gdouble r)
{
  return generic_limiter (r, 2.);
}

static gdouble sweby_limiter (gdouble r)
{
  return generic_limiter (r, 1.5);
}

static gdouble center_limited_gradient (FttCell * cell,
					FttComponent c,
					guint v,
					gdouble (* limiter) (gdouble))
{
  FttDirection d = 2*c;
  FttCellFace f1;
  gdouble v0;

  f1 = gfs_cell_face (cell, FTT_OPPOSITE_DIRECTION (d));
  v0 = GFS_VALUEI (cell, v);
  if (f1.neighbor) {
    FttCellFace f2 = gfs_cell_face (cell, d);
    if (f2.neighbor) {
      /* two neighbors */
      gdouble x1 = 1., v1, x2 = 1., v2;
      v1 = gfs_neighbor_value (&f1, v, &x1);
      v2 = gfs_neighbor_value (&f2, v, &x2);

      gdouble g;
      if (v0 == v1)
	g = 0.;
      else
	g = (* limiter) ((v2 - v0)*x1/((v0 - v1)*x2))*(v0 - v1)/x1;
      return g;
    }
  }
  /* only one or no neighbors */
  return 0.;
}

/**
 * gfs_center_minmod_gradient:
 * @cell: a #FttCell.
 * @c: a component.
 * @v: a #GfsVariable index.
 *
 * The gradient is normalized by the size of the cell and is limited
 * using a minmod limiter.
 *
 * Returns: the value of the @c component of the gradient of variable @v
 * at the center of the cell.  
 */
gdouble gfs_center_minmod_gradient (FttCell * cell,
				    FttComponent c,
				    guint v)
{
  g_return_val_if_fail (cell != NULL, 0.);
  g_return_val_if_fail (c < FTT_DIMENSION, 0.);

  return center_limited_gradient (cell, c, v, minmod_limiter);
}

/**
 * gfs_center_superbee_gradient:
 * @cell: a #FttCell.
 * @c: a component.
 * @v: a #GfsVariable index.
 *
 * The gradient is normalized by the size of the cell and is limited
 * using a superbee limiter.
 *
 * Returns: the value of the @c component of the gradient of variable @v
 * at the center of the cell.  
 */
gdouble gfs_center_superbee_gradient (FttCell * cell,
				      FttComponent c,
				      guint v)
{
  g_return_val_if_fail (cell != NULL, 0.);
  g_return_val_if_fail (c < FTT_DIMENSION, 0.);

  return center_limited_gradient (cell, c, v, superbee_limiter);
}

/**
 * gfs_center_sweby_gradient:
 * @cell: a #FttCell.
 * @c: a component.
 * @v: a #GfsVariable index.
 *
 * The gradient is normalized by the size of the cell and is limited
 * using a Sweby limiter (beta = 1.5).
 *
 * Returns: the value of the @c component of the gradient of variable @v
 * at the center of the cell.  
 */
gdouble gfs_center_sweby_gradient (FttCell * cell,
				   FttComponent c,
				   guint v)
{
  g_return_val_if_fail (cell != NULL, 0.);
  g_return_val_if_fail (c < FTT_DIMENSION, 0.);

  return center_limited_gradient (cell, c, v, sweby_limiter);
}

/**
 * gfs_center_regular_gradient:
 * @cell: a #FttCell.
 * @c: a component.
 * @v: a #GfsVariable.
 *
 * The gradient is normalized by the size of the cell. Only regular
 * Cartesian stencils are used to compute the gradient.
 *
 * Returns: the value of the @c component of the gradient of variable @v
 * at the center of the cell. 
 */
gdouble gfs_center_regular_gradient (FttCell * cell,
				     FttComponent c,
				     GfsVariable * v)
{
  g_return_val_if_fail (cell != NULL, 0.);
  g_return_val_if_fail (c < FTT_DIMENSION, 0.);
  g_return_val_if_fail (v != NULL, 0.);

  FttCell * n1 = ftt_cell_neighbor (cell, 2*c);
  guint level = ftt_cell_level (cell);
  if (n1) {
    if (ftt_cell_level (n1) < level)
      return gfs_center_regular_gradient (ftt_cell_parent (cell), c, v)/2.;
    FttCell * n2 = ftt_cell_neighbor (cell, 2*c + 1);
    if (n2) {
      if (ftt_cell_level (n2) < level)
	return gfs_center_regular_gradient (ftt_cell_parent (cell), c, v)/2.;
      /* two neighbors: second-order differencing (parabola) */
      return (GFS_VALUE (n1, v) - GFS_VALUE (n2, v))/2.;
    }
    else
      /* one neighbor: first-order differencing */
      return GFS_VALUE (n1, v) - GFS_VALUE (cell, v);
  }
  else {
    FttCell * n2 = ftt_cell_neighbor (cell, 2*c + 1);
    if (n2) {
      if (ftt_cell_level (n2) < level)
	return gfs_center_regular_gradient (ftt_cell_parent (cell), c, v)/2.;
      /* one neighbor: first-order differencing */
      return GFS_VALUE (cell, v) - GFS_VALUE (n2, v);
    }
  }
  /* no neighbors */
  return 0.;
}

/**
 * gfs_center_regular_2nd_derivative:
 * @cell: a #FttCell.
 * @c: a component.
 * @v: a #GfsVariable.
 *
 * The derivative is normalized by the size of the cell squared. Only
 * regular Cartesian stencils are used to compute the derivative.
 *
 * Returns: the value of the @c component of the 2nd derivative of
 * variable @v at the center of the cell.
 */
gdouble gfs_center_regular_2nd_derivative (FttCell * cell, 
					   FttComponent c, 
					   GfsVariable * v)
{
  g_return_val_if_fail (cell != NULL, 0.);
  g_return_val_if_fail (c < FTT_DIMENSION, 0.);
  g_return_val_if_fail (v != NULL, 0.);

  FttCell * n1 = ftt_cell_neighbor (cell, 2*c);
  FttCell * n2 = ftt_cell_neighbor (cell, 2*c + 1);
  if (n1 && n2) {
    guint level = ftt_cell_level (cell);
    if (ftt_cell_level (n1) < level || ftt_cell_level (n2) < level)
      return gfs_center_regular_2nd_derivative (ftt_cell_parent (cell), c, v)/4.;
    return GFS_VALUE (n1, v) - 2.*GFS_VALUE (cell, v) + GFS_VALUE (n2, v);
  }
  /* one or no neighbors */
  return 0.;
}

/**
 * gfs_face_gradient:
 * @face: a #FttCellFace.
 * @g: the #GfsGradient.
 * @v: a #GfsVariable index.
 * @max_level: the maximum cell level to consider (-1 means no restriction).
 *
 * Set the value of @g as the gradient of variable @v on the
 * @face. The value returned is second order accurate in space and
 * conservative, in the sense that values at a coarse/fine cell
 * boundary are consistent.
 *
 * The value of the gradient (normalised by the size of @face->cell)
 * is given by: @g->b - @g->a*GFS_VALUE (@face->cell, @v).
 */
void gfs_face_gradient (const FttCellFace * face,
			GfsGradient * g,
			guint v,
			gint max_level)
{
  guint level;

  g_return_if_fail (face != NULL);

  g->a = g->b = 0.;
  if (face->neighbor == NULL || GFS_FACE_FRACTION (face) == 0.)
    return;

  level = ftt_cell_level (face->cell);
  if (ftt_cell_level (face->neighbor) < level) {
    /* neighbor is at a shallower level */
    Gradient gcf;

    gcf = gradient_fine_coarse (face, v);
    g->a = gcf.a;
    g->b = gcf.b*GFS_VALUEI (face->neighbor, v) + gcf.c;
  }
  else {
    if (level == max_level || FTT_CELL_IS_LEAF (face->neighbor)) {
      /* neighbor is at the same level */
      g->a = 1.;
      g->b = GFS_VALUEI (face->neighbor, v);
    }
    else {
      /* neighbor is at a deeper level */
      FttCellChildren children;
      FttCellFace f;
      guint i, n;
      gdouble s;
      
      f.d = FTT_OPPOSITE_DIRECTION (face->d);
      n = ftt_cell_children_direction (face->neighbor, f.d, &children);
      f.neighbor = face->cell;
      for (i = 0; i < n; i++)
	if ((f.cell = children.c[i])) {
	  Gradient gcf;
	  gcf = gradient_fine_coarse (&f, v);
	  s = GFS_FACE_FRACTION (&f);
	  g->a += s*gcf.b;
	  g->b += s*(gcf.a*GFS_VALUEI (f.cell, v) - gcf.c);
	}
      s = GFS_FACE_FRACTION (face)*n/2.;
      g->a /= s;
      g->b /= s;
    }
  }
}

/* face_weighted_gradient_stencil() needs to be updated whenever
   this function is modified                                   */
static void face_weighted_gradient (const FttCellFace * face,
				    GfsGradient * g,
				    guint v,
				    gint max_level,
				    guint dimension)
{
  guint level;

  g_return_if_fail (face != NULL);

  g->a = g->b = 0.;
  if (face->neighbor == NULL)
    return;

  level = ftt_cell_level (face->cell);
  if (ftt_cell_level (face->neighbor) < level) {
    /* neighbor is at a shallower level */
    Gradient gcf;
    gdouble w = GFS_STATE (face->cell)->f[face->d].v;

    gcf = gradient_fine_coarse (face, v);
    g->a = w*gcf.a;
    g->b = w*(gcf.b*GFS_VALUEI (face->neighbor, v) + gcf.c);
  }
  else {
    if (level == max_level || FTT_CELL_IS_LEAF (face->neighbor)) {
      /* neighbor is at the same level */
      gdouble w = GFS_STATE (face->cell)->f[face->d].v;

      g->a = w;
      g->b = w*GFS_VALUEI (face->neighbor, v);
    }
    else {
      /* neighbor is at a deeper level */
      FttCellChildren children;
      FttCellFace f;
      guint i, n;
      
      f.d = FTT_OPPOSITE_DIRECTION (face->d);
      n = ftt_cell_children_direction (face->neighbor, f.d, &children);
      f.neighbor = face->cell;
      for (i = 0; i < n; i++) 
	if ((f.cell = children.c[i])) {
	  Gradient gcf;
	  gdouble w = GFS_STATE (f.cell)->f[f.d].v;
	
	  gcf = gradient_fine_coarse (&f, v);
	  g->a += w*gcf.b;
	  g->b += w*(gcf.a*GFS_VALUEI (f.cell, v) - gcf.c);
	}
      if (dimension > 2) {
	g->a /= n/2.;
	g->b /= n/2.;
      }
    }
  }
}

/**
 * gfs_face_weighted_gradient:
 * @face: a #FttCellFace.
 * @g: the #GfsGradient.
 * @v: a #GfsVariable index.
 * @max_level: the maximum cell level to consider (-1 means no restriction).
 *
 * Set the value of @g as the gradient of variable @v on the @face
 * weighted by the value of the @v field of the face state vector of the
 * corresponding cell. The value returned is second order accurate in
 * space and conservative, in the sense that values at a coarse/fine
 * cell boundary are consistent.  
 */

void gfs_face_weighted_gradient (const FttCellFace * face,
				 GfsGradient * g,
				 guint v,
				 gint max_level)
{
  face_weighted_gradient (face, g, v, max_level, FTT_DIMENSION);
}

void gfs_face_weighted_gradient_2D (const FttCellFace * face,
				    GfsGradient * g,
				    guint v,
				    gint max_level)
{
  face_weighted_gradient (face, g, v, max_level, 2);
}

static void fullest_directions (const FttCellFace * face,
				FttDirection d[FTT_DIMENSION])
{
  FttComponent c = face->d/2, i;
  FttCell * mixed = GFS_IS_MIXED (face->cell) ? face->cell : face->neighbor;
  GfsSolidVector * s = GFS_STATE (mixed)->solid;

  d[0] = face->d;
  for (i = 1; i < FTT_DIMENSION; i++) {
    guint cp = (c + i) % FTT_DIMENSION;
    d[i] = s->s[2*cp] > s->s[2*cp + 1] ? 2*cp : 2*cp + 1;
  }
}

static FttCell * cell_corner_neighbor1 (FttCell * cell,
					FttDirection * d,
					gint max_level)
{
  if (!cell)
    return NULL;
  FttCell * neighbor = ftt_cell_neighbor (cell, d[0]);
  if (!neighbor)
    return NULL;
  else {
    guint level = ftt_cell_level (cell);
    if (ftt_cell_level (neighbor) < level)
      /* neighbor is at a shallower level */
      return neighbor;
    else {
      if (level == max_level || FTT_CELL_IS_LEAF (neighbor))
	/* neighbor is at the same level */
	return neighbor;
      else {
	/* neighbor is at a deeper level */
	guint i;
	FttDirection d1[FTT_DIMENSION];
	d1[0] = FTT_OPPOSITE_DIRECTION (d[0]);
	for (i = 1; i < FTT_DIMENSION; i++)
	  d1[i] = d[i];
	return ftt_cell_child_corner (neighbor, d1);
      }
    }
  }
}

#if FTT_2D
# define N_CELLS 4
#else  /* 3D */
# define N_CELLS 8
#endif /* 3D */

static gboolean inverse (gdouble mi[N_CELLS - 1][N_CELLS - 1])
{
#if FTT_2D
  gdouble m[N_CELLS - 1][N_CELLS - 1], det;
  guint i, j;
  
  for (i = 0; i < N_CELLS - 1; i++)
    for (j = 0; j < N_CELLS - 1; j++)
      m[i][j] = mi[i][j];
  
  det = (m[0][0]*(m[1][1]*m[2][2] - m[2][1]*m[1][2]) -
	 m[0][1]*(m[1][0]*m[2][2] - m[2][0]*m[1][2]) +
	 m[0][2]*(m[1][0]*m[2][1] - m[2][0]*m[1][1]));
  if (det == 0.)
    return FALSE;
  
  mi[0][0] = (m[1][1]*m[2][2] - m[1][2]*m[2][1])/det; 
  mi[0][1] = (m[2][1]*m[0][2] - m[0][1]*m[2][2])/det;
  mi[0][2] = (m[0][1]*m[1][2] - m[1][1]*m[0][2])/det; 
  mi[1][0] = (m[1][2]*m[2][0] - m[1][0]*m[2][2])/det; 
  mi[1][1] = (m[0][0]*m[2][2] - m[2][0]*m[0][2])/det; 
  mi[1][2] = (m[1][0]*m[0][2] - m[0][0]*m[1][2])/det; 
  mi[2][0] = (m[1][0]*m[2][1] - m[2][0]*m[1][1])/det; 
  mi[2][1] = (m[2][0]*m[0][1] - m[0][0]*m[2][1])/det; 
  mi[2][2] = (m[0][0]*m[1][1] - m[0][1]*m[1][0])/det; 
#else /* 3D */
  gint indxc[N_CELLS - 1], indxr[N_CELLS - 1], ipiv[N_CELLS - 1];
  gint i, icol = 0, irow = 0, j, k, l, ll;
  gdouble big, dum, pivinv, temp;
  
#define SWAP(a,b) {temp=(a);(a)=(b);(b)=temp;}

  for (j = 0; j < N_CELLS - 1; j++) 
    ipiv[j] = -1;

  for (i = 0; i < N_CELLS - 1; i++) {
    big = 0.0;
    for (j = 0; j < N_CELLS - 1; j++)
      if (ipiv[j] != 0)
	for (k = 0; k < N_CELLS - 1; k++) {
	  if (ipiv[k] == -1) {
	    if (fabs (mi[j][k]) >= big) {
	      big = fabs (mi[j][k]);
	      irow = j;
	      icol = k;
	    }
	  }
	}
    ipiv[icol]++;
    if (irow != icol)
      for (l = 0; l < N_CELLS - 1; l++) 
	SWAP (mi[irow][l], mi[icol][l]);
    indxr[i] = irow;
    indxc[i] = icol;
    if (mi[icol][icol] == 0.)
      return FALSE;
    pivinv = 1.0/mi[icol][icol];
    mi[icol][icol] = 1.0;
    for (l = 0; l < N_CELLS - 1; l++) mi[icol][l] *= pivinv;
    for (ll = 0; ll < N_CELLS - 1; ll++)
      if (ll != icol) {
	dum = mi[ll][icol];
	mi[ll][icol] = 0.0;
	for (l = 0; l < N_CELLS - 1; l++)
	  mi[ll][l] -= mi[icol][l]*dum;
      }
  }
  for (l = N_CELLS - 1 - 1; l >= 0; l--) {
    if (indxr[l] != indxc[l])
      for (k = 0; k < N_CELLS - 1; k++)
	SWAP (mi[k][indxr[l]], mi[k][indxc[l]]);
  }
#endif /* 3D */
  return TRUE;
}

#if (!FTT_2D)
static void draw_cell (FttCell * cell, gdouble r, gdouble g, gdouble b,
		       const gchar * name)
{
  FttVector p;
  gdouble size = ftt_cell_size (cell)/2.;

  ftt_cell_pos (cell, &p);
  fprintf (stderr,
	   "(geometry \"%s\" = OFF 8 6 12\n"
	   "%g %g %g\n"
	   "%g %g %g\n"
	   "%g %g %g\n"
	   "%g %g %g\n"
	   "%g %g %g\n"
	   "%g %g %g\n"
	   "%g %g %g\n"
	   "%g %g %g\n"
	   "4 3 2 1 0 %g %g %g\n"
	   "4 4 5 6 7 %g %g %g\n"
	   "4 2 3 7 6 %g %g %g\n"
	   "4 0 1 5 4 %g %g %g\n"
	   "4 0 4 7 3 %g %g %g\n"
	   "4 1 2 6 5 %g %g %g\n"
	   ")\n",
	   name,
	   p.x - size, p.y - size, p.z - size,
	   p.x + size, p.y - size, p.z - size,
	   p.x + size, p.y + size, p.z - size,
	   p.x - size, p.y + size, p.z - size,
	   p.x - size, p.y - size, p.z + size,
	   p.x + size, p.y - size, p.z + size,
	   p.x + size, p.y + size, p.z + size,
	   p.x - size, p.y + size, p.z + size,
	   r, g, b,
	   r, g, b,
	   r, g, b,
	   r, g, b,
	   r, g, b,
	   r, g, b);
  gfs_cell_cm (cell, &p);
  size /= 8.;
  fprintf (stderr,
	   "(geometry \"cm %s\" = OFF 8 6 12\n"
	   "%g %g %g\n"
	   "%g %g %g\n"
	   "%g %g %g\n"
	   "%g %g %g\n"
	   "%g %g %g\n"
	   "%g %g %g\n"
	   "%g %g %g\n"
	   "%g %g %g\n"
	   "4 3 2 1 0 %g %g %g\n"
	   "4 4 5 6 7 %g %g %g\n"
	   "4 2 3 7 6 %g %g %g\n"
	   "4 0 1 5 4 %g %g %g\n"
	   "4 0 4 7 3 %g %g %g\n"
	   "4 1 2 6 5 %g %g %g\n"
	   ")\n",
	   name,
	   p.x - size, p.y - size, p.z - size,
	   p.x + size, p.y - size, p.z - size,
	   p.x + size, p.y + size, p.z - size,
	   p.x - size, p.y + size, p.z - size,
	   p.x - size, p.y - size, p.z + size,
	   p.x + size, p.y - size, p.z + size,
	   p.x + size, p.y + size, p.z + size,
	   p.x - size, p.y + size, p.z + size,
	   r, g, b,
	   r, g, b,
	   r, g, b,
	   r, g, b,
	   r, g, b,
	   r, g, b);
}

static void output_error_mesh (FttCell ** n)
{
  draw_cell (n[0], 0., 0., 0., "n0");
  draw_cell (n[1], 0.5, 0., 0., "n1");
  draw_cell (n[2], 0., 0.5, 0., "n2");
  draw_cell (n[3], 0., 0., 0.5, "n3");
  draw_cell (n[4], 1., 1., 1., "n4");
  draw_cell (n[5], 1., 0., 0., "n5");
  draw_cell (n[6], 0., 1., 0., "n6");
  draw_cell (n[7], 0., 0., 1., "n7");
  g_assert_not_reached ();
}
#endif /* 3D */

static gboolean face_bilinear (const FttCellFace * face,
			       FttCell ** n,
			       FttVector * o,
			       void (*cell_pos) (const FttCell *, FttVector *),
			       gint max_level,
			       gdouble m[N_CELLS - 1][N_CELLS - 1])
{
  FttDirection d[3], d1[3];
  guint i;
  gdouble size = ftt_cell_size (face->cell);

  fullest_directions (face, d);
  n[0] = face->cell; n[1] = face->neighbor;
  d1[0] = d[1]; d1[1] = d[0]; d1[2] = d[2];
  if ((n[2] = cell_corner_neighbor1 (n[0], d1, max_level)) == NULL)
    return FALSE;
  d1[1] = FTT_OPPOSITE_DIRECTION (d[0]);
  if ((n[3] = cell_corner_neighbor1 (n[1], d1, max_level)) == NULL)
    return FALSE;
  if (n[3] == n[2]) {
    d1[0] = d[0]; d1[1] = FTT_OPPOSITE_DIRECTION (d[1]);
    if ((n[3] = cell_corner_neighbor1 (n[2], d1, max_level)) == NULL)
      return FALSE;
  }

#if FTT_2D
  for (i = 0; i < 3; i++) {
    FttVector cm;
    guint j;

    (*cell_pos) (n[i + 1], &cm);

    for (j = 0; j < FTT_DIMENSION; j++) {
      (&cm.x)[j] -= (&o->x)[j];
      (&cm.x)[j] /= size;
      g_assert (fabs ((&cm.x)[j]) <= 4.);
    }

    m[i][0] = cm.x;
    m[i][1] = cm.y; 
    m[i][2] = cm.x*cm.y;
  }
  g_assert (inverse (m));
#else /* 3D */
  d1[0] = d[2]; d1[1] = d[0]; d1[2] = d[1];
  if ((n[4] = cell_corner_neighbor1 (n[0], d1, max_level)) == NULL)
    return FALSE;
  d1[1] = FTT_OPPOSITE_DIRECTION (d[0]);
  if ((n[5] = cell_corner_neighbor1 (n[1], d1, max_level)) == NULL)
    return FALSE;
  if (n[5] == n[4]) {
    d1[0] = d[0]; d1[1] = d[1]; d1[2] = FTT_OPPOSITE_DIRECTION (d[2]);
    if ((n[5] = cell_corner_neighbor1 (n[4], d1, max_level)) == NULL)
      return FALSE;
  }
  d1[0] = d[2]; d1[1] = d[0]; d1[2] = FTT_OPPOSITE_DIRECTION (d[1]);
  if ((n[6] = cell_corner_neighbor1 (n[2], d1, max_level)) == NULL)
    return FALSE;
  if (n[6] == n[4]) {
    d1[0] = d[1]; d1[1] = d[0]; d1[2] = FTT_OPPOSITE_DIRECTION (d[2]);
    if ((n[6] = cell_corner_neighbor1 (n[4], d1, max_level)) == NULL)
      return FALSE;
  }
  d1[0] = d[2]; d1[1] = FTT_OPPOSITE_DIRECTION (d[0]); d1[2] = FTT_OPPOSITE_DIRECTION (d[1]);
  if ((n[7] = cell_corner_neighbor1 (n[3], d1, max_level)) == NULL)
    return FALSE;
  if (n[7] == n[4] || n[7] == n[5]) {
    d1[0] = d[1]; d1[2] = FTT_OPPOSITE_DIRECTION (d[2]);
    if ((n[7] = cell_corner_neighbor1 (n[5], d1, max_level)) == NULL)
      return FALSE;
  }
  if (n[7] == n[6]) {
    d1[0] = d[0]; d1[1] = FTT_OPPOSITE_DIRECTION (d[1]); d1[2] = FTT_OPPOSITE_DIRECTION (d[2]);
    if ((n[7] = cell_corner_neighbor1 (n[6], d1, max_level)) == NULL)
      return FALSE;
  }

  for (i = 0; i < 7; i++) {
    FttVector cm;
    guint j;

    for (j = i + 1; j < 7; j++)
      if (n[i + 1] == n[j + 1])
	output_error_mesh (n);

    (*cell_pos) (n[i + 1], &cm);

    for (j = 0; j < FTT_DIMENSION; j++) {
      (&cm.x)[j] -= (&o->x)[j];
      (&cm.x)[j] /= size;
      if (fabs ((&cm.x)[j]) > 4.)
	output_error_mesh (n);
    }

    m[i][0] = cm.x;
    m[i][1] = cm.y; 
    m[i][2] = cm.z;
    m[i][3] = cm.x*cm.y;
    m[i][4] = cm.x*cm.z;
    m[i][5] = cm.y*cm.z;
    m[i][6] = cm.x*cm.y*cm.z;
  }
  if (!inverse (m))
    output_error_mesh (n);
#endif /* 3D */

  return TRUE;
}

/* mixed_face_gradient_stencil needs to be updated whenever 
 * this function is modified
 */
/* grad(v) = -a*v(cell) + b*v(neighbor) + c */
static gboolean mixed_face_gradient (const FttCellFace * face,
				     Gradient * g,
				     guint v,
				     gint max_level)
{
  FttCell * n[N_CELLS];
  gdouble m[N_CELLS - 1][N_CELLS - 1];
  FttVector o, cm;
  FttComponent c = face->d/2;
  gdouble h = ftt_cell_size (face->cell);

  gfs_cell_cm (face->cell, &o);
  if (!face_bilinear (face, n, &o, gfs_cell_cm, max_level, m))
    return FALSE;

  gfs_face_ca (face, &cm);

#if FTT_2D
  {
    FttComponent cp = FTT_ORTHOGONAL_COMPONENT (c);
    gdouble vp;
    
    vp = ((&cm.x)[cp] - (&o.x)[cp])/h;
    g->a = ((m[c][0] + m[2][0]*vp) +
	    (m[c][1] + m[2][1]*vp) +
	    (m[c][2] + m[2][2]*vp));
    g->b = m[c][0] + m[2][0]*vp;
    g->c = ((m[c][1] + m[2][1]*vp)*GFS_VALUEI (n[2], v) +
	    (m[c][2] + m[2][2]*vp)*GFS_VALUEI (n[3], v));
  }
#else /* 3D */
  {
    guint j;

    cm.x = (cm.x - o.x)/h;
    cm.y = (cm.y - o.y)/h;
    cm.z = (cm.z - o.z)/h;
    g->c = 0.;
    
    switch (c) {
    case FTT_X:
      g->a = g->b = m[0][0] + cm.y*m[3][0] + cm.z*m[4][0] + cm.y*cm.z*m[6][0];
      for (j = 1; j < N_CELLS - 1; j++) {
	gdouble a = m[0][j] + cm.y*m[3][j] + cm.z*m[4][j] + cm.y*cm.z*m[6][j];
	g->a += a;
	g->c += a*GFS_VALUEI (n[j+1], v);
      }
      break;
    case FTT_Y:
      g->a = g->b = m[1][0] + cm.x*m[3][0] + cm.z*m[5][0] + cm.x*cm.z*m[6][0];
      for (j = 1; j < N_CELLS - 1; j++) {
	gdouble a = m[1][j] + cm.x*m[3][j] + cm.z*m[5][j] + cm.x*cm.z*m[6][j];
	g->a += a;
	g->c += a*GFS_VALUEI (n[j+1], v);
      }
      break;
    case FTT_Z:
      g->a = g->b = m[2][0] + cm.x*m[4][0] + cm.y*m[5][0] + cm.x*cm.y*m[6][0];
      for (j = 1; j < N_CELLS - 1; j++) {
	gdouble a = m[2][j] + cm.x*m[4][j] + cm.y*m[5][j] + cm.x*cm.y*m[6][j];
	g->a += a;
	g->c += a*GFS_VALUEI (n[j+1], v);
      }
      break;
    default:
      g_assert_not_reached ();
    }
  }
#endif /* 3D */

  if (!FTT_FACE_DIRECT (face)) {
    g->a = - g->a;
    g->b = - g->b;
    g->c = - g->c;
  }

  return TRUE;
}

/* face_cm_gradient_stencil needs to be updated whenever 
 * this function is modified
 */
static void face_cm_gradient (const FttCellFace * face,
			      GfsGradient * g,
			      guint v,
			      gint max_level,
			      gboolean weighted)
{
  guint level;
  Gradient gcf;
  gdouble w = weighted ? GFS_STATE (face->cell)->f[face->d].v : 1.;

  g->a = g->b = 0.;
  
  if (face->neighbor == NULL || w == 0.)
    return;

  level = ftt_cell_level (face->cell);
  if (ftt_cell_level (face->neighbor) < level) {
    /* neighbor is at a shallower level */
    if (GFS_IS_MIXED (face->cell) || GFS_IS_MIXED (face->neighbor)) {
      if (!mixed_face_gradient (face, &gcf, v, max_level))
	gcf = gradient_fine_coarse (face, v);
    }
    else
      gcf = gradient_fine_coarse (face, v);
    g->a = w*gcf.a;
    g->b = w*(gcf.b*GFS_VALUEI (face->neighbor, v) + gcf.c);
  }
  else {
    if (level == max_level || FTT_CELL_IS_LEAF (face->neighbor)) {
      /* neighbor is at the same level */
      if (!GFS_IS_MIXED (face->cell) && !GFS_IS_MIXED (face->neighbor)) {
	g->a = w;
	g->b = w*GFS_VALUEI (face->neighbor, v);
      }
      else if (mixed_face_gradient (face, &gcf, v, max_level)) {
	g->a = w*gcf.a;
	g->b = w*(gcf.b*GFS_VALUEI (face->neighbor, v) + gcf.c);
      }
      else {
	g->a = w;
	g->b = w*GFS_VALUEI (face->neighbor, v);
      }
    }
    else {
      /* neighbor is at a deeper level */
      FttCellChildren children;
      FttCellFace f;
      guint i, n;
      
      f.d = FTT_OPPOSITE_DIRECTION (face->d);
      n = ftt_cell_children_direction (face->neighbor, f.d, &children);
      f.neighbor = face->cell;
      for (i = 0; i < n; i++) 
	if ((f.cell = children.c[i])) {
	  w = weighted ? GFS_STATE (f.cell)->f[f.d].v : 1.;
	  if (GFS_IS_MIXED (f.cell) || GFS_IS_MIXED (f.neighbor)) {
	    if (!mixed_face_gradient (&f, &gcf, v, max_level))
	      gcf = gradient_fine_coarse (&f, v);
	  }
	  else
	    gcf = gradient_fine_coarse (&f, v);
	  g->a += w*gcf.b;
	  g->b += w*(gcf.a*GFS_VALUEI (f.cell, v) - gcf.c);
	}
#if !FTT_2D
      g->a /= n/2.;
      g->b /= n/2.;
#endif
    }
  }
}

/**
 * gfs_face_cm_gradient:
 * @face: a #FttCellFace.
 * @g: the #GfsGradient.
 * @v: a #GfsVariable index.
 * @max_level: the maximum cell level to consider (-1 means no restriction).
 *
 * Set the value of @g as the gradient of variable @v on the
 * @face. Variable @v is defined at the center of mass of its
 * cell. Linear interpolation is used to evaluate the gradient in the
 * vicinity of cut cells.
 */
void gfs_face_cm_gradient (const FttCellFace * face,
			   GfsGradient * g,
			   guint v,
			   gint max_level)
{
  g_return_if_fail (face != NULL);
  g_return_if_fail (g != NULL);

  face_cm_gradient (face, g, v, max_level, FALSE);
}

/**
 * gfs_face_cm_weighted_gradient:
 * @face: a #FttCellFace.
 * @g: the #GfsGradient.
 * @v: a #GfsVariable index.
 * @max_level: the maximum cell level to consider (-1 means no restriction).
 *
 * Set the value of @g as the gradient of variable @v on the @face
 * weighted by the value of the @v field of the face state vector of
 * the corresponding cell. Variable @v is defined at the center of
 * mass of its cell. Linear interpolation is used to evaluate the
 * gradient in the vicinity of cut cells.
 */
/* gfs_face_cm_weighted_gradient_stencil needs to be updated whenever 
 * this function is modified
 */
void gfs_face_cm_weighted_gradient (const FttCellFace * face,
				    GfsGradient * g,
				    guint v,
				    gint max_level)
{
  g_return_if_fail (face != NULL);
  g_return_if_fail (g != NULL);

  face_cm_gradient (face, g, v, max_level, TRUE);
}

static gboolean cell_bilinear (FttCell * cell,
			       FttCell ** n,
			       FttVector * o,
			       void (*cell_pos) (const FttCell *, FttVector *),
			       gint max_level,
			       gdouble m[N_CELLS - 1][N_CELLS - 1])
{
  GfsSolidVector * s = GFS_STATE (cell)->solid;  
  FttCellFace f;
  FttDirection d[FTT_DIMENSION];
  FttComponent c;

  if ((s->s[FTT_RIGHT] == 0. && s->s[FTT_LEFT] == 0.) ||
      (s->s[FTT_RIGHT] == 1. && s->s[FTT_LEFT] == 1.))
    return FALSE;

  for (c = 0; c < FTT_DIMENSION; c++)
    d[c] = s->s[2*c] > s->s[2*c + 1] ? 2*c : 2*c + 1;
  f.cell = cell;
  f.d = d[0];
  f.neighbor = cell_corner_neighbor1 (cell, d, max_level);

  return face_bilinear (&f, n, o, cell_pos, max_level, m);
}

/**
 * gfs_cell_dirichlet_gradient:
 * @cell: a #FttCell.
 * @v: a #GfsVariable index.
 * @max_level: the maximum cell level to consider (-1 means no restriction).
 * @v0: the Dirichlet value on the boundary.
 * @grad: a #FttVector.
 *
 * Fills @grad with components of the gradient of variable @v
 * interpolated at the center of area of the solid boundary contained
 * in @cell. The gradient is scaled by the size of the cell.
 */
/* gfs_cell_dirichlet_gradient_stencil needs to be updated whenever 
   this function is modified  */
void gfs_cell_dirichlet_gradient (FttCell * cell,
				  guint v,
				  gint max_level,
				  gdouble v0,
				  FttVector * grad)
{
  g_return_if_fail (cell != NULL);
  g_return_if_fail (grad != NULL);

  if (!GFS_IS_MIXED (cell))
    return;
  else {
    FttCell * n[N_CELLS];
    gdouble m[N_CELLS - 1][N_CELLS - 1];
    guint i, c;

    grad->x = grad->y = grad->z = 0.;
    if (!cell_bilinear (cell, n, &GFS_STATE (cell)->solid->ca, 
			gfs_cell_cm, max_level, m))
      return;

    for (i = 0; i < N_CELLS - 1; i++)
      for (c = 0; c < FTT_DIMENSION; c++)
	(&grad->x)[c] += m[c][i]*(GFS_VALUEI (n[i + 1], v) - v0);
  }
}

/**
 * gfs_mixed_cell_gradient:
 * @cell: a mixed #FttCell.
 * @v: a #GfsVariable.
 * @g: the gradient.
 *
 * Fills @g with the components of the gradient of @v at the center of
 * mass of @cell.
 *
 * The gradient is normalized by the size of the cell.
 */
void gfs_mixed_cell_gradient (FttCell * cell,
			      GfsVariable * v,
			      FttVector * g)
{
  FttCell * n[N_CELLS];
  gdouble m[N_CELLS - 1][N_CELLS - 1];
  gdouble v0, h;
  FttVector * o, cm;
  guint i;

  g_return_if_fail (cell != NULL);
  g_return_if_fail (GFS_IS_MIXED (cell));
  g_return_if_fail (v != NULL);
  g_return_if_fail (g != NULL);

  g->x = g->y = g->z = 0.;

  o = &GFS_STATE (cell)->solid->cm;
  v0 = GFS_VALUE (cell, v);
  cm = *o;

  if (v->surface_bc) {
    (* GFS_SURFACE_GENERIC_BC_CLASS (GTS_OBJECT (v->surface_bc)->klass)->bc) (cell, v->surface_bc);
    if (((cell)->flags & GFS_FLAG_DIRICHLET) != 0) {
      o = &GFS_STATE (cell)->solid->ca;
      v0 = GFS_STATE (cell)->solid->fv;
    }
  }
  g_assert (cell_bilinear (cell, n, o, gfs_cell_cm, -1, m));
  
  h = ftt_cell_size (cell);
  cm.x = (cm.x - o->x)/h;
  cm.y = (cm.y - o->y)/h;
  cm.z = (cm.z - o->z)/h;
  for (i = 0; i < N_CELLS - 1; i++) {
    gdouble val = GFS_VALUE (n[i + 1], v) - v0;
#if FTT_2D
    g->x += (m[0][i] + m[2][i]*cm.y)*val;
    g->y += (m[1][i] + m[2][i]*cm.x)*val;
#else /* 3D */
    g->x += (m[0][i] + m[3][i]*cm.y + m[4][i]*cm.z + m[6][i]*cm.y*cm.z)*val;
    g->y += (m[1][i] + m[3][i]*cm.x + m[5][i]*cm.z + m[6][i]*cm.x*cm.z)*val;
    g->z += (m[2][i] + m[4][i]*cm.x + m[5][i]*cm.y + m[6][i]*cm.x*cm.y)*val;
#endif /* 3D */
  }
}

/**
 * gfs_mixed_cell_interpolate:
 * @cell: a mixed #FttCell.
 * @p: a #FttVector.
 * @v: a #GfsVariable.
 *
 * Returns: the value of variable @v interpolated at position @p within @cell.
 */
gdouble gfs_mixed_cell_interpolate (FttCell * cell,
				    FttVector p,
				    GfsVariable * v)
{
  FttCell * n[N_CELLS];
  gdouble m[N_CELLS - 1][N_CELLS - 1], a[N_CELLS - 1];
  gdouble v0, h;
  FttVector * o;
  guint i, j;

  g_return_val_if_fail (cell != NULL, 0.);
  g_return_val_if_fail (GFS_IS_MIXED (cell), 0.);
  g_return_val_if_fail (v != NULL, 0.);

  o = &GFS_STATE (cell)->solid->cm;
  v0 = GFS_VALUE (cell, v);

  if (v->surface_bc) {
    (* GFS_SURFACE_GENERIC_BC_CLASS (GTS_OBJECT (v->surface_bc)->klass)->bc) (cell, v->surface_bc);
    if (((cell)->flags & GFS_FLAG_DIRICHLET) != 0) {
      o = &GFS_STATE (cell)->solid->ca;
      v0 = GFS_STATE (cell)->solid->fv;
    }
  }
  g_assert (cell_bilinear (cell, n, o, gfs_cell_cm, -1, m));

  for (i = 0; i < N_CELLS - 1; i++) {
    a[i] = 0.;
    for (j = 0; j < N_CELLS - 1; j++)
      a[i] += m[i][j]*(GFS_VALUE (n[j + 1], v) - v0);
  }
  
  h = ftt_cell_size (cell);
  p.x = (p.x - o->x)/h;
  p.y = (p.y - o->y)/h;
#if FTT_2D
  return a[0]*p.x + a[1]*p.y + a[2]*p.x*p.y + v0;
#else /* 3D */
  p.z = (p.z - o->z)/h;
  return (a[0]*p.x + a[1]*p.y + a[2]*p.z + 
	  a[3]*p.x*p.y + a[4]*p.x*p.z + a[5]*p.y*p.z + 
	  a[6]*p.x*p.y*p.z + v0);
#endif /* 3D */
}

/**
 * gfs_cell_dirichlet_gradient_flux:
 * @cell: a #FttCell.
 * @v: a #GfsVariable index.
 * @max_level: the maximum cell level to consider (-1 means no restriction).
 * @v0: the Dirichlet value on the boundary.
 *
 * Returns: the flux of the gradient of variable @v through the solid
 * boundary contained in @cell.
 */
/* gfs_cell_dirichlet_gradient_flux_stencil needs to be updated whenever 
   this function is modified  */
gdouble gfs_cell_dirichlet_gradient_flux (FttCell * cell,
					  guint v,
					  gint max_level,
					  gdouble v0)
{
  g_return_val_if_fail (cell != NULL, 0.);

  if (!GFS_IS_MIXED (cell))
    return 0.;
  else {
    GfsSolidVector * s = GFS_STATE (cell)->solid;
    FttVector g;    
    gfs_cell_dirichlet_gradient (cell, v, max_level, v0, &g);
    return g.x*s->v.x + g.y*s->v.y + g.z*s->v.z;
  }
}

static void gfs_cell_dirichlet_gradient_stencil (FttCell * cell,
						 gint max_level,
						 gdouble v0,
						 FttVector * grad,
						 GfsLinearProblem * lp,
						 GfsStencil * stencil,
						 FttVector * weight)
{
  if (GFS_IS_MIXED (cell)) {
    FttCell * n[N_CELLS];
    gdouble m[N_CELLS - 1][N_CELLS - 1];
    guint i, c;

    grad->x = grad->y = grad->z = 0.;
    if (!cell_bilinear (cell, n, &GFS_STATE (cell)->solid->ca, 
			gfs_cell_cm, max_level, m))
      return;

    for (i = 0; i < N_CELLS - 1; i++)
      for (c = 0; c < FTT_DIMENSION; c++) {
	gfs_stencil_add_element (stencil, n[i + 1], lp, m[c][i]*(&weight->x)[c]);
	(&grad->x)[c] += -m[c][i]*v0; /* Remaining Dirichlet contribution */
      }
  }
}

/**
 * gfs_cell_dirichlet_gradient_flux_stencil:
 * @cell: a #FttCell.
 * @max_level: the maximum cell level to consider (-1 means no restriction).
 * @v0: the Dirichlet value on the boundary.
 * @lp: the #GfsLinearProblem.
 * @stencil: a GfsStencil.
 *
 * Returns the stencil accounting for the flux of the gradient of
 * a given variable through the solid boundary contained in @cell.
 * Returns the stencil equivalent to the action of
 * gfs_cell_dirichlet_gradient_flux().
 *
 * Returns: the flux of the gradient of variable @v through the solid
 * boundary contained in @cell.
 */
gdouble gfs_cell_dirichlet_gradient_flux_stencil (FttCell * cell,
						  gint max_level,
						  gdouble v0,
						  GfsLinearProblem * lp,
						  GfsStencil * stencil)
{
  g_return_val_if_fail (cell != NULL, 0.);
  g_return_val_if_fail (lp != NULL, 0.);
  g_return_val_if_fail (stencil != NULL, 0.);

  if (!GFS_IS_MIXED (cell))
    return 0.;
  else {
    GfsSolidVector * s = GFS_STATE (cell)->solid;
    FttVector g;
    gfs_cell_dirichlet_gradient_stencil (cell, max_level, v0, &g, lp, stencil, &s->v);
    return g.x*s->v.x + g.y*s->v.y + g.z*s->v.z;
  }
}

/**
 * gfs_cell_dirichlet_value:
 * @cell: a #FttCell.
 * @v: a #GfsVariable.
 * @max_level: the maximum cell level to consider (-1 means no restriction).
 *
 * Returns: the value of variable @v interpolated at the center of
 * area of the solid boundary contained in @cell.
 */
gdouble gfs_cell_dirichlet_value (FttCell * cell,
				  GfsVariable * v,
				  gint max_level)
{
  g_return_val_if_fail (cell != NULL, 0.);
  g_return_val_if_fail (v != NULL, 0.);

  if (!GFS_IS_MIXED (cell))
    return 0.;
  else {
    FttCell * n[N_CELLS];
    FttVector p;
    gdouble m[N_CELLS - 1][N_CELLS - 1], a[N_CELLS - 1];
    GfsSolidVector * s = GFS_STATE (cell)->solid;
    gdouble v0, size = ftt_cell_size (cell);
    void (* cell_pos) (const FttCell *, FttVector *) = v->centered ? ftt_cell_pos : gfs_cell_cm;
    guint i, j;

    (*cell_pos) (cell, &p);
    if (!cell_bilinear (cell, n, &p, cell_pos, max_level, m))
      return 0.;

    v0 = GFS_VALUE (cell, v);
    for (i = 0; i < N_CELLS - 1; i++) {
      a[i] = 0.;
      for (j = 0; j < N_CELLS - 1; j++)
	a[i] += m[i][j]*(GFS_VALUE (n[j + 1], v) - v0);
    }
    p.x = (s->ca.x - p.x)/size;
    p.y = (s->ca.y - p.y)/size;
#if FTT_2D
    return (a[0]*p.x + a[1]*p.y + a[2]*p.x*p.y + v0);
#else /* 3D */
    p.z = (s->ca.z - p.z)/size;
    return (a[0]*p.x + a[1]*p.y + a[2]*p.z + 
	    a[3]*p.x*p.y + a[4]*p.x*p.z + a[5]*p.y*p.z + 
	    a[6]*p.x*p.y*p.z + v0);
#endif /* 3D */
  }
}

/**
 * gfs_shear_strain_rate_tensor:
 * @cell: a #FttCell.
 * @u: the velocity.
 * @t: the shear strain rate tensor t[i][j] = (d_iu_j+d_ju_i)/2.
 *
 * Fills @t with the shear strain rate tensor at the center of mass of
 * @cell, normalised by the size of the cell.
 */
void gfs_shear_strain_rate_tensor (FttCell * cell, 
				   GfsVariable ** u,
				   gdouble t[FTT_DIMENSION][FTT_DIMENSION])
{
  guint i, j;
  FttVector g[FTT_DIMENSION];

  g_return_if_fail (cell != NULL);
  g_return_if_fail (u != NULL);

  for (i = 0; i < FTT_DIMENSION; i++)
    if (GFS_IS_MIXED (cell))
      gfs_mixed_cell_gradient (cell, u[i], &g[i]);
    else
      for (j = 0; j < FTT_DIMENSION; j++)
	(&g[i].x)[j] = gfs_center_gradient (cell, j, u[i]->i);			     

  for (i = 0; i < FTT_DIMENSION; i++) {
    t[i][i] = (&g[i].x)[i];
    for (j = i + 1; j < FTT_DIMENSION; j++)
      t[i][j] = ((&g[j].x)[i] + (&g[i].x)[j])/2.;
  }
  for (i = 0; i < FTT_DIMENSION; i++)
    for (j = 0; j < i; j++)
      t[i][j] = t[j][i];
}

/**
 * gfs_2nd_principal_invariant:
 * @cell: a #FttCell.
 * @u: the velocity.
 *
 * Returns: the second principal invariant of the shear strain rate
 * tensor of @cell: sqrt(D:D).
 */
gdouble gfs_2nd_principal_invariant (FttCell * cell, GfsVariable ** u)
{
  gdouble t[FTT_DIMENSION][FTT_DIMENSION];
  gdouble D = 0.;
  guint i, j;

  g_return_val_if_fail (cell != NULL, 0.);
  g_return_val_if_fail (u != NULL, 0.);

  gfs_shear_strain_rate_tensor (cell, u, t);
  for (i = 0; i < FTT_DIMENSION; i++)
    for (j = 0; j < FTT_DIMENSION; j++)
      D += t[i][j]*t[i][j];
  return sqrt (D);
}

/**
 * gfs_get_from_below_intensive:
 * @cell: a #FttCell.
 * @v: a #GfsVariable to "get from below".
 *
 * Sets the value of the "intensive" variable @v of @cell by taking
 * the volume weighted average of the values of its children cells.
 *
 * This functions fails if @cell is a leaf of the cell tree.
 */
void gfs_get_from_below_intensive (FttCell * cell, const GfsVariable * v)
{
  gdouble val = 0., sa = 0.;
  guint i;
  FttCellChildren child;

  g_return_if_fail (cell != NULL);
  g_return_if_fail (!FTT_CELL_IS_LEAF (cell));
  g_return_if_fail (v != NULL);

  ftt_cell_children (cell, &child);
  for (i = 0; i < FTT_CELLS; i++)
    if (child.c[i] && GFS_HAS_DATA (child.c[i], v)) {
      gdouble a = gfs_domain_cell_fraction (v->domain, child.c[i]);
      val += GFS_VALUE (child.c[i], v)*a;
      sa += a;
    }
  if (sa > 0.)
    GFS_VALUE (cell, v) = val/sa;
  else
    GFS_VALUE (cell, v) = GFS_NODATA;
}

/**
 * gfs_cell_coarse_fine:
 * @parent: a #FttCell.
 * @v: a #GfsVariable.
 *
 * Initializes @v on the children of @parent using interpolation.
 *
 * First-order interpolation (straight injection) is used for boundary
 * cells and second-order interpolation for the other cells.  
 */
void gfs_cell_coarse_fine (FttCell * parent, GfsVariable * v)
{
  FttCellChildren child;
  guint n;

  g_return_if_fail (parent != NULL);
  g_return_if_fail (!FTT_CELL_IS_LEAF (parent));
  g_return_if_fail (v != NULL);

  ftt_cell_children (parent, &child);
  for (n = 0; n < FTT_CELLS; n++)
    if (child.c[n])
      GFS_VALUE (child.c[n], v) = GFS_VALUE (parent, v);

  if (!GFS_CELL_IS_BOUNDARY (parent) && GFS_HAS_DATA (parent, v)) {
    FttVector g;
    FttComponent c;
    
    for (c = 0; c < FTT_DIMENSION; c++)
      (&g.x)[c] = gfs_center_van_leer_gradient (parent, c, v->i);

    if (v->domain->cell_metric) {
      gdouble a[FTT_CELLS], sa = 0.;
      for (n = 0; n < FTT_CELLS; n++) {
	a[n] = (* v->domain->cell_metric) (v->domain, child.c[n]);
	sa += a[n];
      }
      g_assert (sa > 0.);
      sa *= 2.;
#if FTT_2D
      double gx1 = g.x*(a[0] + a[2])/sa, gx2 = - g.x*(a[1] + a[3])/sa;
      double gy1 = g.y*(a[2] + a[3])/sa, gy2 = - g.y*(a[0] + a[1])/sa;
      GFS_VALUE (child.c[0], v) += gx2 + gy1;
      GFS_VALUE (child.c[1], v) += gx1 + gy1;
      GFS_VALUE (child.c[2], v) += gx2 + gy2;
      GFS_VALUE (child.c[3], v) += gx1 + gy2;
#else /* 3D */
      double gx1 = g.x*(a[0] + a[2])/sa, gx2 = - g.x*(a[1] + a[3])/sa;
      double gx3 = g.x*(a[4] + a[6])/sa, gx4 = - g.x*(a[5] + a[7])/sa;
      double gy1 = g.y*(a[2] + a[3])/sa, gy2 = - g.y*(a[0] + a[1])/sa;
      double gy3 = g.y*(a[6] + a[7])/sa, gy4 = - g.y*(a[4] + a[5])/sa;
      double gz1 = g.z*(a[2] + a[6])/sa, gz2 = - g.z*(a[0] + a[4])/sa;
      double gz3 = g.z*(a[3] + a[7])/sa, gz4 = - g.z*(a[1] + a[5])/sa;
      GFS_VALUE (child.c[0], v) += gx2 + gy1 + gz1;
      GFS_VALUE (child.c[1], v) += gx1 + gy1 + gz1;
      GFS_VALUE (child.c[2], v) += gx2 + gy2 + gz1;
      GFS_VALUE (child.c[3], v) += gx1 + gy2 + gz1;
      GFS_VALUE (child.c[4], v) += gx2 + gy1 + gz2;
      GFS_VALUE (child.c[5], v) += gx1 + gy1 + gz2;
      GFS_VALUE (child.c[6], v) += gx2 + gy2 + gz2;
      GFS_VALUE (child.c[7], v) += gx1 + gy2 + gz2;
#endif /* 3D */
    }
    else
      for (n = 0; n < FTT_CELLS; n++) 
	if (child.c[n]) {
	  FttVector p;
	  
	  ftt_cell_relative_pos (child.c[n], &p);
	  for (c = 0; c < FTT_DIMENSION; c++)
	    GFS_VALUE (child.c[n], v) += (&p.x)[c]*(&g.x)[c];
	}
  }
}

/**
 * gfs_cell_cleanup:
 * @cell: a #FttCell.
 * @domain: a #GfsDomain.
 *
 * Frees the memory allocated for extra data associated with @cell.
 *
 * This function must be used as "cleanup function" when using
 * ftt_cell_destroy().
 */
void gfs_cell_cleanup (FttCell * cell, GfsDomain * domain)
{
  g_return_if_fail (cell != NULL);
  g_return_if_fail (domain != NULL);

  if (cell->data) {
    GSList * i = domain->variables;
    while (i) {
      GfsVariable * v = i->data;
      if (v->cleanup)
	(* v->cleanup) (cell, v);
      i = i->next;
    }
    if (GFS_STATE (cell)->solid) {
      g_free (GFS_STATE (cell)->solid);
      GFS_STATE (cell)->solid = NULL;
    }    
  }
  g_free (cell->data);
  cell->data = NULL;
}

/**
 * gfs_cell_reset:
 * @cell: a #FttCell.
 * @v: a #GfsVariable to reset.
 *
 * Sets the value of the variable @v of @cell to zero.
 */
void gfs_cell_reset (FttCell * cell, GfsVariable * v)
{
  g_return_if_fail (cell != NULL);
  g_return_if_fail (v != NULL);

  GFS_VALUE (cell, v) = 0.;
}

/**
 * gfs_face_reset:
 * @face: a #FttCellFace.
 * @v: a #GfsVariable to reset.
 *
 * Sets the value of the variable @v of @face to zero (on both sides).
 */
void gfs_face_reset (FttCellFace * face, GfsVariable * v)
{
  g_return_if_fail (face != NULL);
  g_return_if_fail (v != NULL);

  GFS_VALUE (face->cell, v) = GFS_VALUE (face->neighbor, v) = 0.;
}

static void add_stats (const FttCell * cell, gpointer * data)
{
  GtsRange * s = data[0];
  gdouble v = GFS_VALUE (cell, GFS_VARIABLE (data[1]));

  if (v != GFS_NODATA)
    gts_range_add_value (s, v);
}

/**
 * gfs_stats_variable:
 * @root: the root #FttCell of the tree to obtain statistics from.
 * @v: the variable to consider for statistics.
 * @flags: which types of cells are to be visited.
 * @max_depth: maximum depth of the traversal.
 *
 * Traverses the cell tree defined by @root using ftt_cell_traverse()
 * and gathers statistics about variable @v.
 *
 * Returns: a #GtsRange containing the statistics about @v.
 */
GtsRange gfs_stats_variable (FttCell * root,
			     GfsVariable * v,
			     FttTraverseFlags flags,
			     gint max_depth)
{
  GtsRange s;
  gpointer data[2];

  g_return_val_if_fail (root != NULL, s);
  g_return_val_if_fail (v != NULL, s);
  
  gts_range_init (&s);
  data[0] = &s;
  data[1] = v;
  ftt_cell_traverse (root, FTT_PRE_ORDER, flags, max_depth, 
		     (FttCellTraverseFunc) add_stats, data);
  gts_range_update (&s);

  return s;
}

static void add_norm (const FttCell * cell, gpointer * data)
{
  GfsNorm * n = data[0];
  GfsVariable * v = data[1];

  gfs_norm_add (n, GFS_VALUE (cell, v), gfs_cell_volume (cell, v->domain));
}

/**
 * gfs_norm_variable:
 * @root: the root #FttCell of the tree to obtain norm from.
 * @v: the variable to consider for norm statistics.
 * @flags: which types of cells are to be visited.
 * @max_depth: maximum depth of the traversal.
 *
 * Traverses the cell tree defined by @root using ftt_cell_traverse()
 * and gathers norm statistics about variable @v.
 *
 * Returns: a #GfsNorm containing the norm statistics about @v.
 */
GfsNorm gfs_norm_variable (FttCell * root,
			   GfsVariable * v,
			   FttTraverseFlags flags,
			   gint max_depth)
{
  GfsNorm n;
  gpointer data[2];

  g_return_val_if_fail (root != NULL, n);
  g_return_val_if_fail (v != NULL, n);
  
  gfs_norm_init (&n);
  data[0] = &n;
  data[1] = v;
  ftt_cell_traverse (root, FTT_PRE_ORDER, flags, max_depth, 
		     (FttCellTraverseFunc) add_norm, data);
  gfs_norm_update (&n);

  return n;
}

/**
 * gfs_norm_init:
 * @n: a #GfsNorm.
 *
 * Initializes a #GfsNorm.
 */
void gfs_norm_init (GfsNorm * n)
{
  g_return_if_fail (n != NULL);

  n->bias = n->first = n->second = 0.;
  n->infty = - G_MAXDOUBLE;
  n->w = 0.;
}

/**
 * gfs_norm_reset:
 * @n: a #GfsNorm.
 *
 * Sets all the fields of @n to 0.
 */
void gfs_norm_reset (GfsNorm * n)
{
  g_return_if_fail (n != NULL);

  n->bias = n->first = n->second = 0.;
  n->infty = 0.;
  n->w = 0.;
}

/**
 * gfs_norm_add:
 * @n: a #GfsNorm.
 * @val: a value to add to @n.
 * @weight: weight of @val.
 *
 * Adds @val to @n.
 */
void gfs_norm_add (GfsNorm * n, gdouble val, gdouble weight)
{
  g_return_if_fail (n != NULL);

  if (val != GFS_NODATA) {
    n->bias += weight*val;
    val = fabs (val);
    if (weight != 0. && val > n->infty)
      n->infty = val;
    n->first += weight*val;
    n->second += weight*val*val;
    n->w += weight;
  }
}

/**
 * gfs_norm_update:
 * @n: a #GfsNorm.
 * 
 * Updates the fields of @n.
 */
void gfs_norm_update (GfsNorm * n)
{
  g_return_if_fail (n != NULL);

  if (n->w > 0.0) {
    n->bias /= n->w;
    n->first /= n->w;
    n->second = sqrt (n->second/n->w);
  }
  else
    n->infty = 0.0;
}

/**
 * gfs_face_interpolated_value:
 * @face: a #FttFace.
 * @v: a #GfsVariable index.
 *
 * Computes the value of variable @v on the @face using second-order
 * interpolation from the cell-centered values.
 *
 * Note that this function cannot be called for coarse -> fine faces
 * (use gfs_face_interpolated_value_generic() instead).
 *
 * Returns: the value of variable @v on the face.  
 */
gdouble gfs_face_interpolated_value (const FttCellFace * face,
				     guint v)
{
  gdouble x1 = 1., v1;
#if 1
  g_return_val_if_fail (face != NULL, 0.);

  if (face->neighbor) {
    g_assert (FTT_CELL_IS_LEAF (face->neighbor) || 
	      ftt_cell_level (face->neighbor) < ftt_cell_level (face->cell));
    v1 = gfs_neighbor_value (face, v, &x1);
    return ((x1 - 0.5)*GFS_VALUEI (face->cell, v) + 0.5*v1)/x1;
  }
  else
    return GFS_VALUEI (face->cell, v);
#else
  gdouble v0;
  FttCellFace f2;

  g_return_val_if_fail (face != NULL, 0.);

  v0 = GFS_VALUEI (face->cell, v);
  v1 = gfs_neighbor_value (face, v, &x1);
  f2 = gfs_cell_face (face->cell, FTT_OPPOSITE_DIRECTION (face->d));
  if (f2.neighbor) {
    gdouble x2 = 1.;
    gdouble v2 = gfs_neighbor_value (&f2, v, &x2);

    return v0 + (x2*(v1 - v0)*(1. + 2.*x2) - x1*(v0 - v2)*(1. - 2.*x1))
      /(4.*x1*x2*(x1 + x2));
  }
  else
    return ((x1 - 0.5)*v0 + 0.5*v1)/x1;
#endif
}

/**
 * gfs_face_interpolated_value_generic:
 * @face: a #FttFace.
 * @v: a #GfsVariable.
 *
 * Same as gfs_face_interpolated_value() but also works for coarse ->
 * fine faces.
 *
 * Returns: the value of variable @v on the face.  
 */
gdouble gfs_face_interpolated_value_generic (const FttCellFace * face, const GfsVariable * v)
{  
  g_return_val_if_fail (face != NULL, 0.);
  g_return_val_if_fail (v != NULL, 0.);
  
  if (!face->neighbor || FTT_CELL_IS_LEAF (face->neighbor) ||
      ftt_cell_level (face->neighbor) < ftt_cell_level (face->cell))
    return gfs_face_interpolated_value (face, v->i);
  else {
    /* finer neighbor */
    FttCellFace f = { NULL, face->cell, FTT_OPPOSITE_DIRECTION (face->d) };
    FttCellChildren child;
    int i, n = ftt_cell_children_direction (face->neighbor, f.d, &child);
    gdouble avg = 0.;
    for (i = 0; i < n; i++)
      if (child.c[i]) {
	f.cell = child.c[i];
	avg += gfs_face_interpolated_value (&f, v->i)*gfs_domain_face_fraction (v->domain, &f);
      }
    return avg == 0. ? 0. : avg/(gfs_domain_face_fraction (v->domain, face)*n);
  }
}

/**
 * gfs_face_weighted_interpolated_value:
 * @face: a #FttFace.
 * @v: a #GfsVariable index.
 *
 * Computes the value of variable @v on the @face weighted by the
 * value of the @v field of the face state vector using interpolation
 * from the cell-centered values. The value returned is second order
 * accurate in space and conservative, in the sense that values at a
 * coarse/fine cell boundary are consistent.
 *
 * Returns: the weighted value of variable @v on the face.  
 */
gdouble gfs_face_weighted_interpolated_value (const FttCellFace * face,
					      guint v)
{
  g_return_val_if_fail (face != NULL, 0.);

  if (face->neighbor) {
    if (FTT_CELL_IS_LEAF (face->neighbor)) {
      gdouble w = GFS_STATE (face->cell)->f[face->d].v, x1 = 1., v1;
      v1 = gfs_neighbor_value (face, v, &x1);
      return w*((x1 - 0.5)*GFS_VALUEI (face->cell, v) + 0.5*v1)/x1;
    }
    else {
      /* neighbor is at a deeper level */
      FttCellChildren children;
      FttCellFace f;
      gdouble val = 0.;
      guint i, n;
      
      f.d = FTT_OPPOSITE_DIRECTION (face->d);
      n = ftt_cell_children_direction (face->neighbor, f.d, &children);
      f.neighbor = face->cell;
      for (i = 0; i < n; i++)
	if ((f.cell = children.c[i])) {
	  gdouble w = GFS_STATE (f.cell)->f[f.d].v, x1 = 1., v1;
	  v1 = gfs_neighbor_value (&f, v, &x1);
	  val += w*v1;
	}
      return val/n;
    }
  }
  else
    return GFS_STATE (face->cell)->f[face->d].v*GFS_VALUEI (face->cell, v);
}

/**
 * gfs_normal_divergence:
 * @cell: a #FttCell.
 * @v: a #GfsVariable.
 *
 * Adds to variable @v of @cell the integral of the divergence
 * of the (MAC) velocity field in this cell.  
 */
void gfs_normal_divergence (FttCell * cell,
			    GfsVariable * v)
{
  FttCellFace face;
  gdouble div = 0.;

  g_return_if_fail (cell != NULL);
  g_return_if_fail (v != NULL);

  face.cell = cell;
  for (face.d = 0; face.d < FTT_NEIGHBORS; face.d++)
    div += (FTT_FACE_DIRECT (&face) ? 1. : -1.)*
      GFS_STATE (cell)->f[face.d].un*gfs_domain_face_fraction (v->domain, &face);
  GFS_VALUE (cell, v) = div*ftt_cell_size (cell);
}

/**
 * gfs_normal_divergence_2D:
 * @cell: a #FttCell.
 * @v: a #GfsVariable.
 *
 * Fills variable @v of @cell with the integral of the 2D
 * divergence of the (MAC) velocity field in this cell.
 */
void gfs_normal_divergence_2D (FttCell * cell,
			       GfsVariable * v)
{
  FttCellFace face;
  gdouble div = 0.;

  g_return_if_fail (cell != NULL);
  g_return_if_fail (v != NULL);
  
  face.cell = cell;
  for (face.d = 0; face.d < 4; face.d++)
    div += (FTT_FACE_DIRECT (&face) ? 1. : -1.)*
      GFS_STATE (cell)->f[face.d].un*gfs_domain_face_fraction (v->domain, &face);
  GFS_VALUE (cell, v) = div*ftt_cell_size (cell);
}

/**
 * gfs_divergence:
 * @cell: a #FttCell.
 * @v: the components of the vector.
 *
 * Returns: the divergence of the (centered) vector field @v in @cell.
 */
gdouble gfs_divergence (FttCell * cell,
			GfsVariable ** v)
{
  g_return_val_if_fail (cell != NULL, 0.);
  g_return_val_if_fail (v != NULL, 0.);

  GfsDomain * domain = v[0]->domain;
  gdouble div = 0.;
  FttCellNeighbors n;
  ftt_cell_neighbors (cell, &n);
  FttCellFace f;
  f.cell = cell;
  for (f.d = 0; f.d < FTT_NEIGHBORS; f.d++)
    if (n.c[f.d]) {
      f.neighbor = n.c[f.d];
      div += gfs_domain_face_fraction (domain, &f)*
	(FTT_FACE_DIRECT (&f) ? 1. : -1.)*gfs_face_interpolated_value_generic (&f, v[f.d/2]);
    }
  return div/(gfs_domain_cell_fraction (domain, cell)*ftt_cell_size (cell));
}

/**
 * gfs_vorticity:
 * @cell: a #FttCell.
 * @v: the components of the vector.
 *
 * Returns: the vorticity (norm of the vorticity vector in 3D) of the
 * vector field @v in @cell.
 */
gdouble gfs_vorticity (FttCell * cell,
		       GfsVariable ** v)
{
  g_return_val_if_fail (cell != NULL, 0.);
  g_return_val_if_fail (v != NULL, 0.);

#if FTT_2D
  if (GFS_IS_MIXED (cell) || GFS_IS_AXI (v[0]->domain))
    /* fixme: the formulation below should also work (better) for solid cells */
    return (gfs_center_gradient (cell, FTT_X, v[1]->i) -
	    gfs_center_gradient (cell, FTT_Y, v[0]->i))/ftt_cell_size (cell);
  else {
    FttCellNeighbors n;
    ftt_cell_neighbors (cell, &n);
    FttCellFace f = { cell };
    double circ = 0.;
    f.d = FTT_RIGHT; f.neighbor = n.c[f.d];
    circ += gfs_face_interpolated_value_generic (&f, v[1])*
      gfs_domain_face_fraction (v[0]->domain, &f);
    f.d = FTT_LEFT; f.neighbor = n.c[f.d];
    circ -= gfs_face_interpolated_value_generic (&f, v[1])*
      gfs_domain_face_fraction (v[0]->domain, &f);
    f.d = FTT_TOP; f.neighbor = n.c[f.d];
    circ -= gfs_face_interpolated_value_generic (&f, v[0])*
      gfs_domain_face_fraction (v[0]->domain, &f);
    f.d = FTT_BOTTOM; f.neighbor = n.c[f.d];
    circ += gfs_face_interpolated_value_generic (&f, v[0])*
      gfs_domain_face_fraction (v[0]->domain, &f);
    /* vorticity is circulation over area */
    return circ*ftt_cell_size (cell)/gfs_cell_volume (cell, v[0]->domain);
  }
#else  /* FTT_3D */
  /* fixme: does not work with metric */
  FttVector vort;
  vort.x = (gfs_center_gradient (cell, FTT_Y, v[2]->i) -
	    gfs_center_gradient (cell, FTT_Z, v[1]->i));
  vort.y = (gfs_center_gradient (cell, FTT_Z, v[0]->i) -
	    gfs_center_gradient (cell, FTT_X, v[2]->i));
  vort.z = (gfs_center_gradient (cell, FTT_X, v[1]->i) -
	    gfs_center_gradient (cell, FTT_Y, v[0]->i));
  return sqrt (vort.x*vort.x + vort.y*vort.y + vort.z*vort.z)/ftt_cell_size (cell);
#endif /* FTT_3D */
}

/**
 * gfs_vector_norm2:
 * @cell: a #FttCell.
 * @v: the components of the vector.
 *
 * Returns: the squared norm of the vector field @v in @cell.
 */
gdouble gfs_vector_norm2 (FttCell * cell,
			  GfsVariable ** v)
{
  FttComponent c;
  gdouble n = 0.;
  
  g_return_val_if_fail (cell != NULL, 0.);
  g_return_val_if_fail (v != NULL, 0.);

  for (c = 0; c < FTT_DIMENSION; c++)
    n += GFS_VALUE (cell, v[c])*GFS_VALUE (cell, v[c]);
  return n;
}

/**
 * gfs_vector_norm:
 * @cell: a #FttCell.
 * @v: the components of the vector.
 *
 * Returns: the norm of the vector field @v in @cell.
 */
gdouble gfs_vector_norm (FttCell * cell,
			 GfsVariable ** v)
{
  return sqrt (gfs_vector_norm2 (cell, v));
}

/**
 * gfs_vector_lambda2:
 * @cell: a #FttCell.
 * @v: the components of the vector.
 *
 * Returns: The value of the lambda2 eigenvalue used by Jeong and
 * Hussain as vortex criterion (JFM 285, 69-94, 1995), normalized by
 * the square of the size of the cell.
 */
gdouble gfs_vector_lambda2 (FttCell * cell,
			    GfsVariable ** v)
{
  gdouble J[FTT_DIMENSION][FTT_DIMENSION];
  gdouble S2O2[FTT_DIMENSION][FTT_DIMENSION];
  gdouble lambda[FTT_DIMENSION], ev[FTT_DIMENSION][FTT_DIMENSION];
  guint i, j, k;

  g_return_val_if_fail (cell != NULL, 0.);
  g_return_val_if_fail (v != NULL, 0.);

  for (i = 0; i < FTT_DIMENSION; i++)
    for (j = 0; j < FTT_DIMENSION; j++)
      J[i][j] = gfs_center_gradient (cell, j, v[i]->i);
  for (i = 0; i < FTT_DIMENSION; i++)
    for (j = 0; j < FTT_DIMENSION; j++) {
      S2O2[i][j] = 0.;
      for (k = 0; k < FTT_DIMENSION; k++)
	S2O2[i][j] += J[i][k]*J[k][j] + J[k][i]*J[j][k];
    }
  gfs_eigenvalues (S2O2, lambda, ev);
  return lambda[1]/2.;
}

/**
 * gfs_pressure_force:
 * @cell: a #FttCell.
 * @p: a #GfsVariable.
 * @f: a #FttVector.
 *
 * Fills @f with the pressure @p component of the force exerted by the
 * fluid on the fraction of embedded boundary contained in @cell.
 */
void gfs_pressure_force (FttCell * cell,
			 GfsVariable * p,
			 FttVector * f)
{
  GfsSolidVector * s;

  g_return_if_fail (cell != NULL);
  g_return_if_fail (p != NULL);
  g_return_if_fail (f != NULL);

  if ((s = GFS_STATE (cell)->solid)) {
    gdouble size = ftt_cell_size (cell);
    gdouble pv = gfs_cell_dirichlet_value (cell, p, -1)*size;
    FttComponent c;

#if (!FTT_2D)
    pv *= size;
#endif /* 3D */

    gfs_solid_normal (cell, f);
    for (c = 0; c < FTT_DIMENSION; c++)
      (&f->x)[c] *= pv;
  }
  else
    f->x = f->y = f->z = 0.;
}

static void cell_traverse_mixed (FttCell * cell,
				 FttTraverseType order,
				 FttTraverseFlags flags,
				 FttCellTraverseFunc func,
				 gpointer data)
{
  if (!GFS_IS_MIXED (cell))
    return;
  if (order == FTT_PRE_ORDER &&
      (flags == FTT_TRAVERSE_ALL ||
       ((flags & FTT_TRAVERSE_LEAFS) != 0 && FTT_CELL_IS_LEAF (cell)) ||
       ((flags & FTT_TRAVERSE_NON_LEAFS) != 0 && !FTT_CELL_IS_LEAF (cell))))
    (* func) (cell, data);
  if (!FTT_CELL_IS_LEAF (cell)) {
    struct _FttOct * children = cell->children;
    guint n;

    for (n = 0; n < FTT_CELLS; n++) {
      FttCell * c = &(children->cell[n]);

      if (!FTT_CELL_IS_DESTROYED (c))
	cell_traverse_mixed (c, order, flags, func, data);
    }
  }
  if (order == FTT_POST_ORDER &&
      (flags == FTT_TRAVERSE_ALL ||
       ((flags & FTT_TRAVERSE_LEAFS) != 0 && FTT_CELL_IS_LEAF (cell)) ||
       ((flags & FTT_TRAVERSE_NON_LEAFS) != 0 && !FTT_CELL_IS_LEAF (cell))))
    (* func) (cell, data);
}

/**
 * gfs_cell_traverse_mixed:
 * @root: the root #FttCell of the tree to traverse.
 * @order: the order in which the cells are visited - %FTT_PRE_ORDER,
 * %FTT_POST_ORDER. 
 * @flags: which types of children are to be visited.
 * @func: the function to call for each visited #FttCell.
 * @data: user data to pass to @func.
 * 
 * Traverses a cell tree starting at the given root #FttCell. Calls
 * the given function for each mixed cell.
 */
void gfs_cell_traverse_mixed (FttCell * root,
			      FttTraverseType order,
			      FttTraverseFlags flags,
			      FttCellTraverseFunc func,
			      gpointer data)
{
  g_return_if_fail (root != NULL);
  g_return_if_fail (func != NULL);

  cell_traverse_mixed (root, order, flags, func, data);
}

#if FTT_2D
static FttDirection corner[4][FTT_DIMENSION] = {
  { FTT_LEFT,  FTT_BOTTOM },
  { FTT_RIGHT, FTT_BOTTOM },
  { FTT_RIGHT, FTT_TOP },
  { FTT_LEFT,  FTT_TOP }
};
#else  /* 3D */
static FttDirection corner[8][FTT_DIMENSION] = {
  { FTT_LEFT,  FTT_BOTTOM, FTT_FRONT },
  { FTT_RIGHT, FTT_BOTTOM, FTT_FRONT },
  { FTT_RIGHT, FTT_TOP,    FTT_FRONT },
  { FTT_LEFT,  FTT_TOP,    FTT_FRONT },
  { FTT_LEFT,  FTT_BOTTOM, FTT_BACK },
  { FTT_RIGHT, FTT_BOTTOM, FTT_BACK },
  { FTT_RIGHT, FTT_TOP,    FTT_BACK },
  { FTT_LEFT,  FTT_TOP,    FTT_BACK }
};
#endif /* 3D */

/**
 * gfs_cell_corner_values:
 * @cell: a #FttCell containing location @p.
 * @v: a #GfsVariable.
 * @max_level: the maximum cell level to consider (-1 means no restriction).
 * @f: an array with the correct size (4*(FTT_DIMENSION - 1) + 1).
 *
 * Fills @f with the values of @v interpolated at the corners of @cell.
 */
void gfs_cell_corner_values (FttCell * cell, 
			     GfsVariable * v, 
			     gint max_level,
			     gdouble f[4*(FTT_DIMENSION - 1) + 1])
{
  g_return_if_fail (cell != NULL);
  g_return_if_fail (v != NULL);
  g_return_if_fail (f != NULL);

  int i;
  for (i = 0; i < 4*(FTT_DIMENSION - 1); i++)
    f[i] = gfs_cell_corner_value (cell, corner[i], v, max_level);
  f[4*(FTT_DIMENSION - 1)] = GFS_VALUE (cell, v);
}

/**
 * gfs_interpolate_from_corners:
 * @cell: a #FttCell containing location @p.
 * @p: the location at which to interpolate.
 * @f: values at the corners of @cell.
 *
 * Returns: the interpolated value at location @p.
 */
gdouble gfs_interpolate_from_corners (FttCell * cell,
				      FttVector p,
				      gdouble * f)
{
  FttVector o;
  gdouble size;

  g_return_val_if_fail (cell != NULL, 0.);
  g_return_val_if_fail (f != NULL, 0.);

  ftt_cell_pos (cell, &o);
  size = ftt_cell_size (cell)/2.;
  p.x = (p.x - o.x)/size;
  p.y = (p.y - o.y)/size;
#if FTT_2D
  gdouble x = (p.x + p.y)/2., y = (p.y - p.x)/2., v = f[4];
  if (x > 0.)
    v += x*(f[2] - f[4]);
  else
    v -= x*(f[0] - f[4]);
  if (y > 0.)
    v += y*(f[3] - f[4]);
  else
    v -= y*(f[1] - f[4]);
  return v;
#else  /* 3D */
  gdouble c[8];
  
  p.z = (p.z - o.z)/size;
  c[0] = - f[0] + f[1] + f[2] - f[3] - f[4] + f[5] + f[6] - f[7];
  c[1] = - f[0] - f[1] + f[2] + f[3] - f[4] - f[5] + f[6] + f[7];
  c[2] =   f[0] + f[1] + f[2] + f[3] - f[4] - f[5] - f[6] - f[7];
  c[3] =   f[0] - f[1] + f[2] - f[3] + f[4] - f[5] + f[6] - f[7];
  c[4] = - f[0] + f[1] + f[2] - f[3] + f[4] - f[5] - f[6] + f[7];
  c[5] = - f[0] - f[1] + f[2] + f[3] + f[4] + f[5] - f[6] - f[7];
  c[6] =   f[0] - f[1] + f[2] - f[3] - f[4] + f[5] - f[6] + f[7];
  c[7] =   f[0] + f[1] + f[2] + f[3] + f[4] + f[5] + f[6] + f[7];

  return (c[0]*p.x + c[1]*p.y + c[2]*p.z + 
	  c[3]*p.x*p.y + c[4]*p.x*p.z + c[5]*p.y*p.z + 
	  c[6]*p.x*p.y*p.z + 
	  c[7])/8.;
#endif /* 3D */  
}

/**
 * gfs_interpolate:
 * @cell: a #FttCell containing location @p.
 * @p: the location at which to interpolate.
 * @v: a #GfsVariable.
 *
 * Interpolates the @v variable of @cell, at location @p. Linear
 * interpolation is used and the boundaries of the domain are treated
 * as planes of symmetry for all variables.
 *
 * Returns: the interpolated value of variable @v at location @p.
 */
gdouble gfs_interpolate (FttCell * cell,
			 FttVector p,
			 GfsVariable * v)
{
  g_return_val_if_fail (cell != NULL, 0.);
  g_return_val_if_fail (v != NULL, 0.);

  if (GFS_VALUE (cell, v) == GFS_NODATA)
    return GFS_NODATA;

  gdouble f[4*(FTT_DIMENSION - 1) + 1];
  gfs_cell_corner_values (cell, v, -1, f);
  return gfs_interpolate_from_corners (cell, p, f);
}

/**
 * gfs_interpolate_stencil:
 * @cell: a #FttCell.
 * @v: a #GfsVariable.
 *
 * Sets to 1. the @v variable of all the cells which would be used by
 * a call to gfs_interpolate().
 */
void gfs_interpolate_stencil (FttCell * cell,
			      GfsVariable * v)
{
  guint i;

  g_return_if_fail (cell != NULL);
  g_return_if_fail (v != NULL);

  for (i = 0; i < (FTT_DIMENSION == 2 ? 4 : 8); i++) {
    GfsInterpolator inter;
    guint j;

    gfs_cell_corner_interpolator (cell, corner[i], -1, TRUE, &inter);
    for (j = 0; j < inter.n; j++)
      GFS_VALUE (inter.c[j], v) = 1.;
  }
}

/**
 * gfs_center_curvature:
 * @cell: a #FttCell.
 * @c: a component.
 * @v: a #GfsVariable index.
 *
 * The curvature is normalized by the square of the size of the cell.
 *
 * Returns: the value of the @c component of the curvature of variable @v
 * at the center of the cell.  
 */
gdouble gfs_center_curvature (FttCell * cell,
			      FttComponent c,
			      guint v)
{
  FttCellFace f;
  GfsGradient g = { 0., 0. };

  g_return_val_if_fail (cell != NULL, 0.);
  g_return_val_if_fail (c < FTT_DIMENSION, 0.);

  if (GFS_IS_MIXED (cell))
    return 0.;

  f.cell = cell;
  for (f.d = 2*c; f.d <= 2*c + 1; f.d++)
    if ((f.neighbor = ftt_cell_neighbor (cell, f.d))) {
      GfsGradient e;

      gfs_face_gradient (&f, &e, v, -1);
      g.a += e.a;
      g.b += e.b;
    }

  return g.b - g.a*GFS_VALUEI (cell, v);
}

/**
 * gfs_streamline_curvature:
 * @cell: a #FttCell.
 * @v: the components of the vector.
 *
 * The curvature is normalized by the size of the cell.
 *
 * Returns: the value of the curvature of the streamline defined by @v passing
 * through the center of the cell.
 */
gdouble gfs_streamline_curvature (FttCell * cell,
				  GfsVariable ** v)
{
  gdouble u2;

  g_return_val_if_fail (cell != NULL, 0.);
  g_return_val_if_fail (v != NULL, 0.);

  u2 = gfs_vector_norm2 (cell, v);

  if (u2 == 0.)
    return 0.;
  else {
    FttComponent i;
    gdouble ugu = 0.;

    for (i = 0; i < FTT_DIMENSION; i++) {
      FttComponent j;
      gdouble ugui = 0.;

      for (j = 0; j < FTT_DIMENSION; j++)
	ugui += GFS_VALUE (cell, v[j])*gfs_center_gradient (cell, j, v[i]->i);
      ugu += ugui*ugui;
    }
    return sqrt (ugu)/u2;
  }
}

static FttCell * cell_corner_neighbor (FttCell * cell,
				       FttDirection * d,
				       gint max_level,
				       gboolean * t_junction)
{
  FttCell * neighbor = ftt_cell_neighbor (cell, d[0]);
  if (!neighbor)
    return NULL;
  else {
    guint level = ftt_cell_level (cell);
    if (ftt_cell_level (neighbor) < level) {
      /* neighbor is at a shallower level */
      if (ftt_cell_child_corner (ftt_cell_parent (cell), d) != cell)
	*t_junction = TRUE;
      return neighbor;
    }
    else {
      if (level == max_level || FTT_CELL_IS_LEAF (neighbor))
	/* neighbor is at the same level */
	return neighbor;
      else {
	/* neighbor is at a deeper level */
	FttCell * n;
	guint i;
	FttDirection d1[FTT_DIMENSION];
	d1[0] = FTT_OPPOSITE_DIRECTION (d[0]);
	for (i = 1; i < FTT_DIMENSION; i++)
	  d1[i] = d[i];
	n = ftt_cell_child_corner (neighbor, d1);
	return n ? n : neighbor;
      }
    }
  }
}

static void interpolator_merge (GfsInterpolator * a, GfsInterpolator * b)
{
  guint i;
  for (i = 0; i < b->n; i++) {
    FttCell * c = b->c[i];
    guint j;

    for (j = 0; j < a->n && c != a->c[j]; j++)
      ;
    if (j < a->n)
      a->w[j] += b->w[i];
    else {
#if FTT_2D
      g_assert (j < 7);
#else
      g_assert (j < 29);
#endif
      a->c[j] = c;
      a->w[j] = b->w[i];
      a->n++;
    }
  }
}

static void interpolator_scale (GfsInterpolator * a, gdouble b)
{
  guint i;
  for (i = 0; i < a->n; i++)
    a->w[i] *= b;
}

static void t_junction_interpolator (FttCell * cell,
				     FttDirection * d,
				     FttCell * n,
				     gint max_level,
				     gboolean centered,
				     GfsInterpolator * inter)
{
  FttDirection d1[FTT_DIMENSION];
  GfsInterpolator a;

  d1[0] = FTT_OPPOSITE_DIRECTION (d[0]);
#if FTT_2D
  d1[1] = d[1];
  gfs_cell_corner_interpolator (n, d1, max_level, centered, inter);
  d1[1] = FTT_OPPOSITE_DIRECTION (d[1]);
  gfs_cell_corner_interpolator (n, d1, max_level, centered, &a);
  interpolator_merge (inter, &a);
  interpolator_scale (inter, 0.5);
#else /* 3D */
  d1[1] = d[1]; d1[2] = d[2];
  gfs_cell_corner_interpolator (n, d1, max_level, centered, inter);
  if (ftt_cell_neighbor_is_brother (cell, d[1])) {
    d1[1] = FTT_OPPOSITE_DIRECTION (d[1]);
    gfs_cell_corner_interpolator (n, d1, max_level, centered, &a);
    interpolator_merge (inter, &a);
    if (ftt_cell_neighbor_is_brother (cell, d[2])) {
      d1[2] = FTT_OPPOSITE_DIRECTION (d[2]);
      gfs_cell_corner_interpolator (n, d1, max_level, centered, &a);
      interpolator_merge (inter, &a);
      d1[1] = d[1];
      gfs_cell_corner_interpolator (n, d1, max_level, centered, &a);
      interpolator_merge (inter, &a);
      interpolator_scale (inter, 0.25);
    }
    else
      interpolator_scale (inter, 0.5);
  }
  else {
    d1[2] = FTT_OPPOSITE_DIRECTION (d[2]);
    gfs_cell_corner_interpolator (n, d1, max_level, centered, &a);
    interpolator_merge (inter, &a);
    interpolator_scale (inter, 0.5);
  }
#endif /* 3D */
}

static gboolean do_path (FttCell * cell, gint i,
			 FttCell * n[N_CELLS],
			 FttDirection * d,
			 gint max_level,
			 gboolean centered,
			 GfsInterpolator * inter)
{
  /* paths from each cell to neighbors. Cell indices are as in
     doc/figures/indices.fig
     first index: cell index
     second index: path index
     third index: < FTT_DIMENSION: directions.
                  = FTT_DIMENSION: neighboring cell index */
  static gint path[N_CELLS][FTT_DIMENSION][FTT_DIMENSION + 1] = {
#if FTT_2D
    {{1,2,1},   {2,1,2}},
    {{2,-1,3},  {-1,2,0}},
    {{1,-2,3},  {-2,1,0}},
    {{-1,-2,2}, {-2,-1,1}}
#else /* 3D */
    {{1,2,3,1},    {2,1,3,2},    {3,1,2,4}},
    {{2,3,-1,3},   {3,2,-1,5},   {-1,2,3,0}},
    {{1,-2,3,3},   {3,-2,1,6},   {-2,3,1,0}},
    {{3,-1,-2,7},  {-2,-1,3,1},  {-1,-2,3,2}},
    {{2,1,-3,6},   {1,2,-3,5},   {-3,1,2,0}},
    {{2,-1,-3,7},  {-1,2,-3,4},  {-3,2,-1,1}},
    {{1,-2,-3,7},  {-2,1,-3,4},  {-3,1,-2,2}},
    {{-1,-2,-3,6}, {-2,-1,-3,5}, {-3,-1,-2,3}}
#endif /* 3D */
  };
  guint j;

  for (j = 0; j < FTT_DIMENSION; j++) {
    guint k = path[i][j][FTT_DIMENSION];

    if (n[k] == NULL) {
      gboolean t_junction = FALSE;
      FttDirection d1[FTT_DIMENSION];
      guint l;

      for (l = 0; l < FTT_DIMENSION; l++)
	d1[l] = path[i][j][l] < 0 ? FTT_OPPOSITE_DIRECTION (d[- path[i][j][l] - 1]) : 
	  d[path[i][j][l] - 1];
      n[k] = cell_corner_neighbor (cell, d1, max_level, &t_junction);
      if (t_junction) {
	t_junction_interpolator (cell, d1, n[k], max_level, centered, inter);
	return TRUE;
      }
      if (n[k]) {
	t_junction = do_path (n[k], k, n, d, max_level, centered, inter);
	if (t_junction)
	  return TRUE;
      }
    }
  }
  return FALSE;
}

static gdouble distance (FttVector * c, FttCell * cell, gboolean centered)
{
  if (centered || !GFS_IS_MIXED (cell))
    return ftt_cell_size (cell)*
#if FTT_2D
      0.707106781185
#else  /* 3D */
      0.866025403785
#endif /* 3D */
      ;
  else {
    FttVector cm;
    gfs_cell_cm (cell, &cm);
#if FTT_2D
    return sqrt ((cm.x - c->x)*(cm.x - c->x) + (cm.y - c->y)*(cm.y - c->y));
#else /* 3D */
    return sqrt ((cm.x - c->x)*(cm.x - c->x) + (cm.y - c->y)*(cm.y - c->y) +
		 (cm.z - c->z)*(cm.z - c->z));
#endif /* 3D */
  }
}

/**
 * gfs_cell_corner_interpolator:
 * @cell: a #FttCell.
 * @d: a set of perpendicular directions.
 * @max_level: the maximum cell level to consider (-1 means no restriction).
 * @centered: %TRUE if the interpolator is cell-centered.
 * @inter: a #GfsInterpolator.
 *
 * Fills @inter with the interpolator for the corner of @cell defined by @d.
 */
void gfs_cell_corner_interpolator (FttCell * cell,
				   FttDirection d[FTT_DIMENSION],
				   gint max_level,
				   gboolean centered,
				   GfsInterpolator * inter)
{
  FttCell * n[N_CELLS];
  guint i;
  gboolean t_junction;

  g_return_if_fail (cell != NULL);
  g_return_if_fail (inter != NULL);

  while (!FTT_CELL_IS_LEAF (cell) && 
	 ftt_cell_level (cell) != max_level &&
	 (n[0] = ftt_cell_child_corner (cell, d)))
    cell = n[0];
  n[0] = cell;
  for (i = 1; i < N_CELLS; i++)
    n[i] = NULL;
  t_junction = do_path (cell, 0, n, d, max_level, centered, inter);
  if (t_junction)
    return;

  FttVector c;
  gdouble w = 0.;
  int boundaries = 0;
  
  inter->n = 0;
  ftt_corner_pos (cell, d, &c);
  for (i = 0; i < N_CELLS; i++)
    if (n[i]) {
      gdouble a;
      a = 1./(distance (&c, n[i], centered) + 1e-12);
      inter->c[inter->n] = n[i];
      inter->w[inter->n++] = a;
      w += a;
      if (GFS_CELL_IS_BOUNDARY (n[i]))
	boundaries++;
    }

  /* corners of domain receive special treatment */
  if (inter->n == FTT_DIMENSION + 1 && boundaries == FTT_DIMENSION) {
    /* remove central cell from interpolator */
    w -= inter->w[0];
    for (i = 0; i < inter->n - 1; i++) {
      inter->c[i] = inter->c[i + 1];
      inter->w[i] = inter->w[i + 1];
    }
    inter->n--;
  }

  g_assert (w > 0.);
  interpolator_scale (inter, 1./w);
}

/**
 * gfs_cell_corner_value:
 * @cell: a #FttCell.
 * @d: a set of perpendicular directions.
 * @v: a #GfsVariable.
 * @max_level: the maximum cell level to consider (-1 means no restriction).
 *
 * Returns: the value of variable @v interpolated at the corner of
 * @cell defined by @d.
 */
gdouble gfs_cell_corner_value (FttCell * cell,
			       FttDirection d[FTT_DIMENSION],
			       GfsVariable * v,
			       gint max_level)
{
  GfsInterpolator inter;
  gdouble val = 0.;
  guint i;

  g_return_val_if_fail (cell != NULL, 0.);
  g_return_val_if_fail (v != NULL, 0.);

  gfs_cell_corner_interpolator (cell, d, max_level, v->centered, &inter);
  for (i = 0; i < inter.n; i++) {
    gdouble v1 = GFS_VALUE (inter.c[i], v);
    if (v1 == GFS_NODATA)
      return GFS_VALUE (cell, v);
    val += inter.w[i]*v1;
  }
  return val;
}

/* GfsStencil: Object */

/* The convention is that the first element is the diagonal */

/**
 * gfs_stencil_new:
 * @cell: the #FttCell associated with this stencil.
 * @lp: the linear problem.
 * @coeff: the cell coefficient.
 * 
 * Returns: a new #GfsStencil.
 */
GfsStencil * gfs_stencil_new (FttCell * cell, GfsLinearProblem * lp, gdouble coeff)
{
  g_return_val_if_fail (cell != NULL, NULL);
  g_return_val_if_fail (lp != NULL, NULL);

  GfsStencil * s = g_malloc (sizeof (GfsStencil));
  s->id = g_array_new (FALSE, FALSE, sizeof (int));
  s->coeff = g_array_new (FALSE, FALSE, sizeof (double));
  gfs_stencil_add_element (s, cell, lp, coeff);
  return s;
}

/**
 * gfs_stencil_add_element:
 * @stencil: the stencil
 * @cell: a #FttCell.
 * @lp: the linear problem
 * @coeff: the cell coefficient.
 *
 * Adds the contribution of a @cell to @stencil.
 */
void gfs_stencil_add_element (GfsStencil * stencil, 
			      FttCell * cell,
			      GfsLinearProblem * lp,
			      gdouble coeff)
{
  gint i;

  g_return_if_fail (stencil != NULL);
  g_return_if_fail (lp != NULL);

  int id = GFS_VALUE (cell, lp->id);
  if (id < 0) {
    g_assert (GFS_CELL_IS_BOUNDARY (cell));
    FttCell * neighbor = GFS_DOUBLE_TO_POINTER (GFS_VALUE (cell, lp->neighbor));
    g_assert (neighbor);
    id = GFS_VALUE (neighbor, lp->id);
    g_assert (id >= 0);
    coeff *= GFS_VALUE (cell, lp->neighborw);
  }

  for (i = 0; i < stencil->id->len; i++)
    if (g_array_index (stencil->id, int, i) == id) {
      g_array_index (stencil->coeff, double, i) += coeff;
      return;
    }

  g_array_append_val (stencil->id, id);
  g_array_append_val (stencil->coeff, coeff);
}

/**
 * gfs_stencil_destroy:
 * @stencil: a stencil 
 *
 * Destroys a stencil.
 */
void gfs_stencil_destroy (GfsStencil * stencil)
{
  g_return_if_fail (stencil != NULL);
  g_array_free (stencil->id, TRUE);
  g_array_free (stencil->coeff, TRUE);
  g_free (stencil);
}

static void get_average_neighbor_value_stencil (const FttCellFace * face,
						gdouble * x,
						GfsLinearProblem * lp, GfsStencil * stencil,
						gdouble weight)
{
  /* check for corner refinement violation (topology.fig) */
  g_assert (ftt_cell_level (face->neighbor) == ftt_cell_level (face->cell));
  
  if (FTT_CELL_IS_LEAF (face->neighbor))
    gfs_stencil_add_element (stencil, face->neighbor, lp, weight/(*x));
  else {
    FttCellChildren children;
    gdouble a = 0.;
    FttDirection od = FTT_OPPOSITE_DIRECTION (face->d);
    guint i, n;
    
    n = ftt_cell_children_direction (face->neighbor, od, &children);
    for (i = 0; i < n; i++)
      if (children.c[i]) {
	gdouble w = GFS_IS_MIXED (children.c[i]) ? GFS_STATE (children.c[i])->solid->s[od] : 1.;
	a += w;
      }
    if (a > 0.) {
      *x = 3./4.;
      for (i = 0; i < n; i++)
	if (children.c[i]) {
	  gdouble w = GFS_IS_MIXED (children.c[i]) ? GFS_STATE (children.c[i])->solid->s[od] : 1.;

	  gfs_stencil_add_element (stencil, children.c[i], lp, weight*w/a/(*x));
	}
    }
    else
      gfs_stencil_add_element (stencil, face->cell, lp, weight/(*x));
  }
}

#if (FTT_2D || FTT_2D3)
/* v = a*v(cell) + b 
 * 
 * First order 1D interpolation.
 */
static GfsGradient get_interpolate_1D1_stencil (FttCell * cell,
						FttDirection d,
						gdouble x,
						GfsLinearProblem * lp,
						GfsStencil * stencil,
						gdouble weight)
{
  GfsGradient p;
  FttCellFace f;

  f = gfs_cell_face (cell, d);
  if (f.neighbor) {
    gdouble x2 = 1.;
    get_average_neighbor_value_stencil (&f, &x2, lp, stencil, weight*x);
    p.a = 1. - x/x2;
  }
  else
    p.a = 1.;

  return p;
}

#else

/* v = a*v(cell) + b 
 * 
 * First order 2D interpolation.
 */
static GfsGradient get_interpolate_2D1_stencil (FttCell * cell,
						FttDirection d1, FttDirection d2,
						gdouble x, gdouble y,
						GfsLinearProblem * lp, GfsStencil * stencil,
						gdouble weight)
{
  GfsGradient p;
  gdouble y1 = 1.;
  gdouble x2 = 1.;
  gdouble a1 = y, a2 = x;
  FttCellFace f1, f2;

  p.a = 1.;

  f1 = gfs_cell_face (cell, d1);
  if (f1.neighbor) {
    get_average_neighbor_value_stencil (&f1, &y1, lp, stencil, a1*weight);
    a1 /= y1;
    p.a -= a1;
  }
    
  f2 = gfs_cell_face (cell, d2);
  if (f2.neighbor) {
    get_average_neighbor_value_stencil (&f2, &x2, lp, stencil, a2*weight);
    a2 /= x2;
    p.a -= a2;
  }
  
  return p;
}

#endif /* not FTT_2D */

static Gradient gradient_fine_coarse_stencil (const FttCellFace * face,
					      GfsLinearProblem * lp,
					      GfsStencil * stencil, gdouble weight)
{
  Gradient g;
  GfsGradient p;
#if (FTT_2D || FTT_2D3)
  gint dp;
#else  /* FTT_3D */
  gint * dp;
#endif /* FTT_3D */

  g_assert (face != NULL);
  g_assert (ftt_face_type (face) == FTT_FINE_COARSE);

  dp = perpendicular[face->d][FTT_CELL_ID (face->cell)];
#if (FTT_2D || FTT_2D3)
  g_assert (dp >= 0);
  p = get_interpolate_1D1_stencil (face->neighbor, dp, 1./4., lp, stencil, 2./3.*weight);
#else  /* FTT_3D */
  g_assert (dp[0] >= 0 && dp[1] >= 0);
  p = get_interpolate_2D1_stencil (face->neighbor, dp[0], dp[1], 1./4., 1./4., lp,
				   stencil, 2./3.*weight);
#endif /* FTT_3D */

  g.a = 2./3.;
  g.b = 2.*p.a/3.;
  g.c = 0.;

  return g;
}

static void face_weighted_gradient_stencil (const FttCellFace * face,
					    GfsGradient * g,
					    gint max_level,
					    guint dimension,
					    GfsLinearProblem * lp,
					    GfsStencil * stencil)
{
  guint level;

  g_return_if_fail (face != NULL);

  g->a = 0.;
  
  if (face->neighbor == NULL)
    return;

  level = ftt_cell_level (face->cell);
  if (ftt_cell_level (face->neighbor) < level) {
    /* neighbor is at a shallower level */
    Gradient gcf;
    gdouble w = GFS_STATE (face->cell)->f[face->d].v;

    gcf = gradient_fine_coarse_stencil (face, lp, stencil, w);
    g->a = w*gcf.a;
    gfs_stencil_add_element (stencil, face->neighbor, lp,  w*gcf.b); 
  }
  else {
    if (level == max_level || FTT_CELL_IS_LEAF (face->neighbor)) {
      /* neighbor is at the same level */
      gdouble w = GFS_STATE (face->cell)->f[face->d].v;

      g->a = w;
      gfs_stencil_add_element (stencil, face->neighbor, lp, w);
    }
    else {
      /* neighbor is at a deeper level */
      FttCellChildren children;
      FttCellFace f;
      guint i, n;
      
      f.d = FTT_OPPOSITE_DIRECTION (face->d);
      n = ftt_cell_children_direction (face->neighbor, f.d, &children);
      f.neighbor = face->cell;
      for (i = 0; i < n; i++)
	if ((f.cell = children.c[i])) {
	  Gradient gcf;
	  gdouble w = GFS_STATE (f.cell)->f[f.d].v;
	  if (dimension > 2)
	    w /=  n/2; /* fixme??? */
	
	  gcf = gradient_fine_coarse_stencil (&f, lp, stencil, -w);
	  g->a += w*gcf.b;
	  
	  gfs_stencil_add_element (stencil, f.cell, lp, w*gcf.a);
	}
    }
  }
}

/**
 * gfs_face_weighted_gradient_stencil:
 * @face: a FttCellFace
 * @g: a GfsGradient
 * @max_level: an integer
 * @lp: the linear problem
 * @stencil: a stencil 
 *
 * Fills @stencil with the stencil corresponding to the
 * weighted gradient on @face.
 */
void gfs_face_weighted_gradient_stencil (const FttCellFace * face,
					 GfsGradient * g,
					 gint max_level,
					 GfsLinearProblem * lp,
					 GfsStencil * stencil)
{
  face_weighted_gradient_stencil (face, g, max_level, FTT_DIMENSION, lp, stencil);
}

static gboolean mixed_face_gradient_stencil (const FttCellFace * face,
					     Gradient * g,
					     gint max_level,
					     GfsLinearProblem * lp,
					     GfsStencil * stencil,
					     gdouble weight)
{
  FttCell * n[N_CELLS];
  gdouble m[N_CELLS - 1][N_CELLS - 1];
  FttVector o, cm;
  FttComponent c = face->d/2;
  gdouble h = ftt_cell_size (face->cell);
  gdouble w = weight;

  gfs_cell_cm (face->cell, &o);
  if (!face_bilinear (face, n, &o, gfs_cell_cm, max_level, m))
    return FALSE;

  gfs_face_ca (face, &cm);

  if (!FTT_FACE_DIRECT (face))
    w = -w;

#if FTT_2D
  {
    FttComponent cp = FTT_ORTHOGONAL_COMPONENT (c);
    gdouble vp;
    
    vp = ((&cm.x)[cp] - (&o.x)[cp])/h;
    g->a = ((m[c][0] + m[2][0]*vp) +
	    (m[c][1] + m[2][1]*vp) +
	    (m[c][2] + m[2][2]*vp));
    g->b = m[c][0] + m[2][0]*vp;
    gfs_stencil_add_element (stencil, n[2], lp, w*(m[c][1] + m[2][1]*vp));
    gfs_stencil_add_element (stencil, n[3], lp, w*(m[c][2] + m[2][2]*vp));
  }
#else /* 3D */
  {
    guint j;

    cm.x = (cm.x - o.x)/h;
    cm.y = (cm.y - o.y)/h;
    cm.z = (cm.z - o.z)/h;
    g->c = 0.;
    
    switch (c) {
    case FTT_X:
      g->a = g->b = m[0][0] + cm.y*m[3][0] + cm.z*m[4][0] + cm.y*cm.z*m[6][0];
      for (j = 1; j < N_CELLS - 1; j++) {
  	gdouble a = m[0][j] + cm.y*m[3][j] + cm.z*m[4][j] + cm.y*cm.z*m[6][j];
  	g->a += a;
	gfs_stencil_add_element (stencil, n[j+1], lp, w*a);
      }
      break;
    case FTT_Y:
      g->a = g->b = m[1][0] + cm.x*m[3][0] + cm.z*m[5][0] + cm.x*cm.z*m[6][0];
      for (j = 1; j < N_CELLS - 1; j++) {
  	gdouble a = m[1][j] + cm.x*m[3][j] + cm.z*m[5][j] + cm.x*cm.z*m[6][j];
  	g->a += a;
	gfs_stencil_add_element (stencil, n[j+1], lp, w*a);
      }
      break;
    case FTT_Z:
      g->a = g->b = m[2][0] + cm.x*m[4][0] + cm.y*m[5][0] + cm.x*cm.y*m[6][0];
      for (j = 1; j < N_CELLS - 1; j++) {
  	gdouble a = m[2][j] + cm.x*m[4][j] + cm.y*m[5][j] + cm.x*cm.y*m[6][j];
  	g->a += a;
	gfs_stencil_add_element (stencil, n[j+1], lp, w*a);
      }
      break;
    default:
      g_assert_not_reached ();
    }
  }
#endif /* 3D */

  if (!FTT_FACE_DIRECT (face)) {
    g->a = - g->a;
    g->b = - g->b;
  }

  return TRUE;
}

static void face_cm_gradient_stencil (const FttCellFace * face,
				      GfsGradient * g,
				      gint max_level,
				      gboolean weighted,
				      GfsLinearProblem * lp,
				      GfsStencil * stencil)
{
  guint level;
  Gradient gcf;
  gdouble w = weighted ? GFS_STATE (face->cell)->f[face->d].v : 1.;

  g->a = g->b = 0.;
  
  if (face->neighbor == NULL || w == 0.)
    return;

  level = ftt_cell_level (face->cell);
  if (ftt_cell_level (face->neighbor) < level) {
    /* neighbor is at a shallower level */
    if (GFS_IS_MIXED (face->cell) || GFS_IS_MIXED (face->neighbor)) {
      if (!mixed_face_gradient_stencil (face, &gcf, max_level, lp, stencil, w))
	gcf = gradient_fine_coarse_stencil (face, lp, stencil, w);
    }
    else
      gcf = gradient_fine_coarse_stencil (face, lp, stencil, w);
    g->a = w*gcf.a;
    gfs_stencil_add_element (stencil, face->neighbor, lp, w*gcf.b);
  }
  else {
    if (level == max_level || FTT_CELL_IS_LEAF (face->neighbor)) {
      /* neighbor is at the same level */
      if (!GFS_IS_MIXED (face->cell) && !GFS_IS_MIXED (face->neighbor)) {
  	g->a = w;
	gfs_stencil_add_element (stencil, face->neighbor, lp,  w);
      }
      else if (mixed_face_gradient_stencil (face, &gcf, max_level, lp, stencil, w)) {
  	g->a = w*gcf.a;
	gfs_stencil_add_element (stencil, face->neighbor, lp,  w*gcf.b);
      }
      else {
  	g->a = w;
	gfs_stencil_add_element (stencil, face->neighbor, lp,  w);
      }
    }
    else {
      /* neighbor is at a deeper level */
      FttCellChildren children;
      FttCellFace f;
      guint i, n;
      
      f.d = FTT_OPPOSITE_DIRECTION (face->d);
      n = ftt_cell_children_direction (face->neighbor, f.d, &children);
      f.neighbor = face->cell;
      for (i = 0; i < n; i++)
    	if ((f.cell = children.c[i])) {
    	  w = weighted ? GFS_STATE (f.cell)->f[f.d].v : 1.;
    	  if (GFS_IS_MIXED (f.cell) || GFS_IS_MIXED (f.neighbor)) {
    	    if (!mixed_face_gradient_stencil (&f, &gcf, max_level, lp, stencil, w))
    	      gcf = gradient_fine_coarse_stencil (&f, lp, stencil, -w);
    	  }
    	  else
    	    gcf = gradient_fine_coarse_stencil (&f, lp, stencil, -w);
    	  g->a += w*gcf.b;
	  gfs_stencil_add_element (stencil, f.cell, lp,  w*gcf.a);
    	}
    }
  }
}

/**
 * gfs_face_cm_weighted_gradient_stencil:
 * @face: a #FttCellFace.
 * @g: a GfsGradient.
 * @max_level: the maximum cell level to consider (-1 means no restriction).
 * @lp: the linear problem.
 * @stencil: a stencil.
 *
 * Fills @stencil with the stencil corresponding to the
 * weighted gradient on @face. The weights are defined at
 * the center of mass of the cell. Linear interpolation is used
 * to evaluate the gradient in the vicinity of cut cells.
 * This function returns the stencil equivalent to the action of
 * gfs_face_cm_weighted_gradient().
 */
void gfs_face_cm_weighted_gradient_stencil (const FttCellFace * face,
					    GfsGradient * g,
					    gint max_level,
					    GfsLinearProblem * lp,
					    GfsStencil * stencil)
{
  g_return_if_fail (face != NULL);
  g_return_if_fail (g != NULL);
  g_return_if_fail (lp != NULL);
  g_return_if_fail (stencil != NULL);

  face_cm_gradient_stencil (face, g, max_level, TRUE, lp, stencil);
}

/**
 * gfs_cm_gradient:
 * @cell: a #FttCell.
 * @v: a #GfsVariable.
 * @g: a #FttVector.
 *
 * Fills @g with the components of the gradient of @v at the center of
 * mass of @cell.
 *
 * The gradient is normalized by the size of the cell.
 */
void gfs_cm_gradient (FttCell * cell,
		      GfsVariable * v,
		      FttVector * g)
{
  g_return_if_fail (cell != NULL);
  g_return_if_fail (v != NULL);
  g_return_if_fail (g != NULL);

  if (v->centered) {
    FttComponent c;
    for (c = 0; c < FTT_DIMENSION; c++)
      (&g->x)[c] = gfs_center_gradient (cell, c, v->i);
  }
  else if (GFS_IS_MIXED (cell))
    gfs_mixed_cell_gradient (cell, v, g);
  else {
    FttComponent c;
    GfsGradient g1, g2;
    FttCellFace f;
    FttCellNeighbors n;
    gdouble val = GFS_VALUE (cell, v);
    ftt_cell_neighbors (cell, &n);
    f.cell = cell;
    for (c = 0; c < FTT_DIMENSION; c++) {
      f.d = 2*c; f.neighbor = n.c[f.d];
      gfs_face_cm_gradient (&f, &g1, v->i, -1);
      f.d = 2*c + 1; f.neighbor = n.c[f.d];
      gfs_face_cm_gradient (&f, &g2, v->i, -1);
      (&g->x)[c] = (g1.b - g2.b + (g2.a - g1.a)*val)/2.;
    }
  }
}