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 code problem about total value (https://www.cfd-online.com/Forums/fluent-udf/95954-udf-code-problem-about-total-value.html)

ypchen January 9, 2012 01:51

UDF code problem about total value
 
Hi, everybody, here is my code :

Code:

#include "udf.h"
#include "mem.h"
#include "math.h"
#include "stdio.h"
#define mass_alloy 0.295
 
DEFINE_ADJUST(adj_UDS,d)
{
 cell_t c ;
 Thread *t;
 real H_M,mass,sum ;
 thread_loop_c(t,d)
 {
  begin_c_loop(c,t)
  {
  mass= (C_UDSI(c,t,0)-8238)*C_VOLUME(c,t)*3.14159;
  H_M = (mass/mass_alloy)*432.4;
  C_UDMI(c,t,0) = H_M ;
  sum += sum C_UDMI(c,t,0) ;
  }
  end_c_loop(c,t)
 }
}

where C_UDSI(c,t,0) represent the value calculated by UDS, and I wanna get the total scalar for H_M in every cell. My case is transient and It can work, but the result seems the summation did not work. My question is if the summation can't be used in DEFINE_ADJUST macro or another method can be appied to calcuate summation.

Thanks.

Amir January 9, 2012 03:36

Hi,

First of all the summation command is something like this:
Quote:

sum += C_UDMI(c,t,0);
Also you have to initialize the parameter "sum" in this macro:
Quote:

real sum =0.0;
Using this macro is reasonable but you can also use "custom field function" for your purpose without using any UDF.

Bests,

ypchen January 9, 2012 07:42

Hi, Amir,

the reason that I calculate "sum" is I should use the value to calculate "p_eq" as following :
Code:

#include "udf.h"
#include "mem.h"
#include "math.h"
#include "stdio.h"
#define A 18.3
#define B 3704.6
#define a 0.0819
#define b 330
 
DEFINE_ADJUST(adj_peq,d)
{
 cell_t c ;
 Thread *t
real tem ;
thread_loop_c(t,d)
 {
 
  begin_c_loop(c,t)
  {
  tem = C_T(c,t);
  if(sum<1.2972)
  {
  p_eq = 1000*exp((a*1.2972-(B/tem)+A)*(1-((sum-1.2972)*(sum-1.2972))/1.6827));
  }
  if(1.2972 <= sum <= 4.7564)
  {
  p_eq = 1000*exp((a*sum)-(B/tem)+A) ;
  }
  if(sum> 4.7564)
  {
  p_eq = 1000*exp(((a*4.7564)-(B/tem)+A)+b*((sum-4.7564)*(sum-4.7564))/tem) ;
  }
  C_UDMI(c,t,1) = p_eq ;
  }
  end_c_loop(c,t)
 
 }
 
}

where the "sum" is a global varible outside the two macros(or create a UDM to store).
can I import the "custom field function" by UDF ?
besides, I check the "UDF guide " and found that :
Quote:

"A function that is defined using DEFINE ADJUST executes at every iteration and is called at the beginning of every iteration before transport equations are solved."
My case is transient, and I calculate every time step for many iterations. so I have a new qusetion :

if I add the iteration number, the H_M will be added more time ? 


Thank you very much.

Amir January 9, 2012 08:06

Quote:

Originally Posted by ypchen (Post 338463)
where the "sum" is a global varible outside the two macros(or create a UDM to store).

yes; global variable is a good option here and as you said it should be out of the macros unlike your first UDF. Performing another UDM is not proper for such purpose; although it works but just consumes memory.
Quote:

Originally Posted by ypchen (Post 338463)
can I import the "custom field function" by UDF ?

I'm not sure about this. it may be possible with schemes where I don't have any experiences with. You can search threads or open a new one.

Quote:

Originally Posted by ypchen (Post 338463)
if I add the iteration number, the H_M will be added more time ?

To avoid this feature you have 2 options:
1) use a simple if-clause in current UDF and use the commands when the time changes.
2) use DEFINE_EXECUTE_AT_THE_END macro which executes at the end of each time step automatically.

Bests,

ypchen January 10, 2012 01:11

Quote:

use a simple if-clause in current UDF and use the commands when the time changes.
Thank for your reply, Amir. But how to do this ? Can you show a simple example for me ? I have no ideal about this. Thank you .

Amir January 10, 2012 03:28

Quote:

Originally Posted by ypchen (Post 338568)
But how to do this ? Can you show a simple example for me ? I have no ideal about this. Thank you .

Hi,

This is an example of manual:


Quote:

static int last_ts = -1; /* Global variable. Time step is never <0 */
DEFINE_ADJUST(first_iter_only, domain)
{
int curr_ts;
curr_ts = N_TIME;
if (last_ts != curr_ts)
{
last_ts = curr_ts;

/* things to be done only on first iteration of each time step
can be put here */

}
}
Bests,

ypchen January 10, 2012 22:32

You really help me. Thank you very much, Amir.


All times are GMT -4. The time now is 02:48.