v0.16.3
Loading...
Searching...
No Matches
Public Member Functions | Private Member Functions | Private Attributes | List of all members
MoFEM::ConstrainMatrixCtxImpl Struct Reference
Inheritance diagram for MoFEM::ConstrainMatrixCtxImpl:
[legend]
Collaboration diagram for MoFEM::ConstrainMatrixCtxImpl:
[legend]

Public Member Functions

 ConstrainMatrixCtxImpl (MPI_Comm comm, DM x_dm, DM y_dm)
 
 ConstrainMatrixCtxImpl (MPI_Comm comm, VecScatter scatter)
 
 ~ConstrainMatrixCtxImpl () override=default
 
MoFEMErrorCode setConstrainMatrix (Mat constrain_matrix, Mat inverse_cct) override
 
Mat getConstrainMatrix () const override
 
Mat getInverseCCT () const override
 
MoFEMErrorCode setSystemMatrix (Mat system_matrix) override
 
Mat getSystemMatrix () const override
 
MoFEMErrorCode initializeQorP (Vec x) override
 
MoFEMErrorCode initializeQTKQ () override
 
MoFEMErrorCode recalculateCT () override
 
MoFEMErrorCode recalculateCTC () override
 
MoFEMErrorCode applyQ (Vec x, Vec f) override
 
MoFEMErrorCode applyP (Vec x, Vec f) override
 
MoFEMErrorCode applyR (Vec x, Vec f) override
 
MoFEMErrorCode applyRT (Vec x, Vec f) override
 
MoFEMErrorCode applyCTC_QTKQ (Vec x, Vec f) override
 
- Public Member Functions inherited from MoFEM::ConstrainMatrixCtx
virtual ~ConstrainMatrixCtx ()=default
 

Private Member Functions

MoFEMErrorCode invalidateProjectionData ()
 
MoFEMErrorCode invalidateSystemData ()
 
MoFEMErrorCode validatePetscObject (PetscObject object, const char *object_name) const
 
MoFEMErrorCode validateVectorLayout (Vec vec, PetscInt local_size, PetscInt global_size, const char *vector_name) const
 
MoFEMErrorCode validateInverseCCTLayout (Mat constrain_matrix, Mat inverse_cct) const
 
MoFEMErrorCode validateSystemMatrixLayout () const
 
MoFEMErrorCode applyInverseCCT (Vec x, Vec f, bool transpose)
 
MoFEMErrorCode applyQImpl (Vec x, Vec f, bool transpose_inverse)
 

Private Attributes

MPI_Comm comm
 
SmartPetscObj< DM > xDm
 
SmartPetscObj< DM > yDm
 
SmartPetscObj< Mat > C
 
SmartPetscObj< Mat > CT
 
SmartPetscObj< Mat > inverseCCT
 
SmartPetscObj< Mat > CTC
 
SmartPetscObj< Mat > K
 
SmartPetscObj< Vec > Cx
 
SmartPetscObj< Vec > CCTm1Cx
 
SmartPetscObj< Vec > CTCCTm1Cx
 
SmartPetscObj< Vec > CTCx
 
SmartPetscObj< Vec > X
 
SmartPetscObj< Vec > Qx
 
SmartPetscObj< Vec > KQx
 
SmartPetscObj< VecScatter > scatter
 
bool projectionInitialized = false
 
bool qtkqInitialized = false
 
bool buildScatterFromDms = true
 
PetscInt fullLocalSize = 0
 
PetscInt fullGlobalSize = 0
 

Detailed Description

Definition at line 115 of file ConstrainMatrixCtx.cpp.

Constructor & Destructor Documentation

◆ ConstrainMatrixCtxImpl() [1/2]

MoFEM::ConstrainMatrixCtxImpl::ConstrainMatrixCtxImpl ( MPI_Comm  comm,
DM  x_dm,
DM  y_dm 
)

Definition at line 180 of file ConstrainMatrixCtx.cpp.

181 : comm(comm), xDm(x_dm, true), yDm(y_dm, true) {
182 getProjectionEvents();
183}

◆ ConstrainMatrixCtxImpl() [2/2]

MoFEM::ConstrainMatrixCtxImpl::ConstrainMatrixCtxImpl ( MPI_Comm  comm,
VecScatter  scatter 
)

Definition at line 185 of file ConstrainMatrixCtx.cpp.

187 : comm(comm), scatter(scatter, true), buildScatterFromDms(false) {
188 getProjectionEvents();
189}
SmartPetscObj< VecScatter > scatter

◆ ~ConstrainMatrixCtxImpl()

MoFEM::ConstrainMatrixCtxImpl::~ConstrainMatrixCtxImpl ( )
overridedefault

Member Function Documentation

◆ applyCTC_QTKQ()

