diff --git a/Common/include/containers/CLookUpTable.hpp b/Common/include/containers/CLookUpTable.hpp index e455dd1f12d1..6307efe2fa65 100644 --- a/Common/include/containers/CLookUpTable.hpp +++ b/Common/include/containers/CLookUpTable.hpp @@ -386,6 +386,14 @@ class CLookUpTable { return limits_table_x[i_level]; } + /*! + * \brief Determine the minimum and maximum value of the third controlling variable. + * \returns Pair of the values of the first and last table level. + */ + inline std::pair GetTableLimitsZ() const { + return std::make_pair(z_values_levels.front(), z_values_levels.back()); + } + /*! * \brief Check whether requested set of variables are included in the table. */ diff --git a/SU2_CFD/include/fluid/CFluidFlamelet.hpp b/SU2_CFD/include/fluid/CFluidFlamelet.hpp index e59d176eeacc..5f3fb6d09199 100644 --- a/SU2_CFD/include/fluid/CFluidFlamelet.hpp +++ b/SU2_CFD/include/fluid/CFluidFlamelet.hpp @@ -78,6 +78,7 @@ class CFluidFlamelet final : public CFluidModel { MLPToolbox::CIOMap* iomap_PD; /*!< \brief Input-output map for the preferential diffusion scalars. */ MLPToolbox::CIOMap* iomap_Sources; /*!< \brief Input-output map for species source terms. */ MLPToolbox::CIOMap* iomap_LookUp; /*!< \brief Input-output map for passive look-up terms. */ + MLPToolbox::CIOMap* iomap_SourceJacobian; /*!< \brief Input-output map for the progress variable source Jacobian. */ MLPToolbox::CIOMap* iomap_Current; #endif @@ -93,6 +94,8 @@ class CFluidFlamelet final : public CFluidModel { val_vars_LookUp, /*!< \brief References passive look-up terms. */ val_vars_PD; /*!< \brief References to preferential diffusion scalars. */ + su2double source_pv_jac; /*!< \brief Derivative of the progress variable source with respect to the progress variable. */ + void PreprocessLookUp(CConfig* config); /*! \brief @@ -135,6 +138,13 @@ class CFluidFlamelet final : public CFluidModel { unsigned long EvaluateDataSet(const vector& input_scalar, unsigned short lookup_type, vector& output_refs); + /*! + * \brief Derivative of the progress variable source term with respect to the progress variable. + * \note Analytic for the MLP method, one-sided difference for the lookup table. + * \param[in] input_scalar - controlling variables used to interpolate manifold. + */ + su2double GetProgVarSourceJacobian(const vector& input_scalar) override; + /*! * \brief Check for out-of-bounds condition for data set interpolation. * \return - within bounds (0) or out of bounds (1). diff --git a/SU2_CFD/include/fluid/CFluidModel.hpp b/SU2_CFD/include/fluid/CFluidModel.hpp index af9aa3e059b7..29e8bfb89971 100644 --- a/SU2_CFD/include/fluid/CFluidModel.hpp +++ b/SU2_CFD/include/fluid/CFluidModel.hpp @@ -161,6 +161,12 @@ class CFluidModel { return 0; } + /*! + * \brief Derivative of the progress variable source term with respect to the progress variable. + * \param[in] input_scalar - data manifold query data. + */ + virtual su2double GetProgVarSourceJacobian(const vector& input_scalar) { return 0.0; } + /*! * \brief Get fluid dynamic viscosity. */ diff --git a/SU2_CFD/include/numerics/species/flamelet_edge_flux.hpp b/SU2_CFD/include/numerics/species/flamelet_edge_flux.hpp index dba70c948816..748dd720592f 100644 --- a/SU2_CFD/include/numerics/species/flamelet_edge_flux.hpp +++ b/SU2_CFD/include/numerics/species/flamelet_edge_flux.hpp @@ -56,9 +56,7 @@ class CScalarFlux_Flamelet final * \brief Preferential diffusion, two terms with the shape of the ordinary diffusion but of * states the model synthesises: div(D grad(beta - phi)) for each controlling variable, * and a thermal term div(beta_T D grad(T)) on the enthalpy equation. - * \note The thermal term has no implicit part, matching the treatment of the heat flux it - * models; the first term has the same thin shear layer Jacobian as the ordinary diffusion, - * because it is the same operator applied to a shifted state. + * \note The Jacobian uses d(beta - phi)/dphi and dT/dphi from the solver, each with the diffusivity of its flux. */ template FORCEINLINE void extraDiffusionTerms(const FlowIndices& idx, const ScalarFluxOptions& opt, Int iPoint, @@ -104,11 +102,16 @@ class CScalarFlux_Flamelet final if (!opt.oneSided) res.flux_j(iScalar) += D * projGrad; if (opt.implicit) { - res.jac_ii(iScalar, iScalar) += D * proj_on_rho_i; - if (!opt.oneSided) { - res.jac_ij(iScalar, iScalar) -= D * proj_on_rho_j; - res.jac_ji(iScalar, iScalar) -= D * proj_on_rho_i; - res.jac_jj(iScalar, iScalar) += D * proj_on_rho_j; + for (auto jScalar = 0u; jScalar < nControlVars; ++jScalar) { + const auto iEntry = iScalar * nControlVars + jScalar; + const Double dState_i = gatherVariables(iPoint, side_i.scalarNodes.GetPrefDiffJacobian(), iEntry); + const Double dState_j = gatherVariables(jPoint, side_j.scalarNodes.GetPrefDiffJacobian(), iEntry); + res.jac_ii(iScalar, jScalar) += D * proj_on_rho_i * dState_i; + if (!opt.oneSided) { + res.jac_ij(iScalar, jScalar) -= D * proj_on_rho_j * dState_j; + res.jac_ji(iScalar, jScalar) -= D * proj_on_rho_i * dState_i; + res.jac_jj(iScalar, jScalar) += D * proj_on_rho_j * dState_j; + } } } } @@ -132,6 +135,20 @@ class CScalarFlux_Flamelet final res.flux_i(I_ENTH) -= Dth * projGradT; if (!opt.oneSided) res.flux_j(I_ENTH) += Dth * projGradT; + + if (opt.implicit) { + const auto iFirst = nControlVars * nControlVars; + for (auto jScalar = 0u; jScalar < nControlVars; ++jScalar) { + const Double dT_i = gatherVariables(iPoint, side_i.scalarNodes.GetPrefDiffJacobian(), iFirst + jScalar); + const Double dT_j = gatherVariables(jPoint, side_j.scalarNodes.GetPrefDiffJacobian(), iFirst + jScalar); + res.jac_ii(I_ENTH, jScalar) += Dth * proj_on_rho_i * dT_i; + if (!opt.oneSided) { + res.jac_ij(I_ENTH, jScalar) -= Dth * proj_on_rho_j * dT_j; + res.jac_ji(I_ENTH, jScalar) -= Dth * proj_on_rho_i * dT_i; + res.jac_jj(I_ENTH, jScalar) += Dth * proj_on_rho_j * dT_j; + } + } + } } private: diff --git a/SU2_CFD/include/solvers/CSpeciesFlameletSolver.hpp b/SU2_CFD/include/solvers/CSpeciesFlameletSolver.hpp index 7543d6d7f282..5f9d6326dac2 100644 --- a/SU2_CFD/include/solvers/CSpeciesFlameletSolver.hpp +++ b/SU2_CFD/include/solvers/CSpeciesFlameletSolver.hpp @@ -45,6 +45,14 @@ class CSpeciesFlameletSolver final : public CSpeciesSolver { /*!< \brief Number of points outside the manifold domain, shared across OpenMP threads so it can be * accumulated atomically and reduced by the master thread alone in Preprocessing. */ unsigned long n_not_in_domain_local = 0; + bool pd_jacobian = false; /*!< \brief Whether the preferential diffusion Jacobian is evaluated this iteration. */ + passivedouble pd_jacobian_step[MAXNVAR] = {0}; /*!< \brief Finite difference step per controlling variable. */ + + /*! + * \brief Set the finite difference steps of the preferential diffusion Jacobian, a fraction of the global + * range of each controlling variable. + */ + void SetPrefDiffJacobianSteps(); /*! * \brief Compute the preconditioner for low-Mach flows. * \param[in] geometry - Geometrical definition of the problem. @@ -105,7 +113,8 @@ class CSpeciesFlameletSolver final : public CSpeciesSolver { const vector& scalars); /*! - * \brief Retrieve the preferential diffusion scalar values from manifold. + * \brief Retrieve the preferential diffusion scalar values from manifold, and their derivatives to the + * controlling variables when the scheme is implicit. * \param[in] fluid_model_local - pointer to flamelet fluid model. * \param[in] iPoint - node ID. * \param[in] scalars - local scalar solution. diff --git a/SU2_CFD/include/variables/CSpeciesFlameletVariable.hpp b/SU2_CFD/include/variables/CSpeciesFlameletVariable.hpp index 6a8b21969f12..a7a4b0f98842 100644 --- a/SU2_CFD/include/variables/CSpeciesFlameletVariable.hpp +++ b/SU2_CFD/include/variables/CSpeciesFlameletVariable.hpp @@ -39,6 +39,7 @@ class CSpeciesFlameletVariable final : public CSpeciesVariable { MatrixType lookup_scalar; /*!< \brief Vector of the source terms from the lookup table for each scalar equation */ su2vector table_misses; /*!< \brief Vector of lookup table misses. */ MatrixType source_cons_jac; /*!< \brief Consumption-rate Jacobian dS_aux_i/dY_aux_i = source_cons_i, one column per user scalar. */ + VectorType source_pv_jac; /*!< \brief Derivative of the progress variable source with respect to the progress variable. */ public: /*! @@ -102,4 +103,14 @@ class CSpeciesFlameletVariable final : public CSpeciesVariable { inline su2double GetAuxSourceCons(unsigned long iPoint, unsigned long i_aux) const { return source_cons_jac(iPoint, i_aux); } + + /*! + * \brief Store the derivative of the progress variable source with respect to the progress variable. + */ + inline void SetSourceJacobianPV(unsigned long iPoint, su2double val) { source_pv_jac(iPoint) = val; } + + /*! + * \brief Get the derivative of the progress variable source with respect to the progress variable. + */ + inline su2double GetSourceJacobianPV(unsigned long iPoint) const { return source_pv_jac(iPoint); } }; diff --git a/SU2_CFD/include/variables/CSpeciesVariable.hpp b/SU2_CFD/include/variables/CSpeciesVariable.hpp index 197cd9db04a6..edd8465f23ff 100644 --- a/SU2_CFD/include/variables/CSpeciesVariable.hpp +++ b/SU2_CFD/include/variables/CSpeciesVariable.hpp @@ -39,10 +39,23 @@ class CSpeciesVariable : public CScalarVariable { MatrixType SpeciesSourceTerm; /*!< \brief Matrix (nPoint, nVar) of chemical source terms for species transport*/ MatrixType SpeciesSourceJacobian; /*!< \brief Matrix (nPoint, nVar) of the diagonal chemical source Jacobian. */ VectorType HeatRelease; /*!< \brief Vector of heat release due to combustion for species transport*/ + MatrixType PrefDiffJacobian; /*!< \brief Matrix (nPoint, nCV*nCV + nCV): row-major d(beta_i - phi_i)/d(phi_j), then dT/d(phi_j). */ public: static constexpr size_t MAXNVAR = MAX_TRANSPORTED_SPECIES; /*!< \brief Max number of variables for static arrays. */ + /*! + * \brief Derivatives of the preferential diffusion states to the controlling variables, row-major per point. + */ + inline const MatrixType& GetPrefDiffJacobian() const { return PrefDiffJacobian; } + + /*! + * \brief Set one derivative: entry iCV*nCV + jCV of the beta block, or nCV*nCV + jCV for temperature. + */ + inline void SetPrefDiffJacobian(unsigned long iPoint, unsigned long iEntry, su2double val) { + PrefDiffJacobian(iPoint, iEntry) = val; + } + /*! * \brief Constructor of the class. * \param[in] species_inf - species variable values (initialization value). diff --git a/SU2_CFD/src/fluid/CFluidFlamelet.cpp b/SU2_CFD/src/fluid/CFluidFlamelet.cpp index f142da539175..1271725c08eb 100644 --- a/SU2_CFD/src/fluid/CFluidFlamelet.cpp +++ b/SU2_CFD/src/fluid/CFluidFlamelet.cpp @@ -114,6 +114,7 @@ CFluidFlamelet::~CFluidFlamelet() { delete iomap_TD; delete iomap_Sources; delete iomap_LookUp; + delete iomap_SourceJacobian; delete lookup_mlp; if (preferential_diffusion) delete iomap_PD; } @@ -244,6 +245,11 @@ void CFluidFlamelet::PreprocessLookUp(CConfig* config) { iomap_PD = new MLPToolbox::CIOMap(controlling_variable_names, varnames_PD); lookup_mlp->PairVariableswithMLPs(*iomap_PD); } + iomap_SourceJacobian = + new MLPToolbox::CIOMap(controlling_variable_names, {varnames_Sources[I_PROGVAR]}); + iomap_SourceJacobian->AddQueryJacobian(varnames_Sources[I_PROGVAR], controlling_variable_names[I_PROGVAR], + &source_pv_jac); + lookup_mlp->PairVariableswithMLPs(*iomap_SourceJacobian); #endif } else { for (auto iVar = 0u; iVar < varnames_TD.size(); iVar++) { @@ -322,6 +328,12 @@ unsigned long CFluidFlamelet::EvaluateDataSet(const vector& input_sca SU2_MPI::Error(string("Output vector size incompatible with manifold lookup operation."), CURRENT_FUNCTION); if (include_mixture_fraction) { inside = look_up_table->LookUp_XYZ(LUT_idx, output_refs, val_prog, val_enth, val_mixfrac); + /*--- Beyond the outer levels, beta_Z - Z keeps its boundary value. ---*/ + if (lookup_type == FLAMELET_LOOKUP_OPS::PREFDIF) { + const auto limits_z = look_up_table->GetTableLimitsZ(); + const su2double mixfrac_bound = min(max(val_mixfrac, limits_z.first), limits_z.second); + output_refs[FLAMELET_PREF_DIFF_SCALARS::I_BETA_MIXFRAC] += val_mixfrac - mixfrac_bound; + } } else { inside = look_up_table->LookUp_XY(LUT_idx, output_refs, val_prog, val_enth); } @@ -342,3 +354,42 @@ unsigned long CFluidFlamelet::EvaluateDataSet(const vector& input_sca AD::EndPreacc(); return extrapolation; } + +su2double CFluidFlamelet::GetProgVarSourceJacobian(const vector& input_scalar) { + const su2double val_prog = input_scalar[I_PROGVAR], val_enth = input_scalar[I_ENTH]; + const su2double val_mixfrac = include_mixture_fraction ? input_scalar[I_MIXFRAC] : 0.0; + switch (Kind_DataDriven_Method) { + case ENUM_DATADRIVEN_METHOD::LUT: { + const su2double delta = 1e-6; + const unsigned long idx = LUT_idx_Sources[I_PROGVAR]; + auto lookup = [&](const su2double pv, su2double& source) { + if (include_mixture_fraction) return look_up_table->LookUp_XYZ(idx, &source, pv, val_enth, val_mixfrac); + return look_up_table->LookUp_XY(idx, &source, pv, val_enth); + }; + /*--- Forward difference, backward at the manifold edge, zero for a point outside. ---*/ + su2double source, source_pert; + if (!lookup(val_prog, source)) { + source_pv_jac = 0.0; + } else if (lookup(val_prog + delta, source_pert)) { + source_pv_jac = (source_pert - source) / delta; + } else if (lookup(val_prog - delta, source_pert)) { + source_pv_jac = (source - source_pert) / delta; + } else { + source_pv_jac = 0.0; + } + break; + } + case ENUM_DATADRIVEN_METHOD::MLP: { +#ifdef USE_MLPCPP + su2double source; + vector refs_vars{&source}; + if (!lookup_mlp->Predict(*iomap_SourceJacobian, input_scalar, refs_vars)) source_pv_jac = 0.0; +#endif + break; + } + default: + source_pv_jac = 0.0; + break; + } + return source_pv_jac; +} diff --git a/SU2_CFD/src/solvers/CSpeciesFlameletSolver.cpp b/SU2_CFD/src/solvers/CSpeciesFlameletSolver.cpp index 9fdf81663b4e..43dcd57e8b8b 100644 --- a/SU2_CFD/src/solvers/CSpeciesFlameletSolver.cpp +++ b/SU2_CFD/src/solvers/CSpeciesFlameletSolver.cpp @@ -90,6 +90,14 @@ void CSpeciesFlameletSolver::Preprocessing(CGeometry* geometry, CSolver** solver SU2_OMP_SAFE_GLOBAL_ACCESS(config->SetGlobalParam(config->GetKind_Solver(), RunTime_EqSystem); n_not_in_domain_local = 0;) + BEGIN_SU2_OMP_SAFE_GLOBAL_ACCESS { + /*--- While recording, the Jacobian of the untaped pass before the recording is kept. ---*/ + pd_jacobian = flamelet_config_options.preferential_diffusion && + (config->GetKind_TimeIntScheme() == EULER_IMPLICIT) && !AD::TapeActive(); + if (pd_jacobian) SetPrefDiffJacobianSteps(); + } + END_SU2_OMP_SAFE_GLOBAL_ACCESS + /* Update global flame thickness value. */ if (calc_flame_thickness) { /*--- Every thread takes part in this, it reduces over the points of the rank and over ranks, @@ -440,10 +448,14 @@ void CSpeciesFlameletSolver::Source_Residual(CGeometry* geometry, CSolver** solv /*--- Implicit: analytic Jacobian for auxiliary species from the split source form. * S_aux_i = source_prod_i + source_cons_i * Y_aux_i * dS_aux_i/dY_aux_i = source_cons_i - * J_ii += -source_cons_i * V ---*/ + * J_ii += -source_cons_i / rho * V + * Progress variable: only the decreasing part of its source. ---*/ if (implicit) { + const su2double density = solver_container[FLOW_SOL]->GetNodes()->GetDensity(i_point); + const su2double dsource_dpv = fn->GetSourceJacobianPV(i_point); + Jacobian.AddVal2Diag(i_point, I_PROGVAR, fmax(0.0, -dsource_dpv) / density * volume); for (auto i_aux = 0u; i_aux < n_aux; i_aux++) { - Jacobian.AddVal2Diag(i_point, n_CV + i_aux, -fn->GetAuxSourceCons(i_point, i_aux) * volume); + Jacobian.AddVal2Diag(i_point, n_CV + i_aux, -fn->GetAuxSourceCons(i_point, i_aux) / density * volume); } } } @@ -649,6 +661,13 @@ unsigned long CSpeciesFlameletSolver::SetScalarSources(const CConfig* config, CF vector source_scalar(flamelet_config_options.n_scalars); for (auto iCV = 0u; iCV < flamelet_config_options.n_control_vars; iCV++) source_scalar[iCV] = table_sources[iCV]; + /*--- Derivative of the progress variable source for the implicit Jacobian, zero where the source is clipped. ---*/ + if ((config->GetKind_TimeIntScheme() == EULER_IMPLICIT) && !AD::TapeActive()) { + const su2double dsource = + (table_sources[I_PROGVAR] > 0) ? fluid_model_local->GetProgVarSourceJacobian(scalars) : su2double(0.0); + static_cast(nodes)->SetSourceJacobianPV(iPoint, F * dsource); + } + /*--- Source term for the auxiliary species transport equations. ---*/ for (size_t i_aux = 0; i_aux < flamelet_config_options.n_user_scalars; i_aux++) { /*--- The source term for the auxiliary equations consists of a production term and a consumption term: @@ -696,9 +715,98 @@ unsigned long CSpeciesFlameletSolver::SetPreferentialDiffusionScalars(CFluidMode for (auto i_beta = 0u; i_beta < FLAMELET_PREF_DIFF_SCALARS::N_BETA_TERMS; i_beta++) { nodes->SetAuxVar(iPoint, i_beta, beta_scalar[i_beta]); } + if (!pd_jacobian) return misses; + + const auto n_cv = flamelet_config_options.n_control_vars; + if (misses != 0) { + for (auto i_cv = 0u; i_cv < n_cv; i_cv++) { + for (auto j_cv = 0u; j_cv < n_cv; j_cv++) + nodes->SetPrefDiffJacobian(iPoint, i_cv * n_cv + j_cv, (i_cv == j_cv) ? su2double(1.0) : su2double(0.0)); + nodes->SetPrefDiffJacobian(iPoint, n_cv * n_cv + i_cv, 0.0); + } + return misses; + } + + /*--- Central differences of beta and temperature, one-sided at the manifold edge. ---*/ + auto beta_index = [](int i_cv) { + if (i_cv == I_PROGVAR) return I_BETA_PROGVAR; + if (i_cv == I_ENTH) return I_BETA_ENTH; + return I_BETA_MIXFRAC; + }; + const su2double temperature = fluid_model_local->GetTemperature(); + vector scalars_fd(scalars), beta_plus(beta_scalar.size()), beta_minus(beta_scalar.size()); + + for (auto j_cv = 0u; j_cv < n_cv; j_cv++) { + const su2double step = pd_jacobian_step[j_cv]; + scalars_fd[j_cv] = scalars[j_cv] + step; + bool inside_plus = fluid_model_local->EvaluateDataSet(scalars_fd, FLAMELET_LOOKUP_OPS::PREFDIF, beta_plus) == 0; + fluid_model_local->SetTDState_T(temperature, scalars_fd.data()); + inside_plus = inside_plus && (fluid_model_local->GetExtrapolation() == 0); + su2double temperature_plus = fluid_model_local->GetTemperature(); + scalars_fd[j_cv] = scalars[j_cv] - step; + bool inside_minus = fluid_model_local->EvaluateDataSet(scalars_fd, FLAMELET_LOOKUP_OPS::PREFDIF, beta_minus) == 0; + fluid_model_local->SetTDState_T(temperature, scalars_fd.data()); + inside_minus = inside_minus && (fluid_model_local->GetExtrapolation() == 0); + su2double temperature_minus = fluid_model_local->GetTemperature(); + scalars_fd[j_cv] = scalars[j_cv]; + + su2double span = 2 * step; + if (!inside_plus) { + beta_plus = beta_scalar; + temperature_plus = temperature; + span = step; + } + if (!inside_minus) { + beta_minus = beta_scalar; + temperature_minus = temperature; + span = step; + } + const bool usable = inside_plus || inside_minus; + + for (auto i_cv = 0u; i_cv < n_cv; i_cv++) { + su2double value = (i_cv == j_cv) ? su2double(1.0) : su2double(0.0); + if (usable) { + const auto i_beta = beta_index(i_cv); + value = (beta_plus[i_beta] - beta_minus[i_beta]) / span - ((i_cv == j_cv) ? 1.0 : 0.0); + /*--- The total diagonal, ordinary diffusion included, is kept positive. ---*/ + if (i_cv == j_cv) value = max(value, su2double(-0.9)); + } + nodes->SetPrefDiffJacobian(iPoint, i_cv * n_cv + j_cv, value); + } + const su2double d_temperature = usable ? (temperature_plus - temperature_minus) / span : su2double(0.0); + nodes->SetPrefDiffJacobian(iPoint, n_cv * n_cv + j_cv, d_temperature); + } + fluid_model_local->SetTDState_T(temperature, scalars.data()); return misses; } +void CSpeciesFlameletSolver::SetPrefDiffJacobianSteps() { + const auto n_cv = flamelet_config_options.n_control_vars; + /*--- su2double buffers, since MPI_DOUBLE is the AD type in AD builds. ---*/ + su2double local_min[MAXNVAR] = {0}, local_max[MAXNVAR] = {0}, global_min[MAXNVAR] = {0}, + global_max[MAXNVAR] = {0}; + for (auto i_cv = 0u; i_cv < n_cv; i_cv++) { + local_min[i_cv] = std::numeric_limits::max(); + local_max[i_cv] = std::numeric_limits::lowest(); + } + for (auto i_point = 0u; i_point < nPointDomain; i_point++) { + for (auto i_cv = 0u; i_cv < n_cv; i_cv++) { + const passivedouble value = SU2_TYPE::GetValue(nodes->GetSolution(i_point, i_cv)); + local_min[i_cv] = min(SU2_TYPE::GetValue(local_min[i_cv]), value); + local_max[i_cv] = max(SU2_TYPE::GetValue(local_max[i_cv]), value); + } + } + SU2_MPI::Allreduce(local_min, global_min, n_cv, MPI_DOUBLE, MPI_MIN, SU2_MPI::GetComm()); + SU2_MPI::Allreduce(local_max, global_max, n_cv, MPI_DOUBLE, MPI_MAX, SU2_MPI::GetComm()); + /*--- The magnitude floor keeps the step finite when the field is still uniform. ---*/ + for (auto i_cv = 0u; i_cv < n_cv; i_cv++) { + const passivedouble g_min = SU2_TYPE::GetValue(global_min[i_cv]); + const passivedouble g_max = SU2_TYPE::GetValue(global_max[i_cv]); + const passivedouble magnitude = max(fabs(g_min), fabs(g_max)); + pd_jacobian_step[i_cv] = max({1e-3 * (g_max - g_min), 1e-5 * magnitude, 1e-10}); + } +} + void CSpeciesFlameletSolver::Upwind_Residual(CGeometry* geometry, CSolver** solver_container, CNumerics**, CConfig* config, unsigned short iMesh) { SU2_ZONE_SCOPED @@ -757,15 +865,39 @@ su2double CSpeciesFlameletSolver::GetBurntProgressVariable(CFluidModel* fluid_mo SU2_ZONE_SCOPED su2double scalars[MAXNVAR], delta = 1e-3; for (auto iVar = 0u; iVar < nVar; iVar++) scalars[iVar] = scalar_solution[iVar]; - bool outside = false; - scalars[I_PROGVAR] += delta; - while (!outside) { - /*--- Note that 300.0 is a dummy temperature here and not used. ---*/ + + /*--- Note that 300.0 is a dummy temperature here and not used. ---*/ + auto inside = [&](su2double pv) { + scalars[I_PROGVAR] = pv; fluid_model->SetTDState_T(300.0, scalars); - if ((fluid_model->GetExtrapolation() == 1) || fluid_model->GetTemperature() > T_ignition) outside = true; - scalars[I_PROGVAR] += delta; + return fluid_model->GetExtrapolation() == 0; + }; + + /*--- March up while the state stays inside the manifold, until the ignition temperature is exceeded. ---*/ + const su2double pv_unburnt = scalar_solution[I_PROGVAR]; + su2double pv_burnt = pv_unburnt, pv_trial = pv_unburnt + delta; + bool ignited = false; + while (!ignited && inside(pv_trial)) { + pv_burnt = pv_trial; + ignited = fluid_model->GetTemperature() > T_ignition; + pv_trial += delta; + } + + /*--- The march left the manifold: bisect to its edge and keep a margin inside it. ---*/ + if (!ignited) { + su2double pv_out = pv_trial; + for (int iter = 0; iter < 30; iter++) { + const su2double pv_mid = 0.5 * (pv_burnt + pv_out); + if (inside(pv_mid)) + pv_burnt = pv_mid; + else + pv_out = pv_mid; + } + const su2double pv_margin = pv_burnt - 1e-3 * (pv_burnt - pv_unburnt); + if (inside(pv_margin)) pv_burnt = pv_margin; } - su2double pv_burnt = scalars[I_PROGVAR] - delta; + inside(pv_burnt); + if (rank == MASTER_NODE) { cout << "Burnt progress variable determined from flamelet table: " << pv_burnt << endl; cout << "Burnt temperature from flamelet table: " << fluid_model->GetTemperature() << endl; diff --git a/SU2_CFD/src/variables/CSpeciesFlameletVariable.cpp b/SU2_CFD/src/variables/CSpeciesFlameletVariable.cpp index fabe59a038aa..69f46c09744f 100644 --- a/SU2_CFD/src/variables/CSpeciesFlameletVariable.cpp +++ b/SU2_CFD/src/variables/CSpeciesFlameletVariable.cpp @@ -54,10 +54,16 @@ CSpeciesFlameletVariable::CSpeciesFlameletVariable(const su2double* species_inf, lookup_scalar.resize(nPoint, flamelet_config_options.n_lookups) = su2double(0.0); table_misses.resize(nPoint) = 0; source_cons_jac.resize(nPoint, flamelet_config_options.n_user_scalars) = su2double(0.0); + source_pv_jac.resize(nPoint) = su2double(0.0); if (flamelet_config_options.preferential_diffusion) { nAuxVar = FLAMELET_PREF_DIFF_SCALARS::N_BETA_TERMS; AuxVar.resize(nPoint, FLAMELET_PREF_DIFF_SCALARS::N_BETA_TERMS) = su2double(0.0); Grad_AuxVar.resize(nPoint, FLAMELET_PREF_DIFF_SCALARS::N_BETA_TERMS, nDim, 0.0); + const auto n_cv = flamelet_config_options.n_control_vars; + PrefDiffJacobian.resize(nPoint, static_cast(n_cv) * n_cv + n_cv) = su2double(0.0); + /*--- Identity beta block, the value used where no finite difference is available. ---*/ + for (auto iPoint = 0ul; iPoint < nPoint; iPoint++) + for (auto i_cv = 0u; i_cv < n_cv; i_cv++) PrefDiffJacobian(iPoint, i_cv * n_cv + i_cv) = 1.0; } } diff --git a/TestCases/parallel_regression.py b/TestCases/parallel_regression.py index e5fd6ed2db9f..353a991b2fd2 100755 --- a/TestCases/parallel_regression.py +++ b/TestCases/parallel_regression.py @@ -69,7 +69,7 @@ def main(): cfd_flamelet_ch4.cfg_dir = "flamelet/01_laminar_premixed_ch4_flame_cfd" cfd_flamelet_ch4.cfg_file = "lam_prem_ch4_cfd.cfg" cfd_flamelet_ch4.test_iter = 10 - cfd_flamelet_ch4.test_vals = [-10.539588, -12.650786, -5.649832, -13.470491, -17.249262, 94.854000] + cfd_flamelet_ch4.test_vals = [-10.539588, -12.650795, -5.649832, -13.470508, -17.249269, 94.854000] cfd_flamelet_ch4.new_output = True test_list.append(cfd_flamelet_ch4) @@ -78,7 +78,7 @@ def main(): cfd_flamelet_ch4_axi.cfg_dir = "flamelet/05_laminar_premixed_ch4_flame_cfd_axi" cfd_flamelet_ch4_axi.cfg_file = "lam_prem_ch4_cfd_axi.cfg" cfd_flamelet_ch4_axi.test_iter = 10 - cfd_flamelet_ch4_axi.test_vals = [-9.774148, -11.256092, -10.017960, -11.850827, -4.916267, 72.669000] + cfd_flamelet_ch4_axi.test_vals = [-9.774149, -11.256095, -10.017959, -11.850546, -4.916270, 72.669000] cfd_flamelet_ch4_axi.new_output = True test_list.append(cfd_flamelet_ch4_axi) @@ -88,7 +88,7 @@ def main(): cfd_flamelet_ch4_partial_premix.cfg_dir = "flamelet/06_laminar_partial_premixed_ch4_flame_cfd" cfd_flamelet_ch4_partial_premix.cfg_file = "lam_partial_prem_ch4_cfd.cfg" cfd_flamelet_ch4_partial_premix.test_iter = 10 - cfd_flamelet_ch4_partial_premix.test_vals = [-10.482722, -3.621055, -12.932224, -10.154391, 10.000000] + cfd_flamelet_ch4_partial_premix.test_vals = [-10.482684, -3.621055, -12.932224, -10.154389, 10.000000] cfd_flamelet_ch4_partial_premix.new_output = True test_list.append(cfd_flamelet_ch4_partial_premix) @@ -97,7 +97,7 @@ def main(): cfd_flamelet_h2.cfg_dir = "flamelet/07_laminar_premixed_h2_flame_cfd" cfd_flamelet_h2.cfg_file = "laminar_premixed_h2_flame_cfd.cfg" cfd_flamelet_h2.test_iter = 5 - cfd_flamelet_h2.test_vals = [-8.036799, -8.329782, -1.805324, -8.959656] + cfd_flamelet_h2.test_vals = [-8.036777, -8.366742, -1.870716, -9.961387] test_list.append(cfd_flamelet_h2) # Flame ignition methods @@ -105,7 +105,7 @@ def main(): flame_init_methods.cfg_dir = "flamelet/08_flame_init_method" flame_init_methods.cfg_file ="flamelet_initialization.py" flame_init_methods.command = TestCase.Command("mpirun -np 2", "python", "flamelet_initialization.py") - flame_init_methods.test_vals=[1600, 1600] + flame_init_methods.test_vals=[1600, 1547.97] flame_init_methods.new_output = True test_list.append(flame_init_methods) @@ -114,7 +114,7 @@ def main(): cfd_flamelet_ch4_unsteady.cfg_dir = "flamelet/09_laminar_premixed_ch4_flame_unsteady" cfd_flamelet_ch4_unsteady.cfg_file = "lam_prem_ch4_unsteady.cfg" cfd_flamelet_ch4_unsteady.test_iter = 5 - cfd_flamelet_ch4_unsteady.test_vals = [-8.856677, -8.095263, -9.153743, -9.321676] + cfd_flamelet_ch4_unsteady.test_vals = [-8.853384, -8.095207, -9.153687, -9.321693] cfd_flamelet_ch4_unsteady.test_vals_aarch64 = [-8.855500, -8.095195, -9.153704, -9.321686] test_list.append(cfd_flamelet_ch4_unsteady) @@ -2180,7 +2180,7 @@ def main(): cfd_flamelet_ch4_cht.cfg_dir = "flamelet/03_laminar_premixed_ch4_flame_cht_cfd" cfd_flamelet_ch4_cht.cfg_file = "lam_prem_ch4_cht_cfd_master.cfg" cfd_flamelet_ch4_cht.test_iter = 5 - cfd_flamelet_ch4_cht.test_vals = [-7.223149, -6.338839, -8.334392, -10.410156, -3.171365, -10.884464, -12.615132, -6.614396] + cfd_flamelet_ch4_cht.test_vals = [-7.223149, -6.338839, -8.334392, -10.410206, -3.171365, -10.884554, -12.615132, -6.614396] cfd_flamelet_ch4_cht.timeout = 1600 cfd_flamelet_ch4_cht.multizone = True test_list.append(cfd_flamelet_ch4_cht) diff --git a/TestCases/parallel_regression_AD.py b/TestCases/parallel_regression_AD.py index b9bf3f91468e..a02d222e47d2 100644 --- a/TestCases/parallel_regression_AD.py +++ b/TestCases/parallel_regression_AD.py @@ -381,7 +381,7 @@ def main(): discadj_flamelet_ch4_hx.cfg_file = "lam_prem_ch4_hx_ad.cfg" discadj_flamelet_ch4_hx.multizone = False discadj_flamelet_ch4_hx.test_iter = 10 - discadj_flamelet_ch4_hx.test_vals = [-9.083502, -9.033520, -9.529644, -8.442386, -15.399568, -6.169865, -18.881156] + discadj_flamelet_ch4_hx.test_vals = [-9.079012, -9.032987, -9.517245, -7.478949, -15.369708, -6.169865, -18.876518] test_list.append(discadj_flamelet_ch4_hx) # 2D planar laminar premixed flame on isothermal burner with conjugate heat transfer (restart) @@ -390,7 +390,7 @@ def main(): discadj_flamelet_ch4_cht.cfg_file = "lam_prem_ch4_cht_ad_master.cfg" discadj_flamelet_ch4_cht.multizone = True discadj_flamelet_ch4_cht.test_iter = 10 - discadj_flamelet_ch4_cht.test_vals = [-1.545058, 0.628666, -6.533534, -18.651078, -18.648552, -3.794724, -6.572494, 11.000000] + discadj_flamelet_ch4_cht.test_vals = [-1.545086, 0.628721, -6.533534, -18.651002, -18.648552, -3.794724, -6.572494, 11.000000] test_list.append(discadj_flamelet_ch4_cht) ###################################### diff --git a/TestCases/tutorials.py b/TestCases/tutorials.py index 707f08851a2f..dbf2f1ab88ae 100644 --- a/TestCases/tutorials.py +++ b/TestCases/tutorials.py @@ -177,7 +177,7 @@ def main(): premixed_hydrogen.cfg_dir = "../Tutorials/incompressible_flow/Inc_Combustion/1__premixed_hydrogen" premixed_hydrogen.cfg_file = "H2_burner.cfg" premixed_hydrogen.test_iter = 10 - premixed_hydrogen.test_vals = [-8.769581, -9.719802, -10.162061, -3.439369, -11.529638] + premixed_hydrogen.test_vals = [-8.769614, -9.720531, -10.308356, -3.720389, -12.006045] test_list.append(premixed_hydrogen) ### Compressible Flow