jeudi 27 octobre 2016

c++ bool function call in if statement nested in another bool function

The main function of my code contains a loop for calculating certain parameters. The function "isconverged_LBM" check convergence of one such parameter. This is called for all time steps in a loop. This function is called directly form the if statement.

///part A
main ()

{

  // this part inside a loop
    ///check convergernce 
    //calls a defined convergence criterion function (here based on press anisotropic ratio)

    if(LBM::isconverged_LBM(t, t_info_disk, t_conv_check)) {

      break;
    }


}

This is the function that is called from the main loop. the convergence check calculations are made only at a user specified frequency. The calculations are made in another function "isconverged_pr_anisotropy()". The function call here is made from the if statement as well.

///part B
bool LBM::isconverged_LBM(int time, int frequency, int t_activate) {

  ///return true if LBM soln converges on one criterion


  if(t_activate < 0) {
    return false;
  }
  else if(time <= t_activate) {
    return false;
  }
  else if (time % frequency == 0) {
    //check pressure anisotropy ratio convergence

    if(LBM::isconverged_pr_anisotropy()) {

      return true;
    }
    else {
      return false;
    }
  }

  return false;

}

This is the function that is called from the previous if statement.

///part C
bool LBM::isconverged_pr_anisotropy() {

  /// convergence check uses 1.0E-6 as the criterion
  /// start the convergence check if time > time_conv_activate
  /// return 0 if time_conv_activate is set to -1
  ///return 1 if convergence is reached, else return 0

  float conv_criterion_aniso = 1.0e-06;

    if(pr_drp_aniso_ratio == 0) {
      return false;
    }
    else if( (abs( (pr_drp_aniso_ratio - pr_drp_aniso_ratio_old) / pr_drp_aniso_ratio)) < conv_criterion_aniso) {


      //output convergence message on screen
      message.str("");
      message << fixed << setprecision(8);
      message << "\nConvergence reached on pressure drop anisotropy ratio, A " << endl;
      message << "relative change in A " << (abs( (pr_drp_aniso_ratio - pr_drp_aniso_ratio_old) / pr_drp_aniso_ratio)) << endl;
      message_handling(message.str());

      return true;
    }
    else {
      return false;
    }

}

The issue I am facing is that, the second function call from the if statement is never made (to the function "isconverged_pr_anisotropy" in the part B). Instead the control goes to the following return statement and the loop in main breaks.

I was therefore wondering whether it is allowed to make a function call from an if statement if that function is itself called from another if statement evaluation?

The code compiles fine (GCC) and the bool functions are defined correctly.

Apologies in advance is any of this is not clear.

Aucun commentaire:

Enregistrer un commentaire