v0.16.3
Loading...
Searching...
No Matches
operators_tester_direction.cpp
Go to the documentation of this file.
1/**
2 * \file operators_tester_direction.cpp
3 * \example mofem/atom_tests/operators_tester_direction.cpp
4 *
5 * \brief Check OperatorsTester::setDirectionFromFunction
6 *
7 * The direction is a smooth function of the vertex positions, defined here.
8 *
9 * The whole mesh is loaded on every rank and the problem is partitioned
10 * afterwards, so each rank can compute reference values from all mesh
11 * vertices, independently of the DOF partition and numbering. Equal
12 * references on one and two ranks show that the direction does not depend
13 * on the partition.
14 *
15 * Tests (select with -test_nb or -test_name):
16 * 1. values - owned vertex DOFs equal the function, other DOFs are zero
17 * 2. norm - vector norm equals the sum over all mesh vertices
18 * 3. restricted - with ents, only DOFs on those entities are set
19 * 4. ghosts - ghost values equal the function at the ghost entity
20 */
21
22#include <MoFEM.hpp>
23
24using namespace MoFEM;
25
26static char help[] = "Partition-independent test directions\n\n";
27
28namespace {
29
30constexpr int SPACE_DIM = 3;
31constexpr TestingInterface::Tolerance TOL{1e-12, 1e-12};
32constexpr double LENGTH = 0.7;
33
34/** Smooth direction on vertex DOFs, zero on other entities.
35 * Coefficient c of a vertex DOF at X gets
36 * a_c + sum_k b_ck sin(k_ck . X / length + phi_ck), so |value| < 1. With
37 * length of the order of the body size the direction varies slowly over an
38 * element; on H1 fields it is the linear interpolation of the vertex values.
39 */
40OperatorsTester::DirectionFunction smoothVertexDirection(const double length) {
41 return [length](const NumeredDofEntity &dof,
42 const std::array<double, 3> &xyz) {
43 if (type_from_handle(dof.getEnt()) != MBVERTEX)
44 return 0.;
45 // Fixed modes; amplitudes sum to less than one for every coefficient.
46 constexpr double a[3] = {0.20, -0.15, 0.10};
47 constexpr double b[3][2] = {{0.45, 0.25}, {0.40, 0.30}, {0.35, 0.35}};
48 constexpr double k[3][2][3] = {
49 {{1.3, -0.7, 0.9}, {-0.4, 1.1, 1.7}},
50 {{0.8, 1.5, -0.6}, {1.9, -0.3, 0.5}},
51 {{-1.2, 0.6, 1.4}, {0.7, 1.8, -0.9}}};
52 constexpr double phi[3][2] = {{0.3, 1.1}, {-0.8, 0.4}, {1.3, -0.2}};
53 const int c = dof.getDofCoeffIdx() % 3;
54 double value = a[c];
55 for (int m = 0; m != 2; ++m) {
56 double arg = phi[c][m];
57 for (int d = 0; d != 3; ++d)
58 arg += k[c][m][d] * xyz[d] / length;
59 value += b[c][m] * std::sin(arg);
60 }
61 return value;
62 };
63}
64
65} // namespace
66
67int main(int argc, char *argv[]) {
68
69 MoFEM::Core::Initialize(&argc, &argv, (char *)0, help);
70
71 try {
72
73 moab::Core moab_core;
74 moab::Interface &moab = moab_core;
75 MoFEM::Core mofem_core(moab);
76 MoFEM::Interface &m_field = mofem_core;
77 CHKERR DMRegister_MoFEM("DMMOFEM");
78
79 auto simple = m_field.getInterface<Simple>();
81 // Whole mesh on every rank; the problem is partitioned by setUp.
82 CHKERR simple->loadFile("", "");
83 CHKERR simple->addDomainField("U", H1, AINSWORTH_LEGENDRE_BASE, SPACE_DIM);
84 CHKERR simple->addDomainField("S", H1, AINSWORTH_LEGENDRE_BASE, 1);
85 CHKERR simple->setFieldOrder("U", 2);
86 CHKERR simple->setFieldOrder("S", 1);
87 CHKERR simple->setUp(PETSC_FALSE);
88
89 auto dm = simple->getDM();
90 auto opt = m_field.getInterface<OperatorsTester>();
91 auto testing = m_field.getInterface<TestingInterface>();
92 const auto function = smoothVertexDirection(LENGTH);
93 const auto u_bit = m_field.get_field_bit_number("U");
94
95 // Corner vertices of the domain; the file may hold other vertices.
96 Range domain, vertices;
97 CHKERR moab.get_entities_by_dimension(0, SPACE_DIM, domain);
98 CHKERR moab.get_connectivity(domain, vertices, true);
99
100 auto check_values = [&](SmartPetscObj<Vec> v, boost::shared_ptr<Range> ents,
101 bool ghosts) {
103 int size, ghost_size;
104 CHKERR VecGetLocalSize(v, &size);
105 Vec local;
106 CHKERR VecGhostGetLocalForm(v, &local);
107 CHKERR VecGetLocalSize(local, &ghost_size);
108 const double *array;
109 CHKERR VecGetArrayRead(local, &array);
110 double error = 0;
111 int checked = 0;
112 for (auto &dof : *getProblemPtr(dm)->getNumeredColDofsPtr()) {
113 const auto idx = dof->getPetscLocalDofIdx();
114 if (idx < 0 || (ghosts ? (idx < size || idx >= ghost_size)
115 : idx >= size))
116 continue;
117 const auto ent = dof->getEnt();
118 double expected = 0;
119 const bool in_ents = !ents || ents->find(ent) != ents->end();
120 if (dof->getBitNumber() == u_bit && in_ents) {
121 std::array<double, 3> xyz{0., 0., 0.};
122 if (type_from_handle(ent) == MBVERTEX)
123 CHKERR moab.get_coords(&ent, 1, xyz.data());
124 expected = function(*dof, xyz);
125 }
126 error = std::max(error, std::abs(array[idx] - expected));
127 ++checked;
128 }
129 CHKERR VecRestoreArrayRead(local, &array);
130 CHKERR VecGhostRestoreLocalForm(v, &local);
131 CHKERR testing->checkError<TestType::POINTWISE>(
132 ghosts ? "ghost values" : "owned values", error, 1., TOL);
133 int global_checked;
134 CHKERR MPI_Allreduce(&checked, &global_checked, 1, MPI_INT, MPI_SUM,
135 m_field.get_comm());
136 // One rank has no ghosts; on several ranks the H1 field must have some.
137 if (!global_checked && (!ghosts || m_field.get_comm_size() > 1))
138 SETERRQ(m_field.get_comm(), MOFEM_ATOM_TEST_INVALID,
139 "No DOFs checked");
141 };
142
143 // smoothVertexDirection depends on the DOF only through its entity type
144 // and coefficient index, so one vertex DOF per coefficient evaluates it
145 // at any position. Every rank has vertex DOFs of U in its local problem.
146 std::array<boost::shared_ptr<NumeredDofEntity>, SPACE_DIM> coeff_dofs;
147 for (auto &dof : *getProblemPtr(dm)->getNumeredColDofsPtr())
148 if (dof->getBitNumber() == u_bit &&
149 type_from_handle(dof->getEnt()) == MBVERTEX)
150 coeff_dofs[dof->getDofCoeffIdx()] = dof;
151 for (auto &dof : coeff_dofs)
152 if (!dof)
153 SETERRQ(PETSC_COMM_SELF, MOFEM_ATOM_TEST_INVALID,
154 "Missing vertex DOF of U in the local problem");
155
156 // Reference from mesh vertices only: no DOF numbering involved.
157 auto reference_norm2 = [&](const Range &verts) {
158 double sum = 0;
159 for (auto vertex : verts) {
160 std::array<double, 3> xyz;
161 CHK_MOAB_THROW(moab.get_coords(&vertex, 1, xyz.data()), "Get coords");
162 for (int c = 0; c != SPACE_DIM; ++c) {
163 const double value = function(*coeff_dofs[c], xyz);
164 sum += value * value;
165 }
166 }
167 return sum;
168 };
169
170 auto check_norm = [&](SmartPetscObj<Vec> v, const Range &verts,
171 const std::string &name) {
173 double norm;
174 CHKERR VecNorm(v, NORM_2, &norm);
175 const double expected = reference_norm2(verts);
176 CHKERR testing->checkValue<TestType::SCALAR>(name, norm * norm, expected,
177 TOL);
178 MOFEM_LOG("WORLD", Sev::inform)
179 << name << " squared norm " << std::setprecision(15) << norm * norm;
181 };
182
183 Range skin_verts;
184 {
185 Skinner skin(&moab);
186 Range faces;
187 CHKERR skin.find_skin(0, domain, false, faces);
188 CHKERR moab.get_connectivity(faces, skin_verts, true);
189 }
190 auto skin_ptr = boost::make_shared<Range>(skin_verts);
191 // Edges too, to check that only vertex DOFs are non-zero.
192 {
193 Range skin_edges;
194 CHKERR moab.get_adjacencies(skin_verts, 1, false, skin_edges,
195 moab::Interface::UNION);
196 skin_ptr->merge(skin_edges);
197 }
198
199 CHKERR testing->addTestCommandLine("", 1, "values", [&]() {
201 auto v = opt->setDirectionFromFunction(dm, {"U"}, function);
202 CHKERR check_values(v, nullptr, false);
204 });
205 CHKERR testing->addTestCommandLine("", 2, "norm", [&]() {
207 auto v = opt->setDirectionFromFunction(dm, {"U"}, function);
208 CHKERR check_norm(v, vertices, "all vertices");
210 });
211 CHKERR testing->addTestCommandLine("", 3, "restricted", [&]() {
213 auto v = opt->setDirectionFromFunction(dm, {"U"}, function, skin_ptr);
214 CHKERR check_values(v, skin_ptr, false);
215 CHKERR check_norm(v, skin_verts, "skin vertices");
217 });
218 CHKERR testing->addTestCommandLine("", 4, "ghosts", [&]() {
220 auto v = opt->setDirectionFromFunction(dm, {"U"}, function);
221 CHKERR check_values(v, nullptr, true);
223 });
224 CHKERR testing->runTests();
225 }
227
229 return 0;
230}
std::vector< std::array< EntityHandle, 3 > > faces
Sorted keys.
FaceSet skin
std::vector< int > size
double length
std::vector< int > vertices
std::string name
void simple(double P1[], double P2[], double P3[], double c[], const int N)
Definition acoustic.cpp:69
int main()
constexpr double a
constexpr int SPACE_DIM
#define CATCH_ERRORS
Catch errors.
@ AINSWORTH_LEGENDRE_BASE
Ainsworth Cole (Legendre) approx. base .
Definition definitions.h:60
@ H1
continuous field
Definition definitions.h:85
#define MoFEMFunctionBegin
First executable line of each MoFEM function, used for error handling. Final line of MoFEM functions ...
#define CHK_MOAB_THROW(err, msg)
Check error code of MoAB function and throw MoFEM exception.
@ MOFEM_ATOM_TEST_INVALID
Definition definitions.h:40
#define MoFEMFunctionReturn(a)
Last executable line of each PETSc function used for error handling. Replaces return()
#define CHKERR
Inline error check.
PetscErrorCode DMRegister_MoFEM(const char sname[])
Register MoFEM problem.
Definition DMMoFEM.cpp:43
#define MOFEM_LOG(channel, severity)
Log.
PetscBool testing
const double c
speed of light (cm/ns)
const double v
phase velocity of light in medium (cm/ns)
FTensor::Index< 'k', 3 > k
implementation of Data Operators for Forces and Sources
Definition Common.hpp:10
auto type_from_handle(const EntityHandle h)
get type from entity handle
auto getProblemPtr(DM dm)
get problem pointer from DM
Definition DMMoFEM.hpp:1224
static char help[]
static double phi
FTensor::Index< 'm', 3 > m
virtual int get_comm_size() const =0
virtual FieldBitNumber get_field_bit_number(const std::string name) const =0
get field bit number
virtual MPI_Comm & get_comm() const =0
Core (interface) class.
Definition Core.hpp:83
static MoFEMErrorCode Initialize(int *argc, char ***args, const char file[], const char help[])
Initializes the MoFEM database PETSc, MOAB and MPI.
Definition Core.cpp:68
static MoFEMErrorCode Finalize()
Checks for options to be called at the conclusion of the program.
Definition Core.cpp:123
Deprecated interface functions.
keeps information about indexed dofs for the problem
Calculate directional derivative of the right hand side and compare it with tangent matrix derivative...
std::function< double(const NumeredDofEntity &dof, const std::array< double, 3 > &xyz)> DirectionFunction
Value of a test direction for one DOF.
Simple interface for fast problem set-up.
Definition Simple.hpp:27
MoFEMErrorCode getOptions()
get options
Definition Simple.cpp:180
intrusive_ptr for managing petsc objects
MoFEMErrorCode getInterface(IFACE *&iface) const
Get interface reference to pointer of interface.
FieldCoefficientsNumber getDofCoeffIdx() const