读取变量 segmentation fault
-
@李东岳 可是老师比较奇怪的是,我把turbulence调整顺序,放在前面之后,他是可以读取的,在最后的g又会出现同样的错误。下面是我调整顺序后的createFields.H文件。根据调整变量出现的顺序就导致了segmentation fault位置的不同,我猜测是不是和出现错误本身的变量并没有关系,而是里面的某个数读取错误,导致栈储存满了。但是我看了好几遍,对变量的类型进行了检查,发现并没有这方面的错误~所以不是很清楚我要怎么找这个错误。老师您提到的debug我需要再学习一下怎么使用
Info<< "Reading field psi0\n" << endl; volScalarField psi0 ( IOobject ( "psi0", runTime.timeName(), mesh, IOobject::NO_READ, IOobject::AUTO_WRITE ), mesh, dimensionedScalar("psi0",dimless, 0.0) ); Info<< psi0<< endl; Info<< "Reading field psi\n" << endl; volScalarField psi ( IOobject ( "psi", runTime.timeName(), mesh, IOobject::NO_READ, IOobject::AUTO_WRITE ), mesh, dimensionedScalar("delta",dimless, 0.0) ); Info<< psi<< endl; Info<< "Reading field delta\n" << endl; volScalarField delta ( IOobject ( "delta", runTime.timeName(), mesh, IOobject::NO_READ, IOobject::AUTO_WRITE ), mesh, dimensionedScalar("delta",dimless, 0.0) ); Info<< delta << endl; Info<< "Reading field H\n" << endl; volScalarField H ( IOobject ( "H", runTime.timeName(), mesh, IOobject::NO_READ, IOobject::AUTO_WRITE ), mesh, dimensionedScalar("H",dimless, 0.0) ); Info<< H << endl; volScalarField C ( IOobject ( "C", runTime.timeName(), mesh, IOobject::NO_READ, IOobject::AUTO_WRITE ), mesh, dimensionedScalar("C",dimless/dimLength, 0.0) ); Info<< C << endl; /* Info<< "Reading field p\n" << endl; volScalarField p ( IOobject ( "p", runTime.timeName(), mesh, IOobject::MUST_READ, IOobject::AUTO_WRITE ), mesh );*/ Info<< "Reading field p_rgh\n" << endl; volScalarField p_rgh ( IOobject ( "p_rgh", runTime.timeName(), mesh, IOobject::MUST_READ, IOobject::AUTO_WRITE ), mesh ); Info<< p_rgh << endl; Info<< "Reading wetting fluid velocity field Uwetting\n" << endl; volVectorField Uwetting ( IOobject ( "Uwetting", runTime.timeName(), mesh, IOobject::MUST_READ, IOobject::AUTO_WRITE ), mesh ); Info<< Uwetting << endl; Info<< "Reading wetting fluid velocity field UnonWetting\n" << endl; volVectorField UnonWetting ( IOobject ( "UnonWetting", runTime.timeName(), mesh, IOobject::MUST_READ, IOobject::AUTO_WRITE ), mesh ); Info<< UnonWetting << endl; Info<< "Reading field U\n" << endl; volVectorField U ( IOobject ( "U", runTime.timeName(), mesh, IOobject::READ_IF_PRESENT, IOobject::AUTO_WRITE ), UnonWetting + Uwetting ); Info<< U << endl; Info<< "Reading fluid fraction field eps\n" << endl; volScalarField eps ( IOobject ( "eps", runTime.timeName(), mesh, IOobject::MUST_READ, IOobject::AUTO_WRITE ), mesh ); Info<< eps << endl; Info<< "Reading solid indicator\n" << endl; volScalarField Solid ( IOobject ( "Solid", runTime.timeName(), mesh, IOobject::NO_READ, IOobject::AUTO_WRITE ), mesh, dimensionedScalar("solid", dimensionSet(0,0,0,0,0,0,0), 0) ); Info<< "Reading field Tsol (Solid Phase Temperature)\n" << endl; volScalarField Tsol ( IOobject ( "Tsol", runTime.timeName(), mesh, IOobject::MUST_READ, IOobject::AUTO_WRITE ), mesh ); Info<< Tsol << endl; IOdictionary transportProperties ( IOobject ( "transportProperties", runTime.constant(), mesh, IOobject::MUST_READ_IF_MODIFIED, IOobject::NO_WRITE ) ); Info<< "Reading solid density\n" << endl; dimensionedScalar rhoSolid ( "rhoSolid", dimMass/dimLength/dimLength/dimLength, transportProperties ); Info<< rhoSolid << endl; Info<< "Reading solid specific heat capacity\n" << endl; dimensionedScalar CpS ( "CpS", dimAcceleration*dimLength/dimTemperature, transportProperties ); Info<< "Reading solid heat diffusion\n" << endl; dimensionedScalar kappaEffSolid ( "kappaEffSolid", dimForce/dimTime/dimTemperature, transportProperties ); Info<< "Reading interfacial heat transfer coefficient hsf\n" << endl; dimensionedScalar hsf ( "hsf", dimEnergy/dimMass, transportProperties ); #include "createPhi.H" // Creating e based thermo Info<< "Creating e based thermo\n" << endl; autoPtr<twoPhaseMixtureEThermo> thermo ( new twoPhaseMixtureEThermo(U, phi) ); Info<< "1" << endl; Info<< "Construct incompressible turbulence model\n" << endl; autoPtr<incompressible::turbulenceModel> turbulence ( incompressible::turbulenceModel::New(U, phi, thermo()) ); // Create mixture and Info<< "Creating temperaturePhaseChangeTwoPhaseMixture\n" << endl; autoPtr<temperaturePhaseChangeTwoPhaseMixture> mixture = temperaturePhaseChangeTwoPhaseMixture::New(thermo(), mesh); //Info<< "Reading transportProperties\n" << endl; //immiscibleIncompressibleTwoPhaseMixture mm(U, phi); volScalarField& alpha1(thermo->alpha1()); volScalarField& alpha2(thermo->alpha2()); const dimensionedScalar& rho1 = thermo->rho1(); const dimensionedScalar& rho2 = thermo->rho2(); Info<< "Reading nu\n" << endl; dimensionedScalar nu1 ( "nu1", dimViscosity, transportProperties.subDict("liquid").lookup("nu") ); dimensionedScalar nu2 ( "nu2", dimViscosity, transportProperties.subDict("vapour").lookup("nu") ); Info<< "Reading mu\n" << endl; dimensionedScalar mu1("mu1",nu1*rho1); dimensionedScalar mu2("mu2",nu2*rho2); Info<< mu1 << endl; Info<< "Need to store rho for ddt(rho, U)\n" << endl; volScalarField rho ( IOobject ( "rho", runTime.timeName(), mesh, IOobject::READ_IF_PRESENT, IOobject::AUTO_WRITE ), alpha1*rho1 + alpha2*rho2 ); //rho.oldTime(); Info<< "Mass flux\n" << endl; surfaceScalarField rhoPhi ( IOobject ( "rhoPhi", runTime.timeName(), mesh, IOobject::NO_READ, IOobject::NO_WRITE ), fvc::interpolate(rho)*phi ); Info<< "Mass flux by eps\n" << endl; surfaceScalarField rhoPhiByEps ( IOobject ( "rhoPhiByEps", runTime.timeName(), mesh, IOobject::READ_IF_PRESENT, IOobject::NO_WRITE ), rhoPhi*linearInterpolate(1/(eps+SMALL)) ); Info<< "Construct interface from alpha1 distribution\n" << endl; interfaceProperties interface ( alpha1, U, thermo->transportPropertiesDict() ); dimensionedScalar deltaX("deltaX", dimless, thermo->transportPropertiesDict()); dimensionedScalar gamma("gamma", dimless, deltaX.value()*0.75); dimensionedScalar epsilon("epsilon", dimless, deltaX.value()*3.5); dimensionedScalar deltaTau("deltaTau", dimless, deltaX.value()*0.1); dimensionedScalar dimChange ( dimensionedScalar("dimChange", dimLength, 1.0) ); dimensionedScalar sigma ( "sigma", dimMass/dimTime/dimTime, thermo->transportPropertiesDict() ); Info<< "MULES compressed flux is registered in case scalarTransport FO needs it\n" << endl; surfaceScalarField alphaPhi ( IOobject ( "alphaPhi", runTime.timeName(), mesh, IOobject::NO_READ, IOobject::NO_WRITE ), phi*fvc::interpolate(alpha1) ); dimensionedScalar Prt("Prt", dimless, thermo->transportPropertiesDict()); Info<< "kappaEff\n" << endl; volScalarField kappaEff ( IOobject ( "kappaEff", runTime.timeName(), mesh, IOobject::NO_READ, IOobject::NO_WRITE ), thermo->kappa() ); Info<< "Need to store rho for ddt(rhoCp, U)\n" << endl; volScalarField rhoCp ( IOobject ( "rhoCp", runTime.timeName(), mesh, IOobject::NO_READ, IOobject::NO_WRITE ), rho*thermo->Cp() ); //rhoCp.oldTime(); Info<< "1" << endl; #include "createMRF.H" #include "createFvOptions.H" Info<< "1" << endl; #include "readGravitationalAcceleration.H" Info<< "1" << endl; #include "readhRef.H" Info<< "1" << endl; #include "gh.H" Info<< "1" << endl; Info<< "Turbulent Prandtl number\n" << endl; volScalarField& p = thermo->p(); label pRefCell = 0; scalar pRefValue = 0.0; setRefCell ( p, p_rgh, pimple.dict(), pRefCell, pRefValue ); if (p_rgh.needReference()) { p += dimensionedScalar ( "p", p.dimensions(), pRefValue - getRefCellValue(p, pRefCell) ); p_rgh = p - rho*gh; } mesh.setFluxRequired(p_rgh.name()); mesh.setFluxRequired(alpha1.name()); #include "createPorousMediaFields.H"
错误显示:
Selecting incompressible transport model Newtonian Selecting incompressible transport model Newtonian 1 Construct incompressible turbulence model Selecting turbulence model type laminar Selecting laminar stress model Stokes Creating temperaturePhaseChangeTwoPhaseMixture Selecting phaseChange model Lee Reading nu Reading mu mu1 [1 -1 -1 0 0 0 0] 0.0009584 Need to store rho for ddt(rho, U) Mass flux Mass flux by eps Construct interface from alpha1 distribution MULES compressed flux is registered in case scalarTransport FO needs it kappaEff Need to store rho for ddt(rhoCp, U) 1 Segmentation fault (core dumped)
可以看到turbulence可以正确的选择laminar,我看segmentation faul多是和指针错误有关,下面的mu可以正确的计算,是不是可以说这里面的thermo和mixture指针是不存在问题的。
-
case
test.zip -
曾经,我排过一个很狗血的错,一台电脑的某个软件也是这样捉摸不定的出错,不知道在哪里,也没有一点点防备。找了半天发现是磁盘满了。还剩一丢丢空间,所以一开软件,这个空间什么时候被临时文件占满了,就崩了。错误特征完全无法捉摸。
这个,可以
du -h
看看各挂载点的空间。然后,开两个窗口。一个开top
,监视内存;一个开求解器,看内存的实时占用,不过有捕捉不到的风险。可以把网格尽量缩小再试试。这些变量占的空间基本都依托于网格。还有一个点,这两个错误都跟 thermo 有关。thermo好像也和湍流有关
https://www.openfoam.com/documentation/guides/latest/api/classFoam_1_1ThermalDiffusivity.html -
@bestucan 感谢老师的回复。我先一个一个排除一下可能的错误。
找了半天发现是磁盘满了。还剩一丢丢空间,所以一开软件,这个空间什么时候被临时文件占满了,就崩了。错误特征完全无法捉摸。
这个,可以du -h
看看各挂载点的空间。然后,开两个窗口。一个开top
,监视内存;一个开求解器,看内存的实时占用,不过有捕捉不到的风险。可以把网格尽量缩小再试试。这些变量占的空间基本都依托于网格。我查看了一下,看到还有250+GB的空间,而且我的网格数从100✖100✖200减少到5✖5✖10都存在一样的错误,或许可以初步排查是内存的问题。
还有一个点,这两个错误都跟 thermo 有关。thermo好像也和湍流有关
我想请问一下老师,是怎么发现这两个错误和thermo有关系的,我不是很明白。
因为我下面有一个是输出mu的值,而所需要的ρ是从thermo里读取到的,mu输出的值正确也是不能保障thermo指针正确吗。
turbulence引用的时候需要thermo,如果我把turbulence的定义放在thermo后一个,就可以正确读取turbulence。放到后面几个就读取错误了。我先去看一下老师说的监视内存的方法,看具体是什么情况。感谢老师。
-
@hongjiewang 第二个错误信息我看错了,确实不是都和thermo有关。
可是你的第二个错误你说 g 出的错,Info<< "Need to store rho for ddt(rhoCp, U)\n" << endl;
这一句后面只跟一个 “1”,在代码里不是这里么
Info<< "1" << endl;
#include "createMRF.H"
#include "createFvOptions.H"Info<< "1" << endl;
#include "readGravitationalAcceleration.H"createMRF.H 或者 creat FvOptions.H
输出第二个“1”才到g
250的网格都错,和内存无关了。这种面向对象的程序一般写的时候都有脚手架,就像官网的代码有很多框图,但又比那些框图要详细。这留个窟窿那钻个眼都有用,可是只看代码非常费功夫才知道有什么用。不知道有什么用就很容易错。互相之间的依赖关系,先后顺序。那些不报错的是没语法错误了,不一定没语义错误。所以一般都在原有的类似的求解器上增删改;改变结构造个新的,还是挺麻烦的。of的路慢慢熬吧。
如果还不行,用gdb调试,有gdb的of版,gdbof
-
@bestucan 在 读取变量 segmentation fault 中说:
Info<< "1" << endl;
#include "createMRF.H"
#include "createFvOptions.H"Info<< "1" << endl;
#include "readGravitationalAcceleration.H"输出第二个“1”才到g
这里我看串行了,确实不是到g变量。我还是尝试一下gdb调试吧~有结果的话和各位老师分享~
感谢老师~ -
@bestucan 请问我重新编译of的时候,提示错误而终止了,这是什么原因奥~
step1:修改etc/bashrc中的OPTION为Debug
step2:在对应版本的openfoam终端下 source etc/bashrc
step2:.Allwmake -jstate/lumpedPointState.C: In constructor ‘Foam::lumpedPointState::lumpedPointState(const pointField&, const vectorField&, Foam::quaternion::eulerOrder, bool)’: state/lumpedPointState.C:154:49: error: expected ‘;’ before ‘exit’ 154 | << angles_.size() << " angles" << nl | ^ | ; 155 | exit(FatalError);
是在154行nl后添加;就可以了吗
-
@hongjiewang 我也编译过 debug 模式的 of ,就是比正常慢点,没出现过这种错误。这种错误看起来很初级不像是源代码带的。我搜我的代码的这个文件也没这句。
单这个错误,是的,加个分号就行。就是一个语句忘了结尾。
-