libMesh
Public Member Functions | List of all members
MeshFunctionTest Class Reference
Inheritance diagram for MeshFunctionTest:
[legend]

Public Member Functions

 LIBMESH_CPPUNIT_TEST_SUITE (MeshFunctionTest)
 Tests for general MeshFunction capability. More...
 
 CPPUNIT_TEST (test_subdomain_id_sets)
 
 CPPUNIT_TEST (test_bad_gradient_var_with_out_of_mesh_value)
 
 CPPUNIT_TEST (test_bad_hessian_var_with_out_of_mesh_value)
 
 CPPUNIT_TEST (vectorMeshFunctionLagrange)
 
 CPPUNIT_TEST (vectorMeshFunctionNedelec)
 
 CPPUNIT_TEST (vectorMeshFunctionRaviartThomas)
 
 CPPUNIT_TEST (mixedScalarAndVectorVariables)
 
 CPPUNIT_TEST (test_p_level)
 
 CPPUNIT_TEST_SUITE_END ()
 
void setUp ()
 
void tearDown ()
 
void test_subdomain_id_sets ()
 
void test_bad_gradient_var_with_out_of_mesh_value ()
 
void test_bad_hessian_var_with_out_of_mesh_value ()
 
void test_p_level ()
 
DenseVector< Number > read_variable_info_from_output_data (const std::string &mesh_name, const std::string &solutions_name)
 
void vectorMeshFunctionLagrange ()
 
void vectorMeshFunctionNedelec ()
 
void vectorMeshFunctionRaviartThomas ()
 
void mixedScalarAndVectorVariables ()
 

Detailed Description

Definition at line 56 of file mesh_function.C.

Member Function Documentation

◆ CPPUNIT_TEST() [1/8]

MeshFunctionTest::CPPUNIT_TEST ( test_subdomain_id_sets  )

◆ CPPUNIT_TEST() [2/8]

MeshFunctionTest::CPPUNIT_TEST ( test_bad_gradient_var_with_out_of_mesh_value  )

◆ CPPUNIT_TEST() [3/8]

MeshFunctionTest::CPPUNIT_TEST ( test_bad_hessian_var_with_out_of_mesh_value  )

◆ CPPUNIT_TEST() [4/8]

MeshFunctionTest::CPPUNIT_TEST ( vectorMeshFunctionLagrange  )

◆ CPPUNIT_TEST() [5/8]

MeshFunctionTest::CPPUNIT_TEST ( vectorMeshFunctionNedelec  )

◆ CPPUNIT_TEST() [6/8]

MeshFunctionTest::CPPUNIT_TEST ( vectorMeshFunctionRaviartThomas  )

◆ CPPUNIT_TEST() [7/8]

MeshFunctionTest::CPPUNIT_TEST ( mixedScalarAndVectorVariables  )

◆ CPPUNIT_TEST() [8/8]

MeshFunctionTest::CPPUNIT_TEST ( test_p_level  )

◆ CPPUNIT_TEST_SUITE_END()

MeshFunctionTest::CPPUNIT_TEST_SUITE_END ( )

◆ LIBMESH_CPPUNIT_TEST_SUITE()

MeshFunctionTest::LIBMESH_CPPUNIT_TEST_SUITE ( MeshFunctionTest  )

Tests for general MeshFunction capability.

◆ mixedScalarAndVectorVariables()

void MeshFunctionTest::mixedScalarAndVectorVariables ( )
inline

Definition at line 537 of file mesh_function.C.

References libMesh::libmesh_real(), and libMesh::TOLERANCE.

538  {
539  LOG_UNIT_TEST;
540 
541  // Populating the solution data using a helper function
542  DenseVector<Number> output = read_variable_info_from_output_data("solutions/raviart_thomas_solution_mesh.xda","solutions/raviart_thomas_solution.xda");
543  Gradient output_raviart_thomas = VectorValue(output(0),output(1));
544 
545  // Expected value at center mesh
546  Gradient raviart_thomas_expected = VectorValue(0.0772539939808116,-0.0772537479511396);
547  Number monomial_expected = -0.44265566716952;
548  Number scalar_expected = -2.86546e-18;
549 
550  // Checking Raviart Thomas variable family values
551  LIBMESH_ASSERT_FP_EQUAL(libMesh::libmesh_real(output_raviart_thomas(0)), libMesh::libmesh_real(raviart_thomas_expected(0)),
552  TOLERANCE * TOLERANCE);
553  LIBMESH_ASSERT_FP_EQUAL(libMesh::libmesh_real(output_raviart_thomas(1)), libMesh::libmesh_real(raviart_thomas_expected(1)),
554  TOLERANCE * TOLERANCE);
555  // Checking Monomial variable family value
556  LIBMESH_ASSERT_FP_EQUAL(libMesh::libmesh_real(output(2)), libMesh::libmesh_real(monomial_expected),
557  TOLERANCE * TOLERANCE);
558  // Checking scalar variable family value
559  LIBMESH_ASSERT_FP_EQUAL(libMesh::libmesh_real(output(3)), libMesh::libmesh_real(scalar_expected),
560  TOLERANCE * TOLERANCE);
561  }
T libmesh_real(T a)
DenseVector< Number > read_variable_info_from_output_data(const std::string &mesh_name, const std::string &solutions_name)
static constexpr Real TOLERANCE
This class defines a vector in LIBMESH_DIM dimensional Real or Complex space.

