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/)
-   -   A UDF to calculate energy per a cell and then returns new CO2 content in a cell. (https://www.cfd-online.com/Forums/fluent-udf/235125-udf-calculate-energy-per-cell-then-returns-new-co2-content-cell.html)

visitor April 2, 2021 04:49

A UDF to calculate energy per a cell and then returns new CO2 content in a cell.
 
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;
}

Yasser April 2, 2021 12:54

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

visitor April 2, 2021 13:16

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;
}

pakk April 2, 2021 13:55

Still not clear what you are trying with the multiple returns. Only the first one will be used, the rest will be ignored.

visitor April 2, 2021 14:19

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.

Yasser April 2, 2021 15:07

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

visitor April 2, 2021 15:40

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.

Yasser April 2, 2021 16:36

Look at DEFINE_ADJUST examples in the manual ... there are examples that loop over all cells

jean@cfd April 2, 2021 18:50

Creation of udf file to make chemical reactios
 
1 Attachment(s)
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?

jean@cfd April 2, 2021 18:53

Simulation of manned space re entry vehicle with chemkin file
 
I have many doubts in simulation of manned space re entry vehicle by importing chemkin file.how to do it?

Yasser April 2, 2021 19:43

Quote:

Originally Posted by jean@cfd (Post 800553)
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

visitor April 3, 2021 02:16

Adjusting CO2 content in cells
 
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)
}

pakk April 3, 2021 03:41

A function can return one thing. Not four. Please don't ignore this again, I'm trying to help you.

visitor April 3, 2021 04:11

Separating the 'if' conditions and return values
 
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)
}

pakk April 3, 2021 05:49

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.

visitor April 3, 2021 07:24

Adjusting CO2 in a cell. Depending on energy in cell and mass of CO2 in cell.
 
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)
}

Yasser April 3, 2021 19:40

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.

visitor April 4, 2021 01:09

Get in cell; energy, species mass, & temperature. Then return value values.
 
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)
}

pakk April 4, 2021 04:09

Code:

dzdt>species[CO2]=CO2_mass_per_cell;
What is this code supposed to do? Why did you write this?

visitor April 4, 2021 05:36

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?
"


All times are GMT -4. The time now is 21:16.