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

UDF for changing density under a sinusoidal function with respect to time

Register Blogs Community New Posts Updated Threads Search

Like Tree1Likes
  • 1 Post By lolno

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   March 9, 2023, 03:57
Default UDF for changing density under a sinusoidal function with respect to time
  #1
New Member
 
Mohammad Reza Soufivand
Join Date: Mar 2023
Location: Rome, Italy
Posts: 3
Rep Power: 3
Ludwig.Soufi is on a distinguished road
Hello everyone,

I am facing a problem with implementing a UDF code, and I would really appreciate it if someone could guide me in this matter.

The issue is that I have a two-dimensional square domain with four walls. At a specific point with specific coordinates in this domain, the density changes over time according to a sinusoidal function. Initially, the density is equal to one throughout the domain, and then it spreads like a sin wave from the origin of the specific point.

My objective is to extract the pressure fluctuations on a single line and save them in a separate file. To achieve this, I have written a UDF and used three macros. However, I am encountering an error and I am not sure how to fix it.

If anyone has experience in this area and can offer some guidance or help troubleshoot the issue, it would be greatly appreciated. Thank you in advance.
export.jpg
SLDCMPSS.c
Ludwig.Soufi is offline   Reply With Quote

Old   March 10, 2023, 07:03
Default
  #2
New Member
 
Join Date: Aug 2019
Posts: 12
Rep Power: 6
lolno is on a distinguished road
i think you need to parallelize your code in order to write data into a single file. You can refer to the example in fluent udf manual (7.9.2. Writing Files in Parallel)

Code:
/*******************************************************************
This function will write pressures and positions
for a fluid zone to a file on the host machine
********************************************************************/ 

#include "udf.h"
# define FLUID_ID 2

DEFINE_ON_DEMAND(pressures_to_file) {
/* Different variables are needed on different nodes */ 

#if !RP_HOST
Domain *domain=Get_Domain(1); Thread *thread;
cell_t c;
#else int i;
#endif

#if !RP_NODE
FILE *fp = NULL;
char filename[]="press_out.txt";
#endif

#if PARALLEL
    int size; /* data passing variables */
    real *array;
    int pe;
#endif

/* Only Serial and Compute Nodes have data on threads */ 
#if !RP_HOST
thread=Lookup_Thread(domain,FLUID_ID); 
#endif

#if !RP_NODE /* SERIAL or HOST */
if ((fp = fopen(filename, "w"))==NULL)
       Message("\n Warning: Unable to open %s for writing\n",filename);
    else
Message("\nWriting Pressure to %s...",filename);
#endif

/* UDF Now does 3 different things depending on SERIAL, NODE or HOST */
#if !PARALLEL /* SERIAL */ 
begin_c_loop(c,thread)
fprintf(fp, "%g\n", C_P(c,thread));/* Simply write out pressure data */ end_c_loop(c,thread)
#endif /* !PARALLEL */

#if RP_NODE
/* Each Node loads up its data passing array */ 
size=THREAD_N_ELEMENTS_INT(thread);
array = (real *)malloc(size * sizeof(real)); begin_c_loop_int(c,thread)
array[c]= C_P(c,thread); end_c_loop_int(c,thread)

/* Set pe to destination node */
/* If on node_0 send data to host */
/* Else send to node_0 because */
/* compute nodes connect to node_0 & node_0 to host */ 

pe = (I_AM_NODE_ZERO_P) ? node_host : node_zero; 

PRF_CSEND_INT(pe, &size, 1, myid);
PRF_CSEND_REAL(pe, array, size, myid);
free(array);/* free array on nodes after data sent */

/* node_0 now collect data sent by other compute nodes */ 
/* and sends it straight on to the host */

if (I_AM_NODE_ZERO_P) 
  compute_node_loop_not_zero (pe) 
{
PRF_CRECV_INT(pe, &size, 1, pe);
array = (real *)malloc(size * sizeof(real)); 
PRF_CRECV_REAL(pe, array, size, pe); 
PRF_CSEND_INT(node_host, &size, 1, myid); 
PRF_CSEND_REAL(node_host, array, size, myid); 
free((char *)array);
}
#endif /* RP_NODE */

#if RP_HOST
compute_node_loop (pe) /* only acts as a counter in this loop */
{
/* Receive data sent by each node and write it out to the file */ 

PRF_CRECV_INT(node_zero, &size, 1, node_zero);
array = (real *)malloc(size * sizeof(real)); 
PRF_CRECV_REAL(node_zero, array, size, node_zero);

    for (i=0; i<size; i++)
       fprintf(fp, "%g\n", array[i]);
    free(array);
}
#endif /* RP_HOST */

#if !RP_NODE /* SERIAL or HOST */
fclose(fp); /* Close the file that was only opened if on SERIAL or HOST */ Message("Done\n");
#endif 
}
Ludwig.Soufi likes this.
lolno is offline   Reply With Quote

