libMesh
Classes | Functions
systems_test.C File Reference

Go to the source code of this file.

Classes

class  AugmentSparsityOnNodes
 
struct  TripleFunction
 
class  SystemsTest
 

Functions

void assemble_matrix_and_rhs (EquationSystems &es, const std::string &)
 
void assembly_with_dg_fem_context (EquationSystems &es, const std::string &)
 
Number cubic_test (const Point &p, const Parameters &, const std::string &, const std::string &)
 
Number new_linear_test (const Point &p, const Parameters &, const std::string &, const std::string &)
 
Number disc_thirds_test (const Point &p, const Parameters &, const std::string &, const std::string &)
 
 CPPUNIT_TEST_SUITE_REGISTRATION (SystemsTest)
 

Function Documentation

◆ assemble_matrix_and_rhs()

void assemble_matrix_and_rhs ( EquationSystems es,
const std::string &   
)

Definition at line 128 of file systems_test.C.

References libMesh::SparseMatrix< T >::add_matrix(), libMesh::NumericVector< T >::add_vector(), libMesh::NumericVector< T >::close(), libMesh::SparseMatrix< T >::close(), libMesh::DofObject::dof_number(), libMesh::System::get_dof_map(), libMesh::EquationSystems::get_mesh(), libMesh::EquationSystems::get_system(), libMesh::ImplicitSystem::get_system_matrix(), mesh, libMesh::MeshBase::node_ref(), libMesh::NODEELEM, libMesh::System::number(), libMesh::DenseVector< T >::resize(), libMesh::DenseMatrix< T >::resize(), libMesh::ExplicitSystem::rhs, libMesh::Elem::type(), and libMesh::System::variable_number().

Referenced by ConstraintOperatorTest::test1DCoarseningNewNodes(), ConstraintOperatorTest::test1DCoarseningOperator(), ConstraintOperatorTest::testCoreform(), and SystemsTest::testDofCouplingWithVarGroups().

130 {
131  const MeshBase& mesh = es.get_mesh();
133  const DofMap& dof_map = system.get_dof_map();
134 
137 
138  std::vector<dof_id_type> dof_indices;
139 
140  SparseMatrix<Number> & matrix = system.get_system_matrix();
141 
142  MeshBase::const_element_iterator el = mesh.active_local_elements_begin();
143  const MeshBase::const_element_iterator end_el = mesh.active_local_elements_end();
144 
145  for ( ; el != end_el; ++el)
146  {
147  const Elem* elem = *el;
148 
149  if(elem->type() == NODEELEM)
150  {
151  continue;
152  }
153 
154  dof_map.dof_indices (elem, dof_indices);
155  const unsigned int n_dofs = dof_indices.size();
156 
157  Ke.resize (n_dofs, n_dofs);
158  Fe.resize (n_dofs);
159 
160  for(unsigned int i=0; i<n_dofs; i++)
161  {
162  Ke(i,i) = 1.;
163  Fe(i) = 1.;
164  }
165 
166  matrix.add_matrix (Ke, dof_indices);
167  system.rhs->add_vector (Fe, dof_indices);
168  }
169 
170  // Add matrix for extra coupled dofs
171  {
172  const Node & node_1 = mesh.node_ref(1);
173  const Node & node_2 = mesh.node_ref(2);
174  dof_indices.resize(6);
175  dof_indices[0] =
176  node_1.dof_number(system.number(), system.variable_number("u"), 0);
177  dof_indices[1] =
178  node_1.dof_number(system.number(), system.variable_number("v"), 0);
179  dof_indices[2] =
180  node_1.dof_number(system.number(), system.variable_number("w"), 0);
181 
182  dof_indices[3] =
183  node_2.dof_number(system.number(), system.variable_number("u"), 0);
184  dof_indices[4] =
185  node_2.dof_number(system.number(), system.variable_number("v"), 0);
186  dof_indices[5] =
187  node_2.dof_number(system.number(), system.variable_number("w"), 0);
188 
189  const unsigned int n_dofs = dof_indices.size();
190  Ke.resize (n_dofs, n_dofs);
191  Fe.resize (n_dofs);
192 
193  for(unsigned int i=0; i<n_dofs; i++)
194  {
195  Ke(i,i) = 1.;
196  Fe(i) = 1.;
197  }
198 
199  matrix.add_matrix (Ke, dof_indices);
200  system.rhs->add_vector (Fe, dof_indices);
201  }
202 
203  system.rhs->close();
204  matrix.close();
205 }
dof_id_type dof_number(const unsigned int s, const unsigned int var, const unsigned int comp) const
Definition: dof_object.h:1008
A Node is like a Point, but with more information.
Definition: node.h:52
The definition of the const_element_iterator struct.
Definition: mesh_base.h:2521
Manages consistently variables, degrees of freedom, coefficient vectors, matrices and linear solvers ...
void resize(const unsigned int n)
Resize the vector.
Definition: dense_vector.h:396
virtual void add_vector(const T *v, const std::vector< numeric_index_type > &dof_indices)
Computes , where v is a pointer and each dof_indices[i] specifies where to add value v[i]...
This is the base class from which all geometric element types are derived.
Definition: elem.h:94
MeshBase & mesh
NumericVector< Number > * rhs
The system matrix.
const T_sys & get_system(std::string_view name) const
This is the MeshBase class.
Definition: mesh_base.h:80
unsigned int variable_number(std::string_view var) const
Definition: system.C:1398
This class handles the numbering of degrees of freedom on a mesh.
Definition: dof_map.h:179
unsigned int number() const
Definition: system.h:2393
virtual void add_matrix(const DenseMatrix< T > &dm, const std::vector< numeric_index_type > &rows, const std::vector< numeric_index_type > &cols)=0
Add the full matrix dm to the SparseMatrix.
virtual void close()=0
Calls the NumericVector&#39;s internal assembly routines, ensuring that the values are consistent across ...
virtual void close()=0
Calls the SparseMatrix&#39;s internal assembly routines, ensuring that the values are consistent across p...
const MeshBase & get_mesh() const
void resize(const unsigned int new_m, const unsigned int new_n)
Resizes the matrix to the specified size and calls zero().
Definition: dense_matrix.h:895
virtual const Node & node_ref(const dof_id_type i) const
Definition: mesh_base.h:735
const DofMap & get_dof_map() const
Definition: system.h:2417
virtual ElemType type() const =0
const SparseMatrix< Number > & get_system_matrix() const

