2021-04-24

拟一维喷管流动的数值解程序分享

亚声速——超声速等熵喷管流动的CFD解法(非守恒型方程)

本文主要是对安德森的《计算流体力学基础与应用》一书中的应用部分给出了笔者自己写的计算程序,供大家参考。水平有限,请见谅!

文章目录

  • 拟一维喷管流动的数值解程序分享
    • 亚声速——超声速等熵喷管流动的CFD解法(非守恒型方程)
      • Matlab
        • Nozzle.m
        • OneStepNozzle.m
      • C++
        • OneStepNozzle.h
        • OneStepNozzle.cpp
        • 拟一维喷管流动的数值解.cpp

Matlab

Nozzle.m

%%
% Author: CXT
% Date: 2021/4/15 
% Theme: 拟一维亚声速-超声速等熵喷管
%% 物理符号(无量纲量);
% x —— 空间位置;
% A —— 喷管面积;
% rho —— 密度;
% V —— 速度;
% p —— 压力;
% N —— 计算节点数;
%%
clc;
clear;
close all;
%% 初始条件;
N = 31;% 计算节点数;
x = 0:(3-0)/(N-1):3; % 计算区间;
n = 1400; %时间步数;

rho0 = (1 - 0.3146*x)/1;
T0 = (1 - 0.2314*x)/1;
V0 = (0.1 + 1.09*x).*sqrt(T0);
%% 喷管形状;
A = 1 + 2.2*(x - 1.5).^2;
A = A./1;
%% 
rho_t = rho0;
T_t = T0;
V_t = V0;

for i = 1:n
    [rho_t, V_t, T_t, p_t, Ma] = OneStepNozzle(rho_t, V_t, T_t, N, x, A);
    
    plot(i,rho_t(16),'*');
    hold on;
end
hold off;

OneStepNozzle.m

function [rho_t, V_t, T_t, p_t, Ma] = OneStepNozzle(rho, V, T, N, X, A)
%[rho_t, V_t, T_t, p_t, Ma] = OneStepNozzle(rho, V, T) 拟一维喷管流动的一个时间步长的计算
%   rho —— 初始密度;
%   V —— 初始速度;
%   T —— 初始温度;
%   N —— 计算节点数;
%   X —— 计算区间;
%   A —— 喷管形状参数;

%   rho_t —— 下一步的密度;
%   V_t —— 下一步的速度;
%   T_t —— 下一步的温度;
%   p_t —— 下一步的压力;
%   Ma —— 马赫数;
%% 默认参数;
Delta_x = (X(end)-X(1))/(N - 1); %空间步长;
gama = 1.4; %绝热指数;
C = 0.5; %柯朗数;
%% 预估步计算;
% 向前差分;
% 只计算内点;
partial_rho = zeros(1, 3/Delta_x + 1);
partial_V = zeros(1, 3/Delta_x + 1);
partial_T = zeros(1, 3/Delta_x + 1);

for i = 1:N - 1
    
   partial_rho(i) = - V(i)*(rho(i+1) - rho(i))/Delta_x...
                  - rho(i)*(V(i+1) - V(i))/Delta_x...
                  - rho(i)*V(i)*(log(A(i+1)) - log(A(i)))/Delta_x;
              
   partial_V(i) = - V(i)*(V(i+1) - V(i))/Delta_x...
                  - 1/gama*((T(i+1) - T(i))/Delta_x...
                  + T(i)/rho(i)*(rho(i+1)-rho(i))/Delta_x);
    
   partial_T(i) = - V(i)*(T(i+1) - T(i))/Delta_x...
                   - (gama - 1)*T(i)*((V(i+1) - V(i))/Delta_x...
                   + V(i)*(log(A(i+1)) - log(A(i)))/Delta_x);

end
%% 计算时间步长;
Delta_t = min(C * Delta_x ./ (V + sqrt(T)));
%% 计算预估值
bar_rho = rho + partial_rho * Delta_t;
bar_V = V + partial_V * Delta_t;
bar_T = T + partial_T * Delta_t;
%% 计算校正步;
% 向后差分;
% 只计算内点;
bar_partial_rho = zeros(1, 3/Delta_x + 1);
bar_partial_V = zeros(1, 3/Delta_x + 1);
bar_partial_T = zeros(1, 3/Delta_x + 1);

