CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   FLUENT (https://www.cfd-online.com/Forums/fluent/)
-   -   Stop simulation after given conditions (UDF) (https://www.cfd-online.com/Forums/fluent/226310-stop-simulation-after-given-conditions-udf.html)

franc1 April 23, 2020 10:47

Stop simulation after given conditions (UDF)
 
Hi everybody,

How can i stop a transient simulation after a condition is fullfilled?
For instance: a thermal flux (W/m^2) is flowing into my domain and i want to stop the simulation at the time-step at which a given amount of energy (J) is reached. How can it be done?

Thank you in advance!

vinerm April 23, 2020 14:42

Stopping Fluent
 
You can use (cx-interrupt). This is a Scheme command and cannot be called from within UDF. However, this can be called with an if condition withing Calculation Activities > Execute Commands. Let the commands check for the heat flux and as soon as it reaches a certain value, (cx-interrupt) will stop Fluent. Other option is to use convergence conditions. Fluent has convergence conditions that can be used to stop Fluent if a certain value reaches below a threshold. Since it requires value to be reducing, you have to define either 1/heat flux or difference between your threshold and heat flux. This way, as soon as the value reaches below a threshold, Fluent will stop. This has to be done via Report Definitions and then using this Report Definition in Convergence Conditions.

franc1 April 24, 2020 04:18

If i want to apply the second way you suggested there's a problem: since my check value is energy (J) and as BC i have heatflux (W/m^2) this means that during calculation i have to sum up all the values of the previous time-steps. In this way at each time-step i check the integration of the heat flux of all the time-steps passed.

So the problem is: how can i store values calculated at the previous time-steps?

(i tried to do with a scheme execute command, but i don't have scheme programming background and it's quite difficult for me)

vinerm April 24, 2020 04:31

Energy
 
Do you want to check for the energy accumulated in the system? Or do you want to check for the energy supplied until a certain time?

franc1 April 24, 2020 04:33

The accumulated one

vinerm April 24, 2020 04:43

Accumulated Energy
 
If it is accumulated energy, then you do not need to sum over last time-steps. Since it is accumulating, that would be the energy of the cell zone. So, you can just look at the integral of the energy in the cell zone. Depending upon the unit of the quantity you take integral of, it has to be either volume or mass integral.

AlexanderZ April 24, 2020 04:57

you may try to test this function to stop calculation, this is core, if it works go further:
Code:

CX_Interpret_String("(cx-interrupt)");
if works, so you can do everything from UDF

make global variables my_flux, total_energy.
code scheme may look like this:
Code:

#include "udf.h"
real my_flux = 1.e6;
real total_energy = 0.0;

DEFINE_EXECUTE_AT_END(execute_at_end)
{
  total_energy += my_flux*CURRENT_TIMESTEP;
  if (total_energy > 999) // your value here
      {
          CX_Interpret_String("(cx-interrupt)");
      }
}

DEFINE_PROFILE(my_flux_bc,t,i)
{
  face_t f;
  begin_f_loop(f,t)
  {
      F_PROFILE(f,t,i) = my_flux;
  }
  end_f_loop(f,t)
}


franc1 April 24, 2020 10:23

I tried to solve the problem with the convergence method. I defined a parameter as: parameter= (Energy_target - Energy_inlet)

Energy_target is the target i want to reach;
Energy inlet is the difference between the actual and the initial total energy of my sistem.

In this way the "parameter" goes to zero during the simulation, but after this it goes to negative values.

Now, to stop the simulation i have to set a convergence condition. So i set in "convergence condition" the parameter as "report definition", but now i don't find a suitable value of the "stop criterion" to put. I don't find the meaning of this "stop criterion" in the help. What does it mean?

vinerm April 24, 2020 11:00

Stop Criterion
 
Stop criterion is used as the minimum value below within solution is considered converged. So, if you set it to 0.001, it should work for you.

franc1 April 24, 2020 11:32

1 Attachment(s)
I think that there's some problem with the exact definition of stop criterion.

If i put 3000 as "stop criterion" the simulation only does 2 time-steps and then stops.

While if i put the value of 0.001 as "stop criterion" the simulation stops at the time-step i fixed in the "Run calculation" window.

I attached the plot of the parameter.

vinerm April 24, 2020 11:52

Stop Criterion
 
The working of the stop criterion is not very straightforward. It determines residual values over N number of previous iterations or time-step, as per the user input in number of previous iterations to consider. Value is determined as change in report definition value of those N number of iterations or time-steps. E.g., if you use 5 for previous iterations, then Fluent will determine fractional change in value of, say, pressure at inlet, over five iterations. Fraction is based on the latest iteration or time-step. So, for the second iteration, Fluent determine difference between pressure value at second and first iteration and then divides it by pressure at second iteration. For third iteration, it takes difference between pressure value at third and first iteration and then divides by pressure at third iteration and so on. Once the value is below the stop criterion within 5 iterations or time-steps, it stops. If it is not, then it starts again. So, 3000 being a large number, it will stop almost immediately. 0.001 may be too small. Look at the change in value and then use an appropriate number as stop criterion.

franc1 April 24, 2020 12:56

Thank you very much Vinerm

LuckyTran April 24, 2020 13:33

Also keep in mind stop criterion is based on reaching asymptotic behavior (+/-). If you want your simulation to stop immediately as soon as the threshold is reached, you might need to take the absolute value of your parameter and/or set the number of iterations to 1.


All times are GMT -4. The time now is 13:12.