https://mooseframework.inl.gov
Loading...
Searching...
No Matches
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#include "MFEMProblem.h"
14
15namespace Moose::MFEM
16{
17
19 : PerfGraphInterface(problem.getMooseApp()),
20 _problem(problem),
21 _problem_data(problem.getProblemData())
22{
23}
24
25void
27{
30
31 // Set operator size and block structure for trial spaces
34 for (const auto ind : index_range(_trial_variables))
35 _block_true_offsets_trial[ind + 1] = _trial_variables.at(ind)->ParFESpace()->TrueVSize();
36 _block_true_offsets_trial.PartialSum();
37
38 // Set operator size and block structure for test spaces
39 _block_true_offsets_test.SetSize(_test_variables.size() + 1);
41 for (const auto ind : index_range(_test_variables))
42 _block_true_offsets_test[ind + 1] = _test_variables.at(ind)->ParFESpace()->TrueVSize();
43 _block_true_offsets_test.PartialSum();
44
47}
48
49void
51{
54 for (const auto i : index_range(_trial_variables))
55 _true_solution.GetBlock(i) = _trial_variables[i]->GetTrueVector();
56 // Sync the flags from the global vector with the sub-vectors (copies to global vector location)
57 _true_solution.SyncFromBlocks();
58
59 // After initial assignment of the true solution from the grid function, which may contain
60 // initial conditions, we alias the grid function to it
61 for (const auto i : index_range(_trial_variables))
62 _trial_variables[i]->MakeTRef(
65}
66
67void
69{
70 mooseAssert(_trial_true_vector, "The true vector should already have been set");
71 for (const auto trial_var : _trial_variables)
72 {
73 // Sync the memory flags from the global true vector to the gridfunction aliases
74 trial_var->GetTrueVector().SyncMemory(*_trial_true_vector);
75 trial_var->SetFromTrueVector();
76 }
77}
78
79void
80ProblemOperatorBase::SolveWithOperator(mfem::Operator & system_operator,
81 mfem::Operator & linear_operator,
82 const mfem::Vector & rhs,
83 mfem::Vector & x)
84{
85 // Nonlinear solver path for both linear and nonlinear problems. (as a linear problem may still
86 // intentionally be solved through the nonlinear solver machinery when one is provided)
88 {
89 auto & nonlinear_solver = *_problem_data.nonlinear_solver;
90 if (nonlinear_solver.RequiresExternalLinearSolver())
91 {
93 mooseError("The configured MFEM nonlinear solver requires an external linear solver, but "
94 "none was provided.");
95 auto & linear_solver = *_problem_data.jacobian_solver;
96 linear_solver.SetOperator(linear_operator);
97 nonlinear_solver.SetLinearSolver(linear_solver.GetSolver());
98 }
99
100 nonlinear_solver.SetOperator(system_operator);
101 nonlinear_solver.Mult(rhs, x);
102 }
103 // Linear solver path for linear problems.
104 else
105 {
107 mooseError("A linear MFEM solve requires a linear solver, but none was provided.");
108
109 auto & linear_solver = *_problem_data.jacobian_solver;
110 linear_solver.SetOperator(linear_operator);
111 linear_solver.Mult(rhs, x);
112 }
113}
114
115} // namespace Moose::MFEM
116
117#endif
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application.
Definition MooseError.h:311
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
mfem::BlockVector _true_solution
Persistent true-DoF solution vector backing this operator's trial grid functions after Init().
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.
std::vector< mfem::ParGridFunction * > _test_variables
std::vector< std::string > _trial_var_names
Vector of names of state gridfunctions used in formulation, ordered by appearance in block vector dur...
std::vector< std::string > _test_var_names
ProblemOperatorBase(MFEMProblem &problem)
Interface for objects interacting with the PerfGraph.
Utilities for converting between vector(s) of libMesh Points and MFEM Vector(s).
std::shared_ptr< Moose::MFEM::LinearSolverBase > jacobian_solver
std::shared_ptr< Moose::MFEM::NonlinearSolverBase > nonlinear_solver
Moose::MFEM::GridFunctions gridfunctions