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 (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_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 37 of file mesh_function.C.

Member Function Documentation

◆ CPPUNIT_TEST() [1/6]

MeshFunctionTest::CPPUNIT_TEST ( test_subdomain_id_sets  )

◆ CPPUNIT_TEST() [2/6]

MeshFunctionTest::CPPUNIT_TEST ( vectorMeshFunctionLagrange  )

◆ CPPUNIT_TEST() [3/6]

MeshFunctionTest::CPPUNIT_TEST ( vectorMeshFunctionNedelec  )

◆ CPPUNIT_TEST() [4/6]

MeshFunctionTest::CPPUNIT_TEST ( vectorMeshFunctionRaviartThomas  )

◆ CPPUNIT_TEST() [5/6]

MeshFunctionTest::CPPUNIT_TEST ( mixedScalarAndVectorVariables  )

◆ CPPUNIT_TEST() [6/6]

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 336 of file mesh_function.C.

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

337  {
338  LOG_UNIT_TEST;
339 
340  // Populating the solution data using a helper function
341  DenseVector<Number> output = read_variable_info_from_output_data("solutions/raviart_thomas_solution_mesh.xda","solutions/raviart_thomas_solution.xda");
342  Gradient output_raviart_thomas = VectorValue(output(0),output(1));
343 
344  // Expected value at center mesh
345  Gradient raviart_thomas_expected = VectorValue(0.0772539939808116,-0.0772537479511396);
346  Number monomial_expected = -0.44265566716952;
347  Number scalar_expected = -2.86546e-18;
348 
349  // Checking Raviart Thomas variable family values
350  LIBMESH_ASSERT_FP_EQUAL(libMesh::libmesh_real(output_raviart_thomas(0)), libMesh::libmesh_real(raviart_thomas_expected(0)),
351  TOLERANCE * TOLERANCE);
352  LIBMESH_ASSERT_FP_EQUAL(libMesh::libmesh_real(output_raviart_thomas(1)), libMesh::libmesh_real(raviart_thomas_expected(1)),
353  TOLERANCE * TOLERANCE);
354  // Checking Monomial variable family value
355  LIBMESH_ASSERT_FP_EQUAL(libMesh::libmesh_real(output(2)), libMesh::libmesh_real(monomial_expected),
356  TOLERANCE * TOLERANCE);
357  // Checking scalar variable family value
358  LIBMESH_ASSERT_FP_EQUAL(libMesh::libmesh_real(output(3)), libMesh::libmesh_real(scalar_expected),
359  TOLERANCE * TOLERANCE);
360  }
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 234 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().

236  {
237  DenseVector<Number> output;
238 
239  // Reading mesh and solution information from XDA files
241  mesh.read(mesh_name);
242  EquationSystems es(mesh);
243  es.read(solutions_name, READ,
244  EquationSystems::READ_HEADER |
245  EquationSystems::READ_DATA |
246  EquationSystems::READ_ADDITIONAL_DATA);
247  es.update();
248 
249  // Pulling the correct system and variable information from
250  // the XDA files (the default system name is "nl0")
251  System & sys = es.get_system<System>("nl0");
252  std::unique_ptr<NumericVector<Number>> mesh_function_vector =
253  NumericVector<Number>::build(es.comm());
254  mesh_function_vector->init(sys.n_dofs(), false, SERIAL);
255  sys.solution->localize(*mesh_function_vector);
256 
257  // Pulling the total number of variables stored in XDA file
258  std::vector<unsigned int> variables;
259  sys.get_all_variable_numbers(variables);
260  std::sort(variables.begin(),variables.end());
261 
262  // Setting up libMesh::MeshFunction
263  MeshFunction mesh_function(es, *mesh_function_vector,
264  sys.get_dof_map(), variables);
265  mesh_function.init();
266 
267  // Defining input parameters for MeshFunction::operator()
268  const std::set<subdomain_id_type> * subdomain_ids = nullptr;
269  const Point & p = Point(0.5, 0.5);
270 
271  // Supplying the variable values at center of mesh to output
272  (mesh_function)(p, 0.0, output, subdomain_ids);
273 
274  return output;
275  }
This is the EquationSystems class.
virtual void read(const std::string &name, void *mesh_data=nullptr, bool skip_renumber_nodes_and_elements=false, bool skip_find_neighbors=false)=0
Interfaces for reading/writing a mesh to/from a file.
The ReplicatedMesh class is derived from the MeshBase class, and is used to store identical copies of...
libMesh::Parallel::Communicator * TestCommWorld
Definition: driver.C:171
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:1617
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:121
Manages consistently variables, degrees of freedom, and coefficient vectors.
Definition: system.h:96
std::unique_ptr< NumericVector< Number > > solution
Data structure to hold solution values.
Definition: system.h:1593
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:2374
A Point defines a location in LIBMESH_DIM dimensional Real space.
Definition: point.h:39

◆ setUp()

void MeshFunctionTest::setUp ( )
inline

Definition at line 65 of file mesh_function.C.

65 {}

◆ tearDown()

void MeshFunctionTest::tearDown ( )
inline

Definition at line 67 of file mesh_function.C.

67 {}

◆ test_p_level()

void MeshFunctionTest::test_p_level ( )
inline

Definition at line 160 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.

161  {
162  LOG_UNIT_TEST;
163 
165 
167  5, 5, 5,
168  0., 1.,
169  0., 1.,
170  0., 1.,
171  HEX20);
172 
173  // Bump the p-level for all elements. We will create a system with
174  // a FIRST, LAGRANGE variable and then use the additional p-level
175  // to solve with quadratic elements. Note: set_p_level(1) actually
176  // _increases_ the existing variable order by 1, it does not set
177  // it to 1.
178  for (auto & elem : mesh.active_element_ptr_range())
179  elem->set_p_level(1);
180 
181  EquationSystems es(mesh);
182  System & sys = es.add_system<System> ("SimpleSystem");
183  unsigned int u_var = sys.add_variable("u", FIRST, LAGRANGE);
184 
185  es.init();
186  sys.project_solution(projection_function, nullptr, es.parameters);
187 
188 
189  std::unique_ptr<NumericVector<Number>> mesh_function_vector
191  mesh_function_vector->init(sys.n_dofs(), sys.n_local_dofs(),
192  sys.get_dof_map().get_send_list(), false,
193  GHOSTED);
194 
195  sys.solution->localize(*mesh_function_vector,
196  sys.get_dof_map().get_send_list());
197 
198 
199  // So the MeshFunction knows which variables to compute values for.
200  // std::make_unique doesn't like if we try to use {u_var} in-place?
201  std::vector<unsigned int> variables {u_var};
202 
203  auto mesh_function =
204  std::make_unique<MeshFunction>(sys.get_equation_systems(),
205  *mesh_function_vector,
206  sys.get_dof_map(),
207  variables);
208 
209  mesh_function->init();
210  mesh_function->set_point_locator_tolerance(0.0001);
211  DenseVector<Number> vec_values;
212  std::string dummy;
213 
214  // Make sure the MeshFunction's values interpolate the projected solution
215  // at the nodes
216  for (const auto & node : mesh.local_node_ptr_range())
217  {
218  (*mesh_function)(*node, /*time=*/ 0., vec_values);
219  Number mesh_function_value =
220  projection_function(*node,
221  es.parameters,
222  dummy,
223  dummy);
224 
225  LIBMESH_ASSERT_NUMBERS_EQUAL
226  (vec_values(0), mesh_function_value, TOLERANCE*TOLERANCE);
227  }
228  }
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:171
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:721
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:158
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:121
void project_solution(FunctionBase< Number > *f, FunctionBase< Gradient > *g=nullptr) const
Projects arbitrary functions onto the current solution.
Manages consistently variables, degrees of freedom, and coefficient vectors.
Definition: system.h:96
std::unique_ptr< NumericVector< Number > > solution
Data structure to hold solution values.
Definition: system.h:1593
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:1357
const DofMap & get_dof_map() const
Definition: system.h:2374
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 70 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().

