Tube simulation


it’s my first post on this forum.

I am trying to create a 2d tube simulation but the results i’m getting are rather unusual. The velocity is higher in the inlet and outlet before it reaches a steady state in all the tube. Is it normal? I expected it to slowly grow from the inlet to the outlet as time passed until it reached a steady state. I would appreciate any help. Here is the code i used:

#include "xflows2D.h"
#ifndef XFL_PRECOMPILED // Unless precompiled version is used,
  #include "xflows2D.hh"   // include full template code
#include <vector>
#include <cmath>
#include <iostream>
#include <fstream>
#include <iomanip>

using namespace xfl;
using namespace xfl::descriptors;
using namespace std;

typedef double T;
#define DESCRIPTOR D2Q9Descriptor

/// Velocity on the parabolic Poiseuille profile
T poiseuilleVelocity(xint iY, IncomprFlowParam<T> const& parameters) {
    T y = (T)iY / parameters.getResolution();
    return 4.*parameters.getLatticeU() * (y-y*y);

/// Linearly decreasing pressure profile
T poiseuillePressure(xint iX, IncomprFlowParam<T> const& parameters) {
    T Lx = parameters.getNx()-1;
    T Ly = parameters.getNy()-1;
    return 8.*parameters.getLatticeNu()*parameters.getLatticeU() / (Ly*Ly) * (Lx/(T)2-(T)iX);

/// Convert pressure to density according to ideal gas law
T poiseuilleDensity(xint iX, IncomprFlowParam<T> const& parameters) {
    return poiseuillePressure(iX,parameters)*DESCRIPTOR<T>::invCs2 + (T)1;

/// A functional, used to initialize the velocity for the boundary conditions
template<typename T>
class PoiseuilleVelocity {
    PoiseuilleVelocity(IncomprFlowParam<T> parameters_)
        : parameters(parameters_)
    { }
    void operator()(xint iX, xint iY, Array<T,2>& u) const {
        u[0] = poiseuilleVelocity(iY, parameters);
        u[1] = T();
    IncomprFlowParam<T> parameters;

/// A functional, used to initialize the density for the boundary conditions
template<typename T>
class PoiseuilleDensity {
    PoiseuilleDensity(IncomprFlowParam<T> parameters_)
        : parameters(parameters_)
    { }
    T operator()(xint iX, xint iY) const {
        return poiseuilleDensity(iX,parameters);
    IncomprFlowParam<T> parameters;

/// A functional, used to create an initial condition for with zero velocity,
///   and linearly decreasing pressure.
template<typename T>
class PoiseuilleDensityAndZeroVelocity {
    PoiseuilleDensityAndZeroVelocity(IncomprFlowParam<T> parameters_)
        : parameters(parameters_)
    { }
    void operator()(xint iX, xint iY, T& rho, Array<T,2>& u) const {
        rho = poiseuilleDensity(iX,parameters);
        u[0] = T();
        u[1] = T();
    IncomprFlowParam<T> parameters;

enum InletOutletT {pressure, velocity};

template<typename T>
class tubeDomain2D : public xfl::DomainFunctional2D {
    tubeDomain2D(xfl::xint width_, xfl::xint ypos_)
        : width(width_),
    { }
    virtual bool operator() (xfl::xint iX, xfl::xint iY) const {
        return iY < ypos || iY > ypos + width;
    virtual tubeDomain2D<T>* clone() const {
        return new tubeDomain2D<T>(*this);
    xfl::xint width;
    xfl::xint ypos;

void channelSetup( BlockLatticeBase2D<T,DESCRIPTOR>& lattice,
                   IncomprFlowParam<T> const& parameters,
                   OnLatticeBoundaryCondition2D<T,DESCRIPTOR>& boundaryCondition,
                   InletOutletT inletOutlet )
    const xint nx = parameters.getNx();
    const xint ny = parameters.getNy();

    // Note: The following approach illustrated here works only with boun-
    //   daries which are located on the outmost cells of the lattice. For
    //   boundaries inside the lattice, you need to use the version of
    //   "setVelocityConditionOnBlockBoundaries" which takes two Box2D
    //   arguments.

    // Velocity boundary condition on bottom wall. 
    boundaryCondition.setVelocityConditionOnBlockBoundaries (
            lattice, Box2D(0, nx-1, 1, 1),boundary::dirichlet );
    // Velocity boundary condition on top wall. 
    boundaryCondition.setVelocityConditionOnBlockBoundaries (
            lattice, Box2D(0, nx-1, ny-2, ny-2),boundary::dirichlet );

    // Pressure resp. velocity boundary condition on the inlet and outlet.
    if (inletOutlet == pressure) {
        // Note: pressure boundary conditions are currently implemented
        //   only for edges of the boundary, and not for corner nodes.
        boundaryCondition.setPressureConditionOnBlockBoundaries (
                lattice, Box2D(0,0, 1,ny-2) );
        boundaryCondition.setPressureConditionOnBlockBoundaries (
                lattice, Box2D(nx-1,nx-1, 1,ny-2) );
    else {
        boundaryCondition.setVelocityConditionOnBlockBoundaries (
                lattice, Box2D(0,0, 2,ny-3), boundary::neumann );
        boundaryCondition.setVelocityConditionOnBlockBoundaries (
                lattice, Box2D(nx-1,nx-1, 2,ny-3) , boundary::outflow );

    // Define the value of the imposed density on all nodes which have previously been
    //   defined to be pressure boundary nodes.
    setBoundaryDensity (
            lattice, lattice.getBoundingBox(),
            PoiseuilleDensity<T>(parameters) );
    // Define the value of the imposed velocity on all nodes which have previously been
    //   defined to be velocity boundary nodes.
    setBoundaryVelocity (
            lattice, lattice.getBoundingBox(),
            PoiseuilleVelocity<T>(parameters) );
    // Initialize all cells at an equilibrium distribution, with a velocity and density
    //   value of the analytical Poiseuille solution.
    initializeAtEquilibrium (
           lattice, lattice.getBoundingBox(),
           PoiseuilleDensityAndZeroVelocity<T>(parameters) );
    defineDynamics(lattice, lattice.getBoundingBox(),
                   new tubeDomain2D<T>(58,1),
                   new xfl::BounceBack<T,DESCRIPTOR>);

    // Call initialize to get the lattice ready for the simulation.

/// Produce a GIF snapshot of the velocity-norm.
void writeGif(MultiBlockLattice2D<T,DESCRIPTOR>& lattice, xint iter)
    const xint imSize = 600;

    ImageWriter<T> imageWriter("leeloo");
    imageWriter.writeScaledGif(createFileName("u", iter, 6),
                               imSize, imSize );

/// Write the full velocity and the velocity-norm into a VTK file.
void writeVTK(MultiBlockLattice2D<T,DESCRIPTOR>& lattice,
              IncomprFlowParam<T> const& parameters, xint iter)
    T dx = parameters.getDeltaX();
    T dt = parameters.getDeltaT();
    VtkImageOutput2D<T> vtkOut(createFileName("vtk", iter, 6), dx);
    vtkOut.writeData<float>(*computeVelocityNorm(lattice), "velocityNorm", dx/dt);
    vtkOut.writeData<2,float>(*computeVelocity(lattice), "velocity", dx/dt);

T computeRMSerror ( MultiBlockLattice2D<T,DESCRIPTOR>& lattice,
                    IncomprFlowParam<T> const& parameters )
    MultiTensorField2D<T,2> analyticalVelocity(lattice);
    setToFunction( analyticalVelocity, analyticalVelocity.getBoundingBox(),
                   PoiseuilleVelocity<T>(parameters) );
    MultiTensorField2D<T,2> numericalVelocity(lattice);
    computeVelocity(lattice, numericalVelocity, lattice.getBoundingBox());

           // Divide by lattice velocity to normalize the error
    return 1./parameters.getLatticeU() *
           // Compute RMS difference between analytical and numerical solution
               std::sqrt( computeAverage( *computeNormSqr(
                              *subtract(analyticalVelocity, numericalVelocity)
                         ) ) );

int main(int argc, char* argv[]) {
    xflInit(&argc, &argv);


    IncomprFlowParam<T> parameters(
            (T) 2e-2,  // uMax
            (T) 5.,    // Re
            60,        // N
            3.,        // lx
            1.         // ly 
    const T logT     = (T)0.1;
    const T imSave   = (T)0.01;
    const T vtkSave  = (T)2.;
    const T maxT     = (T)5.1;
    // Change this variable to "pressure" if you prefer a pressure boundary
    //   condition with Poiseuille profile for the inlet and the outlet.
    const InletOutletT inletOutlet = velocity;

    writeLogFile(parameters, "Poiseuille flow");

    MultiBlockLattice2D<T, DESCRIPTOR> lattice (
              parameters.getNx(), parameters.getNy(),
              new BGKdynamics<T,DESCRIPTOR>(parameters.getOmega()) );

        boundaryCondition = createLocalBoundaryCondition2D<T,DESCRIPTOR>();

    channelSetup(lattice, parameters, *boundaryCondition, inletOutlet);

    // Main loop over time iterations.
    for (xint iT=0; iT*parameters.getDeltaT()<maxT; ++iT) {
       if (iT%parameters.nStep(imSave)==0) {
            xcout << "Saving Gif ..." << endl;
            writeGif(lattice, iT);

        if (iT%parameters.nStep(vtkSave)==0 && iT>0) {
            xcout << "Saving VTK file ..." << endl;
            writeVTK(lattice, parameters, iT);

        if (iT%parameters.nStep(logT)==0) {
            xcout << "step " << iT
                  << "; t=" << iT*parameters.getDeltaT()
                  << "; RMS error=" << computeRMSerror(lattice, parameters);
            Array<T,2> uCenter;
            xcout << "; center velocity=" << uCenter[0]/parameters.getLatticeU() << endl;

        // Lattice Boltzmann iteration step.

    delete boundaryCondition;

Sorry for the long post couldn’t find spoiler tags. Thanks again