diff --git a/SU2_CFD/include/fluid/CFluidCantera.hpp b/SU2_CFD/include/fluid/CFluidCantera.hpp index 0a065fe90a6c..f417e71bbd1a 100644 --- a/SU2_CFD/include/fluid/CFluidCantera.hpp +++ b/SU2_CFD/include/fluid/CFluidCantera.hpp @@ -60,10 +60,8 @@ class CFluidCantera final : public CFluidModel { const bool Correction_Velocity; /*!< \brief The diffusive species fluxes are corrected to sum to zero. */ su2double Heat_Release{0.0}; /*!< \brief heat release due to combustion */ - bool temperatureIterationFailed{false}; /*!< \brief The last temperature-from-enthalpy iteration did not converge. */ double minTemperature{0.0}; /*!< \brief Lower temperature bound of the Newton iteration. */ double maxTemperature{0.0}; /*!< \brief Upper temperature bound of the Newton iteration. */ - double temperatureGuess{0.0}; /*!< \brief Start of the next temperature iteration, 0 if unset. */ const std::shared_ptr sol; /*!< \brief Object needed to describe a chemically-reacting solution*/ std::vector speciesIndices; /*!< \brief Mechanism index of each transported species. */ std::vector chemicalSourceTerm; /*!< \brief Chemical source term of the transported species. */ @@ -163,11 +161,6 @@ class CFluidCantera final : public CFluidModel { */ inline su2double GetHeatRelease() override { return Heat_Release; } - /*! - * \brief Whether the last state evaluation failed to converge or needed a temperature far outside the data range. - */ - inline bool GetTemperatureIterationFailed() const override { return temperatureIterationFailed; } - /*! * \brief Get enthalpy diffusivity terms. */ @@ -186,18 +179,21 @@ class CFluidCantera final : public CFluidModel { void SetTDState_T(su2double val_temperature, const su2double* val_scalars) override; /*! - * \brief Set the state from enthalpy by Newton iteration on temperature. + * \brief Set the state from enthalpy, starting the temperature iteration from the reference temperature. + * \note A failed iteration is not reported, use SolveTDState_h where that matters. * \param[in] val_enthalpy - Enthalpy value at the point. * \param[in] val_scalars - Scalar mass fractions. */ void SetTDState_h(su2double val_enthalpy, const su2double* val_scalars) override; /*! - * \brief Set the start of the next temperature iteration of SetTDState_h, used once. - * \param[in] val_temperature - Temperature guess. + * \brief Set the state from enthalpy by Newton iteration on temperature. + * \param[in] val_enthalpy - Enthalpy value at the point. + * \param[in] val_temperature_guess - Start of the iteration, <= 0 to start from the reference temperature. + * \param[in] val_scalars - Scalar mass fractions. + * \return True if the iteration converged inside the range of the thermodynamic data. */ - inline void SetTemperatureGuess(su2double val_temperature) override { - temperatureGuess = SU2_TYPE::GetValue(val_temperature); - } + bool SolveTDState_h(su2double val_enthalpy, su2double val_temperature_guess, + const su2double* val_scalars) override; #endif }; diff --git a/SU2_CFD/include/fluid/CFluidModel.hpp b/SU2_CFD/include/fluid/CFluidModel.hpp index af9aa3e059b7..e8db2001d31c 100644 --- a/SU2_CFD/include/fluid/CFluidModel.hpp +++ b/SU2_CFD/include/fluid/CFluidModel.hpp @@ -214,11 +214,6 @@ class CFluidModel { */ inline virtual su2double GetHeatRelease() { return 0.0; } - /*! - * \brief Whether the last temperature-from-enthalpy iteration failed, so the state is non-physical. - */ - inline virtual bool GetTemperatureIterationFailed() const { return false; } - /*! * \brief Get the enthalpy diffusivity terms for all species being solved. * @@ -404,10 +399,19 @@ class CFluidModel { virtual void SetTDState_h(su2double val_enthalpy, const su2double* val_scalars = nullptr) {} /*! - * \brief Virtual member. Initial temperature for the next iterative SetTDState_h call. - * \param[in] val_temperature - Temperature guess, e.g. the previous value at the point. + * \brief Set the state from the enthalpy, solving for the temperature iteratively if the model needs to. + * \note Models that compute the temperature directly ignore the guess and always succeed. + * \param[in] val_enthalpy - Enthalpy value at the point. + * \param[in] val_temperature_guess - Start of the temperature iteration, e.g. the previous temperature at the + * point. A value <= 0 means no guess is available. + * \param[in] val_scalars - Scalar mass fractions. + * \return True if a valid state was found. If false, the state of the model must not be used. */ - virtual void SetTemperatureGuess(su2double val_temperature) {} + virtual bool SolveTDState_h(su2double val_enthalpy, su2double val_temperature_guess, + const su2double* val_scalars = nullptr) { + SetTDState_h(val_enthalpy, val_scalars); + return true; + } /*! * \brief Set fluid eddy viscosity provided by a turbulence model needed for computing effective thermal conductivity. diff --git a/SU2_CFD/include/fluid/CIncIdealGasPolynomial.hpp b/SU2_CFD/include/fluid/CIncIdealGasPolynomial.hpp index e6ef254337bb..2db636cf0d92 100644 --- a/SU2_CFD/include/fluid/CIncIdealGasPolynomial.hpp +++ b/SU2_CFD/include/fluid/CIncIdealGasPolynomial.hpp @@ -90,54 +90,73 @@ class CIncIdealGasPolynomial final : public CFluidModel { } /*! - * \brief Set the Dimensionless State using enthalpy. + * \brief Set the Dimensionless State using enthalpy, starting from the reference temperature. + * \note A failed iteration is not reported, use SolveTDState_h where that matters. * \param[in] val_enthalpy - Enthalpy value at the point. */ void SetTDState_h(su2double val_enthalpy, const su2double* val_scalars = nullptr) override { - Enthalpy = val_enthalpy; - /*--- convergence criterion for temperature in [K], high accuracy needed for restarts. ---*/ - const su2double toll = 1e-5; - su2double temp_iter = 300.0; - su2double Cp_iter = 0.0; - su2double delta_temp_iter = 1e10; - su2double delta_enthalpy_iter; - const int counter_limit = 20; + SolveTDState_h(val_enthalpy, 0.0, val_scalars); + } + + /*! + * \brief Set the Dimensionless State using enthalpy, by Newton-Raphson iteration on temperature. + * \param[in] val_enthalpy - Enthalpy value at the point. + * \param[in] val_temperature_guess - Start of the iteration, <= 0 to start from the reference temperature. + * \param[in] val_scalars - Scalar mass fractions (not used). + * \return True if the iteration converged above the minimum temperature. + */ + bool SolveTDState_h(su2double val_enthalpy, su2double val_temperature_guess, + const su2double* val_scalars = nullptr) override { + /*--- Convergence criterion for temperature, high accuracy needed for restarts. ---*/ + constexpr passivedouble tolerance = 1e-5; + constexpr int maxIterations = 20; - int counter = 0; + Enthalpy = val_enthalpy; + /*--- The guess is passive, the converged temperature does not depend on it. ---*/ + su2double temperature = (val_temperature_guess > 0.0) ? SU2_TYPE::GetValue(val_temperature_guess) + : Std_Ref_Temp_ND; + su2double Cp_iter = coeffs_[0]; + bool converged = false; + bool atMinimum = false; /*--- Compute temperature given enthalpy using Newton-Raphson. ---*/ - while ((abs(delta_temp_iter) > toll) && (counter++ < counter_limit)) { - /* Evaluate the new Cp and enthalpy from the coefficients and temperature. */ + for (int iter = 0; iter < maxIterations; ++iter) { + /*--- Evaluate Cp and the enthalpy from the coefficients at the current temperature. ---*/ Cp_iter = coeffs_[0]; - su2double Enthalpy_iter = coeffs_[0] * (temp_iter - Std_Ref_Temp_ND); + su2double Enthalpy_iter = coeffs_[0] * (temperature - Std_Ref_Temp_ND); su2double t_i = 1.0; su2double tref_i = 1.0; for (int i = 1; i < N; ++i) { - t_i *= temp_iter; + t_i *= temperature; tref_i *= Std_Ref_Temp_ND; Cp_iter += coeffs_[i] * t_i; - Enthalpy_iter += coeffs_[i] * (t_i * temp_iter - tref_i * Std_Ref_Temp_ND) / (i + 1); + Enthalpy_iter += coeffs_[i] * (t_i * temperature - tref_i * Std_Ref_Temp_ND) / (i + 1); } - delta_enthalpy_iter = Enthalpy - Enthalpy_iter; - - delta_temp_iter = delta_enthalpy_iter / Cp_iter; + const su2double delta = (Enthalpy - Enthalpy_iter) / Cp_iter; - temp_iter += delta_temp_iter; - if (temp_iter < Temperature_Min) { - cout << "Warning: Negative temperature has been found during Newton-Raphson." << endl; - temp_iter = Temperature_Min; + /*--- An overshoot below the minimum temperature is clamped and the iteration continues. With a positive Cp + the enthalpy increases with temperature, so a step below the minimum taken from the minimum itself means + the solution is below it, stop and report failure. ---*/ + if (temperature + delta < Temperature_Min) { + if (atMinimum) break; + temperature = Temperature_Min; + atMinimum = true; + continue; + } + temperature += delta; + atMinimum = false; + if (fabs(delta) <= tolerance) { + converged = true; break; } } - Temperature = temp_iter; + Temperature = temperature; Cp = Cp_iter; - if (counter == counter_limit) { - cout << "Warning: Newton-Raphson exceeds max. iterations in temperature computation." << endl; - } Density = Pressure / (Temperature * Gas_Constant); Cv = Cp / Gamma; + return converged; } private: diff --git a/SU2_CFD/include/solvers/CIncEulerSolver.hpp b/SU2_CFD/include/solvers/CIncEulerSolver.hpp index 8d6a9a489af6..5d35e04e66bb 100644 --- a/SU2_CFD/include/solvers/CIncEulerSolver.hpp +++ b/SU2_CFD/include/solvers/CIncEulerSolver.hpp @@ -101,8 +101,9 @@ class CIncEulerSolver : public CFVMFlowSolverBase; BoolVectorType strongBC; /*!< \brief Flag for boundary conditions to indicate if a strong BC has been applied, currently only used to keep track of farfield. */ + /*! + * \brief Set the pressure, temperature and density of a point from its solution. If the temperature cannot be + * found or the state is not physical, the solution is reverted to the old one. + * \param[in] iPoint - Point index. + * \param[in] FluidModel - Fluid model. + * \param[in] scalar - Scalar mass fractions, nullptr if there are none. + * \return False if the solution was reverted. + */ + bool SetThermodynamicState(unsigned long iPoint, CFluidModel* FluidModel, const su2double* scalar); + public: /*! * \brief Constructor of the class. diff --git a/SU2_CFD/src/fluid/CFluidCantera.cpp b/SU2_CFD/src/fluid/CFluidCantera.cpp index 4cb957406fc3..4b8b93a5b8bd 100644 --- a/SU2_CFD/src/fluid/CFluidCantera.cpp +++ b/SU2_CFD/src/fluid/CFluidCantera.cpp @@ -242,18 +242,21 @@ void CFluidCantera::SetTDState_T(const su2double val_temperature, const su2doubl } catch (const CanteraError& error) { SU2_MPI::Error(string("Cantera error in SetTDState_T: ") + error.what(), CURRENT_FUNCTION); } - temperatureIterationFailed = false; } void CFluidCantera::SetTDState_h(const su2double val_enthalpy, const su2double* val_scalars) { + SolveTDState_h(val_enthalpy, 0.0, val_scalars); +} + +bool CFluidCantera::SolveTDState_h(const su2double val_enthalpy, const su2double val_temperature_guess, + const su2double* val_scalars) { /*--- Temperature tolerance in [K], high accuracy needed for restarts. ---*/ constexpr double tolerance = 1e-5; constexpr int maxIterations = 20; SetMassFractions(val_scalars); const double enthalpy = SU2_TYPE::GetValue(val_enthalpy); - double temperature = temperatureGuess; - temperatureGuess = 0.0; + double temperature = SU2_TYPE::GetValue(val_temperature_guess); bool converged = false; try { @@ -283,9 +286,9 @@ void CFluidCantera::SetTDState_h(const su2double val_enthalpy, const su2double* } SetThermoState(temperature); } catch (const CanteraError& error) { - SU2_MPI::Error(string("Cantera error in SetTDState_h: ") + error.what(), CURRENT_FUNCTION); + SU2_MPI::Error(string("Cantera error in SolveTDState_h: ") + error.what(), CURRENT_FUNCTION); } - temperatureIterationFailed = !converged; + return converged; } #else diff --git a/SU2_CFD/src/solvers/CIncEulerSolver.cpp b/SU2_CFD/src/solvers/CIncEulerSolver.cpp index 08af0f78e72b..58d32a64f1ec 100644 --- a/SU2_CFD/src/solvers/CIncEulerSolver.cpp +++ b/SU2_CFD/src/solvers/CIncEulerSolver.cpp @@ -1057,7 +1057,8 @@ void CIncEulerSolver::CommonPreprocessing(CGeometry *geometry, CSolver **solver_ /*--- InnerIter is not reset while recording the discrete adjoint tape. ---*/ if (dual_time && (config->GetInnerIter() == 0 || AD::TapeActive())) { - RecomputeDensity_time_n(solver_container, config); + SU2_OMP_ATOMIC + ErrorCounter += RecomputeDensity_time_n(solver_container, config); } if ((iMesh == MESH_0) && (config->GetComm_Level() == COMM_FULL)) { @@ -1187,11 +1188,13 @@ unsigned long CIncEulerSolver::SetPrimitive_Variables(CSolver **solver_container return nonPhysicalPoints; } -void CIncEulerSolver::RecomputeDensity_time_n(CSolver **solver_container, const CConfig *config) { +unsigned long CIncEulerSolver::RecomputeDensity_time_n(CSolver **solver_container, const CConfig *config) { SU2_ZONE_SCOPED + unsigned long nonPhysicalPoints = 0; + /*--- Only variable-density (non-constant) cases allocate the density history. ---*/ - if (config->GetKind_DensityModel() == INC_DENSITYMODEL::CONSTANT) return; + if (config->GetKind_DensityModel() == INC_DENSITYMODEL::CONSTANT) return nonPhysicalPoints; const bool second_order = (config->GetTime_Marching() == TIME_MARCHING::DT_STEPPING_2ND); @@ -1200,27 +1203,34 @@ void CIncEulerSolver::RecomputeDensity_time_n(CSolver **solver_container, const /*--- The species solver only exists on the fine grid; MG with scalar-dependent density is rejected in CConfig. ---*/ const bool needs_scalars = (config->GetKind_Species_Model() != SPECIES_MODEL::NONE); - if (needs_scalars && speciesNodes == nullptr) return; + if (needs_scalars && speciesNodes == nullptr) return nonPhysicalPoints; SU2_OMP_FOR_STAT(omp_chunk_size) for (unsigned long iPoint = 0; iPoint < nPoint; iPoint++) { - /*--- Per-thread fluid model, mirroring the recipe in SetPrimitive_Variables. ---*/ + /*--- Per-thread fluid model. ---*/ CFluidModel* fluidModel = GetFluidModel(); + const su2double temperatureGuess = nodes->GetTemperature(iPoint); const su2double* scalar_n = speciesNodes ? speciesNodes->GetSolution_time_n(iPoint) : nullptr; const su2double Enthalpy_n = nodes->GetSolution_time_n(iPoint, nDim + 1); - fluidModel->SetTDState_h(Enthalpy_n, scalar_n); - nodes->SetDensity_time_n(iPoint, fluidModel->GetDensity()); + const bool converged_n = fluidModel->SolveTDState_h(Enthalpy_n, temperatureGuess, scalar_n); + nodes->SetDensity_time_n(iPoint, converged_n ? fluidModel->GetDensity() : nodes->GetDensity(iPoint)); + bool converged = converged_n; if (second_order) { const su2double* scalar_n1 = speciesNodes ? speciesNodes->GetSolution_time_n1(iPoint) : nullptr; const su2double Enthalpy_n1 = nodes->GetSolution_time_n1(iPoint, nDim + 1); - fluidModel->SetTDState_h(Enthalpy_n1, scalar_n1); - nodes->SetDensity_time_n1(iPoint, fluidModel->GetDensity()); + const bool converged_n1 = fluidModel->SolveTDState_h(Enthalpy_n1, temperatureGuess, scalar_n1); + nodes->SetDensity_time_n1(iPoint, converged_n1 ? fluidModel->GetDensity() : nodes->GetDensity(iPoint)); + converged = converged && converged_n1; } + + if (!converged) nonPhysicalPoints++; } END_SU2_OMP_FOR + + return nonPhysicalPoints; } void CIncEulerSolver::SetTime_Step(CGeometry *geometry, CSolver **solver_container, CConfig *config, @@ -1581,14 +1591,13 @@ bool CIncEulerSolver::ComputeConsistentExtrapolation(CFluidModel* fluidModel, un const su2double* scalar, su2double* primitive) { SU2_ZONE_SCOPED const CIncEulerVariable::CIndices prim_idx(nDim, 0); - const su2double enthalpy = primitive[prim_idx.Enthalpy()]; - fluidModel->SetTemperatureGuess(primitive[prim_idx.Temperature()]); - fluidModel->SetTDState_h(enthalpy, scalar); + const bool converged = fluidModel->SolveTDState_h(primitive[prim_idx.Enthalpy()], + primitive[prim_idx.Temperature()], scalar); primitive[prim_idx.Temperature()] = fluidModel->GetTemperature(); primitive[prim_idx.Density()] = fluidModel->GetDensity(); - return fluidModel->GetTemperatureIterationFailed(); + return !converged; } void CIncEulerSolver::Source_Residual(CGeometry *geometry, CSolver **solver_container, diff --git a/SU2_CFD/src/variables/CIncEulerVariable.cpp b/SU2_CFD/src/variables/CIncEulerVariable.cpp index 6054ba005349..4202e828c625 100644 --- a/SU2_CFD/src/variables/CIncEulerVariable.cpp +++ b/SU2_CFD/src/variables/CIncEulerVariable.cpp @@ -79,19 +79,19 @@ CIncEulerVariable::CIncEulerVariable(su2double pressure, const su2double *veloci } } -bool CIncEulerVariable::SetPrimVar(unsigned long iPoint, CFluidModel *FluidModel) { - - bool physical = true; +bool CIncEulerVariable::SetThermodynamicState(unsigned long iPoint, CFluidModel* FluidModel, const su2double* scalar) { /*--- Set the value of the pressure ---*/ SetPressure(iPoint); - su2double Enthalpy = Solution(iPoint, nDim +1); - FluidModel->SetTDState_h(Enthalpy); - su2double Temperature = FluidModel->GetTemperature(); + /*--- The temperature of the last valid state is the guess for both solves below. ---*/ + const su2double Temperature_Old = GetTemperature(iPoint); + + su2double Enthalpy = Solution(iPoint, nDim + 1); + const bool temperature_failed = !FluidModel->SolveTDState_h(Enthalpy, Temperature_Old, scalar); - const auto check_temp = SetTemperature(iPoint, Temperature, TemperatureLimits); + const auto check_temp = SetTemperature(iPoint, FluidModel->GetTemperature(), TemperatureLimits); /*--- Use the fluid model to compute the new value of density. Note that the thermodynamic pressure is constant and decoupled @@ -99,29 +99,33 @@ bool CIncEulerVariable::SetPrimVar(unsigned long iPoint, CFluidModel *FluidModel /*--- Set the value of the density ---*/ - const auto check_dens = SetDensity(iPoint, FluidModel->GetDensity()); + const auto check_dens = check_temp ? true : SetDensity(iPoint, FluidModel->GetDensity()); - /*--- Non-physical solution found. Revert to old values. ---*/ + if (!(check_dens || check_temp || temperature_failed)) return true; - if (check_dens || check_temp) { + /*--- Non-physical solution found. Revert to old values. ---*/ - /*--- Copy the old solution ---*/ + for (auto iVar = 0ul; iVar < nVar; iVar++) + Solution(iPoint, iVar) = Solution_Old(iPoint, iVar); - for (auto iVar = 0ul; iVar < nVar; iVar++) - Solution(iPoint, iVar) = Solution_Old(iPoint, iVar); + /*--- Recompute the primitive variables. The old solution was valid at Temperature_Old, if the + solve still fails keep that temperature rather than an unconverged state. ---*/ - /*--- Recompute the primitive variables ---*/ + Enthalpy = Solution(iPoint, nDim + 1); + if (!FluidModel->SolveTDState_h(Enthalpy, Temperature_Old, scalar) && Temperature_Old > 0.0) { + FluidModel->SetTDState_T(Temperature_Old, scalar); + } + SetTemperature(iPoint, FluidModel->GetTemperature(), TemperatureLimits); + SetDensity(iPoint, FluidModel->GetDensity()); - Enthalpy = Solution(iPoint, nDim+1); - FluidModel->SetTDState_h(Enthalpy); - SetTemperature(iPoint, FluidModel->GetTemperature(), TemperatureLimits); - SetDensity(iPoint, FluidModel->GetDensity()); + return false; +} - /*--- Flag this point as non-physical. ---*/ +bool CIncEulerVariable::SetPrimVar(unsigned long iPoint, CFluidModel *FluidModel) { - physical = false; + /*--- Pressure, temperature and density, the solution is reverted if the state is not physical. ---*/ - } + const bool physical = SetThermodynamicState(iPoint, FluidModel, nullptr); /*--- Set the value of the velocity and velocity^2 (requires density) ---*/ diff --git a/SU2_CFD/src/variables/CIncNSVariable.cpp b/SU2_CFD/src/variables/CIncNSVariable.cpp index 021ea34f4339..b32c691a8e5f 100644 --- a/SU2_CFD/src/variables/CIncNSVariable.cpp +++ b/SU2_CFD/src/variables/CIncNSVariable.cpp @@ -53,49 +53,9 @@ CIncNSVariable::CIncNSVariable(su2double pressure, const su2double *velocity, su bool CIncNSVariable::SetPrimVar(unsigned long iPoint, su2double eddy_visc, su2double turb_ke, CFluidModel *FluidModel, const su2double *scalar) { - bool physical = true; + /*--- Pressure, temperature and density, the solution is reverted if the state is not physical. ---*/ - /*--- Set the value of the pressure ---*/ - - SetPressure(iPoint); - - su2double Enthalpy = Solution(iPoint, nDim + 1); - FluidModel->SetTemperatureGuess(GetTemperature(iPoint)); - FluidModel->SetTDState_h(Enthalpy, scalar); - su2double Temperature = FluidModel->GetTemperature(); - const bool temperature_failed = FluidModel->GetTemperatureIterationFailed(); - - auto check_temp = SetTemperature(iPoint, Temperature, TemperatureLimits); - - /*--- Use the fluid model to compute the new value of density. - Note that the thermodynamic pressure is constant and decoupled - from the dynamic pressure being iterated. ---*/ - - /*--- Set the value of the density ---*/ - - const auto check_dens = check_temp ? true: SetDensity(iPoint, FluidModel->GetDensity()); - - /*--- Non-physical solution found. Revert to old values. ---*/ - - if (check_dens || check_temp || temperature_failed) { - - /*--- Copy the old solution ---*/ - - for (auto iVar = 0ul; iVar < nVar; iVar++) - Solution(iPoint,iVar) = Solution_Old(iPoint,iVar); - - /*--- Recompute the primitive variables ---*/ - - Enthalpy = Solution(iPoint, nDim + 1); - FluidModel->SetTDState_h(Enthalpy, scalar); - SetTemperature(iPoint, FluidModel->GetTemperature(), TemperatureLimits); - SetDensity(iPoint, FluidModel->GetDensity()); - - /*--- Flag this point as non-physical. ---*/ - - physical = false; - - } + const bool physical = SetThermodynamicState(iPoint, FluidModel, scalar); /*--- Set the value of the velocity and velocity^2 (requires density) ---*/ diff --git a/UnitTests/SU2_CFD/fluid/CFluidCantera_tests.cpp b/UnitTests/SU2_CFD/fluid/CFluidCantera_tests.cpp index 458eeee73f87..706f063ba1ef 100644 --- a/UnitTests/SU2_CFD/fluid/CFluidCantera_tests.cpp +++ b/UnitTests/SU2_CFD/fluid/CFluidCantera_tests.cpp @@ -99,24 +99,19 @@ TEST_CASE("Fluid_Cantera_LargeMechanism", "[Multicomponent_flow]") { fluid.SetTDState_T(1500.0, scalar); const su2double enthalpy = fluid.GetEnthalpy(); - fluid.SetTDState_h(enthalpy, scalar); - CHECK(fluid.GetTemperatureIterationFailed() == false); + CHECK(fluid.SolveTDState_h(enthalpy, 0.0, scalar)); CHECK(fluid.GetTemperature() == Approx(1500.0).margin(1e-4)); - /*--- The result does not depend on the temperature guess, which is used only once. ---*/ + /*--- The result does not depend on the temperature guess. ---*/ - fluid.SetTemperatureGuess(1490.0); - fluid.SetTDState_h(enthalpy, scalar); + CHECK(fluid.SolveTDState_h(enthalpy, 1490.0, scalar)); CHECK(fluid.GetTemperature() == Approx(1500.0).margin(1e-4)); - fluid.SetTemperatureGuess(5.0); - fluid.SetTDState_h(enthalpy, scalar); - CHECK(fluid.GetTemperatureIterationFailed() == false); + CHECK(fluid.SolveTDState_h(enthalpy, 5.0, scalar)); CHECK(fluid.GetTemperature() == Approx(1500.0).margin(1e-4)); - /*--- An enthalpy outside the range of the thermodynamic data is flagged. ---*/ + /*--- An enthalpy outside the range of the thermodynamic data is reported. ---*/ - fluid.SetTDState_h(1e12, scalar); - CHECK(fluid.GetTemperatureIterationFailed() == true); + CHECK_FALSE(fluid.SolveTDState_h(1e12, 0.0, scalar)); } TEST_CASE("Fluid_Cantera_SourceJacobian", "[Reacting_flow]") { /*--- Diagonal chemical source Jacobian of the one-step methane mechanism. ---*/ diff --git a/UnitTests/SU2_CFD/fluid/CFluidModel_tests.cpp b/UnitTests/SU2_CFD/fluid/CFluidModel_tests.cpp index 7ce2df6868ea..c84b39f95a63 100644 --- a/UnitTests/SU2_CFD/fluid/CFluidModel_tests.cpp +++ b/UnitTests/SU2_CFD/fluid/CFluidModel_tests.cpp @@ -30,6 +30,7 @@ #include "../../../SU2_CFD/include/fluid/CFluidModel.hpp" #include "../../../SU2_CFD/include/fluid/CIdealGas.hpp" #include "../../../SU2_CFD/include/fluid/CDataDrivenFluid.hpp" +#include "../../../SU2_CFD/include/fluid/CIncIdealGasPolynomial.hpp" void FluidModelChecks(CFluidModel* fluid_model, const su2double val_p, const su2double val_T) { /*--- Check consistency of reverse look-up ---*/ @@ -120,3 +121,57 @@ TEST_CASE("Test case for data-driven fluid model") { delete config; delete fluid_model; } + +TEST_CASE("Temperature from enthalpy for the polynomial incompressible ideal gas") { + std::stringstream config_options; + config_options << "SOLVER= INC_NAVIER_STOKES" << std::endl; + config_options << "INC_ENERGY_EQUATION= YES" << std::endl; + config_options << "INC_DENSITY_MODEL= VARIABLE" << std::endl; + config_options << "FLUID_MODEL= INC_IDEAL_GAS_POLY" << std::endl; + config_options << "CP_POLYCOEFFS= (1000.0, 0.2, 0.0, 0.0, 0.0)" << std::endl; + + CConfig config(config_options, SU2_COMPONENT::SU2_CFD, false); + + /*--- Dimensional case, the non-dimensional coefficients are the dimensional ones. ---*/ + for (unsigned short iCoeff = 0; iCoeff < N_POLY_COEFFS; iCoeff++) + config.SetCp_PolyCoeffND(config.GetCp_PolyCoeff(iCoeff), iCoeff); + + CIncIdealGasPolynomial fluid(287.058, 101325.0, STD_REF_TEMP); + fluid.SetCpModel(&config, STD_REF_TEMP); + + fluid.SetTDState_T(1200.0); + const su2double enthalpy = fluid.GetEnthalpy(); + + /*--- The temperature is recovered without a guess and from guesses on either side. ---*/ + for (const su2double guess : {0.0, 1100.0, 1500.0}) { + CHECK(fluid.SolveTDState_h(enthalpy, guess)); + CHECK(fluid.GetTemperature() == Approx(1200.0).margin(1e-4)); + } + + /*--- An enthalpy that requires a temperature below the minimum is reported. ---*/ + CHECK_FALSE(fluid.SolveTDState_h(-1e9, 0.0)); +} + +TEST_CASE("Temperature from enthalpy recovers from an overshoot below the minimum temperature") { + std::stringstream config_options; + config_options << "SOLVER= INC_NAVIER_STOKES" << std::endl; + config_options << "INC_ENERGY_EQUATION= YES" << std::endl; + config_options << "INC_DENSITY_MODEL= VARIABLE" << std::endl; + config_options << "FLUID_MODEL= INC_IDEAL_GAS_POLY" << std::endl; + config_options << "CP_POLYCOEFFS= (1000.0, -0.5, 0.0, 0.0, 0.0)" << std::endl; + + CConfig config(config_options, SU2_COMPONENT::SU2_CFD, false); + + for (unsigned short iCoeff = 0; iCoeff < N_POLY_COEFFS; iCoeff++) + config.SetCp_PolyCoeffND(config.GetCp_PolyCoeff(iCoeff), iCoeff); + + CIncIdealGasPolynomial fluid(287.058, 101325.0, STD_REF_TEMP); + fluid.SetCpModel(&config, STD_REF_TEMP); + + fluid.SetTDState_T(300.0); + const su2double enthalpy = fluid.GetEnthalpy(); + + /*--- Cp decreases with temperature, so the first step from a high guess goes below the minimum temperature. ---*/ + CHECK(fluid.SolveTDState_h(enthalpy, 1800.0)); + CHECK(fluid.GetTemperature() == Approx(300.0).margin(1e-4)); +}