◆ read_variable_info_from_output_data()

DenseVector<Number> MeshFunctionTest::read_variable_info_from_output_data ( const std::string &  mesh_name,
const std::string &  solutions_name 
)
inline

Definition at line 435 of file mesh_function.C.

References libMesh::NumericVector< T >::build(), libMesh::ParallelObject::comm(), libMesh::System::get_all_variable_numbers(), libMesh::System::get_dof_map(), libMesh::EquationSystems::get_system(), libMesh::MeshFunction::init(), mesh, libMesh::System::n_dofs(), libMesh::READ, libMesh::EquationSystems::read(), libMesh::MeshBase::read(), libMesh::EquationSystems::READ_ADDITIONAL_DATA, libMesh::EquationSystems::READ_DATA, libMesh::EquationSystems::READ_HEADER, libMesh::SERIAL, libMesh::System::solution, TestCommWorld, and libMesh::EquationSystems::update().

437  {
438  DenseVector<Number> output;
439 
440  // Reading mesh and solution information from XDA files
442  mesh.read(mesh_name);
443  EquationSystems es(mesh);
444  es.read(solutions_name, READ,
445  EquationSystems::READ_HEADER |
446  EquationSystems::READ_DATA |
447  EquationSystems::READ_ADDITIONAL_DATA);
448  es.update();
449 
450  // Pulling the correct system and variable information from
451  // the XDA files (the default system name is "nl0")
452  System & sys = es.get_system<System>("nl0");
453  std::unique_ptr<NumericVector<Number>> mesh_function_vector =
454  NumericVector<Number>::build(es.comm());
455  mesh_function_vector->init(sys.n_dofs(), false, SERIAL);
456  sys.solution->localize(*mesh_function_vector);
457 
458  // Pulling the total number of variables stored in XDA file
459  std::vector<unsigned int> variables;
460  sys.get_all_variable_numbers(variables);
461  std::sort(variables.begin(),variables.end());
462 
463  // Setting up libMesh::MeshFunction
464  MeshFunction mesh_function(es, *mesh_function_vector,
465  sys.get_dof_map(), variables);
466  mesh_function.init();
467 
468  // Defining input parameters for MeshFunction::operator()
469  const std::set<subdomain_id_type> * subdomain_ids = nullptr;
470  const Point & p = Point(0.5, 0.5);
471 
472  // Supplying the variable values at center of mesh to output
473  (mesh_function)(p, 0.0, output, subdomain_ids);
474 
475  return output;
476  }
This is the EquationSystems class.
The ReplicatedMesh class is derived from the MeshBase class, and is used to store identical copies of...
virtual void read(const std::string &name, void *mesh_data=nullptr, bool skip_renumber_nodes_and_elements=false, bool skip_find_neighbors=false, bool skip_detect_interior_parents=false)=0
Interfaces for reading/writing a mesh to/from a file.
libMesh::Parallel::Communicator * TestCommWorld
Definition: driver.C:218
MeshBase & mesh
Provides a uniform interface to vector storage schemes for different linear algebra libraries...
Definition: vector_fe_ex5.C:44
void get_all_variable_numbers(std::vector< unsigned int > &all_variable_numbers) const
Fills all_variable_numbers with all the variable numbers for the variables that have been added to th...
Definition: system.C:1403
virtual void init(const numeric_index_type n, const numeric_index_type n_local, const bool fast=false, const ParallelType ptype=AUTOMATIC)=0
Change the dimension of the vector to n.
dof_id_type n_dofs() const
Definition: system.C:118
Manages consistently variables, degrees of freedom, and coefficient vectors.
Definition: system.h:98
std::unique_ptr< NumericVector< Number > > solution
Data structure to hold solution values.
Definition: system.h:1655
virtual void init() override
Override the FunctionBase::init() member function.
This class provides function-like objects for data distributed over a mesh.
Definition: mesh_function.h:54
const DofMap & get_dof_map() const
Definition: system.h:2417
A Point defines a location in LIBMESH_DIM dimensional Real space.
Definition: point.h:39

◆ setUp()

void MeshFunctionTest::setUp ( )
inline

Definition at line 88 of file mesh_function.C.

88 {}

◆ tearDown()

void MeshFunctionTest::tearDown ( )
inline

Definition at line 90 of file mesh_function.C.

90 {}

◆ test_bad_gradient_var_with_out_of_mesh_value()

void MeshFunctionTest::test_bad_gradient_var_with_out_of_mesh_value ( )
inline

Definition at line 180 of file mesh_function.C.