Old   March 10, 2023, 14:34
Default
  #3
New Member
 
Mohammad Reza Soufivand
Join Date: Mar 2023
Location: Rome, Italy
Posts: 3
Rep Power: 3
Ludwig.Soufi is on a distinguished road
I worked on this problem and modified the UDF. Now I can initialize correctly. Also, I can change density for a region like x<XSS, but when I want to apply a function for varying density at a specific point (XSS, YSS), it doesn't work properly! Even though I changed the point to a small region with 4 nodes, it didn't work correctly, and the density didn't change. What do you think about that?!
Attached Files
File Type: c Test.c (1.2 KB, 5 views)
Ludwig.Soufi is offline   Reply With Quote

Old   March 10, 2023, 14:37
Default
  #4
New Member
 
Mohammad Reza Soufivand
Join Date: Mar 2023
Location: Rome, Italy
Posts: 3
Rep Power: 3
Ludwig.Soufi is on a distinguished road
Quote:
Originally Posted by lolno View Post
i think you need to parallelize your code in order to write data into a single file. You can refer to the example in fluent udf manual (7.9.2. Writing Files in Parallel)

Code:
/*******************************************************************
This function will write pressures and positions
for a fluid zone to a file on the host machine
********************************************************************/ 

#include "udf.h"
# define FLUID_ID 2

DEFINE_ON_DEMAND(pressures_to_file) {
/* Different variables are needed on different nodes */ 

#if !RP_HOST
Domain *domain=Get_Domain(1); Thread *thread;
cell_t c;
#else int i;
#endif

#if !RP_NODE
FILE *fp = NULL;
char filename[]="press_out.txt";
#endif

#if PARALLEL
    int size; /* data passing variables */
    real *array;
    int pe;
#endif

/* Only Serial and Compute Nodes have data on threads */ 
#if !RP_HOST
thread=Lookup_Thread(domain,FLUID_ID); 
#endif

#if !RP_NODE /* SERIAL or HOST */
if ((fp = fopen(filename, "w"))==NULL)
       Message("\n Warning: Unable to open %s for writing\n",filename);
    else
Message("\nWriting Pressure to %s...",filename);
#endif

/* UDF Now does 3 different things depending on SERIAL, NODE or HOST */
#if !PARALLEL /* SERIAL */ 
begin_c_loop(c,thread)
fprintf(fp, "%g\n", C_P(c,thread));/* Simply write out pressure data */ end_c_loop(c,thread)
#endif /* !PARALLEL */

#if RP_NODE
/* Each Node loads up its data passing array */ 
size=THREAD_N_ELEMENTS_INT(thread);
array = (real *)malloc(size * sizeof(real)); begin_c_loop_int(c,thread)
array[c]= C_P(c,thread); end_c_loop_int(c,thread)

/* Set pe to destination node */
/* If on node_0 send data to host */
/* Else send to node_0 because */
/* compute nodes connect to node_0 & node_0 to host */ 

pe = (I_AM_NODE_ZERO_P) ? node_host : node_zero; 

PRF_CSEND_INT(pe, &size, 1, myid);
PRF_CSEND_REAL(pe, array, size, myid);
free(array);/* free array on nodes after data sent */

/* node_0 now collect data sent by other compute nodes */ 
/* and sends it straight on to the host */

if (I_AM_NODE_ZERO_P) 
  compute_node_loop_not_zero (pe) 
{
PRF_CRECV_INT(pe, &size, 1, pe);
array = (real *)malloc(size * sizeof(real)); 
PRF_CRECV_REAL(pe, array, size, pe); 
PRF_CSEND_INT(node_host, &size, 1, myid); 
PRF_CSEND_REAL(node_host, array, size, myid); 
free((char *)array);
}
#endif /* RP_NODE */

#if RP_HOST
compute_node_loop (pe) /* only acts as a counter in this loop */
{
/* Receive data sent by each node and write it out to the file */ 

PRF_CRECV_INT(node_zero, &size, 1, node_zero);
array = (real *)malloc(size * sizeof(real)); 
PRF_CRECV_REAL(node_zero, array, size, node_zero);

    for (i=0; i<size; i++)
       fprintf(fp, "%g\n", array[i]);
    free(array);
}
#endif /* RP_HOST */

#if !RP_NODE /* SERIAL or HOST */
fclose(fp); /* Close the file that was only opened if on SERIAL or HOST */ Message("Done\n");
#endif 
}
Oh, thank you for your response. But sorry, would you please explain more?
Ludwig.Soufi is offline   Reply With Quote

