v0.14.0
ConstrainMatrixCtx.hpp
Go to the documentation of this file.
1 /** \file ConstrainMatrixCtx.hpp
2  *
3  * Can be used if constrains are linear, i.e. are not function of time.
4  */
5 
6 
7 
8 #ifndef __PROJECTION_MATRIX_CTX_HPP__
9 #define __PROJECTION_MATRIX_CTX_HPP__
10 
11 namespace MoFEM {
12 /**
13  * \brief structure for projection matrices
14  * \ingroup projection_matrix
15  *
16  */
17 
19 
21 
22  KSP kSP;
23  Mat C, CT, CCT, CTC, K;
25  Vec X, Qx, KQx;
27  bool createKSP;
31 
32  // Scatter is created form problem_x to problem_y, or scatter is given
33  // in the constructor
34 
35  VecScatter sCatter;
36  string xProblem, yProblem;
37 
38  PetscLogEvent MOFEM_EVENT_projInit;
39  PetscLogEvent MOFEM_EVENT_projQ;
40  PetscLogEvent MOFEM_EVENT_projP;
41  PetscLogEvent MOFEM_EVENT_projR;
42  PetscLogEvent MOFEM_EVENT_projRT;
43  PetscLogEvent MOFEM_EVENT_projCTC_QTKQ;
44 
45  /**
46  * Construct data structure to build operators for projection matrices
47  *
48  * User need to set matrix C to make it work
49  *
50  * \param x_problem problem on which vector is projected
51  * \param y_problem problem used to construct projection matrices
52  * \param create_ksp create ksp solver otherwise user need to set it up
53  */
54  ConstrainMatrixCtx(MoFEM::Interface &m_field, string x_problem,
55  string y_problem, bool create_ksp = true,
56  bool own_contrain_matrix = false);
57 
58  ConstrainMatrixCtx(MoFEM::Interface &m_field, VecScatter scatter,
59  bool create_ksp = true, bool own_contrain_matrix = false);
60 
61  virtual ~ConstrainMatrixCtx() {
62  ierr = destroyQorP();
63  CHKERRABORT(mField.get_comm(), ierr);
64  ierr = destroyQTKQ();
65  CHKERRABORT(mField.get_comm(), ierr);
66  if (ownConstrainMatrix) {
67  ierr = MatDestroy(&C);
68  CHKERRABORT(mField.get_comm(), ierr);
69  }
70  };
71 
72  PetscReal rTol, absTol, dTol;
73  PetscInt maxIts;
74 
75  /**
76  * \brief initialize vectors and matrices for Q and P shell matrices,
77  * scattering is set based on x_problem and y_problem
78  *
79  * \param x is a vector from problem x
80  */
82 
83  /**
84  * \brief initialize vectors and matrices for CTC+QTKQ shell matrices,
85  * scattering is set based on x_problem and y_problem
86  */
88 
89  /**
90  * \brief re-calculate CT and CCT if C matrix has been changed since
91  * initialization
92  */
94 
95  /**
96  * \brief re-calculate CTC matrix has been changed since initialization
97  */
99 
100  /**
101  * \brief destroy sub-matrices used for shell matrices P, Q, R, RT
102  */
104 
105  /**
106  * \brief destroy sub-matrices used for shell matrix QTKQ
107  */
109 
110  friend MoFEMErrorCode ProjectionMatrixMultOpQ(Mat Q, Vec x, Vec f);
113  friend MoFEMErrorCode ConstrainMatrixMultOpRT(Mat RT, Vec x, Vec f);
114  friend MoFEMErrorCode ConstrainMatrixMultOpCTC_QTKQ(Mat CTC_QTKQ, Vec x,
115  Vec f);
116 
119 };
120 
121 /**
122  * \brief Multiplication operator for Q = I-CTC(CCT)^-1C
123  *
124  * \code
125  * Mat Q; //for problem
126  * ConstrainMatrixCtx
127  projection_matrix_ctx(m_field,problem_name,contrains_problem_name);
128  * CHKERR MatCreateShell(PETSC_COMM_WORLD,m,m,M,M,&projection_matrix_ctx,&Q);
129  * CHKERR
130  MatShellSetOperation(Q,MATOP_MULT,(void(*)(void))ProjectionMatrixMultOpQ);
131  * CHKERR
132  MatShellSetOperation(Q,MATOP_DESTROY,(void(*)(void))ConstrainMatrixDestroyOpPorQ);
133  *
134  * \endcode
135 
136  * \ingroup projection_matrix
137 
138  */
140 
141 /**
142  * \brief Multiplication operator for P = CT(CCT)^-1C
143  *
144  * \code
145  * Mat P; //for problem
146  * ConstrainMatrixCtx
147  projection_matrix_ctx(m_field,problem_name,contrains_problem_name);
148  * CHKERR MatCreateShell(PETSC_COMM_WORLD,m,m,M,M,&projection_matrix_ctx,&P);
149  * CHKERR
150  MatShellSetOperation(P,MATOP_MULT,(void(*)(void))ConstrainMatrixMultOpP);
151  *
152  * \endcode
153 
154  * \ingroup projection_matrix
155 
156  */
158 
159 /**
160  * \brief Multiplication operator for R = CT(CCT)^-1
161  *
162  * \code
163  * Mat R; //for problem
164  * ConstrainMatrixCtx
165  projection_matrix_ctx(m_field,problem_name,contrains_problem_name);
166  * CHKERR MatCreateShell(PETSC_COMM_WORLD,m,m,M,M,&projection_matrix_ctx,&R);
167  * CHKERR
168  MatShellSetOperation(R,MATOP_MULT,(void(*)(void))ConstrainMatrixMultOpR);
169  *
170  * \endcode
171 
172  * \ingroup projection_matrix
173 
174  */
176 
177 /**
178  * \brief Multiplication operator for RT = (CCT)^-TC
179  *
180  * \code
181  * Mat RT; //for problem
182  * ConstrainMatrixCtx
183  projection_matrix_ctx(m_field,problem_name,contrains_problem_name);
184  * CHKERR MatCreateShell(PETSC_COMM_WORLD,m,m,M,M,&projection_matrix_ctx,&RT);
185  * CHKERR
186  MatShellSetOperation(RT,MATOP_MULT,(void(*)(void))ConstrainMatrixMultOpRT);
187  *
188  * \endcode
189 
190  * \ingroup projection_matrix
191 
192  */
194 
195 /**
196  * \brief Multiplication operator for RT = (CCT)^-TC
197  *
198  * \code
199  * Mat CTC_QTKQ; //for problem
200  * ConstrainMatrixCtx
201  projection_matrix_ctx(m_field,problem_name,contrains_problem_name);
202  * CHKERR
203  MatCreateShell(PETSC_COMM_WORLD,m,m,M,M,&projection_matrix_ctx,&CTC_QTKQ);
204  * CHKERR
205  MatShellSetOperation(CTC_QTKQ,MATOP_MULT,(void(*)(void))ConstrainMatrixMultOpCTC_QTKQ);
206  * CHKERR
207  MatShellSetOperation(CTC_QTKQ,MATOP_DESTROY,(void(*)(void))ConstrainMatrixDestroyOpQTKQ);
208  *
209  * \endcode
210  *
211 
212  * \ingroup projection_matrix
213 
214  */
216 
217 /**
218  * \brief Destroy shell matrix Q
219  *
220  * \code
221  * Mat Q; //for problem
222  * ConstrainMatrixCtx
223  projection_matrix_ctx(m_field,problem_name,contrains_problem_name);
224  * CHKERR MatCreateShell(PETSC_COMM_WORLD,m,m,M,M,&projection_matrix_ctx,&Q);
225  * CHKERR
226  MatShellSetOperation(Q,MATOP_MULT,(void(*)(void))ProjectionMatrixMultOpQ);
227  * CHKERR
228  MatShellSetOperation(Q,MATOP_DESTROY,(void(*)(void))ConstrainMatrixDestroyOpPorQ);
229  *
230  * \endcode
231 
232  * \ingroup projection_matrix
233 
234  */
236 
237 /**
238  * \brief Destroy shell matrix
239  *
240  * \code
241  * Mat CTC_QTKQ; //for problem
242  * ConstrainMatrixCtx
243  projection_matrix_ctx(m_field,problem_name,contrains_problem_name);
244  * CHKERR MatCreateShell(PETSC_COMM_WORLD,m,m,M,M,&projection_matrix_ctx,&Q);
245  * CHKERR
246  MatShellSetOperation(Q,MATOP_MULT,(void(*)(void))ConstrainMatrixMultOpCTC_QTKQ);
247  * CHKERR
248  MatShellSetOperation(Q,MATOP_DESTROY,(void(*)(void))mat_destroy_QTKQ);
249  *
250  * \endcode
251 
252  * \ingroup projection_matrix
253 
254  */
256 
257 } // namespace MoFEM
258 
259 #endif // __PROJECTION_MATRIX_CTX_HPP__
260 
261 /**
262  \defgroup projection_matrix Constrain Projection Matrix
263  \ingroup mofem_basic_finite_elements
264 **/
MoFEM::ConstrainMatrixCtx::ownConstrainMatrix
bool ownConstrainMatrix
Definition: ConstrainMatrixCtx.hpp:30
MoFEM::ConstrainMatrixCtx::yProblem
string yProblem
Definition: ConstrainMatrixCtx.hpp:36
MoFEM::ConstrainMatrixCtx::initializeQorP
MoFEMErrorCode initializeQorP(Vec x)
initialize vectors and matrices for Q and P shell matrices, scattering is set based on x_problem and ...
Definition: ConstrainMatrixCtx.cpp:45
MoFEM::ConstrainMatrixCtx::maxIts
PetscInt maxIts
Definition: ConstrainMatrixCtx.hpp:73
MoFEM::ConstrainMatrixCtx::Cx
Vec Cx
Definition: ConstrainMatrixCtx.hpp:24
MoFEM::ConstrainMatrixMultOpRT
MoFEMErrorCode ConstrainMatrixMultOpRT(Mat RT, Vec x, Vec f)
Multiplication operator for RT = (CCT)^-TC.
Definition: ConstrainMatrixCtx.cpp:243
MoFEM::ConstrainMatrixCtx::recalculateCTC
MoFEMErrorCode recalculateCTC()
re-calculate CTC matrix has been changed since initialization
Definition: ConstrainMatrixCtx.cpp:143
MoFEM::ConstrainMatrixCtx::destroyQorP
MoFEMErrorCode destroyQorP()
destroy sub-matrices used for shell matrices P, Q, R, RT
Definition: ConstrainMatrixCtx.cpp:94
MoFEM::CoreInterface::get_comm
virtual MPI_Comm & get_comm() const =0
MoFEM::ConstrainMatrixDestroyOpPorQ
MoFEMErrorCode ConstrainMatrixDestroyOpPorQ(Mat Q)
Destroy shell matrix Q.
Definition: ConstrainMatrixCtx.cpp:290
MoFEM::ConstrainMatrixCtx::CCT
Mat CCT
Definition: ConstrainMatrixCtx.hpp:23
MoFEM::ConstrainMatrixCtx::MOFEM_EVENT_projP
PetscLogEvent MOFEM_EVENT_projP
Definition: ConstrainMatrixCtx.hpp:40
MoFEM::ConstrainMatrixMultOpP
MoFEMErrorCode ConstrainMatrixMultOpP(Mat P, Vec x, Vec f)
Multiplication operator for P = CT(CCT)^-1C.
Definition: ConstrainMatrixCtx.cpp:197
MoFEM::Exceptions::MoFEMErrorCode
PetscErrorCode MoFEMErrorCode
MoFEM/PETSc error code.
Definition: Exceptions.hpp:56
MoFEM::ConstrainMatrixCtx::MOFEM_EVENT_projCTC_QTKQ
PetscLogEvent MOFEM_EVENT_projCTC_QTKQ
Definition: ConstrainMatrixCtx.hpp:43
MoFEM::ConstrainMatrixCtx::ConstrainMatrixDestroyOpPorQ
friend MoFEMErrorCode ConstrainMatrixDestroyOpPorQ()
MoFEM::ConstrainMatrixCtx::absTol
PetscReal absTol
Definition: ConstrainMatrixCtx.hpp:72
MoFEM::ConstrainMatrixCtx::ConstrainMatrixMultOpCTC_QTKQ
friend MoFEMErrorCode ConstrainMatrixMultOpCTC_QTKQ(Mat CTC_QTKQ, Vec x, Vec f)
Multiplication operator for RT = (CCT)^-TC.
Definition: ConstrainMatrixCtx.cpp:261
MoFEM::ConstrainMatrixCtx::initQTKQ
bool initQTKQ
Definition: ConstrainMatrixCtx.hpp:26
MoFEM::DeprecatedCoreInterface
Deprecated interface functions.
Definition: DeprecatedCoreInterface.hpp:16
MoFEM::ConstrainMatrixCtx::C
Mat C
Definition: ConstrainMatrixCtx.hpp:23
MoFEM::ConstrainMatrixCtx::CTC
Mat CTC
Definition: ConstrainMatrixCtx.hpp:23
MoFEM::ConstrainMatrixCtx::ProjectionMatrixMultOpQ
friend MoFEMErrorCode ProjectionMatrixMultOpQ(Mat Q, Vec x, Vec f)
Multiplication operator for Q = I-CTC(CCT)^-1C.
Definition: ConstrainMatrixCtx.cpp:163
MoFEM::ConstrainMatrixCtx::ConstrainMatrixMultOpRT
friend MoFEMErrorCode ConstrainMatrixMultOpRT(Mat RT, Vec x, Vec f)
Multiplication operator for RT = (CCT)^-TC.
Definition: ConstrainMatrixCtx.cpp:243
MoFEM::ConstrainMatrixCtx::CTCx
Vec CTCx
Definition: ConstrainMatrixCtx.hpp:24
MoFEM::ConstrainMatrixCtx::destroyQTKQ
MoFEMErrorCode destroyQTKQ()
destroy sub-matrices used for shell matrix QTKQ
Definition: ConstrainMatrixCtx.cpp:151
MoFEM::ConstrainMatrixCtx::initializeQTKQ
MoFEMErrorCode initializeQTKQ()
initialize vectors and matrices for CTC+QTKQ shell matrices, scattering is set based on x_problem and...
Definition: ConstrainMatrixCtx.cpp:114
MoFEM::ConstrainMatrixCtx
structure for projection matrices
Definition: ConstrainMatrixCtx.hpp:18
MoFEM::ConstrainMatrixCtx::cancelKSPMonitor
bool cancelKSPMonitor
Definition: ConstrainMatrixCtx.hpp:29
MoFEM
implementation of Data Operators for Forces and Sources
Definition: Common.hpp:10
MoFEM::ConstrainMatrixCtx::MOFEM_EVENT_projQ
PetscLogEvent MOFEM_EVENT_projQ
Definition: ConstrainMatrixCtx.hpp:39
MoFEM::ProjectionMatrixMultOpQ
MoFEMErrorCode ProjectionMatrixMultOpQ(Mat Q, Vec x, Vec f)
Multiplication operator for Q = I-CTC(CCT)^-1C.
Definition: ConstrainMatrixCtx.cpp:163
R
@ R
Definition: free_surface.cpp:394
MoFEM::ConstrainMatrixMultOpCTC_QTKQ
MoFEMErrorCode ConstrainMatrixMultOpCTC_QTKQ(Mat CTC_QTKQ, Vec x, Vec f)
Multiplication operator for RT = (CCT)^-TC.
Definition: ConstrainMatrixCtx.cpp:261
MoFEM::ConstrainMatrixCtx::CT
Mat CT
Definition: ConstrainMatrixCtx.hpp:23
MoFEM::ConstrainMatrixCtx::~ConstrainMatrixCtx
virtual ~ConstrainMatrixCtx()
Definition: ConstrainMatrixCtx.hpp:61
MoFEM::ConstrainMatrixCtx::sCatter
VecScatter sCatter
Definition: ConstrainMatrixCtx.hpp:35
EshelbianPlasticity::P
@ P
Definition: EshelbianContact.cpp:197
MoFEM::ConstrainMatrixCtx::dTol
PetscReal dTol
Definition: ConstrainMatrixCtx.hpp:72
MoFEM::ConstrainMatrixCtx::recalculateCTandCCT
MoFEMErrorCode recalculateCTandCCT()
re-calculate CT and CCT if C matrix has been changed since initialization
Definition: ConstrainMatrixCtx.cpp:85
MoFEM::ConstrainMatrixCtx::xProblem
string xProblem
Definition: ConstrainMatrixCtx.hpp:36
MoFEM::ConstrainMatrixCtx::rTol
PetscReal rTol
Definition: ConstrainMatrixCtx.hpp:70
MoFEM::ConstrainMatrixCtx::K
Mat K
Definition: ConstrainMatrixCtx.hpp:23
MoFEM::ConstrainMatrixCtx::MOFEM_EVENT_projR
PetscLogEvent MOFEM_EVENT_projR
Definition: ConstrainMatrixCtx.hpp:41
MoFEM::ConstrainMatrixCtx::createKSP
bool createKSP
Definition: ConstrainMatrixCtx.hpp:27
MoFEM::ConstrainMatrixCtx::CT_CCTm1_Cx
Vec CT_CCTm1_Cx
Definition: ConstrainMatrixCtx.hpp:24
MoFEM::ConstrainMatrixDestroyOpQTKQ
MoFEMErrorCode ConstrainMatrixDestroyOpQTKQ(Mat QTKQ)
Destroy shell matrix.
Definition: ConstrainMatrixCtx.cpp:298
MoFEM::ConstrainMatrixCtx::createScatter
bool createScatter
Definition: ConstrainMatrixCtx.hpp:28
MoFEM::ConstrainMatrixCtx::mField
MoFEM::Interface & mField
Definition: ConstrainMatrixCtx.hpp:20
MoFEM::ConstrainMatrixCtx::ConstrainMatrixMultOpP
friend MoFEMErrorCode ConstrainMatrixMultOpP(Mat P, Vec x, Vec f)
Multiplication operator for P = CT(CCT)^-1C.
Definition: ConstrainMatrixCtx.cpp:197
MoFEM::ConstrainMatrixCtx::MOFEM_EVENT_projInit
PetscLogEvent MOFEM_EVENT_projInit
Definition: ConstrainMatrixCtx.hpp:38
MoFEM::ConstrainMatrixCtx::X
Vec X
Definition: ConstrainMatrixCtx.hpp:25
MoFEM::ConstrainMatrixCtx::initQorP
bool initQorP
Definition: ConstrainMatrixCtx.hpp:26
MoFEM::ConstrainMatrixCtx::ConstrainMatrixMultOpR
friend MoFEMErrorCode ConstrainMatrixMultOpR(Mat R, Vec x, Vec f)
Multiplication operator for R = CT(CCT)^-1.
Definition: ConstrainMatrixCtx.cpp:221
MoFEM::ConstrainMatrixCtx::Qx
Vec Qx
Definition: ConstrainMatrixCtx.hpp:25
HenckyOps::f
auto f
Definition: HenckyOps.hpp:15
MoFEM::ConstrainMatrixMultOpR
MoFEMErrorCode ConstrainMatrixMultOpR(Mat R, Vec x, Vec f)
Multiplication operator for R = CT(CCT)^-1.
Definition: ConstrainMatrixCtx.cpp:221
MoFEM::Exceptions::ierr
static MoFEMErrorCodeGeneric< PetscErrorCode > ierr
Definition: Exceptions.hpp:76
EigenMatrix::Vec
const FTensor::Tensor2< T, Dim, Dim > Vec
Definition: MatrixFunction.hpp:66
MoFEM::ConstrainMatrixCtx::MOFEM_EVENT_projRT
PetscLogEvent MOFEM_EVENT_projRT
Definition: ConstrainMatrixCtx.hpp:42
MoFEM::ConstrainMatrixCtx::KQx
Vec KQx
Definition: ConstrainMatrixCtx.hpp:25
MoFEM::ConstrainMatrixCtx::CCTm1_Cx
Vec CCTm1_Cx
Definition: ConstrainMatrixCtx.hpp:24
MoFEM::ConstrainMatrixCtx::ConstrainMatrixCtx
ConstrainMatrixCtx(MoFEM::Interface &m_field, string x_problem, string y_problem, bool create_ksp=true, bool own_contrain_matrix=false)
Definition: ConstrainMatrixCtx.cpp:19
MoFEM::ConstrainMatrixCtx::kSP
KSP kSP
Definition: ConstrainMatrixCtx.hpp:22
MoFEM::ConstrainMatrixCtx::ConstrainMatrixDestroyOpQTKQ
friend MoFEMErrorCode ConstrainMatrixDestroyOpQTKQ()