https://mooseframework.inl.gov
ProblemOperatorBase.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 
10 #ifdef MOOSE_MFEM_ENABLED
11 
12 #include "ProblemOperatorBase.h"
13 
14 class MFEMProblem;
15 
16 namespace Moose::MFEM
17 {
18 
20  : _problem(problem), _problem_data(problem.getProblemData())
21 {
22 }
23 
24 void
26 {
29 
30  // Set operator size and block structure for trial spaces
31  _block_true_offsets_trial.SetSize(_trial_variables.size() + 1);
33  for (const auto ind : index_range(_trial_variables))
34  _block_true_offsets_trial[ind + 1] = _trial_variables.at(ind)->ParFESpace()->TrueVSize();
35  _block_true_offsets_trial.PartialSum();
36 
37  // Set operator size and block structure for test spaces
38  _block_true_offsets_test.SetSize(_test_variables.size() + 1);
40  for (const auto ind : index_range(_test_variables))
41  _block_true_offsets_test[ind + 1] = _test_variables.at(ind)->ParFESpace()->TrueVSize();
42  _block_true_offsets_test.PartialSum();
43 
46 }
47 
48 void
49 ProblemOperatorBase::Init(mfem::BlockVector & X)
50 {
51  X.Update(_block_true_offsets_trial);
52  for (const auto i : index_range(_trial_variables))
53  X.GetBlock(i) = _trial_variables[i]->GetTrueVector();
54  // Sync the flags from the global vector with the sub-vectors (copies to global vector location)
55  X.SyncFromBlocks();
56 
57  // After initial assignment of X from the grid function, which may contain initial conditions,
58  // we alias the grid function to X
59  for (const auto i : index_range(_trial_variables))
60  _trial_variables[i]->MakeTRef(
61  _trial_variables[i]->ParFESpace(), X, _block_true_offsets_trial[i]);
62  _trial_true_vector = &X;
63 }
64 
65 void
67 {
68  mooseAssert(_trial_true_vector, "The true vector should already have been set");
69  for (const auto trial_var : _trial_variables)
70  {
71  // Sync the memory flags from the global true vector to the gridfunction aliases
72  trial_var->GetTrueVector().SyncMemory(*_trial_true_vector);
73  trial_var->SetFromTrueVector();
74  }
75 }
76 
77 void
78 ProblemOperatorBase::SolveWithOperator(mfem::Operator & system_operator,
79  mfem::Operator & linear_operator,
80  const mfem::Vector & rhs,
81  mfem::Vector & x)
82 {
83  // Nonlinear solver path for both linear and nonlinear problems. (as a linear problem may still
84  // intentionally be solved through the nonlinear solver machinery when one is provided)
86  {
87  auto & nonlinear_solver = *_problem_data.nonlinear_solver;
88  if (nonlinear_solver.RequiresExternalLinearSolver())
89  {
91  mooseError("The configured MFEM nonlinear solver requires an external linear solver, but "
92  "none was provided.");
93  auto & linear_solver = *_problem_data.jacobian_solver;
94  linear_solver.SetOperator(linear_operator);
95  nonlinear_solver.SetLinearSolver(linear_solver.GetSolver());
96  }
97 
98  nonlinear_solver.SetOperator(system_operator);
99  nonlinear_solver.Mult(rhs, x);
100  }
101  // Linear solver path for linear problems.
102  else
103  {
105  mooseError("A linear MFEM solve requires a linear solver, but none was provided.");
106 
107  auto & linear_solver = *_problem_data.jacobian_solver;
108  linear_solver.SetOperator(linear_operator);
109  linear_solver.Mult(rhs, x);
110  }
111 }
112 
113 } // namespace Moose::MFEM
114 
115 #endif
std::vector< std::string > _test_var_names
std::vector< std::string > _trial_var_names
Vector of names of state gridfunctions used in formulation, ordered by appearance in block vector dur...
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:311
ProblemOperatorBase(MFEMProblem &problem)
void SolveWithOperator(mfem::Operator &system_operator, mfem::Operator &linear_operator, const mfem::Vector &rhs, mfem::Vector &x)
Solve the current system operator using the configured nonlinear and linear solvers.
mfem::Array< int > _block_true_offsets_test
std::shared_ptr< Moose::MFEM::LinearSolverBase > jacobian_solver
std::vector< mfem::ParGridFunction * > _test_variables
mfem::Array< int > _block_true_offsets_trial
T * Get(const std::string &field_name) const
Returns a non-owning pointer to the field. This is guaranteed to return a non-null pointer...
std::vector< mfem::ParGridFunction * > _trial_variables
std::shared_ptr< Moose::MFEM::NonlinearSolverBase > nonlinear_solver
virtual void Init(mfem::BlockVector &X)
Utilities for converting between vector(s) of libMesh Points and MFEM Vector(s).
Moose::MFEM::GridFunctions gridfunctions
auto index_range(const T &sizable)