CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   Fluent UDF and Scheme Programming (https://www.cfd-online.com/Forums/fluent-udf/)
-   -   Writing udf regarding adjacent cells (https://www.cfd-online.com/Forums/fluent-udf/218715-writing-udf-regarding-adjacent-cells.html)

vkumar12 July 1, 2019 14:26

Writing udf regarding adjacent cells
 
I have a problem where a mass source term is dependent on the temperature of the adjacent cells. The equation which i want to put in the udf form is given like this:

Smj = (A * ▲ Tj)/ Vj

Where Smj = Mass source of a species in jth cell
A - constant
▲ Tj - Temperature diff between two adjacent cells
Vj - Volume of Jth cell

I think i understand the basic udf, and I dont know how do i put temperature of two adjacent cells in the udf. If any one help writing a UDF , i would be very grateful

I am trying to write but stuck, It may be simple for those who are experienced in this.

DEFINE_SOURCE(_mass, c, t, dS, eqn)
{
real source;
real temp = C_T(c,t);
if (673<=C_T(c,t)<=1073)
source = -----------------------???
else
source = 0;
dS[eqn] = 0;
return source;
}

Thanks
Varun

AlexanderZ July 1, 2019 21:53

I didn't test it. May be it should be something like this:
Code:

#include "udf.h"

DEFINE_ADJUST(calculate_source, d)
{
        face_t f
        cell_t c,c0, c1 = -1;
        Thread *t,*t0, *t1 = NULL;
        real Temp0, Temp1;
        // get temperature
        thread_loop_f (t,domain)
        {
                begin_f_loop (f,t)
                {
                        c0 = F_C0(f,t);
                        t0 = F_C0_THREAD(f,t);
                        if (BOUNDARY_FACE_THREAD_P(t)) /*Most face values will be available*/
                        {
                                // you should define something here for case, when there is no more adjusted cells (external boundary)
                                Temp0 = C_T(c0,t0);
                                Temp1 = 0.0;
                        }
                        else{       
                                c1 = F_C1(f,t); /* Get cell on other side of face */
                                t1 = F_C1_THREAD(f,t);
                                Temp0 = C_T(c0,t0);
                                Temp1 = C_T(c1,t1);
                        }
                        C_UDMI(c0,t0,0) = Temp0;
                        C_UDMI(c1,t1,1) = Temp1;
                }
                end_f_loop (f,t)
        }
        //calculate source
        thread_loop_c (t,domain)
        {
                begin_c_loop (c,t)
                {
                        C_UDMI(c,t,2) = C_UDMI(c,t,0) - C_UDMI(c,t,1);
                }
                end_c_loop (c,t)
        }
}

DEFINE_SOURCE(_mass, c, t, dS, eqn)
{
real source;
real temp = C_T(c,t);
if (673<=C_T(c,t)<=1073)
source = C_UDMI(c,t,2);
else
source = 0;
dS[eqn] = 0;
return source;
}

give your feedback here

best regards

vkumar12 July 2, 2019 10:19

Structural reference error
 
Thank you very much for the help.
However when I tried to interpret the udf, it is giving structure reference not implemented on the following line.

t0 = F_C0_THREAD(f,t);

Any idea why?

Thanks

AlexanderZ July 3, 2019 00:16

compile udf

best regards

vkumar12 July 15, 2019 16:12

Thank you for your help
I did download the visual studio and managed to compiled it. It was loaded successfully
I did assign 3 UDM for the memory and then hook the define adjust function in the fluent and ran the calculation, However it is giving me segmentation fault. Not sure why. I think it is to do with UDMI
Any help and input I would appreciate.
I feel I am close, but not there yet.
THank you very much for your time

AlexanderZ July 15, 2019 22:18

show your code and output logs always

if you are using code above than there was mistake
Code:

thread_loop_f (t,domain)
to be
Code:

thread_loop_f (t,d)
best regards

vkumar12 July 16, 2019 08:49

Hello
I actually did that change and it is after the fact I am getting the segmentation fault
Here is complete code. The segmentation fault occurs when I ran the calculation with "Define adjust function hook up". If I don't hook the define adjust , it works (at least the iteration parts)




#include "udf.h"
DEFINE_ADJUST(calculate_source, d)
{
face_t f;
cell_t c,c0, c1 = -1;
Thread *t,*t0, *t1 = NULL;
real Temp0, Temp1;
// get temperature
thread_loop_f (t,d)
{
begin_f_loop (f,t)
{
c0 = F_C0(f,t);
t0 = F_C0_THREAD(f,t);
if (BOUNDARY_FACE_THREAD_P(t)) /*Most face values will be available*/
{
// you should define something here for case, when there is no more adjusted cells (external boundary)
Temp0 = C_T(c0,t0);
Temp1 = 0.0;
}
else{
c1 = F_C1(f,t); /* Get cell on other side of face */
t1 = F_C1_THREAD(f,t);
Temp0 = C_T(c0,t0);
Temp1 = C_T(c1,t1);
}
C_UDMI(c0,t0,0) = Temp0;
C_UDMI(c1,t1,1) = Temp1;
}
end_f_loop (f,t)
}
//calculate source
thread_loop_c (t,d)
{
begin_c_loop (c,t)
{
C_UDMI(c,t,2) = C_UDMI(c,t,0) - C_UDMI(c,t,1);
}
end_c_loop (c,t)
}
}
DEFINE_SOURCE(Ch4_mass, c, t, dS, eqn)
{
real source;
real temp = C_T(c,t);
if (673<=C_T(c,t)<=1073)
source = (-0.000000081917+2*0.00000000011114*C_T(c,t)-3*0.000000000000045649*C_T(c,t)*C_T(c,t))*C_UDMI(c ,t,2)/C_VOLUME(c,t);
else
source = 0;
if (source<0)
source = 0;
else
source = source;
dS[eqn] = 0;
return source;
}


P.S I changed the Define Adjust code, in order to see where the problem is to


Temp0 = C_T(c0,t0);
Temp1 = C_T(c1,t1);
}
}
end_f_loop (f,t)
}
//calculate source
thread_loop_c (t,d)
{
begin_c_loop (c,t)
{
C_UDMI(c,t,0) = C_T(c0,t0)-C_T(c1,t1);
}
end_c_loop (c,t)
}
}
And it works but I belive it may be wrong and not doing what it is supposed to ?
Thank you for you time