References libMesh::EquationSystems::add_system(), libMesh::System::add_variable(), bilinear_function(), libMesh::MeshTools::Generation::build_square(), libMesh::ParallelObject::comm(), libMesh::CONSTANT, libMesh::System::current_local_solution, libMesh::FIRST, libMesh::System::get_dof_map(), libMesh::MeshFunction::init(), libMesh::EquationSystems::init(), libMesh::invalid_uint, libMesh::LAGRANGE, TIMPI::Communicator::max(), mesh, libMesh::MONOMIAL, libMesh::EquationSystems::parameters, libMesh::ParallelObject::processor_id(), libMesh::DofObject::processor_id(), libMesh::System::project_solution(), libMesh::QUAD4, libMesh::MeshBase::sub_point_locator(), TestCommWorld, and libMesh::TOLERANCE.

181  {
182  LOG_UNIT_TEST;
183 
185 
187  4, 4,
188  0., 1.,
189  0., 1.,
190  QUAD4);
191 
192  EquationSystems es(mesh);
193  System & sys = es.add_system<System>("SimpleSystem");
194  const unsigned int u_var = sys.add_variable("u", FIRST, LAGRANGE);
195  const unsigned int v_var = sys.add_variable("v", CONSTANT, MONOMIAL);
196 
197  es.init();
198  sys.project_solution(bilinear_function, nullptr, es.parameters);
199 
200  std::vector<unsigned int> variables {u_var, v_var, libMesh::invalid_uint};
201  MeshFunction mesh_function(es,
203  sys.get_dof_map(),
204  variables);
205  mesh_function.init();
206 
207  DenseVector<Number> out_of_mesh_value(3);
208  out_of_mesh_value(0) = -99.0;
209  out_of_mesh_value(1) = 5.25;
210  out_of_mesh_value(1) = 42;
211  mesh_function.enable_out_of_mesh_mode(out_of_mesh_value);
212 
213  // Pick an element centroid so u and v agree
214  const Point good_p(0.375, 0.625, 0.0);
215 
216  // On a distributed mesh not every processor may have every
217  // element
218  const Elem * elem = (*mesh.sub_point_locator())(good_p);
219  bool someone_found_elem = elem;
220  mesh.comm().max(someone_found_elem);
221  CPPUNIT_ASSERT(someone_found_elem);
222 
223  std::vector<Gradient> gradients;
224  mesh_function.gradient(good_p, 0.0, gradients);
225 
226  CPPUNIT_ASSERT_EQUAL(std::size_t(3), gradients.size());
227 
228  // Let's only test our evaluation where we know we can evaluate, in parallel
229  if (!elem || elem->processor_id() != mesh.processor_id())
230  return;
231 
232  LIBMESH_ASSERT_NUMBERS_EQUAL(8.0, gradients[0](0), TOLERANCE * TOLERANCE);
233  LIBMESH_ASSERT_NUMBERS_EQUAL(80.0, gradients[0](1), TOLERANCE * TOLERANCE);
234  if (LIBMESH_DIM > 2)
235  LIBMESH_ASSERT_NUMBERS_EQUAL(0.0, gradients[0](2), TOLERANCE * TOLERANCE);
236 
237  // Piecewise-constant functions have zero gradients
238  for (unsigned int d = 0; d < LIBMESH_DIM; ++d)
239  LIBMESH_ASSERT_NUMBERS_EQUAL(0, gradients[1](d), TOLERANCE * TOLERANCE);
240 
241  LIBMESH_ASSERT_NUMBERS_EQUAL(out_of_mesh_value(2),
242  gradients[2](0),
243  TOLERANCE * TOLERANCE);
244  for (unsigned int d = 1; d < LIBMESH_DIM; ++d)
245  LIBMESH_ASSERT_NUMBERS_EQUAL(0, gradients[2](d),
246  TOLERANCE * TOLERANCE);
247 
248  const Point bad_p(1.375, 0.625, 0.0);
249  mesh_function.gradient(bad_p, 0.0, gradients);
250 
251  for (unsigned int vn = 0; vn != 3; ++vn)
252  {
253  LIBMESH_ASSERT_NUMBERS_EQUAL(out_of_mesh_value(vn),
254  gradients[vn](0),
255  TOLERANCE * TOLERANCE);
256  for (unsigned int d = 1; d < LIBMESH_DIM; ++d)
257  LIBMESH_ASSERT_NUMBERS_EQUAL(0, gradients[vn](d),
258  TOLERANCE * TOLERANCE);
259  }
260  }
This is the EquationSystems class.
The ReplicatedMesh class is derived from the MeshBase class, and is used to store identical copies of...
const unsigned int invalid_uint
A number which is used quite often to represent an invalid or uninitialized value for an unsigned int...
Definition: libmesh.h:303
std::unique_ptr< PointLocatorBase > sub_point_locator() const
Definition: mesh_base.C:1826
libMesh::Parallel::Communicator * TestCommWorld
Definition: driver.C:218
static constexpr Real TOLERANCE
This is the base class from which all geometric element types are derived.
Definition: elem.h:94
MeshBase & mesh
const Parallel::Communicator & comm() const
Number bilinear_function(const Point &p, const Parameters &, const std::string &, const std::string &)
Definition: mesh_function.C:37
void build_square(UnstructuredMesh &mesh, const unsigned int nx, const unsigned int ny, const Real xmin=0., const Real xmax=1., const Real ymin=0., const Real ymax=1., const ElemType type=INVALID_ELEM, const bool gauss_lobatto_grid=false)
A specialized build_cube() for 2D meshes.
Manages consistently variables, degrees of freedom, and coefficient vectors.
Definition: system.h:98
virtual void init() override
Override the FunctionBase::init() member function.
unsigned int add_variable(std::string_view var, const FEType &type, const std::set< subdomain_id_type > *const active_subdomains=nullptr)
Adds the variable var to the list of variables for this system.
Definition: system.C:1344
void project_solution(FunctionBase< Number > *f, FunctionBase< Gradient > *g=nullptr, std::optional< ConstElemRange > active_local_range=std::nullopt, std::optional< std::vector< unsigned int >> variable_numbers=std::nullopt) const
Projects arbitrary functions onto the current solution.
void max(const T &r, T &o, Request &req) const
std::unique_ptr< NumericVector< Number > > current_local_solution
All the values I need to compute my contribution to the simulation at hand.
Definition: system.h:1667
This class provides function-like objects for data distributed over a mesh.
Definition: mesh_function.h:54
processor_id_type processor_id() const
const DofMap & get_dof_map() const
Definition: system.h:2417
processor_id_type processor_id() const
Definition: dof_object.h:881
A Point defines a location in LIBMESH_DIM dimensional Real space.
Definition: point.h:39

