File: cs_lagr_sde.c

package info (click to toggle)
code-saturne 4.3.3%2Brepack-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 77,992 kB
  • sloc: ansic: 281,257; f90: 122,305; python: 56,490; makefile: 3,915; xml: 3,285; cpp: 3,183; sh: 1,139; lex: 176; yacc: 101; sed: 16
file content (2000 lines) | stat: -rw-r--r-- 68,958 bytes parent folder | download
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
/*============================================================================
 * Methods for particle deposition
 *============================================================================*/

/*
  This file is part of Code_Saturne, a general-purpose CFD tool.

  Copyright (C) 1998-2016 EDF S.A.

  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., 51 Franklin
  Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

/*----------------------------------------------------------------------------*/

/*============================================================================
 * Functions dealing with particle deposition
 *============================================================================*/

#include "cs_defs.h"
#include "cs_math.h"

/*----------------------------------------------------------------------------
 * Standard C library headers
 *----------------------------------------------------------------------------*/

#include <math.h>

/*----------------------------------------------------------------------------
 *  Local headers
 *----------------------------------------------------------------------------*/

#include "bft_mem.h"

#include "cs_mesh.h"
#include "cs_mesh_quantities.h"
#include "cs_physical_constants.h"
#include "cs_physical_model.h"
#include "cs_prototypes.h"
#include "cs_thermal_model.h"

#include "cs_lagr.h"
#include "cs_lagr_deposition_model.h"
#include "cs_lagr_roughness.h"
#include "cs_lagr_tracking.h"
#include "cs_lagr_prototypes.h"

/*----------------------------------------------------------------------------
 *  Header for the current file
 *----------------------------------------------------------------------------*/

#include "cs_lagr_sde.h"

/*----------------------------------------------------------------------------*/

BEGIN_C_DECLS

/*! \cond DOXYGEN_SHOULD_SKIP_THIS */

/*============================================================================
 * Static global variables
 *============================================================================*/

/* Boltzmann constant */
static const double _k_boltz = 1.38e-23;

/*! (DOXYGEN_SHOULD_SKIP_THIS) \endcond */

/*============================================================================
 * Private function prototypes (definitions follow)
 *============================================================================*/

/*============================================================================
 * Private function definitions
 *============================================================================*/

/* ------------------------------------------------------------------------------*/
/* INTEGRATION DES EDS PAR UN SCHEMA D'ORDRE 1 (scheO1)
 *
 * Parameters
 * \param[in] taup      temps caracteristique dynamique
 * \param[in] tlag      temps caracteristique fluide
 * \param[in] piil      terme dans l'integration des eds up
 * \param[in] bx        caracteristiques de la turbulence
 * \param[in] vagaus    variables aleatoires gaussiennes
 * \param[in] gradpr    pressure gradient
 * \param[in] romp      masse volumique des particules
 * \param[in] fextla    champ de forces exterieur utilisateur (m/s2)
 */
/*------------------------------------------------------------------------------*/

static void
_lages1(cs_real_t     dtp,
        cs_real_t    *taup,
        cs_real_3_t  *tlag,
        cs_real_3_t  *piil,
        cs_real_t    *bx,
        cs_real_33_t *vagaus,
        cs_real_3_t  *gradpr,
        cs_real_t    *romp,
        cs_real_t    *brgaus,
        cs_real_t    *terbru,
        cs_real_3_t  *fextla)
{
  /* Particles management */
  cs_lagr_particle_set_t  *p_set = cs_glob_lagr_particle_set;
  const cs_lagr_attribute_map_t  *p_am = p_set->p_am;

  cs_lagr_extra_module_t *extra = cs_get_lagr_extra_module();

  /* Initialisations*/
  cs_real_3_t grav = {cs_glob_physical_constants->gx,
                      cs_glob_physical_constants->gy,
                      cs_glob_physical_constants->gz};

  cs_real_t tkelvi =  273.15;

  cs_real_t vitf = 0.0;

  cs_real_t aux1, aux2, aux3, aux4, aux5, aux6, aux7, aux8, aux9, aux10, aux11;
  cs_real_t ter1f, ter2f, ter3f;
  cs_real_t ter1p, ter2p, ter3p, ter4p, ter5p;
  cs_real_t ter1x, ter2x, ter3x, ter4x, ter5x;
  cs_real_t p11, p21, p22, p31, p32, p33;
  cs_real_t omega2, gama2, omegam;
  cs_real_t grga2, gagam, gaome;
  cs_real_t tbrix1, tbrix2, tbriu;

  cs_lnum_t nor = cs_glob_lagr_time_step->nor;

  /* Integration des eds sur les particules */

  for (cs_lnum_t ip = 0; ip < p_set->n_particles; ip++) {

    unsigned char *particle = p_set->p_buffer + p_am->extents * ip;

    cs_lnum_t cell_id = cs_lagr_particle_get_cell_id(particle, p_am);

    if (cell_id >= 0) {

      cs_real_t *old_part_vel      = cs_lagr_particle_attr_n(particle, p_am, 1,
                                                             CS_LAGR_VELOCITY);
      cs_real_t *old_part_vel_seen = cs_lagr_particle_attr_n(particle, p_am, 1,
                                                             CS_LAGR_VELOCITY_SEEN);
      cs_real_t *part_vel          = cs_lagr_particle_attr(particle, p_am,
                                                           CS_LAGR_VELOCITY);
      cs_real_t *part_vel_seen     = cs_lagr_particle_attr(particle, p_am,
                                                           CS_LAGR_VELOCITY_SEEN);
      cs_real_t *part_coords       = cs_lagr_particle_attr(particle, p_am,
                                                           CS_LAGR_COORDS);
      cs_real_t *old_part_coords   = cs_lagr_particle_attr_n(particle, p_am, 1,
                                                             CS_LAGR_COORDS);

      cs_real_t rom = extra->cromf->val[cell_id];

      for (cs_lnum_t id = 0; id < 3; id++) {

        vitf = extra->vel->vals[1][cell_id * 3 + id];

        /* --> (2.1) Calcul preliminaires :    */
        /* ----------------------------   */
        /* calcul de II*TL+<u> et [(grad<P>/rhop+g)*tau_p+<Uf>] ?  */

        cs_real_t tci = piil[ip][id] * tlag[ip][id] + vitf;
        cs_real_t force = 0.0;

        if (cs_glob_lagr_time_scheme->iadded_mass == 0)
          force = (- gradpr[cell_id][id] / romp[ip]
                   + grav[id] + fextla[ip][id]) * taup[ip];

        /* Added-mass term?     */
        else
          force =   (- gradpr[cell_id][id] / romp[ip]
                     * (1.0 + 0.5 * cs_glob_lagr_time_scheme->added_mass_const)
                  / (1.0 + 0.5 * cs_glob_lagr_time_scheme->added_mass_const * rom / romp[ip])
                  + grav[id] + fextla[ip][id]) * taup[ip];


        /* --> (2.2) Calcul des coefficients/termes deterministes */
        /* ----------------------------------------------------    */

        aux1 = exp(-dtp / taup[ip]);
        aux2 = exp(-dtp / tlag[ip][id]);
        aux3 = tlag[ip][id] / (tlag[ip][id] - taup[ip]);
        aux4 = tlag[ip][id] / (tlag[ip][id] + taup[ip]);
        aux5 = tlag[ip][id] * (1.0 - aux2);
        aux6 = pow(bx[p_set->n_particles * (3 * nor + id) + ip], 2.0) * tlag[ip][id];
        aux7 = tlag[ip][id] - taup[ip];
        aux8 = pow(bx[p_set->n_particles * (3 * nor + id) + ip], 2.0) * pow(aux3, 2);

        /* --> trajectory terms */
        cs_real_t aa = taup[ip] * (1.0 - aux1);
        cs_real_t bb = (aux5 - aa) * aux3;
        cs_real_t cc = dtp - aa - bb;

        ter1x = aa * old_part_vel[id];
        ter2x = bb * old_part_vel_seen[id];
        ter3x = cc * tci;
        ter4x = (dtp - aa) * force;

        /* --> flow-seen velocity terms   */
        ter1f = old_part_vel_seen[id] * aux2;
        ter2f = tci * (1.0 - aux2);

        /* --> termes pour la vitesse des particules     */
        cs_real_t dd = aux3 * (aux2 - aux1);
        cs_real_t ee = 1.0 - aux1;

        ter1p = old_part_vel[id] * aux1;
        ter2p = old_part_vel_seen[id] * dd;
        ter3p = tci * (ee - dd);
        ter4p = force * ee;

        /* --> (2.3) Coefficients computation for the stochastic integral    */
        /* --> Integral for particles position */
        gama2  = 0.5 * (1.0 - aux2 * aux2);
        omegam = (0.5 * aux4 * (aux5 - aux2 * aa) - 0.5 * aux2 * bb) * sqrt(aux6);
        omega2 =  aux7 * (aux7 * dtp - 2.0 * (tlag[ip][id] * aux5 - taup[ip] * aa))
                 + 0.5 * tlag[ip][id] * tlag[ip][id] * aux5 * (1.0 + aux2)
                 + 0.5 * taup[ip] * taup[ip] * aa * (1.0 + aux1)
                 - 2.0 * aux4 * tlag[ip][id] * taup[ip] * taup[ip] * (1.0 - aux1 * aux2);
        omega2 = aux8 * omega2;

        if (CS_ABS(gama2) > cs_math_epzero) {

          p21 = omegam / sqrt(gama2);
          p22 = omega2 - pow(p21, 2);
          p22 = sqrt(CS_MAX(0.0, p22));

        }
        else {

          p21 = 0.0;
          p22 = 0.0;

        }

        ter5x = p21 * vagaus[ip][id][0] + p22 * vagaus[ip][id][1];

        /* --> integral for the flow-seen velocity  */
        p11   = sqrt(gama2 * aux6);
        ter3f = p11 * vagaus[ip][id][0];

        /* --> integral for the particles velocity  */
        aux9  = 0.5 * tlag[ip][id] * (1.0 - aux2 * aux2);
        aux10 = 0.5 * taup[ip] * (1.0 - aux1 * aux1);
        aux11 =   taup[ip] * tlag[ip][id]
                * (1.0 - aux1 * aux2)
                / (taup[ip] + tlag[ip][id]);

        grga2 = (aux9 - 2.0 * aux11 + aux10) * aux8;
        gagam = (aux9 - aux11) * (aux8 / aux3);
        gaome = ( (tlag[ip][id] - taup[ip]) * (aux5 - aa)
                  - tlag[ip][id] * aux9
                  - taup[ip] * aux10
                  + (tlag[ip][id] + taup[ip]) * aux11)
                * aux8;

        if (p11 > cs_math_epzero)
          p31 = gagam / p11;
        else
          p31 = 0.0;

        if (p22 > cs_math_epzero)
          p32 = (gaome - p31 * p21) / p22;
        else
          p32 = 0.0;

        p33 = grga2 - pow(p31, 2) - pow(p32, 2);
        p33 = sqrt(CS_MAX(0.0, p33));
        ter5p = p31 * vagaus[ip][id][0] + p32 * vagaus[ip][id][1] + p33 * vagaus[ip][id][2];

        /* --> (2.3) Calcul des Termes dans le cas du mouvement Brownien :   */
        if (cs_glob_lagr_brownian->lamvbr == 1) {

          /* Calcul de la temperature du fluide en fonction du type  */
          /* d'ecoulement    */
          cs_real_t tempf;
          if (   cs_glob_physical_model_flag[CS_COMBUSTION_COAL] >= 0
              || cs_glob_physical_model_flag[CS_COMBUSTION_PCLC] >= 0)
            tempf = extra->t_gaz->val[cell_id];

          else if (   cs_glob_physical_model_flag[CS_COMBUSTION_3PT] >= 0
                   || cs_glob_physical_model_flag[CS_COMBUSTION_EBU] >= 0
                   || cs_glob_physical_model_flag[CS_ELECTRIC_ARCS] >= 0
                   || cs_glob_physical_model_flag[CS_JOULE_EFFECT] >= 0)
            tempf = extra->temperature->val[cell_id];

          else if (   cs_glob_thermal_model->itherm == 1
                   && cs_glob_thermal_model->itpscl == 2)
            tempf = extra->scal_t->val[cell_id] + tkelvi;

          else if (   cs_glob_thermal_model->itherm == 1
                   && cs_glob_thermal_model->itpscl == 1)
            tempf = extra->scal_t->val[cell_id];

          else if (cs_glob_thermal_model->itherm == 2){

            cs_lnum_t mode  = 1;
            CS_PROCF (usthht,USTHHT) (&mode, &(extra->scal_t->val[cell_id]), &tempf);

            tempf = tempf + tkelvi;

          }

          else
            tempf = cs_glob_fluid_properties->t0;

          cs_real_t p_mass = cs_lagr_particle_get_real(particle, p_am, CS_LAGR_MASS);

          cs_real_t ddbr = sqrt(2.0 * _k_boltz * tempf / (p_mass * taup[ip]));

          cs_real_t tix2 = pow((taup[ip] * ddbr), 2) * (dtp - taup[ip] * (1.0 - aux1) * (3.0 - aux1) / 2.0);
          cs_real_t tiu2 = ddbr * ddbr * taup[ip] * (1.0 - exp( -2.0 * dtp / taup[ip])) / 2.0;

          cs_real_t tixiu  = pow((ddbr * taup[ip] * (1.0 - aux1)), 2) / 2.0;

          tbrix2 = tix2 - (tixiu * tixiu) / tiu2;

          if (tbrix2 > 0.0)
            tbrix2    = sqrt(tbrix2) * brgaus[ip * 6 + id];
          else
            tbrix2    = 0.0;

          if (tiu2 > 0.0)
            tbrix1    = tixiu / sqrt(tiu2) * brgaus[ip * 6 + id + 3];
          else
            tbrix1    = 0.0;

          if (tiu2 > 0.0) {

            tbriu      = sqrt(tiu2) * brgaus[ip * 6 + id + 3];
            terbru[ip] = sqrt(tiu2);

          }
          else {

            tbriu     = 0.0;
            terbru[ip]  = 0.0;

          }

        }
        else {

          tbrix1  = 0.0;
          tbrix2  = 0.0;
          tbriu   = 0.0;

        }

        /* Finalisation des ecritures */

        /* --> trajectoire */
        part_coords[id] = old_part_coords[id] + ter1x + ter2x + ter3x
                                              + ter4x + ter5x + tbrix1 + tbrix2;

        /* --> vitesse fluide vu     */
        part_vel_seen[id] = ter1f + ter2f + ter3f;

        /* --> vitesse particules    */
        part_vel[id] = ter1p + ter2p + ter3p + ter4p + ter5p + tbriu;

      }


    }

  }
}

