libMesh
systems_test.C
Go to the documentation of this file.
1 // Ignore unused parameter warnings coming from cppunit headers
2 #include <libmesh/ignore_warnings.h>
3 #include <cppunit/extensions/HelperMacros.h>
4 #include <cppunit/TestCase.h>
5 #include <libmesh/restore_warnings.h>
6 
7 #include <libmesh/equation_systems.h>
8 #include <libmesh/mesh.h>
9 #include <libmesh/mesh_generation.h>
10 #include <libmesh/replicated_mesh.h>
11 #include <libmesh/mesh_function.h>
12 #include <libmesh/numeric_vector.h>
13 
14 #include "test_comm.h"
15 
16 // THE CPPUNIT_TEST_SUITE_END macro expands to code that involves
17 // std::auto_ptr, which in turn produces -Wdeprecated-declarations
18 // warnings. These can be ignored in GCC as long as we wrap the
19 // offending code in appropriate pragmas. We can't get away with a
20 // single ignore_warnings.h inclusion at the beginning of this file,
21 // since the libmesh headers pull in a restore_warnings.h at some
22 // point. We also don't bother restoring warnings at the end of this
23 // file since it's not a header.
24 #include <libmesh/ignore_warnings.h>
25 
26 using namespace libMesh;
27 
29  const Parameters&,
30  const std::string&,
31  const std::string&)
32 {
33  const Real & x = p(0);
34  const Real & y = p(1);
35  const Real & z = p(2);
36 
37  return x*(1-x)*(1-x) + x*x*(1-y) + x*(1-y)*(1-z) + y*(1-y)*z + z*(1-z)*(1-z);
38 }
39 
40 
41 class SystemsTest : public CppUnit::TestCase {
42 public:
43  CPPUNIT_TEST_SUITE( SystemsTest );
44 
45  CPPUNIT_TEST( testProjectHierarchicEdge3 );
46  CPPUNIT_TEST( testProjectHierarchicQuad9 );
47  CPPUNIT_TEST( testProjectHierarchicTri6 );
48  CPPUNIT_TEST( testProjectHierarchicHex27 );
49  CPPUNIT_TEST( testProjectMeshFunctionHex27 );
50 
51  CPPUNIT_TEST_SUITE_END();
52 
53 private:
54 
55 public:
56  void setUp()
57  {}
58 
59  void tearDown()
60  {}
61 
62 
63 
64  void testProjectLine(const ElemType elem_type)
65  {
67 
68  EquationSystems es(mesh);
69  System &sys = es.add_system<System> ("SimpleSystem");
70  sys.add_variable("u", THIRD, HIERARCHIC);
71 
73  3,
74  0., 1.,
75  elem_type);
76 
77  es.init();
78  sys.project_solution(cubic_test, NULL, es.parameters);
79 
80  for (Real x = 0.1; x < 1; x += 0.2)
81  {
82  Point p(x);
83  CPPUNIT_ASSERT_DOUBLES_EQUAL(libmesh_real(sys.point_value(0,p)),
84  libmesh_real(cubic_test(p,es.parameters,"","")),
86  }
87  }
88 
89 
90 
91  void testProjectSquare(const ElemType elem_type)
92  {
94 
95  EquationSystems es(mesh);
96  System &sys = es.add_system<System> ("SimpleSystem");
97  sys.add_variable("u", THIRD, HIERARCHIC);
98 
100  3, 3,
101  0., 1., 0., 1.,
102  elem_type);
103 
104  es.init();
105  sys.project_solution(cubic_test, NULL, es.parameters);
106 
107  for (Real x = 0.1; x < 1; x += 0.2)
108  for (Real y = 0.1; y < 1; y += 0.2)
109  {
110  Point p(x,y);
111  CPPUNIT_ASSERT_DOUBLES_EQUAL(libmesh_real(sys.point_value(0,p)),
112  libmesh_real(cubic_test(p,es.parameters,"","")),
114  }
115  }
116 
117 
118  void testProjectCube(const ElemType elem_type)
119  {
121 
122  EquationSystems es(mesh);
123  System &sys = es.add_system<System> ("SimpleSystem");
124  sys.add_variable("u", THIRD, HIERARCHIC);
125 
127  3, 3, 3,
128  0., 1., 0., 1., 0., 1.,
129  elem_type);
130 
131  es.init();
132  sys.project_solution(cubic_test, NULL, es.parameters);
133 
134  for (Real x = 0.1; x < 1; x += 0.2)
135  for (Real y = 0.1; y < 1; y += 0.2)
136  for (Real z = 0.1; z < 1; z += 0.2)
137  {
138  Point p(x,y,z);
139  CPPUNIT_ASSERT_DOUBLES_EQUAL(libmesh_real(sys.point_value(0,p)),
140  libmesh_real(cubic_test(p,es.parameters,"","")),
142  }
143  }
144 
146  {
147  // The source mesh needs to exist everywhere it's queried, so we
148  // use a ReplicatedMesh
150 
151  EquationSystems es(mesh);
152  System &sys = es.add_system<System> ("SimpleSystem");
153  sys.add_variable("u", THIRD, HIERARCHIC);
154 
156  3, 3, 3,
157  0., 1., 0., 1., 0., 1.,
158  elem_type);
159 
160  es.init();
161  sys.project_solution(cubic_test, NULL, es.parameters);
162 
163  std::vector<unsigned int> variables;
164  sys.get_all_variable_numbers(variables);
165  std::sort(variables.begin(),variables.end());
166 
167  UniquePtr<NumericVector<Number>> mesh_function_vector =
169  mesh_function_vector->init(sys.n_dofs(), false, SERIAL);
170  sys.solution->localize( *mesh_function_vector );
171 
172  MeshFunction mesh_function(es,
173  *mesh_function_vector,
174  sys.get_dof_map(),
175  variables);
176  mesh_function.init();
177 
178  // Make a second system and project onto it using a MeshFunction
179  Mesh proj_mesh(*TestCommWorld);
180  EquationSystems proj_es(proj_mesh);
181 
182  System &proj_sys = proj_es.add_system<System> ("ProjectionSystem");
183  proj_sys.add_variable("u", SECOND, LAGRANGE);
184 
186  5, 5, 5,
187  0., 1., 0., 1., 0., 1.,
188  elem_type);
189 
190  proj_es.init();
191  proj_sys.project_solution(&mesh_function);
192 
193  for (Real x = 0.1; x < 1; x += 0.2)
194  for (Real y = 0.1; y < 1; y += 0.2)
195  for (Real z = 0.1; z < 1; z += 0.2)
196  {
197  Point p(x,y,z);
198  CPPUNIT_ASSERT_DOUBLES_EQUAL(libmesh_real(proj_sys.point_value(0,p)),
199  libmesh_real(cubic_test(p,es.parameters,"","")),
201  }
202  }
203 
204  void testProjectHierarchicEdge3() { testProjectLine(EDGE3); }
205  void testProjectHierarchicQuad9() { testProjectSquare(QUAD9); }
206  void testProjectHierarchicTri6() { testProjectSquare(TRI6); }
207  void testProjectHierarchicHex27() { testProjectCube(HEX27); }
208  void testProjectMeshFunctionHex27() { testProjectCubeWithMeshFunction(HEX27); }
209 
210 };
211 
T libmesh_real(T a)
Number point_value(unsigned int var, const Point &p, const bool insist_on_success=true) const
Definition: system.C:2011
Number cubic_test(const Point &p, const Parameters &, const std::string &, const std::string &)
Definition: systems_test.C:28
This is the EquationSystems class.
The ReplicatedMesh class is derived from the MeshBase class, and is used to store identical copies of...
void testProjectHierarchicTri6()
Definition: systems_test.C:206
This class provides the ability to map between arbitrary, user-defined strings and several data types...
Definition: parameters.h:63
libMesh::Parallel::Communicator * TestCommWorld
Definition: driver.C:28
ImplicitSystem & sys
void setUp()
Definition: systems_test.C:56
ElemType
Defines an enum for geometric element types.
unsigned int add_variable(const std::string &var, const FEType &type, const std::set< subdomain_id_type > *const active_subdomains=libmesh_nullptr)
Adds the variable var to the list of variables for this system.
Definition: system.C:1101
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.
static const Real TOLERANCE
void project_solution(FunctionBase< Number > *f, FunctionBase< Gradient > *g=libmesh_nullptr) const
Projects arbitrary functions onto the current solution.
The libMesh namespace provides an interface to certain functionality in the library.
CPPUNIT_TEST_SUITE_REGISTRATION(SystemsTest)
std::unique_ptr< T > UniquePtr
Definition: auto_ptr.h:46
static UniquePtr< 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...
void tearDown()
Definition: systems_test.C:59
PetscErrorCode Vec x
const DofMap & get_dof_map() const
Definition: system.h:2030
This is the base class for classes which contain information related to any physical process that mig...
Definition: system.h:76
void testProjectHierarchicEdge3()
Definition: systems_test.C:204
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.
void testProjectSquare(const ElemType elem_type)
Definition: systems_test.C:91
UniquePtr< NumericVector< Number > > solution
Data structure to hold solution values.
Definition: system.h:1523
void testProjectCube(const ElemType elem_type)
Definition: systems_test.C:118
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
const Parallel::Communicator & comm() const
Parameters parameters
Data structure holding arbitrary parameters.
void build_line(UnstructuredMesh &mesh, const unsigned int nx, const Real xmin=0., const Real xmax=1., const ElemType type=INVALID_ELEM, const bool gauss_lobatto_grid=false)
A specialized build_cube() for 1D meshes.
dof_id_type n_dofs() const
Definition: system.C:148
virtual void init()
Initialize all the systems.
This class provides function-like objects for data distributed over a mesh.
Definition: mesh_function.h:53
void testProjectMeshFunctionHex27()
Definition: systems_test.C:208
The Mesh class is a thin wrapper, around the ReplicatedMesh class by default.
Definition: mesh.h:50
void testProjectHierarchicHex27()
Definition: systems_test.C:207
void testProjectLine(const ElemType elem_type)
Definition: systems_test.C:64
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:1278
void testProjectCubeWithMeshFunction(const ElemType elem_type)
Definition: systems_test.C:145
A Point defines a location in LIBMESH_DIM dimensional Real space.
Definition: point.h:38
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.
virtual void init() libmesh_override
Override the FunctionBase::init() member function by calling our own and specifying the Trees::NODES ...
Definition: mesh_function.h:96
void testProjectHierarchicQuad9()
Definition: systems_test.C:205