MoFEMErrorCode MoFEM::ConstrainMatrixCtxImpl::applyCTC_QTKQ ( Vec  x,
Vec  f 
)
overridevirtual

Apply C^T C + Q^T K Q.

Implements MoFEM::ConstrainMatrixCtx.

Definition at line 658 of file ConstrainMatrixCtx.cpp.

658 {
660 const auto &events = getProjectionEvents();
661 CHKERR PetscLogEventBegin(events.projectionCTCPlusQTKQ, 0, 0, 0, 0);
662 LogEventScope event_scope(events.projectionCTCPlusQTKQ, comm);
663
668 "C^T C + Q^T K Q input");
670 "C^T C + Q^T K Q output");
671
672 // Consume x into CTCx before Qx/f below can overwrite it, so that x and f
673 // may safely alias the same Vec.
674 CHKERR VecScatterBegin(scatter, x, X, INSERT_VALUES, SCATTER_FORWARD);
675 CHKERR VecScatterEnd(scatter, x, X, INSERT_VALUES, SCATTER_FORWARD);
676 CHKERR MatMult(CTC, X, CTCx);
677
678 CHKERR applyQImpl(x, Qx, false);
679 CHKERR MatMult(K, Qx, KQx);
680 CHKERR applyQImpl(KQx, f, true);
681 CHKERR VecScatterBegin(scatter, CTCx, f, ADD_VALUES, SCATTER_REVERSE);
682 CHKERR VecScatterEnd(scatter, CTCx, f, ADD_VALUES, SCATTER_REVERSE);
683
684 CHKERR event_scope.end();
686}
#define MoFEMFunctionBegin
First executable line of each MoFEM function, used for error handling. Final line of MoFEM functions ...
#define MoFEMFunctionReturn(a)
Last executable line of each PETSc function used for error handling. Replaces return()
#define CHKERR
Inline error check.
MoFEMErrorCode applyQImpl(Vec x, Vec f, bool transpose_inverse)
MoFEMErrorCode initializeQTKQ() override
MoFEMErrorCode validateVectorLayout(Vec vec, PetscInt local_size, PetscInt global_size, const char *vector_name) const
MoFEMErrorCode validateSystemMatrixLayout() const
MoFEMErrorCode initializeQorP(Vec x) override

◆ applyInverseCCT()

MoFEMErrorCode MoFEM::ConstrainMatrixCtxImpl::applyInverseCCT ( Vec  x,
Vec  f,
bool  transpose 
)
private

Definition at line 525 of file ConstrainMatrixCtx.cpp.

526 {
528 if (transpose) {
529 PetscBool has_mult_transpose;
530 CHKERR MatHasOperation(inverseCCT, MATOP_MULT_TRANSPOSE,
531 &has_mult_transpose);
532 if (!has_mult_transpose) {
533 MOFEM_LOG("SELF", Sev::error)
534 << "Inverse C C^T action does not implement MatMultTranspose";
536 "Inverse C C^T action does not implement MatMultTranspose");
537 }
538 CHKERR MatMultTranspose(inverseCCT, x, f);
539 } else {
540 CHKERR MatMult(inverseCCT, x, f);
541 }
543}
#define MoFEMFunctionReturnHot(a)
Last executable line of each PETSc function used for error handling. Replaces return()
@ MOFEM_DATA_INCONSISTENCY
Definition definitions.h:31
#define MoFEMFunctionBeginHot
First executable line of each MoFEM function, used for error handling. Final line of MoFEM functions ...
#define MOFEM_LOG(channel, severity)
Log.

◆ applyP()

MoFEMErrorCode MoFEM::ConstrainMatrixCtxImpl::applyP ( Vec  x,
Vec  f 
)
overridevirtual

Apply P = C^T S C for the supplied inverse action S.

Implements MoFEM::ConstrainMatrixCtx.

Definition at line 575 of file ConstrainMatrixCtx.cpp.

575 {
577 const auto &events = getProjectionEvents();
578 CHKERR PetscLogEventBegin(events.projectionP, 0, 0, 0, 0);
579 LogEventScope event_scope(events.projectionP, comm);
580
584 CHKERR VecScatterBegin(scatter, x, X, INSERT_VALUES, SCATTER_FORWARD);
585 CHKERR VecScatterEnd(scatter, x, X, INSERT_VALUES, SCATTER_FORWARD);
586 CHKERR MatMult(C, X, Cx);
588 CHKERR MatMult(CT, CCTm1Cx, CTCCTm1Cx);
589 CHKERR VecZeroEntries(f);
590 CHKERR VecGhostUpdateBegin(f, INSERT_VALUES, SCATTER_FORWARD);
591 CHKERR VecGhostUpdateEnd(f, INSERT_VALUES, SCATTER_FORWARD);
592 CHKERR VecScatterBegin(scatter, CTCCTm1Cx, f, INSERT_VALUES, SCATTER_REVERSE);
593 CHKERR VecScatterEnd(scatter, CTCCTm1Cx, f, INSERT_VALUES, SCATTER_REVERSE);
594
595 CHKERR event_scope.end();
597}
MoFEMErrorCode applyInverseCCT(Vec x, Vec f, bool transpose)

