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