%matplotlib inline
import os
import fluiddyn as fld
import fluidlab

Tutorial: working in the laboratory (user perspective)

FluidDyn uses the object-oriented programming concepts. It deals with objects, which is a very natural way to represent and drive experiments since experiments consist in objects interacting with each other.

Regarding the laboratory, each physical object (a pump, a traverse, a probe, an acquisition board, a tank filled with a stratified fluid…) is represented and controlled by an instance of a class. The experimental results can also be represented by other types of objects.

Example of a conductivity probe attached to a moving traverse

Let’s consider a real-life example, how to use a conductivity probe attached to a moving traverse. FluidDyn provides the class fluidlab.objects.probes.MovingConductivityProbe which can be used like this:

# import the class representing the moving conductivity probe
from fluidlab.objects.probes import MovingConductivityProbe

# create an instance of this class
probe = MovingConductivityProbe()

    # set a parameter, the sample rate (in Hz)

    # just move the probe (in mm and mm/s)
    probe.move(deltaz=-100, speed=50)

    # just measure without moving (in s)
    measurements1 = probe.measure(duration=5)

    # move and measure (in mm and mm/s)
    measurements2 = probe.move_measure(deltaz=100, speed=100)
except AttributeError:

Of course this is a very simple example and there are more options to create the object probe and for the functions. Look at the documentation, i.e. in this case here: fluidlab.objects.probes.MovingConductivityProbe.

Save and load an object

For some classes of FluidDyn, the objects can be saved in a file and loaded afterwards. This is a very useful feature! To see how it works, we can consider the example of a tank filled with a stratified fluid, which is represented by the class fluidlab.objects.tanks.StratifiedTank. Let’s first see how we create a tank:

from fluidlab.objects.tanks import StratifiedTank

# create a tank with a linear stratification (see the doc of the class)
tank = StratifiedTank(
    H=550, S=100,
    z=[0, 500], rho=[1.1, 1])

The numerical object tank contains some information and can be use to do useful. We can for example fill the physical tank with the wanted profile (which makes use of some pumps also controlled by FluidDyn, see the class fluidlab.objects.pumps.MasterFlexPumps):

Warning: can not fill without pumps. It will only perform a test of
the filling. To really fill the tank, set argument pumps to True or to
an instance of class MasterFlexPumps.

flowrate_tot: 879.68 ml/min
vol_to_pump: 192.00 ml
time for the filling:  0.22 min
volume pumped / volume to pump = 0.9927
The filling is finished.
../_images/tuto_lab_user_11_1.png ../_images/tuto_lab_user_11_2.png

The numerical object tank can be saved in a file tank.h5 with its function save (the documentation explains how to control where the file is saved):

if os.path.exists('/tmp/tank.h5'):

If we come back some days later and we want to use again this particular instance of fluidlab.objects.tanks.StratifiedTank. Let’s assume that the file is in a directory /tmp/exp0. If we really know that this file contains the information for loading an object of fluidlab.objects.tanks.StratifiedTank, we can obtain the numerical representation of the tank by doing:

tank = StratifiedTank(str_path='/tmp')

But most of the case, it is easier and safer to use the function fluiddyn.util.util.create_object_from_file() like this:

path_to_tank_h5 = '/tmp/tank.h5'
tank = fld.create_object_from_file(path_to_tank_h5)

The function create_object_from_file() gets the correct class from information written in the file, calls the constructor of this class and return the object.