求解器adv121Foam.C
/*---------------------------------------------------------------------------*\
Changed from scalarTransportFoam to adv121Foam
Application
adv121Foam
Description
Solves training examples 1.2.1 problem.
\*---------------------------------------------------------------------------*/
#include "fvCFD.H"
#include "fvOptions.H"
#include "simpleControl.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
int main(int argc, char *argv[])
{
#include "setRootCaseLists.H"
#include "createTime.H"
#include "createMesh.H"
simpleControl simple(mesh);
#include "createFields.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
Info<< "\nCalculating scalar transport\n" << endl;
#include "CourantNo.H"
while (simple.loop(runTime))
{
Info<< "Time = " << runTime.timeName() << nl << endl;
while (simple.correctNonOrthogonal())
{
fvScalarMatrix TEqn
(
fvm::ddt(T)
+ fvm::div(phi, T)
==
fvOptions(T)
);
TEqn.relax();
fvOptions.constrain(TEqn);
TEqn.solve();
fvOptions.correct(T);
}
runTime.write();
}
Info<< "End\n" << endl;
return 0;
}
// ************************************************************************* //
createFields.H
Info<< "Reading field T\n" << endl;
volScalarField T
(
IOobject
(
"T",
runTime.timeName(),
mesh,
IOobject::MUST_READ,
IOobject::AUTO_WRITE
),
mesh
);
Info<< "Reading field U\n" << endl;
volVectorField U
(
IOobject
(
"U",
runTime.timeName(),
mesh,
IOobject::MUST_READ,
IOobject::AUTO_WRITE
),
mesh
);
Info<< "Reading transportProperties\n" << endl;
IOdictionary transportProperties
(
IOobject
(
"transportProperties",
runTime.constant(),
mesh,
IOobject::MUST_READ_IF_MODIFIED,
IOobject::NO_WRITE
)
);
Info<< "Reading diffusivity DT\n" << endl;
dimensionedScalar DT
(
transportProperties.lookup("DT")
);
const scalar pi(M_PI);
using std::sin;
forAll(T,celli)
{
scalar xx = mesh.C()[celli].x();
scalar yy = mesh.C()[celli].y();
T[celli] = sin(2*pi*(xx+yy));
}
T.correctBoundaryConditions();
T.write();
#include "createPhi.H"
#include "createFvOptions.H"
算例blockMeshDict
/*--------------------------------*- C++ -*----------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | Website: https://openfoam.org
\\ / A nd | Version: 7
\\/ M anipulation |
\*---------------------------------------------------------------------------*/
FoamFile
{
version 2.0;
format ascii;
class dictionary;
object blockMeshDict;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
convertToMeters 1.0;
vertices
(
(0 0 0)
(1 0 0)
(1 1 0)
(0 1 0)
(0 0 0.1)
(1 0 0.1)
(1 1 0.1)
(0 1 0.1)
);
blocks
(
hex (0 1 2 3 4 5 6 7) (20 20 1) simpleGrading (1 1 1)
);
edges
(
);
boundary
(
upperWall
{
type patch;
faces
(
(2 3 7 6)
);
}
lowerWall
{
type patch;
faces
(
(1 5 4 0)
);
}
leftWall
{
type patch;
faces
(
(0 4 7 3)
);
}
rightWall
{
type patch;
faces
(
(1 2 6 5)
);
}
frontAndBack
{
type empty;
faces
(
(4 5 6 7)
(0 3 2 1)
);
}
);
mergePatchPairs
(
);
// ************************************************************************* //
controlDict
/*--------------------------------*- C++ -*----------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | Website: https://openfoam.org
\\ / A nd | Version: 7
\\/ M anipulation |
\*---------------------------------------------------------------------------*/
FoamFile
{
version 2.0;
format ascii;
class dictionary;
location "system";
object controlDict;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
application scalarTransportFoam;
startFrom startTime;
startTime 0;
stopAt endTime;
endTime 1.0;
deltaT 1e-4;
writeControl adjustableRunTime;
writeInterval 0.1;
purgeWrite 0;
writeFormat ascii;
writePrecision 6;
writeCompression off;
timeFormat general;
timePrecision 6;
runTimeModifiable true;
adjustTimeStep true; // 调整时间步长
maxCo 0.2; // 最大 Co 数
maxDeltaT 1e-4; // 最大时间步长
// ************************************************************************* //
boundary
/*--------------------------------*- C++ -*----------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | Website: https://openfoam.org
\\ / A nd | Version: 7
\\/ M anipulation |
\*---------------------------------------------------------------------------*/
FoamFile
{
version 2.0;
format ascii;
class polyBoundaryMesh;
location "constant/polyMesh";
object boundary;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
5
(
upperWall
{
type patch;
nFaces 20;
startFace 760;
}
lowerWall
{
type patch;
nFaces 20;
startFace 780;
}
leftWall
{
type patch;
nFaces 20;
startFace 800;
}
rightWall
{
type patch;
nFaces 20;
startFace 820;
}
frontAndBack
{
type empty;
inGroups List<word> 1(empty);
nFaces 800;
startFace 840;
}
)
// ************************************************************************* //
80t.geo
Point(1) = {0, 0, 0, 1e22};
Point(2) = {1.0, 0, 0, 1e22};
Point(3) = {1.0, 1.0, 0, 1e22};
Point(4) = {0, 1.0, 0, 1e22};
//+
Line(1) = {1, 2};
Line(2) = {2, 3};
Line(3) = {3, 4};
Line(4) = {4, 1};
//+
Line Loop(1) = {1, 2, 3, 4};
Plane Surface(1) = {1};
Transfinite Line {3,1} = 81 Using Progression 1;
Transfinite Line {2,4} = 81 Using Progression 1;
Transfinite Surface {1};
// Recombine Surface {1};
Extrude {0, 0, 0.1} {
Surface{1}; Layers{1}; Recombine;
}
Physical Surface("frontAndBack") = {26, 1};
Physical Surface("leftWall") = {25};
Physical Surface("rightWall") = {17};
Physical Surface("upperWall") = {21};
Physical Surface("lowerWall") = {13};
Physical Volume("box") = {1};
matlab代码
clearvars; clc; % adv121data80t.m % edit
datapath20t = 'D:\SJTU_senior_1st_total\Sundry\grad\hw\hw1\data\1.2.1\80t\'; % edit
Ex20tName = 'T.0.xlsx'; Nu20tName = 'T.n.xlsx';
Ex20tName = [datapath20t, Ex20tName];
Nu20tName = [datapath20t, Nu20tName];
matEx20t = importdata(Ex20tName);
matEx20t = matEx20t.data;
matEx20t = matEx20t(3:size(matEx20t,1),1);
matNu20t = importdata(Nu20tName);
matNu20t = matNu20t.data;
matNu20t = matNu20t(3:size(matNu20t,1),1);
errNu20t = matNu20t - matEx20t;
errNu20t = abs(errNu20t);
Omega = 1 / size(matEx20t,1);
sumNh = Omega * sum(errNu20t);
sumEx = Omega * sum(abs(matEx20t));
L1err80t = sumNh / sumEx; % edit
X = ['L1err80tri:',num2str(L1err80t)]; % edit
targetName = 'D:\SJTU_senior_1st_total\Sundry\grad\hw\hw1\data\1.2.1\mat\80t.mat'; % edit
Delta80t = size(matEx20t,1); % edit
save(targetName,'L1err80t','Delta80t'); % edit
disp(X);
绘图代码
clearvars; clc; % adv121Van80t.m % edit
dataPath = 'D:\SJTU_senior_1st_total\Sundry\grad\hw\hw1\data\1.2.1\20-80tria\';
targetPath = 'D:\SJTU_senior_1st_total\Sundry\grad\hw\hw1\figs\1.2.1\';
targetName = 'Van80t.png'; % edit
targetName = [targetPath, targetName];
tria100nameY025 = '80triaX050nu.xlsx'; % edit
tria100nameY025 = [dataPath, tria100nameY025];
tria100matY025 = importdata(tria100nameY025);
tria100exNameY025 = '80triaX050ex.xlsx'; % edit
tria100exNameY025 = [dataPath, tria100exNameY025];
tria100matexY025 = importdata(tria100exNameY025);
figure(1);
y_llim = -1.2; y_rlim = 1.2;
set(gca, 'ylim', [y_llim, y_rlim]); hold on;
x_to_plot = tria100matY025(:,1); y_to_plot = tria100matY025(:,2);
size = 20;
scatter(x_to_plot, y_to_plot, size, 'Black', 'c', 'filled');
hold on;
x_to_plot = tria100matexY025(:,1); y_to_plot = tria100matexY025(:,2);
plot(x_to_plot, y_to_plot, 'Black', 'LineWidth', 1);
title('1.2.1 80 Tria. VanLeer X = 0', 'FontSize', 15); % edit
legend('Numerical','Analytic','Location','NW', 'FontSize', 15);
saveas(gcf, targetName); close all;
1.2.1.vanLeer | 精度是1阶和2阶的混合 | ||||||
quads | L1Err | order | triangles | L1Err | order | ||
20*20 | 0.15008164 | 1.430943443 | 20*20*2 | 0.0411813 | 1.83771948 | ||
40*40 | 0.05566352 | 1.650732443 | 40*40*2 | 0.01152103 | 1.183593618 | ||
80*80 | 0.01522256 | 1.870521444 | 80*80*2 | 0.00798189 | 0.529467755 |