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

Looping over all parcels to check if they are located inside a cell-zone

Register Blogs Community New Posts Updated Threads Search

Like Tree3Likes
  • 1 Post By AlexanderZ
  • 1 Post By AlexanderZ
  • 1 Post By Tobi

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   July 28, 2021, 14:27
Default Looping over all parcels to check if they are located inside a cell-zone
  #1
Super Moderator
 
Tobi's Avatar
 
Tobias Holzmann
Join Date: Oct 2010
Location: Tussenhausen
Posts: 2,708
Blog Entries: 6
Rep Power: 51
Tobi has a spectacular aura aboutTobi has a spectacular aura aboutTobi has a spectacular aura about
Send a message via ICQ to Tobi Send a message via Skype™ to Tobi
Hey everybody, I do have a simulation ongoing in which I evaporate h2o2-h2o droplets almost at a relative saturation of 90 % (maximum = 93% as the droplets do have 12 mass-% h2o2 and 88 mass-% h2o -> this lead to 0.93 mole of h2o and hence maximum 93 %). However, the system gets filled up with a lot of parcels and I don't have any air that is flushing the room. Hence, I would like too loop through all parcels and check if they are inside a cell zone (far away from the area of interest). If they are inside, I want to remove them from the system by calling the appropriate makro (MARK_TP).


I would like to do that by using a EXECUTE_ON_DEMAND (after each flow time step). I have the following code right now. I get the material first and take all cells from the cell zone of cell id = 50. After that I loop over all cells of these cell zone and loop over all parcels if there are some inside the cell. At the end I want to remove the parcel but for that purpose I need to have the "Tracked_Particle" pointer and I don't know how to get it.



Code:
DEFINE_ON_DEMAND(removeParcels)
{
    // Fluid domain
    Domain *domain = Get_Domain(1);
    
    // Cell zone id
    int Cell_Zone_ID = 50;
    
    cell_t c;
    Thread *t = Lookup_Thread(domain, Cell_Zone_ID);

    // Particle
    Particle *p;
    
    int particleId = 0;
    

    // Looping over all cells of the cell zone
    begin_c_loop(c, t)
    {
        // Loop over all parcels in the coresponding cell
        begin_particle_cell_loop(p, c, t)
        {
            // If we are here, the parcel is inside the cell that is inside
            // the cell zone - hence we can remove it
            
            // Need to get the tracked parcel pointer --> how
            Tracked_Particle *tp;
            
            // Remove the parcel
            MARK_TP(tp, P_FL_REMOVED);

            // this sthould not work:
            // MARK_TP(p, P_FL_REMOVED);
        }
    }

    end_c_loop(c,t)
}
Any suggestions are warmly welcomed. Futhermore, it would be interesting to know how I can make the cell-zone-id dynamic by setting it via TUI rather than hard-code it to the code.
__________________
Keep foaming,
Tobias Holzmann
Tobi is offline   Reply With Quote

Old   July 28, 2021, 15:13
Default
  #2
Super Moderator
 
Tobi's Avatar
 
Tobias Holzmann
Join Date: Oct 2010
Location: Tussenhausen
Posts: 2,708
Blog Entries: 6
Rep Power: 51
Tobi has a spectacular aura aboutTobi has a spectacular aura aboutTobi has a spectacular aura about
Send a message via ICQ to Tobi Send a message via Skype™ to Tobi
I have the solution:
Code:
DEFINE_DPM_SCALAR_UPDATE(parcelRemoval, c, t, initialize, tp)
{
    if (initialize)
    {
        // Fluid domain
        Domain *domain = Get_Domain(1);

        // Cell zone id
        int Cell_Zone_ID = 598;

        cell_t cCZ; 
        Thread *tCZ = Lookup_Thread(domain, Cell_Zone_ID);

        // Looping over all cells of the cell zone
        begin_c_loop(cCZ, tCZ)
        {
            // Check if cCZ == c -> parcel inside
            if (cCZ == c)
            {   
                MARK_TP(tp, P_FL_REMOVED);
            }
        }
        end_c_loop(cCZ, tCZ)
    }
}

The only thing that is missing is to set the cell-zone-id via TUI rather than hard-code it.
__________________
Keep foaming,
Tobias Holzmann
Tobi is offline   Reply With Quote

Old   July 29, 2021, 00:28
Default
  #3
Senior Member
 
