In [2]:
import os
os.sys.path.append("C:\\Users\\amin\\Documents\\Repos\\OpenPNM")

Summary

One of the main applications of OpenPNM is simulating transport phenomena such as Fickian diffusion, advection diffusion, reactive transport, etc. In this example, we will learn how to perform Fickian diffusion on a Cubic network. The algorithm works fine with every other network type, but for now we want to keep it simple.

Problem setup

Generating network

First, we need to generate a Cubic network. For now, we stick to a 2d network, but you might as well try it in 3d!


In [3]:
import openpnm as op
net = op.network.Cubic(shape=[1, 10, 10], spacing=1e-5)

Adding geometry

Next, we need to add a geometry to the generated network. A geometry contains information about size of the pores/throats in a network. OpenPNM has tons of prebuilt geometries that represent the microstructure of different materials such as Toray090 carbon papers, sand stone, electrospun fibers, etc. For now, we stick to a sample geometry called StickAndBall that assigns random values to pore/throat diameters.


In [4]:
geom = op.geometry.StickAndBall(network=net, pores=net.Ps, throats=net.Ts)

Adding phase

Next, we need to add a phase to our simulation. A phase object(s) contain(s) thermophysical information about the working fluid(s) in the simulation. OpenPNM has tons of prebuilt phases as well! For this simulation, we use air as our working fluid.


In [5]:
air = op.phases.Air(network=net)

Adding physics

Finally, we need to add a physics. A physics object contains information about the working fluid in the simulation that depend on the geometry of the network. A good example is diffusive conductance, which not only depends on the thermophysical properties of the working fluid, but also depends on the geometry of pores/throats.


In [6]:
phys_air = op.physics.Standard(network=net, phase=air, geometry=geom)


2018-07-17 12:54:56,757 | WARNING  | root._regen | throat.entry_pressure was not run since the following properties are missing: ['pore.surface_tension', 'throat.diameter']
2018-07-17 12:54:56,764 | WARNING  | root._regen | throat.electrical_conductance was not run since the following properties are missing: ['pore.electrical_conductivity', 'throat.electrical_conductivity', 'throat.equivalent_area', 'throat.conduit_lengths']

Performing Fickian diffusion

Now that everything's set up, it's time to perform our Fickian diffusion simulation. For this purpose, we need to add the FickianDiffusion algorithm to our simulation. Here's how we do it:


In [7]:
fd = op.algorithms.FickianDiffusion(network=net, phase=air)

Note that network and phase are required parameters for pretty much every algorithm we add, since we need to specify on which network and for which phase do we want to run the algorithm.

Adding boundary conditions

Next, we need to add some boundary conditions to the simulation. By default, OpenPNM assumes zero flux for the boundary pores.


In [8]:
inlet  = net.pores('left') 
outlet = net.pores('right')
fd.set_value_BC(pores=inlet, values=1.0)
fd.set_value_BC(pores=outlet, values=0.0)

set_value_BC applies the so-called "Dirichlet" boundary condition to the specified pores. Note that unless you want to apply a single value to all of the specified pores (like we just did), you must pass a list (or ndarray) as the values parameter.

Running the algorithm

Now, it's time to run the algorithm. This is done by calling the run method attached to the algorithm object.


In [9]:
fd.run();

Post processing

When an algorithm is successfully run, the results are attached to the same object. To access the results, you need to know the quantity for which the algorithm was solving. For instance, FickianDiffusion solves for the quantity pore.concentration, which is somewhat intuitive. However, if you ever forget it, or wanted to manually check the quantity, you can take a look at the algorithm settings:


In [10]:
print(fd.settings)


――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――
key                                 value
――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――
sources                             []
tolerance                           0.001
max_iter                            10000
relaxation_source                   1
relaxation_quantity                 1
phase                               phase_01
conductance                         throat.diffusive_conductance
quantity                            pore.concentration
solver                              spsolve
prefix                              alg
t_scheme                            None
――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――

Now that we know the quantity for which FickianDiffusion was solved, let's take a look at the results:


In [11]:
c = fd['pore.concentration']
print(c)


[ 1.          1.          1.          1.          1.          1.          1.
  1.          1.          1.          0.81199709  0.81629168  0.8490866
  0.8529373   0.85730148  0.87483529  0.88897593  0.9032197   0.91227587
  0.93132204  0.72484557  0.73114582  0.72939405  0.73529898  0.73381801
  0.76652935  0.80844507  0.82330037  0.84367147  0.81629361  0.65556961
  0.64883278  0.63878149  0.67468601  0.66796821  0.67620795  0.68007297
  0.72162453  0.72227546  0.67377261  0.58415966  0.55170024  0.51182718
  0.55149707  0.59437937  0.57836235  0.5331584   0.53454373  0.54756606
  0.63751058  0.46150047  0.46453505  0.4380063   0.42821799  0.45772314
  0.44735808  0.43942785  0.38166875  0.32668998  0.40129193  0.35331757
  0.3735379   0.37832499  0.37605676  0.34267032  0.31670982  0.29714827
  0.26464821  0.23503142  0.2068611   0.28701181  0.30763968  0.32755607
  0.26823647  0.22172434  0.1796034   0.1446477   0.14256056  0.15383017
  0.14256304  0.13434275  0.15249638  0.1565395   0.10279381  0.10016087
  0.06172419  0.07125155  0.09741781  0.09577904  0.08228388  0.          0.
  0.          0.          0.          0.          0.          0.          0.
  0.        ]

Heatmap

Well, it's hard to make sense out of a bunch of numbers! Let's visualize the results. Since the network is 2d, we can simply reshape the results in form of a 2d array similar to the shape of the network and plot the heatmap of it using matplotlib.


In [12]:
print('Network shape:', net._shape)
c2d = c.reshape((net._shape))


Network shape: (1, 10, 10)

In [13]:
import matplotlib.pyplot as plt
plt.imshow(c2d[0,:,:])
plt.title('Concentration (mol/m$^3$)')
plt.colorbar()


Out[13]:
<matplotlib.colorbar.Colorbar at 0x1c3e37c5dd8>

Calculating heat flux

You might as well be interested in calculating the mass flux from a boundary! This is easily done in OpenPNM via calling the rate method attached to the algorithm. Let's see how it works:


In [14]:
rate_inlet = fd.rate(pores=inlet)[0]
print('Mass flow rate from inlet:', rate_inlet, 'mol/s')


Mass flow rate from inlet: 8.25565563271e-12 mol/s