v0.16.3
Loading...
Searching...
No Matches
Functions | Variables
operators_tester_direction.cpp File Reference
#include <MoFEM.hpp>

Go to the source code of this file.

Functions

int main (int argc, char *argv[])
 

Variables

static char help [] = "Partition-independent test directions\n\n"
 

Function Documentation

◆ main()

int main ( int  argc,
char *  argv[] 
)

Definition at line 67 of file operators_tester_direction.cpp.

67 {
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
std::vector< int > vertices
std::string name
void simple(double P1[], double P2[], double P3[], double c[], const int N)
Definition acoustic.cpp:69
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)
const FTensor::Tensor2< T, Dim, Dim > Vec
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[]
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.
Calculate directional derivative of the right hand side and compare it with tangent matrix derivative...
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.

Variable Documentation

◆ help

char help[] = "Partition-independent test directions\n\n"
static

Definition at line 26 of file operators_tester_direction.cpp.