CFD Online Discussion Forums

CFD Online Discussion Forums (
-   OpenFOAM Programming & Development (
-   -   ERROR: attempt to access element from zero sized list (

suhas jain June 26, 2013 08:03

ERROR: attempt to access element from zero sized list
Hi everyone,

// loop over faces
forAll(faces, facei)
label faceI = faces[facei];

//find patch
label patchI = psi.mesh().boundaryMesh().whichPatch(faceI);

// if facei is in patch: patchI < -1
if (patchI > -1)
if (!isA<emptyFvPatch>(psi.boundaryField()[patchI]))

faceI = psi.mesh().boundaryMesh()[patchI].whichFace(faceI);
psiBoundary = psi.boundaryField()[patchI][faceI];

Info << " Face: " << faceI << " Psi value: " << psiBoundary;


I am using the above code to access the value of psi (a distance function) at the boundaries. But during the execution of the "psiBoundary"( as seen above) I am getting this error


attempt to access element from zero sized list

From function UList<T>::checkIndex(const label)
in file /opt/openfoam220/src/OpenFOAM/lnInclude/UListI.H at line 103.

FOAM aborting

#0 Foam::error::printStack(Foam::Ostream&) in "/opt/openfoam220/platforms/linux64GccDPOpt/lib/"
#1 Foam::error::abort() in "/opt/openfoam220/platforms/linux64GccDPOpt/lib/"
at /opt/openfoam220/src/OpenFOAM/lnInclude/errorManip.H:85
at /opt/openfoam220/src/OpenFOAM/lnInclude/UListI.H:103
at /opt/openfoam220/src/OpenFOAM/lnInclude/UListI.H:168
at ~/OpenFOAM/suhas-2.2.0/applications/solvers/levelSetFoam/reinit.H:169
#6 __libc_start_main in "/lib/x86_64-linux-gnu/"


I just know this is something related to the index " i " in the UListI.H but I couldn't figure out what exactly is going on there.:confused:

Any advice?

Thanks in advance,

marupio June 26, 2013 10:31

First, I get that error when the list / field has been declared, but has not yet been allocated, and I try accessing it. It usually means it wasn't built properly. Maybe psi isn't built properly.

Second, are you mixing up facei and faceI? (This is bad practice to differentiate variables by case alone, but OpenFOAM is guilty of this too.)

Just some thoughts.

suhas jain June 27, 2013 07:46

Thank you for the reply,

I checked everything but I dint see any mistake in psi field. According to the error, it is accessing value from zero sized array but when I printed out the value of "psi.boundaryField().size()", I got the value as 6, and I even tried printing out "psi.boundaryField()", It gave the correct boundary conditions as defined by me. That means it's not a zero sized array. But I still get the same error at Cell=0 ,patchI=0 and faceI=0 when I use "psi.boundaryField()[patchI][faceI] "

suhas jain June 27, 2013 08:18

I think it might be accessing an empty patch. How to give a condition so that it doesn't access an empty patch?


marupio June 27, 2013 11:36

It's true that empty patch fields do not initialize the boundary fields, so that would explain the error you are getting.

psi.boundaryField().size() would tell you how many boundary patches you have... that's not where the error would lie. If you try psi.boundaryfield()[0].size(), or [1], [2], etc. what do you get?

Also, why are you finding which patch the face is in? It seems unnecessary. You have psi.boundaryField(), so why not restructure it just to loop over the structure of psi.boundaryField(), rather than all the faces? Such as:


forAll(psi.boundaryField(), patchI)
        forAll(psi.boundaryField()[patchI], localFaceI)
                psiBoundary = psi.boundaryField()[patchI][localFaceI];
                Info << " Local face: " << localFaceI;
                Info << " Psi value: " << psiBoundary << endl;

The second forAll should prevent access of any uninitialized fields.

All times are GMT -4. The time now is 22:29.