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 Community New Posts Updated Threads Search

Like Tree2Likes

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   April 2, 2021, 04:49
Default A UDF to calculate energy per a cell and then returns new CO2 content in a cell.
  #1
Senior Member
 
AH
Join Date: Apr 2014
Posts: 282
Rep Power: 13
visitor is on a distinguished road
My first attempt to thermal split CO2 in cells, and return new values of CO2, O2, and CO. Also the remaining energy per a cell (consumed due to CO2 splitting). ANY recommendations, advise please? Ignore values of temperature and energy for now.

#include "udf.h"
DEFINE_PROPERTY(cell_CO2_split, cell, thread)
{
/*program to handle CO2 thermal splitting, assumed figures for now*/
/* define CO2, CO, and O2 as real, and they must be in mol values */
real CO2;
real CO;
real O2;
/* define temperature in cell and get its value */
real temp = C_T(cell, thread);
/*
define as real the ‘energy_IN_cell_per_mol_wt’. This needs to be obtained as energy in J/mol per a cell. Divided by the mol weight value. For CO2.*/
dzdt>energy=dh_dt; /*gas phase enthalpy J/s.*/
dzdt>energy=new_dh_dt;
dzdt>species[CO2=gas_mass_cell]; /*gas phase species mass kg/s*/
energy_IN_cell_per_mol_wt=dh_dt/gas_mass_cell;
real energy_IN_cell_per_mol_wt = C_E(cell,thread);
if (temp ≥ 500 && < 1000)
if (energy_IN_cell_per_mol_wt ≥(100e3/0.044) && <(150e3/0.044));
/*Above division by 0.044 gives energy per a cell per molar weight of CO2. Where 0.044 is the molar weight of CO2.*/
CO=1;
O2=1;
CO2=0;
/*
Below; energy taken (125e3) to split CO2, must be subtracted from cell. Then new value returned
*/
new_dh_dt=(dh_dt) - 125e3; /*subtract energy taken by CO2 per a cell.*/
return CO;
return O2;
return CO2;
return new_dh_dt;
endif;
else
CO2=1.;
return CO2;
}
visitor is offline   Reply With Quote

Old   April 2, 2021, 12:54
Default
  #2
Member
 
Yasser Selima
Join Date: Mar 2009
Location: Canada
Posts: 52
Rep Power: 19
Yasser is on a distinguished road
Where are you going to attach this define_property? And how can you have all these return lines?

You need to use DEFINE_ADJUST to loop over the cells and save the values of the species fractions in user defined memory ... then use define source, read the values from UDM and return them
Yasser is offline   Reply With Quote

Old   April 2, 2021, 13:16
Default
  #3
Senior Member
 
AH
Join Date: Apr 2014
Posts: 282
Rep Power: 13
visitor is on a distinguished road
Thanks, I have applied "DEFINE_ADJUST'. Will this do? Or should their be a looping over cells, after ''' if (temp ≥ 500 && < 1000) '''? Basically read energy and CO2 mass in a cell, process with if statements and return; energy C,& O2 values into the cell. Continue looping with all cells ...

Thanks.

#include "udf.h"
DEFINE_ADJUST(cell_CO2_split, cell, thread)
{
/*program to handle CO2 thermal splitting, assumed figures for now*/
/* define CO2, CO, and O2 as real, and they must be in mol values */
real CO2;
real CO;
real O2;
/* define temperature in cell and get its value */
real temp = C_T(cell, thread);
/*
define as real the ‘energy_IN_cell_per_mol_wt’. This needs to be obtained as energy in J/mol per a cell. Divided by the mol weight value. For CO2.*/
dzdt>energy=dh_dt; /*gas phase enthalpy J/s.*/
dzdt>energy=new_dh_dt;
dzdt>species[CO2=gas_mass_cell]; /*gas phase species mass kg/s*/
energy_IN_cell_per_mol_wt=dh_dt/gas_mass_cell;
real energy_IN_cell_per_mol_wt = C_E(cell,thread);
if (temp ≥ 500 && < 1000)
if (energy_IN_cell_per_mol_wt ≥(100e3/0.044) && <(150e3/0.044));
/*Above division by 0.044 gives energy per a cell per molar weight of CO2. Where 0.044 is the molar weight of CO2.*/
CO=1;
O2=1;
CO2=0;
/*
Below; energy taken (125e3) to split CO2, must be subtracted from cell. Then new value returned
*/
new_dh_dt=(dh_dt) - 125e3; /*subtract energy taken by CO2 per a cell.*/
return CO;
return O2;
return CO2;
return new_dh_dt;
endif;
else
CO2=1.;
return CO2;
}
visitor is offline   Reply With Quote