for i = 2:N-1
    
   bar_partial_rho(i) = - bar_V(i)*(bar_rho(i) - bar_rho(i-1))/Delta_x...
                        - bar_rho(i)*(bar_V(i) - bar_V(i-1))/Delta_x...
                        - bar_rho(i)*bar_V(i)*(log(A(i)) - log(A(i-1)))/Delta_x;
   
   bar_partial_V(i) = - bar_V(i)*(bar_V(i) - bar_V(i-1))/Delta_x...
                       - ((bar_T(i) - bar_T(i-1))/Delta_x...
                       + bar_T(i)*(bar_rho(i) - bar_rho(i-1))/(bar_rho(i)*Delta_x))/gama;
    
   bar_partial_T(i) = - bar_V(i)*(bar_T(i) - bar_T(i-1))/Delta_x...
                      - (gama - 1)*bar_T(i)*((bar_V(i) - bar_V(i-1))/Delta_x...
                      + bar_V(i)*(log(A(i)) - log(A(i-1)))/Delta_x); 

end
%% 计算平均时间导数;
partial_rho_av = (partial_rho + bar_partial_rho)/2;
partial_V_av = (partial_V + bar_partial_V)/2;
partial_T_av = (partial_T + bar_partial_T)/2;
%% 校正值;
% t + Delta_t 时的参数;
rho_t = rho + partial_rho_av*Delta_t;
V_t = V + partial_V_av*Delta_t;
T_t = T + partial_T_av*Delta_t;
p_t = rho_t.*T_t;
%% 边界点处的流场变量;
rho_t(1) = 1; % 滞止参数;
V_t(1) = 2*V_t(2) - V_t(3); % 允许边界上的 V 是可变化的;
T_t(1) = 1; % 滞止参数;
p_t(1) = rho_t(1)*T_t(1); % 滞止参数;

rho_t(N) = 2*rho_t(N-1) - rho_t(N-2);
V_t(N) = 2*V_t(N-1) - V_t(N-2);
T_t(N) = 2*T_t(N-1) - T_t(N-2);
p_t(N) = rho_t(N)*T_t(N);
%% 马赫数
Ma = V_t./sqrt(T_t);
end

C++

OneStepNozzle.h

#pragma once
#include 
#include 
#include //setprecision(3)
using namespace std;

class Nozzle
{
     
private:
	double* rho;//密度;
	double* V;//速度;
	double* T;//温度;
	double* p;//压力;

	中间变量
	double* partial_rho;
	double* partial_V;
	double* partial_T;

	double* bar_rho;
	double* bar_V;
	double* bar_T;

	double* bar_partial_rho;
	double* bar_partial_V;
	double* bar_partial_T;

	double* partial_rho_av;
	double* partial_V_av;
	double* partial_T_av;
	

	double* Ma;//马赫数; 

	int N; //计算节点数;
	double X_first; 
	double X_end;//计算区间[X_first, X_end];
	double* A; //喷管面积;

	double Delta_x;// 空间步长;
	double Delta_t;//时间步长;
	double gama;// 绝热指数;
	double C;// 柯朗数;

	int iterations;//迭代次数;

public:
	void _init();

	Nozzle(double x_f, double x_e, double a[], double rho0[], double T0[], double V0[], int n, int it);
	
	Nozzle(const Nozzle& N);

	void oneStepmacCormack();

	void iterativeCompute();//迭代计算;

	void print();//输出最终的计算结果;

	~Nozzle();
};

OneStepNozzle.cpp

#include "OneStepNozzle.h"

void Nozzle::_init()
{
     
	this->Delta_x = (X_end - X_first) / (N - 1);// 空间步长;
	this->Delta_t = -1;//时间步长;
	this->gama = 1.4;// 绝热指数;
	this->C = 0.5;// 柯朗数;

	//创建向量;
	//this->rho = new double[N];
	//this->V = new double[N];
	//this->T = new double[N];

	this->partial_rho = new double[N];
	this->partial_V = new double[N];
	this->partial_T = new double[N];

	this->bar_rho = new double[N];
	this->bar_V = new double[N];
	this->bar_T = new double[N];

	this->bar_partial_rho = new double[N];
	this->bar_partial_V = new double[N];
	this->bar_partial_T = new double[N];

	this->partial_rho_av = new double[N];
	this->partial_V_av = new double[N];
	this->partial_T_av = new double[N];

	this->p = new double[N];
	this->Ma = new double[N];
}

Nozzle::Nozzle(double x_f,double x_e,double a[], double rho0[],double T0[], double V0[], int n,int it):
	X_first(x_f), X_end(x_e),A(a),rho(rho0),V(V0),T(T0), N(n), iterations(it)
{
     
	//用户需要输入:计算区间[x_f,x_e];
	//           喷管形状向量 a[];
	//           初始密度 rho0[];
	//           初始温度T0[];
	//           初始速度V0[];
	//           节点数 n;
	//           迭代次数 it;

	_init();
}