/* ==============================================================================
 * INTEGRATION DES EDS PAR UN SCHEMA D'ORDRE 2 (sche2c)
 *    Lorsqu'il y a eu interaction avec une face de bord,
 *    les calculs de la vitesse de la particule et de
 *    la vitesse du fluide vu sont forcement a l'ordre 1
 *    (meme si on est a l'ordre 2 par ailleurs).
 * --------------------------------------------------------------------
 * Parameters
 * \param[in] taup      temps caracteristique dynamique
 * \param[in] tlag      temps caracteristique fluide
 * \param[in] piil      terme dans l'integration des eds up
 * \param[in] bx        caracteristiques de la turbulence
 * \param[in] tsfext    infos pour couplage retour dynamique
 * \param[in] vagaus    variables aleatoires gaussiennes
 * \param[in] gradpr    pressure gradient
 * \param[in] romp      masse volumique des particules
 * \param[in] brgaus
 * \param[in] terbru
 * \param[in] fextla    champ de forces exterieur utilisateur (m/s2)
 * -----------------------------------------------------------------------------*/

static void
_lages2(cs_real_t     dtp,
        cs_real_t    *taup,
        cs_real_3_t  *tlag,
        cs_real_3_t  *piil,
        cs_real_t    *bx,
        cs_real_t    *tsfext,
        cs_real_33_t *vagaus,
        cs_real_3_t  *gradpr,
        cs_real_t    *romp,
        cs_real_t    *brgaus,
        cs_real_t    *terbru,
        cs_real_3_t  *fextla)
{
  cs_real_t  aux0, aux1, aux2, aux3, aux4, aux5, aux6, aux7, aux8, aux9;
  cs_real_t  aux10, aux11, aux12, aux14, aux15, aux16, aux17, aux18, aux19;
  cs_real_t  aux20;
  cs_real_t  ter1, ter2, ter3, ter4, ter5;
  cs_real_t  sige, tapn, gamma2, omegam;
  cs_real_t  omega2, grgam2, gagam, gaome;
  cs_real_t  p11, p21, p22, p31, p32, p33;
  cs_real_t  tbriu;


  /* Particles management */
  cs_lagr_particle_set_t  *p_set = cs_glob_lagr_particle_set;
  const cs_lagr_attribute_map_t  *p_am = p_set->p_am;

  cs_lagr_extra_module_t *extra = cs_get_lagr_extra_module();

  /* ====================================================================   */
  /* 0.  GESTION MEMOIRE  */
  /* ====================================================================   */

  /* ==============================================================================*/
  /* 1. INITIALISATIONS                                                            */
  /* ==============================================================================*/
  cs_real_t grav[] = {cs_glob_physical_constants->gx,
                      cs_glob_physical_constants->gy,
                      cs_glob_physical_constants->gz};

  cs_lnum_t nor = cs_glob_lagr_time_step->nor;

  cs_real_t *auxl;
  BFT_MALLOC(auxl, p_set->n_particles*6, cs_real_t);

  /* =============================================================================
   * 2. INTEGRATION DES EDS SUR LES PARTICULES
   * =============================================================================*/

  /* =============================================================================
   * 2.1 CALCUL A CHAQUE SOUS PAS DE TEMPS
   * ==============================================================================*/
  /* --> Calcul de tau_p*A_p et de II*TL+<u> :
   *     -------------------------------------*/

  for (cs_lnum_t id = 0; id < 3; id++) {

    for (cs_lnum_t ip = 0; ip < p_set->n_particles; ip++) {

      unsigned char *particle = p_set->p_buffer + p_am->extents * ip;

      cs_lnum_t cell_id = cs_lagr_particle_get_cell_id(particle, p_am);

      if (cell_id >= 0) {

        cs_real_t rom   = extra->cromf->val[cell_id];

        if (cs_glob_lagr_time_scheme->iadded_mass == 0)
          auxl[ip * 6 + id] = (- gradpr[cell_id][id] / romp[ip] + grav[id] + fextla[ip][id]) * taup[ip];

        /* Added-mass term?     */
        else
          auxl[ip * 6 + id] =   ( - gradpr[cell_id][id] / romp[ip]
                                  * (1.0 + 0.5 * cs_glob_lagr_time_scheme->added_mass_const)
                                  / (1.0 + 0.5 * cs_glob_lagr_time_scheme->added_mass_const * rom / romp[ip])
                                  + grav[id] + fextla[ip][id] )
                              * taup[ip];

        auxl[ip * 6 + id + 3] =   piil[ip][id] * tlag[ip][id]
                                + extra->vel->vals[nor][cell_id];

      }

    }

  }

  /* ==============================================================================*/
  /* 2.2 ETAPE DE PREDICTION : */
  /* ==============================================================================*/

  if (nor == 1) {

    /* --> Sauvegarde de tau_p^n */
    for (cs_lnum_t ip = 0; ip < p_set->n_particles; ip++) {

      unsigned char *particle = p_set->p_buffer + p_am->extents * ip;

      if (cs_lagr_particle_get_real(particle, p_am, CS_LAGR_CELL_NUM) >= 0)
        cs_lagr_particle_set_real(particle, p_am, CS_LAGR_TAUP_AUX, taup[ip]);

    }

    /* --> Sauvegarde couplage   */
    if (cs_glob_lagr_time_scheme->iilagr == 2) {

      for (cs_lnum_t ip = 0; ip < p_set->n_particles; ip++) {

        unsigned char *particle = p_set->p_buffer + p_am->extents * ip;

        if (cs_lagr_particle_get_real(particle, p_am, CS_LAGR_CELL_NUM) >= 0) {

          aux0     = -dtp / taup[ip];
          aux1     =  exp (aux0);
          tsfext[ip]      =   taup[ip]
                            * cs_lagr_particle_get_real(particle, p_am, CS_LAGR_MASS)
                            * (-aux1 + (aux1 - 1.0) / aux0);

        }

      }

    }

    /* --> Chargement des termes a t = t_n :    */
    for (cs_lnum_t id = 0; id < 3; id++) {

      for (cs_lnum_t ip = 0; ip < p_set->n_particles; ip++) {

        unsigned char *particle = p_set->p_buffer + p_am->extents * ip;

        cs_lnum_t cell_id = cs_lagr_particle_get_cell_id(particle, p_am);

        if (cell_id >= 0) {

          cs_real_t *old_part_vel      = cs_lagr_particle_attr_n(particle, p_am,
                                                                 1, CS_LAGR_VELOCITY);
          cs_real_t *old_part_vel_seen = cs_lagr_particle_attr_n(particle, p_am,
                                                                 1, CS_LAGR_VELOCITY_SEEN);
          cs_real_t *pred_part_vel_seen = cs_lagr_particle_attr(particle, p_am,
                                                                CS_LAGR_PRED_VELOCITY_SEEN);
          cs_real_t *pred_part_vel = cs_lagr_particle_attr(particle, p_am,
                                                           CS_LAGR_PRED_VELOCITY);


          aux0    =  -dtp / taup[ip];
          aux1    =  -dtp / tlag[ip][id];
          aux2    = exp(aux0);
          aux3    = exp(aux1);
          aux4    = tlag[ip][id] / (tlag[ip][id] - taup[ip]);
          aux5    = aux3 - aux2;

          pred_part_vel_seen[id] =   0.5 * old_part_vel_seen[id]
                                   * aux3 + auxl[ip * 6 + id + 3]
                                   * (-aux3 + (aux3 - 1.0) / aux1);

          ter1    = 0.5 * old_part_vel[id] * aux2;
          ter2    = 0.5 * old_part_vel_seen[id] * aux4 * aux5;
          ter3    = auxl[ip * 6 + id + 3] * (-aux2 + ((tlag[ip][id] + taup[ip]) / dtp) * (1.0 - aux2)
                                             - (1.0 + tlag[ip][id] / dtp) * aux4 * aux5);
          ter4    = auxl[ip * 6 + id] * ( -aux2 + (aux2 - 1.0) / aux0);
          pred_part_vel[id] = ter1 + ter2 + ter3 + ter4;

        }

      }

    }

    /* --> Schema d'Euler : */
    _lages1 (dtp, taup, tlag, piil, bx, vagaus, gradpr, romp, brgaus, terbru, fextla);
  }

  else {

    /* ==============================================================================*/
    /* 2.2 ETAPE DE CORRECTION :                                                     */
    /* ==============================================================================*/
    /* --> Calcul de Us :                                                            */

    for (cs_lnum_t id = 0; id < 3; id++) {

      for (cs_lnum_t ip = 0; ip < p_set->n_particles; ip++) {

        unsigned char *particle = p_set->p_buffer + p_am->extents * ip;

        cs_lnum_t cell_id = cs_lagr_particle_get_cell_id(particle, p_am);

        if (    cell_id >= 0
             && cs_lagr_particle_get_lnum(particle, p_am,CS_LAGR_SWITCH_ORDER_1) == 0) {

          cs_real_t *part_vel
            = cs_lagr_particle_attr(particle, p_am, CS_LAGR_VELOCITY);
          cs_real_t *part_vel_seen
            = cs_lagr_particle_attr(particle, p_am, CS_LAGR_VELOCITY_SEEN);
          cs_real_t *old_part_vel
            = cs_lagr_particle_attr_n(particle, p_am, 1, CS_LAGR_VELOCITY);
          cs_real_t *old_part_vel_seen
            = cs_lagr_particle_attr_n(particle, p_am, 1, CS_LAGR_VELOCITY_SEEN);
          cs_real_t *pred_part_vel_seen
            = cs_lagr_particle_attr(particle, p_am, CS_LAGR_PRED_VELOCITY_SEEN);
          cs_real_t *pred_part_vel
            = cs_lagr_particle_attr(particle, p_am, CS_LAGR_PRED_VELOCITY);

          aux0    =  -dtp / taup[ip];
          aux1    =  -dtp / tlag[ip][id];
          aux2    = exp(aux0);
          aux3    = exp(aux1);
          aux4    = tlag[ip][id] / (tlag[ip][id] - taup[ip]);
          aux5    = aux3 - aux2;
          aux6    = aux3 * aux3;

          ter1    = 0.5 * old_part_vel_seen[id] * aux3;
          ter2    = auxl[ip * 6 + id + 3] * (1.0 - (aux3 - 1.0) / aux1);
          ter3    =  -aux6 + (aux6 - 1.0) / (2.0 * aux1);
          ter4    = 1.0 - (aux6 - 1.0) / (2.0 * aux1);

          sige    =   (  ter3 * bx[p_set->n_particles * (3 * (nor-1) + id) + ip]
                       + ter4 * bx[p_set->n_particles * (3 * nor + id) + ip])
                    * (1.0 / (1.0 - aux6));

          ter5    = 0.5 * tlag[ip][id] * (1.0 - aux6);


          part_vel_seen[id] = pred_part_vel_seen[id] + ter1 + ter2 + sige * sqrt(ter5) * vagaus[ip][id][0];

          /* --> Calcul de Up :   */
          ter1    = 0.5 * old_part_vel[id] * aux2;
          ter2    = 0.5 * old_part_vel_seen[id] * aux4 * aux5;
          ter3    = auxl[ip * 6 + id + 3]
            * (1.0 - ((tlag[ip][id] + taup[ip]) / dtp) * (1.0 - aux2) + (tlag[ip][id] / dtp) * aux4 * aux5)
            + auxl[ip * 6 + id] * (1.0 - (aux2 - 1.0) / aux0);

          tapn    = cs_lagr_particle_get_real(particle, p_am, CS_LAGR_TAUP_AUX);

          aux7    = exp( -dtp / tapn);
          aux8    = 1.0 - aux3 * aux7;
          aux9    = 1.0 - aux6;
          aux10   = 1.0 - aux7 * aux7;
          aux11   = tapn / (tlag[ip][id] + tapn);
          aux12   = tlag[ip][id] / (tlag[ip][id] - tapn);
          aux14   = tlag[ip][id] - tapn;
          aux15   = tlag[ip][id] * (1.0 - aux3);
          aux16   = tapn * (1.0 - aux7);
          aux17   = sige * sige * aux12 * aux12;
          aux18   = 0.5 * tlag[ip][id] * aux9;
          aux19   = 0.5 * tapn * aux10;
          aux20   = tlag[ip][id] * aux11 * aux8;

          /* ---> calcul de la matrice de correlation */
          gamma2  = sige * sige * aux18;
          grgam2  = aux17 * (aux18 - 2.0 * aux20 + aux19);
          gagam   = sige * sige * aux12 * (aux18 - aux20);
          omega2  = aux17 * (aux14 * (aux14 * dtp - 2.0 * tlag[ip][id] * aux15 + 2.0 * tapn * aux16) + tlag[ip][id] * tlag[ip][id] * aux18 + tapn * tapn * aux19- 2.0 * tlag[ip][id] * tapn * aux20);
          omegam  = aux14 * (1.0 - aux3) - aux18 + tapn * aux11 * aux8;
          omegam  = omegam * sige * sige * aux12 * tlag[ip][id];
          gaome   = aux17 * (aux14 * (aux15 - aux16) - tlag[ip][id] * aux18 - tapn * aux19 + tapn * tlag[ip][id] * aux8);

          /* ---> simulation du vecteur Gaussien */

          p11     = sqrt(CS_MAX(0.0, gamma2));
          if (p11 > cs_math_epzero) {

            p21  = omegam / p11;
            p22  = omega2 - p21 * p21;
            p22  = sqrt(CS_MAX(0.0, p22));

          }
          else {

            p21  = 0.0;
            p22  = 0.0;

          }

          if (p11 > cs_math_epzero)
            p31  = gagam / p11;
          else
            p31  = 0.0;

          if (p22 > cs_math_epzero)
            p32  = (gaome - p31 * p21) / p22;
          else
            p32  = 0.0;

          p33     = grgam2 - p31 * p31 - p32 * p32;
          p33     = sqrt(CS_MAX(0.0, p33));
          ter4    = p31 * vagaus[ip][id][0] + p32 * vagaus[ip][id][1] + p33 * vagaus[ip][id][2];

          /* ---> Calcul des Termes dans le cas du mouvement Brownien     */
          if (cs_glob_lagr_brownian->lamvbr == 1)
            tbriu = terbru[ip] * brgaus[ip * 6 + id + 3];
          else
            tbriu = 0.0;

          /* ---> finalisation de l'ecriture     */

          part_vel[id] = pred_part_vel[id] + ter1 + ter2 + ter3 + ter4 + tbriu;

        }

      }

    }

  }

  BFT_FREE(auxl);
}

