CFD Online Discussion Forums

CFD Online Discussion Forums (http://www.cfd-online.com/Forums/)
-   Fluent UDF and Scheme Programming (http://www.cfd-online.com/Forums/fluent-udf/)
-   -   Resolved: Changing boundary condition with UDF according to pressure outlet boundary (http://www.cfd-online.com/Forums/fluent-udf/76589-resolved-changing-boundary-condition-udf-according-pressure-outlet-boundary.html)

alpemre May 30, 2010 07:43

Resolved: Changing boundary condition with UDF according to pressure outlet boundary
 
Hello everybody, my problem was:
I was trying to set a temperature to a velocity inlet in my problem according to the average temperature of the air leaving the domain from a pressure outlet. I searched through the forum to find a solution to this but I could not find a clear answer. Now I managed to write a parallel UDF (for 8 cpus) as a remedy to this problem. Hope this helps if you have the same problem:

Code:

#include "udf.h"

/* Average temperature is calculated and share with other CPUs */

DEFINE_ADJUST(average_exit_temp, domain)
{
#if !RP_HOST    /*  either serial or compute node process is involved */

        face_t f;
        real sum_temp=0.0; 
        int nfaces = 0, i;
        int ID  = 21;        //on_hava_cikisi
        int ID0 = 31;        //uflec_orta_sag
        int ID1 = 30;        //uflec_orta_sol
        int ID2 = 28;        //uflec_sag
        int ID3 = 29;        //uflec_sag_cam
        int ID4 = 26;        //uflec_sol
        int ID5 = 27;        //uflec_sol_cam

        /* Determination of zone IDs */
        Thread *thread  = Lookup_Thread(domain, ID );
        Thread *thread0 = Lookup_Thread(domain, ID0);
        Thread *thread1 = Lookup_Thread(domain, ID1);
        Thread *thread2 = Lookup_Thread(domain, ID2);
        Thread *thread3 = Lookup_Thread(domain, ID3);
        Thread *thread4 = Lookup_Thread(domain, ID4);
        Thread *thread5 = Lookup_Thread(domain, ID5);

        /* Calculation of average exit temperature and sending to other nodes. The thread on which the average temperature is calculated is on node zero*/
        if(I_AM_NODE_ZERO_P)
        {
                begin_f_loop(f, thread)
                {
                        nfaces = nfaces + 1;
                        sum_temp = sum_temp + F_T(f,thread);
                }
                end_f_loop(f,thread)
                sum_temp = sum_temp/nfaces;
                Message("Node %d :  Average exit temperature = %f\n", myid, sum_temp);
                compute_node_loop_not_zero(i)
                {
                        PRF_CSEND_REAL(i, &sum_temp, 1, myid);
                }
        }

        /* Reception of average temperature from node zero */
        if(! I_AM_NODE_ZERO_P)
        {
                PRF_CRECV_REAL(0, &sum_temp, 1, 0);
                Message("Node %d : Average exit temperature = %f\n", myid, sum_temp);
        }

        /* User defined memory of thread0 are filled with sum_temp */
        begin_f_loop(f, thread0)
        {
                F_UDMI(f,thread0,0) = sum_temp;
        }
        end_f_loop(f,thread0)

        /* User defined memory of thread1 are filled with sum_temp */
        begin_f_loop(f, thread1)
        {
                F_UDMI(f,thread1,1) = sum_temp;
        }
        end_f_loop(f,thread1)

        /* User defined memory of thread2 are filled with sum_temp */
        begin_f_loop(f, thread2)
        {
                F_UDMI(f,thread2,2) = sum_temp;
        }
        end_f_loop(f,thread2)

        /* User defined memory of thread3 are filled with sum_temp */
        begin_f_loop(f, thread3)
        {
                F_UDMI(f,thread3,3) = sum_temp;
        }
        end_f_loop(f,thread3)

        /* User defined memory of thread4 are filled with sum_temp */
        begin_f_loop(f, thread4)
        {
                F_UDMI(f,thread4,4) = sum_temp;
        }
        end_f_loop(f,thread4)

        /* User defined memory of thread5 are filled with sum_temp */
        begin_f_loop(f, thread5)
        {
                F_UDMI(f,thread5,5) = sum_temp;
        }
        end_f_loop(f,thread5)

#endif
}

/* Boundary conditions are filled from user defined memory locations  */

DEFINE_PROFILE(temp_uflec_orta_sag,t,i)
{
        #if !RP_HOST
        face_t f;

        begin_f_loop(f,t)
        {
                F_PROFILE(f,t,i) = F_UDMI(f,t,0);
        }
        end_f_loop(f,t)
        #endif
}

DEFINE_PROFILE(temp_uflec_orta_sol,t,i)
{
        #if !RP_HOST
        face_t f;

        begin_f_loop(f,t)
        {
                F_PROFILE(f,t,i) = F_UDMI(f,t,1);
        }
        end_f_loop(f,t)
        #endif
}

DEFINE_PROFILE(temp_uflec_sag,t,i)
{
        #if !RP_HOST
        face_t f;

        begin_f_loop(f,t)
        {
                F_PROFILE(f,t,i) = F_UDMI(f,t,2);
        }
        end_f_loop(f,t)
        #endif
}

DEFINE_PROFILE(temp_uflec_sag_cam,t,i)
{
        #if !RP_HOST
        face_t f;

        begin_f_loop(f,t)
        {
                F_PROFILE(f,t,i) = F_UDMI(f,t,3);
        }
        end_f_loop(f,t)
        #endif
}

DEFINE_PROFILE(temp_uflec_sol,t,i)
{
        #if !RP_HOST
        face_t f;

        begin_f_loop(f,t)
        {
                F_PROFILE(f,t,i) = F_UDMI(f,t,4);
        }
        end_f_loop(f,t)
        #endif
}

DEFINE_PROFILE(temp_uflec_sol_cam,t,i)
{
        #if !RP_HOST
        face_t f;

        begin_f_loop(f,t)
        {
                F_PROFILE(f,t,i) = F_UDMI(f,t,5);
        }
        end_f_loop(f,t)
        #endif
}

alpemre

ehooi January 3, 2011 03:58

help
 
Hi there,
Thank you very much for this. Can I know some of the function of the lines you have written?

1) Message("Node %d : Average exit temperature = %f\n", myid, sum_temp);

and

2)compute_node_loop_not_zero(i)

I am very new in UDF in ANSYS Fluent and will appreciate all the help I can get.

Thank you.


EH

CMA December 17, 2012 15:46

How can I do the same job as you did but not for parallel nodes?
 
Hi,

I am trying to do the same with my code, but since it's not parallel, I am omitting every line in your code that relates to parallel CPUs. Here is my code:

#include "udf.h"

DEFINE_ADJUST(average_exit_temp, domain)
{

face_t f;
real tempa=0.0;
real totalarea=0.0;
real avetempa=0.0;
real A[ND_ND];

int ID1 = 4; /* Zone ID for Outflow zone from Boundary Conditions panel */
Thread *outlet_thread = Lookup_Thread(domain, ID1);

int ID2 = 7; /* Zone ID for Inlet zone from Boundary Conditions panel */
Thread *inlet_thread = Lookup_Thread(domain, ID2);

/* Loop over faces in a face thread to get the information stored on faces.*/
begin_f_loop(f,outlet_thread)
{
/* F_T gets face temperature. += causes all face areas/temperatures to be added together. */
totalarea += F_AREA(A,f,outlet_thread);
tempa += F_AREA(A,f,outlet_thread)*F_T(f,outlet_thread);
}
end_f_loop(f,outlet_thread)

avetempa = tempa/totalarea + 30*200/(0.561*4182);
printf("average temperature= %e\n",avetempa);

begin_f_loop(f, inlet_thread)
{
F_UDMI(f,inlet_thread,1) = avetempa;
}

}

DEFINE_PROFILE(Inlettemp,t,i)
{

real flow_time;
flow_time = RP_Get_Real("flow-time");
face_t face;

if(flow_time<=5.0)
{
printf("t= %e\n",flow_time);
begin_f_loop(face, t)
{
F_PROFILE(face, t, i)=283.0;
}
end_f_loop(face, t)
}

else
{

begin_f_loop(face, t)
{
F_PROFILE(face, t, i) = F_UDMI(f,t,1);
}
end_f_loop(face, t)
printf("t= %e\n",flow_time);
}
}

and I get a parse error on the Define_Profile line. Note that my case file and my c file are in the same directory. Do you have any idea what might be wrong here?

Thank you in advance for your help.

CMA

Quote:

Originally Posted by alpemre (Post 260872)
Hello everybody, my problem was:
I was trying to set a temperature to a velocity inlet in my problem according to the average temperature of the air leaving the domain from a pressure outlet. I searched through the forum to find a solution to this but I could not find a clear answer. Now I managed to write a parallel UDF (for 8 cpus) as a remedy to this problem. Hope this helps if you have the same problem:

Code:

#include "udf.h"

/* Average temperature is calculated and share with other CPUs */

DEFINE_ADJUST(average_exit_temp, domain)
{
#if !RP_HOST    /*  either serial or compute node process is involved */

        face_t f;
        real sum_temp=0.0; 
        int nfaces = 0, i;
        int ID  = 21;        //on_hava_cikisi
        int ID0 = 31;        //uflec_orta_sag
        int ID1 = 30;        //uflec_orta_sol
        int ID2 = 28;        //uflec_sag
        int ID3 = 29;        //uflec_sag_cam
        int ID4 = 26;        //uflec_sol
        int ID5 = 27;        //uflec_sol_cam

        /* Determination of zone IDs */
        Thread *thread  = Lookup_Thread(domain, ID );
        Thread *thread0 = Lookup_Thread(domain, ID0);
        Thread *thread1 = Lookup_Thread(domain, ID1);
        Thread *thread2 = Lookup_Thread(domain, ID2);
        Thread *thread3 = Lookup_Thread(domain, ID3);
        Thread *thread4 = Lookup_Thread(domain, ID4);
        Thread *thread5 = Lookup_Thread(domain, ID5);

        /* Calculation of average exit temperature and sending to other nodes. The thread on which the average temperature is calculated is on node zero*/
        if(I_AM_NODE_ZERO_P)
        {
                begin_f_loop(f, thread)
                {
                        nfaces = nfaces + 1;
                        sum_temp = sum_temp + F_T(f,thread);
                }
                end_f_loop(f,thread)
                sum_temp = sum_temp/nfaces;
                Message("Node %d :  Average exit temperature = %f\n", myid, sum_temp);
                compute_node_loop_not_zero(i)
                {
                        PRF_CSEND_REAL(i, &sum_temp, 1, myid);
                }
        }

        /* Reception of average temperature from node zero */
        if(! I_AM_NODE_ZERO_P)
        {
                PRF_CRECV_REAL(0, &sum_temp, 1, 0);
                Message("Node %d : Average exit temperature = %f\n", myid, sum_temp);
        }

        /* User defined memory of thread0 are filled with sum_temp */
        begin_f_loop(f, thread0)
        {
                F_UDMI(f,thread0,0) = sum_temp;
        }
        end_f_loop(f,thread0)

        /* User defined memory of thread1 are filled with sum_temp */
        begin_f_loop(f, thread1)
        {
                F_UDMI(f,thread1,1) = sum_temp;
        }
        end_f_loop(f,thread1)

        /* User defined memory of thread2 are filled with sum_temp */
        begin_f_loop(f, thread2)
        {
                F_UDMI(f,thread2,2) = sum_temp;
        }
        end_f_loop(f,thread2)

        /* User defined memory of thread3 are filled with sum_temp */
        begin_f_loop(f, thread3)
        {
                F_UDMI(f,thread3,3) = sum_temp;
        }
        end_f_loop(f,thread3)

        /* User defined memory of thread4 are filled with sum_temp */
        begin_f_loop(f, thread4)
        {
                F_UDMI(f,thread4,4) = sum_temp;
        }
        end_f_loop(f,thread4)

        /* User defined memory of thread5 are filled with sum_temp */
        begin_f_loop(f, thread5)
        {
                F_UDMI(f,thread5,5) = sum_temp;
        }
        end_f_loop(f,thread5)

#endif
}

/* Boundary conditions are filled from user defined memory locations  */

DEFINE_PROFILE(temp_uflec_orta_sag,t,i)
{
        #if !RP_HOST
        face_t f;

        begin_f_loop(f,t)
        {
                F_PROFILE(f,t,i) = F_UDMI(f,t,0);
        }
        end_f_loop(f,t)
        #endif
}

DEFINE_PROFILE(temp_uflec_orta_sol,t,i)
{
        #if !RP_HOST
        face_t f;

        begin_f_loop(f,t)
        {
                F_PROFILE(f,t,i) = F_UDMI(f,t,1);
        }
        end_f_loop(f,t)
        #endif
}

DEFINE_PROFILE(temp_uflec_sag,t,i)
{
        #if !RP_HOST
        face_t f;

        begin_f_loop(f,t)
        {
                F_PROFILE(f,t,i) = F_UDMI(f,t,2);
        }
        end_f_loop(f,t)
        #endif
}

DEFINE_PROFILE(temp_uflec_sag_cam,t,i)
{
        #if !RP_HOST
        face_t f;

        begin_f_loop(f,t)
        {
                F_PROFILE(f,t,i) = F_UDMI(f,t,3);
        }
        end_f_loop(f,t)
        #endif
}

DEFINE_PROFILE(temp_uflec_sol,t,i)
{
        #if !RP_HOST
        face_t f;

        begin_f_loop(f,t)
        {
                F_PROFILE(f,t,i) = F_UDMI(f,t,4);
        }
        end_f_loop(f,t)
        #endif
}

DEFINE_PROFILE(temp_uflec_sol_cam,t,i)
{
        #if !RP_HOST
        face_t f;

        begin_f_loop(f,t)
        {
                F_PROFILE(f,t,i) = F_UDMI(f,t,5);
        }
        end_f_loop(f,t)
        #endif
}

alpemre


taff224 January 11, 2013 08:13

Hi CMA,

Not sure if you have solved this ourself but as I am 'borrowing' your code I thought I better reply with the fix.

I think the error you have is that you don't have an

end_f_loop(f,inlet_thread)

at the end of the DEFINE_ADJUST()

The parse error kicks in once you leave the function so it looks like it is relatign to the next function, but it isn't as I am only using our first function and also got the parse error.

Taff

CMA January 11, 2013 10:44

Hi Taff,

There are a couple of errors in the above code including the one you mentioned. For example where I calculate the area of a face, my code gets the vector area which creates another error. However, if you are only using the general structure of the code, you will be fine. I will be happy to hear your suggestions, if there's any.

Quote:

Originally Posted by taff224 (Post 401363)
Hi CMA,

Not sure if you have solved this ourself but as I am 'borrowing' your code I thought I better reply with the fix.

I think the error you have is that you don't have an

end_f_loop(f,inlet_thread)

at the end of the DEFINE_ADJUST()

The parse error kicks in once you leave the function so it looks like it is relatign to the next function, but it isn't as I am only using our first function and also got the parse error.

Taff


taff224 January 11, 2013 10:53

I'm struggling a bit to be honest, first attempt at UDFs and not quite sure I have it worked out.

For some reason my system isn't processing the begin_f_loop loop, I think because I am running multi-processor i have included the if(I_AM_ NODE_ZERO_P) and for some reason this is stopping the loop occuring, maybe the thread isn't in the node_zero processors area?

I'm sure I have something stupid going on.

If you have a working version would wouldn't mind sharing i'd like to have a look at it.

Thanks

Taff

Quote:

Originally Posted by CMA (Post 401403)
Hi Taff,

There are a couple of errors in the above code including the one you mentioned. For example where I calculate the area of a face, my code gets the vector area which creates another error. However, if you are only using the general structure of the code, you will be fine. I will be happy to hear your suggestions, if there's any.


massoudepsilon April 11, 2013 23:16

Thank you so much. It was really helpful

massoudepsilon April 18, 2013 02:39

Hi. Sorry just one question. How can I find that my outlet thread is on which node? because in this code you said that "The thread on which the average temperature is calculated is on node zero"

thank you so much.

alpemre April 23, 2013 11:43

answer to ehooi
 
Quote:

Originally Posted by ehooi (Post 289068)
1) Message("Node %d : Average exit temperature = %f\n", myid, sum_temp);
EH

