v0.16.3
Loading...
Searching...
No Matches
ConstrainMatrixCtx.hpp
Go to the documentation of this file.
1/** \file ConstrainMatrixCtx.hpp
2 *
3 * Matrix-free projection and lifting operations for a supplied constraint
4 * matrix and constraint-space inverse action.
5 */
6
7#ifndef MOFEM_CONSTRAIN_MATRIX_CTX_HPP
8 #define MOFEM_CONSTRAIN_MATRIX_CTX_HPP
9
10namespace MoFEM {
11
12/**
13 * \brief API for matrices that project onto, or lift from, a constraint space.
14 * \ingroup projection_matrix
15 */
17
18 virtual ~ConstrainMatrixCtx() = default;
19
20 /**
21 * Set the constraint matrix C and its row-space inverse action atomically.
22 *
23 * @p inverse_cct represents (C C^T)^-1 on the selected constraint space, or
24 * the corresponding embedded generalized inverse for a masked full space.
25 * It must have the row layout of C in both dimensions and support MatMult().
26 * MatMultTranspose() is additionally required by applyRT() and
27 * applyCTC_QTKQ(). Both PETSc objects are retained.
28 *
29 * All data derived from the previous pair are invalidated. For in-place
30 * value changes which preserve layouts and sparsity, update @p inverse_cct
31 * in the same transaction and call recalculateCT() and recalculateCTC().
32 */
33 virtual MoFEMErrorCode setConstrainMatrix(Mat constrain_matrix,
34 Mat inverse_cct) = 0;
35
36 /** Get the constraint matrix C. */
37 virtual Mat getConstrainMatrix() const = 0;
38
39 /** Get the supplied inverse action for C C^T. */
40 virtual Mat getInverseCCT() const = 0;
41
42 /**
43 * Set the matrix K used by C^T C + Q^T K Q and retain a PETSc reference.
44 * Work vectors derived from the previous K are invalidated.
45 */
46 virtual MoFEMErrorCode setSystemMatrix(Mat system_matrix) = 0;
47
48 /** Get the matrix K used by C^T C + Q^T K Q. */
49 virtual Mat getSystemMatrix() const = 0;
50
51 /**
52 * Initialize matrices, vectors, and the scatter used by Q, P, R, and R^T.
53 * The operation is idempotent until the C/inverse-action pair is replaced.
54 *
55 * @param x Vector defining the full problem layout.
56 */
57 virtual MoFEMErrorCode initializeQorP(Vec x) = 0;
58
59 /**
60 * Initialize matrices and work vectors used by C^T C + Q^T K Q.
61 * The operation is idempotent until C or K is replaced.
62 */
64
65 /**
66 * Recalculate C^T after an in-place value change in C.
67 * The layout and nonzero structure of C must be unchanged. The caller must
68 * update the supplied inverse action in the same transaction. Projection
69 * MATSHELL objects already created from this context do not receive a PETSc
70 * state increment; recreate them before passing the changed operator to a
71 * consumer which caches by matrix state.
72 */
74
75 /**
76 * Recalculate C^T C after an in-place value change in C.
77 * The layout and nonzero structure of C must be unchanged.
78 */
80
81 /** Apply Q = I - C^T S C for the supplied inverse action S. */
82 virtual MoFEMErrorCode applyQ(Vec x, Vec f) = 0;
83
84 /** Apply P = C^T S C for the supplied inverse action S. */
85 virtual MoFEMErrorCode applyP(Vec x, Vec f) = 0;
86
87 /** Apply R = C^T S for the supplied inverse action S. */
88 virtual MoFEMErrorCode applyR(Vec x, Vec f) = 0;
89
90 /** Apply R^T = S^T C for the supplied inverse action S. */
91 virtual MoFEMErrorCode applyRT(Vec x, Vec f) = 0;
92
93 /** Apply C^T C + Q^T K Q. */
94 virtual MoFEMErrorCode applyCTC_QTKQ(Vec x, Vec f) = 0;
95};
96
97/**
98 * Create a context whose scatter is built from two DMs.
99 *
100 * @param x_dm DM owning the full vector layout.
101 * @param y_dm DM whose column numbering matches the constraint matrix.
102 * @param constrain_matrix Constraint matrix C.
103 * @param inverse_cct Supplied inverse action for C C^T.
104 */
105boost::shared_ptr<ConstrainMatrixCtx>
106createConstrainMatrixCtx(DM x_dm, DM y_dm, Mat constrain_matrix,
107 Mat inverse_cct);
108
109/**
110 * Create a context using a supplied scatter.
111 *
112 * The communicator is obtained from the scatter.
113 *
114 * @param scatter Scatter between the full and constrained vector layouts.
115 * @param constrain_matrix Constraint matrix C.
116 * @param inverse_cct Supplied inverse action for C C^T.
117 */
118boost::shared_ptr<ConstrainMatrixCtx>
119createConstrainMatrixCtx(VecScatter scatter, Mat constrain_matrix,
120 Mat inverse_cct);
121
122/**
123 * Create a KSP-backed shell action for (C C^T)^-1.
124 *
125 * The returned square MATSHELL retains @p constrain_matrix. It refreshes
126 * C C^T and the KSP when values of C change, applies KSPSolve() in MatMult(),
127 * and applies KSPSolveTranspose() in MatMultTranspose(). A change of C layout
128 * requires a new shell matrix. Updating C through this backend does not
129 * refresh C^T cached by a ConstrainMatrixCtx; call recalculateCT() and, when
130 * needed, recalculateCTC() on that context in the same update transaction.
131 * A zero-dimensional constraint space is applied as the zero action without
132 * constructing a KSP.
133 *
134 * If @p ksp is nullptr, the shell creates and configures a KSP using
135 * @p options_prefix. The default gives options such as
136 * `-constraint_matrix_ksp_type` and `-constraint_matrix_pc_type`. A supplied
137 * KSP remains caller-configured except that every multiply enforces a zero
138 * initial guess, as required for PETSc MatMult overwrite semantics.
139 *
140 * @param constrain_matrix Constraint matrix C.
141 * @param ksp Optional caller-configured KSP.
142 * @param options_prefix Prefix for an internally created KSP.
143 */
145createCCTInverseKSP(Mat constrain_matrix, KSP ksp = nullptr,
146 const std::string &options_prefix = "constraint_matrix_");
147
148/**
149 * Create a lifetime-safe shell matrix for Q.
150 *
151 * The returned PETSc matrix retains @p ctx until the matrix is destroyed.
152 * @param ctx Constraint-matrix context.
153 * @param x Vector defining the full problem layout.
154 */
156createProjectionMatrixQ(const boost::shared_ptr<ConstrainMatrixCtx> &ctx,
157 Vec x);
158
159/** Create a lifetime-safe shell matrix for P. */
161createProjectionMatrixP(const boost::shared_ptr<ConstrainMatrixCtx> &ctx,
162 Vec x);
163
164/** Create a lifetime-safe shell matrix for R. */
166createProjectionMatrixR(const boost::shared_ptr<ConstrainMatrixCtx> &ctx,
167 Vec x);
168
169/** Create a lifetime-safe shell matrix for R^T. */
171createProjectionMatrixRT(const boost::shared_ptr<ConstrainMatrixCtx> &ctx,
172 Vec x);
173
174/** Create a lifetime-safe shell matrix for C^T C + Q^T K Q. */
176 const boost::shared_ptr<ConstrainMatrixCtx> &ctx, Vec x);
177
178} // namespace MoFEM
179
180#endif // MOFEM_CONSTRAIN_MATRIX_CTX_HPP
181
182/**
183 \defgroup projection_matrix Constraint Projection Matrix
184 \ingroup mofem_petsc_solvers
185**/
SmartPetscObj< KSP > ksp
PetscErrorCode MoFEMErrorCode
MoFEM/PETSc error code.
implementation of Data Operators for Forces and Sources
Definition Common.hpp:10
SmartPetscObj< Mat > createCCTInverseKSP(Mat constrain_matrix, KSP ksp=nullptr, const std::string &options_prefix="constraint_matrix_")
SmartPetscObj< Mat > createProjectionMatrixP(const boost::shared_ptr< ConstrainMatrixCtx > &ctx, Vec x)
boost::shared_ptr< ConstrainMatrixCtx > createConstrainMatrixCtx(DM x_dm, DM y_dm, Mat constrain_matrix, Mat inverse_cct)
SmartPetscObj< Mat > createProjectionMatrixR(const boost::shared_ptr< ConstrainMatrixCtx > &ctx, Vec x)
SmartPetscObj< Mat > createProjectionMatrixCTCPlusQTKQ(const boost::shared_ptr< ConstrainMatrixCtx > &ctx, Vec x)
SmartPetscObj< Mat > createProjectionMatrixQ(const boost::shared_ptr< ConstrainMatrixCtx > &ctx, Vec x)
SmartPetscObj< Mat > createProjectionMatrixRT(const boost::shared_ptr< ConstrainMatrixCtx > &ctx, Vec x)
API for matrices that project onto, or lift from, a constraint space.
virtual MoFEMErrorCode applyR(Vec x, Vec f)=0
virtual MoFEMErrorCode recalculateCT()=0
virtual Mat getSystemMatrix() const =0
virtual MoFEMErrorCode applyCTC_QTKQ(Vec x, Vec f)=0
virtual ~ConstrainMatrixCtx()=default
virtual MoFEMErrorCode initializeQorP(Vec x)=0
virtual MoFEMErrorCode recalculateCTC()=0
virtual MoFEMErrorCode applyP(Vec x, Vec f)=0
virtual MoFEMErrorCode setSystemMatrix(Mat system_matrix)=0
virtual Mat getConstrainMatrix() const =0
virtual MoFEMErrorCode setConstrainMatrix(Mat constrain_matrix, Mat inverse_cct)=0
virtual MoFEMErrorCode applyRT(Vec x, Vec f)=0
virtual MoFEMErrorCode initializeQTKQ()=0
virtual MoFEMErrorCode applyQ(Vec x, Vec f)=0
virtual Mat getInverseCCT() const =0
intrusive_ptr for managing petsc objects