libMesh
Loading...
Searching...
No Matches
Functions
miscellaneous_ex9.C File Reference

Go to the source code of this file.

Functions

void assemble_poisson (EquationSystems &es, const ElementSideMap &lower_to_upper)
 Assemble the system matrix and rhs vector.
 
int main (int argc, char **argv)
 

Function Documentation

◆ assemble_poisson()

void assemble_poisson ( EquationSystems es,
const ElementSideMap lower_to_upper 
)

Assemble the system matrix and rhs vector.

Definition at line 202 of file miscellaneous_ex9.C.

204{
205 const MeshBase & mesh = es.get_mesh();
206 const unsigned int dim = mesh.mesh_dimension();
207
208 Real R = es.parameters.get<Real>("R");
209
210 LinearImplicitSystem & system = es.get_system<LinearImplicitSystem>("Poisson");
211
212 const DofMap & dof_map = system.get_dof_map();
213
214 FEType fe_type = dof_map.variable_type(0);
215
216 std::unique_ptr<FEBase> fe (FEBase::build(dim, fe_type));
217 std::unique_ptr<FEBase> fe_elem_face (FEBase::build(dim, fe_type));
218 std::unique_ptr<FEBase> fe_neighbor_face (FEBase::build(dim, fe_type));
219
220 QGauss qrule (dim, fe_type.default_quadrature_order());
221 QGauss qface(dim-1, fe_type.default_quadrature_order());
222
223 fe->attach_quadrature_rule (&qrule);
224 fe_elem_face->attach_quadrature_rule (&qface);
225 fe_neighbor_face->attach_quadrature_rule (&qface);
226
227 const std::vector<Real> & JxW = fe->get_JxW();
228 const std::vector<std::vector<Real>> & phi = fe->get_phi();
229 const std::vector<std::vector<RealGradient>> & dphi = fe->get_dphi();
230
231 const std::vector<Real> & JxW_face = fe_elem_face->get_JxW();
232
233 const std::vector<Point> & qface_points = fe_elem_face->get_xyz();
234
235 const std::vector<std::vector<Real>> & phi_face = fe_elem_face->get_phi();
236 const std::vector<std::vector<Real>> & phi_neighbor_face = fe_neighbor_face->get_phi();
237
240
245
246 std::vector<dof_id_type> dof_indices;
247 SparseMatrix<Number> & matrix = system.get_system_matrix();
248
249 for (const auto & elem : mesh.active_local_element_ptr_range())
250 {
251 dof_map.dof_indices (elem, dof_indices);
252 const unsigned int n_dofs = dof_indices.size();
253
254 fe->reinit (elem);
255
256 Ke.resize (n_dofs, n_dofs);
257 Fe.resize (n_dofs);
258
259 // Assemble element interior terms for the matrix
260 for (unsigned int qp=0; qp<qrule.n_points(); qp++)
261 for (unsigned int i=0; i<n_dofs; i++)
262 for (unsigned int j=0; j<n_dofs; j++)
263 Ke(i,j) += JxW[qp]*(dphi[i][qp]*dphi[j][qp]);
264
265 // Boundary flux provides forcing in this example
266 {
267 for (auto side : elem->side_index_range())
268 if (elem->neighbor_ptr(side) == nullptr)
269 {
270 if (mesh.get_boundary_info().has_boundary_id (elem, side, MIN_Z_BOUNDARY))
271 {
272 fe_elem_face->reinit(elem, side);
273
274 for (unsigned int qp=0; qp<qface.n_points(); qp++)
275 for (std::size_t i=0; i<phi.size(); i++)
276 Fe(i) += JxW_face[qp] * phi_face[i][qp];
277 }
278
279 }
280 }
281
282 // Add boundary terms on the crack
283 {
284 for (auto side : elem->side_index_range())
285 if (elem->neighbor_ptr(side) == nullptr)
286 {
287 // Found the lower side of the crack. Assemble terms due to lower and upper in here.
288 if (mesh.get_boundary_info().has_boundary_id (elem, side, CRACK_BOUNDARY_LOWER))
289 {
290 fe_elem_face->reinit(elem, side);
291
292 ElementSideMap::const_iterator ltu_it =
293 lower_to_upper.find(std::make_pair(elem, side));
294 libmesh_assert(ltu_it != lower_to_upper.end());
295
296 const Elem * neighbor = ltu_it->second;
297
298 std::vector<Point> qface_neighbor_points;
299 FEMap::inverse_map (elem->dim(), neighbor,
300 qface_points,
301 qface_neighbor_points);
302 fe_neighbor_face->reinit(neighbor, &qface_neighbor_points);
303
304 std::vector<dof_id_type> neighbor_dof_indices;
305 dof_map.dof_indices (neighbor, neighbor_dof_indices);
306 const unsigned int n_neighbor_dofs = neighbor_dof_indices.size();
307
308 Kne.resize (n_neighbor_dofs, n_dofs);
309 Ken.resize (n_dofs, n_neighbor_dofs);
310 Kee.resize (n_dofs, n_dofs);
311 Knn.resize (n_neighbor_dofs, n_neighbor_dofs);
312
313 // Lower-to-lower coupling term
314 for (unsigned int qp=0; qp<qface.n_points(); qp++)
315 for (unsigned int i=0; i<n_dofs; i++)
316 for (unsigned int j=0; j<n_dofs; j++)
317 Kee(i,j) -= JxW_face[qp] * (1./R)*(phi_face[i][qp] * phi_face[j][qp]);
318
319 // Lower-to-upper coupling term
320 for (unsigned int qp=0; qp<qface.n_points(); qp++)
321 for (unsigned int i=0; i<n_dofs; i++)
322 for (unsigned int j=0; j<n_neighbor_dofs; j++)
323 Ken(i,j) += JxW_face[qp] * (1./R)*(phi_face[i][qp] * phi_neighbor_face[j][qp]);
324
325 // Upper-to-upper coupling term
326 for (unsigned int qp=0; qp<qface.n_points(); qp++)
327 for (unsigned int i=0; i<n_neighbor_dofs; i++)
328 for (unsigned int j=0; j<n_neighbor_dofs; j++)
329 Knn(i,j) -= JxW_face[qp] * (1./R)*(phi_neighbor_face[i][qp] * phi_neighbor_face[j][qp]);
330
331 // Upper-to-lower coupling term
332 for (unsigned int qp=0; qp<qface.n_points(); qp++)
333 for (unsigned int i=0; i<n_neighbor_dofs; i++)
334 for (unsigned int j=0; j<n_dofs; j++)
335 Kne(i,j) += JxW_face[qp] * (1./R)*(phi_neighbor_face[i][qp] * phi_face[j][qp]);
336
337 matrix.add_matrix(Kne, neighbor_dof_indices, dof_indices);
338 matrix.add_matrix(Ken, dof_indices, neighbor_dof_indices);
339 matrix.add_matrix(Kee, dof_indices);
340 matrix.add_matrix(Knn, neighbor_dof_indices);
341 }
342 }
343 }
344
345 dof_map.constrain_element_matrix_and_vector (Ke, Fe, dof_indices);
346
347 matrix.add_matrix (Ke, dof_indices);
348 system.rhs->add_vector (Fe, dof_indices);
349 }
350}
unsigned int dim
bool has_boundary_id(const Node *const node, const boundary_id_type id) const
Defines a dense matrix for use in Finite Element-type computations.
void resize(const unsigned int new_m, const unsigned int new_n)
Resizes the matrix to the specified size and calls zero().
Defines a dense vector for use in Finite Element-type computations.
void resize(const unsigned int n)
Resize the vector.
This class handles the numbering of degrees of freedom on a mesh.
Definition dof_map.h:181
This is the base class from which all geometric element types are derived.
Definition elem.h:96
const MeshBase & get_mesh() const
Parameters parameters
Data structure holding arbitrary parameters.
const T_sys & get_system(std::string_view name) const
static std::unique_ptr< FEGenericBase > build(const unsigned int dim, const FEType &type)
Builds a specific finite element type.
static Point inverse_map(const unsigned int dim, const Elem *elem, const Point &p, const Real tolerance=TOLERANCE, const bool secure=true, const bool extra_checks=true)
Definition fe_map.C:1512
class FEType hides (possibly multiple) FEFamily and approximation orders, thereby enabling specialize...
Definition fe_type.h:197
Order default_quadrature_order() const
Definition fe_type.h:415
Manages consistently variables, degrees of freedom, coefficient vectors, matrices and linear solvers ...
This is the MeshBase class.
Definition mesh_base.h:81
const BoundaryInfo & get_boundary_info() const
The information about boundary ids on the mesh.
Definition mesh_base.h:170
unsigned int mesh_dimension() const
Definition mesh_base.C:430
const T & get(std::string_view) const
Definition parameters.h:451
This class implements specific orders of Gauss quadrature.
Generic sparse matrix.
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.
MeshBase & mesh
libmesh_assert(ctx)
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real

References libMesh::SparseMatrix< T >::add_matrix(), libMesh::FEGenericBase< OutputType >::build(), libMesh::FEType::default_quadrature_order(), dim, libMesh::Parameters::get(), libMesh::MeshBase::get_boundary_info(), libMesh::EquationSystems::get_mesh(), libMesh::EquationSystems::get_system(), libMesh::BoundaryInfo::has_boundary_id(), libMesh::FEMap::inverse_map(), libMesh::libmesh_assert(), mesh, libMesh::MeshBase::mesh_dimension(), libMesh::QBase::n_points(), libMesh::EquationSystems::parameters, libMesh::Real, libMesh::DenseVector< T >::resize(), and libMesh::DenseMatrix< T >::resize().

Referenced by main().

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 98 of file miscellaneous_ex9.C.

99{
100 // Initialize libMesh.
101 LibMeshInit init (argc, argv);
102
103 // This example uses an ExodusII input file
104#ifndef LIBMESH_HAVE_EXODUS_API
105 libmesh_example_requires(false, "--enable-exodus");
106#endif
107
108 // Skip this 3D example if libMesh was compiled as 1D or 2D-only.
109 libmesh_example_requires(3 <= LIBMESH_DIM, "3D support");
110
111 // We use Dirichlet boundary conditions here
112#ifndef LIBMESH_ENABLE_DIRICHLET
113 libmesh_example_requires(false, "--enable-dirichlet");
114#endif
115
116 const Real R = libMesh::command_line_next("-R", 2.);
117
118 Mesh mesh(init.comm());
119
120 EquationSystems equation_systems (mesh);
121
122 LinearImplicitSystem & system =
123 equation_systems.add_system<LinearImplicitSystem> ("Poisson");
124 system.add_variable("u", FIRST, LAGRANGE);
125
126 // We want to call assemble_poisson "manually" so that we can pass in
127 // lower_to_upper, hence set assemble_before_solve = false
128 system.assemble_before_solve = false;
129
130#ifdef LIBMESH_ENABLE_DIRICHLET
131 // Impose zero Dirichlet boundary condition on MAX_Z_BOUNDARY
133
134 // Most DirichletBoundary users will want to supply a "locally
135 // indexed" functor
136 DirichletBoundary dirichlet_bc({MAX_Z_BOUNDARY}, {0}, zf,
138 system.get_dof_map().add_dirichlet_boundary(dirichlet_bc);
139#endif // LIBMESH_ENABLE_DIRICHLET
140
141 // Attach an object to the DofMap that will augment the sparsity pattern
142 // due to the degrees-of-freedom on the "crack"
143 //
144 // By attaching this object *before* reading our mesh, we also
145 // ensure that the connected elements will not be deleted on a
146 // distributed mesh.
147 AugmentSparsityOnInterface augment_sparsity
148 (mesh, CRACK_BOUNDARY_LOWER, CRACK_BOUNDARY_UPPER);
149 system.get_dof_map().add_coupling_functor(augment_sparsity);
150
151 mesh.read("miscellaneous_ex9.exo");
152
153 equation_systems.init();
154 equation_systems.print_info();
155
156 // Set the jump term coefficient, it will be used in assemble_poisson
157 equation_systems.parameters.set<Real>("R") = R;
158
159 // Assemble and then solve
160 assemble_poisson(equation_systems,
161 augment_sparsity.get_lower_to_upper());
162 system.solve();
163
164#ifdef LIBMESH_HAVE_EXODUS_API
165 // Plot the solution
166 ExodusII_IO (mesh).write_equation_systems ("solution.exo",
167 equation_systems);
168#endif
169
170#ifdef LIBMESH_ENABLE_AMR
171 // Possibly solve on a refined mesh next.
172 MeshRefinement mesh_refinement (mesh);
173 unsigned int n_refinements =
174 libMesh::command_line_next("-n_refinements", 0);
175
176 for (unsigned int r = 0; r != n_refinements; ++r)
177 {
178 std::cout << "Refining the mesh" << std::endl;
179
180 mesh_refinement.uniformly_refine ();
181 equation_systems.reinit();
182
183 assemble_poisson(equation_systems,
184 augment_sparsity.get_lower_to_upper());
185 system.solve();
186
187#ifdef LIBMESH_HAVE_EXODUS_API
188 // Plot the refined solution
189 std::ostringstream out;
190 out << "solution_" << r << ".exo";
192 equation_systems);
193#endif
194
195 }
196
197#endif
198
199 return 0;
200}
This class allows one to associate Dirichlet boundary values with a given set of mesh boundary ids an...
void add_dirichlet_boundary(const DirichletBoundary &dirichlet_boundary)
Adds a copy of the specified Dirichlet boundary to the system.
void add_coupling_functor(GhostingFunctor &coupling_functor, bool to_mesh=true)
Adds a functor which can specify coupling requirements for creation of sparse matrices.
Definition dof_map.C:2005
This is the EquationSystems class.
The ExodusII_IO class implements reading meshes in the ExodusII file format from Sandia National Labs...
Definition exodusII_io.h:53
virtual void write_equation_systems(const std::string &fname, const EquationSystems &es, const std::set< std::string > *system_names=nullptr) override
Writes out the solution for no specific time or timestep.
The LibMeshInit class, when constructed, initializes the dependent libraries (e.g.
Definition libmesh.h:92
virtual void solve() override
Assembles & solves the linear system A*x=b.
virtual void read(const std::string &name, void *mesh_data=nullptr, bool skip_renumber_nodes_and_elements=false, bool skip_find_neighbors=false, bool skip_detect_interior_parents=false)=0
Interfaces for reading/writing a mesh to/from a file.
Implements (adaptive) mesh refinement algorithms for a MeshBase.
The Mesh class is a thin wrapper, around the ReplicatedMesh class by default.
Definition mesh.h:51
bool assemble_before_solve
Flag which tells the system to whether or not to call the user assembly function during each call to ...
Definition system.h:1609
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
const DofMap & get_dof_map() const
Definition system.h:2417
ConstFunction that simply returns 0.
void assemble_poisson(EquationSystems &es, const ElementSideMap &lower_to_upper)
Assemble the system matrix and rhs vector.
void init(triangulateio &t)
Initializes the fields of t to nullptr/0 as necessary.
OStreamProxy out
T command_line_next(std::string name, T default_value)
Use GetPot's search()/next() functions to get following arguments from the command line.
Definition libmesh.C:1025

References libMesh::DofMap::add_coupling_functor(), libMesh::DofMap::add_dirichlet_boundary(), libMesh::EquationSystems::add_system(), libMesh::System::add_variable(), libMesh::System::assemble_before_solve, assemble_poisson(), libMesh::command_line_next(), libMesh::FIRST, libMesh::System::get_dof_map(), AugmentSparsityOnInterface::get_lower_to_upper(), libMesh::EquationSystems::init(), libMesh::LAGRANGE, libMesh::LOCAL_VARIABLE_ORDER, main(), mesh, libMesh::out, libMesh::EquationSystems::parameters, libMesh::EquationSystems::print_info(), libMesh::MeshBase::read(), libMesh::Real, libMesh::EquationSystems::reinit(), libMesh::Parameters::set(), libMesh::LinearImplicitSystem::solve(), libMesh::MeshRefinement::uniformly_refine(), and libMesh::ExodusII_IO::write_equation_systems().