CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > Software User Forums > ANSYS > FLUENT > Fluent UDF and Scheme Programming

Finding a cell closest to a point

Register Blogs Community New Posts Updated Threads Search

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   May 23, 2022, 08:49
Question Finding a cell closest to a point
  #1
Member
 
Mohammad Shafiee
Join Date: Apr 2021
Posts: 30
Rep Power: 7
Mohammad74 is on a distinguished road
Hello,
I've written a DEFINE_EXECUTE_ON_LOADING macro to find a cell in the domain closest to a specified point. I did it by looping over all the cells and finding the cell with minimum distance from that point. Later I will use this cell's identifiers (hubthread_id, hubcell_id in the code below) in a DEFINE_ADJUST macro from another source file to address it. Here's a shortened version of my code:

Code:
#include "udf.h"

Thread *hubthread_id ;
cell_t hubcell_id ;

DEFIINE_EXECUTE_ON_LOADING(macro_name,libname)
{
    Domain *dom ;
    Thread *c_thread ;
    cell_t cell ;
    double d, d_new, c[3], temp[3];
    double x_h[3] = {0.0,3.0,0.0} ;

    dom = Get_Domain(1) ;
    d = 1.2 ;

    thread_loop_c(c_thread,dom)
    {
        begin_c_loop(cell,c_thread)
        {
            C_CENTROID(c, cell, c_thread);
            d_new = sqrt(pow(x[0]-c[0],2)+pow(x[1]-c[1],2)+pow(x[2]-c[2],2));
            if (d_new < d)
            {
                hubthread_id = c_thread ;
                hubcell_id = cell ;
                d = d_new ;
            }
                  
        }
        end_c_loop(cell,c_thread)
    }

    C_CENTROID(temp, hubcell_id, hubthread_id);               /*I'm trying to see if its working properly, but gave me an error*/
    Message("\n %d %d %d", temp[0],temp[1],temp[2]) ;

}

I can compile it with no errors but after loading the library the last two lines are causing my code to break with these errors:

SharedScreenshot.jpg

Any reasons why this is happening? or any better ways to do this whole process?

Last edited by Mohammad74; May 23, 2022 at 11:24.
Mohammad74 is offline   Reply With Quote

Old   May 23, 2022, 22:39
Default
  #2
Senior Member
 
Alexander
Join Date: Apr 2013
Posts: 2,363
Rep Power: 34
AlexanderZ will become famous soon enoughAlexanderZ will become famous soon enough
no idea how you could compile it without errors, as there are few typos

I recommend you to compile code always
I was able to compile following code, test it:
Code:
#include "udf.h"

Thread *hubthread_id ;
cell_t hubcell_id ;

DEFINE_EXECUTE_ON_LOADING(macro_name,libname)
{
    Domain *dom ;
    Thread *c_thread ;
    cell_t cell ;
    real d, d_new, c[ND_ND], temp[ND_ND];
    real x[3] = {0.0,3.0,0.0} ;

    dom = Get_Domain(1) ;
    d = 1.2 ;

    thread_loop_c(c_thread,dom)
    {
        begin_c_loop(cell,c_thread)
        {
            C_CENTROID(c, cell, c_thread);
            d_new = sqrt(pow(x[0]-c[0],2)+pow(x[1]-c[1],2)+pow(x[2]-c[2],2));
            if (d_new < d)
            {
                hubthread_id = c_thread ;
                hubcell_id = cell ;
                d = d_new ;
            }
                  
        }
        end_c_loop(cell,c_thread)
    }

    C_CENTROID(temp, hubcell_id, hubthread_id);               /*I'm trying to see if its working properly, but gave me an error*/
    Message0("\n %f %f %f", temp[0],temp[1],temp[2]) ;

}
__________________
best regards


******************************
press LIKE if this message was helpful
AlexanderZ is offline   Reply With Quote

Old   May 24, 2022, 08:17
Default
  #3
Member
 
Mohammad Shafiee
Join Date: Apr 2021
Posts: 30
Rep Power: 7
Mohammad74 is on a distinguished road
Quote:
Originally Posted by AlexanderZ View Post
no idea how you could compile it without errors, as there are few typos
Hi,
I'm sorry, Yeah you are correct about typos. But i made these mistakes while typing the code in this forum.
Anyway, I compiled the code you provided and again it was compiled with no errors, but after loading the library, it gives a lot of errors and I couldn't find the reason behind it. Here are images of my tui after loading the library made from your code:

1.jpg2.jpg
Mohammad74 is offline   Reply With Quote

Reply

Tags
cell centroid, fluent 19.0, udf and programming


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
[blockMesh] Floating Point Exception while generating wedge based mesh jns-v OpenFOAM Meshing & Mesh Conversion 9 July 8, 2021 05:36
Simulating fire in a tunnel luca1992 OpenFOAM 14 August 16, 2017 13:50
[General] Finding the exact location of a cell paraview arieljeds ParaView 0 January 7, 2016 07:12
FvMatrix coefficients shrina OpenFOAM Running, Solving & CFD 10 October 3, 2013 14:38
Errors in UDF shashank312 Fluent UDF and Scheme Programming 6 May 30, 2013 20:30


All times are GMT -4. The time now is 13:16.