libMesh
mesh_function.C
Go to the documentation of this file.
1 #include <libmesh/equation_systems.h>
2 #include <libmesh/replicated_mesh.h>
3 #include <libmesh/mesh_generation.h>
4 #include <libmesh/dof_map.h>
5 #include <libmesh/system.h>
6 #include <libmesh/mesh_function.h>
7 #include <libmesh/numeric_vector.h>
8 #include <libmesh/elem.h>
9 
10 #include "test_comm.h"
11 #include "libmesh_cppunit.h"
12 
13 
14 using namespace libMesh;
15 
17  const Parameters &,
18  const std::string &,
19  const std::string &)
20 {
21  return
22  cos(.5*libMesh::pi*p(0)) *
23  sin(.5*libMesh::pi*p(1)) *
24  cos(.5*libMesh::pi*p(2));
25 }
26 
27 class MeshFunctionTest : public CppUnit::TestCase
28 {
32 public:
33  CPPUNIT_TEST_SUITE( MeshFunctionTest );
34 
35 #if LIBMESH_DIM > 1
36 #ifdef LIBMESH_ENABLE_AMR
37  CPPUNIT_TEST( test_p_level );
38 #endif
39 #endif
40 
41  CPPUNIT_TEST_SUITE_END();
42 
43 protected:
44 
45 public:
46  void setUp() {}
47 
48  void tearDown() {}
49 
50  // test that mesh function works correctly with non-zero
51  // Elem::p_level() values.
52 #ifdef LIBMESH_ENABLE_AMR
53  void test_p_level()
54  {
56 
58  5, 5, 5,
59  0., 1.,
60  0., 1.,
61  0., 1.,
62  HEX20);
63 
64  // Bump the p-level for all elements. We will create a system with
65  // a FIRST, LAGRANGE variable and then use the additional p-level
66  // to solve with quadratic elements. Note: set_p_level(1) actually
67  // _increases_ the existing variable order by 1, it does not set
68  // it to 1.
69  for (auto & elem : mesh.active_element_ptr_range())
70  elem->set_p_level(1);
71 
73  System & sys = es.add_system<System> ("SimpleSystem");
74  unsigned int u_var = sys.add_variable("u", FIRST, LAGRANGE);
75 
76  es.init();
78 
79 
80  std::unique_ptr<NumericVector<Number>> mesh_function_vector
82  mesh_function_vector->init(sys.n_dofs(), sys.n_local_dofs(),
83  sys.get_dof_map().get_send_list(), false,
84  GHOSTED);
85 
86  sys.solution->localize(*mesh_function_vector,
87  sys.get_dof_map().get_send_list());
88 
89  // So the MeshFunction knows which variables to compute values for.
90  std::vector<unsigned int> variables(1);
91  variables[0] = u_var;
92 
93  auto mesh_function =
94  libmesh_make_unique<MeshFunction>(sys.get_equation_systems(),
95  *mesh_function_vector,
96  sys.get_dof_map(),
97  variables);
98 
99  mesh_function->init();
100  mesh_function->set_point_locator_tolerance(0.0001);
101  DenseVector<Number> vec_values;
102  std::string dummy;
103 
104  // Make sure the MeshFunction's values interpolate the projected solution
105  // at the nodes
106  for (const auto & node : mesh.local_node_ptr_range())
107  {
108  (*mesh_function)(*node, /*time=*/ 0., vec_values);
109  Number mesh_function_value =
110  projection_function(*node,
111  es.parameters,
112  dummy,
113  dummy);
114 
115  LIBMESH_ASSERT_FP_EQUAL
116  (libmesh_real(vec_values(0)),
117  libmesh_real(mesh_function_value),
119  }
120  }
121 #endif // LIBMESH_ENABLE_AMR
122 };
123 
124 
libMesh::System
Manages consistently variables, degrees of freedom, and coefficient vectors.
Definition: system.h:100
libMesh::HEX20
Definition: enum_elem_type.h:48
libMesh::Number
Real Number
Definition: libmesh_common.h:195
libMesh::pi
const Real pi
.
Definition: libmesh.h:237
libMesh::System::get_equation_systems
const EquationSystems & get_equation_systems() const
Definition: system.h:720
libMesh::EquationSystems::add_system
virtual System & add_system(const std::string &system_type, const std::string &name)
Add the system of type system_type named name to the systems array.
Definition: equation_systems.C:345
CPPUNIT_TEST_SUITE_REGISTRATION
CPPUNIT_TEST_SUITE_REGISTRATION(MeshFunctionTest)
libMesh::libmesh_real
T libmesh_real(T a)
Definition: libmesh_common.h:166
libMesh::MeshBase::active_element_ptr_range
virtual SimpleRange< element_iterator > active_element_ptr_range()=0
libMesh
The libMesh namespace provides an interface to certain functionality in the library.
Definition: factoryfunction.C:55
MeshFunctionTest::test_p_level
void test_p_level()
Definition: mesh_function.C:53
libMesh::MeshTools::Generation::build_cube
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.
Definition: mesh_generation.C:298
libMesh::TOLERANCE
static const Real TOLERANCE
Definition: libmesh_common.h:128
libMesh::ParallelObject::comm
const Parallel::Communicator & comm() const
Definition: parallel_object.h:94
libMesh::GHOSTED
Definition: enum_parallel_type.h:37
mesh
MeshBase & mesh
Definition: mesh_communication.C:1257
MeshFunctionTest::tearDown
void tearDown()
Definition: mesh_function.C:48
libMesh::NumericVector::build
static std::unique_ptr< NumericVector< T > > build(const Parallel::Communicator &comm, const SolverPackage solver_package=libMesh::default_solver_package())
Builds a NumericVector on the processors in communicator comm using the linear solver package specifi...
Definition: numeric_vector.C:49
libMesh::ReplicatedMesh
The ReplicatedMesh class is derived from the MeshBase class, and is used to store identical copies of...
Definition: replicated_mesh.h:47
libMesh::System::n_local_dofs
dof_id_type n_local_dofs() const
Definition: system.C:187
libMesh::System::add_variable
unsigned int add_variable(const std::string &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:1069
libMesh::MeshBase::local_node_ptr_range
virtual SimpleRange< node_iterator > local_node_ptr_range()=0
TestCommWorld
libMesh::Parallel::Communicator * TestCommWorld
Definition: driver.C:111
libMesh::EquationSystems::init
virtual void init()
Initialize all the systems.
Definition: equation_systems.C:96
libMesh::Point
A Point defines a location in LIBMESH_DIM dimensional Real space.
Definition: point.h:38
MeshFunctionTest
Definition: mesh_function.C:27
projection_function
Number projection_function(const Point &p, const Parameters &, const std::string &, const std::string &)
Definition: mesh_function.C:16
libMesh::DofMap::get_send_list
const std::vector< dof_id_type > & get_send_list() const
Definition: dof_map.h:496
libMesh::EquationSystems
This is the EquationSystems class.
Definition: equation_systems.h:74
libMesh::System::solution
std::unique_ptr< NumericVector< Number > > solution
Data structure to hold solution values.
Definition: system.h:1539
MeshFunctionTest::setUp
void setUp()
Definition: mesh_function.C:46
libmesh_cppunit.h
libMesh::System::get_dof_map
const DofMap & get_dof_map() const
Definition: system.h:2099
libMesh::System::n_dofs
dof_id_type n_dofs() const
Definition: system.C:150
libMesh::LAGRANGE
Definition: enum_fe_family.h:36
test_comm.h
libMesh::System::project_solution
void project_solution(FunctionBase< Number > *f, FunctionBase< Gradient > *g=nullptr) const
Projects arbitrary functions onto the current solution.
Definition: system_projection.C:950
libMesh::FIRST
Definition: enum_order.h:42
libMesh::Parameters
This class provides the ability to map between arbitrary, user-defined strings and several data types...
Definition: parameters.h:59
libMesh::DenseVector< Number >
libMesh::EquationSystems::parameters
Parameters parameters
Data structure holding arbitrary parameters.
Definition: equation_systems.h:557