Alexander
Join Date: Apr 2013
Posts: 2,363
Rep Power: 34
AlexanderZ will become famous soon enoughAlexanderZ will become famous soon enough
to get the ID from TUI you may use rpvars
1. execute following script in fluent console, or put it inside the journal or scheme and run
Code:
(ti-menu-load-string (format #f "(define (make-new-rpvar name default type)(rp-var-define name default type #f))"))
(ti-menu-load-string (format #f "(make-new-rpvar 'tui_cell_zone_id 598 'int)"))
now you have rpvar which contains ID
2. read it in UDF
Code:
DEFINE_DPM_SCALAR_UPDATE(parcelRemoval, c, t, initialize, tp)
{
    if (initialize)
    {
        // Fluid domain
        Domain *domain = Get_Domain(1);

        // Cell zone id
        int Cell_Zone_ID = 111;

        cell_t cCZ; 
		
		#if !RP_NODE
			if (RP_Variable_Exists_P("tui_cell_zone_id"))
				Cell_Zone_ID = RP_Get_Integer("tui_cell_zone_id");
			Message("Cell_Zone_ID on host-> %d\n",Cell_Zone_ID);
		#endif
		
		host_to_node_int_1(Cell_Zone_ID);
		Message0("DEBUG: Cell_Zone_ID on nodes-> %d\n",Cell_Zone_ID);
        Thread *tCZ = Lookup_Thread(domain, Cell_Zone_ID);

        // Looping over all cells of the cell zone
        begin_c_loop(cCZ, tCZ)
        {
            // Check if cCZ == c -> parcel inside
            if (cCZ == c)
            {   
                MARK_TP(tp, P_FL_REMOVED);
            }
        }
        end_c_loop(cCZ, tCZ)
    }
}
I didn't test this code

3. Actually, I will be more efficient to rad rpvar outside DEFINE_DPM_SCALAR_UPDATE macro as it is executed on each cell/thread
Tobi likes this.
__________________
best regards


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

Old   July 29, 2021, 03:14
Default
  #4
Super Moderator
 
Tobi's Avatar
 
Tobias Holzmann
Join Date: Oct 2010
Location: Tussenhausen
Posts: 2,708
Blog Entries: 6
Rep Power: 51
Tobi has a spectacular aura aboutTobi has a spectacular aura aboutTobi has a spectacular aura about
Send a message via ICQ to Tobi Send a message via Skype™ to Tobi
Hey,

I can make the cellZoneId a global variable and read it via an execute-on-demand or via the execute-on-loading. One last question, as you are using the commands for nodes and hosts.


The following code works perfectly, but I get for each node the Message regarding the removed parcels. So to simplify the output, it would be great to sum the nRemovedParcels from all nodes to the host and only show the host messages using Message0(). However, I am not sure how to get the other node values and sum it to the host guy to finally get the overall removed parcels. Any idea?



Code:
DEFINE_DPM_SCALAR_UPDATE(parcelRemoval, c, t, initialize, tp)
{
    if (initialize)
    {
        // Check if the time is equal to the one we set
        real currentFlowTime = CURRENT_TIME;

        // Cell zone id
        int cellZoneId = 598;

        // New time step
        if (currentFlowTime > flowTime)
        {
            flowTime = currentFlowTime;

            if (nRemovedParcels != 0)
            {

                // Output how much parcels were removed in the last time step
                // For each node that removes parcels, this guy is outputted
                // Hence, we can have more outputs
                Message
                (
                    "In the last time-step, %i parcels were removed from "
                    "the set cell-zone (id = %i)\n", nRemovedParcels, cellZoneId
                );

                // Reset the nRemovedParcels
                nRemovedParcels = 0;
            }
        }

        // Fluid domain
        Domain *domain = Get_Domain(1);

        cell_t cCZ;
        Thread *tCZ = Lookup_Thread(domain, cellZoneId);

        // Looping over all cells of the cell zone
        begin_c_loop(cCZ, tCZ)
        {
            // Check if cCZ == c -> parcel inside
            if (cCZ == c)
            {
                MARK_TP(tp, P_FL_REMOVED);
                nRemovedParcels++;
            }
        }
        end_c_loop(cCZ, tCZ)
    }
}
__________________
Keep foaming,
Tobias Holzmann
Tobi is offline   Reply With Quote

Old   July 29, 2021, 03:37
Default
  #5
Senior Member
 
Alexander
Join Date: Apr 2013
Posts: 2,363
Rep Power: 34
AlexanderZ will become famous soon enoughAlexanderZ will become famous soon enough
you may try the following code
cause you are using summation of variable over all nodes, you need global summation, which is executed on nodes
Message0 is data from node0, not from host

Code:
DEFINE_DPM_SCALAR_UPDATE(parcelRemoval, c, t, initialize, tp)
{
    if (initialize)
    {
        // Check if the time is equal to the one we set
        real currentFlowTime = CURRENT_TIME;

        // Cell zone id
        int cellZoneId = 598;

        // New time step
        if (currentFlowTime > flowTime)
        {
            flowTime = currentFlowTime;

            if (nRemovedParcels != 0)
            {

                // Output how much parcels were removed in the last time step
                // For each node that removes parcels, this guy is outputted
                // Hence, we can have more outputs
                Message0
                (
                    "In the last time-step, %i parcels were removed from "
                    "the set cell-zone (id = %i)\n", nRemovedParcels, cellZoneId
                );

                // Reset the nRemovedParcels
                nRemovedParcels = 0;
            }
        }

        // Fluid domain
        Domain *domain = Get_Domain(1);

        cell_t cCZ;
        Thread *tCZ = Lookup_Thread(domain, cellZoneId);

        // Looping over all cells of the cell zone
        begin_c_loop(cCZ, tCZ)
        {
            // Check if cCZ == c -> parcel inside
            if (cCZ == c)
            {
                MARK_TP(tp, P_FL_REMOVED);
                nRemovedParcels++;
            }
        }
        end_c_loop(cCZ, tCZ)
		#if RP_NODE
			nRemovedParcels = PRF_GISUM1(nRemovedParcels);
		#endif /*RP_NODE*/
    }
}
__________________
best regards


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

Old   July 29, 2021, 04:24
Default
  #6
Super Moderator
 
Tobi's Avatar
 
Tobias Holzmann
Join Date: Oct 2010
Location: Tussenhausen
Posts: 2,708
Blog Entries: 6
Rep Power: 51
Tobi has a spectacular aura aboutTobi has a spectacular aura aboutTobi has a spectacular aura about
Send a message via ICQ to Tobi Send a message via Skype™ to Tobi
Hey Alexander,

thank you very much for your reply. You are a great support. First off all, the TUI command is:
Code:
> (make-new-rpvar 'cell_zone_id_remove_parcels <ID> 'integer #f)
Doing, so, I can access it via your hints and set / redistribute it to all nodes. I did this using a DEFINED_ON_DEMAND function. Hence, I need to execute it once to set the data and fine. After your hints, I also checked the UDF UG again and found the function (global summation). I will now try your solution and give some feedback.
__________________
Keep foaming,
Tobias Holzmann
Tobi is offline   Reply With Quote

Old   July 29, 2021, 06:32
Default
  #7
Super Moderator
 
Tobi's Avatar
 
Tobias Holzmann
Join Date: Oct 2010
Location: Tussenhausen
Posts: 2,708
Blog Entries: 6
Rep Power: 51
Tobi has a spectacular aura aboutTobi has a spectacular aura aboutTobi has a spectacular aura about
Send a message via ICQ to Tobi Send a message via Skype™ to Tobi
Hey Alex,


for any reason my code works not correctly...
It removes cells from the other cell zone and I don't know why this happens. Any suggestion?


Bildschirmfoto von 2021-07-29 12-28-40.jpg
__________________
Keep foaming,
Tobias Holzmann
Tobi is offline   Reply With Quote

Old   July 29, 2021, 06:49
Default
  #8
Super Moderator
 
Tobi's Avatar
 
Tobias Holzmann
Join Date: Oct 2010
Location: Tussenhausen
Posts: 2,708
Blog Entries: 6
Rep Power: 51
Tobi has a spectacular aura aboutTobi has a spectacular aura aboutTobi has a spectacular aura about
Send a message via ICQ to Tobi Send a message via Skype™ to Tobi
Okay anything is completely wrong here. I checked the cell position coming from the function and also the cell-zone position. Both are zero. Furthermore, I took the parcel pointer to get the cell position again and everything is zero even though, I can see a lot of parcels.


Code:
            // Looping over all cells of the cell zone
            begin_c_loop(cCZ, tCZ)
            {
                // Check if cCZ == c -> parcel inside
                if (cCZ == c)
                {
                    cell_t cParcel = TP_CELL(tp);
                    Thread *tcParcel = TP_CELL_THREAD(tp);

                    int pos[3];
                    int posParcel[3];
                    int posParcel2[3];

                    C_CENTROID(pos, cCZ, tCZ);
                    C_CENTROID(posParcel, c, t);
                    C_CENTROID(posParcel2, cParcel, tcParcel);

                    Message("CellZone-Cell == cell of parcel  >> %d == %d\n", cCZ, c);
                    Message("CellZone-Cell pos (x|y|z) = (%f | %f | %f)\n", pos[0], pos[1], pos[2]);
                    Message("Parcel  -Cell pos (x|y|z) = (%f | %f | %f)\n", posParcel[0], posParcel[1], posParcel[2]);
                    Message("Parcel  -Cell pos (x|y|z) = (%f | %f | %f)\n", posParcel2[0], posParcel2[1], posParcel2[2]);

                    // Mark parcels as vanished
                    MARK_TP(tp, P_FL_REMOVED);

                    // Increment counter
                    nRemovedParcels++;
                }
            }
            end_c_loop(cCZ, tCZ)
The TUI output:
Code:
CellZone-Cell == cell of parcel  >> 161 == 161
CellZone-Cell pos (x|y|z) = (0.000000 | 0.000000 | 0.000000)
Parcel  -Cell pos (x|y|z) = (0.000000 | 0.000000 | 0.000000)
Parcel  -Cell pos (x|y|z) = (0.000000 | 0.000000 | 0.000000)
CellZone-Cell == cell of parcel  >> 168 == 168
CellZone-Cell pos (x|y|z) = (0.000000 | 0.000000 | 0.000000)
Parcel  -Cell pos (x|y|z) = (0.000000 | 0.000000 | 0.000000)
Parcel  -Cell pos (x|y|z) = (0.000000 | 0.000000 | 0.000000)
Any parallel stuff which we need to take into consideration here - or anything missing? This code will crash directly:
Code:
begin_c_loop(cCZ, tCZ)
{
      Message("Cell %d\n", cCZ);
}
end_c_loop(cCZ, tCZ)
No idea why. However, the code works partially correct because if the parcels reach the cell zone, they get vanished instantaneously. However, in addition there are more parcels removed and I don't know why.
__________________
Keep foaming,
Tobias Holzmann
Tobi is offline   Reply With Quote

Old   July 30, 2021, 01:36
Default
  #9
Senior Member
 
Alexander
Join Date: Apr 2013
Posts: 2,363
Rep Power: 34
AlexanderZ will become famous soon enoughAlexanderZ will become famous soon enough
you have zeroes because of wrong type of variables
Code:
real pos[3];
real posParcel[3];
real posParcel2[3];
this code should work without any problems, there is no need to parallelize it
Code:
begin_c_loop(cCZ, tCZ)
{
      Message("Cell %d\n", cCZ);
}
end_c_loop(cCZ, tCZ)

about number of vanished particles.... Unfortunately, I don't see any reason for that
Tobi likes this.
__________________
best regards


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

Old   August 2, 2021, 03:17
Default
  #10
Super Moderator
 
Tobi's Avatar
 
Tobias Holzmann
Join Date: Oct 2010
Location: Tussenhausen
Posts: 2,708
Blog Entries: 6
Rep Power: 51
Tobi has a spectacular aura aboutTobi has a spectacular aura aboutTobi has a spectacular aura about
Send a message via ICQ to Tobi Send a message via Skype™ to Tobi
Hey Alex,

for any reason this piece of code is not correct:
Code:
 cell_t cCZ; 
Thread *tCZ = Lookup_Thread(domain, Cell_Zone_ID);

begin_c_loop(cCZ, tCZ)
{
      // Do something
}
end_c_loop(cCZ, tCZ)
A more reliable and easier way is the following:
Code:
if (THREAD_ID(t) == Cell_Zone_ID)
{
    //Remove the parcel and do other stuff
}
The last one works perfectly and I donīt get any stupid results anymore. I will also try the accumulation of the integer (summation) as you described.
And sure ... my fault ... the position is not an integer
alainislas likes this.
__________________
Keep foaming,
Tobias Holzmann
Tobi is offline   Reply With Quote

Reply


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
Fluent Mesh Check Fail aunmhd FLUENT 1 August 9, 2020 06:23
Fluent UDF wrong number of cells in parallel - correct in serial dralexpe Fluent UDF and Scheme Programming 7 May 17, 2018 08:26
[Commercial meshers] Mesh conversion problem (fluent3DMeshToFoam) Aadhavan OpenFOAM Meshing & Mesh Conversion 2 March 8, 2018 01:47
critical error during installation of openfoam Fabio88 OpenFOAM Installation 21 June 2, 2010 03:01
Problems in compiling paraview in Suse 10.3 platform chiven OpenFOAM Installation 3 December 1, 2009 07:21


All times are GMT -4. The time now is 21:12.