Old   April 2, 2021, 13:55
Default
  #4
Senior Member
 
Join Date: Nov 2013
Posts: 1,965
Rep Power: 26
pakk will become famous soon enough
Still not clear what you are trying with the multiple returns. Only the first one will be used, the rest will be ignored.
__________________
"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 2, 2021, 14:19
Default
  #5
Senior Member
 
AH
Join Date: Apr 2014
Posts: 282
Rep Power: 13
visitor is on a distinguished road
Considering a domain with so many cells containing air and CO2.

The UDF should read; temperature (K), enthalpy (J/s) and moler weight of CO2.

Process with an 'if' conditions. Then return to the same cell new values of; enthalpy (J/s), C and O2. Then continue the same for other cells, over and over again.

Thanks.
jean@cfd likes this.
visitor is offline   Reply With Quote

Old   April 2, 2021, 15:07
Default
  #6
Member
 
Yasser Selima
Join Date: Mar 2009
Location: Canada
Posts: 52
Rep Power: 19
Yasser is on a distinguished road
You need to loop on all cells (This will be a loop on cells inside a loop on cell threads) and then do the if statement ... then save the data into UDM

Does the molar weight of CO2 change?

C_T(c,t) gives you temperature
C_H(c,t) gives you enthalpy
Yasser is offline   Reply With Quote

Old   April 2, 2021, 15:40
Default
  #7
Senior Member
 
AH
Join Date: Apr 2014
Posts: 282
Rep Power: 13
visitor is on a distinguished road
Yes, or if there is away of immediately updating a cell, maybe quicker than looping.

Don't how, and i am looking for an example in fluent manual. Any help out please, thanks.
visitor is offline   Reply With Quote

Old   April 2, 2021, 16:36
Default
  #8
Member
 
Yasser Selima
Join Date: Mar 2009
Location: Canada
Posts: 52
Rep Power: 19
Yasser is on a distinguished road
Look at DEFINE_ADJUST examples in the manual ... there are examples that loop over all cells
Yasser is offline   Reply With Quote

Old   April 2, 2021, 18:50
Default Creation of udf file to make chemical reactios
  #9
New Member
 
Join Date: Mar 2021
Posts: 14
Rep Power: 5
jean@cfd is on a distinguished road
How can I create udf file for these reactions in ANSYS FLUENT TO PURSUE SIMULATIONS FOR MANNED SPACE RE ENTRYCAPSULE WITH CHEMICAL REACTIONS INCLUDED IN FILE?
Attached Files
File Type: txt reaction mps 2 corrected.txt (737 Bytes, 7 views)
jean@cfd is offline   Reply With Quote

Old   April 2, 2021, 18:53
Default Simulation of manned space re entry vehicle with chemkin file
  #10
New Member
 
Join Date: Mar 2021
Posts: 14
Rep Power: 5
jean@cfd is on a distinguished road
I have many doubts in simulation of manned space re entry vehicle by importing chemkin file.how to do it?

Last edited by jean@cfd; April 3, 2021 at 03:43.
jean@cfd is offline   Reply With Quote

Old   April 2, 2021, 19:43
Default
  #11
Member
 