◆ assembly_with_dg_fem_context()

void assembly_with_dg_fem_context ( EquationSystems es,
const std::string &   
)

Definition at line 208 of file systems_test.C.

References libMesh::SparseMatrix< T >::add_matrix(), libMesh::NumericVector< T >::add_vector(), libMesh::FEMContext::elem_fe_reinit(), libMesh::DiffContext::get_dof_indices(), libMesh::FEGenericBase< OutputType >::get_dphi(), libMesh::DGFEMContext::get_elem_elem_jacobian(), libMesh::DiffContext::get_elem_jacobian(), libMesh::DGFEMContext::get_elem_neighbor_jacobian(), libMesh::DiffContext::get_elem_residual(), libMesh::FEMContext::get_element_fe(), libMesh::FEMContext::get_element_qrule(), libMesh::FEAbstract::get_JxW(), libMesh::EquationSystems::get_mesh(), libMesh::DGFEMContext::get_neighbor_dof_indices(), libMesh::DGFEMContext::get_neighbor_elem_jacobian(), libMesh::DGFEMContext::get_neighbor_neighbor_jacobian(), libMesh::DGFEMContext::get_neighbor_side_fe(), libMesh::FEGenericBase< OutputType >::get_phi(), libMesh::FEMContext::get_side(), libMesh::FEMContext::get_side_fe(), libMesh::FEMContext::get_side_qrule(), libMesh::EquationSystems::get_system(), libMesh::ImplicitSystem::get_system_matrix(), mesh, libMesh::QBase::n_points(), libMesh::Elem::neighbor_ptr(), libMesh::DGFEMContext::neighbor_side_fe_reinit(), libMesh::FEMContext::pre_fe_reinit(), libMesh::ExplicitSystem::rhs, libMesh::DGFEMContext::set_neighbor(), libMesh::FEMContext::side, and libMesh::DGFEMContext::side_fe_reinit().

Referenced by SystemsTest::testAssemblyWithDgFemContext(), and SystemsTest::testSetSystemParameterOverEquationSystem().

210 {
211  const MeshBase& mesh = es.get_mesh();
213 
216 
217  std::vector<dof_id_type> dof_indices;
218  SparseMatrix<Number> & matrix = system.get_system_matrix();
219 
220  DGFEMContext context(system);
221  {
222  // For efficiency, we should prerequest all
223  // the data we will need to build the
224  // linear system before doing an element loop.
225  FEBase* elem_fe = NULL;
226  context.get_element_fe(0, elem_fe);
227  elem_fe->get_JxW();
228  elem_fe->get_phi();
229  elem_fe->get_dphi();
230 
231  FEBase* side_fe = NULL;
232  context.get_side_fe( 0, side_fe );
233  side_fe->get_JxW();
234  side_fe->get_phi();
235 
236  FEBase* neighbor_side_fe = NULL;
237  context.get_neighbor_side_fe(0, neighbor_side_fe);
238  neighbor_side_fe->get_phi();
239  }
240 
241  for (const auto & elem : mesh.active_local_element_ptr_range())
242  {
243  context.pre_fe_reinit(system, elem);
244  context.elem_fe_reinit();
245 
246  // Element interior assembly
247  {
248  FEBase* elem_fe = NULL;
249  context.get_element_fe(0, elem_fe);
250 
251  const std::vector<Real> &JxW = elem_fe->get_JxW();
252  const std::vector<std::vector<Real> >& phi = elem_fe->get_phi();
253  const std::vector<std::vector<RealGradient> >& dphi = elem_fe->get_dphi();
254 
255  unsigned int n_dofs = context.get_dof_indices(0).size();
256  unsigned int n_qpoints = context.get_element_qrule().n_points();
257 
258  for (unsigned int qp=0; qp != n_qpoints; qp++)
259  for (unsigned int i=0; i != n_dofs; i++)
260  for (unsigned int j=0; j != n_dofs; j++)
261  context.get_elem_jacobian()(i,j) += JxW[qp] * dphi[i][qp]*dphi[j][qp];
262 
263  for (unsigned int qp=0; qp != n_qpoints; qp++)
264  for (unsigned int i=0; i != n_dofs; i++)
265  context.get_elem_residual()(i) += JxW[qp] * phi[i][qp];
266  }
267 
268  matrix.add_matrix (context.get_elem_jacobian(), context.get_dof_indices());
269  system.rhs->add_vector (context.get_elem_residual(), context.get_dof_indices());
270 
271  // Element side assembly
272  for (context.side = 0; context.side != elem->n_sides(); ++context.side)
273  {
274  // If there is a neighbor, then we proceed with assembly
275  // that involves elem and neighbor
276  const Elem* neighbor = elem->neighbor_ptr(context.get_side());
277  if(neighbor)
278  {
279  context.side_fe_reinit();
280  context.set_neighbor(*neighbor);
281 
282  // This call initializes neighbor data, and also sets
283  // context.dg_terms_are_active() to true
284  context.neighbor_side_fe_reinit();
285 
286  FEBase* side_fe = NULL;
287  context.get_side_fe(0, side_fe);
288 
289  const std::vector<Real> &JxW_face = side_fe->get_JxW();
290  const std::vector<std::vector<Real> >& phi_face = side_fe->get_phi();
291 
292  FEBase* neighbor_side_fe = NULL;
293  context.get_neighbor_side_fe(0, neighbor_side_fe);
294 
295  // These shape functions have been evaluated on the quadrature points
296  // for elem->side on the neighbor element
297  const std::vector<std::vector<Real> >& phi_neighbor_face =
298  neighbor_side_fe->get_phi();
299 
300  const unsigned int n_dofs = context.get_dof_indices(0).size();
301  const unsigned int n_neighbor_dofs = context.get_neighbor_dof_indices(0).size();
302  unsigned int n_sidepoints = context.get_side_qrule().n_points();
303 
304  for (unsigned int qp=0; qp<n_sidepoints; qp++)
305  {
306  for (unsigned int i=0; i<n_dofs; i++)
307  for (unsigned int j=0; j<n_dofs; j++)
308  {
309  context.get_elem_elem_jacobian()(i,j) +=
310  JxW_face[qp] * phi_face[i][qp] * phi_face[j][qp];
311  }
312 
313  for (unsigned int i=0; i<n_dofs; i++)
314  for (unsigned int j=0; j<n_neighbor_dofs; j++)
315  {
316  context.get_elem_neighbor_jacobian()(i,j) +=
317  JxW_face[qp] * phi_face[i][qp] * phi_neighbor_face[j][qp];
318  }
319 
320  for (unsigned int i=0; i<n_neighbor_dofs; i++)
321  for (unsigned int j=0; j<n_neighbor_dofs; j++)
322  {
323  context.get_neighbor_neighbor_jacobian()(i,j) +=
324  JxW_face[qp] * phi_neighbor_face[i][qp] * phi_neighbor_face[j][qp];
325  }
326 
327  for (unsigned int i=0; i<n_neighbor_dofs; i++)
328  for (unsigned int j=0; j<n_dofs; j++)
329  {
330  context.get_neighbor_elem_jacobian()(i,j) +=
331  JxW_face[qp] * phi_neighbor_face[i][qp] * phi_face[j][qp];
332  }
333  }
334 
335  matrix.add_matrix (context.get_elem_elem_jacobian(),
336  context.get_dof_indices(),
337  context.get_dof_indices());
338 
339  matrix.add_matrix (context.get_elem_neighbor_jacobian(),
340  context.get_dof_indices(),
341  context.get_neighbor_dof_indices());
342 
343  matrix.add_matrix (context.get_neighbor_elem_jacobian(),
344  context.get_neighbor_dof_indices(),
345  context.get_dof_indices());
346 
347  matrix.add_matrix (context.get_neighbor_neighbor_jacobian(),
348  context.get_neighbor_dof_indices(),
349  context.get_neighbor_dof_indices());
350  }
351  }
352  }
353 }
Manages consistently variables, degrees of freedom, coefficient vectors, matrices and linear solvers ...
virtual void add_vector(const T *v, const std::vector< numeric_index_type > &dof_indices)
Computes , where v is a pointer and each dof_indices[i] specifies where to add value v[i]...
This is the base class from which all geometric element types are derived.
Definition: elem.h:94
MeshBase & mesh
NumericVector< Number > * rhs
The system matrix.
const T_sys & get_system(std::string_view name) const
This is the MeshBase class.
Definition: mesh_base.h:80
const std::vector< std::vector< OutputGradient > > & get_dphi() const
Definition: fe_base.h:230
virtual void add_matrix(const DenseMatrix< T > &dm, const std::vector< numeric_index_type > &rows, const std::vector< numeric_index_type > &cols)=0
Add the full matrix dm to the SparseMatrix.
This class extends FEMContext in order to provide extra data required to perform local element residu...
virtual_for_inffe const std::vector< Real > & get_JxW() const
Definition: fe_abstract.h:303
void set_neighbor(const unsigned int i, Elem *n)
Assigns n as the neighbor.
Definition: elem.h:2632
const Elem * neighbor_ptr(unsigned int i) const
Definition: elem.h:2612
const MeshBase & get_mesh() const
const SparseMatrix< Number > & get_system_matrix() const
This class forms the foundation from which generic finite elements may be derived.
const std::vector< std::vector< OutputShape > > & get_phi() const
Definition: fe_base.h:207

◆ CPPUNIT_TEST_SUITE_REGISTRATION()

CPPUNIT_TEST_SUITE_REGISTRATION ( SystemsTest  )

◆ cubic_test()

Number cubic_test ( const Point p,
const Parameters ,
const std::string &  ,
const std::string &   
)

Definition at line 356 of file systems_test.C.

References libMesh::Real.

Referenced by TripleFunction::component(), TripleFunction::operator()(), SystemsTest::testProjectCubeWithMeshFunction(), and SystemsTest::tripleValueTest().

360 {
361  const Real & x = p(0);
362  const Real & y = LIBMESH_DIM > 1 ? p(1) : 0;
363  const Real & z = LIBMESH_DIM > 2 ? p(2) : 0;
364 
365  return x*(1-x)*(1-x) + x*x*(1-y) + x*(1-y)*(1-z) + y*(1-y)*z + z*(1-z)*(1-z);
366 }
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real

◆ disc_thirds_test()

Number disc_thirds_test ( const Point p,
const Parameters ,
const std::string &  ,
const std::string &   
)

Definition at line 382 of file systems_test.C.

References libMesh::Real.

Referenced by TripleFunction::component(), TripleFunction::operator()(), and SystemsTest::tripleValueTest().

386 {
387  const Real & x = p(0);
388  const Real & y = LIBMESH_DIM > 1 ? p(1) : 0;
389  const Real & z = LIBMESH_DIM > 2 ? p(2) : 0;
390 
391  return (3*x < 1) + (3*y < 2) + (3*z > 2);
392 }
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real

◆ new_linear_test()

Number new_linear_test ( const Point p,
const Parameters ,
const std::string &  ,
const std::string &   
)

Definition at line 369 of file systems_test.C.

References libMesh::Real.

Referenced by TripleFunction::component(), TripleFunction::operator()(), and SystemsTest::tripleValueTest().

373 {
374  const Real & x = p(0);
375  const Real & y = LIBMESH_DIM > 1 ? p(1) : 0;
376  const Real & z = LIBMESH_DIM > 2 ? p(2) : 0;
377 
378  return x + 2*y + 3*z - 1;
379 }
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real