v0.16.3
Loading...
Searching...
No Matches
mofem/atom_tests/operators_tester_direction.cpp

Check OperatorsTester::setDirectionFromFunction.

Check OperatorsTester::setDirectionFromFunctionThe direction is a smooth function of the vertex positions, defined here.

The whole mesh is loaded on every rank and the problem is partitioned afterwards, so each rank can compute reference values from all mesh vertices, independently of the DOF partition and numbering. Equal references on one and two ranks show that the direction does not depend on the partition.

Tests (select with -test_nb or -test_name):

  1. values - owned vertex DOFs equal the function, other DOFs are zero
  2. norm - vector norm equals the sum over all mesh vertices
  3. restricted - with ents, only DOFs on those entities are set
  4. ghosts - ghost values equal the function at the ghost entity
/**
* \file operators_tester_direction.cpp
* \example mofem/atom_tests/operators_tester_direction.cpp
*
* \brief Check OperatorsTester::setDirectionFromFunction
*
* The direction is a smooth function of the vertex positions, defined here.
*
* The whole mesh is loaded on every rank and the problem is partitioned
* afterwards, so each rank can compute reference values from all mesh
* vertices, independently of the DOF partition and numbering. Equal
* references on one and two ranks show that the direction does not depend
* on the partition.
*
* Tests (select with -test_nb or -test_name):
* 1. values - owned vertex DOFs equal the function, other DOFs are zero
* 2. norm - vector norm equals the sum over all mesh vertices
* 3. restricted - with ents, only DOFs on those entities are set
* 4. ghosts - ghost values equal the function at the ghost entity
*/
#include <MoFEM.hpp>
using namespace MoFEM;
static char help[] = "Partition-independent test directions\n\n";
namespace {
constexpr int SPACE_DIM = 3;
constexpr TestingInterface::Tolerance TOL{1e-12, 1e-12};
constexpr double LENGTH = 0.7;
/** Smooth direction on vertex DOFs, zero on other entities.
* Coefficient c of a vertex DOF at X gets
* a_c + sum_k b_ck sin(k_ck . X / length + phi_ck), so |value| < 1. With
* length of the order of the body size the direction varies slowly over an
* element; on H1 fields it is the linear interpolation of the vertex values.
*/
OperatorsTester::DirectionFunction smoothVertexDirection(const double length) {
return [length](const NumeredDofEntity &dof,
const std::array<double, 3> &xyz) {
if (type_from_handle(dof.getEnt()) != MBVERTEX)
return 0.;
// Fixed modes; amplitudes sum to less than one for every coefficient.
constexpr double a[3] = {0.20, -0.15, 0.10};
constexpr double b[3][2] = {{0.45, 0.25}, {0.40, 0.30}, {0.35, 0.35}};
constexpr double k[3][2][3] = {
{{1.3, -0.7, 0.9}, {-0.4, 1.1, 1.7}},
{{0.8, 1.5, -0.6}, {1.9, -0.3, 0.5}},
{{-1.2, 0.6, 1.4}, {0.7, 1.8, -0.9}}};
constexpr double phi[3][2] = {{0.3, 1.1}, {-0.8, 0.4}, {1.3, -0.2}};
const int c = dof.getDofCoeffIdx() % 3;
double value = a[c];
for (int m = 0; m != 2; ++m) {
double arg = phi[c][m];
for (int d = 0; d != 3; ++d)
arg += k[c][m][d] * xyz[d] / length;
value += b[c][m] * std::sin(arg);
}
return value;
};
}
} // namespace
int main(int argc, char *argv[]) {
MoFEM::Core::Initialize(&argc, &argv, (char *)0, help);
try {
moab::Core moab_core;
moab::Interface &moab = moab_core;
MoFEM::Core mofem_core(moab);
MoFEM::Interface &m_field = mofem_core;
CHKERR DMRegister_MoFEM("DMMOFEM");
auto simple = m_field.getInterface<Simple>();
// Whole mesh on every rank; the problem is partitioned by setUp.
CHKERR simple->loadFile("", "");
CHKERR simple->addDomainField("S", H1, AINSWORTH_LEGENDRE_BASE, 1);
CHKERR simple->setFieldOrder("U", 2);
CHKERR simple->setFieldOrder("S", 1);
CHKERR simple->setUp(PETSC_FALSE);
auto dm = simple->getDM();
auto opt = m_field.getInterface<OperatorsTester>();
const auto function = smoothVertexDirection(LENGTH);
const auto u_bit = m_field.get_field_bit_number("U");
// Corner vertices of the domain; the file may hold other vertices.
Range domain, vertices;
CHKERR moab.get_entities_by_dimension(0, SPACE_DIM, domain);
CHKERR moab.get_connectivity(domain, vertices, true);
auto check_values = [&](SmartPetscObj<Vec> v, boost::shared_ptr<Range> ents,
bool ghosts) {
int size, ghost_size;
CHKERR VecGetLocalSize(v, &size);
Vec local;
CHKERR VecGhostGetLocalForm(v, &local);
CHKERR VecGetLocalSize(local, &ghost_size);
const double *array;
CHKERR VecGetArrayRead(local, &array);
double error = 0;
int checked = 0;
for (auto &dof : *getProblemPtr(dm)->getNumeredColDofsPtr()) {
const auto idx = dof->getPetscLocalDofIdx();
if (idx < 0 || (ghosts ? (idx < size || idx >= ghost_size)
: idx >= size))
continue;
const auto ent = dof->getEnt();
double expected = 0;
const bool in_ents = !ents || ents->find(ent) != ents->end();
if (dof->getBitNumber() == u_bit && in_ents) {
std::array<double, 3> xyz{0., 0., 0.};
if (type_from_handle(ent) == MBVERTEX)
CHKERR moab.get_coords(&ent, 1, xyz.data());
expected = function(*dof, xyz);
}
error = std::max(error, std::abs(array[idx] - expected));
++checked;
}
CHKERR VecRestoreArrayRead(local, &array);
CHKERR VecGhostRestoreLocalForm(v, &local);
CHKERR testing->checkError<TestType::POINTWISE>(
ghosts ? "ghost values" : "owned values", error, 1., TOL);
int global_checked;
CHKERR MPI_Allreduce(&checked, &global_checked, 1, MPI_INT, MPI_SUM,
m_field.get_comm());
// One rank has no ghosts; on several ranks the H1 field must have some.
if (!global_checked && (!ghosts || m_field.get_comm_size() > 1))
SETERRQ(m_field.get_comm(), MOFEM_ATOM_TEST_INVALID,
"No DOFs checked");
};
// smoothVertexDirection depends on the DOF only through its entity type
// and coefficient index, so one vertex DOF per coefficient evaluates it
// at any position. Every rank has vertex DOFs of U in its local problem.
std::array<boost::shared_ptr<NumeredDofEntity>, SPACE_DIM> coeff_dofs;
for (auto &dof : *getProblemPtr(dm)->getNumeredColDofsPtr())
if (dof->getBitNumber() == u_bit &&
type_from_handle(dof->getEnt()) == MBVERTEX)
coeff_dofs[dof->getDofCoeffIdx()] = dof;
for (auto &dof : coeff_dofs)
if (!dof)
SETERRQ(PETSC_COMM_SELF, MOFEM_ATOM_TEST_INVALID,
"Missing vertex DOF of U in the local problem");
// Reference from mesh vertices only: no DOF numbering involved.
auto reference_norm2 = [&](const Range &verts) {
double sum = 0;
for (auto vertex : verts) {
std::array<double, 3> xyz;
CHK_MOAB_THROW(moab.get_coords(&vertex, 1, xyz.data()), "Get coords");
for (int c = 0; c != SPACE_DIM; ++c) {
const double value = function(*coeff_dofs[c], xyz);
sum += value * value;
}
}
return sum;
};
auto check_norm = [&](SmartPetscObj<Vec> v, const Range &verts,
const std::string &name) {
double norm;
CHKERR VecNorm(v, NORM_2, &norm);
const double expected = reference_norm2(verts);
CHKERR testing->checkValue<TestType::SCALAR>(name, norm * norm, expected,
TOL);
MOFEM_LOG("WORLD", Sev::inform)
<< name << " squared norm " << std::setprecision(15) << norm * norm;
};
Range skin_verts;
{
Skinner skin(&moab);
CHKERR skin.find_skin(0, domain, false, faces);
CHKERR moab.get_connectivity(faces, skin_verts, true);
}
auto skin_ptr = boost::make_shared<Range>(skin_verts);
// Edges too, to check that only vertex DOFs are non-zero.
{
Range skin_edges;
CHKERR moab.get_adjacencies(skin_verts, 1, false, skin_edges,
moab::Interface::UNION);
skin_ptr->merge(skin_edges);
}
CHKERR testing->addTestCommandLine("", 1, "values", [&]() {
auto v = opt->setDirectionFromFunction(dm, {"U"}, function);
CHKERR check_values(v, nullptr, false);
});
CHKERR testing->addTestCommandLine("", 2, "norm", [&]() {
auto v = opt->setDirectionFromFunction(dm, {"U"}, function);
CHKERR check_norm(v, vertices, "all vertices");
});
CHKERR testing->addTestCommandLine("", 3, "restricted", [&]() {
auto v = opt->setDirectionFromFunction(dm, {"U"}, function, skin_ptr);
CHKERR check_values(v, skin_ptr, false);
CHKERR check_norm(v, skin_verts, "skin vertices");
});
CHKERR testing->addTestCommandLine("", 4, "ghosts", [&]() {
auto v = opt->setDirectionFromFunction(dm, {"U"}, function);
CHKERR check_values(v, nullptr, true);
});
CHKERR testing->runTests();
}
return 0;
}
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
static char help[]
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.
#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 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