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

Public Member Functions

 LIBMESH_CPPUNIT_TEST_SUITE (ProjectSolutionTest)
 
 CPPUNIT_TEST (test_partial_project_solution)
 
 CPPUNIT_TEST_SUITE_END ()
 

Private Member Functions

void test_partial_project_solution ()
 

Detailed Description

Definition at line 42 of file project_solution_test.C.

Member Function Documentation

◆ CPPUNIT_TEST()

ProjectSolutionTest::CPPUNIT_TEST ( test_partial_project_solution  )

◆ CPPUNIT_TEST_SUITE_END()

ProjectSolutionTest::CPPUNIT_TEST_SUITE_END ( )

◆ LIBMESH_CPPUNIT_TEST_SUITE()

ProjectSolutionTest::LIBMESH_CPPUNIT_TEST_SUITE ( ProjectSolutionTest  )

◆ test_partial_project_solution()

void ProjectSolutionTest::test_partial_project_solution ( )
inlineprivate

Definition at line 50 of file project_solution_test.C.

References libMesh::EquationSystems::add_system(), libMesh::System::add_variable(), libMesh::MeshTools::Generation::build_square(), libMesh::CONSTANT, libMesh::DofMap::dof_indices(), libMesh::System::get_dof_map(), libMesh::EquationSystems::init(), mesh, libMesh::MONOMIAL, libMesh::EquationSystems::parameters, libMesh::System::project_solution(), libMesh::QUAD4, libMesh::Real, libMesh::System::solution, and TestCommWorld.

51  {
52  LOG_UNIT_TEST;
53 
54 #if LIBMESH_DIM < 2
55  return;
56 #else
57  ReplicatedMesh mesh(*TestCommWorld, /*dim=*/2);
58 
60  /*nx=*/5, /*ny=*/5,
61  /*xmin=*/-1., /*xmax=*/1.,
62  /*ymin=*/-1., /*ymax=*/1.,
63  QUAD4);
64 
66  System &sys = es.add_system<System>("ProjSys");
67 
68  // two dofs per element
69  const unsigned int u_var = sys.add_variable("u", CONSTANT, MONOMIAL);
70  const unsigned int v_var = sys.add_variable("v", CONSTANT, MONOMIAL);
71 
72  es.init();
73 
74  // baseline
75  sys.project_solution(baseline_linear_function, /*gptr=*/nullptr, es.parameters);
76  // ExodusII_IO(mesh).write_equation_systems("before_project.e", es);
77 
78  const DofMap &dof_map = sys.get_dof_map();
79 
80  // collect elements whose vertex_average lies inside the circle
81  const Real r2 = 0.5 * 0.5;
82 
83  std::vector<const Elem *> selected_elems;
84  for (const auto &e : mesh.active_local_element_ptr_range())
85  {
86  const Point c = e->vertex_average();
87  if (c(0) * c(0) + c(1) * c(1) <= r2)
88  selected_elems.push_back(e);
89  }
90 
91  // ConstElemRange expects mesh element iterators OR a vec_type*
92  // Here we use the vector-backed constructor.
93  ConstElemRange circle_range(&selected_elems);
94 
95  // project only on selected elements and only on u variable
96  std::vector<unsigned int> vars_to_project = {u_var};
97  sys.project_solution(circle_projection_function, /*gptr=*/nullptr, es.parameters, circle_range, vars_to_project);
98  // ExodusII_IO(mesh).write_equation_systems("after_project.e", es);
99 
100  // Check that restricted projection only overwrites elements inside the circle.
101  // Outside elements must keep the baseline projection.
102  const Real tol = 1e-3;
103 
104  for (const auto &e : mesh.active_local_element_ptr_range())
105  {
106  const Point c = e->vertex_average();
107  const bool inside = (c(0) * c(0) + c(1) * c(1) <= r2);
108 
109  std::vector<dof_id_type> u_indices, v_indices;
110  dof_map.dof_indices(e, u_indices, u_var);
111  dof_map.dof_indices(e, v_indices, v_var);
112 
113  const Number u_val = (*sys.solution)(u_indices[0]);
114  const Number v_val = (*sys.solution)(v_indices[0]);
115 
116  const Number val_baseline = baseline_linear_function(c, es.parameters, "", "");
117  const Number val_projected = circle_projection_function(c, es.parameters, "", "");
118  if (inside)
119  // Inside the circle, values should be equal to the projected value.
120  LIBMESH_ASSERT_NUMBERS_EQUAL(u_val, val_projected, tol);
121  else
122  // Outside the circle, values must remain unchanged.
123  LIBMESH_ASSERT_NUMBERS_EQUAL(u_val, val_baseline, tol);
124 
125  // v variable was never projected, should always equal baseline
126  LIBMESH_ASSERT_NUMBERS_EQUAL(v_val, val_baseline, tol);
127  }
128 #endif
129  }
This is the EquationSystems class.
The ReplicatedMesh class is derived from the MeshBase class, and is used to store identical copies of...
void dof_indices(const Elem *const elem, std::vector< dof_id_type > &di) const
Definition: dof_map.C:2201
libMesh::Parallel::Communicator * TestCommWorld
Definition: driver.C:218
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.
The StoredRange class defines a contiguous, divisible set of objects.
Definition: stored_range.h:54
This class handles the numbering of degrees of freedom on a mesh.
Definition: dof_map.h:179
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.
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
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

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