CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > Software User Forums > OpenFOAM > OpenFOAM Pre-Processing

Selecting cell closest to coordinates (and setting its field value)

Register Blogs Members List Search Today's Posts Mark Forums Read

Like Tree2Likes
  • 1 Post By Antimony
  • 1 Post By hansjoerg

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   May 13, 2019, 09:03
Default Selecting cell closest to coordinates (and setting its field value)
  #1
New Member
 
Join Date: Mar 2019
Posts: 4
Rep Power: 8
rando_foamer is on a distinguished road
Hi all,

I am wondering how to go about doing the following, and hope someone can help. I've been trying to find a solution using setFields and funkySetFields, but can't find the right approach.

I want to modify the initial condition of a single cell in my geometry, and I want to select this cell based off its proximity to a provided set of coordinates e.g. (x=0.5,y=1.0,z=0.5). This set of coordinates will be close to, but not necessarily directly on, a patch face. For example, given the following simple box as described by this blockMeshDict:

Code:
/*--------------------------------*- C++ -*----------------------------------*\
| =========                 |                                                 |
| \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox           |
|  \\    /   O peration     | Version:  5                                     |
|   \\  /    A nd           | Web:      www.OpenFOAM.org                      |
|    \\/     M anipulation  |                                                 |
\*---------------------------------------------------------------------------*/
FoamFile
{
    version     2.0;
    format      ascii;
    class       dictionary;
    object      blockMeshDict;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

convertToMeters 1;

vertices
(   
    (0 0 0)
    (1 0 0) 
    (1 1 0) 
    (0 1 0)
    (0 0 1)
    (1 0 1) 
    (1 1 1) 
    (0 1 1)
);

blocks
(
    hex (0 1 2 3 4 5 6 7) (3 6 100) simpleGrading (1 1 1)
    
);

edges
(
    
);

boundary
(
    inlet
    {
        type patch;
        faces
        (
          (0 1 2 3)
        );
    }
    
    outlet
    {
        type wall;
        faces
        (
          (7 6 5 4)
        );
    }
    
    defaultFaces
    {
        type empty;
        faces ();
    }
    
);

mergePatchPairs
(
);

// ************************************************************************* //
This is obviously a box of dimensions (1 1 1) with a single block.

Ideally I would do this completely in setFields, by finding the cell that has its centre closest to the coordinates (0.5 1.0 0.5), then set its value for my volScalarField (e.g. T). I would not necessarily know what the coordinates were prior to runtime (they would be defined by some other code) and so can't manually create a block specifically for the location.

From what I can gather, there is no "coordinatesToCell" function in setFields, i.e. the choices are:

Code:
(
boundaryToFace
boxToCell
boxToFace
boxToPoint
cellToCell
cellToFace
cellToPoint
cylinderAnnulusToCell
cylinderToCell
faceToCell
faceToFace
faceToPoint
faceZoneToCell
faceZoneToFaceZone
fieldToCell
labelToCell
labelToFace
labelToPoint
nbrToCell
nearestToCell
nearestToPoint
normalToFace
patchToFace
pointToCell
pointToFace
pointToPoint
regionToCell
rotatedBoxToCell
setToCellZone
setToFaceZone
setToPointZone
setsToFaceZone
shapeToCell
sphereToCell
surfaceToCell
surfaceToPoint
zoneToCell
zoneToFace
zoneToPoint
)
The closest I can find is the "pointTo*" and "nearestToCell" functions. But from what I understand, I would need to provide a point reference, not coordinates. I don't think I can use "boxToCell" without knowing beforehand what the cell size was, so I didn't accidentally select more than one cell (or zero cells). I'm also not sure what many of the above options actually do (descriptions are very vague in places).

At the moment I am contemplating creating a Python script that finds the face that is nearest to the given coordinates (calculating the average of face points from the polyMesh folder), then using the "faceToCell" function in setFields.

Hopefully there's a better way!

Thanks all for your help.
rando_foamer is offline   Reply With Quote

Old   May 13, 2019, 22:30
Default
  #2
Senior Member
 
Join Date: Aug 2013
Posts: 407
Rep Power: 17
Antimony is on a distinguished road
Hi,

Doesn't nearestToCell do exactly what you want it to do? From https://github.com/OpenFOAM/OpenFOAM...et/topoSetDict, what it says is this:

Quote:
// // Cells with cellCentre nearest to coordinates
// source nearestToCell;
// sourceInfo
// {
// points ((0 0 0) (1 1 1)(2 2 2));
// }
So, if you replace the points in the example with your own, should work I think. No?

Cheers,
Antimony
xiangxiang likes this.
Antimony is offline   Reply With Quote

Old   May 14, 2019, 06:15
Default
  #3
New Member
 
Join Date: Mar 2019
Posts: 4
Rep Power: 8
rando_foamer is on a distinguished road
Aha, you are right, of course. I had the syntax wrong, and I misunderstood the error message.

For those interested, the correct syntax to use for the setFieldsDict (rather than topoSetDict) is as follows:

Code:
/*--------------------------------*- C++ -*----------------------------------*\
| =========                 |                                                 |
| \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox           |
|  \\    /   O peration     | Version:  v1812                                 |
|   \\  /    A nd           | Web:      www.OpenFOAM.com                      |
|    \\/     M anipulation  |                                                 |
\*---------------------------------------------------------------------------*/
FoamFile
{
    version     2.0;
    format      ascii;
    class       dictionary;
    location    "system";
    object      setFieldsDict;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

defaultFieldValues
(
  volScalarFieldValue T 0
);

regions
(
nearestToCell
{
  points ( (0.5 1.0 0.5) );
  fieldValues
    (
      volScalarFieldValue T 1.0
    );
}
);
Thanks Antimony, much appreciated.
rando_foamer is offline   Reply With Quote

Old   April 28, 2025, 10:59
Default find cell containing coordinates
  #4
New Member
 
Hansjoerg Seybold
Join Date: Mar 2009
Posts: 16
Rep Power: 18
hansjoerg is on a distinguished road
Hi,

I know this is a very old thread, but I could not find a valid solution to a very related the question, namely finding the cell(s) containing points (given by their coordinates).

The suggested solution "nearestToCell" is only valid for isotropic meshes as nearestToCell finds the Cell whose cell-center is closest to the given point. In anisotropic meshes, with big and small cells next to each other, it can happen that the closest cell center of a point is not the cell containing the point, but a neighboring cell.

Thus I am still looking for a way to select the cell(s) by a list of coordinates.

(a cell is marked if any of the points is inside the cell)




Thanks a lot!
zd7s18533 likes this.
hansjoerg is offline   Reply With Quote

Old   June 2, 2025, 11:57
Default
  #5
New Member
 
anonymous.
Join Date: Jan 2020
Posts: 18
Rep Power: 7
zd7s18533 is on a distinguished road
Hi hansjoerg
I think the simplest method is to use BoxToCell, which select cells based on cell centres inside box(es).




There is another more complex method, if you look at the source file of nearestToCell, you will find the following code
void Foam::nearestToCell::combine(topoSet& set, const bool add) const
{
forAll(points_, pointi)
{
addOrDelete(set, mesh_.findNearestCell(points_[pointi]), add);
}
}
It actually calls the findNearestCell function, which is located in the meshSearch. H file. In fact, the file also has a function called findCell
//- Find cell containing location.
// If seed provided walks and falls back to linear/tree search.
// (so handles holes correctly)s
// Returns -1 if not in domain.
label findCell
(
const point& location,
const label seedCelli = -1,
const bool useTreeSearch = true
) const;
I think you can try manually modifying the code to meet your needs
zd7s18533 is offline   Reply With Quote

Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Setting BC for each cell on face Geisel Fluent UDF and Scheme Programming 3 July 2, 2010 03:52


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