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/)
-   -   Segmentation error with DEFINE_SOURCE (https://www.cfd-online.com/Forums/fluent-udf/201889-segmentation-error-define_source.html)

eng.masoud May 14, 2018 18:04

Segmentation error with DEFINE_SOURCE
 
Hi fellows,
I am simulating a 2phase problem (liquid and air) that liquid phase comes from inlet and depositing on wall substrate. I am using heat source to apply and make it molten.
I defined the code as below, to select only second phase cells and apply on them but after starting Ive got the segmentation error. Would you please guide me on this regard.

Thanks


#include "udf.h"
DEFINE_SOURCE(heatsource, c, thsec, dS, eqn)
{
real x[ND_ND], source, current_time;
real xc; /* xc - x coordinate */
real yc; /* yc - y coordinate */

Thread *thmix;
thsec = THREAD_SUB_THREAD(thmix,3);

C_CENTROID(x,c,thsec);
xc=x[0]; /* the x coordinate of the cell - 0th component of vector x */
yc=x[1]; /* the y coordinate of the cell - 1st component of vector x */

if (xc>=0.0045 & xc<=0.0077 & yc>=-0.001 & yc<=0.0029)

{
source=10e11;}

else {
source=0;}

dS[eqn] = 0.0; /* the source does not depend on temperature so dS = 0 */

return source;

}

pakk May 15, 2018 05:43

Your problem is in the line
Code:

thsec = THREAD_SUB_THREAD(thmix,3);
Up until that point, Fluent tells you that it wants to know the source corresponding to thread 'thsec' as input for this function, and you told the compiler to reserve memory for the thread 'thmix'.
And in this line of code, you tell Fluent to change 'thsec' (which makes no sense), and that it should be changed into something that depends on 'thmix', but you did not tell Fluent yet what 'thmix' is.

I have no idea what you try to accomplish with this line of code. It looks like your code becomes error-free if you simply remove this line, but then it might not do what you want it to do.

annan May 15, 2018 07:40

Hey eng.masoud,

I guess you're adding the source term to an equation defined for the mixture thread, so the thread *thmix that appears here : DEFINE_SOURCE(heatsource, c, thmix, dS, eqn) is actually your mixture thread.
If you want to work on the secondary phase only, you need to change the following code lines :
"Thread *thmix;
thsec = THREAD_SUB_THREAD(thmix,3);"
with :
"Thread *thsec;
thsec = THREAD_SUB_THREAD(thmix,1);"
index 1 corresponds to the secondary phase while index 0 corresponds to the primary phase.
I think those are the lines that cause segmentation fault, because you were trying to redefine the mixture thread which Fluent already knows with a phase thread.

Hope this will help.
Good luck
Annan

eng.masoud May 15, 2018 10:57

Hi Annan
Thanks for your response.
Ive applied yr mentioned modifications but still doesn't work (segmentation error was solved). As you can see from attached picture, I want to apply the heat only on 2nd phase cells coming from inlet. On this regard, I defined a rectangular area as judgment to apply the heat on the 2nd phase cells inside of this area. But instead of heating up the specific phase, heat was applied on the whole rectangular area.


#include "udf.h"
DEFINE_SOURCE(heatsource, c, thmix, dS, eqn)
{
real x[ND_ND], source, current_time;
real xc; /* xc - x coordinate */
real yc; /* yc - y coordinate */

Thread *thsec;
thsec = THREAD_SUB_THREAD(thmix,1);

C_CENTROID(x,c,thsec);
xc=x[0]; /* the x coordinate of the cell - 0th component of vector x */
yc=x[1]; /* the y coordinate of the cell - 1st component of vector x */


if (xc>=0.0045 & xc<=0.0077 & yc>=-0.001 & yc<=0.0029)

{
source=10e11;}

else {
source=0;}

dS[eqn] = 0.0; /* the source does not depend on temperature so dS = 0 */

return source;
}

https://drive.google.com/file/d/1Ol3PFxu2RA_WnJqSP_ASaXvt3JGNStq4/view?usp=sharing[/URL]

obscureed May 15, 2018 11:04

Hi eng.masoud,

I agree with annan's comments. (And, currently, you don't need to delve into sub-threads, because centroid information will be in thmix, the supplied superthread or mixture thread.)

Looking ahead, though, a few more comments:
(1) The standard C way to combine logical tests using AND is "&&" not "&". You might find that "&" works because of some integer bit-related machinery, but "&&" is the correct way.
(2) Your constant source rate of 10e11 seems very high for typical applications. It has units W/m3. For example, if the material has density 1000 kg/m3 and specific heat capacity 2000 J/(kg.K), then it would take a fraction of a millisecond to increase temperature by 100K. Maybe this is correct for your application, but please can you check this? You might find that convergence is easier if you drop the constant by several orders of magnitude, even if only for debugging.
(3) You have a real variable called "current_time". Perhaps you know that there is a built-in macro "CURRENT_TIME", which you might be able to use instead. It is always a double. I used to believe that it was always present on all compute-nodes, and could be used repeatedly without the expense of an RP_Get_Double every time, but now I am not so sure.

Good luck,
Ed

eng.masoud May 15, 2018 11:08

Hi Pakk
thanks for your response. For better elaboration plz have a look at attached picture and let me know your opinion on this regard.

cheers,

eng.masoud May 15, 2018 11:22

Quote:

Originally Posted by obscureed (Post 692404)
Hi eng.masoud,

I agree with annan's comments. (And, currently, you don't need to delve into sub-threads, because centroid information will be in thmix, the supplied superthread or mixture thread.)

Looking ahead, though, a few more comments:
(1) The standard C way to combine logical tests using AND is "&&" not "&". You might find that "&" works because of some integer bit-related machinery, but "&&" is the correct way.
(2) Your constant source rate of 10e11 seems very high for typical applications. It has units W/m3. For example, if the material has density 1000 kg/m3 and specific heat capacity 2000 J/(kg.K), then it would take a fraction of a millisecond to increase temperature by 100K. Maybe this is correct for your application, but please can you check this? You might find that convergence is easier if you drop the constant by several orders of magnitude, even if only for debugging.
(3) You have a real variable called "current_time". Perhaps you know that there is a built-in macro "CURRENT_TIME", which you might be able to use instead. It is always a double. I used to believe that it was always present on all compute-nodes, and could be used repeatedly without the expense of an RP_Get_Double every time, but now I am not so sure.

Good luck,
Ed

Hi Ed
Thanks for your prompt response,
later on I will apply the heat source equation but this time for simplicity I just used constant value to make sure the correctness of my code.


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