网格问题代码

#include                                       //声明头文件
#include
#include
#include
#include
#include
#include
#include
using namespace std;
//D3Q19 3维19个离散速度
const int Q = 19;
int e[Q][3] = { { 0, 0, 0 }, { 1, 0, 0 }, { -1, 0, 0 }, { 0, 1, 0 }, { 0, -1, 0 }, { 0, 0, 1 }, { 0, 0, -1 }, { 1, 1, 0 }, { -1, -1, 0 }, { 1, -1, 0 },
{ -1, 1, 0 }, { 1, 0, 1 }, { -1, 0, -1 }, { 1, 0, -1 }, { -1, 0, 1 }, { 0, 1, 1 }, { 0, -1, -1 }, { 0, 1, -1 }, { 0, -1, 1 } };
double w[Q] = { 1. / 3, 1. / 18, 1. / 18, 1. / 18, 1. / 18, 1. / 18, 1. / 18, 1. / 36, 1. / 36, 1. / 36, 1. / 36, 1. / 36, 1. / 36, 1. / 36, 1. / 36, 1. / 36, 1. / 36, 1. / 36, 1. / 36 };

//定义物理尺寸
const int NX = 100;
const int NY = 100;
const int NZ = 100;
const double M = NX*NY*NZ;                                    //10*10*10的立方体为网格区域
//lattice boltzmann method
double rho[NX + 1][NY + 1][NZ + 1], u[NX + 1][NY + 1][NZ + 1][3], u0[NX + 1][NX + 1][NZ + 1][3];     //声明密度,演化后速度,演化前速度
double velocity;                                                                                     //定义速度
double f[NX + 1][NY + 1][NZ + 1][Q], F[NX + 1][NY + 1][NZ + 1][Q], feqq[NX + 1][NY + 1][NZ + 1][Q], ftemp[NX + 1][NY + 1][NZ + 1][Q];//声明速度分布函数演化前与演化后的,平衡态分布函数,临时分布函数
int i, j, k, q, ip, jp, kp, n;                                                                       //
double c, Re, dx, dy, dz, LX, LY, LZ, dt, rho0, tau_f, niu, omega, error, temp;//声明格子速度、雷诺数、空间步长、方向长度、时间步长、初始密度、弛豫时间、运动粘度、离散速度比、临时变量
//QuartetStructureGenerationSet
double arrgrid[NX + 1][NY + 1][NZ + 1], solidBoundaryNode[NX + 1][NY + 1][NZ + 1];               //定义格点  以及边界点(三维)
double rho_in = 1.0;                                                             //内部密度
double rho_out = 0.5;                                                            //外部密度
void initilization();                                                          //声明初始化子函数
void QuartetStructureGenerationSet();                                          //声明QSGS
double feq(int q, double rho, double u[3]);                                   //
void Evolution();                                                              //
void outputdata(int m);                                                        //声明输出
void Error();
void boundary();                                                               //声明边界条件
void Macroscopic();
void output3d();
int main()
{
 initilization();
 QuartetStructureGenerationSet();
 output3d();
 for (n = 0;; n++)
 {
  Evolution();
  Macroscopic();
  boundary();
  if (n % 100 == 0)
  {
   Error();
   cout << "The " << n << " th computation result: The max relative error of uvw is: "
    << setiosflags(ios::scientific) << error << endl;
   if (n >= 500)
   {
    if (n % 500 == 0)
     outputdata(n);
    if (error<1.0e-8)
     break;
   }
  }
 }
 return 0;
}
void initilization()                                                //定义初始化子函数
{
 dx = 1.0;                                                   //空间步长、时间步长均为1
 dy = 1.0;
 dz = 1.0;
 dt = dx;
 c = dx / dt;
 LX = dx*NX;                                                 //空间方向长度
 LY = dy*NY;
 LZ = dz*NZ;
 tau_f = 1.35;                                               //弛豫时间定为1.35?(tau_f = 3*niu+0.5,niu未知)
 omega = 1.0 / tau_f;                                        //书P48 式子3.103
 rho0 = 0.5;                                                 //初始密度为0.5
 for (k = 0; k <= NZ; k++)
 {
  for (j = 0; j <= NY; j++)
  {
   for (i = 0; i <= NX; i++)                   //初始化三维速度、密度
   {
    u[i][j][k][0] = 0.0;
    u[i][j][k][1] = 0.0;
    u[i][j][k][2] = 0.0;
    rho[i][j][k] = rho0;
    for (q = 0; q < Q; q++)            //格点个数以及分布函数
    {
     f[i][j][k][q] = feq(q, rho[i][j][k], u[i][j][k]);
    }
   }
  }
 }
}
void QuartetStructureGenerationSet()
{
 double d1_6 = 0.02;                                                      //定义六个主方向(接触)生长概率为0.02
 double d7_18 = d1_6 / 2.;                                                //定义十二个次方向(水平)生长概率为0.01
 double d19_26 = d1_6 / 8.;                                               //定义六个第三方向(对角线)生长概率为0.0025
 double n = 0.8;                                                         //定义孔隙率为0.8
 double cdd = 0.001;                                                     //定义生长核分布概率为0.001
 double numtotal_need = (1 - n)*NX*NY*NZ;                                //定义生长相的数量
 int numsoild = 0;                                                       //定义初始生长核数量为0
 int Tnumsoild;                                                          //定义整型生长核总量
 double soild[M+1][3];
 srand((unsigned)time(NULL));
 //第一步,遍历所有网格,生成固相内核
 for (i = 0; i <= NX; i++)                                               //生成格点[0][0][0],,,,[0][0][10];[0][1][0],,,[0][1][10];;;;[10][10][10]
 {
  for (j = 0; j <= NY; j++)
  {
   for (k = 0; k <= NZ; k++)
   {
    arrgrid[i][j][k] = 0;                           //初始化每一个格点,初始化后,格点的坐标变为[0][0][0],并赋值为0
    soild[numsoild][0] = 0;
    soild[numsoild][1] = 0;
    soild[numsoild][2] = 0;
    if (((rand() % 1000) / 1000.0) < cdd)           //随机分配一个0~0.9999的数值给每一个格点
    {
     arrgrid[i][j][k] = 1;                   //若该值小于生长核分布概率0.001,则赋值为1,并记录下它的坐标[i][j][k](表示该点为生长核)
     soild[numsoild][0] = i;
     soild[numsoild][1] = j;
     soild[numsoild][2] = k;
     numsoild = numsoild + 1;                //记录生长核的数量
    }
   }
  }
 }
 Tnumsoild = numsoild;                                                          //获得生长核总数
 //第二步,从固相核向周围26个方向生长
 while (Tnumsoild < numtotal_need)                                              //当生长核总数小于生长相(200)时,执行以下循环
 {
  for (int index_soild = 0; index_soild < Tnumsoild; index_soild++)      //分别记录所有生长核的三维坐标
  {
   int index_i = soild[index_soild][0];
   int index_j = soild[index_soild][1];
   int index_k = soild[index_soild][2];
   //1向右方向生长
   if (index_j < NY - 1)                                         //对于内部生长核而言
   {
    i = index_i;
    j = index_j + 1;
    k = index_k;
    if (arrgrid[i][j][k] == 0 && ((rand() % 1000) / 1000.0) < d1_6)            //该生长核右方向节点并非生长核并且方向随机生长率满足设定值
    {
     numsoild = numsoild + 1;                                           //该点成为生长核,并记录其坐标,其余方向同理
     arrgrid[i][j][k] = 1;
     soild[numsoild][0] = i;
     soild[numsoild][1] = j;
     soild[numsoild][2] = k;
    }
   }

   //2向后方向生长
   if (index_i > 0)
   {
    i = index_i - 1;
    j = index_j;
    k = index_k;
    if (arrgrid[i][j][k] == 0 && ((rand() % 1000) / 1000.0) < d1_6)
    {
     numsoild = numsoild + 1;
     arrgrid[i][j][k] = 1;
     soild[numsoild][0] = i;
     soild[numsoild][1] = j;
     soild[numsoild][2] = k;
    }
   }

   //3向左方向生长
   if (index_j > 0)
   {
    i = index_i;
    j = index_j - 1;
    k = index_k;
    if (arrgrid[i][j][k] == 0 && ((rand() % 1000) / 1000.0) < d1_6)
    {
     numsoild = numsoild + 1;
     arrgrid[i][j][k] = 1;
     soild[numsoild][0] = i;
     soild[numsoild][1] = j;
     soild[numsoild][2] = k;
    }
   }

   //4向前方向生长
   if (index_i < NX - 1)
   {
    i = index_i + 1;
    j = index_j;
    k = index_k;
    if (arrgrid[i][j][k] == 0 && ((rand() % 1000) / 1000.0) < d1_6)
    {
     numsoild = numsoild + 1;
     arrgrid[i][j][k] = 1;
     soild[numsoild][0] = i;
     soild[numsoild][1] = j;
     soild[numsoild][2] = k;
    }
   }
   //5向上方向生长
   if (index_k < NZ - 1)
   {
    i = index_i;
    j = index_j;
    k = index_k + 1;
    if (arrgrid[i][j][k] == 0 && ((rand() % 1000) / 1000.0) < d1_6)
    {
     numsoild = numsoild + 1;
     arrgrid[i][j][k] = 1;
     soild[numsoild][0] = i;
     soild[numsoild][1] = j;
     soild[numsoild][2] = k;
    }
   }
   //6向下方向生长
   if (index_k > 0)
   {
    i = index_i;
    j = index_j;
    k = index_k - 1;
    if (arrgrid[i][j][k] == 0 && ((rand() % 1000) / 1000.0) < d1_6)
    {
     numsoild = numsoild + 1;
     arrgrid[i][j][k] = 1;
     soild[numsoild][0] = i;
     soild[numsoild][1] = j;
     soild[numsoild][2] = k;
    }
   }
   //7向水平方向右前生长
   if (index_i < NX - 1 && index_j < NY - 1)
   {
    i = index_i + 1;
    j = index_j + 1;
    k = index_k;
    if (arrgrid[i][j][k] == 0 && ((rand() % 1000) / 1000.0) < d7_18)
    {
     numsoild = numsoild + 1;
     arrgrid[i][j][k] = 1;
     soild[numsoild][0] = i;
     soild[numsoild][1] = j;
     soild[numsoild][2] = k;
    }
   }
   //8向水平方向左前生长
   if (index_i < NX - 1 && index_j > 0)
   {
    i = index_i + 1;
    j = index_j - 1;
    k = index_k;
    if (arrgrid[i][j][k] == 0 && ((rand() % 1000) / 1000.0) < d7_18)
    {
     numsoild = numsoild + 1;
     arrgrid[i][j][k] = 1;
     soild[numsoild][0] = i;
     soild[numsoild][1] = j;
     soild[numsoild][2] = k;
    }
   }
   //9向水平方向右后生长
   if (index_i > 0 && index_j < NY - 1)
   {
    i = index_i - 1;
    j = index_j + 1;
    k = index_k;
    if (arrgrid[i][j][k] == 0 && ((rand() % 1000) / 1000.0) < d7_18)
    {
     numsoild = numsoild + 1;
     arrgrid[i][j][k] = 1;
     soild[numsoild][0] = i;
     soild[numsoild][1] = j;
     soild[numsoild][2] = k;
    }
   }
   //10向水平方向左后生长
   if (index_i > 0 && index_j > 0)
   {
    i = index_i - 1;
    j = index_j - 1;
    k = index_k;
    if (arrgrid[i][j][k] == 0 && ((rand() % 1000) / 1000.0) < d7_18)
    {
     numsoild = numsoild + 1;
     arrgrid[i][j][k] = 1;
     soild[numsoild][0] = i;
     soild[numsoild][1] = j;
     soild[numsoild][2] = k;
    }
   }
   //11向右上方向生长
   if (index_j < NY - 1 && index_k < NZ - 1)
   {
    i = index_i;
    j = index_j + 1;
    k = index_k + 1;
    if (arrgrid[i][j][k] == 0 && ((rand() % 1000) / 1000.0) < d7_18)
    {
     numsoild = numsoild + 1;
     arrgrid[i][j][k] = 1;
     soild[numsoild][0] = i;
     soild[numsoild][1] = j;
     soild[numsoild][2] = k;
    }
   }
   //12向右下方向生长
   if (index_j 0)
   {
    i = index_i;
    j = index_j + 1;
    k = index_k - 1;
    if (arrgrid[i][j][k] == 0 && ((rand() % 1000) / 1000.0) < d7_18)
    {
     numsoild = numsoild + 1;
     arrgrid[i][j][k] = 1;
     soild[numsoild][0] = i;
     soild[numsoild][1] = j;
     soild[numsoild][2] = k;
    }
   }
   //13向左上方向生长
   if (index_j > 0 && index_k < NZ - 1)
   {
    i = index_i;
    j = index_j - 1;
    k = index_k + 1;
    if (arrgrid[i][j][k] == 0 && ((rand() % 1000) / 1000.0) < d7_18)
    {
     numsoild = numsoild + 1;
     arrgrid[i][j][k] = 1;
     soild[numsoild][0] = i;
     soild[numsoild][1] = j;
     soild[numsoild][2] = k;
    }
   }
   //14向左下方向生长
   if (index_j > 0 && index_k > 0)
   {
    i = index_i;
    j = index_j - 1;
    k = index_k - 1;
    if (arrgrid[i][j][k] == 0 && ((rand() % 1000) / 1000.0) < d7_18)
    {
     numsoild = numsoild + 1;
     arrgrid[i][j][k] = 1;
     soild[numsoild][0] = i;
     soild[numsoild][1] = j;
     soild[numsoild][2] = k;
    }
   }
   //15向前上方向生长
   if (index_i < NX - 1 && index_k < NZ - 1)
   {
    i = index_i + 1;
    j = index_j;
    k = index_k + 1;
    if (arrgrid[i][j][k] == 0 && ((rand() % 1000) / 1000.0) < d7_18)
    {
     numsoild = numsoild + 1;
     arrgrid[i][j][k] = 1;
     soild[numsoild][0] = i;
     soild[numsoild][1] = j;
     soild[numsoild][2] = k;
    }
   }
   //16向前下方向生长
   if (index_i 0)
   {
    i = index_i + 1;
    j = index_j;
    k = index_k - 1;
    if (arrgrid[i][j][k] == 0 && ((rand() % 1000) / 1000.0) < d7_18)
    {
     numsoild = numsoild + 1;
     arrgrid[i][j][k] = 1;
     soild[numsoild][0] = i;
     soild[numsoild][1] = j;
     soild[numsoild][2] = k;
    }
   }
   //17向后上方向生长
   if (index_i > 0 && index_k < NZ - 1)
   {
    i = index_i - 1;
    j = index_j;
    k = index_k + 1;
    if (arrgrid[i][j][k] == 0 && ((rand() % 1000) / 1000.0) < d7_18)
    {
     numsoild = numsoild + 1;
     arrgrid[i][j][k] = 1;
     soild[numsoild][0] = i;
     soild[numsoild][1] = j;
     soild[numsoild][2] = k;
    }
   }
   //18向后下方向生长
   if (index_i > 0 && index_k > 0)
   {
    i = index_i - 1;
    j = index_j;
    k = index_k - 1;
    if (arrgrid[i][j][k] == 0 && ((rand() % 1000) / 1000.0) < d7_18)
    {
     numsoild = numsoild + 1;
     arrgrid[i][j][k] = 1;
     soild[numsoild][0] = i;
     soild[numsoild][1] = j;
     soild[numsoild][2] = k;
    }
   }
   //19向右前上对角线方向生长
   if (index_i < NX - 1 && index_j < NY - 1 && index_k < NZ - 1)
   {
    i = index_i + 1;
    j = index_j + 1;
    k = index_k + 1;
    if (arrgrid[i][j][k] == 0 && ((rand() % 1000) / 1000.0) < d19_26)
    {
     numsoild = numsoild + 1;
     arrgrid[i][j][k] = 1;
     soild[numsoild][0] = i;
     soild[numsoild][1] = j;
     soild[numsoild][2] = k;
    }
   }
   //20向右后上对角线方向生长
   if (index_i > 0 && index_j < NY - 1 && index_k < NZ - 1)
   {
    i = index_i - 1;
    j = index_j + 1;
    k = index_k + 1;
    if (arrgrid[i][j][k] == 0 && ((rand() % 1000) / 1000.0) < d19_26)
    {
     numsoild = numsoild + 1;
     arrgrid[i][j][k] = 1;
     soild[numsoild][0] = i;
     soild[numsoild][1] = j;
     soild[numsoild][2] = k;
    }
   }
   //21向左后上对角线方向生长
   if (index_i > 0 && index_j > 0 && index_k < NZ - 1)
   {
    i = index_i - 1;
    j = index_j - 1;
    k = index_k + 1;
    if (arrgrid[i][j][k] == 0 && ((rand() % 1000) / 1000.0) < d19_26)
    {
     numsoild = numsoild + 1;
     arrgrid[i][j][k] = 1;
     soild[numsoild][0] = i;
     soild[numsoild][1] = j;
     soild[numsoild][2] = k;
    }
   }
   //22向左前上对角线方向生长
   if (index_i0 && index_k < NZ - 1)
   {
    i = index_i + 1;
    j = index_j - 1;
    k = index_k + 1;
    if (arrgrid[i][j][k] == 0 && ((rand() % 1000) / 1000.0) < d19_26)
    {
     numsoild = numsoild + 1;
     arrgrid[i][j][k] = 1;
     soild[numsoild][0] = i;
     soild[numsoild][1] = j;
     soild[numsoild][2] = k;
    }
   }
   //23向右前下对角线方向生长
   if (index_i < NX - 1 && index_j0)
   {
    i = index_i + 1;
    j = index_j + 1;
    k = index_k - 1;
    if (arrgrid[i][j][k] == 0 && ((rand() % 1000) / 1000.0) < d19_26)
    {
     numsoild = numsoild + 1;
     arrgrid[i][j][k] = 1;
     soild[numsoild][0] = i;
     soild[numsoild][1] = j;
     soild[numsoild][2] = k;
    }
   }
   //24向右后下对角线方向生长
   if (index_i > 0 && index_j0)
   {
    i = index_i - 1;
    j = index_j + 1;
    k = index_k - 1;
    if (arrgrid[i][j][k] == 0 && ((rand() % 1000) / 1000.0) < d19_26)
    {
     numsoild = numsoild + 1;
     arrgrid[i][j][k] = 1;
     soild[numsoild][0] = i;
     soild[numsoild][1] = j;
     soild[numsoild][2] = k;
    }
   }
   //25向左后下对角线方向生长
   if (index_i > 0 && index_j > 0 && index_k > 0)
   {
    i = index_i - 1;
    j = index_j - 1;
    k = index_k - 1;
    if (arrgrid[i][j][k] == 0 && ((rand() % 1000) / 1000.0) < d19_26)
    {
     numsoild = numsoild + 1;
     arrgrid[i][j][k] = 1;
     soild[numsoild][0] = i;
     soild[numsoild][1] = j;
     soild[numsoild][2] = k;
    }
   }
   //26向左前下对角线方向生长
   if (index_i0 && index_k > 0)
   {
    i = index_i + 1;
    j = index_j - 1;
    k = index_k - 1;
    if (arrgrid[i][j][k] == 0 && ((rand() % 1000) / 1000.0) < d19_26)
    {
     numsoild = numsoild + 1;
     arrgrid[i][j][k] = 1;
     soild[numsoild][0] = i;
     soild[numsoild][1] = j;
     soild[numsoild][2] = k;
    }
   }
  }
  Tnumsoild = numsoild;//统计生长相个数
 }
}
double feq(int q, double rho, double u[3])                       //定义平衡态分布函数
{
 double eu, uvw, feq;
 eu = e[q][0] * u[0] + e[q][1] * u[1] + e[q][2] * u[2];   //离散速度
 uvw = u[0] * u[0] + u[1] * u[1] + u[2] * u[2];           //u^2项
 feq = w[q] * rho* (1.0 + 3.0*eu + 4.5*eu*eu - 1.5*uvw);  //书P49 式子3.106
 return feq;
}
void Evolution()                                                 //演化
{
 //Collision step 碰撞过程
 for (k = 0; k <= NZ; k++)
 {
  for (j = 0; j <= NY; j++)
  {
   for (i = 0; i <= NX; i++)
   {
    for (q = 0; q < Q; q++)                             //对于19个方向
    {
     if (arrgrid[i][j][k] == 0)                 //对于网格内的所有点进行划分,如果某点为间隙(非生长相),则进行逼近
     {
      f[i][j][k][q] = f[i][j][k][q] + omega*(feq(q, rho[i][j][k], u[i][j][k]) - f[i][j][k][q]);//得到演化后的速度分布函数,书P48 式子3.104
     }
    }
   }
  }
 }
 //streaming
 for (k = 0; k <= NZ; k++)
 {
  for (j = 0; j <= NY; j++)
  {
   for (i = 0; i <= NX; i++)
   {
    for (q = 0; q < Q; q++)
    {
     ip = i + e[q][0];         //演化后的三维位置
     jp = j + e[q][1];
     kp = k + e[q][2];
     if (ip >= 0 && jp >= 0 && kp >= 0 && ip <= NX && jp <= NY && kp <= NZ)          //该点位于网格区域内部
     {
      ftemp[ip][jp][kp][q] = f[i][j][k][q];         //得到一个演化后的临时速度分布函数
     }
    }
   }
  }
 }
 for (k = 0; k <= NZ; k++)
 {
  for (j = 0; j <= NY; j++)
  {
   for (i = 0; i <= NX; i++)
   {
    for (q = 0; q < Q; q++)
    {
     f[i][j][k][q] = ftemp[i][j][k][q];
    }
   }
  }
 }
}
void Macroscopic()          //计算宏观量
{
 //macroscopic
 for (k = 0; k <= NZ; k++)
 {
  for (j = 0; j <= NY; j++)
  {
   for (i = 0; i <= NX; i++)
   {
    u0[i][j][k][0] = u[i][j][k][0];         //初始状态为0
    u0[i][j][k][1] = u[i][j][k][1];
    u0[i][j][k][2] = u[i][j][k][2];
    rho[i][j][k] = 0.0;
    u[i][j][k][0] = 0.0;
    u[i][j][k][1] = 0.0;
    u[i][j][k][2] = 0.0;
    if (arrgrid[i][j][k] == 0)                     //如果时孔隙,19个方向演化
    {
     for (q = 0; q < Q; q++)
     {
      rho[i][j][k] += f[i][j][k][q];
      u[i][j][k][0] += e[q][0] * f[i][j][k][q];
      u[i][j][k][1] += e[q][1] * f[i][j][k][q];
      u[i][j][k][2] += e[q][2] * f[i][j][k][q];
     }
     u[i][j][k][0] = u[i][j][k][0] / rho[i][j][k];
     u[i][j][k][1] = u[i][j][k][1] / rho[i][j][k];
     u[i][j][k][2] = u[i][j][k][2] / rho[i][j][k];
    }
   }
  }
 }
}