◆ test_bad_hessian_var_with_out_of_mesh_value()

void MeshFunctionTest::test_bad_hessian_var_with_out_of_mesh_value ( )
inline

Definition at line 263 of file mesh_function.C.

References libMesh::EquationSystems::add_system(), libMesh::System::add_variable(), biquadratic_function(), libMesh::MeshTools::Generation::build_square(), libMesh::ParallelObject::comm(), libMesh::CONSTANT, libMesh::System::current_local_solution, libMesh::System::get_dof_map(), libMesh::MeshFunction::init(), libMesh::EquationSystems::init(), libMesh::invalid_uint, libMesh::LAGRANGE, TIMPI::Communicator::max(), mesh, libMesh::MONOMIAL, libMesh::EquationSystems::parameters, libMesh::ParallelObject::processor_id(), libMesh::DofObject::processor_id(), libMesh::System::project_solution(), libMesh::QUAD9, libMesh::Real, libMesh::SECOND, libMesh::MeshBase::sub_point_locator(), TestCommWorld, and libMesh::TOLERANCE.

264  {
265  LOG_UNIT_TEST;
266 
267  // Hessian calculations are a little weaker in FP on some systems
268  const Real tol = TOLERANCE * std::sqrt(TOLERANCE);
269 
271 
273  4, 4,
274  0., 1.,
275  0., 1.,
276  QUAD9);
277 
278  EquationSystems es(mesh);
279  System & sys = es.add_system<System>("SimpleSystem");
280  unsigned int u_var = sys.add_variable("u", SECOND, LAGRANGE);
281  unsigned int v_var = sys.add_variable("v", CONSTANT, MONOMIAL);
282 
283  es.init();
284  sys.project_solution(biquadratic_function, nullptr, es.parameters);
285 
286  std::vector<unsigned int> variables {u_var, v_var, libMesh::invalid_uint};
287  MeshFunction mesh_function(es,
289  sys.get_dof_map(),
290  variables);
291  mesh_function.init();
292 
293  DenseVector<Number> out_of_mesh_value(3);
294  out_of_mesh_value(0) = -99.0;
295  out_of_mesh_value(1) = 5.25;
296  out_of_mesh_value(2) = 42;
297  mesh_function.enable_out_of_mesh_mode(out_of_mesh_value);
298 
299  std::vector<Tensor> hessians;
300  const Point p(0.4, 0.6, 0.0);
301 
302  // On a distributed mesh not every processor may have every
303  // element
304  const Elem * elem = (*mesh.sub_point_locator())(p);
305  bool someone_found_elem = elem;
306  mesh.comm().max(someone_found_elem);
307  CPPUNIT_ASSERT(someone_found_elem);
308 
309  mesh_function.hessian(p, 0.0, hessians);
310 
311  CPPUNIT_ASSERT_EQUAL(std::size_t(3), hessians.size());
312 
313  // Let's only test our evaluation where we know we can evaluate, in parallel
314  if (!elem || elem->processor_id() != mesh.processor_id())
315  return;
316 
317  LIBMESH_ASSERT_NUMBERS_EQUAL(15.0, hessians[0](0,0), tol);
318  LIBMESH_ASSERT_NUMBERS_EQUAL(0.75, hessians[0](0,1), tol);
319  LIBMESH_ASSERT_NUMBERS_EQUAL(0.75, hessians[0](1,0), tol);
320  LIBMESH_ASSERT_NUMBERS_EQUAL(150.0, hessians[0](1,1), tol);
321 
322  if (LIBMESH_DIM > 2)
323  for (unsigned int d = 0; d < LIBMESH_DIM; ++d)
324  for (unsigned int d2 = 0; d2 < LIBMESH_DIM; ++d2)
325  if (d>1 || d2>1)
326  LIBMESH_ASSERT_NUMBERS_EQUAL(0, hessians[0](d,d2),
327  tol);
328 
329  // Piecewise-constant functions have zero Hessians
330  for (unsigned int d = 0; d < LIBMESH_DIM; ++d)
331  for (unsigned int d2 = 0; d2 < LIBMESH_DIM; ++d2)
332  LIBMESH_ASSERT_NUMBERS_EQUAL(0, hessians[1](d,d2), tol);
333 
334  LIBMESH_ASSERT_NUMBERS_EQUAL(out_of_mesh_value(2),
335  hessians[2](0,0),
336  tol);
337  for (unsigned int d = 0; d < LIBMESH_DIM; ++d)
338  for (unsigned int d2 = 0; d2 < LIBMESH_DIM; ++d2)
339  if (d || d2)
340  LIBMESH_ASSERT_NUMBERS_EQUAL(0, hessians[2](d,d2),
341  tol);
342 
343  const Point bad_p(1.375, 0.625, 0.0);
344  mesh_function.hessian(bad_p, 0.0, hessians);
345 
346  for (unsigned int vn = 0; vn != 2; ++vn)
347  {
348  LIBMESH_ASSERT_NUMBERS_EQUAL(out_of_mesh_value(vn),
349  hessians[vn](0,0), tol);
350  for (unsigned int d = 0; d < LIBMESH_DIM; ++d)
351  for (unsigned int d2 = 0; d2 < LIBMESH_DIM; ++d2)
352  if (d || d2)
353  LIBMESH_ASSERT_NUMBERS_EQUAL(0, hessians[vn](d,d2), tol);
354  }
355  }
Number biquadratic_function(const Point &p, const Parameters &, const std::string &, const std::string &)
Definition: mesh_function.C:46
This is the EquationSystems class.
The ReplicatedMesh class is derived from the MeshBase class, and is used to store identical copies of...
const unsigned int invalid_uint
A number which is used quite often to represent an invalid or uninitialized value for an unsigned int...
Definition: libmesh.h:303
std::unique_ptr< PointLocatorBase > sub_point_locator() const
Definition: mesh_base.C:1826
libMesh::Parallel::Communicator * TestCommWorld
Definition: driver.C:218
static constexpr Real TOLERANCE
This is the base class from which all geometric element types are derived.
Definition: elem.h:94
MeshBase & mesh
const Parallel::Communicator & comm() const
void build_square(UnstructuredMesh &mesh, const unsigned int nx, const unsigned int ny, const Real xmin=0., const Real xmax=1., const Real ymin=0., const Real ymax=1., const ElemType type=INVALID_ELEM, const bool gauss_lobatto_grid=false)
A specialized build_cube() for 2D meshes.
Manages consistently variables, degrees of freedom, and coefficient vectors.
Definition: system.h:98
virtual void init() override
Override the FunctionBase::init() member function.
unsigned int add_variable(std::string_view var, const FEType &type, const std::set< subdomain_id_type > *const active_subdomains=nullptr)
Adds the variable var to the list of variables for this system.
Definition: system.C:1344
void project_solution(FunctionBase< Number > *f, FunctionBase< Gradient > *g=nullptr, std::optional< ConstElemRange > active_local_range=std::nullopt, std::optional< std::vector< unsigned int >> variable_numbers=std::nullopt) const
Projects arbitrary functions onto the current solution.
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
void max(const T &r, T &o, Request &req) const
std::unique_ptr< NumericVector< Number > > current_local_solution
All the values I need to compute my contribution to the simulation at hand.
Definition: system.h:1667
This class provides function-like objects for data distributed over a mesh.
Definition: mesh_function.h:54
processor_id_type processor_id() const
const DofMap & get_dof_map() const
Definition: system.h:2417
processor_id_type processor_id() const
Definition: dof_object.h:881
A Point defines a location in LIBMESH_DIM dimensional Real space.
Definition: point.h:39