◆ applyQ()

MoFEMErrorCode MoFEM::ConstrainMatrixCtxImpl::applyQ ( Vec  x,
Vec  f 
)
overridevirtual

Apply Q = I - C^T S C for the supplied inverse action S.

Implements MoFEM::ConstrainMatrixCtx.

Definition at line 563 of file ConstrainMatrixCtx.cpp.

563 {
565 const auto &events = getProjectionEvents();
566 CHKERR PetscLogEventBegin(events.projectionQ, 0, 0, 0, 0);
567 LogEventScope event_scope(events.projectionQ, comm);
568
569 CHKERR applyQImpl(x, f, false);
570
571 CHKERR event_scope.end();
573}

◆ applyQImpl()

MoFEMErrorCode MoFEM::ConstrainMatrixCtxImpl::applyQImpl ( Vec  x,
Vec  f,
bool  transpose_inverse 
)
private

Definition at line 545 of file ConstrainMatrixCtx.cpp.

546 {
551 CHKERR VecCopy(x, f);
552 CHKERR VecScatterBegin(scatter, x, X, INSERT_VALUES, SCATTER_FORWARD);
553 CHKERR VecScatterEnd(scatter, x, X, INSERT_VALUES, SCATTER_FORWARD);
554 CHKERR MatMult(C, X, Cx);
555 CHKERR applyInverseCCT(Cx, CCTm1Cx, transpose_inverse);
556 CHKERR MatMult(CT, CCTm1Cx, CTCCTm1Cx);
557 CHKERR VecScale(CTCCTm1Cx, -1);
558 CHKERR VecScatterBegin(scatter, CTCCTm1Cx, f, ADD_VALUES, SCATTER_REVERSE);
559 CHKERR VecScatterEnd(scatter, CTCCTm1Cx, f, ADD_VALUES, SCATTER_REVERSE);
561}

◆ applyR()

MoFEMErrorCode MoFEM::ConstrainMatrixCtxImpl::applyR ( Vec  x,
Vec  f 
)
overridevirtual

Apply R = C^T S for the supplied inverse action S.

Implements MoFEM::ConstrainMatrixCtx.

Definition at line 599 of file ConstrainMatrixCtx.cpp.

599 {
601 const auto &events = getProjectionEvents();
602 CHKERR PetscLogEventBegin(events.projectionR, 0, 0, 0, 0);
603 LogEventScope event_scope(events.projectionR, comm);
604
606 MOFEM_LOG("SELF", Sev::error)
607 << "R requires initializeQorP() or a previously created Q/P matrix";
609 "Projection context is not initialized");
610 }
611 PetscInt constraint_local_size;
612 PetscInt constraint_global_size;
613 CHKERR VecGetLocalSize(Cx, &constraint_local_size);
614 CHKERR VecGetSize(Cx, &constraint_global_size);
615 CHKERR validateVectorLayout(x, constraint_local_size, constraint_global_size,
616 "R input");
618 CHKERR applyInverseCCT(x, CCTm1Cx, false);
619 CHKERR MatMult(CT, CCTm1Cx, CTCCTm1Cx);
620 CHKERR VecZeroEntries(f);
621 CHKERR VecGhostUpdateBegin(f, INSERT_VALUES, SCATTER_FORWARD);
622 CHKERR VecGhostUpdateEnd(f, INSERT_VALUES, SCATTER_FORWARD);
623 CHKERR VecScatterBegin(scatter, CTCCTm1Cx, f, INSERT_VALUES, SCATTER_REVERSE);
624 CHKERR VecScatterEnd(scatter, CTCCTm1Cx, f, INSERT_VALUES, SCATTER_REVERSE);
625
626 CHKERR event_scope.end();
628}

◆ applyRT()

MoFEMErrorCode MoFEM::ConstrainMatrixCtxImpl::applyRT ( Vec  x,
Vec  f 
)
overridevirtual

Apply R^T = S^T C for the supplied inverse action S.

Implements MoFEM::ConstrainMatrixCtx.

Definition at line 630 of file ConstrainMatrixCtx.cpp.