/* ----------------------------------------------------------------------------
 *
 * Deposition submodel:
 *  1/ Modification of the coordinate system (global ->local)
 *  2/ Call of subroutine lagcli
 *  3/ Integration of the stochastic differential equations
 *     in the 2 directions different from the normal to the boundary face
 *  4/ Modification of the coordinate system (local ->global)
 *  5/ Update of the particle position
 *
 * parameters:
 *    ifac              <--
 *    ip                <--
 *    taup(nbpart)      <--     temps caracteristique dynamique
 *    piil(nbpart,3)    <--     terme dans l'integration des eds up
 *    vagaus            <--     variables aleatoires gaussiennes
 *    (nbpart,nvgaus)
 *    gradpr(3,ncel)    <--     gradient de pression
 *    romp              <--     masse volumique des particules
 *    tempf             <--     temperature of the fluid (K)
 *    romf              <--     density of the fluid
 *    ustar             <--     friction velocity
 *    lvisq             <--     wall-unit lengthscale
 *    tvisq             <--     wall-unit timescale
 *    depint            <--     interface location near-wall/core-flow
 * ----------------------------------------------------------------------------- */

static void
_lagesd(cs_real_t     dtp,
        cs_lnum_t    *ifac,
        cs_lnum_t    *ip,
        cs_real_t    *taup,
        cs_real_3_t  *piil,
        cs_real_33_t *vagaus,
        cs_real_3_t  *gradpr,
        cs_real_t    *romp,
        cs_real_t    *tempf,
        cs_real_t    *romf,
        cs_real_t    *ustar,
        cs_real_t    *lvisq,
        cs_real_t    *tvisq,
        cs_real_t    *depint)
{
  /* mesh and mesh quantities */
  cs_mesh_quantities_t *mq = cs_glob_mesh_quantities;

  /* Particles management */
  cs_lagr_particle_set_t  *p_set = cs_glob_lagr_particle_set;
  const cs_lagr_attribute_map_t  *p_am = p_set->p_am;

  cs_lagr_extra_module_t *extra = cs_get_lagr_extra_module();

  /* Hydrodynamic drag on a deposited particle     */
  cs_real_t drag[3];

  /* Hydrodynamic torque on a deposited particle   */
  cs_real_t tordrg[3], tordrg_norm;

  /* Map field arrays     */
  cs_real_t *vela = extra->vel->vals[1];

  /* ===========================================================================
   * 1. INITIALISATIONS
   * ======================================================================== */

  cs_real_3_t grav = {cs_glob_physical_constants->gx,
                      cs_glob_physical_constants->gy,
                      cs_glob_physical_constants->gz};

  /* particle data */
  unsigned char *particle = p_set->p_buffer + p_am->extents * *ip;

  cs_real_t p_mass = cs_lagr_particle_get_real(particle, p_am,
                                               CS_LAGR_MASS);
  cs_real_t p_diam = cs_lagr_particle_get_real(particle, p_am,
                                               CS_LAGR_DIAMETER);
  cs_real_t p_stat_w = cs_lagr_particle_get_real(particle, p_am,
                                                 CS_LAGR_STAT_WEIGHT);

  cs_lnum_t cell_id = cs_lagr_particle_get_cell_id(particle, p_am);

  /* Friction velocity    */

  *ifac  = cs_lagr_particle_get_lnum(particle, p_am,
                                     CS_LAGR_NEIGHBOR_FACE_ID);
  *ustar = extra->uetbor[*ifac];

  /* Constants for the calculation of bxp and tlp  */
  cs_real_t c0 = 2.1;
  cs_real_t cl = 1.0 / (0.5 + (3.0 / 4.0) * c0);

  /* Pointer on the density w.r.t the flow    */
  *romf = extra->cromf->val[cell_id];

  cs_real_t visccf = extra->viscl->val[cell_id] / *romf;

  cs_real_t yplus = cs_lagr_particle_get_real(particle, p_am,
                                              CS_LAGR_YPLUS);

  /* Turbulent kinetic energy and dissipation w.r.t y+  */
  cs_real_t energi, dissip;
  if (yplus <= 5.0) {

    energi = 0.1 * (pow (yplus, 2)) * pow (*ustar, 2);
    dissip = 0.2 * pow (*ustar, 4) / visccf;

  }

  else if (   yplus > 5.0 && yplus <= 30.0) {

    energi = pow (*ustar, 2) / pow ((0.09), 0.5);
    dissip = 0.2 * pow (*ustar, 4) / visccf;

  }
  else if (   yplus > 30.0 && yplus <= 100.0) {

    energi   = pow (*ustar, 2) / pow ((0.09), 0.5);
    dissip   = pow (*ustar, 4) / (0.41 * yplus * visccf);

  }

  /* ===========================================================================
   * 2. Reference frame change:
   * --------------------------
   * global reference frame --> local reference frame for the boundary face
   * ======================================================================== */

  const cs_real_3_t *rot_m
    = (const cs_real_3_t *)cs_glob_lagr_b_face_proj[*ifac];

  /* 2.1 - particle velocity   */

  cs_real_t *old_part_vel = cs_lagr_particle_attr_n(particle, p_am, 1,
                                                    CS_LAGR_VELOCITY);
  cs_real_t vpart[3];

  cs_math_33_3_product(rot_m, old_part_vel, vpart);

  cs_real_t vpart0[3] = {vpart[0], vpart[1], vpart[2]};

  /* 2.2 - flow-seen velocity  */

  cs_real_t *old_part_vel_seen = cs_lagr_particle_attr_n(particle, p_am, 1,
                                                         CS_LAGR_VELOCITY_SEEN);
  cs_real_t vvue[3];

  cs_math_33_3_product(rot_m, old_part_vel_seen, vvue);

  /* 2.3 - Gravity vector */

  cs_real_t ggp[3];

  cs_math_33_3_product(rot_m, grav, ggp);

  /* 2.4 - flow velocity  */

  cs_real_t vflui[3];

  cs_math_33_3_product(rot_m, vela, vflui);

  cs_real_t norm = sqrt(pow(vflui[1],2)+pow(vflui[2],2));

  /* Velocity norm w.r.t y+    */
  cs_real_t norm_vit;

  if (yplus <= 5.0)
    norm_vit = yplus * *ustar;

  else if (   yplus > 5.0 && yplus <= 30.0)
    norm_vit = ( -3.05 + 5.0 * log(yplus)) * *ustar;

  else if (   yplus > 30.0
           && yplus < 100.0)
    norm_vit = (2.5 * log (yplus) + 5.5) * *ustar;

  if (norm_vit > 0.) {
    vflui[1] = norm_vit * vflui[1] / norm;
    vflui[2] = norm_vit * vflui[2] / norm;
  }
  else {
    vflui[1] = 0.;
    vflui[2] = 0.;
  }

  /* 2.5 - pressure gradient   */

  cs_real_t gdpr[3];

  cs_math_33_3_product(rot_m, gradpr[cell_id], gdpr);

  /* 2.6 - "piil" term    */

  cs_real_t piilp[3];

  cs_math_33_3_product(rot_m, piil[*ip], piilp);

  /* 2.7 - tlag */

  cs_real_t tlp = cs_math_epzero;
  if (energi > 0.0) {

    tlp = cl * energi / dissip;
    tlp = CS_MAX(tlp, cs_math_epzero);

  }

  /* 2.8 - bx   */
  cs_real_t bxp = sqrt(c0 * dissip);

  /* =========================================================================
   * 3. Integration of the EDS on the particles
   * =========================================================================*/

  /* Retrieve of the turbulent kinetic energy */
  cs_real_t  enertur;
  if (extra->itytur == 2 || extra->iturb == 50 || extra->iturb == 60)
    enertur  = extra->cvar_k->vals[1][cell_id];

  else if (extra->itytur == 3)
    enertur  = 0.5 * (  extra->cvar_r11->vals[1][cell_id]
                      + extra->cvar_r22->vals[1][cell_id]
                      + extra->cvar_r33->vals[1][cell_id]);

  cs_lnum_t marko  = cs_lagr_particle_get_lnum(particle, p_am,
                                               CS_LAGR_MARKO_VALUE);
  cs_real_t interf = cs_lagr_particle_get_real(particle, p_am, CS_LAGR_INTERF);
  cs_real_3_t depl;

  cs_lagr_deposition(dtp,
                     &marko,
                     tempf,
                     lvisq,
                     tvisq,
                     &vpart[0],
                     &vvue[0],
                     &depl[0],
                     &p_diam,
                     &romp[*ip],
                     &taup[*ip],
                     &yplus,
                     &interf,
                     &enertur,
                     &ggp[0],
                     &vflui[0],
                     &gdpr[0],
                     &piilp[0],
                     depint);

  if (cs_lagr_particle_get_lnum(particle, p_am, CS_LAGR_DEPOSITION_FLAG) != CS_LAGR_PART_IN_FLOW) {

    depl[0]  = 0.0;
    vpart[0] = 0.0;

  }

  /* Integration in the 2 other directions    */
  if (cs_lagr_particle_get_lnum(particle, p_am,
                                CS_LAGR_DEPOSITION_FLAG) == CS_LAGR_PART_IN_FLOW) {

    for (cs_lnum_t id = 1; id < 3; id++) {

      cs_real_t tci   = piilp[id] * tlp + vflui[id];
      cs_real_t force = (- gdpr[id] / romp[*ip] + ggp[id]) * taup[*ip];
      cs_real_t aux1  = exp ( -dtp / taup[*ip]);
      cs_real_t aux2  = exp ( -dtp / tlp);
      cs_real_t aux3  = tlp / (tlp - taup[*ip]);
      cs_real_t aux4  = tlp / (tlp + taup[*ip]);
      cs_real_t aux5  = tlp * (1.0 - aux2);
      cs_real_t aux6  = bxp * bxp * tlp;
      cs_real_t aux7  = tlp - taup[*ip];
      cs_real_t aux8  = bxp * bxp * pow (aux3, 2);

      /* --> Terms for the trajectory   */
      cs_real_t aa    = taup[*ip] * (1.0 - aux1);
      cs_real_t bb    = (aux5 - aa) * aux3;
      cs_real_t cc    = dtp - aa - bb;
      cs_real_t ter1x = aa * vpart[id];
      cs_real_t ter2x = bb * vvue[id];
      cs_real_t ter3x = cc * tci;
      cs_real_t ter4x = (dtp - aa) * force;

      /* --> Terms for the flow-seen velocity     */
      cs_real_t ter1f = vvue[id] * aux2;
      cs_real_t ter2f = tci * (1.0 - aux2);

      /* --> Terms for the particles velocity     */
      cs_real_t dd    = aux3 * (aux2 - aux1);
      cs_real_t ee    = 1.0 - aux1;
      cs_real_t ter1p = vpart[id] * aux1;
      cs_real_t ter2p = vvue[id] * dd;
      cs_real_t ter3p = tci * (ee - dd);
      cs_real_t ter4p = force * ee;

      /* --> (2.3) Coefficients computation for the stochastic integrals:  */
      cs_real_t gama2  = 0.5 * (1.0 - aux2 * aux2);
      cs_real_t omegam = (0.5 * aux4 * (aux5 - aux2 * aa) - 0.5 * aux2 * bb) * sqrt (aux6);
      cs_real_t omega2 =   aux7
                         * (aux7 * dtp - 2.0 * (tlp * aux5 - taup[*ip] * aa))
                         + 0.5 * tlp * tlp * aux5 * (1.0 + aux2)
                         + 0.5 * pow(taup[*ip], 2.0) * aa * (1.0 + aux1)
                         - 2.0 * aux4 * tlp * pow(taup[*ip],2.0) * (1.0 - aux1 * aux2);
      omega2 *= aux8 ;

      cs_real_t  p11, p21, p22, p31, p32, p33;

      if (CS_ABS(gama2) >cs_math_epzero) {

        p21    = omegam / sqrt (gama2);
        p22    = omega2 - pow (p21, 2);
        p22    = sqrt(CS_MAX(0.0, p22));

      }
      else {

        p21    = 0.0;
        p22    = 0.0;

      }

      cs_real_t ter5x = p21 * vagaus[*ip][id][0] + p22 * vagaus[*ip][id][1];

      /* --> Integral for the flow-seen velocity  */

      p11   = sqrt(gama2 * aux6);
      cs_real_t ter3f = p11 * vagaus[*ip][id][0];

      /* --> Integral for particles velocity */

      cs_real_t aux9  = 0.5 * tlp * (1.0 - aux2 * aux2);
      cs_real_t aux10 = 0.5 * taup[*ip] * (1.0 - aux1 * aux1);
      cs_real_t aux11 = taup[*ip] * tlp * (1.0 - aux1 * aux2) / (taup[*ip] + tlp);
      cs_real_t grga2 = (aux9 - 2.0 * aux11 + aux10) * aux8;
      cs_real_t gagam = (aux9 - aux11) * (aux8 / aux3);
      cs_real_t gaome = (  (tlp - taup[*ip]) * (aux5 - aa)
                         - tlp * aux9
                         - taup[*ip] * aux10
                         + (tlp + taup[*ip]) * aux11) * aux8;

      if (p11 > cs_math_epzero)
        p31 = gagam / p11;

      else
        p31 = 0.0;

      if (p22 > cs_math_epzero)
        p32 = (gaome - p31 * p21) / p22;

      else
        p32 = 0.0;

      p33 = grga2 - pow(p31, 2) - pow(p32, 2);
      p33 = sqrt (CS_MAX(0.0, p33));

      cs_real_t ter5p =   p31 * vagaus[*ip][id][0]
                        + p32 * vagaus[*ip][id][1]
                        + p33 * vagaus[*ip][id][2];

      /* --> trajectory  */
      depl[id] = ter1x + ter2x + ter3x + ter4x + ter5x;

      /* --> flow-seen velocity    */
      vvue[id] = ter1f + ter2f + ter3f;

      /* --> particles velocity    */
      vpart[id] = ter1p + ter2p + ter3p + ter4p + ter5p;

    }

  }
  else {

    for (cs_lnum_t id = 1; id < 3; id++) {

      cs_real_t tci   = piilp[id] * tlp + vflui[id];
      cs_real_t aux2  = exp ( -dtp / tlp);
      cs_real_t aux6  = bxp * bxp * tlp;

      /* --> Terms for the flow-seen velocity     */
      cs_real_t ter1f = vvue[id] * aux2;
      cs_real_t ter2f = tci * (1.0 - aux2);

      /* --> (2.3) Coefficients computation for the stochastic integrals:  */
      cs_real_t gama2 = 0.5 * (1.0 - aux2 * aux2);

      /* --> Integral for the flow-seen velocity  */
      cs_real_t p11   = sqrt (gama2 * aux6);
      cs_real_t ter3f = p11 * vagaus[*ip][id][0];

      /* --> flow-seen velocity    */
      vvue[id] = ter1f + ter2f + ter3f;

    }

  }

  if (cs_glob_lagr_reentrained_model->ireent == 1) {

    if (cs_lagr_particle_get_lnum(particle, p_am,
                                  CS_LAGR_DEPOSITION_FLAG) != CS_LAGR_PART_IN_FLOW) {

      /* Resuspension model
       * Calculation of the hydrodynamic drag and torque
       * applied on the deposited particle   */

      drag[0] =  3.0 * cs_math_pi * p_diam
               * (vvue[0] - vpart[0]) * visccf * *romf * 3.39;
      tordrg[0] = 0.0;

      for (cs_lnum_t id = 1; id < 3; id++) {

        drag[id]   =  3.0 * cs_math_pi * p_diam
                    * (vvue[id] - vpart[id]) * visccf * *romf * 1.7;
        tordrg[id] = 1.4 * drag[id] * p_diam * 0.5;

      }

      /* Is there direct wall-normal lift-off of the particle ?  */
      if (   CS_ABS (drag[0]) > cs_lagr_particle_get_real(particle, p_am,
                                                       CS_LAGR_ADHESION_FORCE)
          && drag[0] < 0.0) {

        /* The particle is resuspended    */
        cs_lagr_particle_set_lnum(particle, p_am, CS_LAGR_DEPOSITION_FLAG, CS_LAGR_PART_IN_FLOW);
        cs_lagr_particle_set_real(particle, p_am, CS_LAGR_ADHESION_FORCE, 0.0);
        cs_lagr_particle_set_real(particle, p_am, CS_LAGR_ADHESION_TORQUE, 0.0);

        if (p_am->count[0][CS_LAGR_N_LARGE_ASPERITIES] > 0)
          cs_lagr_particle_set_lnum(particle, p_am,
                                    CS_LAGR_N_LARGE_ASPERITIES, 0);

        if (p_am->count[0][CS_LAGR_N_SMALL_ASPERITIES] > 0)
          cs_lagr_particle_set_lnum(particle, p_am,
                                    CS_LAGR_N_SMALL_ASPERITIES, 0);

        if (p_am->count[0][CS_LAGR_DISPLACEMENT_NORM] > 0)
          cs_lagr_particle_set_real(particle, p_am,
                                    CS_LAGR_DISPLACEMENT_NORM, 0.0);

        cs_real_t adhesion_force =
          cs_lagr_particle_get_real(particle, p_am, CS_LAGR_ADHESION_FORCE);
        vpart[0] = CS_MIN(-1.0 / p_mass * CS_ABS(drag[0] - adhesion_force) * dtp,
                          0.001);
        vpart[1] = 0.0;
        vpart[2] = 0.0;

        /* Update of the number and weight of resuspended particles     */
        p_set->n_part_resusp += 1;
        p_set->weight_resusp += p_stat_w;

        if (cs_glob_lagr_boundary_interactions->iflmbd == 1) {

          cs_lnum_t n_f_id
            = cs_lagr_particle_get_lnum(particle, p_am, CS_LAGR_NEIGHBOR_FACE_ID);

          cs_lnum_t nfabor = cs_glob_mesh->n_b_faces;

          bound_stat[n_f_id + nfabor * cs_glob_lagr_boundary_interactions->ires] += p_stat_w;

          bound_stat[n_f_id + nfabor * cs_glob_lagr_boundary_interactions->iflres] +=
            p_stat_w + (  p_stat_w * p_mass / mq->b_f_face_surf[n_f_id]);

          bound_stat[n_f_id + nfabor * cs_glob_lagr_boundary_interactions->iflm]  +=
            - (  p_stat_w * p_mass / mq->b_f_face_surf[n_f_id]);

        }

      }
      /* No direct normal lift-off */
      else {

        /* Calculation of the norm of the hydrodynamic
         * torque and drag (tangential) */

        tordrg_norm  = sqrt (pow (tordrg[1], 2) + pow (tordrg[2], 2));

        cs_real_t adh_tor[3];
        adh_tor[1]  = - cs_lagr_particle_get_real(particle, p_am,
                                                  CS_LAGR_ADHESION_TORQUE)
                      / tordrg_norm * tordrg[1];

        adh_tor[2]  = - cs_lagr_particle_get_real(particle, p_am,
                                                  CS_LAGR_ADHESION_TORQUE)
                      / tordrg_norm * tordrg[2];

        cs_real_t cst_1, cst_4;

        for (cs_lnum_t id = 1; id < 3; id++) {

          cs_real_t iner_tor = (7.0 / 5.0) * p_mass * pow ((p_diam * 0.5), 2);

          cst_4 =   6 * cs_math_pi * visccf
                  * *romf * 1.7 * 1.4
                  * pow(p_diam * 0.5, 2);

          cst_1 = cst_4 * (p_diam * 0.5) / iner_tor;

          vpart0[id] = vpart[id];
          vpart[id]  =  (vpart0[id] - vvue[id] - adh_tor[id] / cst_4)
                      * exp ( -cst_1 * dtp)
                      + vvue[id] + adh_tor[id] / cst_4 ;

        }

        cs_real_t scalax  = vpart[1] * vvue[1] + vpart[2] * vvue[2];

        if (scalax > 0.0) {

          /* The calculated particle velocity is aligned   */
          /* with the flow seen   */
          /* --> The particle starts or keep on rolling    */

          cs_lagr_particle_set_lnum(particle, p_am,CS_LAGR_DEPOSITION_FLAG, CS_LAGR_PART_ROLLING);

          vpart[0] = 0.0;

          for (cs_lnum_t id = 1; id < 3; id++) {

            if (CS_ABS (vpart[id]) > CS_ABS (vvue[id]))
              /* The velocity of the rolling particle cannot   */
              /* exceed the surrounding fluid velocity    */
              vpart[id] = vvue[id];

            cs_real_t kk  = vpart0[id] - vvue[id] - adh_tor[id] / cst_4;
            cs_real_t kkk = vvue[id] + adh_tor[id] / cst_4;

            depl[id] =   ( kkk * dtp + kk / cst_1 * (1. - exp ( -cst_1 * dtp)));

          }

        }
        /* if (scalax..) */
        else {

          /* The particle is not set into motion or stops
           * the flag is set to 10 and velocity and displacement are null */

          cs_lagr_particle_set_lnum(particle, p_am,CS_LAGR_DEPOSITION_FLAG, CS_LAGR_PART_NO_MOTION);

          for (cs_lnum_t id = 1; id < 3; id++) {

            depl[id]     = 0.0;
            vpart[id]    = 0.0;

          }

        } /* if (scalax..)   */

      }

    }

  }
  /* if ireent.eq.0 --> Motionless deposited particle   */
  else {

    if (cs_lagr_particle_get_lnum(particle, p_am,
                                  CS_LAGR_DEPOSITION_FLAG) != CS_LAGR_PART_IN_FLOW) {

      for (cs_lnum_t id = 1; id < 3; id++) {

        vpart[id] = 0.0;
        vvue[id]  = 0.0;
        depl[id]  = 0.0;

      }

    }

  }

  /* ===========================================================================
   * 3. Reference frame change:
   * --------------------------
   * local reference frame for the boundary face --> global reference frame
   * ======================================================================== */

  /* 3.1 - Displacement   */

  cs_real_t depg[3];

  cs_math_33t_3_product(rot_m, depl, depg);

  /* 3.2 - Particle velocity   */

  cs_real_t *part_vel = cs_lagr_particle_attr(particle, p_am,
                                              CS_LAGR_VELOCITY);

  cs_math_33t_3_product(rot_m, vpart, part_vel);

  /* 3.3 - flow-seen velocity  */

  cs_real_t *part_vel_seen = cs_lagr_particle_attr(particle, p_am,
                                                   CS_LAGR_VELOCITY_SEEN);

  cs_math_33t_3_product(rot_m, vvue, part_vel_seen);

  /* ===========================================================================
   * 5. Computation of the new particle position
   * ======================================================================== */

  cs_real_t *part_coords = cs_lagr_particle_attr(particle, p_am,
                                                 CS_LAGR_COORDS);
  for (cs_lnum_t id = 0 ; id < 3; id++)
    part_coords[id] += depg[id];

  return;

}

