CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   OpenFOAM Programming & Development (https://www.cfd-online.com/Forums/openfoam-programming-development/)
-   -   Creating labelList from cells with certain attribute (https://www.cfd-online.com/Forums/openfoam-programming-development/150567-creating-labellist-cells-certain-attribute.html)

juhuettn March 25, 2015 08:47

Creating labelList from cells with certain attribute
 
Hello Foamers,

I am stuck in the moment with following problem:
I want to use the setValues command to manipulate my Matrix just before solving. I not only want to set the values of a boundary, I want to set the values of internal cells. To be more precise, I want to set the values of internal cells which have a certain attribute. The attribute is a blending function value of each cell.

My basic idea of solving this problem is to create a volScalarField with all the cells forfilling the attribute. The size of this field needs to be exactly the amount of the chosen cells, so I do not override too many values in the matrix. I would then create a labelList from that field and use these two as arguments for the setValue funtion.

I do not know how to create the field with exactly the amount of cells which have the attribute. I thought of using a forAll loop, but for this I have to initiate a field which has already a given size.

I am very helpful for any help! Thanks, Julian

jhoepken March 26, 2015 03:10

Maybe it's just me, but I don't really get what you ask for. You want to set values in a volScalarField as well as on the boundary? Could you explain this in brief?

juhuettn March 27, 2015 02:36

Hello Jens,

Sorry, I thought it would maybe be too confusing :-).

I want to implement a two-layer turbulence model, so I need to use the transport equation for k and epsilon in the outer boundary layer, but closer to the wall I need to use algebraic equations to solve the turbulence parameters.

The problem is, that I need to include the algebraic into the matrix before solving it. So I need to set up the matrix with the standard transport equations and then replace some parts of the matrix with the algebraic equations. I do not need to set the values at the wall, as they are already set by the boundary conditions, but the values of the cells which forfill the attribute to use the algebraic equations. For this, I want to use the "setValue" command. To use the setValues I need to give the function a labellist and a volScalarField with the values to set for the cells.

What I am now struggeling with is to built a labellist and a volScalarField with the cells forfilling an attribute (saying some scalar of the cell is smaller than some number), for which the setValues command should be used to replace the transport equations with the algebraic equations befor solving the matrix.

I hope this was somehow more understandable :-).

juhuettn March 27, 2015 05:30

Maybe someone can help me to get there step by step. In the moment I am using following piece to start with to build my labelList:

Code:

label CellCount = 0;

forAll (epsilon_, cellI)
{
    if (checkFctn[cellI] < 0.8)
    {
        "find CellID[cellI]" //Here, I would like to write out the cellID to use the ID as value for the labellist. Is there a function to do that?
       
        cellCount++; //Amount of cells with checkFctn < 0.8 to use as size for labellist
    }

labelList (cellCount, cellIDs);

I do not know how to write out the cellIDs during the forAll loop and extract them to use them as values in the labelList.

Tanks for any kind of help,
Julian

juhuettn March 31, 2015 10:32

Hello Foamers,

I will try again to get this going by posting my process. I now managed to create a labellist with the cell IDs I am needing as argument for my setValue() function. As far as I understood, the setValue() function needs also a volScalarField with the same size as the labelList to work correctly (is that right)?

I created the labelList as follows:

Code:

   

    // Dissipation Sequation
    tmp<fvScalarMatrix> epsEqn
    (
        fvm::ddt(epsilon_)       
        + fvm::div(phi_, epsilon_)
        - fvm::laplacian(DepsilonEff(), epsilon_)
    ==
        C1_*G*epsilon_/k_
        - fvm::Sp(C2_*epsilon_/k_, epsilon_)
    );

    epsEqn().relax();

    //create labelList with cells to change for setValues function

    const tmp<volScalarField> tfCheck = fCheck();
    const volScalarField& fCheckTemp = tfCheck();

    label n = 0;
    forAll(epsilon_, cellI)
    {
        if (fCheckTemp[cellI]<0.8)
        {
            n++;
        }
    }

    cout <<"n="<< n;

    labelList setElements(n);

    n = 0;

    forAll(epsilon_, cellI)
    {
        if (fCheckTemp[cellI]<0.8)
        {
            setElements[n++] = cellI;
        }
    }

    const labelUList& keepCells = setElements;

    epsEqn().setValues(keepCells, epsilonOverwrite_);

    solve(epsEqn);

My problem now is, that the Field "epsilonOverwrite" needs to have the same size as the labelList keepCells (I think so). The code compiles when I use a Field with the full mesh size, but the simulation crashes (I think, the wrong values are picked from the field).

Does anybody know how to create a volScalarField only including the cells of a given labelList?

I would be so thankful for any kind of help or support!

Julian

api April 26, 2019 18:58

In case this issue is still pending, I am positing my solution here. There are some redundant variables. So please ignore them.

codeSetValue
#{
Pout<< "**codeSetValue**" << endl;
int my_cs_sz =0;
List<label> my_labs;
const volScalarField& alpha_m2 = mesh().lookupObject<volScalarField>("alpha.air");
forAll(alpha_m2, celli)
{
if(alpha_m2[celli]>0){
my_labs.append(celli);
}
}

cellSet my_set(mesh(),"arc");
const labelUList& my_cel =my_set.toc();
forAll(my_cel,id)
{
my_cs_sz+=1;
}

//List<scalar> my_list(my_cs_sz);
List<scalar> my_list(my_labs.size());
scalar my_temp = 782 + mesh().time().value()*30000;
my_list =(my_temp);
eqn.setValues(my_cel,my_list);
Pout<< my_cs_sz << endl;
//eqn.setValues() = 8000;
#};
}


I was trying to set temperature values to locations based on alpha.

Samuel Twain November 8, 2019 20:25

Hello api:
I want to set different value for different elememt in my_lisy. But it seems that in your code the operator= is only reloaded to set the same value for all elements in my_list. Could you please give me some advices.
Thanks
samuel

api November 10, 2019 19:06

you can set the list to any value. I am not sure if i understand your question well. I think the snippet below is what you are looking for.



Code:

            codeSetValue
            #{
              List<label> my_labs;
              List<scalar> my_list;
              scalar my_temp=0.0,xc=0.0,yc=0.0;
              const volScalarField& alpha_m2 =mesh().lookupObject<volScalarField>("alpha.air");
              forAll(alpha_m2,celli)
              {
              my_temp=0;
              xc=0.0;
              yc=0.0;
                if(alpha_m2[celli]>0.2){
                my_labs.append(celli);
                xc=fabs(0.5-mesh().C()[celli].x());
                yc=fabs(1.0-mesh().C()[celli].y());
                my_temp=578-xc*100-yc*100;  // define any function to set the value for each induvidual cell
                my_list.append(my_temp);  // setting the above defined value to the cellid identified by the if condition
                }
              }
              eqn.setValues(my_labs, my_list);
            #};


Samuel Twain November 10, 2019 23:06

Thanks a lot!!!!!That's really what I need!
samuel


All times are GMT -4. The time now is 01:50.