630 {
632 const auto &events = getProjectionEvents();
633 CHKERR PetscLogEventBegin(events.projectionRT, 0, 0, 0, 0);
634 LogEventScope event_scope(events.projectionRT, comm);
635
637 MOFEM_LOG("SELF", Sev::error)
638 << "R^T requires initializeQorP() or a previously created Q/P matrix";
640 "Projection context is not initialized");
641 }
642 PetscInt constraint_local_size;
643 PetscInt constraint_global_size;
644 CHKERR VecGetLocalSize(Cx, &constraint_local_size);
645 CHKERR VecGetSize(Cx, &constraint_global_size);
647 CHKERR validateVectorLayout(f, constraint_local_size, constraint_global_size,
648 "R^T output");
649 CHKERR VecScatterBegin(scatter, x, X, INSERT_VALUES, SCATTER_FORWARD);
650 CHKERR VecScatterEnd(scatter, x, X, INSERT_VALUES, SCATTER_FORWARD);
651 CHKERR MatMult(C, X, Cx);
652 CHKERR applyInverseCCT(Cx, f, true);
653
654 CHKERR event_scope.end();
656}

◆ getConstrainMatrix()

Mat MoFEM::ConstrainMatrixCtxImpl::getConstrainMatrix ( ) const
overridevirtual

Get the constraint matrix C.

Implements MoFEM::ConstrainMatrixCtx.

Definition at line 355 of file ConstrainMatrixCtx.cpp.

355{ return C.get(); }

◆ getInverseCCT()

Mat MoFEM::ConstrainMatrixCtxImpl::getInverseCCT ( ) const
overridevirtual

Get the supplied inverse action for C C^T.

Implements MoFEM::ConstrainMatrixCtx.

Definition at line 357 of file ConstrainMatrixCtx.cpp.

357{ return inverseCCT.get(); }

◆ getSystemMatrix()

Mat MoFEM::ConstrainMatrixCtxImpl::getSystemMatrix ( ) const
overridevirtual

Get the matrix K used by C^T C + Q^T K Q.

Implements MoFEM::ConstrainMatrixCtx.

Definition at line 367 of file ConstrainMatrixCtx.cpp.

367{ return K.get(); }

◆ initializeQorP()

MoFEMErrorCode MoFEM::ConstrainMatrixCtxImpl::initializeQorP ( Vec  x)
overridevirtual

Initialize matrices, vectors, and the scatter used by Q, P, R, and R^T. The operation is idempotent until the C/inverse-action pair is replaced.

Parameters
xVector defining the full problem layout.

Implements MoFEM::ConstrainMatrixCtx.

Definition at line 369 of file ConstrainMatrixCtx.cpp.