◆ test_p_level()

void MeshFunctionTest::test_p_level ( )
inline

Definition at line 361 of file mesh_function.C.

References libMesh::EquationSystems::add_system(), libMesh::System::add_variable(), libMesh::NumericVector< T >::build(), libMesh::MeshTools::Generation::build_cube(), libMesh::ParallelObject::comm(), libMesh::FIRST, libMesh::System::get_dof_map(), libMesh::System::get_equation_systems(), libMesh::DofMap::get_send_list(), libMesh::GHOSTED, libMesh::HEX20, libMesh::EquationSystems::init(), libMesh::LAGRANGE, mesh, libMesh::System::n_dofs(), libMesh::System::n_local_dofs(), libMesh::EquationSystems::parameters, libMesh::System::project_solution(), projection_function(), libMesh::System::solution, TestCommWorld, and libMesh::TOLERANCE.

362  {
363  LOG_UNIT_TEST;
364 
366 
368  5, 5, 5,
369  0., 1.,
370  0., 1.,
371  0., 1.,
372  HEX20);
373 
374  // Bump the p-level for all elements. We will create a system with
375  // a FIRST, LAGRANGE variable and then use the additional p-level
376  // to solve with quadratic elements. Note: set_p_level(1) actually
377  // _increases_ the existing variable order by 1, it does not set
378  // it to 1.
379  for (auto & elem : mesh.active_element_ptr_range())
380  elem->set_p_level(1);
381 
382  EquationSystems es(mesh);
383  System & sys = es.add_system<System> ("SimpleSystem");
384  unsigned int u_var = sys.add_variable("u", FIRST, LAGRANGE);
385 
386  es.init();
387  sys.project_solution(projection_function, nullptr, es.parameters);
388 
389 
390  std::unique_ptr<NumericVector<Number>> mesh_function_vector
392  mesh_function_vector->init(sys.n_dofs(), sys.n_local_dofs(),
393  sys.get_dof_map().get_send_list(), false,
394  GHOSTED);
395 
396  sys.solution->localize(*mesh_function_vector,
397  sys.get_dof_map().get_send_list());
398 
399 
400  // So the MeshFunction knows which variables to compute values for.
401  // std::make_unique doesn't like if we try to use {u_var} in-place?
402  std::vector<unsigned int> variables {u_var};
403 
404  auto mesh_function =
405  std::make_unique<MeshFunction>(sys.get_equation_systems(),
406  *mesh_function_vector,
407  sys.get_dof_map(),
408  variables);
409 
410  mesh_function->init();
411  mesh_function->set_point_locator_tolerance(0.0001);
412  DenseVector<Number> vec_values;
413  std::string dummy;
414 
415  // Make sure the MeshFunction's values interpolate the projected solution
416  // at the nodes
417  for (const auto & node : mesh.local_node_ptr_range())
418  {
419  (*mesh_function)(*node, /*time=*/ 0., vec_values);
420  Number mesh_function_value =
421  projection_function(*node,
422  es.parameters,
423  dummy,
424  dummy);
425 
426  LIBMESH_ASSERT_NUMBERS_EQUAL
427  (vec_values(0), mesh_function_value, TOLERANCE*TOLERANCE);
428  }
429  }
This is the EquationSystems class.
The ReplicatedMesh class is derived from the MeshBase class, and is used to store identical copies of...
libMesh::Parallel::Communicator * TestCommWorld
Definition: driver.C:218
static constexpr Real TOLERANCE
Number projection_function(const Point &p, const Parameters &, const std::string &, const std::string &)
Definition: mesh_function.C:17
const EquationSystems & get_equation_systems() const
Definition: system.h:767
MeshBase & mesh
Provides a uniform interface to vector storage schemes for different linear algebra libraries...
Definition: vector_fe_ex5.C:44
const Parallel::Communicator & comm() const
dof_id_type n_local_dofs() const
Definition: system.C:155
virtual void init(const numeric_index_type n, const numeric_index_type n_local, const bool fast=false, const ParallelType ptype=AUTOMATIC)=0
Change the dimension of the vector to n.
dof_id_type n_dofs() const
Definition: system.C:118
Manages consistently variables, degrees of freedom, and coefficient vectors.
Definition: system.h:98
std::unique_ptr< NumericVector< Number > > solution
Data structure to hold solution values.
Definition: system.h:1655
unsigned int add_variable(std::string_view var, const FEType &type, const std::set< subdomain_id_type > *const active_subdomains=nullptr)
Adds the variable var to the list of variables for this system.
Definition: system.C:1344
void project_solution(FunctionBase< Number > *f, FunctionBase< Gradient > *g=nullptr, std::optional< ConstElemRange > active_local_range=std::nullopt, std::optional< std::vector< unsigned int >> variable_numbers=std::nullopt) const
Projects arbitrary functions onto the current solution.
const DofMap & get_dof_map() const
Definition: system.h:2417
const std::vector< dof_id_type > & get_send_list() const
Definition: dof_map.h:533
void build_cube(UnstructuredMesh &mesh, const unsigned int nx=0, const unsigned int ny=0, const unsigned int nz=0, const Real xmin=0., const Real xmax=1., const Real ymin=0., const Real ymax=1., const Real zmin=0., const Real zmax=1., const ElemType type=INVALID_ELEM, const bool gauss_lobatto_grid=false)
Builds a (elements) cube.

