# FLUENT V6 - Compute solution residuals in a User-Defined Function (UDF)

 Client wants to calculate solution residuals in a UDF so as to control solver settings.The following UDF will do this.#include "udf.h"real calc_residual(Domain* d, Var_Attribute *sv, int n, real scale, char* eqname);DEFINE_ON_DEMAND(residuals){ Domain* d = Get_Domain(1); cxboolean scale = TRUE; real residual; Var_Attribute *sv; int i; int n = nres - 1; char eqname[128]; Message("nResidualsn===============================================n"); /* Loop over all equation variables */ for(i=SV_MAX-1, sv=sv_master; i>=0; i--, sv++) { residual = calc_residual(d, sv, n, scale, eqname); if (residual > 0) Message("%16s: %13.6en", eqname, residual); }}real calc_residual(Domain* d, Var_Attribute *sv, int n, real scale, char* eqname){ real resid = 0.0;#if !PARALLEL real sf; int eqn_index; eqn_index = NULLP(SV_METHOD(sv,residual_p)) ? EQ_NULL : SV_METHOD(sv,residual_p)(d,sv,RESIDUAL_INDEX); /* This will be tru if equation is being solved */ if ((eqn_index != EQ_NULL) && (SV_METHOD(sv,residual_p)(d,sv,RESIDUAL_DISPLAY))) { if (n>0) { resid = DOMAIN_RES(d, eqn_index)[n]; strcpy(eqname, DOMAIN_EQN_LABEL(d, eqn_index)); if (scale) { sf = DOMAIN_RES_SCALE(d, eqn_index)[n]; if (sf > 0) resid /= sf; } } else resid = 1.0; } else resid = 0.0;#else Message0("Warning: Function calc_residual() Not implemented in Paralleln");#endif return resid;}

 No comments yet. Be the first to add a comment!