https://mooseframework.inl.gov
Loading...
Searching...
No Matches
SubChannel1PhaseProblem.C
Go to the documentation of this file.
1//* This file is part of the MOOSE framework
2//* https://mooseframework.inl.gov
3//*
4//* All rights reserved, see COPYRIGHT for full restrictions
5//* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6//*
7//* Licensed under LGPL 2.1, please see LICENSE for details
8//* https://www.gnu.org/licenses/lgpl-2.1.html
9
11#include "SystemBase.h"
12#include "libmesh/petsc_vector.h"
13#include "libmesh/dense_matrix.h"
14#include "libmesh/dense_vector.h"
15#include <iostream>
16#include <cmath>
17#include "AuxiliarySystem.h"
18#include "SCM.h"
21#include "SCMHTCClosureBase.h"
23#include "TransientBase.h"
24#include "ImplicitEuler.h"
25
31
32PetscErrorCode
33formFunction(SNES, Vec x, Vec f, void * ctx)
34{
35 const PetscScalar * xx;
36 PetscScalar * ff;
37 PetscInt size;
38
40 Ctx * cc = static_cast<Ctx *>(ctx);
41 LibmeshPetscCallQ(VecGetSize(x, &size));
42
43 libMesh::DenseVector<Real> solution_seed(size, 0.0);
44 LibmeshPetscCallQ(VecGetArrayRead(x, &xx));
45 for (PetscInt i = 0; i < size; i++)
46 solution_seed(i) = xx[i];
47
48 LibmeshPetscCallQ(VecRestoreArrayRead(x, &xx));
49
50 libMesh::DenseVector<Real> Wij_residual_vector =
51 cc->schp->residualFunction(cc->iblock, solution_seed);
52
53 LibmeshPetscCallQ(VecGetArray(f, &ff));
54 for (int i = 0; i < size; i++)
55 ff[i] = Wij_residual_vector(i);
56
57 LibmeshPetscCallQ(VecRestoreArray(f, &ff));
58 PetscFunctionReturn(LIBMESH_PETSC_SUCCESS);
59}
60
63{
64 // Enumerations
65 MooseEnum schemes("upwind downwind central_difference exponential", "central_difference");
66 MooseEnum gravity_direction("counter_flow co_flow none", "counter_flow");
67
68 // Inputs
71 params.addClassDescription("Base class of the subchannel solvers");
72 params.addParam<unsigned int>("n_blocks", 1, "The number of blocks in the axial direction");
73 params.addParam<Real>("P_tol", 1e-6, "Pressure tolerance");
74 params.addRangeCheckedParam<int>(
75 "P_maxit",
76 0,
77 "P_maxit >= 0",
78 "Maximum number of pressure iterations; zero selects the solver's automatic limit");
79 params.addParam<Real>("T_tol", 1e-6, "Temperature tolerance");
80 params.addParam<int>("T_maxit", 100, "Maximum number of iterations for inner temperature loop");
81 params.addRangeCheckedParam<Real>(
82 "T_relaxation",
83 1.0,
84 "T_relaxation > 0 & T_relaxation <= 1",
85 "Relaxation factor for temperature updates in the inner thermal-hydraulic iteration");
86 params.addRangeCheckedParam<unsigned int>(
87 "enthalpy_subcycles",
88 1,
89 "enthalpy_subcycles > 0",
90 "Number of enthalpy, temperature, and property updates performed per flow solve");
91 params.addRangeCheckedParam<Real>(
92 "mass_flow_equation_relaxation",
93 1.0,
94 "mass_flow_equation_relaxation > 0 & mass_flow_equation_relaxation <= 1",
95 "Equation relaxation factor for mass flow rate in the implicit non-segregated solve");
96 params.addRangeCheckedParam<Real>(
97 "pressure_equation_relaxation",
98 1.0,
99 "pressure_equation_relaxation > 0 & pressure_equation_relaxation <= 1",
100 "Equation relaxation factor for pressure in the implicit non-segregated solve");
101 params.addRangeCheckedParam<Real>(
102 "crossflow_equation_relaxation",
103 0.1,
104 "crossflow_equation_relaxation > 0 & crossflow_equation_relaxation <= 1",
105 "Equation relaxation factor for crossflow in the implicit non-segregated solve");
106 params.addRangeCheckedParam<Real>(
107 "mass_flow_relaxation",
108 1.0,
109 "mass_flow_relaxation > 0 & mass_flow_relaxation <= 1",
110 "Post-solve relaxation factor for mass flow rate updates in the implicit non-segregated "
111 "solve");
112 params.addRangeCheckedParam<Real>(
113 "pressure_relaxation",
114 1.0,
115 "pressure_relaxation > 0 & pressure_relaxation <= 1",
116 "Post-solve relaxation factor for pressure updates in the implicit non-segregated solve");
117 params.addRangeCheckedParam<Real>(
118 "crossflow_relaxation",
119 1.0,
120 "crossflow_relaxation > 0 & crossflow_relaxation <= 1",
121 "Post-solve relaxation factor for crossflow updates in the implicit non-segregated solve");
122 params.addParam<PetscReal>("rtol", 1e-6, "Relative tolerance for ksp solver");
123 params.addParam<PetscReal>("atol", 1e-6, "Absolute tolerance for ksp solver");
124 params.addParam<PetscReal>("dtol", 1e5, "Divergence tolerance or ksp solver");
125 params.addParam<PetscInt>("maxit", 1e4, "Maximum number of iterations for ksp solver");
126 params.addParam<MooseEnum>(
127 "interpolation_scheme",
128 schemes,
129 "Interpolation scheme used for the method. Default is central_difference");
130 params.addParam<MooseEnum>(
131 "gravity", gravity_direction, "Direction of gravity. Default is counter_flow");
132 params.addParam<bool>(
133 "implicit", false, "Boolean to define the use of explicit or implicit solution.");
134 params.addParam<bool>("staggered_pressure",
135 false,
136 "Boolean to define the use of staggered or collocated pressure.");
137 params.addParam<bool>(
138 "segregated", true, "Boolean to define whether to use a segregated solution.");
139 params.addParam<bool>(
140 "verbose_subchannel", false, "Boolean to print out information related to subchannel solve.");
141 params.addRequiredParam<bool>("compute_density", "Flag that enables the calculation of density");
142 params.addRequiredParam<bool>("compute_viscosity",
143 "Flag that enables the calculation of viscosity");
144 params.addRequiredParam<bool>(
145 "compute_power",
146 "Flag that informs whether we solve the Enthalpy/Temperature equations or not");
147 params.addRequiredParam<PostprocessorName>(
148 "P_out",
149 "The postprocessor (or scalar) that provides the absolute outlet pressure [Pa]. The solved "
150 "pressure variable P is relative to this value.");
151 params.addRequiredParam<UserObjectName>("fp", "Fluid properties user object name");
152 params.addRequiredParam<UserObjectName>("friction_closure",
153 "Closure computing the friction factor");
154 params.addRequiredParam<UserObjectName>(
155 "mixing_closure",
156 "Closure computing the turbulent mixing, wire-induced "
157 "mixing and sweep flow mixing parameter where applicable");
158 params.addParam<UserObjectName>(
159 "pin_HTC_closure", "Closure computing HTC on fuel pin (required if pin mesh exists).");
160 params.addParam<UserObjectName>("duct_HTC_closure",
161 "Closure computing HTC on duct (required if duct mesh exists).");
162 params.addParam<bool>(
163 "full_output", false, "Flag that enables the output of the maximum number of variables.");
164 params.addDeprecatedParam<Real>("beta",
165 "Thermal diffusion coefficient used in turbulent crossflow.",
166 "Use closure system instead.");
167 params.addDeprecatedParam<bool>(
168 "constant_beta",
169 true,
170 "Boolean to define the use of a constant beta or beta correlation (Kim and Chung, 2001)",
171 "Use closure system instead.");
172
174 "P_tol P_maxit T_tol T_maxit T_relaxation enthalpy_subcycles "
175 "mass_flow_equation_relaxation pressure_equation_relaxation crossflow_equation_relaxation "
176 "mass_flow_relaxation pressure_relaxation crossflow_relaxation rtol atol dtol maxit",
177 "Solver tolerances and iterations");
178 params.addParamNamesToGroup("implicit segregated staggered_pressure interpolation_scheme",
179 "Solution method");
180 params.addParamNamesToGroup("fp friction_closure mixing_closure pin_HTC_closure duct_HTC_closure",
181 "Closures");
182 params.addParamNamesToGroup("compute_density compute_viscosity compute_power gravity",
183 "Physics models");
184 params.addParamNamesToGroup("verbose_subchannel full_output", "Output");
185
186 return params;
187}
188
190 : ExternalProblem(params),
192 _friction_args(/*i_ch=*/0, /*Re=*/1.0, /*S=*/0.0, /*w_perim=*/0.0),
193 _nusselt_args(
194 /*Re=*/1.0, /*Pr=*/1.0, std::numeric_limits<unsigned int>::max(), /*iz=*/0, /*i_ch=*/0),
195 _P_out(getPostprocessorValue("P_out")),
196 _fp(nullptr),
197 _subchannel_mesh(SCM::getMesh<SubChannelMesh>(_mesh)),
198 _n_blocks(getParam<unsigned int>("n_blocks")),
199 _Wij(declareRestartableData<libMesh::DenseMatrix<Real>>("Wij")),
200 _Wij_old(declareRestartableData<libMesh::DenseMatrix<Real>>("Wij_old")),
201 _g_grav(9.81),
202 _kij(_subchannel_mesh.getKij()),
203 _one(1.0),
204 _compute_density(getParam<bool>("compute_density")),
205 _compute_viscosity(getParam<bool>("compute_viscosity")),
206 _compute_power(getParam<bool>("compute_power")),
207 _pin_mesh_exist(_subchannel_mesh.pinMeshExist()),
208 _duct_mesh_exist(_subchannel_mesh.ductMeshExist()),
209 _bulk_Re(1.0),
210 _bulk_V(1.0),
211 _P_tol(getParam<Real>("P_tol")),
212 _P_maxit(getParam<int>("P_maxit")),
213 _T_tol(getParam<Real>("T_tol")),
214 _T_maxit(getParam<int>("T_maxit")),
215 _T_relaxation(getParam<Real>("T_relaxation")),
216 _enthalpy_subcycles(getParam<unsigned int>("enthalpy_subcycles")),
217 _mass_flow_equation_relaxation(getParam<Real>("mass_flow_equation_relaxation")),
218 _pressure_equation_relaxation(getParam<Real>("pressure_equation_relaxation")),
219 _crossflow_equation_relaxation(getParam<Real>("crossflow_equation_relaxation")),
220 _mass_flow_relaxation(getParam<Real>("mass_flow_relaxation")),
221 _pressure_relaxation(getParam<Real>("pressure_relaxation")),
222 _crossflow_relaxation(getParam<Real>("crossflow_relaxation")),
223 _rtol(getParam<PetscReal>("rtol")),
224 _atol(getParam<PetscReal>("atol")),
225 _dtol(getParam<PetscReal>("dtol")),
226 _maxit(getParam<PetscInt>("maxit")),
227 _interpolation_scheme(getParam<MooseEnum>("interpolation_scheme")),
228 _gravity_direction(getParam<MooseEnum>("gravity")),
229 _dir_grav(computeGravityDir(_gravity_direction)),
230 _implicit_bool(getParam<bool>("implicit")),
231 _staggered_pressure_bool(getParam<bool>("staggered_pressure")),
232 _segregated_bool(getParam<bool>("segregated")),
233 _verbose_subchannel(getParam<bool>("verbose_subchannel")),
234 _friction_closure(nullptr),
235 _mixing_closure(nullptr),
236 _pin_HTC_closure(nullptr),
237 _duct_HTC_closure(nullptr),
238 _Tpin_soln(nullptr),
239 _duct_heat_flux_soln(nullptr),
240 _Tduct_soln(nullptr),
241 _HTC_soln(nullptr)
242{
243 if (params.isParamSetByUser("beta") || params.isParamSetByUser("constant_beta"))
244 paramError("beta",
245 "You are using a deprecated parameter. Please use the mixing_closure system.");
246 if (_pin_mesh_exist && !isParamValid("pin_HTC_closure"))
247 paramError("pin_HTC_closure", "required when a pin mesh exists.");
248 if (_duct_mesh_exist && !isParamValid("duct_HTC_closure"))
249 paramError("duct_HTC_closure", "required when a duct mesh exists.");
251 paramError("segregated", "A non-segregated solve requires 'implicit = true'.");
253 for (const auto * relaxation_param : {"mass_flow_equation_relaxation",
254 "pressure_equation_relaxation",
255 "crossflow_equation_relaxation",
256 "mass_flow_relaxation",
257 "pressure_relaxation",
258 "crossflow_relaxation"})
259 if (params.isParamSetByUser(relaxation_param))
260 paramError(relaxation_param,
261 "The '",
262 relaxation_param,
263 "' parameter is only used by the implicit non-segregated solve. Set "
264 "'implicit = true' and 'segregated = false' to use it.");
265 // NOTE: The four quantities below are 0 for processor_id != 0
270 // NOTE: The four quantities above are 0 for processor_id != 0
273 // Pressure drop (lives on subchannel nodes)
275 _DP.zero();
276 // Turbulent crossflow (stuff that live on the gaps)
277 if (!_app.isRestarting() && !_app.isRecovering())
278 {
280 _Wij.zero();
282 _Wij_old.zero();
283 }
284
286 _WijPrime.zero();
289 _converged = true;
290
291 // Mass conservation components
292 LibmeshPetscCall(
294 LibmeshPetscCall(createPetscVector(_Wij_vec, _block_size * _n_gaps));
295 LibmeshPetscCall(createPetscVector(_prod, _block_size * _n_channels));
296 LibmeshPetscCall(createPetscVector(_prodp, _block_size * _n_channels));
297 LibmeshPetscCall(createPetscMatrix(
300 LibmeshPetscCall(createPetscMatrix(
302
303 // Axial momentum conservation components
304 LibmeshPetscCall(createPetscMatrix(
307 LibmeshPetscCall(createPetscMatrix(
310 LibmeshPetscCall(createPetscMatrix(
313 LibmeshPetscCall(createPetscMatrix(
316 LibmeshPetscCall(createPetscMatrix(
320 LibmeshPetscCall(createPetscMatrix(
323 LibmeshPetscCall(
326
327 // Lateral momentum conservation components
328 LibmeshPetscCall(
331 LibmeshPetscCall(createPetscMatrix(
334 LibmeshPetscCall(
337 LibmeshPetscCall(
340 LibmeshPetscCall(
343
344 // Energy conservation components
345 LibmeshPetscCall(createPetscMatrix(
348 LibmeshPetscCall(createPetscMatrix(
351 LibmeshPetscCall(createPetscMatrix(
355 LibmeshPetscCall(
358
360 {
362 ": When implicit number of blocks can't be equal to number of cells. This will "
363 "cause problems with the subchannel interpolation scheme.");
364 }
365}
366
367void
369{
370 // Create variables for output and storage. This comes before the base class setup, which runs
371 // the user objects executed on initial, such as SCMPinPowerPostprocessor, that read them.
372 _mdot_soln = std::make_unique<SolutionHandle>(getVariable(0, SubChannelApp::MASS_FLOW_RATE));
373 _SumWij_soln = std::make_unique<SolutionHandle>(getVariable(0, SubChannelApp::SUM_CROSSFLOW));
374 _P_soln = std::make_unique<SolutionHandle>(getVariable(0, SubChannelApp::PRESSURE));
375 if (getParam<bool>("full_output"))
376 {
377 _DP_soln = std::make_unique<SolutionHandle>(getVariable(0, SubChannelApp::PRESSURE_DROP));
378 _ff_soln = std::make_unique<SolutionHandle>(getVariable(0, SubChannelApp::FRICTION_FACTOR));
379 }
380 _h_soln = std::make_unique<SolutionHandle>(getVariable(0, SubChannelApp::ENTHALPY));
381 _T_soln = std::make_unique<SolutionHandle>(getVariable(0, SubChannelApp::TEMPERATURE));
382 if (_pin_mesh_exist)
383 {
384 _Tpin_soln = std::make_unique<SolutionHandle>(getVariable(0, SubChannelApp::PIN_TEMPERATURE));
385 _Dpin_soln = std::make_unique<SolutionHandle>(getVariable(0, SubChannelApp::PIN_DIAMETER));
386 _HTC_soln =
387 std::make_unique<SolutionHandle>(getVariable(0, SubChannelApp::HEAT_TRANSFER_COEFFICIENT));
388 }
389 _rho_soln = std::make_unique<SolutionHandle>(getVariable(0, SubChannelApp::DENSITY));
390 _mu_soln = std::make_unique<SolutionHandle>(getVariable(0, SubChannelApp::VISCOSITY));
391 _S_flow_soln = std::make_unique<SolutionHandle>(getVariable(0, SubChannelApp::SURFACE_AREA));
392 _w_perim_soln = std::make_unique<SolutionHandle>(getVariable(0, SubChannelApp::WETTED_PERIMETER));
393 _q_prime_soln = std::make_unique<SolutionHandle>(getVariable(0, SubChannelApp::LINEAR_HEAT_RATE));
395 std::make_unique<SolutionHandle>(getVariable(0, SubChannelApp::DISPLACEMENT));
397 {
399 std::make_unique<SolutionHandle>(getVariable(0, SubChannelApp::DUCT_HEAT_FLUX));
400 _Tduct_soln = std::make_unique<SolutionHandle>(getVariable(0, SubChannelApp::DUCT_TEMPERATURE));
401 }
402
404
405 _fp = &getUserObject<SinglePhaseFluidProperties>(getParam<UserObjectName>("fp"));
407 &getUserObject<SCMFrictionClosureBase>(getParam<UserObjectName>("friction_closure"));
409 &getUserObject<SCMMixingClosureBase>(getParam<UserObjectName>("mixing_closure"));
410
413
414 if (_pin_mesh_exist)
416 &getUserObject<SCMHTCClosureBase>(getParam<UserObjectName>("pin_HTC_closure"));
419 &getUserObject<SCMHTCClosureBase>(getParam<UserObjectName>("duct_HTC_closure"));
420}
421
422void
424{
425 if (processor_id() == 0)
426 {
427 Real viscosity_in = 0.0;
428 Real volumetric_flow_in = 0.0;
429 Real mass_flow_in = 0.0;
430 for (const auto i_ch : make_range(_n_channels))
431 {
432 auto * node_in = _subchannel_mesh.getChannelNode(i_ch, 0);
433 const Real mdot_in = (*_mdot_soln)(node_in);
434 const Real rho_in = (*_rho_soln)(node_in);
435 viscosity_in += mdot_in * (*_mu_soln)(node_in);
436 volumetric_flow_in += mdot_in / rho_in;
437 mass_flow_in += mdot_in;
438 }
439 const Real bulk_Dh = _subchannel_mesh.getAssemblyHydraulicDiameter();
440 const Real flow_area = _subchannel_mesh.getAssemblyFlowArea();
441 const Real inlet_mu = viscosity_in / mass_flow_in;
442 _bulk_Re = mass_flow_in * bulk_Dh / (inlet_mu * flow_area);
443 _bulk_V = volumetric_flow_in / flow_area;
444 }
445 // Only rank 0 owns the subchannel DOFs (SingleRankPartitioner), so share the result
448
449 if (MooseUtils::absoluteFuzzyEqual(_bulk_Re, 0.0))
450 mooseError("The computed bulk Reynolds number is zero.");
451
452 if (MooseUtils::absoluteFuzzyEqual(_bulk_V, 0.0))
453 mooseError("The computed bulk velocity is zero.");
454
456}
457
458void
460{
461 const Real tol = libMesh::TOLERANCE;
462 const auto pin_diameter = _subchannel_mesh.getPinDiameter();
463
464 if (_pin_mesh_exist)
465 {
466 for (unsigned int iz = 0; iz < _n_cells + 1; iz++)
467 for (unsigned int i_pin = 0; i_pin < _n_pins; i_pin++)
468 {
469 auto * node = _subchannel_mesh.getPinNode(i_pin, iz);
470 const Real Dpin = (*_Dpin_soln)(node);
471 if (std::abs(Dpin) <= tol)
472 mooseError("Dpin is zero at node ",
473 node->id(),
474 ". You must initialize Dpin to a non-zero value.");
475 if (std::abs(Dpin - pin_diameter) > tol)
476 _deformation = true;
477 }
478 }
479
480 for (unsigned int iz = 0; iz < _n_cells + 1 && !_deformation; iz++)
481 for (unsigned int i_ch = 0; i_ch < _n_channels && !_deformation; i_ch++)
482 {
483 auto * node = _subchannel_mesh.getChannelNode(i_ch, iz);
484 auto subch_type = _subchannel_mesh.getSubchannelType(i_ch);
485
486 if ((subch_type == EChannelType::CORNER || subch_type == EChannelType::EDGE) &&
487 std::abs((*_displacement_soln)(node)) > tol)
488 _deformation = true;
489 }
490}
491
493{
494 PetscErrorCode ierr = cleanUp();
495 if (ierr)
496 mooseError(name(), ": Error in memory cleanup");
497}
498
499PetscErrorCode
501{
503 // We need to clean up the petsc matrices/vectors
504 // Mass conservation components
505 LibmeshPetscCall(MatDestroy(&_mc_sumWij_mat));
506 LibmeshPetscCall(VecDestroy(&_Wij_vec));
507 LibmeshPetscCall(VecDestroy(&_prod));
508 LibmeshPetscCall(VecDestroy(&_prodp));
509 LibmeshPetscCall(MatDestroy(&_mc_axial_convection_mat));
510 LibmeshPetscCall(VecDestroy(&_mc_axial_convection_rhs));
511 LibmeshPetscCall(MatDestroy(&_mc_density_pressure_mat));
512
513 // Axial momentum conservation components
514 LibmeshPetscCall(MatDestroy(&_amc_turbulent_cross_flows_mat));
515 LibmeshPetscCall(VecDestroy(&_amc_turbulent_cross_flows_rhs));
516 LibmeshPetscCall(MatDestroy(&_amc_time_derivative_mat));
517 LibmeshPetscCall(VecDestroy(&_amc_time_derivative_rhs));
518 LibmeshPetscCall(MatDestroy(&_amc_advective_derivative_mat));
519 LibmeshPetscCall(VecDestroy(&_amc_advective_derivative_rhs));
520 LibmeshPetscCall(MatDestroy(&_amc_cross_derivative_mat));
521 LibmeshPetscCall(VecDestroy(&_amc_cross_derivative_rhs));
522 LibmeshPetscCall(MatDestroy(&_amc_friction_force_mat));
523 LibmeshPetscCall(VecDestroy(&_amc_friction_force_rhs));
524 LibmeshPetscCall(VecDestroy(&_amc_gravity_rhs));
525 LibmeshPetscCall(MatDestroy(&_amc_pressure_force_mat));
526 LibmeshPetscCall(VecDestroy(&_amc_pressure_force_rhs));
527 LibmeshPetscCall(MatDestroy(&_amc_sys_mdot_mat));
528 LibmeshPetscCall(VecDestroy(&_amc_sys_mdot_rhs));
529
530 // Lateral momentum conservation components
531 LibmeshPetscCall(MatDestroy(&_cmc_time_derivative_mat));
532 LibmeshPetscCall(VecDestroy(&_cmc_time_derivative_rhs));
533 LibmeshPetscCall(MatDestroy(&_cmc_advective_derivative_mat));
534 LibmeshPetscCall(VecDestroy(&_cmc_advective_derivative_rhs));
535 LibmeshPetscCall(MatDestroy(&_cmc_friction_force_mat));
536 LibmeshPetscCall(VecDestroy(&_cmc_friction_force_rhs));
537 LibmeshPetscCall(MatDestroy(&_cmc_pressure_force_mat));
538 LibmeshPetscCall(VecDestroy(&_cmc_pressure_force_rhs));
539 LibmeshPetscCall(MatDestroy(&_cmc_sys_Wij_mat));
540 LibmeshPetscCall(VecDestroy(&_cmc_sys_Wij_rhs));
541
542 // Energy conservation components
543 LibmeshPetscCall(MatDestroy(&_hc_time_derivative_mat));
544 LibmeshPetscCall(VecDestroy(&_hc_time_derivative_rhs));
545 LibmeshPetscCall(MatDestroy(&_hc_advective_derivative_mat));
546 LibmeshPetscCall(VecDestroy(&_hc_advective_derivative_rhs));
547 LibmeshPetscCall(MatDestroy(&_hc_cross_derivative_mat));
548 LibmeshPetscCall(VecDestroy(&_hc_cross_derivative_rhs));
549 LibmeshPetscCall(VecDestroy(&_hc_added_heat_rhs));
550 LibmeshPetscCall(MatDestroy(&_hc_sys_h_mat));
551 LibmeshPetscCall(VecDestroy(&_hc_sys_h_rhs));
552
553 PetscFunctionReturn(LIBMESH_PETSC_SUCCESS);
554}
555
556bool
558{
559 return _converged;
560}
561
562PetscScalar
564{
565 switch (_interpolation_scheme)
566 {
567 case 0: // upwind interpolation
568 return 1.0;
569 case 1: // downwind interpolation
570 return 0.0;
571 case 2: // central_difference interpolation
572 return 0.5;
573 case 3: // exponential interpolation (Peclet limited)
574 return ((Peclet - 1.0) * std::exp(Peclet) + 1) / (Peclet * (std::exp(Peclet) - 1.) + 1e-10);
575 default:
577 ": Interpolation scheme should be a string: upwind, downwind, central_difference, "
578 "exponential");
579 }
580}
581
582PetscScalar
584 PetscScalar botValue,
585 PetscScalar Peclet)
586{
587 PetscScalar alpha = computeInterpolationCoefficients(Peclet);
588 return alpha * botValue + (1.0 - alpha) * topValue;
589}
590
591void
593{
594 const unsigned int last_node = (iblock + 1) * _block_size;
595 const unsigned int first_node = iblock * _block_size + 1;
596 // Initial guess, port crossflow of block (iblock) into a vector that will act as my initial guess
597 libMesh::DenseVector<Real> solution_seed(_n_gaps * _block_size, 0.0);
598 for (unsigned int iz = first_node; iz < last_node + 1; iz++)
599 {
600 for (unsigned int i_gap = 0; i_gap < _n_gaps; i_gap++)
601 {
602 int i = _n_gaps * (iz - first_node) + i_gap; // column wise transfer
603 solution_seed(i) = _Wij(i_gap, iz);
604 }
605 }
606
607 // Solving the combined lateral momentum equation for Wij using a PETSc solver and update vector
608 // root
610 LibmeshPetscCall(petscSnesSolver(iblock, solution_seed, root));
611
612 // Assign the solution to the cross-flow matrix
613 int i = 0;
614 for (unsigned int iz = first_node; iz < last_node + 1; iz++)
615 {
616 for (unsigned int i_gap = 0; i_gap < _n_gaps; i_gap++)
617 {
618 _Wij(i_gap, iz) = root(i);
619 i++;
620 }
621 }
622}
623
624void
626{
627 const unsigned int last_node = (iblock + 1) * _block_size;
628 const unsigned int first_node = iblock * _block_size + 1;
629 // Add to solution vector if explicit
630 if (!_implicit_bool)
631 {
632 for (unsigned int iz = first_node; iz < last_node + 1; iz++)
633 {
634 for (unsigned int i_ch = 0; i_ch < _n_channels; i_ch++)
635 {
636 auto * node_out = _subchannel_mesh.getChannelNode(i_ch, iz);
637 Real sumWij = 0.0;
638 // Calculate sum of crossflow into channel i from channels j around i
639 unsigned int counter = 0;
640 for (auto i_gap : _subchannel_mesh.getChannelGaps(i_ch))
641 {
642 sumWij += _subchannel_mesh.getCrossflowSign(i_ch, counter) * _Wij(i_gap, iz);
643 counter++;
644 }
645 // The net crossflow coming out of cell i [kg/sec]
646 _SumWij_soln->set(node_out, sumWij);
647 }
648 }
649 }
650 // Add to matrix if implicit
651 else
652 {
653 LibmeshPetscCall(MatZeroEntries(_mc_density_pressure_mat));
654 for (unsigned int iz = first_node; iz < last_node + 1; iz++)
655 {
656 unsigned int iz_ind = iz - first_node;
657 for (unsigned int i_ch = 0; i_ch < _n_channels; i_ch++)
658 {
659 // Calculate sum of crossflow into channel i from channels j around i
660 unsigned int counter = 0;
661 for (auto i_gap : _subchannel_mesh.getChannelGaps(i_ch))
662 {
663 PetscInt row = i_ch + _n_channels * iz_ind;
664 PetscInt col = i_gap + _n_gaps * iz_ind;
665 PetscScalar value = _subchannel_mesh.getCrossflowSign(i_ch, counter);
666 LibmeshPetscCall(MatSetValues(_mc_sumWij_mat, 1, &row, 1, &col, &value, INSERT_VALUES));
667 counter++;
668 }
669 }
670 }
671 LibmeshPetscCall(MatAssemblyBegin(_mc_sumWij_mat, MAT_FINAL_ASSEMBLY));
672 LibmeshPetscCall(MatAssemblyEnd(_mc_sumWij_mat, MAT_FINAL_ASSEMBLY));
674 {
675 Vec loc_prod;
676 Vec loc_Wij;
677 LibmeshPetscCall(VecDuplicate(_amc_sys_mdot_rhs, &loc_prod));
678 LibmeshPetscCall(VecDuplicate(_Wij_vec, &loc_Wij));
680 loc_Wij, _Wij, first_node, last_node, _n_gaps));
681 LibmeshPetscCall(MatMult(_mc_sumWij_mat, loc_Wij, loc_prod));
682 LibmeshPetscCall(populateSolutionChan<SolutionHandle>(
683 loc_prod, *_SumWij_soln, first_node, last_node, _n_channels));
684 LibmeshPetscCall(VecDestroy(&loc_prod));
685 LibmeshPetscCall(VecDestroy(&loc_Wij));
686 }
687 }
688}
689
690void
692{
693 const unsigned int last_node = (iblock + 1) * _block_size;
694 const unsigned int first_node = iblock * _block_size + 1;
695 if (!_implicit_bool)
696 {
697 for (unsigned int iz = first_node; iz < last_node + 1; iz++)
698 {
699 auto dz = _z_grid[iz] - _z_grid[iz - 1];
700 for (unsigned int i_ch = 0; i_ch < _n_channels; i_ch++)
701 {
702 auto * node_in = _subchannel_mesh.getChannelNode(i_ch, iz - 1);
703 auto * node_out = _subchannel_mesh.getChannelNode(i_ch, iz);
704 auto volume = dz * (*_S_flow_soln)(node_in);
705 auto time_term = _TR * ((*_rho_soln)(node_out)-_rho_soln->old(node_out)) * volume / _dt;
706 // Wij positive out of i into j;
707 auto mdot_out = (*_mdot_soln)(node_in) - (*_SumWij_soln)(node_out)-time_term;
708 if (mdot_out < 0)
709 {
710 _console << "Wij = : " << _Wij << "\n";
712 " : Calculation of negative mass flow mdot_out = : ",
713 mdot_out,
714 " Axial Level= : ",
715 iz,
716 " - Implicit solves are required for recirculating flow.");
717 }
718 _mdot_soln->set(node_out, mdot_out); // kg/sec
719 }
720 }
721 }
722 else
723 {
724 for (unsigned int iz = first_node; iz < last_node + 1; iz++)
725 {
726 auto dz = _z_grid[iz] - _z_grid[iz - 1];
727 auto iz_ind = iz - first_node;
728 for (unsigned int i_ch = 0; i_ch < _n_channels; i_ch++)
729 {
730 auto * node_in = _subchannel_mesh.getChannelNode(i_ch, iz - 1);
731 auto * node_out = _subchannel_mesh.getChannelNode(i_ch, iz);
732 auto volume = dz * (*_S_flow_soln)(node_in);
733
734 // Adding time derivative to the RHS
735 auto time_term = _TR * ((*_rho_soln)(node_out)-_rho_soln->old(node_out)) * volume / _dt;
736 PetscInt row_vec = i_ch + _n_channels * iz_ind;
737 PetscScalar value_vec = -1.0 * time_term;
738 LibmeshPetscCall(
739 VecSetValues(_mc_axial_convection_rhs, 1, &row_vec, &value_vec, INSERT_VALUES));
740
741 // Linearize transient density with respect to pressure at constant enthalpy. The
742 // temperature response at constant h is required even for a rho(T)-only liquid model:
743 //
744 // (d rho / d p)_h = (d rho / d p)_T
745 // - (d rho / d T)_p (d h / d p)_T / (d h / d T)_p.
746 //
747 // Without this Jacobian contribution, pressure-density coupling remains in the outer
748 // fixed-point iteration and scales as O(1 / dt). As the time step decreases, the iteration
749 // can become increasingly slow or noncontractive and may require stronger relaxation.
750 if (_TR && _compute_density && _compute_power && iz < last_node)
751 {
752 Real rho, drho_dp_T, drho_dT;
753 _fp->rho_from_p_T(
754 (*_P_soln)(node_out) + _P_out, (*_T_soln)(node_out), rho, drho_dp_T, drho_dT);
755 Real h, dh_dp_T, dh_dT;
756 _fp->h_from_p_T((*_P_soln)(node_out) + _P_out, (*_T_soln)(node_out), h, dh_dp_T, dh_dT);
757 const Real drho_dp_h = drho_dp_T - drho_dT * dh_dp_T / dh_dT;
758 const PetscScalar pressure_coefficient = volume / _dt * drho_dp_h;
759 const PetscInt pressure_col = i_ch + _n_channels * (iz_ind + 1);
760 LibmeshPetscCall(MatSetValues(_mc_density_pressure_mat,
761 1,
762 &row_vec,
763 1,
764 &pressure_col,
765 &pressure_coefficient,
766 INSERT_VALUES));
767 const PetscScalar linearization_rhs = pressure_coefficient * (*_P_soln)(node_out);
768 LibmeshPetscCall(
769 VecSetValues(_mc_axial_convection_rhs, 1, &row_vec, &linearization_rhs, ADD_VALUES));
770 }
771
772 // Imposing bottom boundary condition or adding of diagonal elements
773 if (iz == first_node)
774 {
775 PetscScalar value_vec = (*_mdot_soln)(node_in);
776 PetscInt row_vec = i_ch + _n_channels * iz_ind;
777 LibmeshPetscCall(
778 VecSetValues(_mc_axial_convection_rhs, 1, &row_vec, &value_vec, ADD_VALUES));
779 }
780 else
781 {
782 PetscInt row = i_ch + _n_channels * iz_ind;
783 PetscInt col = i_ch + _n_channels * (iz_ind - 1);
784 PetscScalar value = -1.0;
785 LibmeshPetscCall(
786 MatSetValues(_mc_axial_convection_mat, 1, &row, 1, &col, &value, INSERT_VALUES));
787 }
788
789 // Adding diagonal elements
790 PetscInt row = i_ch + _n_channels * iz_ind;
791 PetscInt col = i_ch + _n_channels * iz_ind;
792 PetscScalar value = 1.0;
793 LibmeshPetscCall(
794 MatSetValues(_mc_axial_convection_mat, 1, &row, 1, &col, &value, INSERT_VALUES));
795
796 // Adding cross flows RHS
798 {
799 PetscScalar value_vec_2 = -1.0 * (*_SumWij_soln)(node_out);
800 PetscInt row_vec_2 = i_ch + _n_channels * iz_ind;
801 LibmeshPetscCall(
802 VecSetValues(_mc_axial_convection_rhs, 1, &row_vec_2, &value_vec_2, ADD_VALUES));
803 }
804 }
805 }
806 LibmeshPetscCall(MatAssemblyBegin(_mc_axial_convection_mat, MAT_FINAL_ASSEMBLY));
807 LibmeshPetscCall(MatAssemblyEnd(_mc_axial_convection_mat, MAT_FINAL_ASSEMBLY));
808 LibmeshPetscCall(MatAssemblyBegin(_mc_density_pressure_mat, MAT_FINAL_ASSEMBLY));
809 LibmeshPetscCall(MatAssemblyEnd(_mc_density_pressure_mat, MAT_FINAL_ASSEMBLY));
810
812 {
813 KSP ksploc;
814 PC pc;
815 Vec sol;
816 LibmeshPetscCall(VecDuplicate(_mc_axial_convection_rhs, &sol));
817 LibmeshPetscCall(KSPCreate(PETSC_COMM_SELF, &ksploc));
818 LibmeshPetscCall(KSPSetOperators(ksploc, _mc_axial_convection_mat, _mc_axial_convection_mat));
819 LibmeshPetscCall(KSPGetPC(ksploc, &pc));
820 LibmeshPetscCall(PCSetType(pc, PCJACOBI));
821 LibmeshPetscCall(KSPSetTolerances(ksploc, _rtol, _atol, _dtol, _maxit));
822 LibmeshPetscCall(KSPSetFromOptions(ksploc));
823 LibmeshPetscCall(KSPSolve(ksploc, _mc_axial_convection_rhs, sol));
824 LibmeshPetscCall(populateSolutionChan<SolutionHandle>(
825 sol, *_mdot_soln, first_node, last_node, _n_channels));
826 LibmeshPetscCall(VecZeroEntries(_mc_axial_convection_rhs));
827 LibmeshPetscCall(KSPDestroy(&ksploc));
828 LibmeshPetscCall(VecDestroy(&sol));
829 }
830 }
831}
832
833void
835{
836 const unsigned int last_node = (iblock + 1) * _block_size;
837 const unsigned int first_node = iblock * _block_size + 1;
838 if (!_implicit_bool)
839 {
840 for (unsigned int iz = first_node; iz < last_node + 1; iz++)
841 {
842 auto k_grid = _subchannel_mesh.getKGrid();
843 auto dz = _z_grid[iz] - _z_grid[iz - 1];
844 for (unsigned int i_ch = 0; i_ch < _n_channels; i_ch++)
845 {
846 auto * node_in = _subchannel_mesh.getChannelNode(i_ch, iz - 1);
847 auto * node_out = _subchannel_mesh.getChannelNode(i_ch, iz);
848 auto rho_in = (*_rho_soln)(node_in);
849 auto rho_out = (*_rho_soln)(node_out);
850 auto mu_in = (*_mu_soln)(node_in);
851 auto S = (*_S_flow_soln)(node_in);
852 auto w_perim = (*_w_perim_soln)(node_in);
853 // hydraulic diameter in the i direction
854 auto Dh_i = 4.0 * S / w_perim;
855 auto time_term = _TR * ((*_mdot_soln)(node_out)-_mdot_soln->old(node_out)) * dz / _dt -
856 dz * 2.0 * (*_mdot_soln)(node_out) * (rho_out - _rho_soln->old(node_out)) /
857 rho_in / _dt;
858 auto mass_term1 =
859 Utility::pow<2>((*_mdot_soln)(node_out)) * (1.0 / S / rho_out - 1.0 / S / rho_in);
860 auto mass_term2 = -2.0 * (*_mdot_soln)(node_out) * (*_SumWij_soln)(node_out) / S / rho_in;
861 auto crossflow_term = 0.0;
862 auto turbulent_term = 0.0;
863 unsigned int counter = 0;
864 for (auto i_gap : _subchannel_mesh.getChannelGaps(i_ch))
865 {
866 auto chans = _subchannel_mesh.getGapChannels(i_gap);
867 unsigned int ii_ch = chans.first;
868 unsigned int jj_ch = chans.second;
869 auto * node_in_i = _subchannel_mesh.getChannelNode(ii_ch, iz - 1);
870 auto * node_in_j = _subchannel_mesh.getChannelNode(jj_ch, iz - 1);
871 auto * node_out_i = _subchannel_mesh.getChannelNode(ii_ch, iz);
872 auto * node_out_j = _subchannel_mesh.getChannelNode(jj_ch, iz);
873 auto rho_i = (*_rho_soln)(node_in_i);
874 auto rho_j = (*_rho_soln)(node_in_j);
875 auto Si = (*_S_flow_soln)(node_in_i);
876 auto Sj = (*_S_flow_soln)(node_in_j);
877 Real u_star = 0.0;
878 // figure out donor axial velocity
879 if (_Wij(i_gap, iz) > 0.0)
880 u_star = (*_mdot_soln)(node_out_i) / Si / rho_i;
881 else
882 u_star = (*_mdot_soln)(node_out_j) / Sj / rho_j;
883
884 crossflow_term +=
885 _subchannel_mesh.getCrossflowSign(i_ch, counter) * _Wij(i_gap, iz) * u_star;
886
887 turbulent_term += _WijPrime(i_gap, iz) * (2 * (*_mdot_soln)(node_out) / rho_in / S -
888 (*_mdot_soln)(node_out_j) / Sj / rho_j -
889 (*_mdot_soln)(node_out_i) / Si / rho_i);
890 counter++;
891 }
892 turbulent_term *= _CT;
893 auto Re = (((*_mdot_soln)(node_in) / S) * Dh_i / mu_in);
894 _friction_args = FrictionStruct(i_ch, Re, S, w_perim);
896 if (_ff_soln)
897 _ff_soln->set(node_out, ff);
899 auto ki = 0.0;
900 if ((*_mdot_soln)(node_out) >= 0)
901 ki = k_grid[i_ch][iz - 1];
902 else
903 ki = k_grid[i_ch][iz];
904 auto friction_term = (ff * dz / Dh_i + ki) * 0.5 *
905 (*_mdot_soln)(node_out)*std::abs((*_mdot_soln)(node_out)) /
906 (S * (*_rho_soln)(node_out));
907 auto gravity_term = _dir_grav * _g_grav * (*_rho_soln)(node_out)*dz * S;
908 auto DP = (1 / S) * (time_term + mass_term1 + mass_term2 + crossflow_term + turbulent_term +
909 friction_term + gravity_term); // Pa
910 _DP(i_ch, iz) = DP;
911 if (_DP_soln)
912 _DP_soln->set(node_out, DP);
913 }
914 }
915 }
916 else
917 {
918 LibmeshPetscCall(MatZeroEntries(_amc_time_derivative_mat));
919 LibmeshPetscCall(MatZeroEntries(_amc_advective_derivative_mat));
920 LibmeshPetscCall(MatZeroEntries(_amc_cross_derivative_mat));
921 LibmeshPetscCall(MatZeroEntries(_amc_friction_force_mat));
922 LibmeshPetscCall(VecZeroEntries(_amc_time_derivative_rhs));
923 LibmeshPetscCall(VecZeroEntries(_amc_advective_derivative_rhs));
924 LibmeshPetscCall(VecZeroEntries(_amc_cross_derivative_rhs));
925 LibmeshPetscCall(VecZeroEntries(_amc_friction_force_rhs));
926 LibmeshPetscCall(VecZeroEntries(_amc_gravity_rhs));
927 LibmeshPetscCall(MatZeroEntries(_amc_sys_mdot_mat));
928 LibmeshPetscCall(VecZeroEntries(_amc_sys_mdot_rhs));
929 for (unsigned int iz = first_node; iz < last_node + 1; iz++)
930 {
931 auto k_grid = _subchannel_mesh.getKGrid();
932 auto dz = _z_grid[iz] - _z_grid[iz - 1];
933 auto iz_ind = iz - first_node;
934 for (unsigned int i_ch = 0; i_ch < _n_channels; i_ch++)
935 {
936 // inlet and outlet nodes
937 auto * node_in = _subchannel_mesh.getChannelNode(i_ch, iz - 1);
938 auto * node_out = _subchannel_mesh.getChannelNode(i_ch, iz);
939
940 // interpolation weight coefficient
941 PetscScalar Pe = 0.5;
942 if (_interpolation_scheme == 3)
943 {
944 // Compute the Peclet number
945 auto S_in = (*_S_flow_soln)(node_in);
946 auto S_out = (*_S_flow_soln)(node_out);
947 auto S_interp = computeInterpolatedValue(S_out, S_in, 0.5);
948 auto w_perim_in = (*_w_perim_soln)(node_in);
949 auto w_perim_out = (*_w_perim_soln)(node_out);
950 auto w_perim_interp = this->computeInterpolatedValue(w_perim_out, w_perim_in, 0.5);
951 auto mdot_loc =
952 this->computeInterpolatedValue((*_mdot_soln)(node_out), (*_mdot_soln)(node_in), 0.5);
953 auto mu_in = (*_mu_soln)(node_in);
954 auto mu_out = (*_mu_soln)(node_out);
955 auto mu_interp = this->computeInterpolatedValue(mu_out, mu_in, 0.5);
956 auto Dh_i = 4.0 * S_interp / w_perim_interp;
957 // Compute friction factor
958 auto Re = ((mdot_loc / S_interp) * Dh_i / mu_interp);
959 _friction_args = FrictionStruct(i_ch, Re, S_interp, w_perim_interp);
961 if (_ff_soln)
962 _ff_soln->set(node_out, ff);
964 auto ki = 0.0;
965 if ((*_mdot_soln)(node_out) >= 0)
966 ki = k_grid[i_ch][iz - 1];
967 else
968 ki = k_grid[i_ch][iz];
969 Pe = 1.0 / ((ff * dz / Dh_i + ki) * 0.5) * mdot_loc / std::abs(mdot_loc);
970 }
971 auto alpha = computeInterpolationCoefficients(Pe);
972
973 // inlet, outlet, and interpolated density
974 auto rho_in = (*_rho_soln)(node_in);
975 auto rho_out = (*_rho_soln)(node_out);
976 auto rho_interp = computeInterpolatedValue(rho_out, rho_in, Pe);
977
978 // inlet, outlet, and interpolated viscosity
979 auto mu_in = (*_mu_soln)(node_in);
980 auto mu_out = (*_mu_soln)(node_out);
981 auto mu_interp = computeInterpolatedValue(mu_out, mu_in, Pe);
982
983 // inlet, outlet, and interpolated axial surface area
984 auto S_in = (*_S_flow_soln)(node_in);
985 auto S_out = (*_S_flow_soln)(node_out);
986 auto S_interp = computeInterpolatedValue(S_out, S_in, Pe);
987
988 // inlet, outlet, and interpolated wetted perimeter
989 auto w_perim_in = (*_w_perim_soln)(node_in);
990 auto w_perim_out = (*_w_perim_soln)(node_out);
991 auto w_perim_interp = computeInterpolatedValue(w_perim_out, w_perim_in, Pe);
992
993 // hydraulic diameter in the i direction
994 auto Dh_i = 4.0 * S_interp / w_perim_interp;
995
996 // Keep temporal storage independent of the spatial interpolation scheme. Applying the
997 // upwind coefficient to this term puts the storage on the upstream node and leaves the
998 // first axial row without transient inertia in the upwind limit.
999 PetscInt row_tt = i_ch + _n_channels * iz_ind;
1000 PetscInt col_tt = i_ch + _n_channels * iz_ind;
1001 PetscScalar value_tt = _TR * dz / _dt;
1002 LibmeshPetscCall(MatSetValues(
1003 _amc_time_derivative_mat, 1, &row_tt, 1, &col_tt, &value_tt, INSERT_VALUES));
1004
1005 PetscScalar value_vec_tt = _TR * _mdot_soln->old(node_out) * dz / _dt;
1006 PetscInt row_vec_tt = i_ch + _n_channels * iz_ind;
1007 LibmeshPetscCall(
1008 VecSetValues(_amc_time_derivative_rhs, 1, &row_vec_tt, &value_vec_tt, ADD_VALUES));
1009
1011 if (iz == first_node)
1012 {
1013 PetscScalar value_vec_at = Utility::pow<2>((*_mdot_soln)(node_in)) / (S_in * rho_in);
1014 PetscInt row_vec_at = i_ch + _n_channels * iz_ind;
1015 LibmeshPetscCall(VecSetValues(
1016 _amc_advective_derivative_rhs, 1, &row_vec_at, &value_vec_at, ADD_VALUES));
1017 }
1018 else
1019 {
1020 PetscInt row_at = i_ch + _n_channels * iz_ind;
1021 PetscInt col_at = i_ch + _n_channels * (iz_ind - 1);
1022 PetscScalar value_at = -1.0 * std::abs((*_mdot_soln)(node_in)) / (S_in * rho_in);
1023 LibmeshPetscCall(MatSetValues(
1024 _amc_advective_derivative_mat, 1, &row_at, 1, &col_at, &value_at, INSERT_VALUES));
1025 }
1026
1027 // Adding diagonal elements
1028 PetscInt row_at = i_ch + _n_channels * iz_ind;
1029 PetscInt col_at = i_ch + _n_channels * iz_ind;
1030 PetscScalar value_at = std::abs((*_mdot_soln)(node_out)) / (S_out * rho_out);
1031 LibmeshPetscCall(MatSetValues(
1032 _amc_advective_derivative_mat, 1, &row_at, 1, &col_at, &value_at, INSERT_VALUES));
1033
1035 unsigned int counter = 0;
1036 unsigned int cross_index = iz; // iz-1;
1037 for (auto i_gap : _subchannel_mesh.getChannelGaps(i_ch))
1038 {
1039 auto chans = _subchannel_mesh.getGapChannels(i_gap);
1040 unsigned int ii_ch = chans.first;
1041 unsigned int jj_ch = chans.second;
1042 auto * node_in_i = _subchannel_mesh.getChannelNode(ii_ch, iz - 1);
1043 auto * node_in_j = _subchannel_mesh.getChannelNode(jj_ch, iz - 1);
1044 auto * node_out_i = _subchannel_mesh.getChannelNode(ii_ch, iz);
1045 auto * node_out_j = _subchannel_mesh.getChannelNode(jj_ch, iz);
1046 auto rho_i =
1047 computeInterpolatedValue((*_rho_soln)(node_out_i), (*_rho_soln)(node_in_i), Pe);
1048 auto rho_j =
1049 computeInterpolatedValue((*_rho_soln)(node_out_j), (*_rho_soln)(node_in_j), Pe);
1050 auto S_i =
1051 computeInterpolatedValue((*_S_flow_soln)(node_out_i), (*_S_flow_soln)(node_in_i), Pe);
1052 auto S_j =
1053 computeInterpolatedValue((*_S_flow_soln)(node_out_j), (*_S_flow_soln)(node_in_j), Pe);
1054 auto u_star = 0.0;
1055 // figure out donor axial velocity
1056 if (_Wij(i_gap, cross_index) > 0.0)
1057 {
1058 if (iz == first_node)
1059 {
1060 u_star = (*_mdot_soln)(node_in_i) / S_i / rho_i;
1061 PetscScalar value_vec_ct = -1.0 * alpha *
1062 _subchannel_mesh.getCrossflowSign(i_ch, counter) *
1063 _Wij(i_gap, cross_index) * u_star;
1064 PetscInt row_vec_ct = i_ch + _n_channels * iz_ind;
1065 LibmeshPetscCall(VecSetValues(
1066 _amc_cross_derivative_rhs, 1, &row_vec_ct, &value_vec_ct, ADD_VALUES));
1067 }
1068 else
1069 {
1070 PetscScalar value_ct = alpha * _subchannel_mesh.getCrossflowSign(i_ch, counter) *
1071 _Wij(i_gap, cross_index) / S_i / rho_i;
1072 PetscInt row_ct = i_ch + _n_channels * iz_ind;
1073 PetscInt col_ct = ii_ch + _n_channels * (iz_ind - 1);
1074 LibmeshPetscCall(MatSetValues(
1075 _amc_cross_derivative_mat, 1, &row_ct, 1, &col_ct, &value_ct, ADD_VALUES));
1076 }
1077 PetscScalar value_ct = (1.0 - alpha) *
1078 _subchannel_mesh.getCrossflowSign(i_ch, counter) *
1079 _Wij(i_gap, cross_index) / S_i / rho_i;
1080 PetscInt row_ct = i_ch + _n_channels * iz_ind;
1081 PetscInt col_ct = ii_ch + _n_channels * iz_ind;
1082 LibmeshPetscCall(MatSetValues(
1083 _amc_cross_derivative_mat, 1, &row_ct, 1, &col_ct, &value_ct, ADD_VALUES));
1084 }
1085 else if (_Wij(i_gap, cross_index) < 0.0) // _Wij=0 operations not necessary
1086 {
1087 if (iz == first_node)
1088 {
1089 u_star = (*_mdot_soln)(node_in_j) / S_j / rho_j;
1090 PetscScalar value_vec_ct = -1.0 * alpha *
1091 _subchannel_mesh.getCrossflowSign(i_ch, counter) *
1092 _Wij(i_gap, cross_index) * u_star;
1093 PetscInt row_vec_ct = i_ch + _n_channels * iz_ind;
1094 LibmeshPetscCall(VecSetValues(
1095 _amc_cross_derivative_rhs, 1, &row_vec_ct, &value_vec_ct, ADD_VALUES));
1096 }
1097 else
1098 {
1099 PetscScalar value_ct = alpha * _subchannel_mesh.getCrossflowSign(i_ch, counter) *
1100 _Wij(i_gap, cross_index) / S_j / rho_j;
1101 PetscInt row_ct = i_ch + _n_channels * iz_ind;
1102 PetscInt col_ct = jj_ch + _n_channels * (iz_ind - 1);
1103 LibmeshPetscCall(MatSetValues(
1104 _amc_cross_derivative_mat, 1, &row_ct, 1, &col_ct, &value_ct, ADD_VALUES));
1105 }
1106 PetscScalar value_ct = (1.0 - alpha) *
1107 _subchannel_mesh.getCrossflowSign(i_ch, counter) *
1108 _Wij(i_gap, cross_index) / S_j / rho_j;
1109 PetscInt row_ct = i_ch + _n_channels * iz_ind;
1110 PetscInt col_ct = jj_ch + _n_channels * iz_ind;
1111 LibmeshPetscCall(MatSetValues(
1112 _amc_cross_derivative_mat, 1, &row_ct, 1, &col_ct, &value_ct, ADD_VALUES));
1113 }
1114
1115 if (iz == first_node)
1116 {
1117 PetscScalar value_vec_ct = -2.0 * alpha * (*_mdot_soln)(node_in)*_CT *
1118 _WijPrime(i_gap, cross_index) / (rho_interp * S_interp);
1119 value_vec_ct += alpha * (*_mdot_soln)(node_in_j)*_CT * _WijPrime(i_gap, cross_index) /
1120 (rho_j * S_j);
1121 value_vec_ct += alpha * (*_mdot_soln)(node_in_i)*_CT * _WijPrime(i_gap, cross_index) /
1122 (rho_i * S_i);
1123 PetscInt row_vec_ct = i_ch + _n_channels * iz_ind;
1124 LibmeshPetscCall(
1125 VecSetValues(_amc_cross_derivative_rhs, 1, &row_vec_ct, &value_vec_ct, ADD_VALUES));
1126 }
1127 else
1128 {
1129 PetscScalar value_center_ct =
1130 2.0 * alpha * _CT * _WijPrime(i_gap, cross_index) / (rho_interp * S_interp);
1131 PetscInt row_ct = i_ch + _n_channels * iz_ind;
1132 PetscInt col_ct = i_ch + _n_channels * (iz_ind - 1);
1133 LibmeshPetscCall(MatSetValues(
1134 _amc_cross_derivative_mat, 1, &row_ct, 1, &col_ct, &value_center_ct, ADD_VALUES));
1135
1136 PetscScalar value_left_ct =
1137 -1.0 * alpha * _CT * _WijPrime(i_gap, cross_index) / (rho_j * S_j);
1138 row_ct = i_ch + _n_channels * iz_ind;
1139 col_ct = jj_ch + _n_channels * (iz_ind - 1);
1140 LibmeshPetscCall(MatSetValues(
1141 _amc_cross_derivative_mat, 1, &row_ct, 1, &col_ct, &value_left_ct, ADD_VALUES));
1142
1143 PetscScalar value_right_ct =
1144 -1.0 * alpha * _CT * _WijPrime(i_gap, cross_index) / (rho_i * S_i);
1145 row_ct = i_ch + _n_channels * iz_ind;
1146 col_ct = ii_ch + _n_channels * (iz_ind - 1);
1147 LibmeshPetscCall(MatSetValues(
1148 _amc_cross_derivative_mat, 1, &row_ct, 1, &col_ct, &value_right_ct, ADD_VALUES));
1149 }
1150
1151 PetscScalar value_center_ct =
1152 2.0 * (1.0 - alpha) * _CT * _WijPrime(i_gap, cross_index) / (rho_interp * S_interp);
1153 PetscInt row_ct = i_ch + _n_channels * iz_ind;
1154 PetscInt col_ct = i_ch + _n_channels * iz_ind;
1155 LibmeshPetscCall(MatSetValues(
1156 _amc_cross_derivative_mat, 1, &row_ct, 1, &col_ct, &value_center_ct, ADD_VALUES));
1157
1158 PetscScalar value_left_ct =
1159 -1.0 * (1.0 - alpha) * _CT * _WijPrime(i_gap, cross_index) / (rho_j * S_j);
1160 row_ct = i_ch + _n_channels * iz_ind;
1161 col_ct = jj_ch + _n_channels * iz_ind;
1162 LibmeshPetscCall(MatSetValues(
1163 _amc_cross_derivative_mat, 1, &row_ct, 1, &col_ct, &value_left_ct, ADD_VALUES));
1164
1165 PetscScalar value_right_ct =
1166 -1.0 * (1.0 - alpha) * _CT * _WijPrime(i_gap, cross_index) / (rho_i * S_i);
1167 row_ct = i_ch + _n_channels * iz_ind;
1168 col_ct = ii_ch + _n_channels * iz_ind;
1169 LibmeshPetscCall(MatSetValues(
1170 _amc_cross_derivative_mat, 1, &row_ct, 1, &col_ct, &value_right_ct, ADD_VALUES));
1171 counter++;
1172 }
1173
1175 PetscScalar mdot_interp =
1176 computeInterpolatedValue((*_mdot_soln)(node_out), (*_mdot_soln)(node_in), Pe);
1177 auto Re = ((mdot_interp / S_interp) * Dh_i / mu_interp);
1178 _friction_args = FrictionStruct(i_ch, Re, S_interp, w_perim_interp);
1180 if (_ff_soln)
1181 _ff_soln->set(node_out, ff);
1183 auto ki = 0.0;
1184 if ((*_mdot_soln)(node_out) >= 0)
1185 ki = k_grid[i_ch][iz - 1];
1186 else
1187 ki = k_grid[i_ch][iz];
1188 auto coef = (ff * dz / Dh_i + ki) * 0.5 * std::abs((*_mdot_soln)(node_out)) /
1189 (S_interp * rho_interp);
1190 if (iz == first_node)
1191 {
1192 PetscScalar value_vec = -1.0 * alpha * coef * (*_mdot_soln)(node_in);
1193 PetscInt row_vec = i_ch + _n_channels * iz_ind;
1194 LibmeshPetscCall(
1195 VecSetValues(_amc_friction_force_rhs, 1, &row_vec, &value_vec, ADD_VALUES));
1196 }
1197 else
1198 {
1199 PetscInt row = i_ch + _n_channels * iz_ind;
1200 PetscInt col = i_ch + _n_channels * (iz_ind - 1);
1201 PetscScalar value = alpha * coef;
1202 LibmeshPetscCall(
1203 MatSetValues(_amc_friction_force_mat, 1, &row, 1, &col, &value, INSERT_VALUES));
1204 }
1205
1206 // Adding diagonal elements
1207 PetscInt row = i_ch + _n_channels * iz_ind;
1208 PetscInt col = i_ch + _n_channels * iz_ind;
1209 PetscScalar value = (1.0 - alpha) * coef;
1210 LibmeshPetscCall(
1211 MatSetValues(_amc_friction_force_mat, 1, &row, 1, &col, &value, INSERT_VALUES));
1212
1214 PetscScalar value_vec = _dir_grav * -1.0 * _g_grav * rho_interp * dz * S_interp;
1215 PetscInt row_vec = i_ch + _n_channels * iz_ind;
1216 LibmeshPetscCall(VecSetValues(_amc_gravity_rhs, 1, &row_vec, &value_vec, ADD_VALUES));
1217 }
1218 }
1220 LibmeshPetscCall(MatZeroEntries(_amc_sys_mdot_mat));
1221 LibmeshPetscCall(VecZeroEntries(_amc_sys_mdot_rhs));
1222 LibmeshPetscCall(MatAssemblyBegin(_amc_time_derivative_mat, MAT_FINAL_ASSEMBLY));
1223 LibmeshPetscCall(MatAssemblyEnd(_amc_time_derivative_mat, MAT_FINAL_ASSEMBLY));
1224 LibmeshPetscCall(MatAssemblyBegin(_amc_advective_derivative_mat, MAT_FINAL_ASSEMBLY));
1225 LibmeshPetscCall(MatAssemblyEnd(_amc_advective_derivative_mat, MAT_FINAL_ASSEMBLY));
1226 LibmeshPetscCall(MatAssemblyBegin(_amc_cross_derivative_mat, MAT_FINAL_ASSEMBLY));
1227 LibmeshPetscCall(MatAssemblyEnd(_amc_cross_derivative_mat, MAT_FINAL_ASSEMBLY));
1228 LibmeshPetscCall(MatAssemblyBegin(_amc_friction_force_mat, MAT_FINAL_ASSEMBLY));
1229 LibmeshPetscCall(MatAssemblyEnd(_amc_friction_force_mat, MAT_FINAL_ASSEMBLY));
1230 LibmeshPetscCall(MatAssemblyBegin(_amc_sys_mdot_mat, MAT_FINAL_ASSEMBLY));
1231 LibmeshPetscCall(MatAssemblyEnd(_amc_sys_mdot_mat, MAT_FINAL_ASSEMBLY));
1232 // Matrix
1233#if !PETSC_VERSION_LESS_THAN(3, 15, 0)
1234 LibmeshPetscCall(
1235 MatAXPY(_amc_sys_mdot_mat, 1.0, _amc_time_derivative_mat, UNKNOWN_NONZERO_PATTERN));
1236 LibmeshPetscCall(MatAssemblyBegin(_amc_sys_mdot_mat, MAT_FINAL_ASSEMBLY));
1237 LibmeshPetscCall(MatAssemblyEnd(_amc_sys_mdot_mat, MAT_FINAL_ASSEMBLY));
1238 LibmeshPetscCall(
1239 MatAXPY(_amc_sys_mdot_mat, 1.0, _amc_advective_derivative_mat, UNKNOWN_NONZERO_PATTERN));
1240 LibmeshPetscCall(MatAssemblyBegin(_amc_sys_mdot_mat, MAT_FINAL_ASSEMBLY));
1241 LibmeshPetscCall(MatAssemblyEnd(_amc_sys_mdot_mat, MAT_FINAL_ASSEMBLY));
1242 LibmeshPetscCall(
1243 MatAXPY(_amc_sys_mdot_mat, 1.0, _amc_cross_derivative_mat, UNKNOWN_NONZERO_PATTERN));
1244 LibmeshPetscCall(MatAssemblyBegin(_amc_sys_mdot_mat, MAT_FINAL_ASSEMBLY));
1245 LibmeshPetscCall(MatAssemblyEnd(_amc_sys_mdot_mat, MAT_FINAL_ASSEMBLY));
1246 LibmeshPetscCall(
1247 MatAXPY(_amc_sys_mdot_mat, 1.0, _amc_friction_force_mat, UNKNOWN_NONZERO_PATTERN));
1248#else
1249 LibmeshPetscCall(
1250 MatAXPY(_amc_sys_mdot_mat, 1.0, _amc_time_derivative_mat, DIFFERENT_NONZERO_PATTERN));
1251 LibmeshPetscCall(MatAssemblyBegin(_amc_sys_mdot_mat, MAT_FINAL_ASSEMBLY));
1252 LibmeshPetscCall(MatAssemblyEnd(_amc_sys_mdot_mat, MAT_FINAL_ASSEMBLY));
1253 LibmeshPetscCall(
1254 MatAXPY(_amc_sys_mdot_mat, 1.0, _amc_advective_derivative_mat, DIFFERENT_NONZERO_PATTERN));
1255 LibmeshPetscCall(MatAssemblyBegin(_amc_sys_mdot_mat, MAT_FINAL_ASSEMBLY));
1256 LibmeshPetscCall(MatAssemblyEnd(_amc_sys_mdot_mat, MAT_FINAL_ASSEMBLY));
1257 LibmeshPetscCall(
1258 MatAXPY(_amc_sys_mdot_mat, 1.0, _amc_cross_derivative_mat, DIFFERENT_NONZERO_PATTERN));
1259 LibmeshPetscCall(MatAssemblyBegin(_amc_sys_mdot_mat, MAT_FINAL_ASSEMBLY));
1260 LibmeshPetscCall(MatAssemblyEnd(_amc_sys_mdot_mat, MAT_FINAL_ASSEMBLY));
1261 LibmeshPetscCall(
1262 MatAXPY(_amc_sys_mdot_mat, 1.0, _amc_friction_force_mat, DIFFERENT_NONZERO_PATTERN));
1263#endif
1264 LibmeshPetscCall(MatAssemblyBegin(_amc_sys_mdot_mat, MAT_FINAL_ASSEMBLY));
1265 LibmeshPetscCall(MatAssemblyEnd(_amc_sys_mdot_mat, MAT_FINAL_ASSEMBLY));
1266 // RHS
1267 LibmeshPetscCall(VecAXPY(_amc_sys_mdot_rhs, 1.0, _amc_time_derivative_rhs));
1268 LibmeshPetscCall(VecAXPY(_amc_sys_mdot_rhs, 1.0, _amc_advective_derivative_rhs));
1269 LibmeshPetscCall(VecAXPY(_amc_sys_mdot_rhs, 1.0, _amc_cross_derivative_rhs));
1270 LibmeshPetscCall(VecAXPY(_amc_sys_mdot_rhs, 1.0, _amc_friction_force_rhs));
1271 LibmeshPetscCall(VecAXPY(_amc_sys_mdot_rhs, 1.0, _amc_gravity_rhs));
1272 if (_segregated_bool)
1273 {
1274 // Assembly the matrix system
1275 LibmeshPetscCall(populateVectorFromHandle<SolutionHandle>(
1276 _prod, *_mdot_soln, first_node, last_node, _n_channels));
1277 Vec ls;
1278 LibmeshPetscCall(VecDuplicate(_amc_sys_mdot_rhs, &ls));
1279 LibmeshPetscCall(MatMult(_amc_sys_mdot_mat, _prod, ls));
1280 LibmeshPetscCall(VecAXPY(ls, -1.0, _amc_sys_mdot_rhs));
1281 PetscScalar * xx;
1282 LibmeshPetscCall(VecGetArray(ls, &xx));
1283 for (unsigned int iz = first_node; iz < last_node + 1; iz++)
1284 {
1285 auto iz_ind = iz - first_node;
1286 for (unsigned int i_ch = 0; i_ch < _n_channels; i_ch++)
1287 {
1288 // Setting nodes
1289 auto * node_in = _subchannel_mesh.getChannelNode(i_ch, iz - 1);
1290 auto * node_out = _subchannel_mesh.getChannelNode(i_ch, iz);
1291
1292 // inlet, outlet, and interpolated axial surface area
1293 auto S_in = (*_S_flow_soln)(node_in);
1294 auto S_out = (*_S_flow_soln)(node_out);
1295 auto S_interp = computeInterpolatedValue(S_out, S_in, 0.5);
1296
1297 // Setting solutions
1298 if (S_interp != 0)
1299 {
1300 auto DP = (1 / S_interp) * xx[iz_ind * _n_channels + i_ch];
1301 _DP(i_ch, iz) = DP;
1302 if (_DP_soln)
1303 _DP_soln->set(node_out, DP);
1304 }
1305 else
1306 {
1307 auto DP = 0.0;
1308 _DP(i_ch, iz) = DP;
1309 if (_DP_soln)
1310 _DP_soln->set(node_out, DP);
1311 }
1312 }
1313 }
1314 LibmeshPetscCall(VecDestroy(&ls));
1315 }
1316 }
1317}
1318
1319void
1321{
1322 const unsigned int last_node = (iblock + 1) * _block_size;
1323 const unsigned int first_node = iblock * _block_size + 1;
1324 if (!_implicit_bool)
1325 {
1327 {
1328 for (unsigned int iz = last_node; iz > first_node - 1; iz--)
1329 {
1330 // Calculate pressure in the inlet of the cell assuming known outlet
1331 for (unsigned int i_ch = 0; i_ch < _n_channels; i_ch++)
1332 {
1333 auto * node_out = _subchannel_mesh.getChannelNode(i_ch, iz);
1334 auto * node_in = _subchannel_mesh.getChannelNode(i_ch, iz - 1);
1335 // update Pressure solution
1336 _P_soln->set(node_in, (*_P_soln)(node_out) + _DP(i_ch, iz));
1337 }
1338 }
1339 }
1340 else
1341 {
1342 for (unsigned int iz = last_node; iz > first_node - 1; iz--)
1343 {
1344 // Calculate pressure in the inlet of the cell assuming known outlet
1345 for (unsigned int i_ch = 0; i_ch < _n_channels; i_ch++)
1346 {
1347 auto * node_out = _subchannel_mesh.getChannelNode(i_ch, iz);
1348 auto * node_in = _subchannel_mesh.getChannelNode(i_ch, iz - 1);
1349 // update Pressure solution
1350 // Note: assuming uniform axial discretization in the curren code
1351 // We will need to update this later if we allow non-uniform refinements in the axial
1352 // direction
1353 PetscScalar Pe = 0.5;
1354 auto alpha = computeInterpolationCoefficients(Pe);
1355 if (iz == last_node)
1356 {
1357 _P_soln->set(node_in, (*_P_soln)(node_out) + _DP(i_ch, iz) / 2.0);
1358 }
1359 else
1360 {
1361 _P_soln->set(node_in,
1362 (*_P_soln)(node_out) + (1.0 - alpha) * _DP(i_ch, iz) +
1363 alpha * _DP(i_ch, iz - 1));
1364 }
1365 }
1366 }
1367 }
1368 }
1369 else
1370 {
1372 {
1373 LibmeshPetscCall(VecZeroEntries(_amc_pressure_force_rhs));
1374 for (unsigned int iz = last_node; iz > first_node - 1; iz--)
1375 {
1376 auto iz_ind = iz - first_node;
1377 // Calculate pressure in the inlet of the cell assuming known outlet
1378 for (unsigned int i_ch = 0; i_ch < _n_channels; i_ch++)
1379 {
1380 auto * node_out = _subchannel_mesh.getChannelNode(i_ch, iz);
1381 auto * node_in = _subchannel_mesh.getChannelNode(i_ch, iz - 1);
1382
1383 // inlet, outlet, and interpolated axial surface area
1384 auto S_in = (*_S_flow_soln)(node_in);
1385 auto S_out = (*_S_flow_soln)(node_out);
1386 auto S_interp = computeInterpolatedValue(S_out, S_in, 0.5);
1387
1388 // Creating matrix of coefficients
1389 PetscInt row = i_ch + _n_channels * iz_ind;
1390 PetscInt col = i_ch + _n_channels * iz_ind;
1391 PetscScalar value = -1.0 * S_interp;
1392 LibmeshPetscCall(
1393 MatSetValues(_amc_pressure_force_mat, 1, &row, 1, &col, &value, INSERT_VALUES));
1394
1395 if (iz == last_node)
1396 {
1397 PetscScalar value = -1.0 * (*_P_soln)(node_out)*S_interp;
1398 PetscInt row = i_ch + _n_channels * iz_ind;
1399 LibmeshPetscCall(VecSetValues(_amc_pressure_force_rhs, 1, &row, &value, ADD_VALUES));
1400 }
1401 else
1402 {
1403 PetscInt row = i_ch + _n_channels * iz_ind;
1404 PetscInt col = i_ch + _n_channels * (iz_ind + 1);
1405 PetscScalar value = 1.0 * S_interp;
1406 LibmeshPetscCall(
1407 MatSetValues(_amc_pressure_force_mat, 1, &row, 1, &col, &value, INSERT_VALUES));
1408 }
1409
1410 if (_segregated_bool)
1411 {
1412 auto dp_out = _DP(i_ch, iz);
1413 PetscScalar value_v = -1.0 * dp_out * S_interp;
1414 PetscInt row_v = i_ch + _n_channels * iz_ind;
1415 LibmeshPetscCall(
1416 VecSetValues(_amc_pressure_force_rhs, 1, &row_v, &value_v, ADD_VALUES));
1417 }
1418 }
1419 }
1420 // Solving pressure problem
1421 LibmeshPetscCall(MatAssemblyBegin(_amc_pressure_force_mat, MAT_FINAL_ASSEMBLY));
1422 LibmeshPetscCall(MatAssemblyEnd(_amc_pressure_force_mat, MAT_FINAL_ASSEMBLY));
1423 if (_segregated_bool)
1424 {
1425 KSP ksploc;
1426 PC pc;
1427 Vec sol;
1428 LibmeshPetscCall(VecDuplicate(_amc_pressure_force_rhs, &sol));
1429 LibmeshPetscCall(KSPCreate(PETSC_COMM_SELF, &ksploc));
1430 LibmeshPetscCall(KSPSetOperators(ksploc, _amc_pressure_force_mat, _amc_pressure_force_mat));
1431 LibmeshPetscCall(KSPGetPC(ksploc, &pc));
1432 LibmeshPetscCall(PCSetType(pc, PCJACOBI));
1433 LibmeshPetscCall(KSPSetTolerances(ksploc, _rtol, _atol, _dtol, _maxit));
1434 LibmeshPetscCall(KSPSetFromOptions(ksploc));
1435 LibmeshPetscCall(KSPSolve(ksploc, _amc_pressure_force_rhs, sol));
1436 PetscScalar * xx;
1437 LibmeshPetscCall(VecGetArray(sol, &xx));
1438 // update Pressure solution
1439 for (unsigned int iz = last_node; iz > first_node - 1; iz--)
1440 {
1441 auto iz_ind = iz - first_node;
1442 for (unsigned int i_ch = 0; i_ch < _n_channels; i_ch++)
1443 {
1444 auto * node_in = _subchannel_mesh.getChannelNode(i_ch, iz - 1);
1445 PetscScalar value = xx[iz_ind * _n_channels + i_ch];
1446 _P_soln->set(node_in, value);
1447 }
1448 }
1449 LibmeshPetscCall(VecZeroEntries(_amc_pressure_force_rhs));
1450 LibmeshPetscCall(KSPDestroy(&ksploc));
1451 LibmeshPetscCall(VecDestroy(&sol));
1452 }
1453 }
1454 else
1455 {
1456 LibmeshPetscCall(VecZeroEntries(_amc_pressure_force_rhs));
1457 for (unsigned int iz = last_node; iz > first_node - 1; iz--)
1458 {
1459 auto iz_ind = iz - first_node;
1460 // Calculate pressure in the inlet of the cell assuming known outlet
1461 for (unsigned int i_ch = 0; i_ch < _n_channels; i_ch++)
1462 {
1463 auto * node_out = _subchannel_mesh.getChannelNode(i_ch, iz);
1464 auto * node_in = _subchannel_mesh.getChannelNode(i_ch, iz - 1);
1465
1466 // inlet, outlet, and interpolated axial surface area
1467 auto S_in = (*_S_flow_soln)(node_in);
1468 auto S_out = (*_S_flow_soln)(node_out);
1469 auto S_interp = computeInterpolatedValue(S_out, S_in, 0.5);
1470
1471 // Creating matrix of coefficients
1472 PetscInt row = i_ch + _n_channels * iz_ind;
1473 PetscInt col = i_ch + _n_channels * iz_ind;
1474 PetscScalar value = -1.0 * S_interp;
1475 LibmeshPetscCall(
1476 MatSetValues(_amc_pressure_force_mat, 1, &row, 1, &col, &value, INSERT_VALUES));
1477
1478 if (iz == last_node)
1479 {
1480 PetscScalar value = -1.0 * (*_P_soln)(node_out)*S_interp;
1481 PetscInt row = i_ch + _n_channels * iz_ind;
1482 LibmeshPetscCall(VecSetValues(_amc_pressure_force_rhs, 1, &row, &value, ADD_VALUES));
1483
1484 auto dp_out = _DP(i_ch, iz);
1485 PetscScalar value_v = -1.0 * dp_out / 2.0 * S_interp;
1486 PetscInt row_v = i_ch + _n_channels * iz_ind;
1487 LibmeshPetscCall(
1488 VecSetValues(_amc_pressure_force_rhs, 1, &row_v, &value_v, ADD_VALUES));
1489 }
1490 else
1491 {
1492 PetscInt row = i_ch + _n_channels * iz_ind;
1493 PetscInt col = i_ch + _n_channels * (iz_ind + 1);
1494 PetscScalar value = 1.0 * S_interp;
1495 LibmeshPetscCall(
1496 MatSetValues(_amc_pressure_force_mat, 1, &row, 1, &col, &value, INSERT_VALUES));
1497
1498 if (_segregated_bool)
1499 {
1500 auto dp_in = _DP(i_ch, iz - 1);
1501 auto dp_out = _DP(i_ch, iz);
1502 auto dp_interp = computeInterpolatedValue(dp_out, dp_in, 0.5);
1503 PetscScalar value_v = -1.0 * dp_interp * S_interp;
1504 PetscInt row_v = i_ch + _n_channels * iz_ind;
1505 LibmeshPetscCall(
1506 VecSetValues(_amc_pressure_force_rhs, 1, &row_v, &value_v, ADD_VALUES));
1507 }
1508 }
1509 }
1510 }
1511 // Solving pressure problem
1512 LibmeshPetscCall(MatAssemblyBegin(_amc_pressure_force_mat, MAT_FINAL_ASSEMBLY));
1513 LibmeshPetscCall(MatAssemblyEnd(_amc_pressure_force_mat, MAT_FINAL_ASSEMBLY));
1515 _console << "Block: " << iblock << " - Axial momentum pressure force matrix assembled"
1516 << std::endl;
1517
1518 if (_segregated_bool)
1519 {
1520 KSP ksploc;
1521 PC pc;
1522 Vec sol;
1523 LibmeshPetscCall(VecDuplicate(_amc_pressure_force_rhs, &sol));
1524 LibmeshPetscCall(KSPCreate(PETSC_COMM_SELF, &ksploc));
1525 LibmeshPetscCall(KSPSetOperators(ksploc, _amc_pressure_force_mat, _amc_pressure_force_mat));
1526 LibmeshPetscCall(KSPGetPC(ksploc, &pc));
1527 LibmeshPetscCall(PCSetType(pc, PCJACOBI));
1528 LibmeshPetscCall(KSPSetTolerances(ksploc, _rtol, _atol, _dtol, _maxit));
1529 LibmeshPetscCall(KSPSetFromOptions(ksploc));
1530 LibmeshPetscCall(KSPSolve(ksploc, _amc_pressure_force_rhs, sol));
1531 PetscScalar * xx;
1532 LibmeshPetscCall(VecGetArray(sol, &xx));
1533 // update Pressure solution
1534 for (unsigned int iz = last_node; iz > first_node - 1; iz--)
1535 {
1536 auto iz_ind = iz - first_node;
1537 for (unsigned int i_ch = 0; i_ch < _n_channels; i_ch++)
1538 {
1539 auto * node_in = _subchannel_mesh.getChannelNode(i_ch, iz - 1);
1540 PetscScalar value = xx[iz_ind * _n_channels + i_ch];
1541 _P_soln->set(node_in, value);
1542 }
1543 }
1544 LibmeshPetscCall(VecZeroEntries(_amc_pressure_force_rhs));
1545 LibmeshPetscCall(KSPDestroy(&ksploc));
1546 LibmeshPetscCall(VecDestroy(&sol));
1547 }
1548 }
1549 }
1550}
1551
1552Real
1554{
1555 const unsigned int last_node = (iblock + 1) * _block_size;
1556 const unsigned int first_node = iblock * _block_size + 1;
1557 std::vector<Real> residual;
1558 residual.reserve(_block_size * _n_channels);
1559 Real residual_norm_sq = 0.0;
1560 Real temperature_norm_sq = 0.0;
1561 for (unsigned int iz = first_node; iz < last_node + 1; iz++)
1562 {
1563 for (unsigned int i_ch = 0; i_ch < _n_channels; i_ch++)
1564 {
1565 auto * node = _subchannel_mesh.getChannelNode(i_ch, iz);
1566 const Real T = (*_T_soln)(node);
1567 const Real T_from_ph = _fp->T_from_p_h((*_P_soln)(node) + _P_out, (*_h_soln)(node));
1568 residual.push_back(T_from_ph - T);
1569 residual_norm_sq += Utility::pow<2>(residual.back());
1570 temperature_norm_sq += Utility::pow<2>(T);
1571 }
1572 }
1573
1574 // Set the temperature solution with relaxation if needed
1575 std::size_t i = 0;
1576 for (unsigned int iz = first_node; iz < last_node + 1; iz++)
1577 for (unsigned int i_ch = 0; i_ch < _n_channels; i_ch++)
1578 {
1579 auto * node = _subchannel_mesh.getChannelNode(i_ch, iz);
1580 _T_soln->set(node, (*_T_soln)(node) + _T_relaxation * residual[i]);
1581 ++i;
1582 }
1583
1584 return std::sqrt(residual_norm_sq) / (std::sqrt(temperature_norm_sq) + 1e-14);
1585}
1586
1587void
1589{
1590 const unsigned int last_node = (iblock + 1) * _block_size;
1591 const unsigned int first_node = iblock * _block_size + 1;
1592 if (iblock == 0)
1593 {
1594 for (unsigned int i_ch = 0; i_ch < _n_channels; i_ch++)
1595 {
1596 auto * node = _subchannel_mesh.getChannelNode(i_ch, 0);
1597 _rho_soln->set(node, _fp->rho_from_p_T((*_P_soln)(node) + _P_out, (*_T_soln)(node)));
1598 }
1599 }
1600 for (unsigned int iz = first_node; iz < last_node + 1; iz++)
1601 {
1602 for (unsigned int i_ch = 0; i_ch < _n_channels; i_ch++)
1603 {
1604 auto * node = _subchannel_mesh.getChannelNode(i_ch, iz);
1605 _rho_soln->set(node, _fp->rho_from_p_T((*_P_soln)(node) + _P_out, (*_T_soln)(node)));
1606 }
1607 }
1608}
1609
1610void
1612{
1613 const unsigned int last_node = (iblock + 1) * _block_size;
1614 const unsigned int first_node = iblock * _block_size + 1;
1615 if (iblock == 0)
1616 {
1617 for (unsigned int i_ch = 0; i_ch < _n_channels; i_ch++)
1618 {
1619 auto * node = _subchannel_mesh.getChannelNode(i_ch, 0);
1620 _mu_soln->set(node, _fp->mu_from_p_T((*_P_soln)(node) + _P_out, (*_T_soln)(node)));
1621 }
1622 }
1623 for (unsigned int iz = first_node; iz < last_node + 1; iz++)
1624 {
1625 for (unsigned int i_ch = 0; i_ch < _n_channels; i_ch++)
1626 {
1627 auto * node = _subchannel_mesh.getChannelNode(i_ch, iz);
1628 _mu_soln->set(node, _fp->mu_from_p_T((*_P_soln)(node) + _P_out, (*_T_soln)(node)));
1629 }
1630 }
1631}
1632
1633void
1635{
1636 const unsigned int last_node = (iblock + 1) * _block_size;
1637 const unsigned int first_node = iblock * _block_size + 1;
1638 // Cross flow residual
1639 if (!_implicit_bool)
1640 {
1641 const Real & pitch = _subchannel_mesh.getPitch();
1642 for (unsigned int iz = first_node; iz < last_node + 1; iz++)
1643 {
1644 auto dz = _z_grid[iz] - _z_grid[iz - 1];
1645 for (unsigned int i_gap = 0; i_gap < _n_gaps; i_gap++)
1646 {
1647 auto chans = _subchannel_mesh.getGapChannels(i_gap);
1648 unsigned int i_ch = chans.first;
1649 unsigned int j_ch = chans.second;
1650 auto * node_in_i = _subchannel_mesh.getChannelNode(i_ch, iz - 1);
1651 auto * node_out_i = _subchannel_mesh.getChannelNode(i_ch, iz);
1652 auto * node_in_j = _subchannel_mesh.getChannelNode(j_ch, iz - 1);
1653 auto * node_out_j = _subchannel_mesh.getChannelNode(j_ch, iz);
1654 auto rho_i = (*_rho_soln)(node_in_i);
1655 auto rho_j = (*_rho_soln)(node_in_j);
1656 auto Si = (*_S_flow_soln)(node_in_i);
1657 auto Sj = (*_S_flow_soln)(node_in_j);
1658 auto Sij = dz * _subchannel_mesh.getGapWidth(iz, i_gap);
1659 auto Lij = pitch;
1660 // total local form loss in the ij direction
1661 auto friction_term = _kij * _Wij(i_gap, iz) * std::abs(_Wij(i_gap, iz));
1662 auto DPij = (*_P_soln)(node_in_i) - (*_P_soln)(node_in_j);
1663 // Figure out donor cell density
1664 auto rho_star = 0.0;
1665 if (_Wij(i_gap, iz) > 0.0)
1666 rho_star = rho_i;
1667 else if (_Wij(i_gap, iz) < 0.0)
1668 rho_star = rho_j;
1669 else
1670 rho_star = (rho_i + rho_j) / 2.0;
1671 auto mass_term_out =
1672 (*_mdot_soln)(node_out_i) / (*_S_flow_soln)(node_out_i) / (*_rho_soln)(node_out_i) +
1673 (*_mdot_soln)(node_out_j) / (*_S_flow_soln)(node_out_j) / (*_rho_soln)(node_out_j);
1674 auto mass_term_in =
1675 (*_mdot_soln)(node_in_i) / Si / rho_i + (*_mdot_soln)(node_in_j) / Sj / rho_j;
1676 auto term_out = Sij * rho_star * (Lij / dz) * mass_term_out * _Wij(i_gap, iz);
1677 auto term_in = Sij * rho_star * (Lij / dz) * mass_term_in * _Wij(i_gap, iz - 1);
1678 auto inertia_term = term_out - term_in;
1679 auto pressure_term = 2 * Utility::pow<2>(Sij) * DPij * rho_star;
1680 auto time_term =
1681 _TR * 2.0 * (_Wij(i_gap, iz) - _Wij_old(i_gap, iz)) * Lij * Sij * rho_star / _dt;
1682
1683 _Wij_residual_matrix(i_gap, iz - 1 - iblock * _block_size) =
1684 time_term + friction_term + inertia_term - pressure_term;
1685 }
1686 }
1687 }
1688 else
1689 {
1690 // Initializing to zero the elements of the lateral momentum assembly
1691 LibmeshPetscCall(MatZeroEntries(_cmc_time_derivative_mat));
1692 LibmeshPetscCall(MatZeroEntries(_cmc_advective_derivative_mat));
1693 LibmeshPetscCall(MatZeroEntries(_cmc_friction_force_mat));
1694 LibmeshPetscCall(MatZeroEntries(_cmc_pressure_force_mat));
1695 LibmeshPetscCall(VecZeroEntries(_cmc_time_derivative_rhs));
1696 LibmeshPetscCall(VecZeroEntries(_cmc_advective_derivative_rhs));
1697 LibmeshPetscCall(VecZeroEntries(_cmc_friction_force_rhs));
1698 LibmeshPetscCall(VecZeroEntries(_cmc_pressure_force_rhs));
1699 LibmeshPetscCall(MatZeroEntries(_cmc_sys_Wij_mat));
1700 LibmeshPetscCall(VecZeroEntries(_cmc_sys_Wij_rhs));
1701 const Real & pitch = _subchannel_mesh.getPitch();
1702 for (unsigned int iz = first_node; iz < last_node + 1; iz++)
1703 {
1704 auto dz = _z_grid[iz] - _z_grid[iz - 1];
1705 auto iz_ind = iz - first_node;
1706 for (unsigned int i_gap = 0; i_gap < _n_gaps; i_gap++)
1707 {
1708 auto chans = _subchannel_mesh.getGapChannels(i_gap);
1709 unsigned int i_ch = chans.first;
1710 unsigned int j_ch = chans.second;
1711 auto * node_in_i = _subchannel_mesh.getChannelNode(i_ch, iz - 1);
1712 auto * node_out_i = _subchannel_mesh.getChannelNode(i_ch, iz);
1713 auto * node_in_j = _subchannel_mesh.getChannelNode(j_ch, iz - 1);
1714 auto * node_out_j = _subchannel_mesh.getChannelNode(j_ch, iz);
1715
1716 // inlet, outlet, and interpolated densities
1717 auto rho_i_in = (*_rho_soln)(node_in_i);
1718 auto rho_i_out = (*_rho_soln)(node_out_i);
1719 auto rho_i_interp = computeInterpolatedValue(rho_i_out, rho_i_in, 0.5);
1720 auto rho_j_in = (*_rho_soln)(node_in_j);
1721 auto rho_j_out = (*_rho_soln)(node_out_j);
1722 auto rho_j_interp = computeInterpolatedValue(rho_j_out, rho_j_in, 0.5);
1723
1724 // inlet, outlet, and interpolated areas
1725 auto S_i_in = (*_S_flow_soln)(node_in_i);
1726 auto S_i_out = (*_S_flow_soln)(node_out_i);
1727 auto S_j_in = (*_S_flow_soln)(node_in_j);
1728 auto S_j_out = (*_S_flow_soln)(node_out_j);
1729
1730 // Cross-sectional gap area
1731 auto Sij = dz * _subchannel_mesh.getGapWidth(iz, i_gap);
1732 auto Lij = pitch;
1733
1734 // Figure out donor cell density
1735 auto rho_star = 0.0;
1736 if (_Wij(i_gap, iz) > 0.0)
1737 rho_star = rho_i_interp;
1738 else if (_Wij(i_gap, iz) < 0.0)
1739 rho_star = rho_j_interp;
1740 else
1741 rho_star = (rho_i_interp + rho_j_interp) / 2.0;
1742
1743 // Assembling time derivative
1744 PetscScalar time_factor = _TR * Lij * Sij * rho_star / _dt;
1745 PetscInt row_td = i_gap + _n_gaps * iz_ind;
1746 PetscInt col_td = i_gap + _n_gaps * iz_ind;
1747 PetscScalar value_td = time_factor;
1748 LibmeshPetscCall(MatSetValues(
1749 _cmc_time_derivative_mat, 1, &row_td, 1, &col_td, &value_td, INSERT_VALUES));
1750 PetscScalar value_td_rhs = time_factor * _Wij_old(i_gap, iz);
1751 LibmeshPetscCall(
1752 VecSetValues(_cmc_time_derivative_rhs, 1, &row_td, &value_td_rhs, INSERT_VALUES));
1753
1754 // Assembling inertial term
1755 PetscScalar Pe = 0.5;
1756 auto alpha = computeInterpolationCoefficients(Pe);
1757 auto mass_term_out = (*_mdot_soln)(node_out_i) / S_i_out / rho_i_out +
1758 (*_mdot_soln)(node_out_j) / S_j_out / rho_j_out;
1759 auto mass_term_in = (*_mdot_soln)(node_in_i) / S_i_in / rho_i_in +
1760 (*_mdot_soln)(node_in_j) / S_j_in / rho_j_in;
1761 auto term_out = Sij * rho_star * (Lij / dz) * mass_term_out / 2.0;
1762 auto term_in = Sij * rho_star * (Lij / dz) * mass_term_in / 2.0;
1763 if (iz == first_node)
1764 {
1765 PetscInt row_ad = i_gap + _n_gaps * iz_ind;
1766 PetscScalar value_ad = term_in * alpha * _Wij(i_gap, iz - 1);
1767 LibmeshPetscCall(
1768 VecSetValues(_cmc_advective_derivative_rhs, 1, &row_ad, &value_ad, ADD_VALUES));
1769
1770 PetscInt col_ad = i_gap + _n_gaps * iz_ind;
1771 value_ad = -1.0 * term_in * (1.0 - alpha) + term_out * alpha;
1772 LibmeshPetscCall(MatSetValues(
1773 _cmc_advective_derivative_mat, 1, &row_ad, 1, &col_ad, &value_ad, INSERT_VALUES));
1774
1775 col_ad = i_gap + _n_gaps * (iz_ind + 1);
1776 value_ad = term_out * (1.0 - alpha);
1777 LibmeshPetscCall(MatSetValues(
1778 _cmc_advective_derivative_mat, 1, &row_ad, 1, &col_ad, &value_ad, INSERT_VALUES));
1779 }
1780 else if (iz == last_node)
1781 {
1782 PetscInt row_ad = i_gap + _n_gaps * iz_ind;
1783 PetscInt col_ad = i_gap + _n_gaps * (iz_ind - 1);
1784 PetscScalar value_ad = -1.0 * term_in * alpha;
1785 LibmeshPetscCall(MatSetValues(
1786 _cmc_advective_derivative_mat, 1, &row_ad, 1, &col_ad, &value_ad, INSERT_VALUES));
1787
1788 col_ad = i_gap + _n_gaps * iz_ind;
1789 value_ad = -1.0 * term_in * (1.0 - alpha) + term_out * alpha;
1790 LibmeshPetscCall(MatSetValues(
1791 _cmc_advective_derivative_mat, 1, &row_ad, 1, &col_ad, &value_ad, INSERT_VALUES));
1792
1793 value_ad = -1.0 * term_out * (1.0 - alpha) * _Wij(i_gap, iz);
1794 LibmeshPetscCall(
1795 VecSetValues(_cmc_advective_derivative_rhs, 1, &row_ad, &value_ad, ADD_VALUES));
1796 }
1797 else
1798 {
1799 PetscInt row_ad = i_gap + _n_gaps * iz_ind;
1800 PetscInt col_ad = i_gap + _n_gaps * (iz_ind - 1);
1801 PetscScalar value_ad = -1.0 * term_in * alpha;
1802 LibmeshPetscCall(MatSetValues(
1803 _cmc_advective_derivative_mat, 1, &row_ad, 1, &col_ad, &value_ad, INSERT_VALUES));
1804
1805 col_ad = i_gap + _n_gaps * iz_ind;
1806 value_ad = -1.0 * term_in * (1.0 - alpha) + term_out * alpha;
1807 LibmeshPetscCall(MatSetValues(
1808 _cmc_advective_derivative_mat, 1, &row_ad, 1, &col_ad, &value_ad, INSERT_VALUES));
1809
1810 col_ad = i_gap + _n_gaps * (iz_ind + 1);
1811 value_ad = term_out * (1.0 - alpha);
1812 LibmeshPetscCall(MatSetValues(
1813 _cmc_advective_derivative_mat, 1, &row_ad, 1, &col_ad, &value_ad, INSERT_VALUES));
1814 }
1815 // Assembling friction force
1816 PetscInt row_ff = i_gap + _n_gaps * iz_ind;
1817 PetscInt col_ff = i_gap + _n_gaps * iz_ind;
1818 PetscScalar value_ff = _kij * std::abs(_Wij(i_gap, iz)) / 2.0;
1819 LibmeshPetscCall(MatSetValues(
1820 _cmc_friction_force_mat, 1, &row_ff, 1, &col_ff, &value_ff, INSERT_VALUES));
1821
1822 // Assembling pressure force
1824
1826 {
1827 PetscScalar pressure_factor = Utility::pow<2>(Sij) * rho_star;
1828 PetscInt row_pf = i_gap + _n_gaps * iz_ind;
1829 PetscInt col_pf = i_ch + _n_channels * iz_ind;
1830 PetscScalar value_pf = -1.0 * alpha * pressure_factor;
1831 LibmeshPetscCall(
1832 MatSetValues(_cmc_pressure_force_mat, 1, &row_pf, 1, &col_pf, &value_pf, ADD_VALUES));
1833 col_pf = j_ch + _n_channels * iz_ind;
1834 value_pf = alpha * pressure_factor;
1835 LibmeshPetscCall(
1836 MatSetValues(_cmc_pressure_force_mat, 1, &row_pf, 1, &col_pf, &value_pf, ADD_VALUES));
1837
1838 if (iz == last_node)
1839 {
1840 PetscInt row_pf = i_gap + _n_gaps * iz_ind;
1841 PetscScalar value_pf = (1.0 - alpha) * pressure_factor * (*_P_soln)(node_out_i);
1842 LibmeshPetscCall(
1843 VecSetValues(_cmc_pressure_force_rhs, 1, &row_pf, &value_pf, ADD_VALUES));
1844 value_pf = -1.0 * (1.0 - alpha) * pressure_factor * (*_P_soln)(node_out_j);
1845 LibmeshPetscCall(
1846 VecSetValues(_cmc_pressure_force_rhs, 1, &row_pf, &value_pf, ADD_VALUES));
1847 }
1848 else
1849 {
1850 row_pf = i_gap + _n_gaps * iz_ind;
1851 col_pf = i_ch + _n_channels * (iz_ind + 1);
1852 value_pf = -1.0 * (1.0 - alpha) * pressure_factor;
1853 LibmeshPetscCall(MatSetValues(
1854 _cmc_pressure_force_mat, 1, &row_pf, 1, &col_pf, &value_pf, ADD_VALUES));
1855 col_pf = j_ch + _n_channels * (iz_ind + 1);
1856 value_pf = (1.0 - alpha) * pressure_factor;
1857 LibmeshPetscCall(MatSetValues(
1858 _cmc_pressure_force_mat, 1, &row_pf, 1, &col_pf, &value_pf, ADD_VALUES));
1859 }
1860 }
1861 else
1862 {
1863 PetscScalar pressure_factor = Utility::pow<2>(Sij) * rho_star;
1864 PetscInt row_pf = i_gap + _n_gaps * iz_ind;
1865 PetscInt col_pf = i_ch + _n_channels * iz_ind;
1866 PetscScalar value_pf = -1.0 * pressure_factor;
1867 LibmeshPetscCall(
1868 MatSetValues(_cmc_pressure_force_mat, 1, &row_pf, 1, &col_pf, &value_pf, ADD_VALUES));
1869 col_pf = j_ch + _n_channels * iz_ind;
1870 value_pf = pressure_factor;
1871 LibmeshPetscCall(
1872 MatSetValues(_cmc_pressure_force_mat, 1, &row_pf, 1, &col_pf, &value_pf, ADD_VALUES));
1873 }
1874 }
1875 }
1877 LibmeshPetscCall(MatZeroEntries(_cmc_sys_Wij_mat));
1878 LibmeshPetscCall(VecZeroEntries(_cmc_sys_Wij_rhs));
1879 LibmeshPetscCall(MatAssemblyBegin(_cmc_time_derivative_mat, MAT_FINAL_ASSEMBLY));
1880 LibmeshPetscCall(MatAssemblyEnd(_cmc_time_derivative_mat, MAT_FINAL_ASSEMBLY));
1881 LibmeshPetscCall(MatAssemblyBegin(_cmc_advective_derivative_mat, MAT_FINAL_ASSEMBLY));
1882 LibmeshPetscCall(MatAssemblyEnd(_cmc_advective_derivative_mat, MAT_FINAL_ASSEMBLY));
1883 LibmeshPetscCall(MatAssemblyBegin(_cmc_friction_force_mat, MAT_FINAL_ASSEMBLY));
1884 LibmeshPetscCall(MatAssemblyEnd(_cmc_friction_force_mat, MAT_FINAL_ASSEMBLY));
1885 LibmeshPetscCall(MatAssemblyBegin(_cmc_pressure_force_mat, MAT_FINAL_ASSEMBLY));
1886 LibmeshPetscCall(MatAssemblyEnd(_cmc_pressure_force_mat, MAT_FINAL_ASSEMBLY));
1887 LibmeshPetscCall(MatAssemblyBegin(_cmc_sys_Wij_mat, MAT_FINAL_ASSEMBLY));
1888 LibmeshPetscCall(MatAssemblyEnd(_cmc_sys_Wij_mat, MAT_FINAL_ASSEMBLY));
1889 // Matrix
1890#if !PETSC_VERSION_LESS_THAN(3, 15, 0)
1891 LibmeshPetscCall(
1892 MatAXPY(_cmc_sys_Wij_mat, 1.0, _cmc_time_derivative_mat, UNKNOWN_NONZERO_PATTERN));
1893 LibmeshPetscCall(MatAssemblyBegin(_cmc_sys_Wij_mat, MAT_FINAL_ASSEMBLY));
1894 LibmeshPetscCall(MatAssemblyEnd(_cmc_sys_Wij_mat, MAT_FINAL_ASSEMBLY));
1895 LibmeshPetscCall(
1896 MatAXPY(_cmc_sys_Wij_mat, 1.0, _cmc_advective_derivative_mat, UNKNOWN_NONZERO_PATTERN));
1897 LibmeshPetscCall(MatAssemblyBegin(_cmc_sys_Wij_mat, MAT_FINAL_ASSEMBLY));
1898 LibmeshPetscCall(MatAssemblyEnd(_cmc_sys_Wij_mat, MAT_FINAL_ASSEMBLY));
1899 LibmeshPetscCall(
1900 MatAXPY(_cmc_sys_Wij_mat, 1.0, _cmc_friction_force_mat, UNKNOWN_NONZERO_PATTERN));
1901#else
1902 LibmeshPetscCall(
1903 MatAXPY(_cmc_sys_Wij_mat, 1.0, _cmc_time_derivative_mat, DIFFERENT_NONZERO_PATTERN));
1904 LibmeshPetscCall(MatAssemblyBegin(_cmc_sys_Wij_mat, MAT_FINAL_ASSEMBLY));
1905 LibmeshPetscCall(MatAssemblyEnd(_cmc_sys_Wij_mat, MAT_FINAL_ASSEMBLY));
1906 LibmeshPetscCall(
1907 MatAXPY(_cmc_sys_Wij_mat, 1.0, _cmc_advective_derivative_mat, DIFFERENT_NONZERO_PATTERN));
1908 LibmeshPetscCall(MatAssemblyBegin(_cmc_sys_Wij_mat, MAT_FINAL_ASSEMBLY));
1909 LibmeshPetscCall(MatAssemblyEnd(_cmc_sys_Wij_mat, MAT_FINAL_ASSEMBLY));
1910 LibmeshPetscCall(
1911 MatAXPY(_cmc_sys_Wij_mat, 1.0, _cmc_friction_force_mat, DIFFERENT_NONZERO_PATTERN));
1912#endif
1913 LibmeshPetscCall(MatAssemblyBegin(_cmc_sys_Wij_mat, MAT_FINAL_ASSEMBLY));
1914 LibmeshPetscCall(MatAssemblyEnd(_cmc_sys_Wij_mat, MAT_FINAL_ASSEMBLY));
1915 // RHS
1916 LibmeshPetscCall(VecAXPY(_cmc_sys_Wij_rhs, 1.0, _cmc_time_derivative_rhs));
1917 LibmeshPetscCall(VecAXPY(_cmc_sys_Wij_rhs, 1.0, _cmc_advective_derivative_rhs));
1918 LibmeshPetscCall(VecAXPY(_cmc_sys_Wij_rhs, 1.0, _cmc_friction_force_rhs));
1919
1920 if (_segregated_bool)
1921 {
1922 // Assembly the matrix system
1923 Vec sol_holder_P;
1924 LibmeshPetscCall(createPetscVector(sol_holder_P, _block_size * _n_gaps));
1925 Vec sol_holder_W;
1926 LibmeshPetscCall(createPetscVector(sol_holder_W, _block_size * _n_gaps));
1927 LibmeshPetscCall(populateVectorFromHandle<SolutionHandle>(
1928 _prodp, *_P_soln, first_node - 1, last_node - 1, _n_channels));
1930 _Wij_vec, _Wij, first_node, last_node, _n_gaps));
1931 LibmeshPetscCall(MatMult(_cmc_sys_Wij_mat, _Wij_vec, sol_holder_W));
1932 LibmeshPetscCall(VecAXPY(sol_holder_W, -1.0, _cmc_sys_Wij_rhs));
1933 LibmeshPetscCall(MatMult(_cmc_pressure_force_mat, _prodp, sol_holder_P));
1934 LibmeshPetscCall(VecAXPY(sol_holder_P, -1.0, _cmc_pressure_force_rhs));
1935 LibmeshPetscCall(VecAXPY(sol_holder_W, 1.0, sol_holder_P));
1936 PetscScalar * xx;
1937 LibmeshPetscCall(VecGetArray(sol_holder_W, &xx));
1938 for (unsigned int iz = first_node; iz < last_node + 1; iz++)
1939 {
1940 auto iz_ind = iz - first_node;
1941 for (unsigned int i_gap = 0; i_gap < _n_gaps; i_gap++)
1942 {
1943 _Wij_residual_matrix(i_gap, iz - 1 - iblock * _block_size) = xx[iz_ind * _n_gaps + i_gap];
1944 }
1945 }
1946 LibmeshPetscCall(VecDestroy(&sol_holder_P));
1947 LibmeshPetscCall(VecDestroy(&sol_holder_W));
1948 }
1949 }
1950}
1951
1952void
1954{
1955 const unsigned int last_node = (iblock + 1) * _block_size;
1956 const unsigned int first_node = iblock * _block_size + 1;
1957 _mixing_closure->computeBlockMixingParameters(first_node, last_node);
1958 for (unsigned int iz = first_node; iz < last_node + 1; iz++)
1959 {
1960 auto dz = _z_grid[iz] - _z_grid[iz - 1];
1961 for (unsigned int i_gap = 0; i_gap < _n_gaps; i_gap++)
1962 {
1963 auto chans = _subchannel_mesh.getGapChannels(i_gap);
1964 unsigned int i_ch = chans.first;
1965 unsigned int j_ch = chans.second;
1966 auto * node_in_i = _subchannel_mesh.getChannelNode(i_ch, iz - 1);
1967 auto * node_out_i = _subchannel_mesh.getChannelNode(i_ch, iz);
1968 auto * node_in_j = _subchannel_mesh.getChannelNode(j_ch, iz - 1);
1969 auto * node_out_j = _subchannel_mesh.getChannelNode(j_ch, iz);
1970 auto Si_in = (*_S_flow_soln)(node_in_i);
1971 auto Sj_in = (*_S_flow_soln)(node_in_j);
1972 auto Si_out = (*_S_flow_soln)(node_out_i);
1973 auto Sj_out = (*_S_flow_soln)(node_out_j);
1974 auto gap = _subchannel_mesh.getGapWidth(iz, i_gap);
1975 auto Sij = dz * gap;
1976 // Mass flux of the gap: total mass flow rate of the two subchannels over their total flow
1977 // area, averaged over the inlet and outlet of the cell. This is the area-weighted mean of rho
1978 // V of the two subchannels; Eq. (30) of Pacio et al. (2022) uses the arithmetic mean rho (V_i
1979 // + V_j) / 2, which differs when the two subchannels have different flow areas
1980 auto avg_massflux =
1981 0.5 * (((*_mdot_soln)(node_in_i) + (*_mdot_soln)(node_in_j)) / (Si_in + Sj_in) +
1982 ((*_mdot_soln)(node_out_i) + (*_mdot_soln)(node_out_j)) / (Si_out + Sj_out));
1983 auto beta = computeMixingParameter(i_gap, iz);
1984
1985 if (!_implicit_bool)
1986 {
1987 _WijPrime(i_gap, iz) = beta * avg_massflux * Sij;
1988 }
1989 else
1990 {
1991 auto iz_ind = iz - first_node;
1992 PetscScalar base_value = beta * 0.5 * Sij;
1993
1994 // Bottom values
1995 if (iz == first_node)
1996 {
1997 PetscScalar value_tl = -1.0 * base_value / (Si_in + Sj_in) *
1998 ((*_mdot_soln)(node_in_i) + (*_mdot_soln)(node_in_j));
1999 PetscInt row = i_gap + _n_gaps * iz_ind;
2000 LibmeshPetscCall(
2001 VecSetValues(_amc_turbulent_cross_flows_rhs, 1, &row, &value_tl, INSERT_VALUES));
2002 }
2003 else
2004 {
2005 PetscScalar value_tl = base_value / (Si_in + Sj_in);
2006 PetscInt row = i_gap + _n_gaps * iz_ind;
2007
2008 PetscInt col_ich = i_ch + _n_channels * (iz_ind - 1);
2009 LibmeshPetscCall(MatSetValues(
2010 _amc_turbulent_cross_flows_mat, 1, &row, 1, &col_ich, &value_tl, INSERT_VALUES));
2011
2012 PetscInt col_jch = j_ch + _n_channels * (iz_ind - 1);
2013 LibmeshPetscCall(MatSetValues(
2014 _amc_turbulent_cross_flows_mat, 1, &row, 1, &col_jch, &value_tl, INSERT_VALUES));
2015 }
2016
2017 // Top values
2018 PetscScalar value_bl = base_value / (Si_out + Sj_out);
2019 PetscInt row = i_gap + _n_gaps * iz_ind;
2020
2021 PetscInt col_ich = i_ch + _n_channels * iz_ind;
2022 LibmeshPetscCall(MatSetValues(
2023 _amc_turbulent_cross_flows_mat, 1, &row, 1, &col_ich, &value_bl, INSERT_VALUES));
2024
2025 PetscInt col_jch = j_ch + _n_channels * iz_ind;
2026 LibmeshPetscCall(MatSetValues(
2027 _amc_turbulent_cross_flows_mat, 1, &row, 1, &col_jch, &value_bl, INSERT_VALUES));
2028 }
2029 }
2030 }
2031
2032 if (_implicit_bool)
2033 {
2034 LibmeshPetscCall(MatAssemblyBegin(_amc_turbulent_cross_flows_mat, MAT_FINAL_ASSEMBLY));
2035 LibmeshPetscCall(MatAssemblyEnd(_amc_turbulent_cross_flows_mat, MAT_FINAL_ASSEMBLY));
2036
2038 Vec loc_prod;
2039 Vec loc_Wij;
2040 LibmeshPetscCall(VecDuplicate(_amc_sys_mdot_rhs, &loc_prod));
2041 LibmeshPetscCall(VecDuplicate(_Wij_vec, &loc_Wij));
2042 LibmeshPetscCall(populateVectorFromHandle<SolutionHandle>(
2043 loc_prod, *_mdot_soln, first_node, last_node, _n_channels));
2044 LibmeshPetscCall(MatMult(_amc_turbulent_cross_flows_mat, loc_prod, loc_Wij));
2045 LibmeshPetscCall(VecAXPY(loc_Wij, -1.0, _amc_turbulent_cross_flows_rhs));
2047 loc_Wij, _WijPrime, first_node, last_node, _n_gaps));
2048 LibmeshPetscCall(VecDestroy(&loc_prod));
2049 LibmeshPetscCall(VecDestroy(&loc_Wij));
2050 }
2051}
2052
2053Real
2054SubChannel1PhaseProblem::computeMixingParameter(unsigned int i_gap, unsigned int iz) const
2055{
2056 auto beta = _mixing_closure->computeMixingParameter(i_gap, iz);
2057 if (!std::isfinite(beta) || beta < 0.0)
2058 mooseError(name(),
2059 ": Mixing closure returned invalid beta = ",
2060 beta,
2061 " for gap ",
2062 i_gap,
2063 " at axial index ",
2064 iz,
2065 ". Beta must be finite and non-negative.");
2066
2067 return beta;
2068}
2069
2070Real
2071SubChannel1PhaseProblem::computeSweepFlowMixingParameter(unsigned int i_gap, unsigned int iz) const
2072{
2073 auto beta = _mixing_closure->computeSweepFlowMixingParameter(i_gap, iz);
2074 if (!std::isfinite(beta) || beta < 0.0)
2075 mooseError(name(),
2076 ": Mixing closure returned invalid sweep-flow coefficient = ",
2077 beta,
2078 " for gap ",
2079 i_gap,
2080 " at axial index ",
2081 iz,
2082 ". sweep-flow coefficient must be finite and non-negative.");
2083
2084 return beta;
2085}
2086
2089{
2090 const unsigned int last_node = (iblock + 1) * _block_size;
2091 const unsigned int first_node = iblock * _block_size + 1;
2092 libMesh::DenseVector<Real> Wij_residual_vector(_n_gaps * _block_size, 0.0);
2093 // Assign the solution to the cross-flow matrix
2094 int i = 0;
2095 for (unsigned int iz = first_node; iz < last_node + 1; iz++)
2096 {
2097 for (unsigned int i_gap = 0; i_gap < _n_gaps; i_gap++)
2098 {
2099 _Wij(i_gap, iz) = solution(i);
2100 i++;
2101 }
2102 }
2103
2104 // Calculating sum of crossflows
2105 computeSumWij(iblock);
2106 // Solving axial flux
2107 computeMdot(iblock);
2108 // Calculation of turbulent Crossflow
2109 computeWijPrime(iblock);
2110 // Solving for Pressure Drop
2111 computeDP(iblock);
2112 // Solving for pressure
2113 computeP(iblock);
2114 // Populating lateral crossflow residual matrix
2115 computeWijResidual(iblock);
2116
2117 // Turn the residual matrix into a residual vector
2118 for (unsigned int iz = 0; iz < _block_size; iz++)
2119 {
2120 for (unsigned int i_gap = 0; i_gap < _n_gaps; i_gap++)
2121 {
2122 int i = _n_gaps * iz + i_gap; // column wise transfer
2123 Wij_residual_vector(i) = _Wij_residual_matrix(i_gap, iz);
2124 }
2125 }
2126 return Wij_residual_vector;
2127}
2128
2129PetscErrorCode
2131 const libMesh::DenseVector<Real> & solution,
2133{
2134 SNES snes;
2135 KSP ksp;
2136 PC pc;
2137 Vec x, r;
2138 PetscScalar * xx;
2139
2141 LibmeshPetscCall(SNESCreate(PETSC_COMM_SELF, &snes));
2142 LibmeshPetscCall(VecCreate(PETSC_COMM_SELF, &x));
2143 LibmeshPetscCall(VecSetSizes(x, PETSC_DECIDE, _block_size * _n_gaps));
2144 LibmeshPetscCall(VecSetFromOptions(x));
2145 LibmeshPetscCall(VecDuplicate(x, &r));
2146
2147#if PETSC_VERSION_LESS_THAN(3, 13, 0)
2148 LibmeshPetscCall(PetscOptionsSetValue(PETSC_NULL, "-snes_mf", PETSC_NULL));
2149#else
2150 LibmeshPetscCall(SNESSetUseMatrixFree(snes, PETSC_FALSE, PETSC_TRUE));
2151#endif
2152 Ctx ctx;
2153 ctx.iblock = iblock;
2154 ctx.schp = this;
2155 LibmeshPetscCall(SNESSetFunction(snes, r, formFunction, &ctx));
2156 LibmeshPetscCall(SNESGetKSP(snes, &ksp));
2157 LibmeshPetscCall(KSPGetPC(ksp, &pc));
2158 LibmeshPetscCall(PCSetType(pc, PCNONE));
2159 LibmeshPetscCall(KSPSetTolerances(ksp, _rtol, _atol, _dtol, _maxit));
2160 LibmeshPetscCall(SNESSetFromOptions(snes));
2161 LibmeshPetscCall(VecGetArray(x, &xx));
2162 for (unsigned int i = 0; i < _block_size * _n_gaps; i++)
2163 {
2164 xx[i] = solution(i);
2165 }
2166 LibmeshPetscCall(VecRestoreArray(x, &xx));
2167
2168 LibmeshPetscCall(SNESSolve(snes, NULL, x));
2169 LibmeshPetscCall(VecGetArray(x, &xx));
2170 for (unsigned int i = 0; i < _block_size * _n_gaps; i++)
2171 root(i) = xx[i];
2172
2173 LibmeshPetscCall(VecRestoreArray(x, &xx));
2174 LibmeshPetscCall(VecDestroy(&x));
2175 LibmeshPetscCall(VecDestroy(&r));
2176 LibmeshPetscCall(SNESDestroy(&snes));
2177 PetscFunctionReturn(LIBMESH_PETSC_SUCCESS);
2178}
2179
2180PetscErrorCode
2182 Mat A, Vec rhs, unsigned int first_node, unsigned int last_node, const char * ksp_prefix)
2183{
2185
2186 // Create solution vector with rhs layout
2187 Vec x = nullptr;
2188 LibmeshPetscCall(VecDuplicate(rhs, &x));
2189
2190 // KSP setup
2191 KSP ksp = nullptr;
2192 PC pc = nullptr;
2193 LibmeshPetscCall(KSPCreate(PETSC_COMM_SELF, &ksp));
2194 LibmeshPetscCall(KSPSetOperators(ksp, A, A));
2195 LibmeshPetscCall(KSPGetPC(ksp, &pc));
2196 LibmeshPetscCall(PCSetType(pc, PCJACOBI));
2197 LibmeshPetscCall(KSPSetTolerances(ksp, _rtol, _atol, _dtol, _maxit));
2198 if (ksp_prefix && *ksp_prefix)
2199 LibmeshPetscCall(KSPSetOptionsPrefix(ksp, ksp_prefix));
2200 LibmeshPetscCall(KSPSetFromOptions(ksp));
2201
2202 // Solve
2203 LibmeshPetscCall(KSPSolve(ksp, rhs, x));
2204 KSPConvergedReason reason;
2205 LibmeshPetscCall(KSPGetConvergedReason(ksp, &reason));
2206 if (reason < 0)
2207 {
2208 PetscInt iterations;
2209 PetscReal residual_norm;
2210 LibmeshPetscCall(KSPGetIterationNumber(ksp, &iterations));
2211 LibmeshPetscCall(KSPGetResidualNorm(ksp, &residual_norm));
2212 mooseError(name(),
2213 ": enthalpy linear solve failed: ",
2214 KSPConvergedReasons[reason],
2215 " (",
2216 static_cast<int>(reason),
2217 ") after ",
2218 iterations,
2219 " iterations; residual norm = ",
2220 residual_norm,
2221 ".");
2222 }
2223
2224 // Scatter to _h_soln with sanity checks
2225 PetscScalar * xx = nullptr;
2226 LibmeshPetscCall(VecGetArray(x, &xx));
2227 for (unsigned int iz = first_node; iz <= last_node; ++iz)
2228 {
2229 const unsigned int iz_ind = iz - first_node;
2230 for (unsigned int i_ch = 0; i_ch < _n_channels; ++i_ch)
2231 {
2232 auto * node_out = _subchannel_mesh.getChannelNode(i_ch, iz);
2233 const PetscScalar h_out = xx[iz_ind * _n_channels + i_ch];
2234 if (h_out < 0.0)
2235 mooseError(
2236 name(), " : Calculation of negative Enthalpy h_out = ", h_out, " Axial Level = ", iz);
2237 _h_soln->set(node_out, h_out);
2238 }
2239 }
2240 LibmeshPetscCall(VecRestoreArray(x, &xx));
2241
2242 // Cleanup
2243 LibmeshPetscCall(KSPDestroy(&ksp));
2244 LibmeshPetscCall(VecDestroy(&x));
2245
2246 PetscFunctionReturn(LIBMESH_PETSC_SUCCESS);
2247}
2248
2249Real
2250SubChannel1PhaseProblem::computeAddedHeatDuct(unsigned int i_ch, unsigned int iz) const
2251{
2252 mooseAssert(iz > 0, "Trapezoidal rule requires starting at index 1 at least");
2253 if (_duct_mesh_exist)
2254 {
2255 auto subch_type = _subchannel_mesh.getSubchannelType(i_ch);
2256 if (subch_type == EChannelType::EDGE || subch_type == EChannelType::CORNER)
2257 {
2258 auto dz = _z_grid[iz] - _z_grid[iz - 1];
2259 auto * node_in_chan = _subchannel_mesh.getChannelNode(i_ch, iz - 1);
2260 auto * node_out_chan = _subchannel_mesh.getChannelNode(i_ch, iz);
2261 auto * node_in_duct = _subchannel_mesh.getDuctNodeFromChannel(node_in_chan);
2262 auto * node_out_duct = _subchannel_mesh.getDuctNodeFromChannel(node_out_chan);
2263 auto heat_rate_in = (*_duct_heat_flux_soln)(node_in_duct);
2264 auto heat_rate_out = (*_duct_heat_flux_soln)(node_out_duct);
2265 auto width = getSubChannelPeripheralDuctWidth(i_ch);
2266 return 0.5 * (heat_rate_in + heat_rate_out) * dz * width;
2267 }
2268 else
2269 {
2270 return 0.0;
2271 }
2272 }
2273 else
2274 {
2275 return 0.0;
2276 }
2277}
2278
2279PetscErrorCode
2281{
2283 // ---------- helper functions -----------------------------
2284 auto V = [&](const std::string & s)
2285 {
2287 _console << s << std::endl;
2288 };
2289
2290 auto DupMatAssembled = [&](Mat src, Mat * dst)
2291 {
2292 if (src)
2293 {
2294 LibmeshPetscCall(MatDuplicate(src, MAT_COPY_VALUES, dst));
2295 LibmeshPetscCall(MatAssemblyBegin(*dst, MAT_FINAL_ASSEMBLY));
2296 LibmeshPetscCall(MatAssemblyEnd(*dst, MAT_FINAL_ASSEMBLY));
2297 }
2298 else
2299 *dst = NULL;
2300 };
2301
2302 auto DupVecCopy = [&](Vec src, Vec * dst)
2303 {
2304 LibmeshPetscCall(VecDuplicate(src, dst));
2305 LibmeshPetscCall(VecCopy(src, *dst));
2306 };
2307
2308 const PetscInt Q = 3; // [mass conservation, axial momentum, cross momentum]
2309
2310 // small indexer
2311 auto Idx = [&](PetscInt r, PetscInt c) { return Q * r + c; };
2312
2313 // arrays that MUST be declared before lambdas use them
2314 std::vector<Mat> mat_array(Q * Q, NULL);
2315 std::vector<Vec> vec_array(Q, NULL);
2316
2317 // generic assembler for one governing equation row in the nested matrix
2318 auto AssembleEquation = [&](PetscInt f,
2319 Mat A0,
2320 Mat A1,
2321 Mat A2, // three blocks in row f (can be nullptr)
2322 Vec rhs, // base RHS for equation f
2323 Vec rhs_add, // optional extra RHS to add (can be nullptr)
2324 const char * label) // e.g. "Mass", "Lin mom", "Cross mom"
2325 {
2326 DupMatAssembled(A0, &mat_array[Idx(f, 0)]);
2327 DupMatAssembled(A1, &mat_array[Idx(f, 1)]);
2328 DupMatAssembled(A2, &mat_array[Idx(f, 2)]);
2329 DupVecCopy(rhs, &vec_array[f]);
2330 if (rhs_add)
2331 LibmeshPetscCall(VecAXPY(vec_array[f], 1.0, rhs_add));
2332 V(std::string(label) + " system assembled");
2333 };
2334
2335 // -----------------------------------------------------------------------------
2336 // Helper lambda that applies per-equation under-relaxation by modifying BOTH the
2337 // matrix block and the RHS for that equation.
2338 //
2339 // Specifically, with A_ff for the equation and D = diag(A_ff):
2340 // 1) Matrix diagonal scaling: D <- D / alpha, then A_ff's diagonal is replaced
2341 // with D. For alpha < 1 this increases diagonal dominance (more damping).
2342 // 2) RHS blending with the previous solution x_old:
2343 // rhs_f <- rhs_f + (1 - alpha) * (D / alpha) * x_old
2344 // where x_old is provided by the caller via `populate(work)`.
2345 //
2346 // Net effect: the solved x satisfies
2347 // A_ff x = rhs_f_original + ((1 - alpha)/alpha) * D * (x_old - x),
2348 // which damps updates toward x_old without changing the converged solution.
2349 // This equation relaxation is independent of the optional post-solve solution relaxation.
2350 // -----------------------------------------------------------------------------
2351 auto relaxEquation =
2352 [&](Mat diagonal_block, Vec rhs, Vec work, const Real relaxation, auto && populate)
2353 {
2354 if (relaxation == 1.0)
2355 return;
2356
2357 Vec diagonal = nullptr;
2358 LibmeshPetscCall(VecDuplicate(rhs, &diagonal));
2359
2360 // 1) diagonal_block: diag <- diag / relaxation
2361 LibmeshPetscCall(MatGetDiagonal(diagonal_block, diagonal));
2362 LibmeshPetscCall(VecScale(diagonal, 1.0 / relaxation));
2363 LibmeshPetscCall(MatDiagonalSet(diagonal_block, diagonal, INSERT_VALUES));
2364
2365 // 2) work <- x_old (caller-provided populator)
2366 LibmeshPetscCall(populate(work));
2367
2368 // 3) rhs += (1 - relaxation) * (diag .* work)
2369 LibmeshPetscCall(VecScale(diagonal, 1.0 - relaxation));
2370 LibmeshPetscCall(VecPointwiseMult(work, work, diagonal));
2371 LibmeshPetscCall(VecAXPY(rhs, 1.0, work));
2372
2373 LibmeshPetscCall(VecDestroy(&diagonal));
2374 };
2375
2376 // indices
2377 const unsigned int first_node = iblock * _block_size + 1;
2378 const unsigned int last_node = (iblock + 1) * _block_size;
2379
2380 // ---------- assemble per-block operators -----------------
2381 computeSumWij(iblock);
2382 computeMdot(iblock);
2383 computeWijPrime(iblock);
2384 computeDP(iblock);
2385 computeP(iblock);
2386 computeWijResidual(iblock);
2387
2388 V("Starting nested system.");
2389
2390 // Populate nested matrix with the individual physics
2391 // equation 0: Mass conservation
2392 AssembleEquation(/*f=*/0,
2395 /*A2=*/_mc_sumWij_mat,
2397 /*rhs_add=*/nullptr,
2398 /*label=*/"Mass");
2399
2400 // equation 1: Axial momentum conservation
2401 AssembleEquation(/*f=*/1,
2402 /*A0=*/_amc_sys_mdot_mat,
2404 /*A2=*/nullptr,
2406 /*rhs_add=*/_amc_sys_mdot_rhs,
2407 /*label=*/"Lin mom");
2408
2409 // equation 2: Cross momentum conservation
2410 AssembleEquation(/*f=*/2,
2411 /*A0=*/nullptr,
2413 /*A2=*/_cmc_sys_Wij_mat,
2414 /*rhs=*/_cmc_sys_Wij_rhs,
2415 /*rhs_add=*/_cmc_pressure_force_rhs,
2416 /*label=*/"Cross mom");
2417
2418 // ========================== Relaxation ====================
2419 if (true)
2420 {
2421 LibmeshPetscCall(populateVectorFromHandle<SolutionHandle>(
2422 _prod, *_mdot_soln, first_node, last_node, _n_channels));
2423
2424 Vec mdot_estimate;
2425 LibmeshPetscCall(createPetscVector(mdot_estimate, _block_size * _n_channels));
2426 Vec pmat_diag;
2427 LibmeshPetscCall(createPetscVector(pmat_diag, _block_size * _n_channels));
2428 Vec p_estimate;
2429 LibmeshPetscCall(createPetscVector(p_estimate, _block_size * _n_channels));
2430 Vec unity_vec;
2431 LibmeshPetscCall(createPetscVector(unity_vec, _block_size * _n_channels));
2432 LibmeshPetscCall(VecSet(unity_vec, 1.0));
2433 Vec sol_holder_P;
2434 LibmeshPetscCall(createPetscVector(sol_holder_P, _block_size * _n_gaps));
2435 Vec unity_vec_Wij;
2436 LibmeshPetscCall(createPetscVector(unity_vec_Wij, _block_size * _n_gaps));
2437 LibmeshPetscCall(VecSet(unity_vec_Wij, 1.0));
2438 Vec _Wij_loc_vec;
2439 LibmeshPetscCall(createPetscVector(_Wij_loc_vec, _block_size * _n_gaps));
2440 Vec _Wij_old_loc_vec;
2441 LibmeshPetscCall(createPetscVector(_Wij_old_loc_vec, _block_size * _n_gaps));
2442
2443 // ---- scale estimates ----
2444 // mdot_estimate = A(1,0) * mdot
2445 LibmeshPetscCall(MatMult(mat_array[Q /* (1,0) */], _prod, mdot_estimate));
2446
2447 // p_estimate = mdot_est / (diag(A(1,1)) + eps)
2448 LibmeshPetscCall(MatGetDiagonal(mat_array[Q + 1], pmat_diag));
2449 LibmeshPetscCall(VecAXPY(pmat_diag, 1e-10, unity_vec));
2450 LibmeshPetscCall(VecPointwiseDivide(p_estimate, mdot_estimate, pmat_diag));
2451
2452 // sol_holder_P = A(2,1) * p_estimate - rhs_cmc_pressure
2453 LibmeshPetscCall(MatMult(mat_array[2 * Q + 1], p_estimate, sol_holder_P));
2454 LibmeshPetscCall(VecAXPY(sol_holder_P, -1.0, _cmc_pressure_force_rhs));
2455
2456 // sumWij_loc from sol_holder_P (accumulate)
2457 Vec sumWij_loc;
2458 LibmeshPetscCall(createPetscVector(sumWij_loc, _block_size * _n_channels));
2459 for (unsigned int iz = first_node; iz <= last_node; ++iz)
2460 {
2461 const auto iz_ind = iz - first_node;
2462 for (unsigned int i_ch = 0; i_ch < _n_channels; ++i_ch)
2463 {
2464 PetscScalar sumWij = 0.0;
2465 unsigned int counter = 0;
2466 for (auto i_gap : _subchannel_mesh.getChannelGaps(i_ch))
2467 {
2468 PetscInt row_vec = i_gap + _n_gaps * iz_ind;
2469 PetscScalar loc_Wij_value;
2470 LibmeshPetscCall(VecGetValues(sol_holder_P, 1, &row_vec, &loc_Wij_value));
2471 sumWij += _subchannel_mesh.getCrossflowSign(i_ch, counter) * loc_Wij_value;
2472 counter++;
2473 }
2474 PetscInt row_vec = i_ch + _n_channels * iz_ind;
2475 LibmeshPetscCall(VecSetValues(sumWij_loc, 1, &row_vec, &sumWij, INSERT_VALUES));
2476 }
2477 }
2478 LibmeshPetscCall(VecAssemblyBegin(sumWij_loc));
2479 LibmeshPetscCall(VecAssemblyEnd(sumWij_loc));
2480
2481 // ---- robust scale measurements ----
2482 PetscScalar min_mdot, sum_mdot, avg_mdot;
2483 PetscInt n;
2484 LibmeshPetscCall(VecAbs(_prod));
2485 LibmeshPetscCall(VecMin(_prod, NULL, &min_mdot));
2486 LibmeshPetscCall(VecSum(_prod, &sum_mdot)); // Sum all entries
2487 LibmeshPetscCall(VecGetSize(_prod, &n)); // Number of entries
2488 avg_mdot = sum_mdot / static_cast<PetscScalar>(n);
2489
2490 V("Average estimated mdot: " + std::to_string(avg_mdot));
2491 V("Minimum estimated mdot: " + std::to_string(min_mdot));
2492
2493 LibmeshPetscCall(VecAbs(sumWij_loc));
2494 LibmeshPetscCall(VecMax(sumWij_loc, NULL, &_max_sumWij));
2495 _max_sumWij = std::max(1e-10, _max_sumWij);
2496 V("Maximum estimated Wij: " + std::to_string(_max_sumWij));
2497
2499 _Wij_loc_vec, _Wij, first_node, last_node, _n_gaps));
2500 LibmeshPetscCall(VecAbs(_Wij_loc_vec));
2502 _Wij_old_loc_vec, _Wij_old, first_node, last_node, _n_gaps));
2503 LibmeshPetscCall(VecAbs(_Wij_old_loc_vec));
2504 LibmeshPetscCall(VecAXPY(_Wij_loc_vec, -1.0, _Wij_old_loc_vec));
2505
2506 PetscScalar relax_factor;
2507 LibmeshPetscCall(VecAbs(_Wij_loc_vec));
2508#if !PETSC_VERSION_LESS_THAN(3, 16, 0)
2509 LibmeshPetscCall(VecMean(_Wij_loc_vec, &relax_factor));
2510#else
2511 VecSum(_Wij_loc_vec, &relax_factor);
2512 relax_factor /= _block_size * _n_gaps;
2513#endif
2514 relax_factor = relax_factor / _max_sumWij + 0.5;
2515 V("Relax base value: " + std::to_string(relax_factor));
2516
2517 // ---- crossflow resistance inflation ----
2518 const PetscScalar resistance_relaxation = 0.9;
2519 _added_K = _max_sumWij / min_mdot;
2520 V("New cross resistance: " + std::to_string(_added_K));
2521 _added_K = (_added_K * resistance_relaxation + (1.0 - resistance_relaxation) * _added_K_old) *
2522 relax_factor;
2523 V("Relaxed cross resistance: " + std::to_string(_added_K));
2524
2525 // Snap-up lower-bounding
2526 if (_added_K < 10 && _added_K >= 1.0)
2527 _added_K = 1.0;
2528 if (_added_K < 1.0 && _added_K >= 0.1)
2529 _added_K = 0.5;
2530 if (_added_K < 0.1 && _added_K >= 0.01)
2531 _added_K = 1. / 3.;
2532 if (_added_K < 1e-2 && _added_K >= 1e-3)
2533 _added_K = 0.1;
2534 V("Actual added cross resistance: " + std::to_string(_added_K));
2535 LibmeshPetscCall(VecScale(unity_vec_Wij, _added_K));
2537
2538 LibmeshPetscCall(MatDiagonalSet(mat_array[2 * Q + 2], unity_vec_Wij, ADD_VALUES));
2539
2540 // ---- cleanup temp vectors used above ----
2541 LibmeshPetscCall(VecDestroy(&mdot_estimate));
2542 LibmeshPetscCall(VecDestroy(&pmat_diag));
2543 LibmeshPetscCall(VecDestroy(&unity_vec));
2544 LibmeshPetscCall(VecDestroy(&p_estimate));
2545 LibmeshPetscCall(VecDestroy(&sol_holder_P));
2546 LibmeshPetscCall(VecDestroy(&unity_vec_Wij));
2547 LibmeshPetscCall(VecDestroy(&sumWij_loc));
2548 LibmeshPetscCall(VecDestroy(&_Wij_loc_vec));
2549 LibmeshPetscCall(VecDestroy(&_Wij_old_loc_vec));
2550 }
2551
2552 V("Relax mdot: " + std::to_string(_mass_flow_equation_relaxation));
2553 V("Relax P: " + std::to_string(_pressure_equation_relaxation));
2554 V("Relax Wij: " + std::to_string(_crossflow_equation_relaxation));
2555
2556 relaxEquation(mat_array[Idx(0, 0)],
2557 vec_array[0],
2558 _prod,
2560 [&](Vec work)
2561 {
2562 return populateVectorFromHandle<SolutionHandle>(
2563 work, *_mdot_soln, first_node, last_node, _n_channels);
2564 });
2565 V("mdot relaxed");
2566
2567 relaxEquation(mat_array[Idx(1, 1)],
2568 vec_array[1],
2569 _prodp,
2571 [&](Vec work)
2572 {
2573 // The pressure block represents the node below each axial momentum cell because
2574 // the outlet pressure is prescribed.
2575 return populateVectorFromHandle<SolutionHandle>(
2576 work, *_P_soln, first_node - 1, last_node - 1, _n_channels);
2577 });
2578 V("P relaxed");
2579
2580 relaxEquation(mat_array[Idx(2, 2)],
2581 vec_array[2],
2582 _Wij_vec,
2584 [&](Vec work)
2585 {
2586 return populateVectorFromDense<libMesh::DenseMatrix<Real>>(
2587 work, _Wij, first_node, last_node, _n_gaps);
2588 });
2589 V("Wij relaxed");
2590 V("Linear solver relaxed");
2591
2592 // ======================== Create and configure KSP =========================
2593 Mat A_nest;
2594 Vec b_nest;
2595 Vec x_nest;
2596 LibmeshPetscCall(MatCreateNest(PETSC_COMM_SELF, Q, NULL, Q, NULL, mat_array.data(), &A_nest));
2597 LibmeshPetscCall(VecCreateNest(PETSC_COMM_SELF, Q, NULL, vec_array.data(), &b_nest));
2598 V("Nested system created");
2599
2600 KSP ksp;
2601 PC pc;
2602 LibmeshPetscCall(KSPCreate(PETSC_COMM_SELF, &ksp));
2603 LibmeshPetscCall(KSPSetOptionsPrefix(ksp, "scm_coupled_"));
2604 LibmeshPetscCall(KSPSetType(ksp, KSPFGMRES));
2605 LibmeshPetscCall(KSPSetOperators(ksp, A_nest, A_nest));
2606 LibmeshPetscCall(KSPGetPC(ksp, &pc));
2607 LibmeshPetscCall(PCSetType(pc, PCFIELDSPLIT));
2608 LibmeshPetscCall(KSPSetTolerances(ksp, _rtol, _atol, _dtol, _maxit));
2609
2610 // split equations
2611 std::vector<IS> rows(Q);
2612 LibmeshPetscCall(MatNestGetISs(A_nest, rows.data(), NULL));
2613 for (PetscInt j = 0; j < Q; ++j)
2614 {
2615 IS part;
2616 LibmeshPetscCall(ISDuplicate(rows[j], &part));
2617 LibmeshPetscCall(PCFieldSplitSetIS(pc, NULL, part));
2618 LibmeshPetscCall(ISDestroy(&part));
2619 }
2620 LibmeshPetscCall(KSPSetFromOptions(ksp));
2621 V("Linear solver assembled");
2622
2623 // ============================== Solve =====================================
2624 LibmeshPetscCall(VecDuplicate(b_nest, &x_nest));
2625 LibmeshPetscCall(VecSet(x_nest, 0.0));
2626 LibmeshPetscCall(KSPSolve(ksp, b_nest, x_nest));
2627 KSPConvergedReason reason;
2628 LibmeshPetscCall(KSPGetConvergedReason(ksp, &reason));
2629 if (reason < 0)
2630 {
2631 PetscInt iterations;
2632 PetscReal residual_norm;
2633 LibmeshPetscCall(KSPGetIterationNumber(ksp, &iterations));
2634 LibmeshPetscCall(KSPGetResidualNorm(ksp, &residual_norm));
2635 mooseError(name(),
2636 ": coupled mass/momentum linear solve failed: ",
2637 KSPConvergedReasons[reason],
2638 " (",
2639 static_cast<int>(reason),
2640 ") after ",
2641 iterations,
2642 " iterations; residual norm = ",
2643 residual_norm,
2644 ".");
2645 }
2646
2647 // destroy solver containers first
2648 LibmeshPetscCall(VecDestroy(&b_nest));
2649 LibmeshPetscCall(MatDestroy(&A_nest));
2650 LibmeshPetscCall(KSPDestroy(&ksp));
2651 for (PetscInt i = 0; i < Q * Q; i++)
2652 LibmeshPetscCall(MatDestroy(&mat_array[i]));
2653 for (PetscInt i = 0; i < Q; i++)
2654 LibmeshPetscCall(VecDestroy(&vec_array[i]));
2655 V("Solver elements destroyed");
2656
2657 // ====================== Extract & scatter the solution =====================
2658 Vec sol_mdot, sol_p, sol_Wij;
2659 V("Vectors to hold solution created");
2660 PetscInt num_vecs;
2661 Vec * loc_vecs;
2662 LibmeshPetscCall(VecNestGetSubVecs(x_nest, &num_vecs, &loc_vecs));
2663 LibmeshPetscCall(VecDuplicate(_mc_axial_convection_rhs, &sol_mdot));
2664 LibmeshPetscCall(VecCopy(loc_vecs[0], sol_mdot));
2665 LibmeshPetscCall(VecDuplicate(_amc_sys_mdot_rhs, &sol_p));
2666 LibmeshPetscCall(VecCopy(loc_vecs[1], sol_p));
2667 LibmeshPetscCall(VecDuplicate(_cmc_sys_Wij_rhs, &sol_Wij));
2668 LibmeshPetscCall(VecCopy(loc_vecs[2], sol_Wij));
2669 V("Solution from coupled solver copied to solution vectors");
2670
2671 // Apply independent post-solve relaxation to the fixed-point solution update.
2672 auto relaxSolution = [&](Vec solution, Vec old_solution, const Real relaxation)
2673 {
2674 LibmeshPetscCall(VecScale(solution, relaxation));
2675 LibmeshPetscCall(VecAXPY(solution, 1.0 - relaxation, old_solution));
2676 };
2677 LibmeshPetscCall(populateVectorFromHandle<SolutionHandle>(
2678 _prod, *_mdot_soln, first_node, last_node, _n_channels));
2679 LibmeshPetscCall(populateVectorFromHandle<SolutionHandle>(
2680 _prodp, *_P_soln, first_node - 1, last_node - 1, _n_channels));
2681
2682 // Measure the pressure update before post-solve relaxation so changing that relaxation does
2683 // not change the meaning of the outer pressure tolerance.
2684 Vec pressure_residual;
2685 LibmeshPetscCall(VecDuplicate(sol_p, &pressure_residual));
2686 LibmeshPetscCall(VecCopy(sol_p, pressure_residual));
2687 LibmeshPetscCall(VecAXPY(pressure_residual, -1.0, _prodp));
2688
2689 const PetscScalar * residual_array;
2690 const PetscScalar * old_pressure_array;
2691 PetscInt pressure_size;
2692 LibmeshPetscCall(VecGetSize(pressure_residual, &pressure_size));
2693 LibmeshPetscCall(VecGetArrayRead(pressure_residual, &residual_array));
2694 LibmeshPetscCall(VecGetArrayRead(_prodp, &old_pressure_array));
2695 Real residual_norm_sq = 0.0;
2696 Real pressure_norm_sq = 0.0;
2697 for (PetscInt i = 0; i < pressure_size; ++i)
2698 {
2699 residual_norm_sq += Utility::pow<2>(residual_array[i]);
2700 pressure_norm_sq += Utility::pow<2>(old_pressure_array[i] + _P_out);
2701 }
2702 LibmeshPetscCall(VecRestoreArrayRead(pressure_residual, &residual_array));
2703 LibmeshPetscCall(VecRestoreArrayRead(_prodp, &old_pressure_array));
2704 LibmeshPetscCall(VecDestroy(&pressure_residual));
2707 std::sqrt(residual_norm_sq) / (std::sqrt(pressure_norm_sq) + 1e-14));
2708
2709 relaxSolution(sol_mdot, _prod, _mass_flow_relaxation);
2710 relaxSolution(sol_p, _prodp, _pressure_relaxation);
2712 _Wij_vec, _Wij, first_node, last_node, _n_gaps));
2713 relaxSolution(sol_Wij, _Wij_vec, _crossflow_relaxation);
2714
2715 // mass flow
2716 LibmeshPetscCall(populateSolutionChan<SolutionHandle>(
2717 sol_mdot, *_mdot_soln, first_node, last_node, _n_channels));
2718
2719 // pressure
2720 {
2721 PetscScalar * sol_p_array;
2722 LibmeshPetscCall(VecGetArray(sol_p, &sol_p_array));
2723 for (unsigned int iz = last_node; iz > first_node - 1; iz--)
2724 {
2725 const auto iz_ind = iz - first_node;
2726 for (unsigned int i_ch = 0; i_ch < _n_channels; i_ch++)
2727 {
2728 auto * node_in = _subchannel_mesh.getChannelNode(i_ch, iz - 1);
2729 PetscScalar value = sol_p_array[iz_ind * _n_channels + i_ch];
2730 _P_soln->set(node_in, value);
2731 }
2732 }
2733 LibmeshPetscCall(VecRestoreArray(sol_p, &sol_p_array));
2734 }
2735
2736 // crossflow dense + sumWij + correction factor
2738 sol_Wij, _Wij, first_node, last_node, _n_gaps));
2739
2740 LibmeshPetscCall(MatMult(_mc_sumWij_mat, sol_Wij, _prod));
2741 LibmeshPetscCall(populateSolutionChan<SolutionHandle>(
2742 _prod, *_SumWij_soln, first_node, last_node, _n_channels));
2743
2744 LibmeshPetscCall(VecAbs(_prod));
2745 LibmeshPetscCall(VecMax(_prod, NULL, &_max_sumWij_new));
2746 V("Maximum estimated Wij new: " + std::to_string(_max_sumWij_new));
2748 V("Correction factor: " + std::to_string(_correction_factor));
2749 V("Solutions assigned to MOOSE variables.");
2750
2751 // cleanup solution objects
2752 LibmeshPetscCall(VecDestroy(&x_nest));
2753 LibmeshPetscCall(VecDestroy(&sol_mdot));
2754 LibmeshPetscCall(VecDestroy(&sol_p));
2755 LibmeshPetscCall(VecDestroy(&sol_Wij));
2756 V("Solutions destroyed.");
2757
2758 PetscFunctionReturn(LIBMESH_PETSC_SUCCESS);
2759}
2760
2761void
2763{
2764 _console << "Executing subchannel solver\n";
2765 _dt = (isTransient() ? dt() : _one);
2766 _TR = isTransient();
2767
2768 // The subchannel solver hardcodes a first-order backward (implicit) Euler time discretization, so
2769 // any other time integrator a user selects is silently ignored. Warn once if one is requested.
2771 {
2773 if (isTransient())
2774 if (auto * transient = dynamic_cast<TransientBase *>(_app.getExecutioner()))
2775 for (const auto * ti : transient->getTimeIntegrators())
2776 if (!dynamic_cast<const ImplicitEuler *>(ti))
2777 mooseWarning("The subchannel solver always uses implicit (backward) Euler time "
2778 "integration; the requested '",
2779 ti->type(),
2780 "' time integrator is ignored.");
2781 }
2782
2785 // Small helper functions to reduce repetition
2786 // Verbose print helper (no-op unless _verbose_subchannel is true)
2787 auto V = [&](const std::string & s)
2788 {
2790 _console << s << std::endl;
2791 };
2792 auto saveValues = [&](const SolutionHandle & solution,
2793 const unsigned int first_node,
2794 const unsigned int last_node)
2795 {
2796 std::vector<Real> values;
2797 values.reserve((last_node - first_node + 1) * _n_channels);
2798 for (unsigned int iz = first_node; iz <= last_node; ++iz)
2799 for (unsigned int i_ch = 0; i_ch < _n_channels; ++i_ch)
2800 values.push_back(solution(_subchannel_mesh.getChannelNode(i_ch, iz)));
2801 return values;
2802 };
2803 auto relativeChange = [&](const SolutionHandle & solution,
2804 const std::vector<Real> & old_values,
2805 const unsigned int first_node,
2806 const unsigned int last_node,
2807 const Real reference_offset)
2808 {
2809 Real difference_norm_sq = 0.0;
2810 Real reference_norm_sq = 0.0;
2811 std::size_t i = 0;
2812 for (unsigned int iz = first_node; iz <= last_node; ++iz)
2813 for (unsigned int i_ch = 0; i_ch < _n_channels; ++i_ch)
2814 {
2815 const Real value = solution(_subchannel_mesh.getChannelNode(i_ch, iz));
2816 difference_norm_sq += Utility::pow<2>(value - old_values[i]);
2817 reference_norm_sq += Utility::pow<2>(old_values[i] + reference_offset);
2818 ++i;
2819 }
2820 return std::sqrt(difference_norm_sq) / (std::sqrt(reference_norm_sq) + 1e-14);
2821 };
2822 V("Solution initialized");
2823 Real P_error = 1.0;
2824 unsigned int P_it = 0;
2825 unsigned int P_it_max;
2826 bool temperature_converged = true;
2827
2828 if (_segregated_bool)
2829 P_it_max = 20 * _n_blocks;
2830 else
2831 P_it_max = 100;
2832
2833 if ((_n_blocks == 1) && (_segregated_bool))
2834 P_it_max = 5;
2835
2836 if (_P_maxit > 0)
2837 P_it_max = _P_maxit;
2838
2839 while ((P_error > _P_tol && P_it < P_it_max))
2840 {
2841 P_it += 1;
2842 temperature_converged = true;
2844 _console << "Solving Outer Iteration : " << P_it << std::endl;
2845 const auto P_old = saveValues(*_P_soln, 0, _n_cells);
2846 for (unsigned int iblock = 0; iblock < _n_blocks; iblock++)
2847 {
2848 int last_level = (iblock + 1) * _block_size;
2849 int first_level = iblock * _block_size + 1;
2850 Real T_block_error = 1.0;
2851 auto T_it = 0;
2852 _console << "Solving Block: " << iblock << " From first level: " << first_level
2853 << " to last level: " << last_level << std::endl;
2854
2855 while (T_block_error > _T_tol && T_it < _T_maxit)
2856 {
2857 if (processor_id() == 0)
2858 {
2859 if (_segregated_bool)
2860 computeWijFromSolve(iblock);
2861 else
2862 {
2863 LibmeshPetscCall(implicitPetscSolve(iblock));
2864 computeWijPrime(iblock);
2865 V("Done with main solve.");
2866 }
2867 }
2868
2869 // A value of one preserves the original ordering by refreshing the flow solution before
2870 // every thermal update. Larger values opt into lagging flow during thermal subcycles.
2871 for (const auto enthalpy_subcycle : make_range(_enthalpy_subcycles))
2872 {
2873 if (T_block_error <= _T_tol || T_it >= _T_maxit)
2874 break;
2875
2876 V("Enthalpy subcycle: " + std::to_string(enthalpy_subcycle + 1));
2877 T_it += 1;
2878 const auto T_old = saveValues(*_T_soln, first_level, last_level);
2879 // We are only computing quantities on rank 0
2880 if (processor_id() > 0)
2881 {
2882 // Rank zero supplies the error through the maximum reduction below.
2883 T_block_error = 0.0;
2884 goto aux_close;
2885 }
2886
2887 if (_compute_power)
2888 {
2889 computeh(iblock);
2890 T_block_error = computeT(iblock);
2891 V("Done with thermal solve.");
2892 }
2893
2894 V("Start updating thermophysical properties.");
2895 if (_compute_density)
2896 computeRho(iblock);
2898 computeMu(iblock);
2899 V("Done updating thermophysical properties.");
2900
2901 // We must do a global assembly to make sure data is parallel consistent before we do
2902 // things like compute L2 norms
2903 aux_close:
2904 _aux->solution().close();
2905
2906 if (!_compute_power)
2907 T_block_error =
2908 relativeChange(*_T_soln, T_old, first_level, last_level, /*reference_offset=*/0.0);
2909 _console << "T_block_error: " << T_block_error << std::endl;
2910
2911 // All processes must have the same iteration count
2912 comm().max(T_block_error);
2913 }
2914 }
2915 const bool block_converged = T_block_error <= _T_tol;
2916 temperature_converged &= block_converged;
2917 if (!block_converged)
2918 {
2919 _console << "Reached maximum number of temperature iterations for block: " << iblock
2920 << std::endl;
2921 }
2922 }
2923 P_error = _segregated_bool ? relativeChange(*_P_soln, P_old, 0, _n_cells, _P_out)
2925 comm().max(P_error);
2926 _console << "P_error :" << P_error << std::endl;
2927 V("Iteration: " + std::to_string(P_it));
2928 V("Maximum iterations: " + std::to_string(P_it_max));
2929 }
2930 // Cache only the final iteration status. Earlier outer iterations may fail their thermal
2931 // tolerance and subsequently recover.
2932 const bool pressure_converged = P_error <= _P_tol;
2933 if (!pressure_converged)
2934 {
2935 _console << "Reached maximum number of axial pressure iterations" << std::endl;
2936 }
2937 _converged = pressure_converged && temperature_converged;
2938 // update old crossflow matrix
2939 _Wij_old = _Wij;
2940 _console << "Finished executing subchannel solver\n";
2941
2942 // set SumWij at the inlet equal to the one on the first axial level (for visualization purposes)
2943 for (unsigned int i_ch = 0; i_ch < _n_channels; i_ch++)
2944 {
2945 auto * node_in = _subchannel_mesh.getChannelNode(i_ch, 0);
2946 auto * node_out = _subchannel_mesh.getChannelNode(i_ch, 1);
2947 _SumWij_soln->set(node_in, (*_SumWij_soln)(node_out)); // kg/sec
2948 }
2949
2950 if (_pin_mesh_exist)
2951 {
2952 // Assign average HTC to subchannels. This is exact if all pins have the same diameter
2953 for (unsigned int iz = 0; iz < _n_cells + 1; ++iz)
2954 {
2955 for (unsigned int i_ch = 0; i_ch < _n_channels; i_ch++)
2956 {
2957 const auto * node = _subchannel_mesh.getChannelNode(i_ch, iz);
2958 auto mu = (*_mu_soln)(node);
2959 auto S = (*_S_flow_soln)(node);
2960 auto w_perim = (*_w_perim_soln)(node);
2961 auto Dh_i = 4.0 * S / w_perim;
2962 auto Re = (((*_mdot_soln)(node) / S) * Dh_i / mu);
2963 auto k = _fp->k_from_p_T((*_P_soln)(node) + _P_out, (*_T_soln)(node));
2964 auto cp = _fp->cp_from_p_T((*_P_soln)(node) + _P_out, (*_T_soln)(node));
2965 auto Pr = (*_mu_soln)(node)*cp / k;
2966 // Create Friction structure
2967 _friction_args = FrictionStruct(i_ch, Re, S, w_perim);
2968
2969 Real sumhw = 0.0;
2970 for (auto i_pin : _subchannel_mesh.getChannelPins(i_ch))
2971 {
2972 // Create nusselt number structure
2973 _nusselt_args = NusseltStruct(Re, Pr, i_pin, iz, i_ch);
2974
2975 // Compute HTC
2977 }
2978
2979 // Set HTC
2980 _HTC_soln->set(node, sumhw / _subchannel_mesh.getChannelPins(i_ch).size());
2981 }
2982 }
2983 _HTC_soln->close();
2984
2985 _console << "Commencing calculation of Pin surface temperature \n";
2986 for (unsigned int i_pin = 0; i_pin < _n_pins; i_pin++)
2987 {
2988 for (unsigned int iz = 0; iz < _n_cells + 1; ++iz)
2989 {
2990 const auto * pin_node = _subchannel_mesh.getPinNode(i_pin, iz);
2991 Real sumTemp = 0.0;
2992 // Calculate sum of pin surface temperatures that the channels around the pin see
2993 for (auto i_ch : _subchannel_mesh.getPinChannels(i_pin))
2994 {
2995 const auto * node = _subchannel_mesh.getChannelNode(i_ch, iz);
2996 auto mu = (*_mu_soln)(node);
2997 auto S = (*_S_flow_soln)(node);
2998 auto w_perim = (*_w_perim_soln)(node);
2999 auto Dh_i = 4.0 * S / w_perim;
3000 auto Re = (((*_mdot_soln)(node) / S) * Dh_i / mu);
3001 auto k = _fp->k_from_p_T((*_P_soln)(node) + _P_out, (*_T_soln)(node));
3002 auto cp = _fp->cp_from_p_T((*_P_soln)(node) + _P_out, (*_T_soln)(node));
3003 auto Pr = (*_mu_soln)(node)*cp / k;
3004 // Create Friction structure
3005 _friction_args = FrictionStruct(i_ch, Re, S, w_perim);
3006 // Create nusselt number structure
3007 _nusselt_args = NusseltStruct(Re, Pr, i_pin, iz, i_ch);
3008 // Compute HTC
3010 // Compute surface temperature contribution from subchannel side
3011 sumTemp +=
3012 (*_q_prime_soln)(pin_node) / ((*_Dpin_soln)(pin_node)*M_PI * hw) + (*_T_soln)(node);
3013 }
3014 if (_subchannel_mesh.getPinChannels(i_pin).size() > 0)
3015 _Tpin_soln->set(pin_node, sumTemp / _subchannel_mesh.getPinChannels(i_pin).size());
3016 else
3017 mooseError("Pin was not found for pin index: " + std::to_string(i_pin));
3018 }
3019 }
3020 }
3021
3023 if (_duct_mesh_exist && processor_id() == 0)
3024 {
3025 _console << "Commencing calculation of duct surface temperature " << std::endl;
3026 auto duct_nodes = _subchannel_mesh.getDuctNodes();
3027 for (Node * dn : duct_nodes)
3028 {
3029 auto * node_chan = _subchannel_mesh.getChannelNodeFromDuct(dn);
3030 auto mu = (*_mu_soln)(node_chan);
3031 auto S = (*_S_flow_soln)(node_chan);
3032 auto w_perim = (*_w_perim_soln)(node_chan);
3033 auto Dh_i = 4.0 * S / w_perim;
3034 auto Re = (((*_mdot_soln)(node_chan) / S) * Dh_i / mu);
3035 auto k = _fp->k_from_p_T((*_P_soln)(node_chan) + _P_out, (*_T_soln)(node_chan));
3036 auto cp = _fp->cp_from_p_T((*_P_soln)(node_chan) + _P_out, (*_T_soln)(node_chan));
3037 auto Pr = (*_mu_soln)(node_chan)*cp / k;
3038
3039 // Create nusselt number structure (consistent with pin case)
3040 const libMesh::Point & node_point = *_subchannel_mesh.getChannelNodeFromDuct(dn);
3041 const unsigned int iz = _subchannel_mesh.getZIndex(node_point);
3042 const unsigned int i_ch = _subchannel_mesh.channelIndex(node_point);
3043
3044 // Create nusselt number structure
3045 _nusselt_args = NusseltStruct(Re, Pr, std::numeric_limits<unsigned int>::max(), iz, i_ch);
3046
3047 // Create Friction structure
3048 _friction_args = FrictionStruct(i_ch, Re, S, w_perim);
3049
3050 // Compute HTC
3052
3053 // Compute Channel Temperature
3054 auto T_chan = (*_duct_heat_flux_soln)(dn) / hw + (*_T_soln)(node_chan);
3055 _Tduct_soln->set(dn, T_chan);
3056 }
3057 }
3058 _aux->solution().close();
3059 _aux->update();
3060
3061 // Called before the rank-0 early return since it broadcasts to all ranks
3063
3064 if (processor_id() != 0)
3065 return;
3066 Real power_in = 0.0;
3067 Real power_out = 0.0;
3068 Real mass_flow_in = 0.0;
3069 Real mass_flow_out = 0.0;
3070 for (unsigned int i_ch = 0; i_ch < _n_channels; i_ch++)
3071 {
3072 auto * node_in = _subchannel_mesh.getChannelNode(i_ch, 0);
3073 auto * node_out = _subchannel_mesh.getChannelNode(i_ch, _n_cells);
3074 const Real mdot_in = (*_mdot_soln)(node_in);
3075 power_in += mdot_in * (*_h_soln)(node_in);
3076 power_out += (*_mdot_soln)(node_out) * (*_h_soln)(node_out);
3077 mass_flow_in += mdot_in;
3078 mass_flow_out += (*_mdot_soln)(node_out);
3079 }
3080 auto h_bulk_out = power_out / mass_flow_out;
3081 auto T_bulk_out = _fp->T_from_p_h(_P_out, h_bulk_out);
3082
3083 const Real bulk_Dh = _subchannel_mesh.getAssemblyHydraulicDiameter();
3085 {
3086 _console << " ======================================= " << std::endl;
3087 _console << " ======== Subchannel Print Outs ======== " << std::endl;
3088 _console << " ======================================= " << std::endl;
3089 _console << "Total flow area :" << _subchannel_mesh.getAssemblyFlowArea() << " m^2"
3090 << std::endl;
3091 _console << "Assembly hydraulic diameter :" << bulk_Dh << " m" << std::endl;
3092 _console << "Assembly Re number :" << _bulk_Re << " [-]" << std::endl;
3093 _console << "Bulk coolant temperature at outlet :" << T_bulk_out << " K" << std::endl;
3094 _console << "Power added to coolant is : " << power_out - power_in << " Watt" << std::endl;
3095 _console << "Mass flow rate in is : " << mass_flow_in << " kg/sec" << std::endl;
3096 _console << "Mass balance is : " << mass_flow_out - mass_flow_in << " kg/sec" << std::endl;
3097 _console << "User defined outlet pressure is : " << _P_out << " Pa" << std::endl;
3098 _console << " ======================================= " << std::endl;
3099 }
3100
3101 if (MooseUtils::absoluteFuzzyLessEqual((power_out - power_in), -1.0))
3103 "Energy conservation equation might not be solved correctly, Power added to coolant: " +
3104 std::to_string(power_out - power_in) + " Watt ");
3105}
3106
3107void
Real f(Real x)
Test function for Brents method.
const double tol
const std::vector< double > x
const double mu
const double Re
const double rho
const double T
std::array< Real, 2 > values
PetscFunctionBegin
PetscErrorCode formFunction(SNES, Vec x, Vec f, void *ctx)
void ErrorVector unsigned int
const ConsoleStream _console
static InputParameters validParams()
std::shared_ptr< AuxiliarySystem > _aux
virtual Real & dt() const
virtual const MooseVariableFieldBase & getVariable(const THREAD_ID tid, const std::string &var_name, Moose::VarKindType expected_var_type=Moose::VarKindType::VAR_ANY, Moose::VarFieldType expected_var_field_type=Moose::VarFieldType::VAR_FIELD_ANY) const override
virtual void transient(bool trans)
virtual bool isTransient() const override
void initialSetup() override
void addParamNamesToGroup(const std::string &space_delim_names, const std::string group_name)
bool isParamSetByUser(const std::string &name) const
void addRequiredParam(const std::string &name, const std::string &doc_string)
void addDeprecatedParam(const std::string &name, const T &value, const std::string &doc_string, const std::string &deprecation_message)
void addParam(const std::string &name, const std::initializer_list< typename T::value_type > &value, const std::string &doc_string)
void addClassDescription(const std::string &doc_string)
void addRangeCheckedParam(const std::string &name, const T &value, const std::string &parsed_function, const std::string &doc_string)
bool isRestarting() const
Executioner * getExecutioner() const
bool isRecovering() const
const std::string & name() const
void paramError(const std::string &param, Args... args) const
void mooseError(Args &&... args) const
void mooseWarning(Args &&... args) const
bool isParamValid(const std::string &name) const
MooseApp & _app
static InputParameters validParams()
virtual Real computeFrictionFactor(const FrictionStruct &friction_info) const =0
Computes the friction factor for the local conditions.
Real computeHTC(const FrictionStruct &friction_info, const NusseltStruct &nusselt_info, const Real conduction_k) const
Computes the convective heat transfer coefficient for the local conditions.
virtual void computeBulkMixingParameters() const
Precomputes the mixing coefficients that depend only on the bulk flow.
virtual Real computeSweepFlowMixingParameter(const unsigned int i_gap, const unsigned int iz) const
Computes the wire-wrap sweep-flow coefficient for peripheral gaps.
virtual void computeBlockMixingParameters(const unsigned int, const unsigned int) const
Precomputes the mixing coefficients that depend on the flow solution of the axial levels first_node t...
virtual Real getCT() const
Return the Turbulent modeling parameter.
virtual Real computeMixingParameter(const unsigned int i_gap, const unsigned int iz) const =0
Computes the turbulent mixing coefficient for the local conditions around gap(i_gap) and axial level(...
Provide a simple RAII interface for linear lagrange solution variables.
Base class for the 1-phase steady-state/transient subchannel solver.
libMesh::DenseMatrix< Real > _DP
void computeP(int iblock)
Computes Pressure per channel for block iblock.
const Real & _mass_flow_equation_relaxation
Equation relaxation factor for mass flow rate in the coupled implicit solve.
const Real & _pressure_equation_relaxation
Equation relaxation factor for pressure in the coupled implicit solve.
PetscErrorCode createPetscVector(Vec &v, PetscInt n)
Petsc Functions.
virtual void syncSolutions(Direction direction) override
const bool _segregated_bool
Segregated solve.
PetscScalar computeInterpolatedValue(PetscScalar topValue, PetscScalar botValue, PetscScalar Peclet=0.0)
const SCMMixingClosureBase * _mixing_closure
Turbulent Mixing closure object.
Real computeSweepFlowMixingParameter(unsigned int i_gap, unsigned int iz) const
Computes and validates the sweep-flow mixing parameter.
const SCMHTCClosureBase * _pin_HTC_closure
HTC closure objects.
const SCMHTCClosureBase * _duct_HTC_closure
SubChannel1PhaseProblem(const InputParameters &params)
Mat _cmc_friction_force_mat
Cross momentum conservation - friction force.
std::unique_ptr< SolutionHandle > _w_perim_soln
PetscErrorCode petscSnesSolver(int iblock, const libMesh::DenseVector< Real > &solution, libMesh::DenseVector< Real > &root)
Computes solution of nonlinear equation using snes and provided a residual in a formFunction.
const PetscReal & _dtol
The divergence tolerance for the ksp linear solver.
PetscErrorCode createPetscMatrix(Mat &M, PetscInt n, PetscInt m)
virtual void initializeSolution()=0
Function to initialize the solution & geometry fields.
std::unique_ptr< SolutionHandle > _displacement_soln
Mat _hc_time_derivative_mat
Enthalpy Enthalpy conservation - time derivative.
Mat _cmc_time_derivative_mat
Cross momentum Cross momentum conservation - time derivative.
const SCMFrictionClosureBase * _friction_closure
Friction closure object.
Mat _amc_turbulent_cross_flows_mat
Axial momentum Axial momentum conservation - compute turbulent cross fluxes.
static InputParameters validParams()
PetscErrorCode solveAndPopulateEnthalpy(Mat A, Vec rhs, unsigned int first_node, unsigned int last_node, const char *ksp_prefix)
Solve a linear system (A * x = rhs) with a simple PCJACOBI KSP and populate the enthalpy solution int...
const SinglePhaseFluidProperties * _fp
Non-owning pointer to fluid properties user object.
Mat _amc_sys_mdot_mat
Axial momentum system matrix.
Mat _cmc_sys_Wij_mat
Lateral momentum system matrix.
libMesh::DenseMatrix< Real > _WijPrime
void computeRho(int iblock)
Computes Density per channel for block iblock.
virtual Real getSubChannelPeripheralDuctWidth(unsigned int i_ch) const =0
Function that computes the width of the duct cell that the peripheral subchannel i_ch sees.
std::unique_ptr< SolutionHandle > _DP_soln
const MooseEnum _interpolation_scheme
The interpolation method used in constructing the systems.
const bool _staggered_pressure_bool
Flag to define the usage of staggered or collocated pressure.
std::unique_ptr< SolutionHandle > _rho_soln
Mat _mc_sumWij_mat
Matrices and vectors to be used in implicit assembly Mass conservation Mass conservation - sum of cro...
std::unique_ptr< SolutionHandle > _duct_heat_flux_soln
struct SubChannel1PhaseProblem::FrictionStruct _friction_args
void computeDP(int iblock)
Computes Pressure Drop per channel for block iblock.
const Real & _T_tol
Convergence tolerance for the temperature loop in internal solve.
const bool _duct_mesh_exist
Flag that informs if there is a duct mesh or not.
std::unique_ptr< SolutionHandle > _S_flow_soln
void computeWijResidual(int iblock)
Computes Residual Matrix based on the lateral momentum conservation equation for block iblock.
const bool _compute_power
Flag that informs if we need to solve the Enthalpy/Temperature equations or not.
const PostprocessorValue & _P_out
Outlet pressure postprocessor value.
virtual void initialSetup() override
std::unique_ptr< SolutionHandle > _Dpin_soln
Mat _cmc_pressure_force_mat
Cross momentum conservation - pressure force.
std::unique_ptr< SolutionHandle > _P_soln
Real _bulk_Re
Assembly bulk Reynolds number.
unsigned int _n_blocks
number of axial blocks
void detectDeformation()
Detects whether pin diameter or duct displacement fields require geometry recalculation.
Mat _amc_time_derivative_mat
Axial momentum conservation - time derivative.
libMesh::DenseMatrix< Real > & _Wij
std::unique_ptr< SolutionHandle > _Tduct_soln
bool _deformation
Flag that activates the effect of deformation (pin/duct) based on the auxvalues for displacement,...
libMesh::DenseMatrix< Real > _Wij_residual_matrix
friend PetscErrorCode formFunction(SNES snes, Vec x, Vec f, void *ctx)
This is the residual Vector function in a form compatible with the SNES PETC solvers.
std::unique_ptr< SolutionHandle > _h_soln
std::unique_ptr< SolutionHandle > _T_soln
virtual void computeh(int iblock)=0
Computes Enthalpy per channel for block iblock.
std::unique_ptr< SolutionHandle > _mu_soln
Real _TR
Flag that activates or deactivates the transient parts of the equations we solve by multiplication.
virtual void externalSolve() override
PetscScalar computeInterpolationCoefficients(PetscScalar Peclet=0.0)
Functions that computes the interpolation scheme given the Peclet number.
Mat _cmc_advective_derivative_mat
Cross momentum conservation - advective (Eulerian) derivative.
bool _time_integrator_checked
Whether the time integrator has been checked for consistency with the implementation.
Vec _hc_added_heat_rhs
Enthalpy conservation - source and sink.
const Real & _P_tol
Convergence tolerance for the pressure loop in external solve.
Mat _amc_friction_force_mat
Axial momentum conservation - friction force.
Real _CT
Turbulent modeling parameter used in axial momentum equation.
PetscErrorCode populateDenseFromVector(const Vec &x, T &solution, const unsigned int first_axial_level, const unsigned int last_axial_level, const unsigned int cross_dimension)
Real _pressure_fixed_point_error
Maximum pressure fixed-point update before solution relaxation over the blocks.
Mat _mc_density_pressure_mat
Mass conservation - pressure derivative of transient density.
const bool _compute_viscosity
Flag that activates or deactivates the calculation of viscosity.
Mat _hc_advective_derivative_mat
Enthalpy conservation - advective (Eulerian) derivative;.
const PetscInt & _maxit
The maximum number of iterations to use for the ksp linear solver.
PetscErrorCode implicitPetscSolve(int iblock)
Computes implicit solve using PetSc.
const bool _compute_density
Flag that activates or deactivates the calculation of density.
libMesh::DenseVector< Real > residualFunction(int iblock, libMesh::DenseVector< Real > solution)
Computes Residual Vector based on the lateral momentum conservation equation for block iblock & updat...
bool _converged
Variable that informs whether we exited external solve with a converged solution or not.
void computeSumWij(int iblock)
Computes net diversion crossflow per channel for block iblock.
const int & _T_maxit
Maximum iterations for the inner temperature loop.
const Real & _T_relaxation
Relaxation factor for temperature updates in the inner thermal-hydraulic iteration.
const Real & _crossflow_relaxation
Relaxation factor for crossflow updates in the coupled implicit solve.
std::unique_ptr< SolutionHandle > _Tpin_soln
const bool _pin_mesh_exist
Flag that informs if there is a pin mesh or not.
Mat _amc_pressure_force_mat
Axial momentum conservation - pressure force.
void computeMdot(int iblock)
Computes mass flow per channel for block iblock.
Real _bulk_V
Assembly bulk velocity.
std::unique_ptr< SolutionHandle > _SumWij_soln
const bool _implicit_bool
Flag to define the usage of a implicit or explicit solution.
const int & _P_maxit
Maximum number of pressure iterations; zero selects the solver's existing automatic limit.
std::vector< Real > _z_grid
axial location of nodes
virtual bool solverSystemConverged(const unsigned int) override
const Real & _mass_flow_relaxation
Relaxation factor for mass flow rate updates in the coupled implicit solve.
Mat _amc_cross_derivative_mat
Axial momentum conservation - cross flux derivative.
void computeWijFromSolve(int iblock)
Computes diversion crossflow per gap for block iblock.
const unsigned int & _enthalpy_subcycles
Number of enthalpy, temperature, and property updates performed per flow solve.
void computeMu(int iblock)
Computes Viscosity per channel for block iblock.
PetscErrorCode populateVectorFromDense(Vec &x, const T &solution, const unsigned int first_axial_level, const unsigned int last_axial_level, const unsigned int cross_dimension)
libMesh::DenseMatrix< Real > & _Wij_old
struct SubChannel1PhaseProblem::NusseltStruct _nusselt_args
std::unique_ptr< SolutionHandle > _mdot_soln
Solutions handles and link to TH tables properties.
std::unique_ptr< SolutionHandle > _q_prime_soln
virtual Real computeAddedHeatDuct(unsigned int i_ch, unsigned int iz) const
Non-pure: implemented in the base (or override in a child if needed)
const PetscReal & _rtol
The relative convergence tolerance, (relative decrease) for the ksp linear solver.
const Real & _crossflow_equation_relaxation
Equation relaxation factor for crossflow in the coupled implicit solve.
Vec _amc_gravity_rhs
Axial momentum conservation - buoyancy force No implicit matrix.
Mat _mc_axial_convection_mat
Mass conservation - axial convection.
PetscScalar _added_K
Added resistances for monolithic convergence.
Mat _amc_advective_derivative_mat
Axial momentum conservation - advective (Eulerian) derivative.
void computeBulkReynoldsNumber()
Computes the assembly bulk Reynolds number from inlet flow conditions.
Real computeT(int iblock)
Computes and relaxes Temperature per channel for block iblock.
void computeWijPrime(int iblock)
Computes turbulent crossflow per gap for block iblock.
Real computeMixingParameter(unsigned int i_gap, unsigned int iz) const
Computes and validates the turbulent mixing parameter.
std::unique_ptr< SolutionHandle > _ff_soln
const bool _verbose_subchannel
Boolean to printout information related to subchannel solve.
Mat _hc_cross_derivative_mat
Enthalpy conservation - cross flux derivative.
const PetscReal & _atol
The absolute convergence tolerance for the ksp linear solver.
const Real & _pressure_relaxation
Relaxation factor for pressure updates in the coupled implicit solve.
std::unique_ptr< SolutionHandle > _HTC_soln
static const std::string ENTHALPY
static const std::string DISPLACEMENT
static const std::string PRESSURE_DROP
static const std::string WETTED_PERIMETER
static const std::string PRESSURE
static const std::string DUCT_TEMPERATURE
static const std::string FRICTION_FACTOR
static const std::string MASS_FLOW_RATE
static const std::string SURFACE_AREA
static const std::string VISCOSITY
static const std::string DENSITY
static const std::string PIN_DIAMETER
static const std::string HEAT_TRANSFER_COEFFICIENT
static const std::string LINEAR_HEAT_RATE
static const std::string DUCT_HEAT_FLUX
static const std::string SUM_CROSSFLOW
static const std::string PIN_TEMPERATURE
static const std::string TEMPERATURE
Base class for subchannel meshes.
virtual unsigned int getNumOfChannels() const =0
Return the number of channels per layer.
virtual const std::vector< unsigned int > & getChannelPins(unsigned int i_chan) const =0
Return a vector of pin indices for a given channel index.
virtual unsigned int channelIndex(const Point &point) const =0
Real getAssemblyFlowArea() const
Return undeformed bundle inlet flow area.
Real getAssemblyHydraulicDiameter() const
Return undeformed bundle-average hydraulic diameter.
virtual const std::vector< Real > & getZGrid() const
Get axial location of layers.
Node * getChannelNodeFromDuct(Node *duct_node) const
Function that gets the channel node from the duct node.
virtual Real getGapWidth(unsigned int axial_index, unsigned int gap_index) const =0
Return gap width for a given gap index.
virtual const Real & getPitch() const
Return the undeformed pitch between 2 subchannels.
virtual const std::pair< unsigned int, unsigned int > & getGapChannels(unsigned int i_gap) const =0
Return a pair of subchannel indices for a given gap index.
const std::vector< Node * > & getDuctNodes() const
Function that returns the vector with the duct nodes.
Node * getDuctNodeFromChannel(Node *channel_node) const
Function that gets the duct node from the channel node.
virtual unsigned int getNumOfPins() const =0
Return the number of pins.
virtual EChannelType getSubchannelType(unsigned int index) const =0
Return the type of the subchannel for given subchannel index.
virtual const Real & getCrossflowSign(unsigned int i_chan, unsigned int i_local) const =0
Return a sign for the crossflow given a subchannel index and local neighbor index.
virtual const std::vector< std::vector< Real > > & getKGrid() const
Get axial cell location and value of loss coefficient.
virtual unsigned int getZIndex(const Point &point) const
Get axial index of point.
virtual Node * getChannelNode(unsigned int i_chan, unsigned int iz) const =0
Get the subchannel mesh node for a given channel index and elevation index.
virtual unsigned int getNumOfGapsPerLayer() const =0
Return the number of gaps per layer.
virtual const std::vector< unsigned int > & getChannelGaps(unsigned int i_chan) const =0
Return a vector of gap indices for a given channel index.
virtual unsigned int getNumOfAxialCells() const
Return the number of axial cells.
virtual const Real & getPinDiameter() const
Return undeformed Pin diameter.
virtual Node * getPinNode(unsigned int i_pin, unsigned int iz) const =0
Get the pin mesh node for a given pin index and elevation index.
virtual const std::vector< unsigned int > & getPinChannels(unsigned int i_pin) const =0
Return a vector of channel indices for a given Pin index.
void max(const T &r, T &o, Request &req) const
void broadcast(T &data, const unsigned int root_id=0, const bool identical_sizes=false) const
void resize(const unsigned int new_m, const unsigned int new_n)
virtual void zero() override final
const Parallel::Communicator & _communicator
processor_id_type processor_id() const
const Parallel::Communicator & comm() const
Definition SCM.h:17
The following methods are specializations for using the Parallel::packed_range_* routines for a vecto...
static constexpr Real TOLERANCE
SubChannel1PhaseProblem * schp
structure with the needed information to compute the friction factor at a specific subchannel cell
structure with the needed information to compute the Nusselt number at a specific subchannel cell and...