CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > Software User Forums > ANSYS > FLUENT > Fluent UDF and Scheme Programming

A UDF to calculate energy per a cell and then returns new CO2 content in a cell.

Register Blogs Members List Search Today's Posts Mark Forums Read

Like Tree2Likes

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   April 9, 2021, 14:26
Default
  #41
Member
 
Yasser Selima
Join Date: Mar 2009
Location: Canada
Posts: 52
Rep Power: 19
Yasser is on a distinguished road
Basic programming

Each function, or Macro in our case is defined with arguments ..

DEFINE_MASS_TRANSFER(a , a, c, d) for example ... where a, b, c, d could be real, domain, thread .. or whatever . When you decide to use this function, you must look at the documentation of the function and see what is a, b, c and d ... if you wrote a function DEFINE_MASS_TRANSFER(a) , it will not compile ... the compiler won't know what to do ...

Second, As Pakk mentioned many times, first return command, will be the last line read by the solver ... you can not have two return lines

Third, programming languages like C or fortran, you need to declare every internal variable before using it. This is simply asking the compiler to reserve a place in the memory for this variable
for example

Code:
real Energy_per_cell;
Now the solver reserves a place for a real number and knows it is called Energy_per_cell. Then

Code:
Energy_per_cell = C_H(cell,thread);
Now the solver knows its value ...

Finally, look at last comment of pakk .. see how he wrote the IF statement.
Yasser is offline   Reply With Quote

Old   April 9, 2021, 15:22
Default
  #42
Senior Member
 
AH
Join Date: Apr 2014
Posts: 282
Rep Power: 13
visitor is on a distinguished road
Yes Pakk, just 1 millisec to finish breaking up CO2. By the way I have made some changes in the last few lines.

Also in the code line below, it is calculated quantity. But how do I tell fluent that this is quantity is carbon C?
"
Return (12/44)*((C_VOLUME(c, thread))*(C R(c, thread)))*(C YI(c, thread,i));
"
visitor is offline   Reply With Quote

Old   April 9, 2021, 15:29
Default
  #43
Senior Member
 
AH
Join Date: Apr 2014
Posts: 282
Rep Power: 13
visitor is on a distinguished road
Dear Yasser

Thanks, Yes I understood the one return per an 'if' statement. Will correct this, just missed in copy and pasting.

Regarding DEFINE_MASS_TRANSFER(a , a, c, d).. I have seen this in documents, but don't know how to use it. That is why I have just entered for now; DEFINE_MASS_TRANSFER(cell_update_CO2, domain).
visitor is offline   Reply With Quote

Old   April 9, 2021, 15:49
Default
  #44
Senior Member
 
Join Date: Nov 2013
Posts: 1,965
Rep Power: 26
pakk will become famous soon enough
Quote:
Originally Posted by visitor View Post
Yes Pakk, just 1 millisec to finish breaking up CO2.
Your code is not doing that. Not even close.
__________________
"The UDF library you are trying to load (libudf) is not compiled for parallel use on the current platform" is NOT the error after compiling. It is the error after loading. To see compiler errors, look at your screen after you click "build".
pakk is offline   Reply With Quote

Old   April 9, 2021, 15:57
Default
  #45
Senior Member
 
AH
Join Date: Apr 2014
Posts: 282
Rep Power: 13
visitor is on a distinguished road
I have edited the time delay, as shown. Now using this, should wait for one milli second or 0.001s.

For(float CURRENT_TIME, (CURRENT_TIME+0.001), 0.001)
{
If((CURRENT_TIME+0.001) - CURRENT_TIME= 0.001)
Return ***;
}
visitor is offline   Reply With Quote

Old   April 9, 2021, 18:54
Default
  #46
Member
 
Yasser Selima
Join Date: Mar 2009
Location: Canada
Posts: 52
Rep Power: 19
Yasser is on a distinguished road
This mass_transfer function will be called every time step .. it will return values to the solver to be applied during this time step .. your delay, is just saying to the processor wait for 1 millisecond before sending the answer to the solver ... and the solver will apply it for this time step regardless of the waiting time.
Yasser is offline   Reply With Quote

Old   April 9, 2021, 23:05
Default
  #47
Senior Member
 