AlexanderZ July 16, 2019 22:08

was
Code:

else{
c1 = F_C1(f,t); /* Get cell on other side of face */
t1 = F_C1_THREAD(f,t);
Temp0 = C_T(c0,t0);
Temp1 = C_T(c1,t1);
}
C_UDMI(c0,t0,0) = Temp0;
C_UDMI(c1,t1,1) = Temp1;
}

new
Code:

else{
c1 = F_C1(f,t); /* Get cell on other side of face */
t1 = F_C1_THREAD(f,t);
Temp0 = C_T(c0,t0);
Temp1 = C_T(c1,t1);
C_UDMI(c1,t1,1) = Temp1;
}
C_UDMI(c0,t0,0) = Temp0;
}

Also you should add initialization macro DEFINE_INIT
Code:

DEFINE_INIT(my_init_func,d)
{
cell_t c;
Thread *t;
/* loop over all cell threads in the domain */
thread_loop_c(t,d)
{
/* loop over all cells */
begin_c_loop(c,t)
{
C_UDMI(c,t,0) = 0.0;
C_UDMI(c,t,1) = 0.0;
C_UDMI(c,t,2) = 0.0;
}
end_c_loop_all(c,t)
}
}

best regards

vkumar12 July 23, 2019 10:51

Thank you very much for your help.
The new UDF you provide is working and compiled successfully.
P.s I used 2019 VS community version and Ansys 17.1
It was essential to open the Ansys workbench file with VS studio command prompt.

Thank you again for your kind help. I really appreciate.

vkumar12 August 1, 2019 10:21

4 Attachment(s)
Dear Alexander
I need your help on the same topic. As I mentioned earlier the UDF compiled properly but the values for the adjacent cells temperatures coming to be same and hence the C_UDMI 2 came to be zero and making all the results goes to zero.
I think it would be better if I explain my problem here in detail.
My model contains a solid porous body (fiber) which is 1/8" thick and 100" long and it is heated in a furnace. Fiber is travelling at a speed so I used MRF model to simulate that. Furnace is 7 zones and alls are heated electrically.
What I was trying to do is to calculate the mass fraction of gases coming out of the fiber. See the attached picture for the geometry (attachment 1) and meshing (attachment 2)
And that source term is dependent on how is the rate of fiber heating and hence I need the adjacent cell temperatures. I think I am still confused what are the adjacent cells in my problem and hence I highlighted in the attached picture.

Some observation after I calculation finishes:

When I plot C_UDMI (0) and C_UDMI (1) for the interior fiber with respect to direction of furnace length , it gives me a profile (attached) but only problem is the profile of C_UDMI (0) and C_UDMI (1) are exactly same. and hence C_UDMI 2 which is the difference of adjacent cells gives value of zero.



Thanks in advance for your help.

AlexanderZ August 2, 2019 00:28

Am I right, that adjusted cells your are interested in are cells of fiber only?

You want to get temperature difference between adjusted cells of fiber?

best regards

vkumar12 August 2, 2019 05:50

Yes that is correct.

AlexanderZ August 2, 2019 08:15

unfortunately have no idea now

best regards

vkumar12 August 2, 2019 08:29

I think it automatically chooses that domain when I put the source term in cell zone condition of fiber . So I believe it will automatically chooses the fiber domain to calculate the temp of adjacent cells .

In the meshing picture i attached , do you think my assumption of adjacent cell is right , it is the cells adjacent to the face of a particular cell right ?

AlexanderZ August 4, 2019 22:04

you may try to monitor temperatures only in fiber part (make it explicitly)

you should add following In DEFINE_ADJUST macro above begin_f_loop(f, thread){
Code:

int ID = 1;
/* Zone ID for wall-1 zone from Boundary Conditions task page */
Thread *thread = Lookup_Thread(domain, ID);

change 1 to id of interior-fiber in your model.
Go to GUI -> boundary conditions -> select interior-fiber -> under zone window you may check ID

best regards


All times are GMT -4. The time now is 15:49.