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/Common/include/option_structure.hpp b/Common/include/option_structure.hpp index dddd503d9890..fb4f8bcc8e06 100644 --- a/Common/include/option_structure.hpp +++ b/Common/include/option_structure.hpp @@ -1546,6 +1546,8 @@ enum FLAMELET_LOOKUP_OPS { PREFDIF, /*!< \brief Preferential diffusion scalars. */ SOURCES, /*!< \brief Scalar source terms (controlling variables, passive species).*/ LOOKUP, /*!< \brief Passive look-up variables specified in config. */ + SOURCES_JACOBIAN, /*!< \brief Scalar source terms and the derivative of the progress variable source. */ + PREFDIF_JACOBIAN, /*!< \brief Preferential diffusion scalars and their derivatives to the controlling variables. */ }; /*! diff --git a/SU2_CFD/include/fluid/CFluidFlamelet.hpp b/SU2_CFD/include/fluid/CFluidFlamelet.hpp index e59d176eeacc..1f827be3314b 100644 --- a/SU2_CFD/include/fluid/CFluidFlamelet.hpp +++ b/SU2_CFD/include/fluid/CFluidFlamelet.hpp @@ -78,6 +78,9 @@ 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_SourcesJacobian; /*!< \brief Map for source terms and the progress variable source Jacobian. */ + MLPToolbox::CIOMap* iomap_PDJacobian; /*!< \brief Map for preferential diffusion scalars and their Jacobian. */ + MLPToolbox::CIOMap* iomap_TempJacobian; /*!< \brief Map for the temperature and its Jacobian. */ MLPToolbox::CIOMap* iomap_Current; #endif @@ -93,6 +96,13 @@ 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 = 0.0; /*!< \brief Derivative of the progress variable source to the progress variable. */ + + vector pd_jac_beta, /*!< \brief Row-major d(beta_i)/d(phi_j) of the MLP, zero where unsupported. */ + pd_jac_temperature, /*!< \brief dT/d(phi_j) of the MLP, zero where unsupported. */ + pd_values_temperature; /*!< \brief Temperature returned alongside the MLP Jacobian. */ + vector pd_refs_temperature; /*!< \brief Reference to the temperature value above. */ + void PreprocessLookUp(CConfig* config); /*! \brief @@ -135,6 +145,26 @@ class CFluidFlamelet final : public CFluidModel { unsigned long EvaluateDataSet(const vector& input_scalar, unsigned short lookup_type, vector& output_refs); + /*! + * \brief Derivatives of the preferential diffusion scalars and of the temperature to the controlling variables. + * \note The thermodynamic state must be set for input_scalar. For MLP, dbeta is taken from the preceding + * EvaluateDataSet with PREFDIF_JACOBIAN at input_scalar. Central differences for LUT. + * \param[in] input_scalar - controlling variables used to interpolate manifold. + * \param[in] beta - preferential diffusion scalars at input_scalar. + * \param[in] step - finite difference step per controlling variable. + * \param[out] dbeta - row-major d(beta_i)/d(phi_j), with beta_i the scalar of controlling variable i. + * \param[out] dtemperature - dT/d(phi_j). + * \param[out] usable - whether column j holds a derivative, false at the edge of the manifold. + */ + void EvaluatePrefDiffJacobian(const vector& input_scalar, const vector& beta, + const passivedouble* step, su2double* dbeta, su2double* dtemperature, bool* usable); + + /*! + * \brief Derivative of the progress variable source with respect to the progress variable. + * \note Set by EvaluateDataSet with SOURCES_JACOBIAN, zero where the source is not positive. + */ + inline su2double GetChemicalSourceJacobian(int iVar) override { return (iVar == I_PROGVAR) ? source_pv_jac : 0.0; } + /*! * \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/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/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..19d9416a0bca 100644 --- a/SU2_CFD/src/fluid/CFluidFlamelet.cpp +++ b/SU2_CFD/src/fluid/CFluidFlamelet.cpp @@ -107,6 +107,13 @@ CFluidFlamelet::CFluidFlamelet(CConfig* config, su2double value_pressure_operati } } +/*--- Index of the preferential diffusion scalar belonging to a controlling variable. ---*/ +static unsigned short BetaIndex(unsigned short i_cv) { + if (i_cv == I_PROGVAR) return I_BETA_PROGVAR; + if (i_cv == I_ENTH) return I_BETA_ENTH; + return I_BETA_MIXFRAC; +} + CFluidFlamelet::~CFluidFlamelet() { if (Kind_DataDriven_Method == ENUM_DATADRIVEN_METHOD::LUT) delete look_up_table; #ifdef USE_MLPCPP @@ -114,8 +121,13 @@ CFluidFlamelet::~CFluidFlamelet() { delete iomap_TD; delete iomap_Sources; delete iomap_LookUp; + delete iomap_SourcesJacobian; delete lookup_mlp; - if (preferential_diffusion) delete iomap_PD; + if (preferential_diffusion) { + delete iomap_PD; + delete iomap_PDJacobian; + delete iomap_TempJacobian; + } } #endif } @@ -244,6 +256,47 @@ void CFluidFlamelet::PreprocessLookUp(CConfig* config) { iomap_PD = new MLPToolbox::CIOMap(controlling_variable_names, varnames_PD); lookup_mlp->PairVariableswithMLPs(*iomap_PD); } + iomap_SourcesJacobian = new MLPToolbox::CIOMap(controlling_variable_names, varnames_Sources); + if (!noSource(varnames_Sources[I_PROGVAR])) + iomap_SourcesJacobian->AddQueryJacobian(varnames_Sources[I_PROGVAR], controlling_variable_names[I_PROGVAR], + &source_pv_jac); + lookup_mlp->PairVariableswithMLPs(*iomap_SourcesJacobian); + + if (preferential_diffusion) { + /*--- A derivative is queried only where a network has the controlling variable as input, else it is zero. ---*/ + auto has_derivative = [](const MLPToolbox::CIOMap& map, const string& output, const string& input) { + for (const auto& network : map.GetNetworksInQuery()) { + const auto network_inputs = network.MLP->GetInputVars(); + const auto network_outputs = network.MLP->GetOutputVars(); + if (find(network_outputs.begin(), network_outputs.end(), output) != network_outputs.end() && + find(network_inputs.begin(), network_inputs.end(), input) != network_inputs.end()) + return true; + } + return false; + }; + + pd_jac_beta.assign(n_control_vars * n_control_vars, 0.0); + pd_jac_temperature.assign(n_control_vars, 0.0); + pd_values_temperature.assign(1, 0.0); + pd_refs_temperature = {&pd_values_temperature[0]}; + + vector beta_names; + for (auto i_cv = 0u; i_cv < n_control_vars; i_cv++) beta_names.push_back(varnames_PD[BetaIndex(i_cv)]); + iomap_PDJacobian = new MLPToolbox::CIOMap(controlling_variable_names, varnames_PD); + iomap_TempJacobian = new MLPToolbox::CIOMap(controlling_variable_names, {varnames_TD[LOOKUP_TD::TEMPERATURE]}); + for (auto j_cv = 0u; j_cv < n_control_vars; j_cv++) { + for (auto i_cv = 0u; i_cv < n_control_vars; i_cv++) { + if (has_derivative(*iomap_PD, beta_names[i_cv], controlling_variable_names[j_cv])) + iomap_PDJacobian->AddQueryJacobian(beta_names[i_cv], controlling_variable_names[j_cv], + &pd_jac_beta[i_cv * n_control_vars + j_cv]); + } + if (has_derivative(*iomap_TD, varnames_TD[LOOKUP_TD::TEMPERATURE], controlling_variable_names[j_cv])) + iomap_TempJacobian->AddQueryJacobian(varnames_TD[LOOKUP_TD::TEMPERATURE], controlling_variable_names[j_cv], + &pd_jac_temperature[j_cv]); + } + lookup_mlp->PairVariableswithMLPs(*iomap_PDJacobian); + lookup_mlp->PairVariableswithMLPs(*iomap_TempJacobian); + } #endif } else { for (auto iVar = 0u; iVar < varnames_TD.size(); iVar++) { @@ -285,6 +338,10 @@ unsigned long CFluidFlamelet::EvaluateDataSet(const vector& input_sca vector val_vars; vector refs_vars; vector LUT_idx; + const bool with_source_jacobian = (lookup_type == FLAMELET_LOOKUP_OPS::SOURCES_JACOBIAN); + if (with_source_jacobian) source_pv_jac = 0.0; + if (lookup_type == FLAMELET_LOOKUP_OPS::PREFDIF_JACOBIAN) + fill(pd_jac_beta.begin(), pd_jac_beta.end(), su2double(0.0)); switch (lookup_type) { case FLAMELET_LOOKUP_OPS::THERMO: LUT_idx = LUT_idx_TD; @@ -302,6 +359,18 @@ unsigned long CFluidFlamelet::EvaluateDataSet(const vector& input_sca LUT_idx = LUT_idx_Sources; #ifdef USE_MLPCPP iomap_Current = iomap_Sources; +#endif + break; + case FLAMELET_LOOKUP_OPS::PREFDIF_JACOBIAN: + LUT_idx = LUT_idx_PD; +#ifdef USE_MLPCPP + iomap_Current = iomap_PDJacobian; +#endif + break; + case FLAMELET_LOOKUP_OPS::SOURCES_JACOBIAN: + LUT_idx = LUT_idx_Sources; +#ifdef USE_MLPCPP + iomap_Current = iomap_SourcesJacobian; #endif break; case FLAMELET_LOOKUP_OPS::LOOKUP: @@ -322,9 +391,30 @@ 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); } + if (with_source_jacobian && inside) { + /*--- Forward difference, backward at the manifold edge. ---*/ + 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); + }; + su2double source_pert; + if (lookup(val_prog + delta, source_pert)) { + source_pv_jac = (source_pert - output_refs[I_PROGVAR]) / delta; + } else if (lookup(val_prog - delta, source_pert)) { + source_pv_jac = (output_refs[I_PROGVAR] - source_pert) / delta; + } + } break; case ENUM_DATADRIVEN_METHOD::MLP: refs_vars.resize(output_refs.size()); @@ -336,9 +426,66 @@ unsigned long CFluidFlamelet::EvaluateDataSet(const vector& input_sca default: break; } + if (with_source_jacobian && (!inside || output_refs[I_PROGVAR] <= 0)) source_pv_jac = 0.0; if (inside) extrapolation = 0; else extrapolation = 1; for (auto iVar = 0u; iVar < output_refs.size(); iVar++) AD::SetPreaccOut(output_refs[iVar]); AD::EndPreacc(); return extrapolation; } + +void CFluidFlamelet::EvaluatePrefDiffJacobian(const vector& input_scalar, const vector& beta, + const passivedouble* step, su2double* dbeta, su2double* dtemperature, + bool* usable) { + const auto n_cv = n_control_vars; + + if (Kind_DataDriven_Method == ENUM_DATADRIVEN_METHOD::MLP) { +#ifdef USE_MLPCPP + fill(pd_jac_temperature.begin(), pd_jac_temperature.end(), su2double(0.0)); + const bool inside = lookup_mlp->Predict(*iomap_TempJacobian, input_scalar, pd_refs_temperature); + for (auto j_cv = 0u; j_cv < n_cv; j_cv++) { + usable[j_cv] = inside; + dtemperature[j_cv] = pd_jac_temperature[j_cv]; + for (auto i_cv = 0u; i_cv < n_cv; i_cv++) dbeta[i_cv * n_cv + j_cv] = pd_jac_beta[i_cv * n_cv + j_cv]; + } +#endif + return; + } + + /*--- Central differences, one-sided where a perturbed state is outside the manifold. ---*/ + const unsigned long extrapolation_state = extrapolation; + vector state(input_scalar), beta_plus(beta.size()), beta_minus(beta.size()), thermo(LOOKUP_TD::SIZE); + + auto evaluate = [&](vector& beta_out, su2double& temperature_out) { + bool inside = EvaluateDataSet(state, FLAMELET_LOOKUP_OPS::PREFDIF, beta_out) == 0; + inside = (EvaluateDataSet(state, FLAMELET_LOOKUP_OPS::THERMO, thermo) == 0) && inside; + temperature_out = thermo[LOOKUP_TD::TEMPERATURE]; + return inside; + }; + + for (auto j_cv = 0u; j_cv < n_cv; j_cv++) { + su2double temperature_plus, temperature_minus; + state[j_cv] = input_scalar[j_cv] + step[j_cv]; + bool inside_plus = evaluate(beta_plus, temperature_plus); + state[j_cv] = input_scalar[j_cv] - step[j_cv]; + bool inside_minus = evaluate(beta_minus, temperature_minus); + state[j_cv] = input_scalar[j_cv]; + + su2double span = 2 * step[j_cv]; + if (!inside_plus) { + beta_plus = beta; + temperature_plus = Temperature; + span = step[j_cv]; + } + if (!inside_minus) { + beta_minus = beta; + temperature_minus = Temperature; + span = step[j_cv]; + } + usable[j_cv] = inside_plus || inside_minus; + for (auto i_cv = 0u; i_cv < n_cv; i_cv++) + dbeta[i_cv * n_cv + j_cv] = (beta_plus[BetaIndex(i_cv)] - beta_minus[BetaIndex(i_cv)]) / span; + dtemperature[j_cv] = (temperature_plus - temperature_minus) / span; + } + extrapolation = extrapolation_state; +} diff --git a/SU2_CFD/src/solvers/CSpeciesFlameletSolver.cpp b/SU2_CFD/src/solvers/CSpeciesFlameletSolver.cpp index 9fdf81663b4e..910059c3ee21 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 = nodes->GetChemicalSourceJacobian(i_point)[I_PROGVAR]; + 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); } } } @@ -639,8 +651,12 @@ unsigned long CSpeciesFlameletSolver::SetScalarSources(const CConfig* config, CF SU2_ZONE_SCOPED /*--- Compute total source terms from the production and consumption. ---*/ + /*--- The progress variable source derivative is only needed by the implicit scheme, outside the recording. ---*/ + const bool jacobian = (config->GetKind_TimeIntScheme() == EULER_IMPLICIT) && !AD::TapeActive(); + vector table_sources(flamelet_config_options.n_control_vars + 2 * flamelet_config_options.n_user_scalars); - unsigned long misses = fluid_model_local->EvaluateDataSet(scalars, FLAMELET_LOOKUP_OPS::SOURCES, table_sources); + unsigned long misses = fluid_model_local->EvaluateDataSet( + scalars, jacobian ? FLAMELET_LOOKUP_OPS::SOURCES_JACOBIAN : FLAMELET_LOOKUP_OPS::SOURCES, table_sources); table_sources[I_PROGVAR] = fmax(0, table_sources[I_PROGVAR]); nodes->SetTableMisses(iPoint, misses); @@ -649,6 +665,10 @@ 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]; + if (jacobian) { + nodes->SetChemicalSourceJacobian(iPoint, F * fluid_model_local->GetChemicalSourceJacobian(I_PROGVAR), I_PROGVAR); + } + /*--- 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: @@ -691,14 +711,72 @@ unsigned long CSpeciesFlameletSolver::SetPreferentialDiffusionScalars(CFluidMode /*--- Retrieve the preferential diffusion scalar values from the manifold. ---*/ vector beta_scalar(FLAMELET_PREF_DIFF_SCALARS::N_BETA_TERMS); - unsigned long misses = fluid_model_local->EvaluateDataSet(scalars, FLAMELET_LOOKUP_OPS::PREFDIF, beta_scalar); + unsigned long misses = fluid_model_local->EvaluateDataSet( + scalars, pd_jacobian ? FLAMELET_LOOKUP_OPS::PREFDIF_JACOBIAN : FLAMELET_LOOKUP_OPS::PREFDIF, beta_scalar); 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; + } + + su2double dbeta[MAXNVAR * MAXNVAR], dtemperature[MAXNVAR]; + bool usable[MAXNVAR]; + static_cast(fluid_model_local) + ->EvaluatePrefDiffJacobian(scalars, beta_scalar, pd_jacobian_step, dbeta, dtemperature, usable); + + for (auto j_cv = 0u; j_cv < n_cv; j_cv++) { + for (auto i_cv = 0u; i_cv < n_cv; i_cv++) { + const su2double identity = (i_cv == j_cv) ? su2double(1.0) : su2double(0.0); + su2double value = identity; + if (usable[j_cv]) { + value = dbeta[i_cv * n_cv + j_cv] - identity; + /*--- 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); + } + nodes->SetPrefDiffJacobian(iPoint, n_cv * n_cv + j_cv, usable[j_cv] ? dtemperature[j_cv] : su2double(0.0)); + } 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 +835,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..1f182266fbf6 100644 --- a/SU2_CFD/src/variables/CSpeciesFlameletVariable.cpp +++ b/SU2_CFD/src/variables/CSpeciesFlameletVariable.cpp @@ -55,9 +55,19 @@ CSpeciesFlameletVariable::CSpeciesFlameletVariable(const su2double* species_inf, table_misses.resize(nPoint) = 0; source_cons_jac.resize(nPoint, flamelet_config_options.n_user_scalars) = su2double(0.0); + const bool implicit = (config->GetKind_TimeIntScheme_Species() == EULER_IMPLICIT); + if (implicit) SpeciesSourceJacobian.resize(nPoint, nVar) = 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); + if (implicit) { + 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 derivative 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