/*--------------------------------------------------------------*/
/* Deposition sub-model:
 *    Main subroutine of the submodel
 *    1/ Calculation of the normalized wall-normal distance of
 *           the boundary-cell particles
 *   2/ Sorting of the particles with respect to their normalized
 *           wall-normal distance
 *         * If y^+ > depint : the standard Langevin model is applied
 *         * If y^+ < depint : the deposition submodel is applied
 *
 * Parameters
 * \param[in] dtp
 * \param[in] taup(nbpart)           temps caracteristique dynamique
 * \param[in] tlag(nbpart)           temps caracteristique fluide
 * \param[in] piil(nbpart,3)         terme dans l'integration des eds up
 * \param[in] bx(nbpart,3,2)         caracteristiques de la turbulence
 * \param[in] vagaus(nbpart,nvgaus)  variables aleatoires gaussiennes
 * \param[in] gradpr(3,ncel)         gradient de pression
 * \param[in] romp                   masse volumique des particules
 * \param[in] fextla(ncelet,3)       champ de forces exterieur utilisateur (m/s2)
 * \param[in] vislen
 */
/*--------------------------------------------------------------*/

static void
_lagdep(cs_real_t     dtp,
        cs_real_t    *taup,
        cs_real_3_t  *tlag,
        cs_real_3_t  *piil,
        cs_real_t    *bx,
        cs_real_33_t *vagaus,
        cs_real_3_t  *gradpr,
        cs_real_t    *romp,
        cs_real_3_t  *fextla,
        cs_real_t    *vislen)
{
  /* Particles management */
  cs_lagr_particle_set_t  *p_set = cs_glob_lagr_particle_set;
  const cs_lagr_attribute_map_t  *p_am = p_set->p_am;
  const cs_mesh_quantities_t  *fvq = cs_glob_mesh_quantities;

  cs_lagr_extra_module_t *extra = cs_get_lagr_extra_module();

  /* Initialisations*/
  cs_real_3_t grav = {cs_glob_physical_constants->gx,
                      cs_glob_physical_constants->gy,
                      cs_glob_physical_constants->gz};

  cs_real_t tkelvi =  273.15;

  cs_real_t vitf = 0.0;

  cs_real_t aux1, aux2, aux3, aux4, aux5, aux6, aux7, aux8, aux9, aux10, aux11;
  cs_real_t ter1f, ter2f, ter3f;
  cs_real_t ter1p, ter2p, ter3p, ter4p, ter5p;
  cs_real_t ter1x, ter2x, ter3x, ter4x, ter5x;
  cs_real_t p11, p21, p22, p31, p32, p33;
  cs_real_t omega2, gama2, omegam;
  cs_real_t grga2, gagam, gaome;

  cs_lnum_t nor = cs_glob_lagr_time_step->nor;


  /* Interface location between near-wall region   */
  /* and core of the flow (normalized units)  */

  cs_real_t depint      = 100.0;

  /* loop on the particles  */
  for (cs_lnum_t ip = 0; ip < p_set->n_particles; ip++) {

    unsigned char *particle = p_set->p_buffer + p_am->extents * ip;

    cs_lnum_t cell_id = cs_lagr_particle_get_cell_id(particle, p_am);

    if (cell_id >= 0 &&
        cs_lagr_particle_get_lnum(particle, p_am, CS_LAGR_DEPOSITION_FLAG)
        != CS_LAGR_PART_IMPOSED_MOTION) {

      cs_real_t *old_part_vel      = cs_lagr_particle_attr_n(particle, p_am, 1,
                                                             CS_LAGR_VELOCITY);
      cs_real_t *old_part_vel_seen = cs_lagr_particle_attr_n(particle, p_am, 1,
                                                             CS_LAGR_VELOCITY_SEEN);
      cs_real_t *part_vel          = cs_lagr_particle_attr(particle, p_am,
                                                           CS_LAGR_VELOCITY);
      cs_real_t *part_vel_seen     = cs_lagr_particle_attr(particle, p_am,
                                                           CS_LAGR_VELOCITY_SEEN);
      cs_real_t *part_coords       = cs_lagr_particle_attr(particle, p_am,
                                                           CS_LAGR_COORDS);
      cs_real_t *old_part_coords   = cs_lagr_particle_attr_n(particle, p_am, 1,
                                                             CS_LAGR_COORDS);

      cs_real_t romf = extra->cromf->val[cell_id];

      /* Fluid temperature computation depending on the type of flow  */
      cs_real_t tempf;

      if (   cs_glob_physical_model_flag[CS_COMBUSTION_COAL] >= 0
          || cs_glob_physical_model_flag[CS_COMBUSTION_PCLC] >= 0
          || cs_glob_physical_model_flag[CS_COMBUSTION_FUEL] >= 0)
        tempf = extra->t_gaz->val[cell_id];

      else if (   cs_glob_physical_model_flag[CS_COMBUSTION_3PT] >= 0
               || cs_glob_physical_model_flag[CS_COMBUSTION_EBU] >= 0
               || cs_glob_physical_model_flag[CS_ELECTRIC_ARCS] >= 0
               || cs_glob_physical_model_flag[CS_JOULE_EFFECT] >= 0)
        tempf = extra->temperature->val[cell_id];

      else if (   cs_glob_thermal_model->itherm == 1
                  && cs_glob_thermal_model->itpscl == 2)
        tempf = extra->scal_t->val[cell_id] + tkelvi;

      else if (   cs_glob_thermal_model->itherm == 1
                  && cs_glob_thermal_model->itpscl == 1)
        tempf = extra->scal_t->val[cell_id];

      else if (cs_glob_thermal_model->itherm == 2){

        cs_lnum_t mode  = 1;
        CS_PROCF (usthht,USTHHT) (&mode, &(extra->scal_t->val[cell_id]), &tempf);

        tempf = tempf + tkelvi;

      }

      else
        tempf = cs_glob_fluid_properties->t0;

      /* ==============================================================    */
      /*   If y^+ is greater than the interface location,   */
      /*   the standard model is applied     */
      /* ==============================================================    */

      if (cs_lagr_particle_get_real(particle, p_am, CS_LAGR_YPLUS) > depint &&
          cs_lagr_particle_get_lnum(particle, p_am, CS_LAGR_DEPOSITION_FLAG)
          == CS_LAGR_PART_IN_FLOW) {

        cs_lagr_particle_set_lnum(particle, p_am, CS_LAGR_MARKO_VALUE, -1);

        for (cs_lnum_t id = 0; id < 3; id++) {

          vitf = extra->vel->vals[1][cell_id * 3 + id];

          /* --> (2.1) Calcul preliminaires :    */
          /* ----------------------------   */
          /* calcul de II*TL+<u> et [(grad<P>/rhop+g)*tau_p+<Uf>] ?  */

          cs_real_t tci = piil[ip][id] * tlag[ip][id] + vitf;
          cs_real_t force = 0.0;

          if (cs_glob_lagr_time_scheme->iadded_mass == 0)
            force = (- gradpr[cell_id][id] / romp[ip]
                     + grav[id] + fextla[ip][id]) * taup[ip];

          /* Added-mass term?     */
          else
            force =   ( - gradpr[cell_id][id] / romp[ip]
                  * (1.0 + 0.5 * cs_glob_lagr_time_scheme->added_mass_const)
                  / (1.0 + 0.5 * cs_glob_lagr_time_scheme->added_mass_const * romf / romp[ip])
                  + grav[id] + fextla[ip][id])* taup[ip];


          /* --> (2.2) Calcul des coefficients/termes deterministes */
          /* ----------------------------------------------------    */

          aux1 = exp(-dtp / taup[ip]);
          aux2 = exp(-dtp / tlag[ip][id]);
          aux3 = tlag[ip][id] / (tlag[ip][id] - taup[ip]);
          aux4 = tlag[ip][id] / (tlag[ip][id] + taup[ip]);
          aux5 = tlag[ip][id] * (1.0 - aux2);
          aux6 = pow(bx[p_set->n_particles * (3 * nor + id) + ip], 2.0) * tlag[ip][id];
          aux7 = tlag[ip][id] - taup[ip];
          aux8 = pow(bx[p_set->n_particles * (3 * nor + id) + ip], 2.0) * pow(aux3, 2);

          /* --> trajectory terms */
          cs_real_t aa = taup[ip] * (1.0 - aux1);
          cs_real_t bb = (aux5 - aa) * aux3;
          cs_real_t cc = dtp - aa - bb;

          ter1x = aa * old_part_vel[id];
          ter2x = bb * old_part_vel_seen[id];
          ter3x = cc * tci;
          ter4x = (dtp - aa) * force;

          /* --> flow-seen velocity terms   */
          ter1f = old_part_vel_seen[id] * aux2;
          ter2f = tci * (1.0 - aux2);

          /* --> termes pour la vitesse des particules     */
          cs_real_t dd = aux3 * (aux2 - aux1);
          cs_real_t ee = 1.0 - aux1;

          ter1p = old_part_vel[id] * aux1;
          ter2p = old_part_vel_seen[id] * dd;
          ter3p = tci * (ee - dd);
          ter4p = force * ee;

          /* --> (2.3) Coefficients computation for the stochastic integral    */
          /* --> Integral for particles position */
          gama2  = 0.5 * (1.0 - aux2 * aux2);
          omegam = (0.5 * aux4 * (aux5 - aux2 * aa) - 0.5 * aux2 * bb) * sqrt(aux6);
          omega2 =  aux7 * (aux7 * dtp - 2.0 * (tlag[ip][id] * aux5 - taup[ip] * aa))
                   + 0.5 * tlag[ip][id] * tlag[ip][id] * aux5 * (1.0 + aux2)
                   + 0.5 * taup[ip] * taup[ip] * aa * (1.0 + aux1)
                   - 2.0 * aux4 * tlag[ip][id] * taup[ip] * taup[ip] * (1.0 - aux1 * aux2);
          omega2 = aux8 * omega2;

          if (CS_ABS(gama2) > cs_math_epzero) {

            p21 = omegam / sqrt(gama2);
            p22 = omega2 - pow(p21, 2);
            p22 = sqrt(CS_MAX(0.0, p22));

          }
          else {

            p21 = 0.0;
            p22 = 0.0;

          }

          ter5x = p21 * vagaus[ip][id][0] + p22 * vagaus[ip][id][1];

          /* --> integral for the flow-seen velocity  */
          p11   = sqrt(gama2 * aux6);
          ter3f = p11 * vagaus[ip][id][0];

          /* --> integral for the particles velocity  */
          aux9  = 0.5 * tlag[ip][id] * (1.0 - aux2 * aux2);
          aux10 = 0.5 * taup[ip] * (1.0 - aux1 * aux1);
          aux11 =   taup[ip] * tlag[ip][id]
                  * (1.0 - aux1 * aux2)
                  / (taup[ip] + tlag[ip][id]);

          grga2 = (aux9 - 2.0 * aux11 + aux10) * aux8;
          gagam = (aux9 - aux11) * (aux8 / aux3);
          gaome = ( (tlag[ip][id] - taup[ip]) * (aux5 - aa)
                    - tlag[ip][id] * aux9
                    - taup[ip] * aux10
                    + (tlag[ip][id] + taup[ip]) * aux11)
                  * aux8;

          if (p11 > cs_math_epzero)
            p31 = gagam / p11;
          else
            p31 = 0.0;

          if (p22 > cs_math_epzero)
            p32 = (gaome - p31 * p21) / p22;
          else
            p32 = 0.0;

          p33 = grga2 - pow(p31, 2) - pow(p32, 2);
          p33 = sqrt(CS_MAX(0.0, p33));
          ter5p = p31 * vagaus[ip][id][0] + p32 * vagaus[ip][id][1] + p33 * vagaus[ip][id][2];

          /*  Update of the particle state-vector     */

          part_coords[id] = old_part_coords[id] + ter1x + ter2x + ter3x + ter4x + ter5x;

          part_vel_seen[id] =  ter1f + ter2f + ter3f;

          part_vel[id]      = ter1p + ter2p + ter3p + ter4p + ter5p;

        }

      }
      /* ====================================================================
       *   Otherwise, the deposition submodel is applied :
       * =====================================================================  */
      else {

        if (  cs_lagr_particle_get_real(particle, p_am,
                                        CS_LAGR_YPLUS)
            < cs_lagr_particle_get_real(particle, p_am,
                                        CS_LAGR_INTERF) ) {

          if (cs_lagr_particle_get_lnum(particle, p_am, CS_LAGR_MARKO_VALUE) < 0)
            cs_lagr_particle_set_lnum(particle, p_am, CS_LAGR_MARKO_VALUE, 10);
          else
            cs_lagr_particle_set_lnum(particle, p_am, CS_LAGR_MARKO_VALUE, 0);

        }
        else {

          if (cs_lagr_particle_get_lnum(particle, p_am, CS_LAGR_MARKO_VALUE) < 0)
            cs_lagr_particle_set_lnum(particle, p_am, CS_LAGR_MARKO_VALUE, 20);

          else if (cs_lagr_particle_get_lnum(particle, p_am, CS_LAGR_MARKO_VALUE) == 0)
            cs_lagr_particle_set_lnum(particle, p_am, CS_LAGR_MARKO_VALUE, 30);

        }

        cs_lnum_t ifac = cs_lagr_particle_get_lnum
                           (particle, p_am, CS_LAGR_NEIGHBOR_FACE_ID);
        cs_real_t ustar = extra->uetbor[ifac];
        cs_real_t lvisq = vislen[ifac];

        cs_real_t tvisq;
        if (ustar > 0.0)
          tvisq = lvisq / ustar;
        else
          tvisq = cs_math_big_r;

        _lagesd(dtp,
                &ifac,
                &ip,
                taup,
                piil,
                vagaus,
                gradpr,
                romp,
                &tempf,
                &romf,
                &ustar,
                &lvisq,
                &tvisq,
                &depint);

      }

    }

    /* Specific treatment for particles with
     * DEPOSITION_FLAG == CS_LAGR_PART_IMPOSED_MOTION */
    else if (cell_id >= 0) {
      cs_real_t disp[3] = {0., 0., 0.};

      cs_real_t *old_part_coords = cs_lagr_particle_attr_n(particle, p_am, 1,
                                                           CS_LAGR_COORDS);
      cs_real_t *part_coords = cs_lagr_particle_attr(particle, p_am,
                                                     CS_LAGR_COORDS);

      cs_real_t *part_vel_seen = cs_lagr_particle_attr(particle, p_am,
                                                       CS_LAGR_VELOCITY_SEEN);

      cs_real_t *part_vel = cs_lagr_particle_attr(particle, p_am,
                                                  CS_LAGR_VELOCITY);

      cs_user_lagr_imposed_motion(old_part_coords,
                                  dtp,
                                  &disp);

      for (cs_lnum_t id = 0; id < 3; id++) {

        part_coords[id] = old_part_coords[id] + disp[id];

        part_vel_seen[id] =  0.0;

        part_vel[id] = disp[id]/dtp;

      }
    }
  }

}