Nozzle::Nozzle(const Nozzle& _N)
{
     
	//浅拷贝;
	this->N = _N.N;
	this->X_first = _N.X_first;
	this->X_end = _N.X_end;

	this->Delta_x = _N.Delta_x;
	this->Delta_t = _N.Delta_t;

	this->gama = _N.gama;
	this->C = _N.C;

	this->iterations = _N.iterations;

	//深拷贝;
	this->rho = new double(*_N.rho);
	this->V = new double(*_N.V);
	this->T = new double(*_N.T);

	this->partial_rho = new double(*_N.partial_rho);
	this->partial_V = new double(*_N.partial_V);
	this->partial_T = new double(*_N.partial_T);

	this->bar_rho = new double(*_N.bar_rho);
	this->bar_V = new double(*_N.bar_V);
	this->bar_T = new double(*_N.bar_T);

	this->bar_partial_rho = new double(*_N.bar_partial_rho);
	this->bar_partial_V = new double(*_N.bar_partial_V);
	this->bar_partial_T = new double(*_N.bar_partial_T);

	this->partial_rho_av = new double(*_N.partial_rho_av);
	this->partial_V_av = new double(*_N.partial_V_av);
	this->partial_T_av = new double(*_N.partial_T_av);

	this->p = new double(*_N.p);
	this->Ma = new double(*_N.Ma);

	this->A = new double(*_N.A);
}

void Nozzle::oneStepmacCormack()//使用Mac Cormack法计算;
{
     
	 预估步计算;
	// 向前差分;
	// 只计算内点;
	for (int i = 0; i <= N - 2; i++)
	{
     
		partial_rho[i] = -V[i] * (rho[i + 1] - rho[i]) / Delta_x
					   - rho[i] * (V[i + 1] - V[i]) / Delta_x
			           - rho[i] * V[i] * (log(A[i + 1]) - log(A[i])) / Delta_x;

		partial_V[i] = -V[i] * (V[i + 1] - V[i]) / Delta_x
			         - 1 / gama * ((T[i + 1] - T[i]) / Delta_x
				     + T[i] / rho[i] * (rho[i + 1] - rho[i]) / Delta_x);

		partial_T[i] = -V[i] * (T[i + 1] - T[i]) / Delta_x
			- (gama - 1) * T[i] * ((V[i + 1] - V[i]) / Delta_x
				+ V[i] * (log(A[i + 1]) - log(A[i])) / Delta_x);
	}
	partial_rho[N-1] = 0;
	partial_V[N-1] = 0;
	partial_T[N-1] = 0;

	计算最小时间步长;

	Delta_t = C * Delta_x / (V[0] + sqrt(T[0]));//将首节点计算的时间步长赋值给 Delta_t;

	for (int i = 1;i < N;i++)//从第个二节点计算到最后一个节点;
	{
     
		if (C * Delta_x / (V[i] + sqrt(T[i])) <= this->Delta_t)
		{
     
			this->Delta_t = C * Delta_x / (V[i] + sqrt(T[i]));
		}
	}

	// 计算预估值
	for (int i = 0;i < N;i++)
	{
     
		bar_rho[i] = rho[i] + partial_rho[i] * Delta_t;
		bar_V[i] = V[i] + partial_V[i] * Delta_t;
		bar_T[i] = T[i] + partial_T[i] * Delta_t;
	}

	 计算校正步;
	// 向后差分;
	// 只计算内点;
	for (int i = 1;i <= N - 1;i++)
	{
     
		bar_partial_rho[i] = -bar_V[i] * (bar_rho[i] - bar_rho[i - 1]) / Delta_x
			- bar_rho[i] * (bar_V[i] - bar_V[i - 1]) / Delta_x
			- bar_rho[i] * bar_V[i] * (log(A[i]) - log(A[i - 1])) / Delta_x;

		bar_partial_V[i] = -bar_V[i] * (bar_V[i] - bar_V[i - 1]) / Delta_x
			- ((bar_T[i] - bar_T[i - 1]) / Delta_x
				+ bar_T[i] * (bar_rho[i] - bar_rho[i - 1]) / (bar_rho[i] * Delta_x)) / gama;

		bar_partial_T[i] = -bar_V[i] * (bar_T[i] - bar_T[i - 1]) / Delta_x
			- (gama - 1) * bar_T[i] * ((bar_V[i] - bar_V[i - 1]) / Delta_x
				+ bar_V[i] * (log(A[i]) - log(A[i - 1])) / Delta_x);
	}
	bar_partial_rho[0] = 0;
	bar_partial_T[0] = 0;
	bar_partial_V[0] = 0;

	 计算平均时间导数;
	for (int i = 0;i < N;i++)
	{
     
		partial_rho_av[i] = (partial_rho[i] + bar_partial_rho[i]) / 2;
		partial_V_av[i] = (partial_V[i] + bar_partial_V[i]) / 2;
		partial_T_av[i] = (partial_T[i] + bar_partial_T[i]) / 2;
	}
	
	 校正值;
	// t + Delta_t 时的参数;

	for (int i = 0;i < N;i++)
	{
     
		rho[i] = rho[i] + partial_rho_av[i] * Delta_t;
		V[i] = V[i] + partial_V_av[i] * Delta_t;
		T[i] = T[i] + partial_T_av[i] * Delta_t;
		p[i] = rho[i] * T[i];
	}

	 边界点处的流场变量;
	rho[0] = 1;// 滞止参数;
	V[0] = 2 * V[1] - V[2];// 允许边界上的 V 是可变化的;
	T[0] = 1;// 滞止参数;
	p[0] = rho[0] * T[0];// 滞止参数;

	rho[N - 1] = 2 * rho[N - 2] - rho[N - 3];
	V[N - 1] = 2 * V[N - 2] - V[N - 3];
	T[N - 1] = 2 * T[N - 2] - T[N - 3];
	p[N - 1] = rho[N - 1] * T[N - 1];

	 马赫数
	for (int i = 0;i < N;i++)
	{
     
		Ma[i] = V[i] / sqrt(T[i]);
	}
}