void boundary()          //定义边界层
{
 // bounce back
 for (k = 0; k <= NZ; k++)
 {
  for (j = 0; j <= NY; j++)
  {
   for (i = 0; i <= NX; i++)
   {
    if (arrgrid[i][j][k] == 1)            //在生长相处,格点的速度分布进行反向交换
    {
     temp = f[i][j][k][1];
     f[i][j][k][1] = f[i][j][k][2];
     f[i][j][k][2] = temp;
     temp = f[i][j][k][3];
     f[i][j][k][3] = f[i][j][k][4];
     f[i][j][k][4] = temp;
     temp = f[i][j][k][5];
     f[i][j][k][5] = f[i][j][k][6];
     f[i][j][k][6] = temp;
     temp = f[i][j][k][7];
     f[i][j][k][7] = f[i][j][k][8];
     f[i][j][k][8] = temp;
     temp = f[i][j][k][9];
     f[i][j][k][9] = f[i][j][k][10];
     f[i][j][k][10] = temp;
     temp = f[i][j][k][11];
     f[i][j][k][11] = f[i][j][k][12];
     f[i][j][k][12] = temp;
     temp = f[i][j][k][13];
     f[i][j][k][13] = f[i][j][k][14];
     f[i][j][k][14] = temp;
     temp = f[i][j][k][15];
     f[i][j][k][15] = f[i][j][k][16];
     f[i][j][k][16] = temp;
     temp = f[i][j][k][17];
     f[i][j][k][17] = f[i][j][k][18];
     f[i][j][k][18] = temp;
    }
   }
  }
 }
 //left and right boundary                   左右边界,密度,左边取内部密度,右边取外部密度;取相邻格点的参数作为边界处的参数,速度分布进行演化
 for (k = 1; k < NZ; k++)
 {
  for (j = 1; j < NY; j++)
  {
   for (q = 0; q < Q; q++)
   {
    rho[0][j][k] = rho_in;
    u[0][j][k][0] = u[1][j][k][0];
    u[0][j][k][1] = u[1][j][k][1];
    u[0][j][k][2] = u[1][j][k][2];
    f[0][j][k][q] = feq(q, rho[0][j][k], u[0][j][k]) + f[1][j][k][q] - feq(q, rho[1][j][k], u[1][j][k]);
    rho[NX][j][k] = rho_out;
    u[NX][j][k][0] = u[NX - 1][j][k][0];
    u[NX][j][k][1] = u[NX - 1][j][k][1];
    u[NX][j][k][2] = u[NX - 1][j][k][2];
    f[NX][j][k][q] = feq(q, rho[NX][j][k], u[NX][j][k]) + f[NX - 1][j][k][q] - feq(q, rho[NX - 1][j][k], u[NX - 1][j][k]);
   }
  }
 }
 //top and bottom boundary                   上下边界的密度取相邻格点的密度;速度为0;速度分布进行演化
 for (j = 0; j <= NY; j++)
 {
  for (i = 0; i <= NX; i++)
  {
   for (q = 0; q < Q; q++)
   {
    rho[i][j][0] = rho[i][j][1];
    u[i][j][0][0] = 0;
    u[i][j][0][1] = 0;
    u[i][j][0][2] = 0;
    f[i][j][0][q] = feq(q, rho[i][j][0], u[i][j][0]) + f[i][j][1][q] - feq(q, rho[i][j][1], u[i][j][1]);
    rho[i][j][NZ] = rho[i][j][NZ - 1];
    u[i][j][NZ][0] = 0;
    u[i][j][NZ][1] = 0;
    u[i][j][NZ][2] = 0;
    f[i][j][NZ][q] = feq(q, rho[i][j][NZ], u[i][j][NZ]) + f[i][j][NZ - 1][q] - feq(q, rho[i][j][NZ - 1], u[i][j][NZ - 1]);
   }
  }
 }
 //former and behind boundary             前后的边界与上下的相同
 for (k = 0; k <= NZ; k++)
 {
  for (i = 0; i <= NX; i++)
  {
   for (q = 0; q < Q; q++)
   {
    rho[i][0][k] = rho[i][1][k];
    u[i][0][k][0] = 0;
    u[i][0][k][1] = 0;
    u[i][0][k][2] = 0;
    f[i][0][k][q] = feq(q, rho[i][0][k], u[i][0][k]) + f[i][1][k][q] - feq(q, rho[i][1][k], u[i][1][k]);
    rho[i][NY][k] = rho[i][NY - 1][k];
    u[i][NY][k][0] = 0;
    u[i][NY][k][1] = 0;
    u[i][NY][k][2] = 0;
    f[i][NY][k][q] = feq(q, rho[i][NY][k], u[i][NY][k]) + f[i][NY - 1][k][q] - feq(q, rho[i][NY - 1][k], u[i][NY - 1][k]);
   }
  }
 }
}
void Error()         //比较误差
{
 double temp1, temp2;
 temp1 = 0;
 temp2 = 0;
 for (k = 0; k <= NZ; k++)
 {
  for (j = 0; j <= NY; j++)
  {
   for (i = 0; i <= NX; i++)
   {
    temp1 += ((u[i][j][k][0] - u0[i][j][k][0])*(u[i][j][k][0] - u0[i][j][k][0]) +
     (u[i][j][k][1] - u0[i][j][k][1])*(u[i][j][k][1] - u0[i][j][k][1]) +
     (u[i][j][k][2] - u0[i][j][k][2])*(u[i][j][k][2] - u0[i][j][k][2]));
    temp2 += (u[i][j][k][0] * u[i][j][k][0] + u[i][j][k][1] * u[i][j][k][1] + u[i][j][k][2] * u[i][j][k][2]);
   }
  }
 }
 temp1 = sqrt(temp1);
 temp2 = sqrt(temp2);
 error = temp1 / (temp2 + 1e-30);
}
void outputdata(int m)              //输出文件  格子玻尔兹曼在多孔介质中的传热;多孔介质
{
 ostringstream name;         //三维坐标;三维速度;密度;是孔隙还是生长相;动量;格子的区域为多大
 name << "LBM_heat_transfer_through_porous_media_" << m << ".dat";
 ofstream out(name.str().c_str());
 out << "Titel=\"porous media\"\n"
  << "VARIABLES=\"X\",\"Y\",\"Z\",\"U\",\"V\",\"W\",\"rho\",\"arr\",\"vel\"\n" << "ZONE T=\"BOX\",I="
  << NX + 1 << ",J=" << NY + 1 << ",K=" << NZ + 1 << ",F=POINT" << endl;
 for (k = 0; k <= NZ; k++)
 {
  for (j = 0; j <= NY; j++)
  {
   for (i = 0; i <= NX; i++)
   {
    out << double(i)*dx << " " << double(j)*dy << " " << double(k)*dy
     << " " << u[i][j][k][0] << " " << u[i][j][k][1] << " " << u[i][j][k][2] << " "
     << rho[i][j][k] << " " << arrgrid[i][j][k] << " "
     << pow(u[i][j][k][0] * u[i][j][k][0] + u[i][j][k][1] * u[i][j][k][1] + u[i][j][k][2] * u[i][j][k][2], 0.5) << endl;
   }
  }
 }
}
void output3d()
{
 ostringstream name2;
 name2<<"3D.plt";
 ofstream out(name2.str().c_str());
 out<<"variables=X,Y,Z,S"< out<<"zone I="<   }
  }
 }
}


你可能感兴趣的:(网格问题代码)