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文件當中。

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