◆ test_subdomain_id_sets()

void MeshFunctionTest::test_subdomain_id_sets ( )
inline

Definition at line 93 of file mesh_function.C.

References libMesh::EquationSystems::add_system(), libMesh::System::add_variable(), libMesh::MeshTools::Generation::build_square(), libMesh::CONSTANT, libMesh::System::current_local_solution, libMesh::DenseVector< T >::empty(), libMesh::System::get_dof_map(), libMesh::System::get_equation_systems(), libMesh::MeshFunction::init(), libMesh::EquationSystems::init(), libMesh::libmesh_real(), mesh, libMesh::MONOMIAL, libMesh::EquationSystems::parameters, libMesh::System::project_solution(), libMesh::QUAD4, libMesh::Real, TestCommWorld, libMesh::TOLERANCE, and trilinear_function().

94  {
95  LOG_UNIT_TEST;
96 
98 
100  4, 4,
101  0., 1.,
102  0., 1.,
103  QUAD4);
104 
105  // Set a subdomain id for all elements, based on location.
106  for (auto & elem : mesh.active_element_ptr_range())
107  {
108  Point c = elem->vertex_average();
109  elem->subdomain_id() =
110  subdomain_id_type(c(0)*4) + subdomain_id_type(c(1)*4)*10;
111  }
112 
113  // Add a discontinuous variable so we can easily see what side of
114  // an interface we're querying
115  EquationSystems es(mesh);
116  System & sys = es.add_system<System> ("SimpleSystem");
117  unsigned int u_var = sys.add_variable("u", CONSTANT, MONOMIAL);
118 
119  es.init();
120  sys.project_solution(trilinear_function, nullptr, es.parameters);
121 
122  MeshFunction mesh_function (sys.get_equation_systems(),
124  sys.get_dof_map(),
125  u_var);
126 
127  // Checkerboard pattern
128  const std::set<subdomain_id_type> sbdids1 {0,2,11,13,20,22,31,33};
129  const std::set<subdomain_id_type> sbdids2 {1,3,10,12,21,23,30,32};
130  mesh_function.init();
131  mesh_function.enable_out_of_mesh_mode(DenseVector<Number>());
132  mesh_function.set_subdomain_ids(&sbdids1);
133 
134  DenseVector<Number> vec_values;
135  const std::string dummy;
136 
137  // Make sure the MeshFunction's values interpolate the projected solution
138  // at the nodes
139  for (auto & elem : mesh.active_local_element_ptr_range())
140  {
141  const Point c = elem->vertex_average();
142  const Real expected_value =
143  libmesh_real(trilinear_function(c, es.parameters, dummy, dummy));
144  const std::vector<Point> offsets
145  {{0,-1/8.}, {1/8.,0}, {0,1/8.}, {-1/8.,0}};
146  for (Point offset : offsets)
147  {
148  const Point p = c + offset;
149  mesh_function(p, 0, vec_values, &sbdids1);
150  const Number retval1 = vec_values.empty() ? -12345 : vec_values(0);
151  mesh_function(p, 0, vec_values, &sbdids2);
152  const Number retval2 = vec_values.empty() ? -12345 : vec_values(0);
153  mesh_function(c, 0, vec_values, nullptr);
154  const Number retval3 = vec_values.empty() ? -12345 : vec_values(0);
155 
156  LIBMESH_ASSERT_NUMBERS_EQUAL
157  (retval3, expected_value, TOLERANCE * TOLERANCE);
158 
159  if (sbdids1.count(elem->subdomain_id()))
160  {
161  CPPUNIT_ASSERT(!sbdids2.count(elem->subdomain_id()));
162  LIBMESH_ASSERT_NUMBERS_EQUAL
163  (retval1, expected_value, TOLERANCE * TOLERANCE);
164 
165  mesh_function(c, 0, vec_values, &sbdids2);
166  CPPUNIT_ASSERT(vec_values.empty());
167  }
168  else
169  {
170  LIBMESH_ASSERT_NUMBERS_EQUAL
171  (retval2, expected_value, TOLERANCE * TOLERANCE);
172 
173  mesh_function(c, 0, vec_values, &sbdids1);
174  CPPUNIT_ASSERT(vec_values.empty());
175  }
176  }
177  }
178  }
T libmesh_real(T a)
This is the EquationSystems class.
The ReplicatedMesh class is derived from the MeshBase class, and is used to store identical copies of...
Number trilinear_function(const Point &p, const Parameters &, const std::string &, const std::string &)
Definition: mesh_function.C:28
libMesh::Parallel::Communicator * TestCommWorld
Definition: driver.C:218
static constexpr Real TOLERANCE
TestClass subdomain_id_type
Based on the 4-byte comment warning above, this probably doesn&#39;t work with exodusII at all...
Definition: id_types.h:43
const EquationSystems & get_equation_systems() const
Definition: system.h:767
MeshBase & mesh
void build_square(UnstructuredMesh &mesh, const unsigned int nx, const unsigned int ny, const Real xmin=0., const Real xmax=1., const Real ymin=0., const Real ymax=1., const ElemType type=INVALID_ELEM, const bool gauss_lobatto_grid=false)
A specialized build_cube() for 2D meshes.
Manages consistently variables, degrees of freedom, and coefficient vectors.
Definition: system.h:98
virtual bool empty() const override final
Definition: dense_vector.h:109
virtual void init() override
Override the FunctionBase::init() member function.
unsigned int add_variable(std::string_view var, const FEType &type, const std::set< subdomain_id_type > *const active_subdomains=nullptr)
Adds the variable var to the list of variables for this system.
Definition: system.C:1344
void project_solution(FunctionBase< Number > *f, FunctionBase< Gradient > *g=nullptr, std::optional< ConstElemRange > active_local_range=std::nullopt, std::optional< std::vector< unsigned int >> variable_numbers=std::nullopt) const
Projects arbitrary functions onto the current solution.
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
std::unique_ptr< NumericVector< Number > > current_local_solution
All the values I need to compute my contribution to the simulation at hand.
Definition: system.h:1667
This class provides function-like objects for data distributed over a mesh.
Definition: mesh_function.h:54
const DofMap & get_dof_map() const
Definition: system.h:2417
A Point defines a location in LIBMESH_DIM dimensional Real space.
Definition: point.h:39