369 {
373
375 CHKERR validatePetscObject(getPetscObject(x), "full vector x");
377 CHKERR validatePetscObject(getPetscObject(xDm.get()), "full problem DM");
378 CHKERR validatePetscObject(getPetscObject(yDm.get()), "constraint DM");
379 } else {
381 "constraint scatter");
382 }
383
384 const auto &events = getProjectionEvents();
385 CHKERR PetscLogEventBegin(events.projectionInit, 0, 0, 0, 0);
386 LogEventScope event_scope(events.projectionInit, comm);
387
388 Mat raw_ct;
389 CHKERR MatTranspose(C, MAT_INITIAL_MATRIX, &raw_ct);
390 SmartPetscObj<Mat> new_ct(raw_ct);
391
392 Vec raw_x;
393 Vec raw_cx;
394 CHKERR MatCreateVecs(C, &raw_x, &raw_cx);
395 SmartPetscObj<Vec> new_x(raw_x);
396 SmartPetscObj<Vec> new_cx(raw_cx);
397
398 Vec raw_cctm1_cx;
399 CHKERR VecDuplicate(new_cx, &raw_cctm1_cx);
400 SmartPetscObj<Vec> new_cctm1_cx(raw_cctm1_cx);
401
402 Vec raw_ct_cctm1_cx;
403 CHKERR VecDuplicate(new_x, &raw_ct_cctm1_cx);
404 SmartPetscObj<Vec> new_ct_cctm1_cx(raw_ct_cctm1_cx);
405
406 SmartPetscObj<VecScatter> new_scatter = scatter;
408 MoFEM::Interface *x_m_field_ptr;
409 MoFEM::Interface *y_m_field_ptr;
410 CHKERR DMoFEMGetInterfacePtr(xDm, &x_m_field_ptr);
411 CHKERR DMoFEMGetInterfacePtr(yDm, &y_m_field_ptr);
412 if (x_m_field_ptr != y_m_field_ptr) {
413 MOFEM_LOG("SELF", Sev::error)
414 << "The full problem DM and constraint DM belong to different MoFEM "
415 "interfaces";
417 "DMs must belong to the same MoFEM interface");
418 }
419
420 const MoFEM::Problem *x_problem_ptr;
421 const MoFEM::Problem *y_problem_ptr;
422 CHKERR DMMoFEMGetProblemPtr(xDm, &x_problem_ptr);
423 CHKERR DMMoFEMGetProblemPtr(yDm, &y_problem_ptr);
424 CHKERR x_m_field_ptr->getInterface<VecManager>()->vecScatterCreate(
425 x, x_problem_ptr->getName(), ROW, new_x, y_problem_ptr->getName(), COL,
426 new_scatter);
427 }
428
429 PetscInt new_full_local_size;
430 PetscInt new_full_global_size;
431 CHKERR VecGetLocalSize(x, &new_full_local_size);
432 CHKERR VecGetSize(x, &new_full_global_size);
433
434 PetscInt c_rows;
435 PetscInt c_columns;
436 PetscInt inverse_cct_rows;
437 PetscInt inverse_cct_columns;
438 CHKERR MatGetSize(C, &c_rows, &c_columns);
439 CHKERR MatGetSize(inverseCCT, &inverse_cct_rows, &inverse_cct_columns);
440 MOFEM_LOG("WORLD", Sev::verbose)
441 << "Constraint projection initialized: C size (" << c_rows << ", "
442 << c_columns << "), inverse C C^T action size (" << inverse_cct_rows
443 << ", " << inverse_cct_columns << "), full vector size "
444 << new_full_global_size;
445
446 CT = new_ct;
447 X = new_x;
448 Cx = new_cx;
449 CCTm1Cx = new_cctm1_cx;
450 CTCCTm1Cx = new_ct_cctm1_cx;
451 scatter = new_scatter;
452 fullLocalSize = new_full_local_size;
453 fullGlobalSize = new_full_global_size;
455
456 CHKERR event_scope.end();
458}
@ COL
@ ROW
PetscErrorCode DMMoFEMGetProblemPtr(DM dm, const MoFEM::Problem **problem_ptr)
Get pointer to problem data structure.
Definition DMMoFEM.cpp:451
PetscErrorCode DMoFEMGetInterfacePtr(DM dm, MoFEM::Interface **m_field_ptr)
Get pointer to MoFEM::Interface.
Definition DMMoFEM.cpp:439
const FTensor::Tensor2< T, Dim, Dim > Vec
PetscObject getPetscObject(T obj)
MoFEMErrorCode validateInverseCCTLayout(Mat constrain_matrix, Mat inverse_cct) const
MoFEMErrorCode validatePetscObject(PetscObject object, const char *object_name) const
Deprecated interface functions.
keeps basic data about problem
MoFEMErrorCode getInterface(IFACE *&iface) const
Get interface reference to pointer of interface.

◆ initializeQTKQ()

MoFEMErrorCode MoFEM::ConstrainMatrixCtxImpl::initializeQTKQ ( )
overridevirtual

Initialize matrices and work vectors used by C^T C + Q^T K Q. The operation is idempotent until C or K is replaced.

Implements MoFEM::ConstrainMatrixCtx.

Definition at line 472 of file ConstrainMatrixCtx.cpp.

472 {
474 if (qtkqInitialized)
476
477 CHKERR validatePetscObject(getPetscObject(C.get()), "constraint matrix C");
479
480 const auto &events = getProjectionEvents();
481 CHKERR PetscLogEventBegin(events.projectionInit, 0, 0, 0, 0);
482 LogEventScope event_scope(events.projectionInit, comm);
483
484 Mat raw_ctc;
485 CHKERR MatTransposeMatMult(C, C, MAT_INITIAL_MATRIX, PETSC_DEFAULT, &raw_ctc);
486 SmartPetscObj<Mat> new_ctc(raw_ctc);
487
488 Vec raw_qx;
489 Vec raw_kqx;
490 CHKERR MatCreateVecs(K, &raw_qx, &raw_kqx);
491 SmartPetscObj<Vec> new_qx(raw_qx);
492 SmartPetscObj<Vec> new_kqx(raw_kqx);
493
494 Vec raw_ctcx;
495 CHKERR MatCreateVecs(new_ctc, nullptr, &raw_ctcx);
496 SmartPetscObj<Vec> new_ctcx(raw_ctcx);
497
498 PetscInt ctc_rows;
499 PetscInt ctc_columns;
500 CHKERR MatGetSize(new_ctc, &ctc_rows, &ctc_columns);
501 MOFEM_LOG("WORLD", Sev::verbose)
502 << "Initialized C^T C + Q^T K Q work data: C^T C size (" << ctc_rows
503 << ", " << ctc_columns << ")";
504
505 CTC = new_ctc;
506 Qx = new_qx;
507 KQx = new_kqx;
508 CTCx = new_ctcx;
509 qtkqInitialized = true;
510
511 CHKERR event_scope.end();
513}

