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 fault while using DEFINE_PROPERTY macro (https://www.cfd-online.com/Forums/fluent-udf/198029-segmentation-fault-while-using-define_property-macro.html)

leo1993 January 24, 2018 08:57

SEGMENTATION fault while using DEFINE_PROPERTY macro
 
Dear all,

I am relatively new to programming in c, and completely new in writing UDF's for Fluent. Now, I want to write an UDF that defines the thermal conductivity for the primary phase and the secondary phase. I am using the DEFINE_PROPERTY macro to do this. I have tried this in the following way:
Code:

/*********************************************************************
  UDF that simulates the correlation for the conductivity proposed by Shen et al.                                   
**********************************************************************/
#include "udf.h"

DEFINE_PROPERTY(cell_conductivity,cell,thread)
{
        /* Variable fluid properties for water-vapor subcooling @353.15 K as defined by Mills */
        real T_SAT                                                = 373.15;
        real k_v                                                = 24.8e-3;
        real k_l                                                = 0.674;
        real ktc_;
        /* Enhancement factor n, as proposed by Shen. Enhances the energy transportation in the two-phase region. */
        real n                                                        = 1.;
       
        Domain *subdomain_gas;
        Domain *subdomain_liq;
        int phase_domain_index_gas;               
        int phase_domain_index_liq;
        Thread *gas;
        Thread *liq;
        real temp;
       
        subdomain_gas                                        = Get_Domain(2);                                                                                /* retrieve pointer to primary phase domain */
        subdomain_liq                                        = Get_Domain(3);                                                                                /* retrieve pointer to secondary phase domain */
        phase_domain_index_gas                                = PHASE_DOMAIN_INDEX(subdomain_gas);                               
        phase_domain_index_liq                                = PHASE_DOMAIN_INDEX(subdomain_liq);
        gas                                                = THREAD_SUB_THREAD(thread, phase_domain_index_gas);
        liq                                                = THREAD_SUB_THREAD(thread, phase_domain_index_liq);
        temp                                                = C_T(cell,thread);


 
        if (temp >= T_SAT){
                ktc_ = C_VOF(cell,liq) * k_l + C_VOF(cell,gas) * k_v;
        }
        else {
                ktc_ = C_VOF(cell,liq) * k_l + n * C_VOF(cell,gas) * k_v;
        }
        return ktc_;
}

The problem is the following. I am able to compile the code and to load it, but when I specify the user defined conductivity in the material properties, I receive a segmentation fault.
I would be very happy if someone could help me. Also, feel free to give any tips on programming if you like.

Cheers!

pakk January 25, 2018 03:43

This UDF needs some data to work, specifically the temperature and the VOF.
If you attach this UDF before initializing data, and Fluent wants to calculate the conductivity, Fluent will try to get the temperature/VOF, and fail, and give an error.

The simplest solution: initialize your data first (with a constant conductivity), and attach the UDF after that.

leo1993 January 25, 2018 03:59

I tried this, but this didn't work. I still receive the segmentation error. Any other suggestions?

leo1993 January 25, 2018 05:51

After doing some tests, I found out that C_VOF(c,t), returns the volume fraction of the secondary phase (seems counter-intuitive). Then I could fix the problem in the following way.
Code:

/*********************************************************************
  UDF that simulates the correlation for the conductivity proposed by Shen et al.                                   
**********************************************************************/
#include "udf.h"

DEFINE_PROPERTY(cell_conductivity,cell,thread)
{
        /* Variable fluid properties for water-vapor subcooling @353.15 K as defined by Mills */
        real T_SAT                                                      = 373.15;
        real k_v                                                = 0.0248;
        real k_l                                                = 0.674;
        real ktc_;
        /* Enhancement factor n, as proposed by Shen. Enhances the energy transportation in the two-phase region. */
        real n                                                        = 1.;
        real temp;       
        temp                                                        = C_T(cell,thread);
       
    ktc_ = 0.;
        if (temp >= T_SAT)
        {
                ktc_ = C_VOF(cell,thread) * k_l + (1-C_VOF(cell,thread)) * k_v;
        }
        else
        {
                ktc_ = C_VOF(cell,thread) * k_l + n * (1-C_VOF(cell,thread)) * k_v;
        }
        return ktc_;
}

I checked the solution with hand calculations, and it worked out! However, I am still wondering what went wrong in the first code.

pakk January 25, 2018 05:58

Eliminate the possible problems...

Does it work if you remove temperature?
Code:

DEFINE_PROPERTY(cell_conductivity,cell,thread)
{
    /* Variable fluid properties for water-vapor subcooling @353.15 K as defined by Mills */
        real T_SAT                        = 373.15;
    real k_v                        = 24.8e-3;
    real k_l                        = 0.674;
    real ktc_;
    /* Enhancement factor n, as proposed by Shen. Enhances the energy transportation in the two-phase region. */
    real n                            = 1.;
   
    Domain *subdomain_gas;
    Domain *subdomain_liq;
    int phase_domain_index_gas;       
    int phase_domain_index_liq;
    Thread *gas;
    Thread *liq;
    real temp;
   
    subdomain_gas                    = Get_Domain(2);                                        /* retrieve pointer to primary phase domain */
    subdomain_liq                    = Get_Domain(3);                                        /* retrieve pointer to secondary phase domain */
    phase_domain_index_gas                    = PHASE_DOMAIN_INDEX(subdomain_gas);               
    phase_domain_index_liq                    = PHASE_DOMAIN_INDEX(subdomain_liq);
        gas                        = THREAD_SUB_THREAD(thread, phase_domain_index_gas);
        liq                        = THREAD_SUB_THREAD(thread, phase_domain_index_liq);
    temp                            = 300.;    /* <-- change is here! */


 
    if (temp >= T_SAT){
        ktc_ = C_VOF(cell,liq) * k_l + C_VOF(cell,gas) * k_v;
    }
    else {
        ktc_ = C_VOF(cell,liq) * k_l + n * C_VOF(cell,gas) * k_v;
    }
    return ktc_;
}

If so, your problem is with temperature. If not, simplify it further. (Replace C_VOF by a constant value, and so on.)
Knowing which part of the code is really given problems, makes it much easier to find the solution.

leo1993 January 26, 2018 05:56

The problem lies in C_VOF(cell, liq) or C_VOF(cell, gas). Now I have tried to fix this the following way.
Code:

#include "udf.h"

DEFINE_PROPERTY(cell_conductivity,c,thread)

{
        /* Variable fluid properties for water-vapor subcooling @353.15 K as defined by Mills */
    real T_SAT                                                = 373.1;
        real k_v                                                = 0.0248;
        real k_l                                                = 0.674;
        real ktc_;
       
        /* Enhancement factor n, as proposed by Shen. Enhances the energy transportation in the two-phase region. */
        real n                                                        = 1.;
       
        real temp                                                = C_T(c,thread);
        Domain *mix_domain = Get_Domain(1);
        Thread *mix_thread;
        Thread **pt;

        mp_thread_loop_c(mix_thread, mix_domain, pt)
        {       
                ktc_ = 0.;
                if (temp >= T_SAT)
                {
                        ktc_ = C_VOF(c,pt[1]) * k_l + C_VOF(c,pt[0]) * k_v;
                }
                else
                {
                        ktc_ = C_VOF(c,pt[1]) * k_l + n * C_VOF(c,pt[0]) * k_v;
                }
                Message(": %g\n", C_VOF(c,pt[0]));
        }
       
        return (ktc_);
}

However, the weird thing is that, when initializing, the message produces 1's, 0's and the density of the primary phase. While I think it should only produce 1's. FYI, when initialized, there is only the primary phase in the domain (vapor).
After doing some other tests, I found out that C_VOF(c,pt[1])) always returns '1', and C_VOF(c,pt[0]) always returns '0'. But why? It should return the volume fraction right?
Any other suggestions to obtain the volume fraction of a cell?


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