◆ vectorMeshFunctionLagrange()

void MeshFunctionTest::vectorMeshFunctionLagrange ( )
inline

Definition at line 479 of file mesh_function.C.

References libMesh::libmesh_real(), and libMesh::TOLERANCE.

480  {
481  LOG_UNIT_TEST;
482 
483  // Populating the solution data using a helper function
484  DenseVector<Number> output = read_variable_info_from_output_data("solutions/lagrange_vec_solution_mesh.xda","solutions/lagrange_vec_solution.xda");
485  Gradient output_vec = VectorValue(output(0),output(1));
486 
487  // Expected value at center mesh
488  Gradient output_expected = VectorValue(0.100977281077292,0.201954562154583);
489 
490  LIBMESH_ASSERT_FP_EQUAL(libMesh::libmesh_real(output_vec(0)), libMesh::libmesh_real(output_expected(0)),
491  TOLERANCE * TOLERANCE);
492  LIBMESH_ASSERT_FP_EQUAL(libMesh::libmesh_real(output_vec(1)), libMesh::libmesh_real(output_expected(1)),
493  TOLERANCE * TOLERANCE);
494  }
T libmesh_real(T a)
DenseVector< Number > read_variable_info_from_output_data(const std::string &mesh_name, const std::string &solutions_name)
static constexpr Real TOLERANCE
This class defines a vector in LIBMESH_DIM dimensional Real or Complex space.

