Palabos案例解析(二)tutorial_1_5.cpp案例————回答Just1ceOne同学的疑问

Palabos2.0原始基础案例tutorial_1_5.cpp

文件位置:\palabos-v2.0r0\examples\tutorial\tutorial_1/tutorial_1_5.cpp

注:Just1ceOne同学的所有问题都标注在源代码当中,答案写在源代码后面。
/* This file is part of the Palabos library.
 *
 * Copyright (C) 2011-2017 FlowKit Sarl
 * Route d'Oron 2
 * 1010 Lausanne, Switzerland
 * E-mail contact: [email protected]
 *
 * The most recent release of Palabos can be downloaded at 
 * <http://www.palabos.org/>
 *
 * The library Palabos is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * The library 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "palabos2D.h"
#include "palabos2D.hh"
#include <vector>
#include <iostream>
#include <iomanip>

/* Code 1.5 in the Palabos tutorial
 */

using namespace plb;
using namespace std;

typedef double T;
#define DESCRIPTOR plb::descriptors::D2Q9Descriptor

/// Velocity on the parabolic Poiseuille profile
T poiseuilleVelocity(plint iY, IncomprFlowParam<T> const& parameters) {
    T y = (T)iY / parameters.getResolution();
    return 4.*parameters.getLatticeU() * (y-y*y);
    //////Q1:getLatticeU是读取parameter中的哪一个值呀?
}

/// A functional, used to initialize the velocity for the boundary conditions
template<typename T>
class PoiseuilleVelocity {
public:
    PoiseuilleVelocity(IncomprFlowParam<T> parameters_)
        : parameters(parameters_)
    { }
    /// This version of the operator returns the velocity only,
    ///    to instantiate the boundary condition.
    void operator()(plint iX, plint iY, Array<T,2>& u) const {
        u[0] = poiseuilleVelocity(iY, parameters);
        u[1] = T();                                                     
        ///////Q2:这两个运算符重载中u[1]是什么意思呀?
    }
    /// This version of the operator returns also a constant value for
    ///    the density, to create the initial condition.
    void operator()(plint iX, plint iY, T& rho, Array<T,2>& u) const {
        u[0] = poiseuilleVelocity(iY, parameters);
        u[1] = T();
        rho  = (T)1;
    }
private:
    IncomprFlowParam<T> parameters;
};

void channelSetup (
        MultiBlockLattice2D<T,DESCRIPTOR>& lattice,
        IncomprFlowParam<T> const& parameters,
        OnLatticeBoundaryCondition2D<T,DESCRIPTOR>& boundaryCondition )
{
    // Create Velocity boundary conditions.
    boundaryCondition.setVelocityConditionOnBlockBoundaries(lattice);

    // Specify the boundary velocity.
    setBoundaryVelocity (
            lattice, lattice.getBoundingBox(), 
            ////////Q3:经常看到lattice中的getBoundingBox函数,它和另一个常出现getBounding分别是什么意思呢?
            PoiseuilleVelocity<T>(parameters) );

    // Create the initial condition.
    initializeAtEquilibrium (
           lattice, lattice.getBoundingBox(), PoiseuilleVelocity<T>(parameters) );

    lattice.initialize();
}

void writeGifs(MultiBlockLattice2D<T,DESCRIPTOR>& lattice, plint iter)
{
    const plint imSize = 600;
    ImageWriter<T> imageWriter("leeloo");
    imageWriter.writeScaledGif(createFileName("u", iter, 6),
                               *computeVelocityNorm(lattice),
                               imSize, imSize );                     
                               /////////Q4:imSize是什么用途呢?
}

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

    global::directories().setOutputDir("./tmp/");

    // Use the class IncomprFlowParam to convert from
    //   dimensionless variables to lattice units, in the
    //   context of incompressible flows.
    IncomprFlowParam<T> parameters(
            (T) 1e-2,  // Reference velocity (the maximum velocity                   
            ///////Q5:parameter中的参数单位是什么呢?存在实际尺寸与晶格的换算关系吗?
            //////////下面116行的imSave和maxT的单位是什么呢?
                       //   in the Poiseuille profile) in lattice units.
            (T) 100.,  // Reynolds number
            100,       // Resolution of the reference length (channel height).
            2.,        // Channel length in dimensionless variables
            1.         // Channel height in dimensionless variables
    );
    const T imSave   = (T)0.1;  // Time intervals at which to save GIF
                                //   images, in dimensionless time units.
    const T maxT     = (T)3.1;  // Total simulation time, in dimensionless
                                //   time units.

    writeLogFile(parameters, "Poiseuille flow");                                     
    /////Q6:这一行不是很明白,但经常看到类似代码,求解......

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

    OnLatticeBoundaryCondition2D<T,DESCRIPTOR>*
        boundaryCondition = createLocalBoundaryCondition2D<T,DESCRIPTOR>();

    channelSetup(lattice, parameters, *boundaryCondition);

    // Main loop over time iterations.
    for (plint iT=0; iT*parameters.getDeltaT()<maxT; ++iT) {
        if (iT%parameters.nStep(imSave)==0 && iT>0) {
            pcout << "Saving Gif at time step " << iT << endl;
            writeGifs(lattice, iT);
        }
        // Execute lattice Boltzmann iteration.
        lattice.collideAndStream();
    }

    delete boundaryCondition;
}

Q1:getLatticeU是读取parameter中的哪一个值呀?

答:在本代码中,parameter是IncomprFlowParam类的一个实例对象,用于统一配置不可压缩流动相关参数,从而可以统一参数的调用,getLatticeU没记错的话应该就是取的其实就是格子速度,也就是你输入的第一个参数值,这个类主要就是为了统一调配参数,并不太涉及任何计算,实现目的主要是为了统一代码写作,防止在需要改某一参数时满篇找对应参数位置修改。在源代码文件units.h文件中有这个类的明确定义,可以打开看一下,注释写的挺明白的。

Q2:这两个运算符重载中u[1]是什么意思呀?

答:u[1]=0,只不过按照规范这么写比较好,写成T(),实际上就是0,u[0]对应x,u[1]对应y,就是配置好一个用来初始化速度密度的对象,在调用setBoundaryVelocity()当中使用函数指针作为参数,函数指针的使用,主要是为了可以重载一些函数,这个位置只要按格式即接口写好相应的函数,就可以简易的设置某些值,是数据处理器类的一个简便使用方法。

Q3:经常看到lattice中的getBoundingBox函数,它和另一个常出现getBounding分别是什么意思呢?

答:getBoundingBox()是获得整个计算域的计算范围,返回一个BoxXD对象,整个Palabos中并不存在getBounding对应的函数,你可能看错了,或者和OpenLB混淆了。

Q4:imSize是什么用途呢?

答:这个是图像输出的一个参数,好像是像素值,图片输出需要多少像素。具体请看源代码包里的io文件夹内的文件。

Q5:parameter中的参数单位是什么呢?存在实际尺寸与晶格的换算关系吗?下面的imSave和maxT的单位是什么呢?

答:得到的是格子速度和对应参数,单位就是格子单位。imSave和maxT是用来控制文件输出和总计算时间步的,imSave是多少步存一次图片数据,maxT是总共计算多少时间步,没有单位。

Q6:这一行不是很明白,但经常看到类似代码,求解…writeLogFile(parameters, “Poiseuille flow”);

答:这个是配套的一套将输入参数输出为固定格式log记录文件的一个函数调用,配套IncomprFlowParam类而存在,同样在units.h文件当中。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章