71  {
72  LOG_UNIT_TEST;
73 
75 
77  4, 4,
78  0., 1.,
79  0., 1.,
80  QUAD4);
81 
82  // Set a subdomain id for all elements, based on location.
83  for (auto & elem : mesh.active_element_ptr_range())
84  {
85  Point c = elem->vertex_average();
86  elem->subdomain_id() =
87  subdomain_id_type(c(0)*4) + subdomain_id_type(c(1)*4)*10;
88  }
89 
90  // Add a discontinuous variable so we can easily see what side of
91  // an interface we're querying
93  System & sys = es.add_system<System> ("SimpleSystem");
94  unsigned int u_var = sys.add_variable("u", CONSTANT, MONOMIAL);
95 
96  es.init();
97  sys.project_solution(trilinear_function, nullptr, es.parameters);
98 
99  MeshFunction mesh_function (sys.get_equation_systems(),
101  sys.get_dof_map(),
102  u_var);
103 
104  // Checkerboard pattern
105  const std::set<subdomain_id_type> sbdids1 {0,2,11,13,20,22,31,33};
106  const std::set<subdomain_id_type> sbdids2 {1,3,10,12,21,23,30,32};
107  mesh_function.init();
108  mesh_function.enable_out_of_mesh_mode(DenseVector<Number>());
109  mesh_function.set_subdomain_ids(&sbdids1);
110 
111  DenseVector<Number> vec_values;
112  const std::string dummy;
113 
114  // Make sure the MeshFunction's values interpolate the projected solution
115  // at the nodes
116  for (auto & elem : mesh.active_local_element_ptr_range())
117  {
118  const Point c = elem->vertex_average();
119  const Real expected_value =
120  libmesh_real(trilinear_function(c, es.parameters, dummy, dummy));
121  const std::vector<Point> offsets
122  {{0,-1/8.}, {1/8.,0}, {0,1/8.}, {-1/8.,0}};
123  for (Point offset : offsets)
124  {
125  const Point p = c + offset;
126  mesh_function(p, 0, vec_values, &sbdids1);
127  const Number retval1 = vec_values.empty() ? -12345 : vec_values(0);
128  mesh_function(p, 0, vec_values, &sbdids2);
129  const Number retval2 = vec_values.empty() ? -12345 : vec_values(0);
130  mesh_function(c, 0, vec_values, nullptr);
131  const Number retval3 = vec_values.empty() ? -12345 : vec_values(0);
132 
133  LIBMESH_ASSERT_NUMBERS_EQUAL
134  (retval3, expected_value, TOLERANCE * TOLERANCE);
135 
136  if (sbdids1.count(elem->subdomain_id()))
137  {
138  CPPUNIT_ASSERT(!sbdids2.count(elem->subdomain_id()));
139  LIBMESH_ASSERT_NUMBERS_EQUAL
140  (retval1, expected_value, TOLERANCE * TOLERANCE);
141 
142  mesh_function(c, 0, vec_values, &sbdids2);
143  CPPUNIT_ASSERT(vec_values.empty());
144  }
145  else
146  {
147  LIBMESH_ASSERT_NUMBERS_EQUAL
148  (retval2, expected_value, TOLERANCE * TOLERANCE);
149 
150  mesh_function(c, 0, vec_values, &sbdids1);
151  CPPUNIT_ASSERT(vec_values.empty());
152  }
153  }
154  }
155  }
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:171
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:721
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.
void project_solution(FunctionBase< Number > *f, FunctionBase< Gradient > *g=nullptr) const
Projects arbitrary functions onto the current solution.
Manages consistently variables, degrees of freedom, and coefficient vectors.
Definition: system.h:96
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:1357
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:1605
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:2374
A Point defines a location in LIBMESH_DIM dimensional Real space.
Definition: point.h:39