/*============================================================================
 * Public function definitions
 *============================================================================*/

/*----------------------------------------------------------------------------*/
/*!
 * \brief Integration of particle equations of motion:
 *
 * - Standard Model : First order  -> call of subroutine lages1
 *                    Second order -> call of subroutine lages2
 * - Deposition submodel (Guingo & Minier, 2008) if needed
 *
 * \param[in]  dt_p      lagrangian time step
 * \param[in]  taup      dynamic characteristic time
 * \param[in]  tlag      fluid characteristic time
 * \param[out] piil      terme in P-U SDE integration
 * \param[in]  bx        turbulence characteristics
 * \param[in]  tsfext    info for return coupling source terms
 * \param[in]  gradpr    pressure gradient
 * \param[in]  gradvf    fluid velocity gradient
 */
/*----------------------------------------------------------------------------*/

void
cs_lagr_sde(cs_real_t      dt_p,
            cs_real_t      taup[],
            cs_real_3_t    tlag[],
            cs_real_3_t    piil[],
            cs_real_t      bx[],
            cs_real_t      tsfext[],
            cs_real_3_t    gradpr[],
            cs_real_33_t   gradvf[],
            cs_real_t      terbru[],
            cs_real_t      vislen[])
{
  cs_lnum_t one = 1;
  cs_real_t *romp;

  cs_lagr_particle_set_t  *p_set = cs_glob_lagr_particle_set;
  const cs_lagr_attribute_map_t *p_am = p_set->p_am;

  BFT_MALLOC(romp, p_set->n_particles, cs_real_t);

  /* Computation of particle density     */
  cs_real_t aa = 6.0 / cs_math_pi;

  for (cs_lnum_t ip = 0; ip < p_set->n_particles; ip++) {

    unsigned char *particle = p_set->p_buffer + p_am->extents * ip;
    if (cs_lagr_particle_get_cell_id(particle, p_am) >= 0) {

      cs_real_t d3 = pow(cs_lagr_particle_get_real(particle, p_am,
                                                   CS_LAGR_DIAMETER),3.0);
      romp[ip] = aa * cs_lagr_particle_get_real(particle, p_am,
                                                CS_LAGR_MASS) / d3;

    }

  }

  /* ====================================================================
   * 2.  Management of user external force fields
   * ====================================================================   */

  /* Allocate temporay arrays  */
  cs_real_3_t *fextla;
  BFT_MALLOC(fextla, p_set->n_particles, cs_real_3_t);

  cs_real_33_t *vagaus;
  BFT_MALLOC(vagaus, p_set->n_particles, cs_real_33_t);

  /* Random values   */
  if (cs_glob_lagr_time_scheme->idistu == 1) {

    if (p_set->n_particles > 0) {

      for (cs_lnum_t ivf = 0; ivf < 3; ivf++) {

        for (cs_lnum_t id = 0; id < 3; id++) {

          for (cs_lnum_t ip = 0; ip < p_set->n_particles; ip++)
            CS_PROCF(normalen, NORMALEN)(&one, &(vagaus[ip][id][ivf]));

        }

      }

    }

  }
  else {

    for (cs_lnum_t ivf = 0; ivf < 3; ivf++) {

      for (cs_lnum_t id = 0; id < 3; id++) {

        for (cs_lnum_t ip = 0; ip < p_set->n_particles; ip++)
          vagaus[ip][id][ivf] = 0.0;

      }

    }

  }

  cs_real_t *brgaus = NULL;

  /* Brownian movement */

  if (cs_glob_lagr_brownian->lamvbr == 1) {

    BFT_MALLOC(brgaus, cs_glob_lagr_const_dim->nbrgau * p_set->n_particles, cs_real_t);

    for (cs_lnum_t ivf = 0; ivf < cs_glob_lagr_const_dim->nbrgau; ivf++) {

      for (cs_lnum_t ip = 0; ip < p_set->n_particles; ip++)
        CS_PROCF(normalen, NORMALEN)
          (&one, &(brgaus[ip * cs_glob_lagr_const_dim->nbrgau + ivf]));

    }

  }

  for (cs_lnum_t ip = 0; ip < p_set->n_particles; ip++) {
    fextla[ip][0] = 0.0;
    fextla[ip][1] = 0.0;
    fextla[ip][2] = 0.0;
  }

  cs_user_lagr_ef(dt_p,
                  (const cs_real_t *)taup,
                  (const cs_real_3_t *)tlag,
                  (const cs_real_3_t *)piil,
                  (const cs_real_t *)bx,
                  (const cs_real_t *)tsfext,
                  (const cs_real_33_t *)vagaus,
                  (const cs_real_3_t *)gradpr,
                  (const cs_real_33_t *)gradvf,
                  romp,
                  fextla);

  /* First order
     ----------- */

  if (cs_glob_lagr_time_scheme->t_order == 1) {

    /* If no deposition sub-model is activated, call of subroutine lages1
       for every particle */

    if (cs_glob_lagr_model->deposition <= 0)
      _lages1(dt_p, taup, tlag, piil, bx, vagaus, gradpr, romp, brgaus, terbru, fextla);

    /* Management of the deposition submodel */

    else
      _lagdep(dt_p, taup, tlag, piil, bx, vagaus, gradpr, romp, fextla, vislen);

  }

  /* Second order
     ------------ */

  else
    _lages2(dt_p, taup, tlag, piil, bx, tsfext,
            vagaus, gradpr, romp, brgaus, terbru, fextla);

  /* Free memory */
  if (cs_glob_lagr_brownian->lamvbr == 1)
    BFT_FREE(brgaus);

  BFT_FREE(romp);
  BFT_FREE(vagaus);
  BFT_FREE(fextla);
}

