v0.13.0
AuxPoissonFunctions.hpp
Go to the documentation of this file.
1 /**
2  * \file AuxPoissonFunctions.hpp
3  * \example AuxPoissonFunctions.hpp
4  *
5  */
6 
7 /* This file is part of MoFEM.
8  * MoFEM is free software: you can redistribute it and/or modify it under
9  * the terms of the GNU Lesser General Public License as published by the
10  * Free Software Foundation, either version 3 of the License, or (at your
11  * option) any later version.
12  *
13  * MoFEM is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
16  * License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with MoFEM. If not, see <http://www.gnu.org/licenses/>. */
20 
21 #ifndef ___AUX_FUNCTIONS_HPP__
22 #define ___AUX_FUNCTIONS_HPP__
23 
24 namespace PoissonExample {
25 
26 struct AuxFunctions {
27 
29  : cOmm(m_field.get_comm()), rAnk(m_field.get_comm_rank()) {}
30 
31  /**
32  * Create ghost vector to assemble errors from all element on distributed
33  mesh.
34  * Ghost vector has size 1, where one element is owned by processor 0, other
35  processor
36  * have one ghost element of zero element at processor 0.
37 
38  * [createGhostVec description]
39  * @param ghost_vec pointer to created ghost vector
40  * @return error code
41  */
42  MoFEMErrorCode createGhostVec(Vec *ghost_vec) const {
43 
45  int ghosts[] = {0};
46  int nb_locals = rAnk == 0 ? 1 : 0;
47  int nb_ghosts = rAnk > 0 ? 1 : 0;
48  CHKERR VecCreateGhost(cOmm, nb_locals, 1, nb_ghosts, ghosts, ghost_vec);
50  }
51 
52  /**
53  * \brief Assemble error vector
54  */
56 
58  CHKERR VecAssemblyBegin(ghost_vec);
59  CHKERR VecAssemblyEnd(ghost_vec);
60  // accumulate errors from processors
61  CHKERR VecGhostUpdateBegin(ghost_vec, ADD_VALUES, SCATTER_REVERSE);
62  CHKERR VecGhostUpdateEnd(ghost_vec, ADD_VALUES, SCATTER_REVERSE);
63  // scatter errors to all processors
64  CHKERR VecGhostUpdateBegin(ghost_vec, INSERT_VALUES, SCATTER_FORWARD);
65  CHKERR VecGhostUpdateEnd(ghost_vec, INSERT_VALUES, SCATTER_FORWARD);
67  }
68 
69  /**
70  * \brief Print error
71  */
73 
75  double *e;
76  CHKERR VecGetArray(ghost_vec, &e);
77  CHKERR PetscPrintf(cOmm, "Approximation error %4.3e\n", sqrt(e[0]));
78  CHKERR VecRestoreArray(ghost_vec, &e);
80  }
81 
82  /**
83  * \brief Test error
84  */
86 
88  double *e;
89  CHKERR VecGetArray(ghost_vec, &e);
90  // Check if error is zero, otherwise throw error
91  const double eps = 1e-8;
92  if ((sqrt(e[0]) > eps) || (!boost::math::isnormal(e[0]))) {
93  SETERRQ(PETSC_COMM_SELF, MOFEM_ATOM_TEST_INVALID,
94  "Test failed, error too big");
95  }
96  CHKERR VecRestoreArray(ghost_vec, &e);
98  }
99 
100 private:
101  MPI_Comm cOmm;
102  const int rAnk;
103 };
104 
105 } // namespace PoissonExample
106 
107 #endif //___AUX_FUNCTIONS_HPP__
static const double eps
#define MoFEMFunctionBegin
First executable line of each MoFEM function, used for error handling. Final line of MoFEM functions ...
Definition: definitions.h:359
@ MOFEM_ATOM_TEST_INVALID
Definition: definitions.h:53
#define MoFEMFunctionReturn(a)
Last executable line of each PETSc function used for error handling. Replaces return()
Definition: definitions.h:429
#define CHKERR
Inline error check.
Definition: definitions.h:548
const FTensor::Tensor2< T, Dim, Dim > Vec
PetscErrorCode MoFEMErrorCode
MoFEM/PETSc error code.
Definition: Exceptions.hpp:67
Deprecated interface functions.
MoFEMErrorCode createGhostVec(Vec *ghost_vec) const
MoFEMErrorCode testError(Vec ghost_vec)
Test error.
AuxFunctions(const MoFEM::Interface &m_field)
MoFEMErrorCode assembleGhostVector(Vec ghost_vec) const
Assemble error vector.
MoFEMErrorCode printError(Vec ghost_vec)
Print error.