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/)
-   -   UDF temperature gradient (https://www.cfd-online.com/Forums/fluent-udf/150785-udf-temperature-gradient.html)

perun333 March 29, 2015 15:44

UDF temperature gradient
 
Hi guys

I would like to create UDF for computing temperature gradient (in time and space). I have 2D axysimmetric model. Some tips? I havent enough experiencies with c++.

`e` March 29, 2015 16:45

Fluent already has a macro for the temperature gradient: C_T_G(c,t). Read the UDF manual for details.

perun333 March 29, 2015 17:07

Quote:

Originally Posted by `e` (Post 538869)
Fluent already has a macro for the temperature gradient: C_T_G(c,t). Read the UDF manual for details.


Could you be more specific?

`e` March 29, 2015 17:21

Read section "3.2.3.7. Gradient (G) and Reconstruction Gradient (RG) Vector Macros" on page 221 of the ANSYS Fluent 15.0 UDF Manual (may have different page number and/or section for different versions of Fluent).

perun333 March 29, 2015 18:03

Quote:

Originally Posted by `e` (Post 538873)
Read section "3.2.3.7. Gradient (G) and Reconstruction Gradient (RG) Vector Macros" on page 221 of the ANSYS Fluent 15.0 UDF Manual (may have different page number and/or section for different versions of Fluent).

is it correct?

#include "udf.h"
DEFINE_ON_DEMAND(Temp_grad_ondemand)
{
Domain *d;
cell_t c;
Thread *t;
d = Get_Domain(1);

thread_loop_c(t,d)
{
begin_c_loop(c,t)
{

C_UDMI(c,t,1) = NV_MAG(C_T_G(c,t));

}
end_c_loop(c,t)
}

}

`e` March 29, 2015 18:25

Your code appears correct if you want the magnitude of the temperature gradient. Have you tried compiling this UDF, and do you get expected results?

perun333 March 29, 2015 18:38

Quote:

Originally Posted by `e` (Post 538885)
Your code appears correct if you want the magnitude of the temperature gradient. Have you tried compiling this UDF, and do you get expected results?


thank you. Do you know how it looks with macro DEFINE_ADJUST?

Thanks

`e` March 29, 2015 18:51

The code for determining the temperature gradient magnitude with a DEFINE_ADJUST macro would look very similar to your DEFINE_ON_DEMAND code. Again, have a read of the UDF manual and return if you have any precise questions.

perun333 March 31, 2015 05:27

Quote:

Originally Posted by `e` (Post 538887)
The code for determining the temperature gradient magnitude with a DEFINE_ADJUST macro would look very similar to your DEFINE_ON_DEMAND code. Again, have a read of the UDF manual and return if you have any precise questions.

I tried to use ADJUST, but didnt work. Any Idea?

# include "udf.h"
# define domain_ID 2
DEFINE_ADJUST(adjust_gradient, domain)
{
Thread *t;
cell_t c;
face_t f;

domain = Get_Domain(domain_ID);

/* Fill UDS with the variable. */
thread_loop_c (t,domain)
{
begin_c_loop (c,t)
{
C_UDMI(c,t,1) = NV_MAG(C_T_G(c,t));
}
end_c_loop (c,t)
}
}

`e` March 31, 2015 06:04

What are the errors and when do they occur?

'domain' is a pointer to the domain that the adjust function is to be applied. Typically you only have one domain and use the macro Get_Domain(1) if you require the pointer for it (but it's provided with DEFINE_ADJUST). Do you have multiple domains or are you confusing domains with cell zones?

perun333 March 31, 2015 06:27

1 Attachment(s)
Quote:

Originally Posted by `e` (Post 539179)
What are the errors and when do they occur?

'domain' is a pointer to the domain that the adjust function is to be applied. Typically you only have one domain and use the macro Get_Domain(1) if you require the pointer for it (but it's provided with DEFINE_ADJUST). Do you have multiple domains or are you confusing domains with cell zones?

It said receive fatal signal. My steps are:

1.Star fluet
2. Sets bounday conditions
3. Interpred program
4. Set funkcions hook =>set it in adjust
5. Star calculation and then error

Thx

`e` March 31, 2015 07:12

I'll be specific, remove the following line of code:

Code:

domain = Get_Domain(domain_ID);
If you're still getting fatal errors crashing Fluent, ensure you have enabled the user defined memory.

perun333 March 31, 2015 07:25

1 Attachment(s)
Quote:

Originally Posted by `e` (Post 539202)
I'll be specific, remove the following line of code:

Code:

domain = Get_Domain(domain_ID);
If you're still getting fatal errors crashing Fluent, ensure you have enabled the user defined memory.

I removed line of code and define memory 1 but still fatal error. :(

pakk March 31, 2015 07:33

Quote:

Originally Posted by perun333 (Post 539207)
I removed line of code and define memory 1 but still fatal error. :(

Yes, you define 1 memory (so location 0) and try to store things in memory location 1! That does not work.

perun333 March 31, 2015 07:36

Quote:

Originally Posted by pakk (Post 539209)
Yes, you define 1 memory (so location 0) and try to store things in memory location 1! That does not work.

So what you recomend?

pakk March 31, 2015 07:39

I recommend that you think about how to solve the problem.

The current situation: you use a memory location that is not defined.
The wanted situation: you use a memory location that is defined.

How would you go from the current situation to the wanted situation? There are two strategies for that.

perun333 March 31, 2015 07:53

Quote:

Originally Posted by pakk (Post 539214)
I recommend that you think about how to solve the problem.

The current situation: you use a memory location that is not defined.
The wanted situation: you use a memory location that is defined.

How would you go from the current situation to the wanted situation? There are two strategies for that.

Sorry I dont have much experiencies with programing in C and really dont know why I have to set this memory:(. I computing heat transfer in 2D axisymetric model with conditions:
- max. time gradient 20°C/h during heating
- max. time gradient 30°C/h during cooling
- max. space temperature gradient 60°C/m

I have found that I have to use type define_adjust and macro C_T_G. So I tried to use this after some upgrades:

# include "udf.h"
DEFINE_ADJUST(adjust_gradient, domain)
{
Thread *t;
cell_t c;
face_t f;
/* Fill UDS with the variable. */
thread_loop_c (t,domain)
{
begin_c_loop (c,t)
{
C_UDMI(c,t,1) = NV_MAG(C_T_G(c,t));
}
end_c_loop (c,t)
}
}

pakk March 31, 2015 08:04

Quote:

Originally Posted by perun333 (Post 539219)
Sorry I dont have much experiencies with programing in C and really dont know why I have to set this memory:(. I computing heat transfer in 2D axisymetric model with conditions:
- max. time gradient 20°C/h during heating
- max. time gradient 30°C/h during cooling
- max. space temperature gradient 60°C/m

I have found that I have to use type define_adjust and macro C_T_G. So I tried to use this after some upgrades:

# include "udf.h"
DEFINE_ADJUST(adjust_gradient, domain)
{
Thread *t;
cell_t c;
face_t f;
/* Fill UDS with the variable. */
thread_loop_c (t,domain)
{
begin_c_loop (c,t)
{
C_UDMI(c,t,1) = NV_MAG(C_T_G(c,t));
}
end_c_loop (c,t)
}
}

My question has nothing to do with programming in c. It has to do with reading skills and basic logic.

The current situation: you use a memory location that is not defined.
The wanted situation: you use a memory location that is defined.

How would you go from the current situation to the wanted situation? There are two strategies for that. This is really not a super-hard question.

perun333 March 31, 2015 09:16

Quote:

Originally Posted by pakk (Post 539223)
My question has nothing to do with programming in c. It has to do with reading skills and basic logic.

The current situation: you use a memory location that is not defined.
The wanted situation: you use a memory location that is defined.

How would you go from the current situation to the wanted situation? There are two strategies for that. This is really not a super-hard question.

Sorry, I thought that this forum helps to sort out problems. But I just only see cynic approach, sorry :/

pakk March 31, 2015 09:24

Quote:

Originally Posted by perun333 (Post 539243)
Sorry, I thought that this forum helps to sort out problems. But I just only see cynic approach, sorry :/

It is bad that you see it that way. The way I see it, I am trying to help you understand the problem, and help you get to a solution.

You give the impression that you don't want to understand anything, but that you just want to hear what you should do to get rid of your error. Is that what you want? In that case, you'll have to hire me, I don't do those jobs for free.

`e` March 31, 2015 17:40

We're both trying to help you out here Ondrej, and have both read your problem, understood it and given guidance. I agree with pakk that these forums are for learning and troubleshooting Fluent issues, not simply doing the work for you. It appears you ignored my comment on the domain pointer solution back in this post, and I had to repeat myself before you fixed that issue. For every solution we provide in threads, I'm sure there's a number of others who stumble across the same issue and find these threads through search engines.

That being said, I think the key issue you face is that computers like to start from zero; for example, you access the fourth element of an array with x[3] and not with x[4]. Applying this logic to your C_UDMI macro: try accessing C_UDMI(c,t,0).

perun333 March 31, 2015 18:19

Quote:

Originally Posted by `e` (Post 539329)
We're both trying to help you out here Ondrej, and have both read your problem, understood it and given guidance. I agree with pakk that these forums are for learning and troubleshooting Fluent issues, not simply doing the work for you. It appears you ignored my comment on the domain pointer solution back in this post, and I had to repeat myself before you fixed that issue. For every solution we provide in threads, I'm sure there's a number of others who stumble across the same issue and find these threads through search engines.

That being said, I think the key issue you face is that computers like to start from zero; for example, you access the fourth element of an array with x[3] and not with x[4]. Applying this logic to your C_UDMI macro: try accessing C_UDMI(c,t,0).

I know sorry for this. I tried to apply your recomendations but with the same result:

Updating solution at time level N... done.
iter energy delta_time time time/iter

Error: received a fatal signal (Segmentation fault).

Error: received a fatal signal (Segmentation fault).
Error Object: #f


This is my updated code:
#include "udf.h"

DEFINE_ADJUST(teste, domain)
{
Thread *t;
cell_t c;

thread_loop_c(t, domain)
{
begin_c_loop_all(c, t)
{
C_UDMI(c,t,0) = NV_MAG(C_T_G(c,t));
}
end_c_loop_all(c, t)
}
}



Then I tried it from another side, but with same resault:

#include "udf.h"
DEFINE_ADJUST(max_C_T_G, domain)
{
cell_t c;
Thread *ct;
real maxT;
maxT = 0.0;
thread_loop_c(ct,domain)
{
begin_c_loop(c,ct)
{
if (C_T_G(c,ct)[0] > maxT)
{
maxT = C_T_G(c,ct)[0];
}
}
end_c_loop(c,ct)
}
printf("Maximum temperature = %f \n", maxT);
}
Sorry for it.

`e` March 31, 2015 18:27

Try emptying your DEFINE_ADJUST UDF and only have a message, for example:

Code:

#include "udf.h"
DEFINE_ADJUST(max_C_T_G, domain)
{
    Message("Hello...\n");
}

If you're not receiving the message above to your screen then you're not re-compiling the UDF correctly and should read my post on how to re-compile UDFs.

Lastly, use Message() instead of printf().

perun333 March 31, 2015 18:45

Quote:

Originally Posted by `e` (Post 539338)
Try emptying your DEFINE_ADJUST UDF and only have a message, for example:

Code:

#include "udf.h"
DEFINE_ADJUST(max_C_T_G, domain)
{
    Message("Hello...\n");
}

If you're not receiving the message above to your screen then you're not re-compiling the UDF correctly and should read my post on how to re-compile UDFs.

Lastly, use Message() instead of printf().

I tried your example and it works, so Im compiling correctly. I think that problem is somewhere inside code...

`e` March 31, 2015 18:52

Work through your code line by line (adding one by one from the working simple message UDF) until you determine which line is causing your error and then fix it, good luck!

perun333 April 1, 2015 03:21

Quote:

Originally Posted by `e` (Post 539343)
Work through your code line by line (adding one by one from the working simple message UDF) until you determine which line is causing your error and then fix it, good luck!

I think thats correct:

#include "udf.h"
DEFINE_ADJUST(max_C_T_G, domain)
{
cell_t c;
Thread *ct;
real maxT;
maxT = 0.0;
thread_loop_c(ct,domain)
{
begin_c_loop(c,ct)
{
if (C_T_G(c,ct)[0] > maxT)
{
maxT = C_T_G(c,ct)[0];
}
}
end_c_loop(c,ct)
}
printf("Maximum temperature = %f \n", maxT);
}

But problem is maybe with axisymetric definition.

pakk April 1, 2015 03:59

The problem could be that your temperature gradient is not yet defined. Is this on the first iteration? If so, let your simulation run for a few time steps before you use this UDF.

And a minor detail: replace
Code:

printf("Maximum temperature = %f \n", maxT);
by
Code:

Message("Maximum temperature gradient = %f K/m.\n", maxT);

perun333 April 1, 2015 15:57

Quote:

Originally Posted by pakk (Post 539402)
The problem could be that your temperature gradient is not yet defined. Is this on the first iteration? If so, let your simulation run for a few time steps before you use this UDF.

And a minor detail: replace
Code:

printf("Maximum temperature = %f \n", maxT);
by
Code:

Message("Maximum temperature gradient = %f K/m.\n", maxT);

Hi Pakk,

I tried your opinions:
-change the line for message in udf
-run transient calculate >>calculation converget
-compiled upgraded udf
-Adjust hook
-calculate
-error:
Updating solution at time level N... done.
iter energy delta_time time time/iter
! 45 solution is converged
45 5.6044e-07 1.0000e+01 1.3000e+02 0:00:02 20

Error: received a fatal signal (Segmentation fault).

Error: received a fatal signal (Segmentation fault).
Error Object: #f

pakk April 2, 2015 04:05

Hmm, then I don't know...

You could try to see if the gradient is the problem by replacing C_T_G(c,ct)[0] by C_T(c,ct). But if the gradient is really the problem, I would not know how to solve that.

If C_T(c,ct) also does not work, the axisymmetry might be the problem. But then, I also would not know how to solve that.

perun333 April 2, 2015 04:32

Quote:

Originally Posted by pakk (Post 539613)
Hmm, then I don't know...

You could try to see if the gradient is the problem by replacing C_T_G(c,ct)[0] by C_T(c,ct). But if the gradient is really the problem, I would not know how to solve that.

If C_T(c,ct) also does not work, the axisymmetry might be the problem. But then, I also would not know how to solve that.

With maximal temperature macro C_T it works

perun333 April 12, 2015 15:45

Quote:

Originally Posted by pakk (Post 539246)
It is bad that you see it that way. The way I see it, I am trying to help you understand the problem, and help you get to a solution.

You give the impression that you don't want to understand anything, but that you just want to hear what you should do to get rid of your error. Is that what you want? In that case, you'll have to hire me, I don't do those jobs for free.

Hi pakk,

I try to compiled and adjusted udf (which have to work) from ansys forum.
Here is code:
#include "udf.h"
DEFINE_ADJUST(total_dissipation, domain)
{
cell_t c;
Thread *ct;
real total_diss;

total_diss = 0.0; /* Initialise total_diss before the loop */

thread_loop_c(ct,domain)
{
begin_c_loop(c,ct)
{
total_diss += C_D(c,ct) * C_VOLUME(c,ct); /* Use += to sum up values */
}
end_c_loop(c,ct)
}
Message("Volume integral of turbulent dissipation = %f \n", total_diss);
}



And I have still same resault:

fatal error (segmentation fault)

I tried to compiled simple udf which contains only hello world and it works. Have you any idea where is problem?

bmahnic May 9, 2016 10:36

Hi!

I think that the solution to your problem is defining the thread value as equal to DT_THREAD(dt).
The reason is in the fact that you are using the dynamic mesh. In that case normal thread definition does not work. Consequentially, your face looping macro aborts the simulation.

Regards, Marko.

wiki123 July 9, 2021 14:03

@OP.

Actually what was being conveyed to you was that by default the first memory location is 0 in Ansys fluent. You defined one memory means you defined 0 serial number memory. It starts with 0.

But you were storing the gradient in the user defined memory 1 which was not existing.


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