Old   March 10, 2023, 18:05
Default
  #5
New Member
 
Join Date: Aug 2019
Posts: 12
Rep Power: 6
lolno is on a distinguished road
Quote:
Originally Posted by Ludwig.Soufi View Post
Oh, thank you for your response. But sorry, would you please explain more?
in your code below, when you use fopen & fprintf to open and write file, it's recommended for the operation to be done in node-host instead of RP_node (computing node). Imagine having 14 compute node trying to access and write data into the same file, some of the data will get overwritten.

Code:
DEFINE_ADJUST(calculate_rp_rms, d)
{
    Domain* d;
	Thread *t;
    cell_t c;
    real rho;
    real rho_per, sumrp, rp_rms, sl;
    real maxit = 1600.0; // maximum number of iterations
    int i, j;
    FILE *fp;
	
    /* Open the output file */
    fp = fopen("rp_rms.txt", "a");
    
    /* Get the density value for this iteration */
    rho = RP_Get_Real("density", 0, 0);
    
    /* Initialize sumrp */
    sumrp = 0.0;
    
    /* Loop over all cells in the domain */
    thread_loop_c(t, d)
    {
        begin_c_loop(c, t)
        {
            /* Get the cell coordinates */
            i = C_UDMI(c, t, 0);
            j = C_UDMI(c, t, 1);
            
            /* Calculate rho_per */
            rho_per = C_R(c, t) - rho;
            
            /* Calculate sumrp */
            sumrp += rho_per * rho_per;
            
            /* Write rp_rms to file for i = 1001 to 2000 and j = 50 */
            if (i >= 1001 && i <= 2000 && j == 50)
            {
                /* Calculate rp_rms */
                rp_rms = sqrt(sumrp / ((i - 1001) * j));
                
                /* Calculate sl */
                if (rp_rms != 0.0)
                {
                    sl = 20.0 * log10(rp_rms);
                }
                else
                {
                    sl = 0.0;
                }
                
                /* Write to file */
                fprintf(fp, "%d %d %f %f\n", i, j, rp_rms, sl);
            }
        }
        end_c_loop(c, t)
    }
    
    /* Close the output file */
    fclose(fp);
}
Reference taken from fluent udf manual:
Writing files in parallel is done in the following stages:
1. The host process opens the file.
2. Compute node-0 sends its data to the host.
3. The other compute nodes send their data to compute node-0.
4. Compute node-0 receives the data from the other compute nodes and sends it to the host.
5. The host receives the data sent from all the compute nodes and writes it to the file.
6. The host closes the file.
lolno is offline   Reply With Quote

Reply

Tags
define_adjust macro, define_init, define_property, udf


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
[Other] refineWallLayer Error Yuby OpenFOAM Meshing & Mesh Conversion 2 November 11, 2021 11:04
laplacianFoam with source term Herwig OpenFOAM Running, Solving & CFD 17 November 19, 2019 13:47
dynamic Mesh is faster than MRF???? sharonyue OpenFOAM Running, Solving & CFD 14 August 26, 2013 07:47
ParaView for OF-1.6-ext Chrisi1984 OpenFOAM Installation 0 December 31, 2010 06:42
Error with Wmake skabilan OpenFOAM Installation 3 July 28, 2009 00:35


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