◆ vectorMeshFunctionNedelec()

void MeshFunctionTest::vectorMeshFunctionNedelec ( )
inline

Definition at line 497 of file mesh_function.C.

References libMesh::libmesh_real(), and libMesh::TOLERANCE.

498  {
499  LOG_UNIT_TEST;
500 
501  // Populating the solution data using a helper function
502  DenseVector<Number> output = read_variable_info_from_output_data("solutions/nedelec_one_solution_mesh.xda","solutions/nedelec_one_solution.xda");
503  Gradient output_vec = VectorValue(output(0),output(1));
504 
505  // Expected value at center mesh
506  Gradient output_expected = VectorValue(0.0949202883998996,-0.0949202883918033);
507 
508  LIBMESH_ASSERT_FP_EQUAL(libMesh::libmesh_real(output_vec(0)), libMesh::libmesh_real(output_expected(0)),
509  TOLERANCE * TOLERANCE);
510  LIBMESH_ASSERT_FP_EQUAL(libMesh::libmesh_real(output_vec(1)), libMesh::libmesh_real(output_expected(1)),
511  TOLERANCE * TOLERANCE);
512  }
T libmesh_real(T a)
DenseVector< Number > read_variable_info_from_output_data(const std::string &mesh_name, const std::string &solutions_name)
static constexpr Real TOLERANCE
This class defines a vector in LIBMESH_DIM dimensional Real or Complex space.

◆ vectorMeshFunctionRaviartThomas()

void MeshFunctionTest::vectorMeshFunctionRaviartThomas ( )
inline

Definition at line 515 of file mesh_function.C.

References libMesh::libmesh_real(), and libMesh::TOLERANCE.

516  {
517  LOG_UNIT_TEST;
518 
519  // Populating the solution data using a helper function
520  DenseVector<Number> output = read_variable_info_from_output_data("solutions/raviart_thomas_solution_mesh.xda","solutions/raviart_thomas_solution.xda");
521  Gradient output_vec = VectorValue(output(0),output(1));
522 
523  // Expected value at center mesh
524  Gradient output_expected = VectorValue(0.0772539939808116,-0.0772537479511396);
525 
526  LIBMESH_ASSERT_FP_EQUAL(libMesh::libmesh_real(output_vec(0)), libMesh::libmesh_real(output_expected(0)),
527  TOLERANCE * TOLERANCE);
528  LIBMESH_ASSERT_FP_EQUAL(libMesh::libmesh_real(output_vec(1)), libMesh::libmesh_real(output_expected(1)),
529  TOLERANCE * TOLERANCE);
530  }
T libmesh_real(T a)
DenseVector< Number > read_variable_info_from_output_data(const std::string &mesh_name, const std::string &solutions_name)
static constexpr Real TOLERANCE
This class defines a vector in LIBMESH_DIM dimensional Real or Complex space.

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