/*----------------------------------------------------------------------------*/
/*!
 * \brief Integration of a stochastic differential equation (SDE) for
 *        a user particle variable (attribute).
 *
 * \f[
 *  \frac{dV}{dt} = \frac{V - PIP}{TCARAC}
 * ]\f
 *
 * When there is interaction with a boundary face, the integration
 * degenerates to order 1 (even if the 2nd order scheme is active).
 *
 * \param[in]  attr    attribute/variable
 * \param[in]  tcarac  variable characteristic time
 * \param[in]  pip     right-hand side associated with SDE
 */
/*----------------------------------------------------------------------------*/

void
cs_lagr_sde_attr(cs_lagr_attribute_t   attr,
                 cs_real_t            *tcarac,
                 cs_real_t            *pip)
{
  /* Particles management */
  cs_lagr_particle_set_t         *p_set = cs_glob_lagr_particle_set;
  const cs_lagr_attribute_map_t  *p_am  = p_set->p_am;

  int ltsvar;

  if (p_set->p_am->source_term_displ[attr] >= 0)
    ltsvar = 1;
  else
    ltsvar = 0;

  int nor = cs_glob_lagr_time_step->nor;

  assert(nor == 1 || nor == 2);

  if (nor == 1) {

    for (cs_lnum_t npt = 0; npt < p_set->n_particles; npt++) {

      unsigned char *particle = p_set->p_buffer + p_am->extents * npt;

      if (cs_lagr_particle_get_cell_id(particle, p_am) >= 0) {

        if (tcarac[npt] <= 0.0)
          bft_error
            (__FILE__, __LINE__, 0,
             _("@\n"
               "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n"
               "@\n"
               "@ @@ ATTENTION : ARRET A L''EXECUTION DU MODULE LAGRANGIEN\n"
               "@    =========\n"
               "@\n"
               "@    LE TEMPS CARACTERISTIQUE LIE A L'EQUATION\n"
               "@      DIFFERENTIELLE STOCHASTIQUE DE LA VARIABLE\n"
               "@      NUMERO %d UNE VALEUR NON PERMISE (CS_LAGR_SDE).\n"
               "@\n"
               "@    TCARAC DEVRAIT ETRE UN ENTIER STRICTEMENT POSITIF\n"
               "@       IL VAUT ICI TCARAC = %e11.4\n"
               "@       POUR LA PARTICULE NUMERO %d\n"
               "@\n"
               "@  Le calcul ne sera pas execute.\n"
               "@\n"
               "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n"
               "@"),
             attr, tcarac[npt], npt);

        cs_real_t aux1 = cs_glob_lagr_time_step->dtp/tcarac[npt];
        cs_real_t aux2 = exp(-aux1);
        cs_real_t ter1 = cs_lagr_particle_get_real(particle, p_am, attr);
        cs_real_t ter2 = pip[npt] * (1.0 - aux2);

        /* Pour le cas NORDRE= 1 ou s'il y a rebond,     */
        /* le ETTP suivant est le resultat final    */
        cs_lagr_particle_set_real(particle, p_am, attr, ter1 + ter2);

        /* Pour le cas NORDRE= 2, on calcule en plus TSVAR pour NOR= 2  */
        if (ltsvar) {
          cs_real_t *part_ptsvar = cs_lagr_particles_source_terms(p_set, npt, attr);
          cs_real_t ter3 = ( -aux2 + (1.0 - aux2) / aux1) * pip[npt];
          *part_ptsvar = 0.5 * ter1 + ter3;

        }

      }

    }

  }
  else if (nor == 2) {

    for (cs_lnum_t npt = 0; npt < p_set->n_particles; npt++) {

      unsigned char *particle = p_set->p_buffer + p_am->extents * npt;

      if (   cs_lagr_particle_get_cell_id(particle, p_am) >= 0
          && cs_lagr_particle_get_lnum(particle, p_am, CS_LAGR_SWITCH_ORDER_1) == 0) {

        if (tcarac [npt] <= 0.0)
          bft_error
            (__FILE__, __LINE__, 0,
             _("@\n"
               "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n"
               "@\n"
               "@ @@ ATTENTION : ARRET A L''EXECUTION DU MODULE LAGRANGIEN\n"
               "@    =========\n"
               "@\n"
               "@    LE TEMPS CARACTERISTIQUE LIE A L'EQUATION\n"
               "@      DIFFERENTIELLE STOCHASTIQUE DE LA VARIABLE\n"
               "@      NUMERO %d UNE VALEUR NON PERMISE (CS_LAGR_SDE).\n"
               "@\n"
               "@    TCARAC DEVRAIT ETRE UN ENTIER STRICTEMENT POSITIF\n"
               "@       IL VAUT ICI TCARAC = %e11.4\n"
               "@       POUR LA PARTICULE NUMERO %d\n"
               "@\n"
               "@  Le calcul ne sera pas execute.\n"
               "@\n"
               "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n"
               "@"),
             attr, tcarac[npt], npt);

        cs_real_t aux1   = cs_glob_lagr_time_step->dtp / tcarac [npt];
        cs_real_t aux2   = exp ( -aux1);
        cs_real_t ter1   = 0.5 * cs_lagr_particle_get_real(particle,
                                                           p_am, attr) * aux2;
        cs_real_t ter2   = pip [npt] * (1.0 - (1.0 - aux2) / aux1);

        /* Pour le cas NORDRE= 2, le ETTP suivant est le resultat final */
        cs_real_t *part_ptsvar = cs_lagr_particles_source_terms(p_set, npt, attr);
        cs_lagr_particle_set_real(particle, p_am, attr,
                                  *part_ptsvar + ter1 + ter2 );

      }

    }

  }
}

/*----------------------------------------------------------------------------*/

END_C_DECLS