void Nozzle::iterativeCompute()//迭代计算;
{
     
	for (int it = 0;it < this->iterations;it++)
	{
     
		oneStepmacCormack();
	}
}

void Nozzle::print()
{
     	
	iterativeCompute();

	cout << "节点" << "\t" << "密度" << "\t" << "速度" << "\t" 
		 << "温度" << "\t" << "压力" << "\t" << "马赫数" << endl;

	for (int i = 0;i < N;i++)
	{
     
		cout << i + 1 << "\t" 
			<< setprecision(4) << rho[i] << "\t" 
			<< setprecision(4) << V[i] << "\t"
			<< setprecision(4) << T[i] << "\t" 
			<< setprecision(4) << p[i] << "\t" 
			<< setprecision(4) << Ma[i] << endl;
	}
}

Nozzle::~Nozzle()
{
     
	//delete[] rho;
	//delete[] V;
	//delete[] T;

	delete[] partial_rho;
	delete[] partial_V;
	delete[] partial_T;

	delete[] bar_rho;
	delete[] bar_V;
	delete[] bar_T;

	delete[] bar_partial_rho;
	delete[] bar_partial_V;
	delete[] bar_partial_T;

	delete[] partial_rho_av;
	delete[] partial_V_av;
	delete[] partial_T_av;

	delete[] p;
	delete[] Ma;
}

拟一维喷管流动的数值解.cpp

/*
 Author: CXT
 Date : 2021 / 4 / 15
 Theme : 拟一维亚声速 - 超声速等熵喷管
 %物理符号(无量纲量);
 x —— 空间位置;
 A —— 喷管面积;
 rho —— 密度;
 V —— 速度;
 p —— 压力;
 N —— 计算节点数;
 */

#include 
#include //sqrt();
#include "OneStepNozzle.h"
using namespace std;

void test()
{
     
	double x_f = 0;//计算区间[x_f,x_e];
	double x_e = 3;
	int n = 31;//节点数;
	int it = 1401;//迭代次数;

	double* x;
	double* a;
	double* rho0;
	double* T0;
	double* V0;

	x = new double[n];
	a = new double[n];
	rho0 = new double[n];
	T0 = new double[n];
	V0 = new double[n];

	//准备节点;
	for (int i = 0; i<n; i++)
	{
     
		x[i] = x_f + (x_e - x_f) / (n - 1) * i;

		a[i] = 1 + 2.2 * pow((x[i] - 1.5),2);
		rho0[i] = (1 - 0.3146 * x[i]) / 1;
		T0[i] = (1 - 0.2314 * x[i]) / 1;
		V0[i] = (0.1 + 1.09 * x[i])*sqrt(T0[i]);
	}

	Nozzle N1(x_f,x_e,a,rho0,T0,V0,n,it);
	N1.print();

	delete[] x;
	delete[] a;
	delete[] rho0;
	delete[] T0;
	delete[] V0;
}

int main()
{
     
	test();

	system("pause");
	return 0;
}

你可能感兴趣的:(计算流体力学,CFD,安德森,算法)