Yasser Selima
Join Date: Mar 2009
Location: Canada
Posts: 52
Rep Power: 19
Yasser is on a distinguished road
Quote:
Originally Posted by jean@cfd View Post
I have many doubts in imulation of manned space re entry vehicle by importing chemkin file.how to do it?
I have never used chemkin and I am not a chemistry guy .. but if you know the reaction inputs and outputs, you can do it by define_source ... As explained in the post above, use define_adjust and loop on all cells. Store the change in mass fractions in UDM and in Define_source, add whatever in the UDM
Yasser is offline   Reply With Quote

Old   April 3, 2021, 02:16
Default Adjusting CO2 content in cells
  #12
Senior Member
 
AH
Join Date: Apr 2014
Posts: 282
Rep Power: 13
visitor is on a distinguished road
Revised with define adjust.

#include "udf.h"
/*adjusing CO2 based on temperature and energy in a cell*/
DEFINE_ADJUST(My_cell_update_CO2, domain)
{
real CO2;
real CO;
real O2;
real dh_dt;
real molwt[CO2];
real mass_CO2;
dzdt>energy=dh_dt; /*gas phase enthalpy J/s.*/
dzdt>energy=new_dh_dt;
dzdt>species[CO2]=gas_mass_cell; /*gas phase species mass kg/s*/

/* define temperature in cell and get its value */
real temp = C_T(cell, thread);
real energy_IN_cell_per_mol_wt = C_E(cell,thread);
face_t f;
Thread *thread = Lookup_Thread(domain, ID);
begin_f_loop(f, thread)
{
energy_IN_cell_per_mol_wt =(dh_dt)/((previous_timestep)*(dzdt/molwt[CO2]);
if (temp ≥ 500 && < 1000)
if (energy_IN_cell_per_mol_wt ≥((100e3/0.044) && <(150e3/0.044));
CO=1;
O2=1;
CO2=0;
/*
Below; energy taken (125e3) to split CO2, must be subtracted from cell. Then new value returned. Dividing by 0.044 to get energy per molar weight.
*/
new_dh_dt; = 0.044*(energy_IN_cell_per_mol_wt – (150e3/0.044));
return CO;
return O2;
return CO2;
return new_dh_dt;
else
CO2=1.;
return CO2;
}
end_f_loop(f,thread)
}
visitor is offline   Reply With Quote

Old   April 3, 2021, 03:41
Default
  #13
Senior Member
 
Join Date: Nov 2013
Posts: 1,965
Rep Power: 26
pakk will become famous soon enough
A function can return one thing. Not four. Please don't ignore this again, I'm trying to help you.
__________________
"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 3, 2021, 04:11
Default Separating the 'if' conditions and return values
  #14
Senior Member
 
AH
Join Date: Apr 2014
Posts: 282
Rep Power: 13
visitor is on a distinguished road
Alright, I have now used four separate 'if' conditions. Each 'if' condition does one evaluation and returns its value back into the cell.


#include "udf.h"
/*adjusing CO2 based on temperature and energy in a cell*/
DEFINE_ADJUST(cell_update_CO2, domain)
{
real CO2;
real CO;
real O2;
real dh_dt;
real molwt[CO2];
real mass_CO2;
dzdt>energy=dh_dt; /*gas phase enthalpy J/s.*/
dzdt>energy=new_dh_dt;
dzdt>species[CO2]=gas_mass_cell; /*gas phase species mass kg/s*/

/* define temperature in cell and get its value */
real temp = C_T(cell, thread);
real energy_IN_cell_per_mol_wt = C_E(cell,thread);
face_t f;
Thread *thread = Lookup_Thread(domain, ID);
begin_f_loop(f, thread)
{
energy_IN_cell_per_mol_wt =(dh_dt)/((previous_timestep)*(dzdt/molwt[CO2]);
if (temp ≥ 500 && < 1000)
if (energy_IN_cell_per_mol_wt ≥((100e3/0.044) && <(150e3/0.044));
CO=1;
return CO;
else
CO=0;
return CO;
}
{
if (temp ≥ 500 && < 1000)
if (energy_IN_cell_per_mol_wt ≥((100e3/0.044) && <(150e3/0.044));
O2=1;
return O2;
endif;
else
O2=1;
return O2;
}
{
if (temp ≥ 500 && < 1000)
if (energy_IN_cell_per_mol_wt ≥((100e3/0.044) && <(150e3/0.044));
new_dh_dt; = 0.044*(energy_IN_cell_per_mol_wt – (150e3/0.044));
return new_dh_dt;
else
return energy_IN_cell_per_mol_wt;
}
end_f_loop(f,thread)
}
visitor is offline   Reply With Quote

Old   April 3, 2021, 05:49
Default
  #15
Senior Member
 
Join Date: Nov 2013
Posts: 1,965
Rep Power: 26
pakk will become famous soon enough
Code:
real dh_dt;
real molwt[CO2];
real mass_CO2;
dzdt>energy=dh_dt;
dzdt>energy=new_dh_dt
Ignoring your syntax errors: you don't understand how code works. (no problem, I also once had to learn it.)
In the first line, you tell Fluent that dh_dt is a number. Fluent does not know anything else. It does not know which number, it does not know what this number physically means. It's your job to take care of these things.
In the fourth line above, you tell Fluent that "energy" should become the number that is in dh_dt. But you did not give it a number yet!
In the fifth line, you immediately change the energy. This makes the fourth line irrelevant, so why is it there? What are you trying to do?
And what do you make the energy? New_dh_dt. What is that? It's the first time you use that name. Fluent does not even know that this is a number. You can not use variables without introducing them.

You must have had some thoughts when you wrote these lines. Some expectations of what they should do. But I don't know them, so it's hard to change your code so it reaches these expectations.

I'm on my phone, so I will not go through your entire code, but these problems are everywhere.
__________________
"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 3, 2021, 07:24
Default Adjusting CO2 in a cell. Depending on energy in cell and mass of CO2 in cell.
  #16
Senior Member
 
AH
Join Date: Apr 2014
Posts: 282
Rep Power: 13
visitor is on a distinguished road
Thanks pakk and Yassir. I have made revisions. Yes programming in species is new to me, and I am not a C++ expert. Thanks for your help.
The 'First_read_energy_IN_cell_per_CO2_mass_in_cell' will have to be corrected too. I just need energy per a cell. Will sort out gradually.
#include "udf.h"
/*Adjusting CO2 based on; temperature, CO2 mass in cell, and energy in a cell*/
DEFINE_ADJUST(cell_update_CO2, domain)
{
dzdt>energy=dh_dt; /*Gas phase enthalpy J/s. Reading must be per a cell*/

dzdt>species[CO2]=CO2_mass_per_cell; /*Gas phase species mass kg/s. Reading per cell*/
/* define temperature in cell and get its value */
real temp = C_T(cell, thread);
face_t f;
Thread *thread = Lookup_Thread(domain, ID);
begin_f_loop(f, thread)
{
First_read_energy_IN_cell_per_CO2_mass_in_cell =(dh_dt)/((CURRENT_TIME-0.001)*(CO2_mass_per_cell));
Second_read_energy_IN_cell_per_CO2_mass_in_cell =(dh_dt)/((CURRENT_TIME)*(CO2_mass_per_cell));
/*The following lines finds the average of the above two readings.*/
energy_IN_cell_per_CO2_mass_in_cell= ((First_read_energy_IN_cell_per_CO2_mass_in_cell) + (Second_read_energy_IN_cell_per_CO2_mass_in_cell))/2;
if (temp ≥ 500 && < 1000)
if (energy_IN_cell_per_CO2_mass_in_cell ≥((100e3/0.044) && <(150e3/0.044));
C=1;
return C;
endif;
}
{
if (energy_IN_cell_per_CO2_mass_in_cell ≥((100e3/0.044) && <(150e3/0.044));
O2=1;
return O2;
endif;
}
{
if (energy_IN_cell_per_CO2_mass_in_cell ≥((100e3/0.044) && <(150e3/0.044));
return (((energy_IN_cell_per_CO2_mass_in_cell)-(150e-3/0.044))*(CO2_mass_per_cell));
return ((energy_IN_cell_per_CO2_mass_in_cell)*(CO2_mass_p er_cell));
}
end_f_loop(f,thread)
}
visitor is offline   Reply With Quote

Old   April 3, 2021, 19:40
Default
  #17
Member
 
Yasser Selima
Join Date: Mar 2009
Location: Canada
Posts: 52
Rep Power: 19
Yasser is on a distinguished road
If you try to write your UDF without looking at the macros available in the UDF manual, you will spend more time and probably will not get it working.

There are many define_ macros , each could be hooked at certain location to do a specified job. Some of the return values to the solver and some of them adjust some parameters and return nothing to the solver.

DEFINE_ADJUST returns nothing to the solver ... but it is called before every iteration. That is why we can use it to do the calculations of the new parameters in the coming iteration. And we need to save these values .. and if the values are different from a cell to the next, we use UDM to store them.

To change the mass fractions of CO2 and CO and O2, you need to use DEFINE_SOURCE or DEFINE_MASS_TRANSFER ... both functions are recalled every iteration, for every cell. Here all you need to do is return the value stored in your UDM.
Yasser is offline   Reply With Quote

Old   April 4, 2021, 01:09
Default Get in cell; energy, species mass, & temperature. Then return value values.
  #18
Senior Member
 
AH
Join Date: Apr 2014
Posts: 282
Rep Power: 13
visitor is on a distinguished road
I have made revisions.
Changed to DEFINE_MASS_TRANSFER.
Changed to using C_H(c,t) . This will get energy per a cell.

#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*/
dzdt>species[CO2]=CO2_mass_per_cell; /*Gas phase species mass kg/s. Reading per cell*/
/* define temperature in cell and get its value */
real temp = C_T(cell, thread);
face_t f;
Thread *thread = Lookup_Thread(domain, ID);
begin_f_loop(f, thread)
{
energy_IN_cell_per_CO2_mass_in_cell =(C_H(c,thread))/(CO2_mass_per_cell);
/*The following lines finds the average of the above two readings.*/
if (temp ≥ 500 && < 1000)
if (energy_IN_cell_per_CO2_mass_in_cell ≥((100e3/0.044) && <(150e3/0.044));
C=1;
return C;
endif;
}
{
if (energy_IN_cell_per_CO2_mass_in_cell ≥((100e3/0.044) && <(150e3/0.044));
O2=1;
return O2;
endif;
}
{
if (energy_IN_cell_per_CO2_mass_in_cell ≥((100e3/0.044) && <(150e3/0.044));
return (((energy_IN_cell_per_CO2_mass_in_cell)-(150e-3/0.044))*(CO2_mass_per_cell));
return ((energy_IN_cell_per_CO2_mass_in_cell)*(CO2_mass_p er_cell));
}
end_f_loop(f,thread)
}
visitor is offline   Reply With Quote

Old   April 4, 2021, 04:09
Default
  #19
Senior Member
 
Join Date: Nov 2013
Posts: 1,965
Rep Power: 26
pakk will become famous soon enough
Code:
dzdt>species[CO2]=CO2_mass_per_cell;
What is this code supposed to do? Why did you write this?
__________________
"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 4, 2021, 05:36
Default
  #20
Senior Member
 
AH
Join Date: Apr 2014
Posts: 282
Rep Power: 13
visitor is on a distinguished road
This was written to obtain mass of CO2 in cell, in kg. Then energy per cell was divided by this mass value.

"
dzdt>species[CO2]=CO2_mass_per_cell;
---------
What is this code supposed to do? Why did you write this?
"
visitor is offline   Reply With Quote

Reply


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 18:49.