Join Date: Nov 2013
Posts: 1,965
Rep Power: 26
pakk will become famous soon enough
Quote:
Originally Posted by visitor View Post
I have edited the time delay, as shown. Now using this, should wait for one milli second or 0.001s.

For(float CURRENT_TIME, (CURRENT_TIME+0.001), 0.001)
{
If((CURRENT_TIME+0.001) - CURRENT_TIME= 0.001)
Return ***;
}
Nope. Even if you would fix the syntax, this would never work.
__________________
"The UDF library you are trying to load (libudf) is not compiled for parallel use on the current platform" is NOT the error after compiling. It is the error after loading. To see compiler errors, look at your screen after you click "build".
pakk is offline   Reply With Quote

Old   April 10, 2021, 08:57
Default
  #48
Senior Member
 
AH
Join Date: Apr 2014
Posts: 282
Rep Power: 13
visitor is on a distinguished road
More revisions;
1.Changed the time delay as shown, for one millisecond.
2.Still not sure how to tell fluent what the returned mass values should be. Whether CO2 or carbon or oxygen.
3.Don't know to fill the DEFINE_MASS_TRANSFER(****,**,**...)
I think the concept is developing, but still more refinement.
Thanks, everyone.

#include "udf.h"
/*Adjusting CO2 based on; temperature, CO2 mass in cell, and energy in a cell*/
DEFINE_MASS_TRANSFER(cell_update_CO2, domain)
{
Real C_H(c,thread); /*Gas phase enthalpy J/s. Reading must be per a cell*/
/* define temperature in cell and get its value */
real temp = C_T(cell, thread);
real massCell:
massCell=(C_VOLUME(c,t))*(C_R(c,t))*(C_YI(c,t,i));
real EnerMasCellCO2;
Float CURRENT_TIME;
face_t f;
Thread *thread = Lookup_Thread(domain, ID);
begin_f_loop(f, thread)
{
/*The i below is the index for CO2 gas in fluent.*/
EnerMasCellCO2=(C_H(c,thread))/( massCell));
/*The following lines finds the average of the above two readings.*/
{
if (temp ≥ 500 && < 1000)
{
if (EnerMasCellCO2 ≥((100e3/0.044) && <(150e3/0.044));
return ((EnerMasCellCO2)-(150e-3/0.018))*massCell;
/*Below returns a mass of zero CO2 gas back in the cell. Assigning the original mass to zero*/
return =((C_VOLUME(c,thread))*(C_R(c,thread)))*(C_YI(c,th read,i))=0;
}
/*return a zero mass of CO2 in cell.*/
return massCell=0;
{
/*Pause for 0.001 second to allow for a reaction time.*/
real pause;
For(float CURRENT_TIME, (CURRENT_TIME+0.001), 0.001)
pause = CURRENT_TIME + 0.001;
{
/*Test if the pause has elapsed for 0.001 second, then resume.*/
If((pause-CURRENT_TIME <= 0.001)
{
/*Below returns the value for C. Multiply the original mass of CO2 by the ratio of C to CO2 molar mass (12/44).*/
return (12/44)*(massCell)*(C_YI(c, thread,i)); /*i is the index for C in here.*/
}
If((pause-CURRENT_TIME= 0.001)
{
/*Below returns the value for C. Multiply the original mass of CO2 by the ratio of C to CO2 molar mass (32/44).*/
Return (32/44)*(massCell)*(C_YI(c, thread,i)); /*I is the index for O2 in here.*/
}
endif;
}
end_f_loop(f,thread)
}
visitor is offline   Reply With Quote

Old   April 10, 2021, 09:29
Default
  #49
Member
 
Yasser Selima
Join Date: Mar 2009
Location: Canada
Posts: 52
Rep Power: 19
Yasser is on a distinguished road
DEFINE_MASS_TRANSFER( name, c, mixture_thread, from_phase_index, from_species_index,
to_phase_index, to_species_index)

name function name.
c cell
mixture_thread mixture-level thread.
from_phase_index Index of phase from which mass is transferred.
from_species_index ID of species from which mass is transferred
to_phase_index Index of phase to which mass is transferred.
to_species_index ID of species to which mass is transferred
Yasser is offline   Reply With Quote

Old   April 10, 2021, 09:40
Default
  #50
Senior Member
 
Join Date: Nov 2013
Posts: 1,965
Rep Power: 26
pakk will become famous soon enough
You can not implement a reaction delay in this way. It's fundamentally wrong. You can continue in this way for 100 years, and will then still fail.

Forget the delay. Get it working without the delay, that's already a challenge.

And use the help that you get. I told you that your if statements were wrong, and told you what to put instead, but you still use your old wrong version.

And Yasser now gave you the info that you could have looked up by yourself. I told you exactly what to Google for...
__________________
"The UDF library you are trying to load (libudf) is not compiled for parallel use on the current platform" is NOT the error after compiling. It is the error after loading. To see compiler errors, look at your screen after you click "build".
pakk is offline   Reply With Quote

Old   April 10, 2021, 11:00
Default
  #51
Senior Member
 
AH
Join Date: Apr 2014
Posts: 282
Rep Power: 13
visitor is on a distinguished road
1. Separated the 'if' statements. Now with 'if' and 'else if'.
2. Some how I will get the wait for reaction time 1 millisecond going.
3. Define mass transfer. This is totally new to me. I don't know what to enter for 'from phase index' and "to phase index"
4.Also "from spices index', I start with CO2 and return just air. So this will have to be N2 and O2.
5. After 1 millisecond, the 'to spices index', I have to return C and O2.
6. Based on notes 4 and 5 above, I think I may need another two DEFINE MASS TRANSFERs.

Thanks pakk and yasser.

#include "udf.h"
/*Adjusting CO2 based on; temperature, CO2 mass in cell, and energy in a cell*/
DEFINE_MASS_TRANSFER(cell_update_CO2, c, mixture_thread, from_phase_index, from_species_index, to_phase_index, to_species_index)
{
Real C_H(c,thread); /*Gas phase enthalpy J/s. Reading must be per a cell*/
/* define temperature in cell and get its value */
real temp = C_T(cell, thread);
real massCell:
massCell=(C_VOLUME(c,t))*(C_R(c,t))*(C_YI(c,t,i));
real EnerMasCellCO2;
Float CURRENT_TIME;
face_t f;
Thread *thread = Lookup_Thread(domain, ID);
begin_f_loop(f, thread)
{
/*The i below is the index for CO2 gas in fluent.*/
EnerMasCellCO2=(C_H(c,thread))/( massCell));
/*The following lines finds the average of the above two readings.*/
{
if ((temp ≥ 500 && < 1000) && (EnerMasCellCO2 ≥((100e3/0.044) && <(150e3/0.044))
return ((EnerMasCellCO2)-(150e-3/0.018))*massCell; /*return new energy value in ell.*/
else if (temp < 1000)
/*Below returns a mass of zero CO2 gas back in the cell. Assigning the original mass to zero*/
return =((C_VOLUME(c,thread))*(C_R(c,thread)))*(C_YI(c,th read,i))=0;
endif;
}
/*Pause for 0.001 second to allow for a reaction time.*/
float pause;
For(float CURRENT_TIME, (CURRENT_TIME+0.001), 0.001)
pause = CURRENT_TIME + 0.001;
{
/*Test if the pause has elapsed for 0.001 second, then resume.*/
If((pause-CURRENT_TIME= 0.001)
{
/*Below returns the value for C. Multiply the original mass of CO2 by the ratio of C to CO2 molar mass (12/44).*/
return (12/44)*(massCell)*(C_YI(c, thread,i)); /*i is the index for C in here.*/
else if ( (pause-CURRENT_TIME) = 0.001)
/*Below returns the value for C. Multiply the original mass of CO2 by the ratio of C to CO2 molar mass (32/44).*/
Return (32/44)*(massCell)*(C_YI(c, thread,i)); /*I is the index for O2 in here.*/
}
endif;
}
end_f_loop(f,thread)
}
visitor is offline   Reply With Quote

Old   April 10, 2021, 11:47
Default
  #52
Senior Member
 
Join Date: Nov 2013
Posts: 1,965
Rep Power: 26
pakk will become famous soon enough
Are you a troll? I am trying to help you, but you ignore the help you get...

1. You did not fix the if statement. All you had to do was copy and paste what I wrote...
2. You will NOT get the reaction time working in this way.
There are ways to do that, but they need a completely different approach then what you do here.
3. You don't have to enter anything for 'from phase index' and "to phase index". It's input for your function. Fluent will give you values.

Get a simple version working before you try the difficult one. The delay is hard, also for experienced programmers. Your if-statement shows that you are not an experienced programmer.
__________________
"The UDF library you are trying to load (libudf) is not compiled for parallel use on the current platform" is NOT the error after compiling. It is the error after loading. To see compiler errors, look at your screen after you click "build".
pakk is offline   Reply With Quote

Old   April 10, 2021, 12:48
Default
  #53
Senior Member
 
AH
Join Date: Apr 2014
Posts: 282
Rep Power: 13
visitor is on a distinguished road
Dear sirs , yes I am not an expert in programming UDF. But I I have to get this done, thanks for your help.
1. The 'if' statements are now isolated.
2. OK I will leave the DEFINE MASS TRANSFER as is in brackets.
3. The return statement is now returning masses. But how will fluent know what the returned mass is? Example when retuning mass for CO2 is zero, will fluent in this case return zero because, originally the index i was zero?
4. How about when retuning masses for C and O2? I am returning a mass as shown below. How will fluent know which is C and which is O2?


#include "udf.h"
/*Adjusting CO2 based on; temperature, CO2 mass in cell, and energy in a cell*/
DEFINE_MASS_TRANSFER(cell_update_CO2, domain)
{
Real C_H(c,thread); /*Gas phase enthalpy J/s. Reading must be per a cell*/
/* define temperature in cell and get its value */
real temp = C_T(cell, thread);
real massCell:
/*The i below is the index for CO2 gas in fluent.*/
massCell=(C_VOLUME(c,t))*(C_R(c,t))*(C_YI(c,t,i));
real EnerMasCellCO2;
Float CURRENT_TIME;
face_t f;
Thread *thread = Lookup_Thread(domain, ID);
begin_f_loop(f, thread)
{
EnerMasCellCO2=(C_H(c,thread))/( massCell));
/*The following lines finds the average of the above two readings.*/
if ((temp ≥ 500 && < 1000) && (EnerMasCellCO2 ≥((100e3/0.044) && <(150e3/0.044))
{
return ((EnerMasCellCO2)-(150e-3/0.018))*massCell; /*return new energy value in ell.*/
}
if (temp < 1000)
{
/*Below returns a mass of zero CO2 gas back in the cell. Assigning the original mass to zero*/
return =((C_VOLUME(c,thread))*(C_R(c,thread)))*(C_YI(c,th read,i))=0;
}
/*Test if the pause has elapsed for 0.001 second, then resume.*/
If((CURRENT_TIME+0.001) – (CURRENT_TIME) <= 0.001)
{
/*Below returns the value for C. Multiply the original mass of CO2 by the ratio of C to CO2 molar mass (12/44).*/
return (12/44)*(massCell)*(C_YI(c, thread,i)); /*i is the index for C in here.*/
}
If((CURRENT_TIME+0.001) – (CURRENT_TIME) <= 0.001)
{
/*Below returns the value for C. Multiply the original mass of CO2 by the ratio of C to CO2 molar mass (32/44).*/
Return (32/44)*(massCell)*(C_YI(c, thread,i)); /*I is the index for O2 in here.*/
}
end_f_loop(f,thread)
}
visitor is offline   Reply With Quote

Old   April 10, 2021, 13:22
Default
  #54
Senior Member
 
Join Date: Nov 2013
Posts: 1,965
Rep Power: 26
pakk will become famous soon enough
1. No, you still did not correct the if statement.

If you are unable/unwilling to follow simple instructions, I really can not help you.

(And your first mistake: you were not clear about your goal. You want to implement a chemical reaction. There is a simple way to do this in Fluent that does not involve a UDF. Please ignore this helpful advice, and continue working on your UDF that will never do what you need.)
__________________
"The UDF library you are trying to load (libudf) is not compiled for parallel use on the current platform" is NOT the error after compiling. It is the error after loading. To see compiler errors, look at your screen after you click "build".
pakk is offline   Reply With Quote

Old   April 11, 2021, 22:29
Default
  #55
Senior Member
 
Alexander
Join Date: Apr 2013
Posts: 2,363
Rep Power: 34
AlexanderZ will become famous soon enoughAlexanderZ will become famous soon enough
compile code, show here log

lol
__________________
best regards


******************************
press LIKE if this message was helpful
AlexanderZ is offline   Reply With Quote

Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 01:33.