Palabos程序代码解读(Code Explanation) | tutorial_1 | tutorial_1_1

/* This file is part of the Palabos library.
 *
 * The Palabos softare is developed since 2011 by FlowKit-Numeca Group Sarl
 * (Switzerland) and the University of Geneva (Switzerland), which jointly
 * own the IP rights for most of the code base. Since October 2019, the
 * Palabos project is maintained by the University of Geneva and accepts
 * source code contributions from the community.
 * 
 * Contact:
 * Jonas Latt
 * Computer Science Department
 * University of Geneva
 * 7 Route de Drize
 * 1227 Carouge, Switzerland
 * jonas.latt@unige.ch
 *
 * The most recent release of Palabos can be downloaded at 
 * <https://palabos.unige.ch/>
 *
 * 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/>.
*/

/* Code 1.1 in the Palabos tutorial
 */

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

using namespace plb;
using namespace std;

//T是灵魂
typedef double T;

//使用D2Q9模型
#define DESCRIPTOR plb::descriptors::D2Q9Descriptor

// Initialize the lattice at zero velocity and constant density, except
//   for a slight density excess on a square sub-domain.
// 以常数密度和零速度定义lattice,而一小块子域的密度要稍稍高一些。
void defineInitialDensityAtCenter(MultiBlockLattice2D<T,DESCRIPTOR>& lattice)
{
    // The lattice is of size nx-by-ny
    // 用plint定义常量nx和ny,分别为流域的x和y长度。
    const plint nx = lattice.getNx();
    const plint ny = lattice.getNy();

    // Create a Box2D which describes the location of cells with a slightly
    //   higher density.
    // plint定义三个变量
    plint centralSquareRadius = nx/6; //方形边长的一半
    plint centerX = nx/3; //流域x轴三分之一的位置,此处意为子域方形的中心点x轴坐标
    plint centerY = ny/4; //流域y轴四分之一的位置,此处意为子域方形的中心点y轴坐标
    //使用Box2D来定义centralSquare,需输入x1,x2,y1,y2,其中x轴从x1至x2,y轴从y1到y2,四个点确定2D域
    //此处x1 = centerX - centralSquareRadius,x2 = centerX + centralSquareRadius
    Box2D centralSquare (
            centerX - centralSquareRadius, centerX + centralSquareRadius,
            centerY - centralSquareRadius, centerY + centralSquareRadius );

    // All cells have initially density rho ...
    // 所有的cell初始化密度rho
    T rho0 = 1.;
    // .. except for those in the box "centralSquare" which have density
    //    rho+deltaRho
    //为流域中部的方形子域准备一个稍微大一点的密度
    T deltaRho = 1.e-4;
    //像规模较大的数据,如LB模拟的粒子群,都储存在palabos的BlockLattice类型里
    //像Array<T,nDim>一般是小型数据,此处为T数据类型(在程序最前已定义为双精度浮点型)
    //nDim为尺寸,此处2表示这个固定尺寸的array有两个元素
    //你也可以在定义这2或3元素Array的同时,储存一些物理向量进去
    Array<T,2> u0((T)0,(T)0);

    // Initialize constant density everywhere.
    // 初始化常数密度,initializeAtEquilibrium(lattice, domain, rho, velocity),在该区域的cells中初始化平衡分布
    initializeAtEquilibrium (
           lattice, lattice.getBoundingBox(), rho0, u0 );

    // And slightly higher density in the central box.
    //中间方形子域的密度稍微高一些
    initializeAtEquilibrium (
           lattice, centralSquare, rho0 + deltaRho, u0 );
	//lattice初始化
    lattice.initialize();
} //至此void defineInitialDensityAtCenter(MultiBlockLattice2D<T,DESCRIPTOR>& lattice)部分结束
//记住这个defineInitialDensityAtCenter,后面还会出现

//下面这行代码,一般通过argc和argv在cpp中用于访问程序参数
//在程序的任何位置都可以访问这两个全局对象
int main(int argc, char* argv[]) {
    plbInit(&argc, &argv);

//设定文件输出位置,即打开tmp文件夹
    global::directories().setOutputDir("./tmp/");

    const plint maxIter = 1000; // Iterate during 1000 steps.迭代次数
    const plint nx = 600;       // Choice of lattice dimensions.流域x轴方向lattice格子数
    const plint ny = 600;
    const T omega = 1.;        // Choice of the relaxation parameter松弛时间
//初始化一个nx,ny,BGK模型的T数据类型(即双精度浮点型)的D2Q9(DESCRIPTOR在程序最上面已定义)的lattice
    MultiBlockLattice2D<T, DESCRIPTOR> lattice (
           nx, ny, new BGKdynamics<T,DESCRIPTOR>(omega) );

//true或false来调整循环边界
    lattice.periodicity().toggleAll(true); // Use periodic boundaries.
//还记得这个defineInitialDensityAtCenter吗?
    defineInitialDensityAtCenter(lattice);

    // Main loop over time iterations.
    // 主要的迭代过程
    for (plint iT=0; iT<maxIter; ++iT) {
        if (iT%40==0) {  // Write an image every 40th time step.以40取余为0时
            pcout << "Writing GIF file at iT=" << iT << endl; 
            // Instantiate an image writer with the color map "leeloo".
            // 初始化绘图,颜色表leeloo
            // leeloo可以换成earth, water, air, fire
            ImageWriter<T> imageWriter("leeloo");
            // Write a GIF file with colors rescaled to the range of values
            //   in the matrix
            // 启动绘图
            imageWriter.writeScaledGif (
                    createFileName("u", iT, 6),
                    *computeVelocityNorm(lattice) );
        }//绘图结束
        // Execute lattice Boltzmann iteration.
        // 执行碰撞和流动
        lattice.collideAndStream();
    }//下一轮循环,直到iT达到maxIter
}

  • 9
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值