This function is used by node 0 to write the average temperature which is calculated before and stored in sum_temp variable.

Quote:

Originally Posted by ehooi (Post 289068)
2)compute_node_loop_not_zero(i)
EH

This is to send a real variable from node 0 to all other nodes.

alpemre April 24, 2013 05:35

answer to massoud
 
Quote:

Originally Posted by massoudepsilon (Post 421409)
Hi. Sorry just one question. How can I find that my outlet thread is on which node? because in this code you said that "The thread on which the average temperature is calculated is on node zero"

I used this UDF years ago and I cannot remember now how I determined it :) Maybe it was so by chance and I found it out by trial and error.

Maybe you can try printing messages from different processors to determine it. Let me know if you learn how to do it.

massoudepsilon April 25, 2013 00:04

thank you so much dear alpemre, I found it by printing message.

mohammadamin67 September 9, 2013 05:01

pressure inlet boundary vs to value of mass flow rate
 
Hi all
I want to write a udf defining the value of the pressure in a pressure inlet boundary vs to value of mass flow rate passing that bouandary. can anyone help me or write this udf for me?
thanks.
M.Azizi

frodooon February 24, 2014 10:18

additionnal question...
 
Hi all,

This post is quite old, but maybe someone will be able to answer this little question.

In the code above, alpemre use user defined memory for each thread, and then assign sum_temp to each one, to finally re-read it in separate DEFINE_PROFILE loops.. This seems quite long, and I'm wondering (since my C skills are quite low..) if we couldn't just declare some global variable "sum_temp", assign to it the computed average temp, and then just use something like F_PROFILE(f,t,i) = sum_temp; . So no more UDM needed. Would that work ?

Thank you,


All times are GMT -4. The time now is 03:24.