◆ vectorMeshFunctionLagrange()

void MeshFunctionTest::vectorMeshFunctionLagrange ( )
inline

Definition at line 278 of file mesh_function.C.

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

279  {
280  LOG_UNIT_TEST;
281 
282  // Populating the solution data using a helper function
283  DenseVector<Number> output = read_variable_info_from_output_data("solutions/lagrange_vec_solution_mesh.xda","solutions/lagrange_vec_solution.xda");
284  Gradient output_vec = VectorValue(output(0),output(1));
285 
286  // Expected value at center mesh
287  Gradient output_expected = VectorValue(0.100977281077292,0.201954562154583);
288 
289  LIBMESH_ASSERT_FP_EQUAL(libMesh::libmesh_real(output_vec(0)), libMesh::libmesh_real(output_expected(0)),
290  TOLERANCE * TOLERANCE);
291  LIBMESH_ASSERT_FP_EQUAL(libMesh::libmesh_real(output_vec(1)), libMesh::libmesh_real(output_expected(1)),
292  TOLERANCE * TOLERANCE);
293  }
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 296 of file mesh_function.C.

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

297  {
298  LOG_UNIT_TEST;
299 
300  // Populating the solution data using a helper function
301  DenseVector<Number> output = read_variable_info_from_output_data("solutions/nedelec_one_solution_mesh.xda","solutions/nedelec_one_solution.xda");
302  Gradient output_vec = VectorValue(output(0),output(1));
303 
304  // Expected value at center mesh
305  Gradient output_expected = VectorValue(0.0949202883998996,-0.0949202883918033);
306 
307  LIBMESH_ASSERT_FP_EQUAL(libMesh::libmesh_real(output_vec(0)), libMesh::libmesh_real(output_expected(0)),
308  TOLERANCE * TOLERANCE);
309  LIBMESH_ASSERT_FP_EQUAL(libMesh::libmesh_real(output_vec(1)), libMesh::libmesh_real(output_expected(1)),
310  TOLERANCE * TOLERANCE);
311  }
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 314 of file mesh_function.C.

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

315  {
316  LOG_UNIT_TEST;
317 
318  // Populating the solution data using a helper function
319  DenseVector<Number> output = read_variable_info_from_output_data("solutions/raviart_thomas_solution_mesh.xda","solutions/raviart_thomas_solution.xda");
320  Gradient output_vec = VectorValue(output(0),output(1));
321 
322  // Expected value at center mesh
323  Gradient output_expected = VectorValue(0.0772539939808116,-0.0772537479511396);
324 
325  LIBMESH_ASSERT_FP_EQUAL(libMesh::libmesh_real(output_vec(0)), libMesh::libmesh_real(output_expected(0)),
326  TOLERANCE * TOLERANCE);
327  LIBMESH_ASSERT_FP_EQUAL(libMesh::libmesh_real(output_vec(1)), libMesh::libmesh_real(output_expected(1)),
328  TOLERANCE * TOLERANCE);
329  }
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: