File size: 39,433 Bytes
f421c2e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "38d84418",
   "metadata": {},
   "source": [
    "# Simulating Single Phase Transport \n",
    "\n",
    "The point of an OpenPNM simulation is ultimately to compute some transport process.  In this notebook we will cover the following subjects:\n",
    "\n",
    "- Defining conductance \n",
    "- Settings boundary conditions \n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cac063b0",
   "metadata": {},
   "source": [
    "Start by defining a network.  We'll use the ``Demo`` class which happens to include all the geometrical pore-scale models already. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "94917bac",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "══════════════════════════════════════════════════════════════════════════════\n",
      "net : <openpnm.network.Demo at 0x23503894050>\n",
      "――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――\n",
      "  #  Properties                                                   Valid Values\n",
      "――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――\n",
      "  2  pore.coords                                                       25 / 25\n",
      "  3  throat.conns                                                      40 / 40\n",
      "  4  pore.coordination_number                                          25 / 25\n",
      "  5  pore.max_size                                                     25 / 25\n",
      "  6  throat.spacing                                                    40 / 40\n",
      "  7  pore.seed                                                         25 / 25\n",
      "  8  pore.diameter                                                     25 / 25\n",
      "  9  throat.max_size                                                   40 / 40\n",
      " 10  throat.diameter                                                   40 / 40\n",
      " 11  throat.cross_sectional_area                                       40 / 40\n",
      " 12  throat.hydraulic_size_factors                                     40 / 40\n",
      " 13  throat.diffusive_size_factors                                     40 / 40\n",
      " 14  throat.lens_volume                                                40 / 40\n",
      " 15  throat.length                                                     40 / 40\n",
      " 16  throat.total_volume                                               40 / 40\n",
      " 17  throat.volume                                                     40 / 40\n",
      " 18  pore.volume                                                       25 / 25\n",
      "――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――\n",
      "  #  Labels                                                 Assigned Locations\n",
      "――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――\n",
      "  2  pore.xmin                                                               5\n",
      "  3  pore.xmax                                                               5\n",
      "  4  pore.ymin                                                               5\n",
      "  5  pore.ymax                                                               5\n",
      "  6  pore.surface                                                           16\n",
      "  7  throat.surface                                                         16\n",
      "  8  pore.left                                                               5\n",
      "  9  pore.right                                                              5\n",
      " 10  pore.front                                                              5\n",
      " 11  pore.back                                                               5\n",
      "――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import openpnm as op\n",
    "op.visualization.set_mpl_style()\n",
    "\n",
    "pn = op.network.Demo(shape=[5, 5, 1], spacing=5e-5)\n",
    "print(pn)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a7a0902f",
   "metadata": {},
   "source": [
    "## Define Phase Viscosity\n",
    "\n",
    "To fully illustrate the process of performing transport calculations, we'll use an empty ``Phase`` object and add all the needed properties manually:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "b5e3a75a",
   "metadata": {},
   "outputs": [],
   "source": [
    "water = op.phase.Phase(network=pn)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b8973bf1",
   "metadata": {},
   "source": [
    "Let's assume that we are interested in pressure driven flow. This requires knowing the viscosity of the phase, so let's add a pore-scale model for computing the viscosity of water:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "029d1797",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "══════════════════════════════════════════════════════════════════════════════\n",
      "phase_01 : <openpnm.phase.Phase at 0x23518f84c80>\n",
      "――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――\n",
      "  #  Properties                                                   Valid Values\n",
      "――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――\n",
      "  2  pore.temperature                                                  25 / 25\n",
      "  3  pore.pressure                                                     25 / 25\n",
      "  4  pore.viscosity                                                    25 / 25\n",
      "――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――\n",
      "  #  Labels                                                 Assigned Locations\n",
      "――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――\n",
      "  2  pore.all                                                               25\n",
      "  3  throat.all                                                             40\n",
      "――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――\n"
     ]
    }
   ],
   "source": [
    "water.add_model(propname='pore.viscosity',\n",
    "                model=op.models.phase.viscosity.water_correlation)\n",
    "print(water)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "21aab8de",
   "metadata": {},
   "source": [
    "And we can check the individual values to verify they make sense:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "8833d1ee",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.00089319 0.00089319 0.00089319 0.00089319 0.00089319 0.00089319\n",
      " 0.00089319 0.00089319 0.00089319 0.00089319 0.00089319 0.00089319\n",
      " 0.00089319 0.00089319 0.00089319 0.00089319 0.00089319 0.00089319\n",
      " 0.00089319 0.00089319 0.00089319 0.00089319 0.00089319 0.00089319\n",
      " 0.00089319]\n"
     ]
    }
   ],
   "source": [
    "print(water['pore.viscosity'])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "95ba0972",
   "metadata": {},
   "source": [
    "## Basic Conductance Calculation\n",
    "\n",
    "Determining the conductance of the conduits between each pair of connected pores is the most important part of performing a simulation. The details of conductance models are covered elsewhere. For this demonstration will assume the very simplest case where all pressure loss occurs in the throats.\n",
    "\n",
    "Recall the Hagan-Poiseuille equation for fluid flow through a cylindrical tube:\n",
    "\n",
    "$$ Q = \\frac{\\pi R^4}{8 \\mu L} \\Delta P$$\n",
    "\n",
    "where $R$ and $L$ are the radius and length of the throat, and $\\mu$ is the viscosity of the fluid.  Together this prefactor can be referred to as the hydraulic conductance, $g_h$, giving:\n",
    "\n",
    "$$ Q = g_h \\Delta P $$\n",
    "\n",
    "So the aim is the compute values of $g_h$ for each throat.  We start by doing this manually:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "647bde31",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1.71474078e-15 4.89487491e-16 6.50123109e-16 2.09291190e-14\n",
      " 5.09786041e-14 8.92694901e-15 9.28972043e-15 2.08610930e-14\n",
      " 1.28181288e-15 1.48187324e-15 2.78733145e-14 7.76058006e-14\n",
      " 1.04922730e-15 1.73610610e-14 2.61741594e-14 7.67328653e-16\n",
      " 6.60091981e-14 6.40072101e-15 5.78941040e-15 3.29178076e-14\n",
      " 2.14954939e-15 2.73094186e-15 5.22858424e-16 9.95589594e-14\n",
      " 1.68107574e-14 3.53246965e-15 1.65944003e-15 7.86513283e-15\n",
      " 7.64403364e-14 2.11288391e-14 9.50956907e-16 1.41678374e-15\n",
      " 2.92086263e-14 2.49925306e-14 9.04335289e-16 1.19842395e-15\n",
      " 1.68312420e-14 6.58546574e-15 2.25965139e-14 8.24975878e-16]\n"
     ]
    }
   ],
   "source": [
    "R = pn['throat.diameter']/2\n",
    "L = pn['throat.length']\n",
    "mu = water['throat.viscosity']  # See ProTip below\n",
    "water['throat.hydraulic_conductance'] = np.pi*R**4/(8*mu*L)\n",
    "print(water['throat.hydraulic_conductance'])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7720d25b",
   "metadata": {},
   "source": [
    "```{admonition} Phase can do automatic interpolation to get throat values\n",
    "  The ``Phase`` class has a special ability to interpolate pore values to throats, and vice-versa. In PNM simulations all the balances are solved for each pore, so the thermodynamic properties like temperature, pressure, etc. are all defined on pores. Consequently, the physical properties are also defined in pores, like viscosity. However, as shown above we often want viscosity values in the throats. OpenPNM provides a shortcut for this, such that if you request a throat property that does not exist, it will attempt to fetch the pores values and do a linear interpolation of values to produce an array of throat values.  There is also a function for this, ``water.interpolate_data('throat.viscosity')``, but the ``water['throat.viscosity']`` shortcut is very convenient. The automatic interpolation can be disabled in the `phase.settings`.\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "d4544d8b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.00089319, 0.00089319, 0.00089319, 0.00089319, 0.00089319,\n",
       "       0.00089319, 0.00089319, 0.00089319, 0.00089319, 0.00089319,\n",
       "       0.00089319, 0.00089319, 0.00089319, 0.00089319, 0.00089319,\n",
       "       0.00089319, 0.00089319, 0.00089319, 0.00089319, 0.00089319,\n",
       "       0.00089319, 0.00089319, 0.00089319, 0.00089319, 0.00089319,\n",
       "       0.00089319, 0.00089319, 0.00089319, 0.00089319, 0.00089319,\n",
       "       0.00089319, 0.00089319, 0.00089319, 0.00089319, 0.00089319,\n",
       "       0.00089319, 0.00089319, 0.00089319, 0.00089319, 0.00089319])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "water.interpolate_data('throat.viscosity')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "cf30019f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.00089319, 0.00089319, 0.00089319, 0.00089319, 0.00089319,\n",
       "       0.00089319, 0.00089319, 0.00089319, 0.00089319, 0.00089319,\n",
       "       0.00089319, 0.00089319, 0.00089319, 0.00089319, 0.00089319,\n",
       "       0.00089319, 0.00089319, 0.00089319, 0.00089319, 0.00089319,\n",
       "       0.00089319, 0.00089319, 0.00089319, 0.00089319, 0.00089319,\n",
       "       0.00089319, 0.00089319, 0.00089319, 0.00089319, 0.00089319,\n",
       "       0.00089319, 0.00089319, 0.00089319, 0.00089319, 0.00089319,\n",
       "       0.00089319, 0.00089319, 0.00089319, 0.00089319, 0.00089319])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "water['throat.viscosity']"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6e71e725",
   "metadata": {},
   "source": [
    "## Create Algorithm Object\n",
    "\n",
    "OpenPNM contains a variety of ``Algorithm`` classes in the ``openpnm.algorithms`` module.  Let's initialize a ``StokesFlow`` algorithm, since this simulates pressure driven flow through the network. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "29b8bf9d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "══════════════════════════════════════════════════════════════════════════════\n",
      "stokes_01 : <openpnm.algorithms.StokesFlow at 0x23518f859a0>\n",
      "――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――\n",
      "  #  Properties                                                   Valid Values\n",
      "――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――\n",
      "  2  pore.bc.rate                                                       0 / 25\n",
      "  3  pore.bc.value                                                      0 / 25\n",
      "――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――\n",
      "  #  Labels                                                 Assigned Locations\n",
      "――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――\n",
      "  2  pore.all                                                               25\n",
      "  3  throat.all                                                             40\n",
      "――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――\n"
     ]
    }
   ],
   "source": [
    "sf = op.algorithms.StokesFlow(network=pn, phase=water)\n",
    "print(sf)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "72c4d6c8",
   "metadata": {},
   "source": [
    "## Assign Boundary Conditions\n",
    "\n",
    "As can be seen in the print-out above there are predefined ``'pore.bc'`` arrays, but they contain no valid values, meaning they are all ``nans``. Once we set some boundary conditions, this will change.  Let's apply pressure BCs on one side of the network, and rate BCs on the other:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "3caad7c7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "══════════════════════════════════════════════════════════════════════════════\n",
      "stokes_01 : <openpnm.algorithms.StokesFlow at 0x23518f859a0>\n",
      "――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――\n",
      "  #  Properties                                                   Valid Values\n",
      "――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――\n",
      "  2  pore.bc.rate                                                       5 / 25\n",
      "  3  pore.bc.value                                                      5 / 25\n",
      "――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――\n",
      "  #  Labels                                                 Assigned Locations\n",
      "――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――\n",
      "  2  pore.all                                                               25\n",
      "  3  throat.all                                                             40\n",
      "――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――\n"
     ]
    }
   ],
   "source": [
    "sf.set_value_BC(pores=pn.pores('left'), values=100_000)\n",
    "sf.set_rate_BC(pores=pn.pores('right'), rates=1e-10)\n",
    "print(sf)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e81f2e4b",
   "metadata": {},
   "source": [
    "```{admonition} All boundary conditions are preceeded with 'pore.bc'\n",
    "  All boundary conditions are stored as 'pore.bc.<type>', which means that OpenPNM's dictionary lookup tricks can be used to see all types and values of bc's using: `sf['pore.bc']` which will return a `dict`. This can be used as shown below:\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "f14cea28",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dict_keys(['rate', 'value'])\n"
     ]
    }
   ],
   "source": [
    "print(sf['pore.bc'].keys())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7114b180",
   "metadata": {},
   "source": [
    "Now we can see there are 5 valid values of each type.  The ``sf`` algorithm will look for ``'throat.hydraulic_conductance'`` on ``water`` by default, so we can just ``run``:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "732f855a",
   "metadata": {},
   "outputs": [],
   "source": [
    "soln = sf.run()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "613df06f",
   "metadata": {},
   "source": [
    "The ``run`` method solves the mass balance around each pore and computes the pressure within each pore that is required to sustain the flow defined by the boundary conditions.  ``soln`` return None, instead the ``'pore.pressure'`` values are written to the dictionary of the algorithm object:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "520819ef",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "══════════════════════════════════════════════════════════════════════════════\n",
      "stokes_01 : <openpnm.algorithms.StokesFlow at 0x23518f859a0>\n",
      "――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――\n",
      "  #  Properties                                                   Valid Values\n",
      "――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――\n",
      "  2  pore.bc.rate                                                       5 / 25\n",
      "  3  pore.bc.value                                                      5 / 25\n",
      "  4  pore.pressure                                                     25 / 25\n",
      "  5  pore.initial_guess                                                25 / 25\n",
      "――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――\n",
      "  #  Labels                                                 Assigned Locations\n",
      "――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――\n",
      "  2  pore.all                                                               25\n",
      "  3  throat.all                                                             40\n",
      "――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――\n"
     ]
    }
   ],
   "source": [
    "print(sf)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bc617629",
   "metadata": {},
   "source": [
    "Finally we can look at how much pressure was required in the \"right\" pores to meet the required flow rate:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "16a3d0d8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([139569.47740193, 138265.16063556, 134437.32490449, 128801.56476401,\n",
       "       131525.91604191])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sf['pore.pressure'][pn.pores('right')]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "55cc13bb",
   "metadata": {},
   "source": [
    "So we can see that 150 kPa was required to accomplish the requested flow. "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "73b9cba6",
   "metadata": {},
   "source": [
    "## Rigorous Conductance Calculation\n",
    "\n",
    "The above demonstration used a very simplistic conductance calculation.  It was also stated that computing conductance is the most important part of doing a PNM simulation.  To finish this notebook, we'll look more closely at this process."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "751a30eb",
   "metadata": {},
   "source": [
    "### Manual Method\n",
    "Let's print the network object again:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "322b2746",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "══════════════════════════════════════════════════════════════════════════════\n",
      "net : <openpnm.network.Demo at 0x23503894050>\n",
      "――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――\n",
      "  #  Properties                                                   Valid Values\n",
      "――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――\n",
      "  2  pore.coords                                                       25 / 25\n",
      "  3  throat.conns                                                      40 / 40\n",
      "  4  pore.coordination_number                                          25 / 25\n",
      "  5  pore.max_size                                                     25 / 25\n",
      "  6  throat.spacing                                                    40 / 40\n",
      "  7  pore.seed                                                         25 / 25\n",
      "  8  pore.diameter                                                     25 / 25\n",
      "  9  throat.max_size                                                   40 / 40\n",
      " 10  throat.diameter                                                   40 / 40\n",
      " 11  throat.cross_sectional_area                                       40 / 40\n",
      " 12  throat.hydraulic_size_factors                                     40 / 40\n",
      " 13  throat.diffusive_size_factors                                     40 / 40\n",
      " 14  throat.lens_volume                                                40 / 40\n",
      " 15  throat.length                                                     40 / 40\n",
      " 16  throat.total_volume                                               40 / 40\n",
      " 17  throat.volume                                                     40 / 40\n",
      " 18  pore.volume                                                       25 / 25\n",
      "――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――\n",
      "  #  Labels                                                 Assigned Locations\n",
      "――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――\n",
      "  2  pore.xmin                                                               5\n",
      "  3  pore.xmax                                                               5\n",
      "  4  pore.ymin                                                               5\n",
      "  5  pore.ymax                                                               5\n",
      "  6  pore.surface                                                           16\n",
      "  7  throat.surface                                                         16\n",
      "  8  pore.left                                                               5\n",
      "  9  pore.right                                                              5\n",
      " 10  pore.front                                                              5\n",
      " 11  pore.back                                                               5\n",
      "――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――\n"
     ]
    }
   ],
   "source": [
    "print(pn)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9f10eed9",
   "metadata": {},
   "source": [
    "Note the ``'throat.hydraulic_size_factors'`` array.  This is computed by a pore-scale model on the ``Demo`` network.  This computation is more rigorous in the following ways:\n",
    "\n",
    "1. The conductance of each half pore and that throat is considered.\n",
    "1. The throat length is computed carefully by accounting for the 'lens' between the intersection of the spherical pore bodies and the cylindrical throat.\n",
    "1. The net cross-sectional area of the pores are computed by integrating between the pore center and the pore-throat intersection point\n",
    "\n",
    "The conductance of each element in the conduit is returned as an *Nt-by-3* array, where columns 1 and 3 contain the hydraulic conductance of the half pore on either end of the throat, and the column 1 contains the throat conductance."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "65e69a58",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[5.53646621e-17, 1.53159095e-18, 5.80704541e-17],\n",
       "       [3.27057594e-17, 4.37205796e-19, 2.21236821e-17],\n",
       "       [2.21236821e-17, 5.80684076e-19, 8.15497372e-17],\n",
       "       [3.86668667e-16, 1.86936997e-17, 2.61233255e-16],\n",
       "       [4.92226337e-16, 4.55336277e-17, 5.49581815e-16],\n",
       "       [2.58445082e-16, 7.97347004e-18, 1.46626967e-16],\n",
       "       [1.46626967e-16, 8.29749419e-18, 2.76612840e-16],\n",
       "       [3.92929505e-16, 1.86329395e-17, 2.57815992e-16],\n",
       "       [5.47578209e-17, 1.14490366e-18, 4.42265276e-17],\n",
       "       [4.42265276e-17, 1.32359576e-18, 9.36797895e-17],\n",
       "       [3.28052595e-16, 2.48961922e-17, 4.16483115e-16],\n",
       "       [6.26881484e-16, 6.93167986e-17, 6.97015739e-16],\n",
       "       [3.55626308e-17, 9.37160330e-19, 7.14255758e-17],\n",
       "       [2.28811833e-16, 1.55067425e-17, 3.58288320e-16],\n",
       "       [4.27538842e-16, 2.33785222e-17, 3.04140609e-16],\n",
       "       [6.77654132e-17, 6.85371006e-19, 2.76338664e-17],\n",
       "       [5.87496107e-16, 5.89588182e-17, 6.14445225e-16],\n",
       "       [2.23127657e-16, 5.71706607e-18, 1.15953527e-16],\n",
       "       [1.15953527e-16, 5.17104896e-18, 1.80634197e-16],\n",
       "       [3.80462677e-16, 2.94018877e-17, 4.22022434e-16],\n",
       "       [5.53646621e-17, 1.91995806e-18, 1.25454365e-16],\n",
       "       [6.32242849e-17, 2.43925255e-18, 1.52617323e-16],\n",
       "       [2.21236821e-17, 4.67012411e-19, 4.49301973e-17],\n",
       "       [7.49012290e-16, 8.89251613e-17, 7.66336228e-16],\n",
       "       [2.59028510e-16, 1.50152163e-17, 2.57815992e-16],\n",
       "       [1.56844691e-16, 3.15516991e-18, 7.90613420e-17],\n",
       "       [1.21737971e-16, 1.48219682e-18, 4.42265276e-17],\n",
       "       [1.46626967e-16, 7.02506545e-18, 1.97567834e-16],\n",
       "       [6.84242037e-16, 6.82758165e-17, 6.26881484e-16],\n",
       "       [2.57815992e-16, 1.88720878e-17, 4.00491151e-16],\n",
       "       [4.78027488e-17, 8.49386100e-19, 3.55626308e-17],\n",
       "       [4.42265276e-17, 1.26545841e-18, 8.18407683e-17],\n",
       "       [3.28052595e-16, 2.60888806e-17, 4.48125260e-16],\n",
       "       [3.97301050e-16, 2.23231021e-17, 3.04140609e-16],\n",
       "       [9.79983978e-17, 8.07744094e-19, 2.76338664e-17],\n",
       "       [3.55626308e-17, 1.07042142e-18, 1.01211637e-16],\n",
       "       [2.28811833e-16, 1.50335130e-17, 3.40542841e-16],\n",
       "       [2.34628765e-16, 5.88207839e-18, 1.15953527e-16],\n",
       "       [3.04140609e-16, 2.01830017e-17, 3.29783316e-16],\n",
       "       [2.76338664e-17, 7.36860986e-19, 8.16642490e-17]])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pn['throat.hydraulic_size_factors']"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "29f6fccb",
   "metadata": {},
   "source": [
    "This data is called the ``size factor`` because it is purely the geometrical information required for the computation of the hydraulic conductance.  So the Hagan-Poisseiulle equation for each element is written as:\n",
    "\n",
    "$$ Q = \\frac{F_h}{\\mu} \\Delta P = g_h \\Delta P$$\n",
    "\n",
    "Note that both the $\\frac{\\pi R^4}{8}$ term and $L$ have been rolled into the $F_h$ value.\n",
    "\n",
    "The total conductance of the pore-throat-pore conduit can be found as the sum of three resistors in series.  Since we have conductance values, we add the inverses, and invert again.  The full expression for the hydraulic conductance between pores i and j, through throat k, is:\n",
    "\n",
    "\n",
    "$$ Q = \\bigg( \\frac{\\mu}{F_{h, i}} + \\frac{\\mu}{F_{h, k}} + \\frac{\\mu}{F_{h, j}} \\bigg) ^ {-1} \\Delta P $$\n",
    "\n",
    "This can be computed by hand:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "0b26948e",
   "metadata": {},
   "outputs": [],
   "source": [
    "F_h = water['throat.hydraulic_size_factors']\n",
    "water['throat.hydraulic_conductance'] = (mu * (1/F_h).sum(axis=1))**(-1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "fc49009f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1.62682953e-15, 4.73790919e-16, 6.29130422e-16, 1.86882996e-14,\n",
       "       4.33728726e-14, 8.22585155e-15, 8.54945776e-15, 1.86310861e-14,\n",
       "       1.22451096e-15, 1.41934189e-15, 2.45435403e-14, 6.41358604e-14,\n",
       "       1.00938371e-15, 1.56258033e-14, 2.31312623e-14, 7.41440712e-16,\n",
       "       5.51773052e-14, 5.95456230e-15, 5.39441483e-15, 2.87003409e-14,\n",
       "       2.04722414e-15, 2.58964133e-15, 5.06889705e-16, 8.06300420e-14,\n",
       "       1.50606027e-14, 3.33244184e-15, 1.58693450e-15, 7.25921434e-15,\n",
       "       6.32419568e-14, 1.88596095e-14, 9.12930789e-16, 1.35697434e-15,\n",
       "       2.56723985e-14, 2.21254255e-14, 8.71671454e-16, 1.15158254e-15,\n",
       "       1.51653544e-14, 6.12147269e-15, 2.00401599e-14, 7.96548498e-16])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "water['throat.hydraulic_conductance']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "3ef96480",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([144533.4827636 , 142990.61847937, 138772.80703712, 132858.77887604,\n",
       "       135952.53964271])"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sf = op.algorithms.StokesFlow(network=pn, phase=water)\n",
    "sf.set_value_BC(pores=pn.pores('left'), values=100_000)\n",
    "sf.set_rate_BC(pores=pn.pores('right'), rates=1e-10)\n",
    "soln = sf.run()\n",
    "sf['pore.pressure'][pn.pores('right')]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5d59ec79",
   "metadata": {},
   "source": [
    "As can be seen the numbers are about the same as with the simple case, but should be somewhat more correct.  In fact, these above pressures are a bit higher, which is because the total conductance of the conduit is lower due to the inclusion of the pore body lengths into the total length, compared to above where only the throat length was included."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4535fccd",
   "metadata": {},
   "source": [
    "### Pore-Scale Model Method\n",
    "\n",
    "Instead of computing the hydraulic conductance manually as done above, there is a pore-scale model available:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "9891436d",
   "metadata": {},
   "outputs": [],
   "source": [
    "water.add_model(propname='throat.hydraulic_conductance',\n",
    "                model=op.models.physics.hydraulic_conductance.generic_hydraulic)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "fd34b943",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([144533.4827636 , 142990.61847937, 138772.80703712, 132858.77887604,\n",
       "       135952.53964271])"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sf = op.algorithms.StokesFlow(network=pn, phase=water)\n",
    "sf.set_value_BC(pores=pn.pores('left'), values=100_000)\n",
    "sf.set_rate_BC(pores=pn.pores('right'), rates=1e-10)\n",
    "soln = sf.run()\n",
    "sf['pore.pressure'][pn.pores('right')]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "56ab903f",
   "metadata": {},
   "source": [
    "Which gives exactly the same result, without have to manually deal with the conductances-in-series calculation."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "graduation_project_TCES",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}