◆ invalidateProjectionData()

MoFEMErrorCode MoFEM::ConstrainMatrixCtxImpl::invalidateProjectionData ( )
private

Definition at line 329 of file ConstrainMatrixCtx.cpp.

329 {
331 projectionInitialized = false;
332 CT.reset();
333 Cx.reset();
334 CCTm1Cx.reset();
335 CTCCTm1Cx.reset();
336 X.reset();
337 fullLocalSize = 0;
338 fullGlobalSize = 0;
340 scatter.reset();
343}

◆ invalidateSystemData()

MoFEMErrorCode MoFEM::ConstrainMatrixCtxImpl::invalidateSystemData ( )
private

Definition at line 319 of file ConstrainMatrixCtx.cpp.

319 {
321 qtkqInitialized = false;
322 CTC.reset();
323 CTCx.reset();
324 Qx.reset();
325 KQx.reset();
327}

◆ recalculateCT()

MoFEMErrorCode MoFEM::ConstrainMatrixCtxImpl::recalculateCT ( )
overridevirtual

Recalculate C^T after an in-place value change in C. The layout and nonzero structure of C must be unchanged. The caller must update the supplied inverse action in the same transaction. Projection MATSHELL objects already created from this context do not receive a PETSc state increment; recreate them before passing the changed operator to a consumer which caches by matrix state.

Implements MoFEM::ConstrainMatrixCtx.

Definition at line 460 of file ConstrainMatrixCtx.cpp.

460 {
464
466 Mat raw_mat = CT.get();
467 CHKERR MatTranspose(C, MAT_REUSE_MATRIX, &raw_mat);
468 MOFEM_LOG("WORLD", Sev::verbose) << "Recalculated C^T";
470}

◆ recalculateCTC()

MoFEMErrorCode MoFEM::ConstrainMatrixCtxImpl::recalculateCTC ( )
overridevirtual

Recalculate C^T C after an in-place value change in C. The layout and nonzero structure of C must be unchanged.

Implements MoFEM::ConstrainMatrixCtx.

Definition at line 515 of file ConstrainMatrixCtx.cpp.

515 {
517 if (!qtkqInitialized)
519 Mat raw_mat = CTC.get();
520 CHKERR MatTransposeMatMult(C, C, MAT_REUSE_MATRIX, PETSC_DEFAULT, &raw_mat);
521 MOFEM_LOG("WORLD", Sev::verbose) << "Recalculated C^T C";
523}

◆ setConstrainMatrix()

MoFEMErrorCode MoFEM::ConstrainMatrixCtxImpl::setConstrainMatrix ( Mat  constrain_matrix,
Mat  inverse_cct 
)
overridevirtual

Set the constraint matrix C and its row-space inverse action atomically.

inverse_cct represents (C C^T)^-1 on the selected constraint space, or the corresponding embedded generalized inverse for a masked full space. It must have the row layout of C in both dimensions and support MatMult(). MatMultTranspose() is additionally required by applyRT() and applyCTC_QTKQ(). Both PETSc objects are retained.

All data derived from the previous pair are invalidated. For in-place value changes which preserve layouts and sparsity, update inverse_cct in the same transaction and call recalculateCT() and recalculateCTC().

Implements MoFEM::ConstrainMatrixCtx.

Definition at line 345 of file ConstrainMatrixCtx.cpp.

346 {
348 CHKERR validateInverseCCTLayout(constrain_matrix, inverse_cct);
349 C = SmartPetscObj<Mat>(constrain_matrix, true);
350 inverseCCT = SmartPetscObj<Mat>(inverse_cct, true);
353}

◆ setSystemMatrix()

MoFEMErrorCode MoFEM::ConstrainMatrixCtxImpl::setSystemMatrix ( Mat  system_matrix)
overridevirtual

Set the matrix K used by C^T C + Q^T K Q and retain a PETSc reference. Work vectors derived from the previous K are invalidated.

Implements MoFEM::ConstrainMatrixCtx.

Definition at line 359 of file ConstrainMatrixCtx.cpp.

359 {
361 CHKERR validatePetscObject(getPetscObject(system_matrix), "system matrix K");
362 K = SmartPetscObj<Mat>(system_matrix, true);
365}

◆ validateInverseCCTLayout()

MoFEMErrorCode MoFEM::ConstrainMatrixCtxImpl::validateInverseCCTLayout ( Mat  constrain_matrix,
Mat  inverse_cct 
) const
private

Definition at line 236 of file ConstrainMatrixCtx.cpp.

237 {
239 CHKERR validatePetscObject(getPetscObject(constrain_matrix),
240 "constraint matrix C");
242 "inverse C C^T action");
243
244 PetscInt c_local_rows;
245 PetscInt c_local_columns;
246 PetscInt c_global_rows;
247 PetscInt c_global_columns;
248 PetscInt inverse_local_rows;
249 PetscInt inverse_local_columns;
250 PetscInt inverse_global_rows;
251 PetscInt inverse_global_columns;
252 CHKERR MatGetLocalSize(constrain_matrix, &c_local_rows, &c_local_columns);
253 CHKERR MatGetSize(constrain_matrix, &c_global_rows, &c_global_columns);
254 CHKERR MatGetLocalSize(inverse_cct, &inverse_local_rows,
255 &inverse_local_columns);
256 CHKERR MatGetSize(inverse_cct, &inverse_global_rows, &inverse_global_columns);
257 if (inverse_local_rows != c_local_rows ||
258 inverse_local_columns != c_local_rows ||
259 inverse_global_rows != c_global_rows ||
260 inverse_global_columns != c_global_rows) {
261 MOFEM_LOG("SELF", Sev::error)
262 << "Inverse C C^T layout, local (" << inverse_local_rows << ", "
263 << inverse_local_columns << "), global (" << inverse_global_rows << ", "
264 << inverse_global_columns
265 << "), does not match the row layout of C, local " << c_local_rows
266 << ", global " << c_global_rows;
268 "Inverse C C^T action has an incompatible layout");
269 }
270
271 PetscBool has_mult;
272 CHKERR MatHasOperation(inverse_cct, MATOP_MULT, &has_mult);
273 if (!has_mult)
275 "Inverse C C^T action does not implement MatMult");
276
277 PetscObject associated_c = nullptr;
278 CHKERR PetscObjectQuery(getPetscObject(inverse_cct),
279 inverseCCTConstraintMatrixKey, &associated_c);
280 if (associated_c && associated_c != getPetscObject(constrain_matrix)) {
281 MOFEM_LOG("SELF", Sev::error)
282 << "KSP-backed inverse C C^T action was created for a different "
283 "constraint matrix";
285 "Inverse C C^T action is associated with a different C");
286 }
288}

◆ validatePetscObject()

MoFEMErrorCode MoFEM::ConstrainMatrixCtxImpl::validatePetscObject ( PetscObject  object,
const char *  object_name 
) const
private

Definition at line 192 of file ConstrainMatrixCtx.cpp.

193 {
195 if (!object) {
196 MOFEM_LOG("SELF", Sev::error)
197 << "Null PETSc object supplied for " << object_name;
198 SETERRQ(comm, MOFEM_DATA_INCONSISTENCY, "Null PETSc object");
199 }
200
201 MPI_Comm object_comm;
202 CHKERR PetscObjectGetComm(object, &object_comm);
203 int relation;
204 CHKERR MPI_Comm_compare(comm, object_comm, &relation);
205 if (relation > MPI_CONGRUENT) {
206 MOFEM_LOG("SELF", Sev::error)
207 << object_name << " uses a communicator incompatible with the context";
209 "PETSc object uses an incompatible communicator");
210 }
212}

◆ validateSystemMatrixLayout()

MoFEMErrorCode MoFEM::ConstrainMatrixCtxImpl::validateSystemMatrixLayout ( ) const
private

Definition at line 290 of file ConstrainMatrixCtx.cpp.

290 {
292 CHKERR validatePetscObject(getPetscObject(K.get()), "system matrix K");
293 PetscInt local_rows;
294 PetscInt local_columns;
295 PetscInt global_rows;
296 PetscInt global_columns;
297 CHKERR MatGetLocalSize(K.get(), &local_rows, &local_columns);
298 CHKERR MatGetSize(K.get(), &global_rows, &global_columns);
299 if (local_rows != local_columns || global_rows != global_columns) {
300 MOFEM_LOG("SELF", Sev::error)
301 << "System matrix K must be square, local size (" << local_rows << ", "
302 << local_columns << "), global size (" << global_rows << ", "
303 << global_columns << ")";
305 "System matrix K must have matching row and column layouts");
306 }
308 (local_rows != fullLocalSize || global_rows != fullGlobalSize)) {
309 MOFEM_LOG("SELF", Sev::error)
310 << "System matrix K layout (" << local_rows << ", " << global_rows
311 << ") does not match the full vector layout (" << fullLocalSize << ", "
312 << fullGlobalSize << ")";
314 "System matrix K is incompatible with the full vector layout");
315 }
317}

◆ validateVectorLayout()

MoFEMErrorCode MoFEM::ConstrainMatrixCtxImpl::validateVectorLayout ( Vec  vec,
PetscInt  local_size,
PetscInt  global_size,
const char *  vector_name 
) const
private

Definition at line 215 of file ConstrainMatrixCtx.cpp.

217 {
219 CHKERR validatePetscObject(getPetscObject(vec), vector_name);
220 PetscInt actual_local_size;
221 PetscInt actual_global_size;
222 CHKERR VecGetLocalSize(vec, &actual_local_size);
223 CHKERR VecGetSize(vec, &actual_global_size);
224 if (actual_local_size != local_size || actual_global_size != global_size) {
225 MOFEM_LOG("SELF", Sev::error)
226 << vector_name << " layout is (" << actual_local_size << ", "
227 << actual_global_size << "), expected (" << local_size << ", "
228 << global_size << ")";
230 "Vector layout is incompatible with the projection context");
231 }
233}

Member Data Documentation

◆ buildScatterFromDms

bool MoFEM::ConstrainMatrixCtxImpl::buildScatterFromDms = true
private

Definition at line 174 of file ConstrainMatrixCtx.cpp.

◆ C

SmartPetscObj<Mat> MoFEM::ConstrainMatrixCtxImpl::C
private

Definition at line 158 of file ConstrainMatrixCtx.cpp.

◆ CCTm1Cx

SmartPetscObj<Vec> MoFEM::ConstrainMatrixCtxImpl::CCTm1Cx
private

Definition at line 164 of file ConstrainMatrixCtx.cpp.

◆ comm

MPI_Comm MoFEM::ConstrainMatrixCtxImpl::comm
private

Definition at line 154 of file ConstrainMatrixCtx.cpp.

◆ CT

SmartPetscObj<Mat> MoFEM::ConstrainMatrixCtxImpl::CT
private

Definition at line 159 of file ConstrainMatrixCtx.cpp.

◆ CTC

SmartPetscObj<Mat> MoFEM::ConstrainMatrixCtxImpl::CTC
private

Definition at line 161 of file ConstrainMatrixCtx.cpp.

◆ CTCCTm1Cx

SmartPetscObj<Vec> MoFEM::ConstrainMatrixCtxImpl::CTCCTm1Cx
private

Definition at line 165 of file ConstrainMatrixCtx.cpp.

◆ CTCx

SmartPetscObj<Vec> MoFEM::ConstrainMatrixCtxImpl::CTCx
private

Definition at line 166 of file ConstrainMatrixCtx.cpp.

◆ Cx

SmartPetscObj<Vec> MoFEM::ConstrainMatrixCtxImpl::Cx
private

Definition at line 163 of file ConstrainMatrixCtx.cpp.

◆ fullGlobalSize

PetscInt MoFEM::ConstrainMatrixCtxImpl::fullGlobalSize = 0
private

Definition at line 177 of file ConstrainMatrixCtx.cpp.

◆ fullLocalSize

PetscInt MoFEM::ConstrainMatrixCtxImpl::fullLocalSize = 0
private

Definition at line 176 of file ConstrainMatrixCtx.cpp.

◆ inverseCCT

SmartPetscObj<Mat> MoFEM::ConstrainMatrixCtxImpl::inverseCCT
private

Definition at line 160 of file ConstrainMatrixCtx.cpp.

◆ K

SmartPetscObj<Mat> MoFEM::ConstrainMatrixCtxImpl::K
private

Definition at line 162 of file ConstrainMatrixCtx.cpp.

◆ KQx

SmartPetscObj<Vec> MoFEM::ConstrainMatrixCtxImpl::KQx
private

Definition at line 169 of file ConstrainMatrixCtx.cpp.

◆ projectionInitialized

bool MoFEM::ConstrainMatrixCtxImpl::projectionInitialized = false
private

Definition at line 172 of file ConstrainMatrixCtx.cpp.

◆ qtkqInitialized

bool MoFEM::ConstrainMatrixCtxImpl::qtkqInitialized = false
private

Definition at line 173 of file ConstrainMatrixCtx.cpp.

◆ Qx

SmartPetscObj<Vec> MoFEM::ConstrainMatrixCtxImpl::Qx
private

Definition at line 168 of file ConstrainMatrixCtx.cpp.

◆ scatter

SmartPetscObj<VecScatter> MoFEM::ConstrainMatrixCtxImpl::scatter
private

Definition at line 170 of file ConstrainMatrixCtx.cpp.

◆ X

SmartPetscObj<Vec> MoFEM::ConstrainMatrixCtxImpl::X
private

Definition at line 167 of file ConstrainMatrixCtx.cpp.

◆ xDm

SmartPetscObj<DM> MoFEM::ConstrainMatrixCtxImpl::xDm
private

Definition at line 155 of file ConstrainMatrixCtx.cpp.

◆ yDm

SmartPetscObj<DM> MoFEM::ConstrainMatrixCtxImpl::yDm
private

Definition at line 156 of file ConstrainMatrixCtx.cpp.


The documentation for this struct was generated from the following file: