CFD Online Discussion Forums

CFD Online Discussion Forums (http://www.cfd-online.com/Forums/)
-   OpenFOAM Programming & Development (http://www.cfd-online.com/Forums/openfoam-programming-development/)
-   -   Conditional statement over the entire mesh (http://www.cfd-online.com/Forums/openfoam-programming-development/82316-conditional-statement-over-entire-mesh.html)

xisto November 23, 2010 07:16

Conditional statement over the entire mesh
 
Dear All,

I want to do an if conditional statement for two variables witch are surfaceScalarFields (MR_N and ML_N).

The code compile without erros, on the other and this will result in a infinite loop.


Code:


forAll(mesh.faces(),faceI) 


  {

 if(mag(MR_N[faceI]) >= scalar(1.0))
    {
     
        surfaceScalarField            M4_neg = M1_neg;
        surfaceScalarField            P5_neg = 1.0/(2.0*MR_N)*(MR_N-mag(MR_N));

   

  if(mag(ML_N[faceI]) >= scalar(1.0)) /

      {
        surfaceScalarField    M4_pos = M1_pos;
        surfaceScalarField    P5_pos = 1.0/(2.0*ML_N)*(ML_N+mag(ML_N));
      }
     
  }

Any suggestions?

Cheers

Carlos

schmittp54 November 23, 2010 09:33

I didn't run the example, but I don't see why it should be an infinite loop. Is it just your impression? Did you debug the loop, printing faceI and mesh.faces().size()?

The first if-clause is missing a "}" which can result in weird effects. Besides that, it should give a compiler error.

The real problem seems to be that you create up to four new surfaceScalarFields per iteration. Depending on the implementation and your mesh size this can be extremely slow. You didn't mention the purpose of this code. But M4_neg...M5_pos are only visible in their surrounding if-clauses and go out of scope again.

Maybe what you want is:
  • Create M4_neg...P5_pos outside of the for-loop
  • Inside the if-clauses set M4_neg[faceI] = M1_neg[faceI]; and respectively the other fields
Patricia

xisto November 23, 2010 09:43

Dear Patricia,

Thanks for your reply

The missing a "}" is just a typo.

I didn't run a debug, I'm not an expert in C++ and I'm just trying to understand if mine implementation is correct.

I will follow your suggestion.

Carlos

xisto November 23, 2010 10:14

Dear Patricia,

Your solution for the problem works.

Thanks

Carlos

xisto November 24, 2010 10:11

Dear Patricia,

I follow your solution and my first impression was that it works.

But now Its giving me the following error:

#0 Foam::error::printStack(Foam::Ostream&) in "/opt/openfoam171/lib/linux64GccDPOpt/libOpenFOAM.so"
#1 Foam::sigSegv::sigSegvHandler(int) in "/opt/openfoam171/lib/linux64GccDPOpt/libOpenFOAM.so"
#2 in "/lib/libc.so.6"
#3
in "/opt/openfoam171/applications/bin/linux64GccDPOpt/ausmFoam"
#4 __libc_start_main in "/lib/libc.so.6"
#5
in "/opt/openfoam171/applications/bin/linux64GccDPOpt/ausmFoam"
Segmentation fault


Which normally is caused by an application accessing memory outside the allocated space.

Any suggestion?

The code:

Code:

        surfaceScalarField    M1_pos = (ML_N+mag(ML_N))/2.0; //Funcao do lado esquerdo
        surfaceScalarField    M1_neg = (MR_N-mag(MR_N))/2.0; //Funcao do lado direito

        surfaceScalarField    M4_neg = -1.0/4.0*magSqr(MR_N-1.0)-1.0/8.0*magSqr(magSqr(MR_N)-1.0);
        surfaceScalarField    P5_neg =  1.0/4.0*magSqr(MR_N-1.0)*(2.0+MR_N)-3.0/16.0*MR_N*magSqr(magSqr(MR_N)-1);

        surfaceScalarField    M4_pos = 1.0/4.0*magSqr(ML_N+1.0)+1.0/8.0*magSqr(magSqr(ML_N)-1.0); 
        surfaceScalarField    P5_pos = 1.0/4.0*magSqr(ML_N+1.0)*(2.0-ML_N)+3.0/16.0*ML_N*magSqr(magSqr(ML_N)-1);




forAll(mesh.faces(),faceI) 

  {

 if(mag(MR_N[faceI]) >= scalar(1.0)) //valores do lado direito

    {
     
            M4_neg[faceI] = M1_neg[faceI]; //Funcao do lado direito
            P5_neg[faceI] = M1_neg[faceI]/MR_N[faceI];
    }

   

  if(mag(ML_N[faceI]) >= scalar(1.0)) //valores do lado esquerdo

      {
        M4_pos[faceI] = M1_pos[faceI]; //Funcao do lado esquerdo
        P5_pos[faceI] = M1_pos[faceI]/ML_N[faceI]; 
      }


     
  }

Carlos

xisto November 24, 2010 10:13

I forgot to say that the error occurs in the following line:


P5_pos[faceI] = M1_pos[faceI]/ML_N[faceI];

schmittp54 November 24, 2010 13:13

Can't help. You'd have to attach a complete example.

Use a debugger to find the value of faceI where the segmentation fault happens. Check that faceI<P5_pos.size() and faceI<M1_pos.size() and faceI<ML_N.size(). I guess that these conditions are violated.
The problem may be different addressing of either only internal or all faces in the fvMesh and surfaceScalarField classes.

Patricia


